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 /* --- Function pointers to field decoders ---
48 * Order in the array must match pb_action_t LTYPE numbering.
50 static const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = {
63 /*******************************
64 * pb_istream_t implementation *
65 *******************************/
67 static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count)
69 uint8_t *source = (uint8_t*)stream->state;
70 stream->state = source + count;
81 bool checkreturn pb_read(pb_istream_t *stream, uint8_t *buf, size_t count)
83 #ifndef PB_BUFFER_ONLY
84 if (buf == NULL && stream->callback != buf_read)
86 /* Skip input bytes */
90 if (!pb_read(stream, tmp, 16))
96 return pb_read(stream, tmp, count);
100 if (stream->bytes_left < count)
101 PB_RETURN_ERROR(stream, "end-of-stream");
103 #ifndef PB_BUFFER_ONLY
104 if (!stream->callback(stream, buf, count))
105 PB_RETURN_ERROR(stream, "io error");
107 if (!buf_read(stream, buf, count))
111 stream->bytes_left -= count;
115 /* Read a single byte from input stream. buf may not be NULL.
116 * This is an optimization for the varint decoding. */
117 static bool checkreturn pb_readbyte(pb_istream_t *stream, uint8_t *buf)
119 if (stream->bytes_left == 0)
120 PB_RETURN_ERROR(stream, "end-of-stream");
122 #ifndef PB_BUFFER_ONLY
123 if (!stream->callback(stream, buf, 1))
124 PB_RETURN_ERROR(stream, "io error");
126 *buf = *(uint8_t*)stream->state;
127 stream->state = (uint8_t*)stream->state + 1;
130 stream->bytes_left--;
135 pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize)
138 #ifdef PB_BUFFER_ONLY
139 stream.callback = NULL;
141 stream.callback = &buf_read;
144 stream.bytes_left = bufsize;
146 stream.errmsg = NULL;
151 /********************
153 ********************/
155 static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
160 if (!pb_readbyte(stream, &byte))
163 if ((byte & 0x80) == 0)
165 /* Quick case, 1 byte value */
172 result = byte & 0x7F;
177 PB_RETURN_ERROR(stream, "varint overflow");
179 if (!pb_readbyte(stream, &byte))
182 result |= (uint32_t)(byte & 0x7F) << bitpos;
183 bitpos = (uint8_t)(bitpos + 7);
184 } while (byte & 0x80);
191 bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
200 PB_RETURN_ERROR(stream, "varint overflow");
202 if (!pb_readbyte(stream, &byte))
205 result |= (uint64_t)(byte & 0x7F) << bitpos;
206 bitpos = (uint8_t)(bitpos + 7);
207 } while (byte & 0x80);
213 bool checkreturn pb_skip_varint(pb_istream_t *stream)
218 if (!pb_read(stream, &byte, 1))
220 } while (byte & 0x80);
224 bool checkreturn pb_skip_string(pb_istream_t *stream)
227 if (!pb_decode_varint32(stream, &length))
230 return pb_read(stream, NULL, length);
233 bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
237 *wire_type = (pb_wire_type_t) 0;
240 if (!pb_decode_varint32(stream, &temp))
242 if (stream->bytes_left == 0)
250 *eof = true; /* Special feature: allow 0-terminated messages. */
255 *wire_type = (pb_wire_type_t)(temp & 7);
259 bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
263 case PB_WT_VARINT: return pb_skip_varint(stream);
264 case PB_WT_64BIT: return pb_read(stream, NULL, 8);
265 case PB_WT_STRING: return pb_skip_string(stream);
266 case PB_WT_32BIT: return pb_read(stream, NULL, 4);
267 default: PB_RETURN_ERROR(stream, "invalid wire_type");
271 /* Read a raw value to buffer, for the purpose of passing it to callback as
272 * a substream. Size is maximum size on call, and actual size on return.
274 static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, uint8_t *buf, size_t *size)
276 size_t max_size = *size;
284 if (*size > max_size) return false;
285 if (!pb_read(stream, buf, 1)) return false;
286 } while (*buf++ & 0x80);
291 return pb_read(stream, buf, 8);
295 return pb_read(stream, buf, 4);
297 default: PB_RETURN_ERROR(stream, "invalid wire_type");
301 /* Decode string length from stream and return a substream with limited length.
302 * Remember to close the substream using pb_close_string_substream().
304 bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
307 if (!pb_decode_varint32(stream, &size))
310 *substream = *stream;
311 if (substream->bytes_left < size)
312 PB_RETURN_ERROR(stream, "parent stream too short");
314 substream->bytes_left = size;
315 stream->bytes_left -= size;
319 void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
321 stream->state = substream->state;
324 stream->errmsg = substream->errmsg;
328 /*************************
329 * Decode a single field *
330 *************************/
332 static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
337 type = iter->pos->type;
338 func = PB_DECODERS[PB_LTYPE(type)];
340 switch (PB_HTYPE(type))
342 case PB_HTYPE_REQUIRED:
343 return func(stream, iter->pos, iter->pData);
345 case PB_HTYPE_OPTIONAL:
346 *(bool*)iter->pSize = true;
347 return func(stream, iter->pos, iter->pData);
349 case PB_HTYPE_REPEATED:
350 if (wire_type == PB_WT_STRING
351 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
355 pb_size_t *size = (pb_size_t*)iter->pSize;
356 pb_istream_t substream;
357 if (!pb_make_string_substream(stream, &substream))
360 while (substream.bytes_left > 0 && *size < iter->pos->array_size)
362 void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
363 if (!func(&substream, iter->pos, pItem))
370 pb_close_string_substream(stream, &substream);
372 if (substream.bytes_left != 0)
373 PB_RETURN_ERROR(stream, "array overflow");
380 pb_size_t *size = (pb_size_t*)iter->pSize;
381 void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
382 if (*size >= iter->pos->array_size)
383 PB_RETURN_ERROR(stream, "array overflow");
386 return func(stream, iter->pos, pItem);
390 PB_RETURN_ERROR(stream, "invalid field type");
394 #ifdef PB_ENABLE_MALLOC
395 /* Allocate storage for the field and store the pointer at iter->pData.
396 * array_size is the number of entries to reserve in an array.
397 * Zero size is not allowed, use pb_free() for releasing.
399 static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
401 void *ptr = *(void**)pData;
403 /* Check for multiplication overflows.
404 * This code avoids the costly division if the sizes are small enough.
405 * Multiplication is safe as long as only half of bits are set
406 * in either multiplicand.
408 const size_t check_limit = (size_t)1 << (sizeof(size_t) * 4);
409 if (data_size >= check_limit || array_size >= check_limit)
411 const size_t size_max = (size_t)-1;
412 if (size_max / array_size < data_size)
414 PB_RETURN_ERROR(stream, "size too large");
418 /* Allocate new or expand previous allocation */
419 /* Note: on failure the old pointer will remain in the structure,
420 * the message must be freed by caller also on error return. */
421 ptr = pb_realloc(ptr, array_size * data_size);
423 PB_RETURN_ERROR(stream, "realloc failed");
425 *(void**)pData = ptr;
429 /* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
430 static void initialize_pointer_field(void *pItem, pb_field_iter_t *iter)
432 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_STRING ||
433 PB_LTYPE(iter->pos->type) == PB_LTYPE_BYTES)
435 *(void**)pItem = NULL;
437 else if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
439 pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, pItem);
444 static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
446 #ifndef PB_ENABLE_MALLOC
447 PB_UNUSED(wire_type);
449 PB_RETURN_ERROR(stream, "no malloc support");
454 type = iter->pos->type;
455 func = PB_DECODERS[PB_LTYPE(type)];
457 switch (PB_HTYPE(type))
459 case PB_HTYPE_REQUIRED:
460 case PB_HTYPE_OPTIONAL:
461 if (PB_LTYPE(type) == PB_LTYPE_STRING ||
462 PB_LTYPE(type) == PB_LTYPE_BYTES)
464 return func(stream, iter->pos, iter->pData);
468 if (!allocate_field(stream, iter->pData, iter->pos->data_size, 1))
471 initialize_pointer_field(*(void**)iter->pData, iter);
472 return func(stream, iter->pos, *(void**)iter->pData);
475 case PB_HTYPE_REPEATED:
476 if (wire_type == PB_WT_STRING
477 && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
479 /* Packed array, multiple items come in at once. */
481 pb_size_t *size = (pb_size_t*)iter->pSize;
482 size_t allocated_size = *size;
484 pb_istream_t substream;
486 if (!pb_make_string_substream(stream, &substream))
489 while (substream.bytes_left)
491 if ((size_t)*size + 1 > allocated_size)
493 /* Allocate more storage. This tries to guess the
494 * number of remaining entries. Round the division
496 allocated_size += (substream.bytes_left - 1) / iter->pos->data_size + 1;
498 if (!allocate_field(&substream, iter->pData, iter->pos->data_size, allocated_size))
505 /* Decode the array entry */
506 pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size);
507 initialize_pointer_field(pItem, iter);
508 if (!func(&substream, iter->pos, pItem))
514 if (*size == PB_SIZE_MAX)
517 stream->errmsg = "too many array entries";
525 pb_close_string_substream(stream, &substream);
531 /* Normal repeated field, i.e. only one item at a time. */
532 pb_size_t *size = (pb_size_t*)iter->pSize;
535 if (*size == PB_SIZE_MAX)
536 PB_RETURN_ERROR(stream, "too many array entries");
539 if (!allocate_field(stream, iter->pData, iter->pos->data_size, *size))
542 pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size - 1);
543 initialize_pointer_field(pItem, iter);
544 return func(stream, iter->pos, pItem);
548 PB_RETURN_ERROR(stream, "invalid field type");
553 static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
555 pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
557 #ifdef PB_OLD_CALLBACK_STYLE
558 void *arg = pCallback->arg;
560 void **arg = &(pCallback->arg);
563 if (pCallback->funcs.decode == NULL)
564 return pb_skip_field(stream, wire_type);
566 if (wire_type == PB_WT_STRING)
568 pb_istream_t substream;
570 if (!pb_make_string_substream(stream, &substream))
575 if (!pCallback->funcs.decode(&substream, iter->pos, arg))
576 PB_RETURN_ERROR(stream, "callback failed");
577 } while (substream.bytes_left);
579 pb_close_string_substream(stream, &substream);
584 /* Copy the single scalar value to stack.
585 * This is required so that we can limit the stream length,
586 * which in turn allows to use same callback for packed and
587 * not-packed fields. */
588 pb_istream_t substream;
590 size_t size = sizeof(buffer);
592 if (!read_raw_value(stream, wire_type, buffer, &size))
594 substream = pb_istream_from_buffer(buffer, size);
596 return pCallback->funcs.decode(&substream, iter->pos, arg);
600 static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
602 switch (PB_ATYPE(iter->pos->type))
604 case PB_ATYPE_STATIC:
605 return decode_static_field(stream, wire_type, iter);
607 case PB_ATYPE_POINTER:
608 return decode_pointer_field(stream, wire_type, iter);
610 case PB_ATYPE_CALLBACK:
611 return decode_callback_field(stream, wire_type, iter);
614 PB_RETURN_ERROR(stream, "invalid field type");
618 /* Default handler for extension fields. Expects a pb_field_t structure
619 * in extension->type->arg. */
620 static bool checkreturn default_extension_decoder(pb_istream_t *stream,
621 pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
623 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
624 pb_field_iter_t iter;
626 if (field->tag != tag)
629 /* Fake a field iterator for the extension field.
630 * It is not actually safe to advance this iterator, but decode_field
631 * will not even try to. */
632 (void)pb_field_iter_begin(&iter, field, extension->dest);
633 iter.pData = extension->dest;
634 iter.pSize = &extension->found;
636 return decode_field(stream, wire_type, &iter);
639 /* Try to decode an unknown field as an extension field. Tries each extension
640 * decoder in turn, until one of them handles the field or loop ends. */
641 static bool checkreturn decode_extension(pb_istream_t *stream,
642 uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter)
644 pb_extension_t *extension = *(pb_extension_t* const *)iter->pData;
645 size_t pos = stream->bytes_left;
647 while (extension != NULL && pos == stream->bytes_left)
650 if (extension->type->decode)
651 status = extension->type->decode(stream, extension, tag, wire_type);
653 status = default_extension_decoder(stream, extension, tag, wire_type);
658 extension = extension->next;
664 /* Step through the iterator until an extension field is found or until all
665 * entries have been checked. There can be only one extension field per
666 * message. Returns false if no extension field is found. */
667 static bool checkreturn find_extension_field(pb_field_iter_t *iter)
669 const pb_field_t *start = iter->pos;
672 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_EXTENSION)
674 (void)pb_field_iter_next(iter);
675 } while (iter->pos != start);
680 /* Initialize message fields to default values, recursively */
681 static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
683 pb_field_iter_t iter;
685 if (!pb_field_iter_begin(&iter, fields, dest_struct))
686 return; /* Empty message type */
691 type = iter.pos->type;
693 if (PB_ATYPE(type) == PB_ATYPE_STATIC)
695 if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL)
697 /* Set has_field to false. Still initialize the optional field
699 *(bool*)iter.pSize = false;
701 else if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
703 /* Set array count to 0, no need to initialize contents. */
704 *(pb_size_t*)iter.pSize = 0;
708 if (PB_LTYPE(iter.pos->type) == PB_LTYPE_SUBMESSAGE)
710 /* Initialize submessage to defaults */
711 pb_message_set_to_defaults((const pb_field_t *) iter.pos->ptr, iter.pData);
713 else if (iter.pos->ptr != NULL)
715 /* Initialize to default value */
716 memcpy(iter.pData, iter.pos->ptr, iter.pos->data_size);
720 /* Initialize to zeros */
721 memset(iter.pData, 0, iter.pos->data_size);
724 else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
726 /* Initialize the pointer to NULL. */
727 *(void**)iter.pData = NULL;
729 /* Initialize array count to 0. */
730 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
732 *(pb_size_t*)iter.pSize = 0;
735 else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
737 /* Don't overwrite callback */
739 } while (pb_field_iter_next(&iter));
742 /*********************
743 * Decode all fields *
744 *********************/
746 bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
748 uint8_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 7) / 8] = {0, 0, 0, 0, 0, 0, 0, 0};
749 uint32_t extension_range_start = 0;
750 pb_field_iter_t iter;
752 /* Return value ignored, as empty message types will be correctly handled by
753 * pb_field_iter_find() anyway. */
754 (void)pb_field_iter_begin(&iter, fields, dest_struct);
756 while (stream->bytes_left)
759 pb_wire_type_t wire_type;
762 if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
770 if (!pb_field_iter_find(&iter, tag))
772 /* No match found, check if it matches an extension. */
773 if (tag >= extension_range_start)
775 if (!find_extension_field(&iter))
776 extension_range_start = (uint32_t)-1;
778 extension_range_start = iter.pos->tag;
780 if (tag >= extension_range_start)
782 size_t pos = stream->bytes_left;
784 if (!decode_extension(stream, tag, wire_type, &iter))
787 if (pos != stream->bytes_left)
789 /* The field was handled */
795 /* No match found, skip data */
796 if (!pb_skip_field(stream, wire_type))
801 if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REQUIRED
802 && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
804 fields_seen[iter.required_field_index >> 3] |= (uint8_t)(1 << (iter.required_field_index & 7));
807 if (!decode_field(stream, wire_type, &iter))
811 /* Check that all required fields were present. */
813 /* First figure out the number of required fields by
814 * seeking to the end of the field array. Usually we
815 * are already close to end after decoding.
817 unsigned req_field_count;
821 req_field_count = iter.required_field_index;
822 last_type = iter.pos->type;
823 } while (pb_field_iter_next(&iter));
825 /* Fixup if last field was also required. */
826 if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.pos->tag != 0)
829 /* Check the whole bytes */
830 for (i = 0; i < (req_field_count >> 3); i++)
832 if (fields_seen[i] != 0xFF)
833 PB_RETURN_ERROR(stream, "missing required field");
836 /* Check the remaining bits */
837 if (fields_seen[req_field_count >> 3] != (0xFF >> (8 - (req_field_count & 7))))
838 PB_RETURN_ERROR(stream, "missing required field");
844 bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
847 pb_message_set_to_defaults(fields, dest_struct);
848 status = pb_decode_noinit(stream, fields, dest_struct);
850 #ifdef PB_ENABLE_MALLOC
852 pb_release(fields, dest_struct);
858 bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
860 pb_istream_t substream;
863 if (!pb_make_string_substream(stream, &substream))
866 status = pb_decode(&substream, fields, dest_struct);
867 pb_close_string_substream(stream, &substream);
871 #ifdef PB_ENABLE_MALLOC
872 void pb_release(const pb_field_t fields[], void *dest_struct)
874 pb_field_iter_t iter;
876 if (!pb_field_iter_begin(&iter, fields, dest_struct))
877 return; /* Empty message type */
882 type = iter.pos->type;
884 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
886 if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
887 (PB_LTYPE(type) == PB_LTYPE_STRING ||
888 PB_LTYPE(type) == PB_LTYPE_BYTES))
890 /* Release entries in repeated string or bytes array */
891 void **pItem = *(void***)iter.pData;
892 pb_size_t count = *(pb_size_t*)iter.pSize;
899 else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
901 /* Release fields in submessages */
902 void *pItem = *(void**)iter.pData;
903 pb_size_t count = (pItem ? 1 : 0);
905 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
907 count = *(pb_size_t*)iter.pSize;
912 pb_release((const pb_field_t*)iter.pos->ptr, pItem);
913 pItem = (uint8_t*)pItem + iter.pos->data_size;
917 /* Release main item */
918 pb_free(*(void**)iter.pData);
919 *(void**)iter.pData = NULL;
921 } while (pb_field_iter_next(&iter));
927 bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
930 if (!pb_decode_varint(stream, &value))
934 *dest = (int64_t)(~(value >> 1));
936 *dest = (int64_t)(value >> 1);
941 bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
943 #ifdef __BIG_ENDIAN__
944 uint8_t *bytes = (uint8_t*)dest;
947 if (!pb_read(stream, lebytes, 4))
950 bytes[0] = lebytes[3];
951 bytes[1] = lebytes[2];
952 bytes[2] = lebytes[1];
953 bytes[3] = lebytes[0];
956 return pb_read(stream, (uint8_t*)dest, 4);
960 bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
962 #ifdef __BIG_ENDIAN__
963 uint8_t *bytes = (uint8_t*)dest;
966 if (!pb_read(stream, lebytes, 8))
969 bytes[0] = lebytes[7];
970 bytes[1] = lebytes[6];
971 bytes[2] = lebytes[5];
972 bytes[3] = lebytes[4];
973 bytes[4] = lebytes[3];
974 bytes[5] = lebytes[2];
975 bytes[6] = lebytes[1];
976 bytes[7] = lebytes[0];
979 return pb_read(stream, (uint8_t*)dest, 8);
983 static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
986 if (!pb_decode_varint(stream, &value))
989 switch (field->data_size)
991 case 1: *(int8_t*)dest = (int8_t)value; break;
992 case 2: *(int16_t*)dest = (int16_t)value; break;
993 case 4: *(int32_t*)dest = (int32_t)value; break;
994 case 8: *(int64_t*)dest = (int64_t)value; break;
995 default: PB_RETURN_ERROR(stream, "invalid data_size");
1001 static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1004 if (!pb_decode_varint(stream, &value))
1007 switch (field->data_size)
1009 case 4: *(uint32_t*)dest = (uint32_t)value; break;
1010 case 8: *(uint64_t*)dest = value; break;
1011 default: PB_RETURN_ERROR(stream, "invalid data_size");
1017 static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1020 if (!pb_decode_svarint(stream, &value))
1023 switch (field->data_size)
1025 case 4: *(int32_t*)dest = (int32_t)value; break;
1026 case 8: *(int64_t*)dest = value; break;
1027 default: PB_RETURN_ERROR(stream, "invalid data_size");
1033 static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
1036 return pb_decode_fixed32(stream, dest);
1039 static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
1042 return pb_decode_fixed64(stream, dest);
1045 static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
1048 pb_bytes_array_t *bdest;
1050 if (!pb_decode_varint32(stream, &size))
1053 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1055 #ifndef PB_ENABLE_MALLOC
1056 PB_RETURN_ERROR(stream, "no malloc support");
1058 if (!allocate_field(stream, dest, PB_BYTES_ARRAY_T_ALLOCSIZE(size), 1))
1060 bdest = *(pb_bytes_array_t**)dest;
1065 if (PB_BYTES_ARRAY_T_ALLOCSIZE(size) > field->data_size)
1066 PB_RETURN_ERROR(stream, "bytes overflow");
1067 bdest = (pb_bytes_array_t*)dest;
1070 if (size > PB_SIZE_MAX)
1072 PB_RETURN_ERROR(stream, "bytes overflow");
1075 bdest->size = (pb_size_t)size;
1076 return pb_read(stream, bdest->bytes, size);
1079 static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
1084 if (!pb_decode_varint32(stream, &size))
1087 /* Space for null terminator */
1088 alloc_size = size + 1;
1090 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1092 #ifndef PB_ENABLE_MALLOC
1093 PB_RETURN_ERROR(stream, "no malloc support");
1095 if (!allocate_field(stream, dest, alloc_size, 1))
1097 dest = *(void**)dest;
1102 if (alloc_size > field->data_size)
1103 PB_RETURN_ERROR(stream, "string overflow");
1106 status = pb_read(stream, (uint8_t*)dest, size);
1107 *((uint8_t*)dest + size) = 0;
1111 static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
1114 pb_istream_t substream;
1115 const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr;
1117 if (!pb_make_string_substream(stream, &substream))
1120 if (field->ptr == NULL)
1121 PB_RETURN_ERROR(stream, "invalid field descriptor");
1123 /* New array entries need to be initialized, while required and optional
1124 * submessages have already been initialized in the top-level pb_decode. */
1125 if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
1126 status = pb_decode(&substream, submsg_fields, dest);
1128 status = pb_decode_noinit(&substream, submsg_fields, dest);
1130 pb_close_string_substream(stream, &substream);