You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

583 lines
22 KiB

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