Add pb_decode_noinit and use it from pb_dec_submessage.
[apps/agl-service-can-low-level.git] / pb_decode.c
index 28856d3..601dc74 100644 (file)
@@ -3,44 +3,52 @@
  * 2011 Petteri Aimonen <jpa@kapsi.fi>
  */
 
+/* The warn_unused_result attribute appeared first in gcc-3.4.0 */
+#if !defined(__GNUC__) || ( __GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
+    #define checkreturn
+#else
+    /* Verify that we remember to check all return values for proper error propagation */
+    #define checkreturn __attribute__((warn_unused_result))
+#endif
+
+#define NANOPB_INTERNALS
 #include "pb.h"
 #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,
-    
-    (pb_decoder_t)&pb_dec_bytes,
-    (pb_decoder_t)&pb_dec_string,
-    (pb_decoder_t)&pb_dec_submessage
+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.
+ */
+static const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = {
+    &pb_dec_varint,
+    &pb_dec_svarint,
+    &pb_dec_fixed32,
+    &pb_dec_fixed64,
+    
+    &pb_dec_bytes,
+    &pb_dec_string,
+    &pb_dec_submessage
 };
 
 /**************
  * 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)
 {
-    bool status;
     if (stream->bytes_left < count)
-        return false;
+        PB_RETURN_ERROR(stream, "end-of-stream");
+    
+    if (!stream->callback(stream, buf, count))
+        PB_RETURN_ERROR(stream, "io error");
     
-    status = stream->callback(stream, buf, count);
     stream->bytes_left -= count;
-    return status;
+    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;
     
@@ -64,34 +72,33 @@ pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize)
  * Helper functions *
  ********************/
 
-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;
-    if (!pb_decode_varint64(stream, &temp))
-        return false;
-    *dest = temp;
-    return true;
+    bool status = pb_decode_varint(stream, &temp);
+    *dest = (uint32_t)temp;
+    return status;
 }
 
-bool pb_decode_varint64(pb_istream_t *stream, uint64_t *dest)
+bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
 {
     uint8_t byte;
-    int bitpos = 0;
+    uint8_t bitpos = 0;
     *dest = 0;
     
     while (bitpos < 64 && pb_read(stream, &byte, 1))
     {
-        *dest |= (byte & 0x7F) << bitpos;
+        *dest |= (uint64_t)(byte & 0x7F) << bitpos;
         bitpos += 7;
         
         if (!(byte & 0x80))
             return true;
     }
     
-    return false;
+    PB_RETURN_ERROR(stream, "varint overflow");
 }
 
-bool pb_skip_varint(pb_istream_t *stream)
+bool checkreturn pb_skip_varint(pb_istream_t *stream)
 {
     uint8_t byte;
     do
@@ -102,7 +109,7 @@ bool pb_skip_varint(pb_istream_t *stream)
     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))
@@ -111,61 +118,78 @@ bool pb_skip_string(pb_istream_t *stream)
     return pb_read(stream, NULL, length);
 }
 
-/* Currently all 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, uint32_t *tag, bool *eof)
+{
+    uint32_t temp;
+    *eof = false;
+    *wire_type = (pb_wire_type_t) 0;
+    *tag = 0;
+    
+    if (!pb_decode_varint32(stream, &temp))
+    {
+        if (stream->bytes_left == 0)
+            *eof = true;
 
-enum wire_type_t {
-    WT_VARINT = 0,
-    WT_64BIT  = 1,
-    WT_STRING = 2,
-    WT_32BIT  = 5
-};
+        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 skip(pb_istream_t *stream, int wire_type)
+bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t 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);
-        default: return false;
+        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: PB_RETURN_ERROR(stream, "invalid wire_type");
     }
 }
 
-/* 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 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)
     {
-        case WT_VARINT:
+        case PB_WT_VARINT:
             *size = 0;
             do
             {
                 (*size)++;
                 if (*size > max_size) return false;
-                if (!pb_read(stream, buf++, 1)) return false;
-            } while (*buf & 0x80);
+                if (!pb_read(stream, buf, 1)) return false;
+            } 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);
         
-        default: return false;
+        default: PB_RETURN_ERROR(stream, "invalid 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)
+/* Decode string length from stream and return a substream with limited length.
+ * Remember to close the substream using pb_close_string_substream().
+ */
+bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
 {
     uint32_t size;
     if (!pb_decode_varint32(stream, &size))
@@ -173,75 +197,153 @@ static bool make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
     
     *substream = *stream;
     if (substream->bytes_left < size)
-        return false;
+        PB_RETURN_ERROR(stream, "parent stream too short");
     
     substream->bytes_left = size;
     stream->bytes_left -= size;
     return true;
 }
 
-bool decode_field(pb_istream_t *stream, int wire_type, const pb_field_t *field, void *dest_struct)
+void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
 {
-    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;
+    stream->state = substream->state;
+}
+
+/* Iterator for pb_field_t list */
+typedef struct {
+    const pb_field_t *start; /* Start of the pb_field_t array */
+    const pb_field_t *current; /* Current position of the iterator */
+    int field_index; /* Zero-based index of the field. */
+    int required_field_index; /* Zero-based index that counts only the required fields */
+    void *dest_struct; /* Pointer to the destination structure to decode to */
+    void *pData; /* Pointer where to store current field value */
+    void *pSize; /* Pointer where to store the size of current array field */
+} 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->required_field_index = 0;
+    iter->pData = (char*)dest_struct + iter->current->data_offset;
+    iter->pSize = (char*)iter->pData + iter->current->size_offset;
+    iter->dest_struct = dest_struct;
+}
+
+static bool pb_field_next(pb_field_iterator_t *iter)
+{
+    bool notwrapped = true;
+    size_t prev_size = iter->current->data_size;
+    
+    if (PB_HTYPE(iter->current->type) == PB_HTYPE_ARRAY)
+        prev_size *= iter->current->array_size;
     
-    switch (PB_HTYPE(field->type))
+    if (PB_HTYPE(iter->current->type) == PB_HTYPE_REQUIRED)
+        iter->required_field_index++;
+    
+    iter->current++;
+    iter->field_index++;
+    if (iter->current->tag == 0)
+    {
+        iter->current = iter->start;
+        iter->field_index = 0;
+        iter->required_field_index = 0;
+        iter->pData = iter->dest_struct;
+        prev_size = 0;
+        notwrapped = false;
+    }
+    
+    iter->pData = (char*)iter->pData + prev_size + iter->current->data_offset;
+    iter->pSize = (char*)iter->pData + iter->current->size_offset;
+    return notwrapped;
+}
+
+static bool checkreturn pb_field_find(pb_field_iterator_t *iter, uint32_t 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 *
+ *************************/
+
+static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t 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;
+                bool status;
+                size_t *size = (size_t*)iter->pSize;
                 pb_istream_t substream;
-                if (!make_string_substream(stream, &substream))
+                if (!pb_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(&substream, iter->current, pItem))
                         return false;
                     (*size)++;
                 }
-                return (substream.bytes_left == 0);
+                status = (substream.bytes_left == 0);
+                pb_close_string_substream(stream, &substream);
+                return status;
             }
             else
             {
                 /* Repeated field */
-                size_t *size = (size_t*)pSize;
-                void *pItem = (uint8_t*)pData + field->data_size * (*size);
-                if (*size >= field->array_size)
-                    return false;
+                size_t *size = (size_t*)iter->pSize;
+                void *pItem = (uint8_t*)iter->pData + iter->current->data_size * (*size);
+                if (*size >= iter->current->array_size)
+                    PB_RETURN_ERROR(stream, "array overflow");
                 
                 (*size)++;
-                return func(stream, field, pItem);
+                return func(stream, iter->current, pItem);
             }
         
         case PB_HTYPE_CALLBACK:
-            if (wire_type == WT_STRING)
+        {
+            pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
+            
+            if (pCallback->funcs.decode == NULL)
+                return pb_skip_field(stream, wire_type);
+            
+            if (wire_type == PB_WT_STRING)
             {
-                pb_callback_t *pCallback = (pb_callback_t*)pData;
                 pb_istream_t substream;
                 
-                if (!make_string_substream(stream, &substream))
+                if (!pb_make_string_substream(stream, &substream))
                     return false;
                 
                 while (substream.bytes_left)
                 {
-                    if (!pCallback->funcs.decode(&substream, field, pCallback->arg))
-                        return false;
+                    if (!pCallback->funcs.decode(&substream, iter->current, pCallback->arg))
+                        PB_RETURN_ERROR(stream, "callback failed");
                 }
+                
+                pb_close_string_substream(stream, &substream);
+                return true;
             }
             else
             {
@@ -250,7 +352,6 @@ bool decode_field(pb_istream_t *stream, int wire_type, const pb_field_t *field,
                  * 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;
                 uint8_t buffer[10];
                 size_t size = sizeof(buffer);
                 
@@ -258,174 +359,224 @@ bool decode_field(pb_istream_t *stream, int wire_type, const pb_field_t *field,
                     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:
-            return false;
+            PB_RETURN_ERROR(stream, "invalid field type");
     }
 }
 
-bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
+/* Initialize message fields to default values, recursively */
+static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
 {
-    /* Used to check for required fields */
-    uint32_t fields_seen = 0;
-    int i;
+    pb_field_iterator_t iter;
+    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 (iter.current->tag == 0)
+            continue;
+        
+        /* Initialize the size field for optional/repeated fields to 0. */
+        if (PB_HTYPE(iter.current->type) == PB_HTYPE_OPTIONAL)
         {
-            *(bool*)pSize = false;
+            *(bool*)iter.pSize = false;
         }
-        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;
+            continue; /* Array is empty, no need to initialize contents */
         }
         
-        if (PB_HTYPE(fields[i].type) != PB_HTYPE_ARRAY &&
-            PB_HTYPE(fields[i].type) != PB_HTYPE_CALLBACK)
+        /* Initialize field contents to default value */
+        if (PB_HTYPE(iter.current->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);
-            }
+            continue; /* Don't overwrite callback */
         }
-    }
+        else if (PB_LTYPE(iter.current->type) == PB_LTYPE_SUBMESSAGE)
+        {
+            pb_message_set_to_defaults((const pb_field_t *) iter.current->ptr, iter.pData);
+        }
+        else if (iter.current->ptr != NULL)
+        {
+            memcpy(iter.pData, iter.current->ptr, iter.current->data_size);
+        }
+        else
+        {
+            memset(iter.pData, 0, iter.current->data_size);
+        }
+    } while (pb_field_next(&iter));
+}
+
+/*********************
+ * Decode all fields *
+ *********************/
+
+bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
+{
+    uint8_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 7) / 8] = {0}; /* Used to check for required fields */
+    pb_field_iterator_t iter;
+    
+    pb_field_init(&iter, fields, dest_struct);
     
     while (stream->bytes_left)
     {
-        uint32_t temp;
-        int tag, wire_type;
-        if (!pb_decode_varint32(stream, &temp))
-            return false;
-        
-        tag = temp >> 3;
-        wire_type = temp & 7;
+        uint32_t tag;
+        pb_wire_type_t wire_type;
+        bool eof;
         
-        i = 0;
-        while (fields[i].tag != 0 && fields[i].tag != tag)
+        if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
         {
-            i++;
+            if (eof)
+                break;
+            else
+                return false;
         }
         
-        if (fields[i].tag == 0) /* No match found, skip data */
+        if (!pb_field_find(&iter, tag))
         {
-            skip(stream, wire_type);
+            /* No match found, skip data */
+            if (!pb_skip_field(stream, wire_type))
+                return false;
             continue;
         }
         
-        fields_seen |= 1 << (i & 31);
+        if (PB_HTYPE(iter.current->type) == PB_HTYPE_REQUIRED
+            && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
+        {
+            fields_seen[iter.required_field_index >> 3] |= 1 << (iter.required_field_index & 7);
+        }
             
-        if (!decode_field(stream, wire_type, &fields[i], dest_struct))
+        if (!decode_field(stream, wire_type, &iter))
             return false;
     }
     
-    /* Check that all required fields (mod 31) were present. */
-    for (i = 0; fields[i].tag != 0; i++)
-    {
-        if (PB_HTYPE(fields[i].type) == PB_HTYPE_REQUIRED &&
-            !(fields_seen & (1 << (i & 31))))
+    /* Check that all required fields were present. */
+    pb_field_init(&iter, fields, dest_struct);
+    do {
+        if (PB_HTYPE(iter.current->type) == PB_HTYPE_REQUIRED &&
+            iter.required_field_index < PB_MAX_REQUIRED_FIELDS &&
+            !(fields_seen[iter.required_field_index >> 3] & (1 << (iter.required_field_index & 7))))
         {
-            return false;
+            PB_RETURN_ERROR(stream, "missing required field");
         }
-    }
+    } while (pb_field_next(&iter));
     
     return true;
 }
 
-/* Field decoders */
-
-bool pb_dec_uint32(pb_istream_t *stream, const pb_field_t *field, uint32_t *dest)
+bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
 {
-    return pb_decode_varint32(stream, dest);
+    pb_message_set_to_defaults(fields, dest_struct);
+    return pb_decode_noinit(stream, fields, dest_struct);
 }
 
-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;
-}
+/* Field decoders */
 
-bool pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, uint32_t *dest)
+bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
 {
-    uint8_t bytes[4] = {0};
-    bool status = pb_read(stream, bytes, 4);
+    uint64_t value;
+    if (!pb_decode_varint(stream, &value))
+        return false;
     
-#ifdef __BIG_ENDIAN__
-    uint8_t lebytes[4] = {bytes[3], bytes[2], bytes[1], bytes[0]};
-    memcpy(dest, lebytes, 4);
-#else
-    memcpy(dest, bytes, 4);
-#endif
-    return status;
-}
-
-bool pb_dec_uint64(pb_istream_t *stream, const pb_field_t *field, uint64_t *dest)
-{
-    return pb_decode_varint64(stream, dest);
+    if (value & 1)
+        *dest = ~(value >> 1);
+    else
+        *dest = value >> 1;
+    
+    return true;
 }
 
-bool pb_dec_sint64(pb_istream_t *stream, const pb_field_t *field, int64_t *dest)
+bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
 {
-    uint64_t *x = (uint64_t*)dest;
-    bool status = pb_decode_varint64(stream, x);
-    *x = (*x >> 1) ^ -(int64_t)(*x & 1);
-    return status;
+    #ifdef __BIG_ENDIAN__
+    uint8_t *bytes = (uint8_t*)dest;
+    uint8_t lebytes[4];
+    
+    if (!pb_read(stream, lebytes, 4))
+        return false;
+    
+    bytes[0] = lebytes[3];
+    bytes[1] = lebytes[2];
+    bytes[2] = lebytes[1];
+    bytes[3] = lebytes[0];
+    return true;
+    #else
+    return pb_read(stream, (uint8_t*)dest, 4);
+    #endif   
 }
 
-bool pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, uint64_t *dest)
+bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
 {
-    uint8_t bytes[8] = {0};
-    bool status = pb_read(stream, bytes, 8);
+    #ifdef __BIG_ENDIAN__
+    uint8_t *bytes = (uint8_t*)dest;
+    uint8_t lebytes[8];
     
-#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);
-#else
-    memcpy(dest, bytes, 4);
-#endif
-    return status;
+    if (!pb_read(stream, lebytes, 8))
+        return false;
+    
+    bytes[0] = lebytes[7];
+    bytes[1] = lebytes[6];
+    bytes[2] = lebytes[5];
+    bytes[3] = lebytes[4];
+    bytes[4] = lebytes[3];
+    bytes[5] = lebytes[2];
+    bytes[6] = lebytes[1];
+    bytes[7] = lebytes[0];
+    return true;
+    #else
+    return pb_read(stream, (uint8_t*)dest, 8);
+    #endif   
 }
 
-bool pb_dec_bool(pb_istream_t *stream, const pb_field_t *field, bool *dest)
+bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
 {
-    uint32_t temp = 0;
-    bool status = pb_decode_varint32(stream, &temp);
-    *(bool*)dest = !!temp;
+    uint64_t value;
+    bool status = pb_decode_varint(stream, &value);
+    
+    switch (field->data_size)
+    {
+        case 1: *(uint8_t*)dest = value; break;
+        case 2: *(uint16_t*)dest = value; break;
+        case 4: *(uint32_t*)dest = value; break;
+        case 8: *(uint64_t*)dest = value; break;
+        default: PB_RETURN_ERROR(stream, "invalid data_size");
+    }
+    
     return status;
 }
 
-bool pb_dec_enum(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)
 {
-    /* 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);
+    int64_t value;
+    bool status = pb_decode_svarint(stream, &value);
+    
+    switch (field->data_size)
+    {
+        case 4: *(int32_t*)dest = value; break;
+        case 8: *(int64_t*)dest = value; break;
+        default: PB_RETURN_ERROR(stream, "invalid data_size");
+    }
+    
     return status;
 }
 
-bool pb_dec_float(pb_istream_t *stream, const pb_field_t *field, float *dest)
+bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
 {
-    return pb_read(stream, (uint8_t*)dest, sizeof(float));
+    UNUSED(field);
+    return pb_decode_fixed32(stream, dest);
 }
 
-bool pb_dec_double(pb_istream_t *stream, const pb_field_t *field, double *dest)
+bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
 {
-    return pb_read(stream, (uint8_t*)dest, sizeof(double));
+    UNUSED(field);
+    return pb_decode_fixed64(stream, dest);
 }
 
-bool pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, uint8_t *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;
     
@@ -434,38 +585,41 @@ bool pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, uint8_t *dest)
         return false;
     x->size = temp;
     
-    /* Note: data_size includes the size of the x.size field, too.
-     * Calculate actual size starting from offset. */
+    /* 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;
+        PB_RETURN_ERROR(stream, "bytes overflow");
     
     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 checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
 {
     uint32_t size;
     bool status;
     if (!pb_decode_varint32(stream, &size))
         return false;
     
-    if (size > field->data_size - 1)
-        return false;
+    /* Check length, noting the null terminator */
+    if (size + 1 > field->data_size)
+        PB_RETURN_ERROR(stream, "string overflow");
     
     status = pb_read(stream, (uint8_t*)dest, size);
     *((uint8_t*)dest + size) = 0;
     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)
 {
+    bool status;
     pb_istream_t substream;
     
-    if (!make_string_substream(stream, &substream))
+    if (!pb_make_string_substream(stream, &substream))
         return false;
     
     if (field->ptr == NULL)
-        return false;
+        PB_RETURN_ERROR(stream, "invalid field descriptor");
     
-    return pb_decode(&substream, (pb_field_t*)field->ptr, dest);
+    status = pb_decode_noinit(&substream, (pb_field_t*)field->ptr, dest);
+    pb_close_string_substream(stream, &substream);
+    return status;
 }