WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
pb.h
Go to the documentation of this file.
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.
3 *
4 * Modified for WPILib Use
5 */
6
7#ifndef PB_H_INCLUDED
8#define PB_H_INCLUDED
9
10/*****************************************************************
11 * Nanopb compilation time options. You can change these here by *
12 * uncommenting the lines, or on the compiler command line. *
13 *****************************************************************/
14
15/* Enable support for dynamically allocated fields */
16/* #define PB_ENABLE_MALLOC 1 */
17
18/* Define this if your CPU / compiler combination does not support
19 * unaligned memory access to packed structures. Note that packed
20 * structures are only used when requested in .proto options. */
21/* #define PB_NO_PACKED_STRUCTS 1 */
22
23/* Increase the number of required fields that are tracked.
24 * A compiler warning will tell if you need this. */
25/* #define PB_MAX_REQUIRED_FIELDS 256 */
26
27/* Add support for tag numbers > 65536 and fields larger than 65536 bytes. */
28/* #define PB_FIELD_32BIT 1 */
29
30/* Disable support for error messages in order to save some code space. */
31/* #define PB_NO_ERRMSG 1 */
32
33/* Disable support for custom streams (support only memory buffers). */
34/* #define PB_BUFFER_ONLY 1 */
35
36/* Disable support for 64-bit datatypes, for compilers without int64_t
37 or to save some code space. */
38/* #define PB_WITHOUT_64BIT 1 */
39
40/* Don't encode scalar arrays as packed. This is only to be used when
41 * the decoder on the receiving side cannot process packed scalar arrays.
42 * Such example is older protobuf.js. */
43/* #define PB_ENCODE_ARRAYS_UNPACKED 1 */
44
45/* Enable conversion of doubles to floats for platforms that do not
46 * support 64-bit doubles. Most commonly AVR. */
47/* #define PB_CONVERT_DOUBLE_FLOAT 1 */
48
49/* Check whether incoming strings are valid UTF-8 sequences. Slows down
50 * the string processing slightly and slightly increases code size. */
51/* #define PB_VALIDATE_UTF8 1 */
52
53/* This can be defined if the platform is little-endian and has 8-bit bytes.
54 * Normally it is automatically detected based on __BYTE_ORDER__ macro. */
55/* #define PB_LITTLE_ENDIAN_8BIT 1 */
56
57/* Configure static assert mechanism. Instead of changing these, set your
58 * compiler to C11 standard mode if possible. */
59/* #define PB_C99_STATIC_ASSERT 1 */
60/* #define PB_NO_STATIC_ASSERT 1 */
61
62/******************************************************************
63 * You usually don't need to change anything below this line. *
64 * Feel free to look around and use the defined macros, though. *
65 ******************************************************************/
66
67
68/* Version of the nanopb library. Just in case you want to check it in
69 * your own program. */
70#define NANOPB_VERSION "nanopb-0.4.9"
71
72/* Include all the system headers needed by nanopb. You will need the
73 * definitions of the following:
74 * - strlen, memcpy, memset functions
75 * - [u]int_least8_t, uint_fast8_t, [u]int_least16_t, [u]int32_t, [u]int64_t
76 * - size_t
77 * - bool
78 *
79 * If you don't have the standard header files, you can instead provide
80 * a custom header that defines or includes all this. In that case,
81 * define PB_SYSTEM_HEADER to the path of this file.
82 */
83#ifdef PB_SYSTEM_HEADER
84#include PB_SYSTEM_HEADER
85#else
86#include <stdint.h>
87#include <stddef.h>
88#include <stdbool.h>
89#include <string.h>
90#include <limits.h>
91
92#include <span>
93#include <string_view>
94
95#ifdef PB_ENABLE_MALLOC
96#include <stdlib.h>
97#endif
98#endif
99
100/* Macro for defining packed structures (compiler dependent).
101 * This just reduces memory requirements, but is not required.
102 */
103#if defined(PB_NO_PACKED_STRUCTS)
104 /* Disable struct packing */
105# define PB_PACKED_STRUCT_START
106# define PB_PACKED_STRUCT_END
107# define pb_packed
108#elif defined(__GNUC__) || defined(__clang__)
109 /* For GCC and clang */
110# define PB_PACKED_STRUCT_START
111# define PB_PACKED_STRUCT_END
112# define pb_packed __attribute__((packed))
113#elif defined(__ICCARM__) || defined(__CC_ARM)
114 /* For IAR ARM and Keil MDK-ARM compilers */
115# define PB_PACKED_STRUCT_START _Pragma("pack(push, 1)")
116# define PB_PACKED_STRUCT_END _Pragma("pack(pop)")
117# define pb_packed
118#elif defined(_MSC_VER) && (_MSC_VER >= 1500)
119 /* For Microsoft Visual C++ */
120# define PB_PACKED_STRUCT_START __pragma(pack(push, 1))
121# define PB_PACKED_STRUCT_END __pragma(pack(pop))
122# define pb_packed
123#else
124 /* Unknown compiler */
125# define PB_PACKED_STRUCT_START
126# define PB_PACKED_STRUCT_END
127# define pb_packed
128#endif
129
130/* Detect endianness */
131#ifndef PB_LITTLE_ENDIAN_8BIT
132#if ((defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN) || \
133 (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || \
134 defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || \
135 defined(__THUMBEL__) || defined(__AARCH64EL__) || defined(_MIPSEL) || \
136 defined(_M_IX86) || defined(_M_X64) || defined(_M_ARM)) \
137 && CHAR_BIT == 8
138#define PB_LITTLE_ENDIAN_8BIT 1
139#endif
140#endif
141
142/* Handly macro for suppressing unreferenced-parameter compiler warnings. */
143#ifndef PB_UNUSED
144#define PB_UNUSED(x) (void)(x)
145#endif
146
147/* Harvard-architecture processors may need special attributes for storing
148 * field information in program memory. */
149#ifndef PB_PROGMEM
150#ifdef __AVR__
151#include <avr/pgmspace.h>
152#define PB_PROGMEM PROGMEM
153#define PB_PROGMEM_READU32(x) pgm_read_dword(&x)
154#else
155#define PB_PROGMEM
156#define PB_PROGMEM_READU32(x) (x)
157#endif
158#endif
159
160/* Compile-time assertion, used for checking compatible compilation options.
161 * If this does not work properly on your compiler, use
162 * #define PB_NO_STATIC_ASSERT to disable it.
163 *
164 * But before doing that, check carefully the error message / place where it
165 * comes from to see if the error has a real cause. Unfortunately the error
166 * message is not always very clear to read, but you can see the reason better
167 * in the place where the PB_STATIC_ASSERT macro was called.
168 */
169#ifndef PB_NO_STATIC_ASSERT
170# ifndef PB_STATIC_ASSERT
171# if defined(__ICCARM__)
172 /* IAR has static_assert keyword but no _Static_assert */
173# define PB_STATIC_ASSERT(COND,MSG) static_assert(COND,#MSG);
174# elif defined(_MSC_VER) && (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112)
175 /* MSVC in C89 mode supports static_assert() keyword anyway */
176# define PB_STATIC_ASSERT(COND,MSG) static_assert(COND,#MSG);
177# elif defined(PB_C99_STATIC_ASSERT)
178 /* Classic negative-size-array static assert mechanism */
179# define PB_STATIC_ASSERT(COND,MSG) typedef char PB_STATIC_ASSERT_MSG(MSG, __LINE__, __COUNTER__)[(COND)?1:-1];
180# define PB_STATIC_ASSERT_MSG(MSG, LINE, COUNTER) PB_STATIC_ASSERT_MSG_(MSG, LINE, COUNTER)
181# define PB_STATIC_ASSERT_MSG_(MSG, LINE, COUNTER) pb_static_assertion_##MSG##_##LINE##_##COUNTER
182# elif defined(__cplusplus)
183 /* C++11 standard static_assert mechanism */
184# define PB_STATIC_ASSERT(COND,MSG) static_assert(COND,#MSG);
185# else
186 /* C11 standard _Static_assert mechanism */
187# define PB_STATIC_ASSERT(COND,MSG) _Static_assert(COND,#MSG);
188# endif
189# endif
190#else
191 /* Static asserts disabled by PB_NO_STATIC_ASSERT */
192# define PB_STATIC_ASSERT(COND,MSG)
193#endif
194
195/* Test that PB_STATIC_ASSERT works
196 * If you get errors here, you may need to do one of these:
197 * - Enable C11 standard support in your compiler
198 * - Define PB_C99_STATIC_ASSERT to enable C99 standard support
199 * - Define PB_NO_STATIC_ASSERT to disable static asserts altogether
200 */
201PB_STATIC_ASSERT(1, STATIC_ASSERT_IS_NOT_WORKING)
202
203/* Number of required fields to keep track of. */
204#ifndef PB_MAX_REQUIRED_FIELDS
205#define PB_MAX_REQUIRED_FIELDS 64
206#endif
207
208#if PB_MAX_REQUIRED_FIELDS < 64
209#error You should not lower PB_MAX_REQUIRED_FIELDS from the default value (64).
210#endif
211
212#ifdef PB_WITHOUT_64BIT
213#ifdef PB_CONVERT_DOUBLE_FLOAT
214/* Cannot use doubles without 64-bit types */
215#undef PB_CONVERT_DOUBLE_FLOAT
216#endif
217#endif
218
219/* Data type for storing encoded data and other byte streams.
220 * This typedef exists to support platforms where uint8_t does not exist.
221 * You can regard it as equivalent on uint8_t on other platforms.
222 */
223#if defined(PB_BYTE_T_OVERRIDE)
224typedef PB_BYTE_T_OVERRIDE pb_byte_t;
225#elif defined(UINT8_MAX)
226typedef uint8_t pb_byte_t;
227#else
228typedef uint_least8_t pb_byte_t;
229#endif
230
231/* List of possible field types. These are used in the autogenerated code.
232 * Least-significant 4 bits tell the scalar type
233 * Most-significant 4 bits specify repeated/required/packed etc.
234 */
236
237/**** Field data types ****/
238
239/* Numeric types */
240#define PB_LTYPE_BOOL 0x00U /* bool */
241#define PB_LTYPE_VARINT 0x01U /* int32, int64, enum, bool */
242#define PB_LTYPE_UVARINT 0x02U /* uint32, uint64 */
243#define PB_LTYPE_SVARINT 0x03U /* sint32, sint64 */
244#define PB_LTYPE_FIXED32 0x04U /* fixed32, sfixed32, float */
245#define PB_LTYPE_FIXED64 0x05U /* fixed64, sfixed64, double */
246
247/* Marker for last packable field type. */
248#define PB_LTYPE_LAST_PACKABLE 0x05U
249
250/* Byte array with pre-allocated buffer.
251 * data_size is the length of the allocated PB_BYTES_ARRAY structure. */
252#define PB_LTYPE_BYTES 0x06U
253
254/* String with pre-allocated buffer.
255 * data_size is the maximum length. */
256#define PB_LTYPE_STRING 0x07U
257
258/* Submessage
259 * submsg_fields is pointer to field descriptions */
260#define PB_LTYPE_SUBMESSAGE 0x08U
261
262/* Submessage with pre-decoding callback
263 * The pre-decoding callback is stored as pb_callback_t right before pSize.
264 * submsg_fields is pointer to field descriptions */
265#define PB_LTYPE_SUBMSG_W_CB 0x09U
266
267/* Extension pseudo-field
268 * The field contains a pointer to pb_extension_t */
269#define PB_LTYPE_EXTENSION 0x0AU
270
271/* Byte array with inline, pre-allocated byffer.
272 * data_size is the length of the inline, allocated buffer.
273 * This differs from PB_LTYPE_BYTES by defining the element as
274 * pb_byte_t[data_size] rather than pb_bytes_array_t. */
275#define PB_LTYPE_FIXED_LENGTH_BYTES 0x0BU
276
277/* Number of declared LTYPES */
278#define PB_LTYPES_COUNT 0x0CU
279#define PB_LTYPE_MASK 0x0FU
280
281/**** Field repetition rules ****/
282
283#define PB_HTYPE_REQUIRED 0x00U
284#define PB_HTYPE_OPTIONAL 0x10U
285#define PB_HTYPE_SINGULAR 0x10U
286#define PB_HTYPE_REPEATED 0x20U
287#define PB_HTYPE_FIXARRAY 0x20U
288#define PB_HTYPE_ONEOF 0x30U
289#define PB_HTYPE_MASK 0x30U
290
291/**** Field allocation types ****/
292
293#define PB_ATYPE_STATIC 0x00U
294#define PB_ATYPE_POINTER 0x80U
295#define PB_ATYPE_CALLBACK 0x40U
296#define PB_ATYPE_MASK 0xC0U
297
298#define PB_ATYPE(x) ((x) & PB_ATYPE_MASK)
299#define PB_HTYPE(x) ((x) & PB_HTYPE_MASK)
300#define PB_LTYPE(x) ((x) & PB_LTYPE_MASK)
301#define PB_LTYPE_IS_SUBMSG(x) (PB_LTYPE(x) == PB_LTYPE_SUBMESSAGE || \
302 PB_LTYPE(x) == PB_LTYPE_SUBMSG_W_CB)
303
304/* Data type used for storing sizes of struct fields
305 * and array counts.
306 */
307#if defined(PB_FIELD_32BIT)
308 typedef uint32_t pb_size_t;
309 typedef int32_t pb_ssize_t;
310#else
311 typedef uint_least16_t pb_size_t;
312 typedef int_least16_t pb_ssize_t;
313#endif
314#define PB_SIZE_MAX ((pb_size_t)-1)
315
316/* Forward declaration of struct types */
320
323 std::string_view file_name;
324 std::span<const uint8_t> file_descriptor;
325};
326
327/* This structure is used in auto-generated constants
328 * to specify struct fields.
329 */
345
346/* Iterator for message descriptor */
348 const pb_msgdesc_t *descriptor; /* Pointer to message descriptor constant */
349 void *message; /* Pointer to start of the structure */
350
351 pb_size_t index; /* Index of the field */
352 pb_size_t field_info_index; /* Index to descriptor->field_info array */
353 pb_size_t required_field_index; /* Index that counts only the required fields */
354 pb_size_t submessage_index; /* Index that counts only submessages */
355
356 pb_size_t tag; /* Tag of current field */
357 pb_size_t data_size; /* sizeof() of a single item */
358 pb_size_t array_size; /* Number of array entries */
359 pb_type_t type; /* Type of current field */
360
361 void *pField; /* Pointer to current field in struct */
362 void *pData; /* Pointer to current data contents. Different than pField for arrays and pointers. */
363 void *pSize; /* Pointer to count/has field */
364
365 const pb_msgdesc_t *submsg_desc; /* For submessage fields, pointer to field descriptor for the submessage. */
366};
367
368/* For compatibility with legacy code */
370
371/* Make sure that the standard integer types are of the expected sizes.
372 * Otherwise fixed32/fixed64 fields can break.
373 *
374 * If you get errors here, it probably means that your stdint.h is not
375 * correct for your platform.
376 */
377#ifndef PB_WITHOUT_64BIT
378PB_STATIC_ASSERT(sizeof(int64_t) == 2 * sizeof(int32_t), INT64_T_WRONG_SIZE)
379PB_STATIC_ASSERT(sizeof(uint64_t) == 2 * sizeof(uint32_t), UINT64_T_WRONG_SIZE)
380#endif
381
382/* This structure is used for 'bytes' arrays.
383 * It has the number of bytes in the beginning, and after that an array.
384 * Note that actual structs used will have a different length of bytes array.
385 */
386#define PB_BYTES_ARRAY_T(n) struct { pb_size_t size; pb_byte_t bytes[n]; }
387#define PB_BYTES_ARRAY_T_ALLOCSIZE(n) ((size_t)n + offsetof(pb_bytes_array_t, bytes))
388
394
395/* This structure is used for giving the callback function.
396 * It is stored in the message structure and filled in by the method that
397 * calls pb_decode.
398 *
399 * The decoding callback will be given a limited-length stream
400 * If the wire type was string, the length is the length of the string.
401 * If the wire type was a varint/fixed32/fixed64, the length is the length
402 * of the actual value.
403 * The function may be called multiple times (especially for repeated types,
404 * but also otherwise if the message happens to contain the field multiple
405 * times.)
406 *
407 * The encoding callback will receive the actual output stream.
408 * It should write all the data in one call, including the field tag and
409 * wire type. It can write multiple fields.
410 *
411 * The callback can be null if you want to skip a field.
412 */
415 /* Callback functions receive a pointer to the arg field.
416 * You can access the value of the field as *arg, and modify it if needed.
417 */
418 union {
419 bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void **arg);
420 bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, void * const *arg);
422
423 /* Free arg for use by callback */
424 void *arg;
425};
426
427extern bool pb_default_field_callback(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_t *field);
428
429/* Wire types. Library user needs these only in encoder callbacks. */
430typedef enum {
435 PB_WT_PACKED = 255 /* PB_WT_PACKED is internal marker for packed arrays. */
437
438/* Structure for defining the handling of unknown/extension fields.
439 * Usually the pb_extension_type_t structure is automatically generated,
440 * while the pb_extension_t structure is created by the user. However,
441 * if you want to catch all unknown fields, you can also create a custom
442 * pb_extension_type_t with your own callback.
443 */
447 /* Called for each unknown field in the message.
448 * If you handle the field, read off all of its data and return true.
449 * If you do not handle the field, do not read anything and return true.
450 * If you run into an error, return false.
451 * Set to NULL for default handler.
452 */
453 bool (*decode)(pb_istream_t *stream, pb_extension_t *extension,
454 uint32_t tag, pb_wire_type_t wire_type);
455
456 /* Called once after all regular fields have been encoded.
457 * If you have something to write, do so and return true.
458 * If you do not have anything to write, just return true.
459 * If you run into an error, return false.
460 * Set to NULL for default handler.
461 */
462 bool (*encode)(pb_ostream_t *stream, const pb_extension_t *extension);
463
464 /* Free field for use by the callback. */
465 const void *arg;
466};
467
469 /* Type describing the extension field. Usually you'll initialize
470 * this to a pointer to the automatically generated structure. */
472
473 /* Destination for the decoded data. This must match the datatype
474 * of the extension field. */
475 void *dest;
476
477 /* Pointer to the next extension handler, or NULL.
478 * If this extension does not match a field, the next handler is
479 * automatically called. */
481
482 /* The decoder sets this to true if the extension was found.
483 * Ignored for encoding. */
484 bool found;
485};
486
487#define pb_extension_init_zero {NULL,NULL,NULL,false}
488
489/* Memory allocation functions to use. You can define pb_realloc and
490 * pb_free to custom functions if you want. */
491#ifdef PB_ENABLE_MALLOC
492# ifndef pb_realloc
493# define pb_realloc(ptr, size) realloc(ptr, size)
494# endif
495# ifndef pb_free
496# define pb_free(ptr) free(ptr)
497# endif
498#endif
499
500/* This is used to inform about need to regenerate .pb.h/.pb.c files. */
501#define PB_PROTO_HEADER_VERSION 40
502
503/* These macros are used to declare pb_field_t's in the constant array. */
504/* Size of a structure member, in bytes. */
505#define pb_membersize(st, m) (sizeof ((st*)0)->m)
506/* Number of entries in an array. */
507#define pb_arraysize(st, m) (pb_membersize(st, m) / pb_membersize(st, m[0]))
508/* Delta from start of one member to the start of another member. */
509#define pb_delta(st, m1, m2) ((int)offsetof(st, m1) - (int)offsetof(st, m2))
510
511/* Force expansion of macro value */
512#define PB_EXPAND(x) x
513
514/* Binding of a message field set into a specific structure */
515#define PB_BIND(msgname, structname, width) \
516 static const uint32_t structname ## _field_info[] PB_PROGMEM = \
517 { \
518 msgname ## _FIELDLIST(PB_GEN_FIELD_INFO_ ## width, structname) \
519 0 \
520 }; \
521 static const pb_msgdesc_t* const structname ## _submsg_info[] = \
522 { \
523 msgname ## _FIELDLIST(PB_GEN_SUBMSG_INFO, structname) \
524 NULL \
525 }; \
526 static const pb_msgdesc_t structname ## _msg = \
527 { \
528 structname ## _field_info, \
529 structname ## _submsg_info, \
530 msgname ## _DEFAULT, \
531 msgname ## _CALLBACK, \
532 0 msgname ## _FIELDLIST(PB_GEN_FIELD_COUNT, structname), \
533 0 msgname ## _FIELDLIST(PB_GEN_REQ_FIELD_COUNT, structname), \
534 0 msgname ## _FIELDLIST(PB_GEN_LARGEST_TAG, structname), \
535 structname::file_descriptor(), \
536 structname::msg_name(), \
537 }; \
538 const pb_msgdesc_t* structname::msg_descriptor(void) noexcept { return &(structname ## _msg); } \
539 msgname ## _FIELDLIST(PB_GEN_FIELD_INFO_ASSERT_ ## width, structname)
540
541#define PB_GEN_FIELD_COUNT(structname, atype, htype, ltype, fieldname, tag) +1
542#define PB_GEN_REQ_FIELD_COUNT(structname, atype, htype, ltype, fieldname, tag) \
543 + (PB_HTYPE_ ## htype == PB_HTYPE_REQUIRED)
544#define PB_GEN_LARGEST_TAG(structname, atype, htype, ltype, fieldname, tag) \
545 * 0 + tag
546
547/* X-macro for generating the entries in struct_field_info[] array. */
548#define PB_GEN_FIELD_INFO_1(structname, atype, htype, ltype, fieldname, tag) \
549 PB_FIELDINFO_1(tag, PB_ATYPE_ ## atype | PB_HTYPE_ ## htype | PB_LTYPE_MAP_ ## ltype, \
550 PB_DATA_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
551 PB_DATA_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
552 PB_SIZE_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
553 PB_ARRAY_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname))
554
555#define PB_GEN_FIELD_INFO_2(structname, atype, htype, ltype, fieldname, tag) \
556 PB_FIELDINFO_2(tag, PB_ATYPE_ ## atype | PB_HTYPE_ ## htype | PB_LTYPE_MAP_ ## ltype, \
557 PB_DATA_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
558 PB_DATA_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
559 PB_SIZE_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
560 PB_ARRAY_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname))
561
562#define PB_GEN_FIELD_INFO_4(structname, atype, htype, ltype, fieldname, tag) \
563 PB_FIELDINFO_4(tag, PB_ATYPE_ ## atype | PB_HTYPE_ ## htype | PB_LTYPE_MAP_ ## ltype, \
564 PB_DATA_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
565 PB_DATA_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
566 PB_SIZE_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
567 PB_ARRAY_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname))
568
569#define PB_GEN_FIELD_INFO_8(structname, atype, htype, ltype, fieldname, tag) \
570 PB_FIELDINFO_8(tag, PB_ATYPE_ ## atype | PB_HTYPE_ ## htype | PB_LTYPE_MAP_ ## ltype, \
571 PB_DATA_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
572 PB_DATA_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
573 PB_SIZE_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
574 PB_ARRAY_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname))
575
576#define PB_GEN_FIELD_INFO_AUTO(structname, atype, htype, ltype, fieldname, tag) \
577 PB_FIELDINFO_AUTO2(PB_FIELDINFO_WIDTH_AUTO(_PB_ATYPE_ ## atype, _PB_HTYPE_ ## htype, _PB_LTYPE_ ## ltype), \
578 tag, PB_ATYPE_ ## atype | PB_HTYPE_ ## htype | PB_LTYPE_MAP_ ## ltype, \
579 PB_DATA_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
580 PB_DATA_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
581 PB_SIZE_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
582 PB_ARRAY_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname))
583
584#define PB_FIELDINFO_AUTO2(width, tag, type, data_offset, data_size, size_offset, array_size) \
585 PB_FIELDINFO_AUTO3(width, tag, type, data_offset, data_size, size_offset, array_size)
586
587#define PB_FIELDINFO_AUTO3(width, tag, type, data_offset, data_size, size_offset, array_size) \
588 PB_FIELDINFO_ ## width(tag, type, data_offset, data_size, size_offset, array_size)
589
590/* X-macro for generating asserts that entries fit in struct_field_info[] array.
591 * The structure of macros here must match the structure above in PB_GEN_FIELD_INFO_x(),
592 * but it is not easily reused because of how macro substitutions work. */
593#define PB_GEN_FIELD_INFO_ASSERT_1(structname, atype, htype, ltype, fieldname, tag) \
594 PB_FIELDINFO_ASSERT_1(tag, PB_ATYPE_ ## atype | PB_HTYPE_ ## htype | PB_LTYPE_MAP_ ## ltype, \
595 PB_DATA_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
596 PB_DATA_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
597 PB_SIZE_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
598 PB_ARRAY_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname))
599
600#define PB_GEN_FIELD_INFO_ASSERT_2(structname, atype, htype, ltype, fieldname, tag) \
601 PB_FIELDINFO_ASSERT_2(tag, PB_ATYPE_ ## atype | PB_HTYPE_ ## htype | PB_LTYPE_MAP_ ## ltype, \
602 PB_DATA_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
603 PB_DATA_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
604 PB_SIZE_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
605 PB_ARRAY_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname))
606
607#define PB_GEN_FIELD_INFO_ASSERT_4(structname, atype, htype, ltype, fieldname, tag) \
608 PB_FIELDINFO_ASSERT_4(tag, PB_ATYPE_ ## atype | PB_HTYPE_ ## htype | PB_LTYPE_MAP_ ## ltype, \
609 PB_DATA_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
610 PB_DATA_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
611 PB_SIZE_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
612 PB_ARRAY_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname))
613
614#define PB_GEN_FIELD_INFO_ASSERT_8(structname, atype, htype, ltype, fieldname, tag) \
615 PB_FIELDINFO_ASSERT_8(tag, PB_ATYPE_ ## atype | PB_HTYPE_ ## htype | PB_LTYPE_MAP_ ## ltype, \
616 PB_DATA_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
617 PB_DATA_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
618 PB_SIZE_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
619 PB_ARRAY_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname))
620
621#define PB_GEN_FIELD_INFO_ASSERT_AUTO(structname, atype, htype, ltype, fieldname, tag) \
622 PB_FIELDINFO_ASSERT_AUTO2(PB_FIELDINFO_WIDTH_AUTO(_PB_ATYPE_ ## atype, _PB_HTYPE_ ## htype, _PB_LTYPE_ ## ltype), \
623 tag, PB_ATYPE_ ## atype | PB_HTYPE_ ## htype | PB_LTYPE_MAP_ ## ltype, \
624 PB_DATA_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
625 PB_DATA_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
626 PB_SIZE_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
627 PB_ARRAY_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname))
628
629#define PB_FIELDINFO_ASSERT_AUTO2(width, tag, type, data_offset, data_size, size_offset, array_size) \
630 PB_FIELDINFO_ASSERT_AUTO3(width, tag, type, data_offset, data_size, size_offset, array_size)
631
632#define PB_FIELDINFO_ASSERT_AUTO3(width, tag, type, data_offset, data_size, size_offset, array_size) \
633 PB_FIELDINFO_ASSERT_ ## width(tag, type, data_offset, data_size, size_offset, array_size)
634
635#define PB_DATA_OFFSET_STATIC(htype, structname, fieldname) PB_DO ## htype(structname, fieldname)
636#define PB_DATA_OFFSET_POINTER(htype, structname, fieldname) PB_DO ## htype(structname, fieldname)
637#define PB_DATA_OFFSET_CALLBACK(htype, structname, fieldname) PB_DO ## htype(structname, fieldname)
638#define PB_DO_PB_HTYPE_REQUIRED(structname, fieldname) offsetof(structname, fieldname)
639#define PB_DO_PB_HTYPE_SINGULAR(structname, fieldname) offsetof(structname, fieldname)
640#define PB_DO_PB_HTYPE_ONEOF(structname, fieldname) offsetof(structname, PB_ONEOF_NAME(FULL, fieldname))
641#define PB_DO_PB_HTYPE_OPTIONAL(structname, fieldname) offsetof(structname, fieldname)
642#define PB_DO_PB_HTYPE_REPEATED(structname, fieldname) offsetof(structname, fieldname)
643#define PB_DO_PB_HTYPE_FIXARRAY(structname, fieldname) offsetof(structname, fieldname)
644
645#define PB_SIZE_OFFSET_STATIC(htype, structname, fieldname) PB_SO ## htype(structname, fieldname)
646#define PB_SIZE_OFFSET_POINTER(htype, structname, fieldname) PB_SO_PTR ## htype(structname, fieldname)
647#define PB_SIZE_OFFSET_CALLBACK(htype, structname, fieldname) PB_SO_CB ## htype(structname, fieldname)
648#define PB_SO_PB_HTYPE_REQUIRED(structname, fieldname) 0
649#define PB_SO_PB_HTYPE_SINGULAR(structname, fieldname) 0
650#define PB_SO_PB_HTYPE_ONEOF(structname, fieldname) PB_SO_PB_HTYPE_ONEOF2(structname, PB_ONEOF_NAME(FULL, fieldname), PB_ONEOF_NAME(UNION, fieldname))
651#define PB_SO_PB_HTYPE_ONEOF2(structname, fullname, unionname) PB_SO_PB_HTYPE_ONEOF3(structname, fullname, unionname)
652#define PB_SO_PB_HTYPE_ONEOF3(structname, fullname, unionname) pb_delta(structname, fullname, which_ ## unionname)
653#define PB_SO_PB_HTYPE_OPTIONAL(structname, fieldname) pb_delta(structname, fieldname, has_ ## fieldname)
654#define PB_SO_PB_HTYPE_REPEATED(structname, fieldname) pb_delta(structname, fieldname, fieldname ## _count)
655#define PB_SO_PB_HTYPE_FIXARRAY(structname, fieldname) 0
656#define PB_SO_PTR_PB_HTYPE_REQUIRED(structname, fieldname) 0
657#define PB_SO_PTR_PB_HTYPE_SINGULAR(structname, fieldname) 0
658#define PB_SO_PTR_PB_HTYPE_ONEOF(structname, fieldname) PB_SO_PB_HTYPE_ONEOF(structname, fieldname)
659#define PB_SO_PTR_PB_HTYPE_OPTIONAL(structname, fieldname) 0
660#define PB_SO_PTR_PB_HTYPE_REPEATED(structname, fieldname) PB_SO_PB_HTYPE_REPEATED(structname, fieldname)
661#define PB_SO_PTR_PB_HTYPE_FIXARRAY(structname, fieldname) 0
662#define PB_SO_CB_PB_HTYPE_REQUIRED(structname, fieldname) 0
663#define PB_SO_CB_PB_HTYPE_SINGULAR(structname, fieldname) 0
664#define PB_SO_CB_PB_HTYPE_ONEOF(structname, fieldname) PB_SO_PB_HTYPE_ONEOF(structname, fieldname)
665#define PB_SO_CB_PB_HTYPE_OPTIONAL(structname, fieldname) 0
666#define PB_SO_CB_PB_HTYPE_REPEATED(structname, fieldname) 0
667#define PB_SO_CB_PB_HTYPE_FIXARRAY(structname, fieldname) 0
668
669#define PB_ARRAY_SIZE_STATIC(htype, structname, fieldname) PB_AS ## htype(structname, fieldname)
670#define PB_ARRAY_SIZE_POINTER(htype, structname, fieldname) PB_AS_PTR ## htype(structname, fieldname)
671#define PB_ARRAY_SIZE_CALLBACK(htype, structname, fieldname) 1
672#define PB_AS_PB_HTYPE_REQUIRED(structname, fieldname) 1
673#define PB_AS_PB_HTYPE_SINGULAR(structname, fieldname) 1
674#define PB_AS_PB_HTYPE_OPTIONAL(structname, fieldname) 1
675#define PB_AS_PB_HTYPE_ONEOF(structname, fieldname) 1
676#define PB_AS_PB_HTYPE_REPEATED(structname, fieldname) pb_arraysize(structname, fieldname)
677#define PB_AS_PB_HTYPE_FIXARRAY(structname, fieldname) pb_arraysize(structname, fieldname)
678#define PB_AS_PTR_PB_HTYPE_REQUIRED(structname, fieldname) 1
679#define PB_AS_PTR_PB_HTYPE_SINGULAR(structname, fieldname) 1
680#define PB_AS_PTR_PB_HTYPE_OPTIONAL(structname, fieldname) 1
681#define PB_AS_PTR_PB_HTYPE_ONEOF(structname, fieldname) 1
682#define PB_AS_PTR_PB_HTYPE_REPEATED(structname, fieldname) 1
683#define PB_AS_PTR_PB_HTYPE_FIXARRAY(structname, fieldname) pb_arraysize(structname, fieldname[0])
684
685#define PB_DATA_SIZE_STATIC(htype, structname, fieldname) PB_DS ## htype(structname, fieldname)
686#define PB_DATA_SIZE_POINTER(htype, structname, fieldname) PB_DS_PTR ## htype(structname, fieldname)
687#define PB_DATA_SIZE_CALLBACK(htype, structname, fieldname) PB_DS_CB ## htype(structname, fieldname)
688#define PB_DS_PB_HTYPE_REQUIRED(structname, fieldname) pb_membersize(structname, fieldname)
689#define PB_DS_PB_HTYPE_SINGULAR(structname, fieldname) pb_membersize(structname, fieldname)
690#define PB_DS_PB_HTYPE_OPTIONAL(structname, fieldname) pb_membersize(structname, fieldname)
691#define PB_DS_PB_HTYPE_ONEOF(structname, fieldname) pb_membersize(structname, PB_ONEOF_NAME(FULL, fieldname))
692#define PB_DS_PB_HTYPE_REPEATED(structname, fieldname) pb_membersize(structname, fieldname[0])
693#define PB_DS_PB_HTYPE_FIXARRAY(structname, fieldname) pb_membersize(structname, fieldname[0])
694#define PB_DS_PTR_PB_HTYPE_REQUIRED(structname, fieldname) pb_membersize(structname, fieldname[0])
695#define PB_DS_PTR_PB_HTYPE_SINGULAR(structname, fieldname) pb_membersize(structname, fieldname[0])
696#define PB_DS_PTR_PB_HTYPE_OPTIONAL(structname, fieldname) pb_membersize(structname, fieldname[0])
697#define PB_DS_PTR_PB_HTYPE_ONEOF(structname, fieldname) pb_membersize(structname, PB_ONEOF_NAME(FULL, fieldname)[0])
698#define PB_DS_PTR_PB_HTYPE_REPEATED(structname, fieldname) pb_membersize(structname, fieldname[0])
699#define PB_DS_PTR_PB_HTYPE_FIXARRAY(structname, fieldname) pb_membersize(structname, fieldname[0][0])
700#define PB_DS_CB_PB_HTYPE_REQUIRED(structname, fieldname) pb_membersize(structname, fieldname)
701#define PB_DS_CB_PB_HTYPE_SINGULAR(structname, fieldname) pb_membersize(structname, fieldname)
702#define PB_DS_CB_PB_HTYPE_OPTIONAL(structname, fieldname) pb_membersize(structname, fieldname)
703#define PB_DS_CB_PB_HTYPE_ONEOF(structname, fieldname) pb_membersize(structname, PB_ONEOF_NAME(FULL, fieldname))
704#define PB_DS_CB_PB_HTYPE_REPEATED(structname, fieldname) pb_membersize(structname, fieldname)
705#define PB_DS_CB_PB_HTYPE_FIXARRAY(structname, fieldname) pb_membersize(structname, fieldname)
706
707#define PB_ONEOF_NAME(type, tuple) PB_EXPAND(PB_ONEOF_NAME_ ## type tuple)
708#define PB_ONEOF_NAME_UNION(unionname,membername,fullname) unionname
709#define PB_ONEOF_NAME_MEMBER(unionname,membername,fullname) membername
710#define PB_ONEOF_NAME_FULL(unionname,membername,fullname) fullname
711
712#define PB_GEN_SUBMSG_INFO(structname, atype, htype, ltype, fieldname, tag) \
713 PB_SUBMSG_INFO_ ## htype(_PB_LTYPE_ ## ltype, structname, fieldname)
714
715#define PB_SUBMSG_INFO_REQUIRED(ltype, structname, fieldname) PB_SI ## ltype(structname ## _ ## fieldname ## _MSGTYPE)
716#define PB_SUBMSG_INFO_SINGULAR(ltype, structname, fieldname) PB_SI ## ltype(structname ## _ ## fieldname ## _MSGTYPE)
717#define PB_SUBMSG_INFO_OPTIONAL(ltype, structname, fieldname) PB_SI ## ltype(structname ## _ ## fieldname ## _MSGTYPE)
718#define PB_SUBMSG_INFO_ONEOF(ltype, structname, fieldname) PB_SUBMSG_INFO_ONEOF2(ltype, structname, PB_ONEOF_NAME(UNION, fieldname), PB_ONEOF_NAME(MEMBER, fieldname))
719#define PB_SUBMSG_INFO_ONEOF2(ltype, structname, unionname, membername) PB_SUBMSG_INFO_ONEOF3(ltype, structname, unionname, membername)
720#define PB_SUBMSG_INFO_ONEOF3(ltype, structname, unionname, membername) PB_SI ## ltype(structname ## _ ## unionname ## _ ## membername ## _MSGTYPE)
721#define PB_SUBMSG_INFO_REPEATED(ltype, structname, fieldname) PB_SI ## ltype(structname ## _ ## fieldname ## _MSGTYPE)
722#define PB_SUBMSG_INFO_FIXARRAY(ltype, structname, fieldname) PB_SI ## ltype(structname ## _ ## fieldname ## _MSGTYPE)
723#define PB_SI_PB_LTYPE_BOOL(t)
724#define PB_SI_PB_LTYPE_BYTES(t)
725#define PB_SI_PB_LTYPE_DOUBLE(t)
726#define PB_SI_PB_LTYPE_ENUM(t)
727#define PB_SI_PB_LTYPE_UENUM(t)
728#define PB_SI_PB_LTYPE_FIXED32(t)
729#define PB_SI_PB_LTYPE_FIXED64(t)
730#define PB_SI_PB_LTYPE_FLOAT(t)
731#define PB_SI_PB_LTYPE_INT32(t)
732#define PB_SI_PB_LTYPE_INT64(t)
733#define PB_SI_PB_LTYPE_MESSAGE(t) PB_SUBMSG_DESCRIPTOR(t)
734#define PB_SI_PB_LTYPE_MSG_W_CB(t) PB_SUBMSG_DESCRIPTOR(t)
735#define PB_SI_PB_LTYPE_SFIXED32(t)
736#define PB_SI_PB_LTYPE_SFIXED64(t)
737#define PB_SI_PB_LTYPE_SINT32(t)
738#define PB_SI_PB_LTYPE_SINT64(t)
739#define PB_SI_PB_LTYPE_STRING(t)
740#define PB_SI_PB_LTYPE_UINT32(t)
741#define PB_SI_PB_LTYPE_UINT64(t)
742#define PB_SI_PB_LTYPE_EXTENSION(t)
743#define PB_SI_PB_LTYPE_FIXED_LENGTH_BYTES(t)
744#define PB_SUBMSG_DESCRIPTOR(t) (t::msg_descriptor()),
745
746/* The field descriptors use a variable width format, with width of either
747 * 1, 2, 4 or 8 of 32-bit words. The two lowest bytes of the first byte always
748 * encode the descriptor size, 6 lowest bits of field tag number, and 8 bits
749 * of the field type.
750 *
751 * Descriptor size is encoded as 0 = 1 word, 1 = 2 words, 2 = 4 words, 3 = 8 words.
752 *
753 * Formats, listed starting with the least significant bit of the first word.
754 * 1 word: [2-bit len] [6-bit tag] [8-bit type] [8-bit data_offset] [4-bit size_offset] [4-bit data_size]
755 *
756 * 2 words: [2-bit len] [6-bit tag] [8-bit type] [12-bit array_size] [4-bit size_offset]
757 * [16-bit data_offset] [12-bit data_size] [4-bit tag>>6]
758 *
759 * 4 words: [2-bit len] [6-bit tag] [8-bit type] [16-bit array_size]
760 * [8-bit size_offset] [24-bit tag>>6]
761 * [32-bit data_offset]
762 * [32-bit data_size]
763 *
764 * 8 words: [2-bit len] [6-bit tag] [8-bit type] [16-bit reserved]
765 * [8-bit size_offset] [24-bit tag>>6]
766 * [32-bit data_offset]
767 * [32-bit data_size]
768 * [32-bit array_size]
769 * [32-bit reserved]
770 * [32-bit reserved]
771 * [32-bit reserved]
772 */
773
774#define PB_FIELDINFO_1(tag, type, data_offset, data_size, size_offset, array_size) \
775 (0 | (((tag) << 2) & 0xFF) | ((type) << 8) | (((uint32_t)(data_offset) & 0xFF) << 16) | \
776 (((uint32_t)(size_offset) & 0x0F) << 24) | (((uint32_t)(data_size) & 0x0F) << 28)),
777
778#define PB_FIELDINFO_2(tag, type, data_offset, data_size, size_offset, array_size) \
779 (1 | (((tag) << 2) & 0xFF) | ((type) << 8) | (((uint32_t)(array_size) & 0xFFF) << 16) | (((uint32_t)(size_offset) & 0x0F) << 28)), \
780 (((uint32_t)(data_offset) & 0xFFFF) | (((uint32_t)(data_size) & 0xFFF) << 16) | (((uint32_t)(tag) & 0x3c0) << 22)),
781
782#define PB_FIELDINFO_4(tag, type, data_offset, data_size, size_offset, array_size) \
783 (2 | (((tag) << 2) & 0xFF) | ((type) << 8) | (((uint32_t)(array_size) & 0xFFFF) << 16)), \
784 ((uint32_t)(int_least8_t)(size_offset) | (((uint32_t)(tag) << 2) & 0xFFFFFF00)), \
785 (data_offset), (data_size),
786
787#define PB_FIELDINFO_8(tag, type, data_offset, data_size, size_offset, array_size) \
788 (3 | (((tag) << 2) & 0xFF) | ((type) << 8)), \
789 ((uint32_t)(int_least8_t)(size_offset) | (((uint32_t)(tag) << 2) & 0xFFFFFF00)), \
790 (data_offset), (data_size), (array_size), 0, 0, 0,
791
792/* These assertions verify that the field information fits in the allocated space.
793 * The generator tries to automatically determine the correct width that can fit all
794 * data associated with a message. These asserts will fail only if there has been a
795 * problem in the automatic logic - this may be worth reporting as a bug. As a workaround,
796 * you can increase the descriptor width by defining PB_FIELDINFO_WIDTH or by setting
797 * descriptorsize option in .options file.
798 */
799#define PB_FITS(value,bits) ((uint32_t)(value) < ((uint32_t)1<<bits))
800#define PB_FIELDINFO_ASSERT_1(tag, type, data_offset, data_size, size_offset, array_size) \
801 PB_STATIC_ASSERT(PB_FITS(tag,6) && PB_FITS(data_offset,8) && PB_FITS(size_offset,4) && PB_FITS(data_size,4) && PB_FITS(array_size,1), FIELDINFO_DOES_NOT_FIT_width1_field ## tag)
802
803#define PB_FIELDINFO_ASSERT_2(tag, type, data_offset, data_size, size_offset, array_size) \
804 PB_STATIC_ASSERT(PB_FITS(tag,10) && PB_FITS(data_offset,16) && PB_FITS(size_offset,4) && PB_FITS(data_size,12) && PB_FITS(array_size,12), FIELDINFO_DOES_NOT_FIT_width2_field ## tag)
805
806#ifndef PB_FIELD_32BIT
807/* Maximum field sizes are still 16-bit if pb_size_t is 16-bit */
808#define PB_FIELDINFO_ASSERT_4(tag, type, data_offset, data_size, size_offset, array_size) \
809 PB_STATIC_ASSERT(PB_FITS(tag,16) && PB_FITS(data_offset,16) && PB_FITS((int_least8_t)size_offset,8) && PB_FITS(data_size,16) && PB_FITS(array_size,16), FIELDINFO_DOES_NOT_FIT_width4_field ## tag)
810
811#define PB_FIELDINFO_ASSERT_8(tag, type, data_offset, data_size, size_offset, array_size) \
812 PB_STATIC_ASSERT(PB_FITS(tag,16) && PB_FITS(data_offset,16) && PB_FITS((int_least8_t)size_offset,8) && PB_FITS(data_size,16) && PB_FITS(array_size,16), FIELDINFO_DOES_NOT_FIT_width8_field ## tag)
813#else
814/* Up to 32-bit fields supported.
815 * Note that the checks are against 31 bits to avoid compiler warnings about shift wider than type in the test.
816 * I expect that there is no reasonable use for >2GB messages with nanopb anyway.
817 */
818#define PB_FIELDINFO_ASSERT_4(tag, type, data_offset, data_size, size_offset, array_size) \
819 PB_STATIC_ASSERT(PB_FITS(tag,30) && PB_FITS(data_offset,31) && PB_FITS(size_offset,8) && PB_FITS(data_size,31) && PB_FITS(array_size,16), FIELDINFO_DOES_NOT_FIT_width4_field ## tag)
820
821#define PB_FIELDINFO_ASSERT_8(tag, type, data_offset, data_size, size_offset, array_size) \
822 PB_STATIC_ASSERT(PB_FITS(tag,30) && PB_FITS(data_offset,31) && PB_FITS(size_offset,8) && PB_FITS(data_size,31) && PB_FITS(array_size,31), FIELDINFO_DOES_NOT_FIT_width8_field ## tag)
823#endif
824
825
826/* Automatic picking of FIELDINFO width:
827 * Uses width 1 when possible, otherwise resorts to width 2.
828 * This is used when PB_BIND() is called with "AUTO" as the argument.
829 * The generator will give explicit size argument when it knows that a message
830 * structure grows beyond 1-word format limits.
831 */
832#define PB_FIELDINFO_WIDTH_AUTO(atype, htype, ltype) PB_FI_WIDTH ## atype(htype, ltype)
833#define PB_FI_WIDTH_PB_ATYPE_STATIC(htype, ltype) PB_FI_WIDTH ## htype(ltype)
834#define PB_FI_WIDTH_PB_ATYPE_POINTER(htype, ltype) PB_FI_WIDTH ## htype(ltype)
835#define PB_FI_WIDTH_PB_ATYPE_CALLBACK(htype, ltype) 2
836#define PB_FI_WIDTH_PB_HTYPE_REQUIRED(ltype) PB_FI_WIDTH ## ltype
837#define PB_FI_WIDTH_PB_HTYPE_SINGULAR(ltype) PB_FI_WIDTH ## ltype
838#define PB_FI_WIDTH_PB_HTYPE_OPTIONAL(ltype) PB_FI_WIDTH ## ltype
839#define PB_FI_WIDTH_PB_HTYPE_ONEOF(ltype) PB_FI_WIDTH ## ltype
840#define PB_FI_WIDTH_PB_HTYPE_REPEATED(ltype) 2
841#define PB_FI_WIDTH_PB_HTYPE_FIXARRAY(ltype) 2
842#define PB_FI_WIDTH_PB_LTYPE_BOOL 1
843#define PB_FI_WIDTH_PB_LTYPE_BYTES 2
844#define PB_FI_WIDTH_PB_LTYPE_DOUBLE 1
845#define PB_FI_WIDTH_PB_LTYPE_ENUM 1
846#define PB_FI_WIDTH_PB_LTYPE_UENUM 1
847#define PB_FI_WIDTH_PB_LTYPE_FIXED32 1
848#define PB_FI_WIDTH_PB_LTYPE_FIXED64 1
849#define PB_FI_WIDTH_PB_LTYPE_FLOAT 1
850#define PB_FI_WIDTH_PB_LTYPE_INT32 1
851#define PB_FI_WIDTH_PB_LTYPE_INT64 1
852#define PB_FI_WIDTH_PB_LTYPE_MESSAGE 2
853#define PB_FI_WIDTH_PB_LTYPE_MSG_W_CB 2
854#define PB_FI_WIDTH_PB_LTYPE_SFIXED32 1
855#define PB_FI_WIDTH_PB_LTYPE_SFIXED64 1
856#define PB_FI_WIDTH_PB_LTYPE_SINT32 1
857#define PB_FI_WIDTH_PB_LTYPE_SINT64 1
858#define PB_FI_WIDTH_PB_LTYPE_STRING 2
859#define PB_FI_WIDTH_PB_LTYPE_UINT32 1
860#define PB_FI_WIDTH_PB_LTYPE_UINT64 1
861#define PB_FI_WIDTH_PB_LTYPE_EXTENSION 1
862#define PB_FI_WIDTH_PB_LTYPE_FIXED_LENGTH_BYTES 2
863
864/* The mapping from protobuf types to LTYPEs is done using these macros. */
865#define PB_LTYPE_MAP_BOOL PB_LTYPE_BOOL
866#define PB_LTYPE_MAP_BYTES PB_LTYPE_BYTES
867#define PB_LTYPE_MAP_DOUBLE PB_LTYPE_FIXED64
868#define PB_LTYPE_MAP_ENUM PB_LTYPE_VARINT
869#define PB_LTYPE_MAP_UENUM PB_LTYPE_UVARINT
870#define PB_LTYPE_MAP_FIXED32 PB_LTYPE_FIXED32
871#define PB_LTYPE_MAP_FIXED64 PB_LTYPE_FIXED64
872#define PB_LTYPE_MAP_FLOAT PB_LTYPE_FIXED32
873#define PB_LTYPE_MAP_INT32 PB_LTYPE_VARINT
874#define PB_LTYPE_MAP_INT64 PB_LTYPE_VARINT
875#define PB_LTYPE_MAP_MESSAGE PB_LTYPE_SUBMESSAGE
876#define PB_LTYPE_MAP_MSG_W_CB PB_LTYPE_SUBMSG_W_CB
877#define PB_LTYPE_MAP_SFIXED32 PB_LTYPE_FIXED32
878#define PB_LTYPE_MAP_SFIXED64 PB_LTYPE_FIXED64
879#define PB_LTYPE_MAP_SINT32 PB_LTYPE_SVARINT
880#define PB_LTYPE_MAP_SINT64 PB_LTYPE_SVARINT
881#define PB_LTYPE_MAP_STRING PB_LTYPE_STRING
882#define PB_LTYPE_MAP_UINT32 PB_LTYPE_UVARINT
883#define PB_LTYPE_MAP_UINT64 PB_LTYPE_UVARINT
884#define PB_LTYPE_MAP_EXTENSION PB_LTYPE_EXTENSION
885#define PB_LTYPE_MAP_FIXED_LENGTH_BYTES PB_LTYPE_FIXED_LENGTH_BYTES
886
887/* These macros are used for giving out error messages.
888 * They are mostly a debugging aid; the main error information
889 * is the true/false return value from functions.
890 * Some code space can be saved by disabling the error
891 * messages if not used.
892 *
893 * PB_SET_ERROR() sets the error message if none has been set yet.
894 * msg must be a constant string literal.
895 * PB_GET_ERROR() always returns a pointer to a string.
896 * PB_RETURN_ERROR() sets the error and returns false from current
897 * function.
898 */
899#ifdef PB_NO_ERRMSG
900#define PB_SET_ERROR(stream, msg) PB_UNUSED(stream)
901#define PB_GET_ERROR(stream) "(errmsg disabled)"
902#else
903#define PB_SET_ERROR(stream, msg) (stream->errmsg = (stream)->errmsg ? (stream)->errmsg : (msg))
904#define PB_GET_ERROR(stream) ((stream)->errmsg ? (stream)->errmsg : "(none)")
905#endif
906
907#define PB_RETURN_ERROR(stream, msg) return PB_SET_ERROR(stream, msg), false
908
909#ifdef __cplusplus
910#if __cplusplus >= 201103L
911#define PB_CONSTEXPR constexpr
912#else // __cplusplus >= 201103L
913#define PB_CONSTEXPR
914#endif // __cplusplus >= 201103L
915
916#if __cplusplus >= 201703L
917#define PB_INLINE_CONSTEXPR inline constexpr
918#else // __cplusplus >= 201703L
919#define PB_INLINE_CONSTEXPR PB_CONSTEXPR
920#endif // __cplusplus >= 201703L
921
922extern "C++"
923{
924namespace nanopb {
925// Each type will be partially specialized by the generator.
926template <typename GenMessageT> struct MessageDescriptor;
927} // namespace nanopb
928}
929#endif /* __cplusplus */
930
931#endif
Definition pb.h:924
#define PB_STATIC_ASSERT(COND, MSG)
Definition pb.h:184
uint_least8_t pb_byte_t
Definition pb.h:228
int_least16_t pb_ssize_t
Definition pb.h:312
uint_least16_t pb_size_t
Definition pb.h:311
bool pb_default_field_callback(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_t *field)
pb_field_iter_t pb_field_t
Definition pb.h:369
pb_wire_type_t
Definition pb.h:430
@ PB_WT_VARINT
Definition pb.h:431
@ PB_WT_32BIT
Definition pb.h:434
@ PB_WT_PACKED
Definition pb.h:435
@ PB_WT_STRING
Definition pb.h:433
@ PB_WT_64BIT
Definition pb.h:432
pb_byte_t pb_type_t
Definition pb.h:235
Definition format.h:3858
Definition pb.h:926
Definition pb.h:389
pb_size_t size
Definition pb.h:390
Definition pb.h:414
bool(* decode)(pb_istream_t *stream, const pb_field_t *field, void **arg)
Definition pb.h:419
void * arg
Definition pb.h:424
union pb_callback_s::@20 funcs
bool(* encode)(pb_ostream_t *stream, const pb_field_t *field, void *const *arg)
Definition pb.h:420
Definition pb.h:468
bool found
Definition pb.h:484
void * dest
Definition pb.h:475
pb_extension_t * next
Definition pb.h:480
const pb_extension_type_t * type
Definition pb.h:471
Definition pb.h:446
const void * arg
Definition pb.h:465
bool(* decode)(pb_istream_t *stream, pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
Definition pb.h:453
bool(* encode)(pb_ostream_t *stream, const pb_extension_t *extension)
Definition pb.h:462
Definition pb.h:347
pb_size_t submessage_index
Definition pb.h:354
pb_type_t type
Definition pb.h:359
pb_size_t tag
Definition pb.h:356
void * pData
Definition pb.h:362
void * pSize
Definition pb.h:363
pb_size_t field_info_index
Definition pb.h:352
pb_size_t index
Definition pb.h:351
void * message
Definition pb.h:349
const pb_msgdesc_t * descriptor
Definition pb.h:348
pb_size_t array_size
Definition pb.h:358
pb_size_t data_size
Definition pb.h:357
void * pField
Definition pb.h:361
pb_size_t required_field_index
Definition pb.h:353
const pb_msgdesc_t * submsg_desc
Definition pb.h:365
Definition pb.h:322
std::span< const uint8_t > file_descriptor
Definition pb.h:324
std::string_view file_name
Definition pb.h:323
Definition pb_decode.h:27
Definition pb.h:331
const pb_byte_t * default_value
Definition pb.h:334
bool(* field_callback)(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_iter_t *field)
Definition pb.h:336
std::string_view proto_name
Definition pb.h:343
pb_size_t required_field_count
Definition pb.h:339
const uint32_t * field_info
Definition pb.h:332
pb_size_t field_count
Definition pb.h:338
pb_size_t largest_tag
Definition pb.h:340
const pb_msgdesc_t *const * submsg_info
Definition pb.h:333
pb_filedesc_t file_descriptor
Definition pb.h:342
Definition pb_encode.h:26