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"
19 /**************************************
20 * Declarations internal to this file *
21 **************************************/
23 /* Iterator for pb_field_t list */
25 const pb_field_t *start; /* Start of the pb_field_t array */
26 const pb_field_t *pos; /* Current position of the iterator */
27 unsigned field_index; /* Zero-based index of the field. */
28 unsigned required_field_index; /* Zero-based index that counts only the required fields */
29 void *dest_struct; /* Pointer to the destination structure to decode to */
30 void *pData; /* Pointer where to store current field value */
31 void *pSize; /* Pointer where to store the size of current array field */
32 } pb_field_iterator_t;
34 typedef bool (*pb_decoder_t)(pb_istream_t *stream, const pb_field_t *field, void *dest) checkreturn;
36 static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count);
37 static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest);
38 static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, uint8_t *buf, size_t *size);
39 static void pb_field_init(pb_field_iterator_t *iter, const pb_field_t *fields, void *dest_struct);
40 static bool pb_field_next(pb_field_iterator_t *iter);
41 static bool checkreturn pb_field_find(pb_field_iterator_t *iter, uint32_t tag);
42 static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter);
43 static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter);
44 static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter);
45 static bool checkreturn default_extension_decoder(pb_istream_t *stream, pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type);
46 static bool checkreturn decode_extension(pb_istream_t *stream, uint32_t tag, pb_wire_type_t wire_type, pb_field_iterator_t *iter);
47 static bool checkreturn find_extension_field(pb_field_iterator_t *iter);
48 static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct);
49 static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest);
50 static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
51 static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
52 static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest);
53 static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest);
54 static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest);
55 static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest);
56 static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest);
57 static bool checkreturn pb_skip_varint(pb_istream_t *stream);
58 static bool checkreturn pb_skip_string(pb_istream_t *stream);
60 /* --- Function pointers to field decoders ---
61 * Order in the array must match pb_action_t LTYPE numbering.
63 static const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = {
76 /*******************************
77 * pb_istream_t implementation *
78 *******************************/
80 static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count)
82 uint8_t *source = (uint8_t*)stream->state;
83 stream->state = source + count;
94 bool checkreturn pb_read(pb_istream_t *stream, uint8_t *buf, size_t count)
96 #ifndef PB_BUFFER_ONLY
97 if (buf == NULL && stream->callback != buf_read)
99 /* Skip input bytes */
103 if (!pb_read(stream, tmp, 16))
109 return pb_read(stream, tmp, count);
113 if (stream->bytes_left < count)
114 PB_RETURN_ERROR(stream, "end-of-stream");
116 #ifndef PB_BUFFER_ONLY
117 if (!stream->callback(stream, buf, count))
118 PB_RETURN_ERROR(stream, "io error");
120 if (!buf_read(stream, buf, count))
124 stream->bytes_left -= count;
128 /* Read a single byte from input stream. buf may not be NULL.
129 * This is an optimization for the varint decoding. */
130 static bool checkreturn pb_readbyte(pb_istream_t *stream, uint8_t *buf)
132 if (stream->bytes_left == 0)
133 PB_RETURN_ERROR(stream, "end-of-stream");
135 #ifndef PB_BUFFER_ONLY
136 if (!stream->callback(stream, buf, 1))
137 PB_RETURN_ERROR(stream, "io error");
139 *buf = *(uint8_t*)stream->state;
140 stream->state = (uint8_t*)stream->state + 1;
143 stream->bytes_left--;
148 pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize)
151 #ifdef PB_BUFFER_ONLY
152 stream.callback = NULL;
154 stream.callback = &buf_read;
157 stream.bytes_left = bufsize;
159 stream.errmsg = NULL;
164 /********************
166 ********************/
168 static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
173 if (!pb_readbyte(stream, &byte))
176 if ((byte & 0x80) == 0)
178 /* Quick case, 1 byte value */
185 result = byte & 0x7F;
190 PB_RETURN_ERROR(stream, "varint overflow");
192 if (!pb_readbyte(stream, &byte))
195 result |= (uint32_t)(byte & 0x7F) << bitpos;
196 bitpos = (uint8_t)(bitpos + 7);
197 } while (byte & 0x80);
204 bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
213 PB_RETURN_ERROR(stream, "varint overflow");
215 if (!pb_readbyte(stream, &byte))
218 result |= (uint64_t)(byte & 0x7F) << bitpos;
219 bitpos = (uint8_t)(bitpos + 7);
220 } while (byte & 0x80);
226 bool checkreturn pb_skip_varint(pb_istream_t *stream)
231 if (!pb_read(stream, &byte, 1))
233 } while (byte & 0x80);
237 bool checkreturn pb_skip_string(pb_istream_t *stream)
240 if (!pb_decode_varint32(stream, &length))
243 return pb_read(stream, NULL, length);
246 bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
250 *wire_type = (pb_wire_type_t) 0;
253 if (!pb_decode_varint32(stream, &temp))
255 if (stream->bytes_left == 0)
263 *eof = true; /* Special feature: allow 0-terminated messages. */
268 *wire_type = (pb_wire_type_t)(temp & 7);
272 bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
276 case PB_WT_VARINT: return pb_skip_varint(stream);
277 case PB_WT_64BIT: return pb_read(stream, NULL, 8);
278 case PB_WT_STRING: return pb_skip_string(stream);
279 case PB_WT_32BIT: return pb_read(stream, NULL, 4);
280 default: PB_RETURN_ERROR(stream, "invalid wire_type");
284 /* Read a raw value to buffer, for the purpose of passing it to callback as
285 * a substream. Size is maximum size on call, and actual size on return.
287 static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, uint8_t *buf, size_t *size)
289 size_t max_size = *size;
297 if (*size > max_size) return false;
298 if (!pb_read(stream, buf, 1)) return false;
299 } while (*buf++ & 0x80);
304 return pb_read(stream, buf, 8);
308 return pb_read(stream, buf, 4);
310 default: PB_RETURN_ERROR(stream, "invalid wire_type");
314 /* Decode string length from stream and return a substream with limited length.
315 * Remember to close the substream using pb_close_string_substream().
317 bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
320 if (!pb_decode_varint32(stream, &size))
323 *substream = *stream;
324 if (substream->bytes_left < size)
325 PB_RETURN_ERROR(stream, "parent stream too short");
327 substream->bytes_left = size;
328 stream->bytes_left -= size;
332 void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
334 stream->state = substream->state;
337 stream->errmsg = substream->errmsg;
341 static void pb_field_init(pb_field_iterator_t *iter, const pb_field_t *fields, void *dest_struct)
343 iter->start = iter->pos = fields;
344 iter->field_index = 0;
345 iter->required_field_index = 0;
346 iter->pData = (char*)dest_struct + iter->pos->data_offset;
347 iter->pSize = (char*)iter->pData + iter->pos->size_offset;
348 iter->dest_struct = dest_struct;
351 static bool pb_field_next(pb_field_iterator_t *iter)
353 bool notwrapped = true;
354 size_t prev_size = iter->pos->data_size;
356 if (PB_ATYPE(iter->pos->type) == PB_ATYPE_STATIC &&
357 PB_HTYPE(iter->pos->type) == PB_HTYPE_REPEATED)
359 prev_size *= iter->pos->array_size;
361 else if (PB_ATYPE(iter->pos->type) == PB_ATYPE_POINTER)
363 prev_size = sizeof(void*);
366 if (iter->pos->tag == 0)
367 return false; /* Only happens with empty message types */
369 if (PB_HTYPE(iter->pos->type) == PB_HTYPE_REQUIRED)
370 iter->required_field_index++;
374 if (iter->pos->tag == 0)
376 iter->pos = iter->start;
377 iter->field_index = 0;
378 iter->required_field_index = 0;
379 iter->pData = iter->dest_struct;
384 iter->pData = (char*)iter->pData + prev_size + iter->pos->data_offset;
385 iter->pSize = (char*)iter->pData + iter->pos->size_offset;
389 static bool checkreturn pb_field_find(pb_field_iterator_t *iter, uint32_t tag)
391 unsigned start = iter->field_index;
394 if (iter->pos->tag == tag &&
395 PB_LTYPE(iter->pos->type) != PB_LTYPE_EXTENSION)
399 (void)pb_field_next(iter);
400 } while (iter->field_index != start);
405 /*************************
406 * Decode a single field *
407 *************************/
409 static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter)
414 type = iter->pos->type;
415 func = PB_DECODERS[PB_LTYPE(type)];
417 switch (PB_HTYPE(type))
419 case PB_HTYPE_REQUIRED:
420 return func(stream, iter->pos, iter->pData);
422 case PB_HTYPE_OPTIONAL:
423 *(bool*)iter->pSize = true;
424 return func(stream, iter->pos, iter->pData);
426 case PB_HTYPE_REPEATED:
427 if (wire_type == PB_WT_STRING
428 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
432 size_t *size = (size_t*)iter->pSize;
433 pb_istream_t substream;
434 if (!pb_make_string_substream(stream, &substream))
437 while (substream.bytes_left > 0 && *size < iter->pos->array_size)
439 void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
440 if (!func(&substream, iter->pos, pItem))
447 pb_close_string_substream(stream, &substream);
449 if (substream.bytes_left != 0)
450 PB_RETURN_ERROR(stream, "array overflow");
457 size_t *size = (size_t*)iter->pSize;
458 void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
459 if (*size >= iter->pos->array_size)
460 PB_RETURN_ERROR(stream, "array overflow");
463 return func(stream, iter->pos, pItem);
467 PB_RETURN_ERROR(stream, "invalid field type");
471 #ifdef PB_ENABLE_MALLOC
472 /* Allocate storage for the field and store the pointer at iter->pData.
473 * array_size is the number of entries to reserve in an array.
475 static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
477 void *ptr = *(void**)pData;
479 /* Check for multiplication overflows. */
481 if (data_size > 0 && array_size > 0)
483 /* Avoid the costly division if the sizes are small enough.
484 * Multiplication is safe as long as only half of bits are set
485 * in either multiplicand.
487 const size_t check_limit = (size_t)1 << (sizeof(size_t) * 4);
488 if (data_size >= check_limit || array_size >= check_limit)
490 const size_t size_max = (size_t)-1;
491 if (size_max / array_size < data_size)
493 PB_RETURN_ERROR(stream, "size too large");
497 size = array_size * data_size;
500 /* Allocate new or expand previous allocation */
501 /* Note: on failure the old pointer will remain in the structure,
502 * the message must be freed by caller also on error return. */
503 ptr = pb_realloc(ptr, size);
505 PB_RETURN_ERROR(stream, "realloc failed");
507 *(void**)pData = ptr;
511 /* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
512 static void initialize_pointer_field(void *pItem, pb_field_iterator_t *iter)
514 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_STRING ||
515 PB_LTYPE(iter->pos->type) == PB_LTYPE_BYTES)
517 *(void**)pItem = NULL;
519 else if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
521 pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, pItem);
526 static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter)
528 #ifndef PB_ENABLE_MALLOC
531 PB_RETURN_ERROR(stream, "no malloc support");
536 type = iter->pos->type;
537 func = PB_DECODERS[PB_LTYPE(type)];
539 switch (PB_HTYPE(type))
541 case PB_HTYPE_REQUIRED:
542 case PB_HTYPE_OPTIONAL:
543 if (PB_LTYPE(type) == PB_LTYPE_STRING ||
544 PB_LTYPE(type) == PB_LTYPE_BYTES)
546 return func(stream, iter->pos, iter->pData);
550 if (!allocate_field(stream, iter->pData, iter->pos->data_size, 1))
553 initialize_pointer_field(*(void**)iter->pData, iter);
554 return func(stream, iter->pos, *(void**)iter->pData);
557 case PB_HTYPE_REPEATED:
558 if (wire_type == PB_WT_STRING
559 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
561 /* Packed array, multiple items come in at once. */
563 size_t *size = (size_t*)iter->pSize;
564 size_t allocated_size = *size;
566 pb_istream_t substream;
568 if (!pb_make_string_substream(stream, &substream))
571 while (substream.bytes_left)
573 if (*size + 1 > allocated_size)
575 /* Allocate more storage. This tries to guess the
576 * number of remaining entries. Round the division
578 allocated_size += (substream.bytes_left - 1) / iter->pos->data_size + 1;
580 if (!allocate_field(&substream, iter->pData, iter->pos->data_size, allocated_size))
587 /* Decode the array entry */
588 pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size);
589 initialize_pointer_field(pItem, iter);
590 if (!func(&substream, iter->pos, pItem))
597 pb_close_string_substream(stream, &substream);
603 /* Normal repeated field, i.e. only one item at a time. */
604 size_t *size = (size_t*)iter->pSize;
608 if (!allocate_field(stream, iter->pData, iter->pos->data_size, *size))
611 pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size - 1);
612 initialize_pointer_field(pItem, iter);
613 return func(stream, iter->pos, pItem);
617 PB_RETURN_ERROR(stream, "invalid field type");
622 static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter)
624 pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
626 #ifdef PB_OLD_CALLBACK_STYLE
627 void *arg = pCallback->arg;
629 void **arg = &(pCallback->arg);
632 if (pCallback->funcs.decode == NULL)
633 return pb_skip_field(stream, wire_type);
635 if (wire_type == PB_WT_STRING)
637 pb_istream_t substream;
639 if (!pb_make_string_substream(stream, &substream))
644 if (!pCallback->funcs.decode(&substream, iter->pos, arg))
645 PB_RETURN_ERROR(stream, "callback failed");
646 } while (substream.bytes_left);
648 pb_close_string_substream(stream, &substream);
653 /* Copy the single scalar value to stack.
654 * This is required so that we can limit the stream length,
655 * which in turn allows to use same callback for packed and
656 * not-packed fields. */
657 pb_istream_t substream;
659 size_t size = sizeof(buffer);
661 if (!read_raw_value(stream, wire_type, buffer, &size))
663 substream = pb_istream_from_buffer(buffer, size);
665 return pCallback->funcs.decode(&substream, iter->pos, arg);
669 static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iterator_t *iter)
671 switch (PB_ATYPE(iter->pos->type))
673 case PB_ATYPE_STATIC:
674 return decode_static_field(stream, wire_type, iter);
676 case PB_ATYPE_POINTER:
677 return decode_pointer_field(stream, wire_type, iter);
679 case PB_ATYPE_CALLBACK:
680 return decode_callback_field(stream, wire_type, iter);
683 PB_RETURN_ERROR(stream, "invalid field type");
687 /* Default handler for extension fields. Expects a pb_field_t structure
688 * in extension->type->arg. */
689 static bool checkreturn default_extension_decoder(pb_istream_t *stream,
690 pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
692 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
693 pb_field_iterator_t iter;
695 if (field->tag != tag)
700 iter.field_index = 0;
701 iter.required_field_index = 0;
702 iter.dest_struct = extension->dest;
703 iter.pData = extension->dest;
704 iter.pSize = &extension->found;
706 return decode_field(stream, wire_type, &iter);
709 /* Try to decode an unknown field as an extension field. Tries each extension
710 * decoder in turn, until one of them handles the field or loop ends. */
711 static bool checkreturn decode_extension(pb_istream_t *stream,
712 uint32_t tag, pb_wire_type_t wire_type, pb_field_iterator_t *iter)
714 pb_extension_t *extension = *(pb_extension_t* const *)iter->pData;
715 size_t pos = stream->bytes_left;
717 while (extension != NULL && pos == stream->bytes_left)
720 if (extension->type->decode)
721 status = extension->type->decode(stream, extension, tag, wire_type);
723 status = default_extension_decoder(stream, extension, tag, wire_type);
728 extension = extension->next;
734 /* Step through the iterator until an extension field is found or until all
735 * entries have been checked. There can be only one extension field per
736 * message. Returns false if no extension field is found. */
737 static bool checkreturn find_extension_field(pb_field_iterator_t *iter)
739 unsigned start = iter->field_index;
742 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_EXTENSION)
744 (void)pb_field_next(iter);
745 } while (iter->field_index != start);
750 /* Initialize message fields to default values, recursively */
751 static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
753 pb_field_iterator_t iter;
754 pb_field_init(&iter, fields, dest_struct);
759 type = iter.pos->type;
761 /* Avoid crash on empty message types (zero fields) */
762 if (iter.pos->tag == 0)
765 if (PB_ATYPE(type) == PB_ATYPE_STATIC)
767 if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL)
769 /* Set has_field to false. Still initialize the optional field
771 *(bool*)iter.pSize = false;
773 else if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
775 /* Set array count to 0, no need to initialize contents. */
776 *(size_t*)iter.pSize = 0;
780 if (PB_LTYPE(iter.pos->type) == PB_LTYPE_SUBMESSAGE)
782 /* Initialize submessage to defaults */
783 pb_message_set_to_defaults((const pb_field_t *) iter.pos->ptr, iter.pData);
785 else if (iter.pos->ptr != NULL)
787 /* Initialize to default value */
788 memcpy(iter.pData, iter.pos->ptr, iter.pos->data_size);
792 /* Initialize to zeros */
793 memset(iter.pData, 0, iter.pos->data_size);
796 else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
798 /* Initialize the pointer to NULL. */
799 *(void**)iter.pData = NULL;
801 /* Initialize array count to 0. */
802 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
804 *(size_t*)iter.pSize = 0;
807 else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
809 /* Don't overwrite callback */
811 } while (pb_field_next(&iter));
814 /*********************
815 * Decode all fields *
816 *********************/
818 bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
820 uint8_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 7) / 8] = {0, 0, 0, 0, 0, 0, 0, 0};
821 uint32_t extension_range_start = 0;
822 pb_field_iterator_t iter;
824 pb_field_init(&iter, fields, dest_struct);
826 while (stream->bytes_left)
829 pb_wire_type_t wire_type;
832 if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
840 if (!pb_field_find(&iter, tag))
842 /* No match found, check if it matches an extension. */
843 if (tag >= extension_range_start)
845 if (!find_extension_field(&iter))
846 extension_range_start = (uint32_t)-1;
848 extension_range_start = iter.pos->tag;
850 if (tag >= extension_range_start)
852 size_t pos = stream->bytes_left;
854 if (!decode_extension(stream, tag, wire_type, &iter))
857 if (pos != stream->bytes_left)
859 /* The field was handled */
865 /* No match found, skip data */
866 if (!pb_skip_field(stream, wire_type))
871 if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REQUIRED
872 && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
874 fields_seen[iter.required_field_index >> 3] |= (uint8_t)(1 << (iter.required_field_index & 7));
877 if (!decode_field(stream, wire_type, &iter))
881 /* Check that all required fields were present. */
883 /* First figure out the number of required fields by
884 * seeking to the end of the field array. Usually we
885 * are already close to end after decoding.
887 unsigned req_field_count;
891 req_field_count = iter.required_field_index;
892 last_type = iter.pos->type;
893 } while (pb_field_next(&iter));
895 /* Fixup if last field was also required. */
896 if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.pos->tag != 0)
899 /* Check the whole bytes */
900 for (i = 0; i < (req_field_count >> 3); i++)
902 if (fields_seen[i] != 0xFF)
903 PB_RETURN_ERROR(stream, "missing required field");
906 /* Check the remaining bits */
907 if (fields_seen[req_field_count >> 3] != (0xFF >> (8 - (req_field_count & 7))))
908 PB_RETURN_ERROR(stream, "missing required field");
914 bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
917 pb_message_set_to_defaults(fields, dest_struct);
918 status = pb_decode_noinit(stream, fields, dest_struct);
920 #ifdef PB_ENABLE_MALLOC
922 pb_release(fields, dest_struct);
928 bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
930 pb_istream_t substream;
933 if (!pb_make_string_substream(stream, &substream))
936 status = pb_decode(&substream, fields, dest_struct);
937 pb_close_string_substream(stream, &substream);
941 #ifdef PB_ENABLE_MALLOC
942 void pb_release(const pb_field_t fields[], void *dest_struct)
944 pb_field_iterator_t iter;
945 pb_field_init(&iter, fields, dest_struct);
950 type = iter.pos->type;
952 /* Avoid crash on empty message types (zero fields) */
953 if (iter.pos->tag == 0)
956 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
958 if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
959 (PB_LTYPE(type) == PB_LTYPE_STRING ||
960 PB_LTYPE(type) == PB_LTYPE_BYTES))
962 /* Release entries in repeated string or bytes array */
963 void **pItem = *(void***)iter.pData;
964 size_t count = *(size_t*)iter.pSize;
971 else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
973 /* Release fields in submessages */
974 void *pItem = *(void**)iter.pData;
975 size_t count = (pItem ? 1 : 0);
977 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
979 count = *(size_t*)iter.pSize;
984 pb_release((const pb_field_t*)iter.pos->ptr, pItem);
985 pItem = (uint8_t*)pItem + iter.pos->data_size;
989 /* Release main item */
990 pb_free(*(void**)iter.pData);
991 *(void**)iter.pData = NULL;
993 } while (pb_field_next(&iter));
999 bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
1002 if (!pb_decode_varint(stream, &value))
1006 *dest = (int64_t)(~(value >> 1));
1008 *dest = (int64_t)(value >> 1);
1013 bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
1015 #ifdef __BIG_ENDIAN__
1016 uint8_t *bytes = (uint8_t*)dest;
1019 if (!pb_read(stream, lebytes, 4))
1022 bytes[0] = lebytes[3];
1023 bytes[1] = lebytes[2];
1024 bytes[2] = lebytes[1];
1025 bytes[3] = lebytes[0];
1028 return pb_read(stream, (uint8_t*)dest, 4);
1032 bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
1034 #ifdef __BIG_ENDIAN__
1035 uint8_t *bytes = (uint8_t*)dest;
1038 if (!pb_read(stream, lebytes, 8))
1041 bytes[0] = lebytes[7];
1042 bytes[1] = lebytes[6];
1043 bytes[2] = lebytes[5];
1044 bytes[3] = lebytes[4];
1045 bytes[4] = lebytes[3];
1046 bytes[5] = lebytes[2];
1047 bytes[6] = lebytes[1];
1048 bytes[7] = lebytes[0];
1051 return pb_read(stream, (uint8_t*)dest, 8);
1055 static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1058 if (!pb_decode_varint(stream, &value))
1061 switch (field->data_size)
1063 case 1: *(int8_t*)dest = (int8_t)value; break;
1064 case 2: *(int16_t*)dest = (int16_t)value; break;
1065 case 4: *(int32_t*)dest = (int32_t)value; break;
1066 case 8: *(int64_t*)dest = (int64_t)value; break;
1067 default: PB_RETURN_ERROR(stream, "invalid data_size");
1073 static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1076 if (!pb_decode_varint(stream, &value))
1079 switch (field->data_size)
1081 case 4: *(uint32_t*)dest = (uint32_t)value; break;
1082 case 8: *(uint64_t*)dest = value; break;
1083 default: PB_RETURN_ERROR(stream, "invalid data_size");
1089 static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1092 if (!pb_decode_svarint(stream, &value))
1095 switch (field->data_size)
1097 case 4: *(int32_t*)dest = (int32_t)value; break;
1098 case 8: *(int64_t*)dest = value; break;
1099 default: PB_RETURN_ERROR(stream, "invalid data_size");
1105 static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
1108 return pb_decode_fixed32(stream, dest);
1111 static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
1114 return pb_decode_fixed64(stream, dest);
1117 static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
1120 pb_bytes_array_t *bdest;
1122 if (!pb_decode_varint32(stream, &size))
1125 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1127 #ifndef PB_ENABLE_MALLOC
1128 PB_RETURN_ERROR(stream, "no malloc support");
1130 if (!allocate_field(stream, dest, PB_BYTES_ARRAY_T_ALLOCSIZE(size), 1))
1132 bdest = *(pb_bytes_array_t**)dest;
1137 if (PB_BYTES_ARRAY_T_ALLOCSIZE(size) > field->data_size)
1138 PB_RETURN_ERROR(stream, "bytes overflow");
1139 bdest = (pb_bytes_array_t*)dest;
1143 return pb_read(stream, bdest->bytes, size);
1146 static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
1151 if (!pb_decode_varint32(stream, &size))
1154 /* Space for null terminator */
1155 alloc_size = size + 1;
1157 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1159 #ifndef PB_ENABLE_MALLOC
1160 PB_RETURN_ERROR(stream, "no malloc support");
1162 if (!allocate_field(stream, dest, alloc_size, 1))
1164 dest = *(void**)dest;
1169 if (alloc_size > field->data_size)
1170 PB_RETURN_ERROR(stream, "string overflow");
1173 status = pb_read(stream, (uint8_t*)dest, size);
1174 *((uint8_t*)dest + size) = 0;
1178 static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
1181 pb_istream_t substream;
1182 const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr;
1184 if (!pb_make_string_substream(stream, &substream))
1187 if (field->ptr == NULL)
1188 PB_RETURN_ERROR(stream, "invalid field descriptor");
1190 /* New array entries need to be initialized, while required and optional
1191 * submessages have already been initialized in the top-level pb_decode. */
1192 if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
1193 status = pb_decode(&substream, submsg_fields, dest);
1195 status = pb_decode_noinit(&substream, submsg_fields, dest);
1197 pb_close_string_substream(stream, &substream);