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 /* Enable support for dynamically allocated fields */
14 /* #define PB_ENABLE_MALLOC 1 */
16 /* Define this if your CPU architecture is big endian, i.e. it
17 * stores the most-significant byte first. */
18 /* #define __BIG_ENDIAN__ 1 */
20 /* Increase the number of required fields that are tracked.
21 * A compiler warning will tell if you need this. */
22 /* #define PB_MAX_REQUIRED_FIELDS 256 */
24 /* Add support for tag numbers > 255 and fields larger than 255 bytes. */
25 /* #define PB_FIELD_16BIT 1 */
27 /* Add support for tag numbers > 65536 and fields larger than 65536 bytes. */
28 /* #define PB_FIELD_32BIT 1 */
30 /* Disable support for error messages in order to save some code space. */
31 /* #define PB_NO_ERRMSG 1 */
33 /* Disable support for custom streams (support only memory buffers). */
34 /* #define PB_BUFFER_ONLY 1 */
36 /* Switch back to the old-style callback function signature.
37 * This was the default until nanopb-0.2.1. */
38 /* #define PB_OLD_CALLBACK_STYLE */
41 /******************************************************************
42 * You usually don't need to change anything below this line. *
43 * Feel free to look around and use the defined macros, though. *
44 ******************************************************************/
47 /* Version of the nanopb library. Just in case you want to check it in
48 * your own program. */
49 #define NANOPB_VERSION nanopb-0.3.0-dev
51 /* Include all the system headers needed by nanopb. You will need the
52 * definitions of the following:
53 * - strlen, memcpy, memset functions
54 * - [u]int8_t, [u]int16_t, [u]int32_t, [u]int64_t
58 * If you don't have the standard header files, you can instead provide
59 * a custom header that defines or includes all this. In that case,
60 * define PB_SYSTEM_HEADER to the path of this file.
62 #ifdef PB_SYSTEM_HEADER
63 #include PB_SYSTEM_HEADER
70 #ifdef PB_ENABLE_MALLOC
75 /* Macro for defining packed structures (compiler dependent).
76 * This just reduces memory requirements, but is not required.
78 #if defined(__GNUC__) || defined(__clang__)
79 /* For GCC and clang */
80 # define PB_PACKED_STRUCT_START
81 # define PB_PACKED_STRUCT_END
82 # define pb_packed __attribute__((packed))
83 #elif defined(__ICCARM__) || defined(__CC_ARM)
84 /* For IAR ARM and Keil MDK-ARM compilers */
85 # define PB_PACKED_STRUCT_START _Pragma("pack(push, 1)")
86 # define PB_PACKED_STRUCT_END _Pragma("pack(pop)")
88 #elif defined(_MSC_VER) && (_MSC_VER >= 1500)
89 /* For Microsoft Visual C++ */
90 # define PB_PACKED_STRUCT_START __pragma(pack(push, 1))
91 # define PB_PACKED_STRUCT_END __pragma(pack(pop))
94 /* Unknown compiler */
95 # define PB_PACKED_STRUCT_START
96 # define PB_PACKED_STRUCT_END
100 /* Handly macro for suppressing unreferenced-parameter compiler warnings. */
102 #define PB_UNUSED(x) (void)(x)
105 /* Compile-time assertion, used for checking compatible compilation options.
106 * If this does not work properly on your compiler, use
107 * #define PB_NO_STATIC_ASSERT to disable it.
109 * But before doing that, check carefully the error message / place where it
110 * comes from to see if the error has a real cause. Unfortunately the error
111 * message is not always very clear to read, but you can see the reason better
112 * in the place where the PB_STATIC_ASSERT macro was called.
114 #ifndef PB_NO_STATIC_ASSERT
115 #ifndef PB_STATIC_ASSERT
116 #define PB_STATIC_ASSERT(COND,MSG) typedef char PB_STATIC_ASSERT_MSG(MSG, __LINE__, __COUNTER__)[(COND)?1:-1];
117 #define PB_STATIC_ASSERT_MSG(MSG, LINE, COUNTER) PB_STATIC_ASSERT_MSG_(MSG, LINE, COUNTER)
118 #define PB_STATIC_ASSERT_MSG_(MSG, LINE, COUNTER) pb_static_assertion_##MSG##LINE##COUNTER
121 #define PB_STATIC_ASSERT(COND,MSG)
124 /* Number of required fields to keep track of. */
125 #ifndef PB_MAX_REQUIRED_FIELDS
126 #define PB_MAX_REQUIRED_FIELDS 64
129 #if PB_MAX_REQUIRED_FIELDS < 64
130 #error You should not lower PB_MAX_REQUIRED_FIELDS from the default value (64).
133 /* List of possible field types. These are used in the autogenerated code.
134 * Least-significant 4 bits tell the scalar type
135 * Most-significant 4 bits specify repeated/required/packed etc.
138 typedef uint8_t pb_type_t;
140 /**** Field data types ****/
143 #define PB_LTYPE_VARINT 0x00 /* int32, int64, enum, bool */
144 #define PB_LTYPE_UVARINT 0x01 /* uint32, uint64 */
145 #define PB_LTYPE_SVARINT 0x02 /* sint32, sint64 */
146 #define PB_LTYPE_FIXED32 0x03 /* fixed32, sfixed32, float */
147 #define PB_LTYPE_FIXED64 0x04 /* fixed64, sfixed64, double */
149 /* Marker for last packable field type. */
150 #define PB_LTYPE_LAST_PACKABLE 0x04
152 /* Byte array with pre-allocated buffer.
153 * data_size is the length of the allocated PB_BYTES_ARRAY structure. */
154 #define PB_LTYPE_BYTES 0x05
156 /* String with pre-allocated buffer.
157 * data_size is the maximum length. */
158 #define PB_LTYPE_STRING 0x06
161 * submsg_fields is pointer to field descriptions */
162 #define PB_LTYPE_SUBMESSAGE 0x07
164 /* Extension pseudo-field
165 * The field contains a pointer to pb_extension_t */
166 #define PB_LTYPE_EXTENSION 0x08
168 /* Number of declared LTYPES */
169 #define PB_LTYPES_COUNT 9
170 #define PB_LTYPE_MASK 0x0F
172 /**** Field repetition rules ****/
174 #define PB_HTYPE_REQUIRED 0x00
175 #define PB_HTYPE_OPTIONAL 0x10
176 #define PB_HTYPE_REPEATED 0x20
177 #define PB_HTYPE_MASK 0x30
179 /**** Field allocation types ****/
181 #define PB_ATYPE_STATIC 0x00
182 #define PB_ATYPE_POINTER 0x80
183 #define PB_ATYPE_CALLBACK 0x40
184 #define PB_ATYPE_MASK 0xC0
186 #define PB_ATYPE(x) ((x) & PB_ATYPE_MASK)
187 #define PB_HTYPE(x) ((x) & PB_HTYPE_MASK)
188 #define PB_LTYPE(x) ((x) & PB_LTYPE_MASK)
190 /* Data type used for storing sizes of struct fields
193 #if defined(PB_FIELD_32BIT)
194 #define PB_SIZE_MAX ((uint32_t)-1)
195 typedef uint32_t pb_size_t;
196 typedef int32_t pb_ssize_t;
197 #elif defined(PB_FIELD_16BIT)
198 #define PB_SIZE_MAX ((uint16_t)-1)
199 typedef uint16_t pb_size_t;
200 typedef int16_t pb_ssize_t;
202 #define PB_SIZE_MAX ((uint8_t)-1)
203 typedef uint8_t pb_size_t;
204 typedef int8_t pb_ssize_t;
207 /* This structure is used in auto-generated constants
208 * to specify struct fields.
209 * You can change field sizes if you need structures
210 * larger than 256 bytes or field tags larger than 256.
211 * The compiler should complain if your .proto has such
212 * structures. Fix that by defining PB_FIELD_16BIT or
215 PB_PACKED_STRUCT_START
216 typedef struct _pb_field_t pb_field_t;
220 pb_size_t data_offset; /* Offset of field data, relative to previous field. */
221 pb_ssize_t size_offset; /* Offset of array size or has-boolean, relative to data */
222 pb_size_t data_size; /* Data size in bytes for a single item */
223 pb_size_t array_size; /* Maximum number of entries in array */
225 /* Field definitions for submessage
226 * OR default value for all other non-array, non-callback types
227 * If null, then field will zeroed. */
232 /* Make sure that the standard integer types are of the expected sizes.
233 * All kinds of things may break otherwise.. atleast all fixed* types.
235 * If you get errors here, it probably means that your stdint.h is not
236 * correct for your platform.
238 PB_STATIC_ASSERT(sizeof(int8_t) == 1, INT8_T_WRONG_SIZE)
239 PB_STATIC_ASSERT(sizeof(uint8_t) == 1, UINT8_T_WRONG_SIZE)
240 PB_STATIC_ASSERT(sizeof(int16_t) == 2, INT16_T_WRONG_SIZE)
241 PB_STATIC_ASSERT(sizeof(uint16_t) == 2, UINT16_T_WRONG_SIZE)
242 PB_STATIC_ASSERT(sizeof(int32_t) == 4, INT32_T_WRONG_SIZE)
243 PB_STATIC_ASSERT(sizeof(uint32_t) == 4, UINT32_T_WRONG_SIZE)
244 PB_STATIC_ASSERT(sizeof(int64_t) == 8, INT64_T_WRONG_SIZE)
245 PB_STATIC_ASSERT(sizeof(uint64_t) == 8, UINT64_T_WRONG_SIZE)
247 /* This structure is used for 'bytes' arrays.
248 * It has the number of bytes in the beginning, and after that an array.
249 * Note that actual structs used will have a different length of bytes array.
251 #define PB_BYTES_ARRAY_T(n) struct { pb_size_t size; uint8_t bytes[n]; }
252 #define PB_BYTES_ARRAY_T_ALLOCSIZE(n) ((size_t)n + offsetof(pb_bytes_array_t, bytes))
254 struct _pb_bytes_array_t {
258 typedef struct _pb_bytes_array_t pb_bytes_array_t;
260 /* This structure is used for giving the callback function.
261 * It is stored in the message structure and filled in by the method that
264 * The decoding callback will be given a limited-length stream
265 * If the wire type was string, the length is the length of the string.
266 * If the wire type was a varint/fixed32/fixed64, the length is the length
267 * of the actual value.
268 * The function may be called multiple times (especially for repeated types,
269 * but also otherwise if the message happens to contain the field multiple
272 * The encoding callback will receive the actual output stream.
273 * It should write all the data in one call, including the field tag and
274 * wire type. It can write multiple fields.
276 * The callback can be null if you want to skip a field.
278 typedef struct _pb_istream_t pb_istream_t;
279 typedef struct _pb_ostream_t pb_ostream_t;
280 typedef struct _pb_callback_t pb_callback_t;
281 struct _pb_callback_t {
282 #ifdef PB_OLD_CALLBACK_STYLE
283 /* Deprecated since nanopb-0.2.1 */
285 bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void *arg);
286 bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, const void *arg);
289 /* New function signature, which allows modifying arg contents in callback. */
291 bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void **arg);
292 bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, void * const *arg);
296 /* Free arg for use by callback */
300 /* Wire types. Library user needs these only in encoder callbacks. */
308 /* Structure for defining the handling of unknown/extension fields.
309 * Usually the pb_extension_type_t structure is automatically generated,
310 * while the pb_extension_t structure is created by the user. However,
311 * if you want to catch all unknown fields, you can also create a custom
312 * pb_extension_type_t with your own callback.
314 typedef struct _pb_extension_type_t pb_extension_type_t;
315 typedef struct _pb_extension_t pb_extension_t;
316 struct _pb_extension_type_t {
317 /* Called for each unknown field in the message.
318 * If you handle the field, read off all of its data and return true.
319 * If you do not handle the field, do not read anything and return true.
320 * If you run into an error, return false.
321 * Set to NULL for default handler.
323 bool (*decode)(pb_istream_t *stream, pb_extension_t *extension,
324 uint32_t tag, pb_wire_type_t wire_type);
326 /* Called once after all regular fields have been encoded.
327 * If you have something to write, do so and return true.
328 * If you do not have anything to write, just return true.
329 * If you run into an error, return false.
330 * Set to NULL for default handler.
332 bool (*encode)(pb_ostream_t *stream, const pb_extension_t *extension);
334 /* Free field for use by the callback. */
338 struct _pb_extension_t {
339 /* Type describing the extension field. Usually you'll initialize
340 * this to a pointer to the automatically generated structure. */
341 const pb_extension_type_t *type;
343 /* Destination for the decoded data. This must match the datatype
344 * of the extension field. */
347 /* Pointer to the next extension handler, or NULL.
348 * If this extension does not match a field, the next handler is
349 * automatically called. */
350 pb_extension_t *next;
352 /* The decoder sets this to true if the extension was found.
353 * Ignored for encoding. */
357 /* Memory allocation functions to use. You can define pb_realloc and
358 * pb_free to custom functions if you want. */
359 #ifdef PB_ENABLE_MALLOC
361 # define pb_realloc(ptr, size) realloc(ptr, size)
364 # define pb_free(ptr) free(ptr)
368 /* These macros are used to declare pb_field_t's in the constant array. */
369 /* Size of a structure member, in bytes. */
370 #define pb_membersize(st, m) (sizeof ((st*)0)->m)
371 /* Number of entries in an array. */
372 #define pb_arraysize(st, m) (pb_membersize(st, m) / pb_membersize(st, m[0]))
373 /* Delta from start of one member to the start of another member. */
374 #define pb_delta(st, m1, m2) ((int)offsetof(st, m1) - (int)offsetof(st, m2))
375 /* Marks the end of the field list */
376 #define PB_LAST_FIELD {0,(pb_type_t) 0,0,0,0,0,0}
378 /* Macros for filling in the data_offset field */
379 /* data_offset for first field in a message */
380 #define PB_DATAOFFSET_FIRST(st, m1, m2) (offsetof(st, m1))
381 /* data_offset for subsequent fields */
382 #define PB_DATAOFFSET_OTHER(st, m1, m2) (offsetof(st, m1) - offsetof(st, m2) - pb_membersize(st, m2))
383 /* Choose first/other based on m1 == m2 (deprecated, remains for backwards compatibility) */
384 #define PB_DATAOFFSET_CHOOSE(st, m1, m2) (int)(offsetof(st, m1) == offsetof(st, m2) \
385 ? PB_DATAOFFSET_FIRST(st, m1, m2) \
386 : PB_DATAOFFSET_OTHER(st, m1, m2))
388 /* Required fields are the simplest. They just have delta (padding) from
389 * previous field end, and the size of the field. Pointer is used for
390 * submessages and default values.
392 #define PB_REQUIRED_STATIC(tag, st, m, fd, ltype, ptr) \
393 {tag, PB_ATYPE_STATIC | PB_HTYPE_REQUIRED | ltype, \
394 fd, 0, pb_membersize(st, m), 0, ptr}
396 /* Optional fields add the delta to the has_ variable. */
397 #define PB_OPTIONAL_STATIC(tag, st, m, fd, ltype, ptr) \
398 {tag, PB_ATYPE_STATIC | PB_HTYPE_OPTIONAL | ltype, \
400 pb_delta(st, has_ ## m, m), \
401 pb_membersize(st, m), 0, ptr}
403 /* Repeated fields have a _count field and also the maximum number of entries. */
404 #define PB_REPEATED_STATIC(tag, st, m, fd, ltype, ptr) \
405 {tag, PB_ATYPE_STATIC | PB_HTYPE_REPEATED | ltype, \
407 pb_delta(st, m ## _count, m), \
408 pb_membersize(st, m[0]), \
409 pb_arraysize(st, m), ptr}
411 /* Allocated fields carry the size of the actual data, not the pointer */
412 #define PB_REQUIRED_POINTER(tag, st, m, fd, ltype, ptr) \
413 {tag, PB_ATYPE_POINTER | PB_HTYPE_REQUIRED | ltype, \
414 fd, 0, pb_membersize(st, m[0]), 0, ptr}
416 /* Optional fields don't need a has_ variable, as information would be redundant */
417 #define PB_OPTIONAL_POINTER(tag, st, m, fd, ltype, ptr) \
418 {tag, PB_ATYPE_POINTER | PB_HTYPE_OPTIONAL | ltype, \
419 fd, 0, pb_membersize(st, m[0]), 0, ptr}
421 /* Repeated fields have a _count field and a pointer to array of pointers */
422 #define PB_REPEATED_POINTER(tag, st, m, fd, ltype, ptr) \
423 {tag, PB_ATYPE_POINTER | PB_HTYPE_REPEATED | ltype, \
424 fd, pb_delta(st, m ## _count, m), \
425 pb_membersize(st, m[0]), 0, ptr}
427 /* Callbacks are much like required fields except with special datatype. */
428 #define PB_REQUIRED_CALLBACK(tag, st, m, fd, ltype, ptr) \
429 {tag, PB_ATYPE_CALLBACK | PB_HTYPE_REQUIRED | ltype, \
430 fd, 0, pb_membersize(st, m), 0, ptr}
432 #define PB_OPTIONAL_CALLBACK(tag, st, m, fd, ltype, ptr) \
433 {tag, PB_ATYPE_CALLBACK | PB_HTYPE_OPTIONAL | ltype, \
434 fd, 0, pb_membersize(st, m), 0, ptr}
436 #define PB_REPEATED_CALLBACK(tag, st, m, fd, ltype, ptr) \
437 {tag, PB_ATYPE_CALLBACK | PB_HTYPE_REPEATED | ltype, \
438 fd, 0, pb_membersize(st, m), 0, ptr}
440 /* Optional extensions don't have the has_ field, as that would be redundant. */
441 #define PB_OPTEXT_STATIC(tag, st, m, fd, ltype, ptr) \
442 {tag, PB_ATYPE_STATIC | PB_HTYPE_OPTIONAL | ltype, \
445 pb_membersize(st, m), 0, ptr}
447 #define PB_OPTEXT_CALLBACK(tag, st, m, fd, ltype, ptr) \
448 {tag, PB_ATYPE_CALLBACK | PB_HTYPE_OPTIONAL | ltype, \
449 0, 0, pb_membersize(st, m), 0, ptr}
451 /* The mapping from protobuf types to LTYPEs is done using these macros. */
452 #define PB_LTYPE_MAP_BOOL PB_LTYPE_VARINT
453 #define PB_LTYPE_MAP_BYTES PB_LTYPE_BYTES
454 #define PB_LTYPE_MAP_DOUBLE PB_LTYPE_FIXED64
455 #define PB_LTYPE_MAP_ENUM PB_LTYPE_VARINT
456 #define PB_LTYPE_MAP_FIXED32 PB_LTYPE_FIXED32
457 #define PB_LTYPE_MAP_FIXED64 PB_LTYPE_FIXED64
458 #define PB_LTYPE_MAP_FLOAT PB_LTYPE_FIXED32
459 #define PB_LTYPE_MAP_INT32 PB_LTYPE_VARINT
460 #define PB_LTYPE_MAP_INT64 PB_LTYPE_VARINT
461 #define PB_LTYPE_MAP_MESSAGE PB_LTYPE_SUBMESSAGE
462 #define PB_LTYPE_MAP_SFIXED32 PB_LTYPE_FIXED32
463 #define PB_LTYPE_MAP_SFIXED64 PB_LTYPE_FIXED64
464 #define PB_LTYPE_MAP_SINT32 PB_LTYPE_SVARINT
465 #define PB_LTYPE_MAP_SINT64 PB_LTYPE_SVARINT
466 #define PB_LTYPE_MAP_STRING PB_LTYPE_STRING
467 #define PB_LTYPE_MAP_UINT32 PB_LTYPE_UVARINT
468 #define PB_LTYPE_MAP_UINT64 PB_LTYPE_UVARINT
469 #define PB_LTYPE_MAP_EXTENSION PB_LTYPE_EXTENSION
471 /* This is the actual macro used in field descriptions.
472 * It takes these arguments:
474 * - Field type: BOOL, BYTES, DOUBLE, ENUM, FIXED32, FIXED64,
475 * FLOAT, INT32, INT64, MESSAGE, SFIXED32, SFIXED64
476 * SINT32, SINT64, STRING, UINT32, UINT64 or EXTENSION
477 * - Field rules: REQUIRED, OPTIONAL or REPEATED
478 * - Allocation: STATIC or CALLBACK
481 * - Previous field name (or field name again for first field)
482 * - Pointer to default value or submsg fields.
485 #define PB_FIELD(tag, type, rules, allocation, message, field, prevfield, ptr) \
486 PB_ ## rules ## _ ## allocation(tag, message, field, \
487 PB_DATAOFFSET_CHOOSE(message, field, prevfield), \
488 PB_LTYPE_MAP_ ## type, ptr)
490 /* This is a new version of the macro used by nanopb generator from
491 * version 0.2.3 onwards. It avoids the use of a ternary expression in
492 * the initialization, which confused some compilers.
494 * - Placement: FIRST or OTHER, depending on if this is the first field in structure.
497 #define PB_FIELD2(tag, type, rules, allocation, placement, message, field, prevfield, ptr) \
498 PB_ ## rules ## _ ## allocation(tag, message, field, \
499 PB_DATAOFFSET_ ## placement(message, field, prevfield), \
500 PB_LTYPE_MAP_ ## type, ptr)
503 /* These macros are used for giving out error messages.
504 * They are mostly a debugging aid; the main error information
505 * is the true/false return value from functions.
506 * Some code space can be saved by disabling the error
507 * messages if not used.
510 #define PB_RETURN_ERROR(stream,msg) \
515 #define PB_GET_ERROR(stream) "(errmsg disabled)"
517 #define PB_RETURN_ERROR(stream,msg) \
519 if ((stream)->errmsg == NULL) \
520 (stream)->errmsg = (msg); \
523 #define PB_GET_ERROR(stream) ((stream)->errmsg ? (stream)->errmsg : "(none)")