MOS Source Code
Loading...
Searching...
No Matches
decode_callbacks.c
Go to the documentation of this file.
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#include "test_helpers.h"
9
10bool print_string(pb_istream_t *stream, const pb_field_t *field, void **arg)
11{
12 uint8_t buffer[1024] = {0};
13
14 /* We could read block-by-block to avoid the large buffer... */
15 if (stream->bytes_left > sizeof(buffer) - 1)
16 return false;
17
18 if (!pb_read(stream, buffer, stream->bytes_left))
19 return false;
20
21 /* Print the string, in format comparable with protoc --decode.
22 * Format comes from the arg defined in main().
23 */
24 printf((char*)*arg, buffer);
25 return true;
26}
27
28bool print_int32(pb_istream_t *stream, const pb_field_t *field, void **arg)
29{
30 uint64_t value;
31 if (!pb_decode_varint(stream, &value))
32 return false;
33
34 printf((char*)*arg, (long)value);
35 return true;
36}
37
38bool print_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 printf((char*)*arg, (long)value);
45 return true;
46}
47
48bool print_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 printf((char*)*arg, (long)value);
55 return true;
56}
57
58int main()
59{
60 uint8_t buffer[1024];
61 size_t length;
62 pb_istream_t stream;
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 = {{{NULL}}};
68
70 length = fread(buffer, 1, 1024, stdin);
71 stream = pb_istream_from_buffer(buffer, length);
72
73 testmessage.submsg.stringvalue.funcs.decode = &print_string;
74 testmessage.submsg.stringvalue.arg = "submsg {\n stringvalue: \"%s\"\n";
75 testmessage.submsg.int32value.funcs.decode = &print_int32;
76 testmessage.submsg.int32value.arg = " int32value: %ld\n";
77 testmessage.submsg.fixed32value.funcs.decode = &print_fixed32;
78 testmessage.submsg.fixed32value.arg = " fixed32value: %ld\n";
79 testmessage.submsg.fixed64value.funcs.decode = &print_fixed64;
80 testmessage.submsg.fixed64value.arg = " fixed64value: %ld\n}\n";
81
82 testmessage.stringvalue.funcs.decode = &print_string;
83 testmessage.stringvalue.arg = "stringvalue: \"%s\"\n";
84 testmessage.int32value.funcs.decode = &print_int32;
85 testmessage.int32value.arg = "int32value: %ld\n";
86 testmessage.fixed32value.funcs.decode = &print_fixed32;
87 testmessage.fixed32value.arg = "fixed32value: %ld\n";
88 testmessage.fixed64value.funcs.decode = &print_fixed64;
89 testmessage.fixed64value.arg = "fixed64value: %ld\n";
90 testmessage.repeatedstring.funcs.decode = &print_string;
91 testmessage.repeatedstring.arg = "repeatedstring: \"%s\"\n";
92
93 if (!pb_decode(&stream, TestMessage_fields, &testmessage))
94 return 1;
95
96 return 0;
97}
bool print_string(pb_istream_t *stream, const pb_field_t *field, void **arg)
bool print_fixed32(pb_istream_t *stream, const pb_field_t *field, void **arg)
bool print_int32(pb_istream_t *stream, const pb_field_t *field, void **arg)
int main()
bool print_fixed64(pb_istream_t *stream, const pb_field_t *field, void **arg)
size_t fread(void *__restrict ptr, size_t size, size_t nmemb, FILE *__restrict stream)
#define stdin
Definition mos_stdio.h:30
pb_field_iter_t pb_field_t
Definition pb.h:359
bool pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
Definition pb_decode.c:82
bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
Definition pb_decode.c:243
bool pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct)
Definition pb_decode.c:1182
bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
Definition pb_decode.c:1370
pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen)
Definition pb_decode.c:143
bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
Definition pb_decode.c:1393
#define NULL
Definition pb_syshdr.h:46
unsigned int uint32_t
Definition pb_syshdr.h:24
unsigned long long uint64_t
Definition pb_syshdr.h:26
unsigned char uint8_t
Definition pb_syshdr.h:20
#define SET_BINARY_MODE(file)
static char buffer[2048]
Definition test_printf.c:7