1 /* pb_decode.c -- decode a protobuf using minimal resources
3 * 2011 Petteri Aimonen <jpa@kapsi.fi>
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.
10 #if !defined(__GNUC__) || ( __GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
13 #define checkreturn __attribute__((warn_unused_result))
17 #include "pb_decode.h"
18 #include "pb_common.h"
20 /**************************************
21 * Declarations internal to this file *
22 **************************************/
24 typedef bool (*pb_decoder_t)(pb_istream_t *stream, const pb_field_t *field, void *dest) checkreturn;
26 static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count);
27 static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest);
28 static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, uint8_t *buf, size_t *size);
29 static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter);
30 static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter);
31 static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter);
32 static bool checkreturn default_extension_decoder(pb_istream_t *stream, pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type);
33 static bool checkreturn decode_extension(pb_istream_t *stream, uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter);
34 static bool checkreturn find_extension_field(pb_field_iter_t *iter);
35 static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct);
36 static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest);
37 static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
38 static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
39 static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest);
40 static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest);
41 static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest);
42 static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest);
43 static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest);
44 static bool checkreturn pb_skip_varint(pb_istream_t *stream);
45 static bool checkreturn pb_skip_string(pb_istream_t *stream);
47 #ifdef PB_ENABLE_MALLOC
48 static void pb_release_single_field(const pb_field_iter_t *iter);
51 /* --- Function pointers to field decoders ---
52 * Order in the array must match pb_action_t LTYPE numbering.
54 static const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = {
67 /*******************************
68 * pb_istream_t implementation *
69 *******************************/
71 static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count)
73 uint8_t *source = (uint8_t*)stream->state;
74 stream->state = source + count;
85 bool checkreturn pb_read(pb_istream_t *stream, uint8_t *buf, size_t count)
87 #ifndef PB_BUFFER_ONLY
88 if (buf == NULL && stream->callback != buf_read)
90 /* Skip input bytes */
94 if (!pb_read(stream, tmp, 16))
100 return pb_read(stream, tmp, count);
104 if (stream->bytes_left < count)
105 PB_RETURN_ERROR(stream, "end-of-stream");
107 #ifndef PB_BUFFER_ONLY
108 if (!stream->callback(stream, buf, count))
109 PB_RETURN_ERROR(stream, "io error");
111 if (!buf_read(stream, buf, count))
115 stream->bytes_left -= count;
119 /* Read a single byte from input stream. buf may not be NULL.
120 * This is an optimization for the varint decoding. */
121 static bool checkreturn pb_readbyte(pb_istream_t *stream, uint8_t *buf)
123 if (stream->bytes_left == 0)
124 PB_RETURN_ERROR(stream, "end-of-stream");
126 #ifndef PB_BUFFER_ONLY
127 if (!stream->callback(stream, buf, 1))
128 PB_RETURN_ERROR(stream, "io error");
130 *buf = *(uint8_t*)stream->state;
131 stream->state = (uint8_t*)stream->state + 1;
134 stream->bytes_left--;
139 pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize)
142 #ifdef PB_BUFFER_ONLY
143 stream.callback = NULL;
145 stream.callback = &buf_read;
148 stream.bytes_left = bufsize;
150 stream.errmsg = NULL;
155 /********************
157 ********************/
159 static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
164 if (!pb_readbyte(stream, &byte))
167 if ((byte & 0x80) == 0)
169 /* Quick case, 1 byte value */
176 result = byte & 0x7F;
181 PB_RETURN_ERROR(stream, "varint overflow");
183 if (!pb_readbyte(stream, &byte))
186 result |= (uint32_t)(byte & 0x7F) << bitpos;
187 bitpos = (uint8_t)(bitpos + 7);
188 } while (byte & 0x80);
195 bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
204 PB_RETURN_ERROR(stream, "varint overflow");
206 if (!pb_readbyte(stream, &byte))
209 result |= (uint64_t)(byte & 0x7F) << bitpos;
210 bitpos = (uint8_t)(bitpos + 7);
211 } while (byte & 0x80);
217 bool checkreturn pb_skip_varint(pb_istream_t *stream)
222 if (!pb_read(stream, &byte, 1))
224 } while (byte & 0x80);
228 bool checkreturn pb_skip_string(pb_istream_t *stream)
231 if (!pb_decode_varint32(stream, &length))
234 return pb_read(stream, NULL, length);
237 bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
241 *wire_type = (pb_wire_type_t) 0;
244 if (!pb_decode_varint32(stream, &temp))
246 if (stream->bytes_left == 0)
254 *eof = true; /* Special feature: allow 0-terminated messages. */
259 *wire_type = (pb_wire_type_t)(temp & 7);
263 bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
267 case PB_WT_VARINT: return pb_skip_varint(stream);
268 case PB_WT_64BIT: return pb_read(stream, NULL, 8);
269 case PB_WT_STRING: return pb_skip_string(stream);
270 case PB_WT_32BIT: return pb_read(stream, NULL, 4);
271 default: PB_RETURN_ERROR(stream, "invalid wire_type");
275 /* Read a raw value to buffer, for the purpose of passing it to callback as
276 * a substream. Size is maximum size on call, and actual size on return.
278 static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, uint8_t *buf, size_t *size)
280 size_t max_size = *size;
288 if (*size > max_size) return false;
289 if (!pb_read(stream, buf, 1)) return false;
290 } while (*buf++ & 0x80);
295 return pb_read(stream, buf, 8);
299 return pb_read(stream, buf, 4);
301 default: PB_RETURN_ERROR(stream, "invalid wire_type");
305 /* Decode string length from stream and return a substream with limited length.
306 * Remember to close the substream using pb_close_string_substream().
308 bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
311 if (!pb_decode_varint32(stream, &size))
314 *substream = *stream;
315 if (substream->bytes_left < size)
316 PB_RETURN_ERROR(stream, "parent stream too short");
318 substream->bytes_left = size;
319 stream->bytes_left -= size;
323 void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
325 stream->state = substream->state;
328 stream->errmsg = substream->errmsg;
332 /*************************
333 * Decode a single field *
334 *************************/
336 static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
341 type = iter->pos->type;
342 func = PB_DECODERS[PB_LTYPE(type)];
344 switch (PB_HTYPE(type))
346 case PB_HTYPE_REQUIRED:
347 return func(stream, iter->pos, iter->pData);
349 case PB_HTYPE_OPTIONAL:
350 *(bool*)iter->pSize = true;
351 return func(stream, iter->pos, iter->pData);
353 case PB_HTYPE_REPEATED:
354 if (wire_type == PB_WT_STRING
355 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
359 pb_size_t *size = (pb_size_t*)iter->pSize;
360 pb_istream_t substream;
361 if (!pb_make_string_substream(stream, &substream))
364 while (substream.bytes_left > 0 && *size < iter->pos->array_size)
366 void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
367 if (!func(&substream, iter->pos, pItem))
374 pb_close_string_substream(stream, &substream);
376 if (substream.bytes_left != 0)
377 PB_RETURN_ERROR(stream, "array overflow");
384 pb_size_t *size = (pb_size_t*)iter->pSize;
385 void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
386 if (*size >= iter->pos->array_size)
387 PB_RETURN_ERROR(stream, "array overflow");
390 return func(stream, iter->pos, pItem);
394 PB_RETURN_ERROR(stream, "invalid field type");
398 #ifdef PB_ENABLE_MALLOC
399 /* Allocate storage for the field and store the pointer at iter->pData.
400 * array_size is the number of entries to reserve in an array.
401 * Zero size is not allowed, use pb_free() for releasing.
403 static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
405 void *ptr = *(void**)pData;
407 /* Check for multiplication overflows.
408 * This code avoids the costly division if the sizes are small enough.
409 * Multiplication is safe as long as only half of bits are set
410 * in either multiplicand.
412 const size_t check_limit = (size_t)1 << (sizeof(size_t) * 4);
413 if (data_size >= check_limit || array_size >= check_limit)
415 const size_t size_max = (size_t)-1;
416 if (size_max / array_size < data_size)
418 PB_RETURN_ERROR(stream, "size too large");
422 /* Allocate new or expand previous allocation */
423 /* Note: on failure the old pointer will remain in the structure,
424 * the message must be freed by caller also on error return. */
425 ptr = pb_realloc(ptr, array_size * data_size);
427 PB_RETURN_ERROR(stream, "realloc failed");
429 *(void**)pData = ptr;
433 /* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
434 static void initialize_pointer_field(void *pItem, pb_field_iter_t *iter)
436 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_STRING ||
437 PB_LTYPE(iter->pos->type) == PB_LTYPE_BYTES)
439 *(void**)pItem = NULL;
441 else if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
443 pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, pItem);
448 static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
450 #ifndef PB_ENABLE_MALLOC
451 PB_UNUSED(wire_type);
453 PB_RETURN_ERROR(stream, "no malloc support");
458 type = iter->pos->type;
459 func = PB_DECODERS[PB_LTYPE(type)];
461 switch (PB_HTYPE(type))
463 case PB_HTYPE_REQUIRED:
464 case PB_HTYPE_OPTIONAL:
465 if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE &&
466 *(void**)iter->pData != NULL)
468 /* Duplicate field, have to release the old allocation first. */
469 pb_release_single_field(iter);
472 if (PB_LTYPE(type) == PB_LTYPE_STRING ||
473 PB_LTYPE(type) == PB_LTYPE_BYTES)
475 return func(stream, iter->pos, iter->pData);
479 if (!allocate_field(stream, iter->pData, iter->pos->data_size, 1))
482 initialize_pointer_field(*(void**)iter->pData, iter);
483 return func(stream, iter->pos, *(void**)iter->pData);
486 case PB_HTYPE_REPEATED:
487 if (wire_type == PB_WT_STRING
488 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
490 /* Packed array, multiple items come in at once. */
492 pb_size_t *size = (pb_size_t*)iter->pSize;
493 size_t allocated_size = *size;
495 pb_istream_t substream;
497 if (!pb_make_string_substream(stream, &substream))
500 while (substream.bytes_left)
502 if ((size_t)*size + 1 > allocated_size)
504 /* Allocate more storage. This tries to guess the
505 * number of remaining entries. Round the division
507 allocated_size += (substream.bytes_left - 1) / iter->pos->data_size + 1;
509 if (!allocate_field(&substream, iter->pData, iter->pos->data_size, allocated_size))
516 /* Decode the array entry */
517 pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size);
518 initialize_pointer_field(pItem, iter);
519 if (!func(&substream, iter->pos, pItem))
525 if (*size == PB_SIZE_MAX)
528 stream->errmsg = "too many array entries";
536 pb_close_string_substream(stream, &substream);
542 /* Normal repeated field, i.e. only one item at a time. */
543 pb_size_t *size = (pb_size_t*)iter->pSize;
546 if (*size == PB_SIZE_MAX)
547 PB_RETURN_ERROR(stream, "too many array entries");
550 if (!allocate_field(stream, iter->pData, iter->pos->data_size, *size))
553 pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size - 1);
554 initialize_pointer_field(pItem, iter);
555 return func(stream, iter->pos, pItem);
559 PB_RETURN_ERROR(stream, "invalid field type");
564 static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
566 pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
568 #ifdef PB_OLD_CALLBACK_STYLE
569 void *arg = pCallback->arg;
571 void **arg = &(pCallback->arg);
574 if (pCallback->funcs.decode == NULL)
575 return pb_skip_field(stream, wire_type);
577 if (wire_type == PB_WT_STRING)
579 pb_istream_t substream;
581 if (!pb_make_string_substream(stream, &substream))
586 if (!pCallback->funcs.decode(&substream, iter->pos, arg))
587 PB_RETURN_ERROR(stream, "callback failed");
588 } while (substream.bytes_left);
590 pb_close_string_substream(stream, &substream);
595 /* Copy the single scalar value to stack.
596 * This is required so that we can limit the stream length,
597 * which in turn allows to use same callback for packed and
598 * not-packed fields. */
599 pb_istream_t substream;
601 size_t size = sizeof(buffer);
603 if (!read_raw_value(stream, wire_type, buffer, &size))
605 substream = pb_istream_from_buffer(buffer, size);
607 return pCallback->funcs.decode(&substream, iter->pos, arg);
611 static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
613 switch (PB_ATYPE(iter->pos->type))
615 case PB_ATYPE_STATIC:
616 return decode_static_field(stream, wire_type, iter);
618 case PB_ATYPE_POINTER:
619 return decode_pointer_field(stream, wire_type, iter);
621 case PB_ATYPE_CALLBACK:
622 return decode_callback_field(stream, wire_type, iter);
625 PB_RETURN_ERROR(stream, "invalid field type");
629 /* Default handler for extension fields. Expects a pb_field_t structure
630 * in extension->type->arg. */
631 static bool checkreturn default_extension_decoder(pb_istream_t *stream,
632 pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
634 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
635 pb_field_iter_t iter;
637 if (field->tag != tag)
640 /* Fake a field iterator for the extension field.
641 * It is not actually safe to advance this iterator, but decode_field
642 * will not even try to. */
643 (void)pb_field_iter_begin(&iter, field, extension->dest);
644 iter.pData = extension->dest;
645 iter.pSize = &extension->found;
647 return decode_field(stream, wire_type, &iter);
650 /* Try to decode an unknown field as an extension field. Tries each extension
651 * decoder in turn, until one of them handles the field or loop ends. */
652 static bool checkreturn decode_extension(pb_istream_t *stream,
653 uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter)
655 pb_extension_t *extension = *(pb_extension_t* const *)iter->pData;
656 size_t pos = stream->bytes_left;
658 while (extension != NULL && pos == stream->bytes_left)
661 if (extension->type->decode)
662 status = extension->type->decode(stream, extension, tag, wire_type);
664 status = default_extension_decoder(stream, extension, tag, wire_type);
669 extension = extension->next;
675 /* Step through the iterator until an extension field is found or until all
676 * entries have been checked. There can be only one extension field per
677 * message. Returns false if no extension field is found. */
678 static bool checkreturn find_extension_field(pb_field_iter_t *iter)
680 const pb_field_t *start = iter->pos;
683 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_EXTENSION)
685 (void)pb_field_iter_next(iter);
686 } while (iter->pos != start);
691 /* Initialize message fields to default values, recursively */
692 static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
694 pb_field_iter_t iter;
696 if (!pb_field_iter_begin(&iter, fields, dest_struct))
697 return; /* Empty message type */
702 type = iter.pos->type;
704 if (PB_ATYPE(type) == PB_ATYPE_STATIC)
706 if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL)
708 /* Set has_field to false. Still initialize the optional field
710 *(bool*)iter.pSize = false;
712 else if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
714 /* Set array count to 0, no need to initialize contents. */
715 *(pb_size_t*)iter.pSize = 0;
719 if (PB_LTYPE(iter.pos->type) == PB_LTYPE_SUBMESSAGE)
721 /* Initialize submessage to defaults */
722 pb_message_set_to_defaults((const pb_field_t *) iter.pos->ptr, iter.pData);
724 else if (iter.pos->ptr != NULL)
726 /* Initialize to default value */
727 memcpy(iter.pData, iter.pos->ptr, iter.pos->data_size);
731 /* Initialize to zeros */
732 memset(iter.pData, 0, iter.pos->data_size);
735 else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
737 /* Initialize the pointer to NULL. */
738 *(void**)iter.pData = NULL;
740 /* Initialize array count to 0. */
741 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
743 *(pb_size_t*)iter.pSize = 0;
746 else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
748 /* Don't overwrite callback */
750 } while (pb_field_iter_next(&iter));
753 /*********************
754 * Decode all fields *
755 *********************/
757 bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
759 uint8_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 7) / 8] = {0, 0, 0, 0, 0, 0, 0, 0};
760 uint32_t extension_range_start = 0;
761 pb_field_iter_t iter;
763 /* Return value ignored, as empty message types will be correctly handled by
764 * pb_field_iter_find() anyway. */
765 (void)pb_field_iter_begin(&iter, fields, dest_struct);
767 while (stream->bytes_left)
770 pb_wire_type_t wire_type;
773 if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
781 if (!pb_field_iter_find(&iter, tag))
783 /* No match found, check if it matches an extension. */
784 if (tag >= extension_range_start)
786 if (!find_extension_field(&iter))
787 extension_range_start = (uint32_t)-1;
789 extension_range_start = iter.pos->tag;
791 if (tag >= extension_range_start)
793 size_t pos = stream->bytes_left;
795 if (!decode_extension(stream, tag, wire_type, &iter))
798 if (pos != stream->bytes_left)
800 /* The field was handled */
806 /* No match found, skip data */
807 if (!pb_skip_field(stream, wire_type))
812 if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REQUIRED
813 && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
815 fields_seen[iter.required_field_index >> 3] |= (uint8_t)(1 << (iter.required_field_index & 7));
818 if (!decode_field(stream, wire_type, &iter))
822 /* Check that all required fields were present. */
824 /* First figure out the number of required fields by
825 * seeking to the end of the field array. Usually we
826 * are already close to end after decoding.
828 unsigned req_field_count;
832 req_field_count = iter.required_field_index;
833 last_type = iter.pos->type;
834 } while (pb_field_iter_next(&iter));
836 /* Fixup if last field was also required. */
837 if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.pos->tag != 0)
840 /* Check the whole bytes */
841 for (i = 0; i < (req_field_count >> 3); i++)
843 if (fields_seen[i] != 0xFF)
844 PB_RETURN_ERROR(stream, "missing required field");
847 /* Check the remaining bits */
848 if (fields_seen[req_field_count >> 3] != (0xFF >> (8 - (req_field_count & 7))))
849 PB_RETURN_ERROR(stream, "missing required field");
855 bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
858 pb_message_set_to_defaults(fields, dest_struct);
859 status = pb_decode_noinit(stream, fields, dest_struct);
861 #ifdef PB_ENABLE_MALLOC
863 pb_release(fields, dest_struct);
869 bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
871 pb_istream_t substream;
874 if (!pb_make_string_substream(stream, &substream))
877 status = pb_decode(&substream, fields, dest_struct);
878 pb_close_string_substream(stream, &substream);
882 #ifdef PB_ENABLE_MALLOC
883 static void pb_release_single_field(const pb_field_iter_t *iter)
886 type = iter->pos->type;
888 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
890 if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
891 (PB_LTYPE(type) == PB_LTYPE_STRING ||
892 PB_LTYPE(type) == PB_LTYPE_BYTES))
894 /* Release entries in repeated string or bytes array */
895 void **pItem = *(void***)iter->pData;
896 pb_size_t count = *(pb_size_t*)iter->pSize;
902 *(pb_size_t*)iter->pSize = 0;
904 else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
906 /* Release fields in submessages */
907 void *pItem = *(void**)iter->pData;
912 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
914 count = *(pb_size_t*)iter->pSize;
915 *(pb_size_t*)iter->pSize = 0;
920 pb_release((const pb_field_t*)iter->pos->ptr, pItem);
921 pItem = (uint8_t*)pItem + iter->pos->data_size;
926 /* Release main item */
927 pb_free(*(void**)iter->pData);
928 *(void**)iter->pData = NULL;
932 void pb_release(const pb_field_t fields[], void *dest_struct)
934 pb_field_iter_t iter;
936 if (!pb_field_iter_begin(&iter, fields, dest_struct))
937 return; /* Empty message type */
941 pb_release_single_field(&iter);
942 } while (pb_field_iter_next(&iter));
948 bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
951 if (!pb_decode_varint(stream, &value))
955 *dest = (int64_t)(~(value >> 1));
957 *dest = (int64_t)(value >> 1);
962 bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
964 #ifdef __BIG_ENDIAN__
965 uint8_t *bytes = (uint8_t*)dest;
968 if (!pb_read(stream, lebytes, 4))
971 bytes[0] = lebytes[3];
972 bytes[1] = lebytes[2];
973 bytes[2] = lebytes[1];
974 bytes[3] = lebytes[0];
977 return pb_read(stream, (uint8_t*)dest, 4);
981 bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
983 #ifdef __BIG_ENDIAN__
984 uint8_t *bytes = (uint8_t*)dest;
987 if (!pb_read(stream, lebytes, 8))
990 bytes[0] = lebytes[7];
991 bytes[1] = lebytes[6];
992 bytes[2] = lebytes[5];
993 bytes[3] = lebytes[4];
994 bytes[4] = lebytes[3];
995 bytes[5] = lebytes[2];
996 bytes[6] = lebytes[1];
997 bytes[7] = lebytes[0];
1000 return pb_read(stream, (uint8_t*)dest, 8);
1004 static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1007 if (!pb_decode_varint(stream, &value))
1010 switch (field->data_size)
1012 case 1: *(int8_t*)dest = (int8_t)value; break;
1013 case 2: *(int16_t*)dest = (int16_t)value; break;
1014 case 4: *(int32_t*)dest = (int32_t)value; break;
1015 case 8: *(int64_t*)dest = (int64_t)value; break;
1016 default: PB_RETURN_ERROR(stream, "invalid data_size");
1022 static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1025 if (!pb_decode_varint(stream, &value))
1028 switch (field->data_size)
1030 case 4: *(uint32_t*)dest = (uint32_t)value; break;
1031 case 8: *(uint64_t*)dest = value; break;
1032 default: PB_RETURN_ERROR(stream, "invalid data_size");
1038 static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1041 if (!pb_decode_svarint(stream, &value))
1044 switch (field->data_size)
1046 case 4: *(int32_t*)dest = (int32_t)value; break;
1047 case 8: *(int64_t*)dest = value; break;
1048 default: PB_RETURN_ERROR(stream, "invalid data_size");
1054 static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
1057 return pb_decode_fixed32(stream, dest);
1060 static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
1063 return pb_decode_fixed64(stream, dest);
1066 static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
1069 pb_bytes_array_t *bdest;
1071 if (!pb_decode_varint32(stream, &size))
1074 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1076 #ifndef PB_ENABLE_MALLOC
1077 PB_RETURN_ERROR(stream, "no malloc support");
1079 if (!allocate_field(stream, dest, PB_BYTES_ARRAY_T_ALLOCSIZE(size), 1))
1081 bdest = *(pb_bytes_array_t**)dest;
1086 if (PB_BYTES_ARRAY_T_ALLOCSIZE(size) > field->data_size)
1087 PB_RETURN_ERROR(stream, "bytes overflow");
1088 bdest = (pb_bytes_array_t*)dest;
1091 if (size > PB_SIZE_MAX)
1093 PB_RETURN_ERROR(stream, "bytes overflow");
1096 bdest->size = (pb_size_t)size;
1097 return pb_read(stream, bdest->bytes, size);
1100 static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
1105 if (!pb_decode_varint32(stream, &size))
1108 /* Space for null terminator */
1109 alloc_size = size + 1;
1111 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1113 #ifndef PB_ENABLE_MALLOC
1114 PB_RETURN_ERROR(stream, "no malloc support");
1116 if (!allocate_field(stream, dest, alloc_size, 1))
1118 dest = *(void**)dest;
1123 if (alloc_size > field->data_size)
1124 PB_RETURN_ERROR(stream, "string overflow");
1127 status = pb_read(stream, (uint8_t*)dest, size);
1128 *((uint8_t*)dest + size) = 0;
1132 static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
1135 pb_istream_t substream;
1136 const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr;
1138 if (!pb_make_string_substream(stream, &substream))
1141 if (field->ptr == NULL)
1142 PB_RETURN_ERROR(stream, "invalid field descriptor");
1144 /* New array entries need to be initialized, while required and optional
1145 * submessages have already been initialized in the top-level pb_decode. */
1146 if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
1147 status = pb_decode(&substream, submsg_fields, dest);
1149 status = pb_decode_noinit(&substream, submsg_fields, dest);
1151 pb_close_string_substream(stream, &substream);