aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>2013-07-06 16:16:00 +0300
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>2013-07-06 16:16:00 +0300
commitbb985e99274b537ee662d630b02664a3825d8829 (patch)
treea6cf90d0005d2a1f1a66977a1a4e4901c9eb24a4
parent314460c2a518bdfc4e5dfb87992d907461a7c0e6 (diff)
Add pb_decode_delimited and pb_encode_delimited wrapper functions.
Update issue 74 Status: FixedInGit
-rw-r--r--pb_decode.c13
-rw-r--r--pb_decode.h6
-rw-r--r--pb_encode.c5
-rw-r--r--pb_encode.h4
-rw-r--r--tests/decode_unittests.c10
-rw-r--r--tests/encode_unittests.c10
6 files changed, 48 insertions, 0 deletions
diff --git a/pb_decode.c b/pb_decode.c
index d0e18ccb..c5336981 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 85efa212..3da3f763 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 0e048ac6..bedfed46 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 d9e0336a..04bdabee 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 1e74c34d..6ad05f00 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 6a8f5e9e..c3634ac8 100644
--- a/tests/encode_unittests.c
+++ b/tests/encode_unittests.c
@@ -245,6 +245,16 @@ int main()
}
{
+ 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;
CallbackContainer msg;