1 /* pb_decode.c -- decode a protobuf using minimal resources
2  *
3  * 2011 Petteri Aimonen <jpa@kapsi.fi>
4  */
5 
6 /* Use the GCC warn_unused_result attribute to check that all return values
7  * are propagated correctly. On other compilers and gcc before 3.4.0 just
8  * ignore the annotation.
9  */
10 #if !defined(__GNUC__) || ( __GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
11     #define checkreturn
12 #else
13     #define checkreturn __attribute__((warn_unused_result))
14 #endif
15 
16 #include "pb.h"
17 #include "pb_decode.h"
18 
19 /**************************************
20  * Declarations internal to this file *
21  **************************************/
22 
23 /* Iterator for pb_field_t list */
24 typedef struct {
25     const pb_field_t *start; /* Start of the pb_field_t array */
26     const pb_field_t *pos; /* Current position of the iterator */
27     unsigned field_index; /* Zero-based index of the field. */
28     unsigned required_field_index; /* Zero-based index that counts only the required fields */
29     void *dest_struct; /* Pointer to the destination structure to decode to */
30     void *pData; /* Pointer where to store current field value */
31     void *pSize; /* Pointer where to store the size of current array field */
32 } pb_field_iterator_t;
33 
34 typedef bool (*pb_decoder_t)(pb_istream_t *stream, const pb_field_t *field, void *dest) checkreturn;
35 
36 static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count);
37 static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest);
38 static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, uint8_t *buf, size_t *size);
39 static void pb_field_init(pb_field_iterator_t *iter, const pb_field_t *fields, void *dest_struct);
40 static bool pb_field_next(pb_field_iterator_t *iter);
41 static bool checkreturn pb_field_find(pb_field_iterator_t *iter, uint32_t tag);
42 static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter);
43 static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter);
44 static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter);
45 static bool checkreturn default_extension_decoder(pb_istream_t *stream, pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type);
46 static bool checkreturn decode_extension(pb_istream_t *stream, uint32_t tag, pb_wire_type_t wire_type, pb_field_iterator_t *iter);
47 static bool checkreturn find_extension_field(pb_field_iterator_t *iter);
48 static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct);
49 static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest);
50 static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
51 static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
52 static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest);
53 static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest);
54 static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest);
55 static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest);
56 static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest);
57 static bool checkreturn pb_skip_varint(pb_istream_t *stream);
58 static bool checkreturn pb_skip_string(pb_istream_t *stream);
59 
60 /* --- Function pointers to field decoders ---
61  * Order in the array must match pb_action_t LTYPE numbering.
62  */
63 static const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = {
64     &pb_dec_varint,
65     &pb_dec_uvarint,
66     &pb_dec_svarint,
67     &pb_dec_fixed32,
68     &pb_dec_fixed64,
69 
70     &pb_dec_bytes,
71     &pb_dec_string,
72     &pb_dec_submessage,
73     NULL /* extensions */
74 };
75 
76 /*******************************
77  * pb_istream_t implementation *
78  *******************************/
79 
buf_read(pb_istream_t * stream,uint8_t * buf,size_t count)80 static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count)
81 {
82     uint8_t *source = (uint8_t*)stream->state;
83     stream->state = source + count;
84 
85     if (buf != NULL)
86     {
87         while (count--)
88             *buf++ = *source++;
89     }
90 
91     return true;
92 }
93 
pb_read(pb_istream_t * stream,uint8_t * buf,size_t count)94 bool checkreturn pb_read(pb_istream_t *stream, uint8_t *buf, size_t count)
95 {
96 #ifndef PB_BUFFER_ONLY
97 	if (buf == NULL && stream->callback != buf_read)
98 	{
99 		/* Skip input bytes */
100 		uint8_t tmp[16];
101 		while (count > 16)
102 		{
103 			if (!pb_read(stream, tmp, 16))
104 				return false;
105 
106 			count -= 16;
107 		}
108 
109 		return pb_read(stream, tmp, count);
110 	}
111 #endif
112 
113     if (stream->bytes_left < count)
114         PB_RETURN_ERROR(stream, "end-of-stream");
115 
116 #ifndef PB_BUFFER_ONLY
117     if (!stream->callback(stream, buf, count))
118         PB_RETURN_ERROR(stream, "io error");
119 #else
120     if (!buf_read(stream, buf, count))
121         return false;
122 #endif
123 
124     stream->bytes_left -= count;
125     return true;
126 }
127 
128 /* Read a single byte from input stream. buf may not be NULL.
129  * This is an optimization for the varint decoding. */
pb_readbyte(pb_istream_t * stream,uint8_t * buf)130 static bool checkreturn pb_readbyte(pb_istream_t *stream, uint8_t *buf)
131 {
132     if (stream->bytes_left == 0)
133         PB_RETURN_ERROR(stream, "end-of-stream");
134 
135 #ifndef PB_BUFFER_ONLY
136     if (!stream->callback(stream, buf, 1))
137         PB_RETURN_ERROR(stream, "io error");
138 #else
139     *buf = *(uint8_t*)stream->state;
140     stream->state = (uint8_t*)stream->state + 1;
141 #endif
142 
143     stream->bytes_left--;
144 
145     return true;
146 }
147 
pb_istream_from_buffer(uint8_t * buf,size_t bufsize)148 pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize)
149 {
150     pb_istream_t stream;
151 #ifdef PB_BUFFER_ONLY
152     stream.callback = NULL;
153 #else
154     stream.callback = &buf_read;
155 #endif
156     stream.state = buf;
157     stream.bytes_left = bufsize;
158 #ifndef PB_NO_ERRMSG
159     stream.errmsg = NULL;
160 #endif
161     return stream;
162 }
163 
164 /********************
165  * Helper functions *
166  ********************/
167 
pb_decode_varint32(pb_istream_t * stream,uint32_t * dest)168 static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
169 {
170     uint8_t byte;
171     uint32_t result;
172 
173     if (!pb_readbyte(stream, &byte))
174         return false;
175 
176     if ((byte & 0x80) == 0)
177     {
178         /* Quick case, 1 byte value */
179         result = byte;
180     }
181     else
182     {
183         /* Multibyte case */
184         uint8_t bitpos = 7;
185         result = byte & 0x7F;
186 
187         do
188         {
189             if (bitpos >= 32)
190                 PB_RETURN_ERROR(stream, "varint overflow");
191 
192             if (!pb_readbyte(stream, &byte))
193                 return false;
194 
195             result |= (uint32_t)(byte & 0x7F) << bitpos;
196             bitpos = (uint8_t)(bitpos + 7);
197         } while (byte & 0x80);
198    }
199 
200    *dest = result;
201    return true;
202 }
203 
pb_decode_varint(pb_istream_t * stream,uint64_t * dest)204 bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
205 {
206     uint8_t byte;
207     uint8_t bitpos = 0;
208     uint64_t result = 0;
209 
210     do
211     {
212         if (bitpos >= 64)
213             PB_RETURN_ERROR(stream, "varint overflow");
214 
215         if (!pb_readbyte(stream, &byte))
216             return false;
217 
218         result |= (uint64_t)(byte & 0x7F) << bitpos;
219         bitpos = (uint8_t)(bitpos + 7);
220     } while (byte & 0x80);
221 
222     *dest = result;
223     return true;
224 }
225 
pb_skip_varint(pb_istream_t * stream)226 bool checkreturn pb_skip_varint(pb_istream_t *stream)
227 {
228     uint8_t byte;
229     do
230     {
231         if (!pb_read(stream, &byte, 1))
232             return false;
233     } while (byte & 0x80);
234     return true;
235 }
236 
pb_skip_string(pb_istream_t * stream)237 bool checkreturn pb_skip_string(pb_istream_t *stream)
238 {
239     uint32_t length;
240     if (!pb_decode_varint32(stream, &length))
241         return false;
242 
243     return pb_read(stream, NULL, length);
244 }
245 
pb_decode_tag(pb_istream_t * stream,pb_wire_type_t * wire_type,uint32_t * tag,bool * eof)246 bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
247 {
248     uint32_t temp;
249     *eof = false;
250     *wire_type = (pb_wire_type_t) 0;
251     *tag = 0;
252 
253     if (!pb_decode_varint32(stream, &temp))
254     {
255         if (stream->bytes_left == 0)
256             *eof = true;
257 
258         return false;
259     }
260 
261     if (temp == 0)
262     {
263         *eof = true; /* Special feature: allow 0-terminated messages. */
264         return false;
265     }
266 
267     *tag = temp >> 3;
268     *wire_type = (pb_wire_type_t)(temp & 7);
269     return true;
270 }
271 
pb_skip_field(pb_istream_t * stream,pb_wire_type_t wire_type)272 bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
273 {
274     switch (wire_type)
275     {
276         case PB_WT_VARINT: return pb_skip_varint(stream);
277         case PB_WT_64BIT: return pb_read(stream, NULL, 8);
278         case PB_WT_STRING: return pb_skip_string(stream);
279         case PB_WT_32BIT: return pb_read(stream, NULL, 4);
280         default: PB_RETURN_ERROR(stream, "invalid wire_type");
281     }
282 }
283 
284 /* Read a raw value to buffer, for the purpose of passing it to callback as
285  * a substream. Size is maximum size on call, and actual size on return.
286  */
read_raw_value(pb_istream_t * stream,pb_wire_type_t wire_type,uint8_t * buf,size_t * size)287 static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, uint8_t *buf, size_t *size)
288 {
289     size_t max_size = *size;
290     switch (wire_type)
291     {
292         case PB_WT_VARINT:
293             *size = 0;
294             do
295             {
296                 (*size)++;
297                 if (*size > max_size) return false;
298                 if (!pb_read(stream, buf, 1)) return false;
299             } while (*buf++ & 0x80);
300             return true;
301 
302         case PB_WT_64BIT:
303             *size = 8;
304             return pb_read(stream, buf, 8);
305 
306         case PB_WT_32BIT:
307             *size = 4;
308             return pb_read(stream, buf, 4);
309 
310         default: PB_RETURN_ERROR(stream, "invalid wire_type");
311     }
312 }
313 
314 /* Decode string length from stream and return a substream with limited length.
315  * Remember to close the substream using pb_close_string_substream().
316  */
pb_make_string_substream(pb_istream_t * stream,pb_istream_t * substream)317 bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
318 {
319     uint32_t size;
320     if (!pb_decode_varint32(stream, &size))
321         return false;
322 
323     *substream = *stream;
324     if (substream->bytes_left < size)
325         PB_RETURN_ERROR(stream, "parent stream too short");
326 
327     substream->bytes_left = size;
328     stream->bytes_left -= size;
329     return true;
330 }
331 
pb_close_string_substream(pb_istream_t * stream,pb_istream_t * substream)332 void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
333 {
334     stream->state = substream->state;
335 
336 #ifndef PB_NO_ERRMSG
337     stream->errmsg = substream->errmsg;
338 #endif
339 }
340 
pb_field_init(pb_field_iterator_t * iter,const pb_field_t * fields,void * dest_struct)341 static void pb_field_init(pb_field_iterator_t *iter, const pb_field_t *fields, void *dest_struct)
342 {
343     iter->start = iter->pos = fields;
344     iter->field_index = 0;
345     iter->required_field_index = 0;
346     iter->pData = (char*)dest_struct + iter->pos->data_offset;
347     iter->pSize = (char*)iter->pData + iter->pos->size_offset;
348     iter->dest_struct = dest_struct;
349 }
350 
pb_field_next(pb_field_iterator_t * iter)351 static bool pb_field_next(pb_field_iterator_t *iter)
352 {
353     bool notwrapped = true;
354     size_t prev_size = iter->pos->data_size;
355 
356     if (PB_ATYPE(iter->pos->type) == PB_ATYPE_STATIC &&
357         PB_HTYPE(iter->pos->type) == PB_HTYPE_REPEATED)
358     {
359         prev_size *= iter->pos->array_size;
360     }
361     else if (PB_ATYPE(iter->pos->type) == PB_ATYPE_POINTER)
362     {
363         prev_size = sizeof(void*);
364     }
365 
366     if (iter->pos->tag == 0)
367         return false; /* Only happens with empty message types */
368 
369     if (PB_HTYPE(iter->pos->type) == PB_HTYPE_REQUIRED)
370         iter->required_field_index++;
371 
372     iter->pos++;
373     iter->field_index++;
374     if (iter->pos->tag == 0)
375     {
376         iter->pos = iter->start;
377         iter->field_index = 0;
378         iter->required_field_index = 0;
379         iter->pData = iter->dest_struct;
380         prev_size = 0;
381         notwrapped = false;
382     }
383 
384     iter->pData = (char*)iter->pData + prev_size + iter->pos->data_offset;
385     iter->pSize = (char*)iter->pData + iter->pos->size_offset;
386     return notwrapped;
387 }
388 
pb_field_find(pb_field_iterator_t * iter,uint32_t tag)389 static bool checkreturn pb_field_find(pb_field_iterator_t *iter, uint32_t tag)
390 {
391     unsigned start = iter->field_index;
392 
393     do {
394         if (iter->pos->tag == tag &&
395             PB_LTYPE(iter->pos->type) != PB_LTYPE_EXTENSION)
396         {
397             return true;
398         }
399         (void)pb_field_next(iter);
400     } while (iter->field_index != start);
401 
402     return false;
403 }
404 
405 /*************************
406  * Decode a single field *
407  *************************/
408 
decode_static_field(pb_istream_t * stream,pb_wire_type_t wire_type,pb_field_iterator_t * iter)409 static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter)
410 {
411     pb_type_t type;
412     pb_decoder_t func;
413 
414     type = iter->pos->type;
415     func = PB_DECODERS[PB_LTYPE(type)];
416 
417     switch (PB_HTYPE(type))
418     {
419         case PB_HTYPE_REQUIRED:
420             return func(stream, iter->pos, iter->pData);
421 
422         case PB_HTYPE_OPTIONAL:
423             *(bool*)iter->pSize = true;
424             return func(stream, iter->pos, iter->pData);
425 
426         case PB_HTYPE_REPEATED:
427             if (wire_type == PB_WT_STRING
428                 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
429             {
430                 /* Packed array */
431                 bool status = true;
432                 size_t *size = (size_t*)iter->pSize;
433                 pb_istream_t substream;
434                 if (!pb_make_string_substream(stream, &substream))
435                     return false;
436 
437                 while (substream.bytes_left > 0 && *size < iter->pos->array_size)
438                 {
439                     void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
440                     if (!func(&substream, iter->pos, pItem))
441                     {
442                         status = false;
443                         break;
444                     }
445                     (*size)++;
446                 }
447                 pb_close_string_substream(stream, &substream);
448 
449                 if (substream.bytes_left != 0)
450                     PB_RETURN_ERROR(stream, "array overflow");
451 
452                 return status;
453             }
454             else
455             {
456                 /* Repeated field */
457                 size_t *size = (size_t*)iter->pSize;
458                 void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
459                 if (*size >= iter->pos->array_size)
460                     PB_RETURN_ERROR(stream, "array overflow");
461 
462                 (*size)++;
463                 return func(stream, iter->pos, pItem);
464             }
465 
466         default:
467             PB_RETURN_ERROR(stream, "invalid field type");
468     }
469 }
470 
471 #ifdef PB_ENABLE_MALLOC
472 /* Allocate storage for the field and store the pointer at iter->pData.
473  * array_size is the number of entries to reserve in an array. */
allocate_field(pb_istream_t * stream,void * pData,size_t data_size,size_t array_size)474 static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
475 {
476     void *ptr = *(void**)pData;
477     size_t size = array_size * data_size;
478 
479     /* Allocate new or expand previous allocation */
480     /* Note: on failure the old pointer will remain in the structure,
481      * the message must be freed by caller also on error return. */
482     ptr = pb_realloc(ptr, size);
483     if (ptr == NULL)
484         PB_RETURN_ERROR(stream, "realloc failed");
485 
486     *(void**)pData = ptr;
487     return true;
488 }
489 
490 /* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
initialize_pointer_field(void * pItem,pb_field_iterator_t * iter)491 static void initialize_pointer_field(void *pItem, pb_field_iterator_t *iter)
492 {
493     if (PB_LTYPE(iter->pos->type) == PB_LTYPE_STRING ||
494         PB_LTYPE(iter->pos->type) == PB_LTYPE_BYTES)
495     {
496         *(void**)pItem = NULL;
497     }
498     else if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
499     {
500         pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, pItem);
501     }
502 }
503 #endif
504 
decode_pointer_field(pb_istream_t * stream,pb_wire_type_t wire_type,pb_field_iterator_t * iter)505 static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter)
506 {
507 #ifndef PB_ENABLE_MALLOC
508     UNUSED(wire_type);
509     UNUSED(iter);
510     PB_RETURN_ERROR(stream, "no malloc support");
511 #else
512     pb_type_t type;
513     pb_decoder_t func;
514 
515     type = iter->pos->type;
516     func = PB_DECODERS[PB_LTYPE(type)];
517 
518     switch (PB_HTYPE(type))
519     {
520         case PB_HTYPE_REQUIRED:
521         case PB_HTYPE_OPTIONAL:
522             if (PB_LTYPE(type) == PB_LTYPE_STRING ||
523                 PB_LTYPE(type) == PB_LTYPE_BYTES)
524             {
525                 return func(stream, iter->pos, iter->pData);
526             }
527             else
528             {
529                 if (!allocate_field(stream, iter->pData, iter->pos->data_size, 1))
530                     return false;
531 
532                 initialize_pointer_field(*(void**)iter->pData, iter);
533                 return func(stream, iter->pos, *(void**)iter->pData);
534             }
535 
536         case PB_HTYPE_REPEATED:
537             if (wire_type == PB_WT_STRING
538                 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
539             {
540                 /* Packed array, multiple items come in at once. */
541                 bool status = true;
542                 size_t *size = (size_t*)iter->pSize;
543                 size_t allocated_size = *size;
544                 void *pItem;
545                 pb_istream_t substream;
546 
547                 if (!pb_make_string_substream(stream, &substream))
548                     return false;
549 
550                 while (substream.bytes_left)
551                 {
552                     if (*size + 1 > allocated_size)
553                     {
554                         /* Allocate more storage. This tries to guess the
555                          * number of remaining entries. Round the division
556                          * upwards. */
557                         allocated_size += (substream.bytes_left - 1) / iter->pos->data_size + 1;
558 
559                         if (!allocate_field(&substream, iter->pData, iter->pos->data_size, allocated_size))
560                         {
561                             status = false;
562                             break;
563                         }
564                     }
565 
566                     /* Decode the array entry */
567                     pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size);
568                     initialize_pointer_field(pItem, iter);
569                     if (!func(&substream, iter->pos, pItem))
570                     {
571                         status = false;
572                         break;
573                     }
574                     (*size)++;
575                 }
576                 pb_close_string_substream(stream, &substream);
577 
578                 return status;
579             }
580             else
581             {
582                 /* Normal repeated field, i.e. only one item at a time. */
583                 size_t *size = (size_t*)iter->pSize;
584                 void *pItem;
585 
586                 (*size)++;
587                 if (!allocate_field(stream, iter->pData, iter->pos->data_size, *size))
588                     return false;
589 
590                 pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size - 1);
591                 initialize_pointer_field(pItem, iter);
592                 return func(stream, iter->pos, pItem);
593             }
594 
595         default:
596             PB_RETURN_ERROR(stream, "invalid field type");
597     }
598 #endif
599 }
600 
decode_callback_field(pb_istream_t * stream,pb_wire_type_t wire_type,pb_field_iterator_t * iter)601 static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter)
602 {
603     pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
604 
605 #ifdef PB_OLD_CALLBACK_STYLE
606     void *arg = pCallback->arg;
607 #else
608     void **arg = &(pCallback->arg);
609 #endif
610 
611     if (pCallback->funcs.decode == NULL)
612         return pb_skip_field(stream, wire_type);
613 
614     if (wire_type == PB_WT_STRING)
615     {
616         pb_istream_t substream;
617 
618         if (!pb_make_string_substream(stream, &substream))
619             return false;
620 
621         do
622         {
623             if (!pCallback->funcs.decode(&substream, iter->pos, arg))
624                 PB_RETURN_ERROR(stream, "callback failed");
625         } while (substream.bytes_left);
626 
627         pb_close_string_substream(stream, &substream);
628         return true;
629     }
630     else
631     {
632         /* Copy the single scalar value to stack.
633          * This is required so that we can limit the stream length,
634          * which in turn allows to use same callback for packed and
635          * not-packed fields. */
636         pb_istream_t substream;
637         uint8_t buffer[10];
638         size_t size = sizeof(buffer);
639 
640         if (!read_raw_value(stream, wire_type, buffer, &size))
641             return false;
642         substream = pb_istream_from_buffer(buffer, size);
643 
644         return pCallback->funcs.decode(&substream, iter->pos, arg);
645     }
646 }
647 
decode_field(pb_istream_t * stream,pb_wire_type_t wire_type,pb_field_iterator_t * iter)648 static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter)
649 {
650     switch (PB_ATYPE(iter->pos->type))
651     {
652         case PB_ATYPE_STATIC:
653             return decode_static_field(stream, wire_type, iter);
654 
655         case PB_ATYPE_POINTER:
656             return decode_pointer_field(stream, wire_type, iter);
657 
658         case PB_ATYPE_CALLBACK:
659             return decode_callback_field(stream, wire_type, iter);
660 
661         default:
662             PB_RETURN_ERROR(stream, "invalid field type");
663     }
664 }
665 
666 /* Default handler for extension fields. Expects a pb_field_t structure
667  * in extension->type->arg. */
default_extension_decoder(pb_istream_t * stream,pb_extension_t * extension,uint32_t tag,pb_wire_type_t wire_type)668 static bool checkreturn default_extension_decoder(pb_istream_t *stream,
669     pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
670 {
671     const pb_field_t *field = (const pb_field_t*)extension->type->arg;
672     pb_field_iterator_t iter;
673 
674     if (field->tag != tag)
675         return true;
676 
677     iter.start = field;
678     iter.pos = field;
679     iter.field_index = 0;
680     iter.required_field_index = 0;
681     iter.dest_struct = extension->dest;
682     iter.pData = extension->dest;
683     iter.pSize = &extension->found;
684 
685     return decode_field(stream, wire_type, &iter);
686 }
687 
688 /* Try to decode an unknown field as an extension field. Tries each extension
689  * decoder in turn, until one of them handles the field or loop ends. */
decode_extension(pb_istream_t * stream,uint32_t tag,pb_wire_type_t wire_type,pb_field_iterator_t * iter)690 static bool checkreturn decode_extension(pb_istream_t *stream,
691     uint32_t tag, pb_wire_type_t wire_type, pb_field_iterator_t *iter)
692 {
693     pb_extension_t *extension = *(pb_extension_t* const *)iter->pData;
694     size_t pos = stream->bytes_left;
695 
696     while (extension != NULL && pos == stream->bytes_left)
697     {
698         bool status;
699         if (extension->type->decode)
700             status = extension->type->decode(stream, extension, tag, wire_type);
701         else
702             status = default_extension_decoder(stream, extension, tag, wire_type);
703 
704         if (!status)
705             return false;
706 
707         extension = extension->next;
708     }
709 
710     return true;
711 }
712 
713 /* Step through the iterator until an extension field is found or until all
714  * entries have been checked. There can be only one extension field per
715  * message. Returns false if no extension field is found. */
find_extension_field(pb_field_iterator_t * iter)716 static bool checkreturn find_extension_field(pb_field_iterator_t *iter)
717 {
718     unsigned start = iter->field_index;
719 
720     do {
721         if (PB_LTYPE(iter->pos->type) == PB_LTYPE_EXTENSION)
722             return true;
723         (void)pb_field_next(iter);
724     } while (iter->field_index != start);
725 
726     return false;
727 }
728 
729 /* Initialize message fields to default values, recursively */
pb_message_set_to_defaults(const pb_field_t fields[],void * dest_struct)730 static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
731 {
732     pb_field_iterator_t iter;
733     pb_field_init(&iter, fields, dest_struct);
734 
735     do
736     {
737         pb_type_t type;
738         type = iter.pos->type;
739 
740         /* Avoid crash on empty message types (zero fields) */
741         if (iter.pos->tag == 0)
742             continue;
743 
744         if (PB_ATYPE(type) == PB_ATYPE_STATIC)
745         {
746             if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL)
747             {
748                 /* Set has_field to false. Still initialize the optional field
749                  * itself also. */
750                 *(bool*)iter.pSize = false;
751             }
752             else if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
753             {
754                 /* Set array count to 0, no need to initialize contents. */
755                 *(size_t*)iter.pSize = 0;
756                 continue;
757             }
758 
759             if (PB_LTYPE(iter.pos->type) == PB_LTYPE_SUBMESSAGE)
760             {
761                 /* Initialize submessage to defaults */
762                 pb_message_set_to_defaults((const pb_field_t *) iter.pos->ptr, iter.pData);
763             }
764             else if (iter.pos->ptr != NULL)
765             {
766                 /* Initialize to default value */
767                 memcpy(iter.pData, iter.pos->ptr, iter.pos->data_size);
768             }
769             else
770             {
771                 /* Initialize to zeros */
772                 memset(iter.pData, 0, iter.pos->data_size);
773             }
774         }
775         else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
776         {
777             /* Initialize the pointer to NULL. */
778             *(void**)iter.pData = NULL;
779 
780             /* Initialize array count to 0. */
781             if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
782             {
783                 *(size_t*)iter.pSize = 0;
784             }
785         }
786         else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
787         {
788             /* Don't overwrite callback */
789         }
790     } while (pb_field_next(&iter));
791 }
792 
793 /*********************
794  * Decode all fields *
795  *********************/
796 
pb_decode_noinit(pb_istream_t * stream,const pb_field_t fields[],void * dest_struct)797 bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
798 {
799     uint8_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 7) / 8] = {0, 0, 0, 0, 0, 0, 0, 0};
800     uint32_t extension_range_start = 0;
801     pb_field_iterator_t iter;
802 
803     pb_field_init(&iter, fields, dest_struct);
804 
805     while (stream->bytes_left)
806     {
807         uint32_t tag;
808         pb_wire_type_t wire_type;
809         bool eof;
810 
811         if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
812         {
813             if (eof)
814                 break;
815             else
816                 return false;
817         }
818 
819         if (!pb_field_find(&iter, tag))
820         {
821             /* No match found, check if it matches an extension. */
822             if (tag >= extension_range_start)
823             {
824                 if (!find_extension_field(&iter))
825                     extension_range_start = (uint32_t)-1;
826                 else
827                     extension_range_start = iter.pos->tag;
828 
829                 if (tag >= extension_range_start)
830                 {
831                     size_t pos = stream->bytes_left;
832 
833                     if (!decode_extension(stream, tag, wire_type, &iter))
834                         return false;
835 
836                     if (pos != stream->bytes_left)
837                     {
838                         /* The field was handled */
839                         continue;
840                     }
841                 }
842             }
843 
844             /* No match found, skip data */
845             if (!pb_skip_field(stream, wire_type))
846                 return false;
847             continue;
848         }
849 
850         if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REQUIRED
851             && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
852         {
853             fields_seen[iter.required_field_index >> 3] |= (uint8_t)(1 << (iter.required_field_index & 7));
854         }
855 
856         if (!decode_field(stream, wire_type, &iter))
857             return false;
858     }
859 
860     /* Check that all required fields were present. */
861     {
862         /* First figure out the number of required fields by
863          * seeking to the end of the field array. Usually we
864          * are already close to end after decoding.
865          */
866         unsigned req_field_count;
867         pb_type_t last_type;
868         unsigned i;
869         do {
870             req_field_count = iter.required_field_index;
871             last_type = iter.pos->type;
872         } while (pb_field_next(&iter));
873 
874         /* Fixup if last field was also required. */
875         if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.pos->tag != 0)
876             req_field_count++;
877 
878         /* Check the whole bytes */
879         for (i = 0; i < (req_field_count >> 3); i++)
880         {
881             if (fields_seen[i] != 0xFF)
882                 PB_RETURN_ERROR(stream, "missing required field");
883         }
884 
885         /* Check the remaining bits */
886         if (fields_seen[req_field_count >> 3] != (0xFF >> (8 - (req_field_count & 7))))
887             PB_RETURN_ERROR(stream, "missing required field");
888     }
889 
890     return true;
891 }
892 
pb_decode(pb_istream_t * stream,const pb_field_t fields[],void * dest_struct)893 bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
894 {
895     bool status;
896     pb_message_set_to_defaults(fields, dest_struct);
897     status = pb_decode_noinit(stream, fields, dest_struct);
898 
899 #ifdef PB_ENABLE_MALLOC
900     if (!status)
901         pb_release(fields, dest_struct);
902 #endif
903 
904     return status;
905 }
906 
pb_decode_delimited(pb_istream_t * stream,const pb_field_t fields[],void * dest_struct)907 bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
908 {
909     pb_istream_t substream;
910     bool status;
911 
912     if (!pb_make_string_substream(stream, &substream))
913         return false;
914 
915     status = pb_decode(&substream, fields, dest_struct);
916     pb_close_string_substream(stream, &substream);
917     return status;
918 }
919 
920 #ifdef PB_ENABLE_MALLOC
pb_release(const pb_field_t fields[],void * dest_struct)921 void pb_release(const pb_field_t fields[], void *dest_struct)
922 {
923     pb_field_iterator_t iter;
924     pb_field_init(&iter, fields, dest_struct);
925 
926     do
927     {
928         pb_type_t type;
929         type = iter.pos->type;
930 
931         /* Avoid crash on empty message types (zero fields) */
932         if (iter.pos->tag == 0)
933             continue;
934 
935         if (PB_ATYPE(type) == PB_ATYPE_POINTER)
936         {
937             if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
938                 (PB_LTYPE(type) == PB_LTYPE_STRING ||
939                  PB_LTYPE(type) == PB_LTYPE_BYTES))
940             {
941                 /* Release entries in repeated string or bytes array */
942                 void **pItem = *(void***)iter.pData;
943                 size_t count = *(size_t*)iter.pSize;
944                 while (count--)
945                 {
946                     pb_free(*pItem);
947                     *pItem++ = NULL;
948                 }
949             }
950             else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
951             {
952                 /* Release fields in submessages */
953                 void *pItem = *(void**)iter.pData;
954                 size_t count = (pItem ? 1 : 0);
955 
956                 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
957                 {
958                     count = *(size_t*)iter.pSize;
959                 }
960 
961                 while (count--)
962                 {
963                     pb_release((const pb_field_t*)iter.pos->ptr, pItem);
964                     pItem = (uint8_t*)pItem + iter.pos->data_size;
965                 }
966             }
967 
968             /* Release main item */
969             pb_free(*(void**)iter.pData);
970             *(void**)iter.pData = NULL;
971         }
972     } while (pb_field_next(&iter));
973 }
974 #endif
975 
976 /* Field decoders */
977 
pb_decode_svarint(pb_istream_t * stream,int64_t * dest)978 bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
979 {
980     uint64_t value;
981     if (!pb_decode_varint(stream, &value))
982         return false;
983 
984     if (value & 1)
985         *dest = (int64_t)(~(value >> 1));
986     else
987         *dest = (int64_t)(value >> 1);
988 
989     return true;
990 }
991 
pb_decode_fixed32(pb_istream_t * stream,void * dest)992 bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
993 {
994     #ifdef __BIG_ENDIAN__
995     uint8_t *bytes = (uint8_t*)dest;
996     uint8_t lebytes[4];
997 
998     if (!pb_read(stream, lebytes, 4))
999         return false;
1000 
1001     bytes[0] = lebytes[3];
1002     bytes[1] = lebytes[2];
1003     bytes[2] = lebytes[1];
1004     bytes[3] = lebytes[0];
1005     return true;
1006     #else
1007     return pb_read(stream, (uint8_t*)dest, 4);
1008     #endif
1009 }
1010 
pb_decode_fixed64(pb_istream_t * stream,void * dest)1011 bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
1012 {
1013     #ifdef __BIG_ENDIAN__
1014     uint8_t *bytes = (uint8_t*)dest;
1015     uint8_t lebytes[8];
1016 
1017     if (!pb_read(stream, lebytes, 8))
1018         return false;
1019 
1020     bytes[0] = lebytes[7];
1021     bytes[1] = lebytes[6];
1022     bytes[2] = lebytes[5];
1023     bytes[3] = lebytes[4];
1024     bytes[4] = lebytes[3];
1025     bytes[5] = lebytes[2];
1026     bytes[6] = lebytes[1];
1027     bytes[7] = lebytes[0];
1028     return true;
1029     #else
1030     return pb_read(stream, (uint8_t*)dest, 8);
1031     #endif
1032 }
1033 
pb_dec_varint(pb_istream_t * stream,const pb_field_t * field,void * dest)1034 static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1035 {
1036     uint64_t value;
1037     if (!pb_decode_varint(stream, &value))
1038         return false;
1039 
1040     switch (field->data_size)
1041     {
1042         case 1: *(int8_t*)dest = (int8_t)value; break;
1043         case 2: *(int16_t*)dest = (int16_t)value; break;
1044         case 4: *(int32_t*)dest = (int32_t)value; break;
1045         case 8: *(int64_t*)dest = (int64_t)value; break;
1046         default: PB_RETURN_ERROR(stream, "invalid data_size");
1047     }
1048 
1049     return true;
1050 }
1051 
pb_dec_uvarint(pb_istream_t * stream,const pb_field_t * field,void * dest)1052 static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1053 {
1054     uint64_t value;
1055     if (!pb_decode_varint(stream, &value))
1056         return false;
1057 
1058     switch (field->data_size)
1059     {
1060         case 4: *(uint32_t*)dest = (uint32_t)value; break;
1061         case 8: *(uint64_t*)dest = value; break;
1062         default: PB_RETURN_ERROR(stream, "invalid data_size");
1063     }
1064 
1065     return true;
1066 }
1067 
pb_dec_svarint(pb_istream_t * stream,const pb_field_t * field,void * dest)1068 static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1069 {
1070     int64_t value;
1071     if (!pb_decode_svarint(stream, &value))
1072         return false;
1073 
1074     switch (field->data_size)
1075     {
1076         case 4: *(int32_t*)dest = (int32_t)value; break;
1077         case 8: *(int64_t*)dest = value; break;
1078         default: PB_RETURN_ERROR(stream, "invalid data_size");
1079     }
1080 
1081     return true;
1082 }
1083 
pb_dec_fixed32(pb_istream_t * stream,const pb_field_t * field,void * dest)1084 static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
1085 {
1086     UNUSED(field);
1087     return pb_decode_fixed32(stream, dest);
1088 }
1089 
pb_dec_fixed64(pb_istream_t * stream,const pb_field_t * field,void * dest)1090 static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
1091 {
1092     UNUSED(field);
1093     return pb_decode_fixed64(stream, dest);
1094 }
1095 
pb_dec_bytes(pb_istream_t * stream,const pb_field_t * field,void * dest)1096 static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
1097 {
1098     uint32_t size;
1099     pb_bytes_array_t *bdest;
1100 
1101     if (!pb_decode_varint32(stream, &size))
1102         return false;
1103 
1104     if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1105     {
1106 #ifndef PB_ENABLE_MALLOC
1107         PB_RETURN_ERROR(stream, "no malloc support");
1108 #else
1109         if (!allocate_field(stream, dest, PB_BYTES_ARRAY_T_ALLOCSIZE(size), 1))
1110             return false;
1111         bdest = *(pb_bytes_array_t**)dest;
1112 #endif
1113     }
1114     else
1115     {
1116         if (PB_BYTES_ARRAY_T_ALLOCSIZE(size) > field->data_size)
1117             PB_RETURN_ERROR(stream, "bytes overflow");
1118         bdest = (pb_bytes_array_t*)dest;
1119     }
1120 
1121     bdest->size = size;
1122     return pb_read(stream, bdest->bytes, size);
1123 }
1124 
pb_dec_string(pb_istream_t * stream,const pb_field_t * field,void * dest)1125 static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
1126 {
1127     uint32_t size;
1128     size_t alloc_size;
1129     bool status;
1130     if (!pb_decode_varint32(stream, &size))
1131         return false;
1132 
1133     /* Space for null terminator */
1134     alloc_size = size + 1;
1135 
1136     if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1137     {
1138 #ifndef PB_ENABLE_MALLOC
1139         PB_RETURN_ERROR(stream, "no malloc support");
1140 #else
1141         if (!allocate_field(stream, dest, alloc_size, 1))
1142             return false;
1143         dest = *(void**)dest;
1144 #endif
1145     }
1146     else
1147     {
1148         if (alloc_size > field->data_size)
1149             PB_RETURN_ERROR(stream, "string overflow");
1150     }
1151 
1152     status = pb_read(stream, (uint8_t*)dest, size);
1153     *((uint8_t*)dest + size) = 0;
1154     return status;
1155 }
1156 
pb_dec_submessage(pb_istream_t * stream,const pb_field_t * field,void * dest)1157 static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
1158 {
1159     bool status;
1160     pb_istream_t substream;
1161     const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr;
1162 
1163     if (!pb_make_string_substream(stream, &substream))
1164         return false;
1165 
1166     if (field->ptr == NULL)
1167         PB_RETURN_ERROR(stream, "invalid field descriptor");
1168 
1169     /* New array entries need to be initialized, while required and optional
1170      * submessages have already been initialized in the top-level pb_decode. */
1171     if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
1172         status = pb_decode(&substream, submsg_fields, dest);
1173     else
1174         status = pb_decode_noinit(&substream, submsg_fields, dest);
1175 
1176     pb_close_string_substream(stream, &substream);
1177     return status;
1178 }
1179