10#if !defined(__GNUC__) || (__GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
13#define checkreturn __attribute__((warn_unused_result))
25static bool checkreturn pb_decode_varint32_eof(
pb_istream_t *
stream, uint32_t *dest,
bool *eof);
26static bool checkreturn read_raw_value(
pb_istream_t *
stream, pb_wire_type_t wire_type, pb_byte_t *buf,
size_t *size);
45#ifdef PB_ENABLE_MALLOC
46static bool checkreturn allocate_field(
pb_istream_t *
stream,
void *pData,
size_t data_size,
size_t array_size);
47static void initialize_pointer_field(
void *pItem,
pb_field_iter_t *field);
52#ifdef PB_WITHOUT_64BIT
53#define pb_int64_t int32_t
54#define pb_uint64_t uint32_t
56#define pb_int64_t int64_t
57#define pb_uint64_t uint64_t
61 uint32_t bitfield[(PB_MAX_REQUIRED_FIELDS + 31) / 32];
68 const pb_byte_t *source = (
const pb_byte_t *)
stream->state;
73 memcpy(buf, source, count *
sizeof(pb_byte_t));
85 if (buf == NULL &&
stream->callback != buf_read) {
89 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");
104#ifndef PB_BUFFER_ONLY
106 PB_RETURN_ERROR(
stream,
"io error");
109 if (!buf_read(
stream, buf, count)) {
114 stream->bytes_left -= count;
121 if (
stream->bytes_left == 0) {
122 PB_RETURN_ERROR(
stream,
"end-of-stream");
125#ifndef PB_BUFFER_ONLY
127 PB_RETURN_ERROR(
stream,
"io error");
130 *buf = *(
const pb_byte_t *)
stream->state;
139pb_istream_t pb_istream_from_buffer(
const pb_byte_t *buf,
size_t msglen) {
152 stream.callback = &buf_read;
155 stream.state = state.state;
156 stream.bytes_left = msglen;
166static bool checkreturn pb_decode_varint32_eof(
pb_istream_t *
stream, uint32_t *dest,
bool *eof) {
170 if (!pb_readbyte(
stream, &
byte)) {
171 if (
stream->bytes_left == 0) {
180 if ((
byte & 0x80) == 0) {
185 uint_fast8_t bitpos = 7;
186 result =
byte & 0x7F;
189 if (!pb_readbyte(
stream, &
byte)) {
195 pb_byte_t sign_extension = (bitpos < 63) ? 0xFF : 0x01;
196 bool valid_extension = ((
byte & 0x7F) == 0x00 ||
197 ((result >> 31) != 0 &&
byte == sign_extension));
199 if (bitpos >= 64 || !valid_extension) {
200 PB_RETURN_ERROR(
stream,
"varint overflow");
202 }
else if (bitpos == 28) {
203 if ((
byte & 0x70) != 0 && (
byte & 0x78) != 0x78) {
204 PB_RETURN_ERROR(
stream,
"varint overflow");
206 result |= (uint32_t)(
byte & 0x0F) << bitpos;
208 result |= (uint32_t)(
byte & 0x7F) << bitpos;
210 bitpos = (uint_fast8_t)(bitpos + 7);
211 }
while (
byte & 0x80);
219 return pb_decode_varint32_eof(
stream, dest, NULL);
222#ifndef PB_WITHOUT_64BIT
225 uint_fast8_t bitpos = 0;
229 if (!pb_readbyte(
stream, &
byte)) {
233 if (bitpos >= 63 && (
byte & 0xFE) != 0) {
234 PB_RETURN_ERROR(
stream,
"varint overflow");
237 result |= (uint64_t)(
byte & 0x7F) << bitpos;
238 bitpos = (uint_fast8_t)(bitpos + 7);
239 }
while (
byte & 0x80);
250 if (!pb_read(
stream, &
byte, 1)) {
253 }
while (
byte & 0x80);
260 if (!pb_decode_varint32(
stream, &length)) {
264 if ((
size_t)length != length) {
265 PB_RETURN_ERROR(
stream,
"size too large");
268 return pb_read(
stream, NULL, (
size_t)length);
271bool checkreturn pb_decode_tag(
pb_istream_t *
stream, pb_wire_type_t *wire_type, uint32_t *tag,
bool *eof) {
275 *wire_type = (pb_wire_type_t)0;
278 if (!pb_decode_varint32_eof(
stream, &temp, eof)) {
283 *wire_type = (pb_wire_type_t)(temp & 7);
289 case PB_WT_VARINT:
return pb_skip_varint(
stream);
290 case PB_WT_64BIT:
return pb_read(
stream, NULL, 8);
291 case PB_WT_STRING:
return pb_skip_string(
stream);
292 case PB_WT_32BIT:
return pb_read(
stream, NULL, 4);
293 default: PB_RETURN_ERROR(
stream,
"invalid wire_type");
300static bool checkreturn read_raw_value(
pb_istream_t *
stream, pb_wire_type_t wire_type, pb_byte_t *buf,
size_t *size) {
301 size_t max_size = *size;
308 if (*size > max_size) {
309 PB_RETURN_ERROR(
stream,
"varint overflow");
312 if (!pb_read(
stream, buf, 1)) {
315 }
while (*buf++ & 0x80);
320 return pb_read(
stream, buf, 8);
324 return pb_read(
stream, buf, 4);
332 default: PB_RETURN_ERROR(
stream,
"invalid wire_type");
342 if (!pb_decode_varint32(
stream, &size)) {
347 if (substream->bytes_left < size) {
348 PB_RETURN_ERROR(
stream,
"parent stream too short");
351 substream->bytes_left = (size_t)size;
352 stream->bytes_left -= (size_t)size;
357 if (substream->bytes_left) {
358 if (!pb_read(substream, NULL, substream->bytes_left)) {
363 stream->state = substream->state;
366 stream->errmsg = substream->errmsg;
375 switch (PB_LTYPE(field->type)) {
377 if (wire_type != PB_WT_VARINT && wire_type != PB_WT_PACKED) {
378 PB_RETURN_ERROR(
stream,
"wrong wire type");
381 return pb_dec_bool(
stream, field);
383 case PB_LTYPE_VARINT:
384 case PB_LTYPE_UVARINT:
385 case PB_LTYPE_SVARINT:
386 if (wire_type != PB_WT_VARINT && wire_type != PB_WT_PACKED) {
387 PB_RETURN_ERROR(
stream,
"wrong wire type");
390 return pb_dec_varint(
stream, field);
392 case PB_LTYPE_FIXED32:
393 if (wire_type != PB_WT_32BIT && wire_type != PB_WT_PACKED) {
394 PB_RETURN_ERROR(
stream,
"wrong wire type");
397 return pb_decode_fixed32(
stream, field->pData);
399 case PB_LTYPE_FIXED64:
400 if (wire_type != PB_WT_64BIT && wire_type != PB_WT_PACKED) {
401 PB_RETURN_ERROR(
stream,
"wrong wire type");
404#ifdef PB_CONVERT_DOUBLE_FLOAT
405 if (field->data_size ==
sizeof(
float)) {
406 return pb_decode_double_as_float(
stream, (
float *)field->pData);
410#ifdef PB_WITHOUT_64BIT
411 PB_RETURN_ERROR(
stream,
"invalid data_size");
413 return pb_decode_fixed64(
stream, field->pData);
417 if (wire_type != PB_WT_STRING) {
418 PB_RETURN_ERROR(
stream,
"wrong wire type");
421 return pb_dec_bytes(
stream, field);
423 case PB_LTYPE_STRING:
424 if (wire_type != PB_WT_STRING) {
425 PB_RETURN_ERROR(
stream,
"wrong wire type");
428 return pb_dec_string(
stream, field);
430 case PB_LTYPE_SUBMESSAGE:
431 case PB_LTYPE_SUBMSG_W_CB:
432 if (wire_type != PB_WT_STRING) {
433 PB_RETURN_ERROR(
stream,
"wrong wire type");
436 return pb_dec_submessage(
stream, field);
438 case PB_LTYPE_FIXED_LENGTH_BYTES:
439 if (wire_type != PB_WT_STRING) {
440 PB_RETURN_ERROR(
stream,
"wrong wire type");
443 return pb_dec_fixed_length_bytes(
stream, field);
446 PB_RETURN_ERROR(
stream,
"invalid field type");
451 switch (PB_HTYPE(field->type)) {
452 case PB_HTYPE_REQUIRED:
453 return decode_basic_field(
stream, wire_type, field);
455 case PB_HTYPE_OPTIONAL:
456 if (field->pSize != NULL) {
457 *(
bool *)field->pSize =
true;
459 return decode_basic_field(
stream, wire_type, field);
461 case PB_HTYPE_REPEATED:
462 if (wire_type == PB_WT_STRING
463 && PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE) {
467 pb_size_t *size = (pb_size_t *)field->pSize;
468 field->pData = (
char *)field->pField + field->data_size * (*size);
470 if (!pb_make_string_substream(
stream, &substream)) {
474 while (substream.bytes_left > 0 && *size < field->array_size) {
475 if (!decode_basic_field(&substream, PB_WT_PACKED, field)) {
480 field->pData = (
char *)field->pData + field->data_size;
483 if (substream.bytes_left != 0) {
484 PB_RETURN_ERROR(
stream,
"array overflow");
486 if (!pb_close_string_substream(
stream, &substream)) {
493 pb_size_t *size = (pb_size_t *)field->pSize;
494 field->pData = (
char *)field->pField + field->data_size * (*size);
496 if ((*size)++ >= field->array_size) {
497 PB_RETURN_ERROR(
stream,
"array overflow");
500 return decode_basic_field(
stream, wire_type, field);
504 if (PB_LTYPE_IS_SUBMSG(field->type) &&
505 *(pb_size_t *)field->pSize != field->tag) {
513 memset(field->pData, 0, (
size_t)field->data_size);
516 if (field->submsg_desc->default_value != NULL ||
517 field->submsg_desc->field_callback != NULL ||
518 field->submsg_desc->submsg_info[0] != NULL) {
520 if (pb_field_iter_begin(&submsg_iter, field->submsg_desc, field->pData)) {
521 if (!pb_message_set_to_defaults(&submsg_iter)) {
522 PB_RETURN_ERROR(
stream,
"failed to set defaults");
527 *(pb_size_t *)field->pSize = field->tag;
529 return decode_basic_field(
stream, wire_type, field);
532 PB_RETURN_ERROR(
stream,
"invalid field type");
536#ifdef PB_ENABLE_MALLOC
541static bool checkreturn allocate_field(
pb_istream_t *
stream,
void *pData,
size_t data_size,
size_t array_size) {
542 void *ptr = *(
void **)pData;
544 if (data_size == 0 || array_size == 0) {
545 PB_RETURN_ERROR(
stream,
"invalid size");
552 if (data_size == 1 && array_size == 1) {
563 const size_t check_limit = (size_t)1 << (
sizeof(
size_t) * 4);
564 if (data_size >= check_limit || array_size >= check_limit) {
565 const size_t size_max = (size_t)-1;
566 if (size_max / array_size < data_size) {
567 PB_RETURN_ERROR(
stream,
"size too large");
575 ptr = pb_realloc(ptr, array_size * data_size);
577 PB_RETURN_ERROR(
stream,
"realloc failed");
580 *(
void **)pData = ptr;
585static void initialize_pointer_field(
void *pItem,
pb_field_iter_t *field) {
586 if (PB_LTYPE(field->type) == PB_LTYPE_STRING ||
587 PB_LTYPE(field->type) == PB_LTYPE_BYTES) {
588 *(
void **)pItem = NULL;
589 }
else if (PB_LTYPE_IS_SUBMSG(field->type)) {
592 memset(pItem, 0, field->data_size);
598#ifndef PB_ENABLE_MALLOC
599 PB_UNUSED(wire_type);
601 PB_RETURN_ERROR(
stream,
"no malloc support");
603 switch (PB_HTYPE(field->type)) {
604 case PB_HTYPE_REQUIRED:
605 case PB_HTYPE_OPTIONAL:
607 if (PB_LTYPE_IS_SUBMSG(field->type) && *(
void **)field->pField != NULL) {
610 pb_release_single_field(field);
613 if (PB_HTYPE(field->type) == PB_HTYPE_ONEOF) {
614 *(pb_size_t *)field->pSize = field->tag;
617 if (PB_LTYPE(field->type) == PB_LTYPE_STRING ||
618 PB_LTYPE(field->type) == PB_LTYPE_BYTES) {
620 field->pData = field->pField;
621 return decode_basic_field(
stream, wire_type, field);
623 if (!allocate_field(
stream, field->pField, field->data_size, 1)) {
627 field->pData = *(
void **)field->pField;
628 initialize_pointer_field(field->pData, field);
629 return decode_basic_field(
stream, wire_type, field);
632 case PB_HTYPE_REPEATED:
633 if (wire_type == PB_WT_STRING
634 && PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE) {
637 pb_size_t *size = (pb_size_t *)field->pSize;
638 size_t allocated_size = *size;
641 if (!pb_make_string_substream(
stream, &substream)) {
645 while (substream.bytes_left) {
646 if (*size == PB_SIZE_MAX) {
648 stream->errmsg =
"too many array entries";
654 if ((
size_t)*size + 1 > allocated_size) {
658 size_t remain = (substream.bytes_left - 1) / field->data_size + 1;
659 if (remain < PB_SIZE_MAX - allocated_size) {
660 allocated_size += remain;
665 if (!allocate_field(&substream, field->pField, field->data_size, allocated_size)) {
672 field->pData = *(
char **)field->pField + field->data_size * (*size);
673 if (field->pData == NULL) {
678 initialize_pointer_field(field->pData, field);
679 if (!decode_basic_field(&substream, PB_WT_PACKED, field)) {
686 if (!pb_close_string_substream(
stream, &substream)) {
693 pb_size_t *size = (pb_size_t *)field->pSize;
695 if (*size == PB_SIZE_MAX) {
696 PB_RETURN_ERROR(
stream,
"too many array entries");
699 if (!allocate_field(
stream, field->pField, field->data_size, (
size_t)(*size + 1))) {
703 field->pData = *(
char **)field->pField + field->data_size * (*size);
705 initialize_pointer_field(field->pData, field);
706 return decode_basic_field(
stream, wire_type, field);
710 PB_RETURN_ERROR(
stream,
"invalid field type");
716 if (!field->descriptor->field_callback) {
717 return pb_skip_field(
stream, wire_type);
720 if (wire_type == PB_WT_STRING) {
722 size_t prev_bytes_left;
724 if (!pb_make_string_substream(
stream, &substream)) {
729 prev_bytes_left = substream.bytes_left;
730 if (!field->descriptor->field_callback(&substream, NULL, field)) {
731 PB_RETURN_ERROR(
stream,
"callback failed");
733 }
while (substream.bytes_left > 0 && substream.bytes_left < prev_bytes_left);
735 if (!pb_close_string_substream(
stream, &substream)) {
746 pb_byte_t buffer[10];
747 size_t size =
sizeof(buffer);
749 if (!read_raw_value(
stream, wire_type, buffer, &size)) {
752 substream = pb_istream_from_buffer(buffer, size);
754 return field->descriptor->field_callback(&substream, NULL, field);
759#ifdef PB_ENABLE_MALLOC
762 if (PB_HTYPE(field->type) == PB_HTYPE_ONEOF) {
763 if (!pb_release_union_field(
stream, field)) {
769 switch (PB_ATYPE(field->type)) {
770 case PB_ATYPE_STATIC:
771 return decode_static_field(
stream, wire_type, field);
773 case PB_ATYPE_POINTER:
774 return decode_pointer_field(
stream, wire_type, field);
776 case PB_ATYPE_CALLBACK:
777 return decode_callback_field(
stream, wire_type, field);
780 PB_RETURN_ERROR(
stream,
"invalid field type");
788 pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type) {
791 if (!pb_field_iter_begin_extension(&iter, extension)) {
792 PB_RETURN_ERROR(
stream,
"invalid extension");
795 if (iter.tag != tag || !iter.message) {
799 extension->found =
true;
800 return decode_field(
stream, wire_type, &iter);
806 uint32_t tag, pb_wire_type_t wire_type,
pb_extension_t *extension) {
807 size_t pos =
stream->bytes_left;
809 while (extension != NULL && pos ==
stream->bytes_left) {
811 if (extension->type->decode) {
812 status = extension->type->decode(
stream, extension, tag, wire_type);
814 status = default_extension_decoder(
stream, extension, tag, wire_type);
821 extension = extension->next;
833 if (PB_LTYPE(type) == PB_LTYPE_EXTENSION) {
835 while (ext != NULL) {
837 if (pb_field_iter_begin_extension(&ext_iter, ext)) {
839 if (!pb_message_set_to_defaults(&ext_iter)) {
845 }
else if (PB_ATYPE(type) == PB_ATYPE_STATIC) {
846 bool init_data =
true;
847 if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL && field->pSize != NULL) {
850 *(
bool *)field->pSize =
false;
851 }
else if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
852 PB_HTYPE(type) == PB_HTYPE_ONEOF) {
855 *(pb_size_t *)field->pSize = 0;
860 if (PB_LTYPE_IS_SUBMSG(field->type) &&
861 (field->submsg_desc->default_value != NULL ||
862 field->submsg_desc->field_callback != NULL ||
863 field->submsg_desc->submsg_info[0] != NULL)) {
868 if (pb_field_iter_begin(&submsg_iter, field->submsg_desc, field->pData)) {
869 if (!pb_message_set_to_defaults(&submsg_iter)) {
875 memset(field->pData, 0, (
size_t)field->data_size);
878 }
else if (PB_ATYPE(type) == PB_ATYPE_POINTER) {
880 *(
void **)field->pField = NULL;
883 if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
884 PB_HTYPE(type) == PB_HTYPE_ONEOF) {
885 *(pb_size_t *)field->pSize = 0;
887 }
else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK) {
897 pb_wire_type_t wire_type = PB_WT_VARINT;
900 if (iter->descriptor->default_value) {
901 defstream = pb_istream_from_buffer(iter->descriptor->default_value, (
size_t)-1);
902 if (!pb_decode_tag(&defstream, &wire_type, &tag, &eof)) {
908 if (!pb_field_set_to_default(iter)) {
912 if (tag != 0 && iter->tag == tag) {
914 if (!decode_field(&defstream, wire_type, iter)) {
917 if (!pb_decode_tag(&defstream, &wire_type, &tag, &eof)) {
922 *(
bool *)iter->pSize =
false;
925 }
while (pb_field_iter_next(iter));
934 uint32_t extension_range_start = 0;
941 pb_size_t fixed_count_field = PB_SIZE_MAX;
942 pb_size_t fixed_count_size = 0;
943 pb_size_t fixed_count_total_size = 0;
946 const uint32_t allbits = ~(uint32_t)0;
949 if (pb_field_iter_begin(&iter, fields, dest_struct)) {
950 if ((flags & PB_DECODE_NOINIT) == 0) {
951 if (!pb_message_set_to_defaults(&iter)) {
952 PB_RETURN_ERROR(
stream,
"failed to set defaults");
957 while (
stream->bytes_left) {
959 pb_wire_type_t wire_type;
962 if (!pb_decode_tag(
stream, &wire_type, &tag, &eof)) {
971 if (flags & PB_DECODE_NULLTERMINATED) {
974 PB_RETURN_ERROR(
stream,
"zero tag");
978 if (!pb_field_iter_find(&iter, tag) || PB_LTYPE(iter.type) == PB_LTYPE_EXTENSION) {
980 if (extension_range_start == 0) {
981 if (pb_field_iter_find_extension(&iter)) {
983 extension_range_start = iter.tag;
987 extension_range_start = (uint32_t)-1;
991 if (tag >= extension_range_start) {
992 size_t pos =
stream->bytes_left;
994 if (!decode_extension(
stream, tag, wire_type, extensions)) {
998 if (pos !=
stream->bytes_left) {
1005 if (!pb_skip_field(
stream, wire_type)) {
1014 if (PB_HTYPE(iter.type) == PB_HTYPE_REPEATED && iter.pSize == &iter.array_size) {
1015 if (fixed_count_field != iter.index) {
1020 if (fixed_count_field != PB_SIZE_MAX &&
1021 fixed_count_size != fixed_count_total_size) {
1022 PB_RETURN_ERROR(
stream,
"wrong size for fixed count field");
1025 fixed_count_field = iter.index;
1026 fixed_count_size = 0;
1027 fixed_count_total_size = iter.array_size;
1030 iter.pSize = &fixed_count_size;
1033 if (PB_HTYPE(iter.type) == PB_HTYPE_REQUIRED
1034 && iter.required_field_index < PB_MAX_REQUIRED_FIELDS) {
1035 uint32_t tmp = ((uint32_t)1 << (iter.required_field_index & 31));
1036 fields_seen.bitfield[iter.required_field_index >> 5] |= tmp;
1039 if (!decode_field(
stream, wire_type, &iter)) {
1045 if (fixed_count_field != PB_SIZE_MAX &&
1046 fixed_count_size != fixed_count_total_size) {
1047 PB_RETURN_ERROR(
stream,
"wrong size for fixed count field");
1052 pb_size_t req_field_count = iter.descriptor->required_field_count;
1054 if (req_field_count > 0) {
1057 if (req_field_count > PB_MAX_REQUIRED_FIELDS) {
1058 req_field_count = PB_MAX_REQUIRED_FIELDS;
1062 for (i = 0; i < (req_field_count >> 5); i++) {
1063 if (fields_seen.bitfield[i] != allbits) {
1064 PB_RETURN_ERROR(
stream,
"missing required field");
1069 if ((req_field_count & 31) != 0) {
1070 if (fields_seen.bitfield[req_field_count >> 5] !=
1071 (allbits >> (uint_least8_t)(32 - (req_field_count & 31)))) {
1072 PB_RETURN_ERROR(
stream,
"missing required field");
1084 if ((flags & PB_DECODE_DELIMITED) == 0) {
1085 status = pb_decode_inner(
stream, fields, dest_struct, flags);
1088 if (!pb_make_string_substream(
stream, &substream)) {
1092 status = pb_decode_inner(&substream, fields, dest_struct, flags);
1094 if (!pb_close_string_substream(
stream, &substream)) {
1099#ifdef PB_ENABLE_MALLOC
1101 pb_release(fields, dest_struct);
1111 status = pb_decode_inner(
stream, fields, dest_struct, 0);
1113#ifdef PB_ENABLE_MALLOC
1115 pb_release(fields, dest_struct);
1122#ifdef PB_ENABLE_MALLOC
1127 pb_size_t old_tag = *(pb_size_t *)field->pSize;
1128 pb_size_t new_tag = field->tag;
1133 if (old_tag == new_tag) {
1138 if (!pb_field_iter_find(&old_field, old_tag)) {
1139 PB_RETURN_ERROR(
stream,
"invalid union tag");
1142 pb_release_single_field(&old_field);
1144 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER) {
1147 *(
void **)field->pField = NULL;
1148 field->pData = NULL;
1159 if (PB_HTYPE(type) == PB_HTYPE_ONEOF) {
1160 if (*(pb_size_t *)field->pSize != field->tag) {
1168 if (PB_LTYPE(type) == PB_LTYPE_EXTENSION) {
1171 while (ext != NULL) {
1173 if (pb_field_iter_begin_extension(&ext_iter, ext)) {
1174 pb_release_single_field(&ext_iter);
1178 }
else if (PB_LTYPE_IS_SUBMSG(type) && PB_ATYPE(type) != PB_ATYPE_CALLBACK) {
1180 pb_size_t count = 1;
1182 if (PB_ATYPE(type) == PB_ATYPE_POINTER) {
1183 field->pData = *(
void **)field->pField;
1185 field->pData = field->pField;
1188 if (PB_HTYPE(type) == PB_HTYPE_REPEATED) {
1189 count = *(pb_size_t *)field->pSize;
1191 if (PB_ATYPE(type) == PB_ATYPE_STATIC && count > field->array_size) {
1193 count = field->array_size;
1198 for (; count > 0; count--) {
1199 pb_release(field->submsg_desc, field->pData);
1200 field->pData = (
char *)field->pData + field->data_size;
1205 if (PB_ATYPE(type) == PB_ATYPE_POINTER) {
1206 if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
1207 (PB_LTYPE(type) == PB_LTYPE_STRING ||
1208 PB_LTYPE(type) == PB_LTYPE_BYTES)) {
1210 void **pItem = *(
void ***)field->pField;
1211 pb_size_t count = *(pb_size_t *)field->pSize;
1212 for (; count > 0; count--) {
1218 if (PB_HTYPE(type) == PB_HTYPE_REPEATED) {
1220 *(pb_size_t *)field->pSize = 0;
1224 pb_free(*(
void **)field->pField);
1225 *(
void **)field->pField = NULL;
1229void pb_release(
const pb_msgdesc_t *fields,
void *dest_struct) {
1235 if (!pb_field_iter_begin(&iter, fields, dest_struct)) {
1239 pb_release_single_field(&iter);
1240 }
while (pb_field_iter_next(&iter));
1249 if (!pb_decode_varint32(
stream, &value)) {
1253 *(
bool *)dest = (value != 0);
1260 if (!pb_decode_varint(
stream, &value)) {
1265 *dest = (pb_int64_t)(~(value >> 1));
1267 *dest = (pb_int64_t)(value >> 1);
1279 if (!pb_read(
stream, u.bytes, 4)) {
1283#if defined(PB_LITTLE_ENDIAN_8BIT) && PB_LITTLE_ENDIAN_8BIT == 1
1285 *(uint32_t *)dest = u.fixed32;
1287 *(uint32_t *)dest = ((uint32_t)u.bytes[0] << 0) |
1288 ((uint32_t)u.bytes[1] << 8) |
1289 ((uint32_t)u.bytes[2] << 16) |
1290 ((uint32_t)u.bytes[3] << 24);
1295#ifndef PB_WITHOUT_64BIT
1302 if (!pb_read(
stream, u.bytes, 8)) {
1306#if defined(PB_LITTLE_ENDIAN_8BIT) && PB_LITTLE_ENDIAN_8BIT == 1
1308 *(uint64_t *)dest = u.fixed64;
1310 *(uint64_t *)dest = ((uint64_t)u.bytes[0] << 0) |
1311 ((uint64_t)u.bytes[1] << 8) |
1312 ((uint64_t)u.bytes[2] << 16) |
1313 ((uint64_t)u.bytes[3] << 24) |
1314 ((uint64_t)u.bytes[4] << 32) |
1315 ((uint64_t)u.bytes[5] << 40) |
1316 ((uint64_t)u.bytes[6] << 48) |
1317 ((uint64_t)u.bytes[7] << 56);
1324 return pb_decode_bool(
stream, (
bool *)field->pData);
1328 if (PB_LTYPE(field->type) == PB_LTYPE_UVARINT) {
1329 pb_uint64_t value, clamped;
1330 if (!pb_decode_varint(
stream, &value)) {
1335 if (field->data_size ==
sizeof(pb_uint64_t)) {
1336 clamped = *(pb_uint64_t *)field->pData = value;
1337 }
else if (field->data_size ==
sizeof(uint32_t)) {
1338 clamped = *(uint32_t *)field->pData = (uint32_t)value;
1339 }
else if (field->data_size ==
sizeof(uint_least16_t)) {
1340 clamped = *(uint_least16_t *)field->pData = (uint_least16_t)value;
1341 }
else if (field->data_size ==
sizeof(uint_least8_t)) {
1342 clamped = *(uint_least8_t *)field->pData = (uint_least8_t)value;
1344 PB_RETURN_ERROR(
stream,
"invalid data_size");
1347 if (clamped != value) {
1348 PB_RETURN_ERROR(
stream,
"integer too large");
1357 if (PB_LTYPE(field->type) == PB_LTYPE_SVARINT) {
1358 if (!pb_decode_svarint(
stream, &svalue)) {
1362 if (!pb_decode_varint(
stream, &value)) {
1372 if (field->data_size ==
sizeof(pb_int64_t)) {
1373 svalue = (pb_int64_t)value;
1375 svalue = (int32_t)value;
1380 if (field->data_size ==
sizeof(pb_int64_t)) {
1381 clamped = *(pb_int64_t *)field->pData = svalue;
1382 }
else if (field->data_size ==
sizeof(int32_t)) {
1383 clamped = *(int32_t *)field->pData = (int32_t)svalue;
1384 }
else if (field->data_size ==
sizeof(int_least16_t)) {
1385 clamped = *(int_least16_t *)field->pData = (int_least16_t)svalue;
1386 }
else if (field->data_size ==
sizeof(int_least8_t)) {
1387 clamped = *(int_least8_t *)field->pData = (int_least8_t)svalue;
1389 PB_RETURN_ERROR(
stream,
"invalid data_size");
1392 if (clamped != svalue) {
1393 PB_RETURN_ERROR(
stream,
"integer too large");
1405 if (!pb_decode_varint32(
stream, &size)) {
1409 if (size > PB_SIZE_MAX) {
1410 PB_RETURN_ERROR(
stream,
"bytes overflow");
1413 alloc_size = PB_BYTES_ARRAY_T_ALLOCSIZE(size);
1414 if (size > alloc_size) {
1415 PB_RETURN_ERROR(
stream,
"size too large");
1418 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER) {
1419#ifndef PB_ENABLE_MALLOC
1420 PB_RETURN_ERROR(
stream,
"no malloc support");
1422 if (
stream->bytes_left < size) {
1423 PB_RETURN_ERROR(
stream,
"end-of-stream");
1426 if (!allocate_field(
stream, field->pData, alloc_size, 1)) {
1432 if (alloc_size > field->data_size) {
1433 PB_RETURN_ERROR(
stream,
"bytes overflow");
1438 dest->size = (pb_size_t)size;
1439 return pb_read(
stream, dest->bytes, (
size_t)size);
1445 pb_byte_t *dest = (pb_byte_t *)field->pData;
1447 if (!pb_decode_varint32(
stream, &size)) {
1451 if (size == (uint32_t)-1) {
1452 PB_RETURN_ERROR(
stream,
"size too large");
1456 alloc_size = (size_t)(size + 1);
1458 if (alloc_size < size) {
1459 PB_RETURN_ERROR(
stream,
"size too large");
1462 if (PB_ATYPE(field->type) == PB_ATYPE_POINTER) {
1463#ifndef PB_ENABLE_MALLOC
1464 PB_RETURN_ERROR(
stream,
"no malloc support");
1466 if (
stream->bytes_left < size) {
1467 PB_RETURN_ERROR(
stream,
"end-of-stream");
1470 if (!allocate_field(
stream, field->pData, alloc_size, 1)) {
1473 dest = *(pb_byte_t **)field->pData;
1476 if (alloc_size > field->data_size) {
1477 PB_RETURN_ERROR(
stream,
"string overflow");
1483 if (!pb_read(
stream, dest, (
size_t)size)) {
1487#ifdef PB_VALIDATE_UTF8
1488 if (!pb_validate_utf8((
const char *)dest)) {
1489 PB_RETURN_ERROR(
stream,
"invalid utf8");
1498 bool submsg_consumed =
false;
1501 if (!pb_make_string_substream(
stream, &substream)) {
1505 if (field->submsg_desc == NULL) {
1506 PB_RETURN_ERROR(
stream,
"invalid field descriptor");
1512 if (PB_LTYPE(field->type) == PB_LTYPE_SUBMSG_W_CB && field->pSize != NULL) {
1515 if (callback->funcs.decode) {
1516 status = callback->funcs.decode(&substream, field, &callback->arg);
1518 if (substream.bytes_left == 0) {
1519 submsg_consumed =
true;
1525 if (status && !submsg_consumed) {
1526 unsigned int flags = 0;
1530 if (PB_ATYPE(field->type) == PB_ATYPE_STATIC &&
1531 PB_HTYPE(field->type) != PB_HTYPE_REPEATED) {
1532 flags = PB_DECODE_NOINIT;
1535 status = pb_decode_inner(&substream, field->submsg_desc, field->pData, flags);
1538 if (!pb_close_string_substream(
stream, &substream)) {
1548 if (!pb_decode_varint32(
stream, &size)) {
1552 if (size > PB_SIZE_MAX) {
1553 PB_RETURN_ERROR(
stream,
"bytes overflow");
1558 memset(field->pData, 0, (
size_t)field->data_size);
1562 if (size != field->data_size) {
1563 PB_RETURN_ERROR(
stream,
"incorrect fixed length bytes size");
1566 return pb_read(
stream, (pb_byte_t *)field->pData, (
size_t)field->data_size);
1569#ifdef PB_CONVERT_DOUBLE_FLOAT
1576 union {
float f; uint32_t i; } out;
1578 if (!pb_decode_fixed64(
stream, &value)) {
1583 sign = (uint_least8_t)((value >> 63) & 1);
1584 exponent = (int)((value >> 52) & 0x7FF) - 1023;
1585 mantissa = (value >> 28) & 0xFFFFFF;
1588 if (exponent == 1024) {
1593 if (exponent > 127) {
1597 }
else if (exponent < -150) {
1601 }
else if (exponent < -126) {
1603 mantissa |= 0x1000000;
1604 mantissa >>= (-126 - exponent);
1609 mantissa = (mantissa + 1) >> 1;
1612 if (mantissa & 0x800000) {
1614 mantissa &= 0x7FFFFF;
1621 out.i |= (uint32_t)(exponent + 127) << 23;
1622 out.i |= (uint32_t)sign << 31;