Merge branch 'dev_get_rid_of_ternary_operator'
[apps/agl-service-can-low-level.git] / pb_decode.h
1 /* pb_decode.h: Functions to decode protocol buffers. Depends on pb_decode.c.
2  * The main function is pb_decode. You also need an input stream, and the
3  * field descriptions created by nanopb_generator.py.
4  */
5
6 #ifndef _PB_DECODE_H_
7 #define _PB_DECODE_H_
8
9 #include "pb.h"
10
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14
15 /* Structure for defining custom input streams. You will need to provide
16  * a callback function to read the bytes from your storage, which can be
17  * for example a file or a network socket.
18  * 
19  * The callback must conform to these rules:
20  *
21  * 1) Return false on IO errors. This will cause decoding to abort.
22  * 2) You can use state to store your own data (e.g. buffer pointer),
23  *    and rely on pb_read to verify that no-body reads past bytes_left.
24  * 3) Your callback may be used with substreams, in which case bytes_left
25  *    is different than from the main stream. Don't use bytes_left to compute
26  *    any pointers.
27  */
28 struct _pb_istream_t
29 {
30 #ifdef PB_BUFFER_ONLY
31     /* Callback pointer is not used in buffer-only configuration.
32      * Having an int pointer here allows binary compatibility but
33      * gives an error if someone tries to assign callback function.
34      */
35     int *callback;
36 #else
37     bool (*callback)(pb_istream_t *stream, uint8_t *buf, size_t count);
38 #endif
39
40     void *state; /* Free field for use by callback implementation */
41     size_t bytes_left;
42     
43 #ifndef PB_NO_ERRMSG
44     const char *errmsg;
45 #endif
46 };
47
48 /***************************
49  * Main decoding functions *
50  ***************************/
51  
52 /* Decode a single protocol buffers message from input stream into a C structure.
53  * Returns true on success, false on any failure.
54  * The actual struct pointed to by dest must match the description in fields.
55  * Callback fields of the destination structure must be initialized by caller.
56  * All other fields will be initialized by this function.
57  *
58  * Example usage:
59  *    MyMessage msg = {};
60  *    uint8_t buffer[64];
61  *    pb_istream_t stream;
62  *    
63  *    // ... read some data into buffer ...
64  *
65  *    stream = pb_istream_from_buffer(buffer, count);
66  *    pb_decode(&stream, MyMessage_fields, &msg);
67  */
68 bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct);
69
70 /* Same as pb_decode, except does not initialize the destination structure
71  * to default values. This is slightly faster if you need no default values
72  * and just do memset(struct, 0, sizeof(struct)) yourself.
73  *
74  * This can also be used for 'merging' two messages, i.e. update only the
75  * fields that exist in the new message.
76  */
77 bool pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct);
78
79 /* Same as pb_decode, except expects the stream to start with the message size
80  * encoded as varint. Corresponds to parseDelimitedFrom() in Google's
81  * protobuf API.
82  */
83 bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct);
84
85
86 /**************************************
87  * Functions for manipulating streams *
88  **************************************/
89
90 /* Create an input stream for reading from a memory buffer.
91  *
92  * Alternatively, you can use a custom stream that reads directly from e.g.
93  * a file or a network socket.
94  */
95 pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize);
96
97 /* Function to read from a pb_istream_t. You can use this if you need to
98  * read some custom header data, or to read data in field callbacks.
99  */
100 bool pb_read(pb_istream_t *stream, uint8_t *buf, size_t count);
101
102
103 /************************************************
104  * Helper functions for writing field callbacks *
105  ************************************************/
106
107 /* Decode the tag for the next field in the stream. Gives the wire type and
108  * field tag. At end of the message, returns false and sets eof to true. */
109 bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof);
110
111 /* Skip the field payload data, given the wire type. */
112 bool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type);
113
114 /* Decode an integer in the varint format. This works for bool, enum, int32,
115  * int64, uint32 and uint64 field types. */
116 bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest);
117
118 /* Decode an integer in the zig-zagged svarint format. This works for sint32
119  * and sint64. */
120 bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest);
121
122 /* Decode a fixed32, sfixed32 or float value. You need to pass a pointer to
123  * a 4-byte wide C variable. */
124 bool pb_decode_fixed32(pb_istream_t *stream, void *dest);
125
126 /* Decode a fixed64, sfixed64 or double value. You need to pass a pointer to
127  * a 8-byte wide C variable. */
128 bool pb_decode_fixed64(pb_istream_t *stream, void *dest);
129
130 /* Make a limited-length substream for reading a PB_WT_STRING field. */
131 bool pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream);
132 void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream);
133
134
135 /*******************************
136  * Internal / legacy functions *
137  *******************************/
138
139 #ifdef NANOPB_INTERNALS
140 bool pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest);
141 bool pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
142 bool pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest);
143 bool pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest);
144
145 bool pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest);
146 bool pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest);
147 bool pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest);
148
149 bool pb_skip_varint(pb_istream_t *stream);
150 bool pb_skip_string(pb_istream_t *stream);
151 #endif
152
153 #ifdef __cplusplus
154 } /* extern "C" */
155 #endif
156
157 #endif