Check for empty message type before incrementing required_field_index.
[apps/agl-service-can-low-level.git] / pb_decode.c
index 7eda89a..a079556 100644 (file)
@@ -308,12 +308,12 @@ static bool pb_field_next(pb_field_iterator_t *iter)
         prev_size *= iter->pos->array_size;
     }
     
-    if (PB_HTYPE(iter->pos->type) == PB_HTYPE_REQUIRED)
-        iter->required_field_index++;
-    
     if (iter->pos->tag == 0)
         return false; /* Only happens with empty message types */
     
+    if (PB_HTYPE(iter->pos->type) == PB_HTYPE_REQUIRED)
+        iter->required_field_index++;
+    
     iter->pos++;
     iter->field_index++;
     if (iter->pos->tag == 0)
@@ -430,11 +430,11 @@ static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type
         if (!pb_make_string_substream(stream, &substream))
             return false;
         
-        while (substream.bytes_left)
+        do
         {
             if (!pCallback->funcs.decode(&substream, iter->pos, arg))
                 PB_RETURN_ERROR(stream, "callback failed");
-        }
+        } while (substream.bytes_left);
         
         pb_close_string_substream(stream, &substream);
         return true;
@@ -603,6 +603,19 @@ bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void
     return pb_decode_noinit(stream, fields, dest_struct);
 }
 
+bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
+{
+    pb_istream_t substream;
+    bool status;
+    
+    if (!pb_make_string_substream(stream, &substream))
+        return false;
+    
+    status = pb_decode(&substream, fields, dest_struct);
+    pb_close_string_substream(stream, &substream);
+    return status;
+}
+
 /* Field decoders */
 
 bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)