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