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 bool checkreturn pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *iter);
52 static void pb_release_single_field(const pb_field_iter_t *iter);
55 /* --- Function pointers to field decoders ---
56 * Order in the array must match pb_action_t LTYPE numbering.
58 static const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = {
71 /*******************************
72 * pb_istream_t implementation *
73 *******************************/
75 static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count)
77 uint8_t *source = (uint8_t*)stream->state;
78 stream->state = source + count;
89 bool checkreturn pb_read(pb_istream_t *stream, uint8_t *buf, size_t count)
91 #ifndef PB_BUFFER_ONLY
92 if (buf == NULL && stream->callback != buf_read)
94 /* Skip input bytes */
98 if (!pb_read(stream, tmp, 16))
104 return pb_read(stream, tmp, count);
108 if (stream->bytes_left < count)
109 PB_RETURN_ERROR(stream, "end-of-stream");
111 #ifndef PB_BUFFER_ONLY
112 if (!stream->callback(stream, buf, count))
113 PB_RETURN_ERROR(stream, "io error");
115 if (!buf_read(stream, buf, count))
119 stream->bytes_left -= count;
123 /* Read a single byte from input stream. buf may not be NULL.
124 * This is an optimization for the varint decoding. */
125 static bool checkreturn pb_readbyte(pb_istream_t *stream, uint8_t *buf)
127 if (stream->bytes_left == 0)
128 PB_RETURN_ERROR(stream, "end-of-stream");
130 #ifndef PB_BUFFER_ONLY
131 if (!stream->callback(stream, buf, 1))
132 PB_RETURN_ERROR(stream, "io error");
134 *buf = *(uint8_t*)stream->state;
135 stream->state = (uint8_t*)stream->state + 1;
138 stream->bytes_left--;
143 pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize)
146 #ifdef PB_BUFFER_ONLY
147 stream.callback = NULL;
149 stream.callback = &buf_read;
152 stream.bytes_left = bufsize;
154 stream.errmsg = NULL;
159 /********************
161 ********************/
163 static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
168 if (!pb_readbyte(stream, &byte))
171 if ((byte & 0x80) == 0)
173 /* Quick case, 1 byte value */
180 result = byte & 0x7F;
185 PB_RETURN_ERROR(stream, "varint overflow");
187 if (!pb_readbyte(stream, &byte))
190 result |= (uint32_t)(byte & 0x7F) << bitpos;
191 bitpos = (uint8_t)(bitpos + 7);
192 } while (byte & 0x80);
199 bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
208 PB_RETURN_ERROR(stream, "varint overflow");
210 if (!pb_readbyte(stream, &byte))
213 result |= (uint64_t)(byte & 0x7F) << bitpos;
214 bitpos = (uint8_t)(bitpos + 7);
215 } while (byte & 0x80);
221 bool checkreturn pb_skip_varint(pb_istream_t *stream)
226 if (!pb_read(stream, &byte, 1))
228 } while (byte & 0x80);
232 bool checkreturn pb_skip_string(pb_istream_t *stream)
235 if (!pb_decode_varint32(stream, &length))
238 return pb_read(stream, NULL, length);
241 bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
245 *wire_type = (pb_wire_type_t) 0;
248 if (!pb_decode_varint32(stream, &temp))
250 if (stream->bytes_left == 0)
258 *eof = true; /* Special feature: allow 0-terminated messages. */
263 *wire_type = (pb_wire_type_t)(temp & 7);
267 bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
271 case PB_WT_VARINT: return pb_skip_varint(stream);
272 case PB_WT_64BIT: return pb_read(stream, NULL, 8);
273 case PB_WT_STRING: return pb_skip_string(stream);
274 case PB_WT_32BIT: return pb_read(stream, NULL, 4);
275 default: PB_RETURN_ERROR(stream, "invalid wire_type");
279 /* Read a raw value to buffer, for the purpose of passing it to callback as
280 * a substream. Size is maximum size on call, and actual size on return.
282 static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, uint8_t *buf, size_t *size)
284 size_t max_size = *size;
292 if (*size > max_size) return false;
293 if (!pb_read(stream, buf, 1)) return false;
294 } while (*buf++ & 0x80);
299 return pb_read(stream, buf, 8);
303 return pb_read(stream, buf, 4);
305 default: PB_RETURN_ERROR(stream, "invalid wire_type");
309 /* Decode string length from stream and return a substream with limited length.
310 * Remember to close the substream using pb_close_string_substream().
312 bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
315 if (!pb_decode_varint32(stream, &size))
318 *substream = *stream;
319 if (substream->bytes_left < size)
320 PB_RETURN_ERROR(stream, "parent stream too short");
322 substream->bytes_left = size;
323 stream->bytes_left -= size;
327 void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
329 stream->state = substream->state;
332 stream->errmsg = substream->errmsg;
336 /*************************
337 * Decode a single field *
338 *************************/
340 static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
345 type = iter->pos->type;
346 func = PB_DECODERS[PB_LTYPE(type)];
348 switch (PB_HTYPE(type))
350 case PB_HTYPE_REQUIRED:
351 return func(stream, iter->pos, iter->pData);
353 case PB_HTYPE_OPTIONAL:
354 *(bool*)iter->pSize = true;
355 return func(stream, iter->pos, iter->pData);
357 case PB_HTYPE_REPEATED:
358 if (wire_type == PB_WT_STRING
359 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
363 pb_size_t *size = (pb_size_t*)iter->pSize;
364 pb_istream_t substream;
365 if (!pb_make_string_substream(stream, &substream))
368 while (substream.bytes_left > 0 && *size < iter->pos->array_size)
370 void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
371 if (!func(&substream, iter->pos, pItem))
378 pb_close_string_substream(stream, &substream);
380 if (substream.bytes_left != 0)
381 PB_RETURN_ERROR(stream, "array overflow");
388 pb_size_t *size = (pb_size_t*)iter->pSize;
389 void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
390 if (*size >= iter->pos->array_size)
391 PB_RETURN_ERROR(stream, "array overflow");
394 return func(stream, iter->pos, pItem);
398 *(pb_size_t*)iter->pSize = iter->pos->tag;
399 if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
401 /* We memset to zero so that any callbacks are set to NULL.
402 * Then set any default values. */
403 memset(iter->pData, 0, iter->pos->data_size);
404 pb_message_set_to_defaults((const pb_field_t*)iter->pos->ptr, iter->pData);
406 return func(stream, iter->pos, iter->pData);
409 PB_RETURN_ERROR(stream, "invalid field type");
413 #ifdef PB_ENABLE_MALLOC
414 /* Allocate storage for the field and store the pointer at iter->pData.
415 * array_size is the number of entries to reserve in an array.
416 * Zero size is not allowed, use pb_free() for releasing.
418 static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
420 void *ptr = *(void**)pData;
422 if (data_size == 0 || array_size == 0)
423 PB_RETURN_ERROR(stream, "invalid size");
425 /* Check for multiplication overflows.
426 * This code avoids the costly division if the sizes are small enough.
427 * Multiplication is safe as long as only half of bits are set
428 * in either multiplicand.
431 const size_t check_limit = (size_t)1 << (sizeof(size_t) * 4);
432 if (data_size >= check_limit || array_size >= check_limit)
434 const size_t size_max = (size_t)-1;
435 if (size_max / array_size < data_size)
437 PB_RETURN_ERROR(stream, "size too large");
442 /* Allocate new or expand previous allocation */
443 /* Note: on failure the old pointer will remain in the structure,
444 * the message must be freed by caller also on error return. */
445 ptr = pb_realloc(ptr, array_size * data_size);
447 PB_RETURN_ERROR(stream, "realloc failed");
449 *(void**)pData = ptr;
453 /* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
454 static void initialize_pointer_field(void *pItem, pb_field_iter_t *iter)
456 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_STRING ||
457 PB_LTYPE(iter->pos->type) == PB_LTYPE_BYTES)
459 *(void**)pItem = NULL;
461 else if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
463 pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, pItem);
468 static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
470 #ifndef PB_ENABLE_MALLOC
471 PB_UNUSED(wire_type);
473 PB_RETURN_ERROR(stream, "no malloc support");
478 type = iter->pos->type;
479 func = PB_DECODERS[PB_LTYPE(type)];
481 switch (PB_HTYPE(type))
483 case PB_HTYPE_REQUIRED:
484 case PB_HTYPE_OPTIONAL:
486 if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE &&
487 *(void**)iter->pData != NULL)
489 /* Duplicate field, have to release the old allocation first. */
490 pb_release_single_field(iter);
493 if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
495 *(pb_size_t*)iter->pSize = iter->pos->tag;
498 if (PB_LTYPE(type) == PB_LTYPE_STRING ||
499 PB_LTYPE(type) == PB_LTYPE_BYTES)
501 return func(stream, iter->pos, iter->pData);
505 if (!allocate_field(stream, iter->pData, iter->pos->data_size, 1))
508 initialize_pointer_field(*(void**)iter->pData, iter);
509 return func(stream, iter->pos, *(void**)iter->pData);
512 case PB_HTYPE_REPEATED:
513 if (wire_type == PB_WT_STRING
514 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
516 /* Packed array, multiple items come in at once. */
518 pb_size_t *size = (pb_size_t*)iter->pSize;
519 size_t allocated_size = *size;
521 pb_istream_t substream;
523 if (!pb_make_string_substream(stream, &substream))
526 while (substream.bytes_left)
528 if ((size_t)*size + 1 > allocated_size)
530 /* Allocate more storage. This tries to guess the
531 * number of remaining entries. Round the division
533 allocated_size += (substream.bytes_left - 1) / iter->pos->data_size + 1;
535 if (!allocate_field(&substream, iter->pData, iter->pos->data_size, allocated_size))
542 /* Decode the array entry */
543 pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size);
544 initialize_pointer_field(pItem, iter);
545 if (!func(&substream, iter->pos, pItem))
551 if (*size == PB_SIZE_MAX)
554 stream->errmsg = "too many array entries";
562 pb_close_string_substream(stream, &substream);
568 /* Normal repeated field, i.e. only one item at a time. */
569 pb_size_t *size = (pb_size_t*)iter->pSize;
572 if (*size == PB_SIZE_MAX)
573 PB_RETURN_ERROR(stream, "too many array entries");
576 if (!allocate_field(stream, iter->pData, iter->pos->data_size, *size))
579 pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size - 1);
580 initialize_pointer_field(pItem, iter);
581 return func(stream, iter->pos, pItem);
585 PB_RETURN_ERROR(stream, "invalid field type");
590 static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
592 pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
594 #ifdef PB_OLD_CALLBACK_STYLE
595 void *arg = pCallback->arg;
597 void **arg = &(pCallback->arg);
600 if (pCallback->funcs.decode == NULL)
601 return pb_skip_field(stream, wire_type);
603 if (wire_type == PB_WT_STRING)
605 pb_istream_t substream;
607 if (!pb_make_string_substream(stream, &substream))
612 if (!pCallback->funcs.decode(&substream, iter->pos, arg))
613 PB_RETURN_ERROR(stream, "callback failed");
614 } while (substream.bytes_left);
616 pb_close_string_substream(stream, &substream);
621 /* Copy the single scalar value to stack.
622 * This is required so that we can limit the stream length,
623 * which in turn allows to use same callback for packed and
624 * not-packed fields. */
625 pb_istream_t substream;
627 size_t size = sizeof(buffer);
629 if (!read_raw_value(stream, wire_type, buffer, &size))
631 substream = pb_istream_from_buffer(buffer, size);
633 return pCallback->funcs.decode(&substream, iter->pos, arg);
637 static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
639 #ifdef PB_ENABLE_MALLOC
640 /* When decoding an oneof field, check if there is old data that must be
642 if (PB_HTYPE(iter->pos->type) == PB_HTYPE_ONEOF)
644 if (!pb_release_union_field(stream, iter))
649 switch (PB_ATYPE(iter->pos->type))
651 case PB_ATYPE_STATIC:
652 return decode_static_field(stream, wire_type, iter);
654 case PB_ATYPE_POINTER:
655 return decode_pointer_field(stream, wire_type, iter);
657 case PB_ATYPE_CALLBACK:
658 return decode_callback_field(stream, wire_type, iter);
661 PB_RETURN_ERROR(stream, "invalid field type");
665 static void iter_from_extension(pb_field_iter_t *iter, pb_extension_t *extension)
667 /* Fake a field iterator for the extension field.
668 * It is not actually safe to advance this iterator, but decode_field
669 * will not even try to. */
670 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
671 (void)pb_field_iter_begin(iter, field, extension->dest);
672 iter->pData = extension->dest;
673 iter->pSize = &extension->found;
675 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
677 /* For pointer extensions, the pointer is stored directly
678 * in the extension structure. This avoids having an extra
680 iter->pData = &extension->dest;
684 /* Default handler for extension fields. Expects a pb_field_t structure
685 * in extension->type->arg. */
686 static bool checkreturn default_extension_decoder(pb_istream_t *stream,
687 pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
689 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
690 pb_field_iter_t iter;
692 if (field->tag != tag)
695 iter_from_extension(&iter, extension);
696 extension->found = true;
697 return decode_field(stream, wire_type, &iter);
700 /* Try to decode an unknown field as an extension field. Tries each extension
701 * decoder in turn, until one of them handles the field or loop ends. */
702 static bool checkreturn decode_extension(pb_istream_t *stream,
703 uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter)
705 pb_extension_t *extension = *(pb_extension_t* const *)iter->pData;
706 size_t pos = stream->bytes_left;
708 while (extension != NULL && pos == stream->bytes_left)
711 if (extension->type->decode)
712 status = extension->type->decode(stream, extension, tag, wire_type);
714 status = default_extension_decoder(stream, extension, tag, wire_type);
719 extension = extension->next;
725 /* Step through the iterator until an extension field is found or until all
726 * entries have been checked. There can be only one extension field per
727 * message. Returns false if no extension field is found. */
728 static bool checkreturn find_extension_field(pb_field_iter_t *iter)
730 const pb_field_t *start = iter->pos;
733 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_EXTENSION)
735 (void)pb_field_iter_next(iter);
736 } while (iter->pos != start);
741 /* Initialize message fields to default values, recursively */
742 static void pb_field_set_to_default(pb_field_iter_t *iter)
745 type = iter->pos->type;
747 if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
749 pb_extension_t *ext = *(pb_extension_t* const *)iter->pData;
752 pb_field_iter_t ext_iter;
754 iter_from_extension(&ext_iter, ext);
755 pb_field_set_to_default(&ext_iter);
759 else if (PB_ATYPE(type) == PB_ATYPE_STATIC)
761 bool init_data = true;
762 if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL)
764 /* Set has_field to false. Still initialize the optional field
766 *(bool*)iter->pSize = false;
768 else if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
769 PB_HTYPE(type) == PB_HTYPE_ONEOF)
771 /* REPEATED: Set array count to 0, no need to initialize contents.
772 ONEOF: Set which_field to 0. */
773 *(pb_size_t*)iter->pSize = 0;
779 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
781 /* Initialize submessage to defaults */
782 pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, iter->pData);
784 else if (iter->pos->ptr != NULL)
786 /* Initialize to default value */
787 memcpy(iter->pData, iter->pos->ptr, iter->pos->data_size);
791 /* Initialize to zeros */
792 memset(iter->pData, 0, iter->pos->data_size);
796 else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
798 /* Initialize the pointer to NULL. */
799 *(void**)iter->pData = NULL;
801 /* Initialize array count to 0. */
802 if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
803 PB_HTYPE(type) == PB_HTYPE_ONEOF)
805 *(pb_size_t*)iter->pSize = 0;
808 else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
810 /* Don't overwrite callback */
814 static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
816 pb_field_iter_t iter;
818 if (!pb_field_iter_begin(&iter, fields, dest_struct))
819 return; /* Empty message type */
823 pb_field_set_to_default(&iter);
824 } while (pb_field_iter_next(&iter));
827 /*********************
828 * Decode all fields *
829 *********************/
831 bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
833 uint8_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 7) / 8] = {0, 0, 0, 0, 0, 0, 0, 0};
834 uint32_t extension_range_start = 0;
835 pb_field_iter_t iter;
837 /* Return value ignored, as empty message types will be correctly handled by
838 * pb_field_iter_find() anyway. */
839 (void)pb_field_iter_begin(&iter, fields, dest_struct);
841 while (stream->bytes_left)
844 pb_wire_type_t wire_type;
847 if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
855 if (!pb_field_iter_find(&iter, tag))
857 /* No match found, check if it matches an extension. */
858 if (tag >= extension_range_start)
860 if (!find_extension_field(&iter))
861 extension_range_start = (uint32_t)-1;
863 extension_range_start = iter.pos->tag;
865 if (tag >= extension_range_start)
867 size_t pos = stream->bytes_left;
869 if (!decode_extension(stream, tag, wire_type, &iter))
872 if (pos != stream->bytes_left)
874 /* The field was handled */
880 /* No match found, skip data */
881 if (!pb_skip_field(stream, wire_type))
886 if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REQUIRED
887 && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
889 uint8_t tmp = (uint8_t)(1 << (iter.required_field_index & 7));
890 fields_seen[iter.required_field_index >> 3] |= tmp;
893 if (!decode_field(stream, wire_type, &iter))
897 /* Check that all required fields were present. */
899 /* First figure out the number of required fields by
900 * seeking to the end of the field array. Usually we
901 * are already close to end after decoding.
903 unsigned req_field_count;
907 req_field_count = iter.required_field_index;
908 last_type = iter.pos->type;
909 } while (pb_field_iter_next(&iter));
911 /* Fixup if last field was also required. */
912 if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.pos->tag != 0)
915 /* Check the whole bytes */
916 for (i = 0; i < (req_field_count >> 3); i++)
918 if (fields_seen[i] != 0xFF)
919 PB_RETURN_ERROR(stream, "missing required field");
922 /* Check the remaining bits */
923 if (fields_seen[req_field_count >> 3] != (0xFF >> (8 - (req_field_count & 7))))
924 PB_RETURN_ERROR(stream, "missing required field");
930 bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
933 pb_message_set_to_defaults(fields, dest_struct);
934 status = pb_decode_noinit(stream, fields, dest_struct);
936 #ifdef PB_ENABLE_MALLOC
938 pb_release(fields, dest_struct);
944 bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
946 pb_istream_t substream;
949 if (!pb_make_string_substream(stream, &substream))
952 status = pb_decode(&substream, fields, dest_struct);
953 pb_close_string_substream(stream, &substream);
957 #ifdef PB_ENABLE_MALLOC
958 /* Given an oneof field, if there has already been a field inside this oneof,
959 * release it before overwriting with a different one. */
960 static bool pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *iter)
962 pb_size_t old_tag = *(pb_size_t*)iter->pSize; /* Previous which_ value */
963 pb_size_t new_tag = iter->pos->tag; /* New which_ value */
966 return true; /* Ok, no old data in union */
968 if (old_tag == new_tag)
969 return true; /* Ok, old data is of same type => merge */
971 /* Release old data. The find can fail if the message struct contains
973 if (!pb_field_iter_find(iter, old_tag))
974 PB_RETURN_ERROR(stream, "invalid union tag");
976 pb_release_single_field(iter);
978 /* Restore iterator to where it should be.
979 * This shouldn't fail unless the pb_field_t structure is corrupted. */
980 if (!pb_field_iter_find(iter, new_tag))
981 PB_RETURN_ERROR(stream, "iterator error");
986 static void pb_release_single_field(const pb_field_iter_t *iter)
989 type = iter->pos->type;
991 if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
993 if (*(pb_size_t*)iter->pSize != iter->pos->tag)
994 return; /* This is not the current field in the union */
997 /* Release anything contained inside an extension or submsg.
998 * This has to be done even if the submsg itself is statically
1000 if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
1002 /* Release fields from all extensions in the linked list */
1003 pb_extension_t *ext = *(pb_extension_t**)iter->pData;
1006 pb_field_iter_t ext_iter;
1007 iter_from_extension(&ext_iter, ext);
1008 pb_release_single_field(&ext_iter);
1012 else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
1014 /* Release fields in submessage or submsg array */
1015 void *pItem = iter->pData;
1016 pb_size_t count = 1;
1018 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
1020 pItem = *(void**)iter->pData;
1023 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
1025 count = *(pb_size_t*)iter->pSize;
1032 pb_release((const pb_field_t*)iter->pos->ptr, pItem);
1033 pItem = (uint8_t*)pItem + iter->pos->data_size;
1038 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
1040 if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
1041 (PB_LTYPE(type) == PB_LTYPE_STRING ||
1042 PB_LTYPE(type) == PB_LTYPE_BYTES))
1044 /* Release entries in repeated string or bytes array */
1045 void **pItem = *(void***)iter->pData;
1046 pb_size_t count = *(pb_size_t*)iter->pSize;
1054 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
1056 /* We are going to release the array, so set the size to 0 */
1057 *(pb_size_t*)iter->pSize = 0;
1060 /* Release main item */
1061 pb_free(*(void**)iter->pData);
1062 *(void**)iter->pData = NULL;
1066 void pb_release(const pb_field_t fields[], void *dest_struct)
1068 pb_field_iter_t iter;
1070 if (!pb_field_iter_begin(&iter, fields, dest_struct))
1071 return; /* Empty message type */
1075 pb_release_single_field(&iter);
1076 } while (pb_field_iter_next(&iter));
1080 /* Field decoders */
1082 bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
1085 if (!pb_decode_varint(stream, &value))
1089 *dest = (int64_t)(~(value >> 1));
1091 *dest = (int64_t)(value >> 1);
1096 bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
1098 #ifdef __BIG_ENDIAN__
1099 uint8_t *bytes = (uint8_t*)dest;
1102 if (!pb_read(stream, lebytes, 4))
1105 bytes[0] = lebytes[3];
1106 bytes[1] = lebytes[2];
1107 bytes[2] = lebytes[1];
1108 bytes[3] = lebytes[0];
1111 return pb_read(stream, (uint8_t*)dest, 4);
1115 bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
1117 #ifdef __BIG_ENDIAN__
1118 uint8_t *bytes = (uint8_t*)dest;
1121 if (!pb_read(stream, lebytes, 8))
1124 bytes[0] = lebytes[7];
1125 bytes[1] = lebytes[6];
1126 bytes[2] = lebytes[5];
1127 bytes[3] = lebytes[4];
1128 bytes[4] = lebytes[3];
1129 bytes[5] = lebytes[2];
1130 bytes[6] = lebytes[1];
1131 bytes[7] = lebytes[0];
1134 return pb_read(stream, (uint8_t*)dest, 8);
1138 static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1143 if (!pb_decode_varint(stream, &value))
1146 /* See issue 97: Google's C++ protobuf allows negative varint values to
1147 * be cast as int32_t, instead of the int64_t that should be used when
1148 * encoding. Previous nanopb versions had a bug in encoding. In order to
1149 * not break decoding of such messages, we cast <=32 bit fields to
1150 * int32_t first to get the sign correct.
1152 if (field->data_size == 8)
1153 svalue = (int64_t)value;
1155 svalue = (int32_t)value;
1157 switch (field->data_size)
1159 case 1: clamped = *(int8_t*)dest = (int8_t)svalue; break;
1160 case 2: clamped = *(int16_t*)dest = (int16_t)svalue; break;
1161 case 4: clamped = *(int32_t*)dest = (int32_t)svalue; break;
1162 case 8: clamped = *(int64_t*)dest = svalue; break;
1163 default: PB_RETURN_ERROR(stream, "invalid data_size");
1166 if (clamped != svalue)
1167 PB_RETURN_ERROR(stream, "integer too large");
1172 static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1174 uint64_t value, clamped;
1175 if (!pb_decode_varint(stream, &value))
1178 switch (field->data_size)
1180 case 1: clamped = *(uint8_t*)dest = (uint8_t)value; break;
1181 case 2: clamped = *(uint16_t*)dest = (uint16_t)value; break;
1182 case 4: clamped = *(uint32_t*)dest = (uint32_t)value; break;
1183 case 8: clamped = *(uint64_t*)dest = value; break;
1184 default: PB_RETURN_ERROR(stream, "invalid data_size");
1187 if (clamped != value)
1188 PB_RETURN_ERROR(stream, "integer too large");
1193 static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1195 int64_t value, clamped;
1196 if (!pb_decode_svarint(stream, &value))
1199 switch (field->data_size)
1201 case 1: clamped = *(int8_t*)dest = (int8_t)value; break;
1202 case 2: clamped = *(int16_t*)dest = (int16_t)value; break;
1203 case 4: clamped = *(int32_t*)dest = (int32_t)value; break;
1204 case 8: clamped = *(int64_t*)dest = value; break;
1205 default: PB_RETURN_ERROR(stream, "invalid data_size");
1208 if (clamped != value)
1209 PB_RETURN_ERROR(stream, "integer too large");
1214 static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
1217 return pb_decode_fixed32(stream, dest);
1220 static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
1223 return pb_decode_fixed64(stream, dest);
1226 static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
1230 pb_bytes_array_t *bdest;
1232 if (!pb_decode_varint32(stream, &size))
1235 if (size > PB_SIZE_MAX)
1236 PB_RETURN_ERROR(stream, "bytes overflow");
1238 alloc_size = PB_BYTES_ARRAY_T_ALLOCSIZE(size);
1239 if (size > alloc_size)
1240 PB_RETURN_ERROR(stream, "size too large");
1242 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1244 #ifndef PB_ENABLE_MALLOC
1245 PB_RETURN_ERROR(stream, "no malloc support");
1247 if (!allocate_field(stream, dest, alloc_size, 1))
1249 bdest = *(pb_bytes_array_t**)dest;
1254 if (alloc_size > field->data_size)
1255 PB_RETURN_ERROR(stream, "bytes overflow");
1256 bdest = (pb_bytes_array_t*)dest;
1259 bdest->size = (pb_size_t)size;
1260 return pb_read(stream, bdest->bytes, size);
1263 static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
1268 if (!pb_decode_varint32(stream, &size))
1271 /* Space for null terminator */
1272 alloc_size = size + 1;
1274 if (alloc_size < size)
1275 PB_RETURN_ERROR(stream, "size too large");
1277 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1279 #ifndef PB_ENABLE_MALLOC
1280 PB_RETURN_ERROR(stream, "no malloc support");
1282 if (!allocate_field(stream, dest, alloc_size, 1))
1284 dest = *(void**)dest;
1289 if (alloc_size > field->data_size)
1290 PB_RETURN_ERROR(stream, "string overflow");
1293 status = pb_read(stream, (uint8_t*)dest, size);
1294 *((uint8_t*)dest + size) = 0;
1298 static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
1301 pb_istream_t substream;
1302 const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr;
1304 if (!pb_make_string_substream(stream, &substream))
1307 if (field->ptr == NULL)
1308 PB_RETURN_ERROR(stream, "invalid field descriptor");
1310 /* New array entries need to be initialized, while required and optional
1311 * submessages have already been initialized in the top-level pb_decode. */
1312 if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
1313 status = pb_decode(&substream, submsg_fields, dest);
1315 status = pb_decode_noinit(&substream, submsg_fields, dest);
1317 pb_close_string_substream(stream, &substream);