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