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 const uint8_t *source = (const uint8_t*)stream->state;
78 stream->state = (uint8_t*)stream->state + 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 = *(const uint8_t*)stream->state;
135 stream->state = (uint8_t*)stream->state + 1;
138 stream->bytes_left--;
143 pb_istream_t pb_istream_from_buffer(const uint8_t *buf, size_t bufsize)
146 /* Cast away the const from buf without a compiler error. We are
147 * careful to use it only in a const manner in the callbacks.
153 #ifdef PB_BUFFER_ONLY
154 stream.callback = NULL;
156 stream.callback = &buf_read;
159 stream.state = state.state;
160 stream.bytes_left = bufsize;
162 stream.errmsg = NULL;
167 /********************
169 ********************/
171 static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
176 if (!pb_readbyte(stream, &byte))
179 if ((byte & 0x80) == 0)
181 /* Quick case, 1 byte value */
188 result = byte & 0x7F;
193 PB_RETURN_ERROR(stream, "varint overflow");
195 if (!pb_readbyte(stream, &byte))
198 result |= (uint32_t)(byte & 0x7F) << bitpos;
199 bitpos = (uint8_t)(bitpos + 7);
200 } while (byte & 0x80);
207 bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
216 PB_RETURN_ERROR(stream, "varint overflow");
218 if (!pb_readbyte(stream, &byte))
221 result |= (uint64_t)(byte & 0x7F) << bitpos;
222 bitpos = (uint8_t)(bitpos + 7);
223 } while (byte & 0x80);
229 bool checkreturn pb_skip_varint(pb_istream_t *stream)
234 if (!pb_read(stream, &byte, 1))
236 } while (byte & 0x80);
240 bool checkreturn pb_skip_string(pb_istream_t *stream)
243 if (!pb_decode_varint32(stream, &length))
246 return pb_read(stream, NULL, length);
249 bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
253 *wire_type = (pb_wire_type_t) 0;
256 if (!pb_decode_varint32(stream, &temp))
258 if (stream->bytes_left == 0)
266 *eof = true; /* Special feature: allow 0-terminated messages. */
271 *wire_type = (pb_wire_type_t)(temp & 7);
275 bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
279 case PB_WT_VARINT: return pb_skip_varint(stream);
280 case PB_WT_64BIT: return pb_read(stream, NULL, 8);
281 case PB_WT_STRING: return pb_skip_string(stream);
282 case PB_WT_32BIT: return pb_read(stream, NULL, 4);
283 default: PB_RETURN_ERROR(stream, "invalid wire_type");
287 /* Read a raw value to buffer, for the purpose of passing it to callback as
288 * a substream. Size is maximum size on call, and actual size on return.
290 static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, uint8_t *buf, size_t *size)
292 size_t max_size = *size;
300 if (*size > max_size) return false;
301 if (!pb_read(stream, buf, 1)) return false;
302 } while (*buf++ & 0x80);
307 return pb_read(stream, buf, 8);
311 return pb_read(stream, buf, 4);
313 default: PB_RETURN_ERROR(stream, "invalid wire_type");
317 /* Decode string length from stream and return a substream with limited length.
318 * Remember to close the substream using pb_close_string_substream().
320 bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
323 if (!pb_decode_varint32(stream, &size))
326 *substream = *stream;
327 if (substream->bytes_left < size)
328 PB_RETURN_ERROR(stream, "parent stream too short");
330 substream->bytes_left = size;
331 stream->bytes_left -= size;
335 void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
337 stream->state = substream->state;
340 stream->errmsg = substream->errmsg;
344 /*************************
345 * Decode a single field *
346 *************************/
348 static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
353 type = iter->pos->type;
354 func = PB_DECODERS[PB_LTYPE(type)];
356 switch (PB_HTYPE(type))
358 case PB_HTYPE_REQUIRED:
359 return func(stream, iter->pos, iter->pData);
361 case PB_HTYPE_OPTIONAL:
362 *(bool*)iter->pSize = true;
363 return func(stream, iter->pos, iter->pData);
365 case PB_HTYPE_REPEATED:
366 if (wire_type == PB_WT_STRING
367 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
371 pb_size_t *size = (pb_size_t*)iter->pSize;
372 pb_istream_t substream;
373 if (!pb_make_string_substream(stream, &substream))
376 while (substream.bytes_left > 0 && *size < iter->pos->array_size)
378 void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
379 if (!func(&substream, iter->pos, pItem))
386 pb_close_string_substream(stream, &substream);
388 if (substream.bytes_left != 0)
389 PB_RETURN_ERROR(stream, "array overflow");
396 pb_size_t *size = (pb_size_t*)iter->pSize;
397 void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
398 if (*size >= iter->pos->array_size)
399 PB_RETURN_ERROR(stream, "array overflow");
402 return func(stream, iter->pos, pItem);
406 *(pb_size_t*)iter->pSize = iter->pos->tag;
407 if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
409 /* We memset to zero so that any callbacks are set to NULL.
410 * Then set any default values. */
411 memset(iter->pData, 0, iter->pos->data_size);
412 pb_message_set_to_defaults((const pb_field_t*)iter->pos->ptr, iter->pData);
414 return func(stream, iter->pos, iter->pData);
417 PB_RETURN_ERROR(stream, "invalid field type");
421 #ifdef PB_ENABLE_MALLOC
422 /* Allocate storage for the field and store the pointer at iter->pData.
423 * array_size is the number of entries to reserve in an array.
424 * Zero size is not allowed, use pb_free() for releasing.
426 static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
428 void *ptr = *(void**)pData;
430 if (data_size == 0 || array_size == 0)
431 PB_RETURN_ERROR(stream, "invalid size");
433 /* Check for multiplication overflows.
434 * This code avoids the costly division if the sizes are small enough.
435 * Multiplication is safe as long as only half of bits are set
436 * in either multiplicand.
439 const size_t check_limit = (size_t)1 << (sizeof(size_t) * 4);
440 if (data_size >= check_limit || array_size >= check_limit)
442 const size_t size_max = (size_t)-1;
443 if (size_max / array_size < data_size)
445 PB_RETURN_ERROR(stream, "size too large");
450 /* Allocate new or expand previous allocation */
451 /* Note: on failure the old pointer will remain in the structure,
452 * the message must be freed by caller also on error return. */
453 ptr = pb_realloc(ptr, array_size * data_size);
455 PB_RETURN_ERROR(stream, "realloc failed");
457 *(void**)pData = ptr;
461 /* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
462 static void initialize_pointer_field(void *pItem, pb_field_iter_t *iter)
464 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_STRING ||
465 PB_LTYPE(iter->pos->type) == PB_LTYPE_BYTES)
467 *(void**)pItem = NULL;
469 else if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
471 pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, pItem);
476 static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
478 #ifndef PB_ENABLE_MALLOC
479 PB_UNUSED(wire_type);
481 PB_RETURN_ERROR(stream, "no malloc support");
486 type = iter->pos->type;
487 func = PB_DECODERS[PB_LTYPE(type)];
489 switch (PB_HTYPE(type))
491 case PB_HTYPE_REQUIRED:
492 case PB_HTYPE_OPTIONAL:
494 if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE &&
495 *(void**)iter->pData != NULL)
497 /* Duplicate field, have to release the old allocation first. */
498 pb_release_single_field(iter);
501 if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
503 *(pb_size_t*)iter->pSize = iter->pos->tag;
506 if (PB_LTYPE(type) == PB_LTYPE_STRING ||
507 PB_LTYPE(type) == PB_LTYPE_BYTES)
509 return func(stream, iter->pos, iter->pData);
513 if (!allocate_field(stream, iter->pData, iter->pos->data_size, 1))
516 initialize_pointer_field(*(void**)iter->pData, iter);
517 return func(stream, iter->pos, *(void**)iter->pData);
520 case PB_HTYPE_REPEATED:
521 if (wire_type == PB_WT_STRING
522 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
524 /* Packed array, multiple items come in at once. */
526 pb_size_t *size = (pb_size_t*)iter->pSize;
527 size_t allocated_size = *size;
529 pb_istream_t substream;
531 if (!pb_make_string_substream(stream, &substream))
534 while (substream.bytes_left)
536 if ((size_t)*size + 1 > allocated_size)
538 /* Allocate more storage. This tries to guess the
539 * number of remaining entries. Round the division
541 allocated_size += (substream.bytes_left - 1) / iter->pos->data_size + 1;
543 if (!allocate_field(&substream, iter->pData, iter->pos->data_size, allocated_size))
550 /* Decode the array entry */
551 pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size);
552 initialize_pointer_field(pItem, iter);
553 if (!func(&substream, iter->pos, pItem))
559 if (*size == PB_SIZE_MAX)
562 stream->errmsg = "too many array entries";
570 pb_close_string_substream(stream, &substream);
576 /* Normal repeated field, i.e. only one item at a time. */
577 pb_size_t *size = (pb_size_t*)iter->pSize;
580 if (*size == PB_SIZE_MAX)
581 PB_RETURN_ERROR(stream, "too many array entries");
584 if (!allocate_field(stream, iter->pData, iter->pos->data_size, *size))
587 pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size - 1);
588 initialize_pointer_field(pItem, iter);
589 return func(stream, iter->pos, pItem);
593 PB_RETURN_ERROR(stream, "invalid field type");
598 static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
600 pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
602 #ifdef PB_OLD_CALLBACK_STYLE
603 void *arg = pCallback->arg;
605 void **arg = &(pCallback->arg);
608 if (pCallback->funcs.decode == NULL)
609 return pb_skip_field(stream, wire_type);
611 if (wire_type == PB_WT_STRING)
613 pb_istream_t substream;
615 if (!pb_make_string_substream(stream, &substream))
620 if (!pCallback->funcs.decode(&substream, iter->pos, arg))
621 PB_RETURN_ERROR(stream, "callback failed");
622 } while (substream.bytes_left);
624 pb_close_string_substream(stream, &substream);
629 /* Copy the single scalar value to stack.
630 * This is required so that we can limit the stream length,
631 * which in turn allows to use same callback for packed and
632 * not-packed fields. */
633 pb_istream_t substream;
635 size_t size = sizeof(buffer);
637 if (!read_raw_value(stream, wire_type, buffer, &size))
639 substream = pb_istream_from_buffer(buffer, size);
641 return pCallback->funcs.decode(&substream, iter->pos, arg);
645 static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
647 #ifdef PB_ENABLE_MALLOC
648 /* When decoding an oneof field, check if there is old data that must be
650 if (PB_HTYPE(iter->pos->type) == PB_HTYPE_ONEOF)
652 if (!pb_release_union_field(stream, iter))
657 switch (PB_ATYPE(iter->pos->type))
659 case PB_ATYPE_STATIC:
660 return decode_static_field(stream, wire_type, iter);
662 case PB_ATYPE_POINTER:
663 return decode_pointer_field(stream, wire_type, iter);
665 case PB_ATYPE_CALLBACK:
666 return decode_callback_field(stream, wire_type, iter);
669 PB_RETURN_ERROR(stream, "invalid field type");
673 static void iter_from_extension(pb_field_iter_t *iter, pb_extension_t *extension)
675 /* Fake a field iterator for the extension field.
676 * It is not actually safe to advance this iterator, but decode_field
677 * will not even try to. */
678 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
679 (void)pb_field_iter_begin(iter, field, extension->dest);
680 iter->pData = extension->dest;
681 iter->pSize = &extension->found;
683 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
685 /* For pointer extensions, the pointer is stored directly
686 * in the extension structure. This avoids having an extra
688 iter->pData = &extension->dest;
692 /* Default handler for extension fields. Expects a pb_field_t structure
693 * in extension->type->arg. */
694 static bool checkreturn default_extension_decoder(pb_istream_t *stream,
695 pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
697 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
698 pb_field_iter_t iter;
700 if (field->tag != tag)
703 iter_from_extension(&iter, extension);
704 extension->found = true;
705 return decode_field(stream, wire_type, &iter);
708 /* Try to decode an unknown field as an extension field. Tries each extension
709 * decoder in turn, until one of them handles the field or loop ends. */
710 static bool checkreturn decode_extension(pb_istream_t *stream,
711 uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter)
713 pb_extension_t *extension = *(pb_extension_t* const *)iter->pData;
714 size_t pos = stream->bytes_left;
716 while (extension != NULL && pos == stream->bytes_left)
719 if (extension->type->decode)
720 status = extension->type->decode(stream, extension, tag, wire_type);
722 status = default_extension_decoder(stream, extension, tag, wire_type);
727 extension = extension->next;
733 /* Step through the iterator until an extension field is found or until all
734 * entries have been checked. There can be only one extension field per
735 * message. Returns false if no extension field is found. */
736 static bool checkreturn find_extension_field(pb_field_iter_t *iter)
738 const pb_field_t *start = iter->pos;
741 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_EXTENSION)
743 (void)pb_field_iter_next(iter);
744 } while (iter->pos != start);
749 /* Initialize message fields to default values, recursively */
750 static void pb_field_set_to_default(pb_field_iter_t *iter)
753 type = iter->pos->type;
755 if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
757 pb_extension_t *ext = *(pb_extension_t* const *)iter->pData;
760 pb_field_iter_t ext_iter;
762 iter_from_extension(&ext_iter, ext);
763 pb_field_set_to_default(&ext_iter);
767 else if (PB_ATYPE(type) == PB_ATYPE_STATIC)
769 bool init_data = true;
770 if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL)
772 /* Set has_field to false. Still initialize the optional field
774 *(bool*)iter->pSize = false;
776 else if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
777 PB_HTYPE(type) == PB_HTYPE_ONEOF)
779 /* REPEATED: Set array count to 0, no need to initialize contents.
780 ONEOF: Set which_field to 0. */
781 *(pb_size_t*)iter->pSize = 0;
787 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
789 /* Initialize submessage to defaults */
790 pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, iter->pData);
792 else if (iter->pos->ptr != NULL)
794 /* Initialize to default value */
795 memcpy(iter->pData, iter->pos->ptr, iter->pos->data_size);
799 /* Initialize to zeros */
800 memset(iter->pData, 0, iter->pos->data_size);
804 else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
806 /* Initialize the pointer to NULL. */
807 *(void**)iter->pData = NULL;
809 /* Initialize array count to 0. */
810 if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
811 PB_HTYPE(type) == PB_HTYPE_ONEOF)
813 *(pb_size_t*)iter->pSize = 0;
816 else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
818 /* Don't overwrite callback */
822 static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
824 pb_field_iter_t iter;
826 if (!pb_field_iter_begin(&iter, fields, dest_struct))
827 return; /* Empty message type */
831 pb_field_set_to_default(&iter);
832 } while (pb_field_iter_next(&iter));
835 /*********************
836 * Decode all fields *
837 *********************/
839 bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
841 uint8_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 7) / 8] = {0, 0, 0, 0, 0, 0, 0, 0};
842 uint32_t extension_range_start = 0;
843 pb_field_iter_t iter;
845 /* Return value ignored, as empty message types will be correctly handled by
846 * pb_field_iter_find() anyway. */
847 (void)pb_field_iter_begin(&iter, fields, dest_struct);
849 while (stream->bytes_left)
852 pb_wire_type_t wire_type;
855 if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
863 if (!pb_field_iter_find(&iter, tag))
865 /* No match found, check if it matches an extension. */
866 if (tag >= extension_range_start)
868 if (!find_extension_field(&iter))
869 extension_range_start = (uint32_t)-1;
871 extension_range_start = iter.pos->tag;
873 if (tag >= extension_range_start)
875 size_t pos = stream->bytes_left;
877 if (!decode_extension(stream, tag, wire_type, &iter))
880 if (pos != stream->bytes_left)
882 /* The field was handled */
888 /* No match found, skip data */
889 if (!pb_skip_field(stream, wire_type))
894 if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REQUIRED
895 && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
897 uint8_t tmp = (uint8_t)(1 << (iter.required_field_index & 7));
898 fields_seen[iter.required_field_index >> 3] |= tmp;
901 if (!decode_field(stream, wire_type, &iter))
905 /* Check that all required fields were present. */
907 /* First figure out the number of required fields by
908 * seeking to the end of the field array. Usually we
909 * are already close to end after decoding.
911 unsigned req_field_count;
915 req_field_count = iter.required_field_index;
916 last_type = iter.pos->type;
917 } while (pb_field_iter_next(&iter));
919 /* Fixup if last field was also required. */
920 if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.pos->tag != 0)
923 /* Check the whole bytes */
924 for (i = 0; i < (req_field_count >> 3); i++)
926 if (fields_seen[i] != 0xFF)
927 PB_RETURN_ERROR(stream, "missing required field");
930 /* Check the remaining bits */
931 if (fields_seen[req_field_count >> 3] != (0xFF >> (8 - (req_field_count & 7))))
932 PB_RETURN_ERROR(stream, "missing required field");
938 bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
941 pb_message_set_to_defaults(fields, dest_struct);
942 status = pb_decode_noinit(stream, fields, dest_struct);
944 #ifdef PB_ENABLE_MALLOC
946 pb_release(fields, dest_struct);
952 bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
954 pb_istream_t substream;
957 if (!pb_make_string_substream(stream, &substream))
960 status = pb_decode(&substream, fields, dest_struct);
961 pb_close_string_substream(stream, &substream);
965 #ifdef PB_ENABLE_MALLOC
966 /* Given an oneof field, if there has already been a field inside this oneof,
967 * release it before overwriting with a different one. */
968 static bool pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *iter)
970 pb_size_t old_tag = *(pb_size_t*)iter->pSize; /* Previous which_ value */
971 pb_size_t new_tag = iter->pos->tag; /* New which_ value */
974 return true; /* Ok, no old data in union */
976 if (old_tag == new_tag)
977 return true; /* Ok, old data is of same type => merge */
979 /* Release old data. The find can fail if the message struct contains
981 if (!pb_field_iter_find(iter, old_tag))
982 PB_RETURN_ERROR(stream, "invalid union tag");
984 pb_release_single_field(iter);
986 /* Restore iterator to where it should be.
987 * This shouldn't fail unless the pb_field_t structure is corrupted. */
988 if (!pb_field_iter_find(iter, new_tag))
989 PB_RETURN_ERROR(stream, "iterator error");
994 static void pb_release_single_field(const pb_field_iter_t *iter)
997 type = iter->pos->type;
999 if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
1001 if (*(pb_size_t*)iter->pSize != iter->pos->tag)
1002 return; /* This is not the current field in the union */
1005 /* Release anything contained inside an extension or submsg.
1006 * This has to be done even if the submsg itself is statically
1008 if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
1010 /* Release fields from all extensions in the linked list */
1011 pb_extension_t *ext = *(pb_extension_t**)iter->pData;
1014 pb_field_iter_t ext_iter;
1015 iter_from_extension(&ext_iter, ext);
1016 pb_release_single_field(&ext_iter);
1020 else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
1022 /* Release fields in submessage or submsg array */
1023 void *pItem = iter->pData;
1024 pb_size_t count = 1;
1026 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
1028 pItem = *(void**)iter->pData;
1031 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
1033 count = *(pb_size_t*)iter->pSize;
1040 pb_release((const pb_field_t*)iter->pos->ptr, pItem);
1041 pItem = (uint8_t*)pItem + iter->pos->data_size;
1046 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
1048 if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
1049 (PB_LTYPE(type) == PB_LTYPE_STRING ||
1050 PB_LTYPE(type) == PB_LTYPE_BYTES))
1052 /* Release entries in repeated string or bytes array */
1053 void **pItem = *(void***)iter->pData;
1054 pb_size_t count = *(pb_size_t*)iter->pSize;
1062 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
1064 /* We are going to release the array, so set the size to 0 */
1065 *(pb_size_t*)iter->pSize = 0;
1068 /* Release main item */
1069 pb_free(*(void**)iter->pData);
1070 *(void**)iter->pData = NULL;
1074 void pb_release(const pb_field_t fields[], void *dest_struct)
1076 pb_field_iter_t iter;
1079 return; /* Ignore NULL pointers, similar to free() */
1081 if (!pb_field_iter_begin(&iter, fields, dest_struct))
1082 return; /* Empty message type */
1086 pb_release_single_field(&iter);
1087 } while (pb_field_iter_next(&iter));
1091 /* Field decoders */
1093 bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
1096 if (!pb_decode_varint(stream, &value))
1100 *dest = (int64_t)(~(value >> 1));
1102 *dest = (int64_t)(value >> 1);
1107 bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
1109 #ifdef __BIG_ENDIAN__
1110 uint8_t *bytes = (uint8_t*)dest;
1113 if (!pb_read(stream, lebytes, 4))
1116 bytes[0] = lebytes[3];
1117 bytes[1] = lebytes[2];
1118 bytes[2] = lebytes[1];
1119 bytes[3] = lebytes[0];
1122 return pb_read(stream, (uint8_t*)dest, 4);
1126 bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
1128 #ifdef __BIG_ENDIAN__
1129 uint8_t *bytes = (uint8_t*)dest;
1132 if (!pb_read(stream, lebytes, 8))
1135 bytes[0] = lebytes[7];
1136 bytes[1] = lebytes[6];
1137 bytes[2] = lebytes[5];
1138 bytes[3] = lebytes[4];
1139 bytes[4] = lebytes[3];
1140 bytes[5] = lebytes[2];
1141 bytes[6] = lebytes[1];
1142 bytes[7] = lebytes[0];
1145 return pb_read(stream, (uint8_t*)dest, 8);
1149 static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1154 if (!pb_decode_varint(stream, &value))
1157 /* See issue 97: Google's C++ protobuf allows negative varint values to
1158 * be cast as int32_t, instead of the int64_t that should be used when
1159 * encoding. Previous nanopb versions had a bug in encoding. In order to
1160 * not break decoding of such messages, we cast <=32 bit fields to
1161 * int32_t first to get the sign correct.
1163 if (field->data_size == 8)
1164 svalue = (int64_t)value;
1166 svalue = (int32_t)value;
1168 switch (field->data_size)
1170 case 1: clamped = *(int8_t*)dest = (int8_t)svalue; break;
1171 case 2: clamped = *(int16_t*)dest = (int16_t)svalue; break;
1172 case 4: clamped = *(int32_t*)dest = (int32_t)svalue; break;
1173 case 8: clamped = *(int64_t*)dest = svalue; break;
1174 default: PB_RETURN_ERROR(stream, "invalid data_size");
1177 if (clamped != svalue)
1178 PB_RETURN_ERROR(stream, "integer too large");
1183 static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1185 uint64_t value, clamped;
1186 if (!pb_decode_varint(stream, &value))
1189 switch (field->data_size)
1191 case 1: clamped = *(uint8_t*)dest = (uint8_t)value; break;
1192 case 2: clamped = *(uint16_t*)dest = (uint16_t)value; break;
1193 case 4: clamped = *(uint32_t*)dest = (uint32_t)value; break;
1194 case 8: clamped = *(uint64_t*)dest = value; break;
1195 default: PB_RETURN_ERROR(stream, "invalid data_size");
1198 if (clamped != value)
1199 PB_RETURN_ERROR(stream, "integer too large");
1204 static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1206 int64_t value, clamped;
1207 if (!pb_decode_svarint(stream, &value))
1210 switch (field->data_size)
1212 case 1: clamped = *(int8_t*)dest = (int8_t)value; break;
1213 case 2: clamped = *(int16_t*)dest = (int16_t)value; break;
1214 case 4: clamped = *(int32_t*)dest = (int32_t)value; break;
1215 case 8: clamped = *(int64_t*)dest = value; break;
1216 default: PB_RETURN_ERROR(stream, "invalid data_size");
1219 if (clamped != value)
1220 PB_RETURN_ERROR(stream, "integer too large");
1225 static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
1228 return pb_decode_fixed32(stream, dest);
1231 static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
1234 return pb_decode_fixed64(stream, dest);
1237 static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
1241 pb_bytes_array_t *bdest;
1243 if (!pb_decode_varint32(stream, &size))
1246 if (size > PB_SIZE_MAX)
1247 PB_RETURN_ERROR(stream, "bytes overflow");
1249 alloc_size = PB_BYTES_ARRAY_T_ALLOCSIZE(size);
1250 if (size > alloc_size)
1251 PB_RETURN_ERROR(stream, "size too large");
1253 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1255 #ifndef PB_ENABLE_MALLOC
1256 PB_RETURN_ERROR(stream, "no malloc support");
1258 if (!allocate_field(stream, dest, alloc_size, 1))
1260 bdest = *(pb_bytes_array_t**)dest;
1265 if (alloc_size > field->data_size)
1266 PB_RETURN_ERROR(stream, "bytes overflow");
1267 bdest = (pb_bytes_array_t*)dest;
1270 bdest->size = (pb_size_t)size;
1271 return pb_read(stream, bdest->bytes, size);
1274 static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
1279 if (!pb_decode_varint32(stream, &size))
1282 /* Space for null terminator */
1283 alloc_size = size + 1;
1285 if (alloc_size < size)
1286 PB_RETURN_ERROR(stream, "size too large");
1288 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1290 #ifndef PB_ENABLE_MALLOC
1291 PB_RETURN_ERROR(stream, "no malloc support");
1293 if (!allocate_field(stream, dest, alloc_size, 1))
1295 dest = *(void**)dest;
1300 if (alloc_size > field->data_size)
1301 PB_RETURN_ERROR(stream, "string overflow");
1304 status = pb_read(stream, (uint8_t*)dest, size);
1305 *((uint8_t*)dest + size) = 0;
1309 static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
1312 pb_istream_t substream;
1313 const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr;
1315 if (!pb_make_string_substream(stream, &substream))
1318 if (field->ptr == NULL)
1319 PB_RETURN_ERROR(stream, "invalid field descriptor");
1321 /* New array entries need to be initialized, while required and optional
1322 * submessages have already been initialized in the top-level pb_decode. */
1323 if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
1324 status = pb_decode(&substream, submsg_fields, dest);
1326 status = pb_decode_noinit(&substream, submsg_fields, dest);
1328 pb_close_string_substream(stream, &substream);