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