Implement extension support for the encoder
[apps/agl-service-can-low-level.git] / pb_decode.c
index e727f33..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)
@@ -664,7 +677,8 @@ bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
 bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
 {
     uint64_t value;
-    bool status = pb_decode_varint(stream, &value);
+    if (!pb_decode_varint(stream, &value))
+        return false;
     
     switch (field->data_size)
     {
@@ -675,13 +689,14 @@ bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, vo
         default: PB_RETURN_ERROR(stream, "invalid data_size");
     }
     
-    return status;
+    return true;
 }
 
 bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
 {
     int64_t value;
-    bool status = pb_decode_svarint(stream, &value);
+    if (!pb_decode_svarint(stream, &value))
+        return false;
     
     switch (field->data_size)
     {
@@ -690,7 +705,7 @@ bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, v
         default: PB_RETURN_ERROR(stream, "invalid data_size");
     }
     
-    return status;
+    return true;
 }
 
 bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)