cddl 0.11.1 → 0.12.1

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: b75dfee163242854b4a139d3a6e84710d9ac31702e57e502ac125b1b5e9dc944
4
- data.tar.gz: 4a5360b99c364b7cd6c1aa518a332e79616e65331a95243ba2406a8d04712639
3
+ metadata.gz: a29d5d85e11598502b09d04e3b08a7ada9b6d0d8a9eb0e425bb01da81b92532b
4
+ data.tar.gz: 61c2a191c264c34600f0b66a8e7cfb2ea1359f3954676828a56a63aa25e3015c
5
5
  SHA512:
6
- metadata.gz: 1127b09451324ff3842890e59e077c96f3c248082c9dac7f194f453faf88d0c9ffc8e7326c8087677ec953c03b62cbe50e582a2d8362876e57375289453ab3e4
7
- data.tar.gz: 74ce9832d9a84dd4204411beecd9b029ddb76eacbc9e09e2056512310cbdb4309d439360d9bc1bf6c86ca60a87b7ca3423f9ef8471481657bda327f816b4426b
6
+ metadata.gz: 671d119f1cb328151c0631cbcd808f5a32d1f82d6c715a64cb5e5c9702b3452c996278a6b5055613de74b00f76489a9874037b336ac3a760947d4a2526d84cc9
7
+ data.tar.gz: 8146d9b66bb87e84b2179507fb9144012676be726dd5f4312da2482a047e46429a6d41d5b1d49097724aa8cfd68519be06e6e8f860e97f0cbc187de0c20242ca
data/bin/cddl CHANGED
@@ -7,10 +7,13 @@ require 'json'
7
7
 
8
8
  Encoding.default_external = "UTF-8" # wake up, smell the coffee
9
9
 
10
+ CDDL_VERSION = Gem.loaded_specs["cddl"].version rescue "unknown-version"
11
+
10
12
  EX_USAGE = 64
11
13
  EX_DATAERR = 65
12
14
 
13
15
  def usage
16
+ warn "cddl tool version #{CDDL_VERSION}"
14
17
  warn "Usage:"
15
18
  warn "#$0 spec.cddl generate [n]"
16
19
  warn "#$0 spec.cddl json-generate [n]"
@@ -26,7 +29,10 @@ def read_arg(arg, remember_fn = true)
26
29
  else
27
30
  usage unless arg
28
31
  $fn = arg if remember_fn
29
- File.read(arg, mode: "rb")
32
+ File.read(arg, mode: "rb").force_encoding(Encoding::UTF_8).scrub { |c|
33
+ warn "*** replaced invalid UTF-8 byte sequence #{c.inspect} by U+FFFD REPLACEMENT CHARACTER"
34
+ 0xFFFD.chr(Encoding::UTF_8)
35
+ }
30
36
  end
31
37
  end
32
38
 
data/cddl.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  spec = Gem::Specification.new do |s|
2
2
  s.name = 'cddl'
3
- s.version = '0.11.1'
3
+ s.version = '0.12.1'
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')
data/data/cddl.abnf CHANGED
@@ -70,27 +70,39 @@ fraction = 1*DIGIT
70
70
  exponent = int
71
71
 
72
72
  text = %x22 *SCHAR %x22
73
- SCHAR = %x20-21 / %x23-7E / SESC
74
- SESC = "\" %x20-7E
73
+ SCHAR = %x20-21 / %x23-5B / %x5D-7E / NONASCII / SESC
74
+
75
+ SESC = "\" ( %x22 / "/" / "\" / ; \" \/ \\
76
+ %x62 / %x66 / %x6E / %x72 / %x74 / ; \b \f \n \r \t
77
+ (%x75 hexchar) ) ; \uXXXX
78
+
79
+ hexchar = "{" (1*"0" [ hexscalar ] / hexscalar) "}" /
80
+ non-surrogate / (high-surrogate "\" %x75 low-surrogate)
81
+ non-surrogate = ((DIGIT / "A"/"B"/"C" / "E"/"F") HEXDIG HEXDIG HEXDIG) /
82
+ ("D" %x30-37 HEXDIG HEXDIG)
83
+ high-surrogate = "D" ("8"/"9"/"A"/"B") HEXDIG HEXDIG
84
+ low-surrogate = "D" ("C"/"D"/"E"/"F") HEXDIG HEXDIG
85
+ hexscalar = "10" HEXDIG HEXDIG HEXDIG HEXDIG / HEXDIG1 HEXDIG HEXDIG HEXDIG HEXDIG
86
+ / non-surrogate / HEXDIG [HEXDIG [HEXDIG]]
75
87
 
76
88
  bytes = [bsqual] %x27 *BCHAR %x27
77
- BCHAR = %x20-26 / %x28-5B / %x5D-7E / SESC / CRLF
78
- bsqual = %x68 ; "h"
79
- / %x62.36.34 ; "b64"
89
+ BCHAR = %x20-26 / %x28-5B / %x5D-7E / NONASCII / SESC / "\'" / CRLF
90
+ bsqual = "h" / "b64"
80
91
 
81
92
  id = EALPHA *(*("-" / ".") (EALPHA / DIGIT))
82
93
  ALPHA = %x41-5A / %x61-7A
83
- EALPHA = %x41-5A / %x61-7A / "@" / "_" / "$"
94
+ EALPHA = ALPHA / "@" / "_" / "$"
84
95
  DIGIT = %x30-39
85
96
  DIGIT1 = %x31-39
86
97
  HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"
98
+ HEXDIG1 = DIGIT1 / "A" / "B" / "C" / "D" / "E" / "F"
87
99
  BINDIG = %x30-31
88
100
 
89
101
  S = *WS
90
102
  WS = SP / NL
91
103
  SP = %x20
92
104
  NL = COMMENT / CRLF
93
- COMMENT = ";" *(SP / VCHAR) CRLF
94
- VCHAR = %x21-7E
105
+ COMMENT = ";" *PCHAR CRLF
106
+ PCHAR = %x20-7E / NONASCII
107
+ NONASCII = %xA0-D7FF / %xE000-10FFFD
95
108
  CRLF = %x0A / %x0D.0A
96
-
data/lib/cddl.rb CHANGED
@@ -61,7 +61,7 @@ module CDDL
61
61
  if upto - presult < 100
62
62
  part2 = source_text[presult...upto]
63
63
  else
64
- part2 = source_text[presult, 50] + "......." + source_text[upto-50, 50]
64
+ part2 = source_text[presult, 50] + "......." + (source_text[upto-50, 50] || "")
65
65
  end
66
66
  warn "*** Look for syntax problems around the #{
67
67
  "%%%".colorize(background: :light_yellow)} markers:\n#{
@@ -747,7 +747,7 @@ module CDDL
747
747
  end
748
748
  elsif t[0] == :anno
749
749
  _, conop, target, control = t
750
- # warn ["EXV0", conop, target, control].inspect
750
+ warn ["EXV0", conop, target, control].inspect if ENV["CDDL_TRACE"]
751
751
  if conop == :cat || conop == :plus || conop == :det
752
752
  ok1, v1, vt1 = extract_value(target)
753
753
  ok2, v2, vt2 = extract_value(control)
@@ -980,6 +980,19 @@ module CDDL
980
980
  }
981
981
  end
982
982
 
983
+ def validate1a_ignore_encoding(d, where)
984
+ if String === d
985
+ validate1a(d.force_encoding(Encoding::BINARY), where) or (
986
+ text = d.force_encoding(Encoding::UTF_8).scrub {
987
+ fail "can't use bytes as text"
988
+ } rescue :NOT_A_TEXT_STRING
989
+ validate1a(text, where)
990
+ )
991
+ else
992
+ validate1a(d, where)
993
+ end
994
+ end
995
+
983
996
  def validate1a(d, where)
984
997
  if ann = validate1(d, where)
985
998
  here = [d, where]
@@ -1039,7 +1052,7 @@ module CDDL
1039
1052
  if conop == :cat || conop == :plus || conop == :det
1040
1053
  ok1, v1, vt1 = extract_value(target)
1041
1054
  ok2, v2, vt2 = extract_value(control)
1042
- # warn ["ANNO0", ok1, v1, vt1, ok2, v2, vt2, d].inspect
1055
+ warn ["ANNO0", ok1, v1, vt1, ok2, v2, vt2, d].inspect if ENV["CDDL_TRACE"]
1043
1056
  if ok1 && ok2
1044
1057
  v2 = Integer(v2) if vt1 == Integer
1045
1058
  if conop == :det
@@ -1048,6 +1061,30 @@ module CDDL
1048
1061
  end
1049
1062
  # warn ["ANNO", ok1, v1, vt1, ok2, v2, vt2, d].inspect
1050
1063
  [] if d == v1 + v2 # XXX Focus ArgumentError
1064
+ elsif conop == :cat && String === d
1065
+ warn ["CAT-L", ok1, v1, vt1, ok2, v2, vt2, d].inspect if ENV["CDDL_TRACE"]
1066
+ if ok1 && vt1 == String
1067
+ # d and lhs (v1) need to agree in encoding
1068
+ if d.encoding == v1.encoding
1069
+ if d[0...v1.length] == v1
1070
+ d2 = d[v1.length..-1]
1071
+ warn ["CAT-L1", d2, d2.encoding, control].inspect if ENV["CDDL_TRACE"]
1072
+ validate1a_ignore_encoding(d2, control)
1073
+ end
1074
+ end
1075
+ elsif ok2 && vt2 == String
1076
+ warn ["CAT-R", ok1, v1, vt1, ok2, v2, vt2, d].inspect if ENV["CDDL_TRACE"]
1077
+ if d[-v2.length..-1] == v2
1078
+ d1 = d[0...-v2.length]
1079
+ validate1a(d1, control)
1080
+ end
1081
+ end
1082
+ elsif conop == :plus && Integer === d
1083
+ if ok1 && vt1 == Integer
1084
+ validate1a(d - Integer(v1), control)
1085
+ elsif ok2 && vt2 == Integer
1086
+ validate1a(d - Integer(v2), target)
1087
+ end
1051
1088
  end
1052
1089
  elsif ann = validate1a(d, target)
1053
1090
  case conop
@@ -1157,14 +1194,75 @@ module CDDL
1157
1194
  end
1158
1195
  )
1159
1196
  when :join
1160
- ok, *v = extract_array(control)
1161
- # warn "@@@JJJ@@@ #{ok.inspect} #{v.inspect}"
1162
- if ok
1163
- expected = v.map {|ve|
1164
- fail "Not a string in #{where}" unless String === ve[0]
1165
- ve[0]
1166
- }.join
1167
- ann if d == expected
1197
+ t = control
1198
+ v = if t[0] == :array
1199
+ t[1..-1].map { |el|
1200
+ if el[0..2] == [:member, 1, 1]
1201
+ ok, v, vt = extract_value(el[4])
1202
+ if ok
1203
+ [true, v, vt]
1204
+ else
1205
+ [false, el[4]]
1206
+ end
1207
+ end
1208
+ }
1209
+ end
1210
+ warn "@@@JOIN@@@ #{v.inspect}" if ENV["CDDL_TRACE"]
1211
+ ok = true
1212
+ if v
1213
+ ix = 0
1214
+ rest = d.dup
1215
+ while ix < v.length
1216
+ if left = v[ix]
1217
+ if left[0] # match constant value first
1218
+ fail "Not a string for #{left.inspect} in #{where}" unless String == left[2]
1219
+ want = left[1]
1220
+ have = rest[0...left[1].length]
1221
+ if want == have
1222
+ rest[0...left[1].length] = ''
1223
+ ix += 1
1224
+ next
1225
+ else
1226
+ fail ".join: want #{want.inspect}, have #{have.inspect}"
1227
+ end
1228
+ else
1229
+ ix += 1
1230
+ if ix == v.length # match remaining
1231
+ warn "@@@JOIN ok in #{ok.inspect} rest #{rest.inspect}" if ENV["CDDL_TRACE"]
1232
+ ok &&= validate1(rest, left[1])
1233
+ warn "@@@JOIN ok out #{ok.inspect}" if ENV["CDDL_TRACE"]
1234
+ # more diag
1235
+ rest = ''
1236
+ next
1237
+ else
1238
+ if mid = v[ix]
1239
+ if mid[0] # have constant value to split over
1240
+ fail "Not a string for #{mid} in #{where}" unless String == mid[2]
1241
+ rest1, rest2 = rest.split(mid[1], 2)
1242
+ if rest2
1243
+ warn "@@@JOIN ok in #{ok.inspect} rest1 #{rest1.inspect}" if ENV["CDDL_TRACE"]
1244
+ ok &&= validate1(rest1, left[1])
1245
+ warn "@@@JOIN ok out #{ok.inspect}" if ENV["CDDL_TRACE"]
1246
+ rest = rest2
1247
+ ix += 1
1248
+ next
1249
+ else
1250
+ fail "Can't find #{mid[1].inspect} in #{rest.inspect}"
1251
+ end
1252
+ else
1253
+ fail "Cannot validate consecutive non-constant members of .join in #{where}"
1254
+ end
1255
+ else
1256
+ fail "Cannot handle .join over #{t[ix+1]} in #{where}"
1257
+ end
1258
+ end
1259
+ end
1260
+ else
1261
+ fail "Cannot handle .join over #{t[ix+1]} in #{where}"
1262
+ end
1263
+ end
1264
+ fail "Can't match #{rest.inspect} for .join in #{where}" if rest != ''
1265
+ ann if ok
1168
1266
  else
1169
1267
  fail "Don't know yet how to validate against #{where}"
1170
1268
  end
@@ -1173,7 +1271,7 @@ module CDDL
1173
1271
  String === d && (
1174
1272
  decoded = case conop
1175
1273
  when :b64u
1176
- /=/ !~ d &&
1274
+ /[+\/=]/ !~ d &&
1177
1275
  Base64.urlsafe_decode64(d)
1178
1276
  when :"b64u-sloppy"
1179
1277
  /[-_=]/ !~ d &&
@@ -1375,6 +1473,18 @@ module CDDL
1375
1473
  end
1376
1474
  end
1377
1475
 
1476
+ STRING_ESCAPES = {
1477
+ "\"" => "\"",
1478
+ "/" => "/",
1479
+ "\\" => "\\",
1480
+ "'" => "'",
1481
+ "b" => "\b",
1482
+ "f" => "\f",
1483
+ "n" => "\n",
1484
+ "r" => "\r",
1485
+ "t" => "\t",
1486
+ }
1487
+
1378
1488
  def value(n)
1379
1489
  # cheat:
1380
1490
  # warn n
@@ -1384,7 +1494,20 @@ module CDDL
1384
1494
  if parts[-1] != ""
1385
1495
  warn "*** Problem decoding byte string #{s.inspect}"
1386
1496
  end
1387
- bsval = parts[0...-1].join("'").gsub(/\\(.)/){$1}
1497
+ bsval = parts[0...-1].join("'").gsub(/\\u([Dd][89AaBb][0-9a-zA-Z]{2})\\u([Dd][CcDdEeFf][0-9a-zA-Z]{2})|\\u([0-9a-zA-Z]{4})|\\u\{([0-9a-zA-Z]+)\}|\r?(\n)|\\([^u])/) {
1498
+ if hex = $3 || $4
1499
+ hex.to_i(16).chr(Encoding::UTF_8)
1500
+ elsif lf = $5
1501
+ lf
1502
+ elsif escaped = $6
1503
+ STRING_ESCAPES[$6] or (
1504
+ fail "Invalid String Escape #{escaped.inspect} in #{s.inspect}"
1505
+ )
1506
+ else
1507
+ ((($1.to_i(16) & 0x3ff) << 10) +
1508
+ ($2.to_i(16) & 0x3ff) + 0x10000).chr(Encoding::UTF_8)
1509
+ end
1510
+ }
1388
1511
  [:bytes,
1389
1512
  case bsqual
1390
1513
  when ""
@@ -1398,7 +1521,17 @@ module CDDL
1398
1521
  end
1399
1522
  ]
1400
1523
  else
1401
- val = eval(n.to_s)
1524
+ if s[-1] == '"'
1525
+ s.gsub!(/\\u([Dd][89AaBb][0-9a-zA-Z]{2})\\u([Dd][CcDdEeFf][0-9a-zA-Z]{2})|\\([^u]|u[0-9a-zA-Z]{4})/) {
1526
+ if $3
1527
+ "\\#$3" # skip this, use eval parser
1528
+ else
1529
+ ((($1.to_i(16) & 0x3ff) << 10) +
1530
+ ($2.to_i(16) & 0x3ff) + 0x10000).chr(Encoding::UTF_8)
1531
+ end
1532
+ }
1533
+ end
1534
+ val = eval(s)
1402
1535
  # warn val
1403
1536
  case val
1404
1537
  when Integer; [:int, val]
@@ -0,0 +1,12 @@
1
+ a = text .b64u bytes
2
+
3
+ ;;gp 10
4
+
5
+ ;;vp "AAAA"
6
+ ;;vp "AAA-"
7
+ ;;vp "AAA_"
8
+ ; fail with 0.11.5:
9
+ ;;vp "AAA+"
10
+ ;;vp "AAA/"
11
+ ;;vp "AAA="
12
+ ;;vp "+kd8"
@@ -1,2 +1,4 @@
1
1
  foo = text .regexp myregexp
2
2
  myregexp = "A" .cat "B"
3
+
4
+ ;;gp 10
@@ -0,0 +1,8 @@
1
+ a1 = '' .cat a2
2
+ a2 = text .b64u ain
3
+ ; a = "e30" .b64u ain
4
+ ain = '{}'
5
+
6
+ ;;gp 3
7
+ ;;vp 'e30'
8
+ ;;vp "e30"
@@ -0,0 +1,3 @@
1
+ a = b<13..15>
2
+ b<T> = #7.<32 .plus T>
3
+ ; cddl dotplus1.cddl gp 10
@@ -0,0 +1,3 @@
1
+ a = d<17..19>
2
+ d<T> = #7.<T .plus 32>
3
+ ; cddl dotplus.cddl gp 10
@@ -0,0 +1,3 @@
1
+ a = d<17..19>
2
+ d<T> = 32.5 .plus T
3
+ ; cddl dotplus4.cddl gp 10
@@ -0,0 +1,3 @@
1
+ a = d<17..19>
2
+ d<T> = T .plus 32.5
3
+ ; cddl dotplus4.cddl gp 10
@@ -0,0 +1,14 @@
1
+ terminal-color = &basecolors
2
+ basecolors = (
3
+ black: 0, red: 1, green: 2, yellow: 3,
4
+ blue: 4, magenta: 5, cyan: 6, white: 7,
5
+ reserved: (8..255) .feature "extension"
6
+ )
7
+ ; extended-color = &(
8
+ ; basecolors,
9
+ ; orange: 8, pink: 9, purple: 10, brown: 11,
10
+ ; )
11
+
12
+ ;;gp 10
13
+ ;;vp 42
14
+ ;;vp 433
@@ -0,0 +1,12 @@
1
+ terminal-color = $terminal-color / text .feature "extension"
2
+ $terminal-color /= &basecolors
3
+ basecolors = (
4
+ black: "black", red: "red", green: "green",
5
+ )
6
+
7
+ pastelcolors = ( pink: "pink", mauve: "mauve", peach: "peach", lavender: "lavender" )
8
+ $terminal-color /= &pastelcolors
9
+
10
+ ;;gp 10
11
+ ;;vp "florp"
12
+ ;;vp "lavender"
@@ -0,0 +1,15 @@
1
+ ; JWT-JWS = text .join ([
2
+ ; b64u<jwt-headers>, ".",
3
+ ; b64u<jwt-payload>, ".",
4
+ ; b64u<jwt-signature>])
5
+ ; ; a = jwt-headers
6
+ ; ; a = b64u<"abc"> ; fails
7
+ ; ; a = b64u<'abc'> ; succeeds
8
+ ; b64u<B> = text .b64u B
9
+ jwt-headers = '' .cat jwt-headers1
10
+ jwt-headers1 = text .json jwt-headermap
11
+ jwt-headermap = { * text => any } ; simplified
12
+ jwt-payload = bytes
13
+ jwt-signature = bytes
14
+
15
+ ;;gp 10
@@ -0,0 +1,15 @@
1
+ JWT-JWS = text .join ([
2
+ b64u<jwt-headers>, ".",
3
+ b64u<jwt-payload>, ".",
4
+ b64u<jwt-signature>])
5
+ ; a = jwt-headers
6
+ ; a = b64u<"abc"> ; fails
7
+ ; a = b64u<'abc'> ; succeeds
8
+ b64u<B> = text .b64u B
9
+ jwt-headers = '' .cat jwt-headers1
10
+ jwt-headers1 = text .json jwt-headermap
11
+ jwt-headermap = { * text => any } ; simplified
12
+ jwt-payload = bytes
13
+ jwt-signature = bytes
14
+
15
+ ;;gp 10
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.11.1
4
+ version: 0.12.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carsten Bormann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-03-02 00:00:00.000000000 Z
11
+ date: 2024-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cbor-diag
@@ -163,6 +163,7 @@ files:
163
163
  - test-data/b.cddl
164
164
  - test-data/b64c-sloppy.cddl
165
165
  - test-data/b64c.cddl
166
+ - test-data/b64u-strict.cddl
166
167
  - test-data/b64u.cddl
167
168
  - test-data/badaddr.cddl
168
169
  - test-data/base32.cddl
@@ -173,6 +174,7 @@ files:
173
174
  - test-data/bpv7a.cddl
174
175
  - test-data/bpv7b.cddl
175
176
  - test-data/cat-re.cddl
177
+ - test-data/catb64.cddl
176
178
  - test-data/cdni-ct.cddl
177
179
  - test-data/complex-occ.cddl
178
180
  - test-data/coral.cddl
@@ -185,7 +187,13 @@ files:
185
187
  - test-data/decimal.cddl
186
188
  - test-data/decimal2.cddl
187
189
  - test-data/det1.cddl
190
+ - test-data/dotplus1.cddl
191
+ - test-data/dotplus2.cddl
192
+ - test-data/dotplus3.cddl
193
+ - test-data/dotplus4.cddl
188
194
  - test-data/dotsize.cddl
195
+ - test-data/enum.cddl
196
+ - test-data/enum1.cddl
189
197
  - test-data/extractor-demo.cddl
190
198
  - test-data/feat1.cddl
191
199
  - test-data/feature-controller.cddl
@@ -213,8 +221,10 @@ files:
213
221
  - test-data/joini.cddl
214
222
  - test-data/joinx.cddl
215
223
  - test-data/json.cddl
224
+ - test-data/json1.cddl
216
225
  - test-data/json2.cddl
217
226
  - test-data/jsoniodef.cddl
227
+ - test-data/jwt.cddl
218
228
  - test-data/kevin5.cddl
219
229
  - test-data/lint1.cddl
220
230
  - test-data/map-group.cddl
@@ -280,7 +290,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
280
290
  - !ruby/object:Gem::Version
281
291
  version: '0'
282
292
  requirements: []
283
- rubygems_version: 3.4.10
293
+ rubygems_version: 3.5.9
284
294
  signing_key:
285
295
  specification_version: 4
286
296
  summary: CDDL generator and validator.