1 
2 #include "upb/reflection.h"
3 
4 #include <string.h>
5 #include "upb/table.int.h"
6 #include "upb/msg.h"
7 
8 #include "upb/port_def.inc"
9 
get_field_size(const upb_msglayout_field * f)10 static size_t get_field_size(const upb_msglayout_field *f) {
11   static unsigned char sizes[] = {
12     0,/* 0 */
13     8, /* UPB_DESCRIPTOR_TYPE_DOUBLE */
14     4, /* UPB_DESCRIPTOR_TYPE_FLOAT */
15     8, /* UPB_DESCRIPTOR_TYPE_INT64 */
16     8, /* UPB_DESCRIPTOR_TYPE_UINT64 */
17     4, /* UPB_DESCRIPTOR_TYPE_INT32 */
18     8, /* UPB_DESCRIPTOR_TYPE_FIXED64 */
19     4, /* UPB_DESCRIPTOR_TYPE_FIXED32 */
20     1, /* UPB_DESCRIPTOR_TYPE_BOOL */
21     sizeof(upb_strview), /* UPB_DESCRIPTOR_TYPE_STRING */
22     sizeof(void*), /* UPB_DESCRIPTOR_TYPE_GROUP */
23     sizeof(void*), /* UPB_DESCRIPTOR_TYPE_MESSAGE */
24     sizeof(upb_strview), /* UPB_DESCRIPTOR_TYPE_BYTES */
25     4, /* UPB_DESCRIPTOR_TYPE_UINT32 */
26     4, /* UPB_DESCRIPTOR_TYPE_ENUM */
27     4, /* UPB_DESCRIPTOR_TYPE_SFIXED32 */
28     8, /* UPB_DESCRIPTOR_TYPE_SFIXED64 */
29     4, /* UPB_DESCRIPTOR_TYPE_SINT32 */
30     8, /* UPB_DESCRIPTOR_TYPE_SINT64 */
31   };
32   return _upb_repeated_or_map(f) ? sizeof(void *) : sizes[f->descriptortype];
33 }
34 
35 /* Strings/bytes are special-cased in maps. */
36 static char _upb_fieldtype_to_mapsize[12] = {
37   0,
38   1,  /* UPB_TYPE_BOOL */
39   4,  /* UPB_TYPE_FLOAT */
40   4,  /* UPB_TYPE_INT32 */
41   4,  /* UPB_TYPE_UINT32 */
42   4,  /* UPB_TYPE_ENUM */
43   sizeof(void*),  /* UPB_TYPE_MESSAGE */
44   8,  /* UPB_TYPE_DOUBLE */
45   8,  /* UPB_TYPE_INT64 */
46   8,  /* UPB_TYPE_UINT64 */
47   0,  /* UPB_TYPE_STRING */
48   0,  /* UPB_TYPE_BYTES */
49 };
50 
51 static const char _upb_fieldtype_to_sizelg2[12] = {
52   0,
53   0,  /* UPB_TYPE_BOOL */
54   2,  /* UPB_TYPE_FLOAT */
55   2,  /* UPB_TYPE_INT32 */
56   2,  /* UPB_TYPE_UINT32 */
57   2,  /* UPB_TYPE_ENUM */
58   UPB_SIZE(2, 3),  /* UPB_TYPE_MESSAGE */
59   3,  /* UPB_TYPE_DOUBLE */
60   3,  /* UPB_TYPE_INT64 */
61   3,  /* UPB_TYPE_UINT64 */
62   UPB_SIZE(3, 4),  /* UPB_TYPE_STRING */
63   UPB_SIZE(3, 4),  /* UPB_TYPE_BYTES */
64 };
65 
66 /** upb_msg *******************************************************************/
67 
upb_msg_new(const upb_msgdef * m,upb_arena * a)68 upb_msg *upb_msg_new(const upb_msgdef *m, upb_arena *a) {
69   return _upb_msg_new(upb_msgdef_layout(m), a);
70 }
71 
in_oneof(const upb_msglayout_field * field)72 static bool in_oneof(const upb_msglayout_field *field) {
73   return field->presence < 0;
74 }
75 
_upb_msg_getraw(const upb_msg * msg,const upb_fielddef * f)76 static upb_msgval _upb_msg_getraw(const upb_msg *msg, const upb_fielddef *f) {
77   const upb_msglayout_field *field = upb_fielddef_layout(f);
78   const char *mem = UPB_PTR_AT(msg, field->offset, char);
79   upb_msgval val = {0};
80   memcpy(&val, mem, get_field_size(field));
81   return val;
82 }
83 
upb_msg_has(const upb_msg * msg,const upb_fielddef * f)84 bool upb_msg_has(const upb_msg *msg, const upb_fielddef *f) {
85   const upb_msglayout_field *field = upb_fielddef_layout(f);
86   if (in_oneof(field)) {
87     return _upb_getoneofcase_field(msg, field) == field->number;
88   } else if (field->presence > 0) {
89     return _upb_hasbit_field(msg, field);
90   } else {
91     UPB_ASSERT(field->descriptortype == UPB_DESCRIPTOR_TYPE_MESSAGE ||
92                field->descriptortype == UPB_DESCRIPTOR_TYPE_GROUP);
93     return _upb_msg_getraw(msg, f).msg_val != NULL;
94   }
95 }
96 
upb_msg_whichoneof(const upb_msg * msg,const upb_oneofdef * o)97 const upb_fielddef *upb_msg_whichoneof(const upb_msg *msg,
98                                        const upb_oneofdef *o) {
99   const upb_fielddef *f = upb_oneofdef_field(o, 0);
100   if (upb_oneofdef_issynthetic(o)) {
101     UPB_ASSERT(upb_oneofdef_fieldcount(o) == 1);
102     return upb_msg_has(msg, f) ? f : NULL;
103   } else {
104     const upb_msglayout_field *field = upb_fielddef_layout(f);
105     uint32_t oneof_case = _upb_getoneofcase_field(msg, field);
106     f = oneof_case ? upb_oneofdef_itof(o, oneof_case) : NULL;
107     UPB_ASSERT((f != NULL) == (oneof_case != 0));
108     return f;
109   }
110 }
111 
upb_msg_get(const upb_msg * msg,const upb_fielddef * f)112 upb_msgval upb_msg_get(const upb_msg *msg, const upb_fielddef *f) {
113   if (!upb_fielddef_haspresence(f) || upb_msg_has(msg, f)) {
114     return _upb_msg_getraw(msg, f);
115   } else {
116     /* TODO(haberman): change upb_fielddef to not require this switch(). */
117     upb_msgval val = {0};
118     switch (upb_fielddef_type(f)) {
119       case UPB_TYPE_INT32:
120       case UPB_TYPE_ENUM:
121         val.int32_val = upb_fielddef_defaultint32(f);
122         break;
123       case UPB_TYPE_INT64:
124         val.int64_val = upb_fielddef_defaultint64(f);
125         break;
126       case UPB_TYPE_UINT32:
127         val.uint32_val = upb_fielddef_defaultuint32(f);
128         break;
129       case UPB_TYPE_UINT64:
130         val.uint64_val = upb_fielddef_defaultuint64(f);
131         break;
132       case UPB_TYPE_FLOAT:
133         val.float_val = upb_fielddef_defaultfloat(f);
134         break;
135       case UPB_TYPE_DOUBLE:
136         val.double_val = upb_fielddef_defaultdouble(f);
137         break;
138       case UPB_TYPE_BOOL:
139         val.bool_val = upb_fielddef_defaultbool(f);
140         break;
141       case UPB_TYPE_STRING:
142       case UPB_TYPE_BYTES:
143         val.str_val.data = upb_fielddef_defaultstr(f, &val.str_val.size);
144         break;
145       case UPB_TYPE_MESSAGE:
146         val.msg_val = NULL;
147         break;
148     }
149     return val;
150   }
151 }
152 
upb_msg_mutable(upb_msg * msg,const upb_fielddef * f,upb_arena * a)153 upb_mutmsgval upb_msg_mutable(upb_msg *msg, const upb_fielddef *f,
154                               upb_arena *a) {
155   const upb_msglayout_field *field = upb_fielddef_layout(f);
156   upb_mutmsgval ret;
157   char *mem = UPB_PTR_AT(msg, field->offset, char);
158   bool wrong_oneof =
159       in_oneof(field) && _upb_getoneofcase_field(msg, field) != field->number;
160 
161   memcpy(&ret, mem, sizeof(void*));
162 
163   if (a && (!ret.msg || wrong_oneof)) {
164     if (upb_fielddef_ismap(f)) {
165       const upb_msgdef *entry = upb_fielddef_msgsubdef(f);
166       const upb_fielddef *key = upb_msgdef_itof(entry, UPB_MAPENTRY_KEY);
167       const upb_fielddef *value = upb_msgdef_itof(entry, UPB_MAPENTRY_VALUE);
168       ret.map = upb_map_new(a, upb_fielddef_type(key), upb_fielddef_type(value));
169     } else if (upb_fielddef_isseq(f)) {
170       ret.array = upb_array_new(a, upb_fielddef_type(f));
171     } else {
172       UPB_ASSERT(upb_fielddef_issubmsg(f));
173       ret.msg = upb_msg_new(upb_fielddef_msgsubdef(f), a);
174     }
175 
176     memcpy(mem, &ret, sizeof(void*));
177 
178     if (wrong_oneof) {
179       *_upb_oneofcase_field(msg, field) = field->number;
180     } else if (field->presence > 0) {
181       _upb_sethas_field(msg, field);
182     }
183   }
184   return ret;
185 }
186 
upb_msg_set(upb_msg * msg,const upb_fielddef * f,upb_msgval val,upb_arena * a)187 void upb_msg_set(upb_msg *msg, const upb_fielddef *f, upb_msgval val,
188                  upb_arena *a) {
189   const upb_msglayout_field *field = upb_fielddef_layout(f);
190   char *mem = UPB_PTR_AT(msg, field->offset, char);
191   UPB_UNUSED(a);  /* We reserve the right to make set insert into a map. */
192   memcpy(mem, &val, get_field_size(field));
193   if (field->presence > 0) {
194     _upb_sethas_field(msg, field);
195   } else if (in_oneof(field)) {
196     *_upb_oneofcase_field(msg, field) = field->number;
197   }
198 }
199 
upb_msg_clearfield(upb_msg * msg,const upb_fielddef * f)200 void upb_msg_clearfield(upb_msg *msg, const upb_fielddef *f) {
201   const upb_msglayout_field *field = upb_fielddef_layout(f);
202   char *mem = UPB_PTR_AT(msg, field->offset, char);
203 
204   if (field->presence > 0) {
205     _upb_clearhas_field(msg, field);
206   } else if (in_oneof(field)) {
207     uint32_t *oneof_case = _upb_oneofcase_field(msg, field);
208     if (*oneof_case != field->number) return;
209     *oneof_case = 0;
210   }
211 
212   memset(mem, 0, get_field_size(field));
213 }
214 
upb_msg_clear(upb_msg * msg,const upb_msgdef * m)215 void upb_msg_clear(upb_msg *msg, const upb_msgdef *m) {
216   _upb_msg_clear(msg, upb_msgdef_layout(m));
217 }
218 
upb_msg_next(const upb_msg * msg,const upb_msgdef * m,const upb_symtab * ext_pool,const upb_fielddef ** out_f,upb_msgval * out_val,size_t * iter)219 bool upb_msg_next(const upb_msg *msg, const upb_msgdef *m,
220                   const upb_symtab *ext_pool, const upb_fielddef **out_f,
221                   upb_msgval *out_val, size_t *iter) {
222   int i = *iter;
223   int n = upb_msgdef_fieldcount(m);
224   const upb_msgval zero = {0};
225   UPB_UNUSED(ext_pool);
226   while (++i < n) {
227     const upb_fielddef *f = upb_msgdef_field(m, i);
228     upb_msgval val = _upb_msg_getraw(msg, f);
229 
230     /* Skip field if unset or empty. */
231     if (upb_fielddef_haspresence(f)) {
232       if (!upb_msg_has(msg, f)) continue;
233     } else {
234       upb_msgval test = val;
235       if (upb_fielddef_isstring(f) && !upb_fielddef_isseq(f)) {
236         /* Clear string pointer, only size matters (ptr could be non-NULL). */
237         test.str_val.data = NULL;
238       }
239       /* Continue if NULL or 0. */
240       if (memcmp(&test, &zero, sizeof(test)) == 0) continue;
241 
242       /* Continue on empty array or map. */
243       if (upb_fielddef_ismap(f)) {
244         if (upb_map_size(test.map_val) == 0) continue;
245       } else if (upb_fielddef_isseq(f)) {
246         if (upb_array_size(test.array_val) == 0) continue;
247       }
248     }
249 
250     *out_val = val;
251     *out_f = f;
252     *iter = i;
253     return true;
254   }
255   *iter = i;
256   return false;
257 }
258 
_upb_msg_discardunknown(upb_msg * msg,const upb_msgdef * m,int depth)259 bool _upb_msg_discardunknown(upb_msg *msg, const upb_msgdef *m, int depth) {
260   size_t iter = UPB_MSG_BEGIN;
261   const upb_fielddef *f;
262   upb_msgval val;
263   bool ret = true;
264 
265   if (--depth == 0) return false;
266 
267   _upb_msg_discardunknown_shallow(msg);
268 
269   while (upb_msg_next(msg, m, NULL /*ext_pool*/, &f, &val, &iter)) {
270     const upb_msgdef *subm = upb_fielddef_msgsubdef(f);
271     if (!subm) continue;
272     if (upb_fielddef_ismap(f)) {
273       const upb_fielddef *val_f = upb_msgdef_itof(subm, 2);
274       const upb_msgdef *val_m = upb_fielddef_msgsubdef(val_f);
275       upb_map *map = (upb_map*)val.map_val;
276       size_t iter = UPB_MAP_BEGIN;
277 
278       if (!val_m) continue;
279 
280       while (upb_mapiter_next(map, &iter)) {
281         upb_msgval map_val = upb_mapiter_value(map, iter);
282         if (!_upb_msg_discardunknown((upb_msg*)map_val.msg_val, val_m, depth)) {
283           ret = false;
284         }
285       }
286     } else if (upb_fielddef_isseq(f)) {
287       const upb_array *arr = val.array_val;
288       size_t i, n = upb_array_size(arr);
289       for (i = 0; i < n; i++) {
290         upb_msgval elem = upb_array_get(arr, i);
291         if (!_upb_msg_discardunknown((upb_msg*)elem.msg_val, subm, depth)) {
292           ret = false;
293         }
294       }
295     } else {
296       if (!_upb_msg_discardunknown((upb_msg*)val.msg_val, subm, depth)) {
297         ret = false;
298       }
299     }
300   }
301 
302   return ret;
303 }
304 
upb_msg_discardunknown(upb_msg * msg,const upb_msgdef * m,int maxdepth)305 bool upb_msg_discardunknown(upb_msg *msg, const upb_msgdef *m, int maxdepth) {
306   return _upb_msg_discardunknown(msg, m, maxdepth);
307 }
308 
309 /** upb_array *****************************************************************/
310 
upb_array_new(upb_arena * a,upb_fieldtype_t type)311 upb_array *upb_array_new(upb_arena *a, upb_fieldtype_t type) {
312   return _upb_array_new(a, 4, _upb_fieldtype_to_sizelg2[type]);
313 }
314 
upb_array_size(const upb_array * arr)315 size_t upb_array_size(const upb_array *arr) {
316   return arr->len;
317 }
318 
upb_array_get(const upb_array * arr,size_t i)319 upb_msgval upb_array_get(const upb_array *arr, size_t i) {
320   upb_msgval ret;
321   const char* data = _upb_array_constptr(arr);
322   int lg2 = arr->data & 7;
323   UPB_ASSERT(i < arr->len);
324   memcpy(&ret, data + (i << lg2), 1 << lg2);
325   return ret;
326 }
327 
upb_array_set(upb_array * arr,size_t i,upb_msgval val)328 void upb_array_set(upb_array *arr, size_t i, upb_msgval val) {
329   char* data = _upb_array_ptr(arr);
330   int lg2 = arr->data & 7;
331   UPB_ASSERT(i < arr->len);
332   memcpy(data + (i << lg2), &val, 1 << lg2);
333 }
334 
upb_array_append(upb_array * arr,upb_msgval val,upb_arena * arena)335 bool upb_array_append(upb_array *arr, upb_msgval val, upb_arena *arena) {
336   if (!_upb_array_realloc(arr, arr->len + 1, arena)) {
337     return false;
338   }
339   arr->len++;
340   upb_array_set(arr, arr->len - 1, val);
341   return true;
342 }
343 
upb_array_resize(upb_array * arr,size_t size,upb_arena * arena)344 bool upb_array_resize(upb_array *arr, size_t size, upb_arena *arena) {
345   return _upb_array_resize(arr, size, arena);
346 }
347 
348 /** upb_map *******************************************************************/
349 
upb_map_new(upb_arena * a,upb_fieldtype_t key_type,upb_fieldtype_t value_type)350 upb_map *upb_map_new(upb_arena *a, upb_fieldtype_t key_type,
351                      upb_fieldtype_t value_type) {
352   return _upb_map_new(a, _upb_fieldtype_to_mapsize[key_type],
353                       _upb_fieldtype_to_mapsize[value_type]);
354 }
355 
upb_map_size(const upb_map * map)356 size_t upb_map_size(const upb_map *map) {
357   return _upb_map_size(map);
358 }
359 
upb_map_get(const upb_map * map,upb_msgval key,upb_msgval * val)360 bool upb_map_get(const upb_map *map, upb_msgval key, upb_msgval *val) {
361   return _upb_map_get(map, &key, map->key_size, val, map->val_size);
362 }
363 
upb_map_clear(upb_map * map)364 void upb_map_clear(upb_map *map) {
365   _upb_map_clear(map);
366 }
367 
upb_map_set(upb_map * map,upb_msgval key,upb_msgval val,upb_arena * arena)368 bool upb_map_set(upb_map *map, upb_msgval key, upb_msgval val,
369                  upb_arena *arena) {
370   return _upb_map_set(map, &key, map->key_size, &val, map->val_size, arena);
371 }
372 
upb_map_delete(upb_map * map,upb_msgval key)373 bool upb_map_delete(upb_map *map, upb_msgval key) {
374   return _upb_map_delete(map, &key, map->key_size);
375 }
376 
upb_mapiter_next(const upb_map * map,size_t * iter)377 bool upb_mapiter_next(const upb_map *map, size_t *iter) {
378   return _upb_map_next(map, iter);
379 }
380 
upb_mapiter_done(const upb_map * map,size_t iter)381 bool upb_mapiter_done(const upb_map *map, size_t iter) {
382   upb_strtable_iter i;
383   UPB_ASSERT(iter != UPB_MAP_BEGIN);
384   i.t = &map->table;
385   i.index = iter;
386   return upb_strtable_done(&i);
387 }
388 
389 /* Returns the key and value for this entry of the map. */
upb_mapiter_key(const upb_map * map,size_t iter)390 upb_msgval upb_mapiter_key(const upb_map *map, size_t iter) {
391   upb_strtable_iter i;
392   upb_msgval ret;
393   i.t = &map->table;
394   i.index = iter;
395   _upb_map_fromkey(upb_strtable_iter_key(&i), &ret, map->key_size);
396   return ret;
397 }
398 
upb_mapiter_value(const upb_map * map,size_t iter)399 upb_msgval upb_mapiter_value(const upb_map *map, size_t iter) {
400   upb_strtable_iter i;
401   upb_msgval ret;
402   i.t = &map->table;
403   i.index = iter;
404   _upb_map_fromvalue(upb_strtable_iter_value(&i), &ret, map->val_size);
405   return ret;
406 }
407 
408 /* void upb_mapiter_setvalue(upb_map *map, size_t iter, upb_msgval value); */
409