WPILibC++ 2027.0.0-alpha-3
Loading...
Searching...
No Matches
decoder.h
Go to the documentation of this file.
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 <setjmp.h>
17#include <stddef.h>
18#include <stdint.h>
19#include <string.h>
20
21#include "upb/mem/arena.h"
26#include "upb/wire/decode.h"
28#include "utf8_range.h"
29
30// Must be last.
31#include "upb/port/def.inc"
32
33#define DECODE_NOGROUP (uint32_t)-1
34
35typedef struct upb_Decoder {
38 upb_Message* original_msg; // Pointer to preserve data to
39 int depth; // Tracks recursion depth to bound stack usage.
40 uint32_t end_group; // field number of END_GROUP tag, else DECODE_NOGROUP.
41 uint16_t options;
44 union {
47 };
49 jmp_buf err;
50
51#ifndef NDEBUG
52 const char* debug_tagstart;
53 const char* debug_valstart;
54 char* trace_ptr;
55 char* trace_end;
56#endif
58
59UPB_INLINE const char* upb_Decoder_Init(upb_Decoder* d, const char* buf,
60 size_t size,
61 const upb_ExtensionRegistry* extreg,
62 int options, upb_Arena* arena,
63 char* trace_buf, size_t trace_size) {
64 upb_EpsCopyInputStream_Init(&d->input, &buf, size,
66
67 d->extreg = extreg;
70 d->options = (uint16_t)options;
71 d->missing_required = false;
73 d->message_is_done = false;
74#ifndef NDEBUG
75 d->trace_ptr = trace_buf;
76 d->trace_end = UPB_PTRADD(trace_buf, trace_size);
77#endif
78 if (trace_buf) *trace_buf = 0; // Null-terminate.
79
80 // Violating the encapsulation of the arena for performance reasons.
81 // This is a temporary arena that we swap into and swap out of when we are
82 // done. The temporary arena only needs to be able to handle allocation,
83 // not fuse or free, so it does not need many of the members to be initialized
84 // (particularly parent_or_count).
86 return buf;
87}
88
94
95// Trace events are used to trace the progress of the decoder.
96// Events:
97// 'D' Fast dispatch
98// 'F' Field successfully parsed fast.
99// '<' Fallback to MiniTable parser.
100// 'M' Field successfully parsed with MiniTable.
101// 'X' Truncated -- trace buffer is full, further events were discarded.
103#ifndef NDEBUG
104 if (d->trace_ptr == NULL) return;
105 if (d->trace_ptr == d->trace_end - 1) {
106 d->trace_ptr[-1] = 'X'; // Truncated.
107 return;
108 }
109 d->trace_ptr[0] = event;
110 d->trace_ptr[1] = '\0';
111 d->trace_ptr++;
112#endif
113}
114
116bool _upb_Decoder_VerifyUtf8Inline(const char* ptr, int len) {
117 return utf8_range_IsValid(ptr, len);
118}
119
120const char* _upb_Decoder_CheckRequired(upb_Decoder* d, const char* ptr,
121 const upb_Message* msg,
122 const upb_MiniTable* m);
123
124/* x86-64 pointers always have the high 16 bits matching. So we can shift
125 * left 8 and right 8 without loss of information. */
126UPB_INLINE intptr_t decode_totable(const upb_MiniTable* tablep) {
127 return ((intptr_t)tablep << 8) | tablep->UPB_PRIVATE(table_mask);
128}
129
131 return (const upb_MiniTable*)(table >> 8);
132}
133
135 const char* ptr, int overrun);
136
137const char* _upb_Decoder_DecodeMessage(upb_Decoder* d, const char* ptr,
138 upb_Message* msg,
139 const upb_MiniTable* layout);
140
145
147 upb_DecodeStatus status);
148
150 upb_EpsCopyInputStream* e, const char* old_end, const char* new_start) {
151 upb_Decoder* d = (upb_Decoder*)e;
153 return new_start;
154}
155
156#include "upb/port/undef.inc"
157
158#endif /* UPB_WIRE_INTERNAL_DECODER_H_ */
#define UPB_PTRADD(ptr, ofs)
Definition def.inc:391
#define UPB_PRIVATE(x)
Definition def.inc:393
#define UPB_NORETURN
Definition def.inc:290
#define UPB_INLINE
Definition def.inc:144
UPB_INLINE bool upb_EpsCopyInputStream_IsDoneWithCallback(upb_EpsCopyInputStream *e, const char **ptr, upb_EpsCopyInputStream_IsDoneFallbackFunc *func)
Definition eps_copy_input_stream.h:112
UPB_INLINE void upb_EpsCopyInputStream_Init(upb_EpsCopyInputStream *e, const char **ptr, size_t size, bool enable_aliasing)
Definition eps_copy_input_stream.h:58
struct upb_ExtensionRegistry upb_ExtensionRegistry
Definition extension_registry.h:59
auto ptr(T p) -> const void *
Converts p to const void* for pointer formatting.
Definition format.h:3963
void UPB_PRIVATE _upb_Arena_SwapOut(struct upb_Arena *des, const struct upb_Arena *src)
void UPB_PRIVATE _upb_Arena_SwapIn(struct upb_Arena *des, const struct upb_Arena *src)
#define UPB_ARENA_SIZE_HACK
Definition arena.h:25
Definition arena.h:29
Definition decoder.h:35
const char * debug_tagstart
Definition decoder.h:52
const upb_ExtensionRegistry * extreg
Definition decoder.h:37
void * foo[UPB_ARENA_SIZE_HACK]
Definition decoder.h:46
upb_DecodeStatus status
Definition decoder.h:48
upb_Arena arena
Definition decoder.h:45
char * trace_ptr
Definition decoder.h:54
const char * debug_valstart
Definition decoder.h:53
bool message_is_done
Definition decoder.h:43
char * trace_end
Definition decoder.h:55
upb_EpsCopyInputStream input
Definition decoder.h:36
uint16_t options
Definition decoder.h:41
bool missing_required
Definition decoder.h:42
int depth
Definition decoder.h:39
jmp_buf err
Definition decoder.h:49
upb_Message * original_msg
Definition decoder.h:38
uint32_t end_group
Definition decoder.h:40
Definition eps_copy_input_stream.h:31
Definition types.h:18
Definition message.h:54
const upb_MiniTableSubInternal * UPB_PRIVATE(subs)
int utf8_range_IsValid(const char *data, size_t len)
upb_DecodeStatus
Definition decode.h:119
@ kUpb_DecodeStatus_Ok
Definition decode.h:120
@ kUpb_DecodeStatus_Malformed
Definition decode.h:121
@ kUpb_DecodeOption_AliasString
Definition decode.h:32
uint16_t upb_DecodeOptions_GetEffectiveMaxDepth(uint32_t options)
UPB_INLINE intptr_t decode_totable(const upb_MiniTable *tablep)
Definition decoder.h:126
const char * _upb_Decoder_DecodeMessage(upb_Decoder *d, const char *ptr, upb_Message *msg, const upb_MiniTable *layout)
UPB_INLINE bool _upb_Decoder_VerifyUtf8Inline(const char *ptr, int len)
Definition decoder.h:116
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)
Definition decoder.h:141
const char * _upb_Decoder_CheckRequired(upb_Decoder *d, const char *ptr, const upb_Message *msg, const upb_MiniTable *m)
UPB_INLINE const upb_MiniTable * decode_totablep(intptr_t table)
Definition decoder.h:130
UPB_NORETURN void * _upb_Decoder_ErrorJmp(upb_Decoder *d, upb_DecodeStatus status)
UPB_INLINE const char * _upb_Decoder_BufferFlipCallback(upb_EpsCopyInputStream *e, const char *old_end, const char *new_start)
Definition decoder.h:149
#define DECODE_NOGROUP
Definition decoder.h:33
struct upb_Decoder upb_Decoder
UPB_INLINE const char * upb_Decoder_Init(upb_Decoder *d, const char *buf, size_t size, const upb_ExtensionRegistry *extreg, int options, upb_Arena *arena, char *trace_buf, size_t trace_size)
Definition decoder.h:59
UPB_INLINE void _upb_Decoder_Trace(upb_Decoder *d, char event)
Definition decoder.h:102
UPB_INLINE upb_DecodeStatus upb_Decoder_Destroy(upb_Decoder *d, upb_Arena *arena)
Definition decoder.h:89