libcbor 0.1.0.pre.alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.travis.yml +16 -0
- data/Gemfile +4 -0
- data/README.md +39 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/examples/data/floats.cbor +0 -0
- data/examples/data/indef_string.cbor +1 -0
- data/examples/data/integer.cbor +1 -0
- data/examples/data/map.cbor +1 -0
- data/examples/data/nested_array.cbor +1 -0
- data/examples/data/tagged_date.cbor +1 -0
- data/examples/load_file.rb +11 -0
- data/lib/libcbor.rb +45 -0
- data/lib/libcbor/cache.rb +26 -0
- data/lib/libcbor/cbor_item.rb +64 -0
- data/lib/libcbor/helpers.rb +82 -0
- data/lib/libcbor/inner.rb +28 -0
- data/lib/libcbor/inner/lib_c.rb +11 -0
- data/lib/libcbor/inner/lib_cbor.rb +135 -0
- data/lib/libcbor/inner/prealloc.rb +10 -0
- data/lib/libcbor/streaming/buffered_decoder.rb +51 -0
- data/lib/libcbor/streaming/callback_simplifier.rb +55 -0
- data/lib/libcbor/streaming/encoder.rb +34 -0
- data/lib/libcbor/tag.rb +4 -0
- data/lib/libcbor/version.rb +3 -0
- data/libcbor.gemspec +28 -0
- metadata +144 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 72e98b82d68080ed07cff8821dc4a1416bc3a9f4
|
4
|
+
data.tar.gz: 99c9cfbfd0952ce27307a538c2983f2e8a0831f0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d7e39fb6fa4221c4bc220612b6b1b65e9f572e19723dfb83dcff4d51d3caa75c86dd667dc9570b506cf386115280a1ce7154c313a8f67aaf37d74cdde468b622
|
7
|
+
data.tar.gz: 5f03642c8f5505fa28a2a950e4706b59333e840cf3fa274511de0aef460fecc3e2f120b4bce89af9183e151ed628384bbbc5ba487d8e0c5611d36bfca37f290c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- jruby
|
4
|
+
- rbx-2.1.1
|
5
|
+
- 2.0.0
|
6
|
+
- 2.1.1
|
7
|
+
- 2.2.1
|
8
|
+
script: bundle exec rspec spec
|
9
|
+
before_install:
|
10
|
+
- wget https://github.com/PJK/libcbor/archive/master.zip
|
11
|
+
- unzip master.zip
|
12
|
+
- cd libcbor-master
|
13
|
+
- cmake .
|
14
|
+
- make cbor cbor_shared
|
15
|
+
- sudo make install
|
16
|
+
- cd ..
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# libcbor
|
2
|
+
|
3
|
+
The Ruby bindings for [libcbor](https://github.com/PJK/libcbor)
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Make sure that `libffi-dev` is present on your system.
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'libcbor'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install libcbor
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it ( https://github.com/[my-github-username]/libcbor/fork )
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "libcbor"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
Binary file
|
@@ -0,0 +1 @@
|
|
1
|
+
eHellofworld!�
|
@@ -0,0 +1 @@
|
|
1
|
+
*
|
@@ -0,0 +1 @@
|
|
1
|
+
�dcbor#)djson�gbaaaaad
|
@@ -0,0 +1 @@
|
|
1
|
+
����
|
@@ -0,0 +1 @@
|
|
1
|
+
�t2013-03-21T20:04:00Z
|
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# Load all CBOR files passed as arguments and prints the result.
|
4
|
+
# Example usage:
|
5
|
+
# $ ./examples/load_file.rb examples/data/*
|
6
|
+
|
7
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..'))
|
8
|
+
require 'lib/libcbor'
|
9
|
+
require 'pp'
|
10
|
+
|
11
|
+
ARGV.each { |_| PP.pp CBOR.decode(IO.read(_)) }
|
data/lib/libcbor.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'ffi'
|
5
|
+
|
6
|
+
require 'libcbor/version'
|
7
|
+
require 'libcbor/inner/lib_cbor'
|
8
|
+
require 'libcbor/inner/lib_c'
|
9
|
+
require 'libcbor/cache'
|
10
|
+
require 'libcbor/tag'
|
11
|
+
require 'libcbor/helpers'
|
12
|
+
require 'libcbor/cbor_item'
|
13
|
+
require 'libcbor/streaming/callback_simplifier'
|
14
|
+
require 'libcbor/streaming/buffered_decoder'
|
15
|
+
require 'libcbor/streaming/encoder'
|
16
|
+
|
17
|
+
module CBOR
|
18
|
+
class DecodingError < StandardError; end
|
19
|
+
|
20
|
+
def self.encode(obj)
|
21
|
+
obj.to_cbor
|
22
|
+
end
|
23
|
+
|
24
|
+
def method_name
|
25
|
+
@@method_name
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.load!(name = nil)
|
29
|
+
@@method_name = name
|
30
|
+
%w{Fixnum Float Array Hash String TrueClass FalseClass NilClass Tag}.each do |klass|
|
31
|
+
const_get(klass).send(:include, const_get('::CBOR::' + klass + 'Helper'))
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.decode(data)
|
36
|
+
res = FFI::MemoryPointer.new LibCBOR::CborLoadResult
|
37
|
+
CBORItem.new(
|
38
|
+
LibCBOR.cbor_load(FFI::MemoryPointer.from_string(data), data.bytes.count, res).
|
39
|
+
tap { |ptr| raise DecodingError if ptr.null? }
|
40
|
+
).value
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module CBOR
|
2
|
+
# Provides caching for simple values (true, false, nil)
|
3
|
+
class Cache
|
4
|
+
@@bfr = FFI::Buffer.new(:uchar, 1)
|
5
|
+
|
6
|
+
def self.get_bool(val)
|
7
|
+
@@bfr.get_bytes(0, LibCBOR.cbor_encode_bool(val, @@bfr, 1))
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.get_null
|
11
|
+
@@bfr.get_bytes(0, LibCBOR.cbor_encode_null(@@bfr, 1))
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.true
|
15
|
+
@@true ||= get_bool(true)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.false
|
19
|
+
@@false ||= get_bool(false)
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.nil
|
23
|
+
@@null ||= get_null
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module CBOR
|
2
|
+
# Wraps native `cbor_item_t *`
|
3
|
+
class CBORItem < Struct.new(:handle)
|
4
|
+
def type
|
5
|
+
LibCBOR.cbor_typeof(handle)
|
6
|
+
end
|
7
|
+
|
8
|
+
def value
|
9
|
+
case type
|
10
|
+
when :uint
|
11
|
+
LibCBOR.cbor_get_int(handle)
|
12
|
+
when :negint
|
13
|
+
-LibCBOR.cbor_get_int(handle) - 1
|
14
|
+
when :string
|
15
|
+
LibCBOR
|
16
|
+
.cbor_string_handle(handle)
|
17
|
+
.get_string(0, LibCBOR.cbor_string_length(handle))
|
18
|
+
when :bytestring
|
19
|
+
LibCBOR
|
20
|
+
.cbor_bytestring_handle(handle)
|
21
|
+
.get_string(0, LibCBOR.cbor_bytestring_length(handle))
|
22
|
+
when :array
|
23
|
+
LibCBOR
|
24
|
+
.cbor_array_handle(handle)
|
25
|
+
.read_array_of_type(LibCBOR::CborItemTRef, :read_pointer, LibCBOR.cbor_array_size(handle))
|
26
|
+
.map { |item| CBORItem.new(item).value }
|
27
|
+
when :map
|
28
|
+
pairs_handle = LibCBOR.cbor_map_handle(handle)
|
29
|
+
Hash[LibCBOR.cbor_map_size(handle).times.map { |idx|
|
30
|
+
pair = LibCBOR::CborPair.new(pairs_handle + LibCBOR::CborPair.size * idx)
|
31
|
+
[pair[:key], pair[:value]].map { |ptr| CBORItem.new(ptr).value }
|
32
|
+
}]
|
33
|
+
when :tag
|
34
|
+
Tag.new(
|
35
|
+
LibCBOR.cbor_tag_value(handle),
|
36
|
+
CBORItem.new(LibCBOR.cbor_tag_item(handle)).value
|
37
|
+
)
|
38
|
+
when :float_ctrl
|
39
|
+
load_float
|
40
|
+
else
|
41
|
+
raise 'Unknown type - the FFI enum mapping is probably broken. Please report this bug.'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
protected
|
46
|
+
|
47
|
+
def load_float
|
48
|
+
if LibCBOR.cbor_float_ctrl_is_ctrl(handle)
|
49
|
+
case ctr_val = LibCBOR.cbor_ctrl_value(handle)
|
50
|
+
when 20
|
51
|
+
false
|
52
|
+
when 21
|
53
|
+
true
|
54
|
+
when 22
|
55
|
+
nil
|
56
|
+
else
|
57
|
+
ctr_val
|
58
|
+
end
|
59
|
+
else
|
60
|
+
LibCBOR.cbor_float_get_float(handle)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module CBOR
|
2
|
+
module FixnumHelper
|
3
|
+
def to_cbor
|
4
|
+
@@bfr ||= FFI::Buffer.new(:uchar, 9)
|
5
|
+
if self >= 0
|
6
|
+
@@bfr.get_bytes(0, LibCBOR.cbor_encode_uint(self, @@bfr, 9))
|
7
|
+
else
|
8
|
+
@@bfr.get_bytes(0, LibCBOR.cbor_encode_negint(-self - 1, @@bfr, 9))
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module FloatHelper
|
14
|
+
def to_cbor
|
15
|
+
@@bfr ||= FFI::Buffer.new(:uchar, 9)
|
16
|
+
@@bfr.get_bytes(0, LibCBOR.cbor_encode_single(self, @@bfr, 9))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module StringHelper
|
21
|
+
def to_cbor
|
22
|
+
@@item ||= LibCBOR.cbor_new_definite_string
|
23
|
+
string = FFI::MemoryPointer.from_string(self)
|
24
|
+
out_bfr = FFI::MemoryPointer.new :pointer
|
25
|
+
out_bfr_len = FFI::MemoryPointer.new :size_t
|
26
|
+
LibCBOR.cbor_string_set_handle(@@item, string, bytes.length)
|
27
|
+
res_len = LibCBOR.cbor_serialize_alloc(@@item, out_bfr, out_bfr_len)
|
28
|
+
out_bfr.read_pointer.get_bytes(0, res_len).tap do
|
29
|
+
LibC.free(out_bfr.read_pointer)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
module ArrayHelper
|
35
|
+
def to_cbor
|
36
|
+
@@bfr ||= FFI::Buffer.new(:uchar, 9)
|
37
|
+
header = @@bfr.get_bytes(0, LibCBOR.cbor_encode_array_start(count, @@bfr, 9))
|
38
|
+
each do |member|
|
39
|
+
header += member.to_cbor
|
40
|
+
end
|
41
|
+
header
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
module HashHelper
|
46
|
+
def to_cbor
|
47
|
+
@@bfr ||= FFI::Buffer.new(:uchar, 9)
|
48
|
+
header = @@bfr.get_bytes(0, LibCBOR.cbor_encode_map_start(count, @@bfr, 9))
|
49
|
+
each do |member|
|
50
|
+
header += member.first.to_cbor
|
51
|
+
header += member.last.to_cbor
|
52
|
+
end
|
53
|
+
header
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
module TagHelper
|
58
|
+
def to_cbor
|
59
|
+
@@bfr ||= FFI::Buffer.new(:uchar, 9)
|
60
|
+
header = @@bfr.get_bytes(0, LibCBOR.cbor_encode_tag(value, @@bfr, 9))
|
61
|
+
header + item.to_cbor
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
module TrueClassHelper
|
66
|
+
def to_cbor
|
67
|
+
Cache.true
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
module FalseClassHelper
|
72
|
+
def to_cbor
|
73
|
+
Cache.false
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
module NilClassHelper
|
78
|
+
def to_cbor
|
79
|
+
Cache.nil
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'ffi'
|
2
|
+
|
3
|
+
module LibC
|
4
|
+
ffi_lib FFI::Library::LIBC
|
5
|
+
extend FFI::Library
|
6
|
+
|
7
|
+
# memory allocators
|
8
|
+
attach_function :malloc, [:size_t], :pointer
|
9
|
+
attach_function :calloc, [:size_t], :pointer
|
10
|
+
attach_function :valloc, [:size_t], :pointer
|
11
|
+
attach_function :realloc, [:pointer, :size_t], :pointer
|
12
|
+
attach_function :free, [:pointer], :void
|
13
|
+
|
14
|
+
# memory movers
|
15
|
+
attach_function :memcpy, [:pointer, :pointer, :size_t], :pointer
|
16
|
+
attach_function :bcopy, [:pointer, :pointer, :size_t], :void
|
17
|
+
|
18
|
+
end # module LibC
|
19
|
+
|
20
|
+
module Libcbor
|
21
|
+
extend FFI::Library
|
22
|
+
ffi_lib 'cbor'
|
23
|
+
attach_function :cbor_new_int8, [], :pointer
|
24
|
+
attach_function :cbor_set_uint16, [:pointer, :ushort], :void
|
25
|
+
attach_function :cbor_set_uint8, [:pointer, :uchar], :void
|
26
|
+
attach_function :cbor_serialize, [:pointer, :pointer, :size_t], :size_t
|
27
|
+
puts 'yo'
|
28
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# Provides
|
2
|
+
module CBOR::LibC
|
3
|
+
extend FFI::Library
|
4
|
+
ffi_lib FFI::Library::LIBC
|
5
|
+
|
6
|
+
attach_function :malloc, [:size_t], :pointer
|
7
|
+
attach_function :free, [:pointer], :void
|
8
|
+
|
9
|
+
attach_function :memcpy, [:pointer, :pointer, :size_t], :pointer
|
10
|
+
attach_function :bcopy, [:pointer, :pointer, :size_t], :void
|
11
|
+
end
|
@@ -0,0 +1,135 @@
|
|
1
|
+
module CBOR
|
2
|
+
module LibCBOR
|
3
|
+
extend FFI::Library
|
4
|
+
ffi_lib 'cbor'
|
5
|
+
|
6
|
+
CborItemTRef = typedef :pointer, :cbor_item_t_ref
|
7
|
+
typedef :pointer, :cbor_item_t_ref_array
|
8
|
+
typedef :pointer, :buffer
|
9
|
+
typedef :pointer, :memblock # unsigned char * - string & bytestring handles, raw blocks
|
10
|
+
|
11
|
+
ErrorCode = enum(:none, :notenoughdata, :nodata, :malformated, :memerror, :syntaxerror)
|
12
|
+
|
13
|
+
class CborError < FFI::Struct
|
14
|
+
layout :position, :size_t,
|
15
|
+
:code, ErrorCode
|
16
|
+
end
|
17
|
+
|
18
|
+
class CborLoadResult < FFI::Struct
|
19
|
+
layout :error, CborError,
|
20
|
+
:read, :size_t
|
21
|
+
end
|
22
|
+
|
23
|
+
class CborPair < FFI::Struct
|
24
|
+
layout :key, CborItemTRef,
|
25
|
+
:value, CborItemTRef
|
26
|
+
end
|
27
|
+
|
28
|
+
typedef :pointer, :cbor_pair_array
|
29
|
+
|
30
|
+
typedef :pointer, :cbor_load_result_ref
|
31
|
+
|
32
|
+
attach_function :cbor_decref, [:pointer], :void
|
33
|
+
attach_function :cbor_serialize, [:pointer, :pointer, :size_t], :size_t
|
34
|
+
attach_function :cbor_encode_uint, [:uint64, :pointer, :size_t], :size_t
|
35
|
+
attach_function :cbor_encode_negint, [:uint64, :pointer, :size_t], :size_t
|
36
|
+
attach_function :cbor_encode_single, [:float, :pointer, :size_t], :size_t
|
37
|
+
attach_function :cbor_encode_array_start, [:size_t, :pointer, :size_t], :size_t
|
38
|
+
attach_function :cbor_encode_map_start, [:size_t, :pointer, :size_t], :size_t
|
39
|
+
attach_function :cbor_encode_tag, [:uint64, :pointer, :size_t], :size_t
|
40
|
+
attach_function :cbor_encode_bool, [:bool, :pointer, :size_t], :size_t
|
41
|
+
attach_function :cbor_encode_null, [:pointer, :size_t], :size_t
|
42
|
+
attach_function :cbor_encode_undef, [:pointer, :size_t], :size_t
|
43
|
+
# size_t cbor_serialize_alloc(const cbor_item_t * item, unsigned char ** buffer, size_t * buffer_size);
|
44
|
+
attach_function :cbor_serialize_alloc, [:pointer, :pointer, :pointer], :size_t
|
45
|
+
|
46
|
+
attach_function :cbor_new_definite_string, [], :pointer
|
47
|
+
# void cbor_string_set_handle(cbor_item_t *item, unsigned char *data, size_t length);
|
48
|
+
attach_function :cbor_string_set_handle, [:pointer, :pointer, :size_t], :void
|
49
|
+
|
50
|
+
Type = enum(:uint, :negint, :bytestring, :string, :array, :map, :tag, :float_ctrl)
|
51
|
+
|
52
|
+
attach_function :cbor_typeof, [:cbor_item_t_ref], Type
|
53
|
+
attach_function :cbor_load, [:buffer, :size_t, :cbor_load_result_ref], :cbor_item_t_ref
|
54
|
+
|
55
|
+
attach_function :cbor_get_int, [:cbor_item_t_ref], :uint64
|
56
|
+
|
57
|
+
attach_function :cbor_string_length, [:cbor_item_t_ref], :size_t
|
58
|
+
attach_function :cbor_string_handle, [:cbor_item_t_ref], :memblock
|
59
|
+
|
60
|
+
attach_function :cbor_bytestring_length, [:cbor_item_t_ref], :size_t
|
61
|
+
attach_function :cbor_bytestring_handle, [:cbor_item_t_ref], :memblock
|
62
|
+
|
63
|
+
attach_function :cbor_array_size, [:cbor_item_t_ref], :size_t
|
64
|
+
attach_function :cbor_array_handle, [:cbor_item_t_ref], :cbor_item_t_ref_array
|
65
|
+
|
66
|
+
attach_function :cbor_map_size, [:cbor_item_t_ref], :size_t
|
67
|
+
attach_function :cbor_map_handle, [:cbor_item_t_ref], :cbor_pair_array
|
68
|
+
|
69
|
+
attach_function :cbor_tag_value, [:cbor_item_t_ref], :uint64
|
70
|
+
attach_function :cbor_tag_item, [:cbor_item_t_ref], :cbor_item_t_ref
|
71
|
+
|
72
|
+
attach_function :cbor_float_ctrl_is_ctrl, [:cbor_item_t_ref], :bool
|
73
|
+
attach_function :cbor_ctrl_value, [:cbor_item_t_ref], :uint8
|
74
|
+
attach_function :cbor_float_get_float, [:cbor_item_t_ref], :double
|
75
|
+
|
76
|
+
callback :cbor_int8_callback, [:pointer, :uint8], :void
|
77
|
+
callback :cbor_int16_callback, [:pointer, :uint16], :void
|
78
|
+
callback :cbor_int32_callback, [:pointer, :uint32], :void
|
79
|
+
callback :cbor_int64_callback, [:pointer, :uint64], :void
|
80
|
+
callback :cbor_simple_callback, [:pointer], :void
|
81
|
+
callback :cbor_string_callback, [:pointer, :pointer, :size_t], :void
|
82
|
+
callback :cbor_collection_callback, [:pointer, :size_t], :void
|
83
|
+
callback :cbor_float_callback, [:pointer, :float], :void
|
84
|
+
callback :cbor_double_callback, [:pointer, :double], :void
|
85
|
+
callback :cbor_bool_callback, [:pointer, :bool], :void
|
86
|
+
|
87
|
+
class CborCallbacks < FFI::Struct
|
88
|
+
layout :uint8, :cbor_int8_callback,
|
89
|
+
:uint16, :cbor_int16_callback,
|
90
|
+
:uint32, :cbor_int32_callback,
|
91
|
+
:uint64, :cbor_int64_callback,
|
92
|
+
|
93
|
+
:negint8, :cbor_int8_callback,
|
94
|
+
:negint16, :cbor_int16_callback,
|
95
|
+
:negint32, :cbor_int32_callback,
|
96
|
+
:negint64, :cbor_int64_callback,
|
97
|
+
|
98
|
+
:byte_string, :cbor_string_callback,
|
99
|
+
:byte_string_start, :cbor_simple_callback,
|
100
|
+
|
101
|
+
:string, :cbor_string_callback,
|
102
|
+
:string_start, :cbor_simple_callback,
|
103
|
+
|
104
|
+
:array_start, :cbor_collection_callback,
|
105
|
+
:indef_array_start, :cbor_simple_callback,
|
106
|
+
|
107
|
+
:map_start, :cbor_collection_callback,
|
108
|
+
:indef_map_start, :cbor_simple_callback,
|
109
|
+
|
110
|
+
:tag, :cbor_int64_callback,
|
111
|
+
|
112
|
+
:float2, :cbor_float_callback,
|
113
|
+
:float4, :cbor_float_callback,
|
114
|
+
:float8, :cbor_double_callback,
|
115
|
+
|
116
|
+
:undefined, :cbor_simple_callback,
|
117
|
+
:null, :cbor_simple_callback,
|
118
|
+
:boolean, :cbor_bool_callback,
|
119
|
+
|
120
|
+
:indef_break, :cbor_simple_callback
|
121
|
+
end
|
122
|
+
|
123
|
+
DecoderStatus = enum(:finished, :not_enough_data, :buffer_error, :error)
|
124
|
+
|
125
|
+
class CborDecoderResult < FFI::Struct
|
126
|
+
layout :read, :size_t,
|
127
|
+
:status, DecoderStatus
|
128
|
+
end
|
129
|
+
|
130
|
+
# buffer, buffer size, callbacks, context
|
131
|
+
attach_function :cbor_stream_decode, [:pointer, :size_t, :pointer, :pointer], CborDecoderResult.by_value
|
132
|
+
|
133
|
+
attach_function :cbor_encode_tag, [:uint64, :memblock, :size_t], :size_t
|
134
|
+
end
|
135
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module CBOR
|
2
|
+
module Streaming
|
3
|
+
class BufferedDecoder
|
4
|
+
def initialize(callbacks = {})
|
5
|
+
@callbacks = {
|
6
|
+
integer: Proc.new {},
|
7
|
+
string: Proc.new {},
|
8
|
+
chunked_string_start: Proc.new {},
|
9
|
+
byte_string: Proc.new {},
|
10
|
+
chunked_byte_string_start: Proc.new {},
|
11
|
+
float: Proc.new {},
|
12
|
+
definite_array: Proc.new {},
|
13
|
+
array_start: Proc.new {},
|
14
|
+
definite_map: Proc.new {},
|
15
|
+
map_start: Proc.new {},
|
16
|
+
tag: Proc.new {},
|
17
|
+
bool: Proc.new {},
|
18
|
+
null: Proc.new {},
|
19
|
+
simple: Proc.new {},
|
20
|
+
break: Proc.new {},
|
21
|
+
}.merge(callbacks)
|
22
|
+
@buffer = ''
|
23
|
+
@proxy = CallbackSimplifier.new(self)
|
24
|
+
end
|
25
|
+
|
26
|
+
def <<(data)
|
27
|
+
@buffer += data
|
28
|
+
loop do
|
29
|
+
read = LibCBOR.cbor_stream_decode(
|
30
|
+
FFI::MemoryPointer.from_string(@buffer),
|
31
|
+
@buffer.bytes.length,
|
32
|
+
@proxy.callback_set.to_ptr,
|
33
|
+
nil
|
34
|
+
)[:read]
|
35
|
+
break if read == 0
|
36
|
+
@buffer = @buffer[read .. -1]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def callback(name, *args)
|
41
|
+
callbacks[name].call(*args)
|
42
|
+
end
|
43
|
+
|
44
|
+
attr_reader :buffer
|
45
|
+
|
46
|
+
protected
|
47
|
+
|
48
|
+
attr_reader :callbacks
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module CBOR
|
2
|
+
module Streaming
|
3
|
+
# Abstracts aways callback specifics, such as integer width
|
4
|
+
class CallbackSimplifier < Struct.new(:target)
|
5
|
+
def callback_set
|
6
|
+
@cset ||= build_callback_set
|
7
|
+
end
|
8
|
+
|
9
|
+
protected
|
10
|
+
|
11
|
+
def build_callback_set
|
12
|
+
callbacks = LibCBOR::CborCallbacks.new
|
13
|
+
callbacks[:uint8] = Proc.new { |_ctx, val| target.callback(:integer, val) }
|
14
|
+
callbacks[:uint16] = callbacks[:uint8]
|
15
|
+
callbacks[:uint32] = callbacks[:uint8]
|
16
|
+
callbacks[:uint64] = callbacks[:uint8]
|
17
|
+
|
18
|
+
callbacks[:negint8] = Proc.new { |_ctx, val| target.callback(:integer, -val - 1) }
|
19
|
+
callbacks[:negint16] = callbacks[:negint8]
|
20
|
+
callbacks[:negint32] = callbacks[:negint8]
|
21
|
+
callbacks[:negint64] = callbacks[:negint8]
|
22
|
+
|
23
|
+
callbacks[:byte_string] = Proc.new { |_ctx, ptr, size|
|
24
|
+
target.callback(:byte_string, ptr.get_string(0, size))
|
25
|
+
}
|
26
|
+
callbacks[:byte_string_start] = Proc.new { |_ctx| target.callback(:chunked_byte_string_start) }
|
27
|
+
|
28
|
+
callbacks[:string] = Proc.new { |_ctx, ptr, size|
|
29
|
+
target.callback(:string, ptr.get_string(0, size))
|
30
|
+
}
|
31
|
+
callbacks[:string_start] = Proc.new { |_ctx| target.callback(:chunked_string_start) }
|
32
|
+
|
33
|
+
callbacks[:array_start] = Proc.new { |_ctx, size| target.callback(:definite_array, size) }
|
34
|
+
callbacks[:indef_array_start] = Proc.new { |_ctx| target.callback(:array_start) }
|
35
|
+
|
36
|
+
callbacks[:map_start] = Proc.new { |_ctx, size| target.callback(:definite_map, size) }
|
37
|
+
callbacks[:indef_map_start] = Proc.new { |_ctx| target.callback(:map_start) }
|
38
|
+
|
39
|
+
callbacks[:tag] = Proc.new { |_ctx, val| target.callback(:tag, val) }
|
40
|
+
|
41
|
+
callbacks[:float2] = Proc.new { |_ctx, val| target.callback(:float, val) }
|
42
|
+
callbacks[:float4] = callbacks[:float2]
|
43
|
+
callbacks[:float8] = callbacks[:float2]
|
44
|
+
|
45
|
+
callbacks[:undefined] = Proc.new { |_ctx| target.callback(:simple, 23) }
|
46
|
+
callbacks[:null] = Proc.new { |_ctx| target.callback(:null) }
|
47
|
+
callbacks[:boolean] = Proc.new { |_ctx, val| target.callback(:bool, val) }
|
48
|
+
|
49
|
+
callbacks[:indef_break] = Proc.new { |_ctx| target.callback(:break) }
|
50
|
+
|
51
|
+
callbacks
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module CBOR
|
2
|
+
module Streaming
|
3
|
+
class Encoder < Struct.new(:target)
|
4
|
+
def <<(object)
|
5
|
+
target.write(object.to_cbor)
|
6
|
+
end
|
7
|
+
|
8
|
+
def start_array
|
9
|
+
target.write("\x9f")
|
10
|
+
end
|
11
|
+
|
12
|
+
def start_map
|
13
|
+
target.write("\xbf")
|
14
|
+
end
|
15
|
+
|
16
|
+
def start_chunked_string
|
17
|
+
target.write("\x9f")
|
18
|
+
end
|
19
|
+
|
20
|
+
def start_chunked_byte_string
|
21
|
+
target.write("\x5f")
|
22
|
+
end
|
23
|
+
|
24
|
+
def tag(value)
|
25
|
+
@@bfr ||= FFI::Buffer.new(:uchar, 9)
|
26
|
+
@@bfr.get_bytes(0, LibCBOR.cbor_encode_tag(value, @@bfr, 9))
|
27
|
+
end
|
28
|
+
|
29
|
+
def break
|
30
|
+
target.write("\xff")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/libcbor/tag.rb
ADDED
data/libcbor.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'libcbor/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "libcbor"
|
8
|
+
spec.version = CBOR::VERSION
|
9
|
+
spec.authors = ["PJK"]
|
10
|
+
spec.email = ["me@pavelkalvoda.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A Ruby binding for libcbor}
|
13
|
+
spec.description = %q{CBOR (Concise Binary Object Representation) implementation based on the libcbor C library. Provides all the encoding and decoding facilities of libcbor, including the streaming interface}
|
14
|
+
spec.homepage = "https://github.com/PJK/libcbor-ruby"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "> 1.8"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "> 3"
|
25
|
+
spec.add_development_dependency "pry"
|
26
|
+
|
27
|
+
spec.add_runtime_dependency "ffi"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: libcbor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0.pre.alpha
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- PJK
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>'
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.8'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>'
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>'
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>'
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: ffi
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: CBOR (Concise Binary Object Representation) implementation based on the
|
84
|
+
libcbor C library. Provides all the encoding and decoding facilities of libcbor,
|
85
|
+
including the streaming interface
|
86
|
+
email:
|
87
|
+
- me@pavelkalvoda.com
|
88
|
+
executables: []
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- .gitignore
|
93
|
+
- .rspec
|
94
|
+
- .travis.yml
|
95
|
+
- Gemfile
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- bin/console
|
99
|
+
- bin/setup
|
100
|
+
- examples/data/floats.cbor
|
101
|
+
- examples/data/indef_string.cbor
|
102
|
+
- examples/data/integer.cbor
|
103
|
+
- examples/data/map.cbor
|
104
|
+
- examples/data/nested_array.cbor
|
105
|
+
- examples/data/tagged_date.cbor
|
106
|
+
- examples/load_file.rb
|
107
|
+
- lib/libcbor.rb
|
108
|
+
- lib/libcbor/cache.rb
|
109
|
+
- lib/libcbor/cbor_item.rb
|
110
|
+
- lib/libcbor/helpers.rb
|
111
|
+
- lib/libcbor/inner.rb
|
112
|
+
- lib/libcbor/inner/lib_c.rb
|
113
|
+
- lib/libcbor/inner/lib_cbor.rb
|
114
|
+
- lib/libcbor/inner/prealloc.rb
|
115
|
+
- lib/libcbor/streaming/buffered_decoder.rb
|
116
|
+
- lib/libcbor/streaming/callback_simplifier.rb
|
117
|
+
- lib/libcbor/streaming/encoder.rb
|
118
|
+
- lib/libcbor/tag.rb
|
119
|
+
- lib/libcbor/version.rb
|
120
|
+
- libcbor.gemspec
|
121
|
+
homepage: https://github.com/PJK/libcbor-ruby
|
122
|
+
licenses: []
|
123
|
+
metadata: {}
|
124
|
+
post_install_message:
|
125
|
+
rdoc_options: []
|
126
|
+
require_paths:
|
127
|
+
- lib
|
128
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - '>='
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - '>'
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: 1.3.1
|
138
|
+
requirements: []
|
139
|
+
rubyforge_project:
|
140
|
+
rubygems_version: 2.4.6
|
141
|
+
signing_key:
|
142
|
+
specification_version: 4
|
143
|
+
summary: A Ruby binding for libcbor
|
144
|
+
test_files: []
|