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, pb_byte_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, pb_byte_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] = {
68 NULL, /* extensions */
69 &pb_dec_bytes /* PB_LTYPE_FIXED_LENGTH_BYTES */
72 /*******************************
73 * pb_istream_t implementation *
74 *******************************/
76 static bool checkreturn buf_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
79 const pb_byte_t *source = (const pb_byte_t*)stream->state;
80 stream->state = (pb_byte_t*)stream->state + count;
84 for (i = 0; i < count; i++)
91 bool checkreturn pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
93 #ifndef PB_BUFFER_ONLY
94 if (buf == NULL && stream->callback != buf_read)
96 /* Skip input bytes */
100 if (!pb_read(stream, tmp, 16))
106 return pb_read(stream, tmp, count);
110 if (stream->bytes_left < count)
111 PB_RETURN_ERROR(stream, "end-of-stream");
113 #ifndef PB_BUFFER_ONLY
114 if (!stream->callback(stream, buf, count))
115 PB_RETURN_ERROR(stream, "io error");
117 if (!buf_read(stream, buf, count))
121 stream->bytes_left -= count;
125 /* Read a single byte from input stream. buf may not be NULL.
126 * This is an optimization for the varint decoding. */
127 static bool checkreturn pb_readbyte(pb_istream_t *stream, pb_byte_t *buf)
129 if (stream->bytes_left == 0)
130 PB_RETURN_ERROR(stream, "end-of-stream");
132 #ifndef PB_BUFFER_ONLY
133 if (!stream->callback(stream, buf, 1))
134 PB_RETURN_ERROR(stream, "io error");
136 *buf = *(const pb_byte_t*)stream->state;
137 stream->state = (pb_byte_t*)stream->state + 1;
140 stream->bytes_left--;
145 pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t bufsize)
148 /* Cast away the const from buf without a compiler error. We are
149 * careful to use it only in a const manner in the callbacks.
155 #ifdef PB_BUFFER_ONLY
156 stream.callback = NULL;
158 stream.callback = &buf_read;
161 stream.state = state.state;
162 stream.bytes_left = bufsize;
164 stream.errmsg = NULL;
169 /********************
171 ********************/
173 static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
178 if (!pb_readbyte(stream, &byte))
181 if ((byte & 0x80) == 0)
183 /* Quick case, 1 byte value */
189 uint_fast8_t bitpos = 7;
190 result = byte & 0x7F;
195 PB_RETURN_ERROR(stream, "varint overflow");
197 if (!pb_readbyte(stream, &byte))
200 result |= (uint32_t)(byte & 0x7F) << bitpos;
201 bitpos = (uint_fast8_t)(bitpos + 7);
202 } while (byte & 0x80);
209 bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
212 uint_fast8_t bitpos = 0;
218 PB_RETURN_ERROR(stream, "varint overflow");
220 if (!pb_readbyte(stream, &byte))
223 result |= (uint64_t)(byte & 0x7F) << bitpos;
224 bitpos = (uint_fast8_t)(bitpos + 7);
225 } while (byte & 0x80);
231 bool checkreturn pb_skip_varint(pb_istream_t *stream)
236 if (!pb_read(stream, &byte, 1))
238 } while (byte & 0x80);
242 bool checkreturn pb_skip_string(pb_istream_t *stream)
245 if (!pb_decode_varint32(stream, &length))
248 return pb_read(stream, NULL, length);
251 bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
255 *wire_type = (pb_wire_type_t) 0;
258 if (!pb_decode_varint32(stream, &temp))
260 if (stream->bytes_left == 0)
268 *eof = true; /* Special feature: allow 0-terminated messages. */
273 *wire_type = (pb_wire_type_t)(temp & 7);
277 bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
281 case PB_WT_VARINT: return pb_skip_varint(stream);
282 case PB_WT_64BIT: return pb_read(stream, NULL, 8);
283 case PB_WT_STRING: return pb_skip_string(stream);
284 case PB_WT_32BIT: return pb_read(stream, NULL, 4);
285 default: PB_RETURN_ERROR(stream, "invalid wire_type");
289 /* Read a raw value to buffer, for the purpose of passing it to callback as
290 * a substream. Size is maximum size on call, and actual size on return.
292 static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, pb_byte_t *buf, size_t *size)
294 size_t max_size = *size;
302 if (*size > max_size) return false;
303 if (!pb_read(stream, buf, 1)) return false;
304 } while (*buf++ & 0x80);
309 return pb_read(stream, buf, 8);
313 return pb_read(stream, buf, 4);
315 default: PB_RETURN_ERROR(stream, "invalid wire_type");
319 /* Decode string length from stream and return a substream with limited length.
320 * Remember to close the substream using pb_close_string_substream().
322 bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
325 if (!pb_decode_varint32(stream, &size))
328 *substream = *stream;
329 if (substream->bytes_left < size)
330 PB_RETURN_ERROR(stream, "parent stream too short");
332 substream->bytes_left = size;
333 stream->bytes_left -= size;
337 void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
339 stream->state = substream->state;
342 stream->errmsg = substream->errmsg;
346 /*************************
347 * Decode a single field *
348 *************************/
350 static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
355 type = iter->pos->type;
356 func = PB_DECODERS[PB_LTYPE(type)];
358 switch (PB_HTYPE(type))
360 case PB_HTYPE_REQUIRED:
361 return func(stream, iter->pos, iter->pData);
363 case PB_HTYPE_OPTIONAL:
364 if (iter->pSize != iter->pData)
365 *(bool*)iter->pSize = true;
366 return func(stream, iter->pos, iter->pData);
368 case PB_HTYPE_REPEATED:
369 if (wire_type == PB_WT_STRING
370 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
374 pb_size_t *size = (pb_size_t*)iter->pSize;
375 pb_istream_t substream;
376 if (!pb_make_string_substream(stream, &substream))
379 while (substream.bytes_left > 0 && *size < iter->pos->array_size)
381 void *pItem = (char*)iter->pData + iter->pos->data_size * (*size);
382 if (!func(&substream, iter->pos, pItem))
389 pb_close_string_substream(stream, &substream);
391 if (substream.bytes_left != 0)
392 PB_RETURN_ERROR(stream, "array overflow");
399 pb_size_t *size = (pb_size_t*)iter->pSize;
400 void *pItem = (char*)iter->pData + iter->pos->data_size * (*size);
401 if (*size >= iter->pos->array_size)
402 PB_RETURN_ERROR(stream, "array overflow");
405 return func(stream, iter->pos, pItem);
409 *(pb_size_t*)iter->pSize = iter->pos->tag;
410 if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
412 /* We memset to zero so that any callbacks are set to NULL.
413 * Then set any default values. */
414 memset(iter->pData, 0, iter->pos->data_size);
415 pb_message_set_to_defaults((const pb_field_t*)iter->pos->ptr, iter->pData);
417 return func(stream, iter->pos, iter->pData);
420 PB_RETURN_ERROR(stream, "invalid field type");
424 #ifdef PB_ENABLE_MALLOC
425 /* Allocate storage for the field and store the pointer at iter->pData.
426 * array_size is the number of entries to reserve in an array.
427 * Zero size is not allowed, use pb_free() for releasing.
429 static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
431 void *ptr = *(void**)pData;
433 if (data_size == 0 || array_size == 0)
434 PB_RETURN_ERROR(stream, "invalid size");
436 /* Check for multiplication overflows.
437 * This code avoids the costly division if the sizes are small enough.
438 * Multiplication is safe as long as only half of bits are set
439 * in either multiplicand.
442 const size_t check_limit = (size_t)1 << (sizeof(size_t) * 4);
443 if (data_size >= check_limit || array_size >= check_limit)
445 const size_t size_max = (size_t)-1;
446 if (size_max / array_size < data_size)
448 PB_RETURN_ERROR(stream, "size too large");
453 /* Allocate new or expand previous allocation */
454 /* Note: on failure the old pointer will remain in the structure,
455 * the message must be freed by caller also on error return. */
456 ptr = pb_realloc(ptr, array_size * data_size);
458 PB_RETURN_ERROR(stream, "realloc failed");
460 *(void**)pData = ptr;
464 /* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
465 static void initialize_pointer_field(void *pItem, pb_field_iter_t *iter)
467 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_STRING ||
468 PB_LTYPE(iter->pos->type) == PB_LTYPE_BYTES)
470 *(void**)pItem = NULL;
472 else if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
474 pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, pItem);
479 static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
481 #ifndef PB_ENABLE_MALLOC
482 PB_UNUSED(wire_type);
484 PB_RETURN_ERROR(stream, "no malloc support");
489 type = iter->pos->type;
490 func = PB_DECODERS[PB_LTYPE(type)];
492 switch (PB_HTYPE(type))
494 case PB_HTYPE_REQUIRED:
495 case PB_HTYPE_OPTIONAL:
497 if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE &&
498 *(void**)iter->pData != NULL)
500 /* Duplicate field, have to release the old allocation first. */
501 pb_release_single_field(iter);
504 if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
506 *(pb_size_t*)iter->pSize = iter->pos->tag;
509 if (PB_LTYPE(type) == PB_LTYPE_STRING ||
510 PB_LTYPE(type) == PB_LTYPE_BYTES)
512 return func(stream, iter->pos, iter->pData);
516 if (!allocate_field(stream, iter->pData, iter->pos->data_size, 1))
519 initialize_pointer_field(*(void**)iter->pData, iter);
520 return func(stream, iter->pos, *(void**)iter->pData);
523 case PB_HTYPE_REPEATED:
524 if (wire_type == PB_WT_STRING
525 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
527 /* Packed array, multiple items come in at once. */
529 pb_size_t *size = (pb_size_t*)iter->pSize;
530 size_t allocated_size = *size;
532 pb_istream_t substream;
534 if (!pb_make_string_substream(stream, &substream))
537 while (substream.bytes_left)
539 if ((size_t)*size + 1 > allocated_size)
541 /* Allocate more storage. This tries to guess the
542 * number of remaining entries. Round the division
544 allocated_size += (substream.bytes_left - 1) / iter->pos->data_size + 1;
546 if (!allocate_field(&substream, iter->pData, iter->pos->data_size, allocated_size))
553 /* Decode the array entry */
554 pItem = *(char**)iter->pData + iter->pos->data_size * (*size);
555 initialize_pointer_field(pItem, iter);
556 if (!func(&substream, iter->pos, pItem))
562 if (*size == PB_SIZE_MAX)
565 stream->errmsg = "too many array entries";
573 pb_close_string_substream(stream, &substream);
579 /* Normal repeated field, i.e. only one item at a time. */
580 pb_size_t *size = (pb_size_t*)iter->pSize;
583 if (*size == PB_SIZE_MAX)
584 PB_RETURN_ERROR(stream, "too many array entries");
587 if (!allocate_field(stream, iter->pData, iter->pos->data_size, *size))
590 pItem = *(char**)iter->pData + iter->pos->data_size * (*size - 1);
591 initialize_pointer_field(pItem, iter);
592 return func(stream, iter->pos, pItem);
596 PB_RETURN_ERROR(stream, "invalid field type");
601 static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
603 pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
605 #ifdef PB_OLD_CALLBACK_STYLE
606 void *arg = pCallback->arg;
608 void **arg = &(pCallback->arg);
611 if (pCallback->funcs.decode == NULL)
612 return pb_skip_field(stream, wire_type);
614 if (wire_type == PB_WT_STRING)
616 pb_istream_t substream;
618 if (!pb_make_string_substream(stream, &substream))
623 if (!pCallback->funcs.decode(&substream, iter->pos, arg))
624 PB_RETURN_ERROR(stream, "callback failed");
625 } while (substream.bytes_left);
627 pb_close_string_substream(stream, &substream);
632 /* Copy the single scalar value to stack.
633 * This is required so that we can limit the stream length,
634 * which in turn allows to use same callback for packed and
635 * not-packed fields. */
636 pb_istream_t substream;
637 pb_byte_t buffer[10];
638 size_t size = sizeof(buffer);
640 if (!read_raw_value(stream, wire_type, buffer, &size))
642 substream = pb_istream_from_buffer(buffer, size);
644 return pCallback->funcs.decode(&substream, iter->pos, arg);
648 static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
650 #ifdef PB_ENABLE_MALLOC
651 /* When decoding an oneof field, check if there is old data that must be
653 if (PB_HTYPE(iter->pos->type) == PB_HTYPE_ONEOF)
655 if (!pb_release_union_field(stream, iter))
660 switch (PB_ATYPE(iter->pos->type))
662 case PB_ATYPE_STATIC:
663 return decode_static_field(stream, wire_type, iter);
665 case PB_ATYPE_POINTER:
666 return decode_pointer_field(stream, wire_type, iter);
668 case PB_ATYPE_CALLBACK:
669 return decode_callback_field(stream, wire_type, iter);
672 PB_RETURN_ERROR(stream, "invalid field type");
676 static void iter_from_extension(pb_field_iter_t *iter, pb_extension_t *extension)
678 /* Fake a field iterator for the extension field.
679 * It is not actually safe to advance this iterator, but decode_field
680 * will not even try to. */
681 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
682 (void)pb_field_iter_begin(iter, field, extension->dest);
683 iter->pData = extension->dest;
684 iter->pSize = &extension->found;
686 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
688 /* For pointer extensions, the pointer is stored directly
689 * in the extension structure. This avoids having an extra
691 iter->pData = &extension->dest;
695 /* Default handler for extension fields. Expects a pb_field_t structure
696 * in extension->type->arg. */
697 static bool checkreturn default_extension_decoder(pb_istream_t *stream,
698 pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
700 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
701 pb_field_iter_t iter;
703 if (field->tag != tag)
706 iter_from_extension(&iter, extension);
707 extension->found = true;
708 return decode_field(stream, wire_type, &iter);
711 /* Try to decode an unknown field as an extension field. Tries each extension
712 * decoder in turn, until one of them handles the field or loop ends. */
713 static bool checkreturn decode_extension(pb_istream_t *stream,
714 uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter)
716 pb_extension_t *extension = *(pb_extension_t* const *)iter->pData;
717 size_t pos = stream->bytes_left;
719 while (extension != NULL && pos == stream->bytes_left)
722 if (extension->type->decode)
723 status = extension->type->decode(stream, extension, tag, wire_type);
725 status = default_extension_decoder(stream, extension, tag, wire_type);
730 extension = extension->next;
736 /* Step through the iterator until an extension field is found or until all
737 * entries have been checked. There can be only one extension field per
738 * message. Returns false if no extension field is found. */
739 static bool checkreturn find_extension_field(pb_field_iter_t *iter)
741 const pb_field_t *start = iter->pos;
744 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_EXTENSION)
746 (void)pb_field_iter_next(iter);
747 } while (iter->pos != start);
752 /* Initialize message fields to default values, recursively */
753 static void pb_field_set_to_default(pb_field_iter_t *iter)
756 type = iter->pos->type;
758 if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
760 pb_extension_t *ext = *(pb_extension_t* const *)iter->pData;
763 pb_field_iter_t ext_iter;
765 iter_from_extension(&ext_iter, ext);
766 pb_field_set_to_default(&ext_iter);
770 else if (PB_ATYPE(type) == PB_ATYPE_STATIC)
772 bool init_data = true;
773 if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL)
775 /* Set has_field to false. Still initialize the optional field
777 *(bool*)iter->pSize = false;
779 else if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
780 PB_HTYPE(type) == PB_HTYPE_ONEOF)
782 /* REPEATED: Set array count to 0, no need to initialize contents.
783 ONEOF: Set which_field to 0. */
784 *(pb_size_t*)iter->pSize = 0;
790 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
792 /* Initialize submessage to defaults */
793 pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, iter->pData);
795 else if (iter->pos->ptr != NULL)
797 /* Initialize to default value */
798 memcpy(iter->pData, iter->pos->ptr, iter->pos->data_size);
802 /* Initialize to zeros */
803 memset(iter->pData, 0, iter->pos->data_size);
807 else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
809 /* Initialize the pointer to NULL. */
810 *(void**)iter->pData = NULL;
812 /* Initialize array count to 0. */
813 if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
814 PB_HTYPE(type) == PB_HTYPE_ONEOF)
816 *(pb_size_t*)iter->pSize = 0;
819 else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
821 /* Don't overwrite callback */
825 static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
827 pb_field_iter_t iter;
829 if (!pb_field_iter_begin(&iter, fields, dest_struct))
830 return; /* Empty message type */
834 pb_field_set_to_default(&iter);
835 } while (pb_field_iter_next(&iter));
838 /*********************
839 * Decode all fields *
840 *********************/
842 bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
844 uint32_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 31) / 32] = {0, 0};
845 const uint32_t allbits = ~(uint32_t)0;
846 uint32_t extension_range_start = 0;
847 pb_field_iter_t iter;
849 /* Return value ignored, as empty message types will be correctly handled by
850 * pb_field_iter_find() anyway. */
851 (void)pb_field_iter_begin(&iter, fields, dest_struct);
853 while (stream->bytes_left)
856 pb_wire_type_t wire_type;
859 if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
867 if (!pb_field_iter_find(&iter, tag))
869 /* No match found, check if it matches an extension. */
870 if (tag >= extension_range_start)
872 if (!find_extension_field(&iter))
873 extension_range_start = (uint32_t)-1;
875 extension_range_start = iter.pos->tag;
877 if (tag >= extension_range_start)
879 size_t pos = stream->bytes_left;
881 if (!decode_extension(stream, tag, wire_type, &iter))
884 if (pos != stream->bytes_left)
886 /* The field was handled */
892 /* No match found, skip data */
893 if (!pb_skip_field(stream, wire_type))
898 if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REQUIRED
899 && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
901 uint32_t tmp = ((uint32_t)1 << (iter.required_field_index & 31));
902 fields_seen[iter.required_field_index >> 5] |= tmp;
905 if (!decode_field(stream, wire_type, &iter))
909 /* Check that all required fields were present. */
911 /* First figure out the number of required fields by
912 * seeking to the end of the field array. Usually we
913 * are already close to end after decoding.
915 unsigned req_field_count;
919 req_field_count = iter.required_field_index;
920 last_type = iter.pos->type;
921 } while (pb_field_iter_next(&iter));
923 /* Fixup if last field was also required. */
924 if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.pos->tag != 0)
927 if (req_field_count > 0)
929 /* Check the whole words */
930 for (i = 0; i < (req_field_count >> 5); i++)
932 if (fields_seen[i] != allbits)
933 PB_RETURN_ERROR(stream, "missing required field");
936 /* Check the remaining bits */
937 if (fields_seen[req_field_count >> 5] != (allbits >> (32 - (req_field_count & 31))))
938 PB_RETURN_ERROR(stream, "missing required field");
945 bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
948 pb_message_set_to_defaults(fields, dest_struct);
949 status = pb_decode_noinit(stream, fields, dest_struct);
951 #ifdef PB_ENABLE_MALLOC
953 pb_release(fields, dest_struct);
959 bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
961 pb_istream_t substream;
964 if (!pb_make_string_substream(stream, &substream))
967 status = pb_decode(&substream, fields, dest_struct);
968 pb_close_string_substream(stream, &substream);
972 #ifdef PB_ENABLE_MALLOC
973 /* Given an oneof field, if there has already been a field inside this oneof,
974 * release it before overwriting with a different one. */
975 static bool pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *iter)
977 pb_size_t old_tag = *(pb_size_t*)iter->pSize; /* Previous which_ value */
978 pb_size_t new_tag = iter->pos->tag; /* New which_ value */
981 return true; /* Ok, no old data in union */
983 if (old_tag == new_tag)
984 return true; /* Ok, old data is of same type => merge */
986 /* Release old data. The find can fail if the message struct contains
988 if (!pb_field_iter_find(iter, old_tag))
989 PB_RETURN_ERROR(stream, "invalid union tag");
991 pb_release_single_field(iter);
993 /* Restore iterator to where it should be.
994 * This shouldn't fail unless the pb_field_t structure is corrupted. */
995 if (!pb_field_iter_find(iter, new_tag))
996 PB_RETURN_ERROR(stream, "iterator error");
1001 static void pb_release_single_field(const pb_field_iter_t *iter)
1004 type = iter->pos->type;
1006 if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
1008 if (*(pb_size_t*)iter->pSize != iter->pos->tag)
1009 return; /* This is not the current field in the union */
1012 /* Release anything contained inside an extension or submsg.
1013 * This has to be done even if the submsg itself is statically
1015 if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
1017 /* Release fields from all extensions in the linked list */
1018 pb_extension_t *ext = *(pb_extension_t**)iter->pData;
1021 pb_field_iter_t ext_iter;
1022 iter_from_extension(&ext_iter, ext);
1023 pb_release_single_field(&ext_iter);
1027 else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
1029 /* Release fields in submessage or submsg array */
1030 void *pItem = iter->pData;
1031 pb_size_t count = 1;
1033 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
1035 pItem = *(void**)iter->pData;
1038 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
1040 count = *(pb_size_t*)iter->pSize;
1042 if (PB_ATYPE(type) == PB_ATYPE_STATIC && count > iter->pos->array_size)
1044 /* Protect against corrupted _count fields */
1045 count = iter->pos->array_size;
1053 pb_release((const pb_field_t*)iter->pos->ptr, pItem);
1054 pItem = (char*)pItem + iter->pos->data_size;
1059 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
1061 if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
1062 (PB_LTYPE(type) == PB_LTYPE_STRING ||
1063 PB_LTYPE(type) == PB_LTYPE_BYTES))
1065 /* Release entries in repeated string or bytes array */
1066 void **pItem = *(void***)iter->pData;
1067 pb_size_t count = *(pb_size_t*)iter->pSize;
1075 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
1077 /* We are going to release the array, so set the size to 0 */
1078 *(pb_size_t*)iter->pSize = 0;
1081 /* Release main item */
1082 pb_free(*(void**)iter->pData);
1083 *(void**)iter->pData = NULL;
1087 void pb_release(const pb_field_t fields[], void *dest_struct)
1089 pb_field_iter_t iter;
1092 return; /* Ignore NULL pointers, similar to free() */
1094 if (!pb_field_iter_begin(&iter, fields, dest_struct))
1095 return; /* Empty message type */
1099 pb_release_single_field(&iter);
1100 } while (pb_field_iter_next(&iter));
1104 /* Field decoders */
1106 bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
1109 if (!pb_decode_varint(stream, &value))
1113 *dest = (int64_t)(~(value >> 1));
1115 *dest = (int64_t)(value >> 1);
1120 bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
1124 if (!pb_read(stream, bytes, 4))
1127 *(uint32_t*)dest = ((uint32_t)bytes[0] << 0) |
1128 ((uint32_t)bytes[1] << 8) |
1129 ((uint32_t)bytes[2] << 16) |
1130 ((uint32_t)bytes[3] << 24);
1134 bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
1138 if (!pb_read(stream, bytes, 8))
1141 *(uint64_t*)dest = ((uint64_t)bytes[0] << 0) |
1142 ((uint64_t)bytes[1] << 8) |
1143 ((uint64_t)bytes[2] << 16) |
1144 ((uint64_t)bytes[3] << 24) |
1145 ((uint64_t)bytes[4] << 32) |
1146 ((uint64_t)bytes[5] << 40) |
1147 ((uint64_t)bytes[6] << 48) |
1148 ((uint64_t)bytes[7] << 56);
1153 static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1158 if (!pb_decode_varint(stream, &value))
1161 /* See issue 97: Google's C++ protobuf allows negative varint values to
1162 * be cast as int32_t, instead of the int64_t that should be used when
1163 * encoding. Previous nanopb versions had a bug in encoding. In order to
1164 * not break decoding of such messages, we cast <=32 bit fields to
1165 * int32_t first to get the sign correct.
1167 if (field->data_size == sizeof(int64_t))
1168 svalue = (int64_t)value;
1170 svalue = (int32_t)value;
1172 /* Cast to the proper field size, while checking for overflows */
1173 if (field->data_size == sizeof(int64_t))
1174 clamped = *(int64_t*)dest = svalue;
1175 else if (field->data_size == sizeof(int32_t))
1176 clamped = *(int32_t*)dest = (int32_t)svalue;
1177 else if (field->data_size == sizeof(int_least16_t))
1178 clamped = *(int_least16_t*)dest = (int_least16_t)svalue;
1179 else if (field->data_size == sizeof(int_least8_t))
1180 clamped = *(int_least8_t*)dest = (int_least8_t)svalue;
1182 PB_RETURN_ERROR(stream, "invalid data_size");
1184 if (clamped != svalue)
1185 PB_RETURN_ERROR(stream, "integer too large");
1190 static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1192 uint64_t value, clamped;
1193 if (!pb_decode_varint(stream, &value))
1196 /* Cast to the proper field size, while checking for overflows */
1197 if (field->data_size == sizeof(uint64_t))
1198 clamped = *(uint64_t*)dest = value;
1199 else if (field->data_size == sizeof(uint32_t))
1200 clamped = *(uint32_t*)dest = (uint32_t)value;
1201 else if (field->data_size == sizeof(uint_least16_t))
1202 clamped = *(uint_least16_t*)dest = (uint_least16_t)value;
1203 else if (field->data_size == sizeof(uint_least8_t))
1204 clamped = *(uint_least8_t*)dest = (uint_least8_t)value;
1206 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_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1216 int64_t value, clamped;
1217 if (!pb_decode_svarint(stream, &value))
1220 /* Cast to the proper field size, while checking for overflows */
1221 if (field->data_size == sizeof(int64_t))
1222 clamped = *(int64_t*)dest = value;
1223 else if (field->data_size == sizeof(int32_t))
1224 clamped = *(int32_t*)dest = (int32_t)value;
1225 else if (field->data_size == sizeof(int_least16_t))
1226 clamped = *(int_least16_t*)dest = (int_least16_t)value;
1227 else if (field->data_size == sizeof(int_least8_t))
1228 clamped = *(int_least8_t*)dest = (int_least8_t)value;
1230 PB_RETURN_ERROR(stream, "invalid data_size");
1232 if (clamped != value)
1233 PB_RETURN_ERROR(stream, "integer too large");
1238 static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
1241 return pb_decode_fixed32(stream, dest);
1244 static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
1247 return pb_decode_fixed64(stream, dest);
1250 static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
1254 pb_bytes_array_t *bdest;
1256 if (!pb_decode_varint32(stream, &size))
1259 if (size > PB_SIZE_MAX)
1260 PB_RETURN_ERROR(stream, "bytes overflow");
1262 alloc_size = PB_BYTES_ARRAY_T_ALLOCSIZE(size);
1263 if (size > alloc_size)
1264 PB_RETURN_ERROR(stream, "size too large");
1266 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1268 #ifndef PB_ENABLE_MALLOC
1269 PB_RETURN_ERROR(stream, "no malloc support");
1271 if (!allocate_field(stream, dest, alloc_size, 1))
1273 bdest = *(pb_bytes_array_t**)dest;
1278 if (PB_LTYPE(field->type) == PB_LTYPE_FIXED_LENGTH_BYTES) {
1279 if (size != field->data_size)
1280 PB_RETURN_ERROR(stream, "incorrect inline bytes size");
1281 return pb_read(stream, (pb_byte_t*)dest, field->data_size);
1284 if (alloc_size > field->data_size)
1285 PB_RETURN_ERROR(stream, "bytes overflow");
1286 bdest = (pb_bytes_array_t*)dest;
1289 bdest->size = (pb_size_t)size;
1290 return pb_read(stream, bdest->bytes, size);
1293 static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
1298 if (!pb_decode_varint32(stream, &size))
1301 /* Space for null terminator */
1302 alloc_size = size + 1;
1304 if (alloc_size < size)
1305 PB_RETURN_ERROR(stream, "size too large");
1307 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1309 #ifndef PB_ENABLE_MALLOC
1310 PB_RETURN_ERROR(stream, "no malloc support");
1312 if (!allocate_field(stream, dest, alloc_size, 1))
1314 dest = *(void**)dest;
1319 if (alloc_size > field->data_size)
1320 PB_RETURN_ERROR(stream, "string overflow");
1323 status = pb_read(stream, (pb_byte_t*)dest, size);
1324 *((pb_byte_t*)dest + size) = 0;
1328 static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
1331 pb_istream_t substream;
1332 const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr;
1334 if (!pb_make_string_substream(stream, &substream))
1337 if (field->ptr == NULL)
1338 PB_RETURN_ERROR(stream, "invalid field descriptor");
1340 /* New array entries need to be initialized, while required and optional
1341 * submessages have already been initialized in the top-level pb_decode. */
1342 if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
1343 status = pb_decode(&substream, submsg_fields, dest);
1345 status = pb_decode_noinit(&substream, submsg_fields, dest);
1347 pb_close_string_substream(stream, &substream);