Allow defining field type in .proto.
[apps/agl-service-can-low-level.git] / pb_decode.c
index 601dc74..8e01fd7 100644 (file)
@@ -36,26 +36,41 @@ static const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = {
  * pb_istream *
  **************/
 
-bool checkreturn pb_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)
 {
-    if (stream->bytes_left < count)
-        PB_RETURN_ERROR(stream, "end-of-stream");
+    uint8_t *source = (uint8_t*)stream->state;
     
-    if (!stream->callback(stream, buf, count))
-        PB_RETURN_ERROR(stream, "io error");
+    if (buf != NULL)
+        memcpy(buf, source, count);
     
-    stream->bytes_left -= count;
+    stream->state = source + count;
     return true;
 }
 
-static bool checkreturn buf_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)
 {
-    uint8_t *source = (uint8_t*)stream->state;
+       if (buf == NULL && stream->callback != buf_read)
+       {
+               /* Skip input bytes */
+               uint8_t tmp[16];
+               while (count > 16)
+               {
+                       if (!pb_read(stream, tmp, 16))
+                               return false;
+                       
+                       count -= 16;
+               }
+               
+               return pb_read(stream, tmp, count);
+       }
+
+    if (stream->bytes_left < count)
+        PB_RETURN_ERROR(stream, "end-of-stream");
     
-    if (buf != NULL)
-        memcpy(buf, source, count);
+    if (!stream->callback(stream, buf, count))
+        PB_RETURN_ERROR(stream, "io error");
     
-    stream->state = source + count;
+    stream->bytes_left -= count;
     return true;
 }
 
@@ -83,7 +98,7 @@ static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
 bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
 {
     uint8_t byte;
-    uint8_t bitpos = 0;
+    int bitpos = 0;
     *dest = 0;
     
     while (bitpos < 64 && pb_read(stream, &byte, 1))
@@ -447,7 +462,7 @@ bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[
         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);
+            fields_seen[iter.required_field_index >> 3] |= (uint8_t)(1 << (iter.required_field_index & 7));
         }
             
         if (!decode_field(stream, wire_type, &iter))
@@ -483,9 +498,9 @@ bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
         return false;
     
     if (value & 1)
-        *dest = ~(value >> 1);
+        *dest = (int64_t)(~(value >> 1));
     else
-        *dest = value >> 1;
+        *dest = (int64_t)(value >> 1);
     
     return true;
 }
@@ -539,9 +554,9 @@ bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, vo
     
     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 1: *(uint8_t*)dest = (uint8_t)value; break;
+        case 2: *(uint16_t*)dest = (uint16_t)value; break;
+        case 4: *(uint32_t*)dest = (uint32_t)value; break;
         case 8: *(uint64_t*)dest = value; break;
         default: PB_RETURN_ERROR(stream, "invalid data_size");
     }
@@ -556,7 +571,7 @@ bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, v
     
     switch (field->data_size)
     {
-        case 4: *(int32_t*)dest = value; break;
+        case 4: *(int32_t*)dest = (int32_t)value; break;
         case 8: *(int64_t*)dest = value; break;
         default: PB_RETURN_ERROR(stream, "invalid data_size");
     }
@@ -612,6 +627,7 @@ bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field
 {
     bool status;
     pb_istream_t substream;
+    const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr;
     
     if (!pb_make_string_substream(stream, &substream))
         return false;
@@ -619,7 +635,13 @@ bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field
     if (field->ptr == NULL)
         PB_RETURN_ERROR(stream, "invalid field descriptor");
     
-    status = pb_decode_noinit(&substream, (pb_field_t*)field->ptr, dest);
+    /* New array entries need to be initialized, while required and optional
+     * submessages have already been initialized in the top-level pb_decode. */
+    if (PB_HTYPE(field->type) == PB_HTYPE_ARRAY)
+        status = pb_decode(&substream, submsg_fields, dest);
+    else
+        status = pb_decode_noinit(&substream, submsg_fields, dest);
+    
     pb_close_string_substream(stream, &substream);
     return status;
 }