aboutsummaryrefslogtreecommitdiffstats
path: root/pb_decode.c
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>2012-08-24 20:43:21 +0300
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>2012-08-24 20:43:39 +0300
commit9383f305dc42eeca8b043c5c6dff3fe2e3e8f4f2 (patch)
treef612bef3fe96720101b2e4c8a6bb4387ea8f8361 /pb_decode.c
parent2941e90e041b0d7872a4ea458e77dcc787ca6ff2 (diff)
Revert "Change the substream implementation in pb_decode."
This reverts commit dc2da0edc568b29361479fb7405c96b1a13442cf. Add pb_close_string_substream() for copying back the state. This makes adding error messages easier in the future, as also them need to be propagated back from the substream.
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;
}