teptris 0.3.21.0-aarch64-linux → 0.3.23.0-aarch64-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.
@@ -30,6 +30,32 @@ typedef struct {
30
30
  char sseg_buf[64][64];
31
31
  int sdepth;
32
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: one frame
47
+ * per applied schema object, in evaluation order. Each entry
48
+ * names its anchor and the frame owner (the applied object), so
49
+ * resolution walks RESOURCES outermost-first (2020-12 8.2.3.2:
50
+ * the outermost resource in the dynamic scope that defines the
51
+ * fragment wins, its first anchor in document order) */
52
+ struct dyn_scope {
53
+ const char *name;
54
+ sch_node *node;
55
+ const jv *res; /* the anchor's resource (nearest $id) */
56
+ sch_node *owner; /* the applied object that entered it */
57
+ } *scopes;
58
+ int n_scopes, cap_scopes;
33
59
  } vctx;
34
60
 
35
61
  /* ---------------------------------------------------------- output -- */
@@ -168,6 +194,187 @@ static void verr(vctx *vc, const char *fmt, ...) {
168
194
  vc->n_errors++;
169
195
  }
170
196
 
197
+ /* ------------------------------------------- annotation frames (2020-12) */
198
+
199
+ static bool frame_grow(vctx *vc) {
200
+ if (vc->n_frames == vc->cap_frames) {
201
+ vc->cap_frames = vc->cap_frames == 0 ? 16 : vc->cap_frames * 2;
202
+ void *nf = realloc(vc->frames, vc->cap_frames * sizeof(*vc->frames));
203
+ if (nf == NULL) return false;
204
+ vc->frames = nf;
205
+ }
206
+ return true;
207
+ }
208
+
209
+ static bool frame_push(vctx *vc, const sch_svalue *v) {
210
+ if (!frame_grow(vc)) return false;
211
+ struct eval_frame *f = &vc->frames[vc->n_frames++];
212
+ memset(f, 0, sizeof(*f));
213
+ f->inst = v;
214
+ return true;
215
+ }
216
+
217
+ /* merge this frame's annotations into the parent (bubbling up through
218
+ * an in-place applicator); answers false when there is no parent or
219
+ * the parent annotated a different instance (cousins/descendants) */
220
+ static void frame_merge_up(vctx *vc) {
221
+ if (vc->n_frames < 2) return;
222
+ struct eval_frame *f = &vc->frames[vc->n_frames - 1];
223
+ struct eval_frame *p = &vc->frames[vc->n_frames - 2];
224
+ if (p->inst != f->inst) return;
225
+ if (f->n_keys > 0) {
226
+ if (p->n_keys + f->n_keys > p->cap_keys) {
227
+ size_t want = p->cap_keys == 0 ? 8 : p->cap_keys;
228
+ while (want < p->n_keys + f->n_keys) want *= 2;
229
+ void *nk = realloc(p->keys, want * sizeof(*p->keys));
230
+ if (nk == NULL) return;
231
+ p->keys = nk;
232
+ p->cap_keys = want;
233
+ }
234
+ memcpy(p->keys + p->n_keys, f->keys, f->n_keys * sizeof(*f->keys));
235
+ p->n_keys += f->n_keys;
236
+ }
237
+ if (f->n_idx > 0) {
238
+ if (p->n_idx + f->n_idx > p->cap_idx) {
239
+ size_t want = p->cap_idx == 0 ? 8 : p->cap_idx;
240
+ while (want < p->n_idx + f->n_idx) want *= 2;
241
+ void *ni = realloc(p->idx, want * sizeof(*p->idx));
242
+ if (ni == NULL) return;
243
+ p->idx = ni;
244
+ p->cap_idx = want;
245
+ }
246
+ memcpy(p->idx + p->n_idx, f->idx, f->n_idx * sizeof(*f->idx));
247
+ p->n_idx += f->n_idx;
248
+ }
249
+ }
250
+
251
+ /* pop the top frame: bubble its annotations into the parent, then
252
+ * release its arrays (a slot above n_frames would never be revisited
253
+ * by frames_free) */
254
+ static void frame_pop(vctx *vc) {
255
+ frame_merge_up(vc);
256
+ struct eval_frame *f = &vc->frames[vc->n_frames - 1];
257
+ free(f->keys);
258
+ free(f->idx);
259
+ vc->n_frames--;
260
+ }
261
+
262
+ static bool frame_key_seen(const struct eval_frame *f, const char *key) {
263
+ for (size_t i = 0; i < f->n_keys; i++) {
264
+ if (strcmp(f->keys[i], key) == 0) return true;
265
+ }
266
+ return false;
267
+ }
268
+
269
+ static bool frame_idx_seen(const struct eval_frame *f, size_t idx) {
270
+ for (size_t i = 0; i < f->n_idx; i++) {
271
+ if (f->idx[i] == idx) return true;
272
+ }
273
+ return false;
274
+ }
275
+
276
+ static void frame_add_key(vctx *vc, const char *key) {
277
+ if (vc->n_frames == 0) return;
278
+ struct eval_frame *f = &vc->frames[vc->n_frames - 1];
279
+ if (frame_key_seen(f, key)) return;
280
+ if (f->n_keys == f->cap_keys) {
281
+ f->cap_keys = f->cap_keys == 0 ? 8 : f->cap_keys * 2;
282
+ void *nk = realloc(f->keys, f->cap_keys * sizeof(*f->keys));
283
+ if (nk == NULL) return;
284
+ f->keys = nk;
285
+ }
286
+ f->keys[f->n_keys++] = key;
287
+ }
288
+
289
+ static void frame_add_idx(vctx *vc, size_t idx) {
290
+ if (vc->n_frames == 0) return;
291
+ struct eval_frame *f = &vc->frames[vc->n_frames - 1];
292
+ if (frame_idx_seen(f, idx)) return;
293
+ if (f->n_idx == f->cap_idx) {
294
+ f->cap_idx = f->cap_idx == 0 ? 8 : f->cap_idx * 2;
295
+ void *ni = realloc(f->idx, f->cap_idx * sizeof(*f->idx));
296
+ if (ni == NULL) return;
297
+ f->idx = ni;
298
+ }
299
+ f->idx[f->n_idx++] = idx;
300
+ }
301
+
302
+ /* drop the annotations a trial contributed to its merge target (the
303
+ * parent frame): failed anyOf/oneOf branches and the not/if trials
304
+ * annotate nothing */
305
+ static void frame_truncate_to(vctx *vc, size_t n_keys, size_t n_idx) {
306
+ if (vc->n_frames < 1) return;
307
+ struct eval_frame *f = &vc->frames[vc->n_frames - 1];
308
+ if (f->n_keys > n_keys) f->n_keys = n_keys;
309
+ if (f->n_idx > n_idx) f->n_idx = n_idx;
310
+ }
311
+
312
+ static void frames_free(vctx *vc) {
313
+ for (int i = 0; i < vc->n_frames; i++) {
314
+ free(vc->frames[i].keys);
315
+ free(vc->frames[i].idx);
316
+ }
317
+ free(vc->frames);
318
+ }
319
+
320
+ /* ------------------------------------------------- dynamic scopes -- */
321
+
322
+ static bool scope_push(vctx *vc, const char *name, sch_node *node,
323
+ const jv *res, sch_node *owner) {
324
+ if (name == NULL || node == NULL || owner == NULL) return true;
325
+ if (vc->n_scopes == vc->cap_scopes) {
326
+ vc->cap_scopes = vc->cap_scopes == 0 ? 16 : vc->cap_scopes * 2;
327
+ void *ns = realloc(vc->scopes, vc->cap_scopes * sizeof(*vc->scopes));
328
+ if (ns == NULL) return false;
329
+ vc->scopes = ns;
330
+ }
331
+ vc->scopes[vc->n_scopes].name = name;
332
+ vc->scopes[vc->n_scopes].node = node;
333
+ vc->scopes[vc->n_scopes].res = res;
334
+ vc->scopes[vc->n_scopes].owner = owner;
335
+ vc->n_scopes++;
336
+ return true;
337
+ }
338
+
339
+ /* 2020-12 $dynamicRef: walk the RESOURCES of the evaluation path
340
+ * outermost-first (the resource of each applied object, in frame
341
+ * order); the first resource that DEFINES an identically-named
342
+ * $dynamicAnchor — the anchor's nearest-$id resource anywhere in the
343
+ * stack — wins, and its first matching anchor in document order is
344
+ * the target (2020-12 8.2.3.2 + appendix C) */
345
+ static sch_node *scope_resolve(vctx *vc, const char *name) {
346
+ for (int i = 0; i < vc->n_scopes; i++) {
347
+ const jv *res = vc->scopes[i].owner->res_id;
348
+ bool seen = false;
349
+ for (int k = 0; k < i; k++) {
350
+ if (vc->scopes[k].owner->res_id == res) {
351
+ seen = true;
352
+ break;
353
+ }
354
+ }
355
+ if (seen) continue;
356
+ for (int j = 0; j < vc->n_scopes; j++) {
357
+ if (vc->scopes[j].res != res) continue;
358
+ if (strcmp(vc->scopes[j].name, name) == 0) {
359
+ return vc->scopes[j].node;
360
+ }
361
+ }
362
+ }
363
+ return NULL;
364
+ }
365
+
366
+ /* 2019-09 $recursiveRef: the first resource on the evaluation path
367
+ * (outermost first) whose ROOT carries $recursiveAnchor: true; the
368
+ * target is that resource's root */
369
+ static sch_node *scope_resolve_recursive(vctx *vc) {
370
+ for (int i = 0; i < vc->n_scopes; i++) {
371
+ if (vc->scopes[i].owner->recursive_anchor != NULL) {
372
+ return vc->scopes[i].owner;
373
+ }
374
+ }
375
+ return NULL;
376
+ }
377
+
171
378
  /* --------------------------------------------------- value equality -- */
172
379
 
173
380
  static bool sv_equal(const sch_svalue *a, const sch_svalue *b) {
@@ -179,6 +386,15 @@ static bool sv_equal(const sch_svalue *a, const sch_svalue *b) {
179
386
  * equals a datetime text */
180
387
  int ad = a->kind >= SV_DATETIME;
181
388
  int bd = b->kind >= SV_DATETIME;
389
+ /* numeric identity per JSON Schema: an integer and a float
390
+ * with the same value are equal (bools stay distinct - their
391
+ * kinds differ, so they never reach this branch) */
392
+ if ((a->kind == SV_INT || a->kind == SV_FLOAT) &&
393
+ (b->kind == SV_INT || b->kind == SV_FLOAT)) {
394
+ long double av = a->kind == SV_INT ? (long double)a->i : (long double)a->f;
395
+ long double bv = b->kind == SV_INT ? (long double)b->i : (long double)b->f;
396
+ return av == bv;
397
+ }
182
398
  if ((ad || bd) && (a->kind == SV_STRING || b->kind == SV_STRING)) {
183
399
  const sch_svalue *dt = ad ? a : b;
184
400
  const sch_svalue *st = ad ? b : a;
@@ -239,21 +455,57 @@ static bool sv_equal(const sch_svalue *a, const sch_svalue *b) {
239
455
  static sch_svalue *jv_to_sv(vctx *vc, jv *v);
240
456
  static void val(vctx *vc, sch_node *n, const sch_svalue *v);
241
457
 
458
+ /* trial: does n accept v? Failed trials contribute no errors AND no
459
+ * annotations; successful anyOf/oneOf branches bubble theirs up */
242
460
  static bool passes(vctx *vc, sch_node *n, const sch_svalue *v) {
243
461
  int save_pings = vc->pings;
244
462
  size_t save_n = vc->n;
245
463
  int save_depth = vc->sdepth;
464
+ size_t keys_before = 0, idx_before = 0;
465
+ if (vc->n_frames > 0) {
466
+ keys_before = vc->frames[vc->n_frames - 1].n_keys;
467
+ idx_before = vc->frames[vc->n_frames - 1].n_idx;
468
+ }
246
469
  vc->mute++;
247
470
  val(vc, n, v);
248
471
  vc->mute--;
249
- if (vc->pings == save_pings) {
250
- return true;
472
+ bool ok = vc->pings == save_pings;
473
+ /* the trial's pings were its own private signal: nested oneOf/
474
+ * anyOf/not/if trials legitimately produced some without the
475
+ * branch failing - consume them so an OUTER trial sees only what
476
+ * this branch really contributed */
477
+ vc->pings = save_pings;
478
+ if (!ok) {
479
+ /* discard the trial's errors and its annotations */
480
+ vc->n = save_n;
481
+ if (vc->buf != NULL) vc->buf[vc->n] = '\0';
482
+ vc->sdepth = save_depth;
483
+ frame_truncate_to(vc, keys_before, idx_before);
251
484
  }
252
- /* discard the trial's errors: rewind the buffer to save_n */
485
+ return ok;
486
+ }
487
+
488
+ /* not/if trials: the branch runs for the verdict only - its
489
+ * annotations are discarded whether it passed or failed */
490
+ static bool passes_annot_free(vctx *vc, sch_node *n, const sch_svalue *v) {
491
+ int save_pings = vc->pings;
492
+ size_t save_n = vc->n;
493
+ int save_depth = vc->sdepth;
494
+ size_t keys_before = 0, idx_before = 0;
495
+ if (vc->n_frames > 0) {
496
+ keys_before = vc->frames[vc->n_frames - 1].n_keys;
497
+ idx_before = vc->frames[vc->n_frames - 1].n_idx;
498
+ }
499
+ bool ok;
500
+ vc->mute++;
501
+ ok = (val(vc, n, v), vc->pings == save_pings);
502
+ vc->mute--;
503
+ vc->pings = save_pings; /* consumed: the signal was private */
253
504
  vc->n = save_n;
254
505
  if (vc->buf != NULL) vc->buf[vc->n] = '\0';
255
506
  vc->sdepth = save_depth;
256
- return false;
507
+ frame_truncate_to(vc, keys_before, idx_before);
508
+ return ok;
257
509
  }
258
510
 
259
511
  static size_t cp_count(const char *s, size_t len) {
@@ -271,14 +523,18 @@ static size_t cp_count(const char *s, size_t len) {
271
523
  return count;
272
524
  }
273
525
 
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 */
526
+ /* the TOML-native kind table (tombi's named formats made explicit):
527
+ * an offset datetime satisfies date-time and only date-time; a local
528
+ * datetime satisfies date-time-local; a local time satisfies
529
+ * time-local (TOML has no offset time); a date satisfies date */
277
530
  static bool format_ok(vctx *vc, int format, const sch_svalue *v) {
278
531
  if (format == FMT_NONE) return true;
279
532
  if (v->kind == SV_DATE) return format == FMT_DATE;
280
- if (v->kind == SV_TIME) return format == FMT_TIME;
281
- if (v->kind == SV_DATETIME) return format == FMT_DATETIME;
533
+ if (v->kind == SV_TIME) return format == FMT_TIME_LOCAL;
534
+ if (v->kind == SV_DATETIME) {
535
+ return v->has_offset ? format == FMT_DATETIME
536
+ : format == FMT_DATETIME_LOCAL;
537
+ }
282
538
  if (v->kind != SV_STRING) return format == FMT_NONE;
283
539
  return sch_format_string_check(format, v->str, v->str_len, vc->scratch);
284
540
  }
@@ -312,6 +568,33 @@ static int b64_val(char c) {
312
568
  return -1;
313
569
  }
314
570
 
571
+ /* decode base64 text into the scratch arena; answers the length and
572
+ * NULL when the text is not valid base64 */
573
+ static char *b64_decode(vctx *vc, const char *text, size_t tlen,
574
+ size_t *out_len) {
575
+ if (!str_is_base64(text, tlen)) return NULL;
576
+ size_t dn = tlen / 4 * 3;
577
+ if (tlen > 0 && text[tlen - 1] == '=') dn--;
578
+ if (tlen > 1 && text[tlen - 2] == '=') dn--;
579
+ char *out = teptris_arena_alloc(vc->scratch, dn + 1);
580
+ size_t o = 0;
581
+ int acc = 0, bits = 0;
582
+ for (size_t i = 0; i < tlen && o < dn; i++) {
583
+ if (text[i] == '=') break;
584
+ int d = b64_val(text[i]);
585
+ if (d < 0) break;
586
+ acc = (acc << 6) | d;
587
+ bits += 6;
588
+ if (bits >= 8) {
589
+ bits -= 8;
590
+ out[o++] = (char)((acc >> bits) & 0xFF);
591
+ }
592
+ }
593
+ out[o] = '\0';
594
+ *out_len = o;
595
+ return out;
596
+ }
597
+
315
598
  /* contentEncoding/contentMediaType, the taplo parity pair: base64 is
316
599
  * the one encoding checked (and decoded for the media step),
317
600
  * application/json the one media type parsed; unknown names on either
@@ -328,49 +611,49 @@ static void check_content(vctx *vc, const sch_node *n, const sch_svalue *v) {
328
611
  }
329
612
  if (n->content_media_type == NULL ||
330
613
  strcmp(n->content_media_type, "application/json") != 0) {
614
+ /* no media interpretation: contentSchema sees the decoded
615
+ * text when an encoding was applied, the raw string when not */
616
+ if (n->content_schema != NULL) {
617
+ const char *payload = text;
618
+ size_t plen = tlen;
619
+ char *out = NULL;
620
+ if (b64 && str_is_base64(text, tlen)) {
621
+ out = b64_decode(vc, text, tlen, &plen);
622
+ payload = out;
623
+ }
624
+ if (payload != NULL) {
625
+ sch_svalue sv;
626
+ memset(&sv, 0, sizeof sv);
627
+ sv.kind = SV_STRING;
628
+ sv.str = payload;
629
+ sv.str_len = plen;
630
+ spush(vc, "contentSchema");
631
+ val(vc, n->content_schema, &sv);
632
+ spop(vc);
633
+ }
634
+ }
331
635
  return; /* unknown media names: annotation */
332
636
  }
333
637
  const char *payload = text;
334
638
  size_t plen = tlen;
335
639
  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;
640
+ /* the media check runs on the decoded text only when it
641
+ * decodes (the crate skips the check otherwise) */
642
+ payload = b64_decode(vc, text, tlen, &plen);
643
+ if (payload == NULL) return;
370
644
  }
371
645
  char jerr[64] = {0};
372
- if (json_read(vc->scratch, payload, plen, jerr, sizeof jerr) == NULL) {
646
+ jv *parsed = json_read(vc->scratch, payload, plen, jerr, sizeof jerr);
647
+ if (parsed == NULL) {
373
648
  verr(vc, "string is not valid JSON (contentMediaType)");
649
+ return;
650
+ }
651
+ if (n->content_schema != NULL) {
652
+ /* contentSchema asserts the parsed value (the media type's
653
+ * interpretation of the decoded content) */
654
+ spush(vc, "contentSchema");
655
+ val(vc, n->content_schema, jv_to_sv(vc, parsed));
656
+ spop(vc);
374
657
  }
375
658
  }
376
659
 
@@ -442,15 +725,31 @@ static int sv_to_st(const sch_svalue *v) {
442
725
  switch (v->kind) {
443
726
  case SV_STRING: return ST_STRING;
444
727
  case SV_INT: return ST_INTEGER | ST_NUMBER;
445
- case SV_FLOAT: return ST_NUMBER;
728
+ case SV_FLOAT:
729
+ /* a float with a zero fractional part is an integer (the
730
+ * 2020-12 rule the suite tests) */
731
+ return (v->f == v->f && v->f != INFINITY && v->f != -INFINITY &&
732
+ v->f == (double)(int64_t)v->f)
733
+ ? ST_INTEGER | ST_NUMBER
734
+ : ST_NUMBER;
446
735
  case SV_BOOL: return ST_BOOLEAN;
447
736
  case SV_TABLE: return ST_OBJECT;
448
737
  case SV_ARRAY: return ST_ARRAY;
449
738
  case SV_DATETIME: case SV_DATE: case SV_TIME: return ST_STRING;
739
+ case SV_NULL: return ST_NULL;
450
740
  default: return 0;
451
741
  }
452
742
  }
453
743
 
744
+ /* one application = one scope frame: every anchor inside the applied
745
+ * schema's own subtree is in scope below it, owned by this object */
746
+ static void scope_frame_push(vctx *vc, sch_node *n) {
747
+ for (size_t i = 0; i < n->n_self_scope; i++) {
748
+ scope_push(vc, n->self_scope[i].name, n->self_scope[i].node,
749
+ n->self_scope[i].res, n);
750
+ }
751
+ }
752
+
454
753
  static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
455
754
  if (n == NULL || v == NULL) return;
456
755
  if (vc->depth >= MAX_DEPTH) {
@@ -458,6 +757,36 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
458
757
  return;
459
758
  }
460
759
 
760
+ /* one annotation frame per application; a $dynamicAnchor /
761
+ * $recursiveAnchor on this schema object enters the dynamic
762
+ * scope for the instance path below it */
763
+ frame_push(vc, v);
764
+ int scopes_base = vc->n_scopes;
765
+ scope_frame_push(vc, n);
766
+
767
+ /* a dynamic $ref resolves through the innermost scope entered on
768
+ * the evaluation path; the compile-resolved target is the static
769
+ * fallback */
770
+ if (n->dynamic_ref != NULL) {
771
+ const char *name = n->dynamic_ref[0] == '#' ? n->dynamic_ref + 1
772
+ : n->dynamic_ref;
773
+ sch_node *target = n->dynamic_recursive ? scope_resolve_recursive(vc)
774
+ : scope_resolve(vc, name);
775
+ if (target == NULL) target = n->dynamic_ref_target;
776
+ if (target != NULL) {
777
+ if (vc->ref_depth >= MAX_DEPTH) {
778
+ verr(vc, "cyclic dynamic $ref recursion");
779
+ } else {
780
+ vc->ref_depth++;
781
+ spush(vc, "$dynamicRef");
782
+ val(vc, target, v);
783
+ vc->n_scopes = scopes_base;
784
+ spop(vc);
785
+ vc->ref_depth--;
786
+ }
787
+ }
788
+ }
789
+
461
790
  if (n->ref != NULL) {
462
791
  /* a $ref cycle that consumes no document structure (a schema
463
792
  * that is its own ref target) would loop forever; legal
@@ -468,13 +797,22 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
468
797
  vc->ref_depth++;
469
798
  spush(vc, "$ref");
470
799
  val(vc, n->ref, v);
800
+ vc->n_scopes = scopes_base;
471
801
  spop(vc);
472
802
  vc->ref_depth--;
473
803
  }
804
+ /* draft 07 and older: the $ref replaces every sibling */
805
+ if (n->ref_only) {
806
+ vc->n_scopes = scopes_base;
807
+ frame_pop(vc);
808
+ return;
809
+ }
474
810
  }
475
811
 
476
812
  if (n->is_false) {
477
813
  verr(vc, "no value is allowed here (the false schema)");
814
+ vc->n_scopes = scopes_base;
815
+ frame_pop(vc);
478
816
  return;
479
817
  }
480
818
 
@@ -502,6 +840,9 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
502
840
  verr(vc, "value does not equal the constant");
503
841
  }
504
842
  }
843
+ if (n->enum_empty) {
844
+ verr(vc, "value does not match the empty enum (nothing matches)");
845
+ }
505
846
  if (n->n_enum > 0) {
506
847
  bool hit = false;
507
848
  for (size_t i = 0; i < n->n_enum && !hit; i++) {
@@ -594,7 +935,9 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
594
935
  matched_known = true;
595
936
  spush_named(vc, "properties", key);
596
937
  path_push(vc, key);
938
+ int p0 = vc->pings;
597
939
  val(vc, n->props[p].schema, mv);
940
+ if (vc->pings == p0) frame_add_key(vc, key);
598
941
  path_pop(vc);
599
942
  spop(vc);
600
943
  break;
@@ -606,7 +949,9 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
606
949
  matched_known = true;
607
950
  spush(vc, "patternProperties");
608
951
  path_push(vc, key);
952
+ int p0 = vc->pings;
609
953
  val(vc, n->pprops[p].schema, mv);
954
+ if (vc->pings == p0) frame_add_key(vc, key);
610
955
  path_pop(vc);
611
956
  spop(vc);
612
957
  }
@@ -618,7 +963,9 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
618
963
  verr(vc, "unknown property '%s' (additionalProperties is false)",
619
964
  key);
620
965
  } else {
966
+ int p0 = vc->pings;
621
967
  val(vc, n->additional, mv);
968
+ if (vc->pings == p0) frame_add_key(vc, key);
622
969
  }
623
970
  path_pop(vc);
624
971
  spop(vc);
@@ -697,13 +1044,17 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
697
1044
  if (i < n->n_prefix) {
698
1045
  spush(vc, "prefixItems");
699
1046
  path_push_idx(vc, (long)i);
1047
+ int p0 = vc->pings;
700
1048
  val(vc, n->prefix[i], v->items[i]);
1049
+ if (vc->pings == p0) frame_add_idx(vc, i);
701
1050
  path_pop(vc);
702
1051
  spop(vc);
703
1052
  } else if (n->items != NULL) {
704
1053
  spush(vc, "items");
705
1054
  path_push_idx(vc, (long)i);
1055
+ int p0 = vc->pings;
706
1056
  val(vc, n->items, v->items[i]);
1057
+ if (vc->pings == p0) frame_add_idx(vc, i);
707
1058
  path_pop(vc);
708
1059
  spop(vc);
709
1060
  } else if (n->add_items != NULL) {
@@ -713,7 +1064,9 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
713
1064
  verr(vc, "array has items beyond the tuple "
714
1065
  "(additionalItems is false)");
715
1066
  } else {
1067
+ int p0 = vc->pings;
716
1068
  val(vc, n->add_items, v->items[i]);
1069
+ if (vc->pings == p0) frame_add_idx(vc, i);
717
1070
  }
718
1071
  path_pop(vc);
719
1072
  spop(vc);
@@ -728,7 +1081,9 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
728
1081
  verr(vc, "array has items beyond the tuple "
729
1082
  "(additionalItems is false)");
730
1083
  } else {
1084
+ int p0 = vc->pings;
731
1085
  val(vc, n->add_items, v->items[i]);
1086
+ if (vc->pings == p0) frame_add_idx(vc, i);
732
1087
  }
733
1088
  path_pop(vc);
734
1089
  spop(vc);
@@ -737,12 +1092,22 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
737
1092
  if (n->has_contains && n->contains != NULL) {
738
1093
  long hits = 0;
739
1094
  for (size_t i = 0; i < v->n_items; i++) {
740
- if (passes(vc, n->contains, v->items[i])) hits++;
1095
+ if (passes(vc, n->contains, v->items[i])) {
1096
+ hits++;
1097
+ }
741
1098
  }
742
1099
  long min = n->has_min_contains ? n->min_contains : 1;
743
1100
  long max = n->has_max_contains ? n->max_contains : (long)v->n_items;
744
1101
  if (hits < min) verr(vc, "fewer matching items than minContains");
745
1102
  if (hits > max) verr(vc, "more matching items than maxContains");
1103
+ /* a successful contains annotates the matching indices */
1104
+ if (hits >= min && hits <= max) {
1105
+ for (size_t i = 0; i < v->n_items; i++) {
1106
+ if (passes(vc, n->contains, v->items[i])) {
1107
+ frame_add_idx(vc, i);
1108
+ }
1109
+ }
1110
+ }
746
1111
  }
747
1112
  }
748
1113
 
@@ -756,7 +1121,9 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
756
1121
  }
757
1122
  if (n->n_any > 0) {
758
1123
  bool ok = false;
759
- for (size_t i = 0; i < n->n_any && !ok; i++) {
1124
+ /* every branch is tried: a successful branch's annotations
1125
+ * bubble up even when an earlier branch already passed */
1126
+ for (size_t i = 0; i < n->n_any; i++) {
760
1127
  if (passes(vc, n->any_of[i], v)) ok = true;
761
1128
  }
762
1129
  if (!ok) verr(vc, "does not match any of the %zu anyOf schemas",
@@ -773,11 +1140,13 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
773
1140
  }
774
1141
  }
775
1142
  if (n->_not != NULL) {
776
- if (passes(vc, n->_not, v)) {
1143
+ if (passes_annot_free(vc, n->_not, v)) {
777
1144
  verr(vc, "must not match the not-schema");
778
1145
  }
779
1146
  }
780
1147
  if (n->if_s != NULL) {
1148
+ /* the if-trial's annotations are retained (the suite: an if
1149
+ * without then/else still evaluates what it matched) */
781
1150
  bool cond = passes(vc, n->if_s, v);
782
1151
  if (cond && n->then_s != NULL) {
783
1152
  spush(vc, "then");
@@ -789,6 +1158,49 @@ static void val(vctx *vc, sch_node *n, const sch_svalue *v) {
789
1158
  spop(vc);
790
1159
  }
791
1160
  }
1161
+
1162
+ /* unevaluatedProperties/Items: evaluated LAST against this
1163
+ * application's own frame (adjacent keywords plus what bubbled
1164
+ * up through in-place applicators); what they apply to then
1165
+ * annotates the frame for any enclosing unevaluated* */
1166
+ if (n->uneval_props != NULL && v->kind == SV_TABLE) {
1167
+ struct eval_frame *f = &vc->frames[vc->n_frames - 1];
1168
+ for (size_t m = 0; m < v->n_members; m++) {
1169
+ const char *key = v->members[m].key;
1170
+ if (frame_key_seen(f, key)) continue;
1171
+ spush(vc, "unevaluatedProperties");
1172
+ path_push(vc, key);
1173
+ if (n->uneval_props->is_false) {
1174
+ verr(vc, "unevaluated property '%s' is not allowed", key);
1175
+ } else {
1176
+ int p0 = vc->pings;
1177
+ val(vc, n->uneval_props, v->members[m].val);
1178
+ if (vc->pings == p0) frame_add_key(vc, key);
1179
+ }
1180
+ path_pop(vc);
1181
+ spop(vc);
1182
+ }
1183
+ }
1184
+ if (n->uneval_items != NULL && v->kind == SV_ARRAY) {
1185
+ struct eval_frame *f = &vc->frames[vc->n_frames - 1];
1186
+ for (size_t i = 0; i < v->n_items; i++) {
1187
+ if (frame_idx_seen(f, i)) continue;
1188
+ spush(vc, "unevaluatedItems");
1189
+ path_push_idx(vc, (long)i);
1190
+ if (n->uneval_items->is_false) {
1191
+ verr(vc, "array has an unevaluated item at index %zu", i);
1192
+ } else {
1193
+ int p0 = vc->pings;
1194
+ val(vc, n->uneval_items, v->items[i]);
1195
+ if (vc->pings == p0) frame_add_idx(vc, i);
1196
+ }
1197
+ path_pop(vc);
1198
+ spop(vc);
1199
+ }
1200
+ }
1201
+
1202
+ vc->n_scopes = scopes_base;
1203
+ frame_pop(vc);
792
1204
  }
793
1205
 
794
1206
  /* -------------------------------------------------- DOM conversion -- */
@@ -833,6 +1245,7 @@ static sch_svalue *node_to_sv(teptris_arena *scratch, const teptris_node *node)
833
1245
  sch_svalue *v = sv_new(scratch, SV_DATETIME);
834
1246
  teptris_datetime dt;
835
1247
  teptris_node_datetime(node, &dt);
1248
+ v->has_offset = kind == TEPTRIS_DATETIME_OFFSET;
836
1249
  /* the RFC 3339 text: equality + format checks run on the
837
1250
  * canonical text the emitter would produce */
838
1251
  char *buf = teptris_arena_alloc(scratch, 64);
@@ -1012,6 +1425,8 @@ TEPTRIS_API teptris_status teptris_schema_validate_doc(
1012
1425
  *out_len = out_n;
1013
1426
  }
1014
1427
  if (vc.buf != NULL) free(vc.buf);
1428
+ frames_free(&vc);
1429
+ free(vc.scopes);
1015
1430
  teptris_arena_destroy(&scratch);
1016
1431
  return st;
1017
1432
  }