Add 'CAN-binder/libs/nanopb/' from commit '278ffb890e3d8722e4c7d824baaf221a1e375fc4'
[apps/agl-service-can-low-level.git] / CAN-binder / libs / nanopb / tests / alltypes_proto3_callback / decode_alltypes_callback.c
1 /* Attempts to test all the datatypes supported by ProtoBuf when used as callback fields.
2  * Note that normally there would be no reason to use callback fields for this,
3  * because each encoder defined here only gives a single field.
4  */
5
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdlib.h>
9 #include <pb_decode.h>
10 #include "alltypes.pb.h"
11 #include "test_helpers.h"
12
13 #define TEST(x) if (!(x)) { \
14     printf("Test " #x " failed (in field %d).\n", field->tag); \
15     return false; \
16     }
17
18 static bool read_varint(pb_istream_t *stream, const pb_field_t *field, void **arg)
19 {
20     uint64_t value;
21     if (!pb_decode_varint(stream, &value))
22         return false;
23     
24     TEST((int64_t)value == (long)*arg);
25     return true;
26 }
27
28 static bool read_svarint(pb_istream_t *stream, const pb_field_t *field, void **arg)
29 {
30     int64_t value;
31     if (!pb_decode_svarint(stream, &value))
32         return false;
33     
34     TEST(value == (long)*arg);
35     return true;
36 }
37
38 static bool read_fixed32(pb_istream_t *stream, const pb_field_t *field, void **arg)
39 {
40     uint32_t value;
41     if (!pb_decode_fixed32(stream, &value))
42         return false;
43     
44     TEST(value == *(uint32_t*)*arg);
45     return true;
46 }
47
48 static bool read_fixed64(pb_istream_t *stream, const pb_field_t *field, void **arg)
49 {
50     uint64_t value;
51     if (!pb_decode_fixed64(stream, &value))
52         return false;
53     
54     TEST(value == *(uint64_t*)*arg);
55     return true;
56 }
57
58 static bool read_string(pb_istream_t *stream, const pb_field_t *field, void **arg)
59 {
60     uint8_t buf[16] = {0};
61     size_t len = stream->bytes_left;
62     
63     if (len > sizeof(buf) - 1 || !pb_read(stream, buf, len))
64         return false;
65     
66     TEST(strcmp((char*)buf, *arg) == 0);
67     return true;
68 }
69
70 static bool read_submsg(pb_istream_t *stream, const pb_field_t *field, void **arg)
71 {
72     SubMessage submsg = {""};
73     SubMessage *ref = *arg;
74     
75     if (!pb_decode(stream, SubMessage_fields, &submsg))
76         return false;
77     
78     TEST(strcmp(submsg.substuff1, ref->substuff1) == 0);
79     TEST(submsg.substuff2 == ref->substuff2);
80     TEST(submsg.substuff3 == ref->substuff3); 
81     return true;
82 }
83
84 static bool read_emptymsg(pb_istream_t *stream, const pb_field_t *field, void **arg)
85 {
86     EmptyMessage emptymsg = {0};
87     return pb_decode(stream, EmptyMessage_fields, &emptymsg);
88 }
89
90 static bool read_repeated_varint(pb_istream_t *stream, const pb_field_t *field, void **arg)
91 {
92     int32_t** expected = (int32_t**)arg;
93     uint64_t value;
94     if (!pb_decode_varint(stream, &value))
95         return false;
96
97     TEST(*(*expected)++ == value);
98     return true;
99 }
100
101 static bool read_repeated_svarint(pb_istream_t *stream, const pb_field_t *field, void **arg)
102 {
103     int32_t** expected = (int32_t**)arg;
104     int64_t value;
105     if (!pb_decode_svarint(stream, &value))
106         return false;
107
108     TEST(*(*expected)++ == value);
109     return true;
110 }
111
112 static bool read_repeated_fixed32(pb_istream_t *stream, const pb_field_t *field, void **arg)
113 {
114     uint32_t** expected = (uint32_t**)arg;
115     uint32_t value;
116     if (!pb_decode_fixed32(stream, &value))
117         return false;
118
119     TEST(*(*expected)++ == value);
120     return true;
121 }
122
123 static bool read_repeated_fixed64(pb_istream_t *stream, const pb_field_t *field, void **arg)
124 {
125     uint64_t** expected = (uint64_t**)arg;
126     uint64_t value;
127     if (!pb_decode_fixed64(stream, &value))
128         return false;
129
130     TEST(*(*expected)++ == value);
131     return true;
132 }
133
134 static bool read_repeated_string(pb_istream_t *stream, const pb_field_t *field, void **arg)
135 {
136     uint8_t*** expected = (uint8_t***)arg;
137     uint8_t buf[16] = {0};
138     size_t len = stream->bytes_left;
139     
140     if (len > sizeof(buf) - 1 || !pb_read(stream, buf, len))
141         return false;
142     
143     TEST(strcmp((char*)*(*expected)++, (char*)buf) == 0);
144     return true;
145 }
146
147 static bool read_repeated_submsg(pb_istream_t *stream, const pb_field_t *field, void **arg)
148 {
149     SubMessage** expected = (SubMessage**)arg;
150     SubMessage submsg = {""};
151     if (!pb_decode(stream, SubMessage_fields, &submsg))
152         return false;
153
154     TEST(strcmp(submsg.substuff1, (*expected)->substuff1) == 0);
155     TEST(submsg.substuff2 == (*expected)->substuff2);
156     TEST(submsg.substuff3 == (*expected)->substuff3);
157     (*expected)++;
158
159     return true;
160 }
161
162 static bool read_limits(pb_istream_t *stream, const pb_field_t *field, void **arg)
163 {
164     Limits decoded = {0};
165     if (!pb_decode(stream, Limits_fields, &decoded))
166         return false;
167
168     TEST(decoded.int32_min  == INT32_MIN);
169     TEST(decoded.int32_max  == INT32_MAX);
170     TEST(decoded.uint32_min == 0);
171     TEST(decoded.uint32_max == UINT32_MAX);
172     TEST(decoded.int64_min  == INT64_MIN);
173     TEST(decoded.int64_max  == INT64_MAX);
174     TEST(decoded.uint64_min == 0);
175     TEST(decoded.uint64_max == UINT64_MAX);
176     TEST(decoded.enum_min   == HugeEnum_Negative);
177     TEST(decoded.enum_max   == HugeEnum_Positive);
178     
179     return true;
180 }
181
182 /* This function is called once from main(), it handles
183    the decoding and checks the fields. */
184 bool check_alltypes(pb_istream_t *stream, int mode)
185 {
186     /* Values for use from callbacks through pointers. */
187     bool status;
188     
189     int32_t     rep_int32[5]    = {0, 0, 0, 0, -2001};
190     int32_t     rep_int64[5]    = {0, 0, 0, 0, -2002};
191     int32_t     rep_uint32[5]   = {0, 0, 0, 0,  2003};
192     int32_t     rep_uint64[5]   = {0, 0, 0, 0,  2004};
193     int32_t     rep_sint32[5]   = {0, 0, 0, 0, -2005};
194     int32_t     rep_sint64[5]   = {0, 0, 0, 0, -2006};
195     int32_t     rep_bool[5]     = {false, false, false, false, true};
196     uint32_t    rep_fixed32[5]  = {0, 0, 0, 0,  2008};
197     int32_t     rep_sfixed32[5] = {0, 0, 0, 0, -2009};
198     float       rep_float[5]    = {0, 0, 0, 0,  2010.0f};
199     uint64_t    rep_fixed64[5]  = {0, 0, 0, 0,  2011};
200     int64_t     rep_sfixed64[5] = {0, 0, 0, 0, -2012};
201     double      rep_double[5]   = {0, 0, 0, 0,  2013.0};
202     char*       rep_string[5]   = {"", "", "", "", "2014"};
203     char*       rep_bytes[5]    = {"", "", "", "", "2015"};
204     SubMessage  rep_submsg[5]   = {{"", 0, 0},
205                                    {"", 0, 0},
206                                    {"", 0, 0},
207                                    {"", 0, 0},
208                                    {"2016", 2016, 2016}};
209     int32_t     rep_enum[5]     = {0, 0, 0, 0, MyEnum_Truth};
210     
211     uint32_t    sng_fixed32     = 3048;
212     int32_t     sng_sfixed32    = 3049;
213     float       sng_float       = 3050.0f;
214     uint64_t    sng_fixed64     = 3051;
215     int64_t     sng_sfixed64    = 3052;
216     double      sng_double      = 3053.0f;
217     SubMessage  sng_submsg      = {"3056", 3056};
218
219     SubMessage  oneof_msg1      = {"4059", 4059};
220     
221     AllTypes alltypes = AllTypes_init_zero;
222
223     /* Bind callbacks for repeated fields */
224     alltypes.rep_int32.funcs.decode = &read_repeated_varint;
225     alltypes.rep_int32.arg = rep_int32;
226     
227     alltypes.rep_int64.funcs.decode = &read_repeated_varint;
228     alltypes.rep_int64.arg = rep_int64;
229     
230     alltypes.rep_uint32.funcs.decode = &read_repeated_varint;
231     alltypes.rep_uint32.arg = rep_uint32;
232     
233     alltypes.rep_uint64.funcs.decode = &read_repeated_varint;
234     alltypes.rep_uint64.arg = rep_uint64;
235     
236     alltypes.rep_sint32.funcs.decode = &read_repeated_svarint;
237     alltypes.rep_sint32.arg = rep_sint32;
238     
239     alltypes.rep_sint64.funcs.decode = &read_repeated_svarint;
240     alltypes.rep_sint64.arg = rep_sint64;
241     
242     alltypes.rep_bool.funcs.decode = &read_repeated_varint;
243     alltypes.rep_bool.arg = rep_bool;
244     
245     alltypes.rep_fixed32.funcs.decode = &read_repeated_fixed32;
246     alltypes.rep_fixed32.arg = rep_fixed32;
247     
248     alltypes.rep_sfixed32.funcs.decode = &read_repeated_fixed32;
249     alltypes.rep_sfixed32.arg = rep_sfixed32;
250     
251     alltypes.rep_float.funcs.decode = &read_repeated_fixed32;
252     alltypes.rep_float.arg = rep_float;
253     
254     alltypes.rep_fixed64.funcs.decode = &read_repeated_fixed64;
255     alltypes.rep_fixed64.arg = rep_fixed64;
256     
257     alltypes.rep_sfixed64.funcs.decode = &read_repeated_fixed64;
258     alltypes.rep_sfixed64.arg = rep_sfixed64;
259     
260     alltypes.rep_double.funcs.decode = &read_repeated_fixed64;
261     alltypes.rep_double.arg = rep_double;
262     
263     alltypes.rep_string.funcs.decode = &read_repeated_string;
264     alltypes.rep_string.arg = rep_string;
265     
266     alltypes.rep_bytes.funcs.decode = &read_repeated_string;
267     alltypes.rep_bytes.arg = rep_bytes;
268     
269     alltypes.rep_submsg.funcs.decode = &read_repeated_submsg;
270     alltypes.rep_submsg.arg = rep_submsg;
271     
272     alltypes.rep_enum.funcs.decode = &read_repeated_varint;
273     alltypes.rep_enum.arg = rep_enum;
274     
275     alltypes.rep_emptymsg.funcs.decode = &read_emptymsg;
276     
277     alltypes.req_limits.funcs.decode = &read_limits;
278     
279     alltypes.end.funcs.decode = &read_varint;
280     alltypes.end.arg = (void*)1099;
281     
282     /* Bind callbacks for optional fields */
283     if (mode == 1)
284     {
285         alltypes.sng_int32.funcs.decode = &read_varint;
286         alltypes.sng_int32.arg = (void*)3041;
287         
288         alltypes.sng_int64.funcs.decode = &read_varint;
289         alltypes.sng_int64.arg = (void*)3042;
290         
291         alltypes.sng_uint32.funcs.decode = &read_varint;
292         alltypes.sng_uint32.arg = (void*)3043;
293         
294         alltypes.sng_uint64.funcs.decode = &read_varint;
295         alltypes.sng_uint64.arg = (void*)3044;
296         
297         alltypes.sng_sint32.funcs.decode = &read_svarint;
298         alltypes.sng_sint32.arg = (void*)3045;
299         
300         alltypes.sng_sint64.funcs.decode = &read_svarint;
301         alltypes.sng_sint64.arg = (void*)3046;
302         
303         alltypes.sng_bool.funcs.decode = &read_varint;
304         alltypes.sng_bool.arg = (void*)true;
305
306         alltypes.sng_fixed32.funcs.decode = &read_fixed32;
307         alltypes.sng_fixed32.arg = &sng_fixed32;
308         
309         alltypes.sng_sfixed32.funcs.decode = &read_fixed32;
310         alltypes.sng_sfixed32.arg = &sng_sfixed32;
311         
312         alltypes.sng_float.funcs.decode = &read_fixed32;
313         alltypes.sng_float.arg = &sng_float;
314         
315         alltypes.sng_fixed64.funcs.decode = &read_fixed64;
316         alltypes.sng_fixed64.arg = &sng_fixed64;
317         
318         alltypes.sng_sfixed64.funcs.decode = &read_fixed64;
319         alltypes.sng_sfixed64.arg = &sng_sfixed64;
320         
321         alltypes.sng_double.funcs.decode = &read_fixed64;
322         alltypes.sng_double.arg = &sng_double;
323         
324         alltypes.sng_string.funcs.decode = &read_string;
325         alltypes.sng_string.arg = "3054";
326         
327         alltypes.sng_bytes.funcs.decode = &read_string;
328         alltypes.sng_bytes.arg = "3055";
329         
330         alltypes.sng_submsg.funcs.decode = &read_submsg;
331         alltypes.sng_submsg.arg = &sng_submsg;
332         
333         alltypes.sng_enum.funcs.decode = &read_varint;
334         alltypes.sng_enum.arg = (void*)MyEnum_Truth;
335         
336         alltypes.sng_emptymsg.funcs.decode = &read_emptymsg;
337
338         alltypes.oneof_msg1.funcs.decode = &read_submsg;
339         alltypes.oneof_msg1.arg = &oneof_msg1;
340     }
341     
342     status = pb_decode(stream, AllTypes_fields, &alltypes);
343     
344 #ifdef PB_ENABLE_MALLOC
345     /* Just to check for any interference between pb_release() and callback fields */
346     pb_release(AllTypes_fields, &alltypes);
347 #endif
348
349     return status;
350 }
351
352 int main(int argc, char **argv)
353 {
354     uint8_t buffer[1024];
355     size_t count;
356     pb_istream_t stream;
357
358     /* Whether to expect the optional values or the default values. */
359     int mode = (argc > 1) ? atoi(argv[1]) : 0;
360     
361     /* Read the data into buffer */
362     SET_BINARY_MODE(stdin);
363     count = fread(buffer, 1, sizeof(buffer), stdin);
364     
365     /* Construct a pb_istream_t for reading from the buffer */
366     stream = pb_istream_from_buffer(buffer, count);
367     
368     /* Decode and print out the stuff */
369     if (!check_alltypes(&stream, mode))
370     {
371         printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
372         return 1;
373     } else {
374         return 0;
375     }
376 }