rbor 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.
- checksums.yaml +7 -0
- data/lib/rbor/array.rb +101 -0
- data/lib/rbor/bool.rb +32 -0
- data/lib/rbor/hash.rb +36 -0
- data/lib/rbor/integer.rb +29 -0
- data/lib/rbor/string.rb +38 -0
- data/lib/rbor.rb +15 -0
- metadata +52 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: f277956fa01f8ecc1f4adcc4fc2168b0b6a7fea002a3330dce4a89b4fa2f83b8
|
|
4
|
+
data.tar.gz: 36568d6d3106fa0193be560d76396f7e8bbaca5e1488970a58051ce1cd2fc468
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 49e15f6656f1beec5746aae587162e7732ba44c33ccbcccfb70b27d30240a27dec73dfa89f538e6dd8bd44d1fbf40c8d6c371535806ad32c942b6827fec2489e
|
|
7
|
+
data.tar.gz: d3641a6e489c7914027f4de2be93b0765b44be898386638376020182778357f8bbf0f2841d76695cc77e51b08d2b525649632f3929829d673c7d6f35e703dcb6
|
data/lib/rbor/array.rb
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
class Array
|
|
2
|
+
|
|
3
|
+
# Serialize an Array into a CBOR array.
|
|
4
|
+
#
|
|
5
|
+
# @author David P. Sugar (r4gus)
|
|
6
|
+
# @return [String] the object serialized to CBOR.
|
|
7
|
+
def cbor_serialize(options = {})
|
|
8
|
+
len = self.length.cbor_serialize.bytes
|
|
9
|
+
len[0] |= 4 << 5
|
|
10
|
+
out = len.pack('C*')
|
|
11
|
+
self.each do |elem|
|
|
12
|
+
out += elem.cbor_serialize
|
|
13
|
+
end
|
|
14
|
+
return out
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Treat the object as CBOR `BINARY` data and deserialize it into a Ruby object.
|
|
18
|
+
#
|
|
19
|
+
# Depending on the major type of the CBOR data, this method will return
|
|
20
|
+
# a object of one of the following types:
|
|
21
|
+
# - major type 0: Integer
|
|
22
|
+
# - major type 1: Integer
|
|
23
|
+
# - major type 2: String
|
|
24
|
+
# - major type 3: String
|
|
25
|
+
# - major type 4: Array
|
|
26
|
+
# - major type 5: Hash
|
|
27
|
+
# - major type 7: FalseClass, TrueClass, NilClass
|
|
28
|
+
#
|
|
29
|
+
# @author David P. Sugar (r4gus)
|
|
30
|
+
def cbor_deserialize
|
|
31
|
+
raise "empty" if self.empty?
|
|
32
|
+
case self[0]
|
|
33
|
+
when 0x00..0x3b
|
|
34
|
+
v = nil
|
|
35
|
+
|
|
36
|
+
case self[0] & 0x1f
|
|
37
|
+
when 0x00..0x17
|
|
38
|
+
v = self[0] & 0x1f, self[1..]
|
|
39
|
+
when 0x18
|
|
40
|
+
v = self[1], self[2..]
|
|
41
|
+
when 0x19
|
|
42
|
+
v = self[1..2].pack("C*").unpack("S>")[0], self[3..]
|
|
43
|
+
when 0x1a
|
|
44
|
+
v = self[1..4].pack("C*").unpack("L>")[0], self[5..]
|
|
45
|
+
when 0x1b
|
|
46
|
+
v = self[1..8].pack("C*").unpack("Q>")[0], self[9..]
|
|
47
|
+
else
|
|
48
|
+
raise "invalid additional information"
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
if self[0] & 1 << 5 != 0
|
|
52
|
+
v[0] = -v[0] - 1
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
return v
|
|
56
|
+
when 0x40..0x5b
|
|
57
|
+
old = self[0] & 0b11100000
|
|
58
|
+
self[0] &= 0x1f
|
|
59
|
+
len, rem = self.cbor_deserialize
|
|
60
|
+
self[0] |= old
|
|
61
|
+
return String.new(rem[0..len - 1].pack('C*'), encoding: "BINARY"), rem[len..]
|
|
62
|
+
when 0x60..0x7b
|
|
63
|
+
old = self[0] & 0b11100000
|
|
64
|
+
self[0] &= 0x1f
|
|
65
|
+
len, rem = self.cbor_deserialize
|
|
66
|
+
self[0] |= old
|
|
67
|
+
return String.new(rem[0..len - 1].pack('C*'), encoding: "UTF-8"), rem[len..]
|
|
68
|
+
when 0x80..0x9b
|
|
69
|
+
old = self[0] & 0b11100000
|
|
70
|
+
self[0] &= 0x1f
|
|
71
|
+
len, rem = self.cbor_deserialize
|
|
72
|
+
self[0] |= old
|
|
73
|
+
out = []
|
|
74
|
+
len.times do |n|
|
|
75
|
+
obj, rem = rem.cbor_deserialize
|
|
76
|
+
out.append obj
|
|
77
|
+
end
|
|
78
|
+
return out, rem
|
|
79
|
+
when 0xa0..0xbb
|
|
80
|
+
old = self[0] & 0b11100000
|
|
81
|
+
self[0] &= 0x1f
|
|
82
|
+
len, rem = self.cbor_deserialize
|
|
83
|
+
self[0] |= old
|
|
84
|
+
out = {}
|
|
85
|
+
len.times do |n|
|
|
86
|
+
k, rem = rem.cbor_deserialize
|
|
87
|
+
v, rem = rem.cbor_deserialize
|
|
88
|
+
out[k] = v
|
|
89
|
+
end
|
|
90
|
+
return out, rem
|
|
91
|
+
when 0xf4
|
|
92
|
+
return false, self[1..]
|
|
93
|
+
when 0xf5
|
|
94
|
+
return true, self[1..]
|
|
95
|
+
when 0xf6
|
|
96
|
+
return nil, self[1..]
|
|
97
|
+
else
|
|
98
|
+
raise "undefined major type"
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
data/lib/rbor/bool.rb
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
class FalseClass
|
|
2
|
+
|
|
3
|
+
# Serialize false into a CBOR string.
|
|
4
|
+
#
|
|
5
|
+
# @author David P. Sugar (r4gus)
|
|
6
|
+
# @return [String] the object serialized to CBOR.
|
|
7
|
+
def cbor_serialize
|
|
8
|
+
return String.new("\xf4", encoding: "BINARY")
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
class TrueClass
|
|
13
|
+
|
|
14
|
+
# Serialize true into a CBOR string.
|
|
15
|
+
#
|
|
16
|
+
# @author David P. Sugar (r4gus)
|
|
17
|
+
# @return [String] the object serialized to CBOR.
|
|
18
|
+
def cbor_serialize
|
|
19
|
+
return String.new("\xf5", encoding: "BINARY")
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
class NilClass
|
|
24
|
+
|
|
25
|
+
# Serialize nil into a CBOR string.
|
|
26
|
+
#
|
|
27
|
+
# @author David P. Sugar (r4gus)
|
|
28
|
+
# @return [String] the object serialized to CBOR.
|
|
29
|
+
def cbor_serialize
|
|
30
|
+
return String.new("\xf6", encoding: "BINARY")
|
|
31
|
+
end
|
|
32
|
+
end
|
data/lib/rbor/hash.rb
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
class Hash
|
|
2
|
+
|
|
3
|
+
# Serialize a Hash into a CBOR map.
|
|
4
|
+
#
|
|
5
|
+
# @author David P. Sugar (r4gus)
|
|
6
|
+
# @return [String] the object serialized to CBOR.
|
|
7
|
+
def cbor_serialize(options = {})
|
|
8
|
+
len = self.length.cbor_serialize.bytes
|
|
9
|
+
len[0] |= 5 << 5
|
|
10
|
+
out = len.pack('C*')
|
|
11
|
+
tmp = self.sort do |a, b|
|
|
12
|
+
if a[0].is_a? Integer and b[0].is_a? Integer
|
|
13
|
+
if a[0] >= 0 and b[0] >= 0 or a[0] < 0 and b[0] < 0
|
|
14
|
+
a[0] <=> b[0]
|
|
15
|
+
elsif a[0] >= 0
|
|
16
|
+
-1
|
|
17
|
+
else
|
|
18
|
+
1
|
|
19
|
+
end
|
|
20
|
+
elsif a[0].is_a? Integer and b[0].is_a? String
|
|
21
|
+
-1
|
|
22
|
+
elsif a[0].is_a? String and b[0].is_a? Integer
|
|
23
|
+
1
|
|
24
|
+
elsif a[0].is_a? String and b[0].is_a? String
|
|
25
|
+
a[0] <=> b[0]
|
|
26
|
+
else
|
|
27
|
+
0
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
tmp.each do |k, v|
|
|
31
|
+
out += k.cbor_serialize
|
|
32
|
+
out += v.cbor_serialize
|
|
33
|
+
end
|
|
34
|
+
return out
|
|
35
|
+
end
|
|
36
|
+
end
|
data/lib/rbor/integer.rb
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
class Integer
|
|
2
|
+
|
|
3
|
+
# Serialize an Integer into a CBOR string.
|
|
4
|
+
#
|
|
5
|
+
# @author David P. Sugar (r4gus)
|
|
6
|
+
# @return [String] the object serialized to CBOR.
|
|
7
|
+
def cbor_serialize
|
|
8
|
+
v = self
|
|
9
|
+
mt = 0
|
|
10
|
+
|
|
11
|
+
if v < 0 and v >= -2**64
|
|
12
|
+
v = -1 - v
|
|
13
|
+
mt = 1
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
case v
|
|
17
|
+
when 0x00..0x17
|
|
18
|
+
return [mt << 5 | v].pack('C')
|
|
19
|
+
when 0x18..0xff
|
|
20
|
+
return [mt << 5 | 24, v].pack('C*')
|
|
21
|
+
when 0x0100..0xffff
|
|
22
|
+
return [mt << 5 | 0x19].pack('C') + [v].pack("S>")
|
|
23
|
+
when 0x00010000..0xffffffff
|
|
24
|
+
return [mt << 5 | 0x1a].pack('C') + [v].pack("L>")
|
|
25
|
+
when 0x0000000100000000..0xffffffffffffffff
|
|
26
|
+
return [mt << 5 | 0x1b].pack('C') + [v].pack("Q>")
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
data/lib/rbor/string.rb
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
class String
|
|
2
|
+
|
|
3
|
+
# Treat the object as CBOR `BINARY` data and deserialize it into a Ruby object.
|
|
4
|
+
#
|
|
5
|
+
# Depending on the major type of the CBOR data, this method will return
|
|
6
|
+
# a object of one of the following types:
|
|
7
|
+
# - major type 0: Integer
|
|
8
|
+
# - major type 1: Integer
|
|
9
|
+
# - major type 2: String
|
|
10
|
+
# - major type 3: String
|
|
11
|
+
# - major type 4: Array
|
|
12
|
+
# - major type 5: Hash
|
|
13
|
+
# - major type 7: FalseClass, TrueClass, NilClass
|
|
14
|
+
#
|
|
15
|
+
# @author David P. Sugar (r4gus)
|
|
16
|
+
def cbor_deserialize
|
|
17
|
+
return self.bytes.cbor_deserialize
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Serialize a String into a CBOR string.
|
|
21
|
+
#
|
|
22
|
+
# If the `encoding` is of type `UTF-8`, the object will be serialized
|
|
23
|
+
# to a text string (major type 3). Otherwise, the obejct will be
|
|
24
|
+
# serialized to a byte string (major type 2).
|
|
25
|
+
#
|
|
26
|
+
# @author David P. Sugar (r4gus)
|
|
27
|
+
# @param options [Hash] the serialization options.
|
|
28
|
+
# @return [String] the object serialized to CBOR.
|
|
29
|
+
def cbor_serialize(options = {})
|
|
30
|
+
len = self.bytesize.cbor_serialize.bytes
|
|
31
|
+
mt = 2
|
|
32
|
+
if self.encoding.to_s == "UTF-8"
|
|
33
|
+
mt = 3
|
|
34
|
+
end
|
|
35
|
+
len[0] = mt << 5 | len[0]
|
|
36
|
+
return len.pack('C*') + String.new(self, encoding: "BINARY")
|
|
37
|
+
end
|
|
38
|
+
end
|
data/lib/rbor.rb
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# The Concise Binary Object Representation (CBOR) is a data format whose
|
|
2
|
+
# design goals include the possibility of extremely small code size, fairly
|
|
3
|
+
# small message size, and extensibility without the need for version negotiation
|
|
4
|
+
# (RFC8949). It is used in different protocols like the Client to Authenticator
|
|
5
|
+
# Protocol CTAP2 which is a essential part of FIDO2 authenticators/ Passkeys.
|
|
6
|
+
|
|
7
|
+
class Cbor
|
|
8
|
+
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
require_relative 'rbor/integer.rb'
|
|
12
|
+
require_relative 'rbor/string.rb'
|
|
13
|
+
require_relative 'rbor/array.rb'
|
|
14
|
+
require_relative 'rbor/hash.rb'
|
|
15
|
+
require_relative 'rbor/bool.rb'
|
metadata
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rbor
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- David P. Sugar
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2024-09-08 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: The Concise Binary Object Representation (CBOR) is a data format whose
|
|
14
|
+
design goals include the possibility of extremely small code size, fairly small
|
|
15
|
+
message size, and extensibility without the need for version negotiation (RFC8949).
|
|
16
|
+
It is used in different protocols like the Client to Authenticator Protocol CTAP2
|
|
17
|
+
which is a essential part of FIDO2 authenticators/ Passkeys.
|
|
18
|
+
email: david@thesugar.de
|
|
19
|
+
executables: []
|
|
20
|
+
extensions: []
|
|
21
|
+
extra_rdoc_files: []
|
|
22
|
+
files:
|
|
23
|
+
- lib/rbor.rb
|
|
24
|
+
- lib/rbor/array.rb
|
|
25
|
+
- lib/rbor/bool.rb
|
|
26
|
+
- lib/rbor/hash.rb
|
|
27
|
+
- lib/rbor/integer.rb
|
|
28
|
+
- lib/rbor/string.rb
|
|
29
|
+
homepage: https://rubygems.org/gems/rbor
|
|
30
|
+
licenses:
|
|
31
|
+
- MIT
|
|
32
|
+
metadata: {}
|
|
33
|
+
post_install_message:
|
|
34
|
+
rdoc_options: []
|
|
35
|
+
require_paths:
|
|
36
|
+
- lib
|
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - ">="
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '0'
|
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
requirements: []
|
|
48
|
+
rubygems_version: 3.5.16
|
|
49
|
+
signing_key:
|
|
50
|
+
specification_version: 4
|
|
51
|
+
summary: Concise Binary Object Representation
|
|
52
|
+
test_files: []
|