• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {0, (pb_type_t)0, 0, 0, 0, 0, 0}
408 
409 /* Macros for filling in the data_offset field */
410 /* data_offset for first field in a message */
411 #define PB_DATAOFFSET_FIRST(st, m1, m2) (offsetof(st, m1))
412 /* data_offset for subsequent fields */
413 #define PB_DATAOFFSET_OTHER(st, m1, m2) \
414   (offsetof(st, m1) - offsetof(st, m2) - pb_membersize(st, m2))
415 /* data offset for subsequent fields inside an union (oneof) */
416 #define PB_DATAOFFSET_UNION(st, m1, m2) (PB_SIZE_MAX)
417 /* Choose first/other based on m1 == m2 (deprecated, remains for backwards
418  * compatibility) */
419 #define PB_DATAOFFSET_CHOOSE(st, m1, m2)      \
420   (int)(offsetof(st, m1) == offsetof(st, m2)  \
421             ? PB_DATAOFFSET_FIRST(st, m1, m2) \
422             : PB_DATAOFFSET_OTHER(st, m1, m2))
423 
424 /* Required fields are the simplest. They just have delta (padding) from
425  * previous field end, and the size of the field. Pointer is used for
426  * submessages and default values.
427  */
428 #define PB_REQUIRED_STATIC(tag, st, m, fd, ltype, ptr) \
429   {tag,                                                \
430    PB_ATYPE_STATIC | PB_HTYPE_REQUIRED | ltype,        \
431    fd,                                                 \
432    0,                                                  \
433    pb_membersize(st, m),                               \
434    0,                                                  \
435    ptr}
436 
437 /* Optional fields add the delta to the has_ variable. */
438 #define PB_OPTIONAL_STATIC(tag, st, m, fd, ltype, ptr) \
439   {tag,                                                \
440    PB_ATYPE_STATIC | PB_HTYPE_OPTIONAL | ltype,        \
441    fd,                                                 \
442    pb_delta(st, has_##m, m),                           \
443    pb_membersize(st, m),                               \
444    0,                                                  \
445    ptr}
446 
447 #define PB_SINGULAR_STATIC(tag, st, m, fd, ltype, ptr) \
448   {tag,                                                \
449    PB_ATYPE_STATIC | PB_HTYPE_OPTIONAL | ltype,        \
450    fd,                                                 \
451    0,                                                  \
452    pb_membersize(st, m),                               \
453    0,                                                  \
454    ptr}
455 
456 /* Repeated fields have a _count field and also the maximum number of entries.
457  */
458 #define PB_REPEATED_STATIC(tag, st, m, fd, ltype, ptr) \
459   {tag,                                                \
460    PB_ATYPE_STATIC | PB_HTYPE_REPEATED | ltype,        \
461    fd,                                                 \
462    pb_delta(st, m##_count, m),                         \
463    pb_membersize(st, m[0]),                            \
464    pb_arraysize(st, m),                                \
465    ptr}
466 
467 /* Allocated fields carry the size of the actual data, not the pointer */
468 #define PB_REQUIRED_POINTER(tag, st, m, fd, ltype, ptr) \
469   {tag,                                                 \
470    PB_ATYPE_POINTER | PB_HTYPE_REQUIRED | ltype,        \
471    fd,                                                  \
472    0,                                                   \
473    pb_membersize(st, m[0]),                             \
474    0,                                                   \
475    ptr}
476 
477 /* Optional fields don't need a has_ variable, as information would be redundant
478  */
479 #define PB_OPTIONAL_POINTER(tag, st, m, fd, ltype, ptr) \
480   {tag,                                                 \
481    PB_ATYPE_POINTER | PB_HTYPE_OPTIONAL | ltype,        \
482    fd,                                                  \
483    0,                                                   \
484    pb_membersize(st, m[0]),                             \
485    0,                                                   \
486    ptr}
487 
488 /* Same as optional fields*/
489 #define PB_SINGULAR_POINTER(tag, st, m, fd, ltype, ptr) \
490   {tag,                                                 \
491    PB_ATYPE_POINTER | PB_HTYPE_OPTIONAL | ltype,        \
492    fd,                                                  \
493    0,                                                   \
494    pb_membersize(st, m[0]),                             \
495    0,                                                   \
496    ptr}
497 
498 /* Repeated fields have a _count field and a pointer to array of pointers */
499 #define PB_REPEATED_POINTER(tag, st, m, fd, ltype, ptr) \
500   {tag,                                                 \
501    PB_ATYPE_POINTER | PB_HTYPE_REPEATED | ltype,        \
502    fd,                                                  \
503    pb_delta(st, m##_count, m),                          \
504    pb_membersize(st, m[0]),                             \
505    0,                                                   \
506    ptr}
507 
508 /* Callbacks are much like required fields except with special datatype. */
509 #define PB_REQUIRED_CALLBACK(tag, st, m, fd, ltype, ptr) \
510   {tag,                                                  \
511    PB_ATYPE_CALLBACK | PB_HTYPE_REQUIRED | ltype,        \
512    fd,                                                   \
513    0,                                                    \
514    pb_membersize(st, m),                                 \
515    0,                                                    \
516    ptr}
517 
518 #define PB_OPTIONAL_CALLBACK(tag, st, m, fd, ltype, ptr) \
519   {tag,                                                  \
520    PB_ATYPE_CALLBACK | PB_HTYPE_OPTIONAL | ltype,        \
521    fd,                                                   \
522    0,                                                    \
523    pb_membersize(st, m),                                 \
524    0,                                                    \
525    ptr}
526 
527 #define PB_SINGULAR_CALLBACK(tag, st, m, fd, ltype, ptr) \
528   {tag,                                                  \
529    PB_ATYPE_CALLBACK | PB_HTYPE_OPTIONAL | ltype,        \
530    fd,                                                   \
531    0,                                                    \
532    pb_membersize(st, m),                                 \
533    0,                                                    \
534    ptr}
535 
536 #define PB_REPEATED_CALLBACK(tag, st, m, fd, ltype, ptr) \
537   {tag,                                                  \
538    PB_ATYPE_CALLBACK | PB_HTYPE_REPEATED | ltype,        \
539    fd,                                                   \
540    0,                                                    \
541    pb_membersize(st, m),                                 \
542    0,                                                    \
543    ptr}
544 
545 /* Optional extensions don't have the has_ field, as that would be redundant.
546  * Furthermore, the combination of OPTIONAL without has_ field is used
547  * for indicating proto3 style fields. Extensions exist in proto2 mode only,
548  * so they should be encoded according to proto2 rules. To avoid the conflict,
549  * extensions are marked as REQUIRED instead.
550  */
551 #define PB_OPTEXT_STATIC(tag, st, m, fd, ltype, ptr) \
552   {tag,                                              \
553    PB_ATYPE_STATIC | PB_HTYPE_REQUIRED | ltype,      \
554    0,                                                \
555    0,                                                \
556    pb_membersize(st, m),                             \
557    0,                                                \
558    ptr}
559 
560 #define PB_OPTEXT_POINTER(tag, st, m, fd, ltype, ptr) \
561   PB_OPTIONAL_POINTER(tag, st, m, fd, ltype, ptr)
562 
563 #define PB_OPTEXT_CALLBACK(tag, st, m, fd, ltype, ptr) \
564   PB_OPTIONAL_CALLBACK(tag, st, m, fd, ltype, ptr)
565 
566 /* The mapping from protobuf types to LTYPEs is done using these macros. */
567 #define PB_LTYPE_MAP_BOOL PB_LTYPE_BOOL
568 #define PB_LTYPE_MAP_BYTES PB_LTYPE_BYTES
569 #define PB_LTYPE_MAP_DOUBLE PB_LTYPE_FIXED64
570 #define PB_LTYPE_MAP_ENUM PB_LTYPE_VARINT
571 #define PB_LTYPE_MAP_UENUM PB_LTYPE_UVARINT
572 #define PB_LTYPE_MAP_FIXED32 PB_LTYPE_FIXED32
573 #define PB_LTYPE_MAP_FIXED64 PB_LTYPE_FIXED64
574 #define PB_LTYPE_MAP_FLOAT PB_LTYPE_FIXED32
575 #define PB_LTYPE_MAP_INT32 PB_LTYPE_VARINT
576 #define PB_LTYPE_MAP_INT64 PB_LTYPE_VARINT
577 #define PB_LTYPE_MAP_MESSAGE PB_LTYPE_SUBMESSAGE
578 #define PB_LTYPE_MAP_SFIXED32 PB_LTYPE_FIXED32
579 #define PB_LTYPE_MAP_SFIXED64 PB_LTYPE_FIXED64
580 #define PB_LTYPE_MAP_SINT32 PB_LTYPE_SVARINT
581 #define PB_LTYPE_MAP_SINT64 PB_LTYPE_SVARINT
582 #define PB_LTYPE_MAP_STRING PB_LTYPE_STRING
583 #define PB_LTYPE_MAP_UINT32 PB_LTYPE_UVARINT
584 #define PB_LTYPE_MAP_UINT64 PB_LTYPE_UVARINT
585 #define PB_LTYPE_MAP_EXTENSION PB_LTYPE_EXTENSION
586 #define PB_LTYPE_MAP_FIXED_LENGTH_BYTES PB_LTYPE_FIXED_LENGTH_BYTES
587 
588 /* This is the actual macro used in field descriptions.
589  * It takes these arguments:
590  * - Field tag number
591  * - Field type:   BOOL, BYTES, DOUBLE, ENUM, UENUM, FIXED32, FIXED64,
592  *                 FLOAT, INT32, INT64, MESSAGE, SFIXED32, SFIXED64
593  *                 SINT32, SINT64, STRING, UINT32, UINT64 or EXTENSION
594  * - Field rules:  REQUIRED, OPTIONAL or REPEATED
595  * - Allocation:   STATIC, CALLBACK or POINTER
596  * - Placement: FIRST or OTHER, depending on if this is the first field in
597  * structure.
598  * - Message name
599  * - Field name
600  * - Previous field name (or field name again for first field)
601  * - Pointer to default value or submsg fields.
602  */
603 
604 #define PB_FIELD(tag, type, rules, allocation, placement, message, field, \
605                  prevfield, ptr)                                          \
606   PB_##rules##_##allocation(                                              \
607       tag, message, field,                                                \
608       PB_DATAOFFSET_##placement(message, field, prevfield),               \
609       PB_LTYPE_MAP_##type, ptr)
610 
611 /* Field description for repeated static fixed count fields.*/
612 #define PB_REPEATED_FIXED_COUNT(tag, type, placement, message, field, \
613                                 prevfield, ptr)                       \
614   {tag,                                                               \
615    PB_ATYPE_STATIC | PB_HTYPE_REPEATED | PB_LTYPE_MAP_##type,         \
616    PB_DATAOFFSET_##placement(message, field, prevfield),              \
617    0,                                                                 \
618    pb_membersize(message, field[0]),                                  \
619    pb_arraysize(message, field),                                      \
620    ptr}
621 
622 /* Field description for oneof fields. This requires taking into account the
623  * union name also, that's why a separate set of macros is needed.
624  */
625 #define PB_ONEOF_STATIC(u, tag, st, m, fd, ltype, ptr) \
626   {tag,                                                \
627    PB_ATYPE_STATIC | PB_HTYPE_ONEOF | ltype,           \
628    fd,                                                 \
629    pb_delta(st, which_##u, u.m),                       \
630    pb_membersize(st, u.m),                             \
631    0,                                                  \
632    ptr}
633 
634 #define PB_ONEOF_POINTER(u, tag, st, m, fd, ltype, ptr) \
635   {tag,                                                 \
636    PB_ATYPE_POINTER | PB_HTYPE_ONEOF | ltype,           \
637    fd,                                                  \
638    pb_delta(st, which_##u, u.m),                        \
639    pb_membersize(st, u.m[0]),                           \
640    0,                                                   \
641    ptr}
642 
643 #define PB_ONEOF_FIELD(union_name, tag, type, rules, allocation, placement, \
644                        message, field, prevfield, ptr)                      \
645   PB_ONEOF_##allocation(                                                    \
646       union_name, tag, message, field,                                      \
647       PB_DATAOFFSET_##placement(message, union_name.field, prevfield),      \
648       PB_LTYPE_MAP_##type, ptr)
649 
650 #define PB_ANONYMOUS_ONEOF_STATIC(u, tag, st, m, fd, ltype, ptr) \
651   {tag,                                                          \
652    PB_ATYPE_STATIC | PB_HTYPE_ONEOF | ltype,                     \
653    fd,                                                           \
654    pb_delta(st, which_##u, m),                                   \
655    pb_membersize(st, m),                                         \
656    0,                                                            \
657    ptr}
658 
659 #define PB_ANONYMOUS_ONEOF_POINTER(u, tag, st, m, fd, ltype, ptr) \
660   {tag,                                                           \
661    PB_ATYPE_POINTER | PB_HTYPE_ONEOF | ltype,                     \
662    fd,                                                            \
663    pb_delta(st, which_##u, m),                                    \
664    pb_membersize(st, m[0]),                                       \
665    0,                                                             \
666    ptr}
667 
668 #define PB_ANONYMOUS_ONEOF_FIELD(union_name, tag, type, rules, allocation,  \
669                                  placement, message, field, prevfield, ptr) \
670   PB_ANONYMOUS_ONEOF_##allocation(                                          \
671       union_name, tag, message, field,                                      \
672       PB_DATAOFFSET_##placement(message, field, prevfield),                 \
673       PB_LTYPE_MAP_##type, ptr)
674 
675 /* These macros are used for giving out error messages.
676  * They are mostly a debugging aid; the main error information
677  * is the true/false return value from functions.
678  * Some code space can be saved by disabling the error
679  * messages if not used.
680  *
681  * PB_SET_ERROR() sets the error message if none has been set yet.
682  *                msg must be a constant string literal.
683  * PB_GET_ERROR() always returns a pointer to a string.
684  * PB_RETURN_ERROR() sets the error and returns false from current
685  *                   function.
686  */
687 #ifdef PB_NO_ERRMSG
688 #define PB_SET_ERROR(stream, msg) PB_UNUSED(stream)
689 #define PB_GET_ERROR(stream) "(errmsg disabled)"
690 #else
691 #define PB_SET_ERROR(stream, msg) \
692   (stream->errmsg = (stream)->errmsg ? (stream)->errmsg : (msg))
693 #define PB_GET_ERROR(stream) ((stream)->errmsg ? (stream)->errmsg : "(none)")
694 #endif
695 
696 #define PB_RETURN_ERROR(stream, msg) return PB_SET_ERROR(stream, msg), false
697 
698 #endif
699