1 /*
2  * Copyright (c) 2009-2022, 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"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20  * 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 #ifndef UPB_MINI_TABLE_DECODE_H_
29 #define UPB_MINI_TABLE_DECODE_H_
30 
31 #include "upb/base/status.h"
32 #include "upb/mem/arena.h"
33 #include "upb/mini_table/common.h"
34 
35 // Must be last.
36 #include "upb/port/def.inc"
37 
38 typedef enum {
39   kUpb_MiniTablePlatform_32Bit,
40   kUpb_MiniTablePlatform_64Bit,
41   kUpb_MiniTablePlatform_Native =
42       UPB_SIZE(kUpb_MiniTablePlatform_32Bit, kUpb_MiniTablePlatform_64Bit),
43 } upb_MiniTablePlatform;
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 // Builds a mini table from the data encoded in the buffer [data, len]. If any
50 // errors occur, returns NULL and sets a status message. In the success case,
51 // the caller must call upb_MiniTable_SetSub*() for all message or proto2 enum
52 // fields to link the table to the appropriate sub-tables.
53 upb_MiniTable* _upb_MiniTable_Build(const char* data, size_t len,
54                                     upb_MiniTablePlatform platform,
55                                     upb_Arena* arena, upb_Status* status);
56 
upb_MiniTable_Build(const char * data,size_t len,upb_Arena * arena,upb_Status * status)57 UPB_API_INLINE upb_MiniTable* upb_MiniTable_Build(const char* data, size_t len,
58                                                   upb_Arena* arena,
59                                                   upb_Status* status) {
60   return _upb_MiniTable_Build(data, len, kUpb_MiniTablePlatform_Native, arena,
61                               status);
62 }
63 
64 // Links a sub-message field to a MiniTable for that sub-message. If a
65 // sub-message field is not linked, it will be treated as an unknown field
66 // during parsing, and setting the field will not be allowed. It is possible
67 // to link the message field later, at which point it will no longer be treated
68 // as unknown. However there is no synchronization for this operation, which
69 // means parallel mutation requires external synchronization.
70 // Returns success/failure.
71 UPB_API bool upb_MiniTable_SetSubMessage(upb_MiniTable* table,
72                                          upb_MiniTableField* field,
73                                          const upb_MiniTable* sub);
74 
75 // Links an enum field to a MiniTable for that enum.
76 // All enum fields must be linked prior to parsing.
77 // Returns success/failure.
78 UPB_API bool upb_MiniTable_SetSubEnum(upb_MiniTable* table,
79                                       upb_MiniTableField* field,
80                                       const upb_MiniTableEnum* sub);
81 
82 // Initializes a MiniTableExtension buffer that has already been allocated.
83 // This is needed by upb_FileDef and upb_MessageDef, which allocate all of the
84 // extensions together in a single contiguous array.
85 const char* _upb_MiniTableExtension_Init(const char* data, size_t len,
86                                          upb_MiniTableExtension* ext,
87                                          const upb_MiniTable* extendee,
88                                          upb_MiniTableSub sub,
89                                          upb_MiniTablePlatform platform,
90                                          upb_Status* status);
91 
upb_MiniTableExtension_Init(const char * data,size_t len,upb_MiniTableExtension * ext,const upb_MiniTable * extendee,upb_MiniTableSub sub,upb_Status * status)92 UPB_API_INLINE const char* upb_MiniTableExtension_Init(
93     const char* data, size_t len, upb_MiniTableExtension* ext,
94     const upb_MiniTable* extendee, upb_MiniTableSub sub, upb_Status* status) {
95   return _upb_MiniTableExtension_Init(data, len, ext, extendee, sub,
96                                       kUpb_MiniTablePlatform_Native, status);
97 }
98 
99 UPB_API upb_MiniTableExtension* _upb_MiniTableExtension_Build(
100     const char* data, size_t len, const upb_MiniTable* extendee,
101     upb_MiniTableSub sub, upb_MiniTablePlatform platform, upb_Arena* arena,
102     upb_Status* status);
103 
upb_MiniTableExtension_Build(const char * data,size_t len,const upb_MiniTable * extendee,upb_Arena * arena,upb_Status * status)104 UPB_API_INLINE upb_MiniTableExtension* upb_MiniTableExtension_Build(
105     const char* data, size_t len, const upb_MiniTable* extendee,
106     upb_Arena* arena, upb_Status* status) {
107   upb_MiniTableSub sub;
108   sub.submsg = NULL;
109   return _upb_MiniTableExtension_Build(
110       data, len, extendee, sub, kUpb_MiniTablePlatform_Native, arena, status);
111 }
112 
upb_MiniTableExtension_BuildMessage(const char * data,size_t len,const upb_MiniTable * extendee,upb_MiniTable * submsg,upb_Arena * arena,upb_Status * status)113 UPB_API_INLINE upb_MiniTableExtension* upb_MiniTableExtension_BuildMessage(
114     const char* data, size_t len, const upb_MiniTable* extendee,
115     upb_MiniTable* submsg, upb_Arena* arena, upb_Status* status) {
116   upb_MiniTableSub sub;
117   sub.submsg = submsg;
118   return _upb_MiniTableExtension_Build(
119       data, len, extendee, sub, kUpb_MiniTablePlatform_Native, arena, status);
120 }
121 
upb_MiniTableExtension_BuildEnum(const char * data,size_t len,const upb_MiniTable * extendee,upb_MiniTableEnum * subenum,upb_Arena * arena,upb_Status * status)122 UPB_API_INLINE upb_MiniTableExtension* upb_MiniTableExtension_BuildEnum(
123     const char* data, size_t len, const upb_MiniTable* extendee,
124     upb_MiniTableEnum* subenum, upb_Arena* arena, upb_Status* status) {
125   upb_MiniTableSub sub;
126   sub.subenum = subenum;
127   return _upb_MiniTableExtension_Build(
128       data, len, extendee, sub, kUpb_MiniTablePlatform_Native, arena, status);
129 }
130 
131 UPB_API upb_MiniTableEnum* upb_MiniTableEnum_Build(const char* data, size_t len,
132                                                    upb_Arena* arena,
133                                                    upb_Status* status);
134 
135 // Like upb_MiniTable_Build(), but the user provides a buffer of layout data so
136 // it can be reused from call to call, avoiding repeated realloc()/free().
137 //
138 // The caller owns `*buf` both before and after the call, and must free() it
139 // when it is no longer in use.  The function will realloc() `*buf` as
140 // necessary, updating `*size` accordingly.
141 upb_MiniTable* upb_MiniTable_BuildWithBuf(const char* data, size_t len,
142                                           upb_MiniTablePlatform platform,
143                                           upb_Arena* arena, void** buf,
144                                           size_t* buf_size, upb_Status* status);
145 
146 // Returns a list of fields that require linking at runtime, to connect the
147 // MiniTable to its sub-messages and sub-enums.  The list of fields will be
148 // written to the `subs` array, which must have been allocated by the caller
149 // and must be large enough to hold a list of all fields in the message.
150 //
151 // The order of the fields returned by this function is significant: it matches
152 // the order expected by upb_MiniTable_Link() below.
153 //
154 // The return value packs the sub-message count and sub-enum count into a single
155 // integer like so:
156 //  return (msg_count << 16) | enum_count;
157 UPB_API uint32_t upb_MiniTable_GetSubList(const upb_MiniTable* mt,
158                                           const upb_MiniTableField** subs);
159 
160 // Links a message to its sub-messages and sub-enums.  The caller must pass
161 // arrays of sub-tables and sub-enums, in the same length and order as is
162 // returned by upb_MiniTable_GetSubList() above.  However, individual elements
163 // of the sub_tables may be NULL if those sub-messages were tree shaken.
164 //
165 // Returns false if either array is too short, or if any of the tables fails
166 // to link.
167 UPB_API bool upb_MiniTable_Link(upb_MiniTable* mt,
168                                 const upb_MiniTable** sub_tables,
169                                 size_t sub_table_count,
170                                 const upb_MiniTableEnum** sub_enums,
171                                 size_t sub_enum_count);
172 
173 #ifdef __cplusplus
174 } /* extern "C" */
175 #endif
176 
177 #include "upb/port/undef.inc"
178 
179 #endif /* UPB_MINI_TABLE_DECODE_H_ */
180