4 /* pb.h: Common parts for nanopb library.
5 * Most of these are quite low-level stuff. For the high-level interface,
6 * see pb_encode.h or pb_decode.h
14 /* This just reduces memory requirements, but is not required. */
15 #define pb_packed __attribute__((packed))
20 /* List of possible field types. These are used in the autogenerated code.
21 * Least-significant 4 bits tell the scalar type
22 * Most-significant 4 bits specify repeated/required/packed etc.
24 * INT32 and UINT32 are treated the same, as are (U)INT64 and (S)FIXED*
25 * These types are simply casted to correct field type when they are
26 * assigned to the memory pointer.
27 * SINT* is different, though, because it is zig-zag coded.
31 /************************
32 * Field contents types *
33 ************************/
36 PB_LTYPE_VARINT = 0x00, /* int32, uint32, int64, uint64, bool, enum */
37 PB_LTYPE_SVARINT = 0x01, /* sint32, sint64 */
38 PB_LTYPE_FIXED = 0x02, /* fixed32, sfixed32, fixed64, sfixed64, float, double */
40 /* Marker for last packable field type. */
41 PB_LTYPE_LAST_PACKABLE = 0x02,
43 /* Byte array with pre-allocated buffer.
44 * data_size is the length of the allocated PB_BYTES_ARRAY structure. */
45 PB_LTYPE_BYTES = 0x03,
47 /* String with pre-allocated buffer.
48 * data_size is the maximum length. */
49 PB_LTYPE_STRING = 0x04,
52 * submsg_fields is pointer to field descriptions */
53 PB_LTYPE_SUBMESSAGE = 0x05,
55 /* Number of declared LTYPES */
62 /* Just the basic, write data at data_offset */
63 PB_HTYPE_REQUIRED = 0x00,
65 /* Write true at size_offset */
66 PB_HTYPE_OPTIONAL = 0x10,
68 /* Read to pre-allocated array
69 * Maximum number of entries is array_size,
70 * actual number is stored at size_offset */
71 PB_HTYPE_ARRAY = 0x20,
73 /* Works for all required/optional/repeated fields.
74 * data_offset points to pb_callback_t structure.
75 * LTYPE should be 0 (it is ignored, but sometimes
76 * used to speculatively index an array). */
77 PB_HTYPE_CALLBACK = 0x30
78 } pb_packed pb_type_t;
80 #define PB_HTYPE(x) ((x) & 0xF0)
81 #define PB_LTYPE(x) ((x) & 0x0F)
83 /* This structure is used in auto-generated constants
84 * to specify struct fields.
85 * You can change field sizes here if you need structures
86 * larger than 256 bytes or field tags larger than 256.
87 * The compiler should complain if your .proto has such
88 * structures ("initializer too large for type").
90 typedef struct _pb_field_t pb_field_t;
94 uint8_t data_offset; /* Offset of field data, relative to previous field. */
95 int8_t size_offset; /* Offset of array size or has-boolean, relative to data */
96 uint8_t data_size; /* Data size in bytes for a single item */
97 uint8_t array_size; /* Maximum number of entries in array */
99 /* Field definitions for submessage
100 * OR default value for all other non-array, non-callback types
101 * If null, then field will zeroed. */
105 /* This structure is used for 'bytes' arrays.
106 * It has the number of bytes in the beginning, and after that an array.
107 * Note that actual structs used will have a different length of bytes array.
114 /* This structure is used for giving the callback function.
115 * It is stored in the message structure and filled in by the method that
118 * The decoding callback will be given a limited-length stream
119 * If the wire type was string, the length is the length of the string.
120 * If the wire type was a varint/fixed32/fixed64, the length is the length
121 * of the actual value.
122 * The function may be called multiple times (especially for repeated types,
123 * but also otherwise if the message happens to contain the field multiple
126 * The encoding callback will receive the actual output stream.
127 * It should write all the data in one call, including the field tag and
128 * wire type. It can write multiple fields.
130 * The callback can be null if you want to skip a field.
132 typedef struct _pb_istream_t pb_istream_t;
133 typedef struct _pb_ostream_t pb_ostream_t;
134 typedef struct _pb_callback_t pb_callback_t;
135 struct _pb_callback_t {
137 bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void *arg);
138 bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, const void *arg);
141 /* Free arg for use by callback */
145 /* Wire types. Library user needs these only in encoder callbacks. */
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_delta_end(st, m1, m2) (offsetof(st, m1) - offsetof(st, m2) - pb_membersize(st, m2))
158 #define PB_LAST_FIELD {0,0,0,0}