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,601 @@
1
+ /* resources.c — the schema reference machinery's compile-time half
2
+ * (the 2020-12 sota sweep).
3
+ *
4
+ * The draft's reference model is a URI space over the schema's own
5
+ * identifiers: "$id" establishes a base URI and names a RESOURCE (the
6
+ * schema object carrying it is that resource's root), "$anchor" and
7
+ * "$dynamicAnchor" attach plain-name fragments to the containing
8
+ * resource, and "$ref"/"$dynamicRef" sites resolve against the nearest
9
+ * enclosing base. All of that is STATIC — a property of the schema
10
+ * text — so the whole map is built once, here, at compile time, by
11
+ * one bounded DFS over the schema DOM ("the identifier crawl"). The
12
+ * compiled economy keeps the schema DOM alive, so the map rides the
13
+ * same handle and dies with it; validation is a pure reader.
14
+ *
15
+ * What the crawl records:
16
+ * - resources: canonical URI (fragment-free) -> the resource root
17
+ * node (the "$id" node; the document root registers the empty
18
+ * base "" when the schema has no "$id");
19
+ * - anchors: canonical URI + '#' + plain name -> the anchor node,
20
+ * flagged when created by "$dynamicAnchor" (the dynamic scope's
21
+ * extension points) vs "$anchor";
22
+ * - ref sites: every "$ref"/"$dynamicRef" object -> the canonical
23
+ * base URI and base resource root it resolves against;
24
+ * - rindex: node id -> owning resource index, for the runtime
25
+ * dynamic-scope stack ($dynamicRef, jsonschema.c).
26
+ *
27
+ * Bounded tables: an identifier past a cap is simply not registered —
28
+ * the reference then fails to resolve at validation (DIAG_REF), the
29
+ * same verdict as any other unresolvable target. Nothing guesses.
30
+ *
31
+ * URI handling is RFC 3986 section 5.2 to the level the conformance
32
+ * suite exercises: schemes, authorities, path merge with dot-segment
33
+ * removal, query carry, fragment split. The JSON Pointer percent
34
+ * layer is decoded by the pointer walk (jsonschema.c). No network: a
35
+ * target outside the schema's own identifier space is unresolvable,
36
+ * never fetched.
37
+ */
38
+
39
+ #include <string.h>
40
+
41
+ #include <yeptris/dom.h>
42
+
43
+ #include "../memory/allocator.h"
44
+ #include "internal.h"
45
+
46
+ /* ---- the tables ----------------------------------------------------------- */
47
+
48
+ /* 1 found (*idx_out), 0 absent. */
49
+ int js_res_find(const js_resources* r, const char* uri, uint32_t* idx_out) {
50
+ for (uint32_t i = 0; i < r->res_n; i++) {
51
+ if (strcmp(r->res[i].uri, uri) == 0) {
52
+ *idx_out = i;
53
+ return 1;
54
+ }
55
+ }
56
+ return 0;
57
+ }
58
+
59
+ /* 1 found (node + dynamic flag), 0 absent. */
60
+ int js_anchor_find(const js_resources* r, const char* key, size_t key_len, uint32_t* node_out,
61
+ uint8_t* dyn_out) {
62
+ for (uint32_t i = 0; i < r->anc_n; i++) {
63
+ if (strlen(r->anc[i].key) == key_len && memcmp(r->anc[i].key, key, key_len) == 0) {
64
+ *node_out = r->anc[i].node;
65
+ *dyn_out = r->anc[i].dynamic;
66
+ return 1;
67
+ }
68
+ }
69
+ return 0;
70
+ }
71
+
72
+ /* The first DYNAMIC anchor with this plain name owned by resource
73
+ * `res_idx` (the dynamic-scope search's per-resource probe). */
74
+ int js_anchor_dynamic_in_res(const js_resources* r, uint32_t res_idx, const char* name,
75
+ size_t name_len, uint32_t* node_out) {
76
+ for (uint32_t i = 0; i < r->anc_n; i++) {
77
+ if (r->anc[i].dynamic != 1 || r->anc[i].res != res_idx) {
78
+ continue;
79
+ }
80
+ const char* hash = strchr(r->anc[i].key, '#');
81
+ if (hash == NULL) {
82
+ continue;
83
+ }
84
+ size_t nl = strlen(hash + 1);
85
+ if (nl == name_len && (name_len == 0 || memcmp(hash + 1, name, name_len) == 0)) {
86
+ *node_out = r->anc[i].node;
87
+ return 1;
88
+ }
89
+ }
90
+ return 0;
91
+ }
92
+
93
+ /* 1 found (base URI and that base's resource root), 0 absent (the
94
+ * caller falls back to the document root). */
95
+ int js_refsite_find(const js_resources* r, uint32_t site_node, const char** base,
96
+ uint32_t* base_root) {
97
+ for (uint32_t i = 0; i < r->site_n; i++) {
98
+ if (r->site[i].site == site_node) {
99
+ *base = r->site[i].base;
100
+ *base_root = r->site[i].base_root;
101
+ return 1;
102
+ }
103
+ }
104
+ return 0;
105
+ }
106
+
107
+ void yep_js_resources_free(js_resources* r) {
108
+ if (r == NULL) {
109
+ return;
110
+ }
111
+ const yep_allocator* sys = yep_system_allocator();
112
+ for (uint32_t i = 0; i < r->res_n; i++) {
113
+ yep_free(sys, r->res[i].uri);
114
+ }
115
+ for (uint32_t i = 0; i < r->anc_n; i++) {
116
+ yep_free(sys, r->anc[i].key);
117
+ }
118
+ for (uint32_t i = 0; i < r->site_n; i++) {
119
+ yep_free(sys, r->site[i].base);
120
+ }
121
+ yep_free(sys, r->res);
122
+ yep_free(sys, r->anc);
123
+ yep_free(sys, r->site);
124
+ yep_free(sys, r->rindex);
125
+ yep_free(sys, r);
126
+ }
127
+
128
+ /* ---- URI machinery (RFC 3986 section 5.2, the exercised subset) ---------- */
129
+
130
+ typedef struct {
131
+ size_t scheme; /* offset, URI_NOSCHEME when absent; length below */
132
+ size_t scheme_len;
133
+ size_t auth; /* offset of the authority (after "//"); length below */
134
+ size_t auth_len;
135
+ size_t path;
136
+ size_t path_len;
137
+ size_t query; /* offset (after '?'); length below; 0 when absent */
138
+ size_t query_len;
139
+ } uri_parts;
140
+
141
+ #define URI_NOSCHEME ((size_t) - 1)
142
+
143
+ /* Splits s[0..n). Every part carries an offset and a length; absent
144
+ * parts are 0. The fragment (after '#') is ignored — canonical forms
145
+ * are fragment-free. */
146
+ static void uri_split(const char* s, size_t n, uri_parts* p) {
147
+ memset(p, 0, sizeof(*p));
148
+ p->scheme = URI_NOSCHEME;
149
+ size_t i = 0;
150
+ /* scheme: ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) ":" */
151
+ if (n > 0 && ((s[0] >= 'a' && s[0] <= 'z') || (s[0] >= 'A' && s[0] <= 'Z'))) {
152
+ size_t j = 1;
153
+ while (j < n &&
154
+ (s[j] == '+' || s[j] == '-' || s[j] == '.' || (s[j] >= 'a' && s[j] <= 'z') ||
155
+ (s[j] >= 'A' && s[j] <= 'Z') || (s[j] >= '0' && s[j] <= '9'))) {
156
+ j++;
157
+ }
158
+ if (j < n && s[j] == ':') {
159
+ p->scheme = 0;
160
+ p->scheme_len = j;
161
+ i = j + 1;
162
+ }
163
+ }
164
+ if (i + 1 < n && s[i] == '/' && s[i + 1] == '/') {
165
+ i += 2;
166
+ p->auth = i;
167
+ while (i < n && s[i] != '/' && s[i] != '?' && s[i] != '#') {
168
+ i++;
169
+ }
170
+ p->auth_len = i - p->auth;
171
+ }
172
+ p->path = i;
173
+ while (i < n && s[i] != '?' && s[i] != '#') {
174
+ i++;
175
+ }
176
+ p->path_len = i - p->path;
177
+ if (i < n && s[i] == '?') {
178
+ p->query = i + 1;
179
+ size_t q = p->query;
180
+ while (q < n && s[q] != '#') {
181
+ q++;
182
+ }
183
+ p->query_len = q - p->query;
184
+ }
185
+ }
186
+
187
+ /* Appends [s, s+n) to out (NUL kept terminated); -1 overlong. */
188
+ static int uri_put(char* out, size_t cap, size_t* n, const char* s, size_t len) {
189
+ if (*n + len + 1 > cap) {
190
+ return -1;
191
+ }
192
+ memcpy(out + *n, s, len);
193
+ *n += len;
194
+ out[*n] = '\0';
195
+ return 0;
196
+ }
197
+
198
+ /* 1 when the path segment is "." or ".." (*dotdot set for the latter). */
199
+ static int uri_dot_seg(const char* p, size_t len, int* dotdot) {
200
+ *dotdot = 0;
201
+ if (len == 1 && p[0] == '.') {
202
+ return 1;
203
+ }
204
+ if (len == 2 && p[0] == '.' && p[1] == '.') {
205
+ *dotdot = 1;
206
+ return 1;
207
+ }
208
+ return 0;
209
+ }
210
+
211
+ /* remove_dot_segments (RFC 3986 5.2.4): emits the path's segments
212
+ * slash-prefixed, dropping "." and popping on ".."; a trailing slash
213
+ * survives as an empty last segment. */
214
+ static int uri_normalize_path(const char* p, size_t n, char* out, size_t cap, size_t* on) {
215
+ typedef struct {
216
+ size_t off;
217
+ size_t len;
218
+ } seg;
219
+ seg segs[64];
220
+ int seg_n = 0;
221
+ int trailing_slash = n > 0 && p[n - 1] == '/';
222
+ size_t i = 0;
223
+ while (i < n) {
224
+ while (i < n && p[i] == '/') {
225
+ i++;
226
+ }
227
+ size_t s = i;
228
+ while (i < n && p[i] != '/') {
229
+ i++;
230
+ }
231
+ if (i == s) {
232
+ break;
233
+ }
234
+ int dotdot = 0;
235
+ if (uri_dot_seg(p + s, i - s, &dotdot)) {
236
+ if (dotdot && seg_n > 0) {
237
+ seg_n--;
238
+ }
239
+ continue;
240
+ }
241
+ if (seg_n >= (int)(sizeof(segs) / sizeof(segs[0]))) {
242
+ return -1;
243
+ }
244
+ segs[seg_n].off = s;
245
+ segs[seg_n].len = i - s;
246
+ seg_n++;
247
+ }
248
+ for (int k = 0; k < seg_n; k++) {
249
+ if (uri_put(out, cap, on, "/", 1) != 0 ||
250
+ uri_put(out, cap, on, p + segs[k].off, segs[k].len) != 0) {
251
+ return -1;
252
+ }
253
+ }
254
+ if (trailing_slash && uri_put(out, cap, on, "/", 1) != 0) {
255
+ return -1;
256
+ }
257
+ return 0;
258
+ }
259
+
260
+ /* Resolves the URI-reference `ref` against the base URI `base`,
261
+ * producing the canonical form WITHOUT its fragment (the caller owns
262
+ * fragment handling — plain-name fragments never reach this). 0 ok,
263
+ * -1 overlong (out holds "" on failure). */
264
+ int js_uri_resolve(const char* base, const char* ref, size_t ref_len, char* out, size_t cap) {
265
+ out[0] = '\0';
266
+ size_t bn = strlen(base);
267
+ if (bn >= YEP_JS_URI_MAX) {
268
+ return -1;
269
+ }
270
+
271
+ /* the reference minus its fragment */
272
+ char r[YEP_JS_URI_MAX];
273
+ size_t rn = 0;
274
+ {
275
+ const char* hash = memchr(ref, '#', ref_len);
276
+ size_t rl = hash != NULL ? (size_t)(hash - ref) : ref_len;
277
+ if (uri_put(r, sizeof(r), &rn, ref, rl) != 0) {
278
+ return -1;
279
+ }
280
+ }
281
+ uri_parts rp;
282
+ uri_split(r, rn, &rp);
283
+
284
+ /* an empty reference is the base itself */
285
+ if (rn == 0) {
286
+ size_t n = 0;
287
+ return uri_put(out, cap, &n, base, bn);
288
+ }
289
+ if (rp.scheme != URI_NOSCHEME) { /* absolute (URN included): verbatim */
290
+ size_t n = 0;
291
+ return uri_put(out, cap, &n, r, rn);
292
+ }
293
+
294
+ /* every remaining form prefixes the base's scheme[:authority] */
295
+ uri_parts bp;
296
+ uri_split(base, bn, &bp);
297
+ size_t on = 0;
298
+ if (bp.scheme != URI_NOSCHEME && uri_put(out, cap, &on, base, bp.scheme_len + 1) != 0) {
299
+ return -1;
300
+ }
301
+ if (rn > 1 && r[0] == '/' && r[1] == '/') { /* network-path: keep the ref's authority */
302
+ return uri_put(out, cap, &on, r, rn) == 0 ? 0 : -1;
303
+ }
304
+ if (bp.auth_len > 0 && (uri_put(out, cap, &on, "//", 2) != 0 ||
305
+ uri_put(out, cap, &on, base + bp.auth, bp.auth_len) != 0)) {
306
+ return -1;
307
+ }
308
+ if (rn > 0 && r[0] == '/') { /* path-absolute: replaces the base path */
309
+ if (uri_normalize_path(r + rp.path, rp.path_len, out, cap, &on) != 0) {
310
+ return -1;
311
+ }
312
+ } else { /* relative path: merge against the base path's directory */
313
+ char merged[YEP_JS_URI_MAX];
314
+ size_t mn = 0;
315
+ size_t dir = bp.path_len;
316
+ while (dir > 0 && base[bp.path + dir - 1] != '/') {
317
+ dir--;
318
+ }
319
+ if (dir > 0 && uri_put(merged, sizeof(merged), &mn, base + bp.path, dir) != 0) {
320
+ return -1;
321
+ }
322
+ if (uri_put(merged, sizeof(merged), &mn, r + rp.path, rp.path_len) != 0) {
323
+ return -1;
324
+ }
325
+ if (uri_normalize_path(merged, mn, out, cap, &on) != 0) {
326
+ return -1;
327
+ }
328
+ }
329
+ /* the reference's query replaces the base's */
330
+ if (rp.query_len > 0 && (uri_put(out, cap, &on, "?", 1) != 0 ||
331
+ uri_put(out, cap, &on, r + rp.query, rp.query_len) != 0)) {
332
+ return -1;
333
+ }
334
+ return 0;
335
+ }
336
+
337
+ /* ---- the identifier crawl ------------------------------------------------- */
338
+
339
+ typedef struct {
340
+ const yep_dom* d;
341
+ js_resources* r;
342
+ } crawl;
343
+
344
+ /* Schema-side string keyword read (the walk's exact rules: a scalar
345
+ * key by bytes, a string-scalar value). 1 + *val_out, 0 absent. */
346
+ static int crawl_kw_str(const crawl* c, uint32_t map_id, const char* name, const char** p,
347
+ size_t* len) {
348
+ const yep_dnode* m = yep_dom_node(c->d, map_id);
349
+ if (m == NULL || m->kind != YEP_DOM_MAPPING) {
350
+ return 0;
351
+ }
352
+ size_t nl = strlen(name);
353
+ uint32_t k = m->first_child;
354
+ while (k != UINT32_MAX) {
355
+ uint32_t v = yep_dom_node(c->d, k) ? yep_dom_node(c->d, k)->next_sibling : UINT32_MAX;
356
+ const yep_dnode* kn = yep_dom_node(c->d, k);
357
+ if (kn != NULL && kn->kind == YEP_DOM_SCALAR) {
358
+ yep_view kv = yep_dom_view(c->d, kn->value);
359
+ if (kv.len == nl && (nl == 0 || memcmp(kv.p, name, nl) == 0)) {
360
+ const yep_dnode* vn = v != UINT32_MAX ? yep_dom_node(c->d, v) : NULL;
361
+ if (vn != NULL && vn->kind == YEP_DOM_SCALAR &&
362
+ yep_validate_scalar_tag(vn) == YEPTRIS_TAG_STR) {
363
+ yep_view vv = yep_dom_view(c->d, vn->value);
364
+ if (vv.p != NULL) {
365
+ *p = vv.p;
366
+ *len = vv.len;
367
+ return 1;
368
+ }
369
+ }
370
+ return 0; /* the keyword is present but unreadable */
371
+ }
372
+ }
373
+ k = v != UINT32_MAX && yep_dom_node(c->d, v) ? yep_dom_node(c->d, v)->next_sibling
374
+ : UINT32_MAX;
375
+ }
376
+ return 0;
377
+ }
378
+
379
+ /* Registers a resource (first registration wins; the same canonical
380
+ * URI names exactly one resource). UINT32_MAX: over cap / OOM — the
381
+ * resource stays unregistered and its references unresolvable. */
382
+ static uint32_t crawl_resource(crawl* c, const char* uri, uint32_t root) {
383
+ uint32_t idx = 0;
384
+ if (js_res_find(c->r, uri, &idx)) {
385
+ return idx;
386
+ }
387
+ if (c->r->res_n >= YEP_JS_MAX_RESOURCES) {
388
+ return UINT32_MAX;
389
+ }
390
+ const yep_allocator* sys = yep_system_allocator();
391
+ size_t n = strlen(uri);
392
+ char* copy = yep_alloc(sys, n + 1);
393
+ js_resource* nr = yep_alloc(sys, (size_t)(c->r->res_n + 1) * sizeof(*nr));
394
+ if (copy == NULL || nr == NULL) {
395
+ yep_free(sys, copy);
396
+ yep_free(sys, nr);
397
+ return UINT32_MAX;
398
+ }
399
+ memcpy(copy, uri, n + 1);
400
+ if (c->r->res_n > 0) {
401
+ memcpy(nr, c->r->res, (size_t)c->r->res_n * sizeof(*nr));
402
+ }
403
+ yep_free(sys, c->r->res);
404
+ c->r->res = nr;
405
+ c->r->res[c->r->res_n].uri = copy;
406
+ c->r->res[c->r->res_n].root = root;
407
+ c->r->res_n++;
408
+ return c->r->res_n - 1;
409
+ }
410
+
411
+ /* Registers an anchor: key = base + '#' + name. Same-key duplicates
412
+ * keep the first (the draft leaves repeated fragments undefined). */
413
+ static void crawl_anchor(crawl* c, const char* base, uint32_t res_idx, const char* name,
414
+ size_t name_len, uint32_t node, uint8_t dynamic) {
415
+ if (c->r->anc_n >= YEP_JS_MAX_ANCHORS) {
416
+ return;
417
+ }
418
+ char key[YEP_JS_URI_MAX];
419
+ size_t bn = strlen(base);
420
+ if (bn + 1 + name_len + 1 > sizeof(key)) {
421
+ return;
422
+ }
423
+ size_t kl = 0;
424
+ if (uri_put(key, sizeof(key), &kl, base, bn) != 0 ||
425
+ uri_put(key, sizeof(key), &kl, "#", 1) != 0 ||
426
+ uri_put(key, sizeof(key), &kl, name, name_len) != 0) {
427
+ return;
428
+ }
429
+ uint32_t dup_node = 0;
430
+ uint8_t dup_dyn = 0;
431
+ if (js_anchor_find(c->r, key, kl, &dup_node, &dup_dyn)) {
432
+ return;
433
+ }
434
+ const yep_allocator* sys = yep_system_allocator();
435
+ char* copy = yep_alloc(sys, kl + 1);
436
+ js_anchor* na = yep_alloc(sys, (size_t)(c->r->anc_n + 1) * sizeof(*na));
437
+ if (copy == NULL || na == NULL) {
438
+ yep_free(sys, copy);
439
+ yep_free(sys, na);
440
+ return;
441
+ }
442
+ memcpy(copy, key, kl + 1);
443
+ if (c->r->anc_n > 0) {
444
+ memcpy(na, c->r->anc, (size_t)c->r->anc_n * sizeof(*na));
445
+ }
446
+ yep_free(sys, c->r->anc);
447
+ c->r->anc = na;
448
+ c->r->anc[c->r->anc_n].key = copy;
449
+ c->r->anc[c->r->anc_n].node = node;
450
+ c->r->anc[c->r->anc_n].res = res_idx;
451
+ c->r->anc[c->r->anc_n].dynamic = dynamic;
452
+ c->r->anc_n++;
453
+ }
454
+
455
+ /* Registers a ref site (the object's base and that base's resource
456
+ * root — where its fragment-only references start walking). */
457
+ static void crawl_refsite(crawl* c, uint32_t site, const char* base, uint32_t base_root) {
458
+ for (uint32_t i = 0; i < c->r->site_n; i++) {
459
+ if (c->r->site[i].site == site) {
460
+ return;
461
+ }
462
+ }
463
+ if (c->r->site_n >= YEP_JS_MAX_REFSITES) {
464
+ return;
465
+ }
466
+ const yep_allocator* sys = yep_system_allocator();
467
+ size_t bn = strlen(base);
468
+ char* copy = yep_alloc(sys, bn + 1);
469
+ js_refsite* ns = yep_alloc(sys, (size_t)(c->r->site_n + 1) * sizeof(*ns));
470
+ if (copy == NULL || ns == NULL) {
471
+ yep_free(sys, copy);
472
+ yep_free(sys, ns);
473
+ return;
474
+ }
475
+ memcpy(copy, base, bn + 1);
476
+ if (c->r->site_n > 0) {
477
+ memcpy(ns, c->r->site, (size_t)c->r->site_n * sizeof(*ns));
478
+ }
479
+ yep_free(sys, c->r->site);
480
+ c->r->site = ns;
481
+ c->r->site[c->r->site_n].site = site;
482
+ c->r->site[c->r->site_n].base = copy;
483
+ c->r->site[c->r->site_n].base_root = base_root;
484
+ c->r->site_n++;
485
+ }
486
+
487
+ static int crawl_node(crawl* c, uint32_t id, const char* base, uint32_t res_idx, int depth);
488
+
489
+ /* One mapping (a schema object): the identifier keywords first (the
490
+ * draft's "$id must be evaluated before $ref" order), then the
491
+ * values. 0 ok, -1 OOM. */
492
+ static int crawl_map(crawl* c, uint32_t map_id, const char* base, uint32_t res_idx, int depth) {
493
+ const char* my_base = base;
494
+ uint32_t my_res = res_idx;
495
+ const char* idv = NULL;
496
+ size_t idl = 0;
497
+ char canon[YEP_JS_URI_MAX];
498
+ if (crawl_kw_str(c, map_id, "$id", &idv, &idl) &&
499
+ js_uri_resolve(base, idv, idl, canon, sizeof(canon)) == 0) {
500
+ uint32_t ri = crawl_resource(c, canon, map_id);
501
+ if (ri != UINT32_MAX) {
502
+ my_base = c->r->res[ri].uri;
503
+ my_res = ri;
504
+ }
505
+ }
506
+ if (map_id < c->r->rindex_n) {
507
+ c->r->rindex[map_id] = my_res;
508
+ }
509
+ const char* av = NULL;
510
+ size_t al = 0;
511
+ if (crawl_kw_str(c, map_id, "$anchor", &av, &al)) {
512
+ crawl_anchor(c, my_base, my_res, av, al, map_id, 0);
513
+ }
514
+ if (crawl_kw_str(c, map_id, "$dynamicAnchor", &av, &al)) {
515
+ crawl_anchor(c, my_base, my_res, av, al, map_id, 1);
516
+ }
517
+ if (crawl_kw_str(c, map_id, "$ref", &av, &al) ||
518
+ crawl_kw_str(c, map_id, "$dynamicRef", &av, &al)) {
519
+ crawl_refsite(c, map_id, my_base, c->r->res[my_res].root);
520
+ }
521
+ /* the values ride the subtree */
522
+ const yep_dnode* m = yep_dom_node(c->d, map_id);
523
+ uint32_t k = m->first_child;
524
+ while (k != UINT32_MAX) {
525
+ uint32_t v = yep_dom_node(c->d, k) ? yep_dom_node(c->d, k)->next_sibling : UINT32_MAX;
526
+ if (v != UINT32_MAX && crawl_node(c, v, my_base, my_res, depth + 1) != 0) {
527
+ return -1;
528
+ }
529
+ k = v != UINT32_MAX && yep_dom_node(c->d, v) ? yep_dom_node(c->d, v)->next_sibling
530
+ : UINT32_MAX;
531
+ }
532
+ return 0;
533
+ }
534
+
535
+ static int crawl_node(crawl* c, uint32_t id, const char* base, uint32_t res_idx, int depth) {
536
+ if (depth > YEP_DOM_MAX_DEPTH) {
537
+ return 0; /* the walk's own guard: deeper nodes are unreachable to it too */
538
+ }
539
+ const yep_dnode* n = yep_dom_node(c->d, id);
540
+ if (n == NULL) {
541
+ return 0;
542
+ }
543
+ if (id < c->r->rindex_n && c->r->rindex[id] == UINT32_MAX) {
544
+ c->r->rindex[id] = res_idx;
545
+ }
546
+ if (n->kind == YEP_DOM_MAPPING) {
547
+ return crawl_map(c, id, base, res_idx, depth);
548
+ }
549
+ if (n->kind == YEP_DOM_SEQUENCE) {
550
+ uint32_t ch = n->first_child;
551
+ while (ch != UINT32_MAX) {
552
+ uint32_t next =
553
+ yep_dom_node(c->d, ch) ? yep_dom_node(c->d, ch)->next_sibling : UINT32_MAX;
554
+ if (crawl_node(c, ch, base, res_idx, depth + 1) != 0) {
555
+ return -1;
556
+ }
557
+ ch = next;
558
+ }
559
+ }
560
+ return 0;
561
+ }
562
+
563
+ /* The constructor: one bounded DFS over the schema DOM. NULL on OOM
564
+ * (the caller reports YEPTRIS_ERROR_MEMORY). */
565
+ js_resources* yep_js_resources_crawl(const yep_dom* d, uint32_t root) {
566
+ const yep_allocator* sys = yep_system_allocator();
567
+ js_resources* r = yep_alloc(sys, sizeof(*r));
568
+ if (r == NULL) {
569
+ return NULL;
570
+ }
571
+ memset(r, 0, sizeof(*r));
572
+ r->rindex_n = d->ncount;
573
+ r->rindex = yep_alloc(sys, (size_t)d->ncount * sizeof(uint32_t));
574
+ if (r->rindex == NULL) {
575
+ yep_free(sys, r);
576
+ return NULL;
577
+ }
578
+ memset(r->rindex, 0xFF, (size_t)d->ncount * sizeof(uint32_t)); /* UINT32_MAX */
579
+
580
+ crawl c = {d, r};
581
+ /* resource 0: the document itself (the empty base, or the root's
582
+ * own "$id") */
583
+ const char* idv = NULL;
584
+ size_t idl = 0;
585
+ char canon[YEP_JS_URI_MAX];
586
+ const char* root_base = "";
587
+ if (crawl_kw_str(&c, root, "$id", &idv, &idl) &&
588
+ js_uri_resolve("", idv, idl, canon, sizeof(canon)) == 0) {
589
+ root_base = canon;
590
+ }
591
+ uint32_t ri = crawl_resource(&c, root_base, root);
592
+ if (ri == UINT32_MAX) {
593
+ yep_js_resources_free(r);
594
+ return NULL;
595
+ }
596
+ if (crawl_node(&c, root, r->res[ri].uri, ri, 0) != 0) {
597
+ yep_js_resources_free(r);
598
+ return NULL;
599
+ }
600
+ return r;
601
+ }