1 /* pb_decode.c -- decode a protobuf using minimal resources
3 * 2011 Petteri Aimonen <jpa@kapsi.fi>
6 /* Use the GCC warn_unused_result attribute to check that all return values
7 * are propagated correctly. On other compilers and gcc before 3.4.0 just
8 * ignore the annotation.
10 #if !defined(__GNUC__) || ( __GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
13 #define checkreturn __attribute__((warn_unused_result))
17 #include "pb_decode.h"
18 #include "pb_common.h"
20 /**************************************
21 * Declarations internal to this file *
22 **************************************/
24 typedef bool (*pb_decoder_t)(pb_istream_t *stream, const pb_field_t *field, void *dest) checkreturn;
26 static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count);
27 static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest);
28 static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, uint8_t *buf, size_t *size);
29 static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter);
30 static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter);
31 static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter);
32 static void iter_from_extension(pb_field_iter_t *iter, pb_extension_t *extension);
33 static bool checkreturn default_extension_decoder(pb_istream_t *stream, pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type);
34 static bool checkreturn decode_extension(pb_istream_t *stream, uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter);
35 static bool checkreturn find_extension_field(pb_field_iter_t *iter);
36 static void pb_field_set_to_default(pb_field_iter_t *iter);
37 static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct);
38 static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest);
39 static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
40 static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
41 static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest);
42 static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest);
43 static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest);
44 static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest);
45 static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest);
46 static bool checkreturn pb_skip_varint(pb_istream_t *stream);
47 static bool checkreturn pb_skip_string(pb_istream_t *stream);
49 #ifdef PB_ENABLE_MALLOC
50 static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size);
51 static bool checkreturn pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *iter);
52 static void pb_release_single_field(const pb_field_iter_t *iter);
55 /* --- Function pointers to field decoders ---
56 * Order in the array must match pb_action_t LTYPE numbering.
58 static const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = {
71 /*******************************
72 * pb_istream_t implementation *
73 *******************************/
75 static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count)
77 uint8_t *source = (uint8_t*)stream->state;
78 stream->state = source + count;
89 bool checkreturn pb_read(pb_istream_t *stream, uint8_t *buf, size_t count)
91 #ifndef PB_BUFFER_ONLY
92 if (buf == NULL && stream->callback != buf_read)
94 /* Skip input bytes */
98 if (!pb_read(stream, tmp, 16))
104 return pb_read(stream, tmp, count);
108 if (stream->bytes_left < count)
109 PB_RETURN_ERROR(stream, "end-of-stream");
111 #ifndef PB_BUFFER_ONLY
112 if (!stream->callback(stream, buf, count))
113 PB_RETURN_ERROR(stream, "io error");
115 if (!buf_read(stream, buf, count))
119 stream->bytes_left -= count;
123 /* Read a single byte from input stream. buf may not be NULL.
124 * This is an optimization for the varint decoding. */
125 static bool checkreturn pb_readbyte(pb_istream_t *stream, uint8_t *buf)
127 if (stream->bytes_left == 0)
128 PB_RETURN_ERROR(stream, "end-of-stream");
130 #ifndef PB_BUFFER_ONLY
131 if (!stream->callback(stream, buf, 1))
132 PB_RETURN_ERROR(stream, "io error");
134 *buf = *(uint8_t*)stream->state;
135 stream->state = (uint8_t*)stream->state + 1;
138 stream->bytes_left--;
143 pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize)
146 #ifdef PB_BUFFER_ONLY
147 stream.callback = NULL;
149 stream.callback = &buf_read;
152 stream.bytes_left = bufsize;
154 stream.errmsg = NULL;
159 /********************
161 ********************/
163 static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
168 if (!pb_readbyte(stream, &byte))
171 if ((byte & 0x80) == 0)
173 /* Quick case, 1 byte value */
180 result = byte & 0x7F;
185 PB_RETURN_ERROR(stream, "varint overflow");
187 if (!pb_readbyte(stream, &byte))
190 result |= (uint32_t)(byte & 0x7F) << bitpos;
191 bitpos = (uint8_t)(bitpos + 7);
192 } while (byte & 0x80);
199 bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
208 PB_RETURN_ERROR(stream, "varint overflow");
210 if (!pb_readbyte(stream, &byte))
213 result |= (uint64_t)(byte & 0x7F) << bitpos;
214 bitpos = (uint8_t)(bitpos + 7);
215 } while (byte & 0x80);
221 bool checkreturn pb_skip_varint(pb_istream_t *stream)
226 if (!pb_read(stream, &byte, 1))
228 } while (byte & 0x80);
232 bool checkreturn pb_skip_string(pb_istream_t *stream)
235 if (!pb_decode_varint32(stream, &length))
238 return pb_read(stream, NULL, length);
241 bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
245 *wire_type = (pb_wire_type_t) 0;
248 if (!pb_decode_varint32(stream, &temp))
250 if (stream->bytes_left == 0)
258 *eof = true; /* Special feature: allow 0-terminated messages. */
263 *wire_type = (pb_wire_type_t)(temp & 7);
267 bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
271 case PB_WT_VARINT: return pb_skip_varint(stream);
272 case PB_WT_64BIT: return pb_read(stream, NULL, 8);
273 case PB_WT_STRING: return pb_skip_string(stream);
274 case PB_WT_32BIT: return pb_read(stream, NULL, 4);
275 default: PB_RETURN_ERROR(stream, "invalid wire_type");
279 /* Read a raw value to buffer, for the purpose of passing it to callback as
280 * a substream. Size is maximum size on call, and actual size on return.
282 static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, uint8_t *buf, size_t *size)
284 size_t max_size = *size;
292 if (*size > max_size) return false;
293 if (!pb_read(stream, buf, 1)) return false;
294 } while (*buf++ & 0x80);
299 return pb_read(stream, buf, 8);
303 return pb_read(stream, buf, 4);
305 default: PB_RETURN_ERROR(stream, "invalid wire_type");
309 /* Decode string length from stream and return a substream with limited length.
310 * Remember to close the substream using pb_close_string_substream().
312 bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
315 if (!pb_decode_varint32(stream, &size))
318 *substream = *stream;
319 if (substream->bytes_left < size)
320 PB_RETURN_ERROR(stream, "parent stream too short");
322 substream->bytes_left = size;
323 stream->bytes_left -= size;
327 void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
329 stream->state = substream->state;
332 stream->errmsg = substream->errmsg;
336 /*************************
337 * Decode a single field *
338 *************************/
340 static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
345 type = iter->pos->type;
346 func = PB_DECODERS[PB_LTYPE(type)];
348 switch (PB_HTYPE(type))
350 case PB_HTYPE_REQUIRED:
351 return func(stream, iter->pos, iter->pData);
353 case PB_HTYPE_OPTIONAL:
354 *(bool*)iter->pSize = true;
355 return func(stream, iter->pos, iter->pData);
357 case PB_HTYPE_REPEATED:
358 if (wire_type == PB_WT_STRING
359 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
363 pb_size_t *size = (pb_size_t*)iter->pSize;
364 pb_istream_t substream;
365 if (!pb_make_string_substream(stream, &substream))
368 while (substream.bytes_left > 0 && *size < iter->pos->array_size)
370 void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
371 if (!func(&substream, iter->pos, pItem))
378 pb_close_string_substream(stream, &substream);
380 if (substream.bytes_left != 0)
381 PB_RETURN_ERROR(stream, "array overflow");
388 pb_size_t *size = (pb_size_t*)iter->pSize;
389 void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
390 if (*size >= iter->pos->array_size)
391 PB_RETURN_ERROR(stream, "array overflow");
394 return func(stream, iter->pos, pItem);
398 *(pb_size_t*)iter->pSize = iter->pos->tag;
399 if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
401 pb_message_set_to_defaults((const pb_field_t*)iter->pos->ptr, iter->pData);
403 return func(stream, iter->pos, iter->pData);
406 PB_RETURN_ERROR(stream, "invalid field type");
410 #ifdef PB_ENABLE_MALLOC
411 /* Allocate storage for the field and store the pointer at iter->pData.
412 * array_size is the number of entries to reserve in an array.
413 * Zero size is not allowed, use pb_free() for releasing.
415 static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
417 void *ptr = *(void**)pData;
419 if (data_size == 0 || array_size == 0)
420 PB_RETURN_ERROR(stream, "invalid size");
422 /* Check for multiplication overflows.
423 * This code avoids the costly division if the sizes are small enough.
424 * Multiplication is safe as long as only half of bits are set
425 * in either multiplicand.
428 const size_t check_limit = (size_t)1 << (sizeof(size_t) * 4);
429 if (data_size >= check_limit || array_size >= check_limit)
431 const size_t size_max = (size_t)-1;
432 if (size_max / array_size < data_size)
434 PB_RETURN_ERROR(stream, "size too large");
439 /* Allocate new or expand previous allocation */
440 /* Note: on failure the old pointer will remain in the structure,
441 * the message must be freed by caller also on error return. */
442 ptr = pb_realloc(ptr, array_size * data_size);
444 PB_RETURN_ERROR(stream, "realloc failed");
446 *(void**)pData = ptr;
450 /* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
451 static void initialize_pointer_field(void *pItem, pb_field_iter_t *iter)
453 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_STRING ||
454 PB_LTYPE(iter->pos->type) == PB_LTYPE_BYTES)
456 *(void**)pItem = NULL;
458 else if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
460 pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, pItem);
465 static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
467 #ifndef PB_ENABLE_MALLOC
468 PB_UNUSED(wire_type);
470 PB_RETURN_ERROR(stream, "no malloc support");
475 type = iter->pos->type;
476 func = PB_DECODERS[PB_LTYPE(type)];
478 switch (PB_HTYPE(type))
480 case PB_HTYPE_REQUIRED:
481 case PB_HTYPE_OPTIONAL:
483 if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE &&
484 *(void**)iter->pData != NULL)
486 /* Duplicate field, have to release the old allocation first. */
487 pb_release_single_field(iter);
490 if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
492 *(pb_size_t*)iter->pSize = iter->pos->tag;
495 if (PB_LTYPE(type) == PB_LTYPE_STRING ||
496 PB_LTYPE(type) == PB_LTYPE_BYTES)
498 return func(stream, iter->pos, iter->pData);
502 if (!allocate_field(stream, iter->pData, iter->pos->data_size, 1))
505 initialize_pointer_field(*(void**)iter->pData, iter);
506 return func(stream, iter->pos, *(void**)iter->pData);
509 case PB_HTYPE_REPEATED:
510 if (wire_type == PB_WT_STRING
511 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
513 /* Packed array, multiple items come in at once. */
515 pb_size_t *size = (pb_size_t*)iter->pSize;
516 size_t allocated_size = *size;
518 pb_istream_t substream;
520 if (!pb_make_string_substream(stream, &substream))
523 while (substream.bytes_left)
525 if ((size_t)*size + 1 > allocated_size)
527 /* Allocate more storage. This tries to guess the
528 * number of remaining entries. Round the division
530 allocated_size += (substream.bytes_left - 1) / iter->pos->data_size + 1;
532 if (!allocate_field(&substream, iter->pData, iter->pos->data_size, allocated_size))
539 /* Decode the array entry */
540 pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size);
541 initialize_pointer_field(pItem, iter);
542 if (!func(&substream, iter->pos, pItem))
548 if (*size == PB_SIZE_MAX)
551 stream->errmsg = "too many array entries";
559 pb_close_string_substream(stream, &substream);
565 /* Normal repeated field, i.e. only one item at a time. */
566 pb_size_t *size = (pb_size_t*)iter->pSize;
569 if (*size == PB_SIZE_MAX)
570 PB_RETURN_ERROR(stream, "too many array entries");
573 if (!allocate_field(stream, iter->pData, iter->pos->data_size, *size))
576 pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size - 1);
577 initialize_pointer_field(pItem, iter);
578 return func(stream, iter->pos, pItem);
582 PB_RETURN_ERROR(stream, "invalid field type");
587 static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
589 pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
591 #ifdef PB_OLD_CALLBACK_STYLE
592 void *arg = pCallback->arg;
594 void **arg = &(pCallback->arg);
597 if (pCallback->funcs.decode == NULL)
598 return pb_skip_field(stream, wire_type);
600 if (wire_type == PB_WT_STRING)
602 pb_istream_t substream;
604 if (!pb_make_string_substream(stream, &substream))
609 if (!pCallback->funcs.decode(&substream, iter->pos, arg))
610 PB_RETURN_ERROR(stream, "callback failed");
611 } while (substream.bytes_left);
613 pb_close_string_substream(stream, &substream);
618 /* Copy the single scalar value to stack.
619 * This is required so that we can limit the stream length,
620 * which in turn allows to use same callback for packed and
621 * not-packed fields. */
622 pb_istream_t substream;
624 size_t size = sizeof(buffer);
626 if (!read_raw_value(stream, wire_type, buffer, &size))
628 substream = pb_istream_from_buffer(buffer, size);
630 return pCallback->funcs.decode(&substream, iter->pos, arg);
634 static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
636 #ifdef PB_ENABLE_MALLOC
637 /* When decoding an oneof field, check if there is old data that must be
639 if (PB_HTYPE(iter->pos->type) == PB_HTYPE_ONEOF)
641 if (!pb_release_union_field(stream, iter))
646 switch (PB_ATYPE(iter->pos->type))
648 case PB_ATYPE_STATIC:
649 return decode_static_field(stream, wire_type, iter);
651 case PB_ATYPE_POINTER:
652 return decode_pointer_field(stream, wire_type, iter);
654 case PB_ATYPE_CALLBACK:
655 return decode_callback_field(stream, wire_type, iter);
658 PB_RETURN_ERROR(stream, "invalid field type");
662 static void iter_from_extension(pb_field_iter_t *iter, pb_extension_t *extension)
664 /* Fake a field iterator for the extension field.
665 * It is not actually safe to advance this iterator, but decode_field
666 * will not even try to. */
667 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
668 (void)pb_field_iter_begin(iter, field, extension->dest);
669 iter->pData = extension->dest;
670 iter->pSize = &extension->found;
672 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
674 /* For pointer extensions, the pointer is stored directly
675 * in the extension structure. This avoids having an extra
677 iter->pData = &extension->dest;
681 /* Default handler for extension fields. Expects a pb_field_t structure
682 * in extension->type->arg. */
683 static bool checkreturn default_extension_decoder(pb_istream_t *stream,
684 pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
686 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
687 pb_field_iter_t iter;
689 if (field->tag != tag)
692 iter_from_extension(&iter, extension);
693 extension->found = true;
694 return decode_field(stream, wire_type, &iter);
697 /* Try to decode an unknown field as an extension field. Tries each extension
698 * decoder in turn, until one of them handles the field or loop ends. */
699 static bool checkreturn decode_extension(pb_istream_t *stream,
700 uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter)
702 pb_extension_t *extension = *(pb_extension_t* const *)iter->pData;
703 size_t pos = stream->bytes_left;
705 while (extension != NULL && pos == stream->bytes_left)
708 if (extension->type->decode)
709 status = extension->type->decode(stream, extension, tag, wire_type);
711 status = default_extension_decoder(stream, extension, tag, wire_type);
716 extension = extension->next;
722 /* Step through the iterator until an extension field is found or until all
723 * entries have been checked. There can be only one extension field per
724 * message. Returns false if no extension field is found. */
725 static bool checkreturn find_extension_field(pb_field_iter_t *iter)
727 const pb_field_t *start = iter->pos;
730 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_EXTENSION)
732 (void)pb_field_iter_next(iter);
733 } while (iter->pos != start);
738 /* Initialize message fields to default values, recursively */
739 static void pb_field_set_to_default(pb_field_iter_t *iter)
742 type = iter->pos->type;
744 if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
746 pb_extension_t *ext = *(pb_extension_t* const *)iter->pData;
749 pb_field_iter_t ext_iter;
751 iter_from_extension(&ext_iter, ext);
752 pb_field_set_to_default(&ext_iter);
756 else if (PB_ATYPE(type) == PB_ATYPE_STATIC)
758 bool init_data = true;
759 if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL)
761 /* Set has_field to false. Still initialize the optional field
763 *(bool*)iter->pSize = false;
765 else if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
766 PB_HTYPE(type) == PB_HTYPE_ONEOF)
768 /* REPEATED: Set array count to 0, no need to initialize contents.
769 ONEOF: Set which_field to 0. */
770 *(pb_size_t*)iter->pSize = 0;
776 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
778 /* Initialize submessage to defaults */
779 pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, iter->pData);
781 else if (iter->pos->ptr != NULL)
783 /* Initialize to default value */
784 memcpy(iter->pData, iter->pos->ptr, iter->pos->data_size);
788 /* Initialize to zeros */
789 memset(iter->pData, 0, iter->pos->data_size);
793 else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
795 /* Initialize the pointer to NULL. */
796 *(void**)iter->pData = NULL;
798 /* Initialize array count to 0. */
799 if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
800 PB_HTYPE(type) == PB_HTYPE_ONEOF)
802 *(pb_size_t*)iter->pSize = 0;
805 else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
807 /* Don't overwrite callback */
811 static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
813 pb_field_iter_t iter;
815 if (!pb_field_iter_begin(&iter, fields, dest_struct))
816 return; /* Empty message type */
820 pb_field_set_to_default(&iter);
821 } while (pb_field_iter_next(&iter));
824 /*********************
825 * Decode all fields *
826 *********************/
828 bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
830 uint8_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 7) / 8] = {0, 0, 0, 0, 0, 0, 0, 0};
831 uint32_t extension_range_start = 0;
832 pb_field_iter_t iter;
834 /* Return value ignored, as empty message types will be correctly handled by
835 * pb_field_iter_find() anyway. */
836 (void)pb_field_iter_begin(&iter, fields, dest_struct);
838 while (stream->bytes_left)
841 pb_wire_type_t wire_type;
844 if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
852 if (!pb_field_iter_find(&iter, tag))
854 /* No match found, check if it matches an extension. */
855 if (tag >= extension_range_start)
857 if (!find_extension_field(&iter))
858 extension_range_start = (uint32_t)-1;
860 extension_range_start = iter.pos->tag;
862 if (tag >= extension_range_start)
864 size_t pos = stream->bytes_left;
866 if (!decode_extension(stream, tag, wire_type, &iter))
869 if (pos != stream->bytes_left)
871 /* The field was handled */
877 /* No match found, skip data */
878 if (!pb_skip_field(stream, wire_type))
883 if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REQUIRED
884 && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
886 fields_seen[iter.required_field_index >> 3] |= (uint8_t)(1 << (iter.required_field_index & 7));
889 if (!decode_field(stream, wire_type, &iter))
893 /* Check that all required fields were present. */
895 /* First figure out the number of required fields by
896 * seeking to the end of the field array. Usually we
897 * are already close to end after decoding.
899 unsigned req_field_count;
903 req_field_count = iter.required_field_index;
904 last_type = iter.pos->type;
905 } while (pb_field_iter_next(&iter));
907 /* Fixup if last field was also required. */
908 if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.pos->tag != 0)
911 /* Check the whole bytes */
912 for (i = 0; i < (req_field_count >> 3); i++)
914 if (fields_seen[i] != 0xFF)
915 PB_RETURN_ERROR(stream, "missing required field");
918 /* Check the remaining bits */
919 if (fields_seen[req_field_count >> 3] != (0xFF >> (8 - (req_field_count & 7))))
920 PB_RETURN_ERROR(stream, "missing required field");
926 bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
929 pb_message_set_to_defaults(fields, dest_struct);
930 status = pb_decode_noinit(stream, fields, dest_struct);
932 #ifdef PB_ENABLE_MALLOC
934 pb_release(fields, dest_struct);
940 bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
942 pb_istream_t substream;
945 if (!pb_make_string_substream(stream, &substream))
948 status = pb_decode(&substream, fields, dest_struct);
949 pb_close_string_substream(stream, &substream);
953 #ifdef PB_ENABLE_MALLOC
954 /* Given an oneof field, if there has already been a field inside this oneof,
955 * release it before overwriting with a different one. */
956 static bool pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *iter)
958 pb_size_t old_tag = *(pb_size_t*)iter->pSize; /* Previous which_ value */
959 pb_size_t new_tag = iter->pos->tag; /* New which_ value */
962 return true; /* Ok, no old data in union */
964 if (old_tag == new_tag)
965 return true; /* Ok, old data is of same type => merge */
967 /* Release old data. The find can fail if the message struct contains
969 if (!pb_field_iter_find(iter, old_tag))
970 PB_RETURN_ERROR(stream, "invalid union tag");
972 pb_release_single_field(iter);
974 /* Restore iterator to where it should be.
975 * This shouldn't fail unless the pb_field_t structure is corrupted. */
976 if (!pb_field_iter_find(iter, new_tag))
977 PB_RETURN_ERROR(stream, "iterator error");
982 static void pb_release_single_field(const pb_field_iter_t *iter)
985 type = iter->pos->type;
987 if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
989 if (*(pb_size_t*)iter->pSize != iter->pos->tag)
990 return; /* This is not the current field in the union */
993 /* Release anything contained inside an extension or submsg.
994 * This has to be done even if the submsg itself is statically
996 if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
998 /* Release fields from all extensions in the linked list */
999 pb_extension_t *ext = *(pb_extension_t**)iter->pData;
1002 pb_field_iter_t ext_iter;
1003 iter_from_extension(&ext_iter, ext);
1004 pb_release_single_field(&ext_iter);
1008 else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
1010 /* Release fields in submessage or submsg array */
1011 void *pItem = iter->pData;
1012 pb_size_t count = 1;
1014 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
1016 pItem = *(void**)iter->pData;
1019 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
1021 count = *(pb_size_t*)iter->pSize;
1028 pb_release((const pb_field_t*)iter->pos->ptr, pItem);
1029 pItem = (uint8_t*)pItem + iter->pos->data_size;
1034 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
1036 if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
1037 (PB_LTYPE(type) == PB_LTYPE_STRING ||
1038 PB_LTYPE(type) == PB_LTYPE_BYTES))
1040 /* Release entries in repeated string or bytes array */
1041 void **pItem = *(void***)iter->pData;
1042 pb_size_t count = *(pb_size_t*)iter->pSize;
1050 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
1052 /* We are going to release the array, so set the size to 0 */
1053 *(pb_size_t*)iter->pSize = 0;
1056 /* Release main item */
1057 pb_free(*(void**)iter->pData);
1058 *(void**)iter->pData = NULL;
1062 void pb_release(const pb_field_t fields[], void *dest_struct)
1064 pb_field_iter_t iter;
1066 if (!pb_field_iter_begin(&iter, fields, dest_struct))
1067 return; /* Empty message type */
1071 pb_release_single_field(&iter);
1072 } while (pb_field_iter_next(&iter));
1076 /* Field decoders */
1078 bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
1081 if (!pb_decode_varint(stream, &value))
1085 *dest = (int64_t)(~(value >> 1));
1087 *dest = (int64_t)(value >> 1);
1092 bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
1094 #ifdef __BIG_ENDIAN__
1095 uint8_t *bytes = (uint8_t*)dest;
1098 if (!pb_read(stream, lebytes, 4))
1101 bytes[0] = lebytes[3];
1102 bytes[1] = lebytes[2];
1103 bytes[2] = lebytes[1];
1104 bytes[3] = lebytes[0];
1107 return pb_read(stream, (uint8_t*)dest, 4);
1111 bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
1113 #ifdef __BIG_ENDIAN__
1114 uint8_t *bytes = (uint8_t*)dest;
1117 if (!pb_read(stream, lebytes, 8))
1120 bytes[0] = lebytes[7];
1121 bytes[1] = lebytes[6];
1122 bytes[2] = lebytes[5];
1123 bytes[3] = lebytes[4];
1124 bytes[4] = lebytes[3];
1125 bytes[5] = lebytes[2];
1126 bytes[6] = lebytes[1];
1127 bytes[7] = lebytes[0];
1130 return pb_read(stream, (uint8_t*)dest, 8);
1134 static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1139 if (!pb_decode_varint(stream, &value))
1142 /* See issue 97: Google's C++ protobuf allows negative varint values to
1143 * be cast as int32_t, instead of the int64_t that should be used when
1144 * encoding. Previous nanopb versions had a bug in encoding. In order to
1145 * not break decoding of such messages, we cast <=32 bit fields to
1146 * int32_t first to get the sign correct.
1148 if (field->data_size == 8)
1149 svalue = (int64_t)value;
1151 svalue = (int32_t)value;
1153 switch (field->data_size)
1155 case 1: clamped = *(int8_t*)dest = (int8_t)svalue; break;
1156 case 2: clamped = *(int16_t*)dest = (int16_t)svalue; break;
1157 case 4: clamped = *(int32_t*)dest = (int32_t)svalue; break;
1158 case 8: clamped = *(int64_t*)dest = svalue; break;
1159 default: PB_RETURN_ERROR(stream, "invalid data_size");
1162 if (clamped != svalue)
1163 PB_RETURN_ERROR(stream, "integer too large");
1168 static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1170 uint64_t value, clamped;
1171 if (!pb_decode_varint(stream, &value))
1174 switch (field->data_size)
1176 case 1: clamped = *(uint8_t*)dest = (uint8_t)value; break;
1177 case 2: clamped = *(uint16_t*)dest = (uint16_t)value; break;
1178 case 4: clamped = *(uint32_t*)dest = (uint32_t)value; break;
1179 case 8: clamped = *(uint64_t*)dest = value; break;
1180 default: PB_RETURN_ERROR(stream, "invalid data_size");
1183 if (clamped != value)
1184 PB_RETURN_ERROR(stream, "integer too large");
1189 static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1191 int64_t value, clamped;
1192 if (!pb_decode_svarint(stream, &value))
1195 switch (field->data_size)
1197 case 1: clamped = *(int8_t*)dest = (int8_t)value; break;
1198 case 2: clamped = *(int16_t*)dest = (int16_t)value; break;
1199 case 4: clamped = *(int32_t*)dest = (int32_t)value; break;
1200 case 8: clamped = *(int64_t*)dest = value; break;
1201 default: PB_RETURN_ERROR(stream, "invalid data_size");
1204 if (clamped != value)
1205 PB_RETURN_ERROR(stream, "integer too large");
1210 static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
1213 return pb_decode_fixed32(stream, dest);
1216 static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
1219 return pb_decode_fixed64(stream, dest);
1222 static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
1226 pb_bytes_array_t *bdest;
1228 if (!pb_decode_varint32(stream, &size))
1231 if (size > PB_SIZE_MAX)
1232 PB_RETURN_ERROR(stream, "bytes overflow");
1234 alloc_size = PB_BYTES_ARRAY_T_ALLOCSIZE(size);
1235 if (size > alloc_size)
1236 PB_RETURN_ERROR(stream, "size too large");
1238 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1240 #ifndef PB_ENABLE_MALLOC
1241 PB_RETURN_ERROR(stream, "no malloc support");
1243 if (!allocate_field(stream, dest, alloc_size, 1))
1245 bdest = *(pb_bytes_array_t**)dest;
1250 if (alloc_size > field->data_size)
1251 PB_RETURN_ERROR(stream, "bytes overflow");
1252 bdest = (pb_bytes_array_t*)dest;
1255 bdest->size = (pb_size_t)size;
1256 return pb_read(stream, bdest->bytes, size);
1259 static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
1264 if (!pb_decode_varint32(stream, &size))
1267 /* Space for null terminator */
1268 alloc_size = size + 1;
1270 if (alloc_size < size)
1271 PB_RETURN_ERROR(stream, "size too large");
1273 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1275 #ifndef PB_ENABLE_MALLOC
1276 PB_RETURN_ERROR(stream, "no malloc support");
1278 if (!allocate_field(stream, dest, alloc_size, 1))
1280 dest = *(void**)dest;
1285 if (alloc_size > field->data_size)
1286 PB_RETURN_ERROR(stream, "string overflow");
1289 status = pb_read(stream, (uint8_t*)dest, size);
1290 *((uint8_t*)dest + size) = 0;
1294 static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
1297 pb_istream_t substream;
1298 const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr;
1300 if (!pb_make_string_substream(stream, &substream))
1303 if (field->ptr == NULL)
1304 PB_RETURN_ERROR(stream, "invalid field descriptor");
1306 /* New array entries need to be initialized, while required and optional
1307 * submessages have already been initialized in the top-level pb_decode. */
1308 if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
1309 status = pb_decode(&substream, submsg_fields, dest);
1311 status = pb_decode_noinit(&substream, submsg_fields, dest);
1313 pb_close_string_substream(stream, &substream);