• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // We encode backwards, to avoid pre-computing lengths (one-pass encode).
9 
10 #include "upb/wire/encode.h"
11 
12 #include <setjmp.h>
13 #include <stdbool.h>
14 #include <stdint.h>
15 #include <string.h>
16 
17 #include "upb/base/descriptor_constants.h"
18 #include "upb/base/internal/endian.h"
19 #include "upb/base/string_view.h"
20 #include "upb/hash/common.h"
21 #include "upb/hash/str_table.h"
22 #include "upb/mem/arena.h"
23 #include "upb/message/array.h"
24 #include "upb/message/internal/accessors.h"
25 #include "upb/message/internal/array.h"
26 #include "upb/message/internal/extension.h"
27 #include "upb/message/internal/map.h"
28 #include "upb/message/internal/map_entry.h"
29 #include "upb/message/internal/map_sorter.h"
30 #include "upb/message/internal/tagged_ptr.h"
31 #include "upb/message/map.h"
32 #include "upb/message/message.h"
33 #include "upb/message/tagged_ptr.h"
34 #include "upb/mini_table/extension.h"
35 #include "upb/mini_table/field.h"
36 #include "upb/mini_table/internal/field.h"
37 #include "upb/mini_table/internal/message.h"
38 #include "upb/mini_table/internal/sub.h"
39 #include "upb/mini_table/message.h"
40 #include "upb/wire/internal/constants.h"
41 #include "upb/wire/types.h"
42 
43 // Must be last.
44 #include "upb/port/def.inc"
45 
46 // Returns the MiniTable corresponding to a given MiniTableField
47 // from an array of MiniTableSubs.
_upb_Encoder_GetSubMiniTable(const upb_MiniTableSubInternal * subs,const upb_MiniTableField * field)48 static const upb_MiniTable* _upb_Encoder_GetSubMiniTable(
49     const upb_MiniTableSubInternal* subs, const upb_MiniTableField* field) {
50   return *subs[field->UPB_PRIVATE(submsg_index)].UPB_PRIVATE(submsg);
51 }
52 
53 #define UPB_PB_VARINT_MAX_LEN 10
54 
55 UPB_NOINLINE
encode_varint64(uint64_t val,char * buf)56 static size_t encode_varint64(uint64_t val, char* buf) {
57   size_t i = 0;
58   do {
59     uint8_t byte = val & 0x7fU;
60     val >>= 7;
61     if (val) byte |= 0x80U;
62     buf[i++] = byte;
63   } while (val);
64   return i;
65 }
66 
encode_zz32(int32_t n)67 static uint32_t encode_zz32(int32_t n) {
68   return ((uint32_t)n << 1) ^ (n >> 31);
69 }
encode_zz64(int64_t n)70 static uint64_t encode_zz64(int64_t n) {
71   return ((uint64_t)n << 1) ^ (n >> 63);
72 }
73 
74 typedef struct {
75   upb_EncodeStatus status;
76   jmp_buf err;
77   upb_Arena* arena;
78   char *buf, *ptr, *limit;
79   int options;
80   int depth;
81   _upb_mapsorter sorter;
82 } upb_encstate;
83 
upb_roundup_pow2(size_t bytes)84 static size_t upb_roundup_pow2(size_t bytes) {
85   size_t ret = 128;
86   while (ret < bytes) {
87     ret *= 2;
88   }
89   return ret;
90 }
91 
encode_err(upb_encstate * e,upb_EncodeStatus s)92 UPB_NORETURN static void encode_err(upb_encstate* e, upb_EncodeStatus s) {
93   UPB_ASSERT(s != kUpb_EncodeStatus_Ok);
94   e->status = s;
95   UPB_LONGJMP(e->err, 1);
96 }
97 
98 UPB_NOINLINE
encode_growbuffer(upb_encstate * e,size_t bytes)99 static void encode_growbuffer(upb_encstate* e, size_t bytes) {
100   size_t old_size = e->limit - e->buf;
101   size_t new_size = upb_roundup_pow2(bytes + (e->limit - e->ptr));
102   char* new_buf = upb_Arena_Realloc(e->arena, e->buf, old_size, new_size);
103 
104   if (!new_buf) encode_err(e, kUpb_EncodeStatus_OutOfMemory);
105 
106   // We want previous data at the end, realloc() put it at the beginning.
107   // TODO: This is somewhat inefficient since we are copying twice.
108   // Maybe create a realloc() that copies to the end of the new buffer?
109   if (old_size > 0) {
110     memmove(new_buf + new_size - old_size, e->buf, old_size);
111   }
112 
113   e->ptr = new_buf + new_size - (e->limit - e->ptr);
114   e->limit = new_buf + new_size;
115   e->buf = new_buf;
116 
117   e->ptr -= bytes;
118 }
119 
120 /* Call to ensure that at least "bytes" bytes are available for writing at
121  * e->ptr.  Returns false if the bytes could not be allocated. */
122 UPB_FORCEINLINE
encode_reserve(upb_encstate * e,size_t bytes)123 void encode_reserve(upb_encstate* e, size_t bytes) {
124   if ((size_t)(e->ptr - e->buf) < bytes) {
125     encode_growbuffer(e, bytes);
126     return;
127   }
128 
129   e->ptr -= bytes;
130 }
131 
132 /* Writes the given bytes to the buffer, handling reserve/advance. */
encode_bytes(upb_encstate * e,const void * data,size_t len)133 static void encode_bytes(upb_encstate* e, const void* data, size_t len) {
134   if (len == 0) return; /* memcpy() with zero size is UB */
135   encode_reserve(e, len);
136   memcpy(e->ptr, data, len);
137 }
138 
encode_fixed64(upb_encstate * e,uint64_t val)139 static void encode_fixed64(upb_encstate* e, uint64_t val) {
140   val = upb_BigEndian64(val);
141   encode_bytes(e, &val, sizeof(uint64_t));
142 }
143 
encode_fixed32(upb_encstate * e,uint32_t val)144 static void encode_fixed32(upb_encstate* e, uint32_t val) {
145   val = upb_BigEndian32(val);
146   encode_bytes(e, &val, sizeof(uint32_t));
147 }
148 
149 UPB_NOINLINE
encode_longvarint(upb_encstate * e,uint64_t val)150 static void encode_longvarint(upb_encstate* e, uint64_t val) {
151   size_t len;
152   char* start;
153 
154   encode_reserve(e, UPB_PB_VARINT_MAX_LEN);
155   len = encode_varint64(val, e->ptr);
156   start = e->ptr + UPB_PB_VARINT_MAX_LEN - len;
157   memmove(start, e->ptr, len);
158   e->ptr = start;
159 }
160 
161 UPB_FORCEINLINE
encode_varint(upb_encstate * e,uint64_t val)162 void encode_varint(upb_encstate* e, uint64_t val) {
163   if (val < 128 && e->ptr != e->buf) {
164     --e->ptr;
165     *e->ptr = val;
166   } else {
167     encode_longvarint(e, val);
168   }
169 }
170 
encode_double(upb_encstate * e,double d)171 static void encode_double(upb_encstate* e, double d) {
172   uint64_t u64;
173   UPB_ASSERT(sizeof(double) == sizeof(uint64_t));
174   memcpy(&u64, &d, sizeof(uint64_t));
175   encode_fixed64(e, u64);
176 }
177 
encode_float(upb_encstate * e,float d)178 static void encode_float(upb_encstate* e, float d) {
179   uint32_t u32;
180   UPB_ASSERT(sizeof(float) == sizeof(uint32_t));
181   memcpy(&u32, &d, sizeof(uint32_t));
182   encode_fixed32(e, u32);
183 }
184 
encode_tag(upb_encstate * e,uint32_t field_number,uint8_t wire_type)185 static void encode_tag(upb_encstate* e, uint32_t field_number,
186                        uint8_t wire_type) {
187   encode_varint(e, (field_number << 3) | wire_type);
188 }
189 
encode_fixedarray(upb_encstate * e,const upb_Array * arr,size_t elem_size,uint32_t tag)190 static void encode_fixedarray(upb_encstate* e, const upb_Array* arr,
191                               size_t elem_size, uint32_t tag) {
192   size_t bytes = upb_Array_Size(arr) * elem_size;
193   const char* data = upb_Array_DataPtr(arr);
194   const char* ptr = data + bytes - elem_size;
195 
196   if (tag || !upb_IsLittleEndian()) {
197     while (true) {
198       if (elem_size == 4) {
199         uint32_t val;
200         memcpy(&val, ptr, sizeof(val));
201         val = upb_BigEndian32(val);
202         encode_bytes(e, &val, elem_size);
203       } else {
204         UPB_ASSERT(elem_size == 8);
205         uint64_t val;
206         memcpy(&val, ptr, sizeof(val));
207         val = upb_BigEndian64(val);
208         encode_bytes(e, &val, elem_size);
209       }
210 
211       if (tag) encode_varint(e, tag);
212       if (ptr == data) break;
213       ptr -= elem_size;
214     }
215   } else {
216     encode_bytes(e, data, bytes);
217   }
218 }
219 
220 static void encode_message(upb_encstate* e, const upb_Message* msg,
221                            const upb_MiniTable* m, size_t* size);
222 
encode_TaggedMessagePtr(upb_encstate * e,upb_TaggedMessagePtr tagged,const upb_MiniTable * m,size_t * size)223 static void encode_TaggedMessagePtr(upb_encstate* e,
224                                     upb_TaggedMessagePtr tagged,
225                                     const upb_MiniTable* m, size_t* size) {
226   if (upb_TaggedMessagePtr_IsEmpty(tagged)) {
227     m = UPB_PRIVATE(_upb_MiniTable_Empty)();
228   }
229   encode_message(e, UPB_PRIVATE(_upb_TaggedMessagePtr_GetMessage)(tagged), m,
230                  size);
231 }
232 
encode_scalar(upb_encstate * e,const void * _field_mem,const upb_MiniTableSubInternal * subs,const upb_MiniTableField * f)233 static void encode_scalar(upb_encstate* e, const void* _field_mem,
234                           const upb_MiniTableSubInternal* subs,
235                           const upb_MiniTableField* f) {
236   const char* field_mem = _field_mem;
237   int wire_type;
238 
239 #define CASE(ctype, type, wtype, encodeval) \
240   {                                         \
241     ctype val = *(ctype*)field_mem;         \
242     encode_##type(e, encodeval);            \
243     wire_type = wtype;                      \
244     break;                                  \
245   }
246 
247   switch (f->UPB_PRIVATE(descriptortype)) {
248     case kUpb_FieldType_Double:
249       CASE(double, double, kUpb_WireType_64Bit, val);
250     case kUpb_FieldType_Float:
251       CASE(float, float, kUpb_WireType_32Bit, val);
252     case kUpb_FieldType_Int64:
253     case kUpb_FieldType_UInt64:
254       CASE(uint64_t, varint, kUpb_WireType_Varint, val);
255     case kUpb_FieldType_UInt32:
256       CASE(uint32_t, varint, kUpb_WireType_Varint, val);
257     case kUpb_FieldType_Int32:
258     case kUpb_FieldType_Enum:
259       CASE(int32_t, varint, kUpb_WireType_Varint, (int64_t)val);
260     case kUpb_FieldType_SFixed64:
261     case kUpb_FieldType_Fixed64:
262       CASE(uint64_t, fixed64, kUpb_WireType_64Bit, val);
263     case kUpb_FieldType_Fixed32:
264     case kUpb_FieldType_SFixed32:
265       CASE(uint32_t, fixed32, kUpb_WireType_32Bit, val);
266     case kUpb_FieldType_Bool:
267       CASE(bool, varint, kUpb_WireType_Varint, val);
268     case kUpb_FieldType_SInt32:
269       CASE(int32_t, varint, kUpb_WireType_Varint, encode_zz32(val));
270     case kUpb_FieldType_SInt64:
271       CASE(int64_t, varint, kUpb_WireType_Varint, encode_zz64(val));
272     case kUpb_FieldType_String:
273     case kUpb_FieldType_Bytes: {
274       upb_StringView view = *(upb_StringView*)field_mem;
275       encode_bytes(e, view.data, view.size);
276       encode_varint(e, view.size);
277       wire_type = kUpb_WireType_Delimited;
278       break;
279     }
280     case kUpb_FieldType_Group: {
281       size_t size;
282       upb_TaggedMessagePtr submsg = *(upb_TaggedMessagePtr*)field_mem;
283       const upb_MiniTable* subm = _upb_Encoder_GetSubMiniTable(subs, f);
284       if (submsg == 0) {
285         return;
286       }
287       if (--e->depth == 0) encode_err(e, kUpb_EncodeStatus_MaxDepthExceeded);
288       encode_tag(e, upb_MiniTableField_Number(f), kUpb_WireType_EndGroup);
289       encode_TaggedMessagePtr(e, submsg, subm, &size);
290       wire_type = kUpb_WireType_StartGroup;
291       e->depth++;
292       break;
293     }
294     case kUpb_FieldType_Message: {
295       size_t size;
296       upb_TaggedMessagePtr submsg = *(upb_TaggedMessagePtr*)field_mem;
297       const upb_MiniTable* subm = _upb_Encoder_GetSubMiniTable(subs, f);
298       if (submsg == 0) {
299         return;
300       }
301       if (--e->depth == 0) encode_err(e, kUpb_EncodeStatus_MaxDepthExceeded);
302       encode_TaggedMessagePtr(e, submsg, subm, &size);
303       encode_varint(e, size);
304       wire_type = kUpb_WireType_Delimited;
305       e->depth++;
306       break;
307     }
308     default:
309       UPB_UNREACHABLE();
310   }
311 #undef CASE
312 
313   encode_tag(e, upb_MiniTableField_Number(f), wire_type);
314 }
315 
encode_array(upb_encstate * e,const upb_Message * msg,const upb_MiniTableSubInternal * subs,const upb_MiniTableField * f)316 static void encode_array(upb_encstate* e, const upb_Message* msg,
317                          const upb_MiniTableSubInternal* subs,
318                          const upb_MiniTableField* f) {
319   const upb_Array* arr = *UPB_PTR_AT(msg, f->UPB_PRIVATE(offset), upb_Array*);
320   bool packed = upb_MiniTableField_IsPacked(f);
321   size_t pre_len = e->limit - e->ptr;
322 
323   if (arr == NULL || upb_Array_Size(arr) == 0) {
324     return;
325   }
326 
327 #define VARINT_CASE(ctype, encode)                                         \
328   {                                                                        \
329     const ctype* start = upb_Array_DataPtr(arr);                           \
330     const ctype* ptr = start + upb_Array_Size(arr);                        \
331     uint32_t tag =                                                         \
332         packed ? 0 : (f->UPB_PRIVATE(number) << 3) | kUpb_WireType_Varint; \
333     do {                                                                   \
334       ptr--;                                                               \
335       encode_varint(e, encode);                                            \
336       if (tag) encode_varint(e, tag);                                      \
337     } while (ptr != start);                                                \
338   }                                                                        \
339   break;
340 
341 #define TAG(wire_type) (packed ? 0 : (f->UPB_PRIVATE(number) << 3 | wire_type))
342 
343   switch (f->UPB_PRIVATE(descriptortype)) {
344     case kUpb_FieldType_Double:
345       encode_fixedarray(e, arr, sizeof(double), TAG(kUpb_WireType_64Bit));
346       break;
347     case kUpb_FieldType_Float:
348       encode_fixedarray(e, arr, sizeof(float), TAG(kUpb_WireType_32Bit));
349       break;
350     case kUpb_FieldType_SFixed64:
351     case kUpb_FieldType_Fixed64:
352       encode_fixedarray(e, arr, sizeof(uint64_t), TAG(kUpb_WireType_64Bit));
353       break;
354     case kUpb_FieldType_Fixed32:
355     case kUpb_FieldType_SFixed32:
356       encode_fixedarray(e, arr, sizeof(uint32_t), TAG(kUpb_WireType_32Bit));
357       break;
358     case kUpb_FieldType_Int64:
359     case kUpb_FieldType_UInt64:
360       VARINT_CASE(uint64_t, *ptr);
361     case kUpb_FieldType_UInt32:
362       VARINT_CASE(uint32_t, *ptr);
363     case kUpb_FieldType_Int32:
364     case kUpb_FieldType_Enum:
365       VARINT_CASE(int32_t, (int64_t)*ptr);
366     case kUpb_FieldType_Bool:
367       VARINT_CASE(bool, *ptr);
368     case kUpb_FieldType_SInt32:
369       VARINT_CASE(int32_t, encode_zz32(*ptr));
370     case kUpb_FieldType_SInt64:
371       VARINT_CASE(int64_t, encode_zz64(*ptr));
372     case kUpb_FieldType_String:
373     case kUpb_FieldType_Bytes: {
374       const upb_StringView* start = upb_Array_DataPtr(arr);
375       const upb_StringView* ptr = start + upb_Array_Size(arr);
376       do {
377         ptr--;
378         encode_bytes(e, ptr->data, ptr->size);
379         encode_varint(e, ptr->size);
380         encode_tag(e, upb_MiniTableField_Number(f), kUpb_WireType_Delimited);
381       } while (ptr != start);
382       return;
383     }
384     case kUpb_FieldType_Group: {
385       const upb_TaggedMessagePtr* start = upb_Array_DataPtr(arr);
386       const upb_TaggedMessagePtr* ptr = start + upb_Array_Size(arr);
387       const upb_MiniTable* subm = _upb_Encoder_GetSubMiniTable(subs, f);
388       if (--e->depth == 0) encode_err(e, kUpb_EncodeStatus_MaxDepthExceeded);
389       do {
390         size_t size;
391         ptr--;
392         encode_tag(e, upb_MiniTableField_Number(f), kUpb_WireType_EndGroup);
393         encode_TaggedMessagePtr(e, *ptr, subm, &size);
394         encode_tag(e, upb_MiniTableField_Number(f), kUpb_WireType_StartGroup);
395       } while (ptr != start);
396       e->depth++;
397       return;
398     }
399     case kUpb_FieldType_Message: {
400       const upb_TaggedMessagePtr* start = upb_Array_DataPtr(arr);
401       const upb_TaggedMessagePtr* ptr = start + upb_Array_Size(arr);
402       const upb_MiniTable* subm = _upb_Encoder_GetSubMiniTable(subs, f);
403       if (--e->depth == 0) encode_err(e, kUpb_EncodeStatus_MaxDepthExceeded);
404       do {
405         size_t size;
406         ptr--;
407         encode_TaggedMessagePtr(e, *ptr, subm, &size);
408         encode_varint(e, size);
409         encode_tag(e, upb_MiniTableField_Number(f), kUpb_WireType_Delimited);
410       } while (ptr != start);
411       e->depth++;
412       return;
413     }
414   }
415 #undef VARINT_CASE
416 
417   if (packed) {
418     encode_varint(e, e->limit - e->ptr - pre_len);
419     encode_tag(e, upb_MiniTableField_Number(f), kUpb_WireType_Delimited);
420   }
421 }
422 
encode_mapentry(upb_encstate * e,uint32_t number,const upb_MiniTable * layout,const upb_MapEntry * ent)423 static void encode_mapentry(upb_encstate* e, uint32_t number,
424                             const upb_MiniTable* layout,
425                             const upb_MapEntry* ent) {
426   const upb_MiniTableField* key_field = upb_MiniTable_MapKey(layout);
427   const upb_MiniTableField* val_field = upb_MiniTable_MapValue(layout);
428   size_t pre_len = e->limit - e->ptr;
429   size_t size;
430   encode_scalar(e, &ent->v, layout->UPB_PRIVATE(subs), val_field);
431   encode_scalar(e, &ent->k, layout->UPB_PRIVATE(subs), key_field);
432   size = (e->limit - e->ptr) - pre_len;
433   encode_varint(e, size);
434   encode_tag(e, number, kUpb_WireType_Delimited);
435 }
436 
encode_map(upb_encstate * e,const upb_Message * msg,const upb_MiniTableSubInternal * subs,const upb_MiniTableField * f)437 static void encode_map(upb_encstate* e, const upb_Message* msg,
438                        const upb_MiniTableSubInternal* subs,
439                        const upb_MiniTableField* f) {
440   const upb_Map* map = *UPB_PTR_AT(msg, f->UPB_PRIVATE(offset), const upb_Map*);
441   const upb_MiniTable* layout = _upb_Encoder_GetSubMiniTable(subs, f);
442   UPB_ASSERT(upb_MiniTable_FieldCount(layout) == 2);
443 
444   if (!map || !upb_Map_Size(map)) return;
445 
446   if (e->options & kUpb_EncodeOption_Deterministic) {
447     _upb_sortedmap sorted;
448     _upb_mapsorter_pushmap(
449         &e->sorter, layout->UPB_PRIVATE(fields)[0].UPB_PRIVATE(descriptortype),
450         map, &sorted);
451     upb_MapEntry ent;
452     while (_upb_sortedmap_next(&e->sorter, map, &sorted, &ent)) {
453       encode_mapentry(e, upb_MiniTableField_Number(f), layout, &ent);
454     }
455     _upb_mapsorter_popmap(&e->sorter, &sorted);
456   } else {
457     intptr_t iter = UPB_STRTABLE_BEGIN;
458     upb_StringView key;
459     upb_value val;
460     while (upb_strtable_next2(&map->table, &key, &val, &iter)) {
461       upb_MapEntry ent;
462       _upb_map_fromkey(key, &ent.k, map->key_size);
463       _upb_map_fromvalue(val, &ent.v, map->val_size);
464       encode_mapentry(e, upb_MiniTableField_Number(f), layout, &ent);
465     }
466   }
467 }
468 
encode_shouldencode(upb_encstate * e,const upb_Message * msg,const upb_MiniTableField * f)469 static bool encode_shouldencode(upb_encstate* e, const upb_Message* msg,
470                                 const upb_MiniTableField* f) {
471   if (f->presence == 0) {
472     // Proto3 presence or map/array.
473     const void* mem = UPB_PTR_AT(msg, f->UPB_PRIVATE(offset), void);
474     switch (UPB_PRIVATE(_upb_MiniTableField_GetRep)(f)) {
475       case kUpb_FieldRep_1Byte: {
476         char ch;
477         memcpy(&ch, mem, 1);
478         return ch != 0;
479       }
480       case kUpb_FieldRep_4Byte: {
481         uint32_t u32;
482         memcpy(&u32, mem, 4);
483         return u32 != 0;
484       }
485       case kUpb_FieldRep_8Byte: {
486         uint64_t u64;
487         memcpy(&u64, mem, 8);
488         return u64 != 0;
489       }
490       case kUpb_FieldRep_StringView: {
491         const upb_StringView* str = (const upb_StringView*)mem;
492         return str->size != 0;
493       }
494       default:
495         UPB_UNREACHABLE();
496     }
497   } else if (UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(f)) {
498     // Proto2 presence: hasbit.
499     return UPB_PRIVATE(_upb_Message_GetHasbit)(msg, f);
500   } else {
501     // Field is in a oneof.
502     return UPB_PRIVATE(_upb_Message_GetOneofCase)(msg, f) ==
503            upb_MiniTableField_Number(f);
504   }
505 }
506 
encode_field(upb_encstate * e,const upb_Message * msg,const upb_MiniTableSubInternal * subs,const upb_MiniTableField * field)507 static void encode_field(upb_encstate* e, const upb_Message* msg,
508                          const upb_MiniTableSubInternal* subs,
509                          const upb_MiniTableField* field) {
510   switch (UPB_PRIVATE(_upb_MiniTableField_Mode)(field)) {
511     case kUpb_FieldMode_Array:
512       encode_array(e, msg, subs, field);
513       break;
514     case kUpb_FieldMode_Map:
515       encode_map(e, msg, subs, field);
516       break;
517     case kUpb_FieldMode_Scalar:
518       encode_scalar(e, UPB_PTR_AT(msg, field->UPB_PRIVATE(offset), void), subs,
519                     field);
520       break;
521     default:
522       UPB_UNREACHABLE();
523   }
524 }
525 
encode_msgset_item(upb_encstate * e,const upb_Extension * ext)526 static void encode_msgset_item(upb_encstate* e, const upb_Extension* ext) {
527   size_t size;
528   encode_tag(e, kUpb_MsgSet_Item, kUpb_WireType_EndGroup);
529   encode_message(e, ext->data.msg_val,
530                  upb_MiniTableExtension_GetSubMessage(ext->ext), &size);
531   encode_varint(e, size);
532   encode_tag(e, kUpb_MsgSet_Message, kUpb_WireType_Delimited);
533   encode_varint(e, upb_MiniTableExtension_Number(ext->ext));
534   encode_tag(e, kUpb_MsgSet_TypeId, kUpb_WireType_Varint);
535   encode_tag(e, kUpb_MsgSet_Item, kUpb_WireType_StartGroup);
536 }
537 
encode_ext(upb_encstate * e,const upb_Extension * ext,bool is_message_set)538 static void encode_ext(upb_encstate* e, const upb_Extension* ext,
539                        bool is_message_set) {
540   if (UPB_UNLIKELY(is_message_set)) {
541     encode_msgset_item(e, ext);
542   } else {
543     upb_MiniTableSubInternal sub;
544     if (upb_MiniTableField_IsSubMessage(&ext->ext->UPB_PRIVATE(field))) {
545       sub.UPB_PRIVATE(submsg) = &ext->ext->UPB_PRIVATE(sub).UPB_PRIVATE(submsg);
546     } else {
547       sub.UPB_PRIVATE(subenum) =
548           ext->ext->UPB_PRIVATE(sub).UPB_PRIVATE(subenum);
549     }
550     encode_field(e, (upb_Message*)&ext->data, &sub,
551                  &ext->ext->UPB_PRIVATE(field));
552   }
553 }
554 
encode_message(upb_encstate * e,const upb_Message * msg,const upb_MiniTable * m,size_t * size)555 static void encode_message(upb_encstate* e, const upb_Message* msg,
556                            const upb_MiniTable* m, size_t* size) {
557   size_t pre_len = e->limit - e->ptr;
558 
559   if (e->options & kUpb_EncodeOption_CheckRequired) {
560     if (m->UPB_PRIVATE(required_count)) {
561       if (!UPB_PRIVATE(_upb_Message_IsInitializedShallow)(msg, m)) {
562         encode_err(e, kUpb_EncodeStatus_MissingRequired);
563       }
564     }
565   }
566 
567   if ((e->options & kUpb_EncodeOption_SkipUnknown) == 0) {
568     size_t unknown_size;
569     const char* unknown = upb_Message_GetUnknown(msg, &unknown_size);
570 
571     if (unknown) {
572       encode_bytes(e, unknown, unknown_size);
573     }
574   }
575 
576   if (m->UPB_PRIVATE(ext) != kUpb_ExtMode_NonExtendable) {
577     /* Encode all extensions together. Unlike C++, we do not attempt to keep
578      * these in field number order relative to normal fields or even to each
579      * other. */
580     size_t ext_count;
581     const upb_Extension* ext =
582         UPB_PRIVATE(_upb_Message_Getexts)(msg, &ext_count);
583     if (ext_count) {
584       if (e->options & kUpb_EncodeOption_Deterministic) {
585         _upb_sortedmap sorted;
586         _upb_mapsorter_pushexts(&e->sorter, ext, ext_count, &sorted);
587         while (_upb_sortedmap_nextext(&e->sorter, &sorted, &ext)) {
588           encode_ext(e, ext, m->UPB_PRIVATE(ext) == kUpb_ExtMode_IsMessageSet);
589         }
590         _upb_mapsorter_popmap(&e->sorter, &sorted);
591       } else {
592         const upb_Extension* end = ext + ext_count;
593         for (; ext != end; ext++) {
594           encode_ext(e, ext, m->UPB_PRIVATE(ext) == kUpb_ExtMode_IsMessageSet);
595         }
596       }
597     }
598   }
599 
600   if (upb_MiniTable_FieldCount(m)) {
601     const upb_MiniTableField* f =
602         &m->UPB_PRIVATE(fields)[m->UPB_PRIVATE(field_count)];
603     const upb_MiniTableField* first = &m->UPB_PRIVATE(fields)[0];
604     while (f != first) {
605       f--;
606       if (encode_shouldencode(e, msg, f)) {
607         encode_field(e, msg, m->UPB_PRIVATE(subs), f);
608       }
609     }
610   }
611 
612   *size = (e->limit - e->ptr) - pre_len;
613 }
614 
upb_Encoder_Encode(upb_encstate * const encoder,const upb_Message * const msg,const upb_MiniTable * const l,char ** const buf,size_t * const size,bool prepend_len)615 static upb_EncodeStatus upb_Encoder_Encode(upb_encstate* const encoder,
616                                            const upb_Message* const msg,
617                                            const upb_MiniTable* const l,
618                                            char** const buf, size_t* const size,
619                                            bool prepend_len) {
620   // Unfortunately we must continue to perform hackery here because there are
621   // code paths which blindly copy the returned pointer without bothering to
622   // check for errors until much later (b/235839510). So we still set *buf to
623   // NULL on error and we still set it to non-NULL on a successful empty result.
624   if (UPB_SETJMP(encoder->err) == 0) {
625     size_t encoded_msg_size;
626     encode_message(encoder, msg, l, &encoded_msg_size);
627     if (prepend_len) {
628       encode_varint(encoder, encoded_msg_size);
629     }
630     *size = encoder->limit - encoder->ptr;
631     if (*size == 0) {
632       static char ch;
633       *buf = &ch;
634     } else {
635       UPB_ASSERT(encoder->ptr);
636       *buf = encoder->ptr;
637     }
638   } else {
639     UPB_ASSERT(encoder->status != kUpb_EncodeStatus_Ok);
640     *buf = NULL;
641     *size = 0;
642   }
643 
644   _upb_mapsorter_destroy(&encoder->sorter);
645   return encoder->status;
646 }
647 
_upb_Encode(const upb_Message * msg,const upb_MiniTable * l,int options,upb_Arena * arena,char ** buf,size_t * size,bool prepend_len)648 static upb_EncodeStatus _upb_Encode(const upb_Message* msg,
649                                     const upb_MiniTable* l, int options,
650                                     upb_Arena* arena, char** buf, size_t* size,
651                                     bool prepend_len) {
652   upb_encstate e;
653   unsigned depth = (unsigned)options >> 16;
654 
655   e.status = kUpb_EncodeStatus_Ok;
656   e.arena = arena;
657   e.buf = NULL;
658   e.limit = NULL;
659   e.ptr = NULL;
660   e.depth = depth ? depth : kUpb_WireFormat_DefaultDepthLimit;
661   e.options = options;
662   _upb_mapsorter_init(&e.sorter);
663 
664   return upb_Encoder_Encode(&e, msg, l, buf, size, prepend_len);
665 }
666 
upb_Encode(const upb_Message * msg,const upb_MiniTable * l,int options,upb_Arena * arena,char ** buf,size_t * size)667 upb_EncodeStatus upb_Encode(const upb_Message* msg, const upb_MiniTable* l,
668                             int options, upb_Arena* arena, char** buf,
669                             size_t* size) {
670   return _upb_Encode(msg, l, options, arena, buf, size, false);
671 }
672 
upb_EncodeLengthPrefixed(const upb_Message * msg,const upb_MiniTable * l,int options,upb_Arena * arena,char ** buf,size_t * size)673 upb_EncodeStatus upb_EncodeLengthPrefixed(const upb_Message* msg,
674                                           const upb_MiniTable* l, int options,
675                                           upb_Arena* arena, char** buf,
676                                           size_t* size) {
677   return _upb_Encode(msg, l, options, arena, buf, size, true);
678 }
679 
upb_EncodeStatus_String(upb_EncodeStatus status)680 const char* upb_EncodeStatus_String(upb_EncodeStatus status) {
681   switch (status) {
682     case kUpb_EncodeStatus_Ok:
683       return "Ok";
684     case kUpb_EncodeStatus_MissingRequired:
685       return "Missing required field";
686     case kUpb_EncodeStatus_MaxDepthExceeded:
687       return "Max depth exceeded";
688     case kUpb_EncodeStatus_OutOfMemory:
689       return "Arena alloc failed";
690     default:
691       return "Unknown encode status";
692   }
693 }
694