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