|
|
@ -11,127 +11,39 @@ |
|
|
|
#include <stdlib.h> |
|
|
|
#include <stdlib.h> |
|
|
|
#include "upb_def.h" |
|
|
|
#include "upb_def.h" |
|
|
|
|
|
|
|
|
|
|
|
/* Functions to read wire values. *********************************************/ |
|
|
|
/* Pure Decoding **************************************************************/ |
|
|
|
|
|
|
|
|
|
|
|
// These functions are internal to the decode, but might be moved into an
|
|
|
|
// The key fast-path varint-decoding routine. There are a lot of possibilities
|
|
|
|
// internal header file if we at some point in the future opt to do code
|
|
|
|
// for optimization/experimentation here.
|
|
|
|
// generation, because the generated code would want to inline these functions.
|
|
|
|
INLINE bool upb_decode_varint_fast(uint8_t **buf, uint8_t *end, uint64_t &val, |
|
|
|
// The same applies to the functions to read .proto values below.
|
|
|
|
upb_status *status) { |
|
|
|
|
|
|
|
*high = 0; |
|
|
|
const uint8_t *upb_get_v_uint64_t_full(const uint8_t *buf, const uint8_t *end, |
|
|
|
uint32_t b; |
|
|
|
uint64_t *val, upb_status *status); |
|
|
|
uint8_t *ptr = p->ptr; |
|
|
|
|
|
|
|
b = *(*buf++); *low = (b & 0x7f) ; if(!(b & 0x80)) goto done; |
|
|
|
// Gets a varint (wire type: UPB_WIRE_TYPE_VARINT).
|
|
|
|
b = *(*buf++); *low |= (b & 0x7f) << 7; if(!(b & 0x80)) goto done; |
|
|
|
INLINE const uint8_t *upb_get_v_uint64_t(const uint8_t *buf, const uint8_t *end, |
|
|
|
b = *(*buf++); *low |= (b & 0x7f) << 14; if(!(b & 0x80)) goto done; |
|
|
|
uint64_t *val, upb_status *status) |
|
|
|
b = *(*buf++); *low |= (b & 0x7f) << 21; if(!(b & 0x80)) goto done; |
|
|
|
{ |
|
|
|
b = *(*buf++); *low |= (b & 0x7f) << 28; |
|
|
|
// We inline this common case (1-byte varints), if that fails we dispatch to
|
|
|
|
*high = (b & 0x7f) >> 3; if(!(b & 0x80)) goto done; |
|
|
|
// the full (non-inlined) version.
|
|
|
|
b = *(*buf++); *high |= (b & 0x7f) << 4; if(!(b & 0x80)) goto done; |
|
|
|
if((*buf & 0x80) == 0) { |
|
|
|
b = *(*buf++); *high |= (b & 0x7f) << 11; if(!(b & 0x80)) goto done; |
|
|
|
*val = *buf & 0x7f; |
|
|
|
b = *(*buf++); *high |= (b & 0x7f) << 18; if(!(b & 0x80)) goto done; |
|
|
|
return buf + 1; |
|
|
|
b = *(*buf++); *high |= (b & 0x7f) << 25; if(!(b & 0x80)) goto done; |
|
|
|
} else { |
|
|
|
|
|
|
|
return upb_get_v_uint64_t_full(buf, end, val, status); |
|
|
|
upb_seterr(status, UPB_ERROR, "Unterminated varint"); |
|
|
|
} |
|
|
|
return false; |
|
|
|
|
|
|
|
done: |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Gets a varint -- called when we only need 32 bits of it. Note that a 32-bit
|
|
|
|
|
|
|
|
// varint is not a true wire type.
|
|
|
|
|
|
|
|
INLINE const uint8_t *upb_get_v_uint32_t(const uint8_t *buf, const uint8_t *end, |
|
|
|
|
|
|
|
uint32_t *val, upb_status *status) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
uint64_t val64; |
|
|
|
|
|
|
|
const uint8_t *ret = upb_get_v_uint64_t(buf, end, &val64, status); |
|
|
|
|
|
|
|
*val = (uint32_t)val64; // Discard the high bits.
|
|
|
|
|
|
|
|
return ret; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Gets a fixed-length 32-bit integer (wire type: UPB_WIRE_TYPE_32BIT).
|
|
|
|
/* Decoding/Buffering of individual values ************************************/ |
|
|
|
INLINE const uint8_t *upb_get_f_uint32_t(const uint8_t *buf, const uint8_t *end, |
|
|
|
|
|
|
|
uint32_t *val, upb_status *status) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
const uint8_t *uint32_end = buf + sizeof(uint32_t); |
|
|
|
|
|
|
|
if(uint32_end > end) { |
|
|
|
|
|
|
|
status->code = UPB_STATUS_NEED_MORE_DATA; |
|
|
|
|
|
|
|
return end; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
memcpy(val, buf, sizeof(uint32_t)); |
|
|
|
|
|
|
|
return uint32_end; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Gets a fixed-length 64-bit integer (wire type: UPB_WIRE_TYPE_64BIT).
|
|
|
|
|
|
|
|
INLINE const uint8_t *upb_get_f_uint64_t(const uint8_t *buf, const uint8_t *end, |
|
|
|
|
|
|
|
uint64_t *val, upb_status *status) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
const uint8_t *uint64_end = buf + sizeof(uint64_t); |
|
|
|
|
|
|
|
if(uint64_end > end) { |
|
|
|
|
|
|
|
status->code = UPB_STATUS_NEED_MORE_DATA; |
|
|
|
|
|
|
|
return end; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
memcpy(val, buf, sizeof(uint64_t)); |
|
|
|
|
|
|
|
return uint64_end; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
INLINE const uint8_t *upb_skip_v_uint64_t(const uint8_t *buf, |
|
|
|
|
|
|
|
const uint8_t *end, |
|
|
|
|
|
|
|
upb_status *status) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
const uint8_t *const maxend = buf + 10; |
|
|
|
|
|
|
|
uint8_t last = 0x80; |
|
|
|
|
|
|
|
for(; buf < (uint8_t*)end && (last & 0x80); buf++) |
|
|
|
|
|
|
|
last = *buf; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(buf >= end && buf <= maxend && (last & 0x80)) { |
|
|
|
|
|
|
|
status->code = UPB_STATUS_NEED_MORE_DATA; |
|
|
|
|
|
|
|
buf = end; |
|
|
|
|
|
|
|
} else if(buf > maxend) { |
|
|
|
|
|
|
|
status->code = UPB_ERROR_UNTERMINATED_VARINT; |
|
|
|
|
|
|
|
buf = end; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return buf; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
INLINE const uint8_t *upb_skip_f_uint32_t(const uint8_t *buf, |
|
|
|
|
|
|
|
const uint8_t *end, |
|
|
|
|
|
|
|
upb_status *status) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
const uint8_t *uint32_end = buf + sizeof(uint32_t); |
|
|
|
|
|
|
|
if(uint32_end > end) { |
|
|
|
|
|
|
|
status->code = UPB_STATUS_NEED_MORE_DATA; |
|
|
|
|
|
|
|
return end; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return uint32_end; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
INLINE const uint8_t *upb_skip_f_uint64_t(const uint8_t *buf, |
|
|
|
|
|
|
|
const uint8_t *end, |
|
|
|
|
|
|
|
upb_status *status) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
const uint8_t *uint64_end = buf + sizeof(uint64_t); |
|
|
|
|
|
|
|
if(uint64_end > end) { |
|
|
|
|
|
|
|
status->code = UPB_STATUS_NEED_MORE_DATA; |
|
|
|
|
|
|
|
return end; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return uint64_end; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Functions to read .proto values. *******************************************/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Performs zig-zag decoding, which is used by sint32 and sint64.
|
|
|
|
// Performs zig-zag decoding, which is used by sint32 and sint64.
|
|
|
|
INLINE int32_t upb_zzdec_32(uint32_t n) { return (n >> 1) ^ -(int32_t)(n & 1); } |
|
|
|
INLINE int32_t upb_zzdec_32(uint32_t n) { return (n >> 1) ^ -(int32_t)(n & 1); } |
|
|
|
INLINE int64_t upb_zzdec_64(uint64_t n) { return (n >> 1) ^ -(int64_t)(n & 1); } |
|
|
|
INLINE int64_t upb_zzdec_64(uint64_t n) { return (n >> 1) ^ -(int64_t)(n & 1); } |
|
|
|
|
|
|
|
|
|
|
|
// Parses a tag, places the result in *tag.
|
|
|
|
|
|
|
|
INLINE const uint8_t *decode_tag(const uint8_t *buf, const uint8_t *end, |
|
|
|
|
|
|
|
upb_tag *tag, upb_status *status) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
uint32_t tag_int; |
|
|
|
|
|
|
|
const uint8_t *ret = upb_get_v_uint32_t(buf, end, &tag_int, status); |
|
|
|
|
|
|
|
tag->wire_type = (upb_wire_type_t)(tag_int & 0x07); |
|
|
|
|
|
|
|
tag->field_number = tag_int >> 3; |
|
|
|
|
|
|
|
return ret; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// The decoder keeps a stack with one entry per level of recursion.
|
|
|
|
// The decoder keeps a stack with one entry per level of recursion.
|
|
|
|
// upb_decoder_frame is one frame of that stack.
|
|
|
|
// upb_decoder_frame is one frame of that stack.
|
|
|
|
typedef struct { |
|
|
|
typedef struct { |
|
|
@ -144,6 +56,7 @@ struct upb_decoder { |
|
|
|
// Immutable state of the decoder.
|
|
|
|
// Immutable state of the decoder.
|
|
|
|
upb_src src; |
|
|
|
upb_src src; |
|
|
|
upb_dispatcher dispatcher; |
|
|
|
upb_dispatcher dispatcher; |
|
|
|
|
|
|
|
upb_bytesrc *bytesrc; |
|
|
|
upb_msgdef *toplevel_msgdef; |
|
|
|
upb_msgdef *toplevel_msgdef; |
|
|
|
upb_decoder_frame stack[UPB_MAX_NESTING]; |
|
|
|
upb_decoder_frame stack[UPB_MAX_NESTING]; |
|
|
|
|
|
|
|
|
|
|
@ -158,66 +71,108 @@ struct upb_decoder { |
|
|
|
// Current input buffer.
|
|
|
|
// Current input buffer.
|
|
|
|
upb_string *buf; |
|
|
|
upb_string *buf; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Our current offset *within* buf.
|
|
|
|
|
|
|
|
upb_strlen_t buf_offset; |
|
|
|
|
|
|
|
|
|
|
|
// The offset within the overall stream represented by the *beginning* of buf.
|
|
|
|
// The offset within the overall stream represented by the *beginning* of buf.
|
|
|
|
upb_strlen_t buf_stream_offset; |
|
|
|
upb_strlen_t buf_stream_offset; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// Our current offset *within* buf. Will be negative if we are buffering
|
|
|
|
// Called only from the slow path, this function copies the next "len" bytes
|
|
|
|
// from previous buffers in tmpbuf.
|
|
|
|
// from the stream to "data", adjusting "buf" and "end" appropriately.
|
|
|
|
upb_strlen_t buf_offset; |
|
|
|
INLINE bool upb_getbuf(upb_decoder *d, void *data, size_t len, |
|
|
|
|
|
|
|
uint8_t **buf, uint8_t **end) { |
|
|
|
|
|
|
|
while (len > 0) { |
|
|
|
|
|
|
|
memcpy(data, *buf, *end-*buf); |
|
|
|
|
|
|
|
len -= (*end-*buf); |
|
|
|
|
|
|
|
if (!upb_bytesrc_getstr(d->bytesrc, d->buf, d->status)) return false; |
|
|
|
|
|
|
|
*buf = upb_string_getrobuf(d->buf); |
|
|
|
|
|
|
|
*end = *buf + upb_string_len(d->buf); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Holds any bytes we have from previous buffers. The number of bytes we
|
|
|
|
// We use this path when we don't have UPB_MAX_ENCODED_SIZE contiguous bytes
|
|
|
|
// have encoded here is -buf_offset, if buf_offset<0, 0 otherwise.
|
|
|
|
// available in our current buffer. We don't inline this because we accept
|
|
|
|
uint8_t tmpbuf[UPB_MAX_ENCODED_SIZE]; |
|
|
|
// that it will be slow and we don't want to pay for two copies of it.
|
|
|
|
}; |
|
|
|
static bool upb_decode_varint_slow(upb_decoder *d) { |
|
|
|
|
|
|
|
uint8_t buf[UPB_MAX_ENCODED_SIZE]; |
|
|
|
|
|
|
|
uint8_t *p = buf, *end = buf + sizeof(buf); |
|
|
|
|
|
|
|
for(int bitpos = 0; p < end && getbyte(d, p) && (last & 0x80); p++, bitpos += 7) |
|
|
|
|
|
|
|
*val |= ((uint64_t)((last = *p) & 0x7F)) << bitpos; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(d->status->code == UPB_EOF && (last & 0x80)) { |
|
|
|
|
|
|
|
upb_seterr(status, UPB_ERROR, |
|
|
|
|
|
|
|
"Provided data ended in the middle of a varint.\n"); |
|
|
|
|
|
|
|
} else if(buf == maxend) { |
|
|
|
|
|
|
|
upb_seterr(status, UPB_ERROR, |
|
|
|
|
|
|
|
"Varint was unterminated after 10 bytes.\n"); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// Success.
|
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
upb_flow_t upb_decode_varint(upb_decoder *d, ptrs *p, |
|
|
|
INLINE bool upb_decode_tag(upb_decoder *d, const uint8_t **_buf, |
|
|
|
uint32_t *low, uint32_t *high) { |
|
|
|
const uint8_t **end, upb_tag *tag) { |
|
|
|
if (p->end - p->ptr > UPB_MAX_ENCODED_SIZE) { |
|
|
|
const uint8_t *buf = *_buf, *end = *_end; |
|
|
|
// Fast path; we know we have a complete varint in our existing buffer.
|
|
|
|
uint32_t tag_int; |
|
|
|
*high = 0; |
|
|
|
// Nearly all tag varints will be either 1 byte (1-16) or 2 bytes (17-2048).
|
|
|
|
uint32_t b; |
|
|
|
if (end - buf < 2) goto slow; // unlikely.
|
|
|
|
uint8_t *ptr = p->ptr; |
|
|
|
tag_int = *buf & 0x7f; |
|
|
|
b = *(buf++); *low = (b & 0x7f) ; if(!(b & 0x80)) goto done; |
|
|
|
if ((*(buf++) & 0x80) == 0) goto done; // predictable if fields are in order
|
|
|
|
b = *(buf++); *low |= (b & 0x7f) << 7; if(!(b & 0x80)) goto done; |
|
|
|
tag_int |= (*buf & 0x7f) << 7; |
|
|
|
b = *(buf++); *low |= (b & 0x7f) << 14; if(!(b & 0x80)) goto done; |
|
|
|
if ((*(buf++) & 0x80) != 0) goto slow; // unlikely.
|
|
|
|
b = *(buf++); *low |= (b & 0x7f) << 21; if(!(b & 0x80)) goto done; |
|
|
|
slow: |
|
|
|
b = *(buf++); *low |= (b & 0x7f) << 28; |
|
|
|
if (!upb_decode_varint_slow(d, _buf, _end)) return false; |
|
|
|
*high = (b & 0x7f) >> 3; if(!(b & 0x80)) goto done; |
|
|
|
buf = *_buf; // Trick the next line into not overwriting us.
|
|
|
|
b = *(buf++); *high |= (b & 0x7f) << 4; if(!(b & 0x80)) goto done; |
|
|
|
done: |
|
|
|
b = *(buf++); *high |= (b & 0x7f) << 11; if(!(b & 0x80)) goto done; |
|
|
|
*_buf = buf; |
|
|
|
b = *(buf++); *high |= (b & 0x7f) << 18; if(!(b & 0x80)) goto done; |
|
|
|
tag->wire_type = (upb_wire_type_t)(tag_int & 0x07); |
|
|
|
b = *(buf++); *high |= (b & 0x7f) << 25; if(!(b & 0x80)) goto done; |
|
|
|
tag->field_number = tag_int >> 3; |
|
|
|
|
|
|
|
return true; |
|
|
|
if(bytes_available >= 10) { |
|
|
|
} |
|
|
|
upb_seterr(&d->src.status, UPB_STATUS_ERROR, "Varint was unterminated " |
|
|
|
|
|
|
|
"after 10 bytes, stream offset: %u", upb_decoder_offset(d)); |
|
|
|
INLINE bool upb_decode_varint(upb_decoder *d, ptrs *p, |
|
|
|
return false; |
|
|
|
uint32_t *low, uint32_t *high) { |
|
|
|
} |
|
|
|
if (p->end - p->ptr >= UPB_MAX_VARINT_ENCODED_SIZE) |
|
|
|
|
|
|
|
return upb_decode_varint_fast(d); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
return upb_decode_varint_slow(d); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
done: |
|
|
|
INLINE bool upb_decode_fixed(upb_decoder *d, upb_wire_type_t wt, |
|
|
|
p->ptr = ptr; |
|
|
|
uint8_t **buf, uint8_t **end, upb_value *val) { |
|
|
|
|
|
|
|
static const char table = {0, 8, 0, 0, 0, 4}; |
|
|
|
|
|
|
|
size_t bytes = table[wt]; |
|
|
|
|
|
|
|
if (*end - *buf >= bytes) { |
|
|
|
|
|
|
|
// Common (fast) case.
|
|
|
|
|
|
|
|
memcpy(&val, *buf, bytes); |
|
|
|
|
|
|
|
*buf += bytes; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// Slow path: we may have to combine one or more buffers to get a whole
|
|
|
|
if (!upb_getbuf(d, &val, bytes, buf, end)) return false; |
|
|
|
// varint worth of data.
|
|
|
|
} |
|
|
|
uint8_t buf[UPB_MAX_ENCODED_SIZE]; |
|
|
|
return true; |
|
|
|
uint8_t *p = buf, *end = buf + sizeof(buf); |
|
|
|
} |
|
|
|
for(ing bitpos = 0; p < end && getbyte(d, p) && (last & 0x80); p++, bitpos += 7) |
|
|
|
|
|
|
|
*val |= ((uint64_t)((last = *p) & 0x7F)) << bitpos; |
|
|
|
// "val" initially holds the length of the string, this is replaced by the
|
|
|
|
|
|
|
|
// contents of the string.
|
|
|
|
if(d->status->code == UPB_EOF && (last & 0x80)) { |
|
|
|
INLINE bool upb_decode_string(upb_decoder *d, upb_value *val, upb_string **str) { |
|
|
|
upb_seterr(status, UPB_ERROR, |
|
|
|
upb_string_recycle(str); |
|
|
|
"Provided data ended in the middle of a varint.\n"); |
|
|
|
upb_strlen_t len = upb_valu_getint32(*val); |
|
|
|
} else if(buf == maxend) { |
|
|
|
if (*end - *buf >= len) { |
|
|
|
upb_seterr(status, UPB_ERROR, |
|
|
|
// Common (fast) case.
|
|
|
|
"Varint was unterminated after 10 bytes.\n"); |
|
|
|
upb_string_substr(*str, d->buf, *buf - upb_string_getrobuf(d->buf), len); |
|
|
|
} else { |
|
|
|
*buf += len; |
|
|
|
// Success.
|
|
|
|
} else { |
|
|
|
return; |
|
|
|
if (!upb_getbuf(d, upb_string_getrwbuf(*str, len), len, buf, end)) |
|
|
|
} |
|
|
|
return false; |
|
|
|
ungetbytes(d, buf, p - buf); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* The main decoding loop *****************************************************/ |
|
|
|
|
|
|
|
|
|
|
|
static const void *get_msgend(upb_decoder *d) |
|
|
|
static const void *get_msgend(upb_decoder *d) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if(d->top->end_offset > 0) |
|
|
|
if(d->top->end_offset > 0) |
|
|
@ -238,36 +193,29 @@ INLINE bool upb_check_type(upb_wire_type_t wt, upb_field_type_t ft) { |
|
|
|
return upb_types[ft].expected_wire_type == wt; |
|
|
|
return upb_types[ft].expected_wire_type == wt; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static bool upb_push(upb_decoder *d, const uint8_t *start, |
|
|
|
// Pushes a new stack frame for a submessage with the given len (which will
|
|
|
|
|
|
|
|
// be zero if the submessage is a group).
|
|
|
|
|
|
|
|
static const uint8_t *push(upb_decoder *d, const uint8_t *start, |
|
|
|
|
|
|
|
uint32_t submsg_len, upb_fielddef *f, |
|
|
|
uint32_t submsg_len, upb_fielddef *f, |
|
|
|
upb_status *status) |
|
|
|
upb_status *status) |
|
|
|
{ |
|
|
|
{ |
|
|
|
d->top->field = f; |
|
|
|
d->top->field = f; |
|
|
|
d->top++; |
|
|
|
d->top++; |
|
|
|
if(d->top >= d->limit) { |
|
|
|
if(d->top >= d->limit) { |
|
|
|
upb_seterr(status, UPB_ERROR_MAX_NESTING_EXCEEDED, |
|
|
|
upb_seterr(status, UPB_ERROR, "Nesting too deep."); |
|
|
|
"Nesting exceeded maximum (%d levels)\n", |
|
|
|
return false; |
|
|
|
UPB_MAX_NESTING); |
|
|
|
|
|
|
|
return NULL; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
upb_decoder_frame *frame = d->top; |
|
|
|
d->top->end_offset = d->completed_offset + submsg_len; |
|
|
|
frame->end_offset = d->completed_offset + submsg_len; |
|
|
|
d->top->msgdef = upb_downcast_msgdef(f->def); |
|
|
|
frame->msgdef = upb_downcast_msgdef(f->def); |
|
|
|
*submsg_end = get_msgend(d); |
|
|
|
|
|
|
|
if (!upb_dispatch_startsubmsg(&d->dispatcher, f)) return false; |
|
|
|
upb_dispatch_startsubmsg(&d->dispatcher, f); |
|
|
|
return true; |
|
|
|
return get_msgend(d); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Pops a stack frame, returning a pointer for where the next submsg should
|
|
|
|
static bool upb_pop(upb_decoder *d, const uint8_t *start, upb_status *status) |
|
|
|
// end (or a pointer that is out of range for a group).
|
|
|
|
|
|
|
|
static const void *pop(upb_decoder *d, const uint8_t *start, upb_status *status) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
d->top--; |
|
|
|
d->top--; |
|
|
|
upb_dispatch_endsubmsg(&d->dispatcher); |
|
|
|
upb_dispatch_endsubmsg(&d->dispatcher); |
|
|
|
return get_msgend(d); |
|
|
|
*submsg_end = get_msgend(d); |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void upb_decoder_run(upb_src *src, upb_status *status) { |
|
|
|
void upb_decoder_run(upb_src *src, upb_status *status) { |
|
|
@ -278,11 +226,13 @@ void upb_decoder_run(upb_src *src, upb_status *status) { |
|
|
|
upb_msgdef *msgdef = d->top->msgdef; |
|
|
|
upb_msgdef *msgdef = d->top->msgdef; |
|
|
|
upb_string *str = NULL; |
|
|
|
upb_string *str = NULL; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
upb_dispatch_startmsg(&d->dispatcher); |
|
|
|
|
|
|
|
|
|
|
|
// Main loop: executed once per tag/field pair.
|
|
|
|
// Main loop: executed once per tag/field pair.
|
|
|
|
while(1) { |
|
|
|
while(1) { |
|
|
|
// Parse/handle tag.
|
|
|
|
// Parse/handle tag.
|
|
|
|
upb_tag tag; |
|
|
|
upb_tag tag; |
|
|
|
CHECK(decode_tag(d, &buf, &end, &tag)); |
|
|
|
CHECK(upb_decode_tag(d, &buf, &end, &tag)); |
|
|
|
|
|
|
|
|
|
|
|
// Decode wire data. Hopefully this branch will predict pretty well
|
|
|
|
// Decode wire data. Hopefully this branch will predict pretty well
|
|
|
|
// since most types will read a varint here.
|
|
|
|
// since most types will read a varint here.
|
|
|
@ -290,24 +240,19 @@ void upb_decoder_run(upb_src *src, upb_status *status) { |
|
|
|
switch (tag.wire_type) { |
|
|
|
switch (tag.wire_type) { |
|
|
|
case UPB_WIRE_TYPE_END_GROUP: |
|
|
|
case UPB_WIRE_TYPE_END_GROUP: |
|
|
|
if(!isgroup(submsg_end)) { |
|
|
|
if(!isgroup(submsg_end)) { |
|
|
|
upb_seterr(status, UPB_STATUS_ERROR, "End group seen but current " |
|
|
|
upb_seterr(status, UPB_ERROR, "Unexpected END_GROUP tag."); |
|
|
|
"message is not a group, byte offset: %zd", |
|
|
|
|
|
|
|
d->completed_offset + (completed - start)); |
|
|
|
|
|
|
|
goto err; |
|
|
|
goto err; |
|
|
|
} |
|
|
|
} |
|
|
|
submsg_end = pop(d, start, status, &msgdef); |
|
|
|
CHECK(upb_pop(d, start, status, &msgdef, &submsg_end)); |
|
|
|
completed = buf; |
|
|
|
goto check_msgend; // We have no value to dispatch.
|
|
|
|
goto check_msgend; |
|
|
|
|
|
|
|
case UPB_WIRE_TYPE_VARINT: |
|
|
|
case UPB_WIRE_TYPE_VARINT: |
|
|
|
case UPB_WIRE_TYPE_DELIMITED: |
|
|
|
case UPB_WIRE_TYPE_DELIMITED: |
|
|
|
// For the delimited case we are parsing the length.
|
|
|
|
// For the delimited case we are parsing the length.
|
|
|
|
CHECK(upb_decode_varint(d, &buf, &end, &val)); |
|
|
|
CHECK(upb_decode_varint(d, &buf, &end, &val)); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case UPB_WIRE_TYPE_32BIT: |
|
|
|
case UPB_WIRE_TYPE_32BIT: |
|
|
|
CHECK(upb_decode_32bit(d, &buf, &end, &val)); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case UPB_WIRE_TYPE_64BIT: |
|
|
|
case UPB_WIRE_TYPE_64BIT: |
|
|
|
CHECK(upb_decode_64bit(d, &buf, &end, &val)); |
|
|
|
CHECK(upb_decode_fixed(d, tag.wire_type, &buf, &end, &val)); |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -315,24 +260,31 @@ void upb_decoder_run(upb_src *src, upb_status *status) { |
|
|
|
upb_fielddef *f = upb_msg_itof(msgdef, tag.field_number); |
|
|
|
upb_fielddef *f = upb_msg_itof(msgdef, tag.field_number); |
|
|
|
|
|
|
|
|
|
|
|
if (!f) { |
|
|
|
if (!f) { |
|
|
|
// Unknown field.
|
|
|
|
if (tag.wire_type == UPB_WIRE_TYPE_DELIMITED) |
|
|
|
|
|
|
|
CHECK(upb_decode_string(d, &val, &str)); |
|
|
|
|
|
|
|
CHECK(upb_dispatch_unknownval(d, tag.field_number, val)); |
|
|
|
} else if (!upb_check_type(tag.wire_type, f->type)) { |
|
|
|
} else if (!upb_check_type(tag.wire_type, f->type)) { |
|
|
|
// Field has incorrect type.
|
|
|
|
// TODO: put more details in this error msg.
|
|
|
|
|
|
|
|
upb_seterr(status, UPB_ERROR, "Field had incorrect type."); |
|
|
|
|
|
|
|
goto err; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Perform any further massaging of the data now that we have the fielddef.
|
|
|
|
// Perform any further massaging of the data now that we have the fielddef.
|
|
|
|
// Now we can distinguish strings from submessages, and we know about
|
|
|
|
// Now we can distinguish strings from submessages, and we know about
|
|
|
|
// zig-zag-encoded types.
|
|
|
|
// zig-zag-encoded types.
|
|
|
|
// TODO: handle packed encoding.
|
|
|
|
// TODO: handle packed encoding.
|
|
|
|
|
|
|
|
// TODO: if we were being paranoid, we could check for 32-bit-varint types
|
|
|
|
|
|
|
|
// that the top 32 bits all match the highest bit of the low 32 bits.
|
|
|
|
|
|
|
|
// If this is not true we are losing data. But the main protobuf library
|
|
|
|
|
|
|
|
// doesn't check this, and it would slow us down, so pass for now.
|
|
|
|
switch (f->type) { |
|
|
|
switch (f->type) { |
|
|
|
case UPB_TYPE(MESSAGE): |
|
|
|
case UPB_TYPE(MESSAGE): |
|
|
|
case UPB_TYPE(GROUP): |
|
|
|
case UPB_TYPE(GROUP): |
|
|
|
CHECK(push(d, start, upb_value_getint32(val), f, status, &msgdef)); |
|
|
|
CHECK(upb_push(d, start, upb_value_getint32(val), f, status, &msgdef)); |
|
|
|
goto check_msgend; |
|
|
|
goto check_msgend; // We have no value to dispatch.
|
|
|
|
case UPB_TYPE(STRING): |
|
|
|
case UPB_TYPE(STRING): |
|
|
|
case UPB_TYPE(BYTES): |
|
|
|
case UPB_TYPE(BYTES): |
|
|
|
CHECK(upb_decode_string(d, str, upb_value_getint32(val))); |
|
|
|
CHECK(upb_decode_string(d, &val, &str)); |
|
|
|
upb_value_setstr(&val, str); |
|
|
|
|
|
|
|
break; |
|
|
|
break; |
|
|
|
case UPB_TYPE(SINT32): |
|
|
|
case UPB_TYPE(SINT32): |
|
|
|
upb_value_setint32(&val, upb_zzdec_32(upb_value_getint32(val))); |
|
|
|
upb_value_setint32(&val, upb_zzdec_32(upb_value_getint32(val))); |
|
|
@ -341,26 +293,27 @@ void upb_decoder_run(upb_src *src, upb_status *status) { |
|
|
|
upb_value_setint64(&val, upb_zzdec_64(upb_value_getint64(val))); |
|
|
|
upb_value_setint64(&val, upb_zzdec_64(upb_value_getint64(val))); |
|
|
|
break; |
|
|
|
break; |
|
|
|
default: |
|
|
|
default: |
|
|
|
// Other types need no further processing at this point.
|
|
|
|
break; // Other types need no further processing at this point.
|
|
|
|
} |
|
|
|
} |
|
|
|
CHECK(upb_dispatch_value(d->sink, f, val, status)); |
|
|
|
CHECK(upb_dispatch_value(d->sink, f, val, status)); |
|
|
|
|
|
|
|
|
|
|
|
check_msgend: |
|
|
|
check_msgend: |
|
|
|
while(buf >= submsg_end) { |
|
|
|
while(buf >= submsg_end) { |
|
|
|
if(buf > submsg_end) { |
|
|
|
if(buf > submsg_end) { |
|
|
|
upb_seterr(status, UPB_ERROR, "Expected submsg end offset " |
|
|
|
upb_seterr(status, UPB_ERROR, "Bad submessage end.") |
|
|
|
"did not lie on a tag/value boundary."); |
|
|
|
|
|
|
|
goto err; |
|
|
|
goto err; |
|
|
|
} |
|
|
|
} |
|
|
|
submsg_end = pop(d, start, status, &msgdef); |
|
|
|
CHECK(upb_pop(d, start, status, &msgdef, &submsg_end)); |
|
|
|
} |
|
|
|
} |
|
|
|
completed = buf; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CHECK(upb_dispatch_endmsg(&d->dispatcher)); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
err: |
|
|
|
err: |
|
|
|
read = (char*)completed - (char*)start; |
|
|
|
if (upb_ok(status)) { |
|
|
|
d->completed_offset += read; |
|
|
|
upb_seterr(status, UPB_ERROR, "Callback returned UPB_BREAK"); |
|
|
|
return read; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void upb_decoder_sethandlers(upb_src *src, upb_handlers *handlers) { |
|
|
|
void upb_decoder_sethandlers(upb_src *src, upb_handlers *handlers) { |
|
|
|