Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-07-13 08:34:21

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2023 Google LLC.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 /*
0009  * Internal implementation details of the decoder that are shared between
0010  * decode.c and decode_fast.c.
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 // Must be last.
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;  // Pointer to preserve data to
0039   int depth;                  // Tracks recursion depth to bound stack usage.
0040   uint32_t end_group;  // field number of END_GROUP tag, else DECODE_NOGROUP.
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;  // Null-terminate.
0079 
0080   // Violating the encapsulation of the arena for performance reasons.
0081   // This is a temporary arena that we swap into and swap out of when we are
0082   // done.  The temporary arena only needs to be able to handle allocation,
0083   // not fuse or free, so it does not need many of the members to be initialized
0084   // (particularly parent_or_count).
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 // Trace events are used to trace the progress of the decoder.
0096 // Events:
0097 //   'D'  Fast dispatch
0098 //   'F'  Field successfully parsed fast.
0099 //   '<'  Fallback to MiniTable parser.
0100 //   'M'  Field successfully parsed with MiniTable.
0101 //   'X'  Truncated -- trace buffer is full, further events were discarded.
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';  // Truncated.
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 /* x86-64 pointers always have the high 16 bits matching. So we can shift
0125  * left 8 and right 8 without loss of information. */
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 /* UPB_WIRE_INTERNAL_DECODER_H_ */