#include "pb_decode.h"
#include <string.h>
-const pb_decoder_t PB_DECODERS[16] = {
- (pb_decoder_t)&pb_dec_uint32,
- (pb_decoder_t)&pb_dec_sint32,
- (pb_decoder_t)&pb_dec_fixed32,
- (pb_decoder_t)&pb_dec_uint64,
- (pb_decoder_t)&pb_dec_sint64,
- (pb_decoder_t)&pb_dec_fixed64,
- (pb_decoder_t)&pb_dec_bool,
- (pb_decoder_t)&pb_dec_enum,
-
- (pb_decoder_t)&pb_dec_float,
- (pb_decoder_t)&pb_dec_double,
+typedef bool (*pb_decoder_t)(pb_istream_t *stream, const pb_field_t *field, void *dest);
+
+/* --- Function pointers to field decoders ---
+ * Order in the array must match pb_action_t LTYPE numbering.
+ */
+static const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = {
+ &pb_dec_varint,
+ &pb_dec_svarint,
+ &pb_dec_fixed,
- (pb_decoder_t)&pb_dec_bytes,
- (pb_decoder_t)&pb_dec_string,
- (pb_decoder_t)&pb_dec_submessage
+ &pb_dec_bytes,
+ &pb_dec_string,
+ &pb_dec_submessage
};
/**************
bool pb_read(pb_istream_t *stream, uint8_t *buf, size_t count)
{
- bool status;
if (stream->bytes_left < count)
return false;
- status = stream->callback(stream, buf, count);
+ if (!stream->callback(stream, buf, count))
+ return false;
+
stream->bytes_left -= count;
- return status;
+ return true;
}
static bool buf_read(pb_istream_t *stream, uint8_t *buf, size_t count)
bool pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
{
uint64_t temp;
- if (!pb_decode_varint64(stream, &temp))
- return false;
+ bool status = pb_decode_varint64(stream, &temp);
*dest = temp;
- return true;
+ return status;
}
bool pb_decode_varint64(pb_istream_t *stream, uint64_t *dest)
while (bitpos < 64 && pb_read(stream, &byte, 1))
{
- *dest |= (byte & 0x7F) << bitpos;
+ *dest |= (uint64_t)(byte & 0x7F) << bitpos;
bitpos += 7;
if (!(byte & 0x80))
* to just assume the correct type and fail safely on corrupt message.
*/
-enum wire_type_t {
- WT_VARINT = 0,
- WT_64BIT = 1,
- WT_STRING = 2,
- WT_32BIT = 5
-};
-
static bool skip(pb_istream_t *stream, int wire_type)
{
switch (wire_type)
{
- case WT_VARINT: return pb_skip_varint(stream);
- case WT_64BIT: return pb_read(stream, NULL, 8);
- case WT_STRING: return pb_skip_string(stream);
- case WT_32BIT: return pb_read(stream, NULL, 4);
+ case PB_WT_VARINT: return pb_skip_varint(stream);
+ case PB_WT_64BIT: return pb_read(stream, NULL, 8);
+ case PB_WT_STRING: return pb_skip_string(stream);
+ case PB_WT_32BIT: return pb_read(stream, NULL, 4);
default: return false;
}
}
-/* Read a raw value to buffer, for the purpose of passing it to callback.
- * Size is maximum size on call, and actual size on return. */
-static bool read_raw_value(pb_istream_t *stream, int wire_type, uint8_t *buf, size_t *size)
+/* Read a raw value to buffer, for the purpose of passing it to callback as
+ * a substream. Size is maximum size on call, and actual size on return.
+ */
+static bool read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, uint8_t *buf, size_t *size)
{
size_t max_size = *size;
switch (wire_type)
{
- case WT_VARINT:
+ case PB_WT_VARINT:
*size = 0;
do
{
} while (*buf++ & 0x80);
return true;
- case WT_64BIT:
+ case PB_WT_64BIT:
*size = 8;
return pb_read(stream, buf, 8);
- case WT_32BIT:
+ case PB_WT_32BIT:
*size = 4;
return pb_read(stream, buf, 4);
return true;
}
-bool decode_field(pb_istream_t *stream, int wire_type, const pb_field_t *field, void *dest_struct)
+/* Iterator for pb_field_t list */
+typedef struct {
+ const pb_field_t *start;
+ const pb_field_t *current;
+ int field_index;
+ void *dest_struct;
+ void *pData;
+ void *pSize;
+} pb_field_iterator_t;
+
+static void pb_field_init(pb_field_iterator_t *iter, const pb_field_t *fields, void *dest_struct)
+{
+ iter->start = iter->current = fields;
+ iter->field_index = 0;
+ iter->pData = dest_struct;
+ iter->dest_struct = dest_struct;
+}
+
+static bool pb_field_next(pb_field_iterator_t *iter)
{
- pb_decoder_t func = PB_DECODERS[PB_LTYPE(field->type)];
- void *pData = (char*)dest_struct + field->data_offset;
- void *pSize = (char*)dest_struct + field->size_offset;
+ bool notwrapped = true;
+ iter->current++;
+ iter->field_index++;
+ if (iter->current->tag == 0)
+ {
+ iter->current = iter->start;
+ iter->field_index = 0;
+ iter->pData = iter->dest_struct;
+ notwrapped = false;
+ }
- switch (PB_HTYPE(field->type))
+ iter->pData = (char*)iter->pData + iter->current->data_offset;
+ iter->pSize = (char*)iter->pData + iter->current->size_offset;
+ return notwrapped;
+}
+
+static bool pb_field_find(pb_field_iterator_t *iter, int tag)
+{
+ int start = iter->field_index;
+
+ do {
+ if (iter->current->tag == tag)
+ return true;
+ pb_field_next(iter);
+ } while (iter->field_index != start);
+
+ return false;
+}
+
+/*************************
+ * Decode a single field *
+ *************************/
+
+bool decode_field(pb_istream_t *stream, int wire_type, pb_field_iterator_t *iter)
+{
+ pb_decoder_t func = PB_DECODERS[PB_LTYPE(iter->current->type)];
+
+ switch (PB_HTYPE(iter->current->type))
{
case PB_HTYPE_REQUIRED:
- return func(stream, field, pData);
+ return func(stream, iter->current, iter->pData);
case PB_HTYPE_OPTIONAL:
- *(bool*)pSize = true;
- return func(stream, field, pData);
+ *(bool*)iter->pSize = true;
+ return func(stream, iter->current, iter->pData);
case PB_HTYPE_ARRAY:
- if (wire_type == WT_STRING
- && PB_LTYPE(field->type) != PB_LTYPE_BYTES
- && PB_LTYPE(field->type) != PB_LTYPE_STRING
- && PB_LTYPE(field->type) != PB_LTYPE_SUBMESSAGE)
+ if (wire_type == PB_WT_STRING
+ && PB_LTYPE(iter->current->type) <= PB_LTYPE_LAST_PACKABLE)
{
/* Packed array */
- size_t *size = (size_t*)pSize;
+ size_t *size = (size_t*)iter->pSize;
pb_istream_t substream;
if (!make_string_substream(stream, &substream))
return false;
- while (substream.bytes_left && *size < field->array_size)
+ while (substream.bytes_left && *size < iter->current->array_size)
{
- void *pItem = (uint8_t*)pData + field->data_size * (*size);
- if (!func(stream, field, pItem))
+ void *pItem = (uint8_t*)iter->pData + iter->current->data_size * (*size);
+ if (!func(stream, iter->current, pItem))
return false;
(*size)++;
}
else
{
/* Repeated field */
- size_t *size = (size_t*)pSize;
- void *pItem = (uint8_t*)pData + field->data_size * (*size);
- if (*size >= field->array_size)
+ size_t *size = (size_t*)iter->pSize;
+ void *pItem = (uint8_t*)iter->pData + iter->current->data_size * (*size);
+ if (*size >= iter->current->array_size)
return false;
(*size)++;
- return func(stream, field, pItem);
+ return func(stream, iter->current, pItem);
}
case PB_HTYPE_CALLBACK:
- if (wire_type == WT_STRING)
+ if (wire_type == PB_WT_STRING)
{
- pb_callback_t *pCallback = (pb_callback_t*)pData;
+ pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
pb_istream_t substream;
if (!make_string_substream(stream, &substream))
while (substream.bytes_left)
{
- if (!pCallback->funcs.decode(&substream, field, pCallback->arg))
+ if (!pCallback->funcs.decode(&substream, iter->current, pCallback->arg))
return false;
}
+ return true;
}
else
{
* which in turn allows to use same callback for packed and
* not-packed fields. */
pb_istream_t substream;
- pb_callback_t *pCallback = (pb_callback_t*)pData;
+ pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
uint8_t buffer[10];
size_t size = sizeof(buffer);
return false;
substream = pb_istream_from_buffer(buffer, size);
- return pCallback->funcs.decode(&substream, field, pCallback->arg);
+ return pCallback->funcs.decode(&substream, iter->current, pCallback->arg);
}
default:
}
}
+/*********************
+ * Decode all fields *
+ *********************/
+
bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
{
- /* Used to check for required fields */
- uint32_t fields_seen = 0;
+ uint32_t fields_seen = 0; /* Used to check for required fields */
+ pb_field_iterator_t iter;
int i;
+ if (fields[0].tag == 0)
+ {
+ /* No fields -> nothing to do */
+ return pb_read(stream, NULL, stream->bytes_left);
+ }
+
+ pb_field_init(&iter, fields, dest_struct);
+
/* Initialize size/has fields and apply default values */
- for (i = 0; fields[i].tag != 0; i++)
+ do
{
- void *pData = (char*)dest_struct + fields[i].data_offset;
- void *pSize = (char*)dest_struct + fields[i].size_offset;
- if (PB_HTYPE(fields[i].type) == PB_HTYPE_OPTIONAL)
+ if (PB_HTYPE(iter.current->type) == PB_HTYPE_OPTIONAL)
{
- *(bool*)pSize = false;
+ *(bool*)iter.pSize = false;
+
+ /* Initialize to default value */
+ if (iter.current->ptr != NULL)
+ memcpy(iter.pData, iter.current->ptr, iter.current->data_size);
+ else
+ memset(iter.pData, 0, iter.current->data_size);
}
- else if (PB_HTYPE(fields[i].type) == PB_HTYPE_ARRAY)
+ else if (PB_HTYPE(iter.current->type) == PB_HTYPE_ARRAY)
{
- *(size_t*)pSize = 0;
+ *(size_t*)iter.pSize = 0;
}
-
- if (PB_HTYPE(fields[i].type) != PB_HTYPE_ARRAY &&
- PB_HTYPE(fields[i].type) != PB_HTYPE_CALLBACK)
- {
- if (fields[i].ptr != NULL)
- {
- memcpy(pData, fields[i].ptr, fields[i].data_size);
- }
- else
- {
- memset(pData, 0, fields[i].data_size);
- }
- }
- }
+ } while (pb_field_next(&iter));
while (stream->bytes_left)
{
uint32_t temp;
int tag, wire_type;
if (!pb_decode_varint32(stream, &temp))
- return false;
+ return stream->bytes_left == 0; /* Was it EOF? */
tag = temp >> 3;
wire_type = temp & 7;
- i = 0;
- while (fields[i].tag != 0 && fields[i].tag != tag)
- {
- i++;
- }
-
- if (fields[i].tag == 0) /* No match found, skip data */
+ if (!pb_field_find(&iter, tag))
{
+ /* No match found, skip data */
skip(stream, wire_type);
continue;
}
- fields_seen |= 1 << (i & 31);
+ fields_seen |= 1 << (iter.field_index & 31);
- if (!decode_field(stream, wire_type, &fields[i], dest_struct))
+ if (!decode_field(stream, wire_type, &iter))
return false;
}
/* Field decoders */
-bool pb_dec_uint32(pb_istream_t *stream, const pb_field_t *field, uint32_t *dest)
-{
- return pb_decode_varint32(stream, dest);
-}
-
-bool pb_dec_sint32(pb_istream_t *stream, const pb_field_t *field, int32_t *dest)
-{
- uint32_t *x = (uint32_t*)dest;
- bool status = pb_decode_varint32(stream, x);
- *x = (*x >> 1) ^ -(int32_t)(*x & 1);
- return status;
-}
-
-bool pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, uint32_t *dest)
+/* Copy destsize bytes from src so that values are casted properly.
+ * On little endian machine, copy first n bytes of src
+ * On big endian machine, copy last n bytes of src
+ * srcsize must always be larger than destsize
+ */
+static void endian_copy(void *dest, void *src, size_t destsize, size_t srcsize)
{
- uint8_t bytes[4] = {0};
- bool status = pb_read(stream, bytes, 4);
-
#ifdef __BIG_ENDIAN__
- uint8_t lebytes[4] = {bytes[3], bytes[2], bytes[1], bytes[0]};
- memcpy(dest, lebytes, 4);
+ memcpy(dest, (char*)src + (srcsize - destsize), destsize);
#else
- memcpy(dest, bytes, 4);
+ memcpy(dest, src, destsize);
#endif
- return status;
}
-bool pb_dec_uint64(pb_istream_t *stream, const pb_field_t *field, uint64_t *dest)
+bool pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
- return pb_decode_varint64(stream, dest);
+ uint64_t temp;
+ bool status = pb_decode_varint64(stream, &temp);
+ endian_copy(dest, &temp, field->data_size, sizeof(temp));
+ return status;
}
-bool pb_dec_sint64(pb_istream_t *stream, const pb_field_t *field, int64_t *dest)
+bool pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
- uint64_t *x = (uint64_t*)dest;
- bool status = pb_decode_varint64(stream, x);
- *x = (*x >> 1) ^ -(int64_t)(*x & 1);
+ uint64_t temp;
+ bool status = pb_decode_varint64(stream, &temp);
+ temp = (temp >> 1) ^ -(int64_t)(temp & 1);
+ endian_copy(dest, &temp, field->data_size, sizeof(temp));
return status;
}
-bool pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, uint64_t *dest)
+bool pb_dec_fixed(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
- uint8_t bytes[8] = {0};
- bool status = pb_read(stream, bytes, 8);
-
#ifdef __BIG_ENDIAN__
- uint8_t lebytes[8] = {bytes[7], bytes[6], bytes[5], bytes[4],
+ uint8_t bytes[8] = {0};
+ bool status = pb_read(stream, bytes, field->data_size);
+ uint8_t bebytes[8] = {bytes[7], bytes[6], bytes[5], bytes[4],
bytes[3], bytes[2], bytes[1], bytes[0]};
- memcpy(dest, lebytes, 4);
+ endian_copy(dest, lebytes, field->data_size, 8);
+ return status;
#else
- memcpy(dest, bytes, 4);
+ return pb_read(stream, (uint8_t*)dest, field->data_size);
#endif
- return status;
-}
-
-bool pb_dec_bool(pb_istream_t *stream, const pb_field_t *field, bool *dest)
-{
- uint32_t temp = 0;
- bool status = pb_decode_varint32(stream, &temp);
- *(bool*)dest = !!temp;
- return status;
-}
-
-bool pb_dec_enum(pb_istream_t *stream, const pb_field_t *field, void *dest)
-{
- /* Enum sizes can vary, copy only data_size amount of bytes. */
- uint32_t temp = 0;
- bool status = pb_decode_varint32(stream, &temp);
- memcpy(dest, &temp, field->data_size);
- return status;
-}
-
-bool pb_dec_float(pb_istream_t *stream, const pb_field_t *field, float *dest)
-{
- return pb_read(stream, (uint8_t*)dest, sizeof(float));
-}
-
-bool pb_dec_double(pb_istream_t *stream, const pb_field_t *field, double *dest)
-{
- return pb_read(stream, (uint8_t*)dest, sizeof(double));
}
-bool pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, uint8_t *dest)
+bool pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
pb_bytes_array_t *x = (pb_bytes_array_t*)dest;
return pb_read(stream, x->bytes, x->size);
}
-bool pb_dec_string(pb_istream_t *stream, const pb_field_t *field, uint8_t *dest)
+bool pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
uint32_t size;
bool status;