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);
122 * blob_data: returns the data pointer for an attribute
125 blob_data(struct blob_attr *attr)
131 * blob_id: returns the id of an attribute
133 static inline unsigned int
134 blob_id(struct blob_attr *attr)
136 int id = (be32_to_cpu(attr->id_len) & BLOB_ATTR_ID_MASK) >> BLOB_ATTR_ID_SHIFT;
141 * blob_len: returns the length of the attribute's payload
143 static inline unsigned int
144 blob_len(const struct blob_attr *attr)
146 return (be32_to_cpu(attr->id_len) & BLOB_ATTR_LEN_MASK) - sizeof(struct blob_attr);
150 * blob_pad_len: returns the complete length of an attribute (including the header)
152 static inline unsigned int
153 blob_raw_len(struct blob_attr *attr)
155 return blob_len(attr) + sizeof(struct blob_attr);
159 * blob_pad_len: returns the padded length of an attribute (including the header)
161 static inline unsigned int
162 blob_pad_len(struct blob_attr *attr)
164 int len = blob_raw_len(attr);
165 len = (len + BLOB_ATTR_ALIGN - 1) & ~(BLOB_ATTR_ALIGN - 1);
170 blob_set_raw_len(struct blob_attr *attr, unsigned int len)
172 int id = blob_id(attr);
173 len &= BLOB_ATTR_LEN_MASK;
174 len |= (id << BLOB_ATTR_ID_SHIFT) & BLOB_ATTR_ID_MASK;
175 attr->id_len = cpu_to_be32(len);
178 static inline uint8_t
179 blob_get_int8(struct blob_attr *attr)
181 return *((uint8_t *) attr->data);
184 static inline uint16_t
185 blob_get_int16(struct blob_attr *attr)
187 uint16_t *tmp = (uint16_t*)attr->data;
188 return be16_to_cpu(*tmp);
191 static inline uint32_t
192 blob_get_int32(struct blob_attr *attr)
194 uint32_t *tmp = (uint32_t*)attr->data;
195 return be32_to_cpu(*tmp);
198 static inline uint64_t
199 blob_get_int64(struct blob_attr *attr)
201 uint64_t *tmp = (uint64_t*)attr->data;
202 return be64_to_cpu(*tmp);
205 static inline const char *
206 blob_get_string(struct blob_attr *attr)
211 static inline struct blob_attr *
212 blob_next(struct blob_attr *attr)
214 return (struct blob_attr *) ((char *) attr + blob_pad_len(attr));
217 extern int blob_buf_init(struct blob_buf *buf, int id);
218 extern struct blob_attr *blob_new(struct blob_buf *buf, int id, int payload);
219 extern void *blob_nest_start(struct blob_buf *buf, int id);
220 extern void blob_nest_end(struct blob_buf *buf, void *cookie);
221 extern struct blob_attr *blob_put(struct blob_buf *buf, int id, const void *ptr, int len);
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))