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))
16 #define NANOPB_INTERNALS
18 #include "pb_decode.h"
20 /**************************************
21 * Declarations internal to this file *
22 **************************************/
24 /* Iterator for pb_field_t list */
26 const pb_field_t *start; /* Start of the pb_field_t array */
27 const pb_field_t *pos; /* Current position of the iterator */
28 unsigned field_index; /* Zero-based index of the field. */
29 unsigned required_field_index; /* Zero-based index that counts only the required fields */
30 void *dest_struct; /* Pointer to the destination structure to decode to */
31 void *pData; /* Pointer where to store current field value */
32 void *pSize; /* Pointer where to store the size of current array field */
33 } pb_field_iterator_t;
35 typedef bool (*pb_decoder_t)(pb_istream_t *stream, const pb_field_t *field, void *dest) checkreturn;
37 static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count);
38 static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest);
39 static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, uint8_t *buf, size_t *size);
40 static void pb_field_init(pb_field_iterator_t *iter, const pb_field_t *fields, void *dest_struct);
41 static bool pb_field_next(pb_field_iterator_t *iter);
42 static bool checkreturn pb_field_find(pb_field_iterator_t *iter, uint32_t tag);
43 static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter);
44 static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter);
45 static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter);
46 static bool checkreturn default_extension_decoder(pb_istream_t *stream, pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type);
47 static bool checkreturn decode_extension(pb_istream_t *stream, uint32_t tag, pb_wire_type_t wire_type, pb_field_iterator_t *iter);
48 static bool checkreturn find_extension_field(pb_field_iterator_t *iter);
49 static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct);
50 static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest);
51 static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
52 static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
53 static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest);
54 static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest);
55 static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest);
56 static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest);
57 static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest);
58 static bool checkreturn pb_skip_varint(pb_istream_t *stream);
59 static bool checkreturn pb_skip_string(pb_istream_t *stream);
61 /* --- Function pointers to field decoders ---
62 * Order in the array must match pb_action_t LTYPE numbering.
64 static const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = {
77 /*******************************
78 * pb_istream_t implementation *
79 *******************************/
81 static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count)
83 uint8_t *source = (uint8_t*)stream->state;
84 stream->state = source + count;
95 bool checkreturn pb_read(pb_istream_t *stream, uint8_t *buf, size_t count)
97 #ifndef PB_BUFFER_ONLY
98 if (buf == NULL && stream->callback != buf_read)
100 /* Skip input bytes */
104 if (!pb_read(stream, tmp, 16))
110 return pb_read(stream, tmp, count);
114 if (stream->bytes_left < count)
115 PB_RETURN_ERROR(stream, "end-of-stream");
117 #ifndef PB_BUFFER_ONLY
118 if (!stream->callback(stream, buf, count))
119 PB_RETURN_ERROR(stream, "io error");
121 if (!buf_read(stream, buf, count))
125 stream->bytes_left -= count;
129 /* Read a single byte from input stream. buf may not be NULL.
130 * This is an optimization for the varint decoding. */
131 static bool checkreturn pb_readbyte(pb_istream_t *stream, uint8_t *buf)
133 if (stream->bytes_left == 0)
134 PB_RETURN_ERROR(stream, "end-of-stream");
136 #ifndef PB_BUFFER_ONLY
137 if (!stream->callback(stream, buf, 1))
138 PB_RETURN_ERROR(stream, "io error");
140 *buf = *(uint8_t*)stream->state;
141 stream->state = (uint8_t*)stream->state + 1;
144 stream->bytes_left--;
149 pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize)
152 #ifdef PB_BUFFER_ONLY
153 stream.callback = NULL;
155 stream.callback = &buf_read;
158 stream.bytes_left = bufsize;
160 stream.errmsg = NULL;
165 /********************
167 ********************/
169 static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
174 if (!pb_readbyte(stream, &byte))
177 if ((byte & 0x80) == 0)
179 /* Quick case, 1 byte value */
186 result = byte & 0x7F;
191 PB_RETURN_ERROR(stream, "varint overflow");
193 if (!pb_readbyte(stream, &byte))
196 result |= (uint32_t)(byte & 0x7F) << bitpos;
197 bitpos = (uint8_t)(bitpos + 7);
198 } while (byte & 0x80);
205 bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
214 PB_RETURN_ERROR(stream, "varint overflow");
216 if (!pb_readbyte(stream, &byte))
219 result |= (uint64_t)(byte & 0x7F) << bitpos;
220 bitpos = (uint8_t)(bitpos + 7);
221 } while (byte & 0x80);
227 bool checkreturn pb_skip_varint(pb_istream_t *stream)
232 if (!pb_read(stream, &byte, 1))
234 } while (byte & 0x80);
238 bool checkreturn pb_skip_string(pb_istream_t *stream)
241 if (!pb_decode_varint32(stream, &length))
244 return pb_read(stream, NULL, length);
247 bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
251 *wire_type = (pb_wire_type_t) 0;
254 if (!pb_decode_varint32(stream, &temp))
256 if (stream->bytes_left == 0)
264 *eof = true; /* Special feature: allow 0-terminated messages. */
269 *wire_type = (pb_wire_type_t)(temp & 7);
273 bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
277 case PB_WT_VARINT: return pb_skip_varint(stream);
278 case PB_WT_64BIT: return pb_read(stream, NULL, 8);
279 case PB_WT_STRING: return pb_skip_string(stream);
280 case PB_WT_32BIT: return pb_read(stream, NULL, 4);
281 default: PB_RETURN_ERROR(stream, "invalid wire_type");
285 /* Read a raw value to buffer, for the purpose of passing it to callback as
286 * a substream. Size is maximum size on call, and actual size on return.
288 static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, uint8_t *buf, size_t *size)
290 size_t max_size = *size;
298 if (*size > max_size) return false;
299 if (!pb_read(stream, buf, 1)) return false;
300 } while (*buf++ & 0x80);
305 return pb_read(stream, buf, 8);
309 return pb_read(stream, buf, 4);
311 default: PB_RETURN_ERROR(stream, "invalid wire_type");
315 /* Decode string length from stream and return a substream with limited length.
316 * Remember to close the substream using pb_close_string_substream().
318 bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
321 if (!pb_decode_varint32(stream, &size))
324 *substream = *stream;
325 if (substream->bytes_left < size)
326 PB_RETURN_ERROR(stream, "parent stream too short");
328 substream->bytes_left = size;
329 stream->bytes_left -= size;
333 void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
335 stream->state = substream->state;
338 stream->errmsg = substream->errmsg;
342 static void pb_field_init(pb_field_iterator_t *iter, const pb_field_t *fields, void *dest_struct)
344 iter->start = iter->pos = fields;
345 iter->field_index = 0;
346 iter->required_field_index = 0;
347 iter->pData = (char*)dest_struct + iter->pos->data_offset;
348 iter->pSize = (char*)iter->pData + iter->pos->size_offset;
349 iter->dest_struct = dest_struct;
352 static bool pb_field_next(pb_field_iterator_t *iter)
354 bool notwrapped = true;
355 size_t prev_size = iter->pos->data_size;
357 if (PB_ATYPE(iter->pos->type) == PB_ATYPE_STATIC &&
358 PB_HTYPE(iter->pos->type) == PB_HTYPE_REPEATED)
360 prev_size *= iter->pos->array_size;
362 else if (PB_ATYPE(iter->pos->type) == PB_ATYPE_POINTER)
364 prev_size = sizeof(void*);
367 if (iter->pos->tag == 0)
368 return false; /* Only happens with empty message types */
370 if (PB_HTYPE(iter->pos->type) == PB_HTYPE_REQUIRED)
371 iter->required_field_index++;
375 if (iter->pos->tag == 0)
377 iter->pos = iter->start;
378 iter->field_index = 0;
379 iter->required_field_index = 0;
380 iter->pData = iter->dest_struct;
385 iter->pData = (char*)iter->pData + prev_size + iter->pos->data_offset;
386 iter->pSize = (char*)iter->pData + iter->pos->size_offset;
390 static bool checkreturn pb_field_find(pb_field_iterator_t *iter, uint32_t tag)
392 unsigned start = iter->field_index;
395 if (iter->pos->tag == tag &&
396 PB_LTYPE(iter->pos->type) != PB_LTYPE_EXTENSION)
400 (void)pb_field_next(iter);
401 } while (iter->field_index != start);
406 /*************************
407 * Decode a single field *
408 *************************/
410 static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter)
415 type = iter->pos->type;
416 func = PB_DECODERS[PB_LTYPE(type)];
418 switch (PB_HTYPE(type))
420 case PB_HTYPE_REQUIRED:
421 return func(stream, iter->pos, iter->pData);
423 case PB_HTYPE_OPTIONAL:
424 *(bool*)iter->pSize = true;
425 return func(stream, iter->pos, iter->pData);
427 case PB_HTYPE_REPEATED:
428 if (wire_type == PB_WT_STRING
429 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
433 size_t *size = (size_t*)iter->pSize;
434 pb_istream_t substream;
435 if (!pb_make_string_substream(stream, &substream))
438 while (substream.bytes_left > 0 && *size < iter->pos->array_size)
440 void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
441 if (!func(&substream, iter->pos, pItem))
448 pb_close_string_substream(stream, &substream);
450 if (substream.bytes_left != 0)
451 PB_RETURN_ERROR(stream, "array overflow");
458 size_t *size = (size_t*)iter->pSize;
459 void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
460 if (*size >= iter->pos->array_size)
461 PB_RETURN_ERROR(stream, "array overflow");
464 return func(stream, iter->pos, pItem);
468 PB_RETURN_ERROR(stream, "invalid field type");
472 #ifdef PB_ENABLE_MALLOC
473 /* Allocate storage for the field and store the pointer at iter->pData.
474 * array_size is the number of entries to reserve in an array. */
475 static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
477 void *ptr = *(void**)pData;
478 size_t size = array_size * data_size;
480 /* Allocate new or expand previous allocation */
481 /* Note: on failure the old pointer will remain in the structure,
482 * the message must be freed by caller also on error return. */
483 ptr = pb_realloc(ptr, size);
485 PB_RETURN_ERROR(stream, "realloc failed");
487 *(void**)pData = ptr;
491 /* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
492 static void initialize_pointer_field(void *pItem, pb_field_iterator_t *iter)
494 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_STRING ||
495 PB_LTYPE(iter->pos->type) == PB_LTYPE_BYTES)
497 *(void**)pItem = NULL;
499 else if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
501 pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, pItem);
506 static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter)
508 #ifndef PB_ENABLE_MALLOC
511 PB_RETURN_ERROR(stream, "no malloc support");
516 type = iter->pos->type;
517 func = PB_DECODERS[PB_LTYPE(type)];
519 switch (PB_HTYPE(type))
521 case PB_HTYPE_REQUIRED:
522 case PB_HTYPE_OPTIONAL:
523 if (PB_LTYPE(type) == PB_LTYPE_STRING ||
524 PB_LTYPE(type) == PB_LTYPE_BYTES)
526 return func(stream, iter->pos, iter->pData);
530 if (!allocate_field(stream, iter->pData, iter->pos->data_size, 1))
533 initialize_pointer_field(*(void**)iter->pData, iter);
534 return func(stream, iter->pos, *(void**)iter->pData);
537 case PB_HTYPE_REPEATED:
538 if (wire_type == PB_WT_STRING
539 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
541 /* Packed array, multiple items come in at once. */
543 size_t *size = (size_t*)iter->pSize;
544 size_t allocated_size = *size;
546 pb_istream_t substream;
548 if (!pb_make_string_substream(stream, &substream))
551 while (substream.bytes_left)
553 if (*size + 1 > allocated_size)
555 /* Allocate more storage. This tries to guess the
556 * number of remaining entries. Round the division
558 allocated_size += (substream.bytes_left - 1) / iter->pos->data_size + 1;
560 if (!allocate_field(&substream, iter->pData, iter->pos->data_size, allocated_size))
567 /* Decode the array entry */
568 pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size);
569 initialize_pointer_field(pItem, iter);
570 if (!func(&substream, iter->pos, pItem))
577 pb_close_string_substream(stream, &substream);
583 /* Normal repeated field, i.e. only one item at a time. */
584 size_t *size = (size_t*)iter->pSize;
588 if (!allocate_field(stream, iter->pData, iter->pos->data_size, *size))
591 pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size - 1);
592 initialize_pointer_field(pItem, iter);
593 return func(stream, iter->pos, pItem);
597 PB_RETURN_ERROR(stream, "invalid field type");
602 static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter)
604 pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
606 #ifdef PB_OLD_CALLBACK_STYLE
607 void *arg = pCallback->arg;
609 void **arg = &(pCallback->arg);
612 if (pCallback->funcs.decode == NULL)
613 return pb_skip_field(stream, wire_type);
615 if (wire_type == PB_WT_STRING)
617 pb_istream_t substream;
619 if (!pb_make_string_substream(stream, &substream))
624 if (!pCallback->funcs.decode(&substream, iter->pos, arg))
625 PB_RETURN_ERROR(stream, "callback failed");
626 } while (substream.bytes_left);
628 pb_close_string_substream(stream, &substream);
633 /* Copy the single scalar value to stack.
634 * This is required so that we can limit the stream length,
635 * which in turn allows to use same callback for packed and
636 * not-packed fields. */
637 pb_istream_t substream;
639 size_t size = sizeof(buffer);
641 if (!read_raw_value(stream, wire_type, buffer, &size))
643 substream = pb_istream_from_buffer(buffer, size);
645 return pCallback->funcs.decode(&substream, iter->pos, arg);
649 static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter)
651 switch (PB_ATYPE(iter->pos->type))
653 case PB_ATYPE_STATIC:
654 return decode_static_field(stream, wire_type, iter);
656 case PB_ATYPE_POINTER:
657 return decode_pointer_field(stream, wire_type, iter);
659 case PB_ATYPE_CALLBACK:
660 return decode_callback_field(stream, wire_type, iter);
663 PB_RETURN_ERROR(stream, "invalid field type");
667 /* Default handler for extension fields. Expects a pb_field_t structure
668 * in extension->type->arg. */
669 static bool checkreturn default_extension_decoder(pb_istream_t *stream,
670 pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
672 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
673 pb_field_iterator_t iter;
676 if (field->tag != tag)
681 iter.field_index = 0;
682 iter.required_field_index = 0;
683 iter.dest_struct = extension->dest;
684 iter.pData = extension->dest;
687 return decode_field(stream, wire_type, &iter);
690 /* Try to decode an unknown field as an extension field. Tries each extension
691 * decoder in turn, until one of them handles the field or loop ends. */
692 static bool checkreturn decode_extension(pb_istream_t *stream,
693 uint32_t tag, pb_wire_type_t wire_type, pb_field_iterator_t *iter)
695 pb_extension_t *extension = *(pb_extension_t* const *)iter->pData;
696 size_t pos = stream->bytes_left;
698 while (extension != NULL && pos == stream->bytes_left)
701 if (extension->type->decode)
702 status = extension->type->decode(stream, extension, tag, wire_type);
704 status = default_extension_decoder(stream, extension, tag, wire_type);
709 extension = extension->next;
715 /* Step through the iterator until an extension field is found or until all
716 * entries have been checked. There can be only one extension field per
717 * message. Returns false if no extension field is found. */
718 static bool checkreturn find_extension_field(pb_field_iterator_t *iter)
720 unsigned start = iter->field_index;
723 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_EXTENSION)
725 (void)pb_field_next(iter);
726 } while (iter->field_index != start);
731 /* Initialize message fields to default values, recursively */
732 static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
734 pb_field_iterator_t iter;
735 pb_field_init(&iter, fields, dest_struct);
740 type = iter.pos->type;
742 /* Avoid crash on empty message types (zero fields) */
743 if (iter.pos->tag == 0)
746 if (PB_ATYPE(type) == PB_ATYPE_STATIC)
748 if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL)
750 /* Set has_field to false. Still initialize the optional field
752 *(bool*)iter.pSize = false;
754 else if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
756 /* Set array count to 0, no need to initialize contents. */
757 *(size_t*)iter.pSize = 0;
761 if (PB_LTYPE(iter.pos->type) == PB_LTYPE_SUBMESSAGE)
763 /* Initialize submessage to defaults */
764 pb_message_set_to_defaults((const pb_field_t *) iter.pos->ptr, iter.pData);
766 else if (iter.pos->ptr != NULL)
768 /* Initialize to default value */
769 memcpy(iter.pData, iter.pos->ptr, iter.pos->data_size);
773 /* Initialize to zeros */
774 memset(iter.pData, 0, iter.pos->data_size);
777 else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
779 /* Initialize the pointer to NULL. */
780 *(void**)iter.pData = NULL;
782 /* Initialize array count to 0. */
783 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
785 *(size_t*)iter.pSize = 0;
788 else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
790 /* Don't overwrite callback */
792 } while (pb_field_next(&iter));
795 /*********************
796 * Decode all fields *
797 *********************/
799 bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
801 uint8_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 7) / 8] = {0, 0, 0, 0, 0, 0, 0, 0};
802 uint32_t extension_range_start = 0;
803 pb_field_iterator_t iter;
805 pb_field_init(&iter, fields, dest_struct);
807 while (stream->bytes_left)
810 pb_wire_type_t wire_type;
813 if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
821 if (!pb_field_find(&iter, tag))
823 /* No match found, check if it matches an extension. */
824 if (tag >= extension_range_start)
826 if (!find_extension_field(&iter))
827 extension_range_start = (uint32_t)-1;
829 extension_range_start = iter.pos->tag;
831 if (tag >= extension_range_start)
833 size_t pos = stream->bytes_left;
835 if (!decode_extension(stream, tag, wire_type, &iter))
838 if (pos != stream->bytes_left)
840 /* The field was handled */
846 /* No match found, skip data */
847 if (!pb_skip_field(stream, wire_type))
852 if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REQUIRED
853 && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
855 fields_seen[iter.required_field_index >> 3] |= (uint8_t)(1 << (iter.required_field_index & 7));
858 if (!decode_field(stream, wire_type, &iter))
862 /* Check that all required fields were present. */
864 /* First figure out the number of required fields by
865 * seeking to the end of the field array. Usually we
866 * are already close to end after decoding.
868 unsigned req_field_count;
872 req_field_count = iter.required_field_index;
873 last_type = iter.pos->type;
874 } while (pb_field_next(&iter));
876 /* Fixup if last field was also required. */
877 if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.pos->tag != 0)
880 /* Check the whole bytes */
881 for (i = 0; i < (req_field_count >> 3); i++)
883 if (fields_seen[i] != 0xFF)
884 PB_RETURN_ERROR(stream, "missing required field");
887 /* Check the remaining bits */
888 if (fields_seen[req_field_count >> 3] != (0xFF >> (8 - (req_field_count & 7))))
889 PB_RETURN_ERROR(stream, "missing required field");
895 bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
898 pb_message_set_to_defaults(fields, dest_struct);
899 status = pb_decode_noinit(stream, fields, dest_struct);
901 #ifdef PB_ENABLE_MALLOC
903 pb_release(fields, dest_struct);
909 bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
911 pb_istream_t substream;
914 if (!pb_make_string_substream(stream, &substream))
917 status = pb_decode(&substream, fields, dest_struct);
918 pb_close_string_substream(stream, &substream);
922 #ifdef PB_ENABLE_MALLOC
923 void pb_release(const pb_field_t fields[], void *dest_struct)
925 pb_field_iterator_t iter;
926 pb_field_init(&iter, fields, dest_struct);
931 type = iter.pos->type;
933 /* Avoid crash on empty message types (zero fields) */
934 if (iter.pos->tag == 0)
937 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
939 if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
940 (PB_LTYPE(type) == PB_LTYPE_STRING ||
941 PB_LTYPE(type) == PB_LTYPE_BYTES))
943 /* Release entries in repeated string or bytes array */
944 void **pItem = *(void***)iter.pData;
945 size_t count = *(size_t*)iter.pSize;
952 else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
954 /* Release fields in submessages */
955 void *pItem = *(void**)iter.pData;
956 size_t count = (pItem ? 1 : 0);
958 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
960 count = *(size_t*)iter.pSize;
965 pb_release((const pb_field_t*)iter.pos->ptr, pItem);
966 pItem = (uint8_t*)pItem + iter.pos->data_size;
970 /* Release main item */
971 pb_free(*(void**)iter.pData);
972 *(void**)iter.pData = NULL;
974 } while (pb_field_next(&iter));
980 bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
983 if (!pb_decode_varint(stream, &value))
987 *dest = (int64_t)(~(value >> 1));
989 *dest = (int64_t)(value >> 1);
994 bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
996 #ifdef __BIG_ENDIAN__
997 uint8_t *bytes = (uint8_t*)dest;
1000 if (!pb_read(stream, lebytes, 4))
1003 bytes[0] = lebytes[3];
1004 bytes[1] = lebytes[2];
1005 bytes[2] = lebytes[1];
1006 bytes[3] = lebytes[0];
1009 return pb_read(stream, (uint8_t*)dest, 4);
1013 bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
1015 #ifdef __BIG_ENDIAN__
1016 uint8_t *bytes = (uint8_t*)dest;
1019 if (!pb_read(stream, lebytes, 8))
1022 bytes[0] = lebytes[7];
1023 bytes[1] = lebytes[6];
1024 bytes[2] = lebytes[5];
1025 bytes[3] = lebytes[4];
1026 bytes[4] = lebytes[3];
1027 bytes[5] = lebytes[2];
1028 bytes[6] = lebytes[1];
1029 bytes[7] = lebytes[0];
1032 return pb_read(stream, (uint8_t*)dest, 8);
1036 bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1039 if (!pb_decode_varint(stream, &value))
1042 switch (field->data_size)
1044 case 1: *(int8_t*)dest = (int8_t)value; break;
1045 case 2: *(int16_t*)dest = (int16_t)value; break;
1046 case 4: *(int32_t*)dest = (int32_t)value; break;
1047 case 8: *(int64_t*)dest = (int64_t)value; break;
1048 default: PB_RETURN_ERROR(stream, "invalid data_size");
1054 bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1057 if (!pb_decode_varint(stream, &value))
1060 switch (field->data_size)
1062 case 4: *(uint32_t*)dest = (uint32_t)value; break;
1063 case 8: *(uint64_t*)dest = value; break;
1064 default: PB_RETURN_ERROR(stream, "invalid data_size");
1070 bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1073 if (!pb_decode_svarint(stream, &value))
1076 switch (field->data_size)
1078 case 4: *(int32_t*)dest = (int32_t)value; break;
1079 case 8: *(int64_t*)dest = value; break;
1080 default: PB_RETURN_ERROR(stream, "invalid data_size");
1086 bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
1089 return pb_decode_fixed32(stream, dest);
1092 bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
1095 return pb_decode_fixed64(stream, dest);
1098 bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
1101 pb_bytes_array_t *bdest;
1103 if (!pb_decode_varint32(stream, &size))
1106 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1108 #ifndef PB_ENABLE_MALLOC
1109 PB_RETURN_ERROR(stream, "no malloc support");
1111 if (!allocate_field(stream, dest, PB_BYTES_ARRAY_T_ALLOCSIZE(size), 1))
1113 bdest = *(pb_bytes_array_t**)dest;
1118 if (PB_BYTES_ARRAY_T_ALLOCSIZE(size) > field->data_size)
1119 PB_RETURN_ERROR(stream, "bytes overflow");
1120 bdest = (pb_bytes_array_t*)dest;
1124 return pb_read(stream, bdest->bytes, size);
1127 bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
1132 if (!pb_decode_varint32(stream, &size))
1135 /* Space for null terminator */
1136 alloc_size = size + 1;
1138 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1140 #ifndef PB_ENABLE_MALLOC
1141 PB_RETURN_ERROR(stream, "no malloc support");
1143 if (!allocate_field(stream, dest, alloc_size, 1))
1145 dest = *(void**)dest;
1150 if (alloc_size > field->data_size)
1151 PB_RETURN_ERROR(stream, "string overflow");
1154 status = pb_read(stream, (uint8_t*)dest, size);
1155 *((uint8_t*)dest + size) = 0;
1159 bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
1162 pb_istream_t substream;
1163 const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr;
1165 if (!pb_make_string_substream(stream, &substream))
1168 if (field->ptr == NULL)
1169 PB_RETURN_ERROR(stream, "invalid field descriptor");
1171 /* New array entries need to be initialized, while required and optional
1172 * submessages have already been initialized in the top-level pb_decode. */
1173 if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
1174 status = pb_decode(&substream, submsg_fields, dest);
1176 status = pb_decode_noinit(&substream, submsg_fields, dest);
1178 pb_close_string_substream(stream, &substream);