yeptris 0.6.15.2 → 0.6.16.2
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/cbor_ruby.c +22 -2
- data/ext/yeptris_native/yeptris_native.c +43 -0
- data/lib/yeptris/document.rb +17 -0
- data/lib/yeptris/json.rb +8 -4
- data/lib/yeptris/psych/class_loader.rb +24 -0
- data/lib/yeptris/psych/scalar_scanner.rb +149 -0
- data/lib/yeptris/psych.rb +39 -7
- data/lib/yeptris/schema.rb +86 -0
- data/lib/yeptris.rb +1 -1
- data/vendor/libyeptris/CMakeLists.txt +1 -1
- data/vendor/libyeptris/src/include/yeptris/schema.h +4 -2
- data/vendor/libyeptris/src/yeptris/cbor/decode.c +310 -177
- data/vendor/libyeptris/src/yeptris/cbor/sink.h +58 -0
- data/vendor/libyeptris/src/yeptris/emit/writer.c +16 -2
- metadata +5 -2
|
@@ -27,6 +27,8 @@
|
|
|
27
27
|
#include <math.h>
|
|
28
28
|
#include <string.h>
|
|
29
29
|
|
|
30
|
+
#include <stdlib.h>
|
|
31
|
+
|
|
30
32
|
#include <yeptris/cbor.h>
|
|
31
33
|
#include <yeptris/resolve.h>
|
|
32
34
|
|
|
@@ -37,29 +39,41 @@
|
|
|
37
39
|
#include "../memory/allocator.h"
|
|
38
40
|
#include "../parse/events.h"
|
|
39
41
|
#include "doc.h"
|
|
42
|
+
#include "sink.h"
|
|
40
43
|
|
|
41
44
|
#define YEP_CBOR_INDEF UINT64_MAX
|
|
42
45
|
#define YEP_CBOR_MAX_ARG_BYTES 8
|
|
43
46
|
|
|
44
47
|
typedef struct {
|
|
45
|
-
uint64_t rem;
|
|
48
|
+
uint64_t rem; /* items left; maps count 2*pairs; INDEF = until break */
|
|
49
|
+
uint64_t consumed; /* items landed (the key/value parity) */
|
|
46
50
|
uint8_t is_map;
|
|
47
51
|
} yep_cframe;
|
|
48
52
|
|
|
53
|
+
#define YEP_CBOR_TAGBUF \
|
|
54
|
+
512 /* a semantic-tag chain over ~23 links is \
|
|
55
|
+
* pathological; the arena form had no cap, \
|
|
56
|
+
* the stack form does (documented) */
|
|
57
|
+
|
|
49
58
|
typedef struct {
|
|
50
|
-
yep_dom* d;
|
|
59
|
+
yep_dom* d; /* the DOM sink's ctx (NULL for pure sinks) */
|
|
51
60
|
const unsigned char* p;
|
|
52
61
|
size_t len;
|
|
53
62
|
size_t i;
|
|
54
63
|
int strict;
|
|
55
64
|
YeptrisStatus status;
|
|
56
|
-
/* pending tag chain
|
|
57
|
-
*
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
65
|
+
/* the pending semantic-tag chain, text form ("N N N", outermost
|
|
66
|
+
* first — the next item's writer consumes it) */
|
|
67
|
+
char tagbuf[YEP_CBOR_TAGBUF];
|
|
68
|
+
uint32_t taglen;
|
|
69
|
+
/* indefinite-string accumulation (chunk framing is the grammar's;
|
|
70
|
+
* the finished span goes to one str/bytes callback) */
|
|
71
|
+
unsigned char* ibuf;
|
|
72
|
+
size_t icap;
|
|
73
|
+
uint32_t ilen;
|
|
74
|
+
const yep_cbor_sink* s;
|
|
61
75
|
yep_cframe frame[YEP_DOM_MAX_DEPTH];
|
|
62
|
-
int depth;
|
|
76
|
+
int depth;
|
|
63
77
|
} yep_cdec;
|
|
64
78
|
|
|
65
79
|
static void cbor_fail(yep_cdec* c, yep_err_code code, const char* msg) {
|
|
@@ -146,116 +160,74 @@ static double cbor_half(uint16_t h) {
|
|
|
146
160
|
return (h & 0x8000u) != 0 ? -val : val;
|
|
147
161
|
}
|
|
148
162
|
|
|
149
|
-
/*
|
|
150
|
-
static
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
/* the pending chain lives in the DOM arena (offsets, not
|
|
155
|
-
* pointers — the arena reallocs as it grows) */
|
|
156
|
-
tag_v.p = c->d->str + (c->pending_tag.off & YEP_SV_OFF);
|
|
157
|
-
tag_v.len = c->pending_tag.len;
|
|
158
|
-
tag = &tag_v;
|
|
159
|
-
c->have_pending = 0;
|
|
160
|
-
}
|
|
161
|
-
uint32_t id = dom_open_node(c->d, YEP_DOM_SCALAR, tag, NULL, 0, YEP_STYLE_PLAIN, 1, 0);
|
|
162
|
-
if (id == UINT32_MAX) {
|
|
163
|
-
c->status = YEPTRIS_ERROR_MEMORY;
|
|
164
|
-
return UINT32_MAX;
|
|
165
|
-
}
|
|
166
|
-
yep_dnode* node = &c->d->nodes[id];
|
|
167
|
-
char* dst = yep_dom_str_tail(c->d, n);
|
|
168
|
-
if (dst == NULL) {
|
|
169
|
-
c->status = YEPTRIS_ERROR_MEMORY;
|
|
170
|
-
return UINT32_MAX;
|
|
171
|
-
}
|
|
172
|
-
memcpy(dst, text, n);
|
|
173
|
-
node->value = yep_dom_str_commit(c->d, n);
|
|
174
|
-
node->tag_id = tag_id;
|
|
175
|
-
if (dom_place(c->d, id) != 0) {
|
|
176
|
-
c->status = YEPTRIS_ERROR_MEMORY;
|
|
177
|
-
return UINT32_MAX;
|
|
163
|
+
/* The pending semantic-tag chain, consumed by the next writer. */
|
|
164
|
+
static const char* cbor_take_tag(yep_cdec* c, uint32_t* len) {
|
|
165
|
+
if (c->taglen == 0) {
|
|
166
|
+
*len = 0;
|
|
167
|
+
return NULL;
|
|
178
168
|
}
|
|
179
|
-
|
|
169
|
+
*len = c->taglen;
|
|
170
|
+
c->taglen = 0;
|
|
171
|
+
return c->tagbuf;
|
|
180
172
|
}
|
|
181
173
|
|
|
182
|
-
/*
|
|
183
|
-
static uint32_t
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
tag_v.len = c->pending_tag.len;
|
|
189
|
-
tag = &tag_v;
|
|
190
|
-
c->have_pending = 0;
|
|
191
|
-
}
|
|
192
|
-
uint32_t id = dom_open_node(c->d, YEP_DOM_SCALAR, tag, NULL, 0, YEP_STYLE_DOUBLE_QUOTED, 0, 0);
|
|
193
|
-
if (id == UINT32_MAX) {
|
|
174
|
+
/* Rendered text (numbers-as-text, literals, diagnostics). */
|
|
175
|
+
static uint32_t cbor_text_node(yep_cdec* c, const char* text, uint32_t n, uint8_t tag_id,
|
|
176
|
+
int is_key) {
|
|
177
|
+
uint32_t tag_len;
|
|
178
|
+
const char* tag = cbor_take_tag(c, &tag_len);
|
|
179
|
+
if (!c->s->text(c->s->ctx, text, n, tag_id, is_key, tag, tag_len)) {
|
|
194
180
|
c->status = YEPTRIS_ERROR_MEMORY;
|
|
195
181
|
return UINT32_MAX;
|
|
196
182
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
183
|
+
return 0; /* item identity is the sink's business */
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/* Definite string (mt2 -> bytes_val, mt3 -> str_val), borrowing the input. */
|
|
187
|
+
static uint32_t cbor_span_node(yep_cdec* c, const unsigned char* p, uint32_t n, uint8_t mt,
|
|
188
|
+
int is_key) {
|
|
189
|
+
uint32_t tag_len;
|
|
190
|
+
const char* tag = cbor_take_tag(c, &tag_len);
|
|
191
|
+
int ok = (mt == 2) ? c->s->bytes_val(c->s->ctx, p, n, 1, is_key, tag, tag_len)
|
|
192
|
+
: c->s->str_val(c->s->ctx, p, n, 1, is_key, tag, tag_len);
|
|
193
|
+
if (!ok) {
|
|
201
194
|
c->status = YEPTRIS_ERROR_MEMORY;
|
|
202
195
|
return UINT32_MAX;
|
|
203
196
|
}
|
|
204
|
-
return
|
|
197
|
+
return 0;
|
|
205
198
|
}
|
|
206
199
|
|
|
207
|
-
static int cbor_open(yep_cdec* c, int is_map) {
|
|
200
|
+
static int cbor_open(yep_cdec* c, int is_map, uint64_t cap) {
|
|
208
201
|
if (c->depth >= YEP_DOM_MAX_DEPTH) {
|
|
209
202
|
yep_error_set(yep_error_tls(), YEP_ERR_UNEXPECTED, 0, 0, c->i,
|
|
210
203
|
"CBOR nesting exceeds the depth guard at byte %zu", c->i);
|
|
211
204
|
c->status = YEPTRIS_ERROR_DEPTH;
|
|
212
205
|
return 0;
|
|
213
206
|
}
|
|
214
|
-
|
|
215
|
-
const
|
|
216
|
-
if (c->
|
|
217
|
-
tag_v.p = c->d->str + (c->pending_tag.off & YEP_SV_OFF);
|
|
218
|
-
tag_v.len = c->pending_tag.len;
|
|
219
|
-
tag = &tag_v;
|
|
220
|
-
c->have_pending = 0;
|
|
221
|
-
}
|
|
222
|
-
uint32_t id =
|
|
223
|
-
dom_open_node(c->d, is_map ? YEP_DOM_MAPPING : YEP_DOM_SEQUENCE, tag, NULL, 0, 0, 0, 1);
|
|
224
|
-
if (id == UINT32_MAX) {
|
|
207
|
+
uint32_t tag_len;
|
|
208
|
+
const char* tag = cbor_take_tag(c, &tag_len);
|
|
209
|
+
if (!c->s->open(c->s->ctx, is_map, cap, tag, tag_len)) {
|
|
225
210
|
c->status = YEPTRIS_ERROR_MEMORY;
|
|
226
211
|
return 0;
|
|
227
212
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
}
|
|
232
|
-
c->d->map_pending_key[c->d->depth] = 0;
|
|
233
|
-
c->d->stack[c->d->depth++] = id;
|
|
234
|
-
c->depth++; /* the frame mirrors the DOM's stack one-to-one */
|
|
213
|
+
/* a sibling at this depth may have left state behind */
|
|
214
|
+
c->frame[c->depth].consumed = 0;
|
|
215
|
+
c->depth++;
|
|
235
216
|
return 1;
|
|
236
217
|
}
|
|
237
218
|
|
|
238
219
|
static int cbor_note_tag(yep_cdec* c, uint64_t tag) {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
n += cbor_u64dec(tag, buf + n);
|
|
245
|
-
char* dst = yep_dom_str_tail(c->d, n);
|
|
246
|
-
if (dst == NULL) {
|
|
247
|
-
c->status = YEPTRIS_ERROR_MEMORY;
|
|
220
|
+
if (c->taglen + 1 + 20 > sizeof c->tagbuf) {
|
|
221
|
+
yep_error_set(yep_error_tls(), YEP_ERR_UNEXPECTED, 0, 0, c->i,
|
|
222
|
+
"semantic-tag chain exceeds %u bytes at byte %zu", (unsigned)sizeof c->tagbuf,
|
|
223
|
+
c->i);
|
|
224
|
+
c->status = YEPTRIS_ERROR_PARSE;
|
|
248
225
|
return 0;
|
|
249
226
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
c->pending_tag = piece;
|
|
253
|
-
c->pending_tag.len = 0;
|
|
254
|
-
c->have_pending = 1;
|
|
227
|
+
if (c->taglen > 0) {
|
|
228
|
+
c->tagbuf[c->taglen++] = ' '; /* separator between chain links */
|
|
255
229
|
}
|
|
256
|
-
|
|
257
|
-
/* piece-by-piece commit keeps the chain contiguous in the arena */
|
|
258
|
-
c->pending_tag.len += n;
|
|
230
|
+
c->taglen += cbor_u64dec(tag, c->tagbuf + c->taglen);
|
|
259
231
|
return 1;
|
|
260
232
|
}
|
|
261
233
|
|
|
@@ -263,7 +235,6 @@ static int cbor_note_tag(yep_cdec* c, uint64_t tag) {
|
|
|
263
235
|
* arena; text chunks validate per chunk — RFC 8949 s3.2.3's code-point
|
|
264
236
|
* boundary rule makes per-chunk validity imply whole-string validity). */
|
|
265
237
|
static int cbor_indef_string(yep_cdec* c, int is_text) {
|
|
266
|
-
uint32_t first = 0;
|
|
267
238
|
uint32_t total = 0;
|
|
268
239
|
for (;;) {
|
|
269
240
|
if (c->i >= c->len) {
|
|
@@ -291,75 +262,55 @@ static int cbor_indef_string(yep_cdec* c, int is_text) {
|
|
|
291
262
|
return 0;
|
|
292
263
|
}
|
|
293
264
|
if (n > 0) { /* zero-length chunks are legal, just empty */
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
265
|
+
if (total + n > c->icap) {
|
|
266
|
+
size_t ncap = c->icap ? c->icap * 2 : 256;
|
|
267
|
+
while (ncap < total + n) {
|
|
268
|
+
ncap *= 2;
|
|
269
|
+
}
|
|
270
|
+
unsigned char* nib = realloc(c->ibuf, ncap);
|
|
271
|
+
if (nib == NULL) {
|
|
272
|
+
c->status = YEPTRIS_ERROR_MEMORY;
|
|
273
|
+
return 0;
|
|
274
|
+
}
|
|
275
|
+
c->ibuf = nib;
|
|
276
|
+
c->icap = ncap;
|
|
303
277
|
}
|
|
278
|
+
memcpy(c->ibuf + total, c->p + c->i, (size_t)n);
|
|
304
279
|
total += (uint32_t)n;
|
|
305
280
|
}
|
|
306
281
|
c->i += (size_t)n;
|
|
307
282
|
}
|
|
308
|
-
/*
|
|
309
|
-
|
|
310
|
-
const
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
tag = &tag_v;
|
|
315
|
-
c->have_pending = 0;
|
|
316
|
-
}
|
|
317
|
-
uint32_t id = dom_open_node(c->d, YEP_DOM_SCALAR, tag, NULL, 0, YEP_STYLE_DOUBLE_QUOTED, 0, 0);
|
|
318
|
-
if (id == UINT32_MAX) {
|
|
319
|
-
c->status = YEPTRIS_ERROR_MEMORY;
|
|
320
|
-
return 0;
|
|
321
|
-
}
|
|
322
|
-
yep_dnode* node = &c->d->nodes[id];
|
|
323
|
-
node->value.off = first;
|
|
324
|
-
node->value.len = total;
|
|
325
|
-
if (dom_place(c->d, id) != 0) {
|
|
283
|
+
/* one value over the accumulated span */
|
|
284
|
+
uint32_t tag_len;
|
|
285
|
+
const char* tag = cbor_take_tag(c, &tag_len);
|
|
286
|
+
int ok =
|
|
287
|
+
(is_text ? c->s->str_val : c->s->bytes_val)(c->s->ctx, c->ibuf, total, 0, 0, tag, tag_len);
|
|
288
|
+
if (!ok) {
|
|
326
289
|
c->status = YEPTRIS_ERROR_MEMORY;
|
|
327
290
|
return 0;
|
|
328
291
|
}
|
|
329
292
|
return 1;
|
|
330
293
|
}
|
|
331
294
|
|
|
332
|
-
/*
|
|
333
|
-
static int cbor_int_node(yep_cdec* c, int negative, uint64_t mag) {
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
295
|
+
/* Known integer: value = negative ? -mag : mag, mag <= 2^63. */
|
|
296
|
+
static int cbor_int_node(yep_cdec* c, int negative, uint64_t mag, int is_key) {
|
|
297
|
+
uint32_t tag_len;
|
|
298
|
+
const char* tag = cbor_take_tag(c, &tag_len);
|
|
299
|
+
if (!c->s->int_val(c->s->ctx, negative, mag, is_key, tag, tag_len)) {
|
|
300
|
+
c->status = YEPTRIS_ERROR_MEMORY;
|
|
301
|
+
return 0;
|
|
339
302
|
}
|
|
340
|
-
|
|
341
|
-
tag_id = YEPTRIS_TAG_INT;
|
|
342
|
-
return cbor_text_node(c, buf, n, tag_id) != UINT32_MAX;
|
|
303
|
+
return 1;
|
|
343
304
|
}
|
|
344
305
|
|
|
345
|
-
static int cbor_float_node(yep_cdec* c, double dv) {
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
n = 3;
|
|
352
|
-
} else if (dv > 1.7976931348623157e308) {
|
|
353
|
-
memcpy(buf, "Infinity", 8);
|
|
354
|
-
n = 8;
|
|
355
|
-
} else if (dv < -1.7976931348623157e308) {
|
|
356
|
-
memcpy(buf, "-Infinity", 9);
|
|
357
|
-
n = 9;
|
|
358
|
-
} else {
|
|
359
|
-
n = (uint32_t)yep_d2s_shortest(dv, buf);
|
|
306
|
+
static int cbor_float_node(yep_cdec* c, double dv, int is_key) {
|
|
307
|
+
uint32_t tag_len;
|
|
308
|
+
const char* tag = cbor_take_tag(c, &tag_len);
|
|
309
|
+
if (!c->s->float_val(c->s->ctx, dv, is_key, tag, tag_len)) {
|
|
310
|
+
c->status = YEPTRIS_ERROR_MEMORY;
|
|
311
|
+
return 0;
|
|
360
312
|
}
|
|
361
|
-
|
|
362
|
-
return cbor_text_node(c, buf, n, YEPTRIS_TAG_FLOAT) != UINT32_MAX;
|
|
313
|
+
return 1;
|
|
363
314
|
}
|
|
364
315
|
|
|
365
316
|
/* The diagnostic renderer for lenient-mode non-string SCALAR map keys
|
|
@@ -408,9 +359,9 @@ static int cbor_key_diag(yep_cdec* c, uint8_t mt, uint64_t val, double dv) {
|
|
|
408
359
|
}
|
|
409
360
|
break;
|
|
410
361
|
default:
|
|
411
|
-
return cbor_float_node(c, dv); /* floats: the shortest text */
|
|
362
|
+
return cbor_float_node(c, dv, 1); /* floats: the shortest text */
|
|
412
363
|
}
|
|
413
|
-
return cbor_text_node(c, buf, n, 0) != UINT32_MAX;
|
|
364
|
+
return cbor_text_node(c, buf, n, 0, 1) != UINT32_MAX;
|
|
414
365
|
}
|
|
415
366
|
|
|
416
367
|
/* Decodes ONE data item (scalar: creates+places the node; container:
|
|
@@ -425,9 +376,10 @@ static int cbor_item(yep_cdec* c) {
|
|
|
425
376
|
uint8_t ai = ib & 0x1F;
|
|
426
377
|
|
|
427
378
|
/* the map-key position decides representation before the item is
|
|
428
|
-
* built
|
|
379
|
+
* built: even items consumed = key, odd = value (the frame's own
|
|
380
|
+
* parity — the DOM's placement machine tracks its own copy) */
|
|
429
381
|
int key_slot = 0;
|
|
430
|
-
if (c->depth > 0 && c->frame[c->depth - 1].is_map &&
|
|
382
|
+
if (c->depth > 0 && c->frame[c->depth - 1].is_map && c->frame[c->depth - 1].consumed % 2 == 0) {
|
|
431
383
|
key_slot = 1;
|
|
432
384
|
}
|
|
433
385
|
|
|
@@ -444,7 +396,7 @@ static int cbor_item(yep_cdec* c) {
|
|
|
444
396
|
if (key_slot) {
|
|
445
397
|
CBOR_REJECT("structure as map key");
|
|
446
398
|
}
|
|
447
|
-
if (!cbor_open(c, 0)) {
|
|
399
|
+
if (!cbor_open(c, 0, YEP_CBOR_INDEF)) {
|
|
448
400
|
return 0;
|
|
449
401
|
}
|
|
450
402
|
c->frame[c->depth - 1].rem = YEP_CBOR_INDEF;
|
|
@@ -454,7 +406,7 @@ static int cbor_item(yep_cdec* c) {
|
|
|
454
406
|
if (key_slot) {
|
|
455
407
|
CBOR_REJECT("structure as map key");
|
|
456
408
|
}
|
|
457
|
-
if (!cbor_open(c, 1)) {
|
|
409
|
+
if (!cbor_open(c, 1, YEP_CBOR_INDEF)) {
|
|
458
410
|
return 0;
|
|
459
411
|
}
|
|
460
412
|
c->frame[c->depth - 1].rem = YEP_CBOR_INDEF;
|
|
@@ -482,9 +434,9 @@ static int cbor_item(yep_cdec* c) {
|
|
|
482
434
|
return cbor_key_diag(c, 0, arg, 0) ? 1 : 0;
|
|
483
435
|
}
|
|
484
436
|
if (arg <= (uint64_t)INT64_MAX) {
|
|
485
|
-
return cbor_int_node(c, 0, arg) ? 1 : 0;
|
|
437
|
+
return cbor_int_node(c, 0, arg, key_slot) ? 1 : 0;
|
|
486
438
|
}
|
|
487
|
-
return cbor_float_node(c, (double)arg) ? 1 : 0;
|
|
439
|
+
return cbor_float_node(c, (double)arg, key_slot) ? 1 : 0;
|
|
488
440
|
}
|
|
489
441
|
case 1: {
|
|
490
442
|
if (key_slot && c->strict) {
|
|
@@ -495,9 +447,9 @@ static int cbor_item(yep_cdec* c) {
|
|
|
495
447
|
}
|
|
496
448
|
if (arg <= (uint64_t)INT64_MAX + 1) { /* -1-2^63 == INT64_MIN */
|
|
497
449
|
uint64_t mag = arg + 1;
|
|
498
|
-
return cbor_int_node(c, 1, mag) ? 1 : 0;
|
|
450
|
+
return cbor_int_node(c, 1, mag, key_slot) ? 1 : 0;
|
|
499
451
|
}
|
|
500
|
-
return cbor_float_node(c, -((double)arg + 1.0)) ? 1 : 0;
|
|
452
|
+
return cbor_float_node(c, -((double)arg + 1.0), key_slot) ? 1 : 0;
|
|
501
453
|
}
|
|
502
454
|
case 2:
|
|
503
455
|
case 3: {
|
|
@@ -518,7 +470,7 @@ static int cbor_item(yep_cdec* c) {
|
|
|
518
470
|
c->i += (size_t)arg;
|
|
519
471
|
return ok ? 1 : 0;
|
|
520
472
|
}
|
|
521
|
-
uint32_t id = cbor_span_node(c, c->p + c->i, (uint32_t)arg);
|
|
473
|
+
uint32_t id = cbor_span_node(c, c->p + c->i, (uint32_t)arg, (uint8_t)mt, key_slot);
|
|
522
474
|
c->i += (size_t)arg;
|
|
523
475
|
return id != UINT32_MAX ? 1 : 0;
|
|
524
476
|
}
|
|
@@ -529,7 +481,7 @@ static int cbor_item(yep_cdec* c) {
|
|
|
529
481
|
if (arg > c->len - c->i) { /* every item consumes >= 1 byte */
|
|
530
482
|
CBOR_REJECT("array overruns the input");
|
|
531
483
|
}
|
|
532
|
-
if (!cbor_open(c, 0)) {
|
|
484
|
+
if (!cbor_open(c, 0, arg)) {
|
|
533
485
|
return 0;
|
|
534
486
|
}
|
|
535
487
|
c->frame[c->depth - 1].rem = arg;
|
|
@@ -543,7 +495,7 @@ static int cbor_item(yep_cdec* c) {
|
|
|
543
495
|
if (arg > (c->len - c->i) / 2) { /* pairs need 2 items each */
|
|
544
496
|
CBOR_REJECT("map overruns the input");
|
|
545
497
|
}
|
|
546
|
-
if (!cbor_open(c, 1)) {
|
|
498
|
+
if (!cbor_open(c, 1, arg * 2)) {
|
|
547
499
|
return 0;
|
|
548
500
|
}
|
|
549
501
|
c->frame[c->depth - 1].rem = arg * 2;
|
|
@@ -566,22 +518,22 @@ static int cbor_item(yep_cdec* c) {
|
|
|
566
518
|
if (key_slot) {
|
|
567
519
|
return cbor_key_diag(c, 7, 20, 0) ? 1 : 0;
|
|
568
520
|
}
|
|
569
|
-
return cbor_text_node(c, "false", 5, YEPTRIS_TAG_BOOL) != UINT32_MAX ? 1 : 0;
|
|
521
|
+
return cbor_text_node(c, "false", 5, YEPTRIS_TAG_BOOL, key_slot) != UINT32_MAX ? 1 : 0;
|
|
570
522
|
case 21:
|
|
571
523
|
if (key_slot) {
|
|
572
524
|
return cbor_key_diag(c, 7, 21, 0) ? 1 : 0;
|
|
573
525
|
}
|
|
574
|
-
return cbor_text_node(c, "true", 4, YEPTRIS_TAG_BOOL) != UINT32_MAX ? 1 : 0;
|
|
526
|
+
return cbor_text_node(c, "true", 4, YEPTRIS_TAG_BOOL, key_slot) != UINT32_MAX ? 1 : 0;
|
|
575
527
|
case 22:
|
|
576
528
|
if (key_slot) {
|
|
577
529
|
return cbor_key_diag(c, 7, 22, 0) ? 1 : 0;
|
|
578
530
|
}
|
|
579
|
-
return cbor_text_node(c, "null", 4, YEPTRIS_TAG_NULL) != UINT32_MAX ? 1 : 0;
|
|
531
|
+
return cbor_text_node(c, "null", 4, YEPTRIS_TAG_NULL, key_slot) != UINT32_MAX ? 1 : 0;
|
|
580
532
|
case 23:
|
|
581
533
|
if (key_slot) {
|
|
582
534
|
return cbor_key_diag(c, 7, 23, 0) ? 1 : 0;
|
|
583
535
|
}
|
|
584
|
-
return cbor_text_node(c, "undefined", 9, 0) != UINT32_MAX ? 1 : 0;
|
|
536
|
+
return cbor_text_node(c, "undefined", 9, 0, key_slot) != UINT32_MAX ? 1 : 0;
|
|
585
537
|
case 24: {
|
|
586
538
|
if (c->len - c->i < 1) {
|
|
587
539
|
CBOR_REJECT("truncated simple value");
|
|
@@ -597,7 +549,7 @@ static int cbor_item(yep_cdec* c) {
|
|
|
597
549
|
memcpy(buf, "simple(", 7);
|
|
598
550
|
uint32_t n = 7 + cbor_u64dec(v, buf + 7);
|
|
599
551
|
buf[n++] = ')';
|
|
600
|
-
return cbor_text_node(c, buf, n, 0) != UINT32_MAX ? 1 : 0;
|
|
552
|
+
return cbor_text_node(c, buf, n, 0, key_slot) != UINT32_MAX ? 1 : 0;
|
|
601
553
|
}
|
|
602
554
|
case 25: {
|
|
603
555
|
if (c->len - c->i < 2) {
|
|
@@ -641,7 +593,7 @@ static int cbor_item(yep_cdec* c) {
|
|
|
641
593
|
memcpy(buf, "simple(", 7);
|
|
642
594
|
uint32_t n = 7 + cbor_u64dec(ai, buf + 7);
|
|
643
595
|
buf[n++] = ')';
|
|
644
|
-
return cbor_text_node(c, buf, n, 0) != UINT32_MAX ? 1 : 0;
|
|
596
|
+
return cbor_text_node(c, buf, n, 0, key_slot) != UINT32_MAX ? 1 : 0;
|
|
645
597
|
}
|
|
646
598
|
CBOR_REJECT("reserved additional information");
|
|
647
599
|
}
|
|
@@ -652,34 +604,191 @@ static int cbor_item(yep_cdec* c) {
|
|
|
652
604
|
if (key_slot) {
|
|
653
605
|
return cbor_key_diag(c, 8, 0, dv) ? 1 : 0;
|
|
654
606
|
}
|
|
655
|
-
return cbor_float_node(c, dv) ? 1 : 0;
|
|
607
|
+
return cbor_float_node(c, dv, key_slot) ? 1 : 0;
|
|
656
608
|
}
|
|
657
609
|
|
|
658
|
-
|
|
659
|
-
|
|
610
|
+
/* The DOM sink: the yeptris_cbor_decode public path. Each impl is the
|
|
611
|
+
* pre-sink writer, byte for byte (the differential suite pins it). */
|
|
612
|
+
|
|
613
|
+
static int dom_sink_text(void* dp, const char* s, uint32_t n, uint8_t tag_id, int is_key,
|
|
614
|
+
const char* tag, uint32_t tag_len) {
|
|
615
|
+
yep_dom* d = dp;
|
|
616
|
+
(void)is_key;
|
|
617
|
+
yep_view tag_v;
|
|
618
|
+
const yep_view* tagp = NULL;
|
|
619
|
+
if (tag != NULL) {
|
|
620
|
+
char* dst = yep_dom_str_tail(d, tag_len);
|
|
621
|
+
if (dst == NULL) {
|
|
622
|
+
return 0;
|
|
623
|
+
}
|
|
624
|
+
memcpy(dst, tag, tag_len);
|
|
625
|
+
yep_sview piece = yep_dom_str_commit(d, tag_len);
|
|
626
|
+
tag_v.p = d->str + (piece.off & YEP_SV_OFF);
|
|
627
|
+
tag_v.len = tag_len;
|
|
628
|
+
tagp = &tag_v;
|
|
629
|
+
}
|
|
630
|
+
uint32_t id = dom_open_node(d, YEP_DOM_SCALAR, tagp, NULL, 0, YEP_STYLE_PLAIN, 1, 0);
|
|
631
|
+
if (id == UINT32_MAX) {
|
|
632
|
+
return 0;
|
|
633
|
+
}
|
|
634
|
+
yep_dnode* node = &d->nodes[id];
|
|
635
|
+
char* dst = yep_dom_str_tail(d, n);
|
|
636
|
+
if (dst == NULL) {
|
|
637
|
+
return 0;
|
|
638
|
+
}
|
|
639
|
+
memcpy(dst, s, n);
|
|
640
|
+
node->value = yep_dom_str_commit(d, n);
|
|
641
|
+
node->tag_id = tag_id;
|
|
642
|
+
return dom_place(d, id) == 0;
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
static int dom_sink_span(void* dp, const unsigned char* p, uint32_t n, int borrowed, int is_key,
|
|
646
|
+
const char* tag, uint32_t tag_len) {
|
|
647
|
+
yep_dom* d = dp;
|
|
648
|
+
(void)is_key;
|
|
649
|
+
(void)borrowed; /* 0 rides the scratch buffer: still copied to the arena */
|
|
650
|
+
yep_view tag_v;
|
|
651
|
+
const yep_view* tagp = NULL;
|
|
652
|
+
if (tag != NULL) {
|
|
653
|
+
char* dst = yep_dom_str_tail(d, tag_len);
|
|
654
|
+
if (dst == NULL) {
|
|
655
|
+
return 0;
|
|
656
|
+
}
|
|
657
|
+
memcpy(dst, tag, tag_len);
|
|
658
|
+
yep_sview piece = yep_dom_str_commit(d, tag_len);
|
|
659
|
+
tag_v.p = d->str + (piece.off & YEP_SV_OFF);
|
|
660
|
+
tag_v.len = tag_len;
|
|
661
|
+
tagp = &tag_v;
|
|
662
|
+
}
|
|
663
|
+
uint32_t id = dom_open_node(d, YEP_DOM_SCALAR, tagp, NULL, 0, YEP_STYLE_DOUBLE_QUOTED, 0, 0);
|
|
664
|
+
if (id == UINT32_MAX) {
|
|
665
|
+
return 0;
|
|
666
|
+
}
|
|
667
|
+
yep_dnode* node = &d->nodes[id];
|
|
668
|
+
if (borrowed) {
|
|
669
|
+
node->value.off = (uint32_t)((const char*)p - d->input_base);
|
|
670
|
+
node->value.len = n;
|
|
671
|
+
} else if (n > 0) { /* the empty indefinite string needs no arena */
|
|
672
|
+
char* dst = yep_dom_str_tail(d, n);
|
|
673
|
+
if (dst == NULL) {
|
|
674
|
+
return 0;
|
|
675
|
+
}
|
|
676
|
+
memcpy(dst, p, n);
|
|
677
|
+
node->value = yep_dom_str_commit(d, n);
|
|
678
|
+
}
|
|
679
|
+
return dom_place(d, id) == 0;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
static int dom_sink_str(void* dp, const unsigned char* p, uint32_t n, int borrowed, int is_key,
|
|
683
|
+
const char* tag, uint32_t tag_len) {
|
|
684
|
+
return dom_sink_span(dp, p, n, borrowed, is_key, tag, tag_len);
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
static int dom_sink_bytes(void* dp, const unsigned char* p, uint32_t n, int borrowed, int is_key,
|
|
688
|
+
const char* tag, uint32_t tag_len) {
|
|
689
|
+
return dom_sink_span(dp, p, n, borrowed, is_key, tag, tag_len);
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
static int dom_sink_open(void* dp, int is_map, uint64_t cap, const char* tag, uint32_t tag_len) {
|
|
693
|
+
yep_dom* d = dp;
|
|
694
|
+
(void)cap;
|
|
695
|
+
yep_view tag_v;
|
|
696
|
+
const yep_view* tagp = NULL;
|
|
697
|
+
if (tag != NULL) {
|
|
698
|
+
char* dst = yep_dom_str_tail(d, tag_len);
|
|
699
|
+
if (dst == NULL) {
|
|
700
|
+
return 0;
|
|
701
|
+
}
|
|
702
|
+
memcpy(dst, tag, tag_len);
|
|
703
|
+
yep_sview piece = yep_dom_str_commit(d, tag_len);
|
|
704
|
+
tag_v.p = d->str + (piece.off & YEP_SV_OFF);
|
|
705
|
+
tag_v.len = tag_len;
|
|
706
|
+
tagp = &tag_v;
|
|
707
|
+
}
|
|
708
|
+
uint32_t id =
|
|
709
|
+
dom_open_node(d, is_map ? YEP_DOM_MAPPING : YEP_DOM_SEQUENCE, tagp, NULL, 0, 0, 0, 1);
|
|
710
|
+
if (id == UINT32_MAX) {
|
|
711
|
+
return 0;
|
|
712
|
+
}
|
|
713
|
+
if (dom_place(d, id) != 0) {
|
|
714
|
+
return 0;
|
|
715
|
+
}
|
|
716
|
+
d->map_pending_key[d->depth] = 0;
|
|
717
|
+
d->stack[d->depth++] = id;
|
|
718
|
+
return 1;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
static int dom_sink_close(void* dp, int is_map) {
|
|
722
|
+
yep_dom* d = dp;
|
|
723
|
+
(void)is_map;
|
|
724
|
+
d->depth--;
|
|
725
|
+
return 1;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
static int dom_sink_int(void* dp, int negative, uint64_t mag, int is_key, const char* tag,
|
|
729
|
+
uint32_t tag_len) {
|
|
730
|
+
char buf[24];
|
|
731
|
+
uint32_t n = 0;
|
|
732
|
+
if (negative) {
|
|
733
|
+
buf[n++] = '-';
|
|
734
|
+
}
|
|
735
|
+
n += cbor_u64dec(mag, buf + n);
|
|
736
|
+
return dom_sink_text(dp, buf, n, YEPTRIS_TAG_INT, is_key, tag, tag_len);
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
static int dom_sink_float(void* dp, double dv, int is_key, const char* tag, uint32_t tag_len) {
|
|
740
|
+
char buf[48];
|
|
741
|
+
(void)is_key;
|
|
742
|
+
uint32_t n;
|
|
743
|
+
if (dv != dv) {
|
|
744
|
+
memcpy(buf, "NaN", 3);
|
|
745
|
+
n = 3;
|
|
746
|
+
} else if (dv > 1.7976931348623157e308) {
|
|
747
|
+
memcpy(buf, "Infinity", 8);
|
|
748
|
+
n = 8;
|
|
749
|
+
} else if (dv < -1.7976931348623157e308) {
|
|
750
|
+
memcpy(buf, "-Infinity", 9);
|
|
751
|
+
n = 9;
|
|
752
|
+
} else {
|
|
753
|
+
n = (uint32_t)yep_d2s_shortest(dv, buf);
|
|
754
|
+
}
|
|
755
|
+
return dom_sink_text(dp, buf, n, YEPTRIS_TAG_FLOAT, is_key, tag, tag_len);
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
static const yep_cbor_sink yep_cbor_dom_sink_impl = {
|
|
759
|
+
NULL, dom_sink_text, dom_sink_int, dom_sink_float,
|
|
760
|
+
dom_sink_str, dom_sink_bytes, dom_sink_open, dom_sink_close,
|
|
761
|
+
};
|
|
762
|
+
|
|
763
|
+
int yep_cbor_decode_gen(const unsigned char* p, size_t len, int strict, size_t* consumed,
|
|
764
|
+
const yep_cbor_sink* sink) {
|
|
660
765
|
yep_cdec c;
|
|
661
766
|
memset(&c, 0, sizeof(c));
|
|
662
|
-
c.
|
|
767
|
+
c.s = sink;
|
|
663
768
|
c.p = p;
|
|
664
769
|
c.len = len;
|
|
665
770
|
c.strict = strict;
|
|
666
771
|
c.status = YEPTRIS_OK;
|
|
667
772
|
int done_top = 0; /* the ONE top-level item is done (tags never count) */
|
|
773
|
+
int rc;
|
|
668
774
|
for (;;) {
|
|
669
775
|
if (c.depth == 0) {
|
|
670
776
|
if (done_top) { /* the root closed; one data item per call */
|
|
671
777
|
if (c.i != len && consumed == NULL) {
|
|
672
778
|
cbor_fail(&c, YEP_ERR_UNEXPECTED, "trailing bytes after the data item");
|
|
673
|
-
|
|
779
|
+
rc = YEPTRIS_ERROR_PARSE;
|
|
780
|
+
goto done;
|
|
674
781
|
}
|
|
675
782
|
if (consumed != NULL) {
|
|
676
783
|
*consumed = c.i;
|
|
677
784
|
}
|
|
678
|
-
|
|
785
|
+
rc = YEPTRIS_OK;
|
|
786
|
+
goto done;
|
|
679
787
|
}
|
|
680
788
|
int st = cbor_item(&c);
|
|
681
789
|
if (st == 0) {
|
|
682
|
-
|
|
790
|
+
rc = (int)c.status;
|
|
791
|
+
goto done;
|
|
683
792
|
}
|
|
684
793
|
if (st == 2) {
|
|
685
794
|
continue; /* tag chain: the content item follows */
|
|
@@ -689,19 +798,26 @@ int yep_cbor_decode_dom(yep_dom* d, const unsigned char* p, size_t len, int stri
|
|
|
689
798
|
}
|
|
690
799
|
yep_cframe* fr = &c.frame[c.depth - 1];
|
|
691
800
|
if (fr->rem == 0) { /* definite container complete */
|
|
692
|
-
c.
|
|
801
|
+
if (!c.s->close(c.s->ctx, fr->is_map)) {
|
|
802
|
+
rc = YEPTRIS_ERROR_MEMORY;
|
|
803
|
+
goto done;
|
|
804
|
+
}
|
|
693
805
|
c.depth--;
|
|
694
806
|
continue;
|
|
695
807
|
}
|
|
696
808
|
if (fr->rem == YEP_CBOR_INDEF) {
|
|
697
809
|
if (c.i < c.len && c.p[c.i] == 0xFF) {
|
|
698
|
-
if (fr->is_map &&
|
|
810
|
+
if (fr->is_map && fr->consumed % 2 == 1) {
|
|
699
811
|
cbor_fail(&c, YEP_ERR_UNEXPECTED,
|
|
700
812
|
"break in a map value position (odd item count)");
|
|
701
|
-
|
|
813
|
+
rc = YEPTRIS_ERROR_PARSE;
|
|
814
|
+
goto done;
|
|
702
815
|
}
|
|
703
816
|
c.i++;
|
|
704
|
-
c.
|
|
817
|
+
if (!c.s->close(c.s->ctx, fr->is_map)) {
|
|
818
|
+
rc = YEPTRIS_ERROR_MEMORY;
|
|
819
|
+
goto done;
|
|
820
|
+
}
|
|
705
821
|
c.depth--;
|
|
706
822
|
continue;
|
|
707
823
|
}
|
|
@@ -709,16 +825,33 @@ int yep_cbor_decode_dom(yep_dom* d, const unsigned char* p, size_t len, int stri
|
|
|
709
825
|
size_t parent = (size_t)(c.depth - 1);
|
|
710
826
|
int st = cbor_item(&c);
|
|
711
827
|
if (st == 0) {
|
|
712
|
-
|
|
828
|
+
rc = (int)c.status;
|
|
829
|
+
goto done;
|
|
713
830
|
}
|
|
714
831
|
if (st == 2) {
|
|
715
832
|
continue; /* tags never consume a container slot */
|
|
716
833
|
}
|
|
834
|
+
c.frame[parent].consumed++;
|
|
717
835
|
if (c.frame[parent].rem != YEP_CBOR_INDEF) {
|
|
718
836
|
c.frame[parent].rem--;
|
|
719
837
|
/* one item (scalar, or a container now open) consumed a parent slot */
|
|
720
838
|
}
|
|
721
839
|
}
|
|
840
|
+
done:
|
|
841
|
+
free(c.ibuf);
|
|
842
|
+
return rc;
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
int yep_cbor_decode_dom_sink(void* dp, const unsigned char* p, size_t len, int strict,
|
|
846
|
+
size_t* consumed) {
|
|
847
|
+
yep_cbor_sink s = yep_cbor_dom_sink_impl;
|
|
848
|
+
s.ctx = dp;
|
|
849
|
+
return yep_cbor_decode_gen(p, len, strict, consumed, &s);
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
int yep_cbor_decode_dom(yep_dom* d, const unsigned char* p, size_t len, int strict,
|
|
853
|
+
size_t* consumed) {
|
|
854
|
+
return yep_cbor_decode_dom_sink(d, p, len, strict, consumed);
|
|
722
855
|
}
|
|
723
856
|
|
|
724
857
|
static YeptrisDocument cbor_wrap(yep_dom* dom, const void* buf, const yep_allocator* sys) {
|