pb.h 19 KB

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