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.
1057 lines
52 KiB
1057 lines
52 KiB
4 years ago
|
/*
|
||
4 years ago
|
* Copyright (c) 2009-2021, Google LLC
|
||
4 years ago
|
* All rights reserved.
|
||
|
*
|
||
|
* Redistribution and use in source and binary forms, with or without
|
||
|
* modification, are permitted provided that the following conditions are met:
|
||
|
* * Redistributions of source code must retain the above copyright
|
||
|
* notice, this list of conditions and the following disclaimer.
|
||
|
* * Redistributions in binary form must reproduce the above copyright
|
||
|
* notice, this list of conditions and the following disclaimer in the
|
||
|
* documentation and/or other materials provided with the distribution.
|
||
|
* * Neither the name of Google LLC nor the
|
||
|
* names of its contributors may be used to endorse or promote products
|
||
|
* derived from this software without specific prior written permission.
|
||
|
*
|
||
3 years ago
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||
|
* ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
|
||
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||
4 years ago
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
|
*/
|
||
|
|
||
4 years ago
|
// Fast decoder: ~3x the speed of decode.c, but requires x86-64/ARM64.
|
||
4 years ago
|
// Also the table size grows by 2x.
|
||
|
//
|
||
4 years ago
|
// Could potentially be ported to other 64-bit archs that pass at least six
|
||
|
// arguments in registers and have 8 unused high bits in pointers.
|
||
4 years ago
|
//
|
||
|
// The overall design is to create specialized functions for every possible
|
||
|
// field type (eg. oneof boolean field with a 1 byte tag) and then dispatch
|
||
|
// to the specialized function as quickly as possible.
|
||
4 years ago
|
|
||
2 years ago
|
#include "upb/wire/decode_fast.h"
|
||
4 years ago
|
|
||
2 years ago
|
#include "upb/internal/array.h"
|
||
2 years ago
|
#include "upb/wire/decode_internal.h"
|
||
4 years ago
|
|
||
2 years ago
|
// Must be last.
|
||
4 years ago
|
#include "upb/port_def.inc"
|
||
|
|
||
4 years ago
|
#if UPB_FASTTABLE
|
||
|
|
||
4 years ago
|
// The standard set of arguments passed to each parsing function.
|
||
|
// Thanks to x86-64 calling conventions, these will stay in registers.
|
||
3 years ago
|
#define UPB_PARSE_PARAMS \
|
||
|
upb_Decoder *d, const char *ptr, upb_Message *msg, intptr_t table, \
|
||
4 years ago
|
uint64_t hasbits, uint64_t data
|
||
|
|
||
4 years ago
|
#define UPB_PARSE_ARGS d, ptr, msg, table, hasbits, data
|
||
|
|
||
3 years ago
|
#define RETURN_GENERIC(m) \
|
||
|
/* Uncomment either of these for debugging purposes. */ \
|
||
|
/* fprintf(stderr, m); */ \
|
||
|
/*__builtin_trap(); */ \
|
||
2 years ago
|
return _upb_FastDecoder_DecodeGeneric(d, ptr, msg, table, hasbits, 0);
|
||
4 years ago
|
|
||
4 years ago
|
typedef enum {
|
||
3 years ago
|
CARD_s = 0, /* Singular (optional, non-repeated) */
|
||
|
CARD_o = 1, /* Oneof */
|
||
|
CARD_r = 2, /* Repeated */
|
||
|
CARD_p = 3 /* Packed Repeated */
|
||
4 years ago
|
} upb_card;
|
||
|
|
||
4 years ago
|
UPB_NOINLINE
|
||
3 years ago
|
static const char* fastdecode_isdonefallback(UPB_PARSE_PARAMS) {
|
||
4 years ago
|
int overrun = data;
|
||
3 years ago
|
int status;
|
||
2 years ago
|
ptr = _upb_Decoder_IsDoneFallbackInline(d, ptr, overrun, &status);
|
||
|
if (ptr == NULL) _upb_FastDecoder_ErrorJmp(d, status);
|
||
|
data = _upb_FastDecoder_LoadTag(ptr);
|
||
|
UPB_MUSTTAIL return _upb_FastDecoder_TagDispatch(UPB_PARSE_ARGS);
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
UPB_FORCEINLINE
|
||
3 years ago
|
static const char* fastdecode_dispatch(UPB_PARSE_PARAMS) {
|
||
4 years ago
|
if (UPB_UNLIKELY(ptr >= d->limit_ptr)) {
|
||
|
int overrun = ptr - d->end;
|
||
|
if (UPB_LIKELY(overrun == d->limit)) {
|
||
4 years ago
|
// Parse is finished.
|
||
4 years ago
|
*(uint32_t*)msg |= hasbits; // Sync hasbits.
|
||
3 years ago
|
const upb_MiniTable* l = decode_totablep(table);
|
||
3 years ago
|
return UPB_UNLIKELY(l->required_count)
|
||
2 years ago
|
? _upb_Decoder_CheckRequired(d, ptr, msg, l)
|
||
3 years ago
|
: ptr;
|
||
4 years ago
|
} else {
|
||
4 years ago
|
data = overrun;
|
||
|
UPB_MUSTTAIL return fastdecode_isdonefallback(UPB_PARSE_ARGS);
|
||
4 years ago
|
}
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
|
// Read two bytes of tag data (for a one-byte tag, the high byte is junk).
|
||
2 years ago
|
data = _upb_FastDecoder_LoadTag(ptr);
|
||
|
UPB_MUSTTAIL return _upb_FastDecoder_TagDispatch(UPB_PARSE_ARGS);
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
UPB_FORCEINLINE
|
||
4 years ago
|
static bool fastdecode_checktag(uint16_t data, int tagbytes) {
|
||
4 years ago
|
if (tagbytes == 1) {
|
||
|
return (data & 0xff) == 0;
|
||
|
} else {
|
||
4 years ago
|
return data == 0;
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
UPB_FORCEINLINE
|
||
3 years ago
|
static const char* fastdecode_longsize(const char* ptr, int* size) {
|
||
4 years ago
|
int i;
|
||
4 years ago
|
UPB_ASSERT(*size & 0x80);
|
||
|
*size &= 0xff;
|
||
4 years ago
|
for (i = 0; i < 3; i++) {
|
||
4 years ago
|
ptr++;
|
||
|
size_t byte = (uint8_t)ptr[-1];
|
||
|
*size += (byte - 1) << (7 + 7 * i);
|
||
|
if (UPB_LIKELY((byte & 0x80) == 0)) return ptr;
|
||
|
}
|
||
|
ptr++;
|
||
|
size_t byte = (uint8_t)ptr[-1];
|
||
|
// len is limited by 2gb not 4gb, hence 8 and not 16 as normally expected
|
||
|
// for a 32 bit varint.
|
||
|
if (UPB_UNLIKELY(byte >= 8)) return NULL;
|
||
|
*size += (byte - 1) << 28;
|
||
|
return ptr;
|
||
|
}
|
||
|
|
||
4 years ago
|
UPB_FORCEINLINE
|
||
3 years ago
|
static bool fastdecode_boundscheck(const char* ptr, size_t len,
|
||
|
const char* end) {
|
||
4 years ago
|
uintptr_t uptr = (uintptr_t)ptr;
|
||
|
uintptr_t uend = (uintptr_t)end + 16;
|
||
|
uintptr_t res = uptr + len;
|
||
|
return res < uptr || res > uend;
|
||
|
}
|
||
|
|
||
|
UPB_FORCEINLINE
|
||
3 years ago
|
static bool fastdecode_boundscheck2(const char* ptr, size_t len,
|
||
|
const char* end) {
|
||
4 years ago
|
// This is one extra branch compared to the more normal:
|
||
|
// return (size_t)(end - ptr) < size;
|
||
|
// However it is one less computation if we are just about to use "ptr + len":
|
||
|
// https://godbolt.org/z/35YGPz
|
||
|
// In microbenchmarks this shows an overall 4% improvement.
|
||
|
uintptr_t uptr = (uintptr_t)ptr;
|
||
|
uintptr_t uend = (uintptr_t)end;
|
||
|
uintptr_t res = uptr + len;
|
||
|
return res < uptr || res > uend;
|
||
|
}
|
||
|
|
||
3 years ago
|
typedef const char* fastdecode_delimfunc(upb_Decoder* d, const char* ptr,
|
||
|
void* ctx);
|
||
4 years ago
|
|
||
|
UPB_FORCEINLINE
|
||
3 years ago
|
static const char* fastdecode_delimited(upb_Decoder* d, const char* ptr,
|
||
|
fastdecode_delimfunc* func, void* ctx) {
|
||
4 years ago
|
ptr++;
|
||
|
int len = (int8_t)ptr[-1];
|
||
|
if (fastdecode_boundscheck2(ptr, len, d->limit_ptr)) {
|
||
|
// Slow case: Sub-message is >=128 bytes and/or exceeds the current buffer.
|
||
|
// If it exceeds the buffer limit, limit/limit_ptr will change during
|
||
|
// sub-message parsing, so we need to preserve delta, not limit.
|
||
|
if (UPB_UNLIKELY(len & 0x80)) {
|
||
|
// Size varint >1 byte (length >= 128).
|
||
|
ptr = fastdecode_longsize(ptr, &len);
|
||
|
if (!ptr) {
|
||
|
// Corrupt wire format: size exceeded INT_MAX.
|
||
|
return NULL;
|
||
|
}
|
||
|
}
|
||
|
if (ptr - d->end + (int)len > d->limit) {
|
||
|
// Corrupt wire format: invalid limit.
|
||
|
return NULL;
|
||
|
}
|
||
2 years ago
|
int delta = _upb_Decoder_PushLimit(d, ptr, len);
|
||
4 years ago
|
ptr = func(d, ptr, ctx);
|
||
2 years ago
|
_upb_Decoder_PopLimit(d, ptr, delta);
|
||
4 years ago
|
} else {
|
||
|
// Fast case: Sub-message is <128 bytes and fits in the current buffer.
|
||
|
// This means we can preserve limit/limit_ptr verbatim.
|
||
3 years ago
|
const char* saved_limit_ptr = d->limit_ptr;
|
||
4 years ago
|
int saved_limit = d->limit;
|
||
|
d->limit_ptr = ptr + len;
|
||
|
d->limit = d->limit_ptr - d->end;
|
||
|
UPB_ASSERT(d->limit_ptr == d->end + UPB_MIN(0, d->limit));
|
||
|
ptr = func(d, ptr, ctx);
|
||
|
d->limit_ptr = saved_limit_ptr;
|
||
|
d->limit = saved_limit;
|
||
|
UPB_ASSERT(d->limit_ptr == d->end + UPB_MIN(0, d->limit));
|
||
|
}
|
||
|
return ptr;
|
||
|
}
|
||
|
|
||
4 years ago
|
/* singular, oneof, repeated field handling ***********************************/
|
||
|
|
||
|
typedef struct {
|
||
3 years ago
|
upb_Array* arr;
|
||
|
void* end;
|
||
4 years ago
|
} fastdecode_arr;
|
||
|
|
||
|
typedef enum {
|
||
|
FD_NEXT_ATLIMIT,
|
||
|
FD_NEXT_SAMEFIELD,
|
||
|
FD_NEXT_OTHERFIELD
|
||
|
} fastdecode_next;
|
||
|
|
||
|
typedef struct {
|
||
3 years ago
|
void* dst;
|
||
4 years ago
|
fastdecode_next next;
|
||
|
uint32_t tag;
|
||
|
} fastdecode_nextret;
|
||
|
|
||
|
UPB_FORCEINLINE
|
||
3 years ago
|
static void* fastdecode_resizearr(upb_Decoder* d, void* dst,
|
||
|
fastdecode_arr* farr, int valbytes) {
|
||
4 years ago
|
if (UPB_UNLIKELY(dst == farr->end)) {
|
||
2 years ago
|
size_t old_size = farr->arr->capacity;
|
||
4 years ago
|
size_t old_bytes = old_size * valbytes;
|
||
|
size_t new_size = old_size * 2;
|
||
|
size_t new_bytes = new_size * valbytes;
|
||
3 years ago
|
char* old_ptr = _upb_array_ptr(farr->arr);
|
||
|
char* new_ptr = upb_Arena_Realloc(&d->arena, old_ptr, old_bytes, new_bytes);
|
||
4 years ago
|
uint8_t elem_size_lg2 = __builtin_ctz(valbytes);
|
||
2 years ago
|
farr->arr->capacity = new_size;
|
||
4 years ago
|
farr->arr->data = _upb_array_tagptr(new_ptr, elem_size_lg2);
|
||
4 years ago
|
dst = (void*)(new_ptr + (old_size * valbytes));
|
||
|
farr->end = (void*)(new_ptr + (new_size * valbytes));
|
||
|
}
|
||
|
return dst;
|
||
|
}
|
||
|
|
||
|
UPB_FORCEINLINE
|
||
|
static bool fastdecode_tagmatch(uint32_t tag, uint64_t data, int tagbytes) {
|
||
|
if (tagbytes == 1) {
|
||
|
return (uint8_t)tag == (uint8_t)data;
|
||
|
} else {
|
||
|
return (uint16_t)tag == (uint16_t)data;
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
UPB_FORCEINLINE
|
||
3 years ago
|
static void fastdecode_commitarr(void* dst, fastdecode_arr* farr,
|
||
4 years ago
|
int valbytes) {
|
||
2 years ago
|
farr->arr->size =
|
||
3 years ago
|
(size_t)((char*)dst - (char*)_upb_array_ptr(farr->arr)) / valbytes;
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
UPB_FORCEINLINE
|
||
3 years ago
|
static fastdecode_nextret fastdecode_nextrepeated(upb_Decoder* d, void* dst,
|
||
|
const char** ptr,
|
||
|
fastdecode_arr* farr,
|
||
4 years ago
|
uint64_t data, int tagbytes,
|
||
|
int valbytes) {
|
||
|
fastdecode_nextret ret;
|
||
3 years ago
|
dst = (char*)dst + valbytes;
|
||
4 years ago
|
|
||
2 years ago
|
if (UPB_LIKELY(!_upb_Decoder_IsDone(d, ptr))) {
|
||
|
ret.tag = _upb_FastDecoder_LoadTag(*ptr);
|
||
4 years ago
|
if (fastdecode_tagmatch(ret.tag, data, tagbytes)) {
|
||
|
ret.next = FD_NEXT_SAMEFIELD;
|
||
|
} else {
|
||
4 years ago
|
fastdecode_commitarr(dst, farr, valbytes);
|
||
4 years ago
|
ret.next = FD_NEXT_OTHERFIELD;
|
||
|
}
|
||
|
} else {
|
||
4 years ago
|
fastdecode_commitarr(dst, farr, valbytes);
|
||
4 years ago
|
ret.next = FD_NEXT_ATLIMIT;
|
||
|
}
|
||
4 years ago
|
|
||
4 years ago
|
ret.dst = dst;
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
4 years ago
|
UPB_FORCEINLINE
|
||
3 years ago
|
static void* fastdecode_fieldmem(upb_Message* msg, uint64_t data) {
|
||
4 years ago
|
size_t ofs = data >> 48;
|
||
3 years ago
|
return (char*)msg + ofs;
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
UPB_FORCEINLINE
|
||
3 years ago
|
static void* fastdecode_getfield(upb_Decoder* d, const char* ptr,
|
||
|
upb_Message* msg, uint64_t* data,
|
||
|
uint64_t* hasbits, fastdecode_arr* farr,
|
||
|
int valbytes, upb_card card) {
|
||
4 years ago
|
switch (card) {
|
||
4 years ago
|
case CARD_s: {
|
||
|
uint8_t hasbit_index = *data >> 24;
|
||
4 years ago
|
// Set hasbit and return pointer to scalar field.
|
||
4 years ago
|
*hasbits |= 1ull << hasbit_index;
|
||
4 years ago
|
return fastdecode_fieldmem(msg, *data);
|
||
4 years ago
|
}
|
||
|
case CARD_o: {
|
||
|
uint16_t case_ofs = *data >> 32;
|
||
3 years ago
|
uint32_t* oneof_case = UPB_PTR_AT(msg, case_ofs, uint32_t);
|
||
4 years ago
|
uint8_t field_number = *data >> 24;
|
||
|
*oneof_case = field_number;
|
||
4 years ago
|
return fastdecode_fieldmem(msg, *data);
|
||
4 years ago
|
}
|
||
4 years ago
|
case CARD_r: {
|
||
3 years ago
|
// Get pointer to upb_Array and allocate/expand if necessary.
|
||
4 years ago
|
uint8_t elem_size_lg2 = __builtin_ctz(valbytes);
|
||
3 years ago
|
upb_Array** arr_p = fastdecode_fieldmem(msg, *data);
|
||
|
char* begin;
|
||
4 years ago
|
*(uint32_t*)msg |= *hasbits;
|
||
|
*hasbits = 0;
|
||
|
if (UPB_LIKELY(!*arr_p)) {
|
||
3 years ago
|
farr->arr = _upb_Array_New(&d->arena, 8, elem_size_lg2);
|
||
4 years ago
|
*arr_p = farr->arr;
|
||
4 years ago
|
} else {
|
||
4 years ago
|
farr->arr = *arr_p;
|
||
4 years ago
|
}
|
||
4 years ago
|
begin = _upb_array_ptr(farr->arr);
|
||
2 years ago
|
farr->end = begin + (farr->arr->capacity * valbytes);
|
||
2 years ago
|
*data = _upb_FastDecoder_LoadTag(ptr);
|
||
2 years ago
|
return begin + (farr->arr->size * valbytes);
|
||
4 years ago
|
}
|
||
4 years ago
|
default:
|
||
|
UPB_UNREACHABLE();
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
UPB_FORCEINLINE
|
||
3 years ago
|
static bool fastdecode_flippacked(uint64_t* data, int tagbytes) {
|
||
4 years ago
|
*data ^= (0x2 ^ 0x0); // Patch data to match packed wiretype.
|
||
|
return fastdecode_checktag(*data, tagbytes);
|
||
|
}
|
||
|
|
||
3 years ago
|
#define FASTDECODE_CHECKPACKED(tagbytes, card, func) \
|
||
|
if (UPB_UNLIKELY(!fastdecode_checktag(data, tagbytes))) { \
|
||
|
if (card == CARD_r && fastdecode_flippacked(&data, tagbytes)) { \
|
||
|
UPB_MUSTTAIL return func(UPB_PARSE_ARGS); \
|
||
|
} \
|
||
|
RETURN_GENERIC("packed check tag mismatch\n"); \
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
/* varint fields **************************************************************/
|
||
4 years ago
|
|
||
4 years ago
|
UPB_FORCEINLINE
|
||
|
static uint64_t fastdecode_munge(uint64_t val, int valbytes, bool zigzag) {
|
||
4 years ago
|
if (valbytes == 1) {
|
||
|
return val != 0;
|
||
|
} else if (zigzag) {
|
||
|
if (valbytes == 4) {
|
||
|
uint32_t n = val;
|
||
|
return (n >> 1) ^ -(int32_t)(n & 1);
|
||
|
} else if (valbytes == 8) {
|
||
|
return (val >> 1) ^ -(int64_t)(val & 1);
|
||
|
}
|
||
|
UPB_UNREACHABLE();
|
||
4 years ago
|
}
|
||
4 years ago
|
return val;
|
||
|
}
|
||
|
|
||
4 years ago
|
UPB_FORCEINLINE
|
||
3 years ago
|
static const char* fastdecode_varint64(const char* ptr, uint64_t* val) {
|
||
4 years ago
|
ptr++;
|
||
|
*val = (uint8_t)ptr[-1];
|
||
|
if (UPB_UNLIKELY(*val & 0x80)) {
|
||
|
int i;
|
||
|
for (i = 0; i < 8; i++) {
|
||
|
ptr++;
|
||
|
uint64_t byte = (uint8_t)ptr[-1];
|
||
|
*val += (byte - 1) << (7 + 7 * i);
|
||
|
if (UPB_LIKELY((byte & 0x80) == 0)) goto done;
|
||
|
}
|
||
|
ptr++;
|
||
|
uint64_t byte = (uint8_t)ptr[-1];
|
||
|
if (byte > 1) {
|
||
|
return NULL;
|
||
|
}
|
||
|
*val += (byte - 1) << 63;
|
||
|
}
|
||
|
done:
|
||
4 years ago
|
UPB_ASSUME(ptr != NULL);
|
||
4 years ago
|
return ptr;
|
||
|
}
|
||
|
|
||
4 years ago
|
#define FASTDECODE_UNPACKEDVARINT(d, ptr, msg, table, hasbits, data, tagbytes, \
|
||
|
valbytes, card, zigzag, packed) \
|
||
|
uint64_t val; \
|
||
3 years ago
|
void* dst; \
|
||
4 years ago
|
fastdecode_arr farr; \
|
||
|
\
|
||
4 years ago
|
FASTDECODE_CHECKPACKED(tagbytes, card, packed); \
|
||
4 years ago
|
\
|
||
|
dst = fastdecode_getfield(d, ptr, msg, &data, &hasbits, &farr, valbytes, \
|
||
|
card); \
|
||
|
if (card == CARD_r) { \
|
||
|
if (UPB_UNLIKELY(!dst)) { \
|
||
|
RETURN_GENERIC("need array resize\n"); \
|
||
|
} \
|
||
|
} \
|
||
|
\
|
||
|
again: \
|
||
|
if (card == CARD_r) { \
|
||
|
dst = fastdecode_resizearr(d, dst, &farr, valbytes); \
|
||
|
} \
|
||
|
\
|
||
|
ptr += tagbytes; \
|
||
|
ptr = fastdecode_varint64(ptr, &val); \
|
||
2 years ago
|
if (ptr == NULL) _upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed); \
|
||
4 years ago
|
val = fastdecode_munge(val, valbytes, zigzag); \
|
||
|
memcpy(dst, &val, valbytes); \
|
||
|
\
|
||
|
if (card == CARD_r) { \
|
||
|
fastdecode_nextret ret = fastdecode_nextrepeated( \
|
||
|
d, dst, &ptr, &farr, data, tagbytes, valbytes); \
|
||
|
switch (ret.next) { \
|
||
3 years ago
|
case FD_NEXT_SAMEFIELD: \
|
||
|
dst = ret.dst; \
|
||
|
goto again; \
|
||
|
case FD_NEXT_OTHERFIELD: \
|
||
|
data = ret.tag; \
|
||
2 years ago
|
UPB_MUSTTAIL return _upb_FastDecoder_TagDispatch(UPB_PARSE_ARGS); \
|
||
3 years ago
|
case FD_NEXT_ATLIMIT: \
|
||
|
return ptr; \
|
||
4 years ago
|
} \
|
||
|
} \
|
||
|
\
|
||
|
UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS);
|
||
4 years ago
|
|
||
4 years ago
|
typedef struct {
|
||
|
uint8_t valbytes;
|
||
|
bool zigzag;
|
||
3 years ago
|
void* dst;
|
||
4 years ago
|
fastdecode_arr farr;
|
||
|
} fastdecode_varintdata;
|
||
|
|
||
|
UPB_FORCEINLINE
|
||
3 years ago
|
static const char* fastdecode_topackedvarint(upb_Decoder* d, const char* ptr,
|
||
|
void* ctx) {
|
||
|
fastdecode_varintdata* data = ctx;
|
||
|
void* dst = data->dst;
|
||
4 years ago
|
uint64_t val;
|
||
|
|
||
2 years ago
|
while (!_upb_Decoder_IsDone(d, &ptr)) {
|
||
4 years ago
|
dst = fastdecode_resizearr(d, dst, &data->farr, data->valbytes);
|
||
|
ptr = fastdecode_varint64(ptr, &val);
|
||
|
if (ptr == NULL) return NULL;
|
||
|
val = fastdecode_munge(val, data->valbytes, data->zigzag);
|
||
|
memcpy(dst, &val, data->valbytes);
|
||
3 years ago
|
dst = (char*)dst + data->valbytes;
|
||
4 years ago
|
}
|
||
|
|
||
|
fastdecode_commitarr(dst, &data->farr, data->valbytes);
|
||
|
return ptr;
|
||
|
}
|
||
|
|
||
3 years ago
|
#define FASTDECODE_PACKEDVARINT(d, ptr, msg, table, hasbits, data, tagbytes, \
|
||
|
valbytes, zigzag, unpacked) \
|
||
|
fastdecode_varintdata ctx = {valbytes, zigzag}; \
|
||
|
\
|
||
|
FASTDECODE_CHECKPACKED(tagbytes, CARD_r, unpacked); \
|
||
|
\
|
||
|
ctx.dst = fastdecode_getfield(d, ptr, msg, &data, &hasbits, &ctx.farr, \
|
||
|
valbytes, CARD_r); \
|
||
|
if (UPB_UNLIKELY(!ctx.dst)) { \
|
||
|
RETURN_GENERIC("need array resize\n"); \
|
||
|
} \
|
||
|
\
|
||
|
ptr += tagbytes; \
|
||
|
ptr = fastdecode_delimited(d, ptr, &fastdecode_topackedvarint, &ctx); \
|
||
|
\
|
||
|
if (UPB_UNLIKELY(ptr == NULL)) { \
|
||
2 years ago
|
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed); \
|
||
3 years ago
|
} \
|
||
|
\
|
||
4 years ago
|
UPB_MUSTTAIL return fastdecode_dispatch(d, ptr, msg, table, hasbits, 0);
|
||
|
|
||
|
#define FASTDECODE_VARINT(d, ptr, msg, table, hasbits, data, tagbytes, \
|
||
|
valbytes, card, zigzag, unpacked, packed) \
|
||
|
if (card == CARD_p) { \
|
||
|
FASTDECODE_PACKEDVARINT(d, ptr, msg, table, hasbits, data, tagbytes, \
|
||
|
valbytes, zigzag, unpacked); \
|
||
|
} else { \
|
||
|
FASTDECODE_UNPACKEDVARINT(d, ptr, msg, table, hasbits, data, tagbytes, \
|
||
|
valbytes, card, zigzag, packed); \
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
#define z_ZZ true
|
||
|
#define b_ZZ false
|
||
|
#define v_ZZ false
|
||
4 years ago
|
|
||
4 years ago
|
/* Generate all combinations:
|
||
4 years ago
|
* {s,o,r,p} x {b1,v4,z4,v8,z8} x {1bt,2bt} */
|
||
4 years ago
|
|
||
4 years ago
|
#define F(card, type, valbytes, tagbytes) \
|
||
4 years ago
|
UPB_NOINLINE \
|
||
3 years ago
|
const char* upb_p##card##type##valbytes##_##tagbytes##bt(UPB_PARSE_PARAMS) { \
|
||
4 years ago
|
FASTDECODE_VARINT(d, ptr, msg, table, hasbits, data, tagbytes, valbytes, \
|
||
|
CARD_##card, type##_ZZ, \
|
||
|
upb_pr##type##valbytes##_##tagbytes##bt, \
|
||
|
upb_pp##type##valbytes##_##tagbytes##bt); \
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
4 years ago
|
#define TYPES(card, tagbytes) \
|
||
|
F(card, b, 1, tagbytes) \
|
||
|
F(card, v, 4, tagbytes) \
|
||
|
F(card, v, 8, tagbytes) \
|
||
|
F(card, z, 4, tagbytes) \
|
||
|
F(card, z, 8, tagbytes)
|
||
|
|
||
|
#define TAGBYTES(card) \
|
||
|
TYPES(card, 1) \
|
||
|
TYPES(card, 2)
|
||
|
|
||
|
TAGBYTES(s)
|
||
|
TAGBYTES(o)
|
||
4 years ago
|
TAGBYTES(r)
|
||
4 years ago
|
TAGBYTES(p)
|
||
4 years ago
|
|
||
|
#undef z_ZZ
|
||
|
#undef b_ZZ
|
||
|
#undef v_ZZ
|
||
|
#undef o_ONEOF
|
||
|
#undef s_ONEOF
|
||
|
#undef r_ONEOF
|
||
4 years ago
|
#undef F
|
||
|
#undef TYPES
|
||
|
#undef TAGBYTES
|
||
4 years ago
|
#undef FASTDECODE_UNPACKEDVARINT
|
||
|
#undef FASTDECODE_PACKEDVARINT
|
||
|
#undef FASTDECODE_VARINT
|
||
4 years ago
|
|
||
|
/* fixed fields ***************************************************************/
|
||
|
|
||
3 years ago
|
#define FASTDECODE_UNPACKEDFIXED(d, ptr, msg, table, hasbits, data, tagbytes, \
|
||
|
valbytes, card, packed) \
|
||
|
void* dst; \
|
||
|
fastdecode_arr farr; \
|
||
|
\
|
||
|
FASTDECODE_CHECKPACKED(tagbytes, card, packed) \
|
||
|
\
|
||
|
dst = fastdecode_getfield(d, ptr, msg, &data, &hasbits, &farr, valbytes, \
|
||
|
card); \
|
||
|
if (card == CARD_r) { \
|
||
|
if (UPB_UNLIKELY(!dst)) { \
|
||
|
RETURN_GENERIC("couldn't allocate array in arena\n"); \
|
||
|
} \
|
||
|
} \
|
||
|
\
|
||
|
again: \
|
||
|
if (card == CARD_r) { \
|
||
|
dst = fastdecode_resizearr(d, dst, &farr, valbytes); \
|
||
|
} \
|
||
|
\
|
||
|
ptr += tagbytes; \
|
||
|
memcpy(dst, ptr, valbytes); \
|
||
|
ptr += valbytes; \
|
||
|
\
|
||
|
if (card == CARD_r) { \
|
||
|
fastdecode_nextret ret = fastdecode_nextrepeated( \
|
||
|
d, dst, &ptr, &farr, data, tagbytes, valbytes); \
|
||
|
switch (ret.next) { \
|
||
|
case FD_NEXT_SAMEFIELD: \
|
||
|
dst = ret.dst; \
|
||
|
goto again; \
|
||
|
case FD_NEXT_OTHERFIELD: \
|
||
|
data = ret.tag; \
|
||
2 years ago
|
UPB_MUSTTAIL return _upb_FastDecoder_TagDispatch(UPB_PARSE_ARGS); \
|
||
3 years ago
|
case FD_NEXT_ATLIMIT: \
|
||
|
return ptr; \
|
||
|
} \
|
||
|
} \
|
||
|
\
|
||
4 years ago
|
UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS);
|
||
|
|
||
4 years ago
|
#define FASTDECODE_PACKEDFIXED(d, ptr, msg, table, hasbits, data, tagbytes, \
|
||
|
valbytes, unpacked) \
|
||
|
FASTDECODE_CHECKPACKED(tagbytes, CARD_r, unpacked) \
|
||
|
\
|
||
|
ptr += tagbytes; \
|
||
|
int size = (uint8_t)ptr[0]; \
|
||
|
ptr++; \
|
||
|
if (size & 0x80) { \
|
||
|
ptr = fastdecode_longsize(ptr, &size); \
|
||
|
} \
|
||
|
\
|
||
|
if (UPB_UNLIKELY(fastdecode_boundscheck(ptr, size, d->limit_ptr) || \
|
||
|
(size % valbytes) != 0)) { \
|
||
2 years ago
|
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed); \
|
||
4 years ago
|
} \
|
||
|
\
|
||
3 years ago
|
upb_Array** arr_p = fastdecode_fieldmem(msg, data); \
|
||
|
upb_Array* arr = *arr_p; \
|
||
4 years ago
|
uint8_t elem_size_lg2 = __builtin_ctz(valbytes); \
|
||
|
int elems = size / valbytes; \
|
||
|
\
|
||
|
if (UPB_LIKELY(!arr)) { \
|
||
3 years ago
|
*arr_p = arr = _upb_Array_New(&d->arena, elems, elem_size_lg2); \
|
||
4 years ago
|
if (!arr) { \
|
||
2 years ago
|
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed); \
|
||
4 years ago
|
} \
|
||
|
} else { \
|
||
3 years ago
|
_upb_Array_Resize(arr, elems, &d->arena); \
|
||
4 years ago
|
} \
|
||
|
\
|
||
3 years ago
|
char* dst = _upb_array_ptr(arr); \
|
||
4 years ago
|
memcpy(dst, ptr, size); \
|
||
2 years ago
|
arr->size = elems; \
|
||
4 years ago
|
\
|
||
|
ptr += size; \
|
||
4 years ago
|
UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS);
|
||
|
|
||
|
#define FASTDECODE_FIXED(d, ptr, msg, table, hasbits, data, tagbytes, \
|
||
|
valbytes, card, unpacked, packed) \
|
||
|
if (card == CARD_p) { \
|
||
|
FASTDECODE_PACKEDFIXED(d, ptr, msg, table, hasbits, data, tagbytes, \
|
||
|
valbytes, unpacked); \
|
||
|
} else { \
|
||
|
FASTDECODE_UNPACKEDFIXED(d, ptr, msg, table, hasbits, data, tagbytes, \
|
||
|
valbytes, card, packed); \
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
|
/* Generate all combinations:
|
||
4 years ago
|
* {s,o,r,p} x {f4,f8} x {1bt,2bt} */
|
||
4 years ago
|
|
||
4 years ago
|
#define F(card, valbytes, tagbytes) \
|
||
|
UPB_NOINLINE \
|
||
3 years ago
|
const char* upb_p##card##f##valbytes##_##tagbytes##bt(UPB_PARSE_PARAMS) { \
|
||
4 years ago
|
FASTDECODE_FIXED(d, ptr, msg, table, hasbits, data, tagbytes, valbytes, \
|
||
|
CARD_##card, upb_ppf##valbytes##_##tagbytes##bt, \
|
||
|
upb_prf##valbytes##_##tagbytes##bt); \
|
||
4 years ago
|
}
|
||
|
|
||
|
#define TYPES(card, tagbytes) \
|
||
4 years ago
|
F(card, 4, tagbytes) \
|
||
|
F(card, 8, tagbytes)
|
||
4 years ago
|
|
||
|
#define TAGBYTES(card) \
|
||
|
TYPES(card, 1) \
|
||
|
TYPES(card, 2)
|
||
|
|
||
|
TAGBYTES(s)
|
||
|
TAGBYTES(o)
|
||
|
TAGBYTES(r)
|
||
4 years ago
|
TAGBYTES(p)
|
||
4 years ago
|
|
||
4 years ago
|
#undef F
|
||
|
#undef TYPES
|
||
|
#undef TAGBYTES
|
||
4 years ago
|
#undef FASTDECODE_UNPACKEDFIXED
|
||
|
#undef FASTDECODE_PACKEDFIXED
|
||
4 years ago
|
|
||
4 years ago
|
/* string fields **************************************************************/
|
||
|
|
||
3 years ago
|
typedef const char* fastdecode_copystr_func(struct upb_Decoder* d,
|
||
3 years ago
|
const char* ptr, upb_Message* msg,
|
||
3 years ago
|
const upb_MiniTable* table,
|
||
|
uint64_t hasbits,
|
||
|
upb_StringView* dst);
|
||
4 years ago
|
|
||
4 years ago
|
UPB_NOINLINE
|
||
3 years ago
|
static const char* fastdecode_verifyutf8(upb_Decoder* d, const char* ptr,
|
||
3 years ago
|
upb_Message* msg, intptr_t table,
|
||
4 years ago
|
uint64_t hasbits, uint64_t data) {
|
||
3 years ago
|
upb_StringView* dst = (upb_StringView*)data;
|
||
2 years ago
|
if (!_upb_Decoder_VerifyUtf8Inline(dst->data, dst->size)) {
|
||
|
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_BadUtf8);
|
||
4 years ago
|
}
|
||
4 years ago
|
UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS);
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
|
#define FASTDECODE_LONGSTRING(d, ptr, msg, table, hasbits, dst, validate_utf8) \
|
||
|
int size = (uint8_t)ptr[0]; /* Could plumb through hasbits. */ \
|
||
|
ptr++; \
|
||
|
if (size & 0x80) { \
|
||
|
ptr = fastdecode_longsize(ptr, &size); \
|
||
|
} \
|
||
|
\
|
||
|
if (UPB_UNLIKELY(fastdecode_boundscheck(ptr, size, d->limit_ptr))) { \
|
||
|
dst->size = 0; \
|
||
2 years ago
|
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed); \
|
||
4 years ago
|
} \
|
||
|
\
|
||
3 years ago
|
if (d->options & kUpb_DecodeOption_AliasString) { \
|
||
4 years ago
|
dst->data = ptr; \
|
||
|
dst->size = size; \
|
||
|
} else { \
|
||
3 years ago
|
char* data = upb_Arena_Malloc(&d->arena, size); \
|
||
4 years ago
|
if (!data) { \
|
||
2 years ago
|
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory); \
|
||
4 years ago
|
} \
|
||
|
memcpy(data, ptr, size); \
|
||
|
dst->data = data; \
|
||
|
dst->size = size; \
|
||
|
} \
|
||
|
\
|
||
|
ptr += size; \
|
||
|
if (validate_utf8) { \
|
||
4 years ago
|
data = (uint64_t)dst; \
|
||
|
UPB_MUSTTAIL return fastdecode_verifyutf8(UPB_PARSE_ARGS); \
|
||
4 years ago
|
} else { \
|
||
|
UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS); \
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
4 years ago
|
UPB_NOINLINE
|
||
3 years ago
|
static const char* fastdecode_longstring_utf8(struct upb_Decoder* d,
|
||
3 years ago
|
const char* ptr, upb_Message* msg,
|
||
4 years ago
|
intptr_t table, uint64_t hasbits,
|
||
|
uint64_t data) {
|
||
3 years ago
|
upb_StringView* dst = (upb_StringView*)data;
|
||
4 years ago
|
FASTDECODE_LONGSTRING(d, ptr, msg, table, hasbits, dst, true);
|
||
4 years ago
|
}
|
||
|
|
||
|
UPB_NOINLINE
|
||
3 years ago
|
static const char* fastdecode_longstring_noutf8(
|
||
|
struct upb_Decoder* d, const char* ptr, upb_Message* msg, intptr_t table,
|
||
|
uint64_t hasbits, uint64_t data) {
|
||
3 years ago
|
upb_StringView* dst = (upb_StringView*)data;
|
||
4 years ago
|
FASTDECODE_LONGSTRING(d, ptr, msg, table, hasbits, dst, false);
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
UPB_FORCEINLINE
|
||
3 years ago
|
static void fastdecode_docopy(upb_Decoder* d, const char* ptr, uint32_t size,
|
||
|
int copy, char* data, upb_StringView* dst) {
|
||
4 years ago
|
d->arena.head.ptr += copy;
|
||
4 years ago
|
dst->data = data;
|
||
4 years ago
|
UPB_UNPOISON_MEMORY_REGION(data, copy);
|
||
|
memcpy(data, ptr, copy);
|
||
|
UPB_POISON_MEMORY_REGION(data + size, copy - size);
|
||
|
}
|
||
|
|
||
3 years ago
|
#define FASTDECODE_COPYSTRING(d, ptr, msg, table, hasbits, data, tagbytes, \
|
||
|
card, validate_utf8) \
|
||
|
upb_StringView* dst; \
|
||
|
fastdecode_arr farr; \
|
||
|
int64_t size; \
|
||
|
size_t arena_has; \
|
||
|
size_t common_has; \
|
||
|
char* buf; \
|
||
|
\
|
||
|
UPB_ASSERT((d->options & kUpb_DecodeOption_AliasString) == 0); \
|
||
|
UPB_ASSERT(fastdecode_checktag(data, tagbytes)); \
|
||
|
\
|
||
|
dst = fastdecode_getfield(d, ptr, msg, &data, &hasbits, &farr, \
|
||
|
sizeof(upb_StringView), card); \
|
||
|
\
|
||
|
again: \
|
||
|
if (card == CARD_r) { \
|
||
|
dst = fastdecode_resizearr(d, dst, &farr, sizeof(upb_StringView)); \
|
||
|
} \
|
||
|
\
|
||
|
size = (uint8_t)ptr[tagbytes]; \
|
||
|
ptr += tagbytes + 1; \
|
||
|
dst->size = size; \
|
||
|
\
|
||
|
buf = d->arena.head.ptr; \
|
||
|
arena_has = _upb_ArenaHas(&d->arena); \
|
||
|
common_has = UPB_MIN(arena_has, (d->end - ptr) + 16); \
|
||
|
\
|
||
|
if (UPB_LIKELY(size <= 15 - tagbytes)) { \
|
||
|
if (arena_has < 16) goto longstr; \
|
||
|
d->arena.head.ptr += 16; \
|
||
|
memcpy(buf, ptr - tagbytes - 1, 16); \
|
||
|
dst->data = buf + tagbytes + 1; \
|
||
|
} else if (UPB_LIKELY(size <= 32)) { \
|
||
|
if (UPB_UNLIKELY(common_has < 32)) goto longstr; \
|
||
|
fastdecode_docopy(d, ptr, size, 32, buf, dst); \
|
||
|
} else if (UPB_LIKELY(size <= 64)) { \
|
||
|
if (UPB_UNLIKELY(common_has < 64)) goto longstr; \
|
||
|
fastdecode_docopy(d, ptr, size, 64, buf, dst); \
|
||
|
} else if (UPB_LIKELY(size < 128)) { \
|
||
|
if (UPB_UNLIKELY(common_has < 128)) goto longstr; \
|
||
|
fastdecode_docopy(d, ptr, size, 128, buf, dst); \
|
||
|
} else { \
|
||
|
goto longstr; \
|
||
|
} \
|
||
|
\
|
||
|
ptr += size; \
|
||
|
\
|
||
|
if (card == CARD_r) { \
|
||
2 years ago
|
if (validate_utf8 && \
|
||
|
!_upb_Decoder_VerifyUtf8Inline(dst->data, dst->size)) { \
|
||
|
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_BadUtf8); \
|
||
3 years ago
|
} \
|
||
|
fastdecode_nextret ret = fastdecode_nextrepeated( \
|
||
|
d, dst, &ptr, &farr, data, tagbytes, sizeof(upb_StringView)); \
|
||
|
switch (ret.next) { \
|
||
|
case FD_NEXT_SAMEFIELD: \
|
||
|
dst = ret.dst; \
|
||
|
goto again; \
|
||
|
case FD_NEXT_OTHERFIELD: \
|
||
|
data = ret.tag; \
|
||
2 years ago
|
UPB_MUSTTAIL return _upb_FastDecoder_TagDispatch(UPB_PARSE_ARGS); \
|
||
3 years ago
|
case FD_NEXT_ATLIMIT: \
|
||
|
return ptr; \
|
||
|
} \
|
||
|
} \
|
||
|
\
|
||
|
if (card != CARD_r && validate_utf8) { \
|
||
|
data = (uint64_t)dst; \
|
||
|
UPB_MUSTTAIL return fastdecode_verifyutf8(UPB_PARSE_ARGS); \
|
||
|
} \
|
||
|
\
|
||
|
UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS); \
|
||
|
\
|
||
|
longstr: \
|
||
|
if (card == CARD_r) { \
|
||
|
fastdecode_commitarr(dst + 1, &farr, sizeof(upb_StringView)); \
|
||
|
} \
|
||
|
ptr--; \
|
||
|
if (validate_utf8) { \
|
||
|
UPB_MUSTTAIL return fastdecode_longstring_utf8(d, ptr, msg, table, \
|
||
|
hasbits, (uint64_t)dst); \
|
||
|
} else { \
|
||
|
UPB_MUSTTAIL return fastdecode_longstring_noutf8(d, ptr, msg, table, \
|
||
|
hasbits, (uint64_t)dst); \
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
4 years ago
|
#define FASTDECODE_STRING(d, ptr, msg, table, hasbits, data, tagbytes, card, \
|
||
|
copyfunc, validate_utf8) \
|
||
3 years ago
|
upb_StringView* dst; \
|
||
4 years ago
|
fastdecode_arr farr; \
|
||
|
int64_t size; \
|
||
|
\
|
||
|
if (UPB_UNLIKELY(!fastdecode_checktag(data, tagbytes))) { \
|
||
|
RETURN_GENERIC("string field tag mismatch\n"); \
|
||
|
} \
|
||
|
\
|
||
3 years ago
|
if (UPB_UNLIKELY((d->options & kUpb_DecodeOption_AliasString) == 0)) { \
|
||
4 years ago
|
UPB_MUSTTAIL return copyfunc(UPB_PARSE_ARGS); \
|
||
|
} \
|
||
|
\
|
||
|
dst = fastdecode_getfield(d, ptr, msg, &data, &hasbits, &farr, \
|
||
3 years ago
|
sizeof(upb_StringView), card); \
|
||
4 years ago
|
\
|
||
|
again: \
|
||
|
if (card == CARD_r) { \
|
||
3 years ago
|
dst = fastdecode_resizearr(d, dst, &farr, sizeof(upb_StringView)); \
|
||
4 years ago
|
} \
|
||
|
\
|
||
|
size = (int8_t)ptr[tagbytes]; \
|
||
|
ptr += tagbytes + 1; \
|
||
|
dst->data = ptr; \
|
||
|
dst->size = size; \
|
||
|
\
|
||
|
if (UPB_UNLIKELY(fastdecode_boundscheck(ptr, size, d->end))) { \
|
||
|
ptr--; \
|
||
|
if (validate_utf8) { \
|
||
|
return fastdecode_longstring_utf8(d, ptr, msg, table, hasbits, \
|
||
|
(uint64_t)dst); \
|
||
|
} else { \
|
||
|
return fastdecode_longstring_noutf8(d, ptr, msg, table, hasbits, \
|
||
|
(uint64_t)dst); \
|
||
|
} \
|
||
|
} \
|
||
|
\
|
||
|
ptr += size; \
|
||
|
\
|
||
|
if (card == CARD_r) { \
|
||
2 years ago
|
if (validate_utf8 && \
|
||
|
!_upb_Decoder_VerifyUtf8Inline(dst->data, dst->size)) { \
|
||
|
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_BadUtf8); \
|
||
4 years ago
|
} \
|
||
|
fastdecode_nextret ret = fastdecode_nextrepeated( \
|
||
3 years ago
|
d, dst, &ptr, &farr, data, tagbytes, sizeof(upb_StringView)); \
|
||
4 years ago
|
switch (ret.next) { \
|
||
3 years ago
|
case FD_NEXT_SAMEFIELD: \
|
||
|
dst = ret.dst; \
|
||
3 years ago
|
if (UPB_UNLIKELY((d->options & kUpb_DecodeOption_AliasString) == 0)) { \
|
||
3 years ago
|
/* Buffer flipped and we can't alias any more. Bounce to */ \
|
||
|
/* copyfunc(), but via dispatch since we need to reload table */ \
|
||
|
/* data also. */ \
|
||
3 years ago
|
fastdecode_commitarr(dst, &farr, sizeof(upb_StringView)); \
|
||
3 years ago
|
data = ret.tag; \
|
||
2 years ago
|
UPB_MUSTTAIL return _upb_FastDecoder_TagDispatch(UPB_PARSE_ARGS); \
|
||
3 years ago
|
} \
|
||
|
goto again; \
|
||
|
case FD_NEXT_OTHERFIELD: \
|
||
4 years ago
|
data = ret.tag; \
|
||
2 years ago
|
UPB_MUSTTAIL return _upb_FastDecoder_TagDispatch(UPB_PARSE_ARGS); \
|
||
3 years ago
|
case FD_NEXT_ATLIMIT: \
|
||
|
return ptr; \
|
||
4 years ago
|
} \
|
||
|
} \
|
||
|
\
|
||
|
if (card != CARD_r && validate_utf8) { \
|
||
|
data = (uint64_t)dst; \
|
||
|
UPB_MUSTTAIL return fastdecode_verifyutf8(UPB_PARSE_ARGS); \
|
||
|
} \
|
||
|
\
|
||
4 years ago
|
UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS);
|
||
4 years ago
|
|
||
4 years ago
|
/* Generate all combinations:
|
||
4 years ago
|
* {p,c} x {s,o,r} x {s, b} x {1bt,2bt} */
|
||
4 years ago
|
|
||
4 years ago
|
#define s_VALIDATE true
|
||
|
#define b_VALIDATE false
|
||
|
|
||
4 years ago
|
#define F(card, tagbytes, type) \
|
||
|
UPB_NOINLINE \
|
||
3 years ago
|
const char* upb_c##card##type##_##tagbytes##bt(UPB_PARSE_PARAMS) { \
|
||
4 years ago
|
FASTDECODE_COPYSTRING(d, ptr, msg, table, hasbits, data, tagbytes, \
|
||
|
CARD_##card, type##_VALIDATE); \
|
||
|
} \
|
||
3 years ago
|
const char* upb_p##card##type##_##tagbytes##bt(UPB_PARSE_PARAMS) { \
|
||
4 years ago
|
FASTDECODE_STRING(d, ptr, msg, table, hasbits, data, tagbytes, \
|
||
|
CARD_##card, upb_c##card##type##_##tagbytes##bt, \
|
||
|
type##_VALIDATE); \
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
4 years ago
|
#define UTF8(card, tagbytes) \
|
||
|
F(card, tagbytes, s) \
|
||
|
F(card, tagbytes, b)
|
||
|
|
||
4 years ago
|
#define TAGBYTES(card) \
|
||
4 years ago
|
UTF8(card, 1) \
|
||
|
UTF8(card, 2)
|
||
4 years ago
|
|
||
4 years ago
|
TAGBYTES(s)
|
||
|
TAGBYTES(o)
|
||
|
TAGBYTES(r)
|
||
4 years ago
|
|
||
4 years ago
|
#undef s_VALIDATE
|
||
|
#undef b_VALIDATE
|
||
4 years ago
|
#undef F
|
||
|
#undef TAGBYTES
|
||
4 years ago
|
#undef FASTDECODE_LONGSTRING
|
||
|
#undef FASTDECODE_COPYSTRING
|
||
|
#undef FASTDECODE_STRING
|
||
4 years ago
|
|
||
4 years ago
|
/* message fields *************************************************************/
|
||
|
|
||
4 years ago
|
UPB_INLINE
|
||
3 years ago
|
upb_Message* decode_newmsg_ceil(upb_Decoder* d, const upb_MiniTable* l,
|
||
|
int msg_ceil_bytes) {
|
||
3 years ago
|
size_t size = l->size + sizeof(upb_Message_Internal);
|
||
|
char* msg_data;
|
||
4 years ago
|
if (UPB_LIKELY(msg_ceil_bytes > 0 &&
|
||
3 years ago
|
_upb_ArenaHas(&d->arena) >= msg_ceil_bytes)) {
|
||
4 years ago
|
UPB_ASSERT(size <= (size_t)msg_ceil_bytes);
|
||
|
msg_data = d->arena.head.ptr;
|
||
|
d->arena.head.ptr += size;
|
||
|
UPB_UNPOISON_MEMORY_REGION(msg_data, msg_ceil_bytes);
|
||
|
memset(msg_data, 0, msg_ceil_bytes);
|
||
|
UPB_POISON_MEMORY_REGION(msg_data + size, msg_ceil_bytes - size);
|
||
|
} else {
|
||
3 years ago
|
msg_data = (char*)upb_Arena_Malloc(&d->arena, size);
|
||
4 years ago
|
memset(msg_data, 0, size);
|
||
4 years ago
|
}
|
||
3 years ago
|
return msg_data + sizeof(upb_Message_Internal);
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
4 years ago
|
typedef struct {
|
||
4 years ago
|
intptr_t table;
|
||
3 years ago
|
upb_Message* msg;
|
||
4 years ago
|
} fastdecode_submsgdata;
|
||
|
|
||
|
UPB_FORCEINLINE
|
||
3 years ago
|
static const char* fastdecode_tosubmsg(upb_Decoder* d, const char* ptr,
|
||
|
void* ctx) {
|
||
|
fastdecode_submsgdata* submsg = ctx;
|
||
4 years ago
|
ptr = fastdecode_dispatch(d, ptr, submsg->msg, submsg->table, 0, 0);
|
||
4 years ago
|
UPB_ASSUME(ptr != NULL);
|
||
4 years ago
|
return ptr;
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
#define FASTDECODE_SUBMSG(d, ptr, msg, table, hasbits, data, tagbytes, \
|
||
|
msg_ceil_bytes, card) \
|
||
|
\
|
||
|
if (UPB_UNLIKELY(!fastdecode_checktag(data, tagbytes))) { \
|
||
|
RETURN_GENERIC("submessage field tag mismatch\n"); \
|
||
|
} \
|
||
|
\
|
||
3 years ago
|
if (--d->depth == 0) { \
|
||
2 years ago
|
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_MaxDepthExceeded); \
|
||
3 years ago
|
} \
|
||
4 years ago
|
\
|
||
3 years ago
|
upb_Message** dst; \
|
||
4 years ago
|
uint32_t submsg_idx = (data >> 16) & 0xff; \
|
||
3 years ago
|
const upb_MiniTable* tablep = decode_totablep(table); \
|
||
|
const upb_MiniTable* subtablep = tablep->subs[submsg_idx].submsg; \
|
||
4 years ago
|
fastdecode_submsgdata submsg = {decode_totable(subtablep)}; \
|
||
|
fastdecode_arr farr; \
|
||
|
\
|
||
|
if (subtablep->table_mask == (uint8_t)-1) { \
|
||
|
RETURN_GENERIC("submessage doesn't have fast tables."); \
|
||
|
} \
|
||
|
\
|
||
|
dst = fastdecode_getfield(d, ptr, msg, &data, &hasbits, &farr, \
|
||
3 years ago
|
sizeof(upb_Message*), card); \
|
||
4 years ago
|
\
|
||
|
if (card == CARD_s) { \
|
||
3 years ago
|
*(uint32_t*)msg |= hasbits; \
|
||
4 years ago
|
hasbits = 0; \
|
||
|
} \
|
||
|
\
|
||
|
again: \
|
||
|
if (card == CARD_r) { \
|
||
3 years ago
|
dst = fastdecode_resizearr(d, dst, &farr, sizeof(upb_Message*)); \
|
||
4 years ago
|
} \
|
||
|
\
|
||
|
submsg.msg = *dst; \
|
||
|
\
|
||
|
if (card == CARD_r || UPB_LIKELY(!submsg.msg)) { \
|
||
|
*dst = submsg.msg = decode_newmsg_ceil(d, subtablep, msg_ceil_bytes); \
|
||
|
} \
|
||
|
\
|
||
|
ptr += tagbytes; \
|
||
|
ptr = fastdecode_delimited(d, ptr, fastdecode_tosubmsg, &submsg); \
|
||
|
\
|
||
|
if (UPB_UNLIKELY(ptr == NULL || d->end_group != DECODE_NOGROUP)) { \
|
||
2 years ago
|
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed); \
|
||
4 years ago
|
} \
|
||
|
\
|
||
|
if (card == CARD_r) { \
|
||
|
fastdecode_nextret ret = fastdecode_nextrepeated( \
|
||
3 years ago
|
d, dst, &ptr, &farr, data, tagbytes, sizeof(upb_Message*)); \
|
||
4 years ago
|
switch (ret.next) { \
|
||
|
case FD_NEXT_SAMEFIELD: \
|
||
|
dst = ret.dst; \
|
||
|
goto again; \
|
||
|
case FD_NEXT_OTHERFIELD: \
|
||
|
d->depth++; \
|
||
|
data = ret.tag; \
|
||
2 years ago
|
UPB_MUSTTAIL return _upb_FastDecoder_TagDispatch(UPB_PARSE_ARGS); \
|
||
4 years ago
|
case FD_NEXT_ATLIMIT: \
|
||
|
d->depth++; \
|
||
|
return ptr; \
|
||
|
} \
|
||
|
} \
|
||
|
\
|
||
|
d->depth++; \
|
||
|
UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS);
|
||
|
|
||
|
#define F(card, tagbytes, size_ceil, ceil_arg) \
|
||
3 years ago
|
const char* upb_p##card##m_##tagbytes##bt_max##size_ceil##b( \
|
||
4 years ago
|
UPB_PARSE_PARAMS) { \
|
||
|
FASTDECODE_SUBMSG(d, ptr, msg, table, hasbits, data, tagbytes, ceil_arg, \
|
||
|
CARD_##card); \
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
4 years ago
|
#define SIZES(card, tagbytes) \
|
||
3 years ago
|
F(card, tagbytes, 64, 64) \
|
||
4 years ago
|
F(card, tagbytes, 128, 128) \
|
||
|
F(card, tagbytes, 192, 192) \
|
||
|
F(card, tagbytes, 256, 256) \
|
||
|
F(card, tagbytes, max, -1)
|
||
4 years ago
|
|
||
4 years ago
|
#define TAGBYTES(card) \
|
||
3 years ago
|
SIZES(card, 1) \
|
||
4 years ago
|
SIZES(card, 2)
|
||
4 years ago
|
|
||
4 years ago
|
TAGBYTES(s)
|
||
|
TAGBYTES(o)
|
||
|
TAGBYTES(r)
|
||
4 years ago
|
|
||
4 years ago
|
#undef TAGBYTES
|
||
|
#undef SIZES
|
||
|
#undef F
|
||
4 years ago
|
#undef FASTDECODE_SUBMSG
|
||
4 years ago
|
|
||
3 years ago
|
#endif /* UPB_FASTTABLE */
|