2
0

pb.h 42 KB

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