pb.h 23 KB

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