10#if (defined(__GNUC__) && ((__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))) || \
11 (defined(__IAR_SYSTEMS_ICC__) && (__VER__ >= 9040001))
12 #define checkreturn __attribute__((warn_unused_result))
46#ifdef PB_ENABLE_MALLOC
47static bool checkreturn allocate_field(pb_istream_t *stream,
void *pData,
size_t data_size,
size_t array_size);
48static void initialize_pointer_field(
void *pItem, pb_field_iter_t *field);
49static bool checkreturn pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *field);
50static void pb_release_single_field(pb_field_iter_t *field);
53#ifdef PB_WITHOUT_64BIT
54#define pb_int64_t int32_t
55#define pb_uint64_t uint32_t
57#define pb_int64_t int64_t
58#define pb_uint64_t uint64_t
72 stream->state = (
pb_byte_t*)stream->state + count;
100 return pb_read(stream, tmp, count);
104 if (stream->bytes_left < count)
107#ifndef PB_BUFFER_ONLY
108 if (!stream->callback(stream, buf, count))
115 if (stream->bytes_left < count)
116 stream->bytes_left = 0;
118 stream->bytes_left -= count;
127 if (stream->bytes_left == 0)
130#ifndef PB_BUFFER_ONLY
131 if (!stream->callback(stream, buf, 1))
135 stream->state = (
pb_byte_t*)stream->state + 1;
138 stream->bytes_left--;
154 stream.callback =
NULL;
159 stream.state = state.state;
160 stream.bytes_left = msglen;
162 stream.errmsg =
NULL;
178 if (stream->bytes_left == 0)
189 if ((
byte & 0x80) == 0)
198 result =
byte & 0x7F;
208 pb_byte_t sign_extension = (bitpos < 63) ? 0xFF : 0x01;
209 bool valid_extension = ((
byte & 0x7F) == 0x00 ||
210 ((result >> 31) != 0 &&
byte == sign_extension));
212 if (bitpos >= 64 || !valid_extension)
217 else if (bitpos == 28)
219 if ((
byte & 0x70) != 0 && (
byte & 0x78) != 0x78)
223 result |= (
uint32_t)(
byte & 0x0F) << bitpos;
227 result |= (
uint32_t)(
byte & 0x7F) << bitpos;
230 }
while (
byte & 0x80);
242#ifndef PB_WITHOUT_64BIT
254 if (bitpos >= 63 && (
byte & 0xFE) != 0)
257 result |= (
uint64_t)(
byte & 0x7F) << bitpos;
259 }
while (
byte & 0x80);
271 if (!
pb_read(stream, &
byte, 1))
273 }
while (
byte & 0x80);
283 if ((
size_t)length != length)
325 size_t max_size = *
size;
333 if (*
size > max_size)
338 }
while (*buf++ & 0x80);
343 return pb_read(stream, buf, 8);
347 return pb_read(stream, buf, 4);
368 *substream = *stream;
369 if (substream->bytes_left <
size)
379 if (substream->bytes_left) {
380 if (!
pb_read(substream,
NULL, substream->bytes_left))
384 stream->state = substream->state;
387 stream->errmsg = substream->errmsg;
424#ifdef PB_CONVERT_DOUBLE_FLOAT
425 if (field->data_size ==
sizeof(
float))
427 return pb_decode_double_as_float(stream, (
float*)field->pData);
431#ifdef PB_WITHOUT_64BIT
475 if (field->pSize !=
NULL)
476 *(
bool*)field->pSize =
true;
485 pb_istream_t substream;
487 field->pData = (
char*)field->pField + field->data_size * (*
size);
500 field->pData = (
char*)field->pData + field->data_size;
503 if (substream.bytes_left != 0)
514 field->pData = (
char*)field->pField + field->data_size * (*
size);
516 if ((*
size)++ >= field->array_size)
533 memset(field->pData, 0, (
size_t)field->data_size);
536 if (field->submsg_desc->default_value !=
NULL ||
537 field->submsg_desc->field_callback !=
NULL ||
538 field->submsg_desc->submsg_info[0] !=
NULL)
540 pb_field_iter_t submsg_iter;
557#ifdef PB_ENABLE_MALLOC
562static bool checkreturn allocate_field(pb_istream_t *stream,
void *pData,
size_t data_size,
size_t array_size)
564 void *ptr = *(
void**)pData;
566 if (data_size == 0 || array_size == 0)
573 if (data_size == 1 && array_size == 1)
585 const size_t check_limit = (
size_t)1 << (
sizeof(
size_t) * 4);
586 if (data_size >= check_limit || array_size >= check_limit)
588 const size_t size_max = (
size_t)-1;
589 if (size_max / array_size < data_size)
599 ptr =
pb_realloc(ptr, array_size * data_size);
603 *(
void**)pData = ptr;
608static void initialize_pointer_field(
void *pItem, pb_field_iter_t *field)
613 *(
void**)pItem =
NULL;
619 memset(pItem, 0, field->data_size);
626#ifndef PB_ENABLE_MALLOC
640 pb_release_single_field(field);
652 field->pData = field->pField;
657 if (!allocate_field(stream, field->pField, field->data_size, 1))
660 field->pData = *(
void**)field->pField;
661 initialize_pointer_field(field->pData, field);
672 size_t allocated_size = *
size;
673 pb_istream_t substream;
678 while (substream.bytes_left)
683 stream->errmsg =
"too many array entries";
689 if ((
size_t)*
size + 1 > allocated_size)
694 size_t remain = (substream.bytes_left - 1) / field->data_size + 1;
696 allocated_size += remain;
700 if (!allocate_field(&substream, field->pField, field->data_size, allocated_size))
708 field->pData = *(
char**)field->pField + field->data_size * (*
size);
709 if (field->pData ==
NULL)
715 initialize_pointer_field(field->pData, field);
737 if (!allocate_field(stream, field->pField, field->data_size, (
size_t)(*
size + 1)))
740 field->pData = *(
char**)field->pField + field->data_size * (*
size);
742 initialize_pointer_field(field->pData, field);
754 if (!field->descriptor->field_callback)
759 pb_istream_t substream;
760 size_t prev_bytes_left;
767 prev_bytes_left = substream.bytes_left;
768 if (!field->descriptor->field_callback(&substream,
NULL, field))
770 PB_SET_ERROR(stream, substream.errmsg ? substream.errmsg :
"callback failed");
773 }
while (substream.bytes_left > 0 && substream.bytes_left < prev_bytes_left);
786 pb_istream_t substream;
794 return field->descriptor->field_callback(&substream,
NULL, field);
800#ifdef PB_ENABLE_MALLOC
805 if (!pb_release_union_field(stream, field))
832 pb_field_iter_t iter;
837 if (iter.tag != tag || !iter.message)
840 extension->found =
true;
849 size_t pos = stream->bytes_left;
851 while (extension !=
NULL && pos == stream->bytes_left)
854 if (extension->type->decode)
855 status = extension->type->decode(stream, extension, tag, wire_type);
862 extension = extension->next;
876 pb_extension_t *ext = *(pb_extension_t*
const *)field->pData;
879 pb_field_iter_t ext_iter;
891 bool init_data =
true;
896 *(
bool*)field->pSize =
false;
910 (field->submsg_desc->default_value !=
NULL ||
911 field->submsg_desc->field_callback !=
NULL ||
912 field->submsg_desc->submsg_info[0] !=
NULL))
917 pb_field_iter_t submsg_iter;
927 memset(field->pData, 0, (
size_t)field->data_size);
934 *(
void**)field->pField =
NULL;
958 if (iter->descriptor->default_value)
970 if (tag != 0 && iter->tag == tag)
979 *(
bool*)iter->pSize =
false;
993 pb_extension_t *extensions =
NULL;
1005 pb_field_iter_t iter;
1016 while (stream->bytes_left)
1045 if (extension_range_start == 0)
1049 extensions = *(pb_extension_t*
const *)iter.pData;
1050 extension_range_start = iter.tag;
1055 extension_range_start = (
uint32_t)-1;
1059 if (tag >= extension_range_start)
1061 size_t pos = stream->bytes_left;
1066 if (pos != stream->bytes_left)
1084 if (fixed_count_field != iter.index) {
1090 fixed_count_size != fixed_count_total_size)
1095 fixed_count_field = iter.index;
1096 fixed_count_size = 0;
1097 fixed_count_total_size = iter.array_size;
1100 iter.pSize = &fixed_count_size;
1107 fields_seen.
bitfield[iter.required_field_index >> 5] |= tmp;
1116 fixed_count_size != fixed_count_total_size)
1123 pb_size_t req_field_count = iter.descriptor->required_field_count;
1125 if (req_field_count > 0)
1133 for (i = 0; i < (req_field_count >> 5); i++)
1135 if (fields_seen.
bitfield[i] != allbits)
1140 if ((req_field_count & 31) != 0)
1142 if (fields_seen.
bitfield[req_field_count >> 5] !=
1164 pb_istream_t substream;
1174#ifdef PB_ENABLE_MALLOC
1188#ifdef PB_ENABLE_MALLOC
1196#ifdef PB_ENABLE_MALLOC
1199static bool pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *field)
1201 pb_field_iter_t old_field = *field;
1208 if (old_tag == new_tag)
1216 pb_release_single_field(&old_field);
1222 *(
void**)field->pField =
NULL;
1223 field->pData =
NULL;
1229static void pb_release_single_field(pb_field_iter_t *field)
1236 if (*(
pb_size_t*)field->pSize != field->tag)
1246 pb_extension_t *ext = *(pb_extension_t**)field->pData;
1249 pb_field_iter_t ext_iter;
1252 pb_release_single_field(&ext_iter);
1264 field->pData = *(
void**)field->pField;
1268 field->pData = field->pField;
1278 count = field->array_size;
1284 for (; count > 0; count--)
1286 pb_release(field->submsg_desc, field->pData);
1287 field->pData = (
char*)field->pData + field->data_size;
1299 void **pItem = *(
void***)field->pField;
1301 for (; count > 0; count--)
1315 pb_free(*(
void**)field->pField);
1316 *(
void**)field->pField =
NULL;
1320void pb_release(
const pb_msgdesc_t *fields,
void *dest_struct)
1322 pb_field_iter_t iter;
1332 pb_release_single_field(&iter);
1352 *(
bool*)dest = (value != 0);
1377 if (!
pb_read(stream, u.bytes, 4))
1380#if defined(PB_LITTLE_ENDIAN_8BIT) && PB_LITTLE_ENDIAN_8BIT == 1
1392#ifndef PB_WITHOUT_64BIT
1400 if (!
pb_read(stream, u.bytes, 8))
1403#if defined(PB_LITTLE_ENDIAN_8BIT) && PB_LITTLE_ENDIAN_8BIT == 1
1436 else if (field->data_size ==
sizeof(
uint32_t))
1445 if (clamped != value)
1480 clamped = *(
pb_int64_t*)field->pData = svalue;
1481 else if (field->data_size ==
sizeof(
int32_t))
1490 if (clamped != svalue)
1501 pb_bytes_array_t *dest;
1510 if (
size > alloc_size)
1515#ifndef PB_ENABLE_MALLOC
1518 if (stream->bytes_left <
size)
1521 if (!allocate_field(stream, field->pData, alloc_size, 1))
1523 dest = *(pb_bytes_array_t**)field->pData;
1528 if (alloc_size > field->data_size)
1530 dest = (pb_bytes_array_t*)field->pData;
1534 return pb_read(stream, dest->bytes, (
size_t)
size);
1552 if (alloc_size <
size)
1557#ifndef PB_ENABLE_MALLOC
1560 if (stream->bytes_left <
size)
1563 if (!allocate_field(stream, field->pData, alloc_size, 1))
1570 if (alloc_size > field->data_size)
1579#ifdef PB_VALIDATE_UTF8
1580 if (!pb_validate_utf8((
const char*)dest))
1590 bool submsg_consumed =
false;
1591 pb_istream_t substream;
1596 if (field->submsg_desc ==
NULL)
1605 pb_callback_t *
callback = (pb_callback_t*)field->pSize - 1;
1610 if (substream.bytes_left == 0)
1612 submsg_consumed =
true;
1618 if (status && !submsg_consumed)
1620 unsigned int flags = 0;
1630 status =
pb_decode_inner(&substream, field->submsg_desc, field->pData, flags);
1652 memset(field->pData, 0, (
size_t)field->data_size);
1656 if (
size != field->data_size)
1659 return pb_read(stream, (
pb_byte_t*)field->pData, (
size_t)field->data_size);
1662#ifdef PB_CONVERT_DOUBLE_FLOAT
1663bool pb_decode_double_as_float(pb_istream_t *stream,
float *dest)
1669 union {
float f;
uint32_t i; } out;
1676 exponent = (
int)((value >> 52) & 0x7FF) - 1023;
1677 mantissa = (value >> 28) & 0xFFFFFF;
1680 if (exponent == 1024)
1694 else if (exponent < -150)
1700 else if (exponent < -126)
1703 mantissa |= 0x1000000;
1704 mantissa >>= (-126 - exponent);
1709 mantissa = (mantissa + 1) >> 1;
1712 if (mantissa & 0x800000)
1715 mantissa &= 0x7FFFFF;
1722 out.i |= (
uint32_t)(exponent + 127) << 23;
bool callback(pb_istream_t *stream, uint8_t *buf, size_t count)
MOSAPI int(2, 3) sprintf(char *__restrict str
#define pb_realloc(ptr, size)
#define PB_LTYPE_SUBMSG_W_CB
#define PB_MAX_REQUIRED_FIELDS
#define PB_LTYPE_EXTENSION
#define PB_ATYPE_CALLBACK
#define PB_SET_ERROR(stream, msg)
#define PB_BYTES_ARRAY_T_ALLOCSIZE(n)
#define PB_HTYPE_REQUIRED
#define PB_LTYPE_LAST_PACKABLE
#define PB_HTYPE_OPTIONAL
#define PB_HTYPE_REPEATED
#define PB_LTYPE_FIXED_LENGTH_BYTES
#define PB_RETURN_ERROR(stream, msg)
#define PB_LTYPE_IS_SUBMSG(x)
#define PB_LTYPE_SUBMESSAGE
bool pb_field_iter_next(pb_field_iter_t *iter)
bool pb_field_iter_find_extension(pb_field_iter_t *iter)
bool pb_field_iter_begin_extension(pb_field_iter_t *iter, pb_extension_t *extension)
bool pb_field_iter_begin(pb_field_iter_t *iter, const pb_msgdesc_t *desc, void *message)
bool pb_field_iter_find(pb_field_iter_t *iter, uint32_t tag)
bool pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
bool pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct)
static bool decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
static bool pb_dec_varint(pb_istream_t *stream, const pb_field_iter_t *field)
static bool buf_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
void pb_release(const pb_msgdesc_t *fields, void *dest_struct)
static bool pb_dec_bytes(pb_istream_t *stream, const pb_field_iter_t *field)
static bool decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
bool pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
static bool pb_dec_bool(pb_istream_t *stream, const pb_field_iter_t *field)
bool pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen)
bool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
static bool pb_dec_fixed_length_bytes(pb_istream_t *stream, const pb_field_iter_t *field)
static bool default_extension_decoder(pb_istream_t *stream, pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
static bool pb_field_set_to_default(pb_field_iter_t *field)
static bool decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
bool pb_decode_bool(pb_istream_t *stream, bool *dest)
bool pb_decode_ex(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct, unsigned int flags)
static bool decode_basic_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
static bool pb_decode_varint32_eof(pb_istream_t *stream, uint32_t *dest, bool *eof)
static bool decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
static bool pb_message_set_to_defaults(pb_field_iter_t *iter)
static bool pb_readbyte(pb_istream_t *stream, pb_byte_t *buf)
bool pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
static bool pb_dec_submessage(pb_istream_t *stream, const pb_field_iter_t *field)
bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
static bool pb_skip_varint(pb_istream_t *stream)
static bool pb_skip_string(pb_istream_t *stream)
static bool pb_decode_inner(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct, unsigned int flags)
static bool pb_dec_string(pb_istream_t *stream, const pb_field_iter_t *field)
static bool decode_extension(pb_istream_t *stream, uint32_t tag, pb_wire_type_t wire_type, pb_extension_t *extension)
static bool read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, pb_byte_t *buf, size_t *size)
#define PB_DECODE_DELIMITED
#define PB_DECODE_NULLTERMINATED
static void * memcpy(void *s1, const void *s2, size_t n)
static void * memset(void *s, int c, size_t n)
unsigned long long uint64_t
uint32_t bitfield[(64+31)/32]