teptris 0.3.20.0-arm-linux → 0.3.22.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 +4 -4
- data/ext/teptris_ext/engine/README.md +1 -1
- data/ext/teptris_ext/engine/src/include/teptris/version.h +2 -2
- data/ext/teptris_ext/engine/src/teptris/schema/formats.c +690 -0
- data/ext/teptris_ext/engine/src/teptris/schema/json_read.c +5 -2
- data/ext/teptris_ext/engine/src/teptris/schema/schema.c +309 -67
- data/ext/teptris_ext/engine/src/teptris/schema/schema_internal.h +62 -1
- data/ext/teptris_ext/engine/src/teptris/schema/validate.c +550 -40
- data/lib/teptris/3.3/teptris_ext.so +0 -0
- data/lib/teptris/3.4/teptris_ext.so +0 -0
- data/lib/teptris/4.0/teptris_ext.so +0 -0
- data/lib/teptris/version.rb +1 -1
- metadata +3 -2
|
@@ -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>
|
|
@@ -29,6 +30,26 @@ typedef struct {
|
|
|
29
30
|
char sseg_buf[64][64];
|
|
30
31
|
int sdepth;
|
|
31
32
|
int truncated;
|
|
33
|
+
/* annotation frames (2020-12 unevaluated* semantics): one per
|
|
34
|
+
* schema-object application; a child application on the SAME
|
|
35
|
+
* instance merges up into its parent at exit (in-place
|
|
36
|
+
* applicators), cousins and different-instance children never
|
|
37
|
+
* do; trials restore the merge target on failure */
|
|
38
|
+
struct eval_frame {
|
|
39
|
+
const sch_svalue *inst;
|
|
40
|
+
const char **keys;
|
|
41
|
+
size_t n_keys, cap_keys;
|
|
42
|
+
size_t *idx;
|
|
43
|
+
size_t n_idx, cap_idx;
|
|
44
|
+
} *frames;
|
|
45
|
+
int n_frames, cap_frames;
|
|
46
|
+
/* dynamic-scope stack for $dynamicRef / $recursiveRef: the
|
|
47
|
+
* innermost anchor on the evaluation path wins */
|
|
48
|
+
struct dyn_scope {
|
|
49
|
+
const char *name;
|
|
50
|
+
sch_node *node;
|
|
51
|
+
} *scopes;
|
|
52
|
+
int n_scopes, cap_scopes;
|
|
32
53
|
} vctx;
|
|
33
54
|
|
|
34
55
|
/* ---------------------------------------------------------- output -- */
|
|
@@ -167,6 +188,153 @@ static void verr(vctx *vc, const char *fmt, ...) {
|
|
|
167
188
|
vc->n_errors++;
|
|
168
189
|
}
|
|
169
190
|
|
|
191
|
+
/* ------------------------------------------- annotation frames (2020-12) */
|
|
192
|
+
|
|
193
|
+
static bool frame_grow(vctx *vc) {
|
|
194
|
+
if (vc->n_frames == vc->cap_frames) {
|
|
195
|
+
vc->cap_frames = vc->cap_frames == 0 ? 16 : vc->cap_frames * 2;
|
|
196
|
+
void *nf = realloc(vc->frames, vc->cap_frames * sizeof(*vc->frames));
|
|
197
|
+
if (nf == NULL) return false;
|
|
198
|
+
vc->frames = nf;
|
|
199
|
+
}
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
static bool frame_push(vctx *vc, const sch_svalue *v) {
|
|
204
|
+
if (!frame_grow(vc)) return false;
|
|
205
|
+
struct eval_frame *f = &vc->frames[vc->n_frames++];
|
|
206
|
+
memset(f, 0, sizeof(*f));
|
|
207
|
+
f->inst = v;
|
|
208
|
+
return true;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/* merge this frame's annotations into the parent (bubbling up through
|
|
212
|
+
* an in-place applicator); answers false when there is no parent or
|
|
213
|
+
* the parent annotated a different instance (cousins/descendants) */
|
|
214
|
+
static void frame_merge_up(vctx *vc) {
|
|
215
|
+
if (vc->n_frames < 2) return;
|
|
216
|
+
struct eval_frame *f = &vc->frames[vc->n_frames - 1];
|
|
217
|
+
struct eval_frame *p = &vc->frames[vc->n_frames - 2];
|
|
218
|
+
if (p->inst != f->inst) return;
|
|
219
|
+
if (f->n_keys > 0) {
|
|
220
|
+
if (p->n_keys + f->n_keys > p->cap_keys) {
|
|
221
|
+
size_t want = p->cap_keys == 0 ? 8 : p->cap_keys;
|
|
222
|
+
while (want < p->n_keys + f->n_keys) want *= 2;
|
|
223
|
+
void *nk = realloc(p->keys, want * sizeof(*p->keys));
|
|
224
|
+
if (nk == NULL) return;
|
|
225
|
+
p->keys = nk;
|
|
226
|
+
p->cap_keys = want;
|
|
227
|
+
}
|
|
228
|
+
memcpy(p->keys + p->n_keys, f->keys, f->n_keys * sizeof(*f->keys));
|
|
229
|
+
p->n_keys += f->n_keys;
|
|
230
|
+
}
|
|
231
|
+
if (f->n_idx > 0) {
|
|
232
|
+
if (p->n_idx + f->n_idx > p->cap_idx) {
|
|
233
|
+
size_t want = p->cap_idx == 0 ? 8 : p->cap_idx;
|
|
234
|
+
while (want < p->n_idx + f->n_idx) want *= 2;
|
|
235
|
+
void *ni = realloc(p->idx, want * sizeof(*p->idx));
|
|
236
|
+
if (ni == NULL) return;
|
|
237
|
+
p->idx = ni;
|
|
238
|
+
p->cap_idx = want;
|
|
239
|
+
}
|
|
240
|
+
memcpy(p->idx + p->n_idx, f->idx, f->n_idx * sizeof(*f->idx));
|
|
241
|
+
p->n_idx += f->n_idx;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/* pop the top frame: bubble its annotations into the parent, then
|
|
246
|
+
* release its arrays (a slot above n_frames would never be revisited
|
|
247
|
+
* by frames_free) */
|
|
248
|
+
static void frame_pop(vctx *vc) {
|
|
249
|
+
frame_merge_up(vc);
|
|
250
|
+
struct eval_frame *f = &vc->frames[vc->n_frames - 1];
|
|
251
|
+
free(f->keys);
|
|
252
|
+
free(f->idx);
|
|
253
|
+
vc->n_frames--;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
static bool frame_key_seen(const struct eval_frame *f, const char *key) {
|
|
257
|
+
for (size_t i = 0; i < f->n_keys; i++) {
|
|
258
|
+
if (strcmp(f->keys[i], key) == 0) return true;
|
|
259
|
+
}
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
static bool frame_idx_seen(const struct eval_frame *f, size_t idx) {
|
|
264
|
+
for (size_t i = 0; i < f->n_idx; i++) {
|
|
265
|
+
if (f->idx[i] == idx) return true;
|
|
266
|
+
}
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
static void frame_add_key(vctx *vc, const char *key) {
|
|
271
|
+
if (vc->n_frames == 0) return;
|
|
272
|
+
struct eval_frame *f = &vc->frames[vc->n_frames - 1];
|
|
273
|
+
if (frame_key_seen(f, key)) return;
|
|
274
|
+
if (f->n_keys == f->cap_keys) {
|
|
275
|
+
f->cap_keys = f->cap_keys == 0 ? 8 : f->cap_keys * 2;
|
|
276
|
+
void *nk = realloc(f->keys, f->cap_keys * sizeof(*f->keys));
|
|
277
|
+
if (nk == NULL) return;
|
|
278
|
+
f->keys = nk;
|
|
279
|
+
}
|
|
280
|
+
f->keys[f->n_keys++] = key;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
static void frame_add_idx(vctx *vc, size_t idx) {
|
|
284
|
+
if (vc->n_frames == 0) return;
|
|
285
|
+
struct eval_frame *f = &vc->frames[vc->n_frames - 1];
|
|
286
|
+
if (frame_idx_seen(f, idx)) return;
|
|
287
|
+
if (f->n_idx == f->cap_idx) {
|
|
288
|
+
f->cap_idx = f->cap_idx == 0 ? 8 : f->cap_idx * 2;
|
|
289
|
+
void *ni = realloc(f->idx, f->cap_idx * sizeof(*f->idx));
|
|
290
|
+
if (ni == NULL) return;
|
|
291
|
+
f->idx = ni;
|
|
292
|
+
}
|
|
293
|
+
f->idx[f->n_idx++] = idx;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/* drop the annotations a trial contributed to its merge target (the
|
|
297
|
+
* parent frame): failed anyOf/oneOf branches and the not/if trials
|
|
298
|
+
* annotate nothing */
|
|
299
|
+
static void frame_truncate_to(vctx *vc, size_t n_keys, size_t n_idx) {
|
|
300
|
+
if (vc->n_frames < 1) return;
|
|
301
|
+
struct eval_frame *f = &vc->frames[vc->n_frames - 1];
|
|
302
|
+
if (f->n_keys > n_keys) f->n_keys = n_keys;
|
|
303
|
+
if (f->n_idx > n_idx) f->n_idx = n_idx;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
static void frames_free(vctx *vc) {
|
|
307
|
+
for (int i = 0; i < vc->n_frames; i++) {
|
|
308
|
+
free(vc->frames[i].keys);
|
|
309
|
+
free(vc->frames[i].idx);
|
|
310
|
+
}
|
|
311
|
+
free(vc->frames);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/* ------------------------------------------------- dynamic scopes -- */
|
|
315
|
+
|
|
316
|
+
static bool scope_push(vctx *vc, const char *name, sch_node *node) {
|
|
317
|
+
if (name == NULL || node == NULL) return true;
|
|
318
|
+
if (vc->n_scopes == vc->cap_scopes) {
|
|
319
|
+
vc->cap_scopes = vc->cap_scopes == 0 ? 16 : vc->cap_scopes * 2;
|
|
320
|
+
void *ns = realloc(vc->scopes, vc->cap_scopes * sizeof(*vc->scopes));
|
|
321
|
+
if (ns == NULL) return false;
|
|
322
|
+
vc->scopes = ns;
|
|
323
|
+
}
|
|
324
|
+
vc->scopes[vc->n_scopes].name = name;
|
|
325
|
+
vc->scopes[vc->n_scopes].node = node;
|
|
326
|
+
vc->n_scopes++;
|
|
327
|
+
return true;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/* the innermost dynamic anchor for a name on the evaluation path */
|
|
331
|
+
static sch_node *scope_resolve(vctx *vc, const char *name) {
|
|
332
|
+
for (int i = vc->n_scopes - 1; i >= 0; i--) {
|
|
333
|
+
if (strcmp(vc->scopes[i].name, name) == 0) return vc->scopes[i].node;
|
|
334
|
+
}
|
|
335
|
+
return NULL;
|
|
336
|
+
}
|
|
337
|
+
|
|
170
338
|
/* --------------------------------------------------- value equality -- */
|
|
171
339
|
|
|
172
340
|
static bool sv_equal(const sch_svalue *a, const sch_svalue *b) {
|
|
@@ -181,8 +349,22 @@ static bool sv_equal(const sch_svalue *a, const sch_svalue *b) {
|
|
|
181
349
|
if ((ad || bd) && (a->kind == SV_STRING || b->kind == SV_STRING)) {
|
|
182
350
|
const sch_svalue *dt = ad ? a : b;
|
|
183
351
|
const sch_svalue *st = ad ? b : a;
|
|
184
|
-
|
|
185
|
-
|
|
352
|
+
if (dt->str_len == st->str_len &&
|
|
353
|
+
memcmp(dt->str, st->str, dt->str_len) == 0) {
|
|
354
|
+
return true;
|
|
355
|
+
}
|
|
356
|
+
/* taplo re-formats offset datetimes with the time crate,
|
|
357
|
+
* which spells UTC as "+00:00" where the canonical TOML
|
|
358
|
+
* form is "Z"; both spellings name the same instant, so
|
|
359
|
+
* const/enum entries match either way */
|
|
360
|
+
if (dt->kind == SV_DATETIME && dt->str_len > 0 &&
|
|
361
|
+
dt->str[dt->str_len - 1] == 'Z' && st->str_len > 6 &&
|
|
362
|
+
memcmp(st->str + st->str_len - 6, "+00:00", 6) == 0 &&
|
|
363
|
+
dt->str_len - 1 == st->str_len - 6 &&
|
|
364
|
+
memcmp(dt->str, st->str, st->str_len - 6) == 0) {
|
|
365
|
+
return true;
|
|
366
|
+
}
|
|
367
|
+
return false;
|
|
186
368
|
}
|
|
187
369
|
return false; /* type-strict (policy 2) */
|
|
188
370
|
}
|
|
@@ -224,21 +406,57 @@ static bool sv_equal(const sch_svalue *a, const sch_svalue *b) {
|
|
|
224
406
|
static sch_svalue *jv_to_sv(vctx *vc, jv *v);
|
|
225
407
|
static void val(vctx *vc, sch_node *n, const sch_svalue *v);
|
|
226
408
|
|
|
409
|
+
/* trial: does n accept v? Failed trials contribute no errors AND no
|
|
410
|
+
* annotations; successful anyOf/oneOf branches bubble theirs up */
|
|
227
411
|
static bool passes(vctx *vc, sch_node *n, const sch_svalue *v) {
|
|
228
412
|
int save_pings = vc->pings;
|
|
229
413
|
size_t save_n = vc->n;
|
|
230
414
|
int save_depth = vc->sdepth;
|
|
415
|
+
size_t keys_before = 0, idx_before = 0;
|
|
416
|
+
if (vc->n_frames > 0) {
|
|
417
|
+
keys_before = vc->frames[vc->n_frames - 1].n_keys;
|
|
418
|
+
idx_before = vc->frames[vc->n_frames - 1].n_idx;
|
|
419
|
+
}
|
|
231
420
|
vc->mute++;
|
|
232
421
|
val(vc, n, v);
|
|
233
422
|
vc->mute--;
|
|
234
|
-
|
|
235
|
-
|
|
423
|
+
bool ok = vc->pings == save_pings;
|
|
424
|
+
/* the trial's pings were its own private signal: nested oneOf/
|
|
425
|
+
* anyOf/not/if trials legitimately produced some without the
|
|
426
|
+
* branch failing - consume them so an OUTER trial sees only what
|
|
427
|
+
* this branch really contributed */
|
|
428
|
+
vc->pings = save_pings;
|
|
429
|
+
if (!ok) {
|
|
430
|
+
/* discard the trial's errors and its annotations */
|
|
431
|
+
vc->n = save_n;
|
|
432
|
+
if (vc->buf != NULL) vc->buf[vc->n] = '\0';
|
|
433
|
+
vc->sdepth = save_depth;
|
|
434
|
+
frame_truncate_to(vc, keys_before, idx_before);
|
|
236
435
|
}
|
|
237
|
-
|
|
436
|
+
return ok;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/* not/if trials: the branch runs for the verdict only - its
|
|
440
|
+
* annotations are discarded whether it passed or failed */
|
|
441
|
+
static bool passes_annot_free(vctx *vc, sch_node *n, const sch_svalue *v) {
|
|
442
|
+
int save_pings = vc->pings;
|
|
443
|
+
size_t save_n = vc->n;
|
|
444
|
+
int save_depth = vc->sdepth;
|
|
445
|
+
size_t keys_before = 0, idx_before = 0;
|
|
446
|
+
if (vc->n_frames > 0) {
|
|
447
|
+
keys_before = vc->frames[vc->n_frames - 1].n_keys;
|
|
448
|
+
idx_before = vc->frames[vc->n_frames - 1].n_idx;
|
|
449
|
+
}
|
|
450
|
+
bool ok;
|
|
451
|
+
vc->mute++;
|
|
452
|
+
ok = (val(vc, n, v), vc->pings == save_pings);
|
|
453
|
+
vc->mute--;
|
|
454
|
+
vc->pings = save_pings; /* consumed: the signal was private */
|
|
238
455
|
vc->n = save_n;
|
|
239
456
|
if (vc->buf != NULL) vc->buf[vc->n] = '\0';
|
|
240
457
|
vc->sdepth = save_depth;
|
|
241
|
-
|
|
458
|
+
frame_truncate_to(vc, keys_before, idx_before);
|
|
459
|
+
return ok;
|
|
242
460
|
}
|
|
243
461
|
|
|
244
462
|
static size_t cp_count(const char *s, size_t len) {
|
|
@@ -256,30 +474,202 @@ static size_t cp_count(const char *s, size_t len) {
|
|
|
256
474
|
return count;
|
|
257
475
|
}
|
|
258
476
|
|
|
259
|
-
/*
|
|
260
|
-
|
|
261
|
-
|
|
477
|
+
/* the TOML-native kind table (tombi's named formats made explicit):
|
|
478
|
+
* an offset datetime satisfies date-time and only date-time; a local
|
|
479
|
+
* datetime satisfies date-time-local; a local time satisfies
|
|
480
|
+
* time-local (TOML has no offset time); a date satisfies date */
|
|
481
|
+
static bool format_ok(vctx *vc, int format, const sch_svalue *v) {
|
|
482
|
+
if (format == FMT_NONE) return true;
|
|
483
|
+
if (v->kind == SV_DATE) return format == FMT_DATE;
|
|
484
|
+
if (v->kind == SV_TIME) return format == FMT_TIME_LOCAL;
|
|
485
|
+
if (v->kind == SV_DATETIME) {
|
|
486
|
+
return v->has_offset ? format == FMT_DATETIME
|
|
487
|
+
: format == FMT_DATETIME_LOCAL;
|
|
488
|
+
}
|
|
489
|
+
if (v->kind != SV_STRING) return format == FMT_NONE;
|
|
490
|
+
return sch_format_string_check(format, v->str, v->str_len, vc->scratch);
|
|
262
491
|
}
|
|
263
|
-
|
|
264
|
-
|
|
492
|
+
|
|
493
|
+
/* --------------------------------------------------------- content -- */
|
|
494
|
+
|
|
495
|
+
static bool str_is_base64(const char *s, size_t len) {
|
|
496
|
+
if (len == 0) return true; /* "" decodes to empty (the crate's take) */
|
|
497
|
+
if (len % 4 != 0) return false;
|
|
498
|
+
size_t pad = 0;
|
|
499
|
+
for (size_t i = 0; i < len; i++) {
|
|
500
|
+
char c = s[i];
|
|
501
|
+
bool alnum = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
|
|
502
|
+
(c >= '0' && c <= '9');
|
|
503
|
+
if (!(alnum || c == '+' || c == '/' || c == '=')) return false;
|
|
504
|
+
if (c == '=') {
|
|
505
|
+
pad++;
|
|
506
|
+
} else if (pad > 0) {
|
|
507
|
+
return false; /* padding only at the end */
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
return pad <= 2;
|
|
265
511
|
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
512
|
+
|
|
513
|
+
static int b64_val(char c) {
|
|
514
|
+
if (c >= 'A' && c <= 'Z') return c - 'A';
|
|
515
|
+
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
|
|
516
|
+
if (c >= '0' && c <= '9') return c - '0' + 52;
|
|
517
|
+
if (c == '+') return 62;
|
|
518
|
+
if (c == '/') return 63;
|
|
519
|
+
return -1;
|
|
269
520
|
}
|
|
270
521
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
if (
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
522
|
+
/* decode base64 text into the scratch arena; answers the length and
|
|
523
|
+
* NULL when the text is not valid base64 */
|
|
524
|
+
static char *b64_decode(vctx *vc, const char *text, size_t tlen,
|
|
525
|
+
size_t *out_len) {
|
|
526
|
+
if (!str_is_base64(text, tlen)) return NULL;
|
|
527
|
+
size_t dn = tlen / 4 * 3;
|
|
528
|
+
if (tlen > 0 && text[tlen - 1] == '=') dn--;
|
|
529
|
+
if (tlen > 1 && text[tlen - 2] == '=') dn--;
|
|
530
|
+
char *out = teptris_arena_alloc(vc->scratch, dn + 1);
|
|
531
|
+
size_t o = 0;
|
|
532
|
+
int acc = 0, bits = 0;
|
|
533
|
+
for (size_t i = 0; i < tlen && o < dn; i++) {
|
|
534
|
+
if (text[i] == '=') break;
|
|
535
|
+
int d = b64_val(text[i]);
|
|
536
|
+
if (d < 0) break;
|
|
537
|
+
acc = (acc << 6) | d;
|
|
538
|
+
bits += 6;
|
|
539
|
+
if (bits >= 8) {
|
|
540
|
+
bits -= 8;
|
|
541
|
+
out[o++] = (char)((acc >> bits) & 0xFF);
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
out[o] = '\0';
|
|
545
|
+
*out_len = o;
|
|
546
|
+
return out;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/* contentEncoding/contentMediaType, the taplo parity pair: base64 is
|
|
550
|
+
* the one encoding checked (and decoded for the media step),
|
|
551
|
+
* application/json the one media type parsed; unknown names on either
|
|
552
|
+
* side are no-ops, exactly like the crate's map misses */
|
|
553
|
+
static void check_content(vctx *vc, const sch_node *n, const sch_svalue *v) {
|
|
554
|
+
if (v->kind != SV_STRING) return;
|
|
555
|
+
const char *text = v->str;
|
|
556
|
+
size_t tlen = v->str_len;
|
|
557
|
+
bool b64 = n->content_encoding != NULL &&
|
|
558
|
+
strcmp(n->content_encoding, "base64") == 0;
|
|
559
|
+
if (b64 && !str_is_base64(text, tlen)) {
|
|
560
|
+
verr(vc, "string is not valid base64 (contentEncoding)");
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
if (n->content_media_type == NULL ||
|
|
564
|
+
strcmp(n->content_media_type, "application/json") != 0) {
|
|
565
|
+
/* no media interpretation: contentSchema sees the decoded
|
|
566
|
+
* text when an encoding was applied, the raw string when not */
|
|
567
|
+
if (n->content_schema != NULL) {
|
|
568
|
+
const char *payload = text;
|
|
569
|
+
size_t plen = tlen;
|
|
570
|
+
char *out = NULL;
|
|
571
|
+
if (b64 && str_is_base64(text, tlen)) {
|
|
572
|
+
out = b64_decode(vc, text, tlen, &plen);
|
|
573
|
+
payload = out;
|
|
574
|
+
}
|
|
575
|
+
if (payload != NULL) {
|
|
576
|
+
sch_svalue sv;
|
|
577
|
+
memset(&sv, 0, sizeof sv);
|
|
578
|
+
sv.kind = SV_STRING;
|
|
579
|
+
sv.str = payload;
|
|
580
|
+
sv.str_len = plen;
|
|
581
|
+
spush(vc, "contentSchema");
|
|
582
|
+
val(vc, n->content_schema, &sv);
|
|
583
|
+
spop(vc);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
return; /* unknown media names: annotation */
|
|
587
|
+
}
|
|
588
|
+
const char *payload = text;
|
|
589
|
+
size_t plen = tlen;
|
|
590
|
+
if (b64) {
|
|
591
|
+
/* the media check runs on the decoded text only when it
|
|
592
|
+
* decodes (the crate skips the check otherwise) */
|
|
593
|
+
payload = b64_decode(vc, text, tlen, &plen);
|
|
594
|
+
if (payload == NULL) return;
|
|
595
|
+
}
|
|
596
|
+
char jerr[64] = {0};
|
|
597
|
+
jv *parsed = json_read(vc->scratch, payload, plen, jerr, sizeof jerr);
|
|
598
|
+
if (parsed == NULL) {
|
|
599
|
+
verr(vc, "string is not valid JSON (contentMediaType)");
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
if (n->content_schema != NULL) {
|
|
603
|
+
/* contentSchema asserts the parsed value (the media type's
|
|
604
|
+
* interpretation of the decoded content) */
|
|
605
|
+
spush(vc, "contentSchema");
|
|
606
|
+
val(vc, n->content_schema, jv_to_sv(vc, parsed));
|
|
607
|
+
spop(vc);
|
|
281
608
|
}
|
|
282
|
-
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
/* ------------------------------------------------------ multipleOf -- */
|
|
612
|
+
|
|
613
|
+
/* value / multipleOf decided exactly on the decimal representations:
|
|
614
|
+
* value = vm * 10^ve, mult = mm * 10^me, and the ratio is an integer
|
|
615
|
+
* iff the mantissas divide once the 2s and 5s are moved across —
|
|
616
|
+
* plain int64 arithmetic, so it runs identically where __int128 does
|
|
617
|
+
* not exist (MSVC) and needs no floating division at all */
|
|
618
|
+
static bool sch_mult_ok(const sch_node *n, const sch_svalue *v) {
|
|
619
|
+
if (v->kind == SV_INT && n->mult_is_int) {
|
|
620
|
+
return n->imult != 0 && v->i % n->imult == 0;
|
|
621
|
+
}
|
|
622
|
+
double fv = v->kind == SV_INT ? (double)v->i : v->f;
|
|
623
|
+
if (!(fv == fv) || fv == INFINITY || fv == -INFINITY) {
|
|
624
|
+
return false; /* nan/inf are never multiples */
|
|
625
|
+
}
|
|
626
|
+
int64_t vm, mm;
|
|
627
|
+
int ve, me;
|
|
628
|
+
if (v->kind == SV_INT) {
|
|
629
|
+
vm = v->i;
|
|
630
|
+
ve = 0;
|
|
631
|
+
} else if (!sch_decimal_of_double(v->f, &vm, &ve)) {
|
|
632
|
+
return false;
|
|
633
|
+
}
|
|
634
|
+
if (n->mult_is_int) {
|
|
635
|
+
mm = n->imult;
|
|
636
|
+
me = 0;
|
|
637
|
+
} else if (n->mult_exact) {
|
|
638
|
+
mm = n->mult_mant;
|
|
639
|
+
me = n->mult_exp;
|
|
640
|
+
} else {
|
|
641
|
+
return false;
|
|
642
|
+
}
|
|
643
|
+
if (mm == 0) return false;
|
|
644
|
+
|
|
645
|
+
/* strip the 2s and 5s out of the multiplier; the remainder is
|
|
646
|
+
* coprime to 10, so it must divide the mantissa itself */
|
|
647
|
+
int b = 0, c = 0;
|
|
648
|
+
while (mm % 2 == 0) {
|
|
649
|
+
mm /= 2;
|
|
650
|
+
b++;
|
|
651
|
+
}
|
|
652
|
+
while (mm % 5 == 0) {
|
|
653
|
+
mm /= 5;
|
|
654
|
+
c++;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
/* the numerator vm * 10^k (k = ve - me) carries 2^(k+v2) * 5^(k+v5) */
|
|
658
|
+
int k = ve - me;
|
|
659
|
+
uint64_t mag = vm < 0 ? (uint64_t)(-(vm + 1)) + 1 : (uint64_t)vm;
|
|
660
|
+
int u2 = 0, u5 = 0;
|
|
661
|
+
uint64_t m = mag;
|
|
662
|
+
while (m % 2 == 0 && u2 < 64) {
|
|
663
|
+
m /= 2;
|
|
664
|
+
u2++;
|
|
665
|
+
}
|
|
666
|
+
m = mag;
|
|
667
|
+
while (m % 5 == 0 && u5 < 64) {
|
|
668
|
+
m /= 5;
|
|
669
|
+
u5++;
|
|
670
|
+
}
|
|
671
|
+
if (k + u2 < b || k + u5 < c) return false;
|
|
672
|
+
return mag % (uint64_t)mm == 0;
|
|
283
673
|
}
|
|
284
674
|
|
|
285
675
|
static int sv_to_st(const sch_svalue *v) {
|
|
@@ -302,6 +692,35 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
|
|
|
302
692
|
return;
|
|
303
693
|
}
|
|
304
694
|
|
|
695
|
+
/* one annotation frame per application; a $dynamicAnchor /
|
|
696
|
+
* $recursiveAnchor on this schema object enters the dynamic
|
|
697
|
+
* scope for the instance path below it */
|
|
698
|
+
frame_push(vc, v);
|
|
699
|
+
int scopes_base = vc->n_scopes;
|
|
700
|
+
scope_push(vc, n->dynamic_anchor, n);
|
|
701
|
+
scope_push(vc, n->recursive_anchor, n);
|
|
702
|
+
|
|
703
|
+
/* a dynamic $ref resolves through the innermost scope entered on
|
|
704
|
+
* the evaluation path; the compile-resolved target is the static
|
|
705
|
+
* fallback */
|
|
706
|
+
if (n->dynamic_ref != NULL) {
|
|
707
|
+
const char *name = n->dynamic_ref[0] == '#' ? n->dynamic_ref + 1
|
|
708
|
+
: n->dynamic_ref;
|
|
709
|
+
sch_node *target = scope_resolve(vc, name);
|
|
710
|
+
if (target == NULL) target = n->dynamic_ref_target;
|
|
711
|
+
if (target != NULL) {
|
|
712
|
+
if (vc->ref_depth >= MAX_DEPTH) {
|
|
713
|
+
verr(vc, "cyclic dynamic $ref recursion");
|
|
714
|
+
} else {
|
|
715
|
+
vc->ref_depth++;
|
|
716
|
+
spush(vc, "$dynamicRef");
|
|
717
|
+
val(vc, target, v);
|
|
718
|
+
spop(vc);
|
|
719
|
+
vc->ref_depth--;
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
|
|
305
724
|
if (n->ref != NULL) {
|
|
306
725
|
/* a $ref cycle that consumes no document structure (a schema
|
|
307
726
|
* that is its own ref target) would loop forever; legal
|
|
@@ -319,6 +738,8 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
|
|
|
319
738
|
|
|
320
739
|
if (n->is_false) {
|
|
321
740
|
verr(vc, "no value is allowed here (the false schema)");
|
|
741
|
+
vc->n_scopes = scopes_base;
|
|
742
|
+
frame_pop(vc);
|
|
322
743
|
return;
|
|
323
744
|
}
|
|
324
745
|
|
|
@@ -385,13 +806,7 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
|
|
|
385
806
|
if (!ok) verr(vc, "value is not below the exclusive maximum");
|
|
386
807
|
}
|
|
387
808
|
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
|
-
}
|
|
809
|
+
bool ok = sch_mult_ok(n, v);
|
|
395
810
|
if (!ok) verr(vc, "value is not a multiple of multipleOf");
|
|
396
811
|
}
|
|
397
812
|
}
|
|
@@ -411,9 +826,10 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
|
|
|
411
826
|
!sch_regex_is_match(n->pattern, v->str, v->str_len)) {
|
|
412
827
|
verr(vc, "string does not match the pattern");
|
|
413
828
|
}
|
|
414
|
-
if (n->format != FMT_NONE && !format_ok(n->format, v)) {
|
|
829
|
+
if (n->format != FMT_NONE && !format_ok(vc, n->format, v)) {
|
|
415
830
|
verr(vc, "string does not match the format");
|
|
416
831
|
}
|
|
832
|
+
check_content(vc, n, v);
|
|
417
833
|
}
|
|
418
834
|
|
|
419
835
|
/* object */
|
|
@@ -443,7 +859,9 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
|
|
|
443
859
|
matched_known = true;
|
|
444
860
|
spush_named(vc, "properties", key);
|
|
445
861
|
path_push(vc, key);
|
|
862
|
+
int p0 = vc->pings;
|
|
446
863
|
val(vc, n->props[p].schema, mv);
|
|
864
|
+
if (vc->pings == p0) frame_add_key(vc, key);
|
|
447
865
|
path_pop(vc);
|
|
448
866
|
spop(vc);
|
|
449
867
|
break;
|
|
@@ -455,7 +873,9 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
|
|
|
455
873
|
matched_known = true;
|
|
456
874
|
spush(vc, "patternProperties");
|
|
457
875
|
path_push(vc, key);
|
|
876
|
+
int p0 = vc->pings;
|
|
458
877
|
val(vc, n->pprops[p].schema, mv);
|
|
878
|
+
if (vc->pings == p0) frame_add_key(vc, key);
|
|
459
879
|
path_pop(vc);
|
|
460
880
|
spop(vc);
|
|
461
881
|
}
|
|
@@ -467,7 +887,9 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
|
|
|
467
887
|
verr(vc, "unknown property '%s' (additionalProperties is false)",
|
|
468
888
|
key);
|
|
469
889
|
} else {
|
|
890
|
+
int p0 = vc->pings;
|
|
470
891
|
val(vc, n->additional, mv);
|
|
892
|
+
if (vc->pings == p0) frame_add_key(vc, key);
|
|
471
893
|
}
|
|
472
894
|
path_pop(vc);
|
|
473
895
|
spop(vc);
|
|
@@ -546,32 +968,70 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
|
|
|
546
968
|
if (i < n->n_prefix) {
|
|
547
969
|
spush(vc, "prefixItems");
|
|
548
970
|
path_push_idx(vc, (long)i);
|
|
971
|
+
int p0 = vc->pings;
|
|
549
972
|
val(vc, n->prefix[i], v->items[i]);
|
|
973
|
+
if (vc->pings == p0) frame_add_idx(vc, i);
|
|
550
974
|
path_pop(vc);
|
|
551
975
|
spop(vc);
|
|
552
976
|
} else if (n->items != NULL) {
|
|
553
977
|
spush(vc, "items");
|
|
554
978
|
path_push_idx(vc, (long)i);
|
|
979
|
+
int p0 = vc->pings;
|
|
555
980
|
val(vc, n->items, v->items[i]);
|
|
981
|
+
if (vc->pings == p0) frame_add_idx(vc, i);
|
|
982
|
+
path_pop(vc);
|
|
983
|
+
spop(vc);
|
|
984
|
+
} else if (n->add_items != NULL) {
|
|
985
|
+
spush(vc, "additionalItems");
|
|
986
|
+
path_push_idx(vc, (long)i);
|
|
987
|
+
if (n->add_items->is_false) {
|
|
988
|
+
verr(vc, "array has items beyond the tuple "
|
|
989
|
+
"(additionalItems is false)");
|
|
990
|
+
} else {
|
|
991
|
+
int p0 = vc->pings;
|
|
992
|
+
val(vc, n->add_items, v->items[i]);
|
|
993
|
+
if (vc->pings == p0) frame_add_idx(vc, i);
|
|
994
|
+
}
|
|
556
995
|
path_pop(vc);
|
|
557
996
|
spop(vc);
|
|
558
997
|
}
|
|
559
998
|
}
|
|
560
|
-
} else if (n->
|
|
561
|
-
/*
|
|
562
|
-
|
|
563
|
-
|
|
999
|
+
} else if (n->add_items != NULL) {
|
|
1000
|
+
/* additionalItems with no tuple: every position */
|
|
1001
|
+
for (size_t i = 0; i < v->n_items; i++) {
|
|
1002
|
+
spush(vc, "additionalItems");
|
|
1003
|
+
path_push_idx(vc, (long)i);
|
|
1004
|
+
if (n->add_items->is_false) {
|
|
1005
|
+
verr(vc, "array has items beyond the tuple "
|
|
1006
|
+
"(additionalItems is false)");
|
|
1007
|
+
} else {
|
|
1008
|
+
int p0 = vc->pings;
|
|
1009
|
+
val(vc, n->add_items, v->items[i]);
|
|
1010
|
+
if (vc->pings == p0) frame_add_idx(vc, i);
|
|
1011
|
+
}
|
|
1012
|
+
path_pop(vc);
|
|
1013
|
+
spop(vc);
|
|
564
1014
|
}
|
|
565
1015
|
}
|
|
566
1016
|
if (n->has_contains && n->contains != NULL) {
|
|
567
1017
|
long hits = 0;
|
|
568
1018
|
for (size_t i = 0; i < v->n_items; i++) {
|
|
569
|
-
if (passes(vc, n->contains, v->items[i]))
|
|
1019
|
+
if (passes(vc, n->contains, v->items[i])) {
|
|
1020
|
+
hits++;
|
|
1021
|
+
}
|
|
570
1022
|
}
|
|
571
1023
|
long min = n->has_min_contains ? n->min_contains : 1;
|
|
572
1024
|
long max = n->has_max_contains ? n->max_contains : (long)v->n_items;
|
|
573
1025
|
if (hits < min) verr(vc, "fewer matching items than minContains");
|
|
574
1026
|
if (hits > max) verr(vc, "more matching items than maxContains");
|
|
1027
|
+
/* a successful contains annotates the matching indices */
|
|
1028
|
+
if (hits >= min && hits <= max) {
|
|
1029
|
+
for (size_t i = 0; i < v->n_items; i++) {
|
|
1030
|
+
if (passes(vc, n->contains, v->items[i])) {
|
|
1031
|
+
frame_add_idx(vc, i);
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
575
1035
|
}
|
|
576
1036
|
}
|
|
577
1037
|
|
|
@@ -585,7 +1045,9 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
|
|
|
585
1045
|
}
|
|
586
1046
|
if (n->n_any > 0) {
|
|
587
1047
|
bool ok = false;
|
|
588
|
-
|
|
1048
|
+
/* every branch is tried: a successful branch's annotations
|
|
1049
|
+
* bubble up even when an earlier branch already passed */
|
|
1050
|
+
for (size_t i = 0; i < n->n_any; i++) {
|
|
589
1051
|
if (passes(vc, n->any_of[i], v)) ok = true;
|
|
590
1052
|
}
|
|
591
1053
|
if (!ok) verr(vc, "does not match any of the %zu anyOf schemas",
|
|
@@ -602,11 +1064,13 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
|
|
|
602
1064
|
}
|
|
603
1065
|
}
|
|
604
1066
|
if (n->_not != NULL) {
|
|
605
|
-
if (
|
|
1067
|
+
if (passes_annot_free(vc, n->_not, v)) {
|
|
606
1068
|
verr(vc, "must not match the not-schema");
|
|
607
1069
|
}
|
|
608
1070
|
}
|
|
609
1071
|
if (n->if_s != NULL) {
|
|
1072
|
+
/* the if-trial's annotations are retained (the suite: an if
|
|
1073
|
+
* without then/else still evaluates what it matched) */
|
|
610
1074
|
bool cond = passes(vc, n->if_s, v);
|
|
611
1075
|
if (cond && n->then_s != NULL) {
|
|
612
1076
|
spush(vc, "then");
|
|
@@ -618,6 +1082,49 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
|
|
|
618
1082
|
spop(vc);
|
|
619
1083
|
}
|
|
620
1084
|
}
|
|
1085
|
+
|
|
1086
|
+
/* unevaluatedProperties/Items: evaluated LAST against this
|
|
1087
|
+
* application's own frame (adjacent keywords plus what bubbled
|
|
1088
|
+
* up through in-place applicators); what they apply to then
|
|
1089
|
+
* annotates the frame for any enclosing unevaluated* */
|
|
1090
|
+
if (n->uneval_props != NULL && v->kind == SV_TABLE) {
|
|
1091
|
+
struct eval_frame *f = &vc->frames[vc->n_frames - 1];
|
|
1092
|
+
for (size_t m = 0; m < v->n_members; m++) {
|
|
1093
|
+
const char *key = v->members[m].key;
|
|
1094
|
+
if (frame_key_seen(f, key)) continue;
|
|
1095
|
+
spush(vc, "unevaluatedProperties");
|
|
1096
|
+
path_push(vc, key);
|
|
1097
|
+
if (n->uneval_props->is_false) {
|
|
1098
|
+
verr(vc, "unevaluated property '%s' is not allowed", key);
|
|
1099
|
+
} else {
|
|
1100
|
+
int p0 = vc->pings;
|
|
1101
|
+
val(vc, n->uneval_props, v->members[m].val);
|
|
1102
|
+
if (vc->pings == p0) frame_add_key(vc, key);
|
|
1103
|
+
}
|
|
1104
|
+
path_pop(vc);
|
|
1105
|
+
spop(vc);
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
if (n->uneval_items != NULL && v->kind == SV_ARRAY) {
|
|
1109
|
+
struct eval_frame *f = &vc->frames[vc->n_frames - 1];
|
|
1110
|
+
for (size_t i = 0; i < v->n_items; i++) {
|
|
1111
|
+
if (frame_idx_seen(f, i)) continue;
|
|
1112
|
+
spush(vc, "unevaluatedItems");
|
|
1113
|
+
path_push_idx(vc, (long)i);
|
|
1114
|
+
if (n->uneval_items->is_false) {
|
|
1115
|
+
verr(vc, "array has an unevaluated item at index %zu", i);
|
|
1116
|
+
} else {
|
|
1117
|
+
int p0 = vc->pings;
|
|
1118
|
+
val(vc, n->uneval_items, v->items[i]);
|
|
1119
|
+
if (vc->pings == p0) frame_add_idx(vc, i);
|
|
1120
|
+
}
|
|
1121
|
+
path_pop(vc);
|
|
1122
|
+
spop(vc);
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
vc->n_scopes = scopes_base;
|
|
1127
|
+
frame_pop(vc);
|
|
621
1128
|
}
|
|
622
1129
|
|
|
623
1130
|
/* -------------------------------------------------- DOM conversion -- */
|
|
@@ -662,6 +1169,7 @@ static sch_svalue *node_to_sv(teptris_arena *scratch, const teptris_node *node)
|
|
|
662
1169
|
sch_svalue *v = sv_new(scratch, SV_DATETIME);
|
|
663
1170
|
teptris_datetime dt;
|
|
664
1171
|
teptris_node_datetime(node, &dt);
|
|
1172
|
+
v->has_offset = kind == TEPTRIS_DATETIME_OFFSET;
|
|
665
1173
|
/* the RFC 3339 text: equality + format checks run on the
|
|
666
1174
|
* canonical text the emitter would produce */
|
|
667
1175
|
char *buf = teptris_arena_alloc(scratch, 64);
|
|
@@ -841,6 +1349,8 @@ TEPTRIS_API teptris_status teptris_schema_validate_doc(
|
|
|
841
1349
|
*out_len = out_n;
|
|
842
1350
|
}
|
|
843
1351
|
if (vc.buf != NULL) free(vc.buf);
|
|
1352
|
+
frames_free(&vc);
|
|
1353
|
+
free(vc.scopes);
|
|
844
1354
|
teptris_arena_destroy(&scratch);
|
|
845
1355
|
return st;
|
|
846
1356
|
}
|