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.
128 lines
4.2 KiB
128 lines
4.2 KiB
1 year ago
|
// Protocol Buffers - Google's data interchange format
|
||
|
// Copyright 2023 Google LLC. All rights reserved.
|
||
|
//
|
||
1 year ago
|
// Use of this source code is governed by a BSD-style
|
||
|
// license that can be found in the LICENSE file or at
|
||
|
// https://developers.google.com/open-source/licenses/bsd
|
||
2 years ago
|
|
||
|
/*
|
||
|
* Internal implementation details of the decoder that are shared between
|
||
|
* decode.c and decode_fast.c.
|
||
|
*/
|
||
|
|
||
11 months ago
|
#ifndef UPB_WIRE_INTERNAL_DECODER_H_
|
||
|
#define UPB_WIRE_INTERNAL_DECODER_H_
|
||
2 years ago
|
|
||
1 year ago
|
#include "upb/mem/internal/arena.h"
|
||
12 months ago
|
#include "upb/message/internal/message.h"
|
||
1 year ago
|
#include "upb/wire/decode.h"
|
||
|
#include "upb/wire/eps_copy_input_stream.h"
|
||
2 years ago
|
#include "utf8_range.h"
|
||
2 years ago
|
|
||
2 years ago
|
// Must be last.
|
||
1 year ago
|
#include "upb/port/def.inc"
|
||
2 years ago
|
|
||
|
#define DECODE_NOGROUP (uint32_t) - 1
|
||
|
|
||
|
typedef struct upb_Decoder {
|
||
2 years ago
|
upb_EpsCopyInputStream input;
|
||
|
const upb_ExtensionRegistry* extreg;
|
||
|
const char* unknown; // Start of unknown data, preserve at buffer flip
|
||
|
upb_Message* unknown_msg; // Pointer to preserve data to
|
||
|
int depth; // Tracks recursion depth to bound stack usage.
|
||
|
uint32_t end_group; // field number of END_GROUP tag, else DECODE_NOGROUP.
|
||
2 years ago
|
uint16_t options;
|
||
|
bool missing_required;
|
||
11 months ago
|
union {
|
||
|
upb_Arena arena;
|
||
|
void* foo[UPB_ARENA_SIZE_HACK];
|
||
|
};
|
||
2 years ago
|
upb_DecodeStatus status;
|
||
2 years ago
|
jmp_buf err;
|
||
|
|
||
|
#ifndef NDEBUG
|
||
|
const char* debug_tagstart;
|
||
|
const char* debug_valstart;
|
||
|
#endif
|
||
|
} upb_Decoder;
|
||
|
|
||
|
/* Error function that will abort decoding with longjmp(). We can't declare this
|
||
|
* UPB_NORETURN, even though it is appropriate, because if we do then compilers
|
||
|
* will "helpfully" refuse to tailcall to it
|
||
|
* (see: https://stackoverflow.com/a/55657013), which will defeat a major goal
|
||
|
* of our optimizations. That is also why we must declare it in a separate file,
|
||
|
* otherwise the compiler will see that it calls longjmp() and deduce that it is
|
||
|
* noreturn. */
|
||
2 years ago
|
const char* _upb_FastDecoder_ErrorJmp(upb_Decoder* d, int status);
|
||
2 years ago
|
|
||
|
extern const uint8_t upb_utf8_offsets[];
|
||
|
|
||
|
UPB_INLINE
|
||
2 years ago
|
bool _upb_Decoder_VerifyUtf8Inline(const char* ptr, int len) {
|
||
12 months ago
|
return utf8_range_IsValid(ptr, len);
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
const char* _upb_Decoder_CheckRequired(upb_Decoder* d, const char* ptr,
|
||
|
const upb_Message* msg,
|
||
1 year ago
|
const upb_MiniTable* m);
|
||
2 years ago
|
|
||
|
/* x86-64 pointers always have the high 16 bits matching. So we can shift
|
||
|
* left 8 and right 8 without loss of information. */
|
||
|
UPB_INLINE intptr_t decode_totable(const upb_MiniTable* tablep) {
|
||
1 year ago
|
return ((intptr_t)tablep << 8) | tablep->UPB_PRIVATE(table_mask);
|
||
2 years ago
|
}
|
||
|
|
||
|
UPB_INLINE const upb_MiniTable* decode_totablep(intptr_t table) {
|
||
|
return (const upb_MiniTable*)(table >> 8);
|
||
|
}
|
||
|
|
||
2 years ago
|
const char* _upb_Decoder_IsDoneFallback(upb_EpsCopyInputStream* e,
|
||
|
const char* ptr, int overrun);
|
||
|
|
||
|
UPB_INLINE bool _upb_Decoder_IsDone(upb_Decoder* d, const char** ptr) {
|
||
2 years ago
|
return upb_EpsCopyInputStream_IsDoneWithCallback(
|
||
|
&d->input, ptr, &_upb_Decoder_IsDoneFallback);
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
UPB_INLINE const char* _upb_Decoder_BufferFlipCallback(
|
||
|
upb_EpsCopyInputStream* e, const char* old_end, const char* new_start) {
|
||
|
upb_Decoder* d = (upb_Decoder*)e;
|
||
|
if (!old_end) _upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed);
|
||
2 years ago
|
|
||
2 years ago
|
if (d->unknown) {
|
||
12 months ago
|
if (!UPB_PRIVATE(_upb_Message_AddUnknown)(
|
||
|
d->unknown_msg, d->unknown, old_end - d->unknown, &d->arena)) {
|
||
2 years ago
|
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
|
||
|
}
|
||
|
d->unknown = new_start;
|
||
2 years ago
|
}
|
||
2 years ago
|
return new_start;
|
||
2 years ago
|
}
|
||
|
|
||
|
#if UPB_FASTTABLE
|
||
|
UPB_INLINE
|
||
2 years ago
|
const char* _upb_FastDecoder_TagDispatch(upb_Decoder* d, const char* ptr,
|
||
|
upb_Message* msg, intptr_t table,
|
||
|
uint64_t hasbits, uint64_t tag) {
|
||
2 years ago
|
const upb_MiniTable* table_p = decode_totablep(table);
|
||
|
uint8_t mask = table;
|
||
|
uint64_t data;
|
||
|
size_t idx = tag & mask;
|
||
|
UPB_ASSUME((idx & 7) == 0);
|
||
|
idx >>= 3;
|
||
1 year ago
|
data = table_p->UPB_PRIVATE(fasttable)[idx].field_data ^ tag;
|
||
|
UPB_MUSTTAIL return table_p->UPB_PRIVATE(fasttable)[idx].field_parser(
|
||
|
d, ptr, msg, table, hasbits, data);
|
||
2 years ago
|
}
|
||
|
#endif
|
||
|
|
||
2 years ago
|
UPB_INLINE uint32_t _upb_FastDecoder_LoadTag(const char* ptr) {
|
||
2 years ago
|
uint16_t tag;
|
||
|
memcpy(&tag, ptr, 2);
|
||
|
return tag;
|
||
|
}
|
||
|
|
||
1 year ago
|
#include "upb/port/undef.inc"
|
||
2 years ago
|
|
||
11 months ago
|
#endif /* UPB_WIRE_INTERNAL_DECODER_H_ */
|