1 #include <assert.h>
2 #include <pb_encode.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include "test_helpers.h"
6 #include "anytest.pb.h"
7 #include "google/protobuf/duration.pb.h"
8 
main()9 int main()
10 {
11     BaseMessage msg = BaseMessage_init_zero;
12     google_protobuf_Duration duration = google_protobuf_Duration_init_zero;
13     uint8_t buffer[256];
14     pb_ostream_t stream;
15     bool status;
16 
17     msg.start = 1234;
18     msg.end = 5678;
19 
20     /* Fill in the Any message header */
21     msg.has_details = true;
22     strncpy(msg.details.type_url, "type.googleapis.com/google.protobuf.Duration", sizeof(msg.details.type_url));
23 
24     /* Encode a Duration message inside the Any message. */
25     duration.seconds = 99999;
26     duration.nanos = 100;
27     stream = pb_ostream_from_buffer(msg.details.value.bytes, sizeof(msg.details.value.bytes));
28     status = pb_encode(&stream, google_protobuf_Duration_fields, &duration);
29     assert(status);
30     msg.details.value.size = stream.bytes_written;
31 
32     /* Then encode the outer message */
33     stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
34     if (pb_encode(&stream, BaseMessage_fields, &msg))
35     {
36         /* Write the result data to stdout */
37         SET_BINARY_MODE(stdout);
38         fwrite(buffer, 1, stream.bytes_written, stdout);
39         return 0;
40     }
41     else
42     {
43         fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
44         return 1;
45     }
46 }
47