pb.h 35 KB

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