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 read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, pb_byte_t *buf, size_t *size);
28 static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter);
29 static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter);
30 static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter);
31 static void iter_from_extension(pb_field_iter_t *iter, pb_extension_t *extension);
32 static bool checkreturn default_extension_decoder(pb_istream_t *stream, pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type);
33 static bool checkreturn decode_extension(pb_istream_t *stream, uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter);
34 static bool checkreturn find_extension_field(pb_field_iter_t *iter);
35 static void pb_field_set_to_default(pb_field_iter_t *iter);
36 static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct);
37 static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest);
38 static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
39 static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
40 static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest);
41 static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest);
42 static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest);
43 static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest);
44 static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest);
45 static bool checkreturn pb_skip_varint(pb_istream_t *stream);
46 static bool checkreturn pb_skip_string(pb_istream_t *stream);
48 #ifdef PB_ENABLE_MALLOC
49 static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size);
50 static bool checkreturn pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *iter);
51 static void pb_release_single_field(const pb_field_iter_t *iter);
54 /* --- Function pointers to field decoders ---
55 * Order in the array must match pb_action_t LTYPE numbering.
57 static const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = {
67 NULL, /* extensions */
68 &pb_dec_bytes /* PB_LTYPE_FIXED_LENGTH_BYTES */
71 /*******************************
72 * pb_istream_t implementation *
73 *******************************/
75 static bool checkreturn buf_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
78 const pb_byte_t *source = (const pb_byte_t*)stream->state;
79 stream->state = (pb_byte_t*)stream->state + count;
83 for (i = 0; i < count; i++)
90 bool checkreturn pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
92 #ifndef PB_BUFFER_ONLY
93 if (buf == NULL && stream->callback != buf_read)
95 /* Skip input bytes */
99 if (!pb_read(stream, tmp, 16))
105 return pb_read(stream, tmp, count);
109 if (stream->bytes_left < count)
110 PB_RETURN_ERROR(stream, "end-of-stream");
112 #ifndef PB_BUFFER_ONLY
113 if (!stream->callback(stream, buf, count))
114 PB_RETURN_ERROR(stream, "io error");
116 if (!buf_read(stream, buf, count))
120 stream->bytes_left -= count;
124 /* Read a single byte from input stream. buf may not be NULL.
125 * This is an optimization for the varint decoding. */
126 static bool checkreturn pb_readbyte(pb_istream_t *stream, pb_byte_t *buf)
128 if (stream->bytes_left == 0)
129 PB_RETURN_ERROR(stream, "end-of-stream");
131 #ifndef PB_BUFFER_ONLY
132 if (!stream->callback(stream, buf, 1))
133 PB_RETURN_ERROR(stream, "io error");
135 *buf = *(const pb_byte_t*)stream->state;
136 stream->state = (pb_byte_t*)stream->state + 1;
139 stream->bytes_left--;
144 pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t bufsize)
147 /* Cast away the const from buf without a compiler error. We are
148 * careful to use it only in a const manner in the callbacks.
154 #ifdef PB_BUFFER_ONLY
155 stream.callback = NULL;
157 stream.callback = &buf_read;
160 stream.state = state.state;
161 stream.bytes_left = bufsize;
163 stream.errmsg = NULL;
168 /********************
170 ********************/
172 bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
177 if (!pb_readbyte(stream, &byte))
180 if ((byte & 0x80) == 0)
182 /* Quick case, 1 byte value */
188 uint_fast8_t bitpos = 7;
189 result = byte & 0x7F;
194 PB_RETURN_ERROR(stream, "varint overflow");
196 if (!pb_readbyte(stream, &byte))
199 result |= (uint32_t)(byte & 0x7F) << bitpos;
200 bitpos = (uint_fast8_t)(bitpos + 7);
201 } while (byte & 0x80);
208 bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
211 uint_fast8_t bitpos = 0;
217 PB_RETURN_ERROR(stream, "varint overflow");
219 if (!pb_readbyte(stream, &byte))
222 result |= (uint64_t)(byte & 0x7F) << bitpos;
223 bitpos = (uint_fast8_t)(bitpos + 7);
224 } while (byte & 0x80);
230 bool checkreturn pb_skip_varint(pb_istream_t *stream)
235 if (!pb_read(stream, &byte, 1))
237 } while (byte & 0x80);
241 bool checkreturn pb_skip_string(pb_istream_t *stream)
244 if (!pb_decode_varint32(stream, &length))
247 return pb_read(stream, NULL, length);
250 bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
254 *wire_type = (pb_wire_type_t) 0;
257 if (!pb_decode_varint32(stream, &temp))
259 if (stream->bytes_left == 0)
267 *eof = true; /* Special feature: allow 0-terminated messages. */
272 *wire_type = (pb_wire_type_t)(temp & 7);
276 bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
280 case PB_WT_VARINT: return pb_skip_varint(stream);
281 case PB_WT_64BIT: return pb_read(stream, NULL, 8);
282 case PB_WT_STRING: return pb_skip_string(stream);
283 case PB_WT_32BIT: return pb_read(stream, NULL, 4);
284 default: PB_RETURN_ERROR(stream, "invalid wire_type");
288 /* Read a raw value to buffer, for the purpose of passing it to callback as
289 * a substream. Size is maximum size on call, and actual size on return.
291 static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, pb_byte_t *buf, size_t *size)
293 size_t max_size = *size;
301 if (*size > max_size) return false;
302 if (!pb_read(stream, buf, 1)) return false;
303 } while (*buf++ & 0x80);
308 return pb_read(stream, buf, 8);
312 return pb_read(stream, buf, 4);
314 default: PB_RETURN_ERROR(stream, "invalid wire_type");
318 /* Decode string length from stream and return a substream with limited length.
319 * Remember to close the substream using pb_close_string_substream().
321 bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
324 if (!pb_decode_varint32(stream, &size))
327 *substream = *stream;
328 if (substream->bytes_left < size)
329 PB_RETURN_ERROR(stream, "parent stream too short");
331 substream->bytes_left = size;
332 stream->bytes_left -= size;
336 bool checkreturn pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
338 if (substream->bytes_left) {
339 if (!pb_read(substream, NULL, substream->bytes_left))
343 stream->state = substream->state;
346 stream->errmsg = substream->errmsg;
351 /*************************
352 * Decode a single field *
353 *************************/
355 static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
360 type = iter->pos->type;
361 func = PB_DECODERS[PB_LTYPE(type)];
363 switch (PB_HTYPE(type))
365 case PB_HTYPE_REQUIRED:
366 return func(stream, iter->pos, iter->pData);
368 case PB_HTYPE_OPTIONAL:
369 if (iter->pSize != iter->pData)
370 *(bool*)iter->pSize = true;
371 return func(stream, iter->pos, iter->pData);
373 case PB_HTYPE_REPEATED:
374 if (wire_type == PB_WT_STRING
375 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
379 pb_size_t *size = (pb_size_t*)iter->pSize;
380 pb_istream_t substream;
381 if (!pb_make_string_substream(stream, &substream))
384 while (substream.bytes_left > 0 && *size < iter->pos->array_size)
386 void *pItem = (char*)iter->pData + iter->pos->data_size * (*size);
387 if (!func(&substream, iter->pos, pItem))
395 if (substream.bytes_left != 0)
396 PB_RETURN_ERROR(stream, "array overflow");
397 if (!pb_close_string_substream(stream, &substream))
405 pb_size_t *size = (pb_size_t*)iter->pSize;
406 void *pItem = (char*)iter->pData + iter->pos->data_size * (*size);
407 if (*size >= iter->pos->array_size)
408 PB_RETURN_ERROR(stream, "array overflow");
411 return func(stream, iter->pos, pItem);
415 *(pb_size_t*)iter->pSize = iter->pos->tag;
416 if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
418 /* We memset to zero so that any callbacks are set to NULL.
419 * Then set any default values. */
420 memset(iter->pData, 0, iter->pos->data_size);
421 pb_message_set_to_defaults((const pb_field_t*)iter->pos->ptr, iter->pData);
423 return func(stream, iter->pos, iter->pData);
426 PB_RETURN_ERROR(stream, "invalid field type");
430 #ifdef PB_ENABLE_MALLOC
431 /* Allocate storage for the field and store the pointer at iter->pData.
432 * array_size is the number of entries to reserve in an array.
433 * Zero size is not allowed, use pb_free() for releasing.
435 static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
437 void *ptr = *(void**)pData;
439 if (data_size == 0 || array_size == 0)
440 PB_RETURN_ERROR(stream, "invalid size");
442 /* Check for multiplication overflows.
443 * This code avoids the costly division if the sizes are small enough.
444 * Multiplication is safe as long as only half of bits are set
445 * in either multiplicand.
448 const size_t check_limit = (size_t)1 << (sizeof(size_t) * 4);
449 if (data_size >= check_limit || array_size >= check_limit)
451 const size_t size_max = (size_t)-1;
452 if (size_max / array_size < data_size)
454 PB_RETURN_ERROR(stream, "size too large");
459 /* Allocate new or expand previous allocation */
460 /* Note: on failure the old pointer will remain in the structure,
461 * the message must be freed by caller also on error return. */
462 ptr = pb_realloc(ptr, array_size * data_size);
464 PB_RETURN_ERROR(stream, "realloc failed");
466 *(void**)pData = ptr;
470 /* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
471 static void initialize_pointer_field(void *pItem, pb_field_iter_t *iter)
473 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_STRING ||
474 PB_LTYPE(iter->pos->type) == PB_LTYPE_BYTES)
476 *(void**)pItem = NULL;
478 else if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
480 pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, pItem);
485 static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
487 #ifndef PB_ENABLE_MALLOC
488 PB_UNUSED(wire_type);
490 PB_RETURN_ERROR(stream, "no malloc support");
495 type = iter->pos->type;
496 func = PB_DECODERS[PB_LTYPE(type)];
498 switch (PB_HTYPE(type))
500 case PB_HTYPE_REQUIRED:
501 case PB_HTYPE_OPTIONAL:
503 if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE &&
504 *(void**)iter->pData != NULL)
506 /* Duplicate field, have to release the old allocation first. */
507 pb_release_single_field(iter);
510 if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
512 *(pb_size_t*)iter->pSize = iter->pos->tag;
515 if (PB_LTYPE(type) == PB_LTYPE_STRING ||
516 PB_LTYPE(type) == PB_LTYPE_BYTES)
518 return func(stream, iter->pos, iter->pData);
522 if (!allocate_field(stream, iter->pData, iter->pos->data_size, 1))
525 initialize_pointer_field(*(void**)iter->pData, iter);
526 return func(stream, iter->pos, *(void**)iter->pData);
529 case PB_HTYPE_REPEATED:
530 if (wire_type == PB_WT_STRING
531 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
533 /* Packed array, multiple items come in at once. */
535 pb_size_t *size = (pb_size_t*)iter->pSize;
536 size_t allocated_size = *size;
538 pb_istream_t substream;
540 if (!pb_make_string_substream(stream, &substream))
543 while (substream.bytes_left)
545 if ((size_t)*size + 1 > allocated_size)
547 /* Allocate more storage. This tries to guess the
548 * number of remaining entries. Round the division
550 allocated_size += (substream.bytes_left - 1) / iter->pos->data_size + 1;
552 if (!allocate_field(&substream, iter->pData, iter->pos->data_size, allocated_size))
559 /* Decode the array entry */
560 pItem = *(char**)iter->pData + iter->pos->data_size * (*size);
561 initialize_pointer_field(pItem, iter);
562 if (!func(&substream, iter->pos, pItem))
568 if (*size == PB_SIZE_MAX)
571 stream->errmsg = "too many array entries";
579 if (!pb_close_string_substream(stream, &substream))
586 /* Normal repeated field, i.e. only one item at a time. */
587 pb_size_t *size = (pb_size_t*)iter->pSize;
590 if (*size == PB_SIZE_MAX)
591 PB_RETURN_ERROR(stream, "too many array entries");
594 if (!allocate_field(stream, iter->pData, iter->pos->data_size, *size))
597 pItem = *(char**)iter->pData + iter->pos->data_size * (*size - 1);
598 initialize_pointer_field(pItem, iter);
599 return func(stream, iter->pos, pItem);
603 PB_RETURN_ERROR(stream, "invalid field type");
608 static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
610 pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
612 #ifdef PB_OLD_CALLBACK_STYLE
613 void *arg = pCallback->arg;
615 void **arg = &(pCallback->arg);
618 if (pCallback->funcs.decode == NULL)
619 return pb_skip_field(stream, wire_type);
621 if (wire_type == PB_WT_STRING)
623 pb_istream_t substream;
625 if (!pb_make_string_substream(stream, &substream))
630 if (!pCallback->funcs.decode(&substream, iter->pos, arg))
631 PB_RETURN_ERROR(stream, "callback failed");
632 } while (substream.bytes_left);
634 if (!pb_close_string_substream(stream, &substream))
641 /* Copy the single scalar value to stack.
642 * This is required so that we can limit the stream length,
643 * which in turn allows to use same callback for packed and
644 * not-packed fields. */
645 pb_istream_t substream;
646 pb_byte_t buffer[10];
647 size_t size = sizeof(buffer);
649 if (!read_raw_value(stream, wire_type, buffer, &size))
651 substream = pb_istream_from_buffer(buffer, size);
653 return pCallback->funcs.decode(&substream, iter->pos, arg);
657 static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
659 #ifdef PB_ENABLE_MALLOC
660 /* When decoding an oneof field, check if there is old data that must be
662 if (PB_HTYPE(iter->pos->type) == PB_HTYPE_ONEOF)
664 if (!pb_release_union_field(stream, iter))
669 switch (PB_ATYPE(iter->pos->type))
671 case PB_ATYPE_STATIC:
672 return decode_static_field(stream, wire_type, iter);
674 case PB_ATYPE_POINTER:
675 return decode_pointer_field(stream, wire_type, iter);
677 case PB_ATYPE_CALLBACK:
678 return decode_callback_field(stream, wire_type, iter);
681 PB_RETURN_ERROR(stream, "invalid field type");
685 static void iter_from_extension(pb_field_iter_t *iter, pb_extension_t *extension)
687 /* Fake a field iterator for the extension field.
688 * It is not actually safe to advance this iterator, but decode_field
689 * will not even try to. */
690 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
691 (void)pb_field_iter_begin(iter, field, extension->dest);
692 iter->pData = extension->dest;
693 iter->pSize = &extension->found;
695 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
697 /* For pointer extensions, the pointer is stored directly
698 * in the extension structure. This avoids having an extra
700 iter->pData = &extension->dest;
704 /* Default handler for extension fields. Expects a pb_field_t structure
705 * in extension->type->arg. */
706 static bool checkreturn default_extension_decoder(pb_istream_t *stream,
707 pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
709 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
710 pb_field_iter_t iter;
712 if (field->tag != tag)
715 iter_from_extension(&iter, extension);
716 extension->found = true;
717 return decode_field(stream, wire_type, &iter);
720 /* Try to decode an unknown field as an extension field. Tries each extension
721 * decoder in turn, until one of them handles the field or loop ends. */
722 static bool checkreturn decode_extension(pb_istream_t *stream,
723 uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter)
725 pb_extension_t *extension = *(pb_extension_t* const *)iter->pData;
726 size_t pos = stream->bytes_left;
728 while (extension != NULL && pos == stream->bytes_left)
731 if (extension->type->decode)
732 status = extension->type->decode(stream, extension, tag, wire_type);
734 status = default_extension_decoder(stream, extension, tag, wire_type);
739 extension = extension->next;
745 /* Step through the iterator until an extension field is found or until all
746 * entries have been checked. There can be only one extension field per
747 * message. Returns false if no extension field is found. */
748 static bool checkreturn find_extension_field(pb_field_iter_t *iter)
750 const pb_field_t *start = iter->pos;
753 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_EXTENSION)
755 (void)pb_field_iter_next(iter);
756 } while (iter->pos != start);
761 /* Initialize message fields to default values, recursively */
762 static void pb_field_set_to_default(pb_field_iter_t *iter)
765 type = iter->pos->type;
767 if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
769 pb_extension_t *ext = *(pb_extension_t* const *)iter->pData;
772 pb_field_iter_t ext_iter;
774 iter_from_extension(&ext_iter, ext);
775 pb_field_set_to_default(&ext_iter);
779 else if (PB_ATYPE(type) == PB_ATYPE_STATIC)
781 bool init_data = true;
782 if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL)
784 /* Set has_field to false. Still initialize the optional field
786 *(bool*)iter->pSize = false;
788 else if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
789 PB_HTYPE(type) == PB_HTYPE_ONEOF)
791 /* REPEATED: Set array count to 0, no need to initialize contents.
792 ONEOF: Set which_field to 0. */
793 *(pb_size_t*)iter->pSize = 0;
799 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
801 /* Initialize submessage to defaults */
802 pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, iter->pData);
804 else if (iter->pos->ptr != NULL)
806 /* Initialize to default value */
807 memcpy(iter->pData, iter->pos->ptr, iter->pos->data_size);
811 /* Initialize to zeros */
812 memset(iter->pData, 0, iter->pos->data_size);
816 else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
818 /* Initialize the pointer to NULL. */
819 *(void**)iter->pData = NULL;
821 /* Initialize array count to 0. */
822 if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
823 PB_HTYPE(type) == PB_HTYPE_ONEOF)
825 *(pb_size_t*)iter->pSize = 0;
828 else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
830 /* Don't overwrite callback */
834 static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
836 pb_field_iter_t iter;
838 if (!pb_field_iter_begin(&iter, fields, dest_struct))
839 return; /* Empty message type */
843 pb_field_set_to_default(&iter);
844 } while (pb_field_iter_next(&iter));
847 /*********************
848 * Decode all fields *
849 *********************/
851 bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
853 uint32_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 31) / 32] = {0, 0};
854 const uint32_t allbits = ~(uint32_t)0;
855 uint32_t extension_range_start = 0;
856 pb_field_iter_t iter;
858 /* Return value ignored, as empty message types will be correctly handled by
859 * pb_field_iter_find() anyway. */
860 (void)pb_field_iter_begin(&iter, fields, dest_struct);
862 while (stream->bytes_left)
865 pb_wire_type_t wire_type;
868 if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
876 if (!pb_field_iter_find(&iter, tag))
878 /* No match found, check if it matches an extension. */
879 if (tag >= extension_range_start)
881 if (!find_extension_field(&iter))
882 extension_range_start = (uint32_t)-1;
884 extension_range_start = iter.pos->tag;
886 if (tag >= extension_range_start)
888 size_t pos = stream->bytes_left;
890 if (!decode_extension(stream, tag, wire_type, &iter))
893 if (pos != stream->bytes_left)
895 /* The field was handled */
901 /* No match found, skip data */
902 if (!pb_skip_field(stream, wire_type))
907 if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REQUIRED
908 && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
910 uint32_t tmp = ((uint32_t)1 << (iter.required_field_index & 31));
911 fields_seen[iter.required_field_index >> 5] |= tmp;
914 if (!decode_field(stream, wire_type, &iter))
918 /* Check that all required fields were present. */
920 /* First figure out the number of required fields by
921 * seeking to the end of the field array. Usually we
922 * are already close to end after decoding.
924 unsigned req_field_count;
928 req_field_count = iter.required_field_index;
929 last_type = iter.pos->type;
930 } while (pb_field_iter_next(&iter));
932 /* Fixup if last field was also required. */
933 if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.pos->tag != 0)
936 if (req_field_count > 0)
938 /* Check the whole words */
939 for (i = 0; i < (req_field_count >> 5); i++)
941 if (fields_seen[i] != allbits)
942 PB_RETURN_ERROR(stream, "missing required field");
945 /* Check the remaining bits */
946 if (fields_seen[req_field_count >> 5] != (allbits >> (32 - (req_field_count & 31))))
947 PB_RETURN_ERROR(stream, "missing required field");
954 bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
957 pb_message_set_to_defaults(fields, dest_struct);
958 status = pb_decode_noinit(stream, fields, dest_struct);
960 #ifdef PB_ENABLE_MALLOC
962 pb_release(fields, dest_struct);
968 bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
970 pb_istream_t substream;
973 if (!pb_make_string_substream(stream, &substream))
976 status = pb_decode(&substream, fields, dest_struct);
978 if (!pb_close_string_substream(stream, &substream))
983 #ifdef PB_ENABLE_MALLOC
984 /* Given an oneof field, if there has already been a field inside this oneof,
985 * release it before overwriting with a different one. */
986 static bool pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *iter)
988 pb_size_t old_tag = *(pb_size_t*)iter->pSize; /* Previous which_ value */
989 pb_size_t new_tag = iter->pos->tag; /* New which_ value */
992 return true; /* Ok, no old data in union */
994 if (old_tag == new_tag)
995 return true; /* Ok, old data is of same type => merge */
997 /* Release old data. The find can fail if the message struct contains
999 if (!pb_field_iter_find(iter, old_tag))
1000 PB_RETURN_ERROR(stream, "invalid union tag");
1002 pb_release_single_field(iter);
1004 /* Restore iterator to where it should be.
1005 * This shouldn't fail unless the pb_field_t structure is corrupted. */
1006 if (!pb_field_iter_find(iter, new_tag))
1007 PB_RETURN_ERROR(stream, "iterator error");
1012 static void pb_release_single_field(const pb_field_iter_t *iter)
1015 type = iter->pos->type;
1017 if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
1019 if (*(pb_size_t*)iter->pSize != iter->pos->tag)
1020 return; /* This is not the current field in the union */
1023 /* Release anything contained inside an extension or submsg.
1024 * This has to be done even if the submsg itself is statically
1026 if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
1028 /* Release fields from all extensions in the linked list */
1029 pb_extension_t *ext = *(pb_extension_t**)iter->pData;
1032 pb_field_iter_t ext_iter;
1033 iter_from_extension(&ext_iter, ext);
1034 pb_release_single_field(&ext_iter);
1038 else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
1040 /* Release fields in submessage or submsg array */
1041 void *pItem = iter->pData;
1042 pb_size_t count = 1;
1044 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
1046 pItem = *(void**)iter->pData;
1049 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
1051 count = *(pb_size_t*)iter->pSize;
1053 if (PB_ATYPE(type) == PB_ATYPE_STATIC && count > iter->pos->array_size)
1055 /* Protect against corrupted _count fields */
1056 count = iter->pos->array_size;
1064 pb_release((const pb_field_t*)iter->pos->ptr, pItem);
1065 pItem = (char*)pItem + iter->pos->data_size;
1070 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
1072 if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
1073 (PB_LTYPE(type) == PB_LTYPE_STRING ||
1074 PB_LTYPE(type) == PB_LTYPE_BYTES))
1076 /* Release entries in repeated string or bytes array */
1077 void **pItem = *(void***)iter->pData;
1078 pb_size_t count = *(pb_size_t*)iter->pSize;
1086 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
1088 /* We are going to release the array, so set the size to 0 */
1089 *(pb_size_t*)iter->pSize = 0;
1092 /* Release main item */
1093 pb_free(*(void**)iter->pData);
1094 *(void**)iter->pData = NULL;
1098 void pb_release(const pb_field_t fields[], void *dest_struct)
1100 pb_field_iter_t iter;
1103 return; /* Ignore NULL pointers, similar to free() */
1105 if (!pb_field_iter_begin(&iter, fields, dest_struct))
1106 return; /* Empty message type */
1110 pb_release_single_field(&iter);
1111 } while (pb_field_iter_next(&iter));
1115 /* Field decoders */
1117 bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
1120 if (!pb_decode_varint(stream, &value))
1124 *dest = (int64_t)(~(value >> 1));
1126 *dest = (int64_t)(value >> 1);
1131 bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
1135 if (!pb_read(stream, bytes, 4))
1138 *(uint32_t*)dest = ((uint32_t)bytes[0] << 0) |
1139 ((uint32_t)bytes[1] << 8) |
1140 ((uint32_t)bytes[2] << 16) |
1141 ((uint32_t)bytes[3] << 24);
1145 bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
1149 if (!pb_read(stream, bytes, 8))
1152 *(uint64_t*)dest = ((uint64_t)bytes[0] << 0) |
1153 ((uint64_t)bytes[1] << 8) |
1154 ((uint64_t)bytes[2] << 16) |
1155 ((uint64_t)bytes[3] << 24) |
1156 ((uint64_t)bytes[4] << 32) |
1157 ((uint64_t)bytes[5] << 40) |
1158 ((uint64_t)bytes[6] << 48) |
1159 ((uint64_t)bytes[7] << 56);
1164 static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1169 if (!pb_decode_varint(stream, &value))
1172 /* See issue 97: Google's C++ protobuf allows negative varint values to
1173 * be cast as int32_t, instead of the int64_t that should be used when
1174 * encoding. Previous nanopb versions had a bug in encoding. In order to
1175 * not break decoding of such messages, we cast <=32 bit fields to
1176 * int32_t first to get the sign correct.
1178 if (field->data_size == sizeof(int64_t))
1179 svalue = (int64_t)value;
1181 svalue = (int32_t)value;
1183 /* Cast to the proper field size, while checking for overflows */
1184 if (field->data_size == sizeof(int64_t))
1185 clamped = *(int64_t*)dest = svalue;
1186 else if (field->data_size == sizeof(int32_t))
1187 clamped = *(int32_t*)dest = (int32_t)svalue;
1188 else if (field->data_size == sizeof(int_least16_t))
1189 clamped = *(int_least16_t*)dest = (int_least16_t)svalue;
1190 else if (field->data_size == sizeof(int_least8_t))
1191 clamped = *(int_least8_t*)dest = (int_least8_t)svalue;
1193 PB_RETURN_ERROR(stream, "invalid data_size");
1195 if (clamped != svalue)
1196 PB_RETURN_ERROR(stream, "integer too large");
1201 static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1203 uint64_t value, clamped;
1204 if (!pb_decode_varint(stream, &value))
1207 /* Cast to the proper field size, while checking for overflows */
1208 if (field->data_size == sizeof(uint64_t))
1209 clamped = *(uint64_t*)dest = value;
1210 else if (field->data_size == sizeof(uint32_t))
1211 clamped = *(uint32_t*)dest = (uint32_t)value;
1212 else if (field->data_size == sizeof(uint_least16_t))
1213 clamped = *(uint_least16_t*)dest = (uint_least16_t)value;
1214 else if (field->data_size == sizeof(uint_least8_t))
1215 clamped = *(uint_least8_t*)dest = (uint_least8_t)value;
1217 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_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1227 int64_t value, clamped;
1228 if (!pb_decode_svarint(stream, &value))
1231 /* Cast to the proper field size, while checking for overflows */
1232 if (field->data_size == sizeof(int64_t))
1233 clamped = *(int64_t*)dest = value;
1234 else if (field->data_size == sizeof(int32_t))
1235 clamped = *(int32_t*)dest = (int32_t)value;
1236 else if (field->data_size == sizeof(int_least16_t))
1237 clamped = *(int_least16_t*)dest = (int_least16_t)value;
1238 else if (field->data_size == sizeof(int_least8_t))
1239 clamped = *(int_least8_t*)dest = (int_least8_t)value;
1241 PB_RETURN_ERROR(stream, "invalid data_size");
1243 if (clamped != value)
1244 PB_RETURN_ERROR(stream, "integer too large");
1249 static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
1252 return pb_decode_fixed32(stream, dest);
1255 static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
1258 return pb_decode_fixed64(stream, dest);
1261 static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
1265 pb_bytes_array_t *bdest;
1267 if (!pb_decode_varint32(stream, &size))
1270 if (size > PB_SIZE_MAX)
1271 PB_RETURN_ERROR(stream, "bytes overflow");
1273 alloc_size = PB_BYTES_ARRAY_T_ALLOCSIZE(size);
1274 if (size > alloc_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 bdest = *(pb_bytes_array_t**)dest;
1289 if (PB_LTYPE(field->type) == PB_LTYPE_FIXED_LENGTH_BYTES) {
1290 if (size != field->data_size)
1291 PB_RETURN_ERROR(stream, "incorrect inline bytes size");
1292 return pb_read(stream, (pb_byte_t*)dest, field->data_size);
1295 if (alloc_size > field->data_size)
1296 PB_RETURN_ERROR(stream, "bytes overflow");
1297 bdest = (pb_bytes_array_t*)dest;
1300 bdest->size = (pb_size_t)size;
1301 return pb_read(stream, bdest->bytes, size);
1304 static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
1309 if (!pb_decode_varint32(stream, &size))
1312 /* Space for null terminator */
1313 alloc_size = size + 1;
1315 if (alloc_size < size)
1316 PB_RETURN_ERROR(stream, "size too large");
1318 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1320 #ifndef PB_ENABLE_MALLOC
1321 PB_RETURN_ERROR(stream, "no malloc support");
1323 if (!allocate_field(stream, dest, alloc_size, 1))
1325 dest = *(void**)dest;
1330 if (alloc_size > field->data_size)
1331 PB_RETURN_ERROR(stream, "string overflow");
1334 status = pb_read(stream, (pb_byte_t*)dest, size);
1335 *((pb_byte_t*)dest + size) = 0;
1339 static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
1342 pb_istream_t substream;
1343 const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr;
1345 if (!pb_make_string_substream(stream, &substream))
1348 if (field->ptr == NULL)
1349 PB_RETURN_ERROR(stream, "invalid field descriptor");
1351 /* New array entries need to be initialized, while required and optional
1352 * submessages have already been initialized in the top-level pb_decode. */
1353 if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
1354 status = pb_decode(&substream, submsg_fields, dest);
1356 status = pb_decode_noinit(&substream, submsg_fields, dest);
1358 if (!pb_close_string_substream(stream, &substream))