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 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_message_set_to_defaults(const pb_field_t fields[], void *dest_struct);
36 static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest);
37 static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
38 static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
39 static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest);
40 static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest);
41 static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest);
42 static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest);
43 static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest);
44 static bool checkreturn pb_skip_varint(pb_istream_t *stream);
45 static bool checkreturn pb_skip_string(pb_istream_t *stream);
47 #ifdef PB_ENABLE_MALLOC
48 static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size);
49 static void pb_release_single_field(const pb_field_iter_t *iter);
52 /* --- Function pointers to field decoders ---
53 * Order in the array must match pb_action_t LTYPE numbering.
55 static const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = {
68 /*******************************
69 * pb_istream_t implementation *
70 *******************************/
72 static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count)
74 uint8_t *source = (uint8_t*)stream->state;
75 stream->state = source + count;
86 bool checkreturn pb_read(pb_istream_t *stream, uint8_t *buf, size_t count)
88 #ifndef PB_BUFFER_ONLY
89 if (buf == NULL && stream->callback != buf_read)
91 /* Skip input bytes */
95 if (!pb_read(stream, tmp, 16))
101 return pb_read(stream, tmp, count);
105 if (stream->bytes_left < count)
106 PB_RETURN_ERROR(stream, "end-of-stream");
108 #ifndef PB_BUFFER_ONLY
109 if (!stream->callback(stream, buf, count))
110 PB_RETURN_ERROR(stream, "io error");
112 if (!buf_read(stream, buf, count))
116 stream->bytes_left -= count;
120 /* Read a single byte from input stream. buf may not be NULL.
121 * This is an optimization for the varint decoding. */
122 static bool checkreturn pb_readbyte(pb_istream_t *stream, uint8_t *buf)
124 if (stream->bytes_left == 0)
125 PB_RETURN_ERROR(stream, "end-of-stream");
127 #ifndef PB_BUFFER_ONLY
128 if (!stream->callback(stream, buf, 1))
129 PB_RETURN_ERROR(stream, "io error");
131 *buf = *(uint8_t*)stream->state;
132 stream->state = (uint8_t*)stream->state + 1;
135 stream->bytes_left--;
140 pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize)
143 #ifdef PB_BUFFER_ONLY
144 stream.callback = NULL;
146 stream.callback = &buf_read;
149 stream.bytes_left = bufsize;
151 stream.errmsg = NULL;
156 /********************
158 ********************/
160 static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
165 if (!pb_readbyte(stream, &byte))
168 if ((byte & 0x80) == 0)
170 /* Quick case, 1 byte value */
177 result = byte & 0x7F;
182 PB_RETURN_ERROR(stream, "varint overflow");
184 if (!pb_readbyte(stream, &byte))
187 result |= (uint32_t)(byte & 0x7F) << bitpos;
188 bitpos = (uint8_t)(bitpos + 7);
189 } while (byte & 0x80);
196 bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
205 PB_RETURN_ERROR(stream, "varint overflow");
207 if (!pb_readbyte(stream, &byte))
210 result |= (uint64_t)(byte & 0x7F) << bitpos;
211 bitpos = (uint8_t)(bitpos + 7);
212 } while (byte & 0x80);
218 bool checkreturn pb_skip_varint(pb_istream_t *stream)
223 if (!pb_read(stream, &byte, 1))
225 } while (byte & 0x80);
229 bool checkreturn pb_skip_string(pb_istream_t *stream)
232 if (!pb_decode_varint32(stream, &length))
235 return pb_read(stream, NULL, length);
238 bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
242 *wire_type = (pb_wire_type_t) 0;
245 if (!pb_decode_varint32(stream, &temp))
247 if (stream->bytes_left == 0)
255 *eof = true; /* Special feature: allow 0-terminated messages. */
260 *wire_type = (pb_wire_type_t)(temp & 7);
264 bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
268 case PB_WT_VARINT: return pb_skip_varint(stream);
269 case PB_WT_64BIT: return pb_read(stream, NULL, 8);
270 case PB_WT_STRING: return pb_skip_string(stream);
271 case PB_WT_32BIT: return pb_read(stream, NULL, 4);
272 default: PB_RETURN_ERROR(stream, "invalid wire_type");
276 /* Read a raw value to buffer, for the purpose of passing it to callback as
277 * a substream. Size is maximum size on call, and actual size on return.
279 static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, uint8_t *buf, size_t *size)
281 size_t max_size = *size;
289 if (*size > max_size) return false;
290 if (!pb_read(stream, buf, 1)) return false;
291 } while (*buf++ & 0x80);
296 return pb_read(stream, buf, 8);
300 return pb_read(stream, buf, 4);
302 default: PB_RETURN_ERROR(stream, "invalid wire_type");
306 /* Decode string length from stream and return a substream with limited length.
307 * Remember to close the substream using pb_close_string_substream().
309 bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
312 if (!pb_decode_varint32(stream, &size))
315 *substream = *stream;
316 if (substream->bytes_left < size)
317 PB_RETURN_ERROR(stream, "parent stream too short");
319 substream->bytes_left = size;
320 stream->bytes_left -= size;
324 void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
326 stream->state = substream->state;
329 stream->errmsg = substream->errmsg;
333 /*************************
334 * Decode a single field *
335 *************************/
337 static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
342 type = iter->pos->type;
343 func = PB_DECODERS[PB_LTYPE(type)];
345 switch (PB_HTYPE(type))
347 case PB_HTYPE_REQUIRED:
348 return func(stream, iter->pos, iter->pData);
350 case PB_HTYPE_OPTIONAL:
351 *(bool*)iter->pSize = true;
352 return func(stream, iter->pos, iter->pData);
354 case PB_HTYPE_REPEATED:
355 if (wire_type == PB_WT_STRING
356 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
360 pb_size_t *size = (pb_size_t*)iter->pSize;
361 pb_istream_t substream;
362 if (!pb_make_string_substream(stream, &substream))
365 while (substream.bytes_left > 0 && *size < iter->pos->array_size)
367 void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
368 if (!func(&substream, iter->pos, pItem))
375 pb_close_string_substream(stream, &substream);
377 if (substream.bytes_left != 0)
378 PB_RETURN_ERROR(stream, "array overflow");
385 pb_size_t *size = (pb_size_t*)iter->pSize;
386 void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
387 if (*size >= iter->pos->array_size)
388 PB_RETURN_ERROR(stream, "array overflow");
391 return func(stream, iter->pos, pItem);
395 PB_RETURN_ERROR(stream, "invalid field type");
399 #ifdef PB_ENABLE_MALLOC
400 /* Allocate storage for the field and store the pointer at iter->pData.
401 * array_size is the number of entries to reserve in an array.
402 * Zero size is not allowed, use pb_free() for releasing.
404 static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
406 void *ptr = *(void**)pData;
408 if (data_size == 0 || array_size == 0)
409 PB_RETURN_ERROR(stream, "invalid size");
411 /* Check for multiplication overflows.
412 * This code avoids the costly division if the sizes are small enough.
413 * Multiplication is safe as long as only half of bits are set
414 * in either multiplicand.
417 const size_t check_limit = (size_t)1 << (sizeof(size_t) * 4);
418 if (data_size >= check_limit || array_size >= check_limit)
420 const size_t size_max = (size_t)-1;
421 if (size_max / array_size < data_size)
423 PB_RETURN_ERROR(stream, "size too large");
428 /* Allocate new or expand previous allocation */
429 /* Note: on failure the old pointer will remain in the structure,
430 * the message must be freed by caller also on error return. */
431 ptr = pb_realloc(ptr, array_size * data_size);
433 PB_RETURN_ERROR(stream, "realloc failed");
435 *(void**)pData = ptr;
439 /* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
440 static void initialize_pointer_field(void *pItem, pb_field_iter_t *iter)
442 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_STRING ||
443 PB_LTYPE(iter->pos->type) == PB_LTYPE_BYTES)
445 *(void**)pItem = NULL;
447 else if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
449 pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, pItem);
454 static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
456 #ifndef PB_ENABLE_MALLOC
457 PB_UNUSED(wire_type);
459 PB_RETURN_ERROR(stream, "no malloc support");
464 type = iter->pos->type;
465 func = PB_DECODERS[PB_LTYPE(type)];
467 switch (PB_HTYPE(type))
469 case PB_HTYPE_REQUIRED:
470 case PB_HTYPE_OPTIONAL:
471 if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE &&
472 *(void**)iter->pData != NULL)
474 /* Duplicate field, have to release the old allocation first. */
475 pb_release_single_field(iter);
478 if (PB_LTYPE(type) == PB_LTYPE_STRING ||
479 PB_LTYPE(type) == PB_LTYPE_BYTES)
481 return func(stream, iter->pos, iter->pData);
485 if (!allocate_field(stream, iter->pData, iter->pos->data_size, 1))
488 initialize_pointer_field(*(void**)iter->pData, iter);
489 return func(stream, iter->pos, *(void**)iter->pData);
492 case PB_HTYPE_REPEATED:
493 if (wire_type == PB_WT_STRING
494 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
496 /* Packed array, multiple items come in at once. */
498 pb_size_t *size = (pb_size_t*)iter->pSize;
499 size_t allocated_size = *size;
501 pb_istream_t substream;
503 if (!pb_make_string_substream(stream, &substream))
506 while (substream.bytes_left)
508 if ((size_t)*size + 1 > allocated_size)
510 /* Allocate more storage. This tries to guess the
511 * number of remaining entries. Round the division
513 allocated_size += (substream.bytes_left - 1) / iter->pos->data_size + 1;
515 if (!allocate_field(&substream, iter->pData, iter->pos->data_size, allocated_size))
522 /* Decode the array entry */
523 pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size);
524 initialize_pointer_field(pItem, iter);
525 if (!func(&substream, iter->pos, pItem))
531 if (*size == PB_SIZE_MAX)
534 stream->errmsg = "too many array entries";
542 pb_close_string_substream(stream, &substream);
548 /* Normal repeated field, i.e. only one item at a time. */
549 pb_size_t *size = (pb_size_t*)iter->pSize;
552 if (*size == PB_SIZE_MAX)
553 PB_RETURN_ERROR(stream, "too many array entries");
556 if (!allocate_field(stream, iter->pData, iter->pos->data_size, *size))
559 pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size - 1);
560 initialize_pointer_field(pItem, iter);
561 return func(stream, iter->pos, pItem);
565 PB_RETURN_ERROR(stream, "invalid field type");
570 static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
572 pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
574 #ifdef PB_OLD_CALLBACK_STYLE
575 void *arg = pCallback->arg;
577 void **arg = &(pCallback->arg);
580 if (pCallback->funcs.decode == NULL)
581 return pb_skip_field(stream, wire_type);
583 if (wire_type == PB_WT_STRING)
585 pb_istream_t substream;
587 if (!pb_make_string_substream(stream, &substream))
592 if (!pCallback->funcs.decode(&substream, iter->pos, arg))
593 PB_RETURN_ERROR(stream, "callback failed");
594 } while (substream.bytes_left);
596 pb_close_string_substream(stream, &substream);
601 /* Copy the single scalar value to stack.
602 * This is required so that we can limit the stream length,
603 * which in turn allows to use same callback for packed and
604 * not-packed fields. */
605 pb_istream_t substream;
607 size_t size = sizeof(buffer);
609 if (!read_raw_value(stream, wire_type, buffer, &size))
611 substream = pb_istream_from_buffer(buffer, size);
613 return pCallback->funcs.decode(&substream, iter->pos, arg);
617 static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
619 switch (PB_ATYPE(iter->pos->type))
621 case PB_ATYPE_STATIC:
622 return decode_static_field(stream, wire_type, iter);
624 case PB_ATYPE_POINTER:
625 return decode_pointer_field(stream, wire_type, iter);
627 case PB_ATYPE_CALLBACK:
628 return decode_callback_field(stream, wire_type, iter);
631 PB_RETURN_ERROR(stream, "invalid field type");
635 /* Default handler for extension fields. Expects a pb_field_t structure
636 * in extension->type->arg. */
637 static bool checkreturn default_extension_decoder(pb_istream_t *stream,
638 pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
640 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
641 pb_field_iter_t iter;
643 if (field->tag != tag)
646 /* Fake a field iterator for the extension field.
647 * It is not actually safe to advance this iterator, but decode_field
648 * will not even try to. */
649 (void)pb_field_iter_begin(&iter, field, extension->dest);
650 iter.pData = extension->dest;
651 iter.pSize = &extension->found;
653 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
655 /* For pointer extensions, the pointer is stored directly
656 * in the extension structure. This avoids having an extra
658 iter.pData = &extension->dest;
661 return decode_field(stream, wire_type, &iter);
664 /* Try to decode an unknown field as an extension field. Tries each extension
665 * decoder in turn, until one of them handles the field or loop ends. */
666 static bool checkreturn decode_extension(pb_istream_t *stream,
667 uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter)
669 pb_extension_t *extension = *(pb_extension_t* const *)iter->pData;
670 size_t pos = stream->bytes_left;
672 while (extension != NULL && pos == stream->bytes_left)
675 if (extension->type->decode)
676 status = extension->type->decode(stream, extension, tag, wire_type);
678 status = default_extension_decoder(stream, extension, tag, wire_type);
683 extension = extension->next;
689 /* Step through the iterator until an extension field is found or until all
690 * entries have been checked. There can be only one extension field per
691 * message. Returns false if no extension field is found. */
692 static bool checkreturn find_extension_field(pb_field_iter_t *iter)
694 const pb_field_t *start = iter->pos;
697 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_EXTENSION)
699 (void)pb_field_iter_next(iter);
700 } while (iter->pos != start);
705 /* Initialize message fields to default values, recursively */
706 static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
708 pb_field_iter_t iter;
710 if (!pb_field_iter_begin(&iter, fields, dest_struct))
711 return; /* Empty message type */
716 type = iter.pos->type;
718 if (PB_ATYPE(type) == PB_ATYPE_STATIC)
720 if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL)
722 /* Set has_field to false. Still initialize the optional field
724 *(bool*)iter.pSize = false;
726 else if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
728 /* Set array count to 0, no need to initialize contents. */
729 *(pb_size_t*)iter.pSize = 0;
733 if (PB_LTYPE(iter.pos->type) == PB_LTYPE_SUBMESSAGE)
735 /* Initialize submessage to defaults */
736 pb_message_set_to_defaults((const pb_field_t *) iter.pos->ptr, iter.pData);
738 else if (iter.pos->ptr != NULL)
740 /* Initialize to default value */
741 memcpy(iter.pData, iter.pos->ptr, iter.pos->data_size);
745 /* Initialize to zeros */
746 memset(iter.pData, 0, iter.pos->data_size);
749 else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
751 /* Initialize the pointer to NULL. */
752 *(void**)iter.pData = NULL;
754 /* Initialize array count to 0. */
755 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
757 *(pb_size_t*)iter.pSize = 0;
760 else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
762 /* Don't overwrite callback */
764 } while (pb_field_iter_next(&iter));
767 /*********************
768 * Decode all fields *
769 *********************/
771 bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
773 uint8_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 7) / 8] = {0, 0, 0, 0, 0, 0, 0, 0};
774 uint32_t extension_range_start = 0;
775 pb_field_iter_t iter;
777 /* Return value ignored, as empty message types will be correctly handled by
778 * pb_field_iter_find() anyway. */
779 (void)pb_field_iter_begin(&iter, fields, dest_struct);
781 while (stream->bytes_left)
784 pb_wire_type_t wire_type;
787 if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
795 if (!pb_field_iter_find(&iter, tag))
797 /* No match found, check if it matches an extension. */
798 if (tag >= extension_range_start)
800 if (!find_extension_field(&iter))
801 extension_range_start = (uint32_t)-1;
803 extension_range_start = iter.pos->tag;
805 if (tag >= extension_range_start)
807 size_t pos = stream->bytes_left;
809 if (!decode_extension(stream, tag, wire_type, &iter))
812 if (pos != stream->bytes_left)
814 /* The field was handled */
820 /* No match found, skip data */
821 if (!pb_skip_field(stream, wire_type))
826 if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REQUIRED
827 && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
829 fields_seen[iter.required_field_index >> 3] |= (uint8_t)(1 << (iter.required_field_index & 7));
832 if (!decode_field(stream, wire_type, &iter))
836 /* Check that all required fields were present. */
838 /* First figure out the number of required fields by
839 * seeking to the end of the field array. Usually we
840 * are already close to end after decoding.
842 unsigned req_field_count;
846 req_field_count = iter.required_field_index;
847 last_type = iter.pos->type;
848 } while (pb_field_iter_next(&iter));
850 /* Fixup if last field was also required. */
851 if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.pos->tag != 0)
854 /* Check the whole bytes */
855 for (i = 0; i < (req_field_count >> 3); i++)
857 if (fields_seen[i] != 0xFF)
858 PB_RETURN_ERROR(stream, "missing required field");
861 /* Check the remaining bits */
862 if (fields_seen[req_field_count >> 3] != (0xFF >> (8 - (req_field_count & 7))))
863 PB_RETURN_ERROR(stream, "missing required field");
869 bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
872 pb_message_set_to_defaults(fields, dest_struct);
873 status = pb_decode_noinit(stream, fields, dest_struct);
875 #ifdef PB_ENABLE_MALLOC
877 pb_release(fields, dest_struct);
883 bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
885 pb_istream_t substream;
888 if (!pb_make_string_substream(stream, &substream))
891 status = pb_decode(&substream, fields, dest_struct);
892 pb_close_string_substream(stream, &substream);
896 #ifdef PB_ENABLE_MALLOC
897 static void pb_release_single_field(const pb_field_iter_t *iter)
900 type = iter->pos->type;
902 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
904 if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
905 (PB_LTYPE(type) == PB_LTYPE_STRING ||
906 PB_LTYPE(type) == PB_LTYPE_BYTES))
908 /* Release entries in repeated string or bytes array */
909 void **pItem = *(void***)iter->pData;
910 pb_size_t count = *(pb_size_t*)iter->pSize;
916 *(pb_size_t*)iter->pSize = 0;
918 else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
920 /* Release fields in submessages */
921 void *pItem = *(void**)iter->pData;
926 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
928 count = *(pb_size_t*)iter->pSize;
929 *(pb_size_t*)iter->pSize = 0;
934 pb_release((const pb_field_t*)iter->pos->ptr, pItem);
935 pItem = (uint8_t*)pItem + iter->pos->data_size;
940 /* Release main item */
941 pb_free(*(void**)iter->pData);
942 *(void**)iter->pData = NULL;
946 void pb_release(const pb_field_t fields[], void *dest_struct)
948 pb_field_iter_t iter;
950 if (!pb_field_iter_begin(&iter, fields, dest_struct))
951 return; /* Empty message type */
955 pb_release_single_field(&iter);
956 } while (pb_field_iter_next(&iter));
962 bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
965 if (!pb_decode_varint(stream, &value))
969 *dest = (int64_t)(~(value >> 1));
971 *dest = (int64_t)(value >> 1);
976 bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
978 #ifdef __BIG_ENDIAN__
979 uint8_t *bytes = (uint8_t*)dest;
982 if (!pb_read(stream, lebytes, 4))
985 bytes[0] = lebytes[3];
986 bytes[1] = lebytes[2];
987 bytes[2] = lebytes[1];
988 bytes[3] = lebytes[0];
991 return pb_read(stream, (uint8_t*)dest, 4);
995 bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
997 #ifdef __BIG_ENDIAN__
998 uint8_t *bytes = (uint8_t*)dest;
1001 if (!pb_read(stream, lebytes, 8))
1004 bytes[0] = lebytes[7];
1005 bytes[1] = lebytes[6];
1006 bytes[2] = lebytes[5];
1007 bytes[3] = lebytes[4];
1008 bytes[4] = lebytes[3];
1009 bytes[5] = lebytes[2];
1010 bytes[6] = lebytes[1];
1011 bytes[7] = lebytes[0];
1014 return pb_read(stream, (uint8_t*)dest, 8);
1018 static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1021 if (!pb_decode_varint(stream, &value))
1024 switch (field->data_size)
1026 case 1: *(int8_t*)dest = (int8_t)value; break;
1027 case 2: *(int16_t*)dest = (int16_t)value; break;
1028 case 4: *(int32_t*)dest = (int32_t)value; break;
1029 case 8: *(int64_t*)dest = (int64_t)value; break;
1030 default: PB_RETURN_ERROR(stream, "invalid data_size");
1036 static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1039 if (!pb_decode_varint(stream, &value))
1042 switch (field->data_size)
1044 case 4: *(uint32_t*)dest = (uint32_t)value; break;
1045 case 8: *(uint64_t*)dest = value; break;
1046 default: PB_RETURN_ERROR(stream, "invalid data_size");
1052 static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1055 if (!pb_decode_svarint(stream, &value))
1058 switch (field->data_size)
1060 case 4: *(int32_t*)dest = (int32_t)value; break;
1061 case 8: *(int64_t*)dest = value; break;
1062 default: PB_RETURN_ERROR(stream, "invalid data_size");
1068 static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
1071 return pb_decode_fixed32(stream, dest);
1074 static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
1077 return pb_decode_fixed64(stream, dest);
1080 static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
1084 pb_bytes_array_t *bdest;
1086 if (!pb_decode_varint32(stream, &size))
1089 if (size > PB_SIZE_MAX)
1090 PB_RETURN_ERROR(stream, "bytes overflow");
1092 alloc_size = PB_BYTES_ARRAY_T_ALLOCSIZE(size);
1093 if (size > alloc_size)
1094 PB_RETURN_ERROR(stream, "size too large");
1096 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1098 #ifndef PB_ENABLE_MALLOC
1099 PB_RETURN_ERROR(stream, "no malloc support");
1101 if (!allocate_field(stream, dest, alloc_size, 1))
1103 bdest = *(pb_bytes_array_t**)dest;
1108 if (alloc_size > field->data_size)
1109 PB_RETURN_ERROR(stream, "bytes overflow");
1110 bdest = (pb_bytes_array_t*)dest;
1113 bdest->size = (pb_size_t)size;
1114 return pb_read(stream, bdest->bytes, size);
1117 static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
1122 if (!pb_decode_varint32(stream, &size))
1125 /* Space for null terminator */
1126 alloc_size = size + 1;
1128 if (alloc_size < size)
1129 PB_RETURN_ERROR(stream, "size too large");
1131 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1133 #ifndef PB_ENABLE_MALLOC
1134 PB_RETURN_ERROR(stream, "no malloc support");
1136 if (!allocate_field(stream, dest, alloc_size, 1))
1138 dest = *(void**)dest;
1143 if (alloc_size > field->data_size)
1144 PB_RETURN_ERROR(stream, "string overflow");
1147 status = pb_read(stream, (uint8_t*)dest, size);
1148 *((uint8_t*)dest + size) = 0;
1152 static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
1155 pb_istream_t substream;
1156 const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr;
1158 if (!pb_make_string_substream(stream, &substream))
1161 if (field->ptr == NULL)
1162 PB_RETURN_ERROR(stream, "invalid field descriptor");
1164 /* New array entries need to be initialized, while required and optional
1165 * submessages have already been initialized in the top-level pb_decode. */
1166 if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
1167 status = pb_decode(&substream, submsg_fields, dest);
1169 status = pb_decode_noinit(&substream, submsg_fields, dest);
1171 pb_close_string_substream(stream, &substream);