commit | author | age
|
a6c014
|
1 |
/* pb_decode.h: Functions to decode protocol buffers. Depends on pb_decode.c. |
L |
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 |
|
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_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 |
/*************************** |
|
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 |
* Note: If this function returns with an error, it will not release any |
|
78 |
* dynamically allocated fields. You will need to call pb_release() yourself. |
|
79 |
*/ |
|
80 |
bool pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); |
|
81 |
|
|
82 |
/* Same as pb_decode, except expects the stream to start with the message size |
|
83 |
* encoded as varint. Corresponds to parseDelimitedFrom() in Google's |
|
84 |
* protobuf API. |
|
85 |
*/ |
|
86 |
bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); |
|
87 |
|
|
88 |
/* Same as pb_decode_delimited, except that it does not initialize the destination structure. |
|
89 |
* See pb_decode_noinit |
|
90 |
*/ |
|
91 |
bool pb_decode_delimited_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); |
|
92 |
|
|
93 |
/* Same as pb_decode, except allows the message to be terminated with a null byte. |
|
94 |
* NOTE: Until nanopb-0.4.0, pb_decode() also allows null-termination. This behaviour |
|
95 |
* is not supported in most other protobuf implementations, so pb_decode_delimited() |
|
96 |
* is a better option for compatibility. |
|
97 |
*/ |
|
98 |
bool pb_decode_nullterminated(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); |
|
99 |
|
|
100 |
#ifdef PB_ENABLE_MALLOC |
|
101 |
/* Release any allocated pointer fields. If you use dynamic allocation, you should |
|
102 |
* call this for any successfully decoded message when you are done with it. If |
|
103 |
* pb_decode() returns with an error, the message is already released. |
|
104 |
*/ |
|
105 |
void pb_release(const pb_field_t fields[], void *dest_struct); |
|
106 |
#endif |
|
107 |
|
|
108 |
|
|
109 |
/************************************** |
|
110 |
* Functions for manipulating streams * |
|
111 |
**************************************/ |
|
112 |
|
|
113 |
/* Create an input stream for reading from a memory buffer. |
|
114 |
* |
|
115 |
* Alternatively, you can use a custom stream that reads directly from e.g. |
|
116 |
* a file or a network socket. |
|
117 |
*/ |
|
118 |
pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t bufsize); |
|
119 |
|
|
120 |
/* Function to read from a pb_istream_t. You can use this if you need to |
|
121 |
* read some custom header data, or to read data in field callbacks. |
|
122 |
*/ |
|
123 |
bool pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count); |
|
124 |
|
|
125 |
|
|
126 |
/************************************************ |
|
127 |
* Helper functions for writing field callbacks * |
|
128 |
************************************************/ |
|
129 |
|
|
130 |
/* Decode the tag for the next field in the stream. Gives the wire type and |
|
131 |
* field tag. At end of the message, returns false and sets eof to true. */ |
|
132 |
bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof); |
|
133 |
|
|
134 |
/* Skip the field payload data, given the wire type. */ |
|
135 |
bool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type); |
|
136 |
|
|
137 |
/* Decode an integer in the varint format. This works for enum, int32, |
|
138 |
* int64, uint32 and uint64 field types. */ |
|
139 |
#ifndef PB_WITHOUT_64BIT |
|
140 |
bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest); |
|
141 |
#else |
|
142 |
#define pb_decode_varint pb_decode_varint32 |
|
143 |
#endif |
|
144 |
|
|
145 |
/* Decode an integer in the varint format. This works for enum, int32, |
|
146 |
* and uint32 field types. */ |
|
147 |
bool pb_decode_varint32(pb_istream_t *stream, uint32_t *dest); |
|
148 |
|
|
149 |
/* Decode a bool value in varint format. */ |
|
150 |
bool pb_decode_bool(pb_istream_t *stream, bool *dest); |
|
151 |
|
|
152 |
/* Decode an integer in the zig-zagged svarint format. This works for sint32 |
|
153 |
* and sint64. */ |
|
154 |
#ifndef PB_WITHOUT_64BIT |
|
155 |
bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest); |
|
156 |
#else |
|
157 |
bool pb_decode_svarint(pb_istream_t *stream, int32_t *dest); |
|
158 |
#endif |
|
159 |
|
|
160 |
/* Decode a fixed32, sfixed32 or float value. You need to pass a pointer to |
|
161 |
* a 4-byte wide C variable. */ |
|
162 |
bool pb_decode_fixed32(pb_istream_t *stream, void *dest); |
|
163 |
|
|
164 |
#ifndef PB_WITHOUT_64BIT |
|
165 |
/* Decode a fixed64, sfixed64 or double value. You need to pass a pointer to |
|
166 |
* a 8-byte wide C variable. */ |
|
167 |
bool pb_decode_fixed64(pb_istream_t *stream, void *dest); |
|
168 |
#endif |
|
169 |
|
|
170 |
/* Make a limited-length substream for reading a PB_WT_STRING field. */ |
|
171 |
bool pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream); |
|
172 |
bool pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream); |
|
173 |
|
|
174 |
#ifdef __cplusplus |
|
175 |
} /* extern "C" */ |
|
176 |
#endif |
|
177 |
|
|
178 |
#endif |