d78115f5fa53108aae0e4b70026b7c0f693163ef
[project/libubox.git] / blob.h
1 /*
2  * blob - library for generating/parsing tagged binary data
3  *
4  * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18
19 #ifndef _BLOB_H__
20 #define _BLOB_H__
21
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <stdint.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <errno.h>
28
29 #if defined(__linux__) || defined(__CYGWIN__)
30 #include <byteswap.h>
31 #include <endian.h>
32
33 #elif defined(__APPLE__)
34 #include <machine/endian.h>
35 #include <machine/byte_order.h>
36 #define bswap_16(x) OSSwapInt16(x)
37 #define bswap_32(x) OSSwapInt32(x)
38 #define bswap_64(x) OSSwapInt64(x)
39 #elif defined(__FreeBSD__)
40 #include <sys/endian.h>
41 #define bswap_16(x) bswap16(x)
42 #define bswap_32(x) bswap32(x)
43 #define bswap_64(x) bswap64(x)
44 #else
45 #include <machine/endian.h>
46 #define bswap_16(x) swap16(x)
47 #define bswap_32(x) swap32(x)
48 #define bswap_64(x) swap64(x)
49 #endif
50
51 #ifndef __BYTE_ORDER
52 #define __BYTE_ORDER BYTE_ORDER
53 #endif
54 #ifndef __BIG_ENDIAN
55 #define __BIG_ENDIAN BIG_ENDIAN
56 #endif
57 #ifndef __LITTLE_ENDIAN
58 #define __LITTLE_ENDIAN LITTLE_ENDIAN
59 #endif
60
61 #if __BYTE_ORDER == __LITTLE_ENDIAN
62
63 #define cpu_to_be64(x) bswap_64(x)
64 #define cpu_to_be32(x) bswap_32(x)
65 #define cpu_to_be16(x) bswap_16(x)
66
67 #define be64_to_cpu(x) bswap_64(x)
68 #define be32_to_cpu(x) bswap_32(x)
69 #define be16_to_cpu(x) bswap_16(x)
70
71 #else
72
73 #define cpu_to_be64(x) (x)
74 #define cpu_to_be32(x) (x)
75 #define cpu_to_be16(x) (x)
76
77 #define be64_to_cpu(x) (x)
78 #define be32_to_cpu(x) (x)
79 #define be16_to_cpu(x) (x)
80
81 #endif
82
83 enum {
84         BLOB_ATTR_UNSPEC,
85         BLOB_ATTR_NESTED,
86         BLOB_ATTR_BINARY,
87         BLOB_ATTR_STRING,
88         BLOB_ATTR_INT8,
89         BLOB_ATTR_INT16,
90         BLOB_ATTR_INT32,
91         BLOB_ATTR_INT64,
92         BLOB_ATTR_LAST
93 };
94
95 #define BLOB_ATTR_ID_MASK  0xff000000
96 #define BLOB_ATTR_ID_SHIFT 24
97 #define BLOB_ATTR_LEN_MASK 0x00ffffff
98 #define BLOB_ATTR_ALIGN    4
99
100 #ifndef __packed
101 #define __packed __attribute__((packed))
102 #endif
103
104 struct blob_attr {
105         uint32_t id_len;
106         char data[];
107 } __packed;
108
109 struct blob_attr_info {
110         unsigned int type;
111         unsigned int minlen;
112         unsigned int maxlen;
113         bool (*validate)(const struct blob_attr_info *, struct blob_attr *);
114 };
115
116 struct blob_buf {
117         struct blob_attr *head;
118         bool (*grow)(struct blob_buf *buf, int minlen);
119         int buflen;
120         void *buf;
121 };
122
123 /*
124  * blob_data: returns the data pointer for an attribute
125  */
126 static inline void *
127 blob_data(const struct blob_attr *attr)
128 {
129         return (void *) attr->data;
130 }
131
132 /*
133  * blob_id: returns the id of an attribute
134  */
135 static inline unsigned int
136 blob_id(const struct blob_attr *attr)
137 {
138         int id = (be32_to_cpu(attr->id_len) & BLOB_ATTR_ID_MASK) >> BLOB_ATTR_ID_SHIFT;
139         return id;
140 }
141
142 /*
143  * blob_len: returns the length of the attribute's payload
144  */
145 static inline unsigned int
146 blob_len(const struct blob_attr *attr)
147 {
148         return (be32_to_cpu(attr->id_len) & BLOB_ATTR_LEN_MASK) - sizeof(struct blob_attr);
149 }
150
151 /*
152  * blob_raw_len: returns the complete length of an attribute (including the header)
153  */
154 static inline unsigned int
155 blob_raw_len(const struct blob_attr *attr)
156 {
157         return blob_len(attr) + sizeof(struct blob_attr);
158 }
159
160 /*
161  * blob_pad_len: returns the padded length of an attribute (including the header)
162  */
163 static inline unsigned int
164 blob_pad_len(const struct blob_attr *attr)
165 {
166         int len = blob_raw_len(attr);
167         len = (len + BLOB_ATTR_ALIGN - 1) & ~(BLOB_ATTR_ALIGN - 1);
168         return len;
169 }
170
171 static inline uint8_t
172 blob_get_u8(const struct blob_attr *attr)
173 {
174         return *((uint8_t *) attr->data);
175 }
176
177 static inline uint16_t
178 blob_get_u16(const struct blob_attr *attr)
179 {
180         uint16_t *tmp = (uint16_t*)attr->data;
181         return be16_to_cpu(*tmp);
182 }
183
184 static inline uint32_t
185 blob_get_u32(const struct blob_attr *attr)
186 {
187         uint32_t *tmp = (uint32_t*)attr->data;
188         return be32_to_cpu(*tmp);
189 }
190
191 static inline uint64_t
192 blob_get_u64(const struct blob_attr *attr)
193 {
194         uint32_t *ptr = blob_data(attr);
195         uint64_t tmp = ((uint64_t) be32_to_cpu(ptr[0])) << 32;
196         tmp |= be32_to_cpu(ptr[1]);
197         return tmp;
198 }
199
200 static inline int8_t
201 blob_get_int8(const struct blob_attr *attr)
202 {
203         return blob_get_u8(attr);
204 }
205
206 static inline int16_t
207 blob_get_int16(const struct blob_attr *attr)
208 {
209         return blob_get_u16(attr);
210 }
211
212 static inline int32_t
213 blob_get_int32(const struct blob_attr *attr)
214 {
215         return blob_get_u32(attr);
216 }
217
218 static inline int64_t
219 blob_get_int64(const struct blob_attr *attr)
220 {
221         return blob_get_u64(attr);
222 }
223
224 static inline const char *
225 blob_get_string(const struct blob_attr *attr)
226 {
227         return attr->data;
228 }
229
230 static inline struct blob_attr *
231 blob_next(const struct blob_attr *attr)
232 {
233         return (struct blob_attr *) ((char *) attr + blob_pad_len(attr));
234 }
235
236 extern void blob_fill_pad(struct blob_attr *attr);
237 extern void blob_set_raw_len(struct blob_attr *attr, unsigned int len);
238 extern bool blob_attr_equal(const struct blob_attr *a1, const struct blob_attr *a2);
239 extern int blob_buf_init(struct blob_buf *buf, int id);
240 extern void blob_buf_free(struct blob_buf *buf);
241 extern void blob_buf_grow(struct blob_buf *buf, int required);
242 extern struct blob_attr *blob_new(struct blob_buf *buf, int id, int payload);
243 extern void *blob_nest_start(struct blob_buf *buf, int id);
244 extern void blob_nest_end(struct blob_buf *buf, void *cookie);
245 extern struct blob_attr *blob_put(struct blob_buf *buf, int id, const void *ptr, int len);
246 extern bool blob_check_type(const void *ptr, int len, int type);
247 extern int blob_parse(struct blob_attr *attr, struct blob_attr **data, const struct blob_attr_info *info, int max);
248 extern struct blob_attr *blob_memdup(struct blob_attr *attr);
249
250 static inline struct blob_attr *
251 blob_put_string(struct blob_buf *buf, int id, const char *str)
252 {
253         return blob_put(buf, id, str, strlen(str) + 1);
254 }
255
256 static inline struct blob_attr *
257 blob_put_u8(struct blob_buf *buf, int id, uint8_t val)
258 {
259         return blob_put(buf, id, &val, sizeof(val));
260 }
261
262 static inline struct blob_attr *
263 blob_put_u16(struct blob_buf *buf, int id, uint16_t val)
264 {
265         val = cpu_to_be16(val);
266         return blob_put(buf, id, &val, sizeof(val));
267 }
268
269 static inline struct blob_attr *
270 blob_put_u32(struct blob_buf *buf, int id, uint32_t val)
271 {
272         val = cpu_to_be32(val);
273         return blob_put(buf, id, &val, sizeof(val));
274 }
275
276 static inline struct blob_attr *
277 blob_put_u64(struct blob_buf *buf, int id, uint64_t val)
278 {
279         val = cpu_to_be64(val);
280         return blob_put(buf, id, &val, sizeof(val));
281 }
282
283 #define blob_put_int8   blob_put_u8
284 #define blob_put_int16  blob_put_u16
285 #define blob_put_int32  blob_put_u32
286 #define blob_put_int64  blob_put_u64
287
288 #define __blob_for_each_attr(pos, attr, rem) \
289         for (pos = (void *) attr; \
290                  rem > 0 && (blob_pad_len(pos) <= rem) && \
291                  (blob_pad_len(pos) >= sizeof(struct blob_attr)); \
292                  rem -= blob_pad_len(pos), pos = blob_next(pos))
293
294
295 #define blob_for_each_attr(pos, attr, rem) \
296         for (rem = blob_len(attr), pos = blob_data(attr); \
297                  rem > 0 && (blob_pad_len(pos) <= rem) && \
298                  (blob_pad_len(pos) >= sizeof(struct blob_attr)); \
299                  rem -= blob_pad_len(pos), pos = blob_next(pos))
300
301
302 #endif