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_dec_fixed_length_bytes(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_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 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 bool checkreturn pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
339 if (substream->bytes_left) {
340 if (!pb_read(substream, NULL, substream->bytes_left))
344 stream->state = substream->state;
347 stream->errmsg = substream->errmsg;
352 /*************************
353 * Decode a single field *
354 *************************/
356 static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
361 type = iter->pos->type;
362 func = PB_DECODERS[PB_LTYPE(type)];
364 switch (PB_HTYPE(type))
366 case PB_HTYPE_REQUIRED:
367 return func(stream, iter->pos, iter->pData);
369 case PB_HTYPE_OPTIONAL:
370 if (iter->pSize != iter->pData)
371 *(bool*)iter->pSize = true;
372 return func(stream, iter->pos, iter->pData);
374 case PB_HTYPE_REPEATED:
375 if (wire_type == PB_WT_STRING
376 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
380 pb_size_t *size = (pb_size_t*)iter->pSize;
381 pb_istream_t substream;
382 if (!pb_make_string_substream(stream, &substream))
385 while (substream.bytes_left > 0 && *size < iter->pos->array_size)
387 void *pItem = (char*)iter->pData + iter->pos->data_size * (*size);
388 if (!func(&substream, iter->pos, pItem))
396 if (substream.bytes_left != 0)
397 PB_RETURN_ERROR(stream, "array overflow");
398 if (!pb_close_string_substream(stream, &substream))
406 pb_size_t *size = (pb_size_t*)iter->pSize;
407 void *pItem = (char*)iter->pData + iter->pos->data_size * (*size);
408 if (*size >= iter->pos->array_size)
409 PB_RETURN_ERROR(stream, "array overflow");
412 return func(stream, iter->pos, pItem);
416 *(pb_size_t*)iter->pSize = iter->pos->tag;
417 if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
419 /* We memset to zero so that any callbacks are set to NULL.
420 * Then set any default values. */
421 memset(iter->pData, 0, iter->pos->data_size);
422 pb_message_set_to_defaults((const pb_field_t*)iter->pos->ptr, iter->pData);
424 return func(stream, iter->pos, iter->pData);
427 PB_RETURN_ERROR(stream, "invalid field type");
431 #ifdef PB_ENABLE_MALLOC
432 /* Allocate storage for the field and store the pointer at iter->pData.
433 * array_size is the number of entries to reserve in an array.
434 * Zero size is not allowed, use pb_free() for releasing.
436 static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
438 void *ptr = *(void**)pData;
440 if (data_size == 0 || array_size == 0)
441 PB_RETURN_ERROR(stream, "invalid size");
443 /* Check for multiplication overflows.
444 * This code avoids the costly division if the sizes are small enough.
445 * Multiplication is safe as long as only half of bits are set
446 * in either multiplicand.
449 const size_t check_limit = (size_t)1 << (sizeof(size_t) * 4);
450 if (data_size >= check_limit || array_size >= check_limit)
452 const size_t size_max = (size_t)-1;
453 if (size_max / array_size < data_size)
455 PB_RETURN_ERROR(stream, "size too large");
460 /* Allocate new or expand previous allocation */
461 /* Note: on failure the old pointer will remain in the structure,
462 * the message must be freed by caller also on error return. */
463 ptr = pb_realloc(ptr, array_size * data_size);
465 PB_RETURN_ERROR(stream, "realloc failed");
467 *(void**)pData = ptr;
471 /* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
472 static void initialize_pointer_field(void *pItem, pb_field_iter_t *iter)
474 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_STRING ||
475 PB_LTYPE(iter->pos->type) == PB_LTYPE_BYTES)
477 *(void**)pItem = NULL;
479 else if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
481 pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, pItem);
486 static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
488 #ifndef PB_ENABLE_MALLOC
489 PB_UNUSED(wire_type);
491 PB_RETURN_ERROR(stream, "no malloc support");
496 type = iter->pos->type;
497 func = PB_DECODERS[PB_LTYPE(type)];
499 switch (PB_HTYPE(type))
501 case PB_HTYPE_REQUIRED:
502 case PB_HTYPE_OPTIONAL:
504 if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE &&
505 *(void**)iter->pData != NULL)
507 /* Duplicate field, have to release the old allocation first. */
508 pb_release_single_field(iter);
511 if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
513 *(pb_size_t*)iter->pSize = iter->pos->tag;
516 if (PB_LTYPE(type) == PB_LTYPE_STRING ||
517 PB_LTYPE(type) == PB_LTYPE_BYTES)
519 return func(stream, iter->pos, iter->pData);
523 if (!allocate_field(stream, iter->pData, iter->pos->data_size, 1))
526 initialize_pointer_field(*(void**)iter->pData, iter);
527 return func(stream, iter->pos, *(void**)iter->pData);
530 case PB_HTYPE_REPEATED:
531 if (wire_type == PB_WT_STRING
532 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
534 /* Packed array, multiple items come in at once. */
536 pb_size_t *size = (pb_size_t*)iter->pSize;
537 size_t allocated_size = *size;
539 pb_istream_t substream;
541 if (!pb_make_string_substream(stream, &substream))
544 while (substream.bytes_left)
546 if ((size_t)*size + 1 > allocated_size)
548 /* Allocate more storage. This tries to guess the
549 * number of remaining entries. Round the division
551 allocated_size += (substream.bytes_left - 1) / iter->pos->data_size + 1;
553 if (!allocate_field(&substream, iter->pData, iter->pos->data_size, allocated_size))
560 /* Decode the array entry */
561 pItem = *(char**)iter->pData + iter->pos->data_size * (*size);
562 initialize_pointer_field(pItem, iter);
563 if (!func(&substream, iter->pos, pItem))
569 if (*size == PB_SIZE_MAX)
572 stream->errmsg = "too many array entries";
580 if (!pb_close_string_substream(stream, &substream))
587 /* Normal repeated field, i.e. only one item at a time. */
588 pb_size_t *size = (pb_size_t*)iter->pSize;
591 if (*size == PB_SIZE_MAX)
592 PB_RETURN_ERROR(stream, "too many array entries");
595 if (!allocate_field(stream, iter->pData, iter->pos->data_size, *size))
598 pItem = *(char**)iter->pData + iter->pos->data_size * (*size - 1);
599 initialize_pointer_field(pItem, iter);
600 return func(stream, iter->pos, pItem);
604 PB_RETURN_ERROR(stream, "invalid field type");
609 static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
611 pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
613 #ifdef PB_OLD_CALLBACK_STYLE
614 void *arg = pCallback->arg;
616 void **arg = &(pCallback->arg);
619 if (pCallback->funcs.decode == NULL)
620 return pb_skip_field(stream, wire_type);
622 if (wire_type == PB_WT_STRING)
624 pb_istream_t substream;
626 if (!pb_make_string_substream(stream, &substream))
631 if (!pCallback->funcs.decode(&substream, iter->pos, arg))
632 PB_RETURN_ERROR(stream, "callback failed");
633 } while (substream.bytes_left);
635 if (!pb_close_string_substream(stream, &substream))
642 /* Copy the single scalar value to stack.
643 * This is required so that we can limit the stream length,
644 * which in turn allows to use same callback for packed and
645 * not-packed fields. */
646 pb_istream_t substream;
647 pb_byte_t buffer[10];
648 size_t size = sizeof(buffer);
650 if (!read_raw_value(stream, wire_type, buffer, &size))
652 substream = pb_istream_from_buffer(buffer, size);
654 return pCallback->funcs.decode(&substream, iter->pos, arg);
658 static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
660 #ifdef PB_ENABLE_MALLOC
661 /* When decoding an oneof field, check if there is old data that must be
663 if (PB_HTYPE(iter->pos->type) == PB_HTYPE_ONEOF)
665 if (!pb_release_union_field(stream, iter))
670 switch (PB_ATYPE(iter->pos->type))
672 case PB_ATYPE_STATIC:
673 return decode_static_field(stream, wire_type, iter);
675 case PB_ATYPE_POINTER:
676 return decode_pointer_field(stream, wire_type, iter);
678 case PB_ATYPE_CALLBACK:
679 return decode_callback_field(stream, wire_type, iter);
682 PB_RETURN_ERROR(stream, "invalid field type");
686 static void iter_from_extension(pb_field_iter_t *iter, pb_extension_t *extension)
688 /* Fake a field iterator for the extension field.
689 * It is not actually safe to advance this iterator, but decode_field
690 * will not even try to. */
691 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
692 (void)pb_field_iter_begin(iter, field, extension->dest);
693 iter->pData = extension->dest;
694 iter->pSize = &extension->found;
696 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
698 /* For pointer extensions, the pointer is stored directly
699 * in the extension structure. This avoids having an extra
701 iter->pData = &extension->dest;
705 /* Default handler for extension fields. Expects a pb_field_t structure
706 * in extension->type->arg. */
707 static bool checkreturn default_extension_decoder(pb_istream_t *stream,
708 pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
710 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
711 pb_field_iter_t iter;
713 if (field->tag != tag)
716 iter_from_extension(&iter, extension);
717 extension->found = true;
718 return decode_field(stream, wire_type, &iter);
721 /* Try to decode an unknown field as an extension field. Tries each extension
722 * decoder in turn, until one of them handles the field or loop ends. */
723 static bool checkreturn decode_extension(pb_istream_t *stream,
724 uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter)
726 pb_extension_t *extension = *(pb_extension_t* const *)iter->pData;
727 size_t pos = stream->bytes_left;
729 while (extension != NULL && pos == stream->bytes_left)
732 if (extension->type->decode)
733 status = extension->type->decode(stream, extension, tag, wire_type);
735 status = default_extension_decoder(stream, extension, tag, wire_type);
740 extension = extension->next;
746 /* Step through the iterator until an extension field is found or until all
747 * entries have been checked. There can be only one extension field per
748 * message. Returns false if no extension field is found. */
749 static bool checkreturn find_extension_field(pb_field_iter_t *iter)
751 const pb_field_t *start = iter->pos;
754 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_EXTENSION)
756 (void)pb_field_iter_next(iter);
757 } while (iter->pos != start);
762 /* Initialize message fields to default values, recursively */
763 static void pb_field_set_to_default(pb_field_iter_t *iter)
766 type = iter->pos->type;
768 if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
770 pb_extension_t *ext = *(pb_extension_t* const *)iter->pData;
773 pb_field_iter_t ext_iter;
775 iter_from_extension(&ext_iter, ext);
776 pb_field_set_to_default(&ext_iter);
780 else if (PB_ATYPE(type) == PB_ATYPE_STATIC)
782 bool init_data = true;
783 if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL && iter->pSize != iter->pData)
785 /* Set has_field to false. Still initialize the optional field
787 *(bool*)iter->pSize = false;
789 else if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
790 PB_HTYPE(type) == PB_HTYPE_ONEOF)
792 /* REPEATED: Set array count to 0, no need to initialize contents.
793 ONEOF: Set which_field to 0. */
794 *(pb_size_t*)iter->pSize = 0;
800 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
802 /* Initialize submessage to defaults */
803 pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, iter->pData);
805 else if (iter->pos->ptr != NULL)
807 /* Initialize to default value */
808 memcpy(iter->pData, iter->pos->ptr, iter->pos->data_size);
812 /* Initialize to zeros */
813 memset(iter->pData, 0, iter->pos->data_size);
817 else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
819 /* Initialize the pointer to NULL. */
820 *(void**)iter->pData = NULL;
822 /* Initialize array count to 0. */
823 if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
824 PB_HTYPE(type) == PB_HTYPE_ONEOF)
826 *(pb_size_t*)iter->pSize = 0;
829 else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
831 /* Don't overwrite callback */
835 static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
837 pb_field_iter_t iter;
839 if (!pb_field_iter_begin(&iter, fields, dest_struct))
840 return; /* Empty message type */
844 pb_field_set_to_default(&iter);
845 } while (pb_field_iter_next(&iter));
848 /*********************
849 * Decode all fields *
850 *********************/
852 bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
854 uint32_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 31) / 32] = {0, 0};
855 const uint32_t allbits = ~(uint32_t)0;
856 uint32_t extension_range_start = 0;
857 pb_field_iter_t iter;
859 /* Return value ignored, as empty message types will be correctly handled by
860 * pb_field_iter_find() anyway. */
861 (void)pb_field_iter_begin(&iter, fields, dest_struct);
863 while (stream->bytes_left)
866 pb_wire_type_t wire_type;
869 if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
877 if (!pb_field_iter_find(&iter, tag))
879 /* No match found, check if it matches an extension. */
880 if (tag >= extension_range_start)
882 if (!find_extension_field(&iter))
883 extension_range_start = (uint32_t)-1;
885 extension_range_start = iter.pos->tag;
887 if (tag >= extension_range_start)
889 size_t pos = stream->bytes_left;
891 if (!decode_extension(stream, tag, wire_type, &iter))
894 if (pos != stream->bytes_left)
896 /* The field was handled */
902 /* No match found, skip data */
903 if (!pb_skip_field(stream, wire_type))
908 if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REQUIRED
909 && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
911 uint32_t tmp = ((uint32_t)1 << (iter.required_field_index & 31));
912 fields_seen[iter.required_field_index >> 5] |= tmp;
915 if (!decode_field(stream, wire_type, &iter))
919 /* Check that all required fields were present. */
921 /* First figure out the number of required fields by
922 * seeking to the end of the field array. Usually we
923 * are already close to end after decoding.
925 unsigned req_field_count;
929 req_field_count = iter.required_field_index;
930 last_type = iter.pos->type;
931 } while (pb_field_iter_next(&iter));
933 /* Fixup if last field was also required. */
934 if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.pos->tag != 0)
937 if (req_field_count > 0)
939 /* Check the whole words */
940 for (i = 0; i < (req_field_count >> 5); i++)
942 if (fields_seen[i] != allbits)
943 PB_RETURN_ERROR(stream, "missing required field");
946 /* Check the remaining bits */
947 if (fields_seen[req_field_count >> 5] != (allbits >> (32 - (req_field_count & 31))))
948 PB_RETURN_ERROR(stream, "missing required field");
955 bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
958 pb_message_set_to_defaults(fields, dest_struct);
959 status = pb_decode_noinit(stream, fields, dest_struct);
961 #ifdef PB_ENABLE_MALLOC
963 pb_release(fields, dest_struct);
969 bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
971 pb_istream_t substream;
974 if (!pb_make_string_substream(stream, &substream))
977 status = pb_decode(&substream, fields, dest_struct);
979 if (!pb_close_string_substream(stream, &substream))
984 #ifdef PB_ENABLE_MALLOC
985 /* Given an oneof field, if there has already been a field inside this oneof,
986 * release it before overwriting with a different one. */
987 static bool pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *iter)
989 pb_size_t old_tag = *(pb_size_t*)iter->pSize; /* Previous which_ value */
990 pb_size_t new_tag = iter->pos->tag; /* New which_ value */
993 return true; /* Ok, no old data in union */
995 if (old_tag == new_tag)
996 return true; /* Ok, old data is of same type => merge */
998 /* Release old data. The find can fail if the message struct contains
1000 if (!pb_field_iter_find(iter, old_tag))
1001 PB_RETURN_ERROR(stream, "invalid union tag");
1003 pb_release_single_field(iter);
1005 /* Restore iterator to where it should be.
1006 * This shouldn't fail unless the pb_field_t structure is corrupted. */
1007 if (!pb_field_iter_find(iter, new_tag))
1008 PB_RETURN_ERROR(stream, "iterator error");
1013 static void pb_release_single_field(const pb_field_iter_t *iter)
1016 type = iter->pos->type;
1018 if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
1020 if (*(pb_size_t*)iter->pSize != iter->pos->tag)
1021 return; /* This is not the current field in the union */
1024 /* Release anything contained inside an extension or submsg.
1025 * This has to be done even if the submsg itself is statically
1027 if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
1029 /* Release fields from all extensions in the linked list */
1030 pb_extension_t *ext = *(pb_extension_t**)iter->pData;
1033 pb_field_iter_t ext_iter;
1034 iter_from_extension(&ext_iter, ext);
1035 pb_release_single_field(&ext_iter);
1039 else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
1041 /* Release fields in submessage or submsg array */
1042 void *pItem = iter->pData;
1043 pb_size_t count = 1;
1045 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
1047 pItem = *(void**)iter->pData;
1050 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
1052 count = *(pb_size_t*)iter->pSize;
1054 if (PB_ATYPE(type) == PB_ATYPE_STATIC && count > iter->pos->array_size)
1056 /* Protect against corrupted _count fields */
1057 count = iter->pos->array_size;
1065 pb_release((const pb_field_t*)iter->pos->ptr, pItem);
1066 pItem = (char*)pItem + iter->pos->data_size;
1071 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
1073 if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
1074 (PB_LTYPE(type) == PB_LTYPE_STRING ||
1075 PB_LTYPE(type) == PB_LTYPE_BYTES))
1077 /* Release entries in repeated string or bytes array */
1078 void **pItem = *(void***)iter->pData;
1079 pb_size_t count = *(pb_size_t*)iter->pSize;
1087 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
1089 /* We are going to release the array, so set the size to 0 */
1090 *(pb_size_t*)iter->pSize = 0;
1093 /* Release main item */
1094 pb_free(*(void**)iter->pData);
1095 *(void**)iter->pData = NULL;
1099 void pb_release(const pb_field_t fields[], void *dest_struct)
1101 pb_field_iter_t iter;
1104 return; /* Ignore NULL pointers, similar to free() */
1106 if (!pb_field_iter_begin(&iter, fields, dest_struct))
1107 return; /* Empty message type */
1111 pb_release_single_field(&iter);
1112 } while (pb_field_iter_next(&iter));
1116 /* Field decoders */
1118 bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
1121 if (!pb_decode_varint(stream, &value))
1125 *dest = (int64_t)(~(value >> 1));
1127 *dest = (int64_t)(value >> 1);
1132 bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
1136 if (!pb_read(stream, bytes, 4))
1139 *(uint32_t*)dest = ((uint32_t)bytes[0] << 0) |
1140 ((uint32_t)bytes[1] << 8) |
1141 ((uint32_t)bytes[2] << 16) |
1142 ((uint32_t)bytes[3] << 24);
1146 bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
1150 if (!pb_read(stream, bytes, 8))
1153 *(uint64_t*)dest = ((uint64_t)bytes[0] << 0) |
1154 ((uint64_t)bytes[1] << 8) |
1155 ((uint64_t)bytes[2] << 16) |
1156 ((uint64_t)bytes[3] << 24) |
1157 ((uint64_t)bytes[4] << 32) |
1158 ((uint64_t)bytes[5] << 40) |
1159 ((uint64_t)bytes[6] << 48) |
1160 ((uint64_t)bytes[7] << 56);
1165 static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1170 if (!pb_decode_varint(stream, &value))
1173 /* See issue 97: Google's C++ protobuf allows negative varint values to
1174 * be cast as int32_t, instead of the int64_t that should be used when
1175 * encoding. Previous nanopb versions had a bug in encoding. In order to
1176 * not break decoding of such messages, we cast <=32 bit fields to
1177 * int32_t first to get the sign correct.
1179 if (field->data_size == sizeof(int64_t))
1180 svalue = (int64_t)value;
1182 svalue = (int32_t)value;
1184 /* Cast to the proper field size, while checking for overflows */
1185 if (field->data_size == sizeof(int64_t))
1186 clamped = *(int64_t*)dest = svalue;
1187 else if (field->data_size == sizeof(int32_t))
1188 clamped = *(int32_t*)dest = (int32_t)svalue;
1189 else if (field->data_size == sizeof(int_least16_t))
1190 clamped = *(int_least16_t*)dest = (int_least16_t)svalue;
1191 else if (field->data_size == sizeof(int_least8_t))
1192 clamped = *(int_least8_t*)dest = (int_least8_t)svalue;
1194 PB_RETURN_ERROR(stream, "invalid data_size");
1196 if (clamped != svalue)
1197 PB_RETURN_ERROR(stream, "integer too large");
1202 static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1204 uint64_t value, clamped;
1205 if (!pb_decode_varint(stream, &value))
1208 /* Cast to the proper field size, while checking for overflows */
1209 if (field->data_size == sizeof(uint64_t))
1210 clamped = *(uint64_t*)dest = value;
1211 else if (field->data_size == sizeof(uint32_t))
1212 clamped = *(uint32_t*)dest = (uint32_t)value;
1213 else if (field->data_size == sizeof(uint_least16_t))
1214 clamped = *(uint_least16_t*)dest = (uint_least16_t)value;
1215 else if (field->data_size == sizeof(uint_least8_t))
1216 clamped = *(uint_least8_t*)dest = (uint_least8_t)value;
1218 PB_RETURN_ERROR(stream, "invalid data_size");
1220 if (clamped != value)
1221 PB_RETURN_ERROR(stream, "integer too large");
1226 static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1228 int64_t value, clamped;
1229 if (!pb_decode_svarint(stream, &value))
1232 /* Cast to the proper field size, while checking for overflows */
1233 if (field->data_size == sizeof(int64_t))
1234 clamped = *(int64_t*)dest = value;
1235 else if (field->data_size == sizeof(int32_t))
1236 clamped = *(int32_t*)dest = (int32_t)value;
1237 else if (field->data_size == sizeof(int_least16_t))
1238 clamped = *(int_least16_t*)dest = (int_least16_t)value;
1239 else if (field->data_size == sizeof(int_least8_t))
1240 clamped = *(int_least8_t*)dest = (int_least8_t)value;
1242 PB_RETURN_ERROR(stream, "invalid data_size");
1244 if (clamped != value)
1245 PB_RETURN_ERROR(stream, "integer too large");
1250 static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
1253 return pb_decode_fixed32(stream, dest);
1256 static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
1259 return pb_decode_fixed64(stream, dest);
1262 static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
1266 pb_bytes_array_t *bdest;
1268 if (!pb_decode_varint32(stream, &size))
1271 if (size > PB_SIZE_MAX)
1272 PB_RETURN_ERROR(stream, "bytes overflow");
1274 alloc_size = PB_BYTES_ARRAY_T_ALLOCSIZE(size);
1275 if (size > alloc_size)
1276 PB_RETURN_ERROR(stream, "size too large");
1278 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1280 #ifndef PB_ENABLE_MALLOC
1281 PB_RETURN_ERROR(stream, "no malloc support");
1283 if (!allocate_field(stream, dest, alloc_size, 1))
1285 bdest = *(pb_bytes_array_t**)dest;
1290 if (alloc_size > field->data_size)
1291 PB_RETURN_ERROR(stream, "bytes overflow");
1292 bdest = (pb_bytes_array_t*)dest;
1295 bdest->size = (pb_size_t)size;
1296 return pb_read(stream, bdest->bytes, size);
1299 static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
1304 if (!pb_decode_varint32(stream, &size))
1307 /* Space for null terminator */
1308 alloc_size = size + 1;
1310 if (alloc_size < size)
1311 PB_RETURN_ERROR(stream, "size too large");
1313 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1315 #ifndef PB_ENABLE_MALLOC
1316 PB_RETURN_ERROR(stream, "no malloc support");
1318 if (!allocate_field(stream, dest, alloc_size, 1))
1320 dest = *(void**)dest;
1325 if (alloc_size > field->data_size)
1326 PB_RETURN_ERROR(stream, "string overflow");
1329 status = pb_read(stream, (pb_byte_t*)dest, size);
1330 *((pb_byte_t*)dest + size) = 0;
1334 static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
1337 pb_istream_t substream;
1338 const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr;
1340 if (!pb_make_string_substream(stream, &substream))
1343 if (field->ptr == NULL)
1344 PB_RETURN_ERROR(stream, "invalid field descriptor");
1346 /* New array entries need to be initialized, while required and optional
1347 * submessages have already been initialized in the top-level pb_decode. */
1348 if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
1349 status = pb_decode(&substream, submsg_fields, dest);
1351 status = pb_decode_noinit(&substream, submsg_fields, dest);
1353 if (!pb_close_string_substream(stream, &substream))
1358 static bool checkreturn pb_dec_fixed_length_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
1362 if (!pb_decode_varint32(stream, &size))
1365 if (size > PB_SIZE_MAX)
1366 PB_RETURN_ERROR(stream, "bytes overflow");
1370 /* As a special case, treat empty bytes string as all zeros for fixed_length_bytes. */
1371 memset(dest, 0, field->data_size);
1375 if (size != field->data_size)
1376 PB_RETURN_ERROR(stream, "incorrect fixed length bytes size");
1378 return pb_read(stream, (pb_byte_t*)dest, field->data_size);