diff options
author | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2012-08-24 20:43:21 +0300 |
---|---|---|
committer | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2012-08-24 20:43:39 +0300 |
commit | 9383f305dc42eeca8b043c5c6dff3fe2e3e8f4f2 (patch) | |
tree | f612bef3fe96720101b2e4c8a6bb4387ea8f8361 | |
parent | 2941e90e041b0d7872a4ea458e77dcc787ca6ff2 (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.
-rw-r--r-- | example_unions/decode.c | 5 | ||||
-rw-r--r-- | pb_decode.c | 33 | ||||
-rw-r--r-- | pb_decode.h | 5 |
3 files changed, 29 insertions, 14 deletions
diff --git a/example_unions/decode.c b/example_unions/decode.c index a7cc7814..d40cd8ce 100644 --- a/example_unions/decode.c +++ b/example_unions/decode.c @@ -45,10 +45,13 @@ const pb_field_t* decode_unionmessage_type(pb_istream_t *stream) bool decode_unionmessage_contents(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct) { pb_istream_t substream; + bool status; if (!pb_make_string_substream(stream, &substream)) return false; - return pb_decode(&substream, fields, dest_struct); + status = pb_decode(&substream, fields, dest_struct); + pb_close_string_substream(stream, &substream); + return status; } int main() 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; } diff --git a/pb_decode.h b/pb_decode.h index 27535c10..2880c070 100644 --- a/pb_decode.h +++ b/pb_decode.h @@ -23,6 +23,10 @@ * * 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 { @@ -69,6 +73,7 @@ bool pb_decode_fixed64(pb_istream_t *stream, void *dest); /* Make a limited-length substream for reading a PB_WT_STRING field. */ bool pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream); +void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream); /* --- Internal functions --- * These functions are not terribly useful for the average library user, but |