1 /* A very simple decoding test case, using person.proto.
2 * Produces output compatible with protoc --decode.
3 * Reads the encoded data from stdin and prints the values
6 * Run e.g. ./test_encode1 | ./test_decode1
10 #include <pb_decode.h>
11 #include "person.pb.h"
13 /* This function is called once from main(), it handles
14 the decoding and printing. */
15 bool print_person(pb_istream_t *stream)
20 if (!pb_decode(stream, Person_fields, &person))
23 /* Now the decoding is done, rest is just to print stuff out. */
25 printf("name: \"%s\"\n", person.name);
26 printf("id: %ld\n", (long)person.id);
29 printf("email: \"%s\"\n", person.email);
31 for (i = 0; i < person.phone_count; i++)
33 Person_PhoneNumber *phone = &person.phone[i];
35 printf(" number: \"%s\"\n", phone->number);
39 case Person_PhoneType_WORK:
40 printf(" type: WORK\n");
43 case Person_PhoneType_HOME:
44 printf(" type: HOME\n");
47 case Person_PhoneType_MOBILE:
48 printf(" type: MOBILE\n");
57 /* This binds the pb_istream_t to stdin */
58 bool callback(pb_istream_t *stream, uint8_t *buf, size_t count)
60 FILE *file = (FILE*)stream->state;
66 while (count-- && fgetc(file) != EOF);
70 status = (fread(buf, 1, count, file) == count);
73 stream->bytes_left = 0;
80 /* Maximum size is specified to prevent infinite length messages from
81 * hanging this in the fuzz test.
83 pb_istream_t stream = {&callback, stdin, 10000};
84 if (!print_person(&stream))
86 printf("Parsing failed.\n");