File indexing completed on 2026-07-13 08:34:21
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef UPB_WIRE_INTERNAL_DECODER_H_
0014 #define UPB_WIRE_INTERNAL_DECODER_H_
0015
0016 #include <setjmp.h>
0017 #include <stddef.h>
0018 #include <stdint.h>
0019 #include <string.h>
0020
0021 #include "upb/mem/arena.h"
0022 #include "upb/mem/internal/arena.h"
0023 #include "upb/mini_table/extension_registry.h"
0024 #include "upb/mini_table/internal/message.h"
0025 #include "upb/mini_table/message.h"
0026 #include "upb/wire/decode.h"
0027 #include "upb/wire/eps_copy_input_stream.h"
0028 #include "utf8_range.h"
0029
0030
0031 #include "upb/port/def.inc"
0032
0033 #define DECODE_NOGROUP (uint32_t)-1
0034
0035 typedef struct upb_Decoder {
0036 upb_EpsCopyInputStream input;
0037 const upb_ExtensionRegistry* extreg;
0038 upb_Message* original_msg;
0039 int depth;
0040 uint32_t end_group;
0041 uint16_t options;
0042 bool missing_required;
0043 bool message_is_done;
0044 union {
0045 upb_Arena arena;
0046 void* foo[UPB_ARENA_SIZE_HACK];
0047 };
0048 upb_DecodeStatus status;
0049 jmp_buf err;
0050
0051 #ifndef NDEBUG
0052 const char* debug_tagstart;
0053 const char* debug_valstart;
0054 char* trace_ptr;
0055 char* trace_end;
0056 #endif
0057 } upb_Decoder;
0058
0059 UPB_INLINE const char* upb_Decoder_Init(upb_Decoder* d, const char* buf,
0060 size_t size,
0061 const upb_ExtensionRegistry* extreg,
0062 int options, upb_Arena* arena,
0063 char* trace_buf, size_t trace_size) {
0064 upb_EpsCopyInputStream_Init(&d->input, &buf, size,
0065 options & kUpb_DecodeOption_AliasString);
0066
0067 d->extreg = extreg;
0068 d->depth = upb_DecodeOptions_GetEffectiveMaxDepth(options);
0069 d->end_group = DECODE_NOGROUP;
0070 d->options = (uint16_t)options;
0071 d->missing_required = false;
0072 d->status = kUpb_DecodeStatus_Ok;
0073 d->message_is_done = false;
0074 #ifndef NDEBUG
0075 d->trace_ptr = trace_buf;
0076 d->trace_end = UPB_PTRADD(trace_buf, trace_size);
0077 #endif
0078 if (trace_buf) *trace_buf = 0;
0079
0080
0081
0082
0083
0084
0085 UPB_PRIVATE(_upb_Arena_SwapIn)(&d->arena, arena);
0086 return buf;
0087 }
0088
0089 UPB_INLINE upb_DecodeStatus upb_Decoder_Destroy(upb_Decoder* d,
0090 upb_Arena* arena) {
0091 UPB_PRIVATE(_upb_Arena_SwapOut)(arena, &d->arena);
0092 return d->status;
0093 }
0094
0095
0096
0097
0098
0099
0100
0101
0102 UPB_INLINE void _upb_Decoder_Trace(upb_Decoder* d, char event) {
0103 #ifndef NDEBUG
0104 if (d->trace_ptr == NULL) return;
0105 if (d->trace_ptr == d->trace_end - 1) {
0106 d->trace_ptr[-1] = 'X';
0107 return;
0108 }
0109 d->trace_ptr[0] = event;
0110 d->trace_ptr[1] = '\0';
0111 d->trace_ptr++;
0112 #endif
0113 };
0114
0115 UPB_INLINE
0116 bool _upb_Decoder_VerifyUtf8Inline(const char* ptr, int len) {
0117 return utf8_range_IsValid(ptr, len);
0118 }
0119
0120 const char* _upb_Decoder_CheckRequired(upb_Decoder* d, const char* ptr,
0121 const upb_Message* msg,
0122 const upb_MiniTable* m);
0123
0124
0125
0126 UPB_INLINE intptr_t decode_totable(const upb_MiniTable* tablep) {
0127 return ((intptr_t)tablep << 8) | tablep->UPB_PRIVATE(table_mask);
0128 }
0129
0130 UPB_INLINE const upb_MiniTable* decode_totablep(intptr_t table) {
0131 return (const upb_MiniTable*)(table >> 8);
0132 }
0133
0134 const char* _upb_Decoder_IsDoneFallback(upb_EpsCopyInputStream* e,
0135 const char* ptr, int overrun);
0136
0137 const char* _upb_Decoder_DecodeMessage(upb_Decoder* d, const char* ptr,
0138 upb_Message* msg,
0139 const upb_MiniTable* layout);
0140
0141 UPB_INLINE bool _upb_Decoder_IsDone(upb_Decoder* d, const char** ptr) {
0142 return upb_EpsCopyInputStream_IsDoneWithCallback(
0143 &d->input, ptr, &_upb_Decoder_IsDoneFallback);
0144 }
0145
0146 UPB_NORETURN void* _upb_Decoder_ErrorJmp(upb_Decoder* d,
0147 upb_DecodeStatus status);
0148
0149 UPB_INLINE const char* _upb_Decoder_BufferFlipCallback(
0150 upb_EpsCopyInputStream* e, const char* old_end, const char* new_start) {
0151 upb_Decoder* d = (upb_Decoder*)e;
0152 if (!old_end) _upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed);
0153 return new_start;
0154 }
0155
0156 #include "upb/port/undef.inc"
0157
0158 #endif