cddl 0.8.14 → 0.8.19

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: 182b4b76590226a9f1f72dd534266693e566b2dea3188e5f0c073c69474c1f07
4
- data.tar.gz: 49170c49b5f8153a0fd589d31ffd7d4b10f7b394828a816c8a240e8880487754
3
+ metadata.gz: 8e344aa8fcd67bc17cbeaba6c564ab2c3b6b9ce63b5df9acdffe060ac32628d3
4
+ data.tar.gz: 32410cfe8d62729a6f05e1174871746af3be5998cc2a56fe50bdfc139b53252d
5
5
  SHA512:
6
- metadata.gz: 5b1cced35bb95e5eaded068c3f66f25498ff8f03c0de30a992132aa7d254fb6ce4fc8571dc7d4d7cc32799acc97da8cbe6b43b9c90b8254ad0bf54a645763667
7
- data.tar.gz: f8a7c15619656ae72b792f9fcac5c408a9180a81f51c825e148b086c21a7ba1fa85ef69f4c329bb0cad019198458c33ebe2e0cfa3a030bd11b46eea324e6645d
6
+ metadata.gz: 7fad1d715e3acbd3062c8aeb49518148de9a61c6385a09bd3baa7b384e398ce52bce13afa268209ff2fc0763ef1e07aea932857e0489a0c1e94d79429346418c
7
+ data.tar.gz: ef603bb0dca1beceb0767e87210b4e3819533924c64014392b694e36a2cc9e41b00f1618484226c950aa804e5690e7c5e7451f527b9ad0379ecc5fda5bed389e
data/bin/cddl CHANGED
@@ -14,8 +14,8 @@ def usage
14
14
  warn "Usage:"
15
15
  warn "#$0 spec.cddl generate [n]"
16
16
  warn "#$0 spec.cddl json-generate [n]"
17
- warn "#$0 spec.cddl validate instance.cbor"
18
- warn "#$0 spec.cddl validate instance.json"
17
+ warn "#$0 spec.cddl validate instance.cbor..."
18
+ warn "#$0 spec.cddl validate instance.json..."
19
19
  exit EX_USAGE
20
20
  end
21
21
 
@@ -48,13 +48,19 @@ def my_diag(v)
48
48
  end
49
49
  end
50
50
 
51
+ retcode = 0
52
+
51
53
  begin
52
54
  case ARGV[1]
53
- when /\A.p/
55
+ when /\A..*p/
54
56
  $annotate = true
55
57
  require_relative '../lib/cbor-pp'
56
58
  end
57
59
  case ARGV[1]
60
+ when /\A..*s/
61
+ $sequence = true
62
+ end
63
+ case ARGV[1]
58
64
  when /\Ar/ # secret
59
65
  $advanced = true
60
66
  my_pp parser.rules
@@ -81,15 +87,25 @@ begin
81
87
  g = parser.generate
82
88
  puts JSON.pretty_generate(g)
83
89
  end
84
- when /\Av/
85
- instance = read_arg(ARGV[2])
86
- instance = CBOR.decode(instance.b) rescue JSON.load(instance)
87
- instance = instance.cbor_clone if $annotate && ENV["EXPERIMENTAL_ANNOTATE"]
88
- ann = parser.validate(instance)
89
- # my_pp ann
90
- instance.cbor_add_annotations_from(ann) rescue nil
91
- my_diag(instance) if $annotate
92
- exit 1 unless ann
90
+ when /\Av(v)?/
91
+ verbose = $1
92
+ ARGV[2..-1].each do |fn|
93
+ instance = read_arg(fn)
94
+ instance = if $sequence
95
+ CBOR.decode("\x9f".b << instance.b << "\xff".b)
96
+ else
97
+ CBOR.decode(instance.b) rescue JSON.load(instance)
98
+ end
99
+ instance = instance.cbor_clone if $annotate && ENV["EXPERIMENTAL_ANNOTATE"]
100
+ warn "#{fn}:" if verbose
101
+ ann = parser.validate(instance)
102
+ # my_pp ann
103
+ instance.cbor_add_annotations_from(ann) rescue nil
104
+ my_diag(instance) if $annotate
105
+ unless ann
106
+ retcode = 1
107
+ end
108
+ end
93
109
  else
94
110
  usage
95
111
  end
@@ -97,3 +113,5 @@ rescue CDDL::ParseError => e
97
113
  warn e.message
98
114
  exit EX_DATAERR
99
115
  end
116
+
117
+ exit retcode
data/cddl.gemspec CHANGED
@@ -1,11 +1,12 @@
1
1
  spec = Gem::Specification.new do |s|
2
2
  s.name = 'cddl'
3
- s.version = '0.8.14'
3
+ s.version = '0.8.19'
4
4
  s.summary = "CDDL generator and validator."
5
5
  s.description = %{A parser, generator, and validator for CDDL}
6
6
  s.add_dependency('cbor-diag')
7
7
  s.add_dependency('abnc')
8
8
  s.add_dependency('json')
9
+ s.add_dependency('abnftt')
9
10
  s.add_dependency('regexp-examples') # , '1.1.0')
10
11
  s.add_dependency('colorize')
11
12
  s.files = `git ls-files`.split("\n").grep(/^[a-z]/)
data/lib/cddl.rb CHANGED
@@ -3,6 +3,7 @@ require 'pp'
3
3
  require 'pathname'
4
4
  require 'cbor-pure' unless defined?(CBOR::Tagged)
5
5
  require 'regexp-examples'
6
+ require 'abnftt'
6
7
  require 'colorize'
7
8
  require 'base64'
8
9
 
@@ -249,6 +250,16 @@ module CDDL
249
250
  h[k] = Regexp.new("\\A#{k}\\z")
250
251
  }
251
252
 
253
+ ABNF_PARSER_FOR_STRING = Hash.new {|h, k|
254
+ grammar = "cddl-t0p--1eve1-f0r--abnf = " << k # XXX
255
+ h[k] = ABNF.from_abnf(grammar)
256
+ }
257
+
258
+ ABNF_ENCODING_FOR_CONOP = {
259
+ abnf: Encoding::UTF_8,
260
+ abnfb: Encoding::BINARY
261
+ }
262
+
252
263
  def generate
253
264
  @recursion = 0
254
265
  generate1(rules)
@@ -504,6 +515,19 @@ module CDDL
504
515
  fail "Don't know yet how to generate #{where}"
505
516
  end
506
517
  REGEXP_FOR_STRING[regexp].random_example(max_repeater_variance: 5)
518
+ when :abnf, :abnfb
519
+ grammar = generate1(control)
520
+ bytes = true if target == [:prim, 2]
521
+ bytes = false if target == [:prim, 3]
522
+ unless !bytes.nil? && String === grammar
523
+ fail "Don't know yet how to generate #{where}"
524
+ end
525
+ out = ABNF_PARSER_FOR_STRING[grammar].generate
526
+ if where[1] == :abnfb
527
+ out = out.codepoints.pack("C*")
528
+ end
529
+ enc = bytes ? Encoding::BINARY : Encoding::UTF_8
530
+ out.force_encoding(enc)
507
531
  when :cbor, :cborseq
508
532
  unless target == [:prim, 2]
509
533
  fail "Don't know yet how to generate #{where}"
@@ -606,7 +630,10 @@ module CDDL
606
630
  if warn
607
631
  if result
608
632
  if $features != {}
609
- warn "** Features potentially used: #{$features.map {|k, v| "#{k}: #{v.keys}"}.join(", ")}"
633
+ ok = ENV["CDDL_FEATURE_OK"] and ok = ok.split(/,\s*/) or ok = []
634
+ # warn([:OK, ok, $features].inspect)
635
+ features = $features.reject {|k, v| ok.include? k.to_s }
636
+ warn "** Features potentially used: #{features.map {|k, v| "#{k}: #{v.keys}"}.join(", ")}" if features != {}
610
637
  end
611
638
  else
612
639
  warn "CDDL validation failure (#{result.inspect} for #{d.inspect}):"
@@ -619,7 +646,7 @@ module CDDL
619
646
 
620
647
  def validate_result(check)
621
648
  check || (
622
- @last_message = yield
649
+ @last_message << yield
623
650
  false
624
651
  )
625
652
  end
@@ -652,7 +679,9 @@ module CDDL
652
679
  ann.concat(ann2)
653
680
  end
654
681
  if occ < s
655
- @last_message = "occur not reached in array #{d} for #{where}"
682
+ # warn "*** lme #{@last_message.encoding} #{@last_message}"
683
+ # warn "*** #{"\noccur #{occ} < #{s}, not reached at #{i} in array #{d} for #{where}".encoding}"
684
+ @last_message << "\noccur #{occ} < #{s}, not reached at #{i} in array #{d} for #{where}"
656
685
  return [false, ann]
657
686
  end
658
687
  end
@@ -893,6 +922,24 @@ module CDDL
893
922
  end
894
923
  end
895
924
  )
925
+ when :abnf, :abnfb
926
+ ann if (
927
+ if String === d
928
+ ok, v, vt = extract_value(control)
929
+ if ok && vt == String
930
+ begin
931
+ ABNF_PARSER_FOR_STRING[v].validate(
932
+ d.dup.force_encoding(ABNF_ENCODING_FOR_CONOP[conop]).codepoints.pack("U*")
933
+ )
934
+ true
935
+ rescue => e
936
+ # warn "*** #{e}" # XXX
937
+ @last_message = e.to_s.force_encoding(Encoding::UTF_8)
938
+ nil
939
+ end
940
+ end
941
+ end
942
+ )
896
943
  when :cbor
897
944
  ann if validate1((CBOR::decode(d) rescue :BAD_CBOR), control)
898
945
  when :cborseq
@@ -1247,7 +1294,8 @@ module CDDL
1247
1294
  BRACE = {"{" => :map, "[" => :array}
1248
1295
  RANGE_EXCLUDE_END = {".." => false, "..." => true}
1249
1296
  SUPPORTED_ANNOTATIONS = [:bits, :size, :regexp, :cbor, :cborseq, :within, :and,
1250
- :default, :lt, :le, :gt, :ge, :eq, :ne, :feature, :cat, :plus]
1297
+ :default, :lt, :le, :gt, :ge, :eq, :ne,
1298
+ :feature, :abnf, :abnfb, :cat, :plus]
1251
1299
 
1252
1300
  def type1(n, canbegroup = false)
1253
1301
  # puts "NVALUE #{n.value.inspect}"
@@ -0,0 +1,3 @@
1
+ foo = text .abnf '4DIGIT
2
+ DIGIT = %x30-39 ; 0-9
3
+ '
@@ -0,0 +1,37 @@
1
+ start = [Tag0, Tag1004]
2
+
3
+ ; for draft-ietf-cbor-date-tag
4
+ Tag1004 = #6.1004(text .abnf full-date)
5
+ ; for RFC 7049
6
+ Tag0 = #6.0(text .abnf date-time)
7
+
8
+ full-date = "full-date" .cat rfc3339
9
+ date-time = "date-time" .cat rfc3339
10
+
11
+ ; Note the trick of idiomatically starting with a newline, separating
12
+ ; off the element in the .cat from the rule-list
13
+ rfc3339 = '
14
+ date-fullyear = 4DIGIT
15
+ date-month = 2DIGIT ; 01-12
16
+ date-mday = 2DIGIT ; 01-28, 01-29, 01-30, 01-31 based on
17
+ ; month/year
18
+ time-hour = 2DIGIT ; 00-23
19
+ time-minute = 2DIGIT ; 00-59
20
+ time-second = 2DIGIT ; 00-58, 00-59, 00-60 based on leap sec
21
+ ; rules
22
+ time-secfrac = "." 1*DIGIT
23
+ time-numoffset = ("+" / "-") time-hour ":" time-minute
24
+ time-offset = "Z" / time-numoffset
25
+
26
+ partial-time = time-hour ":" time-minute ":" time-second
27
+ [time-secfrac]
28
+ full-date = date-fullyear "-" date-month "-" date-mday
29
+ full-time = partial-time time-offset
30
+
31
+ date-time = full-date "T" full-time
32
+ ' .cat rfc5234-core
33
+
34
+ rfc5234-core = '
35
+ DIGIT = %x30-39 ; 0-9
36
+ ; abbreviated here
37
+ '
@@ -0,0 +1,32 @@
1
+ start = [tt, tb, bt, bb]
2
+
3
+
4
+ tt = text .abnf '4DIGIT 1FOO
5
+ DIGIT = %x30-39 ; 0-9
6
+ FOO = %xc0-cf
7
+ '
8
+ tb = text .abnfb '4DIGIT 1FOO
9
+ DIGIT = %x30-39 ; 0-9
10
+ FOO = %xc0-cf
11
+ '
12
+ bt = bytes .abnf '4DIGIT 1FOO
13
+ DIGIT = %x30-39 ; 0-9
14
+ FOO = %xc0-cf
15
+ '
16
+ bb = bytes .abnfb '4DIGIT 1FOO
17
+ DIGIT = %x30-39 ; 0-9
18
+ FOO = %xc0-cf
19
+ '
20
+
21
+ ; ["7408\xC6", "7073\xC3", h'30383131CD', h'32363738CD']
22
+ ; ["5892\u00ca", "2145\u00c6", h'33333338C38B', h'31343033C388']
23
+
24
+
25
+ ; ** ENCOED UTF-8
26
+ ; ** ENCOED UTF-8
27
+ ; ** ENCOED UTF-8
28
+ ; ** ENCOED UTF-8
29
+ ; ["5845\u00c6", "9329\u00cb", h'38393538C389', h'33343230C386']
30
+
31
+
32
+ ; ["1066\u00cc", "9253\xC9", h'38333533C38A', h'32373337C4']
@@ -0,0 +1,2 @@
1
+ foo = text .regexp myregexp
2
+ myregexp = "A" .cat "B"
@@ -0,0 +1 @@
1
+ a = [tstr, tstr, tstr]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cddl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.14
4
+ version: 0.8.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carsten Bormann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-17 00:00:00.000000000 Z
11
+ date: 2021-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cbor-diag
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: abnftt
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: regexp-examples
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -100,6 +114,9 @@ files:
100
114
  - test-data/7807.cddl
101
115
  - test-data/a.cddl
102
116
  - test-data/abignum.cddl
117
+ - test-data/abnf1.cddl
118
+ - test-data/abnf2.cddl
119
+ - test-data/abnf3.cddl
103
120
  - test-data/ambig.cddl
104
121
  - test-data/b.cddl
105
122
  - test-data/badaddr.cddl
@@ -107,6 +124,7 @@ files:
107
124
  - test-data/bpv7.cddl
108
125
  - test-data/bpv7a.cddl
109
126
  - test-data/bpv7b.cddl
127
+ - test-data/cat-re.cddl
110
128
  - test-data/cdni-ct.cddl
111
129
  - test-data/coral.cddl
112
130
  - test-data/coral1.cddl
@@ -150,6 +168,7 @@ files:
150
168
  - test-data/named-group.cddl
151
169
  - test-data/patch1.cddl
152
170
  - test-data/reused_named_group.cddl
171
+ - test-data/sequence.cddl
153
172
  - test-data/structure.cddl
154
173
  - test-data/test-gen.cddl
155
174
  - test-data/toerless0.cddl
@@ -180,7 +199,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
180
199
  - !ruby/object:Gem::Version
181
200
  version: '0'
182
201
  requirements: []
183
- rubygems_version: 3.1.2
202
+ rubygems_version: 3.2.3
184
203
  signing_key:
185
204
  specification_version: 4
186
205
  summary: CDDL generator and validator.