1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 /*
9 * Internal implementation details of the decoder that are shared between
10 * decode.c and decode_fast.c.
11 */
12
13 #ifndef UPB_WIRE_INTERNAL_DECODER_H_
14 #define UPB_WIRE_INTERNAL_DECODER_H_
15
16 #include "upb/mem/internal/arena.h"
17 #include "upb/message/internal/message.h"
18 #include "upb/wire/decode.h"
19 #include "upb/wire/eps_copy_input_stream.h"
20 #include "utf8_range.h"
21
22 // Must be last.
23 #include "upb/port/def.inc"
24
25 #define DECODE_NOGROUP (uint32_t) - 1
26
27 typedef struct upb_Decoder {
28 upb_EpsCopyInputStream input;
29 const upb_ExtensionRegistry* extreg;
30 const char* unknown; // Start of unknown data, preserve at buffer flip
31 upb_Message* unknown_msg; // Pointer to preserve data to
32 int depth; // Tracks recursion depth to bound stack usage.
33 uint32_t end_group; // field number of END_GROUP tag, else DECODE_NOGROUP.
34 uint16_t options;
35 bool missing_required;
36 union {
37 upb_Arena arena;
38 void* foo[UPB_ARENA_SIZE_HACK];
39 };
40 upb_DecodeStatus status;
41 jmp_buf err;
42
43 #ifndef NDEBUG
44 const char* debug_tagstart;
45 const char* debug_valstart;
46 #endif
47 } upb_Decoder;
48
49 /* Error function that will abort decoding with longjmp(). We can't declare this
50 * UPB_NORETURN, even though it is appropriate, because if we do then compilers
51 * will "helpfully" refuse to tailcall to it
52 * (see: https://stackoverflow.com/a/55657013), which will defeat a major goal
53 * of our optimizations. That is also why we must declare it in a separate file,
54 * otherwise the compiler will see that it calls longjmp() and deduce that it is
55 * noreturn. */
56 const char* _upb_FastDecoder_ErrorJmp(upb_Decoder* d, int status);
57
58 extern const uint8_t upb_utf8_offsets[];
59
60 UPB_INLINE
_upb_Decoder_VerifyUtf8Inline(const char * ptr,int len)61 bool _upb_Decoder_VerifyUtf8Inline(const char* ptr, int len) {
62 return utf8_range_IsValid(ptr, len);
63 }
64
65 const char* _upb_Decoder_CheckRequired(upb_Decoder* d, const char* ptr,
66 const upb_Message* msg,
67 const upb_MiniTable* m);
68
69 /* x86-64 pointers always have the high 16 bits matching. So we can shift
70 * left 8 and right 8 without loss of information. */
decode_totable(const upb_MiniTable * tablep)71 UPB_INLINE intptr_t decode_totable(const upb_MiniTable* tablep) {
72 return ((intptr_t)tablep << 8) | tablep->UPB_PRIVATE(table_mask);
73 }
74
decode_totablep(intptr_t table)75 UPB_INLINE const upb_MiniTable* decode_totablep(intptr_t table) {
76 return (const upb_MiniTable*)(table >> 8);
77 }
78
79 const char* _upb_Decoder_IsDoneFallback(upb_EpsCopyInputStream* e,
80 const char* ptr, int overrun);
81
_upb_Decoder_IsDone(upb_Decoder * d,const char ** ptr)82 UPB_INLINE bool _upb_Decoder_IsDone(upb_Decoder* d, const char** ptr) {
83 return upb_EpsCopyInputStream_IsDoneWithCallback(
84 &d->input, ptr, &_upb_Decoder_IsDoneFallback);
85 }
86
_upb_Decoder_BufferFlipCallback(upb_EpsCopyInputStream * e,const char * old_end,const char * new_start)87 UPB_INLINE const char* _upb_Decoder_BufferFlipCallback(
88 upb_EpsCopyInputStream* e, const char* old_end, const char* new_start) {
89 upb_Decoder* d = (upb_Decoder*)e;
90 if (!old_end) _upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed);
91
92 if (d->unknown) {
93 if (!UPB_PRIVATE(_upb_Message_AddUnknown)(
94 d->unknown_msg, d->unknown, old_end - d->unknown, &d->arena)) {
95 _upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
96 }
97 d->unknown = new_start;
98 }
99 return new_start;
100 }
101
102 #if UPB_FASTTABLE
103 UPB_INLINE
_upb_FastDecoder_TagDispatch(upb_Decoder * d,const char * ptr,upb_Message * msg,intptr_t table,uint64_t hasbits,uint64_t tag)104 const char* _upb_FastDecoder_TagDispatch(upb_Decoder* d, const char* ptr,
105 upb_Message* msg, intptr_t table,
106 uint64_t hasbits, uint64_t tag) {
107 const upb_MiniTable* table_p = decode_totablep(table);
108 uint8_t mask = table;
109 uint64_t data;
110 size_t idx = tag & mask;
111 UPB_ASSUME((idx & 7) == 0);
112 idx >>= 3;
113 data = table_p->UPB_PRIVATE(fasttable)[idx].field_data ^ tag;
114 UPB_MUSTTAIL return table_p->UPB_PRIVATE(fasttable)[idx].field_parser(
115 d, ptr, msg, table, hasbits, data);
116 }
117 #endif
118
_upb_FastDecoder_LoadTag(const char * ptr)119 UPB_INLINE uint32_t _upb_FastDecoder_LoadTag(const char* ptr) {
120 uint16_t tag;
121 memcpy(&tag, ptr, 2);
122 return tag;
123 }
124
125 #include "upb/port/undef.inc"
126
127 #endif /* UPB_WIRE_INTERNAL_DECODER_H_ */
128