pb.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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 > 255 and fields larger than 255 bytes. */
  19. #define PB_FIELD_16BIT 1
  20. /* Add support for tag numbers > 65536 and fields larger than 65536 bytes. */
  21. /* #define PB_FIELD_32BIT 1 */
  22. /* Disable support for error messages in order to save some code space. */
  23. #define PB_NO_ERRMSG 1
  24. /* Disable support for custom streams (support only memory buffers). */
  25. /* #define PB_BUFFER_ONLY 1 */
  26. /* Switch back to the old-style callback function signature.
  27. * This was the default until nanopb-0.2.1. */
  28. /* #define PB_OLD_CALLBACK_STYLE */
  29. /******************************************************************
  30. * You usually don't need to change anything below this line. *
  31. * Feel free to look around and use the defined macros, though. *
  32. ******************************************************************/
  33. /* Version of the nanopb library. Just in case you want to check it in
  34. * your own program. */
  35. #define NANOPB_VERSION nanopb-0.3.6-dev
  36. /* Include all the system headers needed by nanopb. You will need the
  37. * definitions of the following:
  38. * - strlen, memcpy, memset functions
  39. * - [u]int_least8_t, uint_fast8_t, [u]int_least16_t, [u]int32_t, [u]int64_t
  40. * - size_t
  41. * - bool
  42. *
  43. * If you don't have the standard header files, you can instead provide
  44. * a custom header that defines or includes all this. In that case,
  45. * define PB_SYSTEM_HEADER to the path of this file.
  46. */
  47. #ifdef PB_SYSTEM_HEADER
  48. #include PB_SYSTEM_HEADER
  49. #else
  50. #include <stdint.h>
  51. #include <stddef.h>
  52. #include <stdbool.h>
  53. #include <string.h>
  54. #ifdef PB_ENABLE_MALLOC
  55. #include <stdlib.h>
  56. #endif
  57. #endif
  58. /* Macro for defining packed structures (compiler dependent).
  59. * This just reduces memory requirements, but is not required.
  60. */
  61. #if defined(PB_NO_PACKED_STRUCTS)
  62. /* Disable struct packing */
  63. # define PB_PACKED_STRUCT_START
  64. # define PB_PACKED_STRUCT_END
  65. # define pb_packed
  66. #elif defined(__GNUC__) || defined(__clang__)
  67. /* For GCC and clang */
  68. # define PB_PACKED_STRUCT_START
  69. # define PB_PACKED_STRUCT_END
  70. # define pb_packed __attribute__((packed))
  71. #elif defined(__ICCARM__) || defined(__CC_ARM)
  72. /* For IAR ARM and Keil MDK-ARM compilers */
  73. # define PB_PACKED_STRUCT_START _Pragma("pack(push, 1)")
  74. # define PB_PACKED_STRUCT_END _Pragma("pack(pop)")
  75. # define pb_packed
  76. #elif defined(_MSC_VER) && (_MSC_VER >= 1500)
  77. /* For Microsoft Visual C++ */
  78. # define PB_PACKED_STRUCT_START __pragma(pack(push, 1))
  79. # define PB_PACKED_STRUCT_END __pragma(pack(pop))
  80. # define pb_packed
  81. #else
  82. /* Unknown compiler */
  83. # define PB_PACKED_STRUCT_START
  84. # define PB_PACKED_STRUCT_END
  85. # define pb_packed
  86. #endif
  87. /* Handly macro for suppressing unreferenced-parameter compiler warnings. */
  88. #ifndef PB_UNUSED
  89. #define PB_UNUSED(x) (void)(x)
  90. #endif
  91. /* Compile-time assertion, used for checking compatible compilation options.
  92. * If this does not work properly on your compiler, use
  93. * #define PB_NO_STATIC_ASSERT to disable it.
  94. *
  95. * But before doing that, check carefully the error message / place where it
  96. * comes from to see if the error has a real cause. Unfortunately the error
  97. * message is not always very clear to read, but you can see the reason better
  98. * in the place where the PB_STATIC_ASSERT macro was called.
  99. */
  100. #ifndef PB_NO_STATIC_ASSERT
  101. #ifndef PB_STATIC_ASSERT
  102. #define PB_STATIC_ASSERT(COND,MSG) typedef char PB_STATIC_ASSERT_MSG(MSG, __LINE__, __COUNTER__)[(COND)?1:-1];
  103. #define PB_STATIC_ASSERT_MSG(MSG, LINE, COUNTER) PB_STATIC_ASSERT_MSG_(MSG, LINE, COUNTER)
  104. #define PB_STATIC_ASSERT_MSG_(MSG, LINE, COUNTER) pb_static_assertion_##MSG##LINE##COUNTER
  105. #endif
  106. #else
  107. #define PB_STATIC_ASSERT(COND,MSG)
  108. #endif
  109. /* Number of required fields to keep track of. */
  110. #ifndef PB_MAX_REQUIRED_FIELDS
  111. #define PB_MAX_REQUIRED_FIELDS 64
  112. #endif
  113. #if PB_MAX_REQUIRED_FIELDS < 64
  114. #error You should not lower PB_MAX_REQUIRED_FIELDS from the default value (64).
  115. #endif
  116. /* List of possible field types. These are used in the autogenerated code.
  117. * Least-significant 4 bits tell the scalar type
  118. * Most-significant 4 bits specify repeated/required/packed etc.
  119. */
  120. typedef uint_least8_t pb_type_t;
  121. /**** Field data types ****/
  122. /* Numeric types */
  123. #define PB_LTYPE_VARINT 0x00 /* int32, int64, enum, bool */
  124. #define PB_LTYPE_UVARINT 0x01 /* uint32, uint64 */
  125. #define PB_LTYPE_SVARINT 0x02 /* sint32, sint64 */
  126. #define PB_LTYPE_FIXED32 0x03 /* fixed32, sfixed32, float */
  127. #define PB_LTYPE_FIXED64 0x04 /* fixed64, sfixed64, double */
  128. /* Marker for last packable field type. */
  129. #define PB_LTYPE_LAST_PACKABLE 0x04
  130. /* Byte array with pre-allocated buffer.
  131. * data_size is the length of the allocated PB_BYTES_ARRAY structure. */
  132. #define PB_LTYPE_BYTES 0x05
  133. /* String with pre-allocated buffer.
  134. * data_size is the maximum length. */
  135. #define PB_LTYPE_STRING 0x06
  136. /* Submessage
  137. * submsg_fields is pointer to field descriptions */
  138. #define PB_LTYPE_SUBMESSAGE 0x07
  139. /* Extension pseudo-field
  140. * The field contains a pointer to pb_extension_t */
  141. #define PB_LTYPE_EXTENSION 0x08
  142. /* Number of declared LTYPES */
  143. #define PB_LTYPES_COUNT 9
  144. #define PB_LTYPE_MASK 0x0F
  145. /**** Field repetition rules ****/
  146. #define PB_HTYPE_REQUIRED 0x00
  147. #define PB_HTYPE_OPTIONAL 0x10
  148. #define PB_HTYPE_REPEATED 0x20
  149. #define PB_HTYPE_ONEOF 0x30
  150. #define PB_HTYPE_MASK 0x30
  151. /**** Field allocation types ****/
  152. #define PB_ATYPE_STATIC 0x00
  153. #define PB_ATYPE_POINTER 0x80
  154. #define PB_ATYPE_CALLBACK 0x40
  155. #define PB_ATYPE_MASK 0xC0
  156. #define PB_ATYPE(x) ((x) & PB_ATYPE_MASK)
  157. #define PB_HTYPE(x) ((x) & PB_HTYPE_MASK)
  158. #define PB_LTYPE(x) ((x) & PB_LTYPE_MASK)
  159. /* Data type used for storing sizes of struct fields
  160. * and array counts.
  161. */
  162. #if defined(PB_FIELD_32BIT)
  163. typedef uint32_t pb_size_t;
  164. typedef int32_t pb_ssize_t;
  165. #elif defined(PB_FIELD_16BIT)
  166. typedef uint_least16_t pb_size_t;
  167. typedef int_least16_t pb_ssize_t;
  168. #else
  169. typedef uint_least8_t pb_size_t;
  170. typedef int_least8_t pb_ssize_t;
  171. #endif
  172. #define PB_SIZE_MAX ((pb_size_t)-1)
  173. /* Data type for storing encoded data and other byte streams.
  174. * This typedef exists to support platforms where uint8_t does not exist.
  175. * You can regard it as equivalent on uint8_t on other platforms.
  176. */
  177. typedef uint_least8_t pb_byte_t;
  178. /* This structure is used in auto-generated constants
  179. * to specify struct fields.
  180. * You can change field sizes if you need structures
  181. * larger than 256 bytes or field tags larger than 256.
  182. * The compiler should complain if your .proto has such
  183. * structures. Fix that by defining PB_FIELD_16BIT or
  184. * PB_FIELD_32BIT.
  185. */
  186. PB_PACKED_STRUCT_START
  187. typedef struct pb_field_s pb_field_t;
  188. struct pb_field_s {
  189. pb_size_t tag;
  190. pb_type_t type;
  191. pb_size_t data_offset; /* Offset of field data, relative to previous field. */
  192. pb_ssize_t size_offset; /* Offset of array size or has-boolean, relative to data */
  193. pb_size_t data_size; /* Data size in bytes for a single item */
  194. pb_size_t array_size; /* Maximum number of entries in array */
  195. /* Field definitions for submessage
  196. * OR default value for all other non-array, non-callback types
  197. * If null, then field will zeroed. */
  198. const void *ptr;
  199. } pb_packed;
  200. PB_PACKED_STRUCT_END
  201. /* Make sure that the standard integer types are of the expected sizes.
  202. * Otherwise fixed32/fixed64 fields can break.
  203. *
  204. * If you get errors here, it probably means that your stdint.h is not
  205. * correct for your platform.
  206. */
  207. PB_STATIC_ASSERT(sizeof(int64_t) == 2 * sizeof(int32_t), INT64_T_WRONG_SIZE)
  208. PB_STATIC_ASSERT(sizeof(uint64_t) == 2 * sizeof(uint32_t), UINT64_T_WRONG_SIZE)
  209. /* This structure is used for 'bytes' arrays.
  210. * It has the number of bytes in the beginning, and after that an array.
  211. * Note that actual structs used will have a different length of bytes array.
  212. */
  213. #define PB_BYTES_ARRAY_T(n) struct { pb_size_t size; pb_byte_t bytes[n]; }
  214. #define PB_BYTES_ARRAY_T_ALLOCSIZE(n) ((size_t)n + offsetof(pb_bytes_array_t, bytes))
  215. struct pb_bytes_array_s {
  216. pb_size_t size;
  217. pb_byte_t bytes[1];
  218. };
  219. typedef struct pb_bytes_array_s pb_bytes_array_t;
  220. /* This structure is used for giving the callback function.
  221. * It is stored in the message structure and filled in by the method that
  222. * calls pb_decode.
  223. *
  224. * The decoding callback will be given a limited-length stream
  225. * If the wire type was string, the length is the length of the string.
  226. * If the wire type was a varint/fixed32/fixed64, the length is the length
  227. * of the actual value.
  228. * The function may be called multiple times (especially for repeated types,
  229. * but also otherwise if the message happens to contain the field multiple
  230. * times.)
  231. *
  232. * The encoding callback will receive the actual output stream.
  233. * It should write all the data in one call, including the field tag and
  234. * wire type. It can write multiple fields.
  235. *
  236. * The callback can be null if you want to skip a field.
  237. */
  238. typedef struct pb_istream_s pb_istream_t;
  239. typedef struct pb_ostream_s pb_ostream_t;
  240. typedef struct pb_callback_s pb_callback_t;
  241. struct pb_callback_s {
  242. #ifdef PB_OLD_CALLBACK_STYLE
  243. /* Deprecated since nanopb-0.2.1 */
  244. union {
  245. bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void *arg);
  246. bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, const void *arg);
  247. } funcs;
  248. #else
  249. /* New function signature, which allows modifying arg contents in callback. */
  250. union {
  251. bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void **arg);
  252. bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, void * const *arg);
  253. } funcs;
  254. #endif
  255. /* Free arg for use by callback */
  256. void *arg;
  257. };
  258. /* Wire types. Library user needs these only in encoder callbacks. */
  259. typedef enum {
  260. PB_WT_VARINT = 0,
  261. PB_WT_64BIT = 1,
  262. PB_WT_STRING = 2,
  263. PB_WT_32BIT = 5
  264. } pb_wire_type_t;
  265. /* Structure for defining the handling of unknown/extension fields.
  266. * Usually the pb_extension_type_t structure is automatically generated,
  267. * while the pb_extension_t structure is created by the user. However,
  268. * if you want to catch all unknown fields, you can also create a custom
  269. * pb_extension_type_t with your own callback.
  270. */
  271. typedef struct pb_extension_type_s pb_extension_type_t;
  272. typedef struct pb_extension_s pb_extension_t;
  273. struct pb_extension_type_s {
  274. /* Called for each unknown field in the message.
  275. * If you handle the field, read off all of its data and return true.
  276. * If you do not handle the field, do not read anything and return true.
  277. * If you run into an error, return false.
  278. * Set to NULL for default handler.
  279. */
  280. bool (*decode)(pb_istream_t *stream, pb_extension_t *extension,
  281. uint32_t tag, pb_wire_type_t wire_type);
  282. /* Called once after all regular fields have been encoded.
  283. * If you have something to write, do so and return true.
  284. * If you do not have anything to write, just return true.
  285. * If you run into an error, return false.
  286. * Set to NULL for default handler.
  287. */
  288. bool (*encode)(pb_ostream_t *stream, const pb_extension_t *extension);
  289. /* Free field for use by the callback. */
  290. const void *arg;
  291. };
  292. struct pb_extension_s {
  293. /* Type describing the extension field. Usually you'll initialize
  294. * this to a pointer to the automatically generated structure. */
  295. const pb_extension_type_t *type;
  296. /* Destination for the decoded data. This must match the datatype
  297. * of the extension field. */
  298. void *dest;
  299. /* Pointer to the next extension handler, or NULL.
  300. * If this extension does not match a field, the next handler is
  301. * automatically called. */
  302. pb_extension_t *next;
  303. /* The decoder sets this to true if the extension was found.
  304. * Ignored for encoding. */
  305. bool found;
  306. };
  307. /* Memory allocation functions to use. You can define pb_realloc and
  308. * pb_free to custom functions if you want. */
  309. #ifdef PB_ENABLE_MALLOC
  310. # ifndef pb_realloc
  311. # define pb_realloc(ptr, size) realloc(ptr, size)
  312. # endif
  313. # ifndef pb_free
  314. # define pb_free(ptr) free(ptr)
  315. # endif
  316. #endif
  317. /* This is used to inform about need to regenerate .pb.h/.pb.c files. */
  318. #define PB_PROTO_HEADER_VERSION 30
  319. /* These macros are used to declare pb_field_t's in the constant array. */
  320. /* Size of a structure member, in bytes. */
  321. #define pb_membersize(st, m) (sizeof ((st*)0)->m)
  322. /* Number of entries in an array. */
  323. #define pb_arraysize(st, m) (pb_membersize(st, m) / pb_membersize(st, m[0]))
  324. /* Delta from start of one member to the start of another member. */
  325. #define pb_delta(st, m1, m2) ((int)offsetof(st, m1) - (int)offsetof(st, m2))
  326. /* Marks the end of the field list */
  327. #define PB_LAST_FIELD {0,(pb_type_t) 0,0,0,0,0,0}
  328. /* Macros for filling in the data_offset field */
  329. /* data_offset for first field in a message */
  330. #define PB_DATAOFFSET_FIRST(st, m1, m2) (offsetof(st, m1))
  331. /* data_offset for subsequent fields */
  332. #define PB_DATAOFFSET_OTHER(st, m1, m2) (offsetof(st, m1) - offsetof(st, m2) - pb_membersize(st, m2))
  333. /* Choose first/other based on m1 == m2 (deprecated, remains for backwards compatibility) */
  334. #define PB_DATAOFFSET_CHOOSE(st, m1, m2) (int)(offsetof(st, m1) == offsetof(st, m2) \
  335. ? PB_DATAOFFSET_FIRST(st, m1, m2) \
  336. : PB_DATAOFFSET_OTHER(st, m1, m2))
  337. /* Required fields are the simplest. They just have delta (padding) from
  338. * previous field end, and the size of the field. Pointer is used for
  339. * submessages and default values.
  340. */
  341. #define PB_REQUIRED_STATIC(tag, st, m, fd, ltype, ptr) \
  342. {tag, PB_ATYPE_STATIC | PB_HTYPE_REQUIRED | ltype, \
  343. fd, 0, pb_membersize(st, m), 0, ptr}
  344. /* Optional fields add the delta to the has_ variable. */
  345. #define PB_OPTIONAL_STATIC(tag, st, m, fd, ltype, ptr) \
  346. {tag, PB_ATYPE_STATIC | PB_HTYPE_OPTIONAL | ltype, \
  347. fd, \
  348. pb_delta(st, has_ ## m, m), \
  349. pb_membersize(st, m), 0, ptr}
  350. /* Repeated fields have a _count field and also the maximum number of entries. */
  351. #define PB_REPEATED_STATIC(tag, st, m, fd, ltype, ptr) \
  352. {tag, PB_ATYPE_STATIC | PB_HTYPE_REPEATED | ltype, \
  353. fd, \
  354. pb_delta(st, m ## _count, m), \
  355. pb_membersize(st, m[0]), \
  356. pb_arraysize(st, m), ptr}
  357. /* Allocated fields carry the size of the actual data, not the pointer */
  358. #define PB_REQUIRED_POINTER(tag, st, m, fd, ltype, ptr) \
  359. {tag, PB_ATYPE_POINTER | PB_HTYPE_REQUIRED | ltype, \
  360. fd, 0, pb_membersize(st, m[0]), 0, ptr}
  361. /* Optional fields don't need a has_ variable, as information would be redundant */
  362. #define PB_OPTIONAL_POINTER(tag, st, m, fd, ltype, ptr) \
  363. {tag, PB_ATYPE_POINTER | PB_HTYPE_OPTIONAL | ltype, \
  364. fd, 0, pb_membersize(st, m[0]), 0, ptr}
  365. /* Repeated fields have a _count field and a pointer to array of pointers */
  366. #define PB_REPEATED_POINTER(tag, st, m, fd, ltype, ptr) \
  367. {tag, PB_ATYPE_POINTER | PB_HTYPE_REPEATED | ltype, \
  368. fd, pb_delta(st, m ## _count, m), \
  369. pb_membersize(st, m[0]), 0, ptr}
  370. /* Callbacks are much like required fields except with special datatype. */
  371. #define PB_REQUIRED_CALLBACK(tag, st, m, fd, ltype, ptr) \
  372. {tag, PB_ATYPE_CALLBACK | PB_HTYPE_REQUIRED | ltype, \
  373. fd, 0, pb_membersize(st, m), 0, ptr}
  374. #define PB_OPTIONAL_CALLBACK(tag, st, m, fd, ltype, ptr) \
  375. {tag, PB_ATYPE_CALLBACK | PB_HTYPE_OPTIONAL | ltype, \
  376. fd, 0, pb_membersize(st, m), 0, ptr}
  377. #define PB_REPEATED_CALLBACK(tag, st, m, fd, ltype, ptr) \
  378. {tag, PB_ATYPE_CALLBACK | PB_HTYPE_REPEATED | ltype, \
  379. fd, 0, pb_membersize(st, m), 0, ptr}
  380. /* Optional extensions don't have the has_ field, as that would be redundant. */
  381. #define PB_OPTEXT_STATIC(tag, st, m, fd, ltype, ptr) \
  382. {tag, PB_ATYPE_STATIC | PB_HTYPE_OPTIONAL | ltype, \
  383. 0, \
  384. 0, \
  385. pb_membersize(st, m), 0, ptr}
  386. #define PB_OPTEXT_POINTER(tag, st, m, fd, ltype, ptr) \
  387. PB_OPTIONAL_POINTER(tag, st, m, fd, ltype, ptr)
  388. #define PB_OPTEXT_CALLBACK(tag, st, m, fd, ltype, ptr) \
  389. PB_OPTIONAL_CALLBACK(tag, st, m, fd, ltype, ptr)
  390. /* The mapping from protobuf types to LTYPEs is done using these macros. */
  391. #define PB_LTYPE_MAP_BOOL PB_LTYPE_VARINT
  392. #define PB_LTYPE_MAP_BYTES PB_LTYPE_BYTES
  393. #define PB_LTYPE_MAP_DOUBLE PB_LTYPE_FIXED64
  394. #define PB_LTYPE_MAP_ENUM PB_LTYPE_VARINT
  395. #define PB_LTYPE_MAP_UENUM PB_LTYPE_UVARINT
  396. #define PB_LTYPE_MAP_FIXED32 PB_LTYPE_FIXED32
  397. #define PB_LTYPE_MAP_FIXED64 PB_LTYPE_FIXED64
  398. #define PB_LTYPE_MAP_FLOAT PB_LTYPE_FIXED32
  399. #define PB_LTYPE_MAP_INT32 PB_LTYPE_VARINT
  400. #define PB_LTYPE_MAP_INT64 PB_LTYPE_VARINT
  401. #define PB_LTYPE_MAP_MESSAGE PB_LTYPE_SUBMESSAGE
  402. #define PB_LTYPE_MAP_SFIXED32 PB_LTYPE_FIXED32
  403. #define PB_LTYPE_MAP_SFIXED64 PB_LTYPE_FIXED64
  404. #define PB_LTYPE_MAP_SINT32 PB_LTYPE_SVARINT
  405. #define PB_LTYPE_MAP_SINT64 PB_LTYPE_SVARINT
  406. #define PB_LTYPE_MAP_STRING PB_LTYPE_STRING
  407. #define PB_LTYPE_MAP_UINT32 PB_LTYPE_UVARINT
  408. #define PB_LTYPE_MAP_UINT64 PB_LTYPE_UVARINT
  409. #define PB_LTYPE_MAP_EXTENSION PB_LTYPE_EXTENSION
  410. /* This is the actual macro used in field descriptions.
  411. * It takes these arguments:
  412. * - Field tag number
  413. * - Field type: BOOL, BYTES, DOUBLE, ENUM, UENUM, FIXED32, FIXED64,
  414. * FLOAT, INT32, INT64, MESSAGE, SFIXED32, SFIXED64
  415. * SINT32, SINT64, STRING, UINT32, UINT64 or EXTENSION
  416. * - Field rules: REQUIRED, OPTIONAL or REPEATED
  417. * - Allocation: STATIC or CALLBACK
  418. * - Placement: FIRST or OTHER, depending on if this is the first field in structure.
  419. * - Message name
  420. * - Field name
  421. * - Previous field name (or field name again for first field)
  422. * - Pointer to default value or submsg fields.
  423. */
  424. #define PB_FIELD(tag, type, rules, allocation, placement, message, field, prevfield, ptr) \
  425. PB_ ## rules ## _ ## allocation(tag, message, field, \
  426. PB_DATAOFFSET_ ## placement(message, field, prevfield), \
  427. PB_LTYPE_MAP_ ## type, ptr)
  428. /* Field description for oneof fields. This requires taking into account the
  429. * union name also, that's why a separate set of macros is needed.
  430. */
  431. #define PB_ONEOF_STATIC(u, tag, st, m, fd, ltype, ptr) \
  432. {tag, PB_ATYPE_STATIC | PB_HTYPE_ONEOF | ltype, \
  433. fd, pb_delta(st, which_ ## u, u.m), \
  434. pb_membersize(st, u.m), 0, ptr}
  435. #define PB_ONEOF_POINTER(u, tag, st, m, fd, ltype, ptr) \
  436. {tag, PB_ATYPE_POINTER | PB_HTYPE_ONEOF | ltype, \
  437. fd, pb_delta(st, which_ ## u, u.m), \
  438. pb_membersize(st, u.m[0]), 0, ptr}
  439. #define PB_ONEOF_FIELD(union_name, tag, type, rules, allocation, placement, message, field, prevfield, ptr) \
  440. PB_ONEOF_ ## allocation(union_name, tag, message, field, \
  441. PB_DATAOFFSET_ ## placement(message, union_name.field, prevfield), \
  442. PB_LTYPE_MAP_ ## type, ptr)
  443. #define PB_ANONYMOUS_ONEOF_STATIC(u, tag, st, m, fd, ltype, ptr) \
  444. {tag, PB_ATYPE_STATIC | PB_HTYPE_ONEOF | ltype, \
  445. fd, pb_delta(st, which_ ## u, m), \
  446. pb_membersize(st, m), 0, ptr}
  447. #define PB_ANONYMOUS_ONEOF_POINTER(u, tag, st, m, fd, ltype, ptr) \
  448. {tag, PB_ATYPE_POINTER | PB_HTYPE_ONEOF | ltype, \
  449. fd, pb_delta(st, which_ ## u, m), \
  450. pb_membersize(st, m[0]), 0, ptr}
  451. #define PB_ANONYMOUS_ONEOF_FIELD(union_name, tag, type, rules, allocation, placement, message, field, prevfield, ptr) \
  452. PB_ANONYMOUS_ONEOF_ ## allocation(union_name, tag, message, field, \
  453. PB_DATAOFFSET_ ## placement(message, field, prevfield), \
  454. PB_LTYPE_MAP_ ## type, ptr)
  455. /* These macros are used for giving out error messages.
  456. * They are mostly a debugging aid; the main error information
  457. * is the true/false return value from functions.
  458. * Some code space can be saved by disabling the error
  459. * messages if not used.
  460. *
  461. * PB_SET_ERROR() sets the error message if none has been set yet.
  462. * msg must be a constant string literal.
  463. * PB_GET_ERROR() always returns a pointer to a string.
  464. * PB_RETURN_ERROR() sets the error and returns false from current
  465. * function.
  466. */
  467. #ifdef PB_NO_ERRMSG
  468. #define PB_SET_ERROR(stream, msg) PB_UNUSED(stream)
  469. #define PB_GET_ERROR(stream) "(errmsg disabled)"
  470. #else
  471. #define PB_SET_ERROR(stream, msg) (stream->errmsg = (stream)->errmsg ? (stream)->errmsg : (msg))
  472. #define PB_GET_ERROR(stream) ((stream)->errmsg ? (stream)->errmsg : "(none)")
  473. #endif
  474. #define PB_RETURN_ERROR(stream, msg) return PB_SET_ERROR(stream, msg), false
  475. #endif