summaryrefslogtreecommitdiffstats
path: root/pb_decode.c
diff options
context:
space:
mode:
Diffstat (limited to 'pb_decode.c')
-rw-r--r--pb_decode.c37
1 files changed, 26 insertions, 11 deletions
diff --git a/pb_decode.c b/pb_decode.c
index 86dec4b7..8e01fd7a 100644
--- a/pb_decode.c
+++ b/pb_decode.c
@@ -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;
}