cddlc 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 119d5084051bfbc6889d6a9b36e22308cbb580c9d71b738e4c99ea2f908496cb
4
- data.tar.gz: 33fafda14412916c5883260cb64bea2793785e359d833e6d9cee37cf76260186
3
+ metadata.gz: 446e27ac8733454e0a3a9a9e2f24a43f600f60a414400419287fe147649a7a1f
4
+ data.tar.gz: 1a4e7b41949419befd15479d0373bf3c316e22c62a6796c6453db6c3467f2b5d
5
5
  SHA512:
6
- metadata.gz: c5936c13d4bca770fbcd628851f14a604e27a95e5c1c76329eb81932e7d9c7e72edf898d4989176baffa1d99a88f827e981fad338bb731560eb5547dba69342b
7
- data.tar.gz: d3fe1f230d47841385cc12dd7cf3092c0feae0326ca6889eefb889ceb55903cac6919075cfaf2879961c185adf695df218cb2e1df418a781e6098bf4269d4d49
6
+ metadata.gz: 5fce60c859d8b361973c52a9e4d0a4e18908a7978d938a91c0e50125d64db3a5d704a9cc7f6ad8d17fb7f242bed3f31bb5df2107459d9fdacc57afae9f8df53e
7
+ data.tar.gz: 2c8a8e5f3653125cb719d6aeb7f46642643b561702824a4fcdd404dd40bcdf3ae362ca5cda5323b97f6da521dc555677d8f5139774a12ef9b2f599539b08850e
data/bin/cddlc CHANGED
@@ -58,6 +58,9 @@ begin
58
58
  opts.on("-u", "--[no-]undefined", "Note undefined names") do |v|
59
59
  $options.note_undefined = v
60
60
  end
61
+ opts.on("-2", "--[no-]cddl2", "Perform some CDDL 2.0 processing") do |v|
62
+ $options.cddl2 = v
63
+ end
61
64
  opts.on("-tFMT", "--to=FMT", [:basic, :neat, :json, :yaml, :enum, :cddl], "Target format") do |v|
62
65
  $options.target = v
63
66
  end
@@ -88,6 +91,46 @@ result = if $options.rules
88
91
 
89
92
  warn "** can't note undefined for target != cddl" if $options.note_undefined && $options.target != :cddl
90
93
 
94
+ if $options.cddl2
95
+ require_relative '../lib/processor/cddl-undefined.rb'
96
+ directives = cddl_file.lines.grep(/^;# /).map(&:chomp).map{|l| l.sub(/^;#\s+/, '')}
97
+ # puts "** Directives: <<"
98
+ # puts directives
99
+ directives.each do |di|
100
+ case di
101
+ when /\Ainclude\s+([-._a-zA-Z0-9]+)(?:\s+as\s+([-._a-zA-Z0-9]+))?\s*\z/
102
+ docref = $1
103
+ explicit_preferred_tag = $2
104
+ preferred_tag = explicit_preferred_tag || docref
105
+ puts "PREFERRED_TAG #{preferred_tag}" if $options.verbose
106
+ puts "DOCREF #{docref}" if $options.verbose
107
+ fn = docref.downcase << ".cddl"
108
+ io = File.open(fn) rescue File.open(CDDL::DATA_DIR + fn) # XXX CDDL_PATH
109
+ include_file = io.read
110
+ # XXX: processing directives recursively!
111
+ included_cddl = CDDL.from_cddl(include_file)
112
+ # XXX: Should namespace that thing now! all names -> preferred_tag.name
113
+ p included_cddl.rules.keys
114
+ # XXX: do once, first, to rename COSE_Key to RFC9052.COSE_Key
115
+ loop do
116
+ undef_rule, _undef_gen = cddl.cddl_undefined # ignore undef_gen for now
117
+ # p undef_rule
118
+ got_more = false
119
+ undef_rule.each do |name|
120
+ if rule = included_cddl.rules[name]
121
+ cddl.rules[name] = rule # XXX must be namespaced!
122
+ puts "IMPORTED #{name} from #{fn}" # ...: #{rule}... if $options.verbose
123
+ got_more = true
124
+ end
125
+ end
126
+ break unless got_more
127
+ end
128
+ else
129
+ warn "** unknown directive '#{di}' ignored"
130
+ end
131
+ end
132
+ end
133
+
91
134
  case $options.target
92
135
  when :basic, nil
93
136
  pp result
@@ -116,32 +159,13 @@ when :enum
116
159
  when :cddl
117
160
  require_relative '../lib/writer/cddl-writer.rb'
118
161
  if $options.note_undefined
119
- require_relative "../lib/processor/cddl-visitor.rb"
120
- used = {}
121
- gen_used = {}
122
- def_gen = {}
123
- cddl.rules.each do |k, v|
124
- def_gen[k[0]] = true if Array === k
125
- cddl.visit(v) do |p, &block|
126
- case p
127
- in ["gen", name, *_gen_args]
128
- gen_used[name] = true
129
- in ["name", name]
130
- used[name] = true
131
- else
132
- false
133
- end
134
- end
162
+ require_relative '../lib/processor/cddl-undefined.rb'
163
+ undef_rule, undef_gen = cddl.cddl_undefined
164
+ undef_rule.each do |k|
165
+ puts ";;; *** undefined: #{k}"
135
166
  end
136
- used.each do |k, v|
137
- unless cddl.rules[k] || cddl.prelude.rules[k]
138
- puts ";;; *** undefined: #{k}"
139
- end
140
- end
141
- gen_used.each do |k, v|
142
- unless def_gen[k]
143
- puts ";;; *** undefined: #{k}<>"
144
- end
167
+ undef_gen.each do |k|
168
+ puts ";;; *** undefined: #{k}<>"
145
169
  end
146
170
  end
147
171
  puts cddl.to_s
data/cddlc.gemspec CHANGED
@@ -1,13 +1,13 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "cddlc"
3
- s.version = "0.1.0"
3
+ s.version = "0.1.1"
4
4
  s.summary = "CDDL (Concise Data Definition Language) converters and miscellaneous tools"
5
5
  s.description = %q{cddlc implements converters and miscellaneous tools for CDDL, RFC 8610}
6
6
  s.author = "Carsten Bormann"
7
7
  s.email = "cabo@tzi.org"
8
8
  s.license = "MIT"
9
9
  s.homepage = "http://github.com/cabo/cddlc"
10
- s.files = Dir['lib/**/*.rb'] + %w(cddlc.gemspec data/prelude.cddl) + Dir['bin/**/*.rb']
10
+ s.files = Dir['lib/**/*.rb'] + %w(cddlc.gemspec) + Dir['data/*.cddl'] + Dir['bin/**/*.rb']
11
11
  s.executables = Dir['bin/*'].map {|x| File.basename(x)}
12
12
  s.required_ruby_version = '>= 1.9.2'
13
13
 
data/data/rfc9052.cddl ADDED
@@ -0,0 +1,154 @@
1
+
2
+ start = COSE_Messages / COSE_Key / COSE_KeySet / Internal_Types
3
+
4
+ ; This is defined to make the tool quieter:
5
+ Internal_Types = Sig_structure / Enc_structure / MAC_structure
6
+
7
+
8
+ label = int / tstr
9
+ values = any
10
+
11
+
12
+ COSE_Messages = COSE_Untagged_Message / COSE_Tagged_Message
13
+
14
+ COSE_Untagged_Message = COSE_Sign / COSE_Sign1 /
15
+ COSE_Encrypt / COSE_Encrypt0 /
16
+ COSE_Mac / COSE_Mac0
17
+
18
+ COSE_Tagged_Message = COSE_Sign_Tagged / COSE_Sign1_Tagged /
19
+ COSE_Encrypt_Tagged / COSE_Encrypt0_Tagged /
20
+ COSE_Mac_Tagged / COSE_Mac0_Tagged
21
+
22
+
23
+ Headers = (
24
+ protected : empty_or_serialized_map,
25
+ unprotected : header_map
26
+ )
27
+
28
+ header_map = {
29
+ Generic_Headers,
30
+ * label => values
31
+ }
32
+
33
+ empty_or_serialized_map = bstr .cbor header_map / bstr .size 0
34
+
35
+
36
+
37
+ Generic_Headers = (
38
+ ? 1 => int / tstr, ; algorithm identifier
39
+ ? 2 => [+label], ; criticality
40
+ ? 3 => tstr / int, ; content type
41
+ ? 4 => bstr, ; key identifier
42
+ ? ( 5 => bstr // ; IV
43
+ 6 => bstr ) ; Partial IV
44
+ )
45
+
46
+
47
+ COSE_Sign_Tagged = #6.98(COSE_Sign)
48
+
49
+
50
+ COSE_Sign = [
51
+ Headers,
52
+ payload : bstr / nil,
53
+ signatures : [+ COSE_Signature]
54
+ ]
55
+
56
+
57
+ COSE_Signature = [
58
+ Headers,
59
+ signature : bstr
60
+ ]
61
+
62
+
63
+ COSE_Sign1_Tagged = #6.18(COSE_Sign1)
64
+
65
+
66
+ COSE_Sign1 = [
67
+ Headers,
68
+ payload : bstr / nil,
69
+ signature : bstr
70
+ ]
71
+
72
+
73
+ Sig_structure = [
74
+ context : "Signature" / "Signature1",
75
+ body_protected : empty_or_serialized_map,
76
+ ? sign_protected : empty_or_serialized_map,
77
+ external_aad : bstr,
78
+ payload : bstr
79
+ ]
80
+
81
+
82
+ COSE_Encrypt_Tagged = #6.96(COSE_Encrypt)
83
+
84
+
85
+ COSE_Encrypt = [
86
+ Headers,
87
+ ciphertext : bstr / nil,
88
+ recipients : [+COSE_recipient]
89
+ ]
90
+
91
+
92
+ COSE_recipient = [
93
+ Headers,
94
+ ciphertext : bstr / nil,
95
+ ? recipients : [+COSE_recipient]
96
+ ]
97
+
98
+
99
+ COSE_Encrypt0_Tagged = #6.16(COSE_Encrypt0)
100
+
101
+
102
+ COSE_Encrypt0 = [
103
+ Headers,
104
+ ciphertext : bstr / nil,
105
+ ]
106
+
107
+
108
+ Enc_structure = [
109
+ context : "Encrypt" / "Encrypt0" / "Enc_Recipient" /
110
+ "Mac_Recipient" / "Rec_Recipient",
111
+ protected : empty_or_serialized_map,
112
+ external_aad : bstr
113
+ ]
114
+
115
+
116
+ COSE_Mac_Tagged = #6.97(COSE_Mac)
117
+
118
+
119
+ COSE_Mac = [
120
+ Headers,
121
+ payload : bstr / nil,
122
+ tag : bstr,
123
+ recipients : [+COSE_recipient]
124
+ ]
125
+
126
+
127
+ COSE_Mac0_Tagged = #6.17(COSE_Mac0)
128
+
129
+
130
+ COSE_Mac0 = [
131
+ Headers,
132
+ payload : bstr / nil,
133
+ tag : bstr,
134
+ ]
135
+
136
+
137
+ MAC_structure = [
138
+ context : "MAC" / "MAC0",
139
+ protected : empty_or_serialized_map,
140
+ external_aad : bstr,
141
+ payload : bstr
142
+ ]
143
+
144
+
145
+ COSE_Key = {
146
+ 1 => tstr / int, ; kty
147
+ ? 2 => bstr, ; kid
148
+ ? 3 => tstr / int, ; alg
149
+ ? 4 => [+ (tstr / int) ], ; key_ops
150
+ ? 5 => bstr, ; Base IV
151
+ * label => values
152
+ }
153
+
154
+ COSE_KeySet = [+COSE_Key]
data/lib/cddlc.rb CHANGED
@@ -66,9 +66,12 @@ class CDDL
66
66
  @rules
67
67
  end
68
68
 
69
+ DATA_DIR = Pathname.new(__FILE__) + "../../data/"
70
+
69
71
  def prelude
70
72
  if @prelude.nil?
71
- @prelude = CDDL.from_cddl(File.read(Pathname.new(__FILE__) + "../../data/prelude.cddl"))
73
+ @prelude = CDDL.from_cddl(File.read(DATA_DIR + "prelude.cddl"))
74
+
72
75
  end
73
76
  @prelude
74
77
  end
@@ -0,0 +1,29 @@
1
+ require_relative "../cddlc.rb"
2
+ require_relative "./cddl-visitor.rb"
3
+ require_relative "./cddl-expander.rb"
4
+
5
+ class CDDL
6
+ def cddl_undefined
7
+ # currently only works on expanded...
8
+ cddl2 = self
9
+ cddl2.expand_generics
10
+ used = {}
11
+ gen_used = {}
12
+ def_gen = {}
13
+ cddl2.rules.each do |k, v|
14
+ def_gen[k[0]] = true if Array === k
15
+ cddl2.visit(v) do |p, &block|
16
+ case p
17
+ in ["gen", name, *_gen_args]
18
+ gen_used[name] = true
19
+ in ["name", name]
20
+ used[name] = true
21
+ else
22
+ false
23
+ end
24
+ end
25
+ end
26
+ [used.keys.reject {|name| cddl2.rules[name] || prelude.rules[name]},
27
+ gen_used.keys.reject {|name| def_gen[name] }]
28
+ end
29
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cddlc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carsten Bormann
@@ -76,10 +76,12 @@ files:
76
76
  - bin/cddlc
77
77
  - cddlc.gemspec
78
78
  - data/prelude.cddl
79
+ - data/rfc9052.cddl
79
80
  - lib/cddlc.rb
80
81
  - lib/parser/cddl-util.rb
81
82
  - lib/parser/cddlgrammar.rb
82
83
  - lib/processor/cddl-expander.rb
84
+ - lib/processor/cddl-undefined.rb
83
85
  - lib/processor/cddl-visitor.rb
84
86
  - lib/writer/cddl-writer.rb
85
87
  homepage: http://github.com/cabo/cddlc