yeptris 0.6.59.1-arm-linux-eabihf → 0.6.60.1-arm-linux-eabihf
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/yeptris_native/version.h +2 -2
- data/lib/yeptris/3.4/native.so +0 -0
- data/lib/yeptris/ffi.rb +19 -0
- data/lib/yeptris/yaml.rb +61 -0
- data/lib/yeptris.rb +1 -1
- data/libyeptris.so +0 -0
- data/vendor/libyeptris/CMakeLists.txt +1 -1
- data/vendor/libyeptris/src/CMakeLists.txt +7 -0
- data/vendor/libyeptris/src/include/yeptris/emit.h +30 -2
- data/vendor/libyeptris/src/include/yeptris/parse.h +52 -0
- data/vendor/libyeptris/src/include/yeptris/resolve.h +8 -0
- data/vendor/libyeptris/src/include/yeptris/validate.h +222 -21
- data/vendor/libyeptris/src/include/yeptris/values.h +8 -2
- data/vendor/libyeptris/src/yeptris/cbor/cbor.h +10 -0
- data/vendor/libyeptris/src/yeptris/cbor/decode.c +5 -7
- data/vendor/libyeptris/src/yeptris/cbor/encode.c +9 -9
- data/vendor/libyeptris/src/yeptris/cbor/from_marshal.c +1 -1
- data/vendor/libyeptris/src/yeptris/common/error.h +2 -0
- data/vendor/libyeptris/src/yeptris/doc.h +20 -0
- data/vendor/libyeptris/src/yeptris/emit/emit.c +85 -13
- data/vendor/libyeptris/src/yeptris/emit/writer.c +158 -642
- data/vendor/libyeptris/src/yeptris/emit/writer.h +15 -1
- data/vendor/libyeptris/src/yeptris/events/push.c +28 -3
- data/vendor/libyeptris/src/yeptris/events/values.c +24 -0
- data/vendor/libyeptris/src/yeptris/marshal/to_json.c +650 -0
- data/vendor/libyeptris/src/yeptris/parse/engine.c +121 -40
- data/vendor/libyeptris/src/yeptris/parse/stream_iter.c +326 -0
- data/vendor/libyeptris/src/yeptris/parse.c +173 -43
- data/vendor/libyeptris/src/yeptris/resolve/compat11.c +1 -1
- data/vendor/libyeptris/src/yeptris/resolve/core12.c +11 -3
- data/vendor/libyeptris/src/yeptris/resolve/full12.c +201 -0
- data/vendor/libyeptris/src/yeptris/resolve/json12.c +12 -6
- data/vendor/libyeptris/src/yeptris/resolve/resolver.h +20 -0
- data/vendor/libyeptris/src/yeptris/validate/compile.c +39 -3
- data/vendor/libyeptris/src/yeptris/validate/internal.h +223 -7
- data/vendor/libyeptris/src/yeptris/validate/jsonschema.c +645 -115
- data/vendor/libyeptris/src/yeptris/validate/output.c +246 -0
- data/vendor/libyeptris/src/yeptris/validate/regexp.c +623 -157
- data/vendor/libyeptris/src/yeptris/validate/registry.c +118 -0
- data/vendor/libyeptris/src/yeptris/validate/resources.c +601 -0
- data/vendor/libyeptris/src/yeptris/validate/stream.c +1155 -0
- data/vendor/libyeptris/src/yeptris/validate/validate.c +288 -145
- metadata +8 -1
|
@@ -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
|
+
}
|