xref: /aosp_15_r20/external/grpc-grpc/third_party/upb/upb/wire/internal/decode_fast.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 // These are the specialized field parser functions for the fast parser.
9 // Generated tables will refer to these by name.
10 //
11 // The function names are encoded with names like:
12 //
13 //   //  123 4
14 //   upb_pss_1bt();   // Parse singular string, 1 byte tag.
15 //
16 // In position 1:
17 //   - 'p' for parse, most function use this
18 //   - 'c' for copy, for when we are copying strings instead of aliasing
19 //
20 // In position 2 (cardinality):
21 //   - 's' for singular, with or without hasbit
22 //   - 'o' for oneof
23 //   - 'r' for non-packed repeated
24 //   - 'p' for packed repeated
25 //
26 // In position 3 (type):
27 //   - 'b1' for bool
28 //   - 'v4' for 4-byte varint
29 //   - 'v8' for 8-byte varint
30 //   - 'z4' for zig-zag-encoded 4-byte varint
31 //   - 'z8' for zig-zag-encoded 8-byte varint
32 //   - 'f4' for 4-byte fixed
33 //   - 'f8' for 8-byte fixed
34 //   - 'm' for sub-message
35 //   - 's' for string (validate UTF-8)
36 //   - 'b' for bytes
37 //
38 // In position 4 (tag length):
39 //   - '1' for one-byte tags (field numbers 1-15)
40 //   - '2' for two-byte tags (field numbers 16-2048)
41 
42 #ifndef UPB_WIRE_INTERNAL_DECODE_FAST_H_
43 #define UPB_WIRE_INTERNAL_DECODE_FAST_H_
44 
45 #include "upb/message/message.h"
46 
47 // Must be last.
48 #include "upb/port/def.inc"
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
54 struct upb_Decoder;
55 
56 // The fallback, generic parsing function that can handle any field type.
57 // This just uses the regular (non-fast) parser to parse a single field.
58 const char* _upb_FastDecoder_DecodeGeneric(struct upb_Decoder* d,
59                                            const char* ptr, upb_Message* msg,
60                                            intptr_t table, uint64_t hasbits,
61                                            uint64_t data);
62 
63 #define UPB_PARSE_PARAMS                                                    \
64   struct upb_Decoder *d, const char *ptr, upb_Message *msg, intptr_t table, \
65       uint64_t hasbits, uint64_t data
66 
67 /* primitive fields ***********************************************************/
68 
69 #define F(card, type, valbytes, tagbytes) \
70   const char* upb_p##card##type##valbytes##_##tagbytes##bt(UPB_PARSE_PARAMS);
71 
72 #define TYPES(card, tagbytes) \
73   F(card, b, 1, tagbytes)     \
74   F(card, v, 4, tagbytes)     \
75   F(card, v, 8, tagbytes)     \
76   F(card, z, 4, tagbytes)     \
77   F(card, z, 8, tagbytes)     \
78   F(card, f, 4, tagbytes)     \
79   F(card, f, 8, tagbytes)
80 
81 #define TAGBYTES(card) \
82   TYPES(card, 1)       \
83   TYPES(card, 2)
84 
85 TAGBYTES(s)
86 TAGBYTES(o)
87 TAGBYTES(r)
88 TAGBYTES(p)
89 
90 #undef F
91 #undef TYPES
92 #undef TAGBYTES
93 
94 /* string fields **************************************************************/
95 
96 #define F(card, tagbytes, type)                                     \
97   const char* upb_p##card##type##_##tagbytes##bt(UPB_PARSE_PARAMS); \
98   const char* upb_c##card##type##_##tagbytes##bt(UPB_PARSE_PARAMS);
99 
100 #define UTF8(card, tagbytes) \
101   F(card, tagbytes, s)       \
102   F(card, tagbytes, b)
103 
104 #define TAGBYTES(card) \
105   UTF8(card, 1)        \
106   UTF8(card, 2)
107 
108 TAGBYTES(s)
109 TAGBYTES(o)
110 TAGBYTES(r)
111 
112 #undef F
113 #undef UTF8
114 #undef TAGBYTES
115 
116 /* sub-message fields *********************************************************/
117 
118 #define F(card, tagbytes, size_ceil, ceil_arg) \
119   const char* upb_p##card##m_##tagbytes##bt_max##size_ceil##b(UPB_PARSE_PARAMS);
120 
121 #define SIZES(card, tagbytes) \
122   F(card, tagbytes, 64, 64)   \
123   F(card, tagbytes, 128, 128) \
124   F(card, tagbytes, 192, 192) \
125   F(card, tagbytes, 256, 256) \
126   F(card, tagbytes, max, -1)
127 
128 #define TAGBYTES(card) \
129   SIZES(card, 1)       \
130   SIZES(card, 2)
131 
132 TAGBYTES(s)
133 TAGBYTES(o)
134 TAGBYTES(r)
135 
136 #undef F
137 #undef SIZES
138 #undef TAGBYTES
139 
140 #undef UPB_PARSE_PARAMS
141 
142 #ifdef __cplusplus
143 } /* extern "C" */
144 #endif
145 
146 #include "upb/port/undef.inc"
147 
148 #endif /* UPB_WIRE_INTERNAL_DECODE_FAST_H_ */
149