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 void iter_from_extension(pb_field_iter_t *iter, pb_extension_t *extension);
33 static bool checkreturn default_extension_decoder(pb_istream_t *stream, pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type);
34 static bool checkreturn decode_extension(pb_istream_t *stream, uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter);
35 static bool checkreturn find_extension_field(pb_field_iter_t *iter);
36 static void pb_field_set_to_default(pb_field_iter_t *iter);
37 static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct);
38 static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest);
39 static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
40 static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
41 static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest);
42 static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest);
43 static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest);
44 static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest);
45 static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest);
46 static bool checkreturn pb_skip_varint(pb_istream_t *stream);
47 static bool checkreturn pb_skip_string(pb_istream_t *stream);
49 #ifdef PB_ENABLE_MALLOC
50 static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size);
51 static void pb_release_single_field(const pb_field_iter_t *iter);
54 /* --- Function pointers to field decoders ---
55 * Order in the array must match pb_action_t LTYPE numbering.
57 static const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = {
70 /*******************************
71 * pb_istream_t implementation *
72 *******************************/
74 static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count)
76 uint8_t *source = (uint8_t*)stream->state;
77 stream->state = source + count;
88 bool checkreturn pb_read(pb_istream_t *stream, uint8_t *buf, size_t count)
90 #ifndef PB_BUFFER_ONLY
91 if (buf == NULL && stream->callback != buf_read)
93 /* Skip input bytes */
97 if (!pb_read(stream, tmp, 16))
103 return pb_read(stream, tmp, count);
107 if (stream->bytes_left < count)
108 PB_RETURN_ERROR(stream, "end-of-stream");
110 #ifndef PB_BUFFER_ONLY
111 if (!stream->callback(stream, buf, count))
112 PB_RETURN_ERROR(stream, "io error");
114 if (!buf_read(stream, buf, count))
118 stream->bytes_left -= count;
122 /* Read a single byte from input stream. buf may not be NULL.
123 * This is an optimization for the varint decoding. */
124 static bool checkreturn pb_readbyte(pb_istream_t *stream, uint8_t *buf)
126 if (stream->bytes_left == 0)
127 PB_RETURN_ERROR(stream, "end-of-stream");
129 #ifndef PB_BUFFER_ONLY
130 if (!stream->callback(stream, buf, 1))
131 PB_RETURN_ERROR(stream, "io error");
133 *buf = *(uint8_t*)stream->state;
134 stream->state = (uint8_t*)stream->state + 1;
137 stream->bytes_left--;
142 pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize)
145 #ifdef PB_BUFFER_ONLY
146 stream.callback = NULL;
148 stream.callback = &buf_read;
151 stream.bytes_left = bufsize;
153 stream.errmsg = NULL;
158 /********************
160 ********************/
162 static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
167 if (!pb_readbyte(stream, &byte))
170 if ((byte & 0x80) == 0)
172 /* Quick case, 1 byte value */
179 result = byte & 0x7F;
184 PB_RETURN_ERROR(stream, "varint overflow");
186 if (!pb_readbyte(stream, &byte))
189 result |= (uint32_t)(byte & 0x7F) << bitpos;
190 bitpos = (uint8_t)(bitpos + 7);
191 } while (byte & 0x80);
198 bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
207 PB_RETURN_ERROR(stream, "varint overflow");
209 if (!pb_readbyte(stream, &byte))
212 result |= (uint64_t)(byte & 0x7F) << bitpos;
213 bitpos = (uint8_t)(bitpos + 7);
214 } while (byte & 0x80);
220 bool checkreturn pb_skip_varint(pb_istream_t *stream)
225 if (!pb_read(stream, &byte, 1))
227 } while (byte & 0x80);
231 bool checkreturn pb_skip_string(pb_istream_t *stream)
234 if (!pb_decode_varint32(stream, &length))
237 return pb_read(stream, NULL, length);
240 bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
244 *wire_type = (pb_wire_type_t) 0;
247 if (!pb_decode_varint32(stream, &temp))
249 if (stream->bytes_left == 0)
257 *eof = true; /* Special feature: allow 0-terminated messages. */
262 *wire_type = (pb_wire_type_t)(temp & 7);
266 bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
270 case PB_WT_VARINT: return pb_skip_varint(stream);
271 case PB_WT_64BIT: return pb_read(stream, NULL, 8);
272 case PB_WT_STRING: return pb_skip_string(stream);
273 case PB_WT_32BIT: return pb_read(stream, NULL, 4);
274 default: PB_RETURN_ERROR(stream, "invalid wire_type");
278 /* Read a raw value to buffer, for the purpose of passing it to callback as
279 * a substream. Size is maximum size on call, and actual size on return.
281 static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, uint8_t *buf, size_t *size)
283 size_t max_size = *size;
291 if (*size > max_size) return false;
292 if (!pb_read(stream, buf, 1)) return false;
293 } while (*buf++ & 0x80);
298 return pb_read(stream, buf, 8);
302 return pb_read(stream, buf, 4);
304 default: PB_RETURN_ERROR(stream, "invalid wire_type");
308 /* Decode string length from stream and return a substream with limited length.
309 * Remember to close the substream using pb_close_string_substream().
311 bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
314 if (!pb_decode_varint32(stream, &size))
317 *substream = *stream;
318 if (substream->bytes_left < size)
319 PB_RETURN_ERROR(stream, "parent stream too short");
321 substream->bytes_left = size;
322 stream->bytes_left -= size;
326 void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
328 stream->state = substream->state;
331 stream->errmsg = substream->errmsg;
335 /*************************
336 * Decode a single field *
337 *************************/
339 static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
344 type = iter->pos->type;
345 func = PB_DECODERS[PB_LTYPE(type)];
347 switch (PB_HTYPE(type))
349 case PB_HTYPE_REQUIRED:
350 return func(stream, iter->pos, iter->pData);
352 case PB_HTYPE_OPTIONAL:
353 *(bool*)iter->pSize = true;
354 return func(stream, iter->pos, iter->pData);
356 case PB_HTYPE_REPEATED:
357 if (wire_type == PB_WT_STRING
358 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
362 pb_size_t *size = (pb_size_t*)iter->pSize;
363 pb_istream_t substream;
364 if (!pb_make_string_substream(stream, &substream))
367 while (substream.bytes_left > 0 && *size < iter->pos->array_size)
369 void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
370 if (!func(&substream, iter->pos, pItem))
377 pb_close_string_substream(stream, &substream);
379 if (substream.bytes_left != 0)
380 PB_RETURN_ERROR(stream, "array overflow");
387 pb_size_t *size = (pb_size_t*)iter->pSize;
388 void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
389 if (*size >= iter->pos->array_size)
390 PB_RETURN_ERROR(stream, "array overflow");
393 return func(stream, iter->pos, pItem);
397 PB_RETURN_ERROR(stream, "invalid field type");
401 #ifdef PB_ENABLE_MALLOC
402 /* Allocate storage for the field and store the pointer at iter->pData.
403 * array_size is the number of entries to reserve in an array.
404 * Zero size is not allowed, use pb_free() for releasing.
406 static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
408 void *ptr = *(void**)pData;
410 if (data_size == 0 || array_size == 0)
411 PB_RETURN_ERROR(stream, "invalid size");
413 /* Check for multiplication overflows.
414 * This code avoids the costly division if the sizes are small enough.
415 * Multiplication is safe as long as only half of bits are set
416 * in either multiplicand.
419 const size_t check_limit = (size_t)1 << (sizeof(size_t) * 4);
420 if (data_size >= check_limit || array_size >= check_limit)
422 const size_t size_max = (size_t)-1;
423 if (size_max / array_size < data_size)
425 PB_RETURN_ERROR(stream, "size too large");
430 /* Allocate new or expand previous allocation */
431 /* Note: on failure the old pointer will remain in the structure,
432 * the message must be freed by caller also on error return. */
433 ptr = pb_realloc(ptr, array_size * data_size);
435 PB_RETURN_ERROR(stream, "realloc failed");
437 *(void**)pData = ptr;
441 /* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
442 static void initialize_pointer_field(void *pItem, pb_field_iter_t *iter)
444 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_STRING ||
445 PB_LTYPE(iter->pos->type) == PB_LTYPE_BYTES)
447 *(void**)pItem = NULL;
449 else if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
451 pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, pItem);
456 static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
458 #ifndef PB_ENABLE_MALLOC
459 PB_UNUSED(wire_type);
461 PB_RETURN_ERROR(stream, "no malloc support");
466 type = iter->pos->type;
467 func = PB_DECODERS[PB_LTYPE(type)];
469 switch (PB_HTYPE(type))
471 case PB_HTYPE_REQUIRED:
472 case PB_HTYPE_OPTIONAL:
473 if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE &&
474 *(void**)iter->pData != NULL)
476 /* Duplicate field, have to release the old allocation first. */
477 pb_release_single_field(iter);
480 if (PB_LTYPE(type) == PB_LTYPE_STRING ||
481 PB_LTYPE(type) == PB_LTYPE_BYTES)
483 return func(stream, iter->pos, iter->pData);
487 if (!allocate_field(stream, iter->pData, iter->pos->data_size, 1))
490 initialize_pointer_field(*(void**)iter->pData, iter);
491 return func(stream, iter->pos, *(void**)iter->pData);
494 case PB_HTYPE_REPEATED:
495 if (wire_type == PB_WT_STRING
496 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
498 /* Packed array, multiple items come in at once. */
500 pb_size_t *size = (pb_size_t*)iter->pSize;
501 size_t allocated_size = *size;
503 pb_istream_t substream;
505 if (!pb_make_string_substream(stream, &substream))
508 while (substream.bytes_left)
510 if ((size_t)*size + 1 > allocated_size)
512 /* Allocate more storage. This tries to guess the
513 * number of remaining entries. Round the division
515 allocated_size += (substream.bytes_left - 1) / iter->pos->data_size + 1;
517 if (!allocate_field(&substream, iter->pData, iter->pos->data_size, allocated_size))
524 /* Decode the array entry */
525 pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size);
526 initialize_pointer_field(pItem, iter);
527 if (!func(&substream, iter->pos, pItem))
533 if (*size == PB_SIZE_MAX)
536 stream->errmsg = "too many array entries";
544 pb_close_string_substream(stream, &substream);
550 /* Normal repeated field, i.e. only one item at a time. */
551 pb_size_t *size = (pb_size_t*)iter->pSize;
554 if (*size == PB_SIZE_MAX)
555 PB_RETURN_ERROR(stream, "too many array entries");
558 if (!allocate_field(stream, iter->pData, iter->pos->data_size, *size))
561 pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size - 1);
562 initialize_pointer_field(pItem, iter);
563 return func(stream, iter->pos, pItem);
567 PB_RETURN_ERROR(stream, "invalid field type");
572 static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
574 pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
576 #ifdef PB_OLD_CALLBACK_STYLE
577 void *arg = pCallback->arg;
579 void **arg = &(pCallback->arg);
582 if (pCallback->funcs.decode == NULL)
583 return pb_skip_field(stream, wire_type);
585 if (wire_type == PB_WT_STRING)
587 pb_istream_t substream;
589 if (!pb_make_string_substream(stream, &substream))
594 if (!pCallback->funcs.decode(&substream, iter->pos, arg))
595 PB_RETURN_ERROR(stream, "callback failed");
596 } while (substream.bytes_left);
598 pb_close_string_substream(stream, &substream);
603 /* Copy the single scalar value to stack.
604 * This is required so that we can limit the stream length,
605 * which in turn allows to use same callback for packed and
606 * not-packed fields. */
607 pb_istream_t substream;
609 size_t size = sizeof(buffer);
611 if (!read_raw_value(stream, wire_type, buffer, &size))
613 substream = pb_istream_from_buffer(buffer, size);
615 return pCallback->funcs.decode(&substream, iter->pos, arg);
619 static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
621 switch (PB_ATYPE(iter->pos->type))
623 case PB_ATYPE_STATIC:
624 return decode_static_field(stream, wire_type, iter);
626 case PB_ATYPE_POINTER:
627 return decode_pointer_field(stream, wire_type, iter);
629 case PB_ATYPE_CALLBACK:
630 return decode_callback_field(stream, wire_type, iter);
633 PB_RETURN_ERROR(stream, "invalid field type");
637 static void iter_from_extension(pb_field_iter_t *iter, pb_extension_t *extension)
639 /* Fake a field iterator for the extension field.
640 * It is not actually safe to advance this iterator, but decode_field
641 * will not even try to. */
642 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
643 (void)pb_field_iter_begin(iter, field, extension->dest);
644 iter->pData = extension->dest;
645 iter->pSize = &extension->found;
647 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
649 /* For pointer extensions, the pointer is stored directly
650 * in the extension structure. This avoids having an extra
652 iter->pData = &extension->dest;
656 /* Default handler for extension fields. Expects a pb_field_t structure
657 * in extension->type->arg. */
658 static bool checkreturn default_extension_decoder(pb_istream_t *stream,
659 pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
661 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
662 pb_field_iter_t iter;
664 if (field->tag != tag)
667 iter_from_extension(&iter, extension);
668 extension->found = true;
669 return decode_field(stream, wire_type, &iter);
672 /* Try to decode an unknown field as an extension field. Tries each extension
673 * decoder in turn, until one of them handles the field or loop ends. */
674 static bool checkreturn decode_extension(pb_istream_t *stream,
675 uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter)
677 pb_extension_t *extension = *(pb_extension_t* const *)iter->pData;
678 size_t pos = stream->bytes_left;
680 while (extension != NULL && pos == stream->bytes_left)
683 if (extension->type->decode)
684 status = extension->type->decode(stream, extension, tag, wire_type);
686 status = default_extension_decoder(stream, extension, tag, wire_type);
691 extension = extension->next;
697 /* Step through the iterator until an extension field is found or until all
698 * entries have been checked. There can be only one extension field per
699 * message. Returns false if no extension field is found. */
700 static bool checkreturn find_extension_field(pb_field_iter_t *iter)
702 const pb_field_t *start = iter->pos;
705 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_EXTENSION)
707 (void)pb_field_iter_next(iter);
708 } while (iter->pos != start);
713 /* Initialize message fields to default values, recursively */
714 static void pb_field_set_to_default(pb_field_iter_t *iter)
717 type = iter->pos->type;
719 if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
721 pb_extension_t *ext = *(pb_extension_t* const *)iter->pData;
724 pb_field_iter_t ext_iter;
726 iter_from_extension(&ext_iter, ext);
727 pb_field_set_to_default(&ext_iter);
731 else if (PB_ATYPE(type) == PB_ATYPE_STATIC)
733 bool init_data = true;
734 if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL)
736 /* Set has_field to false. Still initialize the optional field
738 *(bool*)iter->pSize = false;
740 else if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
742 /* Set array count to 0, no need to initialize contents. */
743 *(pb_size_t*)iter->pSize = 0;
749 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
751 /* Initialize submessage to defaults */
752 pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, iter->pData);
754 else if (iter->pos->ptr != NULL)
756 /* Initialize to default value */
757 memcpy(iter->pData, iter->pos->ptr, iter->pos->data_size);
761 /* Initialize to zeros */
762 memset(iter->pData, 0, iter->pos->data_size);
766 else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
768 /* Initialize the pointer to NULL. */
769 *(void**)iter->pData = NULL;
771 /* Initialize array count to 0. */
772 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
774 *(pb_size_t*)iter->pSize = 0;
777 else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
779 /* Don't overwrite callback */
783 static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
785 pb_field_iter_t iter;
787 if (!pb_field_iter_begin(&iter, fields, dest_struct))
788 return; /* Empty message type */
792 pb_field_set_to_default(&iter);
793 } while (pb_field_iter_next(&iter));
796 /*********************
797 * Decode all fields *
798 *********************/
800 bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
802 uint8_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 7) / 8] = {0, 0, 0, 0, 0, 0, 0, 0};
803 uint32_t extension_range_start = 0;
804 pb_field_iter_t iter;
806 /* Return value ignored, as empty message types will be correctly handled by
807 * pb_field_iter_find() anyway. */
808 (void)pb_field_iter_begin(&iter, fields, dest_struct);
810 while (stream->bytes_left)
813 pb_wire_type_t wire_type;
816 if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
824 if (!pb_field_iter_find(&iter, tag))
826 /* No match found, check if it matches an extension. */
827 if (tag >= extension_range_start)
829 if (!find_extension_field(&iter))
830 extension_range_start = (uint32_t)-1;
832 extension_range_start = iter.pos->tag;
834 if (tag >= extension_range_start)
836 size_t pos = stream->bytes_left;
838 if (!decode_extension(stream, tag, wire_type, &iter))
841 if (pos != stream->bytes_left)
843 /* The field was handled */
849 /* No match found, skip data */
850 if (!pb_skip_field(stream, wire_type))
855 if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REQUIRED
856 && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
858 fields_seen[iter.required_field_index >> 3] |= (uint8_t)(1 << (iter.required_field_index & 7));
861 if (!decode_field(stream, wire_type, &iter))
865 /* Check that all required fields were present. */
867 /* First figure out the number of required fields by
868 * seeking to the end of the field array. Usually we
869 * are already close to end after decoding.
871 unsigned req_field_count;
875 req_field_count = iter.required_field_index;
876 last_type = iter.pos->type;
877 } while (pb_field_iter_next(&iter));
879 /* Fixup if last field was also required. */
880 if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.pos->tag != 0)
883 /* Check the whole bytes */
884 for (i = 0; i < (req_field_count >> 3); i++)
886 if (fields_seen[i] != 0xFF)
887 PB_RETURN_ERROR(stream, "missing required field");
890 /* Check the remaining bits */
891 if (fields_seen[req_field_count >> 3] != (0xFF >> (8 - (req_field_count & 7))))
892 PB_RETURN_ERROR(stream, "missing required field");
898 bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
901 pb_message_set_to_defaults(fields, dest_struct);
902 status = pb_decode_noinit(stream, fields, dest_struct);
904 #ifdef PB_ENABLE_MALLOC
906 pb_release(fields, dest_struct);
912 bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
914 pb_istream_t substream;
917 if (!pb_make_string_substream(stream, &substream))
920 status = pb_decode(&substream, fields, dest_struct);
921 pb_close_string_substream(stream, &substream);
925 #ifdef PB_ENABLE_MALLOC
926 static void pb_release_single_field(const pb_field_iter_t *iter)
929 type = iter->pos->type;
931 /* Release anything contained inside an extension or submsg.
932 * This has to be done even if the submsg itself is statically
934 if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
936 /* Release fields from all extensions in the linked list */
937 pb_extension_t *ext = *(pb_extension_t**)iter->pData;
940 pb_field_iter_t ext_iter;
941 iter_from_extension(&ext_iter, ext);
942 pb_release_single_field(&ext_iter);
946 else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
948 /* Release fields in submessage or submsg array */
949 void *pItem = iter->pData;
952 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
954 pItem = *(void**)iter->pData;
957 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
959 count = *(pb_size_t*)iter->pSize;
966 pb_release((const pb_field_t*)iter->pos->ptr, pItem);
967 pItem = (uint8_t*)pItem + iter->pos->data_size;
972 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
974 if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
975 (PB_LTYPE(type) == PB_LTYPE_STRING ||
976 PB_LTYPE(type) == PB_LTYPE_BYTES))
978 /* Release entries in repeated string or bytes array */
979 void **pItem = *(void***)iter->pData;
980 pb_size_t count = *(pb_size_t*)iter->pSize;
988 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
990 /* We are going to release the array, so set the size to 0 */
991 *(pb_size_t*)iter->pSize = 0;
994 /* Release main item */
995 pb_free(*(void**)iter->pData);
996 *(void**)iter->pData = NULL;
1000 void pb_release(const pb_field_t fields[], void *dest_struct)
1002 pb_field_iter_t iter;
1004 if (!pb_field_iter_begin(&iter, fields, dest_struct))
1005 return; /* Empty message type */
1009 pb_release_single_field(&iter);
1010 } while (pb_field_iter_next(&iter));
1014 /* Field decoders */
1016 bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
1019 if (!pb_decode_varint(stream, &value))
1023 *dest = (int64_t)(~(value >> 1));
1025 *dest = (int64_t)(value >> 1);
1030 bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
1032 #ifdef __BIG_ENDIAN__
1033 uint8_t *bytes = (uint8_t*)dest;
1036 if (!pb_read(stream, lebytes, 4))
1039 bytes[0] = lebytes[3];
1040 bytes[1] = lebytes[2];
1041 bytes[2] = lebytes[1];
1042 bytes[3] = lebytes[0];
1045 return pb_read(stream, (uint8_t*)dest, 4);
1049 bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
1051 #ifdef __BIG_ENDIAN__
1052 uint8_t *bytes = (uint8_t*)dest;
1055 if (!pb_read(stream, lebytes, 8))
1058 bytes[0] = lebytes[7];
1059 bytes[1] = lebytes[6];
1060 bytes[2] = lebytes[5];
1061 bytes[3] = lebytes[4];
1062 bytes[4] = lebytes[3];
1063 bytes[5] = lebytes[2];
1064 bytes[6] = lebytes[1];
1065 bytes[7] = lebytes[0];
1068 return pb_read(stream, (uint8_t*)dest, 8);
1072 static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1075 if (!pb_decode_varint(stream, &value))
1078 switch (field->data_size)
1080 case 1: *(int8_t*)dest = (int8_t)value; break;
1081 case 2: *(int16_t*)dest = (int16_t)value; break;
1082 case 4: *(int32_t*)dest = (int32_t)value; break;
1083 case 8: *(int64_t*)dest = (int64_t)value; break;
1084 default: PB_RETURN_ERROR(stream, "invalid data_size");
1090 static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1093 if (!pb_decode_varint(stream, &value))
1096 switch (field->data_size)
1098 case 4: *(uint32_t*)dest = (uint32_t)value; break;
1099 case 8: *(uint64_t*)dest = value; break;
1100 default: PB_RETURN_ERROR(stream, "invalid data_size");
1106 static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1109 if (!pb_decode_svarint(stream, &value))
1112 switch (field->data_size)
1114 case 4: *(int32_t*)dest = (int32_t)value; break;
1115 case 8: *(int64_t*)dest = value; break;
1116 default: PB_RETURN_ERROR(stream, "invalid data_size");
1122 static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
1125 return pb_decode_fixed32(stream, dest);
1128 static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
1131 return pb_decode_fixed64(stream, dest);
1134 static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
1138 pb_bytes_array_t *bdest;
1140 if (!pb_decode_varint32(stream, &size))
1143 if (size > PB_SIZE_MAX)
1144 PB_RETURN_ERROR(stream, "bytes overflow");
1146 alloc_size = PB_BYTES_ARRAY_T_ALLOCSIZE(size);
1147 if (size > alloc_size)
1148 PB_RETURN_ERROR(stream, "size too large");
1150 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1152 #ifndef PB_ENABLE_MALLOC
1153 PB_RETURN_ERROR(stream, "no malloc support");
1155 if (!allocate_field(stream, dest, alloc_size, 1))
1157 bdest = *(pb_bytes_array_t**)dest;
1162 if (alloc_size > field->data_size)
1163 PB_RETURN_ERROR(stream, "bytes overflow");
1164 bdest = (pb_bytes_array_t*)dest;
1167 bdest->size = (pb_size_t)size;
1168 return pb_read(stream, bdest->bytes, size);
1171 static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
1176 if (!pb_decode_varint32(stream, &size))
1179 /* Space for null terminator */
1180 alloc_size = size + 1;
1182 if (alloc_size < size)
1183 PB_RETURN_ERROR(stream, "size too large");
1185 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1187 #ifndef PB_ENABLE_MALLOC
1188 PB_RETURN_ERROR(stream, "no malloc support");
1190 if (!allocate_field(stream, dest, alloc_size, 1))
1192 dest = *(void**)dest;
1197 if (alloc_size > field->data_size)
1198 PB_RETURN_ERROR(stream, "string overflow");
1201 status = pb_read(stream, (uint8_t*)dest, size);
1202 *((uint8_t*)dest + size) = 0;
1206 static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
1209 pb_istream_t substream;
1210 const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr;
1212 if (!pb_make_string_substream(stream, &substream))
1215 if (field->ptr == NULL)
1216 PB_RETURN_ERROR(stream, "invalid field descriptor");
1218 /* New array entries need to be initialized, while required and optional
1219 * submessages have already been initialized in the top-level pb_decode. */
1220 if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
1221 status = pb_decode(&substream, submsg_fields, dest);
1223 status = pb_decode_noinit(&substream, submsg_fields, dest);
1225 pb_close_string_substream(stream, &substream);