1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
|
/*
* upb - a minimalist implementation of protocol buffers.
*
* Copyright (c) 2008-2013 Google Inc. See LICENSE for details.
* Author: Josh Haberman <jhaberman@gmail.com>
*/
#include <inttypes.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include "upb/pb/decoder.int.h"
#include "upb/pb/varint.int.h"
#ifdef UPB_DUMP_BYTECODE
#include <stdio.h>
#endif
#define CHECK_SUSPEND(x) if (!(x)) return upb_pbdecoder_suspend(d);
// Error messages that are shared between the bytecode and JIT decoders.
const char *kPbDecoderStackOverflow = "Nesting too deep.";
// Error messages shared within this file.
static const char *kUnterminatedVarint = "Unterminated varint.";
/* upb_pbdecoder **************************************************************/
static opcode halt = OP_HALT;
// Whether an op consumes any of the input buffer.
static bool consumes_input(opcode op) {
switch (op) {
case OP_SETDISPATCH:
case OP_STARTMSG:
case OP_ENDMSG:
case OP_STARTSEQ:
case OP_ENDSEQ:
case OP_STARTSUBMSG:
case OP_ENDSUBMSG:
case OP_STARTSTR:
case OP_ENDSTR:
case OP_PUSHTAGDELIM:
case OP_POP:
case OP_SETDELIM:
case OP_SETBIGGROUPNUM:
case OP_CHECKDELIM:
case OP_CALL:
case OP_RET:
case OP_BRANCH:
return false;
default:
return true;
}
}
static bool in_residual_buf(upb_pbdecoder *d, const char *p);
// It's unfortunate that we have to micro-manage the compiler this way,
// especially since this tuning is necessarily specific to one hardware
// configuration. But emperically on a Core i7, performance increases 30-50%
// with these annotations. Every instance where these appear, gcc 4.2.1 made
// the wrong decision and degraded performance in benchmarks.
#define FORCEINLINE static inline __attribute__((always_inline))
#define NOINLINE __attribute__((noinline))
static void seterr(upb_pbdecoder *d, const char *msg) {
// TODO(haberman): encapsulate this access to pipeline->status, but not sure
// exactly what that interface should look like.
upb_status_seterrmsg(d->status, msg);
}
void upb_pbdecoder_seterr(upb_pbdecoder *d, const char *msg) {
seterr(d, msg);
}
/* Buffering ******************************************************************/
// We operate on one buffer at a time, which is either the user's buffer passed
// to our "decode" callback or some residual bytes from the previous buffer.
// How many bytes can be safely read from d->ptr without reading past end-of-buf
// or past the current delimited end.
static size_t curbufleft(upb_pbdecoder *d) {
assert(d->data_end >= d->ptr);
return d->data_end - d->ptr;
}
static const char *ptr(upb_pbdecoder *d) {
return d->ptr;
}
// Overall offset of d->ptr.
uint64_t offset(upb_pbdecoder *d) {
return d->bufstart_ofs + (ptr(d) - d->buf);
}
// Advances d->ptr.
static void advance(upb_pbdecoder *d, size_t len) {
assert(curbufleft(d) >= len);
d->ptr += len;
}
static bool in_buf(const char *p, const char *buf, const char *end) {
return p >= buf && p <= end;
}
static bool in_residual_buf(upb_pbdecoder *d, const char *p) {
return in_buf(p, d->residual, d->residual_end);
}
// Calculates the delim_end value, which represents a combination of the
// current buffer and the stack, so must be called whenever either is updated.
static void set_delim_end(upb_pbdecoder *d) {
size_t delim_ofs = d->top->end_ofs - d->bufstart_ofs;
if (delim_ofs <= (d->end - d->buf)) {
d->delim_end = d->buf + delim_ofs;
d->data_end = d->delim_end;
} else {
d->data_end = d->end;
d->delim_end = NULL;
}
}
static void switchtobuf(upb_pbdecoder *d, const char *buf, const char *end) {
d->ptr = buf;
d->buf = buf;
d->end = end;
set_delim_end(d);
}
static void advancetobuf(upb_pbdecoder *d, const char *buf, size_t len) {
assert(curbufleft(d) == 0);
d->bufstart_ofs += (d->end - d->buf);
switchtobuf(d, buf, buf + len);
}
static void checkpoint(upb_pbdecoder *d) {
// The assertion here is in the interests of efficiency, not correctness.
// We are trying to ensure that we don't checkpoint() more often than
// necessary.
assert(d->checkpoint != ptr(d));
d->checkpoint = ptr(d);
}
// Resumes the decoder from an initial state or from a previous suspend.
int32_t upb_pbdecoder_resume(upb_pbdecoder *d, void *p, const char *buf,
size_t size, const upb_bufhandle *handle) {
UPB_UNUSED(p); // Useless; just for the benefit of the JIT.
d->buf_param = buf;
d->size_param = size;
d->handle = handle;
if (d->residual_end > d->residual) {
// We have residual bytes from the last buffer.
assert(ptr(d) == d->residual);
} else {
switchtobuf(d, buf, buf + size);
}
d->checkpoint = ptr(d);
if (d->top->groupnum < 0) {
CHECK_RETURN(upb_pbdecoder_skipunknown(d, -1, 0));
d->checkpoint = ptr(d);
}
return DECODE_OK;
}
// Suspends the decoder at the last checkpoint, without saving any residual
// bytes. If there are any unconsumed bytes, returns a short byte count.
size_t upb_pbdecoder_suspend(upb_pbdecoder *d) {
d->pc = d->last;
if (d->checkpoint == d->residual) {
// Checkpoint was in residual buf; no user bytes were consumed.
d->ptr = d->residual;
return 0;
} else {
assert(!in_residual_buf(d, d->checkpoint));
assert(d->buf == d->buf_param);
size_t consumed = d->checkpoint - d->buf;
d->bufstart_ofs += consumed;
d->residual_end = d->residual;
switchtobuf(d, d->residual, d->residual_end);
return consumed;
}
}
// Suspends the decoder at the last checkpoint, and saves any unconsumed
// bytes in our residual buffer. This is necessary if we need more user
// bytes to form a complete value, which might not be contiguous in the
// user's buffers. Always consumes all user bytes.
static size_t suspend_save(upb_pbdecoder *d) {
// We hit end-of-buffer before we could parse a full value.
// Save any unconsumed bytes (if any) to the residual buffer.
d->pc = d->last;
if (d->checkpoint == d->residual) {
// Checkpoint was in residual buf; append user byte(s) to residual buf.
assert((d->residual_end - d->residual) + d->size_param <=
sizeof(d->residual));
if (!in_residual_buf(d, ptr(d))) {
d->bufstart_ofs -= (d->residual_end - d->residual);
}
memcpy(d->residual_end, d->buf_param, d->size_param);
d->residual_end += d->size_param;
} else {
// Checkpoint was in user buf; old residual bytes not needed.
assert(!in_residual_buf(d, d->checkpoint));
d->ptr = d->checkpoint;
size_t save = curbufleft(d);
assert(save <= sizeof(d->residual));
memcpy(d->residual, ptr(d), save);
d->residual_end = d->residual + save;
d->bufstart_ofs = offset(d);
}
switchtobuf(d, d->residual, d->residual_end);
return d->size_param;
}
static int32_t skip(upb_pbdecoder *d, size_t bytes) {
assert(!in_residual_buf(d, ptr(d)) || d->size_param == 0);
if (curbufleft(d) >= bytes) {
// Skipped data is all in current buffer.
advance(d, bytes);
return DECODE_OK;
} else {
// Skipped data extends beyond currently available buffers.
d->pc = d->last;
size_t skip = bytes - curbufleft(d);
d->bufstart_ofs += (d->end - d->buf) + skip;
d->residual_end = d->residual;
switchtobuf(d, d->residual, d->residual_end);
return d->size_param + skip;
}
}
FORCEINLINE void consumebytes(upb_pbdecoder *d, void *buf, size_t bytes) {
assert(bytes <= curbufleft(d));
memcpy(buf, ptr(d), bytes);
advance(d, bytes);
}
static NOINLINE int32_t getbytes_slow(upb_pbdecoder *d, void *buf,
size_t bytes) {
const size_t avail = curbufleft(d);
consumebytes(d, buf, avail);
bytes -= avail;
assert(bytes > 0);
if (in_residual_buf(d, ptr(d))) {
advancetobuf(d, d->buf_param, d->size_param);
}
if (curbufleft(d) >= bytes) {
consumebytes(d, buf + avail, bytes);
return DECODE_OK;
} else if (d->data_end == d->delim_end) {
seterr(d, "Submessage ended in the middle of a value or group");
return upb_pbdecoder_suspend(d);
} else {
return suspend_save(d);
}
}
FORCEINLINE int32_t getbytes(upb_pbdecoder *d, void *buf, size_t bytes) {
if (curbufleft(d) >= bytes) {
// Buffer has enough data to satisfy.
consumebytes(d, buf, bytes);
return DECODE_OK;
} else {
return getbytes_slow(d, buf, bytes);
}
}
static NOINLINE size_t peekbytes_slow(upb_pbdecoder *d, void *buf,
size_t bytes) {
size_t ret = curbufleft(d);
memcpy(buf, ptr(d), ret);
if (in_residual_buf(d, ptr(d))) {
size_t copy = UPB_MIN(bytes - ret, d->size_param);
memcpy(buf + ret, d->buf_param, copy);
ret += copy;
}
return ret;
}
FORCEINLINE size_t peekbytes(upb_pbdecoder *d, void *buf, size_t bytes) {
if (curbufleft(d) >= bytes) {
memcpy(buf, ptr(d), bytes);
return bytes;
} else {
return peekbytes_slow(d, buf, bytes);
}
}
/* Decoding of wire types *****************************************************/
NOINLINE int32_t upb_pbdecoder_decode_varint_slow(upb_pbdecoder *d,
uint64_t *u64) {
*u64 = 0;
uint8_t byte = 0x80;
int bitpos;
for(bitpos = 0; bitpos < 70 && (byte & 0x80); bitpos += 7) {
int32_t ret = getbytes(d, &byte, 1);
if (ret >= 0) return ret;
*u64 |= (uint64_t)(byte & 0x7F) << bitpos;
}
if(bitpos == 70 && (byte & 0x80)) {
seterr(d, kUnterminatedVarint);
return upb_pbdecoder_suspend(d);
}
return DECODE_OK;
}
FORCEINLINE int32_t decode_varint(upb_pbdecoder *d, uint64_t *u64) {
if (curbufleft(d) > 0 && !(*ptr(d) & 0x80)) {
*u64 = *ptr(d);
advance(d, 1);
return DECODE_OK;
} else if (curbufleft(d) >= 10) {
// Fast case.
upb_decoderet r = upb_vdecode_fast(ptr(d));
if (r.p == NULL) {
seterr(d, kUnterminatedVarint);
return upb_pbdecoder_suspend(d);
}
advance(d, r.p - ptr(d));
*u64 = r.val;
return DECODE_OK;
} else {
// Slow case -- varint spans buffer seam.
return upb_pbdecoder_decode_varint_slow(d, u64);
}
}
FORCEINLINE int32_t decode_v32(upb_pbdecoder *d, uint32_t *u32) {
uint64_t u64;
int32_t ret = decode_varint(d, &u64);
if (ret >= 0) return ret;
if (u64 > UINT32_MAX) {
seterr(d, "Unterminated 32-bit varint");
// TODO(haberman) guarantee that this function return is >= 0 somehow,
// so we know this path will always be treated as error by our caller.
// Right now the size_t -> int32_t can overflow and produce negative values.
*u32 = 0;
return upb_pbdecoder_suspend(d);
}
*u32 = u64;
return DECODE_OK;
}
// TODO: proper byte swapping for big-endian machines.
FORCEINLINE int32_t decode_fixed32(upb_pbdecoder *d, uint32_t *u32) {
return getbytes(d, u32, 4);
}
// TODO: proper byte swapping for big-endian machines.
FORCEINLINE int32_t decode_fixed64(upb_pbdecoder *d, uint64_t *u64) {
return getbytes(d, u64, 8);
}
int32_t upb_pbdecoder_decode_f32(upb_pbdecoder *d, uint32_t *u32) {
return decode_fixed32(d, u32);
}
int32_t upb_pbdecoder_decode_f64(upb_pbdecoder *d, uint64_t *u64) {
return decode_fixed64(d, u64);
}
static double as_double(uint64_t n) { double d; memcpy(&d, &n, 8); return d; }
static float as_float(uint32_t n) { float f; memcpy(&f, &n, 4); return f; }
static bool push(upb_pbdecoder *d, uint64_t end) {
upb_pbdecoder_frame *fr = d->top;
if (end > fr->end_ofs) {
seterr(d, "Submessage end extends past enclosing submessage.");
return false;
} else if ((fr + 1) == d->limit) {
seterr(d, kPbDecoderStackOverflow);
return false;
}
fr++;
fr->end_ofs = end;
fr->dispatch = NULL;
fr->groupnum = 0;
d->top = fr;
return true;
}
static bool pushtagdelim(upb_pbdecoder *d, uint32_t arg) {
// While we expect to see an "end" tag (either ENDGROUP or a non-sequence
// field number) prior to hitting any enclosing submessage end, pushing our
// existing delim end prevents us from continuing to parse values from a
// corrupt proto that doesn't give us an END tag in time.
if (!push(d, d->top->end_ofs))
return false;
d->top->groupnum = arg;
return true;
}
static void pop(upb_pbdecoder *d) { d->top--; }
NOINLINE int32_t upb_pbdecoder_checktag_slow(upb_pbdecoder *d,
uint64_t expected) {
uint64_t data = 0;
size_t bytes = upb_value_size(expected);
size_t read = peekbytes(d, &data, bytes);
if (read == bytes && data == expected) {
// Advance past matched bytes.
int32_t ok = getbytes(d, &data, read);
UPB_ASSERT_VAR(ok, ok < 0);
return DECODE_OK;
} else if (read < bytes && memcmp(&data, &expected, read) == 0) {
return suspend_save(d);
} else {
return DECODE_MISMATCH;
}
}
int32_t upb_pbdecoder_skipunknown(upb_pbdecoder *d, int32_t fieldnum,
uint8_t wire_type) {
if (fieldnum >= 0)
goto have_tag;
while (true) {
uint32_t tag;
CHECK_RETURN(decode_v32(d, &tag));
wire_type = tag & 0x7;
fieldnum = tag >> 3;
have_tag:
if (fieldnum == 0) {
seterr(d, "Saw invalid field number (0)");
return upb_pbdecoder_suspend(d);
}
// TODO: deliver to unknown field callback.
switch (wire_type) {
case UPB_WIRE_TYPE_32BIT:
CHECK_RETURN(skip(d, 4));
break;
case UPB_WIRE_TYPE_64BIT:
CHECK_RETURN(skip(d, 8));
break;
case UPB_WIRE_TYPE_VARINT: {
uint64_t u64;
CHECK_RETURN(decode_varint(d, &u64));
break;
}
case UPB_WIRE_TYPE_DELIMITED: {
uint32_t len;
CHECK_RETURN(decode_v32(d, &len));
CHECK_RETURN(skip(d, len));
break;
}
case UPB_WIRE_TYPE_START_GROUP:
CHECK_SUSPEND(pushtagdelim(d, -fieldnum));
break;
case UPB_WIRE_TYPE_END_GROUP:
if (fieldnum == -d->top->groupnum) {
pop(d);
} else if (fieldnum == d->top->groupnum) {
return DECODE_ENDGROUP;
} else {
seterr(d, "Unmatched ENDGROUP tag.");
return upb_pbdecoder_suspend(d);
}
break;
default:
seterr(d, "Invalid wire type");
return upb_pbdecoder_suspend(d);
}
if (d->top->groupnum >= 0) {
return DECODE_OK;
}
if (ptr(d) == d->delim_end) {
seterr(d, "Enclosing submessage ended in the middle of value or group");
// Unlike most errors we notice during parsing, right now we have consumed
// all of the user's input.
//
// There are three different options for how to handle this case:
//
// 1. decode() = short count, error = set
// 2. decode() = full count, error = set
// 3. decode() = full count, error NOT set, short count and error will
// be reported on next call to decode() (or end())
//
// (1) and (3) have the advantage that they preserve the invariant that an
// error occurs iff decode() returns a short count.
//
// (2) and (3) have the advantage of reflecting the fact that all of the
// bytes were in fact parsed (and possibly delivered to the unknown field
// handler, in the future when that is supported).
//
// (3) requires extra state in the decode (a place to store the "permanent
// error" that we should return for all subsequent attempts to decode).
// But we likely want this anyway.
//
// Right now we do (1), thanks to the fact that we checkpoint *after* this
// check. (3) may be a better choice long term; unclear at the moment.
return upb_pbdecoder_suspend(d);
}
checkpoint(d);
}
}
static void goto_endmsg(upb_pbdecoder *d) {
upb_value v;
bool found = upb_inttable_lookup32(d->top->dispatch, DISPATCH_ENDMSG, &v);
UPB_ASSERT_VAR(found, found);
d->pc = d->top->base + upb_value_getuint64(v);
}
static int32_t dispatch(upb_pbdecoder *d) {
upb_inttable *dispatch = d->top->dispatch;
// Decode tag.
uint32_t tag;
CHECK_RETURN(decode_v32(d, &tag));
uint8_t wire_type = tag & 0x7;
uint32_t fieldnum = tag >> 3;
// Lookup tag. Because of packed/non-packed compatibility, we have to
// check the wire type against two possibilities.
upb_value val;
if (upb_inttable_lookup32(dispatch, fieldnum, &val)) {
uint64_t v = upb_value_getuint64(val);
if (wire_type == (v & 0xff)) {
d->pc = d->top->base + (v >> 16);
return DECODE_OK;
} else if (wire_type == ((v >> 8) & 0xff)) {
bool found =
upb_inttable_lookup(dispatch, fieldnum + UPB_MAX_FIELDNUMBER, &val);
UPB_ASSERT_VAR(found, found);
d->pc = d->top->base + upb_value_getuint64(val);
return DECODE_OK;
}
}
// Unknown field or ENDGROUP.
int32_t ret = upb_pbdecoder_skipunknown(d, fieldnum, wire_type);
if (ret == DECODE_ENDGROUP) {
goto_endmsg(d);
return DECODE_OK;
} else {
d->pc = d->last - 1; // Rewind to CHECKDELIM.
return ret;
}
}
// Callers know that the stack is more than one deep because the opcodes that
// call this only occur after PUSH operations.
upb_pbdecoder_frame *outer_frame(upb_pbdecoder *d) {
assert(d->top != d->stack);
return d->top - 1;
}
/* The main decoding loop *****************************************************/
size_t upb_pbdecoder_decode(void *closure, const void *hd, const char *buf,
size_t size, const upb_bufhandle *handle) {
upb_pbdecoder *d = closure;
const mgroup *group = hd;
assert(buf);
int32_t result = upb_pbdecoder_resume(d, NULL, buf, size, handle);
if (result == DECODE_ENDGROUP) {
goto_endmsg(d);
}
CHECK_RETURN(result);
UPB_UNUSED(group);
#define VMCASE(op, code) \
case op: { code; if (consumes_input(op)) checkpoint(d); break; }
#define PRIMITIVE_OP(type, wt, name, convfunc, ctype) \
VMCASE(OP_PARSE_ ## type, { \
ctype val; \
CHECK_RETURN(decode_ ## wt(d, &val)); \
upb_sink_put ## name(&d->top->sink, arg, (convfunc)(val)); \
})
while(1) {
d->last = d->pc;
int32_t instruction = *d->pc++;
opcode op = getop(instruction);
uint32_t arg = instruction >> 8;
int32_t longofs = arg;
assert(ptr(d) != d->residual_end);
#ifdef UPB_DUMP_BYTECODE
fprintf(stderr, "s_ofs=%d buf_ofs=%d data_rem=%d buf_rem=%d delim_rem=%d "
"%x %s (%d)\n",
(int)offset(d),
(int)(ptr(d) - d->buf),
(int)(d->data_end - ptr(d)),
(int)(d->end - ptr(d)),
(int)((d->top->end_ofs - d->bufstart_ofs) - (ptr(d) - d->buf)),
(int)(d->pc - 1 - group->bytecode),
upb_pbdecoder_getopname(op),
arg);
#endif
switch (op) {
// Technically, we are losing data if we see a 32-bit varint that is not
// properly sign-extended. We could detect this and error about the data
// loss, but proto2 does not do this, so we pass.
PRIMITIVE_OP(INT32, varint, int32, int32_t, uint64_t)
PRIMITIVE_OP(INT64, varint, int64, int64_t, uint64_t)
PRIMITIVE_OP(UINT32, varint, uint32, uint32_t, uint64_t)
PRIMITIVE_OP(UINT64, varint, uint64, uint64_t, uint64_t)
PRIMITIVE_OP(FIXED32, fixed32, uint32, uint32_t, uint32_t)
PRIMITIVE_OP(FIXED64, fixed64, uint64, uint64_t, uint64_t)
PRIMITIVE_OP(SFIXED32, fixed32, int32, int32_t, uint32_t)
PRIMITIVE_OP(SFIXED64, fixed64, int64, int64_t, uint64_t)
PRIMITIVE_OP(BOOL, varint, bool, bool, uint64_t)
PRIMITIVE_OP(DOUBLE, fixed64, double, as_double, uint64_t)
PRIMITIVE_OP(FLOAT, fixed32, float, as_float, uint32_t)
PRIMITIVE_OP(SINT32, varint, int32, upb_zzdec_32, uint64_t)
PRIMITIVE_OP(SINT64, varint, int64, upb_zzdec_64, uint64_t)
VMCASE(OP_SETDISPATCH,
d->top->base = d->pc - 1;
memcpy(&d->top->dispatch, d->pc, sizeof(void*));
d->pc += sizeof(void*) / sizeof(uint32_t);
)
VMCASE(OP_STARTMSG,
CHECK_SUSPEND(upb_sink_startmsg(&d->top->sink));
)
VMCASE(OP_ENDMSG,
CHECK_SUSPEND(upb_sink_endmsg(&d->top->sink, d->status));
)
VMCASE(OP_STARTSEQ,
upb_pbdecoder_frame *outer = outer_frame(d);
CHECK_SUSPEND(upb_sink_startseq(&outer->sink, arg, &d->top->sink));
)
VMCASE(OP_ENDSEQ,
CHECK_SUSPEND(upb_sink_endseq(&d->top->sink, arg));
)
VMCASE(OP_STARTSUBMSG,
upb_pbdecoder_frame *outer = outer_frame(d);
CHECK_SUSPEND(upb_sink_startsubmsg(&outer->sink, arg, &d->top->sink));
)
VMCASE(OP_ENDSUBMSG,
CHECK_SUSPEND(upb_sink_endsubmsg(&d->top->sink, arg));
)
VMCASE(OP_STARTSTR,
uint32_t len = d->top->end_ofs - offset(d);
upb_pbdecoder_frame *outer = outer_frame(d);
CHECK_SUSPEND(upb_sink_startstr(&outer->sink, arg, len, &d->top->sink));
if (len == 0) {
d->pc++; // Skip OP_STRING.
}
)
VMCASE(OP_STRING,
uint32_t len = curbufleft(d);
size_t n = upb_sink_putstring(&d->top->sink, arg, ptr(d), len, handle);
if (n > len) {
if (n > d->top->end_ofs - offset(d)) {
seterr(d, "Tried to skip past end of string.");
return upb_pbdecoder_suspend(d);
} else {
return skip(d, n);
}
} else if (n < len) {
advance(d, n);
return upb_pbdecoder_suspend(d);
} else {
advance(d, n);
if (d->delim_end == NULL) { // String extends beyond this buf?
d->pc--; // Do OP_STRING again when we resume.
d->bufstart_ofs += size;
d->residual_end = d->residual;
return size;
}
}
)
VMCASE(OP_ENDSTR,
CHECK_SUSPEND(upb_sink_endstr(&d->top->sink, arg));
)
VMCASE(OP_PUSHTAGDELIM,
CHECK_SUSPEND(pushtagdelim(d, arg));
)
VMCASE(OP_SETBIGGROUPNUM,
d->top->groupnum = *d->pc++;
)
VMCASE(OP_POP,
assert(d->top > d->stack);
pop(d);
)
VMCASE(OP_PUSHLENDELIM,
uint32_t len;
CHECK_RETURN(decode_v32(d, &len));
CHECK_SUSPEND(push(d, offset(d) + len));
set_delim_end(d);
)
VMCASE(OP_SETDELIM,
set_delim_end(d);
)
VMCASE(OP_CHECKDELIM,
// We are guaranteed of this assert because we never allow ourselves to
// consume bytes beyond data_end, which covers delim_end when non-NULL.
assert(!(d->delim_end && ptr(d) > d->delim_end));
if (ptr(d) == d->delim_end)
d->pc += longofs;
)
VMCASE(OP_CALL,
d->callstack[d->call_len++] = d->pc;
d->pc += longofs;
)
VMCASE(OP_RET,
assert(d->call_len > 0);
d->pc = d->callstack[--d->call_len];
)
VMCASE(OP_BRANCH,
d->pc += longofs;
)
VMCASE(OP_TAG1,
CHECK_SUSPEND(curbufleft(d) > 0);
uint8_t expected = (arg >> 8) & 0xff;
if (*ptr(d) == expected) {
advance(d, 1);
} else {
int8_t shortofs;
badtag:
shortofs = arg;
if (shortofs == LABEL_DISPATCH) {
CHECK_RETURN(dispatch(d));
} else {
d->pc += shortofs;
break; // Avoid checkpoint().
}
}
)
VMCASE(OP_TAG2,
CHECK_SUSPEND(curbufleft(d) > 0);
uint16_t expected = (arg >> 8) & 0xffff;
if (curbufleft(d) >= 2) {
uint16_t actual;
memcpy(&actual, ptr(d), 2);
if (expected == actual) {
advance(d, 2);
} else {
goto badtag;
}
} else {
int32_t result = upb_pbdecoder_checktag_slow(d, expected);
if (result == DECODE_MISMATCH) goto badtag;
if (result >= 0) return result;
}
)
VMCASE(OP_TAGN, {
uint64_t expected;
memcpy(&expected, d->pc, 8);
d->pc += 2;
int32_t result = upb_pbdecoder_checktag_slow(d, expected);
if (result == DECODE_MISMATCH) goto badtag;
if (result >= 0) return result;
})
VMCASE(OP_HALT, {
return size;
})
}
}
}
void *upb_pbdecoder_startbc(void *closure, const void *pc, size_t size_hint) {
upb_pbdecoder *d = closure;
UPB_UNUSED(size_hint);
d->call_len = 1;
d->pc = pc;
return d;
}
void *upb_pbdecoder_startjit(void *closure, const void *hd, size_t size_hint) {
UPB_UNUSED(hd);
UPB_UNUSED(size_hint);
upb_pbdecoder *d = closure;
d->call_len = 0;
return d;
}
bool upb_pbdecoder_end(void *closure, const void *handler_data) {
upb_pbdecoder *d = closure;
const upb_pbdecodermethod *method = handler_data;
if (d->residual_end > d->residual) {
seterr(d, "Unexpected EOF");
return false;
}
if (d->top->end_ofs != UINT64_MAX) {
seterr(d, "Unexpected EOF inside delimited string");
return false;
}
// Message ends here.
uint64_t end = offset(d);
d->top->end_ofs = end;
char dummy;
#ifdef UPB_USE_JIT_X64
const mgroup *group = (const mgroup*)method->group;
if (group->jit_code) {
if (d->top != d->stack)
d->stack->end_ofs = 0;
group->jit_code(closure, method->code_base.ptr, &dummy, 0, NULL);
} else {
#endif
d->stack->end_ofs = end;
const uint32_t *p = d->pc;
// Check the previous bytecode, but guard against beginning.
if (p != method->code_base.ptr) p--;
if (getop(*p) == OP_CHECKDELIM) {
// Rewind from OP_TAG* to OP_CHECKDELIM.
assert(getop(*d->pc) == OP_TAG1 ||
getop(*d->pc) == OP_TAG2 ||
getop(*d->pc) == OP_TAGN);
d->pc = p;
}
upb_pbdecoder_decode(closure, handler_data, &dummy, 0, NULL);
#ifdef UPB_USE_JIT_X64
}
#endif
if (d->call_len != 0) {
seterr(d, "Unexpected EOF");
return false;
}
return true;
}
void upb_pbdecoder_init(upb_pbdecoder *d, const upb_pbdecodermethod *m,
upb_status *s) {
d->limit = &d->stack[UPB_DECODER_MAX_NESTING];
upb_bytessink_reset(&d->input_, &m->input_handler_, d);
d->method_ = m;
d->callstack[0] = &halt;
d->status = s;
upb_pbdecoder_reset(d);
}
void upb_pbdecoder_reset(upb_pbdecoder *d) {
d->top = d->stack;
d->top->end_ofs = UINT64_MAX;
d->top->groupnum = 0;
d->bufstart_ofs = 0;
d->ptr = d->residual;
d->buf = d->residual;
d->end = d->residual;
d->residual_end = d->residual;
d->call_len = 1;
}
// Not currently required, but to support outgrowing the static stack we need
// this.
void upb_pbdecoder_uninit(upb_pbdecoder *d) {
UPB_UNUSED(d);
}
const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d) {
return d->method_;
}
bool upb_pbdecoder_resetoutput(upb_pbdecoder *d, upb_sink* sink) {
// TODO(haberman): do we need to test whether the decoder is already on the
// stack (like calling this from within a callback)? Should we support
// rebinding the output at all?
assert(sink);
if (d->method_->dest_handlers_) {
if (sink->handlers != d->method_->dest_handlers_)
return false;
}
upb_sink_reset(&d->top->sink, sink->handlers, sink->closure);
return true;
}
upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d) {
return &d->input_;
}
|