1 /*
2  * Copyright (c) 2009-2021, Google LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Google LLC nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 // These are the specialized field parser functions for the fast parser.
29 // Generated tables will refer to these by name.
30 //
31 // The function names are encoded with names like:
32 //
33 //   //  123 4
34 //   upb_pss_1bt();   // Parse singular string, 1 byte tag.
35 //
36 // In position 1:
37 //   - 'p' for parse, most function use this
38 //   - 'c' for copy, for when we are copying strings instead of aliasing
39 //
40 // In position 2 (cardinality):
41 //   - 's' for singular, with or without hasbit
42 //   - 'o' for oneof
43 //   - 'r' for non-packed repeated
44 //   - 'p' for packed repeated
45 //
46 // In position 3 (type):
47 //   - 'b1' for bool
48 //   - 'v4' for 4-byte varint
49 //   - 'v8' for 8-byte varint
50 //   - 'z4' for zig-zag-encoded 4-byte varint
51 //   - 'z8' for zig-zag-encoded 8-byte varint
52 //   - 'f4' for 4-byte fixed
53 //   - 'f8' for 8-byte fixed
54 //   - 'm' for sub-message
55 //   - 's' for string (validate UTF-8)
56 //   - 'b' for bytes
57 //
58 // In position 4 (tag length):
59 //   - '1' for one-byte tags (field numbers 1-15)
60 //   - '2' for two-byte tags (field numbers 16-2048)
61 
62 #ifndef UPB_DECODE_FAST_H_
63 #define UPB_DECODE_FAST_H_
64 
65 #include "upb/msg.h"
66 
67 struct upb_decstate;
68 
69 // The fallback, generic parsing function that can handle any field type.
70 // This just uses the regular (non-fast) parser to parse a single field.
71 const char *fastdecode_generic(struct upb_decstate *d, const char *ptr,
72                                upb_msg *msg, intptr_t table, uint64_t hasbits,
73                                uint64_t data);
74 
75 #define UPB_PARSE_PARAMS                                                 \
76   struct upb_decstate *d, const char *ptr, upb_msg *msg, intptr_t table, \
77       uint64_t hasbits, uint64_t data
78 
79 /* primitive fields ***********************************************************/
80 
81 #define F(card, type, valbytes, tagbytes) \
82   const char *upb_p##card##type##valbytes##_##tagbytes##bt(UPB_PARSE_PARAMS);
83 
84 #define TYPES(card, tagbytes) \
85   F(card, b, 1, tagbytes)     \
86   F(card, v, 4, tagbytes)     \
87   F(card, v, 8, tagbytes)     \
88   F(card, z, 4, tagbytes)     \
89   F(card, z, 8, tagbytes)     \
90   F(card, f, 4, tagbytes)     \
91   F(card, f, 8, tagbytes)
92 
93 #define TAGBYTES(card) \
94   TYPES(card, 1)       \
95   TYPES(card, 2)
96 
97 TAGBYTES(s)
98 TAGBYTES(o)
99 TAGBYTES(r)
100 TAGBYTES(p)
101 
102 #undef F
103 #undef TYPES
104 #undef TAGBYTES
105 
106 /* string fields **************************************************************/
107 
108 #define F(card, tagbytes, type)                                     \
109   const char *upb_p##card##type##_##tagbytes##bt(UPB_PARSE_PARAMS); \
110   const char *upb_c##card##type##_##tagbytes##bt(UPB_PARSE_PARAMS);
111 
112 #define UTF8(card, tagbytes) \
113   F(card, tagbytes, s)       \
114   F(card, tagbytes, b)
115 
116 #define TAGBYTES(card) \
117   UTF8(card, 1)        \
118   UTF8(card, 2)
119 
120 TAGBYTES(s)
121 TAGBYTES(o)
122 TAGBYTES(r)
123 
124 #undef F
125 #undef TAGBYTES
126 
127 /* sub-message fields *********************************************************/
128 
129 #define F(card, tagbytes, size_ceil, ceil_arg) \
130   const char *upb_p##card##m_##tagbytes##bt_max##size_ceil##b(UPB_PARSE_PARAMS);
131 
132 #define SIZES(card, tagbytes) \
133   F(card, tagbytes, 64, 64) \
134   F(card, tagbytes, 128, 128) \
135   F(card, tagbytes, 192, 192) \
136   F(card, tagbytes, 256, 256) \
137   F(card, tagbytes, max, -1)
138 
139 #define TAGBYTES(card) \
140   SIZES(card, 1) \
141   SIZES(card, 2)
142 
143 TAGBYTES(s)
144 TAGBYTES(o)
145 TAGBYTES(r)
146 
147 #undef TAGBYTES
148 #undef SIZES
149 #undef F
150 
151 #undef UPB_PARSE_PARAMS
152 
153 #endif  /* UPB_DECODE_FAST_H_ */
154