Handle unsupported extension field types more gracefully.
[apps/agl-service-can-low-level.git] / tests / callbacks / decode_callbacks.c
1 /* Decoding testcase for callback fields.
2  * Run e.g. ./test_encode_callbacks | ./test_decode_callbacks
3  */
4
5 #include <stdio.h>
6 #include <pb_decode.h>
7 #include "callbacks.pb.h"
8
9 bool print_string(pb_istream_t *stream, const pb_field_t *field, void **arg)
10 {
11     uint8_t buffer[1024] = {0};
12     
13     /* We could read block-by-block to avoid the large buffer... */
14     if (stream->bytes_left > sizeof(buffer) - 1)
15         return false;
16     
17     if (!pb_read(stream, buffer, stream->bytes_left))
18         return false;
19     
20     /* Print the string, in format comparable with protoc --decode.
21      * Format comes from the arg defined in main().
22      */
23     printf((char*)*arg, buffer);
24     return true;
25 }
26
27 bool print_int32(pb_istream_t *stream, const pb_field_t *field, void **arg)
28 {
29     uint64_t value;
30     if (!pb_decode_varint(stream, &value))
31         return false;
32     
33     printf((char*)*arg, (long)value);
34     return true;
35 }
36
37 bool print_fixed32(pb_istream_t *stream, const pb_field_t *field, void **arg)
38 {
39     uint32_t value;
40     if (!pb_decode_fixed32(stream, &value))
41         return false;
42     
43     printf((char*)*arg, (long)value);
44     return true;
45 }
46
47 bool print_fixed64(pb_istream_t *stream, const pb_field_t *field, void **arg)
48 {
49     uint64_t value;
50     if (!pb_decode_fixed64(stream, &value))
51         return false;
52     
53     printf((char*)*arg, (long long)value);
54     return true;
55 }
56
57 int main()
58 {
59     uint8_t buffer[1024];
60     size_t length = fread(buffer, 1, 1024, stdin);
61     pb_istream_t stream = pb_istream_from_buffer(buffer, length);
62     
63     /* Note: empty initializer list initializes the struct with all-0.
64      * This is recommended so that unused callbacks are set to NULL instead
65      * of crashing at runtime.
66      */
67     TestMessage testmessage = {};
68     
69     testmessage.submsg.stringvalue.funcs.decode = &print_string;
70     testmessage.submsg.stringvalue.arg = "submsg {\n  stringvalue: \"%s\"\n";
71     testmessage.submsg.int32value.funcs.decode = &print_int32;
72     testmessage.submsg.int32value.arg = "  int32value: %ld\n";
73     testmessage.submsg.fixed32value.funcs.decode = &print_fixed32;
74     testmessage.submsg.fixed32value.arg = "  fixed32value: %ld\n";
75     testmessage.submsg.fixed64value.funcs.decode = &print_fixed64;
76     testmessage.submsg.fixed64value.arg = "  fixed64value: %lld\n}\n";
77     
78     testmessage.stringvalue.funcs.decode = &print_string;
79     testmessage.stringvalue.arg = "stringvalue: \"%s\"\n";
80     testmessage.int32value.funcs.decode = &print_int32;
81     testmessage.int32value.arg = "int32value: %ld\n";
82     testmessage.fixed32value.funcs.decode = &print_fixed32;
83     testmessage.fixed32value.arg = "fixed32value: %ld\n";
84     testmessage.fixed64value.funcs.decode = &print_fixed64;
85     testmessage.fixed64value.arg = "fixed64value: %lld\n";
86     testmessage.repeatedstring.funcs.decode = &print_string;
87     testmessage.repeatedstring.arg = "repeatedstring: \"%s\"\n";
88     
89     if (!pb_decode(&stream, TestMessage_fields, &testmessage))
90         return 1;
91     
92     return 0;
93 }