summaryrefslogtreecommitdiffstats
path: root/pb_decode.c
diff options
context:
space:
mode:
Diffstat (limited to 'pb_decode.c')
-rw-r--r--pb_decode.c33
1 files changed, 20 insertions, 13 deletions
diff --git a/pb_decode.c b/pb_decode.c
index 59efb3d6..8f72f776 100644
--- a/pb_decode.c
+++ b/pb_decode.c
@@ -186,29 +186,29 @@ 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. */
-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);
-}
-
+/* Decode string length from stream and return a substream with limited length.
+ * Remember to close the substream using pb_close_string_substream().
+ */
bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
{
uint32_t size;
if (!pb_decode_varint32(stream, &size))
return false;
- if (stream->bytes_left < size)
+ *substream = *stream;
+ if (substream->bytes_left < size)
return false;
- substream->callback = &substream_callback;
- substream->state = stream;
substream->bytes_left = size;
-
+ stream->bytes_left -= size;
return true;
}
+void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
+{
+ stream->state = substream->state;
+}
+
/* Iterator for pb_field_t list */
typedef struct {
const pb_field_t *start; /* Start of the pb_field_t array */
@@ -293,6 +293,7 @@ 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 (!pb_make_string_substream(stream, &substream))
@@ -305,7 +306,9 @@ static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_t
return false;
(*size)++;
}
- return (substream.bytes_left == 0);
+ status = (substream.bytes_left == 0);
+ pb_close_string_substream(stream, &substream);
+ return status;
}
else
{
@@ -339,6 +342,7 @@ static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_t
return false;
}
+ pb_close_string_substream(stream, &substream);
return true;
}
else
@@ -602,6 +606,7 @@ 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 (!pb_make_string_substream(stream, &substream))
@@ -610,5 +615,7 @@ bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field
if (field->ptr == NULL)
return false;
- return pb_decode(&substream, (pb_field_t*)field->ptr, dest);
+ status = pb_decode(&substream, (pb_field_t*)field->ptr, dest);
+ pb_close_string_substream(stream, &substream);
+ return status;
}