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 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 #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 */ 74 bool 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 102 bool 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 #ifdef PB_ENABLE_MALLOC 111 /* Release any allocated pointer fields. If you use dynamic allocation, you should 112 * call this for any successfully decoded message when you are done with it. If 113 * pb_decode() returns with an error, the message is already released. 114 */ 115 void pb_release(const pb_msgdesc_t *fields, void *dest_struct); 116 #else 117 /* Allocation is not supported, so release is no-op */ 118 #define pb_release(fields, dest_struct) PB_UNUSED(fields); PB_UNUSED(dest_struct); 119 #endif 120 121 122 /************************************** 123 * Functions for manipulating streams * 124 **************************************/ 125 126 /* Create an input stream for reading from a memory buffer. 127 * 128 * msglen should be the actual length of the message, not the full size of 129 * allocated buffer. 130 * 131 * Alternatively, you can use a custom stream that reads directly from e.g. 132 * a file or a network socket. 133 */ 134 pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen); 135 136 /* Function to read from a pb_istream_t. You can use this if you need to 137 * read some custom header data, or to read data in field callbacks. 138 */ 139 bool pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count); 140 141 142 /************************************************ 143 * Helper functions for writing field callbacks * 144 ************************************************/ 145 146 /* Decode the tag for the next field in the stream. Gives the wire type and 147 * field tag. At end of the message, returns false and sets eof to true. */ 148 bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof); 149 150 /* Skip the field payload data, given the wire type. */ 151 bool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type); 152 153 /* Decode an integer in the varint format. This works for enum, int32, 154 * int64, uint32 and uint64 field types. */ 155 #ifndef PB_WITHOUT_64BIT 156 bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest); 157 #else 158 #define pb_decode_varint pb_decode_varint32 159 #endif 160 161 /* Decode an integer in the varint format. This works for enum, int32, 162 * and uint32 field types. */ 163 bool pb_decode_varint32(pb_istream_t *stream, uint32_t *dest); 164 165 /* Decode a bool value in varint format. */ 166 bool pb_decode_bool(pb_istream_t *stream, bool *dest); 167 168 /* Decode an integer in the zig-zagged svarint format. This works for sint32 169 * and sint64. */ 170 #ifndef PB_WITHOUT_64BIT 171 bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest); 172 #else 173 bool pb_decode_svarint(pb_istream_t *stream, int32_t *dest); 174 #endif 175 176 /* Decode a fixed32, sfixed32 or float value. You need to pass a pointer to 177 * a 4-byte wide C variable. */ 178 bool pb_decode_fixed32(pb_istream_t *stream, void *dest); 179 180 #ifndef PB_WITHOUT_64BIT 181 /* Decode a fixed64, sfixed64 or double value. You need to pass a pointer to 182 * a 8-byte wide C variable. */ 183 bool pb_decode_fixed64(pb_istream_t *stream, void *dest); 184 #endif 185 186 #ifdef PB_CONVERT_DOUBLE_FLOAT 187 /* Decode a double value into float variable. */ 188 bool pb_decode_double_as_float(pb_istream_t *stream, float *dest); 189 #endif 190 191 /* Make a limited-length substream for reading a PB_WT_STRING field. */ 192 bool pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream); 193 bool pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream); 194 195 #ifdef __cplusplus 196 } /* extern "C" */ 197 #endif 198 199 #endif 200