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)
|
bool decode_unionmessage_contents(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
|
||||||
{
|
{
|
||||||
pb_istream_t substream;
|
pb_istream_t substream;
|
||||||
|
bool status;
|
||||||
if (!pb_make_string_substream(stream, &substream))
|
if (!pb_make_string_substream(stream, &substream))
|
||||||
return false;
|
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()
|
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. */
|
/* 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)
|
* Remember to close the substream using pb_close_string_substream().
|
||||||
{
|
*/
|
||||||
pb_istream_t *parent = (pb_istream_t*)stream->state;
|
|
||||||
return pb_read(parent, buf, count);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
|
bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
|
||||||
{
|
{
|
||||||
uint32_t size;
|
uint32_t size;
|
||||||
if (!pb_decode_varint32(stream, &size))
|
if (!pb_decode_varint32(stream, &size))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (stream->bytes_left < size)
|
*substream = *stream;
|
||||||
|
if (substream->bytes_left < size)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
substream->callback = &substream_callback;
|
|
||||||
substream->state = stream;
|
|
||||||
substream->bytes_left = size;
|
substream->bytes_left = size;
|
||||||
|
stream->bytes_left -= size;
|
||||||
return true;
|
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 */
|
/* Iterator for pb_field_t list */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
const pb_field_t *start; /* Start of the pb_field_t array */
|
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)
|
&& PB_LTYPE(iter->current->type) <= PB_LTYPE_LAST_PACKABLE)
|
||||||
{
|
{
|
||||||
/* Packed array */
|
/* Packed array */
|
||||||
|
bool status;
|
||||||
size_t *size = (size_t*)iter->pSize;
|
size_t *size = (size_t*)iter->pSize;
|
||||||
pb_istream_t substream;
|
pb_istream_t substream;
|
||||||
if (!pb_make_string_substream(stream, &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;
|
return false;
|
||||||
(*size)++;
|
(*size)++;
|
||||||
}
|
}
|
||||||
return (substream.bytes_left == 0);
|
status = (substream.bytes_left == 0);
|
||||||
|
pb_close_string_substream(stream, &substream);
|
||||||
|
return status;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -339,6 +342,7 @@ static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_t
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pb_close_string_substream(stream, &substream);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else
|
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 checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
|
||||||
{
|
{
|
||||||
|
bool status;
|
||||||
pb_istream_t substream;
|
pb_istream_t substream;
|
||||||
|
|
||||||
if (!pb_make_string_substream(stream, &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)
|
if (field->ptr == NULL)
|
||||||
return false;
|
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),
|
* 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.
|
* 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
|
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. */
|
/* 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);
|
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 ---
|
/* --- Internal functions ---
|
||||||
* These functions are not terribly useful for the average library user, but
|
* These functions are not terribly useful for the average library user, but
|
||||||
|
|||||||
Reference in New Issue
Block a user