teptris 0.3.20.0-arm-linux → 0.3.21.0-arm-linux

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: 9201c2f6c4e0da55e47d85c6b5533eee1da14cd5fabdce23b7fe9b9cccb341d3
4
- data.tar.gz: 5c9956a2c1e8c7943f144041f9457a8710d7064673fbe55a131c60fd9d3cc1a6
3
+ metadata.gz: 2a92c8bbeab70670630a68895102b94af510089405e13283825e83b79766f1da
4
+ data.tar.gz: 6b01ad69d3edbf50fe1d5a98ad9b79c8ca038df311a82cdb50780ce93334162c
5
5
  SHA512:
6
- metadata.gz: 2a650a24681268136a253ce11469681d038c9d31f6b9ccf693c0b317f19aebaf1b5aa4825530b79b6126db26f6bec584581598fb6024a0d975c951ed3b859d11
7
- data.tar.gz: 5c154830784c8ef1c11bdd1a5d953030ca10b3c149f6eceed114924dcdfcb79d78c2432be1a0bb8fea086401c8f18bf1986acb676cd99b3c387efae5410a1e97
6
+ metadata.gz: 540cf8e78add8e04bb870fc71b1f35dacf3551179579a034bb72a2662bfa07ce772a145610d886473bea316195f7cec8e45c8843fee2bf51e11814aad9a699b8
7
+ data.tar.gz: 64017c0f89bfa34e116cbbaadaa194a565e864cbf5ff8340d7d2d0f45c3ba4ba68a0c681ba58630ace0a9cb361d2e18b90c2483730cdbd015e2b31f4f2d53231
@@ -1,4 +1,4 @@
1
- # libteptris 0.3.20 — vendored source
1
+ # libteptris 0.3.21 — vendored source
2
2
 
3
3
  This directory carries the COMPLETE C source of the engine this
4
4
  gem's extension was built from (the packaging doctrine: compiled
@@ -3,7 +3,7 @@
3
3
 
4
4
  #define TEPTRIS_VERSION_MAJOR 0
5
5
  #define TEPTRIS_VERSION_MINOR 3
6
- #define TEPTRIS_VERSION_PATCH 20
7
- #define TEPTRIS_VERSION_STRING "0.3.20"
6
+ #define TEPTRIS_VERSION_PATCH 21
7
+ #define TEPTRIS_VERSION_STRING "0.3.21"
8
8
 
9
9
  #endif /* TEPTRIS_VERSION_H */
@@ -0,0 +1,656 @@
1
+ /* The format battery: the exact set of format names the jsonschema
2
+ * crate (taplo's validation engine, 0.17) asserts on, mirrored
3
+ * per-format from its implementations. Names outside this set are
4
+ * annotations on both sides. Only plain strings reach this file
5
+ * (TOML datetime values get their formats by kind, policy 3); every
6
+ * check is byte- and length-based because document strings may carry
7
+ * embedded NULs and UTF-8. */
8
+ #include <stdio.h>
9
+ #include <stdlib.h>
10
+ #include <string.h>
11
+
12
+ #include "schema_internal.h"
13
+
14
+ static bool is_digit(char c) { return c >= '0' && c <= '9'; }
15
+
16
+ static bool is_hex(char c) {
17
+ return is_digit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
18
+ }
19
+
20
+ static bool is_ascii_alnum(char c) {
21
+ return is_digit(c) || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
22
+ }
23
+
24
+ static bool all_digits(const char *s, size_t from, size_t n) {
25
+ for (size_t i = from; i < from + n; i++) {
26
+ if (!is_digit(s[i])) return false;
27
+ }
28
+ return true;
29
+ }
30
+
31
+ static int two_digits(const char *s, size_t from) {
32
+ return (s[from] - '0') * 10 + (s[from + 1] - '0');
33
+ }
34
+
35
+ static size_t cp_count(const char *s, size_t len) {
36
+ size_t count = 0;
37
+ size_t i = 0;
38
+ while (i < len) {
39
+ unsigned char b = (unsigned char)s[i];
40
+ size_t step = b < 0x80 ? 1
41
+ : b < 0xE0 ? 2
42
+ : b < 0xF0 ? 3
43
+ : 4;
44
+ i += step > len - i ? 1 : step;
45
+ count++;
46
+ }
47
+ return count;
48
+ }
49
+
50
+ static bool is_ascii_space(char c) {
51
+ return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\v' ||
52
+ c == '\f';
53
+ }
54
+
55
+ /* ------------------------------------------------- date / time atoms -- */
56
+
57
+ static bool leap_year(int y) {
58
+ return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
59
+ }
60
+
61
+ static int days_in_month(int y, int m) {
62
+ static const int dim[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
63
+ if (m == 2 && leap_year(y)) return 29;
64
+ return dim[m - 1];
65
+ }
66
+
67
+ /* YYYY-MM-DD with real calendar ranges (the crate parses the date,
68
+ * then re-checks the zero-padded shape; a strict digits+range check
69
+ * answers both) */
70
+ static bool check_date(const char *s, size_t len) {
71
+ if (len != 10 || s[4] != '-' || s[7] != '-') return false;
72
+ if (!all_digits(s, 0, 4) || !all_digits(s, 5, 2) ||
73
+ !all_digits(s, 8, 2)) {
74
+ return false;
75
+ }
76
+ int y = (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 +
77
+ (s[3] - '0');
78
+ int m = two_digits(s, 5);
79
+ int d = two_digits(s, 8);
80
+ return m >= 1 && m <= 12 && d >= 1 && d <= days_in_month(y, m);
81
+ }
82
+
83
+ /* HH:MM:SS with ranges; answers the consumed length (8) or 0 */
84
+ static size_t check_hms(const char *s, size_t len) {
85
+ if (len < 8 || s[2] != ':' || s[5] != ':') return 0;
86
+ if (!all_digits(s, 0, 2) || !all_digits(s, 3, 2) ||
87
+ !all_digits(s, 6, 2)) {
88
+ return 0;
89
+ }
90
+ int h = two_digits(s, 0);
91
+ int m = two_digits(s, 3);
92
+ int sec = two_digits(s, 6);
93
+ if (h > 23 || m > 59 || sec > 59) return 0;
94
+ return 8;
95
+ }
96
+
97
+ /* the offset tail: Z/z or +HH:MM / -HH:MM; answers its length or 0 */
98
+ static size_t check_offset(const char *s, size_t len) {
99
+ if (len == 0) return 0;
100
+ if (s[0] == 'Z' || s[0] == 'z') return 1;
101
+ if (s[0] != '+' && s[0] != '-') return 0;
102
+ if (len < 6 || s[3] != ':') return 0;
103
+ if (!all_digits(s, 1, 2) || !all_digits(s, 4, 2)) return 0;
104
+ int h = two_digits(s, 1);
105
+ int m = two_digits(s, 4);
106
+ return h <= 23 && m <= 59 ? 6 : 0;
107
+ }
108
+
109
+ /* full-date "T" full-time: the offset is mandatory (RFC 3339, and the
110
+ * crate parses with its RFC 3339 parser); lowercase t/z allowed */
111
+ static bool check_datetime(const char *s, size_t len) {
112
+ if (len < 20) return false;
113
+ if (!check_date(s, 10)) return false;
114
+ if (s[10] != 'T' && s[10] != 't') return false;
115
+ size_t used = check_hms(s + 11, len - 11);
116
+ if (used == 0) return false;
117
+ size_t pos = 11 + used;
118
+ if (pos < len && s[pos] == '.') {
119
+ size_t frac = 0;
120
+ pos++;
121
+ while (pos < len && is_digit(s[pos])) {
122
+ pos++;
123
+ frac++;
124
+ }
125
+ if (frac == 0) return false;
126
+ }
127
+ return len > pos && check_offset(s + pos, len - pos) == len - pos;
128
+ }
129
+
130
+ /* ------------------------------------------------------- rfc atoms -- */
131
+
132
+ /* the crate's email check: a leading dot fails, ".." fails, a dot
133
+ * directly before "@" fails, an "@" ends the (unvalidated) domain;
134
+ * everything else about the domain is accepted */
135
+ static bool check_email(const char *s, size_t len) {
136
+ if (len == 0 || s[0] == '.') return false;
137
+ for (size_t i = 1; i < len; i++) {
138
+ if (s[i] == '@') {
139
+ return s[i - 1] != '.';
140
+ }
141
+ if (s[i] == '.' && s[i - 1] == '.') return false;
142
+ }
143
+ return false; /* no @ */
144
+ }
145
+
146
+ /* the crate's hostname/idn-hostname check (one body for both):
147
+ * nonempty, at most 255 characters, only alphanumerics, '-' and '.',
148
+ * no leading/trailing '-', labels of at most 63 characters. The
149
+ * crate's alphanumeric test is Unicode-wide; the practical subset
150
+ * here is ASCII (flagged as a divergence in SCHEMAS.md) */
151
+ static bool check_hostname(const char *s, size_t len) {
152
+ if (len == 0 || s[0] == '-' || s[len - 1] == '-') return false;
153
+ if (cp_count(s, len) > 255) return false;
154
+ for (size_t i = 0; i < len; i++) {
155
+ char c = s[i];
156
+ if (!(is_ascii_alnum(c) || c == '-' || c == '.')) return false;
157
+ }
158
+ size_t label = 0;
159
+ for (size_t i = 0; i <= len; i++) {
160
+ if (i == len || s[i] == '.') {
161
+ if (cp_count(s + i - label, label) > 63) return false;
162
+ label = 0;
163
+ } else {
164
+ label++;
165
+ }
166
+ }
167
+ return true;
168
+ }
169
+
170
+ /* dotted quad, each group 1-3 digits without a leading zero (unless
171
+ * the group is exactly "0"), at most 255 */
172
+ static bool check_ipv4(const char *s, size_t len) {
173
+ size_t pos = 0;
174
+ for (int g = 0; g < 4; g++) {
175
+ if (g > 0) {
176
+ if (pos >= len || s[pos] != '.') return false;
177
+ pos++;
178
+ }
179
+ size_t start = pos;
180
+ while (pos < len && is_digit(s[pos])) pos++;
181
+ size_t n = pos - start;
182
+ if (n < 1 || n > 3) return false;
183
+ if (n > 1 && s[start] == '0') return false;
184
+ int v = 0;
185
+ for (size_t i = start; i < pos; i++) v = v * 10 + (s[i] - '0');
186
+ if (v > 255) return false;
187
+ }
188
+ return pos == len;
189
+ }
190
+
191
+ /* IPv6 per the std parser the crate uses: 8 groups of 1-4 hex digits
192
+ * with one optional "::" compression and an optional IPv4 tail */
193
+ static bool group_ok(const char *t, size_t n) {
194
+ if (n < 1 || n > 4) return false;
195
+ for (size_t i = 0; i < n; i++) {
196
+ if (!is_hex(t[i])) return false;
197
+ }
198
+ return true;
199
+ }
200
+
201
+ static bool check_ipv6(const char *s, size_t len) {
202
+ const char *tok[9];
203
+ size_t tl[9];
204
+ size_t n = 0, start = 0;
205
+ for (size_t i = 0; i <= len; i++) {
206
+ if (i == len || s[i] == ':') {
207
+ if (n >= 9) return false;
208
+ tok[n] = s + start;
209
+ tl[n] = i - start;
210
+ n++;
211
+ start = i + 1;
212
+ }
213
+ }
214
+ size_t empty_pos[9], ne = 0;
215
+ for (size_t i = 0; i < n; i++) {
216
+ if (tl[i] == 0) empty_pos[ne++] = i;
217
+ }
218
+ bool v4 = tl[n - 1] > 0 && memchr(tok[n - 1], '.', tl[n - 1]) != NULL;
219
+ if (ne == 0) {
220
+ if (n != (v4 ? 7 : 8)) return false;
221
+ } else if (ne == 1) {
222
+ /* a single empty token is a mid-string "::" ("a::b"); at the
223
+ * ends it would be a lone leading/trailing colon */
224
+ if (empty_pos[0] == 0 || empty_pos[0] == n - 1) return false;
225
+ } else if (ne == 2) {
226
+ if (empty_pos[1] != empty_pos[0] + 1) return false;
227
+ } else if (ne == 3) {
228
+ if (n != 3) return false; /* the bare "::" */
229
+ } else {
230
+ return false;
231
+ }
232
+ size_t groups = 0;
233
+ for (size_t i = 0; i < n; i++) {
234
+ if (tl[i] == 0) continue;
235
+ if (i == n - 1 && v4) {
236
+ if (!check_ipv4(tok[i], tl[i])) return false;
237
+ groups += 2;
238
+ } else {
239
+ if (!group_ok(tok[i], tl[i])) return false;
240
+ groups += 1;
241
+ }
242
+ }
243
+ return ne == 0 ? groups == 8 : groups <= 7;
244
+ }
245
+
246
+ /* absolute URL, the practical shape of the url-crate parse: a scheme
247
+ * (letter-first, alnum/+/-/.), a colon, and no control characters or
248
+ * spaces anywhere */
249
+ static bool check_uri(const char *s, size_t len) {
250
+ size_t i = 0;
251
+ if (len == 0 || !((s[0] >= 'a' && s[0] <= 'z') ||
252
+ (s[0] >= 'A' && s[0] <= 'Z'))) {
253
+ return false;
254
+ }
255
+ for (i = 1; i < len; i++) {
256
+ char c = s[i];
257
+ if (c == ':') break;
258
+ if (!(is_ascii_alnum(c) || c == '+' || c == '-' || c == '.')) {
259
+ return false;
260
+ }
261
+ }
262
+ if (i == len || i == 0) return false;
263
+ for (size_t k = 0; k < len; k++) {
264
+ unsigned char c = (unsigned char)s[k];
265
+ if (c < 0x21 || c == 0x7F) return false; /* controls + space */
266
+ }
267
+ return true;
268
+ }
269
+
270
+ /* URI-reference (and iri-reference): the crate's regex reduces to
271
+ * "at most one '#', no backslash, no whitespace" - the optional
272
+ * scheme part cannot make an otherwise-valid string fail because the
273
+ * colon is legal in the reference body */
274
+ static bool check_uri_reference(const char *s, size_t len) {
275
+ bool hash = false;
276
+ for (size_t i = 0; i < len; i++) {
277
+ char c = s[i];
278
+ if (c == '\\') return false;
279
+ if (is_ascii_space(c)) return false;
280
+ if (c == '#') {
281
+ if (hash) return false;
282
+ hash = true;
283
+ }
284
+ }
285
+ return true;
286
+ }
287
+
288
+ /* JSON pointer (RFC 6901): empty, or '/'-led with '~' only in the
289
+ * escapes ~0/~1 */
290
+ static bool check_json_pointer(const char *s, size_t len) {
291
+ if (len == 0) return true;
292
+ if (s[0] != '/') return false;
293
+ for (size_t i = 1; i < len; i++) {
294
+ if (s[i] == '~') {
295
+ if (i + 1 >= len || (s[i + 1] != '0' && s[i + 1] != '1')) {
296
+ return false;
297
+ }
298
+ i++;
299
+ }
300
+ }
301
+ return true;
302
+ }
303
+
304
+ /* relative-json-pointer: non-negative base-10 index without a
305
+ * leading zero, then "#" or a '/'-led pointer tail */
306
+ static bool check_relative_json_pointer(const char *s, size_t len) {
307
+ size_t i = 0;
308
+ if (len == 0) return false;
309
+ if (s[0] == '0') {
310
+ i = 1;
311
+ } else if (s[0] >= '1' && s[0] <= '9') {
312
+ i = 1;
313
+ while (i < len && is_digit(s[i])) i++;
314
+ } else {
315
+ return false;
316
+ }
317
+ if (i == len) return false;
318
+ if (s[i] == '#') return i + 1 == len;
319
+ if (s[i] != '/') return false;
320
+ return check_json_pointer(s + i, len - i);
321
+ }
322
+
323
+ /* uuid: the crate's FromStr accepts the simple (32 hex) and
324
+ * hyphenated (8-4-4-4-12) spellings, case-insensitive */
325
+ static bool check_uuid(const char *s, size_t len) {
326
+ if (len != 32 && len != 36) return false;
327
+ size_t hex_seen = 0;
328
+ for (size_t i = 0; i < len; i++) {
329
+ if (len == 36 && (i == 8 || i == 13 || i == 18 || i == 23)) {
330
+ if (s[i] != '-') return false;
331
+ continue;
332
+ }
333
+ if (!is_hex(s[i])) return false;
334
+ hex_seen++;
335
+ }
336
+ return hex_seen == 32;
337
+ }
338
+
339
+ /* ISO 8601 duration, the practical core the iso8601 crate accepts:
340
+ * an optional sign, P, then at least one nY/nM/nW/nD component and an
341
+ * optional T-section of nH/nM/nS; a fraction is allowed on any unit */
342
+ static bool check_duration(const char *s, size_t len) {
343
+ size_t i = 0;
344
+ if (len == 0) return false;
345
+ if (s[0] == '+' || s[0] == '-') i++;
346
+ if (i >= len || s[i] != 'P') return false;
347
+ i++;
348
+ bool any = false;
349
+ bool in_time = false;
350
+ while (i < len) {
351
+ if (!in_time && s[i] == 'T') {
352
+ in_time = true;
353
+ i++;
354
+ continue;
355
+ }
356
+ size_t start = i;
357
+ while (i < len && is_digit(s[i])) i++;
358
+ if (i == start) return false;
359
+ if (i < len && (s[i] == '.' || s[i] == ',')) {
360
+ i++;
361
+ size_t fs = i;
362
+ while (i < len && is_digit(s[i])) i++;
363
+ if (i == fs) return false;
364
+ }
365
+ if (i >= len) return false;
366
+ char unit = s[i];
367
+ i++;
368
+ if (unit == 'Y' || unit == 'W' || unit == 'D') {
369
+ if (in_time) return false;
370
+ } else if (unit == 'M') {
371
+ /* months before T, minutes after; both fine */
372
+ } else if (unit == 'H' || unit == 'S') {
373
+ if (!in_time) return false;
374
+ } else {
375
+ return false;
376
+ }
377
+ any = true;
378
+ }
379
+ return any;
380
+ }
381
+
382
+ /* ----------------------------------------------------- uri-template -- */
383
+
384
+ /* the crate's template regex: literal characters (nothing in the
385
+ * control/space range or "'<>%\^`{|}) or %XX pairs, plus {…}
386
+ * expressions with an optional operator, comma-separated variable
387
+ * names (word characters or %XX) and :N or * modifiers. Uppercase
388
+ * hex and uppercase variable letters are accepted here (a superset
389
+ * of the crate's lowercase-only spellings) */
390
+ static bool check_uri_template(const char *s, size_t len) {
391
+ size_t i = 0;
392
+ while (i < len) {
393
+ char c = s[i];
394
+ if (c == '%') {
395
+ if (i + 2 >= len || !is_hex(s[i + 1]) || !is_hex(s[i + 2])) {
396
+ return false;
397
+ }
398
+ i += 3;
399
+ continue;
400
+ }
401
+ if (c == '{') {
402
+ i++;
403
+ if (i < len && s[i] != '\0' &&
404
+ strchr("+./;?&=,!@|", s[i]) != NULL) {
405
+ i++;
406
+ }
407
+ bool varspec = false;
408
+ while (i < len && s[i] != '}') {
409
+ /* one varspec: name (:N or *)? */
410
+ size_t name = 0;
411
+ while (i < len && s[i] != '}' && s[i] != ',' &&
412
+ s[i] != ':' && s[i] != '*') {
413
+ if (s[i] == '%') {
414
+ if (i + 2 >= len || !is_hex(s[i + 1]) ||
415
+ !is_hex(s[i + 2])) {
416
+ return false;
417
+ }
418
+ i += 3;
419
+ } else if (is_ascii_alnum(s[i]) || s[i] == '_') {
420
+ i++;
421
+ } else {
422
+ return false;
423
+ }
424
+ name++;
425
+ }
426
+ if (name == 0) return false;
427
+ if (i < len && s[i] == ':') {
428
+ i++;
429
+ size_t ds = i;
430
+ while (i < len && is_digit(s[i])) i++;
431
+ size_t dn = i - ds;
432
+ if (dn < 1 || dn > 4) return false;
433
+ if (s[ds] == '0') return false;
434
+ } else if (i < len && s[i] == '*') {
435
+ i++;
436
+ }
437
+ varspec = true;
438
+ if (i < len && s[i] == ',') {
439
+ i++;
440
+ }
441
+ }
442
+ if (i >= len || !varspec) return false;
443
+ i++; /* the closing brace */
444
+ continue;
445
+ }
446
+ unsigned char b = (unsigned char)c;
447
+ if (b <= 0x20 || b == 0x7F || c == '"' || c == '\'' || c == '<' ||
448
+ c == '>' || c == '\\' || c == '^' || c == '`' || c == '|' ||
449
+ c == '}') {
450
+ return false;
451
+ }
452
+ i++;
453
+ }
454
+ return true;
455
+ }
456
+
457
+ /* --------------------------------------------------------- semver -- */
458
+
459
+ /* numeric identifier: "0" or [1-9][0-9]* (no leading zeros) */
460
+ static bool semver_numeric(const char *s, size_t len) {
461
+ if (len == 0 || len > 19) return false;
462
+ if (s[0] == '0' && len > 1) return false;
463
+ return all_digits(s, 0, len);
464
+ }
465
+
466
+ static bool pre_ident_ok(const char *s, size_t len) {
467
+ for (size_t i = 0; i < len; i++) {
468
+ char c = s[i];
469
+ if (!(is_ascii_alnum(c) || c == '-')) return false;
470
+ }
471
+ return len > 0;
472
+ }
473
+
474
+ /* a dot-separated identifier list; numeric identifiers must not
475
+ * carry leading zeros (semver spec 9/11) */
476
+ static bool pre_list_ok(const char *s, size_t len) {
477
+ size_t start = 0;
478
+ for (size_t i = 0; i <= len; i++) {
479
+ if (i == len || s[i] == '.') {
480
+ size_t n = i - start;
481
+ if (!pre_ident_ok(s + start, n)) return false;
482
+ bool numeric = true;
483
+ for (size_t k = 0; k < n; k++) {
484
+ if (!is_digit(s[start + k])) numeric = false;
485
+ }
486
+ if (numeric && !semver_numeric(s + start, n)) return false;
487
+ start = i + 1;
488
+ }
489
+ }
490
+ return true;
491
+ }
492
+
493
+ /* MAJOR.MINOR.PATCH, then an optional -prerelease and +build */
494
+ static bool check_semver(const char *s, size_t len) {
495
+ size_t num_start[3] = {0, 0, 0}, num_len[3] = {0, 0, 0};
496
+ size_t i = 0;
497
+ for (int part = 0; part < 3; part++) {
498
+ num_start[part] = i;
499
+ while (i < len && is_digit(s[i])) i++;
500
+ num_len[part] = i - num_start[part];
501
+ if (!semver_numeric(s + num_start[part], num_len[part])) return false;
502
+ if (part < 2) {
503
+ if (i >= len || s[i] != '.') return false;
504
+ i++;
505
+ }
506
+ }
507
+ if (i == len) return true;
508
+ if (s[i] == '-') {
509
+ i++;
510
+ size_t start = i;
511
+ while (i < len && s[i] != '+') i++;
512
+ if (!pre_list_ok(s + start, i - start)) return false;
513
+ }
514
+ if (i < len) {
515
+ if (s[i] != '+') return false;
516
+ i++;
517
+ size_t start = i;
518
+ while (i < len && s[i] != '+') i++;
519
+ if (i != len) return false; /* a second '+' is invalid */
520
+ if (!pre_list_ok(s + start, len - start)) return false;
521
+ }
522
+ return i == len;
523
+ }
524
+
525
+ /* semver requirement (the semver crate's VersionReq grammar): a
526
+ * comma-separated comparator list; each is a version with an
527
+ * optional operator (= > >= < <= ^ ~; bare means ^), where MINOR or
528
+ * PATCH may be a wildcard (*, x, X), or a bare wildcard */
529
+ static bool check_semver_req(const char *s, size_t len) {
530
+ size_t i = 0;
531
+ bool any = false; /* at least one comparator */
532
+ while (i < len) {
533
+ while (i < len && (s[i] == ' ' || s[i] == '\t')) i++;
534
+ if (i == len) break;
535
+ if (s[i] == ',') return false; /* empty comparator */
536
+ /* operator */
537
+ if (s[i] == '=' || s[i] == '>' || s[i] == '<' || s[i] == '^' ||
538
+ s[i] == '~') {
539
+ i++;
540
+ if (i < len && (s[i] == '=')) i++; /* >= or <= */
541
+ }
542
+ while (i < len && (s[i] == ' ' || s[i] == '\t')) i++;
543
+ if (i >= len) return false;
544
+ /* MAJOR */
545
+ size_t start = i;
546
+ while (i < len && is_digit(s[i])) i++;
547
+ if (i > start) {
548
+ if (!semver_numeric(s + start, i - start)) return false;
549
+ } else if (s[i] == '*' || s[i] == 'x' || s[i] == 'X') {
550
+ i++;
551
+ any = true;
552
+ /* a bare wildcard ends the comparator */
553
+ while (i < len && (s[i] == ' ' || s[i] == '\t')) i++;
554
+ if (i == len) return true;
555
+ if (s[i] != ',') return false;
556
+ i++;
557
+ continue;
558
+ } else {
559
+ return false;
560
+ }
561
+ any = true;
562
+ /* optional .MINOR */
563
+ if (i < len && s[i] == '.') {
564
+ i++;
565
+ start = i;
566
+ while (i < len && is_digit(s[i])) i++;
567
+ if (i > start) {
568
+ if (!semver_numeric(s + start, i - start)) return false;
569
+ } else if (i < len && (s[i] == '*' || s[i] == 'x' ||
570
+ s[i] == 'X')) {
571
+ i++;
572
+ any = true;
573
+ } else {
574
+ return false;
575
+ }
576
+ /* optional .PATCH */
577
+ if (i < len && s[i] == '.') {
578
+ i++;
579
+ start = i;
580
+ while (i < len && is_digit(s[i])) i++;
581
+ if (i > start) {
582
+ if (!semver_numeric(s + start, i - start)) return false;
583
+ } else if (i < len && (s[i] == '*' || s[i] == 'x' ||
584
+ s[i] == 'X')) {
585
+ i++;
586
+ any = true;
587
+ } else {
588
+ return false;
589
+ }
590
+ /* a full version may carry -prerelease */
591
+ if (i < len && s[i] == '-') {
592
+ i++;
593
+ size_t ps = i;
594
+ while (i < len && s[i] != '+') i++;
595
+ if (!pre_list_ok(s + ps, i - ps)) return false;
596
+ }
597
+ }
598
+ }
599
+ while (i < len && (s[i] == ' ' || s[i] == '\t')) i++;
600
+ if (i == len) break;
601
+ if (s[i] != ',') return false;
602
+ i++;
603
+ }
604
+ return any;
605
+ }
606
+
607
+ /* ----------------------------------------------------------- entry -- */
608
+
609
+ bool sch_format_string_check(int format, const char *s, size_t len,
610
+ teptris_arena *scratch) {
611
+ switch (format) {
612
+ case FMT_DATE: return check_date(s, len);
613
+ case FMT_DATETIME: return check_datetime(s, len);
614
+ case FMT_TIME: {
615
+ size_t used = check_hms(s, len);
616
+ if (used == 0) return false;
617
+ size_t pos = used;
618
+ if (pos < len && s[pos] == '.') {
619
+ size_t frac = 0;
620
+ pos++;
621
+ while (pos < len && is_digit(s[pos])) {
622
+ pos++;
623
+ frac++;
624
+ }
625
+ if (frac != 6) return false; /* the crate's exact 6 digits */
626
+ }
627
+ return len > pos && check_offset(s + pos, len - pos) == len - pos;
628
+ }
629
+ case FMT_EMAIL:
630
+ case FMT_IDN_EMAIL: return check_email(s, len);
631
+ case FMT_HOSTNAME:
632
+ case FMT_IDN_HOSTNAME: return check_hostname(s, len);
633
+ case FMT_IPV4: return check_ipv4(s, len);
634
+ case FMT_IPV6: return check_ipv6(s, len);
635
+ case FMT_URI:
636
+ case FMT_IRI: return check_uri(s, len);
637
+ case FMT_URI_REFERENCE:
638
+ case FMT_IRI_REFERENCE: return check_uri_reference(s, len);
639
+ case FMT_JSON_POINTER: return check_json_pointer(s, len);
640
+ case FMT_RELATIVE_JSON_POINTER:
641
+ return check_relative_json_pointer(s, len);
642
+ case FMT_REGEX: {
643
+ char rerr[128] = {0};
644
+ /* a compile trial in the engine's own NFA: the dialect is
645
+ * teptris's (backreferences and lookaround reject here even
646
+ * though the crate's engine takes them) */
647
+ return sch_regex_compile(scratch, s, len, rerr, sizeof rerr) != NULL;
648
+ }
649
+ case FMT_URI_TEMPLATE: return check_uri_template(s, len);
650
+ case FMT_UUID: return check_uuid(s, len);
651
+ case FMT_DURATION: return check_duration(s, len);
652
+ case FMT_SEMVER: return check_semver(s, len);
653
+ case FMT_SEMVER_REQUIREMENT: return check_semver_req(s, len);
654
+ default: return true;
655
+ }
656
+ }
@@ -1,6 +1,7 @@
1
1
  /* Schema compilation: JSON Schema text -> a compiled sch_node graph.
2
2
  * See schema.h for the policy contract. Unsupported keywords that
3
3
  * would silently change results are rejected here, at compile. */
4
+ #include <math.h>
4
5
  #include <stdio.h>
5
6
  #include <stdlib.h>
6
7
  #include <string.h>
@@ -156,6 +157,25 @@ static int format_kind(const char *name) {
156
157
  if (strcmp(name, "date-time") == 0) return FMT_DATETIME;
157
158
  if (strcmp(name, "date") == 0) return FMT_DATE;
158
159
  if (strcmp(name, "time") == 0) return FMT_TIME;
160
+ if (strcmp(name, "email") == 0) return FMT_EMAIL;
161
+ if (strcmp(name, "idn-email") == 0) return FMT_IDN_EMAIL;
162
+ if (strcmp(name, "hostname") == 0) return FMT_HOSTNAME;
163
+ if (strcmp(name, "idn-hostname") == 0) return FMT_IDN_HOSTNAME;
164
+ if (strcmp(name, "ipv4") == 0) return FMT_IPV4;
165
+ if (strcmp(name, "ipv6") == 0) return FMT_IPV6;
166
+ if (strcmp(name, "uri") == 0) return FMT_URI;
167
+ if (strcmp(name, "iri") == 0) return FMT_IRI;
168
+ if (strcmp(name, "uri-reference") == 0) return FMT_URI_REFERENCE;
169
+ if (strcmp(name, "iri-reference") == 0) return FMT_IRI_REFERENCE;
170
+ if (strcmp(name, "json-pointer") == 0) return FMT_JSON_POINTER;
171
+ if (strcmp(name, "relative-json-pointer") == 0)
172
+ return FMT_RELATIVE_JSON_POINTER;
173
+ if (strcmp(name, "regex") == 0) return FMT_REGEX;
174
+ if (strcmp(name, "uri-template") == 0) return FMT_URI_TEMPLATE;
175
+ if (strcmp(name, "uuid") == 0) return FMT_UUID;
176
+ if (strcmp(name, "duration") == 0) return FMT_DURATION;
177
+ if (strcmp(name, "semver") == 0) return FMT_SEMVER;
178
+ if (strcmp(name, "semver-requirement") == 0) return FMT_SEMVER_REQUIREMENT;
159
179
  return FMT_NONE; /* other formats: annotations (host's choice) */
160
180
  }
161
181
 
@@ -199,6 +219,58 @@ static bool compile_type(sctx *c, sch_node *n, jv *tv, const char *where) {
199
219
  return false;
200
220
  }
201
221
 
222
+ static bool grow_dep_list(sctx *c, sch_kschema **list, size_t *n, size_t *cap) {
223
+ if (*n == *cap) {
224
+ *cap = *cap == 0 ? 4 : *cap * 2;
225
+ sch_kschema *nl =
226
+ teptris_arena_alloc(c->a, *cap * sizeof(**list));
227
+ if (*n > 0) memcpy(nl, *list, *n * sizeof(**list));
228
+ *list = nl;
229
+ }
230
+ return true;
231
+ }
232
+
233
+ /* one "key": [names...] pair (dependentRequired, or dependencies'
234
+ * array form): the fake node reuses the required storage shape */
235
+ static bool sch_compile_dep_req(sctx *c, sch_node *n, const char *key,
236
+ jv *list) {
237
+ grow_dep_list(c, &n->dep_req, &n->n_dep_req, &c->cap_dep_req);
238
+ sch_node *fake = sch_node_new(c);
239
+ size_t dn = jv_item_count(list);
240
+ fake->required = teptris_arena_alloc(c->a, (dn ? dn : 1) * sizeof(char *));
241
+ for (size_t d = 0; d < dn; d++) {
242
+ jv *e = jv_item(list, d);
243
+ if (jv_kind_of(e) != JV_STRING) {
244
+ sfail(c, "dependency entries must be strings", key);
245
+ return false;
246
+ }
247
+ size_t slen = jv_str_len(e);
248
+ char *rb = teptris_arena_alloc(c->a, slen + 1);
249
+ memcpy(rb, jv_str(e), slen);
250
+ rb[slen] = '\0';
251
+ fake->required[d] = rb;
252
+ }
253
+ fake->n_req = dn;
254
+ n->dep_req[n->n_dep_req].key = key;
255
+ n->dep_req[n->n_dep_req].schema = fake;
256
+ n->n_dep_req++;
257
+ return true;
258
+ }
259
+
260
+ /* one "key": {schema} pair (dependentSchemas, or dependencies'
261
+ * schema form; a boolean is a schema) */
262
+ static bool sch_compile_dep_schema(sctx *c, sch_node *n, const char *key,
263
+ jv *dv) {
264
+ grow_dep_list(c, &n->dep_schemas, &n->n_dep_schemas,
265
+ &c->cap_dep_schemas);
266
+ sch_node *s = sch_compile_node(c, dv);
267
+ if (s == NULL && c->err[0] != '\0') return false;
268
+ n->dep_schemas[n->n_dep_schemas].key = key;
269
+ n->dep_schemas[n->n_dep_schemas].schema = s;
270
+ n->n_dep_schemas++;
271
+ return true;
272
+ }
273
+
202
274
  sch_node *sch_compile_node(sctx *c, const jv *v) {
203
275
  bool found, busy;
204
276
  sch_node *memo_hit = memo_get(c, v, &found, &busy);
@@ -331,64 +403,92 @@ sch_node *sch_compile_node(sctx *c, const jv *v) {
331
403
  jv_kind_of(val) == JV_INT) {
332
404
  n->max_props = (long)jv_int(val);
333
405
  n->has_max_props = true;
406
+ } else if (strcmp(k, "dependencies") == 0 &&
407
+ jv_kind_of(val) == JV_OBJECT) {
408
+ /* draft-07 dependencies: an array value is a
409
+ * dependentRequired pair, a schema value a
410
+ * dependentSchemas pair */
411
+ size_t cnt = jv_member_count(val);
412
+ for (size_t p = 0; p < cnt; p++) {
413
+ jv *dv = jv_member_val(val, p);
414
+ if (jv_kind_of(dv) == JV_ARRAY) {
415
+ if (!sch_compile_dep_req(c, n, jv_member_key(val, p), dv))
416
+ return NULL;
417
+ } else if (jv_kind_of(dv) == JV_OBJECT ||
418
+ jv_kind_of(dv) == JV_BOOL) {
419
+ if (!sch_compile_dep_schema(c, n, jv_member_key(val, p),
420
+ dv))
421
+ return NULL;
422
+ } else {
423
+ sfail(c, "dependencies values must be arrays or schemas",
424
+ k);
425
+ return NULL;
426
+ }
427
+ }
334
428
  } else if (strcmp(k, "dependentRequired") == 0 &&
335
429
  jv_kind_of(val) == JV_OBJECT) {
336
430
  size_t cnt = jv_member_count(val);
337
- n->dep_req =
338
- teptris_arena_alloc(c->a, (cnt ? cnt : 1) * sizeof(sch_kschema));
339
431
  for (size_t p = 0; p < cnt; p++) {
340
432
  jv *list = jv_member_val(val, p);
341
433
  if (jv_kind_of(list) != JV_ARRAY) {
342
434
  sfail(c, "dependentRequired values must be arrays", k);
343
435
  return NULL;
344
436
  }
345
- n->dep_req[p].key = jv_member_key(val, p);
346
- /* reuse the required storage shape: an sch_node whose
347
- * required list carries the dependency keys */
348
- sch_node *fake = sch_node_new(c);
349
- size_t dn = jv_item_count(list);
350
- fake->required =
351
- teptris_arena_alloc(c->a, (dn ? dn : 1) * sizeof(char *));
352
- for (size_t d = 0; d < dn; d++) {
353
- jv *e = jv_item(list, d);
354
- if (jv_kind_of(e) != JV_STRING) {
355
- sfail(c, "dependentRequired entries must be strings", k);
356
- return NULL;
357
- }
358
- size_t slen = jv_str_len(e);
359
- char *rb = teptris_arena_alloc(c->a, slen + 1);
360
- memcpy(rb, jv_str(e), slen);
361
- rb[slen] = '\0';
362
- fake->required[d] = rb;
363
- }
364
- fake->n_req = dn;
365
- n->dep_req[p].schema = fake;
437
+ if (!sch_compile_dep_req(c, n, jv_member_key(val, p), list))
438
+ return NULL;
366
439
  }
367
- n->n_dep_req = cnt;
368
440
  } else if (strcmp(k, "dependentSchemas") == 0 &&
369
441
  jv_kind_of(val) == JV_OBJECT) {
370
442
  size_t cnt = jv_member_count(val);
371
- n->dep_schemas = teptris_arena_alloc(
372
- c->a, (cnt ? cnt : 1) * sizeof(sch_kschema));
373
443
  for (size_t p = 0; p < cnt; p++) {
374
- n->dep_schemas[p].key = jv_member_key(val, p);
375
- n->dep_schemas[p].schema = sch_compile_node(c, jv_member_val(val, p));
376
- if (n->dep_schemas[p].schema == NULL && c->err[0] != '\0') return NULL;
444
+ if (!sch_compile_dep_schema(c, n, jv_member_key(val, p),
445
+ jv_member_val(val, p)))
446
+ return NULL;
447
+ }
448
+ } else if (strcmp(k, "contentEncoding") == 0 &&
449
+ jv_kind_of(val) == JV_STRING) {
450
+ n->content_encoding = jv_str(val);
451
+ } else if (strcmp(k, "contentMediaType") == 0 &&
452
+ jv_kind_of(val) == JV_STRING) {
453
+ n->content_media_type = jv_str(val);
454
+ } else if (strcmp(k, "items") == 0 && jv_kind_of(val) == JV_ARRAY) {
455
+ /* draft-07 tuple form: positional prefixes; the union
456
+ * reads them exactly like prefixItems, with
457
+ * additionalItems covering anything beyond */
458
+ size_t cnt = jv_item_count(val);
459
+ size_t base = n->n_prefix;
460
+ size_t total = base + cnt;
461
+ sch_node **np = teptris_arena_alloc(
462
+ c->a, (total ? total : 1) * sizeof(sch_node *));
463
+ if (base > 0) memcpy(np, n->prefix, base * sizeof(sch_node *));
464
+ for (size_t p = 0; p < cnt; p++) {
465
+ np[base + p] = sch_compile_node(c, jv_item(val, p));
466
+ if (np[base + p] == NULL && c->err[0] != '\0') return NULL;
377
467
  }
378
- n->n_dep_schemas = cnt;
468
+ n->prefix = np;
469
+ n->n_prefix = total;
379
470
  } else if (strcmp(k, "items") == 0) {
380
471
  n->items = sch_compile_node(c, val);
381
472
  if (n->items == NULL && c->err[0] != '\0') return NULL;
382
473
  n->items_set = true;
474
+ } else if (strcmp(k, "additionalItems") == 0) {
475
+ n->add_items = sch_compile_node(c, val);
476
+ if (n->add_items == NULL && c->err[0] != '\0') return NULL;
477
+ n->add_items_set = true;
383
478
  } else if (strcmp(k, "prefixItems") == 0 &&
384
479
  jv_kind_of(val) == JV_ARRAY) {
385
480
  size_t cnt = jv_item_count(val);
386
- n->prefix = teptris_arena_alloc(c->a, (cnt ? cnt : 1) * sizeof(sch_node *));
481
+ size_t base = n->n_prefix;
482
+ size_t total = base + cnt;
483
+ sch_node **np = teptris_arena_alloc(
484
+ c->a, (total ? total : 1) * sizeof(sch_node *));
485
+ if (base > 0) memcpy(np, n->prefix, base * sizeof(sch_node *));
387
486
  for (size_t p = 0; p < cnt; p++) {
388
- n->prefix[p] = sch_compile_node(c, jv_item(val, p));
389
- if (n->prefix[p] == NULL && c->err[0] != '\0') return NULL;
487
+ np[base + p] = sch_compile_node(c, jv_item(val, p));
488
+ if (np[base + p] == NULL && c->err[0] != '\0') return NULL;
390
489
  }
391
- n->n_prefix = cnt;
490
+ n->prefix = np;
491
+ n->n_prefix = total;
392
492
  } else if (strcmp(k, "contains") == 0) {
393
493
  n->contains = sch_compile_node(c, val);
394
494
  if (n->contains == NULL && c->err[0] != '\0') return NULL;
@@ -456,6 +556,9 @@ sch_node *sch_compile_node(sctx *c, const jv *v) {
456
556
  if (jv_kind_of(val) == JV_INT) {
457
557
  n->mult_is_int = true;
458
558
  n->imult = jv_int(val);
559
+ } else if (sch_decimal_of_double(m, &n->mult_mant,
560
+ &n->mult_exp)) {
561
+ n->mult_exact = true;
459
562
  }
460
563
  } else if (strcmp(k, "allOf") == 0 && jv_kind_of(val) == JV_ARRAY) {
461
564
  size_t cnt = jv_item_count(val);
@@ -504,6 +607,36 @@ sch_node *sch_compile_node(sctx *c, const jv *v) {
504
607
 
505
608
  /* ------------------------------------------------------------ public -- */
506
609
 
610
+ /* the shortest decimal text that round-trips through d, read back as
611
+ * mant * 10^exp (the same decimal view serde_json/fraction-style
612
+ * exact arithmetic effectively uses) */
613
+ bool sch_decimal_of_double(double d, int64_t *mant, int *exp) {
614
+ if (!(d == d) || d == INFINITY || d == -INFINITY) return false;
615
+ char buf[64];
616
+ for (int prec = 1; prec <= 17; prec++) {
617
+ snprintf(buf, sizeof buf, "%.*g", prec, d);
618
+ if (strtod(buf, NULL) == d) break;
619
+ }
620
+ /* [+-] D[.DDD] [e[+-]NN] */
621
+ const char *p = buf;
622
+ if (*p == '+' || *p == '-') p++;
623
+ int64_t m = 0;
624
+ int e = 0;
625
+ for (; *p >= '0' && *p <= '9'; p++) m = m * 10 + (*p - '0');
626
+ if (*p == '.') {
627
+ for (p++; *p >= '0' && *p <= '9'; p++) {
628
+ m = m * 10 + (*p - '0');
629
+ e--;
630
+ }
631
+ }
632
+ if (*p == 'e' || *p == 'E') {
633
+ e += (int)strtol(p + 1, NULL, 10);
634
+ }
635
+ *mant = m;
636
+ *exp = e;
637
+ return true;
638
+ }
639
+
507
640
  TEPTRIS_API teptris_status teptris_schema_compile(const char *json, size_t len,
508
641
  struct teptris_schema **out,
509
642
  char **diag_buf,
@@ -27,7 +27,29 @@ enum {
27
27
  FMT_NONE = 0,
28
28
  FMT_DATETIME,
29
29
  FMT_DATE,
30
- FMT_TIME
30
+ FMT_TIME,
31
+ /* the taplo parity set: every format the jsonschema crate (the
32
+ * engine behind taplo) asserts on; names outside this set are
33
+ * annotations on both sides */
34
+ FMT_EMAIL,
35
+ FMT_IDN_EMAIL,
36
+ FMT_HOSTNAME,
37
+ FMT_IDN_HOSTNAME,
38
+ FMT_IPV4,
39
+ FMT_IPV6,
40
+ FMT_URI,
41
+ FMT_IRI,
42
+ FMT_URI_REFERENCE,
43
+ FMT_IRI_REFERENCE,
44
+ FMT_JSON_POINTER,
45
+ FMT_RELATIVE_JSON_POINTER,
46
+ FMT_REGEX,
47
+ FMT_URI_TEMPLATE,
48
+ FMT_UUID,
49
+ FMT_DURATION,
50
+ /* taplo's own custom formats (registered by its schema cache) */
51
+ FMT_SEMVER,
52
+ FMT_SEMVER_REQUIREMENT
31
53
  };
32
54
 
33
55
  typedef struct sch_node sch_node;
@@ -80,6 +102,8 @@ struct sch_node {
80
102
  size_t n_prefix;
81
103
  sch_node *items;
82
104
  bool items_set;
105
+ sch_node *add_items; /* additionalItems: applies beyond prefix */
106
+ bool add_items_set;
83
107
  sch_node *contains;
84
108
  long min_contains, max_contains;
85
109
  bool has_contains, has_min_contains, has_max_contains;
@@ -93,6 +117,8 @@ struct sch_node {
93
117
  long min_length, max_length;
94
118
  bool has_min_length, has_max_length;
95
119
  int format;
120
+ const char *content_encoding; /* enforced: "base64" */
121
+ const char *content_media_type; /* enforced: "application/json" */
96
122
 
97
123
  /* numeric keywords: the int form is exact when the constraint was
98
124
  * an exact integer token */
@@ -103,6 +129,11 @@ struct sch_node {
103
129
  double fmult;
104
130
  int64_t imult;
105
131
  bool mult_is_int;
132
+ /* exact-decimal view of a non-integer multipleOf: the
133
+ * shortest-roundtrip mantissa and decimal exponent */
134
+ bool mult_exact;
135
+ int64_t mult_mant;
136
+ int mult_exp;
106
137
 
107
138
  /* value constraints */
108
139
  jv **enum_vals;
@@ -127,6 +158,8 @@ typedef struct {
127
158
  bool in_progress;
128
159
  } *memo;
129
160
  size_t n_memo, cap_memo;
161
+ /* dependency-list capacities (grow-in-place across keywords) */
162
+ size_t cap_dep_req, cap_dep_schemas;
130
163
  /* anchors: "$anchor": name -> subtree */
131
164
  struct {
132
165
  const char *name;
@@ -175,5 +208,14 @@ struct sch_svalue {
175
208
  /* schema.c */
176
209
  sch_node *sch_compile_node(sctx *c, const jv *v);
177
210
  sch_node *sch_resolve_pointer(sctx *c, const char *pointer);
211
+ /* the shortest-roundtrip decimal view of a double: d = mant * 10^exp
212
+ * with |mant| < 10^18; false for non-finite d */
213
+ bool sch_decimal_of_double(double d, int64_t *mant, int *exp);
214
+
215
+ /* formats.c */
216
+ /* the string-side format battery (the taplo parity set); datetimes
217
+ * reach it through their canonical text like any other string */
218
+ bool sch_format_string_check(int format, const char *s, size_t len,
219
+ teptris_arena *scratch);
178
220
 
179
221
  #endif
@@ -2,6 +2,7 @@
2
2
  * document. One tagged-value conversion per validate call (validation
3
3
  * is a separate API; the parse hot path is untouched), then a single
4
4
  * recursive walk collecting every error, up to 256, as a JSON array. */
5
+ #include <math.h>
5
6
  #include <stdarg.h>
6
7
  #include <stdio.h>
7
8
  #include <stdlib.h>
@@ -181,8 +182,22 @@ static bool sv_equal(const sch_svalue *a, const sch_svalue *b) {
181
182
  if ((ad || bd) && (a->kind == SV_STRING || b->kind == SV_STRING)) {
182
183
  const sch_svalue *dt = ad ? a : b;
183
184
  const sch_svalue *st = ad ? b : a;
184
- return dt->str_len == st->str_len &&
185
- memcmp(dt->str, st->str, dt->str_len) == 0;
185
+ if (dt->str_len == st->str_len &&
186
+ memcmp(dt->str, st->str, dt->str_len) == 0) {
187
+ return true;
188
+ }
189
+ /* taplo re-formats offset datetimes with the time crate,
190
+ * which spells UTC as "+00:00" where the canonical TOML
191
+ * form is "Z"; both spellings name the same instant, so
192
+ * const/enum entries match either way */
193
+ if (dt->kind == SV_DATETIME && dt->str_len > 0 &&
194
+ dt->str[dt->str_len - 1] == 'Z' && st->str_len > 6 &&
195
+ memcmp(st->str + st->str_len - 6, "+00:00", 6) == 0 &&
196
+ dt->str_len - 1 == st->str_len - 6 &&
197
+ memcmp(dt->str, st->str, st->str_len - 6) == 0) {
198
+ return true;
199
+ }
200
+ return false;
186
201
  }
187
202
  return false; /* type-strict (policy 2) */
188
203
  }
@@ -256,30 +271,171 @@ static size_t cp_count(const char *s, size_t len) {
256
271
  return count;
257
272
  }
258
273
 
259
- /* RFC 3339-ish shape checks for string values under format */
260
- static bool str_is_date(const char *s, size_t len) {
261
- return len == 10 && s[4] == '-' && s[7] == '-';
262
- }
263
- static bool str_is_time(const char *s, size_t len) {
264
- return len >= 8 && s[2] == ':' && s[5] == ':';
265
- }
266
- static bool str_is_datetime(const char *s, size_t len) {
267
- return len >= 19 && s[4] == '-' && s[7] == '-' &&
268
- (s[10] == 'T' || s[10] == ' ') && s[13] == ':' && s[16] == ':';
269
- }
270
-
271
- static bool format_ok(int format, const sch_svalue *v) {
274
+ /* RFC 3339 shapes for the TOML datetime kinds live in formats.c; the
275
+ * walk only decides whether a kind participates in a format (policy
276
+ * 3) and hands plain strings to the format battery */
277
+ static bool format_ok(vctx *vc, int format, const sch_svalue *v) {
272
278
  if (format == FMT_NONE) return true;
273
279
  if (v->kind == SV_DATE) return format == FMT_DATE;
274
280
  if (v->kind == SV_TIME) return format == FMT_TIME;
275
281
  if (v->kind == SV_DATETIME) return format == FMT_DATETIME;
276
282
  if (v->kind != SV_STRING) return format == FMT_NONE;
277
- switch (format) {
278
- case FMT_DATETIME: return str_is_datetime(v->str, v->str_len);
279
- case FMT_DATE: return str_is_date(v->str, v->str_len);
280
- case FMT_TIME: return str_is_time(v->str, v->str_len);
283
+ return sch_format_string_check(format, v->str, v->str_len, vc->scratch);
284
+ }
285
+
286
+ /* --------------------------------------------------------- content -- */
287
+
288
+ static bool str_is_base64(const char *s, size_t len) {
289
+ if (len == 0) return true; /* "" decodes to empty (the crate's take) */
290
+ if (len % 4 != 0) return false;
291
+ size_t pad = 0;
292
+ for (size_t i = 0; i < len; i++) {
293
+ char c = s[i];
294
+ bool alnum = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
295
+ (c >= '0' && c <= '9');
296
+ if (!(alnum || c == '+' || c == '/' || c == '=')) return false;
297
+ if (c == '=') {
298
+ pad++;
299
+ } else if (pad > 0) {
300
+ return false; /* padding only at the end */
301
+ }
302
+ }
303
+ return pad <= 2;
304
+ }
305
+
306
+ static int b64_val(char c) {
307
+ if (c >= 'A' && c <= 'Z') return c - 'A';
308
+ if (c >= 'a' && c <= 'z') return c - 'a' + 26;
309
+ if (c >= '0' && c <= '9') return c - '0' + 52;
310
+ if (c == '+') return 62;
311
+ if (c == '/') return 63;
312
+ return -1;
313
+ }
314
+
315
+ /* contentEncoding/contentMediaType, the taplo parity pair: base64 is
316
+ * the one encoding checked (and decoded for the media step),
317
+ * application/json the one media type parsed; unknown names on either
318
+ * side are no-ops, exactly like the crate's map misses */
319
+ static void check_content(vctx *vc, const sch_node *n, const sch_svalue *v) {
320
+ if (v->kind != SV_STRING) return;
321
+ const char *text = v->str;
322
+ size_t tlen = v->str_len;
323
+ bool b64 = n->content_encoding != NULL &&
324
+ strcmp(n->content_encoding, "base64") == 0;
325
+ if (b64 && !str_is_base64(text, tlen)) {
326
+ verr(vc, "string is not valid base64 (contentEncoding)");
327
+ return;
328
+ }
329
+ if (n->content_media_type == NULL ||
330
+ strcmp(n->content_media_type, "application/json") != 0) {
331
+ return; /* unknown media names: annotation */
332
+ }
333
+ const char *payload = text;
334
+ size_t plen = tlen;
335
+ if (b64) {
336
+ size_t dn = tlen / 4 * 3;
337
+ if (tlen > 0 && text[tlen - 1] == '=') dn--;
338
+ if (tlen > 1 && text[tlen - 2] == '=') dn--;
339
+ char *out = teptris_arena_alloc(vc->scratch, dn + 1);
340
+ size_t o = 0;
341
+ int acc = 0, bits = 0;
342
+ for (size_t i = 0; i < tlen && o < dn; i++) {
343
+ if (text[i] == '=') break;
344
+ int d = b64_val(text[i]);
345
+ if (d < 0) break;
346
+ acc = (acc << 6) | d;
347
+ bits += 6;
348
+ if (bits >= 8) {
349
+ bits -= 8;
350
+ out[o++] = (char)((acc >> bits) & 0xFF);
351
+ }
352
+ }
353
+ /* the media check runs on the decoded text only when it is
354
+ * UTF-8 (the crate skips the check otherwise) */
355
+ bool utf8 = true;
356
+ for (size_t i = 0; i < o && utf8; i++) {
357
+ unsigned char b = (unsigned char)out[i];
358
+ if (b < 0x80) continue;
359
+ int need = b < 0xE0 ? 1 : b < 0xF0 ? 2 : 3;
360
+ if (i + need >= o || (b & 0xC0) != 0xC0) utf8 = false;
361
+ for (int k = 1; k <= need && utf8; k++) {
362
+ if (((unsigned char)out[i + k] & 0xC0) != 0x80) utf8 = false;
363
+ }
364
+ i += need;
365
+ }
366
+ if (!utf8) return;
367
+ out[o] = '\0';
368
+ payload = out;
369
+ plen = o;
370
+ }
371
+ char jerr[64] = {0};
372
+ if (json_read(vc->scratch, payload, plen, jerr, sizeof jerr) == NULL) {
373
+ verr(vc, "string is not valid JSON (contentMediaType)");
281
374
  }
282
- return true;
375
+ }
376
+
377
+ /* ------------------------------------------------------ multipleOf -- */
378
+
379
+ /* value / multipleOf decided exactly on the decimal representations:
380
+ * value = vm * 10^ve, mult = mm * 10^me, and the ratio is an integer
381
+ * iff the mantissas divide once the 2s and 5s are moved across —
382
+ * plain int64 arithmetic, so it runs identically where __int128 does
383
+ * not exist (MSVC) and needs no floating division at all */
384
+ static bool sch_mult_ok(const sch_node *n, const sch_svalue *v) {
385
+ if (v->kind == SV_INT && n->mult_is_int) {
386
+ return n->imult != 0 && v->i % n->imult == 0;
387
+ }
388
+ double fv = v->kind == SV_INT ? (double)v->i : v->f;
389
+ if (!(fv == fv) || fv == INFINITY || fv == -INFINITY) {
390
+ return false; /* nan/inf are never multiples */
391
+ }
392
+ int64_t vm, mm;
393
+ int ve, me;
394
+ if (v->kind == SV_INT) {
395
+ vm = v->i;
396
+ ve = 0;
397
+ } else if (!sch_decimal_of_double(v->f, &vm, &ve)) {
398
+ return false;
399
+ }
400
+ if (n->mult_is_int) {
401
+ mm = n->imult;
402
+ me = 0;
403
+ } else if (n->mult_exact) {
404
+ mm = n->mult_mant;
405
+ me = n->mult_exp;
406
+ } else {
407
+ return false;
408
+ }
409
+ if (mm == 0) return false;
410
+
411
+ /* strip the 2s and 5s out of the multiplier; the remainder is
412
+ * coprime to 10, so it must divide the mantissa itself */
413
+ int b = 0, c = 0;
414
+ while (mm % 2 == 0) {
415
+ mm /= 2;
416
+ b++;
417
+ }
418
+ while (mm % 5 == 0) {
419
+ mm /= 5;
420
+ c++;
421
+ }
422
+
423
+ /* the numerator vm * 10^k (k = ve - me) carries 2^(k+v2) * 5^(k+v5) */
424
+ int k = ve - me;
425
+ uint64_t mag = vm < 0 ? (uint64_t)(-(vm + 1)) + 1 : (uint64_t)vm;
426
+ int u2 = 0, u5 = 0;
427
+ uint64_t m = mag;
428
+ while (m % 2 == 0 && u2 < 64) {
429
+ m /= 2;
430
+ u2++;
431
+ }
432
+ m = mag;
433
+ while (m % 5 == 0 && u5 < 64) {
434
+ m /= 5;
435
+ u5++;
436
+ }
437
+ if (k + u2 < b || k + u5 < c) return false;
438
+ return mag % (uint64_t)mm == 0;
283
439
  }
284
440
 
285
441
  static int sv_to_st(const sch_svalue *v) {
@@ -385,13 +541,7 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
385
541
  if (!ok) verr(vc, "value is not below the exclusive maximum");
386
542
  }
387
543
  if (n->has_mult) {
388
- bool ok;
389
- if (v->kind == SV_INT && n->mult_is_int) {
390
- ok = n->imult != 0 && v->i % n->imult == 0;
391
- } else {
392
- ok = n->fmult != 0.0 && fv / n->fmult ==
393
- (double)(long long)(fv / n->fmult);
394
- }
544
+ bool ok = sch_mult_ok(n, v);
395
545
  if (!ok) verr(vc, "value is not a multiple of multipleOf");
396
546
  }
397
547
  }
@@ -411,9 +561,10 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
411
561
  !sch_regex_is_match(n->pattern, v->str, v->str_len)) {
412
562
  verr(vc, "string does not match the pattern");
413
563
  }
414
- if (n->format != FMT_NONE && !format_ok(n->format, v)) {
564
+ if (n->format != FMT_NONE && !format_ok(vc, n->format, v)) {
415
565
  verr(vc, "string does not match the format");
416
566
  }
567
+ check_content(vc, n, v);
417
568
  }
418
569
 
419
570
  /* object */
@@ -555,12 +706,32 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
555
706
  val(vc, n->items, v->items[i]);
556
707
  path_pop(vc);
557
708
  spop(vc);
709
+ } else if (n->add_items != NULL) {
710
+ spush(vc, "additionalItems");
711
+ path_push_idx(vc, (long)i);
712
+ if (n->add_items->is_false) {
713
+ verr(vc, "array has items beyond the tuple "
714
+ "(additionalItems is false)");
715
+ } else {
716
+ val(vc, n->add_items, v->items[i]);
717
+ }
718
+ path_pop(vc);
719
+ spop(vc);
558
720
  }
559
721
  }
560
- } else if (n->items_set && n->items == NULL) {
561
- /* "items": false — no items allowed beyond prefix */
562
- if (v->n_items > n->n_prefix) {
563
- verr(vc, "array has items beyond prefixItems (items is false)");
722
+ } else if (n->add_items != NULL) {
723
+ /* additionalItems with no tuple: every position */
724
+ for (size_t i = 0; i < v->n_items; i++) {
725
+ spush(vc, "additionalItems");
726
+ path_push_idx(vc, (long)i);
727
+ if (n->add_items->is_false) {
728
+ verr(vc, "array has items beyond the tuple "
729
+ "(additionalItems is false)");
730
+ } else {
731
+ val(vc, n->add_items, v->items[i]);
732
+ }
733
+ path_pop(vc);
734
+ spop(vc);
564
735
  }
565
736
  }
566
737
  if (n->has_contains && n->contains != NULL) {
Binary file
Binary file
Binary file
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Teptris
4
- VERSION = "0.3.20.0"
4
+ VERSION = "0.3.21.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: teptris
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.20.0
4
+ version: 0.3.21.0
5
5
  platform: arm-linux
6
6
  authors:
7
7
  - teptris contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-02 00:00:00.000000000 Z
11
+ date: 2026-11-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A native C extension over libteptris, a C99 TOML 1.1 parser/emitter.
14
14
  API shape mirrors the tomlib gem.
@@ -52,6 +52,7 @@ files:
52
52
  - ext/teptris_ext/engine/src/teptris/parse/parser.c
53
53
  - ext/teptris_ext/engine/src/teptris/parse/scalars.c
54
54
  - ext/teptris_ext/engine/src/teptris/plan/plan.c
55
+ - ext/teptris_ext/engine/src/teptris/schema/formats.c
55
56
  - ext/teptris_ext/engine/src/teptris/schema/json_read.c
56
57
  - ext/teptris_ext/engine/src/teptris/schema/json_read.h
57
58
  - ext/teptris_ext/engine/src/teptris/schema/regex.c