Revert "Change the substream implementation in pb_decode."
This reverts commit dc2da0edc5.
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.
This commit is contained in:
@@ -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()
|
||||
|
||||
33
pb_decode.c
33
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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user