Complete initialization of pb_istream_t.
[apps/agl-service-can-low-level.git] / pb_decode.c
index 65db511..c9652af 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;
 }
 
@@ -65,6 +80,9 @@ pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize)
     stream.callback = &buf_read;
     stream.state = buf;
     stream.bytes_left = bufsize;
+#ifndef PB_NO_ERRMSG
+    stream.errmsg = NULL;
+#endif
     return stream;
 }
 
@@ -83,7 +101,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 +465,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 +501,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;
 }