{
uint64_t temp;
bool status = pb_decode_varint(stream, &temp);
- *dest = temp;
+ *dest = (uint32_t)temp;
return status;
}
return pb_read(stream, NULL, length);
}
-/* Currently the wire type related stuff is kept hidden from
- * callbacks. They shouldn't need it. It's better for performance
- * to just assume the correct type and fail safely on corrupt message.
- */
+bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, int *tag, bool *eof)
+{
+ uint32_t temp;
+ *eof = false;
+ *wire_type = 0;
+ *tag = 0;
+
+ if (!pb_decode_varint32(stream, &temp))
+ {
+ if (stream->bytes_left == 0)
+ *eof = true;
+
+ return false;
+ }
+
+ if (temp == 0)
+ {
+ *eof = true; /* Special feature: allow 0-terminated messages. */
+ return false;
+ }
+
+ *tag = temp >> 3;
+ *wire_type = (pb_wire_type_t)(temp & 7);
+ return true;
+}
-static bool checkreturn skip(pb_istream_t *stream, pb_wire_type_t wire_type)
+bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
{
switch (wire_type)
{
pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
if (pCallback->funcs.decode == NULL)
- return skip(stream, wire_type);
+ return pb_skip_field(stream, wire_type);
if (wire_type == PB_WT_STRING)
{
while (stream->bytes_left)
{
- uint32_t temp;
int tag;
pb_wire_type_t wire_type;
- if (!pb_decode_varint32(stream, &temp))
+ bool eof;
+
+ if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
{
- if (stream->bytes_left == 0)
- break; /* It was EOF */
+ if (eof)
+ break;
else
- return false; /* It was error */
+ return false;
}
- if (temp == 0)
- break; /* Special feature: allow 0-terminated messages. */
-
- tag = temp >> 3;
- wire_type = (pb_wire_type_t)(temp & 7);
-
if (!pb_field_find(&iter, tag))
{
/* No match found, skip data */
- if (!skip(stream, wire_type))
+ if (!pb_skip_field(stream, wire_type))
return false;
continue;
}
#ifdef __BIG_ENDIAN__
memcpy(dest, (char*)src + (srcsize - destsize), destsize);
#else
+ UNUSED(srcsize);
memcpy(dest, src, destsize);
#endif
}
#ifdef __BIG_ENDIAN__
uint8_t bytes[4] = {0};
bool status = pb_read(stream, bytes, 4);
- uint8_t bebytes[4] = {bytes[3], bytes[2], bytes[1], bytes[0]};
- memcpy(dest, bebytes, 4);
+ if (status) {
+ uint8_t *d = (uint8_t*)dest;
+ d[0] = bytes[3];
+ d[1] = bytes[2];
+ d[2] = bytes[1];
+ d[3] = bytes[0];
+ }
return status;
#else
+ UNUSED(field);
return pb_read(stream, (uint8_t*)dest, 4);
#endif
}
#ifdef __BIG_ENDIAN__
uint8_t bytes[8] = {0};
bool status = pb_read(stream, bytes, 8);
- uint8_t bebytes[8] = {bytes[7], bytes[6], bytes[5], bytes[4],
- bytes[3], bytes[2], bytes[1], bytes[0]};
- memcpy(dest, bebytes, 8);
+ if (status) {
+ uint8_t *d = (uint8_t*)dest;
+ d[0] = bytes[7];
+ d[1] = bytes[6];
+ d[2] = bytes[5];
+ d[3] = bytes[4];
+ d[4] = bytes[3];
+ d[5] = bytes[2];
+ d[6] = bytes[1];
+ d[7] = bytes[0];
+ }
return status;
#else
+ UNUSED(field);
return pb_read(stream, (uint8_t*)dest, 8);
#endif
}
return false;
x->size = temp;
- if (x->size > field->data_size)
+ /* Check length, noting the space taken by the size_t header. */
+ if (x->size > field->data_size - offsetof(pb_bytes_array_t, bytes))
return false;
return pb_read(stream, x->bytes, x->size);
if (!pb_decode_varint32(stream, &size))
return false;
- if (size > field->data_size - 1)
+ /* Check length, noting the null terminator */
+ if (size + 1 > field->data_size)
return false;
status = pb_read(stream, (uint8_t*)dest, size);