git-svn-id: https://svn.kapsi.fi/jpa/nanopb@947 e3a754e5-d11d-0410-8d38-ebb782a927b9
[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_varint(pb_istream_t *stream, const pb_field_t *field, void *dest);
49 bool pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
50 bool pb_dec_fixed(pb_istream_t *stream, const pb_field_t *field, void *dest);
51
52 bool pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, uint8_t *dest);
53 bool pb_dec_string(pb_istream_t *stream, const pb_field_t *field, uint8_t *dest);
54 bool pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest);
55
56 typedef bool (*pb_decoder_t)(pb_istream_t *stream, const pb_field_t *field, void *dest);
57
58 /* --- Function pointers to field decoders ---
59  * Order in the array must match pb_action_t LTYPE numbering.
60  */
61 const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT];
62
63 #endif