Remove the "buf = NULL" => skip requirement from pb_istream_t callbacks.
Rationale: it's easy to implement the callback wrong. Doing so introduces io errors when unknown fields are present in the input. If code is not tested with unknown fields, these bugs can remain hidden for long time. Added a special case for the memory buffer stream, where it gives a small speed benefit. Added testcase for skipping fields with test_decode2 implementation. Update issue 37 Status: FixedInGit
This commit is contained in:
39
pb_decode.c
39
pb_decode.c
@@ -36,18 +36,6 @@ 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)
|
||||
{
|
||||
if (stream->bytes_left < count)
|
||||
PB_RETURN_ERROR(stream, "end-of-stream");
|
||||
|
||||
if (!stream->callback(stream, buf, count))
|
||||
PB_RETURN_ERROR(stream, "io error");
|
||||
|
||||
stream->bytes_left -= count;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t count)
|
||||
{
|
||||
uint8_t *source = (uint8_t*)stream->state;
|
||||
@@ -59,6 +47,33 @@ static bool checkreturn buf_read(pb_istream_t *stream, uint8_t *buf, size_t coun
|
||||
return true;
|
||||
}
|
||||
|
||||
bool checkreturn pb_read(pb_istream_t *stream, uint8_t *buf, size_t count)
|
||||
{
|
||||
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 (!stream->callback(stream, buf, count))
|
||||
PB_RETURN_ERROR(stream, "io error");
|
||||
|
||||
stream->bytes_left -= count;
|
||||
return true;
|
||||
}
|
||||
|
||||
pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize)
|
||||
{
|
||||
pb_istream_t stream;
|
||||
|
||||
Reference in New Issue
Block a user