From bb985e99274b537ee662d630b02664a3825d8829 Mon Sep 17 00:00:00 2001 From: Petteri Aimonen Date: Sat, 6 Jul 2013 16:16:00 +0300 Subject: [PATCH] Add pb_decode_delimited and pb_encode_delimited wrapper functions. Update issue 74 Status: FixedInGit --- pb_decode.c | 13 +++++++++++++ pb_decode.h | 6 ++++++ pb_encode.c | 5 +++++ pb_encode.h | 4 ++++ tests/decode_unittests.c | 10 ++++++++++ tests/encode_unittests.c | 10 ++++++++++ 6 files changed, 48 insertions(+) diff --git a/pb_decode.c b/pb_decode.c index d0e18cc..c533698 100644 --- a/pb_decode.c +++ b/pb_decode.c @@ -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); } +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 */ bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest) diff --git a/pb_decode.h b/pb_decode.h index 85efa21..3da3f76 100644 --- a/pb_decode.h +++ b/pb_decode.h @@ -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); +/* 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 * diff --git a/pb_encode.c b/pb_encode.c index 0e048ac..bedfed4 100644 --- a/pb_encode.c +++ b/pb_encode.c @@ -253,6 +253,11 @@ bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], cons 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 */ bool checkreturn pb_encode_varint(pb_ostream_t *stream, uint64_t value) { diff --git a/pb_encode.h b/pb_encode.h index d9e0336..04bdabe 100644 --- a/pb_encode.h +++ b/pb_encode.h @@ -32,6 +32,10 @@ extern "C" { */ 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 * diff --git a/tests/decode_unittests.c b/tests/decode_unittests.c index 1e74c34..6ad05f0 100644 --- a/tests/decode_unittests.c +++ b/tests/decode_unittests.c @@ -289,6 +289,16 @@ int main() 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) fprintf(stdout, "\n\nSome tests FAILED!\n"); diff --git a/tests/encode_unittests.c b/tests/encode_unittests.c index 6a8f5e9..c3634ac 100644 --- a/tests/encode_unittests.c +++ b/tests/encode_unittests.c @@ -244,6 +244,16 @@ int main() "\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]; pb_ostream_t s; -- 2.16.6