Fix an unsigned vs. signed warning on some compiler.
[apps/agl-service-can-low-level.git] / pb_decode.c
index 2692447..cada00c 100644 (file)
@@ -3,11 +3,19 @@
  * 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.
@@ -15,7 +23,8 @@ typedef bool (*pb_decoder_t)(pb_istream_t *stream, const pb_field_t *field, void
 static const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = {
     &pb_dec_varint,
     &pb_dec_svarint,
-    &pb_dec_fixed,
+    &pb_dec_fixed32,
+    &pb_dec_fixed64,
     
     &pb_dec_bytes,
     &pb_dec_string,
@@ -26,7 +35,7 @@ static const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = {
  * 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;
@@ -38,7 +47,7 @@ bool pb_read(pb_istream_t *stream, uint8_t *buf, size_t count)
     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;
     
@@ -62,15 +71,15 @@ pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize)
  * 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);
-    *dest = temp;
+    *dest = (uint32_t)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;
@@ -88,7 +97,7 @@ bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
     return false;
 }
 
-bool pb_skip_varint(pb_istream_t *stream)
+bool checkreturn pb_skip_varint(pb_istream_t *stream)
 {
     uint8_t byte;
     do
@@ -99,7 +108,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))
@@ -108,12 +117,33 @@ bool pb_skip_string(pb_istream_t *stream)
     return pb_read(stream, NULL, length);
 }
 
-/* Currently the 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 = 0;
+    *tag = 0;
+    
+    if (!pb_decode_varint32(stream, &temp))
+    {
+        if (stream->bytes_left == 0)
+            *eof = true;
+
+        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)
     {
@@ -128,7 +158,7 @@ static bool skip(pb_istream_t *stream, int 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)
@@ -155,8 +185,11 @@ static bool read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, uint8
     }
 }
 
-/* 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.
+ * Before disposing the substream, remember to copy the substream->state back
+ * to stream->state.
+ */
+static bool checkreturn make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
 {
     uint32_t size;
     if (!pb_decode_varint32(stream, &size))
@@ -173,19 +206,21 @@ static bool make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
 
 /* 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;
+    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->pData = dest_struct + iter->current->data_offset;
+    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;
 }
@@ -198,12 +233,16 @@ static bool pb_field_next(pb_field_iterator_t *iter)
     if (PB_HTYPE(iter->current->type) == PB_HTYPE_ARRAY)
         prev_size *= iter->current->array_size;
     
+    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;
@@ -214,7 +253,7 @@ static bool pb_field_next(pb_field_iterator_t *iter)
     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;
     
@@ -231,7 +270,7 @@ static bool pb_field_find(pb_field_iterator_t *iter, int tag)
  * 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, pb_wire_type_t wire_type, pb_field_iterator_t *iter)
 {
     pb_decoder_t func = PB_DECODERS[PB_LTYPE(iter->current->type)];
     
@@ -280,7 +319,7 @@ static bool decode_field(pb_istream_t *stream, int wire_type, pb_field_iterator_
             pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
             
             if (pCallback->funcs.decode == NULL)
-                return skip(stream, wire_type);
+                return pb_skip_field(stream, wire_type);
             
             if (wire_type == PB_WT_STRING)
             {
@@ -294,6 +333,8 @@ static bool decode_field(pb_istream_t *stream, int wire_type, pb_field_iterator_
                     if (!pCallback->funcs.decode(&substream, iter->current, pCallback->arg))
                         return false;
                 }
+                
+                stream->state = substream.state;
                 return true;
             }
             else
@@ -319,88 +360,104 @@ static bool decode_field(pb_istream_t *stream, int wire_type, pb_field_iterator_
     }
 }
 
-/*********************
- * Decode all fields *
- *********************/
-
-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)
 {
-    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;
+        
+        /* Initialize the size field for optional/repeated fields to 0. */
         if (PB_HTYPE(iter.current->type) == PB_HTYPE_OPTIONAL)
         {
             *(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(iter.current->type) == PB_HTYPE_ARRAY)
         {
             *(size_t*)iter.pSize = 0;
+            continue; /* Array is empty, no need to initialize contents */
+        }
+        
+        /* Initialize field contents to default value */
+        if (PB_HTYPE(iter.current->type) == PB_HTYPE_CALLBACK)
+        {
+            continue; /* Don't overwrite callback */
         }
-        else if (PB_HTYPE(iter.current->type) == PB_HTYPE_REQUIRED)
+        else if (PB_LTYPE(iter.current->type) == PB_LTYPE_SUBMESSAGE)
+        {
+            pb_message_set_to_defaults(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(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_message_set_to_defaults(fields, dest_struct);
+    
+    pb_field_init(&iter, fields, dest_struct);
     
     while (stream->bytes_left)
     {
-        uint32_t temp;
-        int tag, wire_type;
-        if (!pb_decode_varint32(stream, &temp))
+        uint32_t tag;
+        pb_wire_type_t wire_type;
+        bool eof;
+        
+        if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
         {
-            if (stream->bytes_left == 0)
-                break; /* It was EOF */
+            if (eof)
+                break;
             else
-                return false; /* It was error */
+                return false;
         }
         
-        if (temp == 0)
-            break; /* Special feature: allow 0-terminated messages. */
-        
-        tag = temp >> 3;
-        wire_type = temp & 7;
-        
         if (!pb_field_find(&iter, tag))
         {
             /* No match found, skip data */
-            if (!skip(stream, wire_type))
+            if (!pb_skip_field(stream, wire_type))
                 return false;
             continue;
         }
         
-        fields_seen |= 1 << (iter.field_index & 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, &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;
         }
-    }
+    } while (pb_field_next(&iter));
     
     return true;
 }
@@ -417,11 +474,12 @@ static void endian_copy(void *dest, void *src, size_t destsize, size_t srcsize)
 #ifdef __BIG_ENDIAN__
     memcpy(dest, (char*)src + (srcsize - destsize), destsize);
 #else
+    UNUSED(srcsize);
     memcpy(dest, src, destsize);
 #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);
@@ -429,7 +487,7 @@ bool pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
     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);
@@ -438,21 +496,49 @@ bool pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
     return status;
 }
 
-bool pb_dec_fixed(pb_istream_t *stream, const pb_field_t *field, void *dest)
+bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
+{
+#ifdef __BIG_ENDIAN__
+    uint8_t bytes[4] = {0};
+    bool status = pb_read(stream, bytes, 4);
+    if (status) {
+        uint8_t *d = (uint8_t*)dest;
+        d[0] = bytes[3];
+        d[1] = bytes[2];
+        d[2] = bytes[1];
+        d[3] = bytes[0];
+    }
+    return status;
+#else
+    UNUSED(field);
+    return pb_read(stream, (uint8_t*)dest, 4);
+#endif
+}
+
+bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
 {
 #ifdef __BIG_ENDIAN__
     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]};
-    endian_copy(dest, lebytes, field->data_size, 8);
+    bool status = pb_read(stream, bytes, 8);
+    if (status) {
+        uint8_t *d = (uint8_t*)dest;
+        d[0] = bytes[7];
+        d[1] = bytes[6];
+        d[2] = bytes[5]; 
+        d[3] = bytes[4];
+        d[4] = bytes[3];
+        d[5] = bytes[2];
+        d[6] = bytes[1];
+        d[7] = bytes[0];
+    }
     return status;
 #else
-    return pb_read(stream, (uint8_t*)dest, field->data_size);
+    UNUSED(field);
+    return pb_read(stream, (uint8_t*)dest, 8);
 #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;
     
@@ -461,20 +547,22 @@ bool pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
         return false;
     x->size = temp;
     
-    if (x->size > field->data_size)
+    /* 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;
     
     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;
     if (!pb_decode_varint32(stream, &size))
         return false;
     
-    if (size > field->data_size - 1)
+    /* Check length, noting the null terminator */
+    if (size + 1 > field->data_size)
         return false;
     
     status = pb_read(stream, (uint8_t*)dest, size);
@@ -482,8 +570,9 @@ bool pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
     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))
@@ -492,5 +581,7 @@ bool pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest
     if (field->ptr == NULL)
         return false;
     
-    return pb_decode(&substream, (pb_field_t*)field->ptr, dest);
+    status = pb_decode(&substream, (pb_field_t*)field->ptr, dest);
+    stream->state = substream.state;
+    return status;
 }