Add PB_MANY_FIELDS option for supporting fields > 255.
[apps/agl-service-can-low-level.git] / pb_decode.c
index eace906..458d5f1 100644 (file)
@@ -117,12 +117,33 @@ bool checkreturn 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, int *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 checkreturn skip(pb_istream_t *stream, pb_wire_type_t wire_type)
+bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
 {
     switch (wire_type)
     {
@@ -185,18 +206,20 @@ static bool checkreturn make_string_substream(pb_istream_t *stream, pb_istream_t
 
 /* 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->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;
@@ -210,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;
@@ -292,7 +319,7 @@ static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_t
             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)
             {
@@ -382,9 +409,8 @@ static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_str
 
 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 */
+    uint8_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 7) / 8] = {}; /* Used to check for required fields */
     pb_field_iterator_t iter;
-    int i;
     
     pb_message_set_to_defaults(fields, dest_struct);
     
@@ -392,46 +418,46 @@ bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void
     
     while (stream->bytes_left)
     {
-        uint32_t temp;
         int tag;
         pb_wire_type_t wire_type;
-        if (!pb_decode_varint32(stream, &temp))
+        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 = (pb_wire_type_t)(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;
 }