aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>2012-08-24 19:35:17 +0300
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>2012-08-24 19:35:17 +0300
commitdc2da0edc568b29361479fb7405c96b1a13442cf (patch)
treeeb7a4143f25bb3f90686f6f8f34a2377e86be7f3
parent86257a2a7000875c8ec1e2eef59de258400ac724 (diff)
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.
-rw-r--r--pb_decode.c29
-rw-r--r--pb_decode.h4
2 files changed, 13 insertions, 20 deletions
diff --git a/pb_decode.c b/pb_decode.c
index 4806832..9df4a89 100644
--- a/pb_decode.c
+++ b/pb_decode.c
@@ -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.
- * Before disposing the substream, remember to copy the substream->state back
- * to stream->state.
- */
+/* Decode string length from stream and return a substream with limited length. */
+static bool substream_callback(pb_istream_t *stream, uint8_t *buf, size_t count)
+{
+ 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)
{
uint32_t size;
if (!pb_decode_varint32(stream, &size))
return false;
- *substream = *stream;
- if (substream->bytes_left < size)
+ if (stream->bytes_left < size)
return false;
+ substream->callback = &substream_callback;
+ substream->state = stream;
substream->bytes_left = size;
- stream->bytes_left -= size;
+
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)
{
/* Packed array */
- bool status;
size_t *size = (size_t*)iter->pSize;
pb_istream_t 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;
(*size)++;
}
- status = (substream.bytes_left == 0);
- stream->state = substream.state;
- return status;
+ return (substream.bytes_left == 0);
}
else
{
@@ -337,7 +338,6 @@ static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_t
return false;
}
- stream->state = substream.state;
return true;
}
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 status;
pb_istream_t 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)
return false;
- status = pb_decode(&substream, (pb_field_t*)field->ptr, dest);
- stream->state = substream.state;
- return status;
+ return pb_decode(&substream, (pb_field_t*)field->ptr, dest);
}
diff --git a/pb_decode.h b/pb_decode.h
index 192326e..9cc67e8 100644
--- a/pb_decode.h
+++ b/pb_decode.h
@@ -23,10 +23,6 @@
*
* 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.
- *
- * 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
{