pb.h 43 KB

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