MOS Source Code
Loading...
Searching...
No Matches
pb_decode.h
Go to the documentation of this file.
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_INCLUDED
7#define PB_DECODE_H_INCLUDED
8
9#include "pb.h"
10
11#ifdef __cplusplus
12extern "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 */
28struct pb_istream_s
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, pb_byte_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#ifndef PB_NO_ERRMSG
49#define PB_ISTREAM_EMPTY {0,0,0,0}
50#else
51#define PB_ISTREAM_EMPTY {0,0,0}
52#endif
53
54/***************************
55 * Main decoding functions *
56 ***************************/
57
58/* Decode a single protocol buffers message from input stream into a C structure.
59 * Returns true on success, false on any failure.
60 * The actual struct pointed to by dest must match the description in fields.
61 * Callback fields of the destination structure must be initialized by caller.
62 * All other fields will be initialized by this function.
63 *
64 * Example usage:
65 * MyMessage msg = {};
66 * uint8_t buffer[64];
67 * pb_istream_t stream;
68 *
69 * // ... read some data into buffer ...
70 *
71 * stream = pb_istream_from_buffer(buffer, count);
72 * pb_decode(&stream, MyMessage_fields, &msg);
73 */
74bool pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct);
75
76/* Extended version of pb_decode, with several options to control
77 * the decoding process:
78 *
79 * PB_DECODE_NOINIT: Do not initialize the fields to default values.
80 * This is slightly faster if you do not need the default
81 * values and instead initialize the structure to 0 using
82 * e.g. memset(). This can also be used for merging two
83 * messages, i.e. combine already existing data with new
84 * values.
85 *
86 * PB_DECODE_DELIMITED: Input message starts with the message size as varint.
87 * Corresponds to parseDelimitedFrom() in Google's
88 * protobuf API.
89 *
90 * PB_DECODE_NULLTERMINATED: Stop reading when field tag is read as 0. This allows
91 * reading null terminated messages.
92 * NOTE: Until nanopb-0.4.0, pb_decode() also allows
93 * null-termination. This behaviour is not supported in
94 * most other protobuf implementations, so PB_DECODE_DELIMITED
95 * is a better option for compatibility.
96 *
97 * Multiple flags can be combined with bitwise or (| operator)
98 */
99#define PB_DECODE_NOINIT 0x01U
100#define PB_DECODE_DELIMITED 0x02U
101#define PB_DECODE_NULLTERMINATED 0x04U
102bool pb_decode_ex(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct, unsigned int flags);
103
104/* Defines for backwards compatibility with code written before nanopb-0.4.0 */
105#define pb_decode_noinit(s,f,d) pb_decode_ex(s,f,d, PB_DECODE_NOINIT)
106#define pb_decode_delimited(s,f,d) pb_decode_ex(s,f,d, PB_DECODE_DELIMITED)
107#define pb_decode_delimited_noinit(s,f,d) pb_decode_ex(s,f,d, PB_DECODE_DELIMITED | PB_DECODE_NOINIT)
108#define pb_decode_nullterminated(s,f,d) pb_decode_ex(s,f,d, PB_DECODE_NULLTERMINATED)
109
110/* Release any allocated pointer fields. If you use dynamic allocation, you should
111 * call this for any successfully decoded message when you are done with it. If
112 * pb_decode() returns with an error, the message is already released.
113 */
114void pb_release(const pb_msgdesc_t *fields, void *dest_struct);
115
116/**************************************
117 * Functions for manipulating streams *
118 **************************************/
119
120/* Create an input stream for reading from a memory buffer.
121 *
122 * msglen should be the actual length of the message, not the full size of
123 * allocated buffer.
124 *
125 * Alternatively, you can use a custom stream that reads directly from e.g.
126 * a file or a network socket.
127 */
128pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen);
129
130/* Function to read from a pb_istream_t. You can use this if you need to
131 * read some custom header data, or to read data in field callbacks.
132 */
133bool pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count);
134
135
136/************************************************
137 * Helper functions for writing field callbacks *
138 ************************************************/
139
140/* Decode the tag for the next field in the stream. Gives the wire type and
141 * field tag. At end of the message, returns false and sets eof to true. */
142bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof);
143
144/* Skip the field payload data, given the wire type. */
145bool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type);
146
147/* Decode an integer in the varint format. This works for enum, int32,
148 * int64, uint32 and uint64 field types. */
149#ifndef PB_WITHOUT_64BIT
150bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest);
151#else
152#define pb_decode_varint pb_decode_varint32
153#endif
154
155/* Decode an integer in the varint format. This works for enum, int32,
156 * and uint32 field types. */
157bool pb_decode_varint32(pb_istream_t *stream, uint32_t *dest);
158
159/* Decode a bool value in varint format. */
160bool pb_decode_bool(pb_istream_t *stream, bool *dest);
161
162/* Decode an integer in the zig-zagged svarint format. This works for sint32
163 * and sint64. */
164#ifndef PB_WITHOUT_64BIT
165bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest);
166#else
167bool pb_decode_svarint(pb_istream_t *stream, int32_t *dest);
168#endif
169
170/* Decode a fixed32, sfixed32 or float value. You need to pass a pointer to
171 * a 4-byte wide C variable. */
172bool pb_decode_fixed32(pb_istream_t *stream, void *dest);
173
174#ifndef PB_WITHOUT_64BIT
175/* Decode a fixed64, sfixed64 or double value. You need to pass a pointer to
176 * a 8-byte wide C variable. */
177bool pb_decode_fixed64(pb_istream_t *stream, void *dest);
178#endif
179
180#ifdef PB_CONVERT_DOUBLE_FLOAT
181/* Decode a double value into float variable. */
182bool pb_decode_double_as_float(pb_istream_t *stream, float *dest);
183#endif
184
185/* Make a limited-length substream for reading a PB_WT_STRING field. */
186bool pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream);
187bool pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream);
188
189#ifdef __cplusplus
190} /* extern "C" */
191#endif
192
193#endif
uint_least8_t pb_byte_t
Definition pb.h:227
pb_wire_type_t
Definition pb.h:420
bool pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
Definition pb_decode.c:82
bool pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct)
Definition pb_decode.c:1182
void pb_release(const pb_msgdesc_t *fields, void *dest_struct)
Definition pb_decode.c:1336
bool pb_decode_ex(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct, unsigned int flags)
Definition pb_decode.c:1154
bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
Definition pb_decode.c:1356
int bool
Definition pb_syshdr.h:57
unsigned int uint32_t
Definition pb_syshdr.h:24
unsigned long long uint64_t
Definition pb_syshdr.h:26
signed int int32_t
Definition pb_syshdr.h:23
signed long long int64_t
Definition pb_syshdr.h:25
bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
Definition pb_decode.c:243
bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
Definition pb_decode.c:1370
bool pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
Definition pb_decode.c:237
bool pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
Definition pb_decode.c:362
pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen)
Definition pb_decode.c:143
bool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
Definition pb_decode.c:308
bool pb_decode_bool(pb_istream_t *stream, bool *dest)
Definition pb_decode.c:1346
bool pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
Definition pb_decode.c:377
bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
Definition pb_decode.c:1393
bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
Definition pb_decode.c:1356
bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
Definition pb_decode.c:291
bool(* callback)(pb_istream_t *stream, pb_byte_t *buf, size_t count)
Definition pb_decode.h:37
const char * errmsg
Definition pb_decode.h:44
void * state
Definition pb_decode.h:40
size_t bytes_left
Definition pb_decode.h:41