mini_racer-csim 0.21.1.0

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.
@@ -0,0 +1,782 @@
1
+ #include <err.h>
2
+ #include <math.h>
3
+ #include <stdbool.h>
4
+ #include <stdint.h>
5
+ #include <stdlib.h>
6
+ #include <stdio.h>
7
+ #include <string.h>
8
+
9
+ static void des_null(void *arg);
10
+ static void des_undefined(void *arg);
11
+ static void des_bool(void *arg, int v);
12
+ static void des_int(void *arg, int64_t v);
13
+ static void des_num(void *arg, double v);
14
+ static void des_date(void *arg, double v);
15
+ // des_bigint: |p| points to |n|/8 quadwords in little-endian order
16
+ // des_bigint: |p| is not quadword aligned
17
+ // des_bigint: |n| is in bytes, not quadwords
18
+ // des_bigint: |n| is zero when bigint is zero
19
+ // des_bigint: |sign| is 1 or -1
20
+ static void des_bigint(void *arg, const void *p, size_t n, int sign);
21
+ static void des_string(void *arg, const char *s, size_t n);
22
+ static void des_string8(void *arg, const uint8_t *s, size_t n);
23
+ // des_string16: |s| is not word aligned
24
+ // des_string16: |n| is in bytes, not code points
25
+ static void des_string16(void *arg, const void *s, size_t n);
26
+ static void des_arraybuffer(void *arg, const void *s, size_t n);
27
+ static void des_array_begin(void *arg);
28
+ static void des_array_end(void *arg);
29
+ // called if e.g. an array object has named properties
30
+ static void des_named_props_begin(void *arg);
31
+ static void des_named_props_end(void *arg);
32
+ static void des_object_begin(void *arg);
33
+ static void des_object_end(void *arg);
34
+ static void des_map_begin(void *arg);
35
+ static void des_map_end(void *arg);
36
+ static void des_object_ref(void *arg, uint32_t id);
37
+ // des_error_begin: followed by des_object_begin + des_object_end calls
38
+ static void des_error_begin(void *arg);
39
+ static void des_error_end(void *arg);
40
+
41
+ // dynamically sized buffer with inline storage so we don't
42
+ // have to worry about allocation failures for small payloads
43
+ typedef struct Buf {
44
+ uint8_t *buf;
45
+ uint32_t len, cap;
46
+ uint8_t buf_s[48];
47
+ } Buf;
48
+
49
+ typedef struct Ser {
50
+ Buf b;
51
+ char err[64];
52
+ } Ser;
53
+
54
+ static const uint8_t the_nan[8] = {0,0,0,0,0,0,0xF8,0x7F}; // canonical nan
55
+
56
+ // note: returns |v| if v in [0,1,2]
57
+ static inline uint32_t next_power_of_two(uint32_t v) {
58
+ v -= 1;
59
+ v |= v >> 1;
60
+ v |= v >> 2;
61
+ v |= v >> 4;
62
+ v |= v >> 8;
63
+ v |= v >> 16;
64
+ v += 1;
65
+ return v;
66
+ }
67
+
68
+ static inline void buf_init(Buf *b)
69
+ {
70
+ b->len = 0;
71
+ b->buf = b->buf_s;
72
+ b->cap = sizeof(b->buf_s);
73
+ }
74
+
75
+ static inline void buf_reset(Buf *b)
76
+ {
77
+ if (b->buf != b->buf_s)
78
+ free(b->buf);
79
+ buf_init(b);
80
+ }
81
+
82
+ static inline void buf_move(Buf *s, Buf *d)
83
+ {
84
+ if (s == d)
85
+ return;
86
+ *d = *s;
87
+ if (s->buf == s->buf_s)
88
+ d->buf = d->buf_s;
89
+ buf_init(s);
90
+ }
91
+
92
+ static inline int buf_grow(Buf *b, size_t n)
93
+ {
94
+ void *p;
95
+
96
+ if ((uint64_t)n + b->len > UINT32_MAX)
97
+ return -1;
98
+ n += b->len;
99
+ if (n < b->cap)
100
+ return 0;
101
+ n = next_power_of_two(n);
102
+ p = NULL;
103
+ if (b->buf != b->buf_s)
104
+ p = b->buf;
105
+ p = realloc(p, n);
106
+ if (!p)
107
+ return -1;
108
+ if (b->buf == b->buf_s)
109
+ memcpy(p, b->buf_s, b->len);
110
+ b->buf = p;
111
+ b->cap = n;
112
+ return 0;
113
+ }
114
+
115
+ static inline int buf_put(Buf *b, const void *p, size_t n)
116
+ {
117
+ if (n == 0)
118
+ return 0;
119
+ if (buf_grow(b, n))
120
+ return -1;
121
+ memcpy(&b->buf[b->len], p, n);
122
+ b->len += n;
123
+ return 0;
124
+ }
125
+
126
+ static inline int buf_putc(Buf *b, uint8_t c)
127
+ {
128
+ return buf_put(b, &c, 1);
129
+ }
130
+
131
+ static inline void w(Ser *s, const void *p, size_t n)
132
+ {
133
+ if (*s->err)
134
+ return;
135
+ if (buf_put(&s->b, p, n))
136
+ snprintf(s->err, sizeof(s->err), "out of memory");
137
+ }
138
+
139
+ static inline void w_byte(Ser *s, uint8_t c)
140
+ {
141
+ w(s, &c, 1);
142
+ }
143
+
144
+ static inline void w_varint(Ser *s, uint64_t v)
145
+ {
146
+ uint8_t b[10]; // 10 == 1 + 64/7
147
+ size_t n;
148
+
149
+ for (n = 0; v > 127; v >>= 7)
150
+ b[n++] = 128 | (v & 127);
151
+ b[n++] = v;
152
+ w(s, b, n);
153
+ }
154
+
155
+ static inline void w_zigzag(Ser *s, int64_t v)
156
+ {
157
+ uint64_t t;
158
+
159
+ if (v < 0) {
160
+ t = -v;
161
+ } else {
162
+ t = v;
163
+ }
164
+ t += t;
165
+ t -= (v < 0);
166
+ w_varint(s, t);
167
+ }
168
+
169
+ static inline int r_varint(const uint8_t **p, const uint8_t *pe, uint64_t *r)
170
+ {
171
+ int i, k;
172
+
173
+ for (i = 0; i < 5; i++) {
174
+ if (*p+i == pe)
175
+ return -1;
176
+ if ((*p)[i] < 128)
177
+ goto ok;
178
+ }
179
+ return -1;
180
+ ok:
181
+ *r = 0;
182
+ for (k = 0; k <= i; k++, (*p)++)
183
+ *r |= (uint64_t)(**p & 127) << 7*k;
184
+ return 0;
185
+ }
186
+
187
+ static inline int r_zigzag(const uint8_t **p, const uint8_t *pe, int64_t *r)
188
+ {
189
+ uint64_t v;
190
+
191
+ if (r_varint(p, pe, &v))
192
+ return -1;
193
+ *r = v&1 ? -(v/2)-1 : v/2;
194
+ return 0;
195
+ }
196
+
197
+ static void ser_init0(Ser *s)
198
+ {
199
+ memset(s, 0, sizeof(*s));
200
+ buf_init(&s->b);
201
+ }
202
+
203
+ static inline void ser_init(Ser *s)
204
+ {
205
+ ser_init0(s);
206
+ w(s, "\xFF\x0F", 2);
207
+ }
208
+
209
+ static void ser_init1(Ser *s, uint8_t c)
210
+ {
211
+ ser_init0(s);
212
+ w_byte(s, c);
213
+ w(s, "\xFF\x0F", 2);
214
+ }
215
+
216
+ static void ser_reset(Ser *s)
217
+ {
218
+ buf_reset(&s->b);
219
+ }
220
+
221
+ static void ser_null(Ser *s)
222
+ {
223
+ w_byte(s, '0');
224
+ }
225
+
226
+ static void ser_undefined(Ser *s)
227
+ {
228
+ w_byte(s, '_');
229
+ }
230
+
231
+ static void ser_bool(Ser *s, int v)
232
+ {
233
+ w_byte(s, "TF"[!v]);
234
+ }
235
+
236
+ static void ser_num(Ser *s, double v)
237
+ {
238
+ w_byte(s, 'N');
239
+ if (isnan(v)) {
240
+ w(s, the_nan, sizeof(the_nan));
241
+ } else {
242
+ w(s, &v, sizeof(v));
243
+ }
244
+ }
245
+
246
+ // ser_bigint: |n| is in bytes, not quadwords
247
+ static void ser_bigint(Ser *s, const uint64_t *p, size_t n, int sign)
248
+ {
249
+ if (*s->err)
250
+ return;
251
+ if (n % 8) {
252
+ snprintf(s->err, sizeof(s->err), "bad bigint");
253
+ return;
254
+ }
255
+ w_byte(s, 'Z');
256
+ // chop off high all-zero words
257
+ n /= 8;
258
+ while (n--)
259
+ if (p[n])
260
+ break;
261
+ if (n == (size_t)-1) {
262
+ w_byte(s, 0); // normalized zero
263
+ } else {
264
+ n = 8*n + 8;
265
+ w_varint(s, 2*n + (sign < 0));
266
+ w(s, p, n);
267
+ }
268
+ }
269
+
270
+ static void ser_int(Ser *s, int64_t v)
271
+ {
272
+ uint64_t t;
273
+ int sign;
274
+
275
+ if (*s->err)
276
+ return;
277
+ if (v < INT32_MIN || v > INT32_MAX) {
278
+ if (v > INT64_MIN/1024)
279
+ if (v <= INT64_MAX/1024)
280
+ return ser_num(s, v);
281
+ t = v < 0 ? -v : v;
282
+ sign = v < 0 ? -1 : 1;
283
+ ser_bigint(s, &t, sizeof(t), sign);
284
+ } else {
285
+ w_byte(s, 'I');
286
+ w_zigzag(s, v);
287
+ }
288
+ }
289
+
290
+ // |v| is the timestamp in milliseconds since the UNIX epoch
291
+ static void ser_date(Ser *s, double v)
292
+ {
293
+ w_byte(s, 'D');
294
+ if (isfinite(v)) {
295
+ w(s, &v, sizeof(v));
296
+ } else {
297
+ w(s, the_nan, sizeof(the_nan));
298
+ }
299
+ }
300
+
301
+ // string must be utf8
302
+ static void ser_string(Ser *s, const char *p, size_t n)
303
+ {
304
+ w_byte(s, 'S');
305
+ w_varint(s, n);
306
+ w(s, p, n);
307
+ }
308
+
309
+ // string must be latin1
310
+ static void ser_string8(Ser *s, const uint8_t *p, size_t n)
311
+ {
312
+ w_byte(s, '"');
313
+ w_varint(s, n);
314
+ w(s, p, n);
315
+ }
316
+
317
+ // string must be utf16le; |n| is in bytes, not code points
318
+ static void ser_string16(Ser *s, const void *p, size_t n)
319
+ {
320
+ w_byte(s, 'c');
321
+ w_varint(s, n);
322
+ w(s, p, n);
323
+ }
324
+
325
+ // Uint8Array: ArrayBuffer header + data + typed array view descriptor
326
+ static void ser_uint8array(Ser *s, const void *p, size_t n)
327
+ {
328
+ w_byte(s, 'B'); // ArrayBuffer tag
329
+ w_varint(s, n); // byte length
330
+ w(s, p, n); // raw bytes
331
+ w_byte(s, 'V'); // typed array view tag
332
+ w_byte(s, 'B'); // Uint8Array type
333
+ w_varint(s, 0); // byteOffset
334
+ w_varint(s, n); // byteLength
335
+ w_varint(s, 0); // flags
336
+ }
337
+
338
+ static void ser_object_begin(Ser *s)
339
+ {
340
+ w_byte(s, 'o');
341
+ }
342
+
343
+ // |count| is the property count
344
+ static void ser_object_end(Ser *s, uint32_t count)
345
+ {
346
+ w_byte(s, '{');
347
+ w_varint(s, count);
348
+ }
349
+
350
+ static void ser_object_ref(Ser *s, uint32_t id)
351
+ {
352
+ w_byte(s, '^');
353
+ w_varint(s, id);
354
+ }
355
+
356
+ static void ser_array_begin(Ser *s, uint32_t count)
357
+ {
358
+ w_byte(s, 'A'); // 'A'=dense, 'a'=sparse
359
+ w_varint(s, count); // element count
360
+ }
361
+
362
+ // |count| is the element count
363
+ static void ser_array_end(Ser *s, uint32_t count)
364
+ {
365
+ w_byte(s, '$');
366
+ w_varint(s, 0); // property count, always zero
367
+ w_varint(s, count); // element count
368
+ }
369
+
370
+ static int bail(char (*err)[64], const char *str)
371
+ {
372
+ snprintf(*err, sizeof(*err), "%s", str);
373
+ return -1;
374
+ }
375
+
376
+ static int des1_num(const uint8_t **p, const uint8_t *pe, double *d)
377
+ {
378
+ if (pe-*p < (int)sizeof(*d))
379
+ return -1;
380
+ memcpy(d, *p, sizeof(*d));
381
+ *p += sizeof(*d);
382
+ if (isnan(*d))
383
+ memcpy(d, the_nan, sizeof(the_nan));
384
+ return 0;
385
+ }
386
+
387
+ static int des1(char (*err)[64], const uint8_t **p, const uint8_t *pe,
388
+ void *arg, int depth)
389
+ {
390
+ uint64_t s, t, u;
391
+ uint8_t c;
392
+ int64_t i;
393
+ double d;
394
+
395
+ if (depth < 0)
396
+ return bail(err, "too much recursion");
397
+ again:
398
+ if (*p >= pe)
399
+ goto too_short;
400
+ switch ((c = *(*p)++)) {
401
+ default:
402
+ if (c > 32 && c < 127) {
403
+ snprintf(*err, sizeof(*err), "bad tag: %c", c);
404
+ } else {
405
+ snprintf(*err, sizeof(*err), "bad tag: %02x", c);
406
+ }
407
+ return -1;
408
+ case '\0': // skip alignment padding for two-byte strings
409
+ if (*p < pe)
410
+ goto again;
411
+ break;
412
+ case '^':
413
+ if (r_varint(p, pe, &u))
414
+ goto bad_varint;
415
+ des_object_ref(arg, u);
416
+ // object refs can (but need not be) followed by a typed array
417
+ // that is a view over the arraybufferview
418
+ goto typed_array;
419
+ case '0':
420
+ des_null(arg);
421
+ break;
422
+ case '_':
423
+ des_undefined(arg);
424
+ break;
425
+ case 'A': // dense array
426
+ if (r_varint(p, pe, &u))
427
+ goto bad_varint;
428
+ t = u;
429
+ des_array_begin(arg);
430
+ while (u--) {
431
+ if (*p >= pe)
432
+ goto too_short;
433
+ // '-' is 'the hole', a marker for representing absent
434
+ // elements that is inserted when a dense array turns
435
+ // sparse during serialization; we replace it with undefined
436
+ if (**p == '-') {
437
+ (*p)++;
438
+ des_undefined(arg);
439
+ } else {
440
+ if (des1(err, p, pe, arg, depth-1))
441
+ return -1;
442
+ }
443
+ }
444
+ for (s = 0; /*empty*/; s++) {
445
+ if (*p >= pe)
446
+ goto too_short;
447
+ if (**p == '$')
448
+ break;
449
+ if (s < 1)
450
+ des_named_props_begin(arg);
451
+ if (des1(err, p, pe, arg, depth-1)) // key
452
+ return -1;
453
+ if (des1(err, p, pe, arg, depth-1)) // value
454
+ return -1;
455
+ }
456
+ (*p)++;
457
+ if (s > 0)
458
+ des_named_props_end(arg);
459
+ if (r_varint(p, pe, &u))
460
+ goto bad_varint;
461
+ if (s != u)
462
+ return bail(err, "array property count mismatch");
463
+ if (r_varint(p, pe, &u))
464
+ goto bad_varint;
465
+ if (t != u)
466
+ return bail(err, "array element count mismatch");
467
+ des_array_end(arg);
468
+ break;
469
+ case 'B': // arraybuffer
470
+ case '~': // resizable arraybuffer (RAB)
471
+ if (r_varint(p, pe, &u))
472
+ goto bad_varint;
473
+ if (c == '~')
474
+ if (r_varint(p, pe, &t)) // maxByteLength, unused
475
+ goto bad_varint;
476
+ if (pe-*p < (int64_t)u)
477
+ goto too_short;
478
+ des_arraybuffer(arg, *p, u);
479
+ *p += u;
480
+ // arraybuffers can (but need not be) followed by a typed array
481
+ // that is a view over the arraybufferview
482
+ // typed arrays aren't efficiently representable in ruby, and the
483
+ // concept of a memory view is wholly unrepresentable, so we
484
+ // simply skip over them; callers get just the arraybuffer
485
+ typed_array:
486
+ if (pe-*p < 2)
487
+ break;
488
+ if (**p != 'V')
489
+ break;
490
+ (*p)++;
491
+ c = *(*p)++;
492
+ // ? DataView
493
+ // B Uint8Array
494
+ // C Uint8ClampedArray
495
+ // D Uint32Array
496
+ // F Float64Array
497
+ // Q BigUint64Array
498
+ // W Uint16Array
499
+ // b Int8Array
500
+ // d Int32Array
501
+ // f Float32Array
502
+ // h Float16Array
503
+ // q BigInt64Array
504
+ // w Int16Array
505
+ if (!strchr("?BCDFQWbdfhqw", **p))
506
+ return bail(err, "bad typed array");
507
+ if (r_varint(p, pe, &t)) // byteOffset
508
+ goto bad_varint;
509
+ if (r_varint(p, pe, &t)) // byteLength
510
+ goto bad_varint;
511
+ if (r_varint(p, pe, &t)) // flags, only non-zero when backed by RAB
512
+ goto bad_varint;
513
+ break;
514
+ case 'a': // sparse array
515
+ // total element count; ignored because we drop sparse entries
516
+ if (r_varint(p, pe, &t))
517
+ goto bad_varint;
518
+ des_array_begin(arg);
519
+ for (u = s = 0;;) {
520
+ if (*p >= pe)
521
+ goto too_short;
522
+ c = **p;
523
+ if (c == '@')
524
+ break;
525
+ if (c == 'I' && !s) {
526
+ u++, (*p)++;
527
+ if (r_zigzag(p, pe, &i)) // array index, ignored
528
+ goto bad_varint;
529
+ if (des1(err, p, pe, arg, depth-1))
530
+ return -1;
531
+ } else {
532
+ if (!s++)
533
+ des_named_props_begin(arg);
534
+ if (des1(err, p, pe, arg, depth-1)) // key
535
+ return -1;
536
+ if (des1(err, p, pe, arg, depth-1)) // value
537
+ return -1;
538
+ }
539
+ }
540
+ (*p)++;
541
+ if (s > 0)
542
+ des_named_props_end(arg);
543
+ if (r_varint(p, pe, &t))
544
+ goto bad_varint;
545
+ if (t != u+s)
546
+ return bail(err, "element count mismatch");
547
+ // total element count; ignored because we drop sparse entries
548
+ if (r_varint(p, pe, &t))
549
+ goto bad_varint;
550
+ des_array_end(arg);
551
+ break;
552
+ case 'D':
553
+ if (des1_num(p, pe, &d))
554
+ goto too_short;
555
+ des_date(arg, d);
556
+ break;
557
+ case 'F': // primitive boolean
558
+ case 'x': // new Boolean(...)
559
+ des_bool(arg, 0);
560
+ break;
561
+ case 'T': // primitive boolean
562
+ case 'y': // new Boolean(...)
563
+ des_bool(arg, 1);
564
+ break;
565
+ case 'I':
566
+ if (r_zigzag(p, pe, &i))
567
+ goto bad_varint;
568
+ des_int(arg, i);
569
+ break;
570
+ case 'N': // primitive number
571
+ case 'n': // new Number(...)
572
+ if (des1_num(p, pe, &d))
573
+ goto too_short;
574
+ des_num(arg, d);
575
+ break;
576
+ case 'Z':
577
+ if (r_varint(p, pe, &u))
578
+ goto bad_varint;
579
+ t = u & 1;
580
+ u = u >> 1;
581
+ if (u & 7)
582
+ return bail(err, "bad bigint");
583
+ // V8's serializer never emits -0n;
584
+ // its deserializer rejects it with DataCloneError
585
+ if (t && !u)
586
+ return bail(err, "negative zero bigint");
587
+ if (pe-*p < (int64_t)u)
588
+ goto too_short;
589
+ des_bigint(arg, *p, u, 1-2*t);
590
+ *p += u;
591
+ break;
592
+ case 'R': // RegExp, deserialized as string
593
+ if (*p >= pe)
594
+ goto too_short;
595
+ switch (**p) {
596
+ default:
597
+ return bail(err, "bad regexp");
598
+ case '"':
599
+ case 'S':
600
+ case 'c':
601
+ break;
602
+ }
603
+ if (des1(err, p, pe, arg, depth-1)) // pattern
604
+ return -1;
605
+ if (r_varint(p, pe, &t)) // flags; ignored
606
+ goto bad_varint;
607
+ break;
608
+ case 's': // string object, decoded as primitive string
609
+ if (*p >= pe)
610
+ goto too_short;
611
+ switch (*(*p)++) {
612
+ case '"':
613
+ goto s_string8;
614
+ case 'S':
615
+ goto s_string;
616
+ case 'c':
617
+ goto s_string16;
618
+ }
619
+ return bail(err, "bad string object");
620
+ case '"': // ascii/latin1
621
+ s_string8:
622
+ if (r_varint(p, pe, &u))
623
+ goto bad_varint;
624
+ if (pe-*p < (int64_t)u)
625
+ goto too_short;
626
+ des_string8(arg, *p, u);
627
+ *p += u;
628
+ break;
629
+ case 'S': // utf8
630
+ s_string:
631
+ if (r_varint(p, pe, &u))
632
+ goto bad_varint;
633
+ if (pe-*p < (int64_t)u)
634
+ goto too_short;
635
+ des_string(arg, (void *)*p, u);
636
+ *p += u;
637
+ break;
638
+ case 'c': // utf16-le
639
+ s_string16:
640
+ if (r_varint(p, pe, &u))
641
+ goto bad_varint;
642
+ if (pe-*p < (int64_t)u)
643
+ goto too_short;
644
+ if (u & 1)
645
+ return bail(err, "bad utf16 string size");
646
+ des_string16(arg, *p, u);
647
+ *p += u;
648
+ break;
649
+ case 'o':
650
+ des_object_begin(arg);
651
+ for (u = 0;; u++) {
652
+ if (pe-*p < 1)
653
+ goto too_short;
654
+ if (**p == '{')
655
+ break;
656
+ if (des1(err, p, pe, arg, depth-1)) // key
657
+ return -1;
658
+ if (des1(err, p, pe, arg, depth-1)) // value
659
+ return -1;
660
+ }
661
+ (*p)++;
662
+ if (r_varint(p, pe, &t))
663
+ goto bad_varint;
664
+ if (t != u)
665
+ return bail(err, "object properties count mismatch");
666
+ des_object_end(arg);
667
+ break;
668
+ case ';': // Map
669
+ des_map_begin(arg);
670
+ for (u = 0; /*empty*/; u++) {
671
+ if (*p >= pe)
672
+ goto too_short;
673
+ if (**p == ':')
674
+ break;
675
+ if (des1(err, p, pe, arg, depth-1)) // key
676
+ return -1;
677
+ if (des1(err, p, pe, arg, depth-1)) // value
678
+ return -1;
679
+ }
680
+ (*p)++;
681
+ if (r_varint(p, pe, &t))
682
+ goto bad_varint;
683
+ if (t != 2*u)
684
+ return bail(err, "map element count mismatch");
685
+ des_map_end(arg);
686
+ break;
687
+ case '\'': // Set
688
+ des_array_begin(arg);
689
+ for (u = 0; /*empty*/; u++) {
690
+ if (*p >= pe)
691
+ goto too_short;
692
+ if (**p == ',')
693
+ break;
694
+ if (des1(err, p, pe, arg, depth-1)) // value
695
+ return -1;
696
+ }
697
+ (*p)++;
698
+ if (r_varint(p, pe, &t))
699
+ goto bad_varint;
700
+ if (t != u)
701
+ return bail(err, "set element count mismatch");
702
+ des_array_end(arg);
703
+ break;
704
+ case 'r':
705
+ // shortest error is /r[.]/ - Error with no message, cause, or stack
706
+ // longest error is /r[EFRSTU]m<string>c<any>s<string>[.]/ where
707
+ // EFRSTU is one of {Eval,Reference,Range,Syntax,Type,URI}Error
708
+ des_error_begin(arg);
709
+ des_object_begin(arg);
710
+ if (*p >= pe)
711
+ goto too_short;
712
+ c = *(*p)++;
713
+ if (!strchr("EFRSTU", c))
714
+ goto r_message;
715
+ if (*p >= pe)
716
+ goto too_short;
717
+ c = *(*p)++;
718
+ r_message:
719
+ if (c != 'm')
720
+ goto r_stack;
721
+ des_string(arg, "message", sizeof("message")-1);
722
+ if (*p >= pe)
723
+ goto too_short;
724
+ if (!strchr("\"Sc", **p))
725
+ return bail(err, "error .message is not a string");
726
+ if (des1(err, p, pe, arg, depth-1))
727
+ return -1;
728
+ if (*p >= pe)
729
+ goto too_short;
730
+ c = *(*p)++;
731
+ r_stack:
732
+ if (c != 's')
733
+ goto r_cause;
734
+ des_string(arg, "stack", sizeof("stack")-1);
735
+ if (*p >= pe)
736
+ goto too_short;
737
+ if (!strchr("\"Sc", **p))
738
+ return bail(err, "error .stack is not a string");
739
+ if (des1(err, p, pe, arg, depth-1))
740
+ return -1;
741
+ if (*p >= pe)
742
+ goto too_short;
743
+ c = *(*p)++;
744
+ r_cause:
745
+ if (c != 'c')
746
+ goto r_end;
747
+ des_string(arg, "cause", sizeof("cause")-1);
748
+ if (des1(err, p, pe, arg, depth-1))
749
+ return -1;
750
+ if (*p >= pe)
751
+ goto too_short;
752
+ c = *(*p)++;
753
+ r_end:
754
+ if (c != '.')
755
+ return bail(err, "bad error object");
756
+ des_object_end(arg);
757
+ des_error_end(arg);
758
+ break;
759
+ }
760
+ return 0;
761
+ too_short:
762
+ return bail(err, "input too short");
763
+ bad_varint:
764
+ return bail(err, "bad varint");
765
+ }
766
+
767
+ int des(char (*err)[64], const void *b, size_t n, void *arg)
768
+ {
769
+ const uint8_t *p, *pe;
770
+
771
+ p = b, pe = p + n;
772
+ if (n < 2)
773
+ return bail(err, "input too short");
774
+ if (*p++ != 255)
775
+ return bail(err, "bad header");
776
+ if (*p++ != 15)
777
+ return bail(err, "bad version");
778
+ while (p < pe)
779
+ if (des1(err, &p, pe, arg, /*depth*/96))
780
+ return -1;
781
+ return 0;
782
+ }