9 /* This just reduces memory requirements, but is not required. */
10 #define pb_packed __attribute__((packed))
15 /* Lightweight output stream. */
16 typedef struct _pb_ostream_t pb_ostream_t;
19 bool (*callback)(pb_ostream_t *stream, const uint8_t *buf, size_t count);
20 void *state; /* Free field for use by callback implementation */
24 /*static inline bool pb_write(pb_ostream_t *stream, const uint8_t *buf, size_t count)
26 bool status = stream->callback(stream, buf, count);
27 stream->bytes_written += count;
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.
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.
42 /************************
43 * Field contents types *
44 ************************/
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,
60 /* Standard float types */
61 PB_LTYPE_FLOAT = 0x08,
62 PB_LTYPE_DOUBLE = 0x09,
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,
68 /* String with pre-allocated buffer.
69 * data_size is the maximum length. */
70 PB_LTYPE_STRING = 0x0B,
73 * submsg_fields is pointer to field descriptions */
74 PB_LTYPE_SUBMESSAGE = 0x0C,
80 /* Just the basic, write data at data_offset */
81 PB_HTYPE_REQUIRED = 0x00,
83 /* Write true at size_offset */
84 PB_HTYPE_OPTIONAL = 0x10,
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,
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;
97 #define PB_HTYPE(x) ((x) & 0xF0)
98 #define PB_LTYPE(x) ((x) & 0x0F)
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").
107 typedef struct _pb_field_t pb_field_t;
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 */
116 /* Field definitions for submessage
117 * OR default value for all other non-array, non-callback types
118 * If null, then field will zeroed. */
122 #define PB_LAST_FIELD {0,0,0,0}
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) \
129 uint8_t bytes[buffersize]; \
132 typedef PB_BYTES_ARRAY(1) pb_bytes_array_t;
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
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
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.
150 typedef struct _pb_istream_t pb_istream_t;
151 typedef struct _pb_callback_t pb_callback_t;
152 struct _pb_callback_t {
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);
158 /* Free arg for use by callback */