Improvements, array support
[apps/agl-service-can-low-level.git] / pb_decode.h
1 #ifndef _PB_DECODE_H_
2 #define _PB_DECODE_H_
3
4 #include <stdbool.h>
5 #include "pb.h"
6
7 // Decode from stream to destination struct.
8 // The actual struct pointed to by dest must match the description in fields.
9 bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct);
10
11 /* --- Helper functions ---
12  * You may want to use these from your caller or callbacks.
13  */
14
15 pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize);
16
17 bool pb_decode_varint32(pb_istream_t *stream, uint32_t *dest);
18 bool pb_decode_varint64(pb_istream_t *stream, uint64_t *dest);
19
20 bool pb_skip_varint(pb_istream_t *stream);
21 bool pb_skip_string(pb_istream_t *stream);
22
23 /* --- Field decoders ---
24  * Each decoder takes stream and field description, and a pointer to the field
25  * in the destination struct (dest = struct_addr + field->data_offset).
26  * For arrays, these functions are called repeatedly.
27  */
28
29 bool pb_dec_uint32(pb_istream_t *stream, const pb_field_t *field, void *dest);
30 bool pb_dec_sint32(pb_istream_t *stream, const pb_field_t *field, void *dest);
31 bool pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest);
32 bool pb_dec_uint64(pb_istream_t *stream, const pb_field_t *field, void *dest);
33 bool pb_dec_sint64(pb_istream_t *stream, const pb_field_t *field, void *dest);
34 bool pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest);
35 bool pb_dec_bool(pb_istream_t *stream, const pb_field_t *field, void *dest);
36 bool pb_dec_enum(pb_istream_t *stream, const pb_field_t *field, void *dest);
37
38 bool pb_dec_float(pb_istream_t *stream, const pb_field_t *field, void *dest);
39 bool pb_dec_double(pb_istream_t *stream, const pb_field_t *field, void *dest);
40
41 bool pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest);
42 bool pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest);
43 bool pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest);
44
45 typedef bool (*pb_decoder_t)(pb_istream_t *stream, const pb_field_t *field, void *dest);
46
47 /* --- Function pointers to field decoders ---
48  * Order in the array must match pb_action_t LTYPE numbering.
49  */
50 const pb_decoder_t PB_DECODERS[16];
51
52 #endif