2 * blob - library for generating/parsing tagged binary data
4 * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License version 2.1
8 * as published by the Free Software Foundation
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
26 #if __BYTE_ORDER == __LITTLE_ENDIAN
28 #if defined(__linux__) || defined(__CYGWIN__)
32 #elif defined(__APPLE__)
33 #include <machine/endian.h>
34 #include <machine/byte_order.h>
35 #define bswap_16(x) OSSwapInt16(x)
36 #define bswap_32(x) OSSwapInt32(x)
37 #define bswap_64(x) OSSwapInt64(x)
38 #elif defined(__FreeBSD__)
39 #include <sys/endian.h>
40 #define bswap_16(x) bswap16(x)
41 #define bswap_32(x) bswap32(x)
42 #define bswap_64(x) bswap64(x)
44 #include <machine/endian.h>
45 #define bswap_16(x) swap16(x)
46 #define bswap_32(x) swap32(x)
47 #define bswap_64(x) swap64(x)
51 #define __BYTE_ORDER BYTE_ORDER
54 #define __BIG_ENDIAN BIG_ENDIAN
56 #ifndef __LITTLE_ENDIAN
57 #define __LITTLE_ENDIAN LITTLE_ENDIAN
60 #define cpu_to_be64(x) bswap_64(x)
61 #define cpu_to_be32(x) bswap_32(x)
62 #define cpu_to_be16(x) bswap_16(x)
64 #define be64_to_cpu(x) bswap_64(x)
65 #define be32_to_cpu(x) bswap_32(x)
66 #define be16_to_cpu(x) bswap_16(x)
70 #define cpu_to_be64(x) (x)
71 #define cpu_to_be32(x) (x)
72 #define cpu_to_be16(x) (x)
74 #define be64_to_cpu(x) (x)
75 #define be32_to_cpu(x) (x)
76 #define be16_to_cpu(x) (x)
92 #define BLOB_ATTR_ID_MASK 0xff000000
93 #define BLOB_ATTR_ID_SHIFT 24
94 #define BLOB_ATTR_LEN_MASK 0x00ffffff
95 #define BLOB_ATTR_ALIGN 4
98 #define __packed __attribute__((packed))
106 struct blob_attr_info
{
110 bool (*validate
)(const struct blob_attr_info
*, struct blob_attr
*);
114 struct blob_attr
*head
;
115 bool (*grow
)(struct blob_buf
*buf
, int minlen
);
121 * blob_data: returns the data pointer for an attribute
124 blob_data(const struct blob_attr
*attr
)
126 return (void *) attr
->data
;
130 * blob_id: returns the id of an attribute
132 static inline unsigned int
133 blob_id(const struct blob_attr
*attr
)
135 int id
= (be32_to_cpu(attr
->id_len
) & BLOB_ATTR_ID_MASK
) >> BLOB_ATTR_ID_SHIFT
;
140 * blob_len: returns the length of the attribute's payload
142 static inline unsigned int
143 blob_len(const struct blob_attr
*attr
)
145 return (be32_to_cpu(attr
->id_len
) & BLOB_ATTR_LEN_MASK
) - sizeof(struct blob_attr
);
149 * blob_pad_len: returns the complete length of an attribute (including the header)
151 static inline unsigned int
152 blob_raw_len(const struct blob_attr
*attr
)
154 return blob_len(attr
) + sizeof(struct blob_attr
);
158 * blob_pad_len: returns the padded length of an attribute (including the header)
160 static inline unsigned int
161 blob_pad_len(const struct blob_attr
*attr
)
163 int len
= blob_raw_len(attr
);
164 len
= (len
+ BLOB_ATTR_ALIGN
- 1) & ~(BLOB_ATTR_ALIGN
- 1);
169 blob_set_raw_len(struct blob_attr
*attr
, unsigned int len
)
171 int id
= blob_id(attr
);
172 len
&= BLOB_ATTR_LEN_MASK
;
173 len
|= (id
<< BLOB_ATTR_ID_SHIFT
) & BLOB_ATTR_ID_MASK
;
174 attr
->id_len
= cpu_to_be32(len
);
177 static inline uint8_t
178 blob_get_u8(const struct blob_attr
*attr
)
180 return *((uint8_t *) attr
->data
);
183 static inline uint16_t
184 blob_get_u16(const struct blob_attr
*attr
)
186 uint16_t *tmp
= (uint16_t*)attr
->data
;
187 return be16_to_cpu(*tmp
);
190 static inline uint32_t
191 blob_get_u32(const struct blob_attr
*attr
)
193 uint32_t *tmp
= (uint32_t*)attr
->data
;
194 return be32_to_cpu(*tmp
);
197 static inline uint64_t
198 blob_get_u64(const struct blob_attr
*attr
)
200 uint64_t *tmp
= (uint64_t*)attr
->data
;
201 return be64_to_cpu(*tmp
);
204 static inline const char *
205 blob_get_string(const struct blob_attr
*attr
)
210 static inline struct blob_attr
*
211 blob_next(const struct blob_attr
*attr
)
213 return (struct blob_attr
*) ((char *) attr
+ blob_pad_len(attr
));
216 extern int blob_buf_init(struct blob_buf
*buf
, int id
);
217 extern struct blob_attr
*blob_new(struct blob_buf
*buf
, int id
, int payload
);
218 extern void *blob_nest_start(struct blob_buf
*buf
, int id
);
219 extern void blob_nest_end(struct blob_buf
*buf
, void *cookie
);
220 extern struct blob_attr
*blob_put(struct blob_buf
*buf
, int id
, const void *ptr
, int len
);
221 extern bool blob_check_type(const void *ptr
, int len
, int type
);
222 extern int blob_parse(struct blob_attr
*attr
, struct blob_attr
**data
, const struct blob_attr_info
*info
, int max
);
224 static inline struct blob_attr
*
225 blob_put_string(struct blob_buf
*buf
, int id
, const char *str
)
227 return blob_put(buf
, id
, str
, strlen(str
) + 1);
230 static inline struct blob_attr
*
231 blob_put_int8(struct blob_buf
*buf
, int id
, uint8_t val
)
233 return blob_put(buf
, id
, &val
, sizeof(val
));
236 static inline struct blob_attr
*
237 blob_put_int16(struct blob_buf
*buf
, int id
, uint16_t val
)
239 val
= cpu_to_be16(val
);
240 return blob_put(buf
, id
, &val
, sizeof(val
));
243 static inline struct blob_attr
*
244 blob_put_int32(struct blob_buf
*buf
, int id
, uint32_t val
)
246 val
= cpu_to_be32(val
);
247 return blob_put(buf
, id
, &val
, sizeof(val
));
250 static inline struct blob_attr
*
251 blob_put_int64(struct blob_buf
*buf
, int id
, uint64_t val
)
253 val
= cpu_to_be64(val
);
254 return blob_put(buf
, id
, &val
, sizeof(val
));
257 #define __blob_for_each_attr(pos, attr, rem) \
258 for (pos = (void *) attr; \
259 (blob_pad_len(pos) <= rem) && \
260 (blob_pad_len(pos) >= sizeof(struct blob_attr)); \
261 rem -= blob_pad_len(pos), pos = blob_next(pos))
264 #define blob_for_each_attr(pos, attr, rem) \
265 for (rem = blob_len(attr), pos = blob_data(attr); \
266 (blob_pad_len(pos) <= rem) && \
267 (blob_pad_len(pos) >= sizeof(struct blob_attr)); \
268 rem -= blob_pad_len(pos), pos = blob_next(pos))