WPILibC++ 2025.1.1
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 * Modified for WPILib Use
6 */
7
8#ifndef PB_DECODE_H_INCLUDED
9#define PB_DECODE_H_INCLUDED
10
11#include "pb.h"
12
13/* Structure for defining custom input streams. You will need to provide
14 * a callback function to read the bytes from your storage, which can be
15 * for example a file or a network socket.
16 *
17 * The callback must conform to these rules:
18 *
19 * 1) Return false on IO errors. This will cause decoding to abort.
20 * 2) You can use state to store your own data (e.g. buffer pointer),
21 * and rely on pb_read to verify that no-body reads past bytes_left.
22 * 3) Your callback may be used with substreams, in which case bytes_left
23 * is different than from the main stream. Don't use bytes_left to compute
24 * any pointers.
25 */
27{
28#ifdef PB_BUFFER_ONLY
29 /* Callback pointer is not used in buffer-only configuration.
30 * Having an int pointer here allows binary compatibility but
31 * gives an error if someone tries to assign callback function.
32 */
33 int *callback;
34#else
35 bool (*callback)(pb_istream_t *stream, pb_byte_t *buf, size_t count);
36#endif
37
38 /* state is a free field for use of the callback function defined above.
39 * Note that when pb_istream_from_buffer() is used, it reserves this field
40 * for its own use.
41 */
42 void *state;
43
44 /* Maximum number of bytes left in this stream. Callback can report
45 * EOF before this limit is reached. Setting a limit is recommended
46 * when decoding directly from file or network streams to avoid
47 * denial-of-service by excessively long messages.
48 */
49 size_t bytes_left;
50
51#ifndef PB_NO_ERRMSG
52 /* Pointer to constant (ROM) string when decoding function returns error */
53 const char *errmsg;
54#endif
55};
56
57#ifndef PB_NO_ERRMSG
58#define PB_ISTREAM_EMPTY {0,0,0,0}
59#else
60#define PB_ISTREAM_EMPTY {0,0,0}
61#endif
62
63/***************************
64 * Main decoding functions *
65 ***************************/
66
67/* Decode a single protocol buffers message from input stream into a C structure.
68 * Returns true on success, false on any failure.
69 * The actual struct pointed to by dest must match the description in fields.
70 * Callback fields of the destination structure must be initialized by caller.
71 * All other fields will be initialized by this function.
72 *
73 * Example usage:
74 * MyMessage msg = {};
75 * uint8_t buffer[64];
76 * pb_istream_t stream;
77 *
78 * // ... read some data into buffer ...
79 *
80 * stream = pb_istream_from_buffer(buffer, count);
81 * pb_decode(&stream, MyMessage_fields, &msg);
82 */
83bool pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct);
84
85/* Extended version of pb_decode, with several options to control
86 * the decoding process:
87 *
88 * PB_DECODE_NOINIT: Do not initialize the fields to default values.
89 * This is slightly faster if you do not need the default
90 * values and instead initialize the structure to 0 using
91 * e.g. memset(). This can also be used for merging two
92 * messages, i.e. combine already existing data with new
93 * values.
94 *
95 * PB_DECODE_DELIMITED: Input message starts with the message size as varint.
96 * Corresponds to parseDelimitedFrom() in Google's
97 * protobuf API.
98 *
99 * PB_DECODE_NULLTERMINATED: Stop reading when field tag is read as 0. This allows
100 * reading null terminated messages.
101 * NOTE: Until nanopb-0.4.0, pb_decode() also allows
102 * null-termination. This behaviour is not supported in
103 * most other protobuf implementations, so PB_DECODE_DELIMITED
104 * is a better option for compatibility.
105 *
106 * Multiple flags can be combined with bitwise or (| operator)
107 */
108#define PB_DECODE_NOINIT 0x01U
109#define PB_DECODE_DELIMITED 0x02U
110#define PB_DECODE_NULLTERMINATED 0x04U
111bool pb_decode_ex(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct, unsigned int flags);
112
113/* Defines for backwards compatibility with code written before nanopb-0.4.0 */
114#define pb_decode_noinit(s,f,d) pb_decode_ex(s,f,d, PB_DECODE_NOINIT)
115#define pb_decode_delimited(s,f,d) pb_decode_ex(s,f,d, PB_DECODE_DELIMITED)
116#define pb_decode_delimited_noinit(s,f,d) pb_decode_ex(s,f,d, PB_DECODE_DELIMITED | PB_DECODE_NOINIT)
117#define pb_decode_nullterminated(s,f,d) pb_decode_ex(s,f,d, PB_DECODE_NULLTERMINATED)
118
119/* Release any allocated pointer fields. If you use dynamic allocation, you should
120 * call this for any successfully decoded message when you are done with it. If
121 * pb_decode() returns with an error, the message is already released.
122 */
123void pb_release(const pb_msgdesc_t *fields, void *dest_struct);
124
125/**************************************
126 * Functions for manipulating streams *
127 **************************************/
128
129/* Create an input stream for reading from a memory buffer.
130 *
131 * msglen should be the actual length of the message, not the full size of
132 * allocated buffer.
133 *
134 * Alternatively, you can use a custom stream that reads directly from e.g.
135 * a file or a network socket.
136 */
138
139/* Function to read from a pb_istream_t. You can use this if you need to
140 * read some custom header data, or to read data in field callbacks.
141 */
142bool pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count);
143
144
145/************************************************
146 * Helper functions for writing field callbacks *
147 ************************************************/
148
149/* Decode the tag for the next field in the stream. Gives the wire type and
150 * field tag. At end of the message, returns false and sets eof to true. */
151bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof);
152
153/* Skip the field payload data, given the wire type. */
155
156/* Decode an integer in the varint format. This works for enum, int32,
157 * int64, uint32 and uint64 field types. */
158#ifndef PB_WITHOUT_64BIT
159bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest);
160#else
161#define pb_decode_varint pb_decode_varint32
162#endif
163
164/* Decode an integer in the varint format. This works for enum, int32,
165 * and uint32 field types. */
166bool pb_decode_varint32(pb_istream_t *stream, uint32_t *dest);
167
168/* Decode a bool value in varint format. */
169bool pb_decode_bool(pb_istream_t *stream, bool *dest);
170
171/* Decode an integer in the zig-zagged svarint format. This works for sint32
172 * and sint64. */
173#ifndef PB_WITHOUT_64BIT
174bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest);
175#else
176bool pb_decode_svarint(pb_istream_t *stream, int32_t *dest);
177#endif
178
179/* Decode a fixed32, sfixed32 or float value. You need to pass a pointer to
180 * a 4-byte wide C variable. */
181bool pb_decode_fixed32(pb_istream_t *stream, void *dest);
182
183#ifndef PB_WITHOUT_64BIT
184/* Decode a fixed64, sfixed64 or double value. You need to pass a pointer to
185 * a 8-byte wide C variable. */
186bool pb_decode_fixed64(pb_istream_t *stream, void *dest);
187#endif
188
189#ifdef PB_CONVERT_DOUBLE_FLOAT
190/* Decode a double value into float variable. */
191bool pb_decode_double_as_float(pb_istream_t *stream, float *dest);
192#endif
193
194/* Make a limited-length substream for reading a PB_WT_STRING field. */
197
198#endif
uint_least8_t pb_byte_t
Definition pb.h:228
pb_wire_type_t
Definition pb.h:430
bool pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
bool pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct)
void pb_release(const pb_msgdesc_t *fields, void *dest_struct)
bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
bool pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
bool pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen)
bool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
bool pb_decode_bool(pb_istream_t *stream, bool *dest)
bool pb_decode_ex(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct, unsigned int flags)
bool pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
Definition pb_decode.h:27
bool(* callback)(pb_istream_t *stream, pb_byte_t *buf, size_t count)
Definition pb_decode.h:35
const char * errmsg
Definition pb_decode.h:53
void * state
Definition pb_decode.h:42
size_t bytes_left
Definition pb_decode.h:49
Definition pb.h:331