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 bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size);
49 static void pb_release_single_field(const pb_field_iter_t *iter);
52 /* --- Function pointers to field decoders ---
53 * Order in the array must match pb_action_t LTYPE numbering.
55 static const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = {
68 /*******************************
69 * pb_istream_t implementation *
70 *******************************/
72 static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count)
74 uint8_t *source = (uint8_t*)stream->state;
75 stream->state = source + count;
86 bool checkreturn pb_read(pb_istream_t *stream, uint8_t *buf, size_t count)
88 #ifndef PB_BUFFER_ONLY
89 if (buf == NULL && stream->callback != buf_read)
91 /* Skip input bytes */
95 if (!pb_read(stream, tmp, 16))
101 return pb_read(stream, tmp, count);
105 if (stream->bytes_left < count)
106 PB_RETURN_ERROR(stream, "end-of-stream");
108 #ifndef PB_BUFFER_ONLY
109 if (!stream->callback(stream, buf, count))
110 PB_RETURN_ERROR(stream, "io error");
112 if (!buf_read(stream, buf, count))
116 stream->bytes_left -= count;
120 /* Read a single byte from input stream. buf may not be NULL.
121 * This is an optimization for the varint decoding. */
122 static bool checkreturn pb_readbyte(pb_istream_t *stream, uint8_t *buf)
124 if (stream->bytes_left == 0)
125 PB_RETURN_ERROR(stream, "end-of-stream");
127 #ifndef PB_BUFFER_ONLY
128 if (!stream->callback(stream, buf, 1))
129 PB_RETURN_ERROR(stream, "io error");
131 *buf = *(uint8_t*)stream->state;
132 stream->state = (uint8_t*)stream->state + 1;
135 stream->bytes_left--;
140 pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize)
143 #ifdef PB_BUFFER_ONLY
144 stream.callback = NULL;
146 stream.callback = &buf_read;
149 stream.bytes_left = bufsize;
151 stream.errmsg = NULL;
156 /********************
158 ********************/
160 static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
165 if (!pb_readbyte(stream, &byte))
168 if ((byte & 0x80) == 0)
170 /* Quick case, 1 byte value */
177 result = byte & 0x7F;
182 PB_RETURN_ERROR(stream, "varint overflow");
184 if (!pb_readbyte(stream, &byte))
187 result |= (uint32_t)(byte & 0x7F) << bitpos;
188 bitpos = (uint8_t)(bitpos + 7);
189 } while (byte & 0x80);
196 bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
205 PB_RETURN_ERROR(stream, "varint overflow");
207 if (!pb_readbyte(stream, &byte))
210 result |= (uint64_t)(byte & 0x7F) << bitpos;
211 bitpos = (uint8_t)(bitpos + 7);
212 } while (byte & 0x80);
218 bool checkreturn pb_skip_varint(pb_istream_t *stream)
223 if (!pb_read(stream, &byte, 1))
225 } while (byte & 0x80);
229 bool checkreturn pb_skip_string(pb_istream_t *stream)
232 if (!pb_decode_varint32(stream, &length))
235 return pb_read(stream, NULL, length);
238 bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
242 *wire_type = (pb_wire_type_t) 0;
245 if (!pb_decode_varint32(stream, &temp))
247 if (stream->bytes_left == 0)
255 *eof = true; /* Special feature: allow 0-terminated messages. */
260 *wire_type = (pb_wire_type_t)(temp & 7);
264 bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
268 case PB_WT_VARINT: return pb_skip_varint(stream);
269 case PB_WT_64BIT: return pb_read(stream, NULL, 8);
270 case PB_WT_STRING: return pb_skip_string(stream);
271 case PB_WT_32BIT: return pb_read(stream, NULL, 4);
272 default: PB_RETURN_ERROR(stream, "invalid wire_type");
276 /* Read a raw value to buffer, for the purpose of passing it to callback as
277 * a substream. Size is maximum size on call, and actual size on return.
279 static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, uint8_t *buf, size_t *size)
281 size_t max_size = *size;
289 if (*size > max_size) return false;
290 if (!pb_read(stream, buf, 1)) return false;
291 } while (*buf++ & 0x80);
296 return pb_read(stream, buf, 8);
300 return pb_read(stream, buf, 4);
302 default: PB_RETURN_ERROR(stream, "invalid wire_type");
306 /* Decode string length from stream and return a substream with limited length.
307 * Remember to close the substream using pb_close_string_substream().
309 bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
312 if (!pb_decode_varint32(stream, &size))
315 *substream = *stream;
316 if (substream->bytes_left < size)
317 PB_RETURN_ERROR(stream, "parent stream too short");
319 substream->bytes_left = size;
320 stream->bytes_left -= size;
324 void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
326 stream->state = substream->state;
329 stream->errmsg = substream->errmsg;
333 /*************************
334 * Decode a single field *
335 *************************/
337 static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
342 type = iter->pos->type;
343 func = PB_DECODERS[PB_LTYPE(type)];
345 switch (PB_HTYPE(type))
347 case PB_HTYPE_REQUIRED:
348 return func(stream, iter->pos, iter->pData);
350 case PB_HTYPE_OPTIONAL:
351 *(bool*)iter->pSize = true;
352 return func(stream, iter->pos, iter->pData);
354 case PB_HTYPE_REPEATED:
355 if (wire_type == PB_WT_STRING
356 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
360 pb_size_t *size = (pb_size_t*)iter->pSize;
361 pb_istream_t substream;
362 if (!pb_make_string_substream(stream, &substream))
365 while (substream.bytes_left > 0 && *size < iter->pos->array_size)
367 void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
368 if (!func(&substream, iter->pos, pItem))
375 pb_close_string_substream(stream, &substream);
377 if (substream.bytes_left != 0)
378 PB_RETURN_ERROR(stream, "array overflow");
385 pb_size_t *size = (pb_size_t*)iter->pSize;
386 void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
387 if (*size >= iter->pos->array_size)
388 PB_RETURN_ERROR(stream, "array overflow");
391 return func(stream, iter->pos, pItem);
395 PB_RETURN_ERROR(stream, "invalid field type");
399 #ifdef PB_ENABLE_MALLOC
400 /* Allocate storage for the field and store the pointer at iter->pData.
401 * array_size is the number of entries to reserve in an array.
402 * Zero size is not allowed, use pb_free() for releasing.
404 static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
406 void *ptr = *(void**)pData;
408 if (data_size == 0 || array_size == 0)
409 PB_RETURN_ERROR(stream, "invalid size");
411 /* Check for multiplication overflows.
412 * This code avoids the costly division if the sizes are small enough.
413 * Multiplication is safe as long as only half of bits are set
414 * in either multiplicand.
417 const size_t check_limit = (size_t)1 << (sizeof(size_t) * 4);
418 if (data_size >= check_limit || array_size >= check_limit)
420 const size_t size_max = (size_t)-1;
421 if (size_max / array_size < data_size)
423 PB_RETURN_ERROR(stream, "size too large");
428 /* Allocate new or expand previous allocation */
429 /* Note: on failure the old pointer will remain in the structure,
430 * the message must be freed by caller also on error return. */
431 ptr = pb_realloc(ptr, array_size * data_size);
433 PB_RETURN_ERROR(stream, "realloc failed");
435 *(void**)pData = ptr;
439 /* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
440 static void initialize_pointer_field(void *pItem, pb_field_iter_t *iter)
442 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_STRING ||
443 PB_LTYPE(iter->pos->type) == PB_LTYPE_BYTES)
445 *(void**)pItem = NULL;
447 else if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
449 pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, pItem);
454 static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
456 #ifndef PB_ENABLE_MALLOC
457 PB_UNUSED(wire_type);
459 PB_RETURN_ERROR(stream, "no malloc support");
464 type = iter->pos->type;
465 func = PB_DECODERS[PB_LTYPE(type)];
467 switch (PB_HTYPE(type))
469 case PB_HTYPE_REQUIRED:
470 case PB_HTYPE_OPTIONAL:
471 if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE &&
472 *(void**)iter->pData != NULL)
474 /* Duplicate field, have to release the old allocation first. */
475 pb_release_single_field(iter);
478 if (PB_LTYPE(type) == PB_LTYPE_STRING ||
479 PB_LTYPE(type) == PB_LTYPE_BYTES)
481 return func(stream, iter->pos, iter->pData);
485 if (!allocate_field(stream, iter->pData, iter->pos->data_size, 1))
488 initialize_pointer_field(*(void**)iter->pData, iter);
489 return func(stream, iter->pos, *(void**)iter->pData);
492 case PB_HTYPE_REPEATED:
493 if (wire_type == PB_WT_STRING
494 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
496 /* Packed array, multiple items come in at once. */
498 pb_size_t *size = (pb_size_t*)iter->pSize;
499 size_t allocated_size = *size;
501 pb_istream_t substream;
503 if (!pb_make_string_substream(stream, &substream))
506 while (substream.bytes_left)
508 if ((size_t)*size + 1 > allocated_size)
510 /* Allocate more storage. This tries to guess the
511 * number of remaining entries. Round the division
513 allocated_size += (substream.bytes_left - 1) / iter->pos->data_size + 1;
515 if (!allocate_field(&substream, iter->pData, iter->pos->data_size, allocated_size))
522 /* Decode the array entry */
523 pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size);
524 initialize_pointer_field(pItem, iter);
525 if (!func(&substream, iter->pos, pItem))
531 if (*size == PB_SIZE_MAX)
534 stream->errmsg = "too many array entries";
542 pb_close_string_substream(stream, &substream);
548 /* Normal repeated field, i.e. only one item at a time. */
549 pb_size_t *size = (pb_size_t*)iter->pSize;
552 if (*size == PB_SIZE_MAX)
553 PB_RETURN_ERROR(stream, "too many array entries");
556 if (!allocate_field(stream, iter->pData, iter->pos->data_size, *size))
559 pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size - 1);
560 initialize_pointer_field(pItem, iter);
561 return func(stream, iter->pos, pItem);
565 PB_RETURN_ERROR(stream, "invalid field type");
570 static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
572 pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
574 #ifdef PB_OLD_CALLBACK_STYLE
575 void *arg = pCallback->arg;
577 void **arg = &(pCallback->arg);
580 if (pCallback->funcs.decode == NULL)
581 return pb_skip_field(stream, wire_type);
583 if (wire_type == PB_WT_STRING)
585 pb_istream_t substream;
587 if (!pb_make_string_substream(stream, &substream))
592 if (!pCallback->funcs.decode(&substream, iter->pos, arg))
593 PB_RETURN_ERROR(stream, "callback failed");
594 } while (substream.bytes_left);
596 pb_close_string_substream(stream, &substream);
601 /* Copy the single scalar value to stack.
602 * This is required so that we can limit the stream length,
603 * which in turn allows to use same callback for packed and
604 * not-packed fields. */
605 pb_istream_t substream;
607 size_t size = sizeof(buffer);
609 if (!read_raw_value(stream, wire_type, buffer, &size))
611 substream = pb_istream_from_buffer(buffer, size);
613 return pCallback->funcs.decode(&substream, iter->pos, arg);
617 static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
619 switch (PB_ATYPE(iter->pos->type))
621 case PB_ATYPE_STATIC:
622 return decode_static_field(stream, wire_type, iter);
624 case PB_ATYPE_POINTER:
625 return decode_pointer_field(stream, wire_type, iter);
627 case PB_ATYPE_CALLBACK:
628 return decode_callback_field(stream, wire_type, iter);
631 PB_RETURN_ERROR(stream, "invalid field type");
635 /* Default handler for extension fields. Expects a pb_field_t structure
636 * in extension->type->arg. */
637 static bool checkreturn default_extension_decoder(pb_istream_t *stream,
638 pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
640 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
641 pb_field_iter_t iter;
643 if (field->tag != tag)
646 /* Fake a field iterator for the extension field.
647 * It is not actually safe to advance this iterator, but decode_field
648 * will not even try to. */
649 (void)pb_field_iter_begin(&iter, field, extension->dest);
650 iter.pData = extension->dest;
651 iter.pSize = &extension->found;
653 return decode_field(stream, wire_type, &iter);
656 /* Try to decode an unknown field as an extension field. Tries each extension
657 * decoder in turn, until one of them handles the field or loop ends. */
658 static bool checkreturn decode_extension(pb_istream_t *stream,
659 uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter)
661 pb_extension_t *extension = *(pb_extension_t* const *)iter->pData;
662 size_t pos = stream->bytes_left;
664 while (extension != NULL && pos == stream->bytes_left)
667 if (extension->type->decode)
668 status = extension->type->decode(stream, extension, tag, wire_type);
670 status = default_extension_decoder(stream, extension, tag, wire_type);
675 extension = extension->next;
681 /* Step through the iterator until an extension field is found or until all
682 * entries have been checked. There can be only one extension field per
683 * message. Returns false if no extension field is found. */
684 static bool checkreturn find_extension_field(pb_field_iter_t *iter)
686 const pb_field_t *start = iter->pos;
689 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_EXTENSION)
691 (void)pb_field_iter_next(iter);
692 } while (iter->pos != start);
697 /* Initialize message fields to default values, recursively */
698 static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
700 pb_field_iter_t iter;
702 if (!pb_field_iter_begin(&iter, fields, dest_struct))
703 return; /* Empty message type */
708 type = iter.pos->type;
710 if (PB_ATYPE(type) == PB_ATYPE_STATIC)
712 if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL)
714 /* Set has_field to false. Still initialize the optional field
716 *(bool*)iter.pSize = false;
718 else if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
720 /* Set array count to 0, no need to initialize contents. */
721 *(pb_size_t*)iter.pSize = 0;
725 if (PB_LTYPE(iter.pos->type) == PB_LTYPE_SUBMESSAGE)
727 /* Initialize submessage to defaults */
728 pb_message_set_to_defaults((const pb_field_t *) iter.pos->ptr, iter.pData);
730 else if (iter.pos->ptr != NULL)
732 /* Initialize to default value */
733 memcpy(iter.pData, iter.pos->ptr, iter.pos->data_size);
737 /* Initialize to zeros */
738 memset(iter.pData, 0, iter.pos->data_size);
741 else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
743 /* Initialize the pointer to NULL. */
744 *(void**)iter.pData = NULL;
746 /* Initialize array count to 0. */
747 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
749 *(pb_size_t*)iter.pSize = 0;
752 else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
754 /* Don't overwrite callback */
756 } while (pb_field_iter_next(&iter));
759 /*********************
760 * Decode all fields *
761 *********************/
763 bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
765 uint8_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 7) / 8] = {0, 0, 0, 0, 0, 0, 0, 0};
766 uint32_t extension_range_start = 0;
767 pb_field_iter_t iter;
769 /* Return value ignored, as empty message types will be correctly handled by
770 * pb_field_iter_find() anyway. */
771 (void)pb_field_iter_begin(&iter, fields, dest_struct);
773 while (stream->bytes_left)
776 pb_wire_type_t wire_type;
779 if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
787 if (!pb_field_iter_find(&iter, tag))
789 /* No match found, check if it matches an extension. */
790 if (tag >= extension_range_start)
792 if (!find_extension_field(&iter))
793 extension_range_start = (uint32_t)-1;
795 extension_range_start = iter.pos->tag;
797 if (tag >= extension_range_start)
799 size_t pos = stream->bytes_left;
801 if (!decode_extension(stream, tag, wire_type, &iter))
804 if (pos != stream->bytes_left)
806 /* The field was handled */
812 /* No match found, skip data */
813 if (!pb_skip_field(stream, wire_type))
818 if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REQUIRED
819 && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
821 fields_seen[iter.required_field_index >> 3] |= (uint8_t)(1 << (iter.required_field_index & 7));
824 if (!decode_field(stream, wire_type, &iter))
828 /* Check that all required fields were present. */
830 /* First figure out the number of required fields by
831 * seeking to the end of the field array. Usually we
832 * are already close to end after decoding.
834 unsigned req_field_count;
838 req_field_count = iter.required_field_index;
839 last_type = iter.pos->type;
840 } while (pb_field_iter_next(&iter));
842 /* Fixup if last field was also required. */
843 if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.pos->tag != 0)
846 /* Check the whole bytes */
847 for (i = 0; i < (req_field_count >> 3); i++)
849 if (fields_seen[i] != 0xFF)
850 PB_RETURN_ERROR(stream, "missing required field");
853 /* Check the remaining bits */
854 if (fields_seen[req_field_count >> 3] != (0xFF >> (8 - (req_field_count & 7))))
855 PB_RETURN_ERROR(stream, "missing required field");
861 bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
864 pb_message_set_to_defaults(fields, dest_struct);
865 status = pb_decode_noinit(stream, fields, dest_struct);
867 #ifdef PB_ENABLE_MALLOC
869 pb_release(fields, dest_struct);
875 bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
877 pb_istream_t substream;
880 if (!pb_make_string_substream(stream, &substream))
883 status = pb_decode(&substream, fields, dest_struct);
884 pb_close_string_substream(stream, &substream);
888 #ifdef PB_ENABLE_MALLOC
889 static void pb_release_single_field(const pb_field_iter_t *iter)
892 type = iter->pos->type;
894 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
896 if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
897 (PB_LTYPE(type) == PB_LTYPE_STRING ||
898 PB_LTYPE(type) == PB_LTYPE_BYTES))
900 /* Release entries in repeated string or bytes array */
901 void **pItem = *(void***)iter->pData;
902 pb_size_t count = *(pb_size_t*)iter->pSize;
908 *(pb_size_t*)iter->pSize = 0;
910 else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
912 /* Release fields in submessages */
913 void *pItem = *(void**)iter->pData;
918 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
920 count = *(pb_size_t*)iter->pSize;
921 *(pb_size_t*)iter->pSize = 0;
926 pb_release((const pb_field_t*)iter->pos->ptr, pItem);
927 pItem = (uint8_t*)pItem + iter->pos->data_size;
932 /* Release main item */
933 pb_free(*(void**)iter->pData);
934 *(void**)iter->pData = NULL;
938 void pb_release(const pb_field_t fields[], void *dest_struct)
940 pb_field_iter_t iter;
942 if (!pb_field_iter_begin(&iter, fields, dest_struct))
943 return; /* Empty message type */
947 pb_release_single_field(&iter);
948 } while (pb_field_iter_next(&iter));
954 bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
957 if (!pb_decode_varint(stream, &value))
961 *dest = (int64_t)(~(value >> 1));
963 *dest = (int64_t)(value >> 1);
968 bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
970 #ifdef __BIG_ENDIAN__
971 uint8_t *bytes = (uint8_t*)dest;
974 if (!pb_read(stream, lebytes, 4))
977 bytes[0] = lebytes[3];
978 bytes[1] = lebytes[2];
979 bytes[2] = lebytes[1];
980 bytes[3] = lebytes[0];
983 return pb_read(stream, (uint8_t*)dest, 4);
987 bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
989 #ifdef __BIG_ENDIAN__
990 uint8_t *bytes = (uint8_t*)dest;
993 if (!pb_read(stream, lebytes, 8))
996 bytes[0] = lebytes[7];
997 bytes[1] = lebytes[6];
998 bytes[2] = lebytes[5];
999 bytes[3] = lebytes[4];
1000 bytes[4] = lebytes[3];
1001 bytes[5] = lebytes[2];
1002 bytes[6] = lebytes[1];
1003 bytes[7] = lebytes[0];
1006 return pb_read(stream, (uint8_t*)dest, 8);
1010 static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1013 if (!pb_decode_varint(stream, &value))
1016 switch (field->data_size)
1018 case 1: *(int8_t*)dest = (int8_t)value; break;
1019 case 2: *(int16_t*)dest = (int16_t)value; break;
1020 case 4: *(int32_t*)dest = (int32_t)value; break;
1021 case 8: *(int64_t*)dest = (int64_t)value; break;
1022 default: PB_RETURN_ERROR(stream, "invalid data_size");
1028 static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1031 if (!pb_decode_varint(stream, &value))
1034 switch (field->data_size)
1036 case 4: *(uint32_t*)dest = (uint32_t)value; break;
1037 case 8: *(uint64_t*)dest = value; break;
1038 default: PB_RETURN_ERROR(stream, "invalid data_size");
1044 static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1047 if (!pb_decode_svarint(stream, &value))
1050 switch (field->data_size)
1052 case 4: *(int32_t*)dest = (int32_t)value; break;
1053 case 8: *(int64_t*)dest = value; break;
1054 default: PB_RETURN_ERROR(stream, "invalid data_size");
1060 static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
1063 return pb_decode_fixed32(stream, dest);
1066 static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
1069 return pb_decode_fixed64(stream, dest);
1072 static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
1076 pb_bytes_array_t *bdest;
1078 if (!pb_decode_varint32(stream, &size))
1081 if (size > PB_SIZE_MAX)
1082 PB_RETURN_ERROR(stream, "bytes overflow");
1084 alloc_size = PB_BYTES_ARRAY_T_ALLOCSIZE(size);
1085 if (size > alloc_size)
1086 PB_RETURN_ERROR(stream, "size too large");
1088 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1090 #ifndef PB_ENABLE_MALLOC
1091 PB_RETURN_ERROR(stream, "no malloc support");
1093 if (!allocate_field(stream, dest, alloc_size, 1))
1095 bdest = *(pb_bytes_array_t**)dest;
1100 if (alloc_size > field->data_size)
1101 PB_RETURN_ERROR(stream, "bytes overflow");
1102 bdest = (pb_bytes_array_t*)dest;
1105 bdest->size = (pb_size_t)size;
1106 return pb_read(stream, bdest->bytes, size);
1109 static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
1114 if (!pb_decode_varint32(stream, &size))
1117 /* Space for null terminator */
1118 alloc_size = size + 1;
1120 if (alloc_size < size)
1121 PB_RETURN_ERROR(stream, "size too large");
1123 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1125 #ifndef PB_ENABLE_MALLOC
1126 PB_RETURN_ERROR(stream, "no malloc support");
1128 if (!allocate_field(stream, dest, alloc_size, 1))
1130 dest = *(void**)dest;
1135 if (alloc_size > field->data_size)
1136 PB_RETURN_ERROR(stream, "string overflow");
1139 status = pb_read(stream, (uint8_t*)dest, size);
1140 *((uint8_t*)dest + size) = 0;
1144 static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
1147 pb_istream_t substream;
1148 const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr;
1150 if (!pb_make_string_substream(stream, &substream))
1153 if (field->ptr == NULL)
1154 PB_RETURN_ERROR(stream, "invalid field descriptor");
1156 /* New array entries need to be initialized, while required and optional
1157 * submessages have already been initialized in the top-level pb_decode. */
1158 if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
1159 status = pb_decode(&substream, submsg_fields, dest);
1161 status = pb_decode_noinit(&substream, submsg_fields, dest);
1163 pb_close_string_substream(stream, &substream);