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