* Field contents types *
************************/
- /* Standard integer types */
- PB_LTYPE_UINT32 = 0x00,
- PB_LTYPE_INT32 = 0x00,
- PB_LTYPE_SINT32 = 0x01,
- PB_LTYPE_FIXED32 = 0x02,
- PB_LTYPE_SFIXED32 = 0x02,
- PB_LTYPE_UINT64 = 0x03,
- PB_LTYPE_INT64 = 0x03,
- PB_LTYPE_SINT64 = 0x04,
- PB_LTYPE_FIXED64 = 0x05,
- PB_LTYPE_SFIXED64 = 0x05,
- PB_LTYPE_BOOL = 0x06,
- PB_LTYPE_ENUM = 0x07,
+ /* Numeric types */
+ PB_LTYPE_VARINT = 0x00, /* int32, uint32, int64, uint64, bool, enum */
+ PB_LTYPE_SVARINT = 0x01, /* sint32, sint64 */
+ PB_LTYPE_FIXED = 0x02, /* fixed32, sfixed32, fixed64, sfixed64, float, double */
- /* Standard float types */
- PB_LTYPE_FLOAT = 0x08,
- PB_LTYPE_DOUBLE = 0x09,
+ /* Marker for last packable field type. */
+ PB_LTYPE_LAST_PACKABLE = 0x02,
/* Byte array with pre-allocated buffer.
* data_size is the length of the allocated PB_BYTES_ARRAY structure. */
- PB_LTYPE_BYTES = 0x0A,
+ PB_LTYPE_BYTES = 0x03,
/* String with pre-allocated buffer.
* data_size is the maximum length. */
- PB_LTYPE_STRING = 0x0B,
+ PB_LTYPE_STRING = 0x04,
/* Submessage
* submsg_fields is pointer to field descriptions */
- PB_LTYPE_SUBMESSAGE = 0x0C,
+ PB_LTYPE_SUBMESSAGE = 0x05,
+
+ /* Number of declared LTYPES */
+ PB_LTYPES_COUNT = 6,
/******************
* Modifier flags *
struct _pb_field_t {
uint8_t tag;
pb_type_t type;
- uint8_t data_offset; /* Offset of actual data or array start */
- uint8_t size_offset; /* Offset of array size or has-boolean */
+ uint8_t data_offset; /* Offset of field data, relative to previous field. */
+ int8_t size_offset; /* Offset of array size or has-boolean, relative to data */
uint8_t data_size; /* Data size in bytes for a single item */
uint8_t array_size; /* Maximum number of entries in array */
#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,
+const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = {
+ (pb_decoder_t)&pb_dec_varint,
+ (pb_decoder_t)&pb_dec_svarint,
+ (pb_decoder_t)&pb_dec_fixed,
(pb_decoder_t)&pb_dec_bytes,
(pb_decoder_t)&pb_dec_string,
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)
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)
+ && PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
{
/* Packed array */
size_t *size = (size_t*)pSize;
if (!pCallback->funcs.decode(&substream, field, pCallback->arg))
return false;
}
+ return true;
}
else
{
if (PB_HTYPE(fields[i].type) == PB_HTYPE_OPTIONAL)
{
*(bool*)pSize = false;
- }
- else if (PB_HTYPE(fields[i].type) == PB_HTYPE_ARRAY)
- {
- *(size_t*)pSize = 0;
- }
-
- if (PB_HTYPE(fields[i].type) != PB_HTYPE_ARRAY &&
- PB_HTYPE(fields[i].type) != PB_HTYPE_CALLBACK)
- {
+
+ /* Initialize to default value */
if (fields[i].ptr != NULL)
- {
memcpy(pData, fields[i].ptr, fields[i].data_size);
- }
else
- {
memset(pData, 0, fields[i].data_size);
- }
+ }
+ else if (PB_HTYPE(fields[i].type) == PB_HTYPE_ARRAY)
+ {
+ *(size_t*)pSize = 0;
}
}
/* 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);
+ bool status = pb_read(stream, bytes, field->data_size);
#ifdef __BIG_ENDIAN__
uint8_t lebytes[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);
#else
- memcpy(dest, bytes, 4);
+ endian_copy(dest, bytes, field->data_size, 8);
#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)
{
pb_bytes_array_t *x = (pb_bytes_array_t*)dest;
* For arrays, these functions are called repeatedly.
*/
-bool pb_dec_uint32(pb_istream_t *stream, const pb_field_t *field, uint32_t *dest);
-bool pb_dec_sint32(pb_istream_t *stream, const pb_field_t *field, int32_t *dest);
-bool pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, uint32_t *dest);
-bool pb_dec_uint64(pb_istream_t *stream, const pb_field_t *field, uint64_t *dest);
-bool pb_dec_sint64(pb_istream_t *stream, const pb_field_t *field, int64_t *dest);
-bool pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, uint64_t *dest);
-bool pb_dec_bool(pb_istream_t *stream, const pb_field_t *field, bool *dest);
-bool pb_dec_enum(pb_istream_t *stream, const pb_field_t *field, void *dest);
-
-bool pb_dec_float(pb_istream_t *stream, const pb_field_t *field, float *dest);
-bool pb_dec_double(pb_istream_t *stream, const pb_field_t *field, double *dest);
+bool pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest);
+bool pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
+bool pb_dec_fixed(pb_istream_t *stream, const pb_field_t *field, void *dest);
bool pb_dec_bytes(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, uint8_t *dest);
/* --- Function pointers to field decoders ---
* Order in the array must match pb_action_t LTYPE numbering.
*/
-const pb_decoder_t PB_DECODERS[16];
+const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT];
#endif