Nenhuma descrição

pb.h 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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.9.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]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. /* Byte array with inline, pre-allocated byffer.
  143. * data_size is the length of the inline, allocated buffer.
  144. * This differs from PB_LTYPE_BYTES by defining the element as
  145. * pb_byte_t[data_size] rather than pb_bytes_array_t. */
  146. #define PB_LTYPE_FIXED_LENGTH_BYTES 0x09
  147. /* Number of declared LTYPES */
  148. #define PB_LTYPES_COUNT 0x0A
  149. #define PB_LTYPE_MASK 0x0F
  150. /**** Field repetition rules ****/
  151. #define PB_HTYPE_REQUIRED 0x00
  152. #define PB_HTYPE_OPTIONAL 0x10
  153. #define PB_HTYPE_REPEATED 0x20
  154. #define PB_HTYPE_ONEOF 0x30
  155. #define PB_HTYPE_MASK 0x30
  156. /**** Field allocation types ****/
  157. #define PB_ATYPE_STATIC 0x00
  158. #define PB_ATYPE_POINTER 0x80
  159. #define PB_ATYPE_CALLBACK 0x40
  160. #define PB_ATYPE_MASK 0xC0
  161. #define PB_ATYPE(x) ((x) & PB_ATYPE_MASK)
  162. #define PB_HTYPE(x) ((x) & PB_HTYPE_MASK)
  163. #define PB_LTYPE(x) ((x) & PB_LTYPE_MASK)
  164. /* Data type used for storing sizes of struct fields
  165. * and array counts.
  166. */
  167. #if defined(PB_FIELD_32BIT)
  168. typedef uint32_t pb_size_t;
  169. typedef int32_t pb_ssize_t;
  170. #elif defined(PB_FIELD_16BIT)
  171. typedef uint_least16_t pb_size_t;
  172. typedef int_least16_t pb_ssize_t;
  173. #else
  174. typedef uint_least8_t pb_size_t;
  175. typedef int_least8_t pb_ssize_t;
  176. #endif
  177. #define PB_SIZE_MAX ((pb_size_t)-1)
  178. /* Data type for storing encoded data and other byte streams.
  179. * This typedef exists to support platforms where uint8_t does not exist.
  180. * You can regard it as equivalent on uint8_t on other platforms.
  181. */
  182. typedef uint_least8_t pb_byte_t;
  183. /* This structure is used in auto-generated constants
  184. * to specify struct fields.
  185. * You can change field sizes if you need structures
  186. * larger than 256 bytes or field tags larger than 256.
  187. * The compiler should complain if your .proto has such
  188. * structures. Fix that by defining PB_FIELD_16BIT or
  189. * PB_FIELD_32BIT.
  190. */
  191. PB_PACKED_STRUCT_START
  192. typedef struct pb_field_s pb_field_t;
  193. struct pb_field_s {
  194. pb_size_t tag;
  195. pb_type_t type;
  196. pb_size_t data_offset; /* Offset of field data, relative to previous field. */
  197. pb_ssize_t size_offset; /* Offset of array size or has-boolean, relative to data */
  198. pb_size_t data_size; /* Data size in bytes for a single item */
  199. pb_size_t array_size; /* Maximum number of entries in array */
  200. /* Field definitions for submessage
  201. * OR default value for all other non-array, non-callback types
  202. * If null, then field will zeroed. */
  203. const void *ptr;
  204. } pb_packed;
  205. PB_PACKED_STRUCT_END
  206. /* Make sure that the standard integer types are of the expected sizes.
  207. * Otherwise fixed32/fixed64 fields can break.
  208. *
  209. * If you get errors here, it probably means that your stdint.h is not
  210. * correct for your platform.
  211. */
  212. #ifndef PB_WITHOUT_64BIT
  213. PB_STATIC_ASSERT(sizeof(int64_t) == 2 * sizeof(int32_t), INT64_T_WRONG_SIZE)
  214. PB_STATIC_ASSERT(sizeof(uint64_t) == 2 * sizeof(uint32_t), UINT64_T_WRONG_SIZE)
  215. #endif
  216. /* This structure is used for 'bytes' arrays.
  217. * It has the number of bytes in the beginning, and after that an array.
  218. * Note that actual structs used will have a different length of bytes array.
  219. */
  220. #define PB_BYTES_ARRAY_T(n) struct { pb_size_t size; pb_byte_t bytes[n]; }
  221. #define PB_BYTES_ARRAY_T_ALLOCSIZE(n) ((size_t)n + offsetof(pb_bytes_array_t, bytes))
  222. struct pb_bytes_array_s {
  223. pb_size_t size;
  224. pb_byte_t bytes[1];
  225. };
  226. typedef struct pb_bytes_array_s pb_bytes_array_t;
  227. /* This structure is used for giving the callback function.
  228. * It is stored in the message structure and filled in by the method that
  229. * calls pb_decode.
  230. *
  231. * The decoding callback will be given a limited-length stream
  232. * If the wire type was string, the length is the length of the string.
  233. * If the wire type was a varint/fixed32/fixed64, the length is the length
  234. * of the actual value.
  235. * The function may be called multiple times (especially for repeated types,
  236. * but also otherwise if the message happens to contain the field multiple
  237. * times.)
  238. *
  239. * The encoding callback will receive the actual output stream.
  240. * It should write all the data in one call, including the field tag and
  241. * wire type. It can write multiple fields.
  242. *
  243. * The callback can be null if you want to skip a field.
  244. */
  245. typedef struct pb_istream_s pb_istream_t;
  246. typedef struct pb_ostream_s pb_ostream_t;
  247. typedef struct pb_callback_s pb_callback_t;
  248. struct pb_callback_s {
  249. #ifdef PB_OLD_CALLBACK_STYLE
  250. /* Deprecated since nanopb-0.2.1 */
  251. union {
  252. bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void *arg);
  253. bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, const void *arg);
  254. } funcs;
  255. #else
  256. /* New function signature, which allows modifying arg contents in callback. */
  257. union {
  258. bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void **arg);
  259. bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, void * const *arg);
  260. } funcs;
  261. #endif
  262. /* Free arg for use by callback */
  263. void *arg;
  264. };
  265. /* Wire types. Library user needs these only in encoder callbacks. */
  266. typedef enum {
  267. PB_WT_VARINT = 0,
  268. PB_WT_64BIT = 1,
  269. PB_WT_STRING = 2,
  270. PB_WT_32BIT = 5
  271. } pb_wire_type_t;
  272. /* Structure for defining the handling of unknown/extension fields.
  273. * Usually the pb_extension_type_t structure is automatically generated,
  274. * while the pb_extension_t structure is created by the user. However,
  275. * if you want to catch all unknown fields, you can also create a custom
  276. * pb_extension_type_t with your own callback.
  277. */
  278. typedef struct pb_extension_type_s pb_extension_type_t;
  279. typedef struct pb_extension_s pb_extension_t;
  280. struct pb_extension_type_s {
  281. /* Called for each unknown field in the message.
  282. * If you handle the field, read off all of its data and return true.
  283. * If you do not handle the field, do not read anything and return true.
  284. * If you run into an error, return false.
  285. * Set to NULL for default handler.
  286. */
  287. bool (*decode)(pb_istream_t *stream, pb_extension_t *extension,
  288. uint32_t tag, pb_wire_type_t wire_type);
  289. /* Called once after all regular fields have been encoded.
  290. * If you have something to write, do so and return true.
  291. * If you do not have anything to write, just return true.
  292. * If you run into an error, return false.
  293. * Set to NULL for default handler.
  294. */
  295. bool (*encode)(pb_ostream_t *stream, const pb_extension_t *extension);
  296. /* Free field for use by the callback. */
  297. const void *arg;
  298. };
  299. struct pb_extension_s {
  300. /* Type describing the extension field. Usually you'll initialize
  301. * this to a pointer to the automatically generated structure. */
  302. const pb_extension_type_t *type;
  303. /* Destination for the decoded data. This must match the datatype
  304. * of the extension field. */
  305. void *dest;
  306. /* Pointer to the next extension handler, or NULL.
  307. * If this extension does not match a field, the next handler is
  308. * automatically called. */
  309. pb_extension_t *next;
  310. /* The decoder sets this to true if the extension was found.
  311. * Ignored for encoding. */
  312. bool found;
  313. };
  314. /* Memory allocation functions to use. You can define pb_realloc and
  315. * pb_free to custom functions if you want. */
  316. #ifdef PB_ENABLE_MALLOC
  317. # ifndef pb_realloc
  318. # define pb_realloc(ptr, size) realloc(ptr, size)
  319. # endif
  320. # ifndef pb_free
  321. # define pb_free(ptr) free(ptr)
  322. # endif
  323. #endif
  324. /* This is used to inform about need to regenerate .pb.h/.pb.c files. */
  325. #define PB_PROTO_HEADER_VERSION 30
  326. /* These macros are used to declare pb_field_t's in the constant array. */
  327. /* Size of a structure member, in bytes. */
  328. #define pb_membersize(st, m) (sizeof ((st*)0)->m)
  329. /* Number of entries in an array. */
  330. #define pb_arraysize(st, m) (pb_membersize(st, m) / pb_membersize(st, m[0]))
  331. /* Delta from start of one member to the start of another member. */
  332. #define pb_delta(st, m1, m2) ((int)offsetof(st, m1) - (int)offsetof(st, m2))
  333. /* Marks the end of the field list */
  334. #define PB_LAST_FIELD {0,(pb_type_t) 0,0,0,0,0,0}
  335. /* Macros for filling in the data_offset field */
  336. /* data_offset for first field in a message */
  337. #define PB_DATAOFFSET_FIRST(st, m1, m2) (offsetof(st, m1))
  338. /* data_offset for subsequent fields */
  339. #define PB_DATAOFFSET_OTHER(st, m1, m2) (offsetof(st, m1) - offsetof(st, m2) - pb_membersize(st, m2))
  340. /* data offset for subsequent fields inside an union (oneof) */
  341. #define PB_DATAOFFSET_UNION(st, m1, m2) (PB_SIZE_MAX)
  342. /* Choose first/other based on m1 == m2 (deprecated, remains for backwards compatibility) */
  343. #define PB_DATAOFFSET_CHOOSE(st, m1, m2) (int)(offsetof(st, m1) == offsetof(st, m2) \
  344. ? PB_DATAOFFSET_FIRST(st, m1, m2) \
  345. : PB_DATAOFFSET_OTHER(st, m1, m2))
  346. /* Required fields are the simplest. They just have delta (padding) from
  347. * previous field end, and the size of the field. Pointer is used for
  348. * submessages and default values.
  349. */
  350. #define PB_REQUIRED_STATIC(tag, st, m, fd, ltype, ptr) \
  351. {tag, PB_ATYPE_STATIC | PB_HTYPE_REQUIRED | ltype, \
  352. fd, 0, pb_membersize(st, m), 0, ptr}
  353. /* Optional fields add the delta to the has_ variable. */
  354. #define PB_OPTIONAL_STATIC(tag, st, m, fd, ltype, ptr) \
  355. {tag, PB_ATYPE_STATIC | PB_HTYPE_OPTIONAL | ltype, \
  356. fd, \
  357. pb_delta(st, has_ ## m, m), \
  358. pb_membersize(st, m), 0, ptr}
  359. #define PB_SINGULAR_STATIC(tag, st, m, fd, ltype, ptr) \
  360. {tag, PB_ATYPE_STATIC | PB_HTYPE_OPTIONAL | ltype, \
  361. fd, 0, pb_membersize(st, m), 0, ptr}
  362. /* Repeated fields have a _count field and also the maximum number of entries. */
  363. #define PB_REPEATED_STATIC(tag, st, m, fd, ltype, ptr) \
  364. {tag, PB_ATYPE_STATIC | PB_HTYPE_REPEATED | ltype, \
  365. fd, \
  366. pb_delta(st, m ## _count, m), \
  367. pb_membersize(st, m[0]), \
  368. pb_arraysize(st, m), ptr}
  369. /* Allocated fields carry the size of the actual data, not the pointer */
  370. #define PB_REQUIRED_POINTER(tag, st, m, fd, ltype, ptr) \
  371. {tag, PB_ATYPE_POINTER | PB_HTYPE_REQUIRED | ltype, \
  372. fd, 0, pb_membersize(st, m[0]), 0, ptr}
  373. /* Optional fields don't need a has_ variable, as information would be redundant */
  374. #define PB_OPTIONAL_POINTER(tag, st, m, fd, ltype, ptr) \
  375. {tag, PB_ATYPE_POINTER | PB_HTYPE_OPTIONAL | ltype, \
  376. fd, 0, pb_membersize(st, m[0]), 0, ptr}
  377. /* Same as optional fields*/
  378. #define PB_SINGULAR_POINTER(tag, st, m, fd, ltype, ptr) \
  379. {tag, PB_ATYPE_POINTER | PB_HTYPE_OPTIONAL | ltype, \
  380. fd, 0, pb_membersize(st, m[0]), 0, ptr}
  381. /* Repeated fields have a _count field and a pointer to array of pointers */
  382. #define PB_REPEATED_POINTER(tag, st, m, fd, ltype, ptr) \
  383. {tag, PB_ATYPE_POINTER | PB_HTYPE_REPEATED | ltype, \
  384. fd, pb_delta(st, m ## _count, m), \
  385. pb_membersize(st, m[0]), 0, ptr}
  386. /* Callbacks are much like required fields except with special datatype. */
  387. #define PB_REQUIRED_CALLBACK(tag, st, m, fd, ltype, ptr) \
  388. {tag, PB_ATYPE_CALLBACK | PB_HTYPE_REQUIRED | ltype, \
  389. fd, 0, pb_membersize(st, m), 0, ptr}
  390. #define PB_OPTIONAL_CALLBACK(tag, st, m, fd, ltype, ptr) \
  391. {tag, PB_ATYPE_CALLBACK | PB_HTYPE_OPTIONAL | ltype, \
  392. fd, 0, pb_membersize(st, m), 0, ptr}
  393. #define PB_SINGULAR_CALLBACK(tag, st, m, fd, ltype, ptr) \
  394. {tag, PB_ATYPE_CALLBACK | PB_HTYPE_OPTIONAL | ltype, \
  395. fd, 0, pb_membersize(st, m), 0, ptr}
  396. #define PB_REPEATED_CALLBACK(tag, st, m, fd, ltype, ptr) \
  397. {tag, PB_ATYPE_CALLBACK | PB_HTYPE_REPEATED | ltype, \
  398. fd, 0, pb_membersize(st, m), 0, ptr}
  399. /* Optional extensions don't have the has_ field, as that would be redundant.
  400. * Furthermore, the combination of OPTIONAL without has_ field is used
  401. * for indicating proto3 style fields. Extensions exist in proto2 mode only,
  402. * so they should be encoded according to proto2 rules. To avoid the conflict,
  403. * extensions are marked as REQUIRED instead.
  404. */
  405. #define PB_OPTEXT_STATIC(tag, st, m, fd, ltype, ptr) \
  406. {tag, PB_ATYPE_STATIC | PB_HTYPE_REQUIRED | ltype, \
  407. 0, \
  408. 0, \
  409. pb_membersize(st, m), 0, ptr}
  410. #define PB_OPTEXT_POINTER(tag, st, m, fd, ltype, ptr) \
  411. PB_OPTIONAL_POINTER(tag, st, m, fd, ltype, ptr)
  412. #define PB_OPTEXT_CALLBACK(tag, st, m, fd, ltype, ptr) \
  413. PB_OPTIONAL_CALLBACK(tag, st, m, fd, ltype, ptr)
  414. /* The mapping from protobuf types to LTYPEs is done using these macros. */
  415. #define PB_LTYPE_MAP_BOOL PB_LTYPE_VARINT
  416. #define PB_LTYPE_MAP_BYTES PB_LTYPE_BYTES
  417. #define PB_LTYPE_MAP_DOUBLE PB_LTYPE_FIXED64
  418. #define PB_LTYPE_MAP_ENUM PB_LTYPE_VARINT
  419. #define PB_LTYPE_MAP_UENUM PB_LTYPE_UVARINT
  420. #define PB_LTYPE_MAP_FIXED32 PB_LTYPE_FIXED32
  421. #define PB_LTYPE_MAP_FIXED64 PB_LTYPE_FIXED64
  422. #define PB_LTYPE_MAP_FLOAT PB_LTYPE_FIXED32
  423. #define PB_LTYPE_MAP_INT32 PB_LTYPE_VARINT
  424. #define PB_LTYPE_MAP_INT64 PB_LTYPE_VARINT
  425. #define PB_LTYPE_MAP_MESSAGE PB_LTYPE_SUBMESSAGE
  426. #define PB_LTYPE_MAP_SFIXED32 PB_LTYPE_FIXED32
  427. #define PB_LTYPE_MAP_SFIXED64 PB_LTYPE_FIXED64
  428. #define PB_LTYPE_MAP_SINT32 PB_LTYPE_SVARINT
  429. #define PB_LTYPE_MAP_SINT64 PB_LTYPE_SVARINT
  430. #define PB_LTYPE_MAP_STRING PB_LTYPE_STRING
  431. #define PB_LTYPE_MAP_UINT32 PB_LTYPE_UVARINT
  432. #define PB_LTYPE_MAP_UINT64 PB_LTYPE_UVARINT
  433. #define PB_LTYPE_MAP_EXTENSION PB_LTYPE_EXTENSION
  434. #define PB_LTYPE_MAP_FIXED_LENGTH_BYTES PB_LTYPE_FIXED_LENGTH_BYTES
  435. /* This is the actual macro used in field descriptions.
  436. * It takes these arguments:
  437. * - Field tag number
  438. * - Field type: BOOL, BYTES, DOUBLE, ENUM, UENUM, FIXED32, FIXED64,
  439. * FLOAT, INT32, INT64, MESSAGE, SFIXED32, SFIXED64
  440. * SINT32, SINT64, STRING, UINT32, UINT64 or EXTENSION
  441. * - Field rules: REQUIRED, OPTIONAL or REPEATED
  442. * - Allocation: STATIC, CALLBACK or POINTER
  443. * - Placement: FIRST or OTHER, depending on if this is the first field in structure.
  444. * - Message name
  445. * - Field name
  446. * - Previous field name (or field name again for first field)
  447. * - Pointer to default value or submsg fields.
  448. */
  449. #define PB_FIELD(tag, type, rules, allocation, placement, message, field, prevfield, ptr) \
  450. PB_ ## rules ## _ ## allocation(tag, message, field, \
  451. PB_DATAOFFSET_ ## placement(message, field, prevfield), \
  452. PB_LTYPE_MAP_ ## type, ptr)
  453. /* Field description for repeated static fixed count fields.*/
  454. #define PB_REPEATED_FIXED_COUNT(tag, type, placement, message, field, prevfield, ptr) \
  455. {tag, PB_ATYPE_STATIC | PB_HTYPE_REPEATED | PB_LTYPE_MAP_ ## type, \
  456. PB_DATAOFFSET_ ## placement(message, field, prevfield), \
  457. 0, \
  458. pb_membersize(message, field[0]), \
  459. pb_arraysize(message, field), ptr}
  460. /* Field description for oneof fields. This requires taking into account the
  461. * union name also, that's why a separate set of macros is needed.
  462. */
  463. #define PB_ONEOF_STATIC(u, tag, st, m, fd, ltype, ptr) \
  464. {tag, PB_ATYPE_STATIC | PB_HTYPE_ONEOF | ltype, \
  465. fd, pb_delta(st, which_ ## u, u.m), \
  466. pb_membersize(st, u.m), 0, ptr}
  467. #define PB_ONEOF_POINTER(u, tag, st, m, fd, ltype, ptr) \
  468. {tag, PB_ATYPE_POINTER | PB_HTYPE_ONEOF | ltype, \
  469. fd, pb_delta(st, which_ ## u, u.m), \
  470. pb_membersize(st, u.m[0]), 0, ptr}
  471. #define PB_ONEOF_FIELD(union_name, tag, type, rules, allocation, placement, message, field, prevfield, ptr) \
  472. PB_ONEOF_ ## allocation(union_name, tag, message, field, \
  473. PB_DATAOFFSET_ ## placement(message, union_name.field, prevfield), \
  474. PB_LTYPE_MAP_ ## type, ptr)
  475. #define PB_ANONYMOUS_ONEOF_STATIC(u, tag, st, m, fd, ltype, ptr) \
  476. {tag, PB_ATYPE_STATIC | PB_HTYPE_ONEOF | ltype, \
  477. fd, pb_delta(st, which_ ## u, m), \
  478. pb_membersize(st, m), 0, ptr}
  479. #define PB_ANONYMOUS_ONEOF_POINTER(u, tag, st, m, fd, ltype, ptr) \
  480. {tag, PB_ATYPE_POINTER | PB_HTYPE_ONEOF | ltype, \
  481. fd, pb_delta(st, which_ ## u, m), \
  482. pb_membersize(st, m[0]), 0, ptr}
  483. #define PB_ANONYMOUS_ONEOF_FIELD(union_name, tag, type, rules, allocation, placement, message, field, prevfield, ptr) \
  484. PB_ANONYMOUS_ONEOF_ ## allocation(union_name, tag, message, field, \
  485. PB_DATAOFFSET_ ## placement(message, field, prevfield), \
  486. PB_LTYPE_MAP_ ## type, ptr)
  487. /* These macros are used for giving out error messages.
  488. * They are mostly a debugging aid; the main error information
  489. * is the true/false return value from functions.
  490. * Some code space can be saved by disabling the error
  491. * messages if not used.
  492. *
  493. * PB_SET_ERROR() sets the error message if none has been set yet.
  494. * msg must be a constant string literal.
  495. * PB_GET_ERROR() always returns a pointer to a string.
  496. * PB_RETURN_ERROR() sets the error and returns false from current
  497. * function.
  498. */
  499. #ifdef PB_NO_ERRMSG
  500. #define PB_SET_ERROR(stream, msg) PB_UNUSED(stream)
  501. #define PB_GET_ERROR(stream) "(errmsg disabled)"
  502. #else
  503. #define PB_SET_ERROR(stream, msg) (stream->errmsg = (stream)->errmsg ? (stream)->errmsg : (msg))
  504. #define PB_GET_ERROR(stream) ((stream)->errmsg ? (stream)->errmsg : "(none)")
  505. #endif
  506. #define PB_RETURN_ERROR(stream, msg) return PB_SET_ERROR(stream, msg), false
  507. #endif