* 2011 Petteri Aimonen <jpa@kapsi.fi>
*/
+
+#ifdef __GNUC__
+/* Verify that we remember to check all return values for proper error propagation */
+#define checkreturn __attribute__((warn_unused_result))
+#else
+#define checkreturn
+#endif
+
#include "pb.h"
#include "pb_decode.h"
#include <string.h>
-typedef bool (*pb_decoder_t)(pb_istream_t *stream, const pb_field_t *field, void *dest);
+typedef bool (*pb_decoder_t)(pb_istream_t *stream, const pb_field_t *field, void *dest) checkreturn;
/* --- Function pointers to field decoders ---
* Order in the array must match pb_action_t LTYPE numbering.
* pb_istream *
**************/
-bool pb_read(pb_istream_t *stream, uint8_t *buf, size_t count)
+bool checkreturn pb_read(pb_istream_t *stream, uint8_t *buf, size_t count)
{
if (stream->bytes_left < count)
return false;
return true;
}
-static bool buf_read(pb_istream_t *stream, uint8_t *buf, size_t count)
+static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count)
{
uint8_t *source = (uint8_t*)stream->state;
* Helper functions *
********************/
-static bool pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
+static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
{
uint64_t temp;
bool status = pb_decode_varint(stream, &temp);
return status;
}
-bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
+bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
{
uint8_t byte;
uint8_t bitpos = 0;
return false;
}
-bool pb_skip_varint(pb_istream_t *stream)
+bool checkreturn pb_skip_varint(pb_istream_t *stream)
{
uint8_t byte;
do
return true;
}
-bool pb_skip_string(pb_istream_t *stream)
+bool checkreturn pb_skip_string(pb_istream_t *stream)
{
uint32_t length;
if (!pb_decode_varint32(stream, &length))
* to just assume the correct type and fail safely on corrupt message.
*/
-static bool skip(pb_istream_t *stream, int wire_type)
+static bool checkreturn skip(pb_istream_t *stream, int wire_type)
{
switch (wire_type)
{
/* 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)
+static bool checkreturn 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)
}
/* Decode string length from stream and return a substream with limited length */
-static bool make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
+static bool checkreturn make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
{
uint32_t size;
if (!pb_decode_varint32(stream, &size))
return notwrapped;
}
-static bool pb_field_find(pb_field_iterator_t *iter, int tag)
+static bool checkreturn pb_field_find(pb_field_iterator_t *iter, int tag)
{
int start = iter->field_index;
* Decode a single field *
*************************/
-static bool decode_field(pb_istream_t *stream, int wire_type, pb_field_iterator_t *iter)
+static bool checkreturn 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)];
while (substream.bytes_left && *size < iter->current->array_size)
{
void *pItem = (uint8_t*)iter->pData + iter->current->data_size * (*size);
- if (!func(stream, iter->current, pItem))
+ if (!func(&substream, iter->current, pItem))
return false;
(*size)++;
}
* Decode all fields *
*********************/
-bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
+bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
{
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 */
do
{
+ if (iter.current->tag == 0)
+ continue;
+
if (PB_HTYPE(iter.current->type) == PB_HTYPE_OPTIONAL)
{
*(bool*)iter.pSize = false;
if (!pb_field_find(&iter, tag))
{
/* No match found, skip data */
- skip(stream, wire_type);
+ if (!skip(stream, wire_type))
+ return false;
continue;
}
#endif
}
-bool pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
+bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
uint64_t temp;
bool status = pb_decode_varint(stream, &temp);
return status;
}
-bool pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
+bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
uint64_t temp;
bool status = pb_decode_varint(stream, &temp);
return status;
}
-bool pb_dec_fixed(pb_istream_t *stream, const pb_field_t *field, void *dest)
+bool checkreturn pb_dec_fixed(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
#ifdef __BIG_ENDIAN__
uint8_t bytes[8] = {0};
#endif
}
-bool pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
+bool checkreturn 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, void *dest)
+bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
uint32_t size;
bool status;
return status;
}
-bool pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
+bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
pb_istream_t substream;