Encoder
[apps/agl-service-can-low-level.git] / pb.h
1 #ifndef _PB_H_
2 #define _PB_H_
3
4 #include <stdint.h>
5 #include <stddef.h>
6 #include <stdbool.h>
7
8 #ifdef __GNUC__
9 /* This just reduces memory requirements, but is not required. */
10 #define pb_packed __attribute__((packed))
11 #else
12 #define pb_packed
13 #endif
14
15 /* Wire types */
16 typedef enum {
17     PB_WT_VARINT = 0,
18     PB_WT_64BIT  = 1,
19     PB_WT_STRING = 2,
20     PB_WT_32BIT  = 5
21 } pb_wire_type_t;
22
23 /* List of possible field types
24  * Least-significant 4 bits tell the scalar type
25  * Most-significant 4 bits specify repeated/required/packed etc.
26  * 
27  * INT32 and UINT32 are treated the same, as are (U)INT64 and (S)FIXED*
28  * These types are simply casted to correct field type when they are
29  * assigned to the memory pointer.
30  * SINT* is different, though, because it is zig-zag coded.
31  */
32
33 typedef enum {
34     /************************
35      * Field contents types *
36      ************************/
37     
38     /* Numeric types */
39     PB_LTYPE_VARINT = 0x00, /* int32, uint32, int64, uint64, bool, enum */
40     PB_LTYPE_SVARINT = 0x01, /* sint32, sint64 */
41     PB_LTYPE_FIXED = 0x02, /* fixed32, sfixed32, fixed64, sfixed64, float, double */
42     
43     /* Marker for last packable field type. */
44     PB_LTYPE_LAST_PACKABLE = 0x02,
45     
46     /* Byte array with pre-allocated buffer.
47      * data_size is the length of the allocated PB_BYTES_ARRAY structure. */
48     PB_LTYPE_BYTES = 0x03,
49     
50     /* String with pre-allocated buffer.
51      * data_size is the maximum length. */
52     PB_LTYPE_STRING = 0x04,
53     
54     /* Submessage
55      * submsg_fields is pointer to field descriptions */
56     PB_LTYPE_SUBMESSAGE = 0x05,
57     
58     /* Number of declared LTYPES */
59     PB_LTYPES_COUNT = 6,
60     
61     /******************
62      * Modifier flags *
63      ******************/
64     
65     /* Just the basic, write data at data_offset */
66     PB_HTYPE_REQUIRED = 0x00,
67     
68     /* Write true at size_offset */
69     PB_HTYPE_OPTIONAL = 0x10,
70     
71     /* Read to pre-allocated array
72      * Maximum number of entries is array_size,
73      * actual number is stored at size_offset */
74     PB_HTYPE_ARRAY = 0x20,
75     
76     /* Works for all required/optional/repeated fields.
77      * data_offset points to pb_callback_t structure.
78      * LTYPE should be 0 (it is ignored, but sometimes
79      * used to speculatively index an array). */
80     PB_HTYPE_CALLBACK = 0x30
81 } pb_packed pb_type_t;
82
83 #define PB_HTYPE(x) ((x) & 0xF0)
84 #define PB_LTYPE(x) ((x) & 0x0F)
85
86 /* This structure is used in auto-generated constants
87  * to specify struct fields.
88  * You can change field sizes here if you need structures
89  * larger than 256 bytes or field tags larger than 256.
90  * The compiler should complain if your .proto has such
91  * structures ("initializer too large for type").
92  */
93 typedef struct _pb_field_t pb_field_t;
94 struct _pb_field_t {
95     uint8_t tag;
96     pb_type_t type;
97     uint8_t data_offset; /* Offset of field data, relative to previous field. */
98     int8_t size_offset; /* Offset of array size or has-boolean, relative to data */
99     uint8_t data_size; /* Data size in bytes for a single item */
100     uint8_t array_size; /* Maximum number of entries in array */
101     
102     /* Field definitions for submessage
103      * OR default value for all other non-array, non-callback types
104      * If null, then field will zeroed. */
105     const void *ptr;
106 } pb_packed;
107
108 /* This structure is used for 'bytes' arrays.
109  * It has the number of bytes in the beginning, and after that an array.
110  * Note that actual structs used will have a different length of bytes array.
111  */
112 typedef struct {
113     size_t size;
114     uint8_t bytes[1];
115 } pb_bytes_array_t;
116
117 /* This structure is used for giving the callback function.
118  * It is stored in the message structure and filled in by the method that
119  * calls pb_decode.
120  *
121  * The decoding callback will be given a limited-length stream
122  * If the wire type was string, the length is the length of the string.
123  * If the wire type was a varint/fixed32/fixed64, the length is the length
124  * of the actual value.
125  * The function may be called multiple times (especially for repeated types,
126  * but also otherwise if the message happens to contain the field multiple
127  * times.)
128  *
129  * The encoding callback will receive the actual output stream.
130  * It should write all the data in one call, including the field tag and
131  * wire type. It can write multiple fields.
132  */
133 typedef struct _pb_istream_t pb_istream_t;
134 typedef struct _pb_ostream_t pb_ostream_t;
135 typedef struct _pb_callback_t pb_callback_t;
136 struct _pb_callback_t {
137     union {
138         bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void *arg);
139         bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, const void *arg);
140     } funcs;
141     
142     /* Free arg for use by callback */
143     void *arg;
144 };
145
146 /* These macros are used to declare pb_field_t's in the constant array. */
147 #define pb_membersize(st, m) (sizeof ((st*)0)->m)
148 #define pb_arraysize(st, m) (pb_membersize(st, m) / pb_membersize(st, m[0]))
149 #define pb_delta(st, m1, m2) ((int)offsetof(st, m1) - (int)offsetof(st, m2))
150 #define PB_LAST_FIELD {0,0,0,0}
151
152
153 #endif