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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a99e458cdc0e473306db192ae994eee3486b4a7368c4e61578e83b6050766786
4
- data.tar.gz: 2524de034a4263811f61cb1bd9cfbbdd46c690da7e631d30e5b22f2ebd2f994c
3
+ metadata.gz: a9c9b7dc8ac55b507646d2a26a8b797854d62dbca6a0ebb281617ac7be3abe9c
4
+ data.tar.gz: a09f26e86e196f1b7e8151f52b22a74decbe8406c126d4fcd6bbd73b45b3ca2a
5
5
  SHA512:
6
- metadata.gz: 28aa0c323e37738493b08fd8d662293de6264d6c553e44e07d62091f0a1084755b4d8f86d2343414694fc78ae83fa37821c480bee8646952bc218fed1f6d8b24
7
- data.tar.gz: bcd65e60a0eb7d3673455d51b513832849801fbc7ffe55a4009d9fe954dc46b8f6ad9a27560d671544449400e9cfe57e8ffab8a9dd47bc93f62e379a65743d35
6
+ metadata.gz: ffbad4c40f8e10c3b769e4e832125b364e85e4ffac6d827a9337129e61b88f65d81ca12839028c5d19cc6e42ad0172b491c912d2aa5aefdfab0ea24b8b84c919
7
+ data.tar.gz: c59156aa20df82792c3d67f3080e032193f668542fb4571687b62181012b20f240ae9099a6555a7bdac792b4287fbea149c9575764101f3e96b9173e9d0b5f15
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cborb (0.1.0)
4
+ cborb (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # Cborb
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/cborb.svg)](https://badge.fury.io/rb/cborb)
3
4
  [![CircleCI](https://circleci.com/gh/murakmii/cborb/tree/master.svg?style=svg)](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:
@@ -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
@@ -0,0 +1,12 @@
1
+ module Cborb::Decoding
2
+ class Concatenated
3
+ extend Forwardable
4
+ include Enumerable
5
+
6
+ def_delegators :@decoded, :each, :size, :length, :<<, :last
7
+
8
+ def initialize
9
+ @decoded = []
10
+ end
11
+ end
12
+ end
@@ -2,7 +2,7 @@ module Cborb::Decoding
2
2
  class Decoder
3
3
  extend Forwardable
4
4
 
5
- def_delegators :@state, :result, :finished?
5
+ def_delegators :@state, :result, :finished?, :remaining_bytes
6
6
 
7
7
  def initialize
8
8
  @state = Cborb::Decoding::State.new
@@ -20,5 +20,10 @@ module Cborb::Decoding
20
20
  @buffer.rewind
21
21
  @buffer.truncate(0)
22
22
  end
23
+
24
+ def peek
25
+ pos = @buffer.pos
26
+ @buffer.read.to_s.tap { @buffer.pos = pos }
27
+ end
23
28
  end
24
29
  end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Cborb
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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
- decoder = Decoding::Decoder.new
42
- decoder.decode(cbor)
43
-
44
- if decoder.finished?
45
- decoder.result
46
- else
47
- raise Cborb::InvalidByteSequenceError
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.1.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-09-23 00:00:00.000000000 Z
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