Change the substream implementation in pb_decode.

This makes it unnecessary to copy back the state, and also relaxes
the requirements on callbacks (bytes_left will always be valid).
It decreases code size by a few bytes, but may be just slightly slower.
This commit is contained in:
Petteri Aimonen
2012-08-24 19:35:17 +03:00
parent 86257a2a70
commit dc2da0edc5
2 changed files with 13 additions and 20 deletions

View File

@@ -185,22 +185,26 @@ static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire
} }
} }
/* Decode string length from stream and return a substream with limited length. /* Decode string length from stream and return a substream with limited length. */
* Before disposing the substream, remember to copy the substream->state back static bool substream_callback(pb_istream_t *stream, uint8_t *buf, size_t count)
* to stream->state. {
*/ pb_istream_t *parent = (pb_istream_t*)stream->state;
return pb_read(parent, buf, count);
}
static bool checkreturn make_string_substream(pb_istream_t *stream, pb_istream_t *substream) static bool checkreturn make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
{ {
uint32_t size; uint32_t size;
if (!pb_decode_varint32(stream, &size)) if (!pb_decode_varint32(stream, &size))
return false; return false;
*substream = *stream; if (stream->bytes_left < size)
if (substream->bytes_left < size)
return false; return false;
substream->callback = &substream_callback;
substream->state = stream;
substream->bytes_left = size; substream->bytes_left = size;
stream->bytes_left -= size;
return true; return true;
} }
@@ -288,7 +292,6 @@ static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_t
&& PB_LTYPE(iter->current->type) <= PB_LTYPE_LAST_PACKABLE) && PB_LTYPE(iter->current->type) <= PB_LTYPE_LAST_PACKABLE)
{ {
/* Packed array */ /* Packed array */
bool status;
size_t *size = (size_t*)iter->pSize; size_t *size = (size_t*)iter->pSize;
pb_istream_t substream; pb_istream_t substream;
if (!make_string_substream(stream, &substream)) if (!make_string_substream(stream, &substream))
@@ -301,9 +304,7 @@ static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_t
return false; return false;
(*size)++; (*size)++;
} }
status = (substream.bytes_left == 0); return (substream.bytes_left == 0);
stream->state = substream.state;
return status;
} }
else else
{ {
@@ -337,7 +338,6 @@ static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_t
return false; return false;
} }
stream->state = substream.state;
return true; return true;
} }
else else
@@ -575,7 +575,6 @@ bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, vo
bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest) bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
{ {
bool status;
pb_istream_t substream; pb_istream_t substream;
if (!make_string_substream(stream, &substream)) if (!make_string_substream(stream, &substream))
@@ -584,7 +583,5 @@ bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field
if (field->ptr == NULL) if (field->ptr == NULL)
return false; return false;
status = pb_decode(&substream, (pb_field_t*)field->ptr, dest); return pb_decode(&substream, (pb_field_t*)field->ptr, dest);
stream->state = substream.state;
return status;
} }

View File

@@ -23,10 +23,6 @@
* *
* 3) You can use state to store your own data (e.g. buffer pointer), * 3) You can use state to store your own data (e.g. buffer pointer),
* and rely on pb_read to verify that no-body reads past bytes_left. * and rely on pb_read to verify that no-body reads past bytes_left.
*
* 4) Your callback may be used with substreams, in which case bytes_left
* is different than from the main stream. Don't use bytes_left to compute
* any pointers.
*/ */
struct _pb_istream_t struct _pb_istream_t
{ {