diff options
-rw-r--r-- | pb.h | 35 | ||||
-rw-r--r-- | pb_decode.c | 125 | ||||
-rw-r--r-- | pb_decode.h | 16 | ||||
-rw-r--r-- | tests/test_decode1.c | 4 |
4 files changed, 56 insertions, 124 deletions
@@ -43,35 +43,28 @@ typedef enum { * 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 * @@ -108,8 +101,8 @@ typedef struct _pb_field_t pb_field_t; 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 */ diff --git a/pb_decode.c b/pb_decode.c index a722e89..a9ae735 100644 --- a/pb_decode.c +++ b/pb_decode.c @@ -7,18 +7,10 @@ #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, @@ -67,10 +59,9 @@ pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize) 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) @@ -197,9 +188,7 @@ bool decode_field(pb_istream_t *stream, int wire_type, const pb_field_t *field, 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; @@ -242,6 +231,7 @@ bool decode_field(pb_istream_t *stream, int wire_type, const pb_field_t *field, if (!pCallback->funcs.decode(&substream, field, pCallback->arg)) return false; } + return true; } else { @@ -280,23 +270,16 @@ bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struc 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; } } @@ -343,88 +326,52 @@ bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struc /* 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; diff --git a/pb_decode.h b/pb_decode.h index ac4d1f7..448b55d 100644 --- a/pb_decode.h +++ b/pb_decode.h @@ -45,17 +45,9 @@ bool pb_skip_string(pb_istream_t *stream); * 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); @@ -66,6 +58,6 @@ typedef bool (*pb_decoder_t)(pb_istream_t *stream, const pb_field_t *field, void /* --- 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 diff --git a/tests/test_decode1.c b/tests/test_decode1.c index 8623fbd..4e0d711 100644 --- a/tests/test_decode1.c +++ b/tests/test_decode1.c @@ -36,7 +36,7 @@ const pb_field_t Person_PhoneNumber_fields[] = { offsetof(Person_PhoneNumber, number), 0, membersize(Person_PhoneNumber, number), 0, 0}, - {2, PB_HTYPE_OPTIONAL | PB_LTYPE_ENUM, + {2, PB_HTYPE_OPTIONAL | PB_LTYPE_VARINT, offsetof(Person_PhoneNumber, type), offsetof(Person_PhoneNumber, has_type), membersize(Person_PhoneNumber, type), 0, @@ -50,7 +50,7 @@ const pb_field_t Person_fields[] = { offsetof(Person, name), 0, membersize(Person, name), 0, 0}, - {2, PB_HTYPE_REQUIRED | PB_LTYPE_INT32, + {2, PB_HTYPE_REQUIRED | PB_LTYPE_VARINT, offsetof(Person, id), 0, membersize(Person, id), 0, 0}, |