cbor-dcbor 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/cbor-dcbor.gemspec +16 -0
- data/lib/cbor-dcbor.rb +73 -0
- data/test/test-dcbor.rb +22 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d2359721ed54d5f9a6c9b53d8680cf34c219c824d255673cca80c7b61b5b98d0
|
4
|
+
data.tar.gz: 601670e9fafd93107819446a0802e5ba38eba633f8a0fe8cdbd0e04cabddf9ef
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d0c1cb8472c3bea21dd32f036f4155e85793b09c1d775601db22b3d17b8b5739e0d33d85052908f149cd955684629b8011713671aeded763df1c791c66852108
|
7
|
+
data.tar.gz: cb4e704864c513c790eabb92acfe0f30fe7b749d1dc0347d4f9d382bf48cd4abb500f806df45b63b3a4452b4d7dfbaeb1bcd931f1556b641516b5d487f1c3d7c
|
data/cbor-dcbor.gemspec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "cbor-dcbor"
|
3
|
+
s.version = "0.0.1"
|
4
|
+
s.summary = "CBOR (Concise Binary Object Representation) \"dCBOR\" encoding"
|
5
|
+
s.description = %q{cbor-dcbor implements "dCBOR" encoding for CBOR, RFC 8949}
|
6
|
+
s.author = "Carsten Bormann"
|
7
|
+
s.email = "cabo@tzi.org"
|
8
|
+
s.license = "Apache-2.0"
|
9
|
+
s.homepage = "http://cbor.io/"
|
10
|
+
s.test_files = Dir['test/**/*.rb']
|
11
|
+
s.files = Dir['lib/**/*.rb'] + %w(cbor-dcbor.gemspec) + Dir['bin/**/*.rb']
|
12
|
+
s.executables = Dir['bin/**/*.rb'].map {|x| File.basename(x)}
|
13
|
+
s.required_ruby_version = '>= 1.9.2'
|
14
|
+
|
15
|
+
s.require_paths = ["lib"]
|
16
|
+
end
|
data/lib/cbor-dcbor.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require "cbor" unless defined? CBOR
|
4
|
+
|
5
|
+
module CBOR
|
6
|
+
module Dcbor
|
7
|
+
|
8
|
+
module Object_Dcbor_CBOR
|
9
|
+
def cbor_prepare_dcbor
|
10
|
+
self
|
11
|
+
end
|
12
|
+
def to_dcbor
|
13
|
+
cbor_prepare_dcbor.to_cbor
|
14
|
+
end
|
15
|
+
end
|
16
|
+
Object.send(:include, Object_Dcbor_CBOR)
|
17
|
+
|
18
|
+
module Integer_Dcbor_CBOR
|
19
|
+
def cbor_prepare_dcbor
|
20
|
+
zigzag, tagnum = if self < 0
|
21
|
+
[-1-self, 3]
|
22
|
+
else
|
23
|
+
[self, 2]
|
24
|
+
end
|
25
|
+
bytes = zigzag.digits(256)
|
26
|
+
if bytes.size + 2 < self.to_cbor.size
|
27
|
+
CBOR::Tagged.new(tagnum, bytes.pack("C*").reverse!)
|
28
|
+
else
|
29
|
+
self
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
Integer.send(:include, Integer_Dcbor_CBOR)
|
34
|
+
|
35
|
+
module Float_Dcbor_CBOR
|
36
|
+
def cbor_prepare_dcbor
|
37
|
+
int = self.to_i
|
38
|
+
if int == self && int.to_cbor.size < self.to_cbor.size
|
39
|
+
int
|
40
|
+
else
|
41
|
+
self
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
Float.send(:include, Float_Dcbor_CBOR)
|
46
|
+
|
47
|
+
module Array_Dcbor_CBOR
|
48
|
+
def cbor_prepare_dcbor
|
49
|
+
map(&:cbor_prepare_dcbor)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
Array.send(:include, Array_Dcbor_CBOR)
|
53
|
+
|
54
|
+
module Hash_Dcbor_CBOR
|
55
|
+
def cbor_prepare_dcbor
|
56
|
+
Hash[map {|k, v|
|
57
|
+
k = k.cbor_prepare_dcbor
|
58
|
+
v = v.cbor_prepare_dcbor
|
59
|
+
cc = k.to_cbor # already prepared
|
60
|
+
[cc, k, v]}.
|
61
|
+
sort.map{|cc, k, v| [k, v]}]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
Hash.send(:include, Hash_Dcbor_CBOR)
|
65
|
+
|
66
|
+
module Tagged_Dcbor_CBOR
|
67
|
+
def cbor_prepare_dcbor
|
68
|
+
CBOR::Tagged.new(tag, value.cbor_prepare_dcbor)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
CBOR::Tagged.send(:include, Tagged_Dcbor_CBOR)
|
72
|
+
end
|
73
|
+
end
|
data/test/test-dcbor.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'cbor-dcbor'
|
2
|
+
|
3
|
+
[[1], [false], [10.3], [10.5], [Float::NAN],
|
4
|
+
[{a: 1, b: [1, 2]}],
|
5
|
+
[{[] => 1, bb: 2}, {bb: 2, [] => 1}],
|
6
|
+
[{b: {mm: 2, m: 3}, "b".b => 1}, {"b".b => 1, b: {m: 3, mm: 2}}],
|
7
|
+
[CBOR::Tagged.new(4711, {aa: 1, b: 2}),
|
8
|
+
CBOR::Tagged.new(4711, {b: 2, aa: 1})],
|
9
|
+
].each do |ex1, ex2, ex3|
|
10
|
+
c1 = ex1.to_cbor
|
11
|
+
cc1 = ex1.to_dcbor
|
12
|
+
# p cc1
|
13
|
+
if ex2.nil?
|
14
|
+
raise [:eq1, c1, cc1].inspect unless c1 == cc1
|
15
|
+
else
|
16
|
+
raise [:ne1, c1, cc1].inspect if c1 == cc1
|
17
|
+
c2 = ex2.to_cbor
|
18
|
+
raise [:eq2, cc1, c2].inspect unless cc1 == c2
|
19
|
+
cc2 = ex2.to_dcbor
|
20
|
+
raise [:eq3, cc1, cc2].inspect unless cc1 == cc2
|
21
|
+
end
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cbor-dcbor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Carsten Bormann
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-05-08 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: cbor-dcbor implements "dCBOR" encoding for CBOR, RFC 8949
|
14
|
+
email: cabo@tzi.org
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- cbor-dcbor.gemspec
|
20
|
+
- lib/cbor-dcbor.rb
|
21
|
+
- test/test-dcbor.rb
|
22
|
+
homepage: http://cbor.io/
|
23
|
+
licenses:
|
24
|
+
- Apache-2.0
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 1.9.2
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubygems_version: 3.4.10
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: CBOR (Concise Binary Object Representation) "dCBOR" encoding
|
45
|
+
test_files:
|
46
|
+
- test/test-dcbor.rb
|