Add pb_decode_delimited and pb_encode_delimited wrapper functions.

Update issue 74
Status: FixedInGit
This commit is contained in:
Petteri Aimonen
2013-07-06 16:16:00 +03:00
parent 314460c2a5
commit bb985e9927
6 changed files with 48 additions and 0 deletions

View File

@@ -603,6 +603,19 @@ bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void
return pb_decode_noinit(stream, fields, dest_struct); return pb_decode_noinit(stream, fields, dest_struct);
} }
bool pb_decode_delimited(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;
status = pb_decode(&substream, fields, dest_struct);
pb_close_string_substream(stream, &substream);
return status;
}
/* Field decoders */ /* Field decoders */
bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest) bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)

View File

@@ -43,6 +43,12 @@ bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struc
*/ */
bool pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); bool pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct);
/* Same as pb_decode, except expects the stream to start with the message size
* encoded as varint. Corresponds to parseDelimitedFrom() in Google's
* protobuf API.
*/
bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct);
/************************************** /**************************************
* Functions for manipulating streams * * Functions for manipulating streams *

View File

@@ -253,6 +253,11 @@ bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], cons
return true; return true;
} }
bool pb_encode_delimited(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
{
return pb_encode_submessage(stream, fields, src_struct);
}
/* Helper functions */ /* Helper functions */
bool checkreturn pb_encode_varint(pb_ostream_t *stream, uint64_t value) bool checkreturn pb_encode_varint(pb_ostream_t *stream, uint64_t value)
{ {

View File

@@ -32,6 +32,10 @@ extern "C" {
*/ */
bool pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct); bool pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct);
/* Same as pb_encode, but prepends the length of the message as a varint.
* Corresponds to writeDelimitedTo() in Google's protobuf API.
*/
bool pb_encode_delimited(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct);
/************************************** /**************************************
* Functions for manipulating streams * * Functions for manipulating streams *

View File

@@ -289,6 +289,16 @@ int main()
TEST((s = S("\x08"), !pb_decode(&s, IntegerArray_fields, &dest))) TEST((s = S("\x08"), !pb_decode(&s, IntegerArray_fields, &dest)))
} }
{
pb_istream_t s;
IntegerContainer dest = {};
COMMENT("Testing pb_decode_delimited")
TEST((s = S("\x09\x0A\x07\x0A\x05\x01\x02\x03\x04\x05"),
pb_decode_delimited(&s, IntegerContainer_fields, &dest)) &&
dest.submsg.data_count == 5)
}
if (status != 0) if (status != 0)
fprintf(stdout, "\n\nSome tests FAILED!\n"); fprintf(stdout, "\n\nSome tests FAILED!\n");

View File

@@ -244,6 +244,16 @@ int main()
"\x0A\x07\x0A\x05\x01\x02\x03\x04\x05")) "\x0A\x07\x0A\x05\x01\x02\x03\x04\x05"))
} }
{
uint8_t buffer[20];
pb_ostream_t s;
IntegerContainer msg = {{5, {1,2,3,4,5}}};
COMMENT("Test pb_encode_delimited.")
TEST(WRITES(pb_encode_delimited(&s, IntegerContainer_fields, &msg),
"\x09\x0A\x07\x0A\x05\x01\x02\x03\x04\x05"))
}
{ {
uint8_t buffer[10]; uint8_t buffer[10];
pb_ostream_t s; pb_ostream_t s;