cbor 0.5.6.2
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 +22 -0
- data/.travis.yml +5 -0
- data/ChangeLog +87 -0
- data/README.rdoc +180 -0
- data/Rakefile +94 -0
- data/cbor.gemspec +26 -0
- data/doclib/cbor.rb +80 -0
- data/doclib/cbor/buffer.rb +193 -0
- data/doclib/cbor/core_ext.rb +133 -0
- data/doclib/cbor/error.rb +14 -0
- data/doclib/cbor/packer.rb +133 -0
- data/doclib/cbor/simple.rb +15 -0
- data/doclib/cbor/tagged.rb +16 -0
- data/doclib/cbor/unpacker.rb +138 -0
- data/ext/cbor/buffer.c +693 -0
- data/ext/cbor/buffer.h +469 -0
- data/ext/cbor/buffer_class.c +516 -0
- data/ext/cbor/buffer_class.h +41 -0
- data/ext/cbor/cbor.h +69 -0
- data/ext/cbor/compat.h +136 -0
- data/ext/cbor/core_ext.c +181 -0
- data/ext/cbor/core_ext.h +35 -0
- data/ext/cbor/extconf.rb +25 -0
- data/ext/cbor/packer.c +169 -0
- data/ext/cbor/packer.h +337 -0
- data/ext/cbor/packer_class.c +304 -0
- data/ext/cbor/packer_class.h +39 -0
- data/ext/cbor/rbinit.c +51 -0
- data/ext/cbor/renamer.h +56 -0
- data/ext/cbor/rmem.c +103 -0
- data/ext/cbor/rmem.h +118 -0
- data/ext/cbor/sysdep.h +135 -0
- data/ext/cbor/sysdep_endian.h +59 -0
- data/ext/cbor/sysdep_types.h +55 -0
- data/ext/cbor/unpacker.c +735 -0
- data/ext/cbor/unpacker.h +133 -0
- data/ext/cbor/unpacker_class.c +417 -0
- data/ext/cbor/unpacker_class.h +39 -0
- data/lib/cbor.rb +9 -0
- data/lib/cbor/version.rb +3 -0
- data/spec/buffer_io_spec.rb +260 -0
- data/spec/buffer_spec.rb +576 -0
- data/spec/cases.cbor +0 -0
- data/spec/cases.cbor_stream +0 -0
- data/spec/cases.json +1 -0
- data/spec/cases.msg +0 -0
- data/spec/cases_compact.msg +0 -0
- data/spec/cases_spec.rb +39 -0
- data/spec/format_spec.rb +445 -0
- data/spec/packer_spec.rb +127 -0
- data/spec/random_compat.rb +24 -0
- data/spec/spec_helper.rb +45 -0
- data/spec/unpacker_spec.rb +238 -0
- metadata +196 -0
@@ -0,0 +1,193 @@
|
|
1
|
+
module CBOR
|
2
|
+
|
3
|
+
class Buffer
|
4
|
+
#
|
5
|
+
# Creates a CBOR::Buffer instance.
|
6
|
+
#
|
7
|
+
# @overload initialize(options={})
|
8
|
+
# @param options [Hash]
|
9
|
+
#
|
10
|
+
# @overload initialize(io, options={})
|
11
|
+
# @param io [IO]
|
12
|
+
# @param options [Hash]
|
13
|
+
# This buffer writes written data into the IO when it is filled.
|
14
|
+
# This buffer reads data from the IO when it is empty.
|
15
|
+
#
|
16
|
+
# _io_ must respond to readpartial(length, [,string]) or read(string) method and
|
17
|
+
# write(string) or append(string) method.
|
18
|
+
#
|
19
|
+
# Supported options:
|
20
|
+
#
|
21
|
+
# * *:io_buffer_size* buffer size to read data from the internal IO. (default: 32768)
|
22
|
+
# * *:read_reference_threshold* the threshold size to enable zero-copy deserialize optimization. Read strings longer than this threshold will refer the original string instead of copying it. (default: 256) (supported in MRI only)
|
23
|
+
# * *:write_reference_threshold* the threshold size to enable zero-copy serialize optimization. The buffer refers written strings longer than this threshold instead of copying it. (default: 524288) (supported in MRI only)
|
24
|
+
#
|
25
|
+
def initialize(*args)
|
26
|
+
end
|
27
|
+
|
28
|
+
#
|
29
|
+
# Makes the buffer empty
|
30
|
+
#
|
31
|
+
# @return nil
|
32
|
+
#
|
33
|
+
def clear
|
34
|
+
end
|
35
|
+
|
36
|
+
#
|
37
|
+
# Returns byte size of the buffer.
|
38
|
+
#
|
39
|
+
# @return nil
|
40
|
+
#
|
41
|
+
def size
|
42
|
+
end
|
43
|
+
|
44
|
+
#
|
45
|
+
# Returns _true_ if the buffer is empty.
|
46
|
+
# This method is slightly faster than _size_.
|
47
|
+
#
|
48
|
+
# @return [Boolean]
|
49
|
+
#
|
50
|
+
def empty?
|
51
|
+
end
|
52
|
+
|
53
|
+
#
|
54
|
+
# Appends the given data to the buffer.
|
55
|
+
#
|
56
|
+
# @param data [String]
|
57
|
+
# @return [Integer] byte size written
|
58
|
+
#
|
59
|
+
def write(data)
|
60
|
+
end
|
61
|
+
|
62
|
+
#
|
63
|
+
# Appends the given data to the buffer.
|
64
|
+
#
|
65
|
+
# @param data [String]
|
66
|
+
# @return [Buffer] self
|
67
|
+
#
|
68
|
+
def <<(data)
|
69
|
+
end
|
70
|
+
|
71
|
+
#
|
72
|
+
# Consumes _n_ bytes from the head of the buffer and returns consumed data.
|
73
|
+
# If the size of the buffer is less than _n_, it reads all of data in the buffer.
|
74
|
+
#
|
75
|
+
# If _n_ is 0, it does nothing and returns an empty string.
|
76
|
+
# If the optional _buffer_ argument is given, the content of the string will be replaced with the consumed data.
|
77
|
+
#
|
78
|
+
# @overload read
|
79
|
+
#
|
80
|
+
# @overload read(n)
|
81
|
+
# @param n [Integer] bytes to read
|
82
|
+
#
|
83
|
+
# @overload read(n, buffer)
|
84
|
+
# @param n [Integer] bytes to read
|
85
|
+
# @param buffer [String] buffer to read into
|
86
|
+
#
|
87
|
+
# @return [String]
|
88
|
+
#
|
89
|
+
def read(n)
|
90
|
+
end
|
91
|
+
|
92
|
+
#
|
93
|
+
# Consumes _n_ bytes from the head of the buffer and returns consumed data.
|
94
|
+
# If the size of the buffer is less than _n_, it does nothing and raises EOFError.
|
95
|
+
#
|
96
|
+
# If _n_ is 0, it does nothing and returns an empty string.
|
97
|
+
# If the optional _buffer_ argument is given, the content of the string will be replaced with the consumed data.
|
98
|
+
#
|
99
|
+
# @overload read_all
|
100
|
+
#
|
101
|
+
# @overload read_all(n)
|
102
|
+
# @param n [Integer] bytes to read
|
103
|
+
#
|
104
|
+
# @overload read_all(n, buffer)
|
105
|
+
# @param n [Integer] bytes to read
|
106
|
+
# @param buffer [String] buffer to read into
|
107
|
+
#
|
108
|
+
# @return [String]
|
109
|
+
#
|
110
|
+
def read_all(n, buffer=nil)
|
111
|
+
end
|
112
|
+
|
113
|
+
#
|
114
|
+
# Consumes _n_ bytes from the head of the buffer.
|
115
|
+
# If the size of the buffer is less than _n_, it skips all of data in the buffer and returns integer less than _n_.
|
116
|
+
#
|
117
|
+
# If _n_ is 0, it does nothing and returns _0_.
|
118
|
+
#
|
119
|
+
# @param n [Integer] byte size to skip
|
120
|
+
# @return [Integer] byte size actually skipped
|
121
|
+
#
|
122
|
+
def skip(n)
|
123
|
+
end
|
124
|
+
|
125
|
+
#
|
126
|
+
# Consumes _n_ bytes from the head of the buffer.
|
127
|
+
# If the size of the buffer is less than _n_, it does nothing and raises EOFError.
|
128
|
+
# If _n_ is 0, it does nothing.
|
129
|
+
#
|
130
|
+
# @param n [Integer] byte size to skip
|
131
|
+
# @return [Buffer] self
|
132
|
+
#
|
133
|
+
def skip_all(n)
|
134
|
+
end
|
135
|
+
|
136
|
+
#
|
137
|
+
# Returns all data in the buffer as a string.
|
138
|
+
# Destructive update to the returned string does NOT effect the buffer.
|
139
|
+
#
|
140
|
+
# @return [String]
|
141
|
+
#
|
142
|
+
def to_str
|
143
|
+
end
|
144
|
+
|
145
|
+
#
|
146
|
+
# Returns content of the buffer as an array of strings.
|
147
|
+
#
|
148
|
+
# This method is sometimes faster than to_s because the internal
|
149
|
+
# structure of the buffer is a queue of buffer chunks.
|
150
|
+
#
|
151
|
+
# @return [Array] array of strings
|
152
|
+
#
|
153
|
+
def to_a
|
154
|
+
end
|
155
|
+
|
156
|
+
#
|
157
|
+
# Internal io
|
158
|
+
#
|
159
|
+
# @return IO
|
160
|
+
#
|
161
|
+
attr_reader :io
|
162
|
+
|
163
|
+
#
|
164
|
+
# Flushes data in the internal buffer to the internal IO.
|
165
|
+
# If internal IO is not set, it does nothing.
|
166
|
+
#
|
167
|
+
# @return [Buffer] self
|
168
|
+
#
|
169
|
+
def flush
|
170
|
+
end
|
171
|
+
|
172
|
+
#
|
173
|
+
# Closes internal IO if its set.
|
174
|
+
# If internal IO is not set, it does nothing
|
175
|
+
#
|
176
|
+
# @return nil
|
177
|
+
#
|
178
|
+
def close
|
179
|
+
end
|
180
|
+
|
181
|
+
#
|
182
|
+
# Writes all of data in the internal buffer into the given IO.
|
183
|
+
# This method consumes and removes data from the internal buffer.
|
184
|
+
# _io_ must respond to write(data) method.
|
185
|
+
#
|
186
|
+
# @param io [IO]
|
187
|
+
# @return [Integer] byte size of written data
|
188
|
+
#
|
189
|
+
def write_to(io)
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
|
2
|
+
class NilClass
|
3
|
+
#
|
4
|
+
# Same as CBOR.encode(self[, io]).
|
5
|
+
#
|
6
|
+
# @return [String] serialized data
|
7
|
+
#
|
8
|
+
def to_cbor(io=nil)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class TrueClass
|
13
|
+
#
|
14
|
+
# Same as CBOR.encode(self[, io]).
|
15
|
+
#
|
16
|
+
# @return [String] serialized data
|
17
|
+
#
|
18
|
+
def to_cbor(io=nil)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class FalseClass
|
23
|
+
#
|
24
|
+
# Same as CBOR.encode(self[, io]).
|
25
|
+
#
|
26
|
+
# @return [String] serialized data
|
27
|
+
#
|
28
|
+
def to_cbor(io=nil)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class Fixnum < Integer
|
33
|
+
#
|
34
|
+
# Same as CBOR.encode(self[, io]).
|
35
|
+
#
|
36
|
+
# @return [String] serialized data
|
37
|
+
#
|
38
|
+
def to_cbor(io=nil)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class Bignum < Integer
|
43
|
+
#
|
44
|
+
# Same as CBOR.encode(self[, io]).
|
45
|
+
#
|
46
|
+
# @return [String] serialized data
|
47
|
+
#
|
48
|
+
def to_cbor(io=nil)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class Float < Numeric
|
53
|
+
#
|
54
|
+
# Same as CBOR.encode(self[, io]).
|
55
|
+
#
|
56
|
+
# @return [String] serialized data
|
57
|
+
#
|
58
|
+
def to_cbor(io=nil)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
class String
|
63
|
+
#
|
64
|
+
# Same as CBOR.encode(self[, io]).
|
65
|
+
#
|
66
|
+
# @return [String] serialized data
|
67
|
+
#
|
68
|
+
def to_cbor(io=nil)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
class Array
|
73
|
+
#
|
74
|
+
# Same as CBOR.encode(self[, io]).
|
75
|
+
#
|
76
|
+
# @return [String] serialized data
|
77
|
+
#
|
78
|
+
def to_cbor(io=nil)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
class Hash
|
83
|
+
#
|
84
|
+
# Same as CBOR.encode(self[, io]).
|
85
|
+
#
|
86
|
+
# @return [String] serialized data
|
87
|
+
#
|
88
|
+
def to_cbor(io=nil)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
class Time
|
93
|
+
#
|
94
|
+
# Same as CBOR.encode(self[, io]).
|
95
|
+
#
|
96
|
+
# @return [String] serialized data
|
97
|
+
#
|
98
|
+
def to_cbor(io=nil)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
class Regexp
|
104
|
+
#
|
105
|
+
# Same as CBOR.encode(self[, io]).
|
106
|
+
#
|
107
|
+
# @return [String] serialized data
|
108
|
+
#
|
109
|
+
def to_cbor(io=nil)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
class URI
|
114
|
+
#
|
115
|
+
# Same as CBOR.encode(self[, io]). This method is only defined if
|
116
|
+
# the class URI was defined when the CBOR extension was loaded, so
|
117
|
+
# +require "uri"+ before +require "cbor"+ if you need this.
|
118
|
+
#
|
119
|
+
# @return [String] serialized data
|
120
|
+
#
|
121
|
+
def to_cbor(io=nil)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
class Symbol
|
126
|
+
#
|
127
|
+
# Same as CBOR.encode(self[, io]).
|
128
|
+
#
|
129
|
+
# @return [String] serialized data
|
130
|
+
#
|
131
|
+
def to_cbor(io=nil)
|
132
|
+
end
|
133
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
module CBOR
|
2
|
+
|
3
|
+
#
|
4
|
+
# CBOR::Packer is an interface to serialize objects into an internal buffer,
|
5
|
+
# which is a CBOR::Buffer.
|
6
|
+
#
|
7
|
+
class Packer
|
8
|
+
#
|
9
|
+
# Creates a CBOR::Packer instance.
|
10
|
+
# See Buffer#initialize for supported options.
|
11
|
+
#
|
12
|
+
# @overload initialize(options={})
|
13
|
+
# @param options [Hash]
|
14
|
+
#
|
15
|
+
# @overload initialize(io, options={})
|
16
|
+
# @param io [IO]
|
17
|
+
# @param options [Hash]
|
18
|
+
# This packer writes serialzied objects into the IO when the internal buffer is filled.
|
19
|
+
# _io_ must respond to write(string) or append(string) method.
|
20
|
+
#
|
21
|
+
def initialize(*args)
|
22
|
+
end
|
23
|
+
|
24
|
+
#
|
25
|
+
# Internal buffer
|
26
|
+
#
|
27
|
+
# @return CBOR::Buffer
|
28
|
+
#
|
29
|
+
attr_reader :buffer
|
30
|
+
|
31
|
+
#
|
32
|
+
# Serializes an object into internal buffer.
|
33
|
+
#
|
34
|
+
# If it could not serialize the object, it raises
|
35
|
+
# NoMethodError: undefined method `to_cbor' for #<the_object>.
|
36
|
+
#
|
37
|
+
# @param obj [Object] object to serialize
|
38
|
+
# @return [Packer] self
|
39
|
+
#
|
40
|
+
def write(obj)
|
41
|
+
end
|
42
|
+
|
43
|
+
alias pack write
|
44
|
+
|
45
|
+
#
|
46
|
+
# Serializes a nil object. Same as write(nil).
|
47
|
+
#
|
48
|
+
def write_nil
|
49
|
+
end
|
50
|
+
|
51
|
+
#
|
52
|
+
# Write a header of an array whose size is _n_.
|
53
|
+
# For example, write_array_header(1).write(true) is same as write([ true ]).
|
54
|
+
#
|
55
|
+
# @return [Packer] self
|
56
|
+
#
|
57
|
+
def write_array_header(n)
|
58
|
+
end
|
59
|
+
|
60
|
+
#
|
61
|
+
# Write a header of an map whose size is _n_.
|
62
|
+
# For example, write_map_header(1).write('key').write(true) is same as write('key'=>true).
|
63
|
+
#
|
64
|
+
# @return [Packer] self
|
65
|
+
#
|
66
|
+
def write_map_header(n)
|
67
|
+
end
|
68
|
+
|
69
|
+
#
|
70
|
+
# Flushes data in the internal buffer to the internal IO. Same as _buffer.flush.
|
71
|
+
# If internal IO is not set, it does nothing.
|
72
|
+
#
|
73
|
+
# @return [Packer] self
|
74
|
+
#
|
75
|
+
def flush
|
76
|
+
end
|
77
|
+
|
78
|
+
#
|
79
|
+
# Makes the internal buffer empty. Same as _buffer.clear_.
|
80
|
+
#
|
81
|
+
# @return nil
|
82
|
+
#
|
83
|
+
def clear
|
84
|
+
end
|
85
|
+
|
86
|
+
#
|
87
|
+
# Returns size of the internal buffer. Same as buffer.size.
|
88
|
+
#
|
89
|
+
# @return [Integer]
|
90
|
+
#
|
91
|
+
def size
|
92
|
+
end
|
93
|
+
|
94
|
+
#
|
95
|
+
# Returns _true_ if the internal buffer is empty. Same as buffer.empty?.
|
96
|
+
# This method is slightly faster than _size_.
|
97
|
+
#
|
98
|
+
# @return [Boolean]
|
99
|
+
#
|
100
|
+
def empty?
|
101
|
+
end
|
102
|
+
|
103
|
+
#
|
104
|
+
# Returns all data in the buffer as a string. Same as buffer.to_str.
|
105
|
+
#
|
106
|
+
# @return [String]
|
107
|
+
#
|
108
|
+
def to_str
|
109
|
+
end
|
110
|
+
|
111
|
+
alias to_s to_str
|
112
|
+
|
113
|
+
#
|
114
|
+
# Returns content of the internal buffer as an array of strings. Same as buffer.to_a.
|
115
|
+
# This method is faster than _to_str_.
|
116
|
+
#
|
117
|
+
# @return [Array] array of strings
|
118
|
+
#
|
119
|
+
def to_a
|
120
|
+
end
|
121
|
+
|
122
|
+
#
|
123
|
+
# Writes all of data in the internal buffer into the given IO. Same as buffer.write_to(io).
|
124
|
+
# This method consumes and removes data from the internal buffer.
|
125
|
+
# _io_ must respond to write(data) method.
|
126
|
+
#
|
127
|
+
# @param io [IO]
|
128
|
+
# @return [Integer] byte size of written data
|
129
|
+
#
|
130
|
+
def write_to(io)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|