1 /* Common parts of the nanopb library. Most of these are quite low-level
2 * stuff. For the high-level interface, see pb_encode.h and pb_decode.h.
8 /*****************************************************************
9 * Nanopb compilation time options. You can change these here by *
10 * uncommenting the lines, or on the compiler command line. *
11 *****************************************************************/
13 /* Define this if your CPU architecture is big endian, i.e. it
14 * stores the most-significant byte first. */
15 /* #define __BIG_ENDIAN__ 1 */
17 /* Increase the number of required fields that are tracked.
18 * A compiler warning will tell if you need this. */
19 /* #define PB_MAX_REQUIRED_FIELDS 256 */
21 /* Add support for tag numbers > 255 and fields larger than 255 bytes. */
22 /* #define PB_FIELD_16BIT 1 */
24 /* Add support for tag numbers > 65536 and fields larger than 65536 bytes. */
25 /* #define PB_FIELD_32BIT 1 */
27 /* Disable support for error messages in order to save some code space. */
28 /* #define PB_NO_ERRMSG 1 */
30 /* Disable support for custom streams (support only memory buffers). */
31 /* #define PB_BUFFER_ONLY 1 */
33 /* Switch back to the old-style callback function signature.
34 * This was the default until nanopb-0.2.1. */
35 /* #define PB_OLD_CALLBACK_STYLE */
38 /******************************************************************
39 * You usually don't need to change anything below this line. *
40 * Feel free to look around and use the defined macros, though. *
41 ******************************************************************/
44 /* Version of the nanopb library. Just in case you want to check it in
45 * your own program. */
46 #define NANOPB_VERSION nanopb-0.2.5-dev
48 /* Include all the system headers needed by nanopb. You will need the
49 * definitions of the following:
50 * - strlen, memcpy, memset functions
51 * - [u]int8_t, [u]int16_t, [u]int32_t, [u]int64_t
55 * If you don't have the standard header files, you can instead provide
56 * a custom header that defines or includes all this. In that case,
57 * define PB_SYSTEM_HEADER to the path of this file.
59 #ifdef PB_SYSTEM_HEADER
60 #include PB_SYSTEM_HEADER
68 /* Macro for defining packed structures (compiler dependent).
69 * This just reduces memory requirements, but is not required.
71 #if defined(__GNUC__) || defined(__clang__)
72 /* For GCC and clang */
73 # define PB_PACKED_STRUCT_START
74 # define PB_PACKED_STRUCT_END
75 # define pb_packed __attribute__((packed))
76 #elif defined(__ICCARM__)
77 /* For IAR ARM compiler */
78 # define PB_PACKED_STRUCT_START _Pragma("pack(push, 1)")
79 # define PB_PACKED_STRUCT_END _Pragma("pack(pop)")
81 #elif defined(_MSC_VER) && (_MSC_VER >= 1500)
82 /* For Microsoft Visual C++ */
83 # define PB_PACKED_STRUCT_START __pragma(pack(push, 1))
84 # define PB_PACKED_STRUCT_END __pragma(pack(pop))
87 /* Unknown compiler */
88 # define PB_PACKED_STRUCT_START
89 # define PB_PACKED_STRUCT_END
93 /* Handly macro for suppressing unreferenced-parameter compiler warnings. */
95 #define UNUSED(x) (void)(x)
98 /* Compile-time assertion, used for checking compatible compilation options.
99 * If this fails on your compiler for some reason, use #define STATIC_ASSERT
101 #ifndef STATIC_ASSERT
102 #define STATIC_ASSERT(COND,MSG) typedef char STATIC_ASSERT_MSG(MSG, __LINE__, __COUNTER__)[(COND)?1:-1];
103 #define STATIC_ASSERT_MSG(MSG, LINE, COUNTER) STATIC_ASSERT_MSG_(MSG, LINE, COUNTER)
104 #define STATIC_ASSERT_MSG_(MSG, LINE, COUNTER) static_assertion_##MSG##LINE##COUNTER
107 /* Number of required fields to keep track of. */
108 #ifndef PB_MAX_REQUIRED_FIELDS
109 #define PB_MAX_REQUIRED_FIELDS 64
112 #if PB_MAX_REQUIRED_FIELDS < 64
113 #error You should not lower PB_MAX_REQUIRED_FIELDS from the default value (64).
116 /* List of possible field types. These are used in the autogenerated code.
117 * Least-significant 4 bits tell the scalar type
118 * Most-significant 4 bits specify repeated/required/packed etc.
121 typedef uint8_t pb_type_t;
123 /**** Field data types ****/
126 #define PB_LTYPE_VARINT 0x00 /* int32, int64, enum, bool */
127 #define PB_LTYPE_UVARINT 0x01 /* uint32, uint64 */
128 #define PB_LTYPE_SVARINT 0x02 /* sint32, sint64 */
129 #define PB_LTYPE_FIXED32 0x03 /* fixed32, sfixed32, float */
130 #define PB_LTYPE_FIXED64 0x04 /* fixed64, sfixed64, double */
132 /* Marker for last packable field type. */
133 #define PB_LTYPE_LAST_PACKABLE 0x04
135 /* Byte array with pre-allocated buffer.
136 * data_size is the length of the allocated PB_BYTES_ARRAY structure. */
137 #define PB_LTYPE_BYTES 0x05
139 /* String with pre-allocated buffer.
140 * data_size is the maximum length. */
141 #define PB_LTYPE_STRING 0x06
144 * submsg_fields is pointer to field descriptions */
145 #define PB_LTYPE_SUBMESSAGE 0x07
147 /* Extension pseudo-field
148 * The field contains a pointer to pb_extension_t */
149 #define PB_LTYPE_EXTENSION 0x08
151 /* Number of declared LTYPES */
152 #define PB_LTYPES_COUNT 9
153 #define PB_LTYPE_MASK 0x0F
155 /**** Field repetition rules ****/
157 #define PB_HTYPE_REQUIRED 0x00
158 #define PB_HTYPE_OPTIONAL 0x10
159 #define PB_HTYPE_REPEATED 0x20
160 #define PB_HTYPE_MASK 0x30
162 /**** Field allocation types ****/
164 #define PB_ATYPE_STATIC 0x00
165 #define PB_ATYPE_POINTER 0x80
166 #define PB_ATYPE_CALLBACK 0x40
167 #define PB_ATYPE_MASK 0xC0
169 #define PB_ATYPE(x) ((x) & PB_ATYPE_MASK)
170 #define PB_HTYPE(x) ((x) & PB_HTYPE_MASK)
171 #define PB_LTYPE(x) ((x) & PB_LTYPE_MASK)
173 /* Data type used for storing sizes of struct fields
176 #if defined(PB_FIELD_32BIT)
177 typedef uint32_t pb_size_t;
178 typedef int32_t pb_ssize_t;
179 #elif defined(PB_FIELD_16BIT)
180 typedef uint16_t pb_size_t;
181 typedef int16_t pb_ssize_t;
183 typedef uint8_t pb_size_t;
184 typedef int8_t pb_ssize_t;
187 /* This structure is used in auto-generated constants
188 * to specify struct fields.
189 * You can change field sizes if you need structures
190 * larger than 256 bytes or field tags larger than 256.
191 * The compiler should complain if your .proto has such
192 * structures. Fix that by defining PB_FIELD_16BIT or
195 PB_PACKED_STRUCT_START
196 typedef struct _pb_field_t pb_field_t;
200 pb_size_t data_offset; /* Offset of field data, relative to previous field. */
201 pb_ssize_t size_offset; /* Offset of array size or has-boolean, relative to data */
202 pb_size_t data_size; /* Data size in bytes for a single item */
203 pb_size_t array_size; /* Maximum number of entries in array */
205 /* Field definitions for submessage
206 * OR default value for all other non-array, non-callback types
207 * If null, then field will zeroed. */
212 /* Make sure that the standard integer types are of the expected sizes.
213 * All kinds of things may break otherwise.. atleast all fixed* types. */
214 STATIC_ASSERT(sizeof(int8_t) == 1, INT8_T_WRONG_SIZE)
215 STATIC_ASSERT(sizeof(uint8_t) == 1, UINT8_T_WRONG_SIZE)
216 STATIC_ASSERT(sizeof(int16_t) == 2, INT16_T_WRONG_SIZE)
217 STATIC_ASSERT(sizeof(uint16_t) == 2, UINT16_T_WRONG_SIZE)
218 STATIC_ASSERT(sizeof(int32_t) == 4, INT32_T_WRONG_SIZE)
219 STATIC_ASSERT(sizeof(uint32_t) == 4, UINT32_T_WRONG_SIZE)
220 STATIC_ASSERT(sizeof(int64_t) == 8, INT64_T_WRONG_SIZE)
221 STATIC_ASSERT(sizeof(uint64_t) == 8, UINT64_T_WRONG_SIZE)
223 /* This structure is used for 'bytes' arrays.
224 * It has the number of bytes in the beginning, and after that an array.
225 * Note that actual structs used will have a different length of bytes array.
227 struct _pb_bytes_array_t {
232 typedef struct _pb_bytes_array_t pb_bytes_array_t;
234 /* This structure is used for giving the callback function.
235 * It is stored in the message structure and filled in by the method that
238 * The decoding callback will be given a limited-length stream
239 * If the wire type was string, the length is the length of the string.
240 * If the wire type was a varint/fixed32/fixed64, the length is the length
241 * of the actual value.
242 * The function may be called multiple times (especially for repeated types,
243 * but also otherwise if the message happens to contain the field multiple
246 * The encoding callback will receive the actual output stream.
247 * It should write all the data in one call, including the field tag and
248 * wire type. It can write multiple fields.
250 * The callback can be null if you want to skip a field.
252 typedef struct _pb_istream_t pb_istream_t;
253 typedef struct _pb_ostream_t pb_ostream_t;
254 typedef struct _pb_callback_t pb_callback_t;
255 struct _pb_callback_t {
256 #ifdef PB_OLD_CALLBACK_STYLE
257 /* Deprecated since nanopb-0.2.1 */
259 bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void *arg);
260 bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, const void *arg);
263 /* New function signature, which allows modifying arg contents in callback. */
265 bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void **arg);
266 bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, void * const *arg);
270 /* Free arg for use by callback */
274 /* Wire types. Library user needs these only in encoder callbacks. */
282 /* Structure for defining the handling of unknown/extension fields.
283 * Usually the pb_extension_type_t structure is automatically generated,
284 * while the pb_extension_t structure is created by the user. However,
285 * if you want to catch all unknown fields, you can also create a custom
286 * pb_extension_type_t with your own callback.
288 typedef struct _pb_extension_type_t pb_extension_type_t;
289 typedef struct _pb_extension_t pb_extension_t;
290 struct _pb_extension_type_t {
291 /* Called for each unknown field in the message.
292 * If you handle the field, read off all of its data and return true.
293 * If you do not handle the field, do not read anything and return true.
294 * If you run into an error, return false.
295 * Set to NULL for default handler.
297 bool (*decode)(pb_istream_t *stream, pb_extension_t *extension,
298 uint32_t tag, pb_wire_type_t wire_type);
300 /* Called once after all regular fields have been encoded.
301 * If you have something to write, do so and return true.
302 * If you do not have anything to write, just return true.
303 * If you run into an error, return false.
304 * Set to NULL for default handler.
306 bool (*encode)(pb_ostream_t *stream, const pb_extension_t *extension);
308 /* Free field for use by the callback. */
312 struct _pb_extension_t {
313 /* Type describing the extension field. Usually you'll initialize
314 * this to a pointer to the automatically generated structure. */
315 const pb_extension_type_t *type;
317 /* Destination for the decoded data. This must match the datatype
318 * of the extension field. */
321 /* Pointer to the next extension handler, or NULL.
322 * If this extension does not match a field, the next handler is
323 * automatically called. */
324 pb_extension_t *next;
327 /* These macros are used to declare pb_field_t's in the constant array. */
328 /* Size of a structure member, in bytes. */
329 #define pb_membersize(st, m) (sizeof ((st*)0)->m)
330 /* Number of entries in an array. */
331 #define pb_arraysize(st, m) (pb_membersize(st, m) / pb_membersize(st, m[0]))
332 /* Delta from start of one member to the start of another member. */
333 #define pb_delta(st, m1, m2) ((int)offsetof(st, m1) - (int)offsetof(st, m2))
334 /* Marks the end of the field list */
335 #define PB_LAST_FIELD {0,(pb_type_t) 0,0,0,0,0,0}
337 /* Macros for filling in the data_offset field */
338 /* data_offset for first field in a message */
339 #define PB_DATAOFFSET_FIRST(st, m1, m2) (offsetof(st, m1))
340 /* data_offset for subsequent fields */
341 #define PB_DATAOFFSET_OTHER(st, m1, m2) (offsetof(st, m1) - offsetof(st, m2) - pb_membersize(st, m2))
342 /* Choose first/other based on m1 == m2 (deprecated, remains for backwards compatibility) */
343 #define PB_DATAOFFSET_CHOOSE(st, m1, m2) (int)(offsetof(st, m1) == offsetof(st, m2) \
344 ? PB_DATAOFFSET_FIRST(st, m1, m2) \
345 : PB_DATAOFFSET_OTHER(st, m1, m2))
347 /* Required fields are the simplest. They just have delta (padding) from
348 * previous field end, and the size of the field. Pointer is used for
349 * submessages and default values.
351 #define PB_REQUIRED_STATIC(tag, st, m, fd, ltype, ptr) \
352 {tag, PB_ATYPE_STATIC | PB_HTYPE_REQUIRED | ltype, \
353 fd, 0, pb_membersize(st, m), 0, ptr}
355 /* Optional fields add the delta to the has_ variable. */
356 #define PB_OPTIONAL_STATIC(tag, st, m, fd, ltype, ptr) \
357 {tag, PB_ATYPE_STATIC | PB_HTYPE_OPTIONAL | ltype, \
359 pb_delta(st, has_ ## m, m), \
360 pb_membersize(st, m), 0, ptr}
362 /* Repeated fields have a _count field and also the maximum number of entries. */
363 #define PB_REPEATED_STATIC(tag, st, m, fd, ltype, ptr) \
364 {tag, PB_ATYPE_STATIC | PB_HTYPE_REPEATED | ltype, \
366 pb_delta(st, m ## _count, m), \
367 pb_membersize(st, m[0]), \
368 pb_arraysize(st, m), ptr}
370 /* Allocated fields carry the size of the actual data, not the pointer */
371 #define PB_REQUIRED_POINTER(tag, st, m, fd, ltype, ptr) \
372 {tag, PB_ATYPE_POINTER | PB_HTYPE_REQUIRED | ltype, \
373 fd, 0, pb_membersize(st, m[0]), 0, ptr}
375 /* Optional fields don't need a has_ variable, as information would be redundant */
376 #define PB_OPTIONAL_POINTER(tag, st, m, fd, ltype, ptr) \
377 {tag, PB_ATYPE_POINTER | PB_HTYPE_OPTIONAL | ltype, \
378 fd, 0, pb_membersize(st, m[0]), 0, ptr}
380 #define PB_REPEATED_POINTER(tag, st, m, fd, ltype, ptr) \
381 {tag, PB_ATYPE_POINTER | PB_HTYPE_REPEATED | ltype, \
383 pb_delta(st, m ## _count, m), \
384 pb_membersize(st, m[0]), 0, ptr}
386 /* Callbacks are much like required fields except with special datatype. */
387 #define PB_REQUIRED_CALLBACK(tag, st, m, fd, ltype, ptr) \
388 {tag, PB_ATYPE_CALLBACK | PB_HTYPE_REQUIRED | ltype, \
389 fd, 0, pb_membersize(st, m), 0, ptr}
391 #define PB_OPTIONAL_CALLBACK(tag, st, m, fd, ltype, ptr) \
392 {tag, PB_ATYPE_CALLBACK | PB_HTYPE_OPTIONAL | ltype, \
393 fd, 0, pb_membersize(st, m), 0, ptr}
395 #define PB_REPEATED_CALLBACK(tag, st, m, fd, ltype, ptr) \
396 {tag, PB_ATYPE_CALLBACK | PB_HTYPE_REPEATED | ltype, \
397 fd, 0, pb_membersize(st, m), 0, ptr}
399 /* Optional extensions don't have the has_ field, as that would be redundant. */
400 #define PB_OPTEXT_STATIC(tag, st, m, fd, ltype, ptr) \
401 {tag, PB_ATYPE_STATIC | PB_HTYPE_OPTIONAL | ltype, \
404 pb_membersize(st, m), 0, ptr}
406 #define PB_OPTEXT_CALLBACK(tag, st, m, fd, ltype, ptr) \
407 {tag, PB_ATYPE_CALLBACK | PB_HTYPE_OPTIONAL | ltype, \
408 0, 0, pb_membersize(st, m), 0, ptr}
410 /* The mapping from protobuf types to LTYPEs is done using these macros. */
411 #define PB_LTYPE_MAP_BOOL PB_LTYPE_VARINT
412 #define PB_LTYPE_MAP_BYTES PB_LTYPE_BYTES
413 #define PB_LTYPE_MAP_DOUBLE PB_LTYPE_FIXED64
414 #define PB_LTYPE_MAP_ENUM PB_LTYPE_VARINT
415 #define PB_LTYPE_MAP_FIXED32 PB_LTYPE_FIXED32
416 #define PB_LTYPE_MAP_FIXED64 PB_LTYPE_FIXED64
417 #define PB_LTYPE_MAP_FLOAT PB_LTYPE_FIXED32
418 #define PB_LTYPE_MAP_INT32 PB_LTYPE_VARINT
419 #define PB_LTYPE_MAP_INT64 PB_LTYPE_VARINT
420 #define PB_LTYPE_MAP_MESSAGE PB_LTYPE_SUBMESSAGE
421 #define PB_LTYPE_MAP_SFIXED32 PB_LTYPE_FIXED32
422 #define PB_LTYPE_MAP_SFIXED64 PB_LTYPE_FIXED64
423 #define PB_LTYPE_MAP_SINT32 PB_LTYPE_SVARINT
424 #define PB_LTYPE_MAP_SINT64 PB_LTYPE_SVARINT
425 #define PB_LTYPE_MAP_STRING PB_LTYPE_STRING
426 #define PB_LTYPE_MAP_UINT32 PB_LTYPE_UVARINT
427 #define PB_LTYPE_MAP_UINT64 PB_LTYPE_UVARINT
428 #define PB_LTYPE_MAP_EXTENSION PB_LTYPE_EXTENSION
430 /* This is the actual macro used in field descriptions.
431 * It takes these arguments:
433 * - Field type: BOOL, BYTES, DOUBLE, ENUM, FIXED32, FIXED64,
434 * FLOAT, INT32, INT64, MESSAGE, SFIXED32, SFIXED64
435 * SINT32, SINT64, STRING, UINT32, UINT64 or EXTENSION
436 * - Field rules: REQUIRED, OPTIONAL or REPEATED
437 * - Allocation: STATIC or CALLBACK
440 * - Previous field name (or field name again for first field)
441 * - Pointer to default value or submsg fields.
444 #define PB_FIELD(tag, type, rules, allocation, message, field, prevfield, ptr) \
445 PB_ ## rules ## _ ## allocation(tag, message, field, \
446 PB_DATAOFFSET_CHOOSE(message, field, prevfield), \
447 PB_LTYPE_MAP_ ## type, ptr)
449 /* This is a new version of the macro used by nanopb generator from
450 * version 0.2.3 onwards. It avoids the use of a ternary expression in
451 * the initialization, which confused some compilers.
453 * - Placement: FIRST or OTHER, depending on if this is the first field in structure.
456 #define PB_FIELD2(tag, type, rules, allocation, placement, message, field, prevfield, ptr) \
457 PB_ ## rules ## _ ## allocation(tag, message, field, \
458 PB_DATAOFFSET_ ## placement(message, field, prevfield), \
459 PB_LTYPE_MAP_ ## type, ptr)
462 /* These macros are used for giving out error messages.
463 * They are mostly a debugging aid; the main error information
464 * is the true/false return value from functions.
465 * Some code space can be saved by disabling the error
466 * messages if not used.
469 #define PB_RETURN_ERROR(stream,msg) return false
470 #define PB_GET_ERROR(stream) "(errmsg disabled)"
472 #define PB_RETURN_ERROR(stream,msg) \
474 if ((stream)->errmsg == NULL) \
475 (stream)->errmsg = (msg); \
478 #define PB_GET_ERROR(stream) ((stream)->errmsg ? (stream)->errmsg : "(none)")