yeptris 0.6.64.0-riscv64-linux-gnu

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.
Files changed (164) hide show
  1. checksums.yaml +7 -0
  2. data/README.adoc +150 -0
  3. data/ext/build_windows_native.rb +60 -0
  4. data/ext/libyeptris/extconf.rb +57 -0
  5. data/ext/yeptris_native/cbor_ruby.c +198 -0
  6. data/ext/yeptris_native/extconf.rb +99 -0
  7. data/ext/yeptris_native/json_ruby.c +414 -0
  8. data/ext/yeptris_native/version.h +29 -0
  9. data/ext/yeptris_native/yeptris_native.c +507 -0
  10. data/lib/yeptris/3.3/native.so +0 -0
  11. data/lib/yeptris/cbor.rb +169 -0
  12. data/lib/yeptris/document.rb +319 -0
  13. data/lib/yeptris/ffi.rb +572 -0
  14. data/lib/yeptris/json/descriptor.rb +266 -0
  15. data/lib/yeptris/json.rb +502 -0
  16. data/lib/yeptris/materializer.rb +389 -0
  17. data/lib/yeptris/node.rb +482 -0
  18. data/lib/yeptris/psych/class_loader.rb +24 -0
  19. data/lib/yeptris/psych/coder_shim.rb +55 -0
  20. data/lib/yeptris/psych/drop_in.rb +63 -0
  21. data/lib/yeptris/psych/encodable.rb +14 -0
  22. data/lib/yeptris/psych/handler.rb +95 -0
  23. data/lib/yeptris/psych/parser.rb +114 -0
  24. data/lib/yeptris/psych/scalar_scanner.rb +149 -0
  25. data/lib/yeptris/psych/tree_builder.rb +119 -0
  26. data/lib/yeptris/psych/visitors.rb +670 -0
  27. data/lib/yeptris/psych.rb +796 -0
  28. data/lib/yeptris/schema.rb +253 -0
  29. data/lib/yeptris/validation.rb +320 -0
  30. data/lib/yeptris/valueml.rb +477 -0
  31. data/lib/yeptris/yaml/descriptor.rb +56 -0
  32. data/lib/yeptris/yaml.rb +668 -0
  33. data/lib/yeptris.rb +130 -0
  34. data/libyeptris.so +0 -0
  35. data/vendor/libyeptris/CMakeLists.txt +280 -0
  36. data/vendor/libyeptris/cmake/yeptris-config.cmake.in +5 -0
  37. data/vendor/libyeptris/cmake/yeptris.pc.in +11 -0
  38. data/vendor/libyeptris/src/CMakeLists.txt +183 -0
  39. data/vendor/libyeptris/src/include/yeptris/api.h +32 -0
  40. data/vendor/libyeptris/src/include/yeptris/cbor.h +174 -0
  41. data/vendor/libyeptris/src/include/yeptris/dom.h +196 -0
  42. data/vendor/libyeptris/src/include/yeptris/emit.h +128 -0
  43. data/vendor/libyeptris/src/include/yeptris/error.h +42 -0
  44. data/vendor/libyeptris/src/include/yeptris/events.h +171 -0
  45. data/vendor/libyeptris/src/include/yeptris/json.h +57 -0
  46. data/vendor/libyeptris/src/include/yeptris/json.hpp +258 -0
  47. data/vendor/libyeptris/src/include/yeptris/jsonc_compat.h +115 -0
  48. data/vendor/libyeptris/src/include/yeptris/marshal.h +75 -0
  49. data/vendor/libyeptris/src/include/yeptris/parse.h +121 -0
  50. data/vendor/libyeptris/src/include/yeptris/plan.h +100 -0
  51. data/vendor/libyeptris/src/include/yeptris/resolve.h +81 -0
  52. data/vendor/libyeptris/src/include/yeptris/schema.h +121 -0
  53. data/vendor/libyeptris/src/include/yeptris/tape.h +128 -0
  54. data/vendor/libyeptris/src/include/yeptris/types.h +38 -0
  55. data/vendor/libyeptris/src/include/yeptris/validate.h +401 -0
  56. data/vendor/libyeptris/src/include/yeptris/values.h +108 -0
  57. data/vendor/libyeptris/src/include/yeptris/version.h.in +29 -0
  58. data/vendor/libyeptris/src/include/yeptris/visit.h +77 -0
  59. data/vendor/libyeptris/src/include/yeptris/yajl_compat.h +135 -0
  60. data/vendor/libyeptris/src/include/yeptris.h +24 -0
  61. data/vendor/libyeptris/src/yeptris/build.c +393 -0
  62. data/vendor/libyeptris/src/yeptris/cbor/cbor.h +35 -0
  63. data/vendor/libyeptris/src/yeptris/cbor/decode.c +1054 -0
  64. data/vendor/libyeptris/src/yeptris/cbor/encode.c +836 -0
  65. data/vendor/libyeptris/src/yeptris/cbor/encode_priv.h +32 -0
  66. data/vendor/libyeptris/src/yeptris/cbor/from_marshal.c +982 -0
  67. data/vendor/libyeptris/src/yeptris/cbor/sink.h +36 -0
  68. data/vendor/libyeptris/src/yeptris/common/chartype.c +41 -0
  69. data/vendor/libyeptris/src/yeptris/common/chartype.h +77 -0
  70. data/vendor/libyeptris/src/yeptris/common/cpu.c +81 -0
  71. data/vendor/libyeptris/src/yeptris/common/cpu.h +40 -0
  72. data/vendor/libyeptris/src/yeptris/common/error.c +65 -0
  73. data/vendor/libyeptris/src/yeptris/common/error.h +81 -0
  74. data/vendor/libyeptris/src/yeptris/common/mutex.h +45 -0
  75. data/vendor/libyeptris/src/yeptris/common/nametab.c +275 -0
  76. data/vendor/libyeptris/src/yeptris/common/nametab.h +68 -0
  77. data/vendor/libyeptris/src/yeptris/common/port.h +74 -0
  78. data/vendor/libyeptris/src/yeptris/common/simd_text.c +49 -0
  79. data/vendor/libyeptris/src/yeptris/common/simd_text.h +342 -0
  80. data/vendor/libyeptris/src/yeptris/common/simd_text_avx2.c +672 -0
  81. data/vendor/libyeptris/src/yeptris/common/simd_text_neon.c +793 -0
  82. data/vendor/libyeptris/src/yeptris/common/simd_text_scalar.c +324 -0
  83. data/vendor/libyeptris/src/yeptris/common/string_view.c +34 -0
  84. data/vendor/libyeptris/src/yeptris/common/string_view.h +93 -0
  85. data/vendor/libyeptris/src/yeptris/doc.h +86 -0
  86. data/vendor/libyeptris/src/yeptris/dom/dom.c +1312 -0
  87. data/vendor/libyeptris/src/yeptris/dom/dom.h +332 -0
  88. data/vendor/libyeptris/src/yeptris/dom/hpool.c +107 -0
  89. data/vendor/libyeptris/src/yeptris/dom/mapindex.c +173 -0
  90. data/vendor/libyeptris/src/yeptris/dom/mapindex.h +61 -0
  91. data/vendor/libyeptris/src/yeptris/dom/mutate.c +431 -0
  92. data/vendor/libyeptris/src/yeptris/dom/ytape.c +2323 -0
  93. data/vendor/libyeptris/src/yeptris/dom/ytape.h +96 -0
  94. data/vendor/libyeptris/src/yeptris/emit/emit.c +670 -0
  95. data/vendor/libyeptris/src/yeptris/emit/float/api.h +43 -0
  96. data/vendor/libyeptris/src/yeptris/emit/float/dragon.c +405 -0
  97. data/vendor/libyeptris/src/yeptris/emit/float/floatint.h +231 -0
  98. data/vendor/libyeptris/src/yeptris/emit/float/fpconv.c +428 -0
  99. data/vendor/libyeptris/src/yeptris/emit/float/print.c +396 -0
  100. data/vendor/libyeptris/src/yeptris/emit/style.c +21 -0
  101. data/vendor/libyeptris/src/yeptris/emit/style.h +32 -0
  102. data/vendor/libyeptris/src/yeptris/emit/writer.c +1951 -0
  103. data/vendor/libyeptris/src/yeptris/emit/writer.h +111 -0
  104. data/vendor/libyeptris/src/yeptris/encoding/bom.c +31 -0
  105. data/vendor/libyeptris/src/yeptris/encoding/encoding.h +63 -0
  106. data/vendor/libyeptris/src/yeptris/encoding/transcode.c +157 -0
  107. data/vendor/libyeptris/src/yeptris/encoding/utf8_validate.c +178 -0
  108. data/vendor/libyeptris/src/yeptris/events/capture.c +166 -0
  109. data/vendor/libyeptris/src/yeptris/events/capture.h +39 -0
  110. data/vendor/libyeptris/src/yeptris/events/iterparse.c +298 -0
  111. data/vendor/libyeptris/src/yeptris/events/pull.c +117 -0
  112. data/vendor/libyeptris/src/yeptris/events/push.c +151 -0
  113. data/vendor/libyeptris/src/yeptris/events/recorder.c +144 -0
  114. data/vendor/libyeptris/src/yeptris/events/values.c +630 -0
  115. data/vendor/libyeptris/src/yeptris/events/values_priv.h +53 -0
  116. data/vendor/libyeptris/src/yeptris/events/yaml_compat.c +86 -0
  117. data/vendor/libyeptris/src/yeptris/events/yaml_compat.h +90 -0
  118. data/vendor/libyeptris/src/yeptris/jsonapi/jsonc_compat.c +687 -0
  119. data/vendor/libyeptris/src/yeptris/jsonapi/yajl_compat.c +728 -0
  120. data/vendor/libyeptris/src/yeptris/marshal/to_json.c +650 -0
  121. data/vendor/libyeptris/src/yeptris/marshal.c +988 -0
  122. data/vendor/libyeptris/src/yeptris/memory/allocator.c +20 -0
  123. data/vendor/libyeptris/src/yeptris/memory/allocator.h +43 -0
  124. data/vendor/libyeptris/src/yeptris/memory/arena.c +150 -0
  125. data/vendor/libyeptris/src/yeptris/memory/arena.h +52 -0
  126. data/vendor/libyeptris/src/yeptris/memory/pool.c +133 -0
  127. data/vendor/libyeptris/src/yeptris/memory/pool.h +36 -0
  128. data/vendor/libyeptris/src/yeptris/parse/engine.c +6528 -0
  129. data/vendor/libyeptris/src/yeptris/parse/engine.h +96 -0
  130. data/vendor/libyeptris/src/yeptris/parse/events.h +137 -0
  131. data/vendor/libyeptris/src/yeptris/parse/numbers.c +374 -0
  132. data/vendor/libyeptris/src/yeptris/parse/numbers.h +33 -0
  133. data/vendor/libyeptris/src/yeptris/parse/scalars.c +435 -0
  134. data/vendor/libyeptris/src/yeptris/parse/scalars.h +87 -0
  135. data/vendor/libyeptris/src/yeptris/parse/stream_iter.c +358 -0
  136. data/vendor/libyeptris/src/yeptris/parse.c +1074 -0
  137. data/vendor/libyeptris/src/yeptris/plan.c +824 -0
  138. data/vendor/libyeptris/src/yeptris/resolve/compat11.c +339 -0
  139. data/vendor/libyeptris/src/yeptris/resolve/core12.c +153 -0
  140. data/vendor/libyeptris/src/yeptris/resolve/full12.c +201 -0
  141. data/vendor/libyeptris/src/yeptris/resolve/json12.c +109 -0
  142. data/vendor/libyeptris/src/yeptris/resolve/resolver.h +103 -0
  143. data/vendor/libyeptris/src/yeptris/resolve/tags.c +250 -0
  144. data/vendor/libyeptris/src/yeptris/scan/json.c +839 -0
  145. data/vendor/libyeptris/src/yeptris/scan/json.h +116 -0
  146. data/vendor/libyeptris/src/yeptris/scan/scan.c +862 -0
  147. data/vendor/libyeptris/src/yeptris/scan/scan.h +177 -0
  148. data/vendor/libyeptris/src/yeptris/schema.c +309 -0
  149. data/vendor/libyeptris/src/yeptris/tape.c +2375 -0
  150. data/vendor/libyeptris/src/yeptris/tape_in.h +39 -0
  151. data/vendor/libyeptris/src/yeptris/validate/compile.c +306 -0
  152. data/vendor/libyeptris/src/yeptris/validate/internal.h +347 -0
  153. data/vendor/libyeptris/src/yeptris/validate/jsonschema.c +1922 -0
  154. data/vendor/libyeptris/src/yeptris/validate/output.c +246 -0
  155. data/vendor/libyeptris/src/yeptris/validate/regexp.c +1600 -0
  156. data/vendor/libyeptris/src/yeptris/validate/registry.c +118 -0
  157. data/vendor/libyeptris/src/yeptris/validate/resources.c +601 -0
  158. data/vendor/libyeptris/src/yeptris/validate/stream.c +1155 -0
  159. data/vendor/libyeptris/src/yeptris/validate/validate.c +1043 -0
  160. data/vendor/libyeptris/src/yeptris/version.c +7 -0
  161. data/vendor/libyeptris/src/yeptris/visit/dom_visit.c +115 -0
  162. data/vendor/libyeptris/src/yeptris/visit/json_visit.c +293 -0
  163. data/vendor/libyeptris/src/yeptris/visit/yaml_visit.c +127 -0
  164. metadata +221 -0
@@ -0,0 +1,1043 @@
1
+ /* validate.c — the schema-family validation walk (#882 slice 1).
2
+ *
3
+ * One engine pass builds the DOM (the eager route: input-borrowed
4
+ * spans, no tape), one walk checks it against the family:
5
+ *
6
+ * - both families: explicit-tag consistency (the tag asserts a type
7
+ * the content cannot carry) and mapping-key uniqueness (spec
8
+ * 3.2.1.3: keys compare by canonical form — 0o13 and 0xB are ONE
9
+ * key; strings byte-wise; collections structurally);
10
+ * - 12_JSON only: mapping keys must be string scalars; plain scalars
11
+ * must match a JSON production (spec 10.2.2's `*` -> Error row);
12
+ * explicit non-JSON tags rejected;
13
+ * - 12_FULL only: the core checks plus the full family's timestamp
14
+ * type — explicit !!timestamp content must re-resolve through the
15
+ * full12 classifier (spec 2.22's four forms), and untagged plains
16
+ * resolve timestamps at parse time (the resolver, not this walk).
17
+ * !!binary/!!merge/!!value/consumer tags ride as passthrough
18
+ * (spec 10.4: arbitrary explicit tags never invalidate a schema).
19
+ *
20
+ * 11_COMPAT is the parse-compat resolver, not a validation family —
21
+ * the entry rejects it (ARG).
22
+ *
23
+ * MECE: parse/ owns the grammar (this walk only reads finished
24
+ * nodes), resolve/ owns typing (the family resolvers), emit/ owns
25
+ * bytes; diagnostics shape lives in validate.h. Allocation: the
26
+ * report and the scratch (line index, path, dedup table) come from
27
+ * the system allocator and are freed before return — nothing borrows
28
+ * the caller's buffer beyond the call.
29
+ */
30
+
31
+ #include <stddef.h>
32
+ #include <stdio.h>
33
+ #include <stdlib.h>
34
+ #include <string.h>
35
+
36
+ #include <yeptris/dom.h>
37
+ #include <yeptris/validate.h>
38
+
39
+ #include "../common/error.h"
40
+ #include "../doc.h"
41
+ #include "../memory/allocator.h"
42
+ #include "../parse/numbers.h"
43
+ #include "../resolve/resolver.h"
44
+ #include "../tape_in.h"
45
+ #include "internal.h"
46
+
47
+ /* ---- line/col resolution ---------------------------------------------- */
48
+
49
+ /* Builds the line-start index once, on the first positioned diagnostic;
50
+ * valid documents never pay. Returns 0 ok, -1 OOM (positions unknown). */
51
+ static int line_index_build(yep_vctx* c) {
52
+ if (c->line_off != NULL) {
53
+ return 0;
54
+ }
55
+ const yep_allocator* sys = yep_system_allocator();
56
+ uint32_t cap = 256;
57
+ uint32_t n = 0;
58
+ uint32_t* off = yep_alloc(sys, cap * sizeof(uint32_t));
59
+ if (off == NULL) {
60
+ return -1;
61
+ }
62
+ off[n++] = 0;
63
+ const char* p = c->input;
64
+ for (size_t i = 0; i < c->input_len; i++) {
65
+ if (p[i] == '\n') {
66
+ if (n == cap) {
67
+ uint32_t ncap = cap * 2;
68
+ uint32_t* no = yep_alloc(sys, (size_t)ncap * sizeof(uint32_t));
69
+ if (no == NULL) {
70
+ yep_free(sys, off);
71
+ return -1;
72
+ }
73
+ memcpy(no, off, (size_t)n * sizeof(uint32_t));
74
+ yep_free(sys, off);
75
+ off = no;
76
+ cap = ncap;
77
+ }
78
+ off[n++] = (uint32_t)(i + 1);
79
+ }
80
+ }
81
+ c->line_off = off;
82
+ c->line_cnt = n;
83
+ c->line_cap = cap;
84
+ return 0;
85
+ }
86
+
87
+ /* 1-based line/col of a byte offset; columns are code points (the
88
+ * libyaml mark semantics — multibyte characters advance one). */
89
+ static void line_col_of(yep_vctx* c, uint32_t off, uint32_t* line, uint32_t* col) {
90
+ *line = 0;
91
+ *col = 0;
92
+ if (off == UINT32_MAX || off >= c->input_len) {
93
+ return;
94
+ }
95
+ if (line_index_build(c) != 0) {
96
+ return;
97
+ }
98
+ uint32_t lo = 0, hi = c->line_cnt; /* find the last line start <= off */
99
+ while (lo + 1 < hi) {
100
+ uint32_t mid = lo + (hi - lo) / 2;
101
+ if (c->line_off[mid] <= off) {
102
+ lo = mid;
103
+ } else {
104
+ hi = mid;
105
+ }
106
+ }
107
+ uint32_t start = c->line_off[lo];
108
+ uint32_t chars = 0;
109
+ for (uint32_t i = start; i <= off; i++) {
110
+ chars += ((((unsigned char)c->input[i]) & 0xC0) != 0x80);
111
+ }
112
+ *line = lo + 1;
113
+ *col = chars;
114
+ }
115
+
116
+ /* ---- path scratch ------------------------------------------------------ */
117
+
118
+ static int path_reserve(yep_vctx* c, size_t need) {
119
+ if (c->path_len + need <= c->path_cap) {
120
+ return 0;
121
+ }
122
+ const yep_allocator* sys = yep_system_allocator();
123
+ size_t ncap = c->path_cap ? c->path_cap * 2 : 256;
124
+ while (ncap < c->path_len + need) {
125
+ ncap *= 2;
126
+ }
127
+ char* np = yep_alloc(sys, ncap);
128
+ if (np == NULL) {
129
+ c->oom = 1;
130
+ return -1;
131
+ }
132
+ if (c->path != NULL) {
133
+ memcpy(np, c->path, c->path_len);
134
+ yep_free(sys, c->path);
135
+ }
136
+ c->path = np;
137
+ c->path_cap = ncap;
138
+ return 0;
139
+ }
140
+
141
+ int yep_validate_path_push(yep_vctx* c, const char* seg, size_t seg_len) {
142
+ /* JSON-pointer escapes: ~ -> ~0, / -> ~1 */
143
+ size_t need = 1 + seg_len + 2; /* worst case: every byte escapes */
144
+ for (size_t i = 0; i < seg_len; i++) {
145
+ if (seg[i] == '~' || seg[i] == '/') {
146
+ need++;
147
+ }
148
+ }
149
+ if (path_reserve(c, need) != 0) {
150
+ return -1;
151
+ }
152
+ c->path[c->path_len++] = '/';
153
+ for (size_t i = 0; i < seg_len; i++) {
154
+ char b = seg[i];
155
+ if (b == '~') {
156
+ c->path[c->path_len++] = '~';
157
+ c->path[c->path_len++] = '0';
158
+ } else if (b == '/') {
159
+ c->path[c->path_len++] = '~';
160
+ c->path[c->path_len++] = '1';
161
+ } else {
162
+ c->path[c->path_len++] = b;
163
+ }
164
+ }
165
+ return 0;
166
+ }
167
+
168
+ int yep_validate_path_push_index(yep_vctx* c, uint32_t idx) {
169
+ char buf[12];
170
+ int n = snprintf(buf, sizeof(buf), "%u", idx);
171
+ return yep_validate_path_push(c, buf, (size_t)n);
172
+ }
173
+
174
+ void yep_validate_path_pop(yep_vctx* c, size_t mark) {
175
+ c->path_len = mark;
176
+ }
177
+
178
+ size_t yep_validate_doc_prefix(yep_vctx* c, int qualified, uint32_t index) {
179
+ size_t mark = c->path_len;
180
+ if (qualified && yep_validate_path_push_index(c, index) != 0) {
181
+ c->oom = 1; /* the path scratch failed: the walk unwinds */
182
+ }
183
+ return mark;
184
+ }
185
+
186
+ /* ---- the report --------------------------------------------------------- */
187
+
188
+ /* The report-path writer (the cap law's one home). */
189
+ void yep_validate_report_put(yep_vctx* c, const yeptris_diagnostic* d) {
190
+ yeptris_validate_report* r = c->rep;
191
+ if (r->count >= YEP_VALIDATE_MAX_DIAGS) {
192
+ /* the cap: the LAST slot becomes the LIMIT sentinel */
193
+ r->truncated = 1;
194
+ yeptris_diagnostic* last = &r->diags[r->count - 1];
195
+ memset(last, 0, sizeof(*last));
196
+ last->code = YEPTRIS_DIAG_LIMIT;
197
+ snprintf(last->detail, sizeof(last->detail), "diagnostics truncated at %d",
198
+ YEP_VALIDATE_MAX_DIAGS);
199
+ return;
200
+ }
201
+ r->diags[r->count++] = *d;
202
+ }
203
+
204
+ void yep_validate_emit(yep_vctx* c, int code, uint32_t off, const char* detail_fmt, ...) {
205
+ if (c->suppress > 0) {
206
+ c->trial_fail++; /* a probe (anyOf/oneOf/not/if): swallow, fail its own extent */
207
+ return;
208
+ }
209
+ c->fail_mark++; /* the application that owns this extent is invalid */
210
+ yeptris_diagnostic d;
211
+ memset(&d, 0, sizeof(d));
212
+ d.code = (uint32_t)code;
213
+ if (off != UINT32_MAX && off < c->input_len) {
214
+ d.offset = off;
215
+ line_col_of(c, off, &d.line, &d.col);
216
+ }
217
+ size_t plen = c->path_len < YEPTRIS_DIAG_PATH_MAX - 1 ? c->path_len : YEPTRIS_DIAG_PATH_MAX - 1;
218
+ if (c->path != NULL) {
219
+ memcpy(d.path, c->path, plen);
220
+ }
221
+ va_list ap;
222
+ va_start(ap, detail_fmt);
223
+ vsnprintf(d.detail, sizeof(d.detail), detail_fmt, ap);
224
+ va_end(ap);
225
+ if (c->stage_put != NULL) {
226
+ /* the event route's staging (#902): the diagnostic joins the
227
+ * OPEN mapping's stage — the flush replays [own tag][duplicates]
228
+ * [pair content] so the report order stays the DOM walk's.
229
+ * NOTHING is dropped here: the walk position is unknown until
230
+ * the flush order is fixed (a mapping's duplicates precede its
231
+ * content), so the cap law applies at the replay, through
232
+ * yep_validate_report_put — the staging holds O(violations),
233
+ * the same input-bounded class as the DOM route's tree. */
234
+ c->staged++;
235
+ c->stage_put(c->stage, &d);
236
+ return;
237
+ }
238
+ yep_validate_report_put(c, &d);
239
+ }
240
+
241
+ /* ---- node facts --------------------------------------------------------- */
242
+
243
+ uint32_t yep_validate_node_off(const yep_dom* d, uint32_t id) {
244
+ uint32_t guard = 0;
245
+ while (id != UINT32_MAX && guard++ < YEP_DOM_MAX_DEPTH) {
246
+ const yep_dnode* n = yep_dom_node(d, id);
247
+ if (n == NULL) {
248
+ return UINT32_MAX;
249
+ }
250
+ if (n->kind == YEP_DOM_SCALAR || n->kind == YEP_DOM_ALIAS) {
251
+ if (n->value.len > 0 && (n->value.off & YEP_SV_INPUT) == 0) {
252
+ return n->value.off & YEP_SV_OFF; /* borrowed from the input */
253
+ }
254
+ return UINT32_MAX; /* arena-owned bytes: no input position */
255
+ }
256
+ id = n->first_child; /* collections: position at their first leaf */
257
+ }
258
+ return UINT32_MAX;
259
+ }
260
+
261
+ /* ---- canonical key/value facts (shared with jsonschema.c) ---------------- */
262
+
263
+ /* The scalar tag law (internal.h's fact core; the DOM wrapper below
264
+ * and the event route's facts both land here). */
265
+ uint8_t yep_validate_tag_of(uint8_t tag_id, int has_tag, int plainish) {
266
+ if (has_tag) {
267
+ return tag_id;
268
+ }
269
+ /* implicit marks the resolver's verdict even at style ANY (the
270
+ * document-level empty scalar carries no token to style) */
271
+ if (plainish) {
272
+ return tag_id;
273
+ }
274
+ return YEPTRIS_TAG_STR;
275
+ }
276
+
277
+ /* The resolved tag id of a scalar node (explicit tag wins, then the
278
+ * plain verdict already on the node, then str for quoted/block). */
279
+ uint8_t yep_validate_scalar_tag(const yep_dnode* n) {
280
+ return yep_validate_tag_of(n->tag_id, n->tag.len > 0,
281
+ n->style == YEP_STYLE_PLAIN || n->implicit);
282
+ }
283
+
284
+ /* Canonical hash of a node (the key-uniqueness fingerprint): tags and
285
+ * canonical forms, numeric values for int/float. */
286
+ uint64_t yep_validate_hash_mix(uint64_t h, uint64_t v) {
287
+ h ^= v + 0x9E3779B97F4A7C15ULL + (h << 6) + (h >> 2);
288
+ return h;
289
+ }
290
+
291
+ /* The scalar arm (internal.h's law): base + tag; int/float fold the
292
+ * numeric VALUE — one bucket for both tags (1 and 1.0 are the same
293
+ * canonical number) — exact for i64-representable magnitudes up to
294
+ * 2^53, and beyond that two distinct ints may collide (equality is
295
+ * the authority, a collision only costs a probe); everything else
296
+ * folds the raw bytes. */
297
+ uint64_t yep_validate_scalar_digest(uint8_t tag, yep_view v) {
298
+ uint64_t h = YEP_VAL_DIGEST_SCALAR + tag;
299
+ if (tag == YEPTRIS_TAG_INT || tag == YEPTRIS_TAG_FLOAT) {
300
+ double dv = 0;
301
+ int64_t iv = 0;
302
+ if (yep_num_i64(v.p, v.len, &iv) == 0) {
303
+ dv = (double)iv;
304
+ } else {
305
+ yep_num_f64(v.p, v.len, &dv);
306
+ }
307
+ uint64_t bits;
308
+ memcpy(&bits, &dv, sizeof(bits));
309
+ return yep_validate_hash_mix(h, bits);
310
+ }
311
+ for (uint32_t i = 0; i < v.len; i++) {
312
+ h = yep_validate_hash_mix(h, (unsigned char)v.p[i]);
313
+ }
314
+ return h;
315
+ }
316
+
317
+ uint64_t yep_validate_node_hash(const yep_dom* d, uint32_t id, int depth) {
318
+ if (depth > 64) {
319
+ return 0xDA7A;
320
+ }
321
+ const yep_dnode* n = yep_dom_node(d, id);
322
+ if (n == NULL) {
323
+ return 0;
324
+ }
325
+ if (n->kind == YEP_DOM_ALIAS) {
326
+ return n->target != UINT32_MAX ? yep_validate_node_hash(d, n->target, depth + 1)
327
+ : yep_validate_hash_mix(YEP_VAL_DIGEST_ALIAS, n->value.len);
328
+ }
329
+ if (n->kind == YEP_DOM_SEQUENCE) {
330
+ uint64_t h = YEP_VAL_DIGEST_SEQ_SEED;
331
+ uint32_t ch = n->first_child;
332
+ while (ch != UINT32_MAX) {
333
+ h = yep_validate_hash_mix(h, yep_validate_node_hash(d, ch, depth + 1));
334
+ ch = yep_dom_node(d, ch) ? yep_dom_node(d, ch)->next_sibling : UINT32_MAX;
335
+ }
336
+ return h;
337
+ }
338
+ if (n->kind == YEP_DOM_MAPPING) {
339
+ /* mappings are UNORDERED (spec 3.2.1.3 canonical form): the
340
+ * fold is commutative (XOR of per-pair mixes) so key order
341
+ * cannot change the fingerprint */
342
+ uint64_t h = YEP_VAL_DIGEST_MAP_SEED;
343
+ uint32_t k = n->first_child;
344
+ while (k != UINT32_MAX) {
345
+ uint32_t v = yep_dom_node(d, k) ? yep_dom_node(d, k)->next_sibling : UINT32_MAX;
346
+ uint64_t pair = yep_validate_hash_mix(YEP_VAL_DIGEST_PAIR_SEED,
347
+ yep_validate_node_hash(d, k, depth + 1));
348
+ pair = yep_validate_hash_mix(pair, yep_validate_node_hash(d, v, depth + 1));
349
+ h ^= pair;
350
+ k = v != UINT32_MAX && yep_dom_node(d, v) ? yep_dom_node(d, v)->next_sibling
351
+ : UINT32_MAX;
352
+ }
353
+ return h;
354
+ }
355
+ /* scalar: tag + canonical form */
356
+ return yep_validate_scalar_digest(yep_validate_scalar_tag(n), yep_dom_view(d, n->value));
357
+ }
358
+
359
+ /* Canonical equality (spec 3.2.1.3): same tag and canonical form.
360
+ * Collections compare structurally; numbers within one tag compare
361
+ * numerically. The two sides may live in different DOMs (the JSON
362
+ * Schema face compares instances against a schema's enum/const).
363
+ * Depth-bounded like the hashes. */
364
+ int yep_validate_node_equal(const yep_dom* da, uint32_t a, const yep_dom* db, uint32_t b,
365
+ int depth) {
366
+ if (depth > 64) {
367
+ return 0;
368
+ }
369
+ const yep_dnode* na = yep_dom_node(da, a);
370
+ const yep_dnode* nb = yep_dom_node(db, b);
371
+ if (na == NULL || nb == NULL) {
372
+ return 0;
373
+ }
374
+ if (na->kind == YEP_DOM_ALIAS) {
375
+ return na->target != UINT32_MAX &&
376
+ yep_validate_node_equal(da, na->target, db, b, depth + 1);
377
+ }
378
+ if (nb->kind == YEP_DOM_ALIAS) {
379
+ return nb->target != UINT32_MAX &&
380
+ yep_validate_node_equal(da, a, db, nb->target, depth + 1);
381
+ }
382
+ if (na->kind != nb->kind) {
383
+ return 0;
384
+ }
385
+ if (na->kind == YEP_DOM_SEQUENCE) {
386
+ if (na->count != nb->count) {
387
+ return 0;
388
+ }
389
+ uint32_t ca = na->first_child, cb = nb->first_child;
390
+ while (ca != UINT32_MAX && cb != UINT32_MAX) {
391
+ if (!yep_validate_node_equal(da, ca, db, cb, depth + 1)) {
392
+ return 0;
393
+ }
394
+ const yep_dnode* da2 = yep_dom_node(da, ca);
395
+ const yep_dnode* db2 = yep_dom_node(db, cb);
396
+ ca = da2 ? da2->next_sibling : UINT32_MAX;
397
+ cb = db2 ? db2->next_sibling : UINT32_MAX;
398
+ }
399
+ return ca == UINT32_MAX && cb == UINT32_MAX;
400
+ }
401
+ if (na->kind == YEP_DOM_MAPPING) {
402
+ if (na->count != nb->count) {
403
+ return 0;
404
+ }
405
+ uint32_t ka = na->first_child;
406
+ while (ka != UINT32_MAX) {
407
+ uint32_t va = yep_dom_node(da, ka) ? yep_dom_node(da, ka)->next_sibling : UINT32_MAX;
408
+ /* find ka's key in b */
409
+ uint32_t kb = nb->first_child;
410
+ int found = 0;
411
+ while (kb != UINT32_MAX && !found) {
412
+ uint32_t vb =
413
+ yep_dom_node(db, kb) ? yep_dom_node(db, kb)->next_sibling : UINT32_MAX;
414
+ if (yep_validate_node_equal(da, ka, db, kb, depth + 1)) {
415
+ if (!yep_validate_node_equal(da, va, db, vb, depth + 1)) {
416
+ return 0;
417
+ }
418
+ found = 1;
419
+ }
420
+ kb = vb != UINT32_MAX && yep_dom_node(db, vb) ? yep_dom_node(db, vb)->next_sibling
421
+ : UINT32_MAX;
422
+ }
423
+ if (!found) {
424
+ return 0;
425
+ }
426
+ ka = va != UINT32_MAX && yep_dom_node(da, va) ? yep_dom_node(da, va)->next_sibling
427
+ : UINT32_MAX;
428
+ }
429
+ return 1;
430
+ }
431
+ /* scalars: numbers (int or float, either tag) compare by numeric
432
+ * value (2020-12: 0 and 0.0 are one constant); everything else
433
+ * requires the same tag */
434
+ uint8_t ta = yep_validate_scalar_tag(na), tb = yep_validate_scalar_tag(nb);
435
+ int numa = ta == YEPTRIS_TAG_INT || ta == YEPTRIS_TAG_FLOAT;
436
+ int numb = tb == YEPTRIS_TAG_INT || tb == YEPTRIS_TAG_FLOAT;
437
+ if (numa != numb) {
438
+ return 0;
439
+ }
440
+ yep_view va = yep_dom_view(da, na->value);
441
+ yep_view vb = yep_dom_view(db, nb->value);
442
+ if (numa) {
443
+ int64_t ia, ib;
444
+ if (yep_num_i64(va.p, va.len, &ia) == 0 && yep_num_i64(vb.p, vb.len, &ib) == 0) {
445
+ return ia == ib;
446
+ }
447
+ double fa, fb;
448
+ if (yep_num_f64(va.p, va.len, &fa) == 0 && yep_num_f64(vb.p, vb.len, &fb) == 0) {
449
+ return fa == fb;
450
+ }
451
+ return 0;
452
+ }
453
+ if (ta != tb) {
454
+ return 0;
455
+ }
456
+ return va.len == vb.len && (va.len == 0 || memcmp(va.p, vb.p, va.len) == 0);
457
+ }
458
+
459
+ /* ---- the family walk ---------------------------------------------------- */
460
+
461
+ typedef struct {
462
+ uint32_t hash;
463
+ uint32_t node; /* 0 = empty slot (node 0 is the DOCUMENT root, never
464
+ * a key inside itself) */
465
+ } key_slot;
466
+
467
+ /* Duplicate-key detection over one mapping: open addressing, expected
468
+ * O(pairs). An allocation failure silently skips (the walk's other
469
+ * checks still run; oom flags the context). */
470
+ static void check_duplicates(yep_vctx* c, uint32_t map_id) {
471
+ const yep_dom* d = c->dom;
472
+ const yep_dnode* m = yep_dom_node(d, map_id);
473
+ uint32_t pairs = m->count / 2;
474
+ if (pairs < 2) {
475
+ return;
476
+ }
477
+ const yep_allocator* sys = yep_system_allocator();
478
+ uint32_t cap = 8;
479
+ while (cap < pairs * 4) {
480
+ cap *= 2;
481
+ }
482
+ key_slot* tbl = yep_alloc(sys, (size_t)cap * sizeof(key_slot));
483
+ if (tbl == NULL) {
484
+ c->oom = 1;
485
+ return;
486
+ }
487
+ memset(tbl, 0, (size_t)cap * sizeof(key_slot));
488
+ uint32_t k = m->first_child;
489
+ while (k != UINT32_MAX) {
490
+ uint32_t v = yep_dom_node(d, k) ? yep_dom_node(d, k)->next_sibling : UINT32_MAX;
491
+ uint64_t h64 = yep_validate_node_hash(d, k, 0);
492
+ uint32_t h = (uint32_t)(h64 ^ (h64 >> 32));
493
+ uint32_t i = h & (cap - 1);
494
+ while (tbl[i].node != 0) {
495
+ if (tbl[i].hash == h && yep_validate_node_equal(d, tbl[i].node - 1, d, k, 0)) {
496
+ /* enumerate EVERY miss: each key whose canonical form
497
+ * already appeared is its own diagnostic (the report
498
+ * cap bounds a hostile mapping) */
499
+ uint32_t off = yep_validate_node_off(d, k);
500
+ yep_view kv = yep_dom_view(d, yep_dom_node(d, k)->value);
501
+ char key[40];
502
+ size_t kl = kv.len < sizeof(key) - 8 ? kv.len : sizeof(key) - 8;
503
+ if (kl > 0 && kv.p != NULL) {
504
+ memcpy(key, kv.p, kl);
505
+ }
506
+ key[kl] = '\0';
507
+ yep_validate_emit(c, YEPTRIS_DIAG_DUPLICATE_KEY, off,
508
+ "duplicate mapping key (\"%s\")", key);
509
+ break;
510
+ }
511
+ i = (i + 1) & (cap - 1);
512
+ }
513
+ if (tbl[i].node == 0) {
514
+ tbl[i].hash = h;
515
+ tbl[i].node = k + 1; /* +1: node 0 means empty */
516
+ }
517
+ k = v != UINT32_MAX && yep_dom_node(d, v) ? yep_dom_node(d, v)->next_sibling : UINT32_MAX;
518
+ }
519
+ yep_free(sys, tbl);
520
+ }
521
+
522
+ static const char* tag_word(uint8_t id) {
523
+ switch (id) {
524
+ case YEPTRIS_TAG_STR:
525
+ return "string";
526
+ case YEPTRIS_TAG_INT:
527
+ return "int";
528
+ case YEPTRIS_TAG_FLOAT:
529
+ return "float";
530
+ case YEPTRIS_TAG_BOOL:
531
+ return "bool";
532
+ case YEPTRIS_TAG_NULL:
533
+ return "null";
534
+ case YEPTRIS_TAG_TIMESTAMP:
535
+ return "timestamp";
536
+ case YEPTRIS_TAG_SEQ:
537
+ return "sequence";
538
+ case YEPTRIS_TAG_MAP:
539
+ return "mapping";
540
+ case YEPTRIS_TAG_BINARY:
541
+ return "binary";
542
+ case YEPTRIS_TAG_MERGE:
543
+ return "merge";
544
+ case YEPTRIS_TAG_VALUE:
545
+ return "value";
546
+ default:
547
+ return "custom";
548
+ }
549
+ }
550
+
551
+ /* Explicit-tag consistency (both families): the tag asserts a type the
552
+ * content cannot carry. seq/map check the node KIND; the five core
553
+ * scalar tags re-resolve the content through the family resolver.
554
+ * internal.h's fact core — the event route feeds the same five facts. */
555
+ void yep_check_tag_facts(yep_vctx* c, int kind, int has_tag, uint8_t tag_id, yep_view value,
556
+ uint32_t off, const yep_resolver* fam, int json_family) {
557
+ if (!has_tag) {
558
+ return; /* no explicit tag */
559
+ }
560
+ uint8_t want = tag_id;
561
+ const char* w = tag_word(want);
562
+ if (want == YEPTRIS_TAG_SEQ || want == YEPTRIS_TAG_MAP || want == YEPTRIS_TAG_STR) {
563
+ int ok = want == YEPTRIS_TAG_STR ? kind == YEP_DOM_SCALAR
564
+ : (want == YEPTRIS_TAG_SEQ && kind == YEP_DOM_SEQUENCE) ||
565
+ (want == YEPTRIS_TAG_MAP && kind == YEP_DOM_MAPPING);
566
+ if (!ok) {
567
+ yep_validate_emit(c, YEPTRIS_DIAG_TAG_MISMATCH, off, "explicit %s tag on a %s", w,
568
+ kind == YEP_DOM_SCALAR
569
+ ? "scalar"
570
+ : (kind == YEP_DOM_SEQUENCE ? "sequence" : "mapping"));
571
+ }
572
+ return;
573
+ }
574
+ if (want >= YEPTRIS_TAG_INT && want <= YEPTRIS_TAG_NULL) {
575
+ if (kind != YEP_DOM_SCALAR) {
576
+ yep_validate_emit(c, YEPTRIS_DIAG_TAG_MISMATCH, off, "explicit %s tag on a %s", w,
577
+ kind == YEP_DOM_SEQUENCE ? "sequence" : "mapping");
578
+ return;
579
+ }
580
+ uint8_t got = fam->resolve(fam->ctx, value.p != NULL ? value.p : "", value.len);
581
+ if (got != want) {
582
+ if (json_family) {
583
+ yep_validate_emit(c, YEPTRIS_DIAG_TAG_MISMATCH, off,
584
+ "explicit %s tag; content is not a JSON %s", w, w);
585
+ } else {
586
+ yep_validate_emit(c, YEPTRIS_DIAG_TAG_MISMATCH, off,
587
+ "explicit %s tag on %s content", w, tag_word(got));
588
+ }
589
+ }
590
+ return;
591
+ }
592
+ if (want == YEPTRIS_TAG_TIMESTAMP && fam->timestamped) {
593
+ /* the FULL family: timestamp is a family type — the explicit
594
+ * tag asserts content the classifier accepts (the SAME
595
+ * classifier the untagged plain form rides, so the two cannot
596
+ * diverge). Core leaves the tag consumer-defined (spec 10.3's
597
+ * core schema has no timestamp); the JSON family rejects it
598
+ * wholesale (checked by the caller). */
599
+ if (kind != YEP_DOM_SCALAR) {
600
+ yep_validate_emit(c, YEPTRIS_DIAG_TAG_MISMATCH, off, "explicit timestamp tag on a %s",
601
+ kind == YEP_DOM_SEQUENCE ? "sequence" : "mapping");
602
+ return;
603
+ }
604
+ uint8_t got = fam->resolve(fam->ctx, value.p != NULL ? value.p : "", value.len);
605
+ if (got != want) {
606
+ yep_validate_emit(c, YEPTRIS_DIAG_TAG_MISMATCH, off,
607
+ "explicit timestamp tag on %s content", tag_word(got));
608
+ }
609
+ return;
610
+ }
611
+ /* binary/merge/value/custom: consumer-defined under every family
612
+ * (spec 10.4: arbitrary explicit tags ride). The binary
613
+ * PASSTHROUGH policy: !!binary is never family-checked — PyYAML's
614
+ * base64 decode is a CONSTRUCTION step, so resolution and this
615
+ * walk keep their hands off the shaping. */
616
+ }
617
+
618
+ static void check_tag(yep_vctx* c, uint32_t id, const yep_resolver* fam, int json_family) {
619
+ const yep_dom* d = c->dom;
620
+ const yep_dnode* n = yep_dom_node(d, id);
621
+ if (n->tag.len == 0) {
622
+ return;
623
+ }
624
+ yep_check_tag_facts(c, (int)n->kind, 1, n->tag_id, yep_dom_view(d, n->value),
625
+ yep_validate_node_off(d, id), fam, json_family);
626
+ }
627
+
628
+ /* json12: a SCALAR mapping key's checks (internal.h's fact core —
629
+ * the DOM walk and the event route both land here, so the two routes
630
+ * cannot diverge on a verdict). A plain key matching no JSON
631
+ * production IS a string (spec 10.2 example 10.8's "A null"/
632
+ * "Booleans" are plain block keys) — the string-key demand bars only
633
+ * keys resolving to non-string productions. */
634
+ void yep_check_json_key_facts(yep_vctx* c, int has_tag, uint8_t tag_id, int plainish,
635
+ yep_view value, uint32_t off) {
636
+ if (has_tag) {
637
+ uint8_t want = tag_id;
638
+ if (want == YEPTRIS_TAG_STR) {
639
+ return;
640
+ }
641
+ if (want >= YEPTRIS_TAG_INT && want <= YEPTRIS_TAG_NULL) {
642
+ yep_validate_emit(c, YEPTRIS_DIAG_KEY_NOT_STRING, off,
643
+ "mapping key is %s (expected a "
644
+ "string)",
645
+ tag_word(want));
646
+ } else {
647
+ yep_validate_emit(c, YEPTRIS_DIAG_NON_JSON_TYPE, off,
648
+ "mapping key carries a non-JSON "
649
+ "tag");
650
+ }
651
+ return;
652
+ }
653
+ if (!plainish) {
654
+ return; /* quoted/block keys are strings */
655
+ }
656
+ int cls = yep_json12_plain(value.p != NULL ? value.p : "", value.len);
657
+ if (cls != 0 && cls != (int)YEPTRIS_TAG_STR) {
658
+ yep_validate_emit(c, YEPTRIS_DIAG_KEY_NOT_STRING, off,
659
+ "mapping key is %s (expected a "
660
+ "string)",
661
+ tag_word((uint8_t)cls));
662
+ }
663
+ }
664
+
665
+ /* json12: a mapping key must be a string scalar. */
666
+ static void check_json_key(yep_vctx* c, uint32_t key_id) {
667
+ const yep_dom* d = c->dom;
668
+ const yep_dnode* n = yep_dom_node(d, key_id);
669
+ if (n->kind == YEP_DOM_ALIAS) {
670
+ if (n->target != UINT32_MAX) {
671
+ check_json_key(c, n->target);
672
+ }
673
+ return;
674
+ }
675
+ if (n->kind != YEP_DOM_SCALAR) {
676
+ uint32_t off = yep_validate_node_off(d, key_id);
677
+ yep_validate_emit(c, YEPTRIS_DIAG_KEY_NOT_STRING, off, "mapping key is a %s",
678
+ n->kind == YEP_DOM_SEQUENCE ? "sequence" : "mapping");
679
+ return;
680
+ }
681
+ yep_check_json_key_facts(c, n->tag.len > 0, n->tag_id,
682
+ n->implicit || n->style == YEP_STYLE_PLAIN, yep_dom_view(d, n->value),
683
+ yep_validate_node_off(d, key_id));
684
+ }
685
+
686
+ /* json12 scalar facts: only explicit non-JSON tags diagnose — example
687
+ * 10.8 resolves implicit no-production plains as strings (keys AND
688
+ * the "Invalid" list), overriding the table's error note. */
689
+ void yep_check_json_scalar_facts(yep_vctx* c, int has_tag, uint8_t tag_id, int plainish,
690
+ yep_view value, uint32_t off) {
691
+ (void)plainish;
692
+ (void)value;
693
+ if (has_tag) {
694
+ uint8_t want = tag_id;
695
+ if (want == YEPTRIS_TAG_TIMESTAMP || want == YEPTRIS_TAG_BINARY ||
696
+ want == YEPTRIS_TAG_MERGE || want == YEPTRIS_TAG_VALUE || want == YEPTRIS_TAG_CUSTOM) {
697
+ yep_validate_emit(c, YEPTRIS_DIAG_NON_JSON_TYPE, off,
698
+ "explicit %s tag is not a JSON type", tag_word(want));
699
+ }
700
+ }
701
+ }
702
+
703
+ static void check_json_scalar(yep_vctx* c, uint32_t id) {
704
+ const yep_dom* d = c->dom;
705
+ const yep_dnode* n = yep_dom_node(d, id);
706
+ yep_check_json_scalar_facts(c, n->tag.len > 0, n->tag_id,
707
+ n->implicit || n->style == YEP_STYLE_PLAIN,
708
+ yep_dom_view(d, n->value), yep_validate_node_off(d, id));
709
+ }
710
+
711
+ static void walk_family(yep_vctx* c, uint32_t id, const yep_resolver* fam, int json_family);
712
+
713
+ /* The tag-driven schema hook (#906 slice 4): a node whose EXPLICIT tag
714
+ * matches a registry entry has its subtree validated against the bound
715
+ * compiled schema. The walk context carries the tag site's path — the
716
+ * subtree's misses extend it — and the shared report (one cap law).
717
+ * Unregistered tags pass untouched; tagged mapping KEYS never reach
718
+ * here (the walk checks keys in place, no instance path of their
719
+ * own). Registration is the only extension: this reads the registry,
720
+ * it never grows a schema branch (OCP). */
721
+ static void check_tag_schema(yep_vctx* c, uint32_t id, const yep_dnode* n) {
722
+ if (c->oom || c->reg == NULL || n->tag.len == 0) {
723
+ return;
724
+ }
725
+ yep_view tv = yep_dom_view(c->dom, n->tag);
726
+ if (tv.p == NULL) {
727
+ return;
728
+ }
729
+ const yeptris_schema* bound = yeptris_schema_registry_lookup(c->reg, tv.p, tv.len);
730
+ if (bound == NULL) {
731
+ return; /* an unregistered tag is nobody's schema */
732
+ }
733
+ yep_schema_view v;
734
+ yep_schema_view_of(bound, &v);
735
+ if (v.dom == NULL) {
736
+ return;
737
+ }
738
+ yep_jsonschema_apply_subtree(v.dom, v.sroot, c, id, v.cache, v.res);
739
+ }
740
+
741
+ static void walk_children(yep_vctx* c, uint32_t first, const yep_resolver* fam, int json_family) {
742
+ uint32_t ch = first;
743
+ int guard = 0;
744
+ while (ch != UINT32_MAX && !c->oom) {
745
+ if (++guard > (int)c->dom->ncount) {
746
+ break; /* defensive: the sibling list is parse-built */
747
+ }
748
+ const yep_dnode* n = yep_dom_node(c->dom, ch);
749
+ uint32_t next = n ? n->next_sibling : UINT32_MAX;
750
+ walk_family(c, ch, fam, json_family);
751
+ ch = next;
752
+ }
753
+ }
754
+
755
+ static void walk_family(yep_vctx* c, uint32_t id, const yep_resolver* fam, int json_family) {
756
+ if (c->oom) {
757
+ return;
758
+ }
759
+ if (++c->depth > YEP_DOM_MAX_DEPTH) {
760
+ if (c->depth == YEP_DOM_MAX_DEPTH + 1) {
761
+ yep_validate_emit(c, YEPTRIS_DIAG_DEPTH, UINT32_MAX,
762
+ "validation walk exceeded the nesting limit");
763
+ }
764
+ c->depth--;
765
+ return;
766
+ }
767
+ const yep_dnode* n = yep_dom_node(c->dom, id);
768
+ if (n == NULL) {
769
+ c->depth--;
770
+ return;
771
+ }
772
+ switch (n->kind) {
773
+ case YEP_DOM_SCALAR:
774
+ check_tag(c, id, fam, json_family);
775
+ if (json_family) {
776
+ check_json_scalar(c, id);
777
+ }
778
+ check_tag_schema(c, id, n);
779
+ break;
780
+ case YEP_DOM_ALIAS:
781
+ break; /* the target is validated at its own place */
782
+ case YEP_DOM_SEQUENCE: {
783
+ check_tag(c, id, fam, json_family);
784
+ check_tag_schema(c, id, n);
785
+ size_t mark = c->path_len;
786
+ uint32_t idx = 0;
787
+ uint32_t ch = n->first_child;
788
+ while (ch != UINT32_MAX && !c->oom) {
789
+ const yep_dnode* cn = yep_dom_node(c->dom, ch);
790
+ uint32_t next = cn ? cn->next_sibling : UINT32_MAX;
791
+ if (yep_validate_path_push_index(c, idx++) == 0) {
792
+ walk_family(c, ch, fam, json_family);
793
+ yep_validate_path_pop(c, mark);
794
+ }
795
+ ch = next;
796
+ }
797
+ break;
798
+ }
799
+ case YEP_DOM_MAPPING: {
800
+ check_tag(c, id, fam, json_family);
801
+ size_t mark = c->path_len;
802
+ check_duplicates(c, id);
803
+ check_tag_schema(c, id, n);
804
+ uint32_t k = n->first_child;
805
+ while (k != UINT32_MAX && !c->oom) {
806
+ uint32_t v =
807
+ yep_dom_node(c->dom, k) ? yep_dom_node(c->dom, k)->next_sibling : UINT32_MAX;
808
+ if (json_family) {
809
+ check_json_key(c, k);
810
+ }
811
+ /* keys: tag-checked like any scalar; collection keys walk
812
+ * under the mapping's path (their own path is undefined) */
813
+ const yep_dnode* kn = yep_dom_node(c->dom, k);
814
+ if (kn != NULL) {
815
+ if (kn->kind == YEP_DOM_SCALAR) {
816
+ check_tag(c, k, fam, json_family);
817
+ } else if (kn->kind != YEP_DOM_ALIAS) {
818
+ walk_children(c, kn->first_child, fam, json_family);
819
+ }
820
+ }
821
+ if (v != UINT32_MAX) {
822
+ yep_view kv = yep_dom_view(c->dom, kn ? kn->value : (yep_sview){0, 0});
823
+ if (yep_validate_path_push(c, kv.p != NULL ? kv.p : "", kv.len) == 0) {
824
+ walk_family(c, v, fam, json_family);
825
+ yep_validate_path_pop(c, mark);
826
+ }
827
+ }
828
+ k = v != UINT32_MAX && yep_dom_node(c->dom, v) ? yep_dom_node(c->dom, v)->next_sibling
829
+ : UINT32_MAX;
830
+ }
831
+ break;
832
+ }
833
+ default:
834
+ break;
835
+ }
836
+ c->depth--;
837
+ }
838
+
839
+ /* ---- the public entries -------------------------------------------------- */
840
+
841
+ YEPTRIS_API size_t yeptris_validate_report_count(const yeptris_validate_report* report) {
842
+ return report ? report->count : 0;
843
+ }
844
+
845
+ YEPTRIS_API const yeptris_diagnostic*
846
+ yeptris_validate_report_at(const yeptris_validate_report* report, size_t index) {
847
+ if (report == NULL || index >= report->count) {
848
+ return NULL;
849
+ }
850
+ return &report->diags[index];
851
+ }
852
+
853
+ YEPTRIS_API void yeptris_validate_report_free(yeptris_validate_report* report) {
854
+ if (report == NULL) {
855
+ return;
856
+ }
857
+ const yep_allocator* sys = yep_system_allocator();
858
+ yep_free(sys, report->diags);
859
+ yep_free(sys, report);
860
+ }
861
+
862
+ /* The report constructor (internal.h's seam — one home for the shape). */
863
+ yeptris_validate_report* yep_validate_report_new(void) {
864
+ const yep_allocator* sys = yep_system_allocator();
865
+ yeptris_validate_report* r = yep_alloc(sys, sizeof(*r));
866
+ if (r == NULL) {
867
+ return NULL;
868
+ }
869
+ r->diags = yep_alloc(sys, YEP_VALIDATE_MAX_DIAGS * sizeof(yeptris_diagnostic));
870
+ if (r->diags == NULL) {
871
+ yep_free(sys, r);
872
+ return NULL;
873
+ }
874
+ r->count = 0;
875
+ r->truncated = 0;
876
+ return r;
877
+ }
878
+
879
+ /* The shared tail: walk every document of the DOM into a fresh report. */
880
+ static YeptrisStatus validate_dom(yep_dom* dom, const yep_resolver* fam, int json_family,
881
+ const yeptris_schema_registry* reg,
882
+ yeptris_validate_report** out) {
883
+ const yep_allocator* sys = yep_system_allocator();
884
+ yeptris_validate_report* r = yep_validate_report_new();
885
+ if (r == NULL) {
886
+ return YEPTRIS_ERROR_MEMORY;
887
+ }
888
+
889
+ yep_vctx c;
890
+ memset(&c, 0, sizeof(c));
891
+ c.dom = dom;
892
+ c.rep = r;
893
+ c.input = dom->input_base;
894
+ c.input_len = dom->input_len;
895
+ c.reg = reg;
896
+
897
+ for (uint32_t i = 0; i < dom->dcount && !c.oom; i++) {
898
+ c.path_len = 0; /* root-relative per document; a multi-document
899
+ * stream addresses its documents as array
900
+ * elements: "/i" + the instance pointer */
901
+ size_t mark = yep_validate_doc_prefix(&c, dom->dcount > 1, i);
902
+ walk_family(&c, dom->docs[i], fam, json_family);
903
+ yep_validate_path_pop(&c, mark);
904
+ }
905
+
906
+ yep_free(sys, c.line_off);
907
+ yep_free(sys, c.path);
908
+
909
+ YeptrisStatus st = YEPTRIS_OK;
910
+ if (c.oom) {
911
+ yeptris_validate_report_free(r);
912
+ return YEPTRIS_ERROR_MEMORY;
913
+ }
914
+ if (r->count > 0) {
915
+ st = YEPTRIS_ERROR_SCHEMA;
916
+ }
917
+ *out = r;
918
+ return st;
919
+ }
920
+
921
+ /* The family selector (internal.h's seam): 11_COMPAT is the
922
+ * parse-compat resolver, not a validation family. */
923
+ int yep_validate_schema_family(YeptrisSchema schema, const yep_resolver** fam, int* json_family) {
924
+ switch (schema) {
925
+ case YEPTRIS_SCHEMA_12_CORE:
926
+ *fam = yep_resolver_core12();
927
+ *json_family = 0;
928
+ return 1;
929
+ case YEPTRIS_SCHEMA_12_JSON:
930
+ *fam = yep_resolver_json12();
931
+ *json_family = 1;
932
+ return 1;
933
+ case YEPTRIS_SCHEMA_12_FULL:
934
+ *fam = yep_resolver_full12();
935
+ *json_family = 0; /* full rides the core checks + timestamp */
936
+ return 1;
937
+ default:
938
+ return 0;
939
+ }
940
+ }
941
+
942
+ /* The options' registry field (size-versioned: an older/short struct
943
+ * simply carries no registry — the emit-options law). */
944
+ static const yeptris_schema_registry* validate_registry(const yeptris_validate_options* opts) {
945
+ if (opts == NULL || opts->size < (uint32_t)(offsetof(yeptris_validate_options, registry) +
946
+ sizeof(opts->registry))) {
947
+ return NULL;
948
+ }
949
+ return opts->registry;
950
+ }
951
+
952
+ YEPTRIS_API YeptrisStatus yeptris_validate_document_ex(YeptrisDocument doc, YeptrisSchema schema,
953
+ const yeptris_validate_options* opts,
954
+ yeptris_validate_report** out) {
955
+ if (out == NULL) {
956
+ return YEPTRIS_ERROR_ARG;
957
+ }
958
+ *out = NULL;
959
+ const yep_resolver* fam;
960
+ int json_family;
961
+ if (!yep_validate_schema_family(schema, &fam, &json_family)) {
962
+ yep_error_set(yep_error_tls(), YEP_ERR_UNEXPECTED, 0, 0, 0,
963
+ "schema %d is not a validation family", (int)schema);
964
+ return YEPTRIS_ERROR_ARG;
965
+ }
966
+ yep_dom* dom = doc ? yep_doc_dom((yeptris_document*)doc) : NULL;
967
+ if (doc != NULL && dom == NULL) {
968
+ return YEPTRIS_ERROR_MEMORY; /* a lazy tree that failed to build */
969
+ }
970
+ if (dom == NULL) {
971
+ /* an empty stream (or NULL doc): valid, empty report */
972
+ yeptris_validate_report* r = yep_validate_report_new();
973
+ if (r == NULL) {
974
+ return YEPTRIS_ERROR_MEMORY;
975
+ }
976
+ *out = r;
977
+ return YEPTRIS_OK;
978
+ }
979
+ return validate_dom(dom, fam, json_family, validate_registry(opts), out);
980
+ }
981
+
982
+ YEPTRIS_API YeptrisStatus yeptris_validate_document(YeptrisDocument doc, YeptrisSchema schema,
983
+ yeptris_validate_report** out) {
984
+ return yeptris_validate_document_ex(doc, schema, NULL, out);
985
+ }
986
+
987
+ YEPTRIS_API YeptrisStatus yeptris_validate_ex(const char* buf, size_t len, YeptrisSchema schema,
988
+ const yeptris_validate_options* opts,
989
+ yeptris_validate_report** out) {
990
+ if (out == NULL || (buf == NULL && len != 0)) {
991
+ return YEPTRIS_ERROR_ARG;
992
+ }
993
+ *out = NULL;
994
+ const yep_resolver* fam;
995
+ int json_family;
996
+ if (!yep_validate_schema_family(schema, &fam, &json_family)) {
997
+ yep_error_set(yep_error_tls(), YEP_ERR_UNEXPECTED, 0, 0, 0,
998
+ "schema %d is not a validation family", (int)schema);
999
+ return YEPTRIS_ERROR_ARG;
1000
+ }
1001
+ YeptrisParseOptions popts;
1002
+ memset(&popts, 0, sizeof(popts));
1003
+ popts.schema = schema; /* the parse resolves implicit types per family */
1004
+ YeptrisStatus st = YEPTRIS_OK;
1005
+ YeptrisDocument doc = yeptris_parse_eager_ex(buf, len, &popts, &st);
1006
+ if (doc == NULL) {
1007
+ if (st == YEPTRIS_OK) {
1008
+ /* an empty stream: valid, empty report */
1009
+ return yeptris_validate_document_ex(NULL, schema, opts, out);
1010
+ }
1011
+ return st; /* the parse's own error */
1012
+ }
1013
+ st = yeptris_validate_document_ex(doc, schema, opts, out);
1014
+ yeptris_document_free(doc);
1015
+ return st;
1016
+ }
1017
+
1018
+ YEPTRIS_API YeptrisStatus yeptris_validate(const char* buf, size_t len, YeptrisSchema schema,
1019
+ yeptris_validate_report** out) {
1020
+ return yeptris_validate_ex(buf, len, schema, NULL, out);
1021
+ }
1022
+
1023
+ YEPTRIS_API YeptrisStatus yeptris_json_schema_validate(const char* schema, size_t schema_len,
1024
+ const char* buf, size_t len,
1025
+ yeptris_validate_report** out) {
1026
+ if (out == NULL || (buf == NULL && len != 0) || (schema == NULL && schema_len != 0)) {
1027
+ return YEPTRIS_ERROR_ARG;
1028
+ }
1029
+ *out = NULL;
1030
+ /* the compiled-schema economy, one call deep (#885 item 3): the
1031
+ * schema parses ONCE here exactly as it always did, rides the SAME
1032
+ * walk and report paths as the retained handle, and dies at return
1033
+ * — the observable statuses and byte-for-byte verdicts are the
1034
+ * suite port's 1298 cases, unchanged */
1035
+ YeptrisStatus st = YEPTRIS_OK;
1036
+ yeptris_schema* s = yep_schema_compile_st(schema, schema_len, &st);
1037
+ if (s == NULL) {
1038
+ return st; /* the schema's parse/arg failure, verbatim */
1039
+ }
1040
+ st = yeptris_schema_validate(s, buf, len, out);
1041
+ yeptris_schema_free(s);
1042
+ return st;
1043
+ }