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 ************************/
47 PB_LTYPE_VARINT = 0x00, /* int32, uint32, int64, uint64, bool, enum */
48 PB_LTYPE_SVARINT = 0x01, /* sint32, sint64 */
49 PB_LTYPE_FIXED = 0x02, /* fixed32, sfixed32, fixed64, sfixed64, float, double */
51 /* Marker for last packable field type. */
52 PB_LTYPE_LAST_PACKABLE = 0x02,
54 /* Byte array with pre-allocated buffer.
55 * data_size is the length of the allocated PB_BYTES_ARRAY structure. */
56 PB_LTYPE_BYTES = 0x03,
58 /* String with pre-allocated buffer.
59 * data_size is the maximum length. */
60 PB_LTYPE_STRING = 0x04,
63 * submsg_fields is pointer to field descriptions */
64 PB_LTYPE_SUBMESSAGE = 0x05,
66 /* Number of declared LTYPES */
73 /* Just the basic, write data at data_offset */
74 PB_HTYPE_REQUIRED = 0x00,
76 /* Write true at size_offset */
77 PB_HTYPE_OPTIONAL = 0x10,
79 /* Read to pre-allocated array
80 * Maximum number of entries is array_size,
81 * actual number is stored at size_offset */
82 PB_HTYPE_ARRAY = 0x20,
84 /* Works for all required/optional/repeated fields.
85 * data_offset points to pb_callback_t structure.
86 * LTYPE is ignored. */
87 PB_HTYPE_CALLBACK = 0x30
88 } pb_packed pb_type_t;
90 #define PB_HTYPE(x) ((x) & 0xF0)
91 #define PB_LTYPE(x) ((x) & 0x0F)
93 /* This structure is used in auto-generated constants
94 * to specify struct fields.
95 * You can change field sizes here if you need structures
96 * larger than 256 bytes or field tags larger than 256.
97 * The compiler should complain if your .proto has such
98 * structures ("initializer too large for type").
100 typedef struct _pb_field_t pb_field_t;
104 uint8_t data_offset; /* Offset of field data, relative to previous field. */
105 int8_t size_offset; /* Offset of array size or has-boolean, relative to data */
106 uint8_t data_size; /* Data size in bytes for a single item */
107 uint8_t array_size; /* Maximum number of entries in array */
109 /* Field definitions for submessage
110 * OR default value for all other non-array, non-callback types
111 * If null, then field will zeroed. */
115 /* This structure is used for 'bytes' arrays.
116 * It has the number of bytes in the beginning, and after that an array. */
117 #define PB_BYTES_ARRAY(buffersize) \
120 uint8_t bytes[buffersize]; \
123 typedef PB_BYTES_ARRAY(1) pb_bytes_array_t;
125 /* This structure is used for giving the callback function.
126 * It is stored in the message structure and filled in by the method that
129 * The decoding callback will be given a limited-length stream
130 * If the wire type was string, the length is the length of the string.
131 * If the wire type was a varint/fixed32/fixed64, the length is the length
132 * of the actual value.
133 * The function may be called multiple times (especially for repeated types,
134 * but also otherwise if the message happens to contain the field multiple
137 * The encoding callback will receive the actual output stream.
138 * It should write all the data in one call, including the field tag and
139 * wire type. It can write multiple fields.
141 typedef struct _pb_istream_t pb_istream_t;
142 typedef struct _pb_callback_t pb_callback_t;
143 struct _pb_callback_t {
145 bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void *arg);
146 bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, void *arg);
149 /* Free arg for use by callback */
153 /* These macros are used to declare pb_field_t's in the constant array. */
154 #define pb_membersize(st, m) (sizeof ((st*)0)->m)
155 #define pb_arraysize(st, m) (pb_membersize(st, m) / pb_membersize(st, m[0]))
156 #define pb_delta(st, m1, m2) ((int)offsetof(st, m1) - (int)offsetof(st, m2))
157 #define PB_LAST_FIELD {0,0,0,0}