Protocol Buffers - Google's data interchange format (grpc依赖)
https://developers.google.com/protocol-buffers/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
361 lines
12 KiB
361 lines
12 KiB
16 years ago
|
/*
|
||
16 years ago
|
* upb - a minimalist implementation of protocol buffers.
|
||
16 years ago
|
*
|
||
16 years ago
|
* Copyright (c) 2008-2009 Joshua Haberman. See LICENSE for details.
|
||
16 years ago
|
*/
|
||
|
|
||
16 years ago
|
#include "upb_parse.h"
|
||
|
|
||
16 years ago
|
#include <assert.h>
|
||
16 years ago
|
#include <string.h>
|
||
|
|
||
|
/* Branch prediction hints for GCC. */
|
||
|
#ifdef __GNUC__
|
||
|
#define likely(x) __builtin_expect((x),1)
|
||
|
#define unlikely(x) __builtin_expect((x),0)
|
||
|
#else
|
||
16 years ago
|
#define likely(x) (x)
|
||
|
#define unlikely(x) (x)
|
||
16 years ago
|
#endif
|
||
|
|
||
16 years ago
|
#define CHECK(func) do { \
|
||
16 years ago
|
upb_status_t status = func; \
|
||
|
if(status != UPB_STATUS_OK) return status; \
|
||
16 years ago
|
} while (0)
|
||
|
|
||
16 years ago
|
/* Lowest-level functions -- these read integers from the input buffer.
|
||
|
* To avoid branches, none of these do bounds checking. So we force clients
|
||
|
* to overallocate their buffers by >=9 bytes. */
|
||
16 years ago
|
|
||
16 years ago
|
static upb_status_t get_v_uint64_t(uint8_t *restrict *buf,
|
||
|
uint64_t *restrict val)
|
||
16 years ago
|
{
|
||
16 years ago
|
uint8_t *ptr = *buf, b;
|
||
16 years ago
|
uint32_t part0 = 0, part1 = 0, part2 = 0;
|
||
|
|
||
16 years ago
|
/* From the original proto2 implementation. */
|
||
16 years ago
|
b = *(ptr++); part0 = (b & 0x7F) ; if (!(b & 0x80)) goto done;
|
||
|
b = *(ptr++); part0 |= (b & 0x7F) << 7; if (!(b & 0x80)) goto done;
|
||
|
b = *(ptr++); part0 |= (b & 0x7F) << 14; if (!(b & 0x80)) goto done;
|
||
|
b = *(ptr++); part0 |= (b & 0x7F) << 21; if (!(b & 0x80)) goto done;
|
||
|
b = *(ptr++); part1 = (b & 0x7F) ; if (!(b & 0x80)) goto done;
|
||
|
b = *(ptr++); part1 |= (b & 0x7F) << 7; if (!(b & 0x80)) goto done;
|
||
|
b = *(ptr++); part1 |= (b & 0x7F) << 14; if (!(b & 0x80)) goto done;
|
||
|
b = *(ptr++); part1 |= (b & 0x7F) << 21; if (!(b & 0x80)) goto done;
|
||
|
b = *(ptr++); part2 = (b & 0x7F) ; if (!(b & 0x80)) goto done;
|
||
|
b = *(ptr++); part2 |= (b & 0x7F) << 7; if (!(b & 0x80)) goto done;
|
||
16 years ago
|
return UPB_ERROR_UNTERMINATED_VARINT;
|
||
16 years ago
|
|
||
|
done:
|
||
16 years ago
|
*buf = ptr;
|
||
16 years ago
|
*val = (uint64_t)part0 | ((uint64_t)part1 << 28) | ((uint64_t)part2 << 56);
|
||
16 years ago
|
return UPB_STATUS_OK;
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
static upb_status_t skip_v_uint64_t(uint8_t **buf)
|
||
16 years ago
|
{
|
||
16 years ago
|
uint8_t *ptr = *buf, b;
|
||
16 years ago
|
b = *(ptr++); if (!(b & 0x80)) goto done;
|
||
|
b = *(ptr++); if (!(b & 0x80)) goto done;
|
||
|
b = *(ptr++); if (!(b & 0x80)) goto done;
|
||
|
b = *(ptr++); if (!(b & 0x80)) goto done;
|
||
|
b = *(ptr++); if (!(b & 0x80)) goto done;
|
||
|
b = *(ptr++); if (!(b & 0x80)) goto done;
|
||
|
b = *(ptr++); if (!(b & 0x80)) goto done;
|
||
|
b = *(ptr++); if (!(b & 0x80)) goto done;
|
||
|
b = *(ptr++); if (!(b & 0x80)) goto done;
|
||
|
b = *(ptr++); if (!(b & 0x80)) goto done;
|
||
16 years ago
|
return UPB_ERROR_UNTERMINATED_VARINT;
|
||
16 years ago
|
|
||
|
done:
|
||
16 years ago
|
*buf = (uint8_t*)ptr;
|
||
16 years ago
|
return UPB_STATUS_OK;
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
static upb_status_t get_v_uint32_t(uint8_t *restrict *buf,
|
||
|
uint32_t *restrict val)
|
||
16 years ago
|
{
|
||
16 years ago
|
uint8_t *ptr = *buf, b;
|
||
16 years ago
|
uint32_t result;
|
||
|
|
||
16 years ago
|
/* From the original proto2 implementation. */
|
||
16 years ago
|
b = *(ptr++); result = (b & 0x7F) ; if (!(b & 0x80)) goto done;
|
||
|
b = *(ptr++); result |= (b & 0x7F) << 7; if (!(b & 0x80)) goto done;
|
||
|
b = *(ptr++); result |= (b & 0x7F) << 14; if (!(b & 0x80)) goto done;
|
||
|
b = *(ptr++); result |= (b & 0x7F) << 21; if (!(b & 0x80)) goto done;
|
||
|
b = *(ptr++); result = (b & 0x7F) << 28; if (!(b & 0x80)) goto done;
|
||
16 years ago
|
return UPB_ERROR_UNTERMINATED_VARINT;
|
||
16 years ago
|
|
||
|
done:
|
||
16 years ago
|
*buf = ptr;
|
||
16 years ago
|
*val = result;
|
||
16 years ago
|
return UPB_STATUS_OK;
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
static upb_status_t get_f_uint32_t(uint8_t *restrict *buf,
|
||
|
uint32_t *restrict val)
|
||
16 years ago
|
{
|
||
16 years ago
|
uint8_t *b = *buf;
|
||
16 years ago
|
#define SHL(val, bits) ((uint32_t)val << bits)
|
||
|
*val = SHL(b[0], 0) | SHL(b[1], 8) | SHL(b[2], 16) | SHL(b[3], 24);
|
||
|
#undef SHL
|
||
|
*buf += sizeof(uint32_t);
|
||
16 years ago
|
return UPB_STATUS_OK;
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
static upb_status_t skip_f_uint32_t(uint8_t **buf)
|
||
16 years ago
|
{
|
||
|
*buf += sizeof(uint32_t);
|
||
16 years ago
|
return UPB_STATUS_OK;
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
static upb_status_t get_f_uint64_t(uint8_t *restrict *buf,
|
||
|
uint64_t *restrict val)
|
||
16 years ago
|
{
|
||
16 years ago
|
uint8_t *b = *buf;
|
||
16 years ago
|
/* TODO: is this worth 32/64 specializing? */
|
||
|
#define SHL(val, bits) ((uint64_t)val << bits)
|
||
|
*val = SHL(b[0], 0) | SHL(b[1], 8) | SHL(b[2], 16) | SHL(b[3], 24) |
|
||
|
SHL(b[4], 32) | SHL(b[5], 40) | SHL(b[6], 48) | SHL(b[7], 56);
|
||
|
#undef SHL
|
||
|
*buf += sizeof(uint64_t);
|
||
16 years ago
|
return UPB_STATUS_OK;
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
static upb_status_t skip_f_uint64_t(uint8_t **buf)
|
||
16 years ago
|
{
|
||
|
*buf += sizeof(uint64_t);
|
||
16 years ago
|
return UPB_STATUS_OK;
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
static int32_t zz_decode_32(uint32_t n) { return (n >> 1) ^ -(int32_t)(n & 1); }
|
||
|
static int64_t zz_decode_64(uint64_t n) { return (n >> 1) ^ -(int64_t)(n & 1); }
|
||
16 years ago
|
|
||
16 years ago
|
/* Functions for reading wire values and converting them to values. These
|
||
|
* are generated with macros because they follow a higly consistent pattern. */
|
||
|
|
||
16 years ago
|
#define WVTOV(type, wire_t, val_t) \
|
||
16 years ago
|
static void wvtov_ ## type(wire_t s, val_t *d)
|
||
16 years ago
|
|
||
|
#define GET(type, v_or_f, wire_t, val_t, member_name) \
|
||
16 years ago
|
static upb_status_t get_ ## type(struct upb_parse_state *s, \
|
||
16 years ago
|
uint8_t *buf, \
|
||
16 years ago
|
struct upb_tagged_value *d) { \
|
||
16 years ago
|
wire_t tmp; \
|
||
16 years ago
|
uint8_t *b = buf; \
|
||
16 years ago
|
CHECK(get_ ## v_or_f ## _ ## wire_t(&b, &tmp)); \
|
||
16 years ago
|
wvtov_ ## type(tmp, &d->v.member_name); \
|
||
16 years ago
|
s->offset += (b-buf); \
|
||
16 years ago
|
return UPB_STATUS_OK; \
|
||
16 years ago
|
}
|
||
16 years ago
|
|
||
16 years ago
|
#define T(type, v_or_f, wire_t, val_t, member_name) \
|
||
|
WVTOV(type, wire_t, val_t); /* prototype for GET below */ \
|
||
|
GET(type, v_or_f, wire_t, val_t, member_name) \
|
||
|
WVTOV(type, wire_t, val_t)
|
||
|
|
||
16 years ago
|
T(DOUBLE, f, uint64_t, double, _double) { memcpy(d, &s, sizeof(double)); }
|
||
|
T(FLOAT, f, uint32_t, float, _float) { memcpy(d, &s, sizeof(float)); }
|
||
|
T(INT32, v, uint32_t, int32_t, int32) { *d = (int32_t)s; }
|
||
|
T(INT64, v, uint64_t, int64_t, int64) { *d = (int64_t)s; }
|
||
|
T(UINT32, v, uint32_t, uint32_t, uint32) { *d = s; }
|
||
|
T(UINT64, v, uint64_t, uint64_t, uint64) { *d = s; }
|
||
|
T(SINT32, v, uint32_t, int32_t, int32) { *d = zz_decode_32(s); }
|
||
|
T(SINT64, v, uint64_t, int64_t, int64) { *d = zz_decode_64(s); }
|
||
|
T(FIXED32, f, uint32_t, uint32_t, uint32) { *d = s; }
|
||
|
T(FIXED64, f, uint64_t, uint64_t, uint64) { *d = s; }
|
||
|
T(SFIXED32, f, uint32_t, int32_t, int32) { *d = (int32_t)s; }
|
||
|
T(SFIXED64, f, uint64_t, int64_t, int64) { *d = (int64_t)s; }
|
||
|
T(BOOL, v, uint32_t, bool, _bool) { *d = (bool)s; }
|
||
16 years ago
|
T(ENUM, v, uint32_t, int32_t, int32) { *d = (int32_t)s; }
|
||
16 years ago
|
#undef WVTOV
|
||
|
#undef GET
|
||
|
#undef T
|
||
|
|
||
16 years ago
|
static void wvtov_delimited(uint32_t s, struct upb_delimited *d, size_t o)
|
||
16 years ago
|
{
|
||
|
d->offset = o;
|
||
|
d->len = s;
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
/* Use BYTES version for both STRING and BYTES, leave UTF-8 checks to client. */
|
||
16 years ago
|
static upb_status_t get_BYTES(struct upb_parse_state *s, uint8_t *buf,
|
||
|
struct upb_tagged_value *d) {
|
||
16 years ago
|
uint32_t tmp;
|
||
16 years ago
|
uint8_t *b = buf;
|
||
16 years ago
|
CHECK(get_v_uint32_t(&b, &tmp));
|
||
|
s->offset += (b-buf); /* advance past length varint. */
|
||
16 years ago
|
wvtov_delimited(tmp, &d->v.delimited, s->offset);
|
||
16 years ago
|
size_t new_offset = s->offset + d->v.delimited.len; /* skip bytes */
|
||
16 years ago
|
if (unlikely(new_offset < s->offset)) return UPB_ERROR_OVERFLOW;
|
||
16 years ago
|
s->offset = new_offset;
|
||
16 years ago
|
return UPB_STATUS_OK;
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
static upb_status_t get_MESSAGE(struct upb_parse_state *s, uint8_t *buf,
|
||
|
struct upb_tagged_value *d) {
|
||
16 years ago
|
/* We're entering a sub-message. */
|
||
|
uint32_t tmp;
|
||
16 years ago
|
uint8_t *b = buf;
|
||
16 years ago
|
CHECK(get_v_uint32_t(&b, &tmp));
|
||
16 years ago
|
s->offset += (b-buf); /* advance past length varint. */
|
||
16 years ago
|
wvtov_delimited(tmp, &d->v.delimited, s->offset);
|
||
16 years ago
|
/* Unlike STRING and BYTES, we *don't* advance past delimited here. */
|
||
16 years ago
|
if (unlikely(++s->top == s->limit)) return UPB_ERROR_STACK_OVERFLOW;
|
||
16 years ago
|
s->top->fieldset = d->field->fieldset;
|
||
16 years ago
|
s->top->end_offset = d->v.delimited.offset + d->v.delimited.len;
|
||
16 years ago
|
if (unlikely(s->top->end_offset < s->offset)) return UPB_ERROR_OVERFLOW;
|
||
|
return UPB_STATUS_OK;
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
struct upb_type_info {
|
||
|
upb_wire_type_t expected_wire_type;
|
||
|
upb_status_t (*get)(struct upb_parse_state *s, uint8_t *buf,
|
||
|
struct upb_tagged_value *d);
|
||
16 years ago
|
};
|
||
16 years ago
|
static struct upb_type_info type_info[] = {
|
||
|
{UPB_WIRE_TYPE_64BIT, get_DOUBLE},
|
||
|
{UPB_WIRE_TYPE_32BIT, get_FLOAT},
|
||
|
{UPB_WIRE_TYPE_VARINT, get_INT32},
|
||
|
{UPB_WIRE_TYPE_VARINT, get_INT64},
|
||
|
{UPB_WIRE_TYPE_VARINT, get_UINT32},
|
||
|
{UPB_WIRE_TYPE_VARINT, get_UINT64},
|
||
|
{UPB_WIRE_TYPE_VARINT, get_SINT32},
|
||
|
{UPB_WIRE_TYPE_VARINT, get_SINT64},
|
||
|
{UPB_WIRE_TYPE_32BIT, get_FIXED32},
|
||
|
{UPB_WIRE_TYPE_64BIT, get_FIXED64},
|
||
|
{UPB_WIRE_TYPE_32BIT, get_SFIXED32},
|
||
|
{UPB_WIRE_TYPE_64BIT, get_SFIXED64},
|
||
|
{UPB_WIRE_TYPE_VARINT, get_BOOL},
|
||
|
{UPB_WIRE_TYPE_DELIMITED, get_BYTES},
|
||
|
{UPB_WIRE_TYPE_DELIMITED, get_BYTES},
|
||
|
{UPB_WIRE_TYPE_VARINT, get_ENUM},
|
||
|
{UPB_WIRE_TYPE_DELIMITED, get_MESSAGE}
|
||
16 years ago
|
};
|
||
16 years ago
|
|
||
16 years ago
|
upb_status_t parse_tag(uint8_t **buf, struct upb_tag *tag)
|
||
16 years ago
|
{
|
||
|
uint32_t tag_int;
|
||
16 years ago
|
CHECK(get_v_uint32_t(buf, &tag_int));
|
||
16 years ago
|
tag->wire_type = (upb_wire_type_t)(tag_int & 0x07);
|
||
16 years ago
|
tag->field_number = tag_int >> 3;
|
||
16 years ago
|
return UPB_STATUS_OK;
|
||
16 years ago
|
}
|
||
16 years ago
|
|
||
16 years ago
|
upb_status_t parse_wire_value(uint8_t *buf, size_t *offset,
|
||
|
upb_wire_type_t wt,
|
||
|
union upb_wire_value *wv)
|
||
16 years ago
|
{
|
||
16 years ago
|
#define READ(expr) CHECK(expr); *offset += (b-buf)
|
||
|
uint8_t *b = buf;
|
||
16 years ago
|
switch(wt) {
|
||
16 years ago
|
case UPB_WIRE_TYPE_VARINT:
|
||
16 years ago
|
READ(get_v_uint64_t(&b, &wv->varint)); break;
|
||
16 years ago
|
case UPB_WIRE_TYPE_64BIT:
|
||
16 years ago
|
READ(get_f_uint64_t(&b, &wv->_64bit)); break;
|
||
16 years ago
|
case UPB_WIRE_TYPE_32BIT:
|
||
16 years ago
|
READ(get_f_uint32_t(&b, &wv->_32bit)); break;
|
||
16 years ago
|
case UPB_WIRE_TYPE_DELIMITED:
|
||
16 years ago
|
wv->delimited.offset = *offset;
|
||
|
READ(get_v_uint32_t(&b, &wv->delimited.len));
|
||
|
size_t new_offset = *offset + wv->delimited.len;
|
||
16 years ago
|
if (new_offset < *offset) return UPB_ERROR_OVERFLOW;
|
||
16 years ago
|
*offset += new_offset;
|
||
16 years ago
|
break;
|
||
16 years ago
|
case UPB_WIRE_TYPE_START_GROUP:
|
||
|
case UPB_WIRE_TYPE_END_GROUP:
|
||
|
return UPB_ERROR_GROUP; /* deprecated, no plans to support. */
|
||
16 years ago
|
}
|
||
16 years ago
|
return UPB_STATUS_OK;
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
upb_status_t skip_wire_value(uint8_t *buf, size_t *offset,
|
||
|
upb_wire_type_t wt)
|
||
16 years ago
|
{
|
||
16 years ago
|
uint8_t *b = buf;
|
||
16 years ago
|
switch(wt) {
|
||
16 years ago
|
case UPB_WIRE_TYPE_VARINT:
|
||
16 years ago
|
READ(skip_v_uint64_t(&b)); break;
|
||
16 years ago
|
case UPB_WIRE_TYPE_64BIT:
|
||
16 years ago
|
READ(skip_f_uint64_t(&b)); break;
|
||
16 years ago
|
case UPB_WIRE_TYPE_32BIT:
|
||
16 years ago
|
READ(skip_f_uint32_t(&b)); break;
|
||
16 years ago
|
case UPB_WIRE_TYPE_DELIMITED: {
|
||
16 years ago
|
/* Have to get (not skip) the length to skip the bytes. */
|
||
|
uint32_t len;
|
||
16 years ago
|
READ(get_v_uint32_t(&b, &len));
|
||
|
size_t new_offset = *offset + len;
|
||
16 years ago
|
if (new_offset < *offset) return UPB_ERROR_OVERFLOW;
|
||
16 years ago
|
*offset += new_offset;
|
||
16 years ago
|
break;
|
||
16 years ago
|
}
|
||
16 years ago
|
case UPB_WIRE_TYPE_START_GROUP:
|
||
|
case UPB_WIRE_TYPE_END_GROUP:
|
||
|
return UPB_ERROR_GROUP; /* deprecated, no plans to support. */
|
||
16 years ago
|
}
|
||
16 years ago
|
return UPB_STATUS_OK;
|
||
16 years ago
|
#undef READ
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
/* Parses and processes the next value from buf. */
|
||
16 years ago
|
upb_status_t upb_parse_field(struct upb_parse_state *s,
|
||
|
uint8_t *buf,
|
||
|
upb_field_number_t *fieldnum,
|
||
|
struct upb_tagged_value *val,
|
||
|
struct upb_tagged_wire_value *wv)
|
||
16 years ago
|
{
|
||
16 years ago
|
/* Check for end-of-message at the current stack depth. */
|
||
|
if(unlikely(s->offset >= s->top->end_offset)) {
|
||
16 years ago
|
/* If the end offset isn't an exact field boundary, the pb is corrupt. */
|
||
16 years ago
|
if(unlikely(s->offset != s->top->end_offset))
|
||
16 years ago
|
return UPB_ERROR_BAD_SUBMESSAGE_END;
|
||
16 years ago
|
s->top--;
|
||
16 years ago
|
return UPB_STATUS_SUBMESSAGE_END;
|
||
16 years ago
|
}
|
||
16 years ago
|
|
||
16 years ago
|
struct upb_tag tag;
|
||
16 years ago
|
uint8_t *b = buf;
|
||
16 years ago
|
CHECK(parse_tag(&b, &tag));
|
||
16 years ago
|
s->offset += (b-buf);
|
||
16 years ago
|
struct upb_field *fd = upb_find_field(s->top->fieldset,
|
||
16 years ago
|
tag.field_number);
|
||
16 years ago
|
upb_status_t unknown_value_status;
|
||
16 years ago
|
if(unlikely(!fd)) {
|
||
16 years ago
|
unknown_value_status = UPB_ERROR_UNKNOWN_VALUE;
|
||
16 years ago
|
goto unknown_value;
|
||
|
}
|
||
16 years ago
|
struct upb_type_info *info = &type_info[fd->type];
|
||
16 years ago
|
if(unlikely(tag.wire_type != info->expected_wire_type)) {
|
||
16 years ago
|
unknown_value_status = UPB_ERROR_MISMATCHED_TYPE;
|
||
16 years ago
|
goto unknown_value;
|
||
|
}
|
||
16 years ago
|
|
||
16 years ago
|
*fieldnum = tag.field_number;
|
||
16 years ago
|
val->field = fd;
|
||
16 years ago
|
CHECK(info->get(s, b, val));
|
||
16 years ago
|
return UPB_STATUS_OK;
|
||
16 years ago
|
|
||
16 years ago
|
unknown_value:
|
||
|
wv->type = tag.wire_type;
|
||
16 years ago
|
CHECK(parse_wire_value(buf, &s->offset, tag.wire_type, &wv->v));
|
||
16 years ago
|
return unknown_value_status;
|
||
16 years ago
|
}
|
||
16 years ago
|
|
||
16 years ago
|
void upb_init_parser(
|
||
|
struct upb_parse_state *state,
|
||
|
struct upb_fieldset *toplevel_fieldset)
|
||
16 years ago
|
{
|
||
|
state->offset = 0;
|
||
16 years ago
|
state->top = state->stack;
|
||
16 years ago
|
state->limit = state->top + UPB_MAX_STACK;
|
||
16 years ago
|
state->top->fieldset = toplevel_fieldset;
|
||
16 years ago
|
state->top->end_offset = SIZE_MAX;
|
||
|
}
|
||
16 years ago
|
|
||
|
static int compare_fields(const void *f1, const void *f2)
|
||
|
{
|
||
16 years ago
|
return ((struct upb_field*)f1)->field_number -
|
||
|
((struct upb_field*)f2)->field_number;
|
||
16 years ago
|
}
|
||
|
|