Reworked low-level parsing: smaller, safer.

pull/13171/head
Joshua Haberman 16 years ago
parent 5e2691460e
commit 2d21601d47
  1. 2
      Makefile
  2. 18
      tests.c
  3. 3
      upb.h
  4. 175
      upb_parse.c
  5. 40
      upb_parse.h

@ -3,7 +3,7 @@
CC=gcc CC=gcc
CXX=g++ CXX=g++
CFLAGS=-std=c99 CFLAGS=-std=c99
CPPFLAGS=-O3 -Wall -Wextra -pedantic -g -DUPB_UNALIGNED_READS_OK -fomit-frame-pointer CPPFLAGS=-O3 -DNDEBUG -Wall -Wextra -pedantic -g -DUPB_UNALIGNED_READS_OK -fomit-frame-pointer
OBJ=upb_parse.o upb_table.o upb_msg.o upb_context.o descriptor.o OBJ=upb_parse.o upb_table.o upb_msg.o upb_context.o descriptor.o
all: $(OBJ) test_table tests all: $(OBJ) test_table tests
clean: clean:

@ -10,7 +10,7 @@ void test_get_v_uint64_t()
uint8_t zero[] = {0x00}; uint8_t zero[] = {0x00};
void *zero_buf = zero; void *zero_buf = zero;
uint64_t zero_val = 0; uint64_t zero_val = 0;
status = get_v_uint64_t(&zero_buf, &zero_val); status = get_v_uint64_t(&zero_buf, sizeof(zero), &zero_val);
assert(status == UPB_STATUS_OK); assert(status == UPB_STATUS_OK);
assert(zero_val == 0); assert(zero_val == 0);
assert(zero_buf == zero + sizeof(zero)); assert(zero_buf == zero + sizeof(zero));
@ -18,29 +18,37 @@ void test_get_v_uint64_t()
uint8_t one[] = {0x01}; uint8_t one[] = {0x01};
void *one_buf = one; void *one_buf = one;
uint64_t one_val = 0; uint64_t one_val = 0;
status = get_v_uint64_t(&one_buf, &one_val); status = get_v_uint64_t(&one_buf, sizeof(one), &one_val);
assert(status == UPB_STATUS_OK); assert(status == UPB_STATUS_OK);
assert(one_val == 1); assert(one_val == 1);
assert(one_buf == one + sizeof(one));
uint8_t twobyte[] = {0xAC, 0x02}; uint8_t twobyte[] = {0xAC, 0x02};
void *twobyte_buf = twobyte; void *twobyte_buf = twobyte;
uint64_t twobyte_val = 0; uint64_t twobyte_val = 0;
status = get_v_uint64_t(&twobyte_buf, &twobyte_val); status = get_v_uint64_t(&twobyte_buf, sizeof(twobyte), &twobyte_val);
assert(status == UPB_STATUS_OK); assert(status == UPB_STATUS_OK);
assert(twobyte_val == 300); assert(twobyte_val == 300);
assert(twobyte_buf == twobyte + sizeof(twobyte));
uint8_t tenbyte[] = {0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x7F}; uint8_t tenbyte[] = {0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x7F};
void *tenbyte_buf = tenbyte; void *tenbyte_buf = tenbyte;
uint64_t tenbyte_val = 0; uint64_t tenbyte_val = 0;
status = get_v_uint64_t(&tenbyte_buf, &tenbyte_val); status = get_v_uint64_t(&tenbyte_buf, sizeof(tenbyte), &tenbyte_val);
assert(status == UPB_STATUS_OK); assert(status == UPB_STATUS_OK);
assert(tenbyte_val == 0x89101c305080c101); assert(tenbyte_val == 0x89101c305080c101);
assert(tenbyte_buf == tenbyte + sizeof(tenbyte));
uint8_t elevenbyte[] = {0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01}; uint8_t elevenbyte[] = {0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01};
void *elevenbyte_buf = elevenbyte; void *elevenbyte_buf = elevenbyte;
uint64_t elevenbyte_val = 0; uint64_t elevenbyte_val = 0;
status = get_v_uint64_t(&elevenbyte_buf, &elevenbyte_val); status = get_v_uint64_t(&elevenbyte_buf, sizeof(elevenbyte), &elevenbyte_val);
assert(status == UPB_ERROR_UNTERMINATED_VARINT); assert(status == UPB_ERROR_UNTERMINATED_VARINT);
status = get_v_uint64_t(&elevenbyte_buf, sizeof(elevenbyte)-1, &elevenbyte_val);
/* Byte 10 is 0x80, so we know it's unterminated. */
assert(status == UPB_ERROR_UNTERMINATED_VARINT);
status = get_v_uint64_t(&elevenbyte_buf, sizeof(elevenbyte)-2, &elevenbyte_val);
assert(status == UPB_STATUS_NEED_MORE_DATA);
} }
int main() int main()

@ -110,6 +110,9 @@ union upb_symbol_ref {
typedef enum upb_status { typedef enum upb_status {
UPB_STATUS_OK = 0, UPB_STATUS_OK = 0,
// The input byte stream ended in the middle of a record.
UPB_STATUS_NEED_MORE_DATA = 1,
// A varint did not terminate before hitting 64 bits. // A varint did not terminate before hitting 64 bits.
UPB_ERROR_UNTERMINATED_VARINT = -1, UPB_ERROR_UNTERMINATED_VARINT = -1,

@ -30,88 +30,93 @@
* To avoid branches, none of these do bounds checking. So we force clients * To avoid branches, none of these do bounds checking. So we force clients
* to overallocate their buffers by >=9 bytes. */ * to overallocate their buffers by >=9 bytes. */
static upb_status_t get_v_uint64_t(void *restrict *buf, static size_t min(size_t a, size_t b) { return a < b ? a : b; }
static upb_status_t get_v_uint64_t(void *restrict *buf, size_t len,
uint64_t *restrict val) uint64_t *restrict val)
{ {
uint8_t *ptr = *buf, b; uint32_t bitpos, bytes = min(len, 10);
uint32_t part0 = 0, part1 = 0, part2 = 0; uint8_t *b = *buf;
uint8_t *end = b + bytes;
/* From the original proto2 implementation. */ uint8_t last = 0x80;
b = *(ptr++); part0 = (b & 0x7F) ; if (!(b & 0x80)) goto done; *val = 0;
b = *(ptr++); part0 |= (b & 0x7F) << 7; if (!(b & 0x80)) goto done; for(bitpos = 0; b < end && (last & 0x80); b++, bitpos += 7)
b = *(ptr++); part0 |= (b & 0x7F) << 14; if (!(b & 0x80)) goto done; *val |= ((uint64_t)((last = *b) & 0x7F)) << bitpos;
b = *(ptr++); part0 |= (b & 0x7F) << 21; if (!(b & 0x80)) goto done;
b = *(ptr++); part1 = (b & 0x7F) ; if (!(b & 0x80)) goto done; if(unlikely(last & 0x80)) {
b = *(ptr++); part1 |= (b & 0x7F) << 7; if (!(b & 0x80)) goto done; return bytes < 10 ? UPB_STATUS_NEED_MORE_DATA : UPB_ERROR_UNTERMINATED_VARINT;
b = *(ptr++); part1 |= (b & 0x7F) << 14; if (!(b & 0x80)) goto done; } else {
b = *(ptr++); part1 |= (b & 0x7F) << 21; if (!(b & 0x80)) goto done; *buf = b;
b = *(ptr++); part2 = (b & 0x7F) ; if (!(b & 0x80)) goto done; return UPB_STATUS_OK;
b = *(ptr++); part2 |= (b & 0x7F) << 7; if (!(b & 0x80)) goto done; }
return UPB_ERROR_UNTERMINATED_VARINT;
done:
*buf = ptr;
*val = (uint64_t)part0 | ((uint64_t)part1 << 28) | ((uint64_t)part2 << 56);
return UPB_STATUS_OK;
} }
static upb_status_t skip_v_uint64_t(void **buf) static upb_status_t skip_v_uint64_t(void **buf, size_t len)
{ {
uint32_t bytes = min(len, 10);
uint8_t *b = *buf; uint8_t *b = *buf;
for(int i = 0; i < 10; i++, b++) { uint8_t *end = b + bytes;
if(!(*b & 0x80)) { uint8_t last = 0x80;
*buf = b; for(; b < end && (last & 0x80); b++)
return UPB_STATUS_OK; last = *b;
}
if(unlikely(b == end)) {
return bytes < 10 ? UPB_STATUS_NEED_MORE_DATA : UPB_ERROR_UNTERMINATED_VARINT;
} else {
*buf = b;
return UPB_STATUS_OK;
} }
return UPB_ERROR_UNTERMINATED_VARINT;
} }
static upb_status_t get_v_uint32_t(void *restrict *buf, static upb_status_t get_v_uint32_t(void *restrict *buf, size_t len,
uint32_t *restrict val) uint32_t *restrict val)
{ {
uint8_t *ptr = *buf, b; uint32_t bitpos, bytes = min(len, 5);
uint32_t result; uint8_t *b = *buf;
uint8_t *end = b + bytes;
/* From the original proto2 implementation. */ uint8_t last = 0x80;
b = *(ptr++); result = (b & 0x7F) ; if (!(b & 0x80)) goto done; *val = 0;
b = *(ptr++); result |= (b & 0x7F) << 7; if (!(b & 0x80)) goto done; for(bitpos = 0; b < end && (last & 0x80); b++, bitpos += 7)
b = *(ptr++); result |= (b & 0x7F) << 14; if (!(b & 0x80)) goto done; *val |= ((uint32_t)((last = *b) & 0x7F)) << bitpos;
b = *(ptr++); result |= (b & 0x7F) << 21; if (!(b & 0x80)) goto done;
b = *(ptr++); result = (b & 0x7F) << 28; if (!(b & 0x80)) goto done; if(unlikely(b == end)) {
return UPB_ERROR_UNTERMINATED_VARINT; return bytes < 5 ? UPB_STATUS_NEED_MORE_DATA : UPB_ERROR_UNTERMINATED_VARINT;
} else {
done: *buf = b;
*buf = ptr; return UPB_STATUS_OK;
*val = result; }
return UPB_STATUS_OK;
} }
#define SHL(val, bits) ((uint32_t)val << bits) #define SHL(val, bits) ((uint32_t)val << bits)
static upb_status_t get_f_uint32_t(void *restrict *buf, static upb_status_t get_f_uint32_t(void *restrict *buf, size_t len,
uint32_t *restrict val) uint32_t *restrict val)
{ {
const uint8_t size = sizeof(uint32_t);
if(unlikely(len < size)) return UPB_STATUS_NEED_MORE_DATA;
uint8_t *b = *buf; uint8_t *b = *buf;
#if UPB_UNALIGNED_READS_OK #if UPB_UNALIGNED_READS_OK
*val = *(uint32_t*)b; *val = *(uint32_t*)b;
#else #else
*val = SHL(b[0], 0) | SHL(b[1], 8) | SHL(b[2], 16) | SHL(b[3], 24); *val = SHL(b[0], 0) | SHL(b[1], 8) | SHL(b[2], 16) | SHL(b[3], 24);
#endif #endif
b += sizeof(uint32_t); b += size;
*buf = b; *buf = b;
return UPB_STATUS_OK; return UPB_STATUS_OK;
} }
#undef SHL #undef SHL
static upb_status_t skip_f_uint32_t(void **buf) static upb_status_t skip_f_uint32_t(void **buf, size_t len)
{ {
*buf = (char*)*buf + sizeof(uint32_t); const uint8_t size = sizeof(uint32_t);
if(unlikely(len < size)) return UPB_STATUS_NEED_MORE_DATA;
*buf = (char*)*buf + size;
return UPB_STATUS_OK; return UPB_STATUS_OK;
} }
static upb_status_t get_f_uint64_t(void *restrict *buf, static upb_status_t get_f_uint64_t(void *restrict *buf, size_t len,
uint64_t *restrict val) uint64_t *restrict val)
{ {
if(unlikely(len < sizeof(uint64_t))) return UPB_STATUS_NEED_MORE_DATA;
#if UPB_UNALIGNED_READS_OK #if UPB_UNALIGNED_READS_OK
*val = *(uint64_t*)*buf; *val = *(uint64_t*)*buf;
*buf = (char*)*buf + sizeof(uint64_t); *buf = (char*)*buf + sizeof(uint64_t);
@ -124,9 +129,11 @@ static upb_status_t get_f_uint64_t(void *restrict *buf,
return UPB_STATUS_OK; return UPB_STATUS_OK;
} }
static upb_status_t skip_f_uint64_t(void **buf) static upb_status_t skip_f_uint64_t(void **buf, size_t len)
{ {
*buf = (char*)*buf + sizeof(uint64_t); const uint8_t size = sizeof(uint64_t);
if(unlikely(len < size)) return UPB_STATUS_NEED_MORE_DATA;
*buf = (char*)*buf + size;
return UPB_STATUS_OK; return UPB_STATUS_OK;
} }
@ -140,10 +147,10 @@ static int64_t zz_decode_64(uint64_t n) { return (n >> 1) ^ -(int64_t)(n & 1); }
static void wvtov_ ## type(wire_t s, val_t *d) static void wvtov_ ## type(wire_t s, val_t *d)
#define GET(type, v_or_f, wire_t, val_t, member_name) \ #define GET(type, v_or_f, wire_t, val_t, member_name) \
static upb_status_t get_ ## type(void **buf, union upb_value *d) { \ static upb_status_t get_ ## type(void **buf, size_t len, val_t *d) { \
wire_t tmp; \ wire_t tmp; \
CHECK(get_ ## v_or_f ## _ ## wire_t(buf, &tmp)); \ CHECK(get_ ## v_or_f ## _ ## wire_t(buf, len, &tmp)); \
wvtov_ ## type(tmp, &d->member_name); \ wvtov_ ## type(tmp, d); \
return UPB_STATUS_OK; \ return UPB_STATUS_OK; \
} }
@ -194,27 +201,27 @@ struct upb_type_info upb_type_info[] = {
[GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SINT64] = {alignof(int64_t), sizeof(int64_t), UPB_WIRE_TYPE_VARINT}, [GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SINT64] = {alignof(int64_t), sizeof(int64_t), UPB_WIRE_TYPE_VARINT},
}; };
upb_status_t upb_parse_tag(void **buf, struct upb_tag *tag) upb_status_t upb_parse_tag(void **buf, size_t len, struct upb_tag *tag)
{ {
uint32_t tag_int; uint32_t tag_int;
CHECK(get_v_uint32_t(buf, &tag_int)); CHECK(get_v_uint32_t(buf, len, &tag_int));
tag->wire_type = (upb_wire_type_t)(tag_int & 0x07); tag->wire_type = (upb_wire_type_t)(tag_int & 0x07);
tag->field_number = tag_int >> 3; tag->field_number = tag_int >> 3;
return UPB_STATUS_OK; return UPB_STATUS_OK;
} }
upb_status_t upb_parse_wire_value(void *buf, size_t *offset, upb_status_t upb_parse_wire_value(void *buf, size_t len, size_t *offset,
upb_wire_type_t wt, upb_wire_type_t wt,
union upb_wire_value *wv) union upb_wire_value *wv)
{ {
#define READ(expr) CHECK(expr); *offset += ((char*)b-(char*)buf) #define READ(expr) CHECK(expr); *offset += ((char*)b-(char*)buf)
void *b = buf; void *b = buf;
switch(wt) { switch(wt) {
case UPB_WIRE_TYPE_VARINT: READ(get_v_uint64_t(&b, &wv->varint)); break; case UPB_WIRE_TYPE_VARINT: READ(get_v_uint64_t(&b, len, &wv->varint)); break;
case UPB_WIRE_TYPE_64BIT: READ(get_f_uint64_t(&b, &wv->_64bit)); break; case UPB_WIRE_TYPE_64BIT: READ(get_f_uint64_t(&b, len, &wv->_64bit)); break;
case UPB_WIRE_TYPE_32BIT: READ(get_f_uint32_t(&b, &wv->_32bit)); break; case UPB_WIRE_TYPE_32BIT: READ(get_f_uint32_t(&b, len, &wv->_32bit)); break;
case UPB_WIRE_TYPE_DELIMITED: case UPB_WIRE_TYPE_DELIMITED:
READ(get_v_uint32_t(&b, &wv->_32bit)); READ(get_v_uint32_t(&b, len, &wv->_32bit));
size_t new_offset = *offset + wv->_32bit; size_t new_offset = *offset + wv->_32bit;
if (new_offset < *offset) return UPB_ERROR_OVERFLOW; if (new_offset < *offset) return UPB_ERROR_OVERFLOW;
*offset = new_offset; *offset = new_offset;
@ -225,19 +232,19 @@ upb_status_t upb_parse_wire_value(void *buf, size_t *offset,
return UPB_STATUS_OK; return UPB_STATUS_OK;
} }
upb_status_t upb_skip_wire_value(void *buf, size_t *offset, upb_status_t upb_skip_wire_value(void *buf, size_t len, size_t *offset,
upb_wire_type_t wt) upb_wire_type_t wt)
{ {
void *b = buf; void *b = buf;
switch(wt) { switch(wt) {
case UPB_WIRE_TYPE_VARINT: READ(skip_v_uint64_t(&b)); break; case UPB_WIRE_TYPE_VARINT: READ(skip_v_uint64_t(&b, len)); break;
case UPB_WIRE_TYPE_64BIT: READ(skip_f_uint64_t(&b)); break; case UPB_WIRE_TYPE_64BIT: READ(skip_f_uint64_t(&b, len)); break;
case UPB_WIRE_TYPE_32BIT: READ(skip_f_uint32_t(&b)); break; case UPB_WIRE_TYPE_32BIT: READ(skip_f_uint32_t(&b, len)); break;
case UPB_WIRE_TYPE_DELIMITED: { case UPB_WIRE_TYPE_DELIMITED: {
/* Have to get (not skip) the length to skip the bytes. */ /* Have to get (not skip) the length to skip the bytes. */
uint32_t len; uint32_t delim_len;
READ(get_v_uint32_t(&b, &len)); READ(get_v_uint32_t(&b, len, &delim_len));
size_t new_offset = *offset + len; size_t new_offset = *offset + delim_len;
if (new_offset < *offset) return UPB_ERROR_OVERFLOW; if (new_offset < *offset) return UPB_ERROR_OVERFLOW;
*offset = new_offset; *offset = new_offset;
break; break;
@ -249,19 +256,31 @@ upb_status_t upb_skip_wire_value(void *buf, size_t *offset,
#undef READ #undef READ
} }
upb_status_t upb_parse_value(void **b, upb_field_type_t ft, upb_status_t upb_parse_value(void **b, size_t len, upb_field_type_t ft,
union upb_value *v) union upb_value *v)
{ {
#define CASE(t) \ #define CASE(t, member_name) \
case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_ ## t: return get_ ## t(b, v); case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_ ## t: \
return get_ ## t(b, len, &v->member_name);
switch(ft) { switch(ft) {
CASE(DOUBLE) CASE(FLOAT) CASE(INT64) CASE(UINT64) CASE(INT32) CASE(FIXED64) CASE(DOUBLE, _double)
CASE(FIXED32) CASE(BOOL) CASE(UINT32) CASE(ENUM) CASE(SFIXED32) CASE(FLOAT, _float)
CASE(SFIXED64) CASE(SINT32) CASE(SINT64) CASE(INT32, int32)
CASE(INT64, int64)
CASE(UINT32, uint32)
CASE(UINT64, uint64)
CASE(SINT32, int32)
CASE(SINT64, int64)
CASE(FIXED32, uint32)
CASE(FIXED64, uint64)
CASE(SFIXED32, int32)
CASE(SFIXED64, int64)
CASE(BOOL, _bool)
CASE(ENUM, int32)
case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_BYTES: case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_BYTES:
case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_STRING: case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_STRING:
case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_MESSAGE: case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_MESSAGE:
return get_UINT32(b, v); return get_INT32(b, len, &v->int32);
default: return 0; /* Including GROUP -- groups have no value. */ default: return 0; /* Including GROUP -- groups have no value. */
} }
#undef CASE #undef CASE
@ -301,6 +320,7 @@ static upb_status_t push_stack_frame(struct upb_parse_state *s, size_t end,
return UPB_STATUS_OK; return UPB_STATUS_OK;
} }
#if 0
upb_status_t upb_parse(struct upb_parse_state *s, void *buf, size_t len, upb_status_t upb_parse(struct upb_parse_state *s, void *buf, size_t len,
size_t *read) size_t *read)
{ {
@ -314,7 +334,7 @@ upb_status_t upb_parse(struct upb_parse_state *s, void *buf, size_t len,
struct upb_tag tag; struct upb_tag tag;
void *b = buf; void *b = buf;
CHECK(upb_parse_tag(&b, &tag)); CHECK(upb_parse_tag(&b, len, &tag));
int tag_bytes = ((char*)b - (char*)buf); int tag_bytes = ((char*)b - (char*)buf);
s->offset += tag_bytes; s->offset += tag_bytes;
buf = b; buf = b;
@ -325,7 +345,7 @@ upb_status_t upb_parse(struct upb_parse_state *s, void *buf, size_t len,
} }
void *user_field_desc; void *user_field_desc;
upb_field_type_t ft = s->tag_cb(s, &tag, &user_field_desc); //upb_field_type_t ft = s->tag_cb(s, &tag, &user_field_desc);
if(ft == 0) { if(ft == 0) {
CHECK(upb_skip_wire_value(b, &s->offset, tag.wire_type)); CHECK(upb_skip_wire_value(b, &s->offset, tag.wire_type));
} else if(ft == GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_GROUP) { } else if(ft == GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_GROUP) {
@ -356,3 +376,4 @@ upb_status_t upb_parse(struct upb_parse_state *s, void *buf, size_t len,
*read = s->offset - start_offset; *read = s->offset - start_offset;
return UPB_STATUS_OK; return UPB_STATUS_OK;
} }
#endif

@ -29,31 +29,14 @@ void upb_parse_state_init(struct upb_parse_state *state, size_t udata_size);
void upb_parse_state_free(struct upb_parse_state *state); void upb_parse_state_free(struct upb_parse_state *state);
/* The callback that is called immediately after a tag has been parsed. The /* The callback that is called immediately after a tag has been parsed. The
* client uses it to decide if it wants to process this value or skip it. If * client must either advance the stream beyond the corresponding value or
* it wants to process it, it must determine its specific .proto type (at this * return an error to indicate that the stream should rewind to before the
* point we only know its wire type) and verify that it matches the wire type. * tag.
* The client will then return the .proto type. To skip the value, the client
* should return 0 (which is not a valid .proto type).
* *
* The client can set user_field_desc to a record describing this field -- this * The client advances the stream beyond the corresponding value by either
* pointer will be supplied to the value callback (for simple values) or the * parsing the value or skipping it. */
* submsg_start callback (for submessages). typedef upb_field_type_t (*upb_tag_cb)(void **buf, struct upb_parse_state *s,
* struct upb_tag *tag);
* TODO: there needs to be a way to skip a delimited field while still knowing
* its offset and length. That could be through this callback or it could be a
* separate callback. */
typedef upb_field_type_t (*upb_tag_cb)(struct upb_parse_state *s,
struct upb_tag *tag,
void **user_field_desc);
/* The callback that is called for individual values. This callback is only
* called when the previously invoked tag_cb has returned nonzero. It receives
* the parsed and converted value as well as the user_field_desc that was set
* by the tag_cb. Note that this function can be called several times in a row
* (ie. with no intervening tag_cb) in the case of packed arrays. For string
* data (bytes and string) str points to the beginning of the string. */
typedef void (*upb_value_cb)(struct upb_parse_state *s, union upb_value *v,
void *str, void *user_field_desc);
/* Callbacks that are called when a submessage begins and ends, respectively. /* Callbacks that are called when a submessage begins and ends, respectively.
* Both are called with the submessage's stack frame at the top of the stack. */ * Both are called with the submessage's stack frame at the top of the stack. */
@ -77,7 +60,6 @@ struct upb_parse_state {
size_t packed_end_offset; /* 0 if not in a packed array. */ size_t packed_end_offset; /* 0 if not in a packed array. */
upb_field_type_t packed_type; upb_field_type_t packed_type;
upb_tag_cb tag_cb; upb_tag_cb tag_cb;
upb_value_cb value_cb;
upb_submsg_start_cb submsg_start_cb; upb_submsg_start_cb submsg_start_cb;
upb_submsg_end_cb submsg_end_cb; upb_submsg_end_cb submsg_end_cb;
}; };
@ -94,7 +76,7 @@ upb_status_t upb_parse(struct upb_parse_state *s, void *buf, size_t len,
/* Parses a single tag from the character data starting at buf, and updates /* Parses a single tag from the character data starting at buf, and updates
* buf to point one past the bytes that were consumed. buf will be incremented * buf to point one past the bytes that were consumed. buf will be incremented
* by at most ten bytes. */ * by at most ten bytes. */
upb_status_t upb_parse_tag(void **buf, struct upb_tag *tag); upb_status_t upb_parse_tag(void **buf, size_t len, struct upb_tag *tag);
extern upb_wire_type_t upb_expected_wire_types[]; extern upb_wire_type_t upb_expected_wire_types[];
/* Returns true if wt is the correct on-the-wire type for ft. */ /* Returns true if wt is the correct on-the-wire type for ft. */
@ -106,19 +88,19 @@ INLINE bool upb_check_type(upb_wire_type_t wt, upb_field_type_t ft) {
* caller must have previously checked that the wire type is appropriate for * caller must have previously checked that the wire type is appropriate for
* this field type. For delimited data, buf is advanced to the beginning of * this field type. For delimited data, buf is advanced to the beginning of
* the delimited data, not the end. */ * the delimited data, not the end. */
upb_status_t upb_parse_value(void **buf, upb_field_type_t ft, upb_status_t upb_parse_value(void **buf, size_t len, upb_field_type_t ft,
union upb_value *value); union upb_value *value);
/* Parses a wire value with the given type (which must have been obtained from /* Parses a wire value with the given type (which must have been obtained from
* a tag that was just parsed) and adds the number of bytes that were consumed * a tag that was just parsed) and adds the number of bytes that were consumed
* to *offset. For delimited types, offset is advanced past the delimited * to *offset. For delimited types, offset is advanced past the delimited
* data. */ * data. */
upb_status_t upb_parse_wire_value(void *buf, size_t *offset, upb_status_t upb_parse_wire_value(void *buf, size_t len, size_t *offset,
upb_wire_type_t wt, upb_wire_type_t wt,
union upb_wire_value *wv); union upb_wire_value *wv);
/* Like the above, but discards the wire value instead of saving it. */ /* Like the above, but discards the wire value instead of saving it. */
upb_status_t upb_skip_wire_value(void *buf, size_t *offset, upb_status_t upb_skip_wire_value(void *buf, size_t len, size_t *offset,
upb_wire_type_t wt); upb_wire_type_t wt);
#ifdef __cplusplus #ifdef __cplusplus

Loading…
Cancel
Save