1/* Common parts of the nanopb library. Most of these are quite low-level
2 * stuff. For the high-level interface, see pb_encode.h and pb_decode.h.
3 */
4
5#ifndef PB_H_INCLUDED
6#define PB_H_INCLUDED
7
8/*****************************************************************
9 * Nanopb compilation time options. You can change these here by *
10 * uncommenting the lines, or on the compiler command line.      *
11 *****************************************************************/
12
13/* Enable support for dynamically allocated fields */
14/* #define PB_ENABLE_MALLOC 1 */
15
16/* Define this if your CPU / compiler combination does not support
17 * unaligned memory access to packed structures. */
18/* #define PB_NO_PACKED_STRUCTS 1 */
19
20/* Increase the number of required fields that are tracked.
21 * A compiler warning will tell if you need this. */
22/* #define PB_MAX_REQUIRED_FIELDS 256 */
23
24/* Add support for tag numbers > 65536 and fields larger than 65536 bytes. */
25/* #define PB_FIELD_32BIT 1 */
26
27/* Disable support for error messages in order to save some code space. */
28/* #define PB_NO_ERRMSG 1 */
29
30/* Disable support for custom streams (support only memory buffers). */
31/* #define PB_BUFFER_ONLY 1 */
32
33/* Switch back to the old-style callback function signature.
34 * This was the default until nanopb-0.2.1. */
35/* #define PB_OLD_CALLBACK_STYLE */
36
37/* Set the fieldinfo width for all messages using automatic width
38 * selection. Valid values are 2, 4 and 8. Usually even if you need
39 * to change the width manually for some reason, it is preferrable
40 * to do so through the descriptorsize option in .options file. */
41/* #define PB_FIELDINFO_WIDTH 4 */
42
43/******************************************************************
44 * You usually don't need to change anything below this line.     *
45 * Feel free to look around and use the defined macros, though.   *
46 ******************************************************************/
47
48
49/* Version of the nanopb library. Just in case you want to check it in
50 * your own program. */
51#define NANOPB_VERSION nanopb-0.4.0-dev
52
53/* Include all the system headers needed by nanopb. You will need the
54 * definitions of the following:
55 * - strlen, memcpy, memset functions
56 * - [u]int_least8_t, uint_fast8_t, [u]int_least16_t, [u]int32_t, [u]int64_t
57 * - size_t
58 * - bool
59 *
60 * If you don't have the standard header files, you can instead provide
61 * a custom header that defines or includes all this. In that case,
62 * define PB_SYSTEM_HEADER to the path of this file.
63 */
64#ifdef PB_SYSTEM_HEADER
65#include PB_SYSTEM_HEADER
66#else
67#include <stdint.h>
68#include <stddef.h>
69#include <stdbool.h>
70#include <string.h>
71#include <limits.h>
72
73#ifdef PB_ENABLE_MALLOC
74#include <stdlib.h>
75#endif
76#endif
77
78#ifdef __cplusplus
79extern "C" {
80#endif
81
82/* Macro for defining packed structures (compiler dependent).
83 * This just reduces memory requirements, but is not required.
84 */
85#if defined(PB_NO_PACKED_STRUCTS)
86    /* Disable struct packing */
87#   define PB_PACKED_STRUCT_START
88#   define PB_PACKED_STRUCT_END
89#   define pb_packed
90#elif defined(__GNUC__) || defined(__clang__)
91    /* For GCC and clang */
92#   define PB_PACKED_STRUCT_START
93#   define PB_PACKED_STRUCT_END
94#   define pb_packed __attribute__((packed))
95#elif defined(__ICCARM__) || defined(__CC_ARM)
96    /* For IAR ARM and Keil MDK-ARM compilers */
97#   define PB_PACKED_STRUCT_START _Pragma("pack(push, 1)")
98#   define PB_PACKED_STRUCT_END _Pragma("pack(pop)")
99#   define pb_packed
100#elif defined(_MSC_VER) && (_MSC_VER >= 1500)
101    /* For Microsoft Visual C++ */
102#   define PB_PACKED_STRUCT_START __pragma(pack(push, 1))
103#   define PB_PACKED_STRUCT_END __pragma(pack(pop))
104#   define pb_packed
105#else
106    /* Unknown compiler */
107#   define PB_PACKED_STRUCT_START
108#   define PB_PACKED_STRUCT_END
109#   define pb_packed
110#endif
111
112/* Handly macro for suppressing unreferenced-parameter compiler warnings. */
113#ifndef PB_UNUSED
114#define PB_UNUSED(x) (void)(x)
115#endif
116
117/* Compile-time assertion, used for checking compatible compilation options.
118 * If this does not work properly on your compiler, use
119 * #define PB_NO_STATIC_ASSERT to disable it.
120 *
121 * But before doing that, check carefully the error message / place where it
122 * comes from to see if the error has a real cause. Unfortunately the error
123 * message is not always very clear to read, but you can see the reason better
124 * in the place where the PB_STATIC_ASSERT macro was called.
125 */
126#ifndef PB_NO_STATIC_ASSERT
127#  ifndef PB_STATIC_ASSERT
128#    if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
129       /* C11 standard _Static_assert mechanism */
130#      define PB_STATIC_ASSERT(COND,MSG) _Static_assert(COND,#MSG);
131#    else
132       /* Classic negative-size-array static assert mechanism */
133#      define PB_STATIC_ASSERT(COND,MSG) typedef char PB_STATIC_ASSERT_MSG(MSG, __LINE__, __COUNTER__)[(COND)?1:-1];
134#      define PB_STATIC_ASSERT_MSG(MSG, LINE, COUNTER) PB_STATIC_ASSERT_MSG_(MSG, LINE, COUNTER)
135#      define PB_STATIC_ASSERT_MSG_(MSG, LINE, COUNTER) pb_static_assertion_##MSG##_##LINE##_##COUNTER
136#    endif
137#  endif
138#else
139   /* Static asserts disabled by PB_NO_STATIC_ASSERT */
140#  define PB_STATIC_ASSERT(COND,MSG)
141#endif
142
143/* Number of required fields to keep track of. */
144#ifndef PB_MAX_REQUIRED_FIELDS
145#define PB_MAX_REQUIRED_FIELDS 64
146#endif
147
148#if PB_MAX_REQUIRED_FIELDS < 64
149#error You should not lower PB_MAX_REQUIRED_FIELDS from the default value (64).
150#endif
151
152/* List of possible field types. These are used in the autogenerated code.
153 * Least-significant 4 bits tell the scalar type
154 * Most-significant 4 bits specify repeated/required/packed etc.
155 */
156
157typedef uint_least8_t pb_type_t;
158
159/**** Field data types ****/
160
161/* Numeric types */
162#define PB_LTYPE_VARINT  0x00 /* int32, int64, enum, bool */
163#define PB_LTYPE_UVARINT 0x01 /* uint32, uint64 */
164#define PB_LTYPE_SVARINT 0x02 /* sint32, sint64 */
165#define PB_LTYPE_FIXED32 0x03 /* fixed32, sfixed32, float */
166#define PB_LTYPE_FIXED64 0x04 /* fixed64, sfixed64, double */
167
168/* Marker for last packable field type. */
169#define PB_LTYPE_LAST_PACKABLE 0x04
170
171/* Byte array with pre-allocated buffer.
172 * data_size is the length of the allocated PB_BYTES_ARRAY structure. */
173#define PB_LTYPE_BYTES 0x05
174
175/* String with pre-allocated buffer.
176 * data_size is the maximum length. */
177#define PB_LTYPE_STRING 0x06
178
179/* Submessage
180 * submsg_fields is pointer to field descriptions */
181#define PB_LTYPE_SUBMESSAGE 0x07
182
183/* Extension pseudo-field
184 * The field contains a pointer to pb_extension_t */
185#define PB_LTYPE_EXTENSION 0x08
186
187/* Byte array with inline, pre-allocated byffer.
188 * data_size is the length of the inline, allocated buffer.
189 * This differs from PB_LTYPE_BYTES by defining the element as
190 * pb_byte_t[data_size] rather than pb_bytes_array_t. */
191#define PB_LTYPE_FIXED_LENGTH_BYTES 0x09
192
193/* Number of declared LTYPES */
194#define PB_LTYPES_COUNT 0x0A
195#define PB_LTYPE_MASK 0x0F
196
197/**** Field repetition rules ****/
198
199#define PB_HTYPE_REQUIRED 0x00
200#define PB_HTYPE_OPTIONAL 0x10
201#define PB_HTYPE_SINGULAR 0x10
202#define PB_HTYPE_REPEATED 0x20
203#define PB_HTYPE_FIXARRAY 0x20
204#define PB_HTYPE_ONEOF    0x30
205#define PB_HTYPE_MASK     0x30
206
207/**** Field allocation types ****/
208
209#define PB_ATYPE_STATIC   0x00
210#define PB_ATYPE_POINTER  0x80
211#define PB_ATYPE_CALLBACK 0x40
212#define PB_ATYPE_MASK     0xC0
213
214#define PB_ATYPE(x) ((x) & PB_ATYPE_MASK)
215#define PB_HTYPE(x) ((x) & PB_HTYPE_MASK)
216#define PB_LTYPE(x) ((x) & PB_LTYPE_MASK)
217
218/* Data type used for storing sizes of struct fields
219 * and array counts.
220 */
221#if defined(PB_FIELD_32BIT)
222    typedef uint32_t pb_size_t;
223    typedef int32_t pb_ssize_t;
224#else
225    typedef uint_least16_t pb_size_t;
226    typedef int_least16_t pb_ssize_t;
227#endif
228#define PB_SIZE_MAX ((pb_size_t)-1)
229
230/* Data type for storing encoded data and other byte streams.
231 * This typedef exists to support platforms where uint8_t does not exist.
232 * You can regard it as equivalent on uint8_t on other platforms.
233 */
234typedef uint_least8_t pb_byte_t;
235
236/* Forward declaration of struct types */
237typedef struct pb_istream_s pb_istream_t;
238typedef struct pb_ostream_s pb_ostream_t;
239typedef struct pb_field_iter_s pb_field_iter_t;
240
241/* This structure is used in auto-generated constants
242 * to specify struct fields.
243 */
244PB_PACKED_STRUCT_START
245typedef struct pb_msgdesc_s pb_msgdesc_t;
246struct pb_msgdesc_s {
247    pb_size_t field_count;
248    const uint32_t *field_info;
249    const pb_msgdesc_t **submsg_info;
250    const pb_byte_t *default_value;
251
252    bool (*field_callback)(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_iter_t *field);
253} pb_packed;
254PB_PACKED_STRUCT_END
255
256/* Iterator for message descriptor */
257struct pb_field_iter_s {
258    const pb_msgdesc_t *descriptor;  /* Pointer to message descriptor constant */
259    void *message;                   /* Pointer to start of the structure */
260
261    pb_size_t index;                 /* Index of the field */
262    pb_size_t field_info_index;      /* Index to descriptor->field_info array */
263    pb_size_t required_field_index;  /* Index that counts only the required fields */
264    pb_size_t submessage_index;      /* Index that counts only submessages */
265
266    pb_size_t tag;                   /* Tag of current field */
267    pb_size_t data_size;             /* sizeof() of a single item */
268    pb_size_t array_size;            /* Number of array entries */
269    pb_type_t type;                  /* Type of current field */
270
271    void *pField;                    /* Pointer to current field in struct */
272    void *pData;                     /* Pointer to current data contents. Different than pField for arrays and pointers. */
273    void *pSize;                     /* Pointer to count/has field */
274
275    const pb_msgdesc_t *submsg_desc; /* For submessage fields, pointer to field descriptor for the submessage. */
276};
277
278/* For compatibility with legacy code */
279typedef pb_field_iter_t pb_field_t;
280
281/* Make sure that the standard integer types are of the expected sizes.
282 * Otherwise fixed32/fixed64 fields can break.
283 *
284 * If you get errors here, it probably means that your stdint.h is not
285 * correct for your platform.
286 */
287#ifndef PB_WITHOUT_64BIT
288PB_STATIC_ASSERT(sizeof(int64_t) == 2 * sizeof(int32_t), INT64_T_WRONG_SIZE)
289PB_STATIC_ASSERT(sizeof(uint64_t) == 2 * sizeof(uint32_t), UINT64_T_WRONG_SIZE)
290#endif
291
292/* This structure is used for 'bytes' arrays.
293 * It has the number of bytes in the beginning, and after that an array.
294 * Note that actual structs used will have a different length of bytes array.
295 */
296#define PB_BYTES_ARRAY_T(n) struct { pb_size_t size; pb_byte_t bytes[n]; }
297#define PB_BYTES_ARRAY_T_ALLOCSIZE(n) ((size_t)n + offsetof(pb_bytes_array_t, bytes))
298
299struct pb_bytes_array_s {
300    pb_size_t size;
301    pb_byte_t bytes[1];
302};
303typedef struct pb_bytes_array_s pb_bytes_array_t;
304
305/* This structure is used for giving the callback function.
306 * It is stored in the message structure and filled in by the method that
307 * calls pb_decode.
308 *
309 * The decoding callback will be given a limited-length stream
310 * If the wire type was string, the length is the length of the string.
311 * If the wire type was a varint/fixed32/fixed64, the length is the length
312 * of the actual value.
313 * The function may be called multiple times (especially for repeated types,
314 * but also otherwise if the message happens to contain the field multiple
315 * times.)
316 *
317 * The encoding callback will receive the actual output stream.
318 * It should write all the data in one call, including the field tag and
319 * wire type. It can write multiple fields.
320 *
321 * The callback can be null if you want to skip a field.
322 */
323typedef struct pb_callback_s pb_callback_t;
324struct pb_callback_s {
325#ifdef PB_OLD_CALLBACK_STYLE
326    /* Deprecated since nanopb-0.2.1 */
327    union {
328        bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void *arg);
329        bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, const void *arg);
330    } funcs;
331#else
332    /* New function signature, which allows modifying arg contents in callback. */
333    union {
334        bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void **arg);
335        bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, void * const *arg);
336    } funcs;
337#endif
338
339    /* Free arg for use by callback */
340    void *arg;
341};
342
343extern bool pb_default_field_callback(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_t *field);
344
345/* Wire types. Library user needs these only in encoder callbacks. */
346typedef enum {
347    PB_WT_VARINT = 0,
348    PB_WT_64BIT  = 1,
349    PB_WT_STRING = 2,
350    PB_WT_32BIT  = 5
351} pb_wire_type_t;
352
353/* Structure for defining the handling of unknown/extension fields.
354 * Usually the pb_extension_type_t structure is automatically generated,
355 * while the pb_extension_t structure is created by the user. However,
356 * if you want to catch all unknown fields, you can also create a custom
357 * pb_extension_type_t with your own callback.
358 */
359typedef struct pb_extension_type_s pb_extension_type_t;
360typedef struct pb_extension_s pb_extension_t;
361struct pb_extension_type_s {
362    /* Called for each unknown field in the message.
363     * If you handle the field, read off all of its data and return true.
364     * If you do not handle the field, do not read anything and return true.
365     * If you run into an error, return false.
366     * Set to NULL for default handler.
367     */
368    bool (*decode)(pb_istream_t *stream, pb_extension_t *extension,
369                   uint32_t tag, pb_wire_type_t wire_type);
370
371    /* Called once after all regular fields have been encoded.
372     * If you have something to write, do so and return true.
373     * If you do not have anything to write, just return true.
374     * If you run into an error, return false.
375     * Set to NULL for default handler.
376     */
377    bool (*encode)(pb_ostream_t *stream, const pb_extension_t *extension);
378
379    /* Free field for use by the callback. */
380    const void *arg;
381};
382
383struct pb_extension_s {
384    /* Type describing the extension field. Usually you'll initialize
385     * this to a pointer to the automatically generated structure. */
386    const pb_extension_type_t *type;
387
388    /* Destination for the decoded data. This must match the datatype
389     * of the extension field. */
390    void *dest;
391
392    /* Pointer to the next extension handler, or NULL.
393     * If this extension does not match a field, the next handler is
394     * automatically called. */
395    pb_extension_t *next;
396
397    /* The decoder sets this to true if the extension was found.
398     * Ignored for encoding. */
399    bool found;
400};
401
402/* Memory allocation functions to use. You can define pb_realloc and
403 * pb_free to custom functions if you want. */
404#ifdef PB_ENABLE_MALLOC
405#   ifndef pb_realloc
406#       define pb_realloc(ptr, size) realloc(ptr, size)
407#   endif
408#   ifndef pb_free
409#       define pb_free(ptr) free(ptr)
410#   endif
411#endif
412
413/* This is used to inform about need to regenerate .pb.h/.pb.c files. */
414#define PB_PROTO_HEADER_VERSION 40
415
416/* These macros are used to declare pb_field_t's in the constant array. */
417/* Size of a structure member, in bytes. */
418#define pb_membersize(st, m) (sizeof ((st*)0)->m)
419/* Number of entries in an array. */
420#define pb_arraysize(st, m) (pb_membersize(st, m) / pb_membersize(st, m[0]))
421/* Delta from start of one member to the start of another member. */
422#define pb_delta(st, m1, m2) ((int)offsetof(st, m1) - (int)offsetof(st, m2))
423
424/* Force expansion of macro value */
425#define PB_EXPAND(x) x
426
427/* Binding of a message field set into a specific structure */
428#define PB_BIND(msgname, structname, width) \
429    const uint32_t structname ## _field_info[] = \
430    { \
431        msgname ## _FIELDLIST(PB_GEN_FIELD_INFO_ ## width, structname) \
432        0 \
433    }; \
434    const pb_msgdesc_t* structname ## _submsg_info[] = \
435    { \
436        msgname ## _FIELDLIST(PB_GEN_SUBMSG_INFO, structname) \
437        NULL \
438    }; \
439    const pb_msgdesc_t structname ## _msg = \
440    { \
441       0 msgname ## _FIELDLIST(PB_GEN_FIELD_COUNT, structname), \
442       structname ## _field_info, \
443       structname ## _submsg_info, \
444       msgname ## _DEFAULT, \
445       msgname ## _CALLBACK, \
446    }; \
447    msgname ## _FIELDLIST(PB_GEN_FIELD_INFO_ASSERT_ ## width, structname)
448
449#define PB_GEN_FIELD_COUNT(structname, atype, htype, ltype, fieldname, tag) +1
450
451#define PB_GEN_FIELD_INFO_1(structname, atype, htype, ltype, fieldname, tag) \
452    PB_GEN_FIELD_INFO(1, structname, atype, htype, ltype, fieldname, tag)
453
454#define PB_GEN_FIELD_INFO_2(structname, atype, htype, ltype, fieldname, tag) \
455    PB_GEN_FIELD_INFO(2, structname, atype, htype, ltype, fieldname, tag)
456
457#define PB_GEN_FIELD_INFO_4(structname, atype, htype, ltype, fieldname, tag) \
458    PB_GEN_FIELD_INFO(4, structname, atype, htype, ltype, fieldname, tag)
459
460#define PB_GEN_FIELD_INFO_8(structname, atype, htype, ltype, fieldname, tag) \
461    PB_GEN_FIELD_INFO(8, structname, atype, htype, ltype, fieldname, tag)
462
463#define PB_GEN_FIELD_INFO_AUTO(structname, atype, htype, ltype, fieldname, tag) \
464    PB_GEN_FIELD_INFO_AUTO2(PB_FIELDINFO_WIDTH_AUTO(atype, htype, ltype), structname, atype, htype, ltype, fieldname, tag)
465
466#define PB_GEN_FIELD_INFO_AUTO2(width, structname, atype, htype, ltype, fieldname, tag) \
467    PB_GEN_FIELD_INFO(width, structname, atype, htype, ltype, fieldname, tag)
468
469#define PB_GEN_FIELD_INFO(width, structname, atype, htype, ltype, fieldname, tag) \
470    PB_FIELDINFO_ ## width(tag, PB_ATYPE_ ## atype | PB_HTYPE_ ## htype | PB_LTYPE_MAP_ ## ltype, \
471                   PB_DATA_OFFSET_ ## atype(htype, structname, fieldname), \
472                   PB_DATA_SIZE_ ## atype(htype, structname, fieldname), \
473                   PB_SIZE_OFFSET_ ## atype(htype, structname, fieldname), \
474                   PB_ARRAY_SIZE_ ## atype(htype, structname, fieldname))
475
476#define PB_GEN_FIELD_INFO_ASSERT_1(structname, atype, htype, ltype, fieldname, tag) \
477    PB_GEN_FIELD_INFO_ASSERT(1, structname, atype, htype, ltype, fieldname, tag)
478
479#define PB_GEN_FIELD_INFO_ASSERT_2(structname, atype, htype, ltype, fieldname, tag) \
480    PB_GEN_FIELD_INFO_ASSERT(2, structname, atype, htype, ltype, fieldname, tag)
481
482#define PB_GEN_FIELD_INFO_ASSERT_4(structname, atype, htype, ltype, fieldname, tag) \
483    PB_GEN_FIELD_INFO_ASSERT(4, structname, atype, htype, ltype, fieldname, tag)
484
485#define PB_GEN_FIELD_INFO_ASSERT_8(structname, atype, htype, ltype, fieldname, tag) \
486    PB_GEN_FIELD_INFO_ASSERT(8, structname, atype, htype, ltype, fieldname, tag)
487
488#define PB_GEN_FIELD_INFO_ASSERT_AUTO(structname, atype, htype, ltype, fieldname, tag) \
489    PB_GEN_FIELD_INFO_ASSERT_AUTO2(PB_FIELDINFO_WIDTH_AUTO(atype, htype, ltype), structname, atype, htype, ltype, fieldname, tag)
490
491#define PB_GEN_FIELD_INFO_ASSERT_AUTO2(width, structname, atype, htype, ltype, fieldname, tag) \
492    PB_GEN_FIELD_INFO_ASSERT(width, structname, atype, htype, ltype, fieldname, tag)
493
494#define PB_GEN_FIELD_INFO_ASSERT(width, structname, atype, htype, ltype, fieldname, tag) \
495    PB_FIELDINFO_ASSERT_ ## width(tag, PB_ATYPE_ ## atype | PB_HTYPE_ ## htype | PB_LTYPE_MAP_ ## ltype, \
496                   PB_DATA_OFFSET_ ## atype(htype, structname, fieldname), \
497                   PB_DATA_SIZE_ ## atype(htype, structname, fieldname), \
498                   PB_SIZE_OFFSET_ ## atype(htype, structname, fieldname), \
499                   PB_ARRAY_SIZE_ ## atype(htype, structname, fieldname))
500
501#define PB_DATA_OFFSET_STATIC(htype, structname, fieldname) PB_DATA_OFFSET_ ## htype(structname, fieldname)
502#define PB_DATA_OFFSET_POINTER(htype, structname, fieldname) PB_DATA_OFFSET_ ## htype(structname, fieldname)
503#define PB_DATA_OFFSET_CALLBACK(htype, structname, fieldname) PB_DATA_OFFSET_ ## htype(structname, fieldname)
504#define PB_DATA_OFFSET_REQUIRED(structname, fieldname) offsetof(structname, fieldname)
505#define PB_DATA_OFFSET_SINGULAR(structname, fieldname) offsetof(structname, fieldname)
506#define PB_DATA_OFFSET_ONEOF(structname, fieldname) offsetof(structname, PB_ONEOF_NAME(FULL, fieldname))
507#define PB_DATA_OFFSET_OPTIONAL(structname, fieldname) offsetof(structname, fieldname)
508#define PB_DATA_OFFSET_REPEATED(structname, fieldname) offsetof(structname, fieldname)
509#define PB_DATA_OFFSET_FIXARRAY(structname, fieldname) offsetof(structname, fieldname)
510
511#define PB_SIZE_OFFSET_STATIC(htype, structname, fieldname) PB_SIZE_OFFSET_ ## htype(structname, fieldname)
512#define PB_SIZE_OFFSET_POINTER(htype, structname, fieldname) PB_SIZE_OFFSET_PTR_ ## htype(structname, fieldname)
513#define PB_SIZE_OFFSET_CALLBACK(htype, structname, fieldname) 0
514#define PB_SIZE_OFFSET_REQUIRED(structname, fieldname) 0
515#define PB_SIZE_OFFSET_SINGULAR(structname, fieldname) 0
516#define PB_SIZE_OFFSET_ONEOF(structname, fieldname) PB_SIZE_OFFSET_ONEOF2(structname, PB_ONEOF_NAME(FULL, fieldname), PB_ONEOF_NAME(UNION, fieldname))
517#define PB_SIZE_OFFSET_ONEOF2(structname, fullname, unionname) PB_SIZE_OFFSET_ONEOF3(structname, fullname, unionname)
518#define PB_SIZE_OFFSET_ONEOF3(structname, fullname, unionname) pb_delta(structname, fullname, which_ ## unionname)
519#define PB_SIZE_OFFSET_OPTIONAL(structname, fieldname) pb_delta(structname, fieldname, has_ ## fieldname)
520#define PB_SIZE_OFFSET_REPEATED(structname, fieldname) pb_delta(structname, fieldname, fieldname ## _count)
521#define PB_SIZE_OFFSET_FIXARRAY(structname, fieldname) 0
522#define PB_SIZE_OFFSET_PTR_REQUIRED(structname, fieldname) 0
523#define PB_SIZE_OFFSET_PTR_SINGULAR(structname, fieldname) 0
524#define PB_SIZE_OFFSET_PTR_ONEOF(structname, fieldname) PB_SIZE_OFFSET_ONEOF(structname, fieldname)
525#define PB_SIZE_OFFSET_PTR_OPTIONAL(structname, fieldname) 0
526#define PB_SIZE_OFFSET_PTR_REPEATED(structname, fieldname) PB_SIZE_OFFSET_REPEATED(structname, fieldname)
527#define PB_SIZE_OFFSET_PTR_FIXARRAY(structname, fieldname) 0
528
529#define PB_ARRAY_SIZE_STATIC(htype, structname, fieldname) PB_ARRAY_SIZE_ ## htype(structname, fieldname)
530#define PB_ARRAY_SIZE_POINTER(htype, structname, fieldname) 1
531#define PB_ARRAY_SIZE_CALLBACK(htype, structname, fieldname) 1
532#define PB_ARRAY_SIZE_REQUIRED(structname, fieldname) 1
533#define PB_ARRAY_SIZE_SINGULAR(structname, fieldname) 1
534#define PB_ARRAY_SIZE_OPTIONAL(structname, fieldname) 1
535#define PB_ARRAY_SIZE_ONEOF(structname, fieldname) 1
536#define PB_ARRAY_SIZE_REPEATED(structname, fieldname) pb_arraysize(structname, fieldname)
537#define PB_ARRAY_SIZE_FIXARRAY(structname, fieldname) pb_arraysize(structname, fieldname)
538
539#define PB_DATA_SIZE_STATIC(htype, structname, fieldname) PB_DATA_SIZE_ ## htype(structname, fieldname)
540#define PB_DATA_SIZE_POINTER(htype, structname, fieldname) PB_DATA_SIZE_PTR_ ## htype(structname, fieldname)
541#define PB_DATA_SIZE_CALLBACK(htype, structname, fieldname) pb_membersize(structname, fieldname)
542#define PB_DATA_SIZE_REQUIRED(structname, fieldname) pb_membersize(structname, fieldname)
543#define PB_DATA_SIZE_SINGULAR(structname, fieldname) pb_membersize(structname, fieldname)
544#define PB_DATA_SIZE_OPTIONAL(structname, fieldname) pb_membersize(structname, fieldname)
545#define PB_DATA_SIZE_ONEOF(structname, fieldname) pb_membersize(structname, PB_ONEOF_NAME(FULL, fieldname))
546#define PB_DATA_SIZE_REPEATED(structname, fieldname) pb_membersize(structname, fieldname[0])
547#define PB_DATA_SIZE_FIXARRAY(structname, fieldname) pb_membersize(structname, fieldname[0])
548#define PB_DATA_SIZE_PTR_REQUIRED(structname, fieldname) pb_membersize(structname, fieldname[0])
549#define PB_DATA_SIZE_PTR_SINGULAR(structname, fieldname) pb_membersize(structname, fieldname[0])
550#define PB_DATA_SIZE_PTR_OPTIONAL(structname, fieldname) pb_membersize(structname, fieldname[0])
551#define PB_DATA_SIZE_PTR_ONEOF(structname, fieldname) pb_membersize(structname, PB_ONEOF_NAME(FULL, fieldname)[0])
552#define PB_DATA_SIZE_PTR_REPEATED(structname, fieldname) pb_membersize(structname, fieldname[0])
553#define PB_DATA_SIZE_PTR_FIXARRAY(structname, fieldname) pb_membersize(structname, fieldname[0])
554
555#define PB_ONEOF_NAME(type, tuple) PB_EXPAND(PB_ONEOF_NAME_ ## type tuple)
556#define PB_ONEOF_NAME_UNION(unionname,membername,fullname) unionname
557#define PB_ONEOF_NAME_MEMBER(unionname,membername,fullname) membername
558#define PB_ONEOF_NAME_FULL(unionname,membername,fullname) fullname
559
560#define PB_GEN_SUBMSG_INFO(structname, atype, htype, ltype, fieldname, tag) \
561    PB_SUBMSG_INFO_ ## htype(ltype, structname, fieldname)
562
563#define PB_SUBMSG_INFO_REQUIRED(ltype, structname, fieldname) PB_SUBMSG_INFO_ ## ltype(structname ## _ ## fieldname ## _MSGTYPE)
564#define PB_SUBMSG_INFO_SINGULAR(ltype, structname, fieldname) PB_SUBMSG_INFO_ ## ltype(structname ## _ ## fieldname ## _MSGTYPE)
565#define PB_SUBMSG_INFO_OPTIONAL(ltype, structname, fieldname) PB_SUBMSG_INFO_ ## ltype(structname ## _ ## fieldname ## _MSGTYPE)
566#define PB_SUBMSG_INFO_ONEOF(ltype, structname, fieldname) PB_SUBMSG_INFO_ONEOF2(ltype, structname, PB_ONEOF_NAME(UNION, fieldname), PB_ONEOF_NAME(MEMBER, fieldname))
567#define PB_SUBMSG_INFO_ONEOF2(ltype, structname, unionname, membername) PB_SUBMSG_INFO_ONEOF3(ltype, structname, unionname, membername)
568#define PB_SUBMSG_INFO_ONEOF3(ltype, structname, unionname, membername) PB_SUBMSG_INFO_ ## ltype(structname ## _ ## unionname ## _ ## membername ## _MSGTYPE)
569#define PB_SUBMSG_INFO_REPEATED(ltype, structname, fieldname) PB_SUBMSG_INFO_ ## ltype(structname ## _ ## fieldname ## _MSGTYPE)
570#define PB_SUBMSG_INFO_FIXARRAY(ltype, structname, fieldname) PB_SUBMSG_INFO_ ## ltype(structname ## _ ## fieldname ## _MSGTYPE)
571#define PB_SUBMSG_INFO_BOOL(t)
572#define PB_SUBMSG_INFO_BYTES(t)
573#define PB_SUBMSG_INFO_DOUBLE(t)
574#define PB_SUBMSG_INFO_ENUM(t)
575#define PB_SUBMSG_INFO_UENUM(t)
576#define PB_SUBMSG_INFO_FIXED32(t)
577#define PB_SUBMSG_INFO_FIXED64(t)
578#define PB_SUBMSG_INFO_FLOAT(t)
579#define PB_SUBMSG_INFO_INT32(t)
580#define PB_SUBMSG_INFO_INT64(t)
581#define PB_SUBMSG_INFO_MESSAGE(t)  PB_SUBMSG_DESCRIPTOR(t)
582#define PB_SUBMSG_INFO_SFIXED32(t)
583#define PB_SUBMSG_INFO_SFIXED64(t)
584#define PB_SUBMSG_INFO_SINT32(t)
585#define PB_SUBMSG_INFO_SINT64(t)
586#define PB_SUBMSG_INFO_STRING(t)
587#define PB_SUBMSG_INFO_UINT32(t)
588#define PB_SUBMSG_INFO_UINT64(t)
589#define PB_SUBMSG_INFO_EXTENSION(t)
590#define PB_SUBMSG_INFO_FIXED_LENGTH_BYTES(t)
591#define PB_SUBMSG_DESCRIPTOR(t)    &(t ## _msg),
592
593/* The field descriptors use a variable width format, with width of either
594 * 1, 2, 4 or 8 of 32-bit words. The two lowest bytes of the first byte always
595 * encode the descriptor size, 6 lowest bits of field tag number, and 8 bits
596 * of the field type.
597 *
598 * Descriptor size is encoded as 0 = 1 word, 1 = 2 words, 2 = 4 words, 3 = 8 words.
599 *
600 * Formats, listed starting with the least significant bit of the first word.
601 * 1 word:  [2-bit len] [6-bit tag] [8-bit type] [8-bit data_offset] [4-bit size_offset] [4-bit data_size]
602 *
603 * 2 words: [2-bit len] [6-bit tag] [8-bit type] [12-bit array_size] [4-bit size_offset]
604 *          [16-bit data_offset] [12-bit data_size] [4-bit tag>>6]
605 *
606 * 4 words: [2-bit len] [6-bit tag] [8-bit type] [16-bit array_size]
607 *          [8-bit size_offset] [24-bit tag>>6]
608 *          [32-bit data_offset]
609 *          [32-bit data_size]
610 *
611 * 8 words: [2-bit len] [6-bit tag] [8-bit type] [16-bit reserved]
612 *          [8-bit size_offset] [24-bit tag>>6]
613 *          [32-bit data_offset]
614 *          [32-bit data_size]
615 *          [32-bit array_size]
616 *          [32-bit reserved]
617 *          [32-bit reserved]
618 *          [32-bit reserved]
619 */
620
621#define PB_FIELDINFO_1(tag, type, data_offset, data_size, size_offset, array_size) \
622    (0 | (((tag) << 2) & 0xFF) | ((type) << 8) | (((uint32_t)(data_offset) & 0xFF) << 16) | \
623     (((uint32_t)(size_offset) & 0x0F) << 24) | (((uint32_t)(data_size) & 0x0F) << 28)),
624
625#define PB_FIELDINFO_2(tag, type, data_offset, data_size, size_offset, array_size) \
626    (1 | (((tag) << 2) & 0xFF) | ((type) << 8) | (((uint32_t)(array_size) & 0xFFF) << 16) | (((uint32_t)(size_offset) & 0x0F) << 28)), \
627    (((uint32_t)(data_offset) & 0xFFFF) | (((uint32_t)(data_size) & 0xFFF) << 16) | (((uint32_t)(tag) & 0x2c0) << 22)),
628
629#define PB_FIELDINFO_4(tag, type, data_offset, data_size, size_offset, array_size) \
630    (2 | (((tag) << 2) & 0xFF) | ((type) << 8) | (((uint32_t)(array_size) & 0xFFFF) << 16)), \
631    ((uint32_t)(int8_t)(size_offset) | (((uint32_t)(tag) << 2) & 0xFFFFFF00)), \
632    (data_offset), (data_size),
633
634#define PB_FIELDINFO_8(tag, type, data_offset, data_size, size_offset, array_size) \
635    (3 | (((tag) << 2) & 0xFF) | ((type) << 8)), \
636    ((uint32_t)(int8_t)(size_offset) | (((uint32_t)(tag) << 2) & 0xFFFFFF00)), \
637    (data_offset), (data_size), (array_size)
638
639/* These assertions verify that the field information fits in the allocated space.
640 * The generator tries to automatically determine the correct width that can fit all
641 * data associated with a message. These asserts will fail only if there has been a
642 * problem in the automatic logic - this may be worth reporting as a bug. As a workaround,
643 * you can increase the descriptor width by defining PB_FIELDINFO_WIDTH or by setting
644 * descriptorsize option in .options file.
645 */
646#define PB_FITS(value,bits) ((uint32_t)(value) < ((uint32_t)1<<bits))
647#define PB_FIELDINFO_ASSERT_1(tag, type, data_offset, data_size, size_offset, array_size) \
648    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)
649
650#define PB_FIELDINFO_ASSERT_2(tag, type, data_offset, data_size, size_offset, array_size) \
651    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)
652
653#ifndef PB_FIELD_32BIT
654/* Maximum field sizes are still 16-bit if pb_size_t is 16-bit */
655#define PB_FIELDINFO_ASSERT_4(tag, type, data_offset, data_size, size_offset, array_size) \
656    PB_STATIC_ASSERT(PB_FITS(tag,16) && PB_FITS(data_offset,16) && PB_FITS((int8_t)size_offset,8) && PB_FITS(data_size,16) && PB_FITS(array_size,16), FIELDINFO_DOES_NOT_FIT_width4_field ## tag)
657
658#define PB_FIELDINFO_ASSERT_8(tag, type, data_offset, data_size, size_offset, array_size) \
659    PB_STATIC_ASSERT(PB_FITS(tag,16) && PB_FITS(data_offset,16) && PB_FITS((int8_t)size_offset,8) && PB_FITS(data_size,16) && PB_FITS(array_size,16), FIELDINFO_DOES_NOT_FIT_width8_field ## tag)
660#else
661/* Up to 32-bit fields supported.
662 * Note that the checks are against 31 bits to avoid compiler warnings about shift wider than type in the test.
663 * I expect that there is no reasonable use for >2GB messages with nanopb anyway.
664 */
665#define PB_FIELDINFO_ASSERT_4(tag, type, data_offset, data_size, size_offset, array_size) \
666    PB_STATIC_ASSERT(PB_FITS(tag,30) && PB_FITS(data_offset,31) && PB_FITS((int8_t)size_offset,8) && PB_FITS(data_size,31) && PB_FITS(array_size,16), FIELDINFO_DOES_NOT_FIT_width4_field ## tag)
667
668#define PB_FIELDINFO_ASSERT_8(tag, type, data_offset, data_size, size_offset, array_size) \
669    PB_STATIC_ASSERT(PB_FITS(tag,30) && PB_FITS(data_offset,31) && PB_FITS((int8_t)size_offset,8) && PB_FITS(data_size,31) && PB_FITS(array_size,31), FIELDINFO_DOES_NOT_FIT_width8_field ## tag)
670#endif
671
672
673/* Automatic picking of FIELDINFO width:
674 * Uses width 1 when possible, otherwise resorts to width 2.
675 */
676
677#ifndef PB_FIELDINFO_WIDTH
678#define PB_FIELDINFO_WIDTH_AUTO(atype, htype, ltype) PB_FIELDINFO_WIDTH_ ## atype(htype, ltype)
679#define PB_FIELDINFO_WIDTH_STATIC(htype, ltype) PB_FIELDINFO_WIDTH_ ## htype(ltype)
680#define PB_FIELDINFO_WIDTH_POINTER(htype, ltype) PB_FIELDINFO_WIDTH_ ## htype(ltype)
681#define PB_FIELDINFO_WIDTH_CALLBACK(htype, ltype) 2
682#define PB_FIELDINFO_WIDTH_REQUIRED(ltype) PB_FIELDINFO_WIDTH_ ## ltype
683#define PB_FIELDINFO_WIDTH_SINGULAR(ltype) PB_FIELDINFO_WIDTH_ ## ltype
684#define PB_FIELDINFO_WIDTH_OPTIONAL(ltype) PB_FIELDINFO_WIDTH_ ## ltype
685#define PB_FIELDINFO_WIDTH_ONEOF(ltype) PB_FIELDINFO_WIDTH_ ## ltype
686#define PB_FIELDINFO_WIDTH_REPEATED(ltype) 2
687#define PB_FIELDINFO_WIDTH_FIXARRAY(ltype) 2
688#define PB_FIELDINFO_WIDTH_BOOL      1
689#define PB_FIELDINFO_WIDTH_BYTES     2
690#define PB_FIELDINFO_WIDTH_DOUBLE    1
691#define PB_FIELDINFO_WIDTH_ENUM      1
692#define PB_FIELDINFO_WIDTH_UENUM     1
693#define PB_FIELDINFO_WIDTH_FIXED32   1
694#define PB_FIELDINFO_WIDTH_FIXED64   1
695#define PB_FIELDINFO_WIDTH_FLOAT     1
696#define PB_FIELDINFO_WIDTH_INT32     1
697#define PB_FIELDINFO_WIDTH_INT64     1
698#define PB_FIELDINFO_WIDTH_MESSAGE   2
699#define PB_FIELDINFO_WIDTH_SFIXED32  1
700#define PB_FIELDINFO_WIDTH_SFIXED64  1
701#define PB_FIELDINFO_WIDTH_SINT32    1
702#define PB_FIELDINFO_WIDTH_SINT64    1
703#define PB_FIELDINFO_WIDTH_STRING    2
704#define PB_FIELDINFO_WIDTH_UINT32    1
705#define PB_FIELDINFO_WIDTH_UINT64    1
706#define PB_FIELDINFO_WIDTH_EXTENSION 1
707#define PB_FIELDINFO_WIDTH_FIXED_LENGTH_BYTES 2
708#else
709#define PB_FIELDINFO_WIDTH_AUTO(atype, htype, ltype) PB_FIELDINFO_WIDTH
710#endif
711
712/* The mapping from protobuf types to LTYPEs is done using these macros. */
713#define PB_LTYPE_MAP_BOOL               PB_LTYPE_VARINT
714#define PB_LTYPE_MAP_BYTES              PB_LTYPE_BYTES
715#define PB_LTYPE_MAP_DOUBLE             PB_LTYPE_FIXED64
716#define PB_LTYPE_MAP_ENUM               PB_LTYPE_VARINT
717#define PB_LTYPE_MAP_UENUM              PB_LTYPE_UVARINT
718#define PB_LTYPE_MAP_FIXED32            PB_LTYPE_FIXED32
719#define PB_LTYPE_MAP_FIXED64            PB_LTYPE_FIXED64
720#define PB_LTYPE_MAP_FLOAT              PB_LTYPE_FIXED32
721#define PB_LTYPE_MAP_INT32              PB_LTYPE_VARINT
722#define PB_LTYPE_MAP_INT64              PB_LTYPE_VARINT
723#define PB_LTYPE_MAP_MESSAGE            PB_LTYPE_SUBMESSAGE
724#define PB_LTYPE_MAP_SFIXED32           PB_LTYPE_FIXED32
725#define PB_LTYPE_MAP_SFIXED64           PB_LTYPE_FIXED64
726#define PB_LTYPE_MAP_SINT32             PB_LTYPE_SVARINT
727#define PB_LTYPE_MAP_SINT64             PB_LTYPE_SVARINT
728#define PB_LTYPE_MAP_STRING             PB_LTYPE_STRING
729#define PB_LTYPE_MAP_UINT32             PB_LTYPE_UVARINT
730#define PB_LTYPE_MAP_UINT64             PB_LTYPE_UVARINT
731#define PB_LTYPE_MAP_EXTENSION          PB_LTYPE_EXTENSION
732#define PB_LTYPE_MAP_FIXED_LENGTH_BYTES PB_LTYPE_FIXED_LENGTH_BYTES
733
734/* These macros are used for giving out error messages.
735 * They are mostly a debugging aid; the main error information
736 * is the true/false return value from functions.
737 * Some code space can be saved by disabling the error
738 * messages if not used.
739 *
740 * PB_SET_ERROR() sets the error message if none has been set yet.
741 *                msg must be a constant string literal.
742 * PB_GET_ERROR() always returns a pointer to a string.
743 * PB_RETURN_ERROR() sets the error and returns false from current
744 *                   function.
745 */
746#ifdef PB_NO_ERRMSG
747#define PB_SET_ERROR(stream, msg) PB_UNUSED(stream)
748#define PB_GET_ERROR(stream) "(errmsg disabled)"
749#else
750#define PB_SET_ERROR(stream, msg) (stream->errmsg = (stream)->errmsg ? (stream)->errmsg : (msg))
751#define PB_GET_ERROR(stream) ((stream)->errmsg ? (stream)->errmsg : "(none)")
752#endif
753
754#define PB_RETURN_ERROR(stream, msg) return PB_SET_ERROR(stream, msg), false
755
756#ifdef __cplusplus
757} /* extern "C" */
758#endif
759
760#ifdef __cplusplus
761#if __cplusplus >= 201103L
762#define PB_CONSTEXPR constexpr
763#else  // __cplusplus >= 201103L
764#define PB_CONSTEXPR
765#endif  // __cplusplus >= 201103L
766
767#if __cplusplus >= 201703L
768#define PB_INLINE_CONSTEXPR inline constexpr
769#else  // __cplusplus >= 201703L
770#define PB_INLINE_CONSTEXPR PB_CONSTEXPR
771#endif  // __cplusplus >= 201703L
772
773namespace nanopb {
774// Each type will be partially specialized by the generator.
775template <typename GenMessageT> struct MessageDescriptor;
776}  // namespace nanopb
777#endif  /* __cplusplus */
778
779#endif
780
781