1 
2 #include "upb/msg.h"
3 
4 #include "upb/table.int.h"
5 
6 #include "upb/port_def.inc"
7 
8 /** upb_msg *******************************************************************/
9 
10 static const size_t overhead = sizeof(upb_msg_internal);
11 
upb_msg_getinternal_const(const upb_msg * msg)12 static const upb_msg_internal *upb_msg_getinternal_const(const upb_msg *msg) {
13   ptrdiff_t size = sizeof(upb_msg_internal);
14   return (upb_msg_internal*)((char*)msg - size);
15 }
16 
_upb_msg_new(const upb_msglayout * l,upb_arena * a)17 upb_msg *_upb_msg_new(const upb_msglayout *l, upb_arena *a) {
18   return _upb_msg_new_inl(l, a);
19 }
20 
_upb_msg_clear(upb_msg * msg,const upb_msglayout * l)21 void _upb_msg_clear(upb_msg *msg, const upb_msglayout *l) {
22   void *mem = UPB_PTR_AT(msg, -sizeof(upb_msg_internal), char);
23   memset(mem, 0, upb_msg_sizeof(l));
24 }
25 
_upb_msg_addunknown(upb_msg * msg,const char * data,size_t len,upb_arena * arena)26 bool _upb_msg_addunknown(upb_msg *msg, const char *data, size_t len,
27                          upb_arena *arena) {
28 
29   upb_msg_internal *in = upb_msg_getinternal(msg);
30   if (!in->unknown) {
31     size_t size = 128;
32     while (size < len) size *= 2;
33     in->unknown = upb_arena_malloc(arena, size + overhead);
34     if (!in->unknown) return false;
35     in->unknown->size = size;
36     in->unknown->len = 0;
37   } else if (in->unknown->size - in->unknown->len < len) {
38     size_t need = in->unknown->len + len;
39     size_t size = in->unknown->size;
40     while (size < need)  size *= 2;
41     in->unknown = upb_arena_realloc(
42         arena, in->unknown, in->unknown->size + overhead, size + overhead);
43     if (!in->unknown) return false;
44     in->unknown->size = size;
45   }
46   memcpy(UPB_PTR_AT(in->unknown + 1, in->unknown->len, char), data, len);
47   in->unknown->len += len;
48   return true;
49 }
50 
_upb_msg_discardunknown_shallow(upb_msg * msg)51 void _upb_msg_discardunknown_shallow(upb_msg *msg) {
52   upb_msg_internal *in = upb_msg_getinternal(msg);
53   if (in->unknown) {
54     in->unknown->len = 0;
55   }
56 }
57 
upb_msg_getunknown(const upb_msg * msg,size_t * len)58 const char *upb_msg_getunknown(const upb_msg *msg, size_t *len) {
59   const upb_msg_internal *in = upb_msg_getinternal_const(msg);
60   if (in->unknown) {
61     *len = in->unknown->len;
62     return (char*)(in->unknown + 1);
63   } else {
64     *len = 0;
65     return NULL;
66   }
67 }
68 
69 /** upb_array *****************************************************************/
70 
_upb_array_realloc(upb_array * arr,size_t min_size,upb_arena * arena)71 bool _upb_array_realloc(upb_array *arr, size_t min_size, upb_arena *arena) {
72   size_t new_size = UPB_MAX(arr->size, 4);
73   int elem_size_lg2 = arr->data & 7;
74   size_t old_bytes = arr->size << elem_size_lg2;
75   size_t new_bytes;
76   void* ptr = _upb_array_ptr(arr);
77 
78   /* Log2 ceiling of size. */
79   while (new_size < min_size) new_size *= 2;
80 
81   new_bytes = new_size << elem_size_lg2;
82   ptr = upb_arena_realloc(arena, ptr, old_bytes, new_bytes);
83 
84   if (!ptr) {
85     return false;
86   }
87 
88   arr->data = _upb_tag_arrptr(ptr, elem_size_lg2);
89   arr->size = new_size;
90   return true;
91 }
92 
getorcreate_array(upb_array ** arr_ptr,int elem_size_lg2,upb_arena * arena)93 static upb_array *getorcreate_array(upb_array **arr_ptr, int elem_size_lg2,
94                                     upb_arena *arena) {
95   upb_array *arr = *arr_ptr;
96   if (!arr) {
97     arr = _upb_array_new(arena, 4, elem_size_lg2);
98     if (!arr) return NULL;
99     *arr_ptr = arr;
100   }
101   return arr;
102 }
103 
_upb_array_resize_fallback(upb_array ** arr_ptr,size_t size,int elem_size_lg2,upb_arena * arena)104 void *_upb_array_resize_fallback(upb_array **arr_ptr, size_t size,
105                                  int elem_size_lg2, upb_arena *arena) {
106   upb_array *arr = getorcreate_array(arr_ptr, elem_size_lg2, arena);
107   return arr && _upb_array_resize(arr, size, arena) ? _upb_array_ptr(arr)
108                                                     : NULL;
109 }
110 
_upb_array_append_fallback(upb_array ** arr_ptr,const void * value,int elem_size_lg2,upb_arena * arena)111 bool _upb_array_append_fallback(upb_array **arr_ptr, const void *value,
112                                 int elem_size_lg2, upb_arena *arena) {
113   upb_array *arr = getorcreate_array(arr_ptr, elem_size_lg2, arena);
114   if (!arr) return false;
115 
116   size_t elems = arr->len;
117 
118   if (!_upb_array_resize(arr, elems + 1, arena)) {
119     return false;
120   }
121 
122   char *data = _upb_array_ptr(arr);
123   memcpy(data + (elems << elem_size_lg2), value, 1 << elem_size_lg2);
124   return true;
125 }
126 
127 /** upb_map *******************************************************************/
128 
_upb_map_new(upb_arena * a,size_t key_size,size_t value_size)129 upb_map *_upb_map_new(upb_arena *a, size_t key_size, size_t value_size) {
130   upb_map *map = upb_arena_malloc(a, sizeof(upb_map));
131 
132   if (!map) {
133     return NULL;
134   }
135 
136   upb_strtable_init2(&map->table, UPB_CTYPE_INT32, 4, upb_arena_alloc(a));
137   map->key_size = key_size;
138   map->val_size = value_size;
139 
140   return map;
141 }
142 
_upb_mapsorter_getkeys(const void * _a,const void * _b,void * a_key,void * b_key,size_t size)143 static void _upb_mapsorter_getkeys(const void *_a, const void *_b, void *a_key,
144                                    void *b_key, size_t size) {
145   const upb_tabent *const*a = _a;
146   const upb_tabent *const*b = _b;
147   upb_strview a_tabkey = upb_tabstrview((*a)->key);
148   upb_strview b_tabkey = upb_tabstrview((*b)->key);
149   _upb_map_fromkey(a_tabkey, a_key, size);
150   _upb_map_fromkey(b_tabkey, b_key, size);
151 }
152 
_upb_mapsorter_cmpi64(const void * _a,const void * _b)153 static int _upb_mapsorter_cmpi64(const void *_a, const void *_b) {
154   int64_t a, b;
155   _upb_mapsorter_getkeys(_a, _b, &a, &b, 8);
156   return a - b;
157 }
158 
_upb_mapsorter_cmpu64(const void * _a,const void * _b)159 static int _upb_mapsorter_cmpu64(const void *_a, const void *_b) {
160   uint64_t a, b;
161   _upb_mapsorter_getkeys(_a, _b, &a, &b, 8);
162   return a - b;
163 }
164 
_upb_mapsorter_cmpi32(const void * _a,const void * _b)165 static int _upb_mapsorter_cmpi32(const void *_a, const void *_b) {
166   int32_t a, b;
167   _upb_mapsorter_getkeys(_a, _b, &a, &b, 4);
168   return a - b;
169 }
170 
_upb_mapsorter_cmpu32(const void * _a,const void * _b)171 static int _upb_mapsorter_cmpu32(const void *_a, const void *_b) {
172   uint32_t a, b;
173   _upb_mapsorter_getkeys(_a, _b, &a, &b, 4);
174   return a - b;
175 }
176 
_upb_mapsorter_cmpbool(const void * _a,const void * _b)177 static int _upb_mapsorter_cmpbool(const void *_a, const void *_b) {
178   bool a, b;
179   _upb_mapsorter_getkeys(_a, _b, &a, &b, 1);
180   return a - b;
181 }
182 
_upb_mapsorter_cmpstr(const void * _a,const void * _b)183 static int _upb_mapsorter_cmpstr(const void *_a, const void *_b) {
184   upb_strview a, b;
185   _upb_mapsorter_getkeys(_a, _b, &a, &b, UPB_MAPTYPE_STRING);
186   size_t common_size = UPB_MIN(a.size, b.size);
187   int cmp = memcmp(a.data, b.data, common_size);
188   if (cmp) return cmp;
189   return a.size - b.size;
190 }
191 
_upb_mapsorter_pushmap(_upb_mapsorter * s,upb_descriptortype_t key_type,const upb_map * map,_upb_sortedmap * sorted)192 bool _upb_mapsorter_pushmap(_upb_mapsorter *s, upb_descriptortype_t key_type,
193                             const upb_map *map, _upb_sortedmap *sorted) {
194   int map_size = _upb_map_size(map);
195   sorted->start = s->size;
196   sorted->pos = sorted->start;
197   sorted->end = sorted->start + map_size;
198 
199   /* Grow s->entries if necessary. */
200   if (sorted->end > s->cap) {
201     s->cap = _upb_lg2ceilsize(sorted->end);
202     s->entries = realloc(s->entries, s->cap * sizeof(*s->entries));
203     if (!s->entries) return false;
204   }
205 
206   s->size = sorted->end;
207 
208   /* Copy non-empty entries from the table to s->entries. */
209   upb_tabent const**dst = &s->entries[sorted->start];
210   const upb_tabent *src = map->table.t.entries;
211   const upb_tabent *end = src + upb_table_size(&map->table.t);
212   for (; src < end; src++) {
213     if (!upb_tabent_isempty(src)) {
214       *dst = src;
215       dst++;
216     }
217   }
218   UPB_ASSERT(dst == &s->entries[sorted->end]);
219 
220   /* Sort entries according to the key type. */
221 
222   int (*compar)(const void *, const void *);
223 
224   switch (key_type) {
225     case UPB_DESCRIPTOR_TYPE_INT64:
226     case UPB_DESCRIPTOR_TYPE_SFIXED64:
227     case UPB_DESCRIPTOR_TYPE_SINT64:
228       compar = _upb_mapsorter_cmpi64;
229       break;
230     case UPB_DESCRIPTOR_TYPE_UINT64:
231     case UPB_DESCRIPTOR_TYPE_FIXED64:
232       compar = _upb_mapsorter_cmpu64;
233       break;
234     case UPB_DESCRIPTOR_TYPE_INT32:
235     case UPB_DESCRIPTOR_TYPE_SINT32:
236     case UPB_DESCRIPTOR_TYPE_SFIXED32:
237     case UPB_DESCRIPTOR_TYPE_ENUM:
238       compar = _upb_mapsorter_cmpi32;
239       break;
240     case UPB_DESCRIPTOR_TYPE_UINT32:
241     case UPB_DESCRIPTOR_TYPE_FIXED32:
242       compar = _upb_mapsorter_cmpu32;
243       break;
244     case UPB_DESCRIPTOR_TYPE_BOOL:
245       compar = _upb_mapsorter_cmpbool;
246       break;
247     case UPB_DESCRIPTOR_TYPE_STRING:
248       compar = _upb_mapsorter_cmpstr;
249       break;
250     default:
251       UPB_UNREACHABLE();
252   }
253 
254   qsort(&s->entries[sorted->start], map_size, sizeof(*s->entries), compar);
255   return true;
256 }
257