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