Allow defining field type in .proto.
[apps/agl-service-can-low-level.git] / pb_decode.c
index 86dec4b..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;
 }