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 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] = {
70 /*******************************
71 * pb_istream_t implementation *
72 *******************************/
74 static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count)
76 uint8_t *source = (uint8_t*)stream->state;
77 stream->state = source + count;
88 bool checkreturn pb_read(pb_istream_t *stream, uint8_t *buf, size_t count)
90 #ifndef PB_BUFFER_ONLY
91 if (buf == NULL && stream->callback != buf_read)
93 /* Skip input bytes */
97 if (!pb_read(stream, tmp, 16))
103 return pb_read(stream, tmp, count);
107 if (stream->bytes_left < count)
108 PB_RETURN_ERROR(stream, "end-of-stream");
110 #ifndef PB_BUFFER_ONLY
111 if (!stream->callback(stream, buf, count))
112 PB_RETURN_ERROR(stream, "io error");
114 if (!buf_read(stream, buf, count))
118 stream->bytes_left -= count;
122 /* Read a single byte from input stream. buf may not be NULL.
123 * This is an optimization for the varint decoding. */
124 static bool checkreturn pb_readbyte(pb_istream_t *stream, uint8_t *buf)
126 if (stream->bytes_left == 0)
127 PB_RETURN_ERROR(stream, "end-of-stream");
129 #ifndef PB_BUFFER_ONLY
130 if (!stream->callback(stream, buf, 1))
131 PB_RETURN_ERROR(stream, "io error");
133 *buf = *(uint8_t*)stream->state;
134 stream->state = (uint8_t*)stream->state + 1;
137 stream->bytes_left--;
142 pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize)
145 #ifdef PB_BUFFER_ONLY
146 stream.callback = NULL;
148 stream.callback = &buf_read;
151 stream.bytes_left = bufsize;
153 stream.errmsg = NULL;
158 /********************
160 ********************/
162 static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
167 if (!pb_readbyte(stream, &byte))
170 if ((byte & 0x80) == 0)
172 /* Quick case, 1 byte value */
179 result = byte & 0x7F;
184 PB_RETURN_ERROR(stream, "varint overflow");
186 if (!pb_readbyte(stream, &byte))
189 result |= (uint32_t)(byte & 0x7F) << bitpos;
190 bitpos = (uint8_t)(bitpos + 7);
191 } while (byte & 0x80);
198 bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
207 PB_RETURN_ERROR(stream, "varint overflow");
209 if (!pb_readbyte(stream, &byte))
212 result |= (uint64_t)(byte & 0x7F) << bitpos;
213 bitpos = (uint8_t)(bitpos + 7);
214 } while (byte & 0x80);
220 bool checkreturn pb_skip_varint(pb_istream_t *stream)
225 if (!pb_read(stream, &byte, 1))
227 } while (byte & 0x80);
231 bool checkreturn pb_skip_string(pb_istream_t *stream)
234 if (!pb_decode_varint32(stream, &length))
237 return pb_read(stream, NULL, length);
240 bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
244 *wire_type = (pb_wire_type_t) 0;
247 if (!pb_decode_varint32(stream, &temp))
249 if (stream->bytes_left == 0)
257 *eof = true; /* Special feature: allow 0-terminated messages. */
262 *wire_type = (pb_wire_type_t)(temp & 7);
266 bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
270 case PB_WT_VARINT: return pb_skip_varint(stream);
271 case PB_WT_64BIT: return pb_read(stream, NULL, 8);
272 case PB_WT_STRING: return pb_skip_string(stream);
273 case PB_WT_32BIT: return pb_read(stream, NULL, 4);
274 default: PB_RETURN_ERROR(stream, "invalid wire_type");
278 /* Read a raw value to buffer, for the purpose of passing it to callback as
279 * a substream. Size is maximum size on call, and actual size on return.
281 static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, uint8_t *buf, size_t *size)
283 size_t max_size = *size;
291 if (*size > max_size) return false;
292 if (!pb_read(stream, buf, 1)) return false;
293 } while (*buf++ & 0x80);
298 return pb_read(stream, buf, 8);
302 return pb_read(stream, buf, 4);
304 default: PB_RETURN_ERROR(stream, "invalid wire_type");
308 /* Decode string length from stream and return a substream with limited length.
309 * Remember to close the substream using pb_close_string_substream().
311 bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
314 if (!pb_decode_varint32(stream, &size))
317 *substream = *stream;
318 if (substream->bytes_left < size)
319 PB_RETURN_ERROR(stream, "parent stream too short");
321 substream->bytes_left = size;
322 stream->bytes_left -= size;
326 void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
328 stream->state = substream->state;
331 stream->errmsg = substream->errmsg;
335 /*************************
336 * Decode a single field *
337 *************************/
339 static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
344 type = iter->pos->type;
345 func = PB_DECODERS[PB_LTYPE(type)];
347 switch (PB_HTYPE(type))
349 case PB_HTYPE_REQUIRED:
350 return func(stream, iter->pos, iter->pData);
352 case PB_HTYPE_OPTIONAL:
353 *(bool*)iter->pSize = true;
354 return func(stream, iter->pos, iter->pData);
356 case PB_HTYPE_REPEATED:
357 if (wire_type == PB_WT_STRING
358 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
362 pb_size_t *size = (pb_size_t*)iter->pSize;
363 pb_istream_t substream;
364 if (!pb_make_string_substream(stream, &substream))
367 while (substream.bytes_left > 0 && *size < iter->pos->array_size)
369 void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
370 if (!func(&substream, iter->pos, pItem))
377 pb_close_string_substream(stream, &substream);
379 if (substream.bytes_left != 0)
380 PB_RETURN_ERROR(stream, "array overflow");
387 pb_size_t *size = (pb_size_t*)iter->pSize;
388 void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
389 if (*size >= iter->pos->array_size)
390 PB_RETURN_ERROR(stream, "array overflow");
393 return func(stream, iter->pos, pItem);
397 *(pb_size_t*)iter->pSize = iter->pos->tag;
398 return func(stream, iter->pos, iter->pData);
401 PB_RETURN_ERROR(stream, "invalid field type");
405 #ifdef PB_ENABLE_MALLOC
406 /* Allocate storage for the field and store the pointer at iter->pData.
407 * array_size is the number of entries to reserve in an array.
408 * Zero size is not allowed, use pb_free() for releasing.
410 static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
412 void *ptr = *(void**)pData;
414 if (data_size == 0 || array_size == 0)
415 PB_RETURN_ERROR(stream, "invalid size");
417 /* Check for multiplication overflows.
418 * This code avoids the costly division if the sizes are small enough.
419 * Multiplication is safe as long as only half of bits are set
420 * in either multiplicand.
423 const size_t check_limit = (size_t)1 << (sizeof(size_t) * 4);
424 if (data_size >= check_limit || array_size >= check_limit)
426 const size_t size_max = (size_t)-1;
427 if (size_max / array_size < data_size)
429 PB_RETURN_ERROR(stream, "size too large");
434 /* Allocate new or expand previous allocation */
435 /* Note: on failure the old pointer will remain in the structure,
436 * the message must be freed by caller also on error return. */
437 ptr = pb_realloc(ptr, array_size * data_size);
439 PB_RETURN_ERROR(stream, "realloc failed");
441 *(void**)pData = ptr;
445 /* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
446 static void initialize_pointer_field(void *pItem, pb_field_iter_t *iter)
448 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_STRING ||
449 PB_LTYPE(iter->pos->type) == PB_LTYPE_BYTES)
451 *(void**)pItem = NULL;
453 else if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
455 pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, pItem);
460 static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
462 #ifndef PB_ENABLE_MALLOC
463 PB_UNUSED(wire_type);
465 PB_RETURN_ERROR(stream, "no malloc support");
470 type = iter->pos->type;
471 func = PB_DECODERS[PB_LTYPE(type)];
473 switch (PB_HTYPE(type))
475 case PB_HTYPE_REQUIRED:
476 case PB_HTYPE_OPTIONAL:
478 if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE &&
479 *(void**)iter->pData != NULL)
481 /* Duplicate field, have to release the old allocation first. */
482 pb_release_single_field(iter);
485 if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
487 *(pb_size_t*)iter->pSize = iter->pos->tag;
490 if (PB_LTYPE(type) == PB_LTYPE_STRING ||
491 PB_LTYPE(type) == PB_LTYPE_BYTES)
493 return func(stream, iter->pos, iter->pData);
497 if (!allocate_field(stream, iter->pData, iter->pos->data_size, 1))
500 initialize_pointer_field(*(void**)iter->pData, iter);
501 return func(stream, iter->pos, *(void**)iter->pData);
504 case PB_HTYPE_REPEATED:
505 if (wire_type == PB_WT_STRING
506 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
508 /* Packed array, multiple items come in at once. */
510 pb_size_t *size = (pb_size_t*)iter->pSize;
511 size_t allocated_size = *size;
513 pb_istream_t substream;
515 if (!pb_make_string_substream(stream, &substream))
518 while (substream.bytes_left)
520 if ((size_t)*size + 1 > allocated_size)
522 /* Allocate more storage. This tries to guess the
523 * number of remaining entries. Round the division
525 allocated_size += (substream.bytes_left - 1) / iter->pos->data_size + 1;
527 if (!allocate_field(&substream, iter->pData, iter->pos->data_size, allocated_size))
534 /* Decode the array entry */
535 pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size);
536 initialize_pointer_field(pItem, iter);
537 if (!func(&substream, iter->pos, pItem))
543 if (*size == PB_SIZE_MAX)
546 stream->errmsg = "too many array entries";
554 pb_close_string_substream(stream, &substream);
560 /* Normal repeated field, i.e. only one item at a time. */
561 pb_size_t *size = (pb_size_t*)iter->pSize;
564 if (*size == PB_SIZE_MAX)
565 PB_RETURN_ERROR(stream, "too many array entries");
568 if (!allocate_field(stream, iter->pData, iter->pos->data_size, *size))
571 pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size - 1);
572 initialize_pointer_field(pItem, iter);
573 return func(stream, iter->pos, pItem);
577 PB_RETURN_ERROR(stream, "invalid field type");
582 static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
584 pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
586 #ifdef PB_OLD_CALLBACK_STYLE
587 void *arg = pCallback->arg;
589 void **arg = &(pCallback->arg);
592 if (pCallback->funcs.decode == NULL)
593 return pb_skip_field(stream, wire_type);
595 if (wire_type == PB_WT_STRING)
597 pb_istream_t substream;
599 if (!pb_make_string_substream(stream, &substream))
604 if (!pCallback->funcs.decode(&substream, iter->pos, arg))
605 PB_RETURN_ERROR(stream, "callback failed");
606 } while (substream.bytes_left);
608 pb_close_string_substream(stream, &substream);
613 /* Copy the single scalar value to stack.
614 * This is required so that we can limit the stream length,
615 * which in turn allows to use same callback for packed and
616 * not-packed fields. */
617 pb_istream_t substream;
619 size_t size = sizeof(buffer);
621 if (!read_raw_value(stream, wire_type, buffer, &size))
623 substream = pb_istream_from_buffer(buffer, size);
625 return pCallback->funcs.decode(&substream, iter->pos, arg);
629 static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
631 switch (PB_ATYPE(iter->pos->type))
633 case PB_ATYPE_STATIC:
634 return decode_static_field(stream, wire_type, iter);
636 case PB_ATYPE_POINTER:
637 return decode_pointer_field(stream, wire_type, iter);
639 case PB_ATYPE_CALLBACK:
640 return decode_callback_field(stream, wire_type, iter);
643 PB_RETURN_ERROR(stream, "invalid field type");
647 static void iter_from_extension(pb_field_iter_t *iter, pb_extension_t *extension)
649 /* Fake a field iterator for the extension field.
650 * It is not actually safe to advance this iterator, but decode_field
651 * will not even try to. */
652 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
653 (void)pb_field_iter_begin(iter, field, extension->dest);
654 iter->pData = extension->dest;
655 iter->pSize = &extension->found;
657 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
659 /* For pointer extensions, the pointer is stored directly
660 * in the extension structure. This avoids having an extra
662 iter->pData = &extension->dest;
666 /* Default handler for extension fields. Expects a pb_field_t structure
667 * in extension->type->arg. */
668 static bool checkreturn default_extension_decoder(pb_istream_t *stream,
669 pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
671 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
672 pb_field_iter_t iter;
674 if (field->tag != tag)
677 iter_from_extension(&iter, extension);
678 extension->found = true;
679 return decode_field(stream, wire_type, &iter);
682 /* Try to decode an unknown field as an extension field. Tries each extension
683 * decoder in turn, until one of them handles the field or loop ends. */
684 static bool checkreturn decode_extension(pb_istream_t *stream,
685 uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter)
687 pb_extension_t *extension = *(pb_extension_t* const *)iter->pData;
688 size_t pos = stream->bytes_left;
690 while (extension != NULL && pos == stream->bytes_left)
693 if (extension->type->decode)
694 status = extension->type->decode(stream, extension, tag, wire_type);
696 status = default_extension_decoder(stream, extension, tag, wire_type);
701 extension = extension->next;
707 /* Step through the iterator until an extension field is found or until all
708 * entries have been checked. There can be only one extension field per
709 * message. Returns false if no extension field is found. */
710 static bool checkreturn find_extension_field(pb_field_iter_t *iter)
712 const pb_field_t *start = iter->pos;
715 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_EXTENSION)
717 (void)pb_field_iter_next(iter);
718 } while (iter->pos != start);
723 /* Initialize message fields to default values, recursively */
724 static void pb_field_set_to_default(pb_field_iter_t *iter)
727 type = iter->pos->type;
729 if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
731 pb_extension_t *ext = *(pb_extension_t* const *)iter->pData;
734 pb_field_iter_t ext_iter;
736 iter_from_extension(&ext_iter, ext);
737 pb_field_set_to_default(&ext_iter);
741 else if (PB_ATYPE(type) == PB_ATYPE_STATIC)
743 bool init_data = true;
744 if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL)
746 /* Set has_field to false. Still initialize the optional field
748 *(bool*)iter->pSize = false;
750 else if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
751 PB_HTYPE(type) == PB_HTYPE_ONEOF)
753 /* REPEATED: Set array count to 0, no need to initialize contents.
754 ONEOF: Set which_field to 0. */
755 *(pb_size_t*)iter->pSize = 0;
761 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
763 /* Initialize submessage to defaults */
764 pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, iter->pData);
766 else if (iter->pos->ptr != NULL)
768 /* Initialize to default value */
769 memcpy(iter->pData, iter->pos->ptr, iter->pos->data_size);
773 /* Initialize to zeros */
774 memset(iter->pData, 0, iter->pos->data_size);
778 else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
780 /* Initialize the pointer to NULL. */
781 *(void**)iter->pData = NULL;
783 /* Initialize array count to 0. */
784 if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
785 PB_HTYPE(type) == PB_HTYPE_ONEOF)
787 *(pb_size_t*)iter->pSize = 0;
790 else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
792 /* Don't overwrite callback */
796 static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
798 pb_field_iter_t iter;
800 if (!pb_field_iter_begin(&iter, fields, dest_struct))
801 return; /* Empty message type */
805 pb_field_set_to_default(&iter);
806 } while (pb_field_iter_next(&iter));
809 /*********************
810 * Decode all fields *
811 *********************/
813 bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
815 uint8_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 7) / 8] = {0, 0, 0, 0, 0, 0, 0, 0};
816 uint32_t extension_range_start = 0;
817 pb_field_iter_t iter;
819 /* Return value ignored, as empty message types will be correctly handled by
820 * pb_field_iter_find() anyway. */
821 (void)pb_field_iter_begin(&iter, fields, dest_struct);
823 while (stream->bytes_left)
826 pb_wire_type_t wire_type;
829 if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
837 if (!pb_field_iter_find(&iter, tag))
839 /* No match found, check if it matches an extension. */
840 if (tag >= extension_range_start)
842 if (!find_extension_field(&iter))
843 extension_range_start = (uint32_t)-1;
845 extension_range_start = iter.pos->tag;
847 if (tag >= extension_range_start)
849 size_t pos = stream->bytes_left;
851 if (!decode_extension(stream, tag, wire_type, &iter))
854 if (pos != stream->bytes_left)
856 /* The field was handled */
862 /* No match found, skip data */
863 if (!pb_skip_field(stream, wire_type))
868 if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REQUIRED
869 && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
871 fields_seen[iter.required_field_index >> 3] |= (uint8_t)(1 << (iter.required_field_index & 7));
874 if (!decode_field(stream, wire_type, &iter))
878 /* Check that all required fields were present. */
880 /* First figure out the number of required fields by
881 * seeking to the end of the field array. Usually we
882 * are already close to end after decoding.
884 unsigned req_field_count;
888 req_field_count = iter.required_field_index;
889 last_type = iter.pos->type;
890 } while (pb_field_iter_next(&iter));
892 /* Fixup if last field was also required. */
893 if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.pos->tag != 0)
896 /* Check the whole bytes */
897 for (i = 0; i < (req_field_count >> 3); i++)
899 if (fields_seen[i] != 0xFF)
900 PB_RETURN_ERROR(stream, "missing required field");
903 /* Check the remaining bits */
904 if (fields_seen[req_field_count >> 3] != (0xFF >> (8 - (req_field_count & 7))))
905 PB_RETURN_ERROR(stream, "missing required field");
911 bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
914 pb_message_set_to_defaults(fields, dest_struct);
915 status = pb_decode_noinit(stream, fields, dest_struct);
917 #ifdef PB_ENABLE_MALLOC
919 pb_release(fields, dest_struct);
925 bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
927 pb_istream_t substream;
930 if (!pb_make_string_substream(stream, &substream))
933 status = pb_decode(&substream, fields, dest_struct);
934 pb_close_string_substream(stream, &substream);
938 #ifdef PB_ENABLE_MALLOC
939 static void pb_release_single_field(const pb_field_iter_t *iter)
942 type = iter->pos->type;
944 if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
946 if (*(pb_size_t*)iter->pSize != iter->pos->tag)
947 return; /* This is not the current field in the union */
950 /* Release anything contained inside an extension or submsg.
951 * This has to be done even if the submsg itself is statically
953 if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
955 /* Release fields from all extensions in the linked list */
956 pb_extension_t *ext = *(pb_extension_t**)iter->pData;
959 pb_field_iter_t ext_iter;
960 iter_from_extension(&ext_iter, ext);
961 pb_release_single_field(&ext_iter);
965 else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
967 /* Release fields in submessage or submsg array */
968 void *pItem = iter->pData;
971 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
973 pItem = *(void**)iter->pData;
976 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
978 count = *(pb_size_t*)iter->pSize;
985 pb_release((const pb_field_t*)iter->pos->ptr, pItem);
986 pItem = (uint8_t*)pItem + iter->pos->data_size;
991 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
993 if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
994 (PB_LTYPE(type) == PB_LTYPE_STRING ||
995 PB_LTYPE(type) == PB_LTYPE_BYTES))
997 /* Release entries in repeated string or bytes array */
998 void **pItem = *(void***)iter->pData;
999 pb_size_t count = *(pb_size_t*)iter->pSize;
1007 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
1009 /* We are going to release the array, so set the size to 0 */
1010 *(pb_size_t*)iter->pSize = 0;
1013 /* Release main item */
1014 pb_free(*(void**)iter->pData);
1015 *(void**)iter->pData = NULL;
1019 void pb_release(const pb_field_t fields[], void *dest_struct)
1021 pb_field_iter_t iter;
1023 if (!pb_field_iter_begin(&iter, fields, dest_struct))
1024 return; /* Empty message type */
1028 pb_release_single_field(&iter);
1029 } while (pb_field_iter_next(&iter));
1033 /* Field decoders */
1035 bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
1038 if (!pb_decode_varint(stream, &value))
1042 *dest = (int64_t)(~(value >> 1));
1044 *dest = (int64_t)(value >> 1);
1049 bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
1051 #ifdef __BIG_ENDIAN__
1052 uint8_t *bytes = (uint8_t*)dest;
1055 if (!pb_read(stream, lebytes, 4))
1058 bytes[0] = lebytes[3];
1059 bytes[1] = lebytes[2];
1060 bytes[2] = lebytes[1];
1061 bytes[3] = lebytes[0];
1064 return pb_read(stream, (uint8_t*)dest, 4);
1068 bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
1070 #ifdef __BIG_ENDIAN__
1071 uint8_t *bytes = (uint8_t*)dest;
1074 if (!pb_read(stream, lebytes, 8))
1077 bytes[0] = lebytes[7];
1078 bytes[1] = lebytes[6];
1079 bytes[2] = lebytes[5];
1080 bytes[3] = lebytes[4];
1081 bytes[4] = lebytes[3];
1082 bytes[5] = lebytes[2];
1083 bytes[6] = lebytes[1];
1084 bytes[7] = lebytes[0];
1087 return pb_read(stream, (uint8_t*)dest, 8);
1091 static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1096 if (!pb_decode_varint(stream, &value))
1099 /* See issue 97: Google's C++ protobuf allows negative varint values to
1100 * be cast as int32_t, instead of the int64_t that should be used when
1101 * encoding. Previous nanopb versions had a bug in encoding. In order to
1102 * not break decoding of such messages, we cast <=32 bit fields to
1103 * int32_t first to get the sign correct.
1105 if (field->data_size == 8)
1106 svalue = (int64_t)value;
1108 svalue = (int32_t)value;
1110 switch (field->data_size)
1112 case 1: clamped = *(int8_t*)dest = (int8_t)svalue; break;
1113 case 2: clamped = *(int16_t*)dest = (int16_t)svalue; break;
1114 case 4: clamped = *(int32_t*)dest = (int32_t)svalue; break;
1115 case 8: clamped = *(int64_t*)dest = svalue; break;
1116 default: PB_RETURN_ERROR(stream, "invalid data_size");
1119 if (clamped != svalue)
1120 PB_RETURN_ERROR(stream, "integer too large");
1125 static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1127 uint64_t value, clamped;
1128 if (!pb_decode_varint(stream, &value))
1131 switch (field->data_size)
1133 case 1: clamped = *(uint8_t*)dest = (uint8_t)value; break;
1134 case 2: clamped = *(uint16_t*)dest = (uint16_t)value; break;
1135 case 4: clamped = *(uint32_t*)dest = (uint32_t)value; break;
1136 case 8: clamped = *(uint64_t*)dest = value; break;
1137 default: PB_RETURN_ERROR(stream, "invalid data_size");
1140 if (clamped != value)
1141 PB_RETURN_ERROR(stream, "integer too large");
1146 static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1148 int64_t value, clamped;
1149 if (!pb_decode_svarint(stream, &value))
1152 switch (field->data_size)
1154 case 1: clamped = *(int8_t*)dest = (int8_t)value; break;
1155 case 2: clamped = *(int16_t*)dest = (int16_t)value; break;
1156 case 4: clamped = *(int32_t*)dest = (int32_t)value; break;
1157 case 8: clamped = *(int64_t*)dest = value; break;
1158 default: PB_RETURN_ERROR(stream, "invalid data_size");
1161 if (clamped != value)
1162 PB_RETURN_ERROR(stream, "integer too large");
1167 static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
1170 return pb_decode_fixed32(stream, dest);
1173 static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
1176 return pb_decode_fixed64(stream, dest);
1179 static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
1183 pb_bytes_array_t *bdest;
1185 if (!pb_decode_varint32(stream, &size))
1188 if (size > PB_SIZE_MAX)
1189 PB_RETURN_ERROR(stream, "bytes overflow");
1191 alloc_size = PB_BYTES_ARRAY_T_ALLOCSIZE(size);
1192 if (size > alloc_size)
1193 PB_RETURN_ERROR(stream, "size too large");
1195 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1197 #ifndef PB_ENABLE_MALLOC
1198 PB_RETURN_ERROR(stream, "no malloc support");
1200 if (!allocate_field(stream, dest, alloc_size, 1))
1202 bdest = *(pb_bytes_array_t**)dest;
1207 if (alloc_size > field->data_size)
1208 PB_RETURN_ERROR(stream, "bytes overflow");
1209 bdest = (pb_bytes_array_t*)dest;
1212 bdest->size = (pb_size_t)size;
1213 return pb_read(stream, bdest->bytes, size);
1216 static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
1221 if (!pb_decode_varint32(stream, &size))
1224 /* Space for null terminator */
1225 alloc_size = size + 1;
1227 if (alloc_size < size)
1228 PB_RETURN_ERROR(stream, "size too large");
1230 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1232 #ifndef PB_ENABLE_MALLOC
1233 PB_RETURN_ERROR(stream, "no malloc support");
1235 if (!allocate_field(stream, dest, alloc_size, 1))
1237 dest = *(void**)dest;
1242 if (alloc_size > field->data_size)
1243 PB_RETURN_ERROR(stream, "string overflow");
1246 status = pb_read(stream, (uint8_t*)dest, size);
1247 *((uint8_t*)dest + size) = 0;
1251 static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
1254 pb_istream_t substream;
1255 const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr;
1257 if (!pb_make_string_substream(stream, &substream))
1260 if (field->ptr == NULL)
1261 PB_RETURN_ERROR(stream, "invalid field descriptor");
1263 /* New array entries need to be initialized, while required and optional
1264 * submessages have already been initialized in the top-level pb_decode. */
1265 if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
1266 status = pb_decode(&substream, submsg_fields, dest);
1268 status = pb_decode_noinit(&substream, submsg_fields, dest);
1270 pb_close_string_substream(stream, &substream);