Improvements, array support
[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> // size_t
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 /* Lightweight input stream.
16  * If buf is NULL, read but don't store bytes.
17  * You have to provide a callback function for reading.
18  * You can use state to store your own data (e.g. buffer pointer),
19  * and rely on pb_read to verify that no-body reads past bytes_left.
20  * However, substreams may change bytes_left so don't use that to
21  * compute any pointers.
22  */
23 typedef struct _pb_istream_t pb_istream_t;
24 struct _pb_istream_t
25 {
26     bool (*callback)(pb_istream_t *stream, uint8_t *buf, size_t count);
27     void *state; // Free field for use by callback implementation
28     size_t bytes_left;
29 };
30
31 static inline bool pb_read(pb_istream_t *stream, uint8_t *buf, size_t count)
32 {
33     if (stream->bytes_left < count)
34         return false;
35     
36     bool status = stream->callback(stream, buf, count);
37     stream->bytes_left -= count;
38     return status;
39 }
40
41 /* Lightweight output stream. */
42 typedef struct _pb_ostream_t pb_ostream_t;
43 struct _pb_ostream_t
44 {
45     bool (*callback)(pb_ostream_t *stream, const uint8_t *buf, size_t count);
46     void *state; // Free field for use by callback implementation
47     size_t bytes_written;
48 };
49
50 static inline bool pb_write(pb_ostream_t *stream, const uint8_t *buf, size_t count)
51 {
52     bool status = stream->callback(stream, buf, count);
53     stream->bytes_written += count;
54     return status;
55 }
56
57 /* List of possible field types
58  * Least-significant 4 bits tell the scalar type
59  * Most-significant 4 bits specify repeated/required/packed etc.
60  * 
61  * INT32 and UINT32 are treated the same, as are (U)INT64 and (S)FIXED*
62  * These types are simply casted to correct field type when they are
63  * assigned to the memory pointer.
64  * SINT* is different, though, because it is zig-zag coded.
65  */
66
67 typedef enum {
68     // Standard integer types
69     PB_LTYPE_UINT32 = 0x00,
70     PB_LTYPE_INT32 = 0x00,
71     PB_LTYPE_SINT32 = 0x01,
72     PB_LTYPE_FIXED32 = 0x02,
73     PB_LTYPE_SFIXED32 = 0x02,
74     PB_LTYPE_UINT64 = 0x03,
75     PB_LTYPE_INT64 = 0x03,
76     PB_LTYPE_SINT64 = 0x04,
77     PB_LTYPE_FIXED64 = 0x05,
78     PB_LTYPE_SFIXED64 = 0x05,
79     PB_LTYPE_BOOL = 0x06,
80     PB_LTYPE_ENUM = 0x07,
81     
82     // Standard float types
83     PB_LTYPE_FLOAT = 0x08,
84     PB_LTYPE_DOUBLE = 0x09,
85     
86     // Byte array with pre-allocated buffer.
87     // data_size is the length of the allocated PB_BYTES_ARRAY structure.
88     PB_LTYPE_BYTES = 0x0A,
89     
90     // String with pre-allocated buffer.
91     // data_size is the maximum length.
92     PB_LTYPE_STRING = 0x0B,
93     
94     // Submessage
95     // submsg_fields is pointer to field descriptions
96     PB_LTYPE_SUBMESSAGE = 0x0C,
97     
98     /////////////
99     // Modifier flags
100     
101     // Just the basic, write data at data_offset
102     PB_HTYPE_REQUIRED = 0x00,
103     
104     // Write true at size_offset
105     PB_HTYPE_OPTIONAL = 0x10,
106     
107     // Read to pre-allocated array
108     // Maximum number of entries is array_size,
109     // actual number is stored at size_offset
110     PB_HTYPE_ARRAY = 0x20,
111     
112     // Works for all required/optional/repeated fields.
113     // data_offset points to pb_callback_t structure.
114     // LTYPE is ignored.
115     PB_HTYPE_CALLBACK = 0x30
116 } pb_packed pb_type_t;
117
118 #define PB_HTYPE(x) ((x) & 0xF0)
119 #define PB_LTYPE(x) ((x) & 0x0F)
120
121 /* This structure is used in auto-generated constants
122  * to specify struct fields.
123  * You can change field sizes here if you need structures
124  * larger than 256 bytes or field tags larger than 256.
125  * The compiler should complain if your .proto has such
126  * structures ("initializer too large for type").
127  */
128 typedef struct _pb_field_t pb_field_t;
129 struct _pb_field_t {
130     uint8_t tag;
131     pb_type_t type;
132     uint8_t data_offset; // Offset of actual data or array start
133     uint8_t size_offset; // Offset of array size or has-boolean
134     uint8_t data_size; // Data size in bytes for a single item
135     uint8_t array_size; // Maximum number of entries in array
136     
137     // Field definitions for submessage
138     // OR default value for all other non-array, non-callback types
139     // If null, then field will zeroed.
140     const void *ptr;
141 } pb_packed;
142
143 #define PB_LAST_FIELD {0,0,0,0}
144
145 // This structure is used for 'bytes' arrays.
146 // It has the number of bytes in the beginning, and after that an array.
147 #define PB_BYTES_ARRAY(buffersize) \
148 struct { \
149     size_t size; \
150     uint8_t bytes[buffersize]; \
151 }
152
153 typedef PB_BYTES_ARRAY(1) pb_bytes_array_t;
154
155 // This structure is used for giving the callback function.
156 //
157 // The decoding callback will be given a limited-length stream
158 // If the wire type was string, the length is the length of the string.
159 // If the wire type was a varint/fixed32/fixed64, the length is the length
160 // of the actual value.
161 // The function may be called multiple times (especially for repeated types,
162 // but also otherwise if the message happens to contain the field multiple
163 // times.)
164 //
165 // The encoding callback will receive the actual output stream.
166 // It should write all the data in one call, including the field tag and
167 // wire type. It can write multiple fields.
168 typedef struct _pb_callback_t pb_callback_t;
169 struct _pb_callback_t {
170     union {
171         bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void *arg);
172         bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, void *arg);
173     } funcs;
174     
175     // Free arg for use by callback
176     void *arg;
177 };
178
179 #endif