LCOV - code coverage report
Current view: top level - flamenco/nanopb - pb.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 34 37 91.9 %
Date: 2025-01-08 12:08:44 Functions: 0 0 -

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

Generated by: LCOV version 1.14