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 size_t *size = (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 size_t *size = (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
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 size_t *size = (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 + 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))
515 pb_close_string_substream(stream, &substream);
521 /* Normal repeated field, i.e. only one item at a time. */
522 size_t *size = (size_t*)iter->pSize;
526 if (!allocate_field(stream, iter->pData, iter->pos->data_size, *size))
529 pItem = *(uint8_t**)iter->pData + iter->pos->data_size * (*size - 1);
530 initialize_pointer_field(pItem, iter);
531 return func(stream, iter->pos, pItem);
535 PB_RETURN_ERROR(stream, "invalid field type");
540 static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
542 pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
544 #ifdef PB_OLD_CALLBACK_STYLE
545 void *arg = pCallback->arg;
547 void **arg = &(pCallback->arg);
550 if (pCallback->funcs.decode == NULL)
551 return pb_skip_field(stream, wire_type);
553 if (wire_type == PB_WT_STRING)
555 pb_istream_t substream;
557 if (!pb_make_string_substream(stream, &substream))
562 if (!pCallback->funcs.decode(&substream, iter->pos, arg))
563 PB_RETURN_ERROR(stream, "callback failed");
564 } while (substream.bytes_left);
566 pb_close_string_substream(stream, &substream);
571 /* Copy the single scalar value to stack.
572 * This is required so that we can limit the stream length,
573 * which in turn allows to use same callback for packed and
574 * not-packed fields. */
575 pb_istream_t substream;
577 size_t size = sizeof(buffer);
579 if (!read_raw_value(stream, wire_type, buffer, &size))
581 substream = pb_istream_from_buffer(buffer, size);
583 return pCallback->funcs.decode(&substream, iter->pos, arg);
587 static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
589 switch (PB_ATYPE(iter->pos->type))
591 case PB_ATYPE_STATIC:
592 return decode_static_field(stream, wire_type, iter);
594 case PB_ATYPE_POINTER:
595 return decode_pointer_field(stream, wire_type, iter);
597 case PB_ATYPE_CALLBACK:
598 return decode_callback_field(stream, wire_type, iter);
601 PB_RETURN_ERROR(stream, "invalid field type");
605 /* Default handler for extension fields. Expects a pb_field_t structure
606 * in extension->type->arg. */
607 static bool checkreturn default_extension_decoder(pb_istream_t *stream,
608 pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
610 const pb_field_t *field = (const pb_field_t*)extension->type->arg;
611 pb_field_iter_t iter;
613 if (field->tag != tag)
616 /* Fake a field iterator for the extension field.
617 * It is not actually safe to advance this iterator, but decode_field
618 * will not even try to. */
619 pb_field_iter_begin(&iter, field, extension->dest);
620 iter.pData = extension->dest;
621 iter.pSize = &extension->found;
623 return decode_field(stream, wire_type, &iter);
626 /* Try to decode an unknown field as an extension field. Tries each extension
627 * decoder in turn, until one of them handles the field or loop ends. */
628 static bool checkreturn decode_extension(pb_istream_t *stream,
629 uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter)
631 pb_extension_t *extension = *(pb_extension_t* const *)iter->pData;
632 size_t pos = stream->bytes_left;
634 while (extension != NULL && pos == stream->bytes_left)
637 if (extension->type->decode)
638 status = extension->type->decode(stream, extension, tag, wire_type);
640 status = default_extension_decoder(stream, extension, tag, wire_type);
645 extension = extension->next;
651 /* Step through the iterator until an extension field is found or until all
652 * entries have been checked. There can be only one extension field per
653 * message. Returns false if no extension field is found. */
654 static bool checkreturn find_extension_field(pb_field_iter_t *iter)
656 const pb_field_t *start = iter->pos;
659 if (PB_LTYPE(iter->pos->type) == PB_LTYPE_EXTENSION)
661 (void)pb_field_iter_next(iter);
662 } while (iter->pos != start);
667 /* Initialize message fields to default values, recursively */
668 static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
670 pb_field_iter_t iter;
671 pb_field_iter_begin(&iter, fields, dest_struct);
676 type = iter.pos->type;
678 /* Avoid crash on empty message types (zero fields) */
679 if (iter.pos->tag == 0)
682 if (PB_ATYPE(type) == PB_ATYPE_STATIC)
684 if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL)
686 /* Set has_field to false. Still initialize the optional field
688 *(bool*)iter.pSize = false;
690 else if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
692 /* Set array count to 0, no need to initialize contents. */
693 *(size_t*)iter.pSize = 0;
697 if (PB_LTYPE(iter.pos->type) == PB_LTYPE_SUBMESSAGE)
699 /* Initialize submessage to defaults */
700 pb_message_set_to_defaults((const pb_field_t *) iter.pos->ptr, iter.pData);
702 else if (iter.pos->ptr != NULL)
704 /* Initialize to default value */
705 memcpy(iter.pData, iter.pos->ptr, iter.pos->data_size);
709 /* Initialize to zeros */
710 memset(iter.pData, 0, iter.pos->data_size);
713 else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
715 /* Initialize the pointer to NULL. */
716 *(void**)iter.pData = NULL;
718 /* Initialize array count to 0. */
719 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
721 *(size_t*)iter.pSize = 0;
724 else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
726 /* Don't overwrite callback */
728 } while (pb_field_iter_next(&iter));
731 /*********************
732 * Decode all fields *
733 *********************/
735 bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
737 uint8_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 7) / 8] = {0, 0, 0, 0, 0, 0, 0, 0};
738 uint32_t extension_range_start = 0;
739 pb_field_iter_t iter;
741 pb_field_iter_begin(&iter, fields, dest_struct);
743 while (stream->bytes_left)
746 pb_wire_type_t wire_type;
749 if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
757 if (!pb_field_iter_find(&iter, tag))
759 /* No match found, check if it matches an extension. */
760 if (tag >= extension_range_start)
762 if (!find_extension_field(&iter))
763 extension_range_start = (uint32_t)-1;
765 extension_range_start = iter.pos->tag;
767 if (tag >= extension_range_start)
769 size_t pos = stream->bytes_left;
771 if (!decode_extension(stream, tag, wire_type, &iter))
774 if (pos != stream->bytes_left)
776 /* The field was handled */
782 /* No match found, skip data */
783 if (!pb_skip_field(stream, wire_type))
788 if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REQUIRED
789 && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
791 fields_seen[iter.required_field_index >> 3] |= (uint8_t)(1 << (iter.required_field_index & 7));
794 if (!decode_field(stream, wire_type, &iter))
798 /* Check that all required fields were present. */
800 /* First figure out the number of required fields by
801 * seeking to the end of the field array. Usually we
802 * are already close to end after decoding.
804 unsigned req_field_count;
808 req_field_count = iter.required_field_index;
809 last_type = iter.pos->type;
810 } while (pb_field_iter_next(&iter));
812 /* Fixup if last field was also required. */
813 if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.pos->tag != 0)
816 /* Check the whole bytes */
817 for (i = 0; i < (req_field_count >> 3); i++)
819 if (fields_seen[i] != 0xFF)
820 PB_RETURN_ERROR(stream, "missing required field");
823 /* Check the remaining bits */
824 if (fields_seen[req_field_count >> 3] != (0xFF >> (8 - (req_field_count & 7))))
825 PB_RETURN_ERROR(stream, "missing required field");
831 bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
834 pb_message_set_to_defaults(fields, dest_struct);
835 status = pb_decode_noinit(stream, fields, dest_struct);
837 #ifdef PB_ENABLE_MALLOC
839 pb_release(fields, dest_struct);
845 bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
847 pb_istream_t substream;
850 if (!pb_make_string_substream(stream, &substream))
853 status = pb_decode(&substream, fields, dest_struct);
854 pb_close_string_substream(stream, &substream);
858 #ifdef PB_ENABLE_MALLOC
859 void pb_release(const pb_field_t fields[], void *dest_struct)
861 pb_field_iter_t iter;
862 pb_field_iter_begin(&iter, fields, dest_struct);
867 type = iter.pos->type;
869 /* Avoid crash on empty message types (zero fields) */
870 if (iter.pos->tag == 0)
873 if (PB_ATYPE(type) == PB_ATYPE_POINTER)
875 if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
876 (PB_LTYPE(type) == PB_LTYPE_STRING ||
877 PB_LTYPE(type) == PB_LTYPE_BYTES))
879 /* Release entries in repeated string or bytes array */
880 void **pItem = *(void***)iter.pData;
881 size_t count = *(size_t*)iter.pSize;
888 else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
890 /* Release fields in submessages */
891 void *pItem = *(void**)iter.pData;
892 size_t count = (pItem ? 1 : 0);
894 if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
896 count = *(size_t*)iter.pSize;
901 pb_release((const pb_field_t*)iter.pos->ptr, pItem);
902 pItem = (uint8_t*)pItem + iter.pos->data_size;
906 /* Release main item */
907 pb_free(*(void**)iter.pData);
908 *(void**)iter.pData = NULL;
910 } while (pb_field_iter_next(&iter));
916 bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
919 if (!pb_decode_varint(stream, &value))
923 *dest = (int64_t)(~(value >> 1));
925 *dest = (int64_t)(value >> 1);
930 bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
932 #ifdef __BIG_ENDIAN__
933 uint8_t *bytes = (uint8_t*)dest;
936 if (!pb_read(stream, lebytes, 4))
939 bytes[0] = lebytes[3];
940 bytes[1] = lebytes[2];
941 bytes[2] = lebytes[1];
942 bytes[3] = lebytes[0];
945 return pb_read(stream, (uint8_t*)dest, 4);
949 bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
951 #ifdef __BIG_ENDIAN__
952 uint8_t *bytes = (uint8_t*)dest;
955 if (!pb_read(stream, lebytes, 8))
958 bytes[0] = lebytes[7];
959 bytes[1] = lebytes[6];
960 bytes[2] = lebytes[5];
961 bytes[3] = lebytes[4];
962 bytes[4] = lebytes[3];
963 bytes[5] = lebytes[2];
964 bytes[6] = lebytes[1];
965 bytes[7] = lebytes[0];
968 return pb_read(stream, (uint8_t*)dest, 8);
972 static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
975 if (!pb_decode_varint(stream, &value))
978 switch (field->data_size)
980 case 1: *(int8_t*)dest = (int8_t)value; break;
981 case 2: *(int16_t*)dest = (int16_t)value; break;
982 case 4: *(int32_t*)dest = (int32_t)value; break;
983 case 8: *(int64_t*)dest = (int64_t)value; break;
984 default: PB_RETURN_ERROR(stream, "invalid data_size");
990 static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
993 if (!pb_decode_varint(stream, &value))
996 switch (field->data_size)
998 case 4: *(uint32_t*)dest = (uint32_t)value; break;
999 case 8: *(uint64_t*)dest = value; break;
1000 default: PB_RETURN_ERROR(stream, "invalid data_size");
1006 static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1009 if (!pb_decode_svarint(stream, &value))
1012 switch (field->data_size)
1014 case 4: *(int32_t*)dest = (int32_t)value; break;
1015 case 8: *(int64_t*)dest = value; break;
1016 default: PB_RETURN_ERROR(stream, "invalid data_size");
1022 static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
1025 return pb_decode_fixed32(stream, dest);
1028 static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
1031 return pb_decode_fixed64(stream, dest);
1034 static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
1037 pb_bytes_array_t *bdest;
1039 if (!pb_decode_varint32(stream, &size))
1042 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1044 #ifndef PB_ENABLE_MALLOC
1045 PB_RETURN_ERROR(stream, "no malloc support");
1047 if (!allocate_field(stream, dest, PB_BYTES_ARRAY_T_ALLOCSIZE(size), 1))
1049 bdest = *(pb_bytes_array_t**)dest;
1054 if (PB_BYTES_ARRAY_T_ALLOCSIZE(size) > field->data_size)
1055 PB_RETURN_ERROR(stream, "bytes overflow");
1056 bdest = (pb_bytes_array_t*)dest;
1060 return pb_read(stream, bdest->bytes, size);
1063 static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
1068 if (!pb_decode_varint32(stream, &size))
1071 /* Space for null terminator */
1072 alloc_size = size + 1;
1074 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1076 #ifndef PB_ENABLE_MALLOC
1077 PB_RETURN_ERROR(stream, "no malloc support");
1079 if (!allocate_field(stream, dest, alloc_size, 1))
1081 dest = *(void**)dest;
1086 if (alloc_size > field->data_size)
1087 PB_RETURN_ERROR(stream, "string overflow");
1090 status = pb_read(stream, (uint8_t*)dest, size);
1091 *((uint8_t*)dest + size) = 0;
1095 static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
1098 pb_istream_t substream;
1099 const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr;
1101 if (!pb_make_string_substream(stream, &substream))
1104 if (field->ptr == NULL)
1105 PB_RETURN_ERROR(stream, "invalid field descriptor");
1107 /* New array entries need to be initialized, while required and optional
1108 * submessages have already been initialized in the top-level pb_decode. */
1109 if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
1110 status = pb_decode(&substream, submsg_fields, dest);
1112 status = pb_decode_noinit(&substream, submsg_fields, dest);
1114 pb_close_string_substream(stream, &substream);