Add pb_decode_delimited and pb_encode_delimited wrapper functions.
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>
Sat, 6 Jul 2013 13:16:00 +0000 (16:16 +0300)
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>
Sat, 6 Jul 2013 13:16:00 +0000 (16:16 +0300)
Update issue 74
Status: FixedInGit

pb_decode.c
pb_decode.h
pb_encode.c
pb_encode.h
tests/decode_unittests.c
tests/encode_unittests.c

index d0e18cc..c533698 100644 (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);
 }
 
+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)
index 85efa21..3da3f76 100644 (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);
 
+/* 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 *
index 0e048ac..bedfed4 100644 (file)
@@ -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)
 {
index d9e0336..04bdabe 100644 (file)
@@ -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 *
index 1e74c34..6ad05f0 100644 (file)
@@ -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");
     
index 6a8f5e9..c3634ac 100644 (file)
@@ -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;