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