cborb 0.1.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.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +27 -0
  3. data/.editorconfig +7 -0
  4. data/.gitignore +13 -0
  5. data/.rspec +2 -0
  6. data/Gemfile +6 -0
  7. data/Gemfile.lock +35 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +63 -0
  10. data/Rakefile +6 -0
  11. data/bin/console +7 -0
  12. data/bin/generate_fixtures +262 -0
  13. data/bin/setup +8 -0
  14. data/cborb.gemspec +27 -0
  15. data/lib/cborb.rb +52 -0
  16. data/lib/cborb/decoding/decoder.rb +19 -0
  17. data/lib/cborb/decoding/ib_jump_table.rb +47 -0
  18. data/lib/cborb/decoding/simple_buffer.rb +24 -0
  19. data/lib/cborb/decoding/state.rb +130 -0
  20. data/lib/cborb/decoding/tagged_value.rb +3 -0
  21. data/lib/cborb/decoding/types/array.rb +20 -0
  22. data/lib/cborb/decoding/types/break.rb +12 -0
  23. data/lib/cborb/decoding/types/byte_string.rb +12 -0
  24. data/lib/cborb/decoding/types/floating_point.rb +18 -0
  25. data/lib/cborb/decoding/types/half_precision_floating_point.rb +28 -0
  26. data/lib/cborb/decoding/types/indefinite_array.rb +23 -0
  27. data/lib/cborb/decoding/types/indefinite_byte_string.rb +25 -0
  28. data/lib/cborb/decoding/types/indefinite_map.rb +24 -0
  29. data/lib/cborb/decoding/types/indefinite_text_string.rb +25 -0
  30. data/lib/cborb/decoding/types/integer.rb +12 -0
  31. data/lib/cborb/decoding/types/integer_decodable.rb +24 -0
  32. data/lib/cborb/decoding/types/map.rb +24 -0
  33. data/lib/cborb/decoding/types/negative_integer.rb +12 -0
  34. data/lib/cborb/decoding/types/root.rb +7 -0
  35. data/lib/cborb/decoding/types/simple_value.rb +17 -0
  36. data/lib/cborb/decoding/types/tag.rb +17 -0
  37. data/lib/cborb/decoding/types/text_string.rb +13 -0
  38. data/lib/cborb/decoding/types/type.rb +24 -0
  39. data/lib/cborb/decoding/types/unassigned_simple_value.rb +13 -0
  40. data/lib/cborb/decoding/types/unknown.rb +8 -0
  41. data/lib/cborb/decoding/unassigned_simple_value.rb +3 -0
  42. data/lib/cborb/errors.rb +11 -0
  43. data/lib/cborb/version.rb +3 -0
  44. metadata +128 -0
@@ -0,0 +1,25 @@
1
+ module Cborb::Decoding::Types
2
+ # To represent major type: 2(indefinite-length)
3
+ #
4
+ # @see https://tools.ietf.org/html/rfc7049#section-2.2.2
5
+ class IndefiniteByteString < Type
6
+ def self.indefinite?
7
+ true
8
+ end
9
+
10
+ def self.decode(state, additional_info)
11
+ state.push_stack(self, String.new.force_encoding(::Encoding::ASCII_8BIT))
12
+ end
13
+
14
+ def self.accept(im_data, type, value)
15
+ if type == Cborb::Decoding::Types::ByteString
16
+ im_data.concat(value)
17
+ Cborb::Decoding::State::CONTINUE
18
+ elsif type == Cborb::Decoding::Types::Break
19
+ im_data
20
+ else
21
+ raise Cborb::DecodingError, "Unexpected chunk for indefinite byte string"
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ module Cborb::Decoding::Types
2
+ # To represent major type: 5(indefinite-length)
3
+ #
4
+ # @see https://tools.ietf.org/html/rfc7049#section-2.2.1
5
+ class IndefiniteMap < Type
6
+ def self.indefinite?
7
+ true
8
+ end
9
+
10
+ def self.decode(state, additional_info)
11
+ state.push_stack(self, [])
12
+ end
13
+
14
+ def self.accept(im_data, type, value)
15
+ if type == Cborb::Decoding::Types::Break
16
+ raise Cborb::DecodingError, "Invalid indefinite-length map" if im_data.size.odd?
17
+ Hash[*im_data]
18
+ else
19
+ im_data << value
20
+ Cborb::Decoding::State::CONTINUE
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ module Cborb::Decoding::Types
2
+ # To represent major type: 3(indefinite-length)
3
+ #
4
+ # @see https://tools.ietf.org/html/rfc7049#section-2.2.2
5
+ class IndefiniteTextString < Type
6
+ def self.indefinite?
7
+ true
8
+ end
9
+
10
+ def self.decode(state, additional_info)
11
+ state.push_stack(self, String.new.force_encoding(::Encoding::UTF_8))
12
+ end
13
+
14
+ def self.accept(im_data, type, value)
15
+ if type == Cborb::Decoding::Types::TextString
16
+ im_data.concat(value)
17
+ Cborb::Decoding::State::CONTINUE
18
+ elsif type == Cborb::Decoding::Types::Break
19
+ im_data
20
+ else
21
+ raise Cborb::DecodingError, "Unexpected chunk for indefinite text string"
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,12 @@
1
+ module Cborb::Decoding::Types
2
+ # To represent major type: 0
3
+ #
4
+ # @see https://tools.ietf.org/html/rfc7049#section-2.1
5
+ class Integer < Type
6
+ extend Cborb::Decoding::Types::IntegerDecodable
7
+
8
+ def self.decode(state, additional_info)
9
+ state.accept_value(self, consume_as_integer(state, additional_info))
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,24 @@
1
+ module Cborb::Decoding::Types
2
+ # In CBOR, The process that decode some bytes as integer is popular.
3
+ # Thus, we modularize that.
4
+ module IntegerDecodable
5
+ UNPACK_TEMPLATES = [
6
+ "C".freeze,
7
+ "S>".freeze,
8
+ "I>".freeze,
9
+ "Q>".freeze,
10
+ ].freeze
11
+
12
+ # @param [Cborb::Decoding::State] state
13
+ # @param [Integer] additional_info
14
+ # @return [Integer]
15
+ def consume_as_integer(state, additional_info)
16
+ return additional_info if additional_info < 24
17
+
18
+ index = additional_info - 24
19
+ bytesize = 2**index
20
+
21
+ state.consume(bytesize).unpack(UNPACK_TEMPLATES[index]).first
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ module Cborb::Decoding::Types
2
+ # To represent major type: 5(definite-length)
3
+ #
4
+ # @see https://tools.ietf.org/html/rfc7049#section-2.1
5
+ class Map < Type
6
+ extend Cborb::Decoding::Types::IntegerDecodable
7
+
8
+ Intermediate = Struct.new(:size, :keys_and_values)
9
+
10
+ def self.decode(state, additional_info)
11
+ im_data = Intermediate.new(consume_as_integer(state, additional_info) * 2, [])
12
+ state.push_stack(self, im_data)
13
+ end
14
+
15
+ def self.accept(im_data, type, value)
16
+ im_data.keys_and_values << value
17
+ if im_data.keys_and_values.size == im_data.size
18
+ Hash[*im_data.keys_and_values]
19
+ else
20
+ Cborb::Decoding::State::CONTINUE
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,12 @@
1
+ module Cborb::Decoding::Types
2
+ # To represent major type: 1
3
+ #
4
+ # @see https://tools.ietf.org/html/rfc7049#section-2.1
5
+ class NegativeInteger < Type
6
+ extend Cborb::Decoding::Types::IntegerDecodable
7
+
8
+ def self.decode(state, additional_info)
9
+ state.accept_value(self, -(consume_as_integer(state, additional_info) + 1))
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ module Cborb::Decoding::Types
2
+ class Root < Type
3
+ def self.accept(im_data, type, value)
4
+ value
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ module Cborb::Decoding::Types
2
+ # To represent part of major type: 7
3
+ #
4
+ # @see https://tools.ietf.org/html/rfc7049#section-2.3
5
+ class SimpleValue < Type
6
+ VALUES = [
7
+ false,
8
+ true,
9
+ nil,
10
+ nil, # doesn't exist "undefined" in ruby
11
+ ].freeze
12
+
13
+ def self.decode(state, additional_info)
14
+ state.accept_value(self, VALUES[additional_info - 20])
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module Cborb::Decoding::Types
2
+ # To represent major type: 6
3
+ #
4
+ # @see https://tools.ietf.org/html/rfc7049#section-2.1
5
+ class Tag < Type
6
+ extend Cborb::Decoding::Types::IntegerDecodable
7
+
8
+ def self.decode(state, additional_info)
9
+ state.push_stack(self, consume_as_integer(state, additional_info))
10
+ end
11
+
12
+ def self.accept(im_data, type, value)
13
+ # Currently, decoder doesn't interpret a tag.
14
+ Cborb::Decoding::TaggedValue.new(im_data, value)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ module Cborb::Decoding::Types
2
+ # To represent major type: 3(definite-length)
3
+ #
4
+ # @see https://tools.ietf.org/html/rfc7049#section-2.1
5
+ class TextString < Type
6
+ extend Cborb::Decoding::Types::IntegerDecodable
7
+
8
+ def self.decode(state, additional_info)
9
+ bytes = state.consume(consume_as_integer(state, additional_info))
10
+ state.accept_value(self, bytes.force_encoding(::Encoding::UTF_8))
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,24 @@
1
+ module Cborb::Decoding
2
+ module Types
3
+ # Base class for all type classes
4
+ class Type
5
+ # @return [Boolean]
6
+ def self.indefinite?
7
+ false
8
+ end
9
+
10
+ # @param [Cborb::Decoding::State] state
11
+ # @param [Integer] additional_info
12
+ def self.decode(state, additional_info)
13
+ raise NotImplementedError
14
+ end
15
+
16
+ # @param [Object] im_data
17
+ # @param [Class] type
18
+ # @param [Object] value
19
+ def self.accept(im_data, type, value)
20
+ raise "#{self} can't accept value"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,13 @@
1
+ module Cborb::Decoding::Types
2
+ # To represent unassigned simple value
3
+ #
4
+ # @see https://tools.ietf.org/html/rfc7049#section-2.3
5
+ class UnassignedSimpleValue < Type
6
+ extend Cborb::Decoding::Types::IntegerDecodable
7
+
8
+ def self.decode(state, additional_info)
9
+ simple_value = consume_as_integer(state, additional_info)
10
+ state.accept_value(self, Cborb::Decoding::UnassignedSimpleValue.new(simple_value))
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ module Cborb::Decoding::Types
2
+ # The pseudo type to represent unknown/invalid initial byte
3
+ class Unknown < Type
4
+ def self.decode(state, additional_info)
5
+ raise Cborb::DecodingError, "Unknown initial byte"
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module Cborb::Decoding
2
+ UnassignedSimpleValue = Struct.new(:number)
3
+ end
@@ -0,0 +1,11 @@
1
+ module Cborb
2
+ class Error < ::StandardError; end
3
+
4
+ class DecodingError < Error; end
5
+
6
+ class InvalidByteSequenceError < DecodingError
7
+ def initialize
8
+ super("CBOR has invalid byte sequence")
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Cborb
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cborb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - murakmii
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-23 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Cborb is a pure ruby decoder for CBOR(RFC 7049)
56
+ email:
57
+ - bonono.jp@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".circleci/config.yml"
63
+ - ".editorconfig"
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/generate_fixtures
73
+ - bin/setup
74
+ - cborb.gemspec
75
+ - lib/cborb.rb
76
+ - lib/cborb/decoding/decoder.rb
77
+ - lib/cborb/decoding/ib_jump_table.rb
78
+ - lib/cborb/decoding/simple_buffer.rb
79
+ - lib/cborb/decoding/state.rb
80
+ - lib/cborb/decoding/tagged_value.rb
81
+ - lib/cborb/decoding/types/array.rb
82
+ - lib/cborb/decoding/types/break.rb
83
+ - lib/cborb/decoding/types/byte_string.rb
84
+ - lib/cborb/decoding/types/floating_point.rb
85
+ - lib/cborb/decoding/types/half_precision_floating_point.rb
86
+ - lib/cborb/decoding/types/indefinite_array.rb
87
+ - lib/cborb/decoding/types/indefinite_byte_string.rb
88
+ - lib/cborb/decoding/types/indefinite_map.rb
89
+ - lib/cborb/decoding/types/indefinite_text_string.rb
90
+ - lib/cborb/decoding/types/integer.rb
91
+ - lib/cborb/decoding/types/integer_decodable.rb
92
+ - lib/cborb/decoding/types/map.rb
93
+ - lib/cborb/decoding/types/negative_integer.rb
94
+ - lib/cborb/decoding/types/root.rb
95
+ - lib/cborb/decoding/types/simple_value.rb
96
+ - lib/cborb/decoding/types/tag.rb
97
+ - lib/cborb/decoding/types/text_string.rb
98
+ - lib/cborb/decoding/types/type.rb
99
+ - lib/cborb/decoding/types/unassigned_simple_value.rb
100
+ - lib/cborb/decoding/types/unknown.rb
101
+ - lib/cborb/decoding/unassigned_simple_value.rb
102
+ - lib/cborb/errors.rb
103
+ - lib/cborb/version.rb
104
+ homepage: https://github.com/murakmii/cborb
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.7.4
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Cborb is a pure ruby decoder for CBOR(RFC 7049)
128
+ test_files: []