ac4d1f7014c65e7f496ef1a5a57972056d484848
[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 /* Lightweight input stream.
8  * If buf is NULL, read but don't store bytes.
9  * You can to provide a callback function for reading or use
10  * pb_istream_from_buffer.
11  * 
12  * You can use state to store your own data (e.g. buffer pointer),
13  * and rely on pb_read to verify that no-body reads past bytes_left.
14  * However, substreams may change bytes_left so don't use that to
15  * compute any pointers.
16  */
17 struct _pb_istream_t
18 {
19     bool (*callback)(pb_istream_t *stream, uint8_t *buf, size_t count);
20     void *state; /* Free field for use by callback implementation */
21     size_t bytes_left;
22 };
23
24 pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize);
25 bool pb_read(pb_istream_t *stream, uint8_t *buf, size_t count);
26
27 /* Decode from stream to destination struct.
28  * The actual struct pointed to by dest must match the description in fields.
29  */
30 bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct);
31
32 /* --- Helper functions ---
33  * You may want to use these from your caller or callbacks.
34  */
35
36 bool pb_decode_varint32(pb_istream_t *stream, uint32_t *dest);
37 bool pb_decode_varint64(pb_istream_t *stream, uint64_t *dest);
38
39 bool pb_skip_varint(pb_istream_t *stream);
40 bool pb_skip_string(pb_istream_t *stream);
41
42 /* --- Field decoders ---
43  * Each decoder takes stream and field description, and a pointer to the field
44  * in the destination struct (dest = struct_addr + field->data_offset).
45  * For arrays, these functions are called repeatedly.
46  */
47
48 bool pb_dec_uint32(pb_istream_t *stream, const pb_field_t *field, uint32_t *dest);
49 bool pb_dec_sint32(pb_istream_t *stream, const pb_field_t *field, int32_t *dest);
50 bool pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, uint32_t *dest);
51 bool pb_dec_uint64(pb_istream_t *stream, const pb_field_t *field, uint64_t *dest);
52 bool pb_dec_sint64(pb_istream_t *stream, const pb_field_t *field, int64_t *dest);
53 bool pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, uint64_t *dest);
54 bool pb_dec_bool(pb_istream_t *stream, const pb_field_t *field, bool *dest);
55 bool pb_dec_enum(pb_istream_t *stream, const pb_field_t *field, void *dest);
56
57 bool pb_dec_float(pb_istream_t *stream, const pb_field_t *field, float *dest);
58 bool pb_dec_double(pb_istream_t *stream, const pb_field_t *field, double *dest);
59
60 bool pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, uint8_t *dest);
61 bool pb_dec_string(pb_istream_t *stream, const pb_field_t *field, uint8_t *dest);
62 bool pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest);
63
64 typedef bool (*pb_decoder_t)(pb_istream_t *stream, const pb_field_t *field, void *dest);
65
66 /* --- Function pointers to field decoders ---
67  * Order in the array must match pb_action_t LTYPE numbering.
68  */
69 const pb_decoder_t PB_DECODERS[16];
70
71 #endif