cborb 0.1.0 → 0.2.0
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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +41 -1
- data/bin/generate_fixtures +20 -0
- data/lib/cborb/decoding/concatenated.rb +12 -0
- data/lib/cborb/decoding/decoder.rb +1 -1
- data/lib/cborb/decoding/simple_buffer.rb +5 -0
- data/lib/cborb/decoding/state.rb +9 -1
- data/lib/cborb/version.rb +1 -1
- data/lib/cborb.rb +22 -8
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a9c9b7dc8ac55b507646d2a26a8b797854d62dbca6a0ebb281617ac7be3abe9c
|
4
|
+
data.tar.gz: a09f26e86e196f1b7e8151f52b22a74decbe8406c126d4fcd6bbd73b45b3ca2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ffbad4c40f8e10c3b769e4e832125b364e85e4ffac6d827a9337129e61b88f65d81ca12839028c5d19cc6e42ad0172b491c912d2aa5aefdfab0ea24b8b84c919
|
7
|
+
data.tar.gz: c59156aa20df82792c3d67f3080e032193f668542fb4571687b62181012b20f240ae9099a6555a7bdac792b4287fbea149c9575764101f3e96b9173e9d0b5f15
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# Cborb
|
2
2
|
|
3
|
+
[](https://badge.fury.io/rb/cborb)
|
3
4
|
[](https://circleci.com/gh/murakmii/cborb/tree/master)
|
4
5
|
|
5
6
|
Cborb is a pure ruby decoder for CBOR([RFC 7049](https://tools.ietf.org/html/rfc7049))
|
@@ -12,10 +13,11 @@ decoder = Cborb::Decoding::Decoder.new
|
|
12
13
|
decoder.decode("\x83\x01")
|
13
14
|
decoder.finished? # => false
|
14
15
|
|
15
|
-
decoder.decode("\x02\x03")
|
16
|
+
decoder.decode("\x02\x03\x04")
|
16
17
|
decoder.finished? # => true
|
17
18
|
|
18
19
|
decoder.result # => [1, 2, 3]
|
20
|
+
decoder.remaining_bytes # => "\x04"
|
19
21
|
|
20
22
|
# Shorthand
|
21
23
|
Cborb.decode("\x83\x01\x02\x03") # => [1, 2, 3]
|
@@ -61,3 +63,41 @@ That contains simple value number.
|
|
61
63
|
Cborb.decode "\xEF"
|
62
64
|
# => #<struct Cborb::Decoding::UnassignedSimpleValue number=15>
|
63
65
|
```
|
66
|
+
|
67
|
+
## Handling concatenated CBOR
|
68
|
+
|
69
|
+
By default, when decoder received concatenated CBOR, that raises error.
|
70
|
+
|
71
|
+
```rb
|
72
|
+
Cborb.decode("\x83\x01\x02\x03\x04") # => Cborb::InvalidByteSequenceError
|
73
|
+
```
|
74
|
+
|
75
|
+
If you want to decode concatenated CBOR, set `concatenated` option to `true`.
|
76
|
+
Decoder decodes whole of concatenated CBOR and returns instance of `Cborb::Decoding::Concatenated`.
|
77
|
+
|
78
|
+
```rb
|
79
|
+
results = Cborb.decode("\x83\x01\x02\x03\x04", concatenated: true)
|
80
|
+
# => #<Cborb::Decoding::Concatenated:0x00007fcb1b8b2e30 @decoded=[[1, 2, 3], 4]>
|
81
|
+
|
82
|
+
results.first # => [1, 2, 3]
|
83
|
+
results.last # => 4
|
84
|
+
```
|
85
|
+
|
86
|
+
## Development
|
87
|
+
|
88
|
+
```bash
|
89
|
+
# Clone
|
90
|
+
git clone git@github.com:murakmii/cborb && cd cborb
|
91
|
+
|
92
|
+
# Setup
|
93
|
+
bin/setup
|
94
|
+
|
95
|
+
# Edit code...
|
96
|
+
|
97
|
+
# Run test
|
98
|
+
bundle exec rspec
|
99
|
+
```
|
100
|
+
|
101
|
+
## Contribution
|
102
|
+
|
103
|
+
Contributions are always welcome :kissing_heart:
|
data/bin/generate_fixtures
CHANGED
@@ -260,3 +260,23 @@ end
|
|
260
260
|
generate "010" do |out|
|
261
261
|
out << "\xF6"
|
262
262
|
end
|
263
|
+
|
264
|
+
# lack data(value of map)
|
265
|
+
generate "011" do |out|
|
266
|
+
out << "\xA2"
|
267
|
+
|
268
|
+
out << generate_definite_string("abc", 0)
|
269
|
+
out << "\x18\x7B"
|
270
|
+
|
271
|
+
out << generate_definite_string("def", 0)
|
272
|
+
end
|
273
|
+
|
274
|
+
# big array
|
275
|
+
generate "012" do |out|
|
276
|
+
out << "\x9B\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
|
277
|
+
end
|
278
|
+
|
279
|
+
# big map
|
280
|
+
generate "013" do |out|
|
281
|
+
out << "\xBB\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
|
282
|
+
end
|
data/lib/cborb/decoding/state.rb
CHANGED
@@ -95,7 +95,6 @@ module Cborb::Decoding
|
|
95
95
|
else
|
96
96
|
@stack.pop
|
97
97
|
if @stack.empty?
|
98
|
-
raise Cborb::InvalidByteSequenceError unless @buffer.eof?
|
99
98
|
@result = value
|
100
99
|
break
|
101
100
|
end
|
@@ -118,6 +117,15 @@ module Cborb::Decoding
|
|
118
117
|
finished? ? @result : nil
|
119
118
|
end
|
120
119
|
|
120
|
+
# @return [String]
|
121
|
+
def remaining_bytes
|
122
|
+
if finished?
|
123
|
+
@buffer.peek
|
124
|
+
else
|
125
|
+
""
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
121
129
|
private
|
122
130
|
|
123
131
|
def loop_consuming
|
data/lib/cborb/version.rb
CHANGED
data/lib/cborb.rb
CHANGED
@@ -25,6 +25,7 @@ require "cborb/decoding/types/half_precision_floating_point"
|
|
25
25
|
require "cborb/decoding/types/floating_point"
|
26
26
|
require "cborb/decoding/types/break"
|
27
27
|
|
28
|
+
require "cborb/decoding/concatenated"
|
28
29
|
require "cborb/decoding/ib_jump_table"
|
29
30
|
require "cborb/decoding/tagged_value"
|
30
31
|
require "cborb/decoding/unassigned_simple_value"
|
@@ -36,16 +37,29 @@ module Cborb
|
|
36
37
|
# The shorthand to decode CBOR
|
37
38
|
#
|
38
39
|
# @param [String] cbor
|
40
|
+
# @param [Boolean] concatenated Whether "cbor" param is constructed by concatenated CBOR byte string.
|
41
|
+
# If it's true, this method returns instance of Cborb::Decoding::Concatenated
|
39
42
|
# @return [Object] decoded data(Array, Hash, etc...)
|
40
|
-
def decode(cbor)
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
43
|
+
def decode(cbor, concatenated: false)
|
44
|
+
results = Decoding::Concatenated.new
|
45
|
+
loop do
|
46
|
+
decoder = Decoding::Decoder.new
|
47
|
+
decoder.decode(cbor)
|
48
|
+
|
49
|
+
raise Cborb::InvalidByteSequenceError unless decoder.finished?
|
50
|
+
|
51
|
+
results << decoder.result
|
52
|
+
|
53
|
+
if decoder.remaining_bytes.empty?
|
54
|
+
break
|
55
|
+
elsif !concatenated
|
56
|
+
raise Cborb::InvalidByteSequenceError
|
57
|
+
end
|
58
|
+
|
59
|
+
cbor = decoder.remaining_bytes
|
48
60
|
end
|
61
|
+
|
62
|
+
concatenated ? results : results.first
|
49
63
|
end
|
50
64
|
|
51
65
|
module_function :decode
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cborb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- murakmii
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-10-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -73,6 +73,7 @@ files:
|
|
73
73
|
- bin/setup
|
74
74
|
- cborb.gemspec
|
75
75
|
- lib/cborb.rb
|
76
|
+
- lib/cborb/decoding/concatenated.rb
|
76
77
|
- lib/cborb/decoding/decoder.rb
|
77
78
|
- lib/cborb/decoding/ib_jump_table.rb
|
78
79
|
- lib/cborb/decoding/simple_buffer.rb
|