add blobmsg validation function
[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  * 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
9  *
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.
14  */
15
16 #ifndef _BLOB_H__
17 #define _BLOB_H__
18
19 #include <stdbool.h>
20 #include <stdlib.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <errno.h>
25
26 #if __BYTE_ORDER == __LITTLE_ENDIAN
27
28 #if defined(__linux__) || defined(__CYGWIN__)
29 #include <byteswap.h>
30 #include <endian.h>
31
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)
43 #else
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)
48 #endif
49
50 #ifndef __BYTE_ORDER
51 #define __BYTE_ORDER BYTE_ORDER
52 #endif
53 #ifndef __BIG_ENDIAN
54 #define __BIG_ENDIAN BIG_ENDIAN
55 #endif
56 #ifndef __LITTLE_ENDIAN
57 #define __LITTLE_ENDIAN LITTLE_ENDIAN
58 #endif
59
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)
63
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)
67
68 #else
69
70 #define cpu_to_be64(x) (x)
71 #define cpu_to_be32(x) (x)
72 #define cpu_to_be16(x) (x)
73
74 #define be64_to_cpu(x) (x)
75 #define be32_to_cpu(x) (x)
76 #define be16_to_cpu(x) (x)
77
78 #endif
79
80 enum {
81         BLOB_ATTR_UNSPEC,
82         BLOB_ATTR_NESTED,
83         BLOB_ATTR_BINARY,
84         BLOB_ATTR_STRING,
85         BLOB_ATTR_INT8,
86         BLOB_ATTR_INT16,
87         BLOB_ATTR_INT32,
88         BLOB_ATTR_INT64,
89         BLOB_ATTR_LAST
90 };
91
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
96
97 #ifndef __packed
98 #define __packed __attribute__((packed))
99 #endif
100
101 struct blob_attr {
102         uint32_t id_len;
103         char data[];
104 } __packed;
105
106 struct blob_attr_info {
107         unsigned int type;
108         unsigned int minlen;
109         unsigned int maxlen;
110         bool (*validate)(const struct blob_attr_info *, struct blob_attr *);
111 };
112
113 struct blob_buf {
114         struct blob_attr *head;
115         bool (*grow)(struct blob_buf *buf, int minlen);
116         int buflen;
117         void *buf;
118         void *priv;
119 };
120
121 /*
122  * blob_data: returns the data pointer for an attribute
123  */
124 static inline void *
125 blob_data(struct blob_attr *attr)
126 {
127         return attr->data;
128 }
129
130 /*
131  * blob_id: returns the id of an attribute
132  */
133 static inline unsigned int
134 blob_id(struct blob_attr *attr)
135 {
136         int id = (be32_to_cpu(attr->id_len) & BLOB_ATTR_ID_MASK) >> BLOB_ATTR_ID_SHIFT;
137         return id;
138 }
139
140 /*
141  * blob_len: returns the length of the attribute's payload
142  */
143 static inline unsigned int
144 blob_len(const struct blob_attr *attr)
145 {
146         return (be32_to_cpu(attr->id_len) & BLOB_ATTR_LEN_MASK) - sizeof(struct blob_attr);
147 }
148
149 /*
150  * blob_pad_len: returns the complete length of an attribute (including the header)
151  */
152 static inline unsigned int
153 blob_raw_len(struct blob_attr *attr)
154 {
155         return blob_len(attr) + sizeof(struct blob_attr);
156 }
157
158 /*
159  * blob_pad_len: returns the padded length of an attribute (including the header)
160  */
161 static inline unsigned int
162 blob_pad_len(struct blob_attr *attr)
163 {
164         int len = blob_raw_len(attr);
165         len = (len + BLOB_ATTR_ALIGN - 1) & ~(BLOB_ATTR_ALIGN - 1);
166         return len;
167 }
168
169 static inline void
170 blob_set_raw_len(struct blob_attr *attr, unsigned int len)
171 {
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);
176 }
177
178 static inline uint8_t
179 blob_get_int8(struct blob_attr *attr)
180 {
181         return *((uint8_t *) attr->data);
182 }
183
184 static inline uint16_t
185 blob_get_int16(struct blob_attr *attr)
186 {
187         uint16_t *tmp = (uint16_t*)attr->data;
188         return be16_to_cpu(*tmp);
189 }
190
191 static inline uint32_t
192 blob_get_int32(struct blob_attr *attr)
193 {
194         uint32_t *tmp = (uint32_t*)attr->data;
195         return be32_to_cpu(*tmp);
196 }
197
198 static inline uint64_t
199 blob_get_int64(struct blob_attr *attr)
200 {
201         uint64_t *tmp = (uint64_t*)attr->data;
202         return be64_to_cpu(*tmp);
203 }
204
205 static inline const char *
206 blob_get_string(struct blob_attr *attr)
207 {
208         return attr->data;
209 }
210
211 static inline struct blob_attr *
212 blob_next(struct blob_attr *attr)
213 {
214         return (struct blob_attr *) ((char *) attr + blob_pad_len(attr));
215 }
216
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);
223
224 static inline struct blob_attr *
225 blob_put_string(struct blob_buf *buf, int id, const char *str)
226 {
227         return blob_put(buf, id, str, strlen(str) + 1);
228 }
229
230 static inline struct blob_attr *
231 blob_put_int8(struct blob_buf *buf, int id, uint8_t val)
232 {
233         return blob_put(buf, id, &val, sizeof(val));
234 }
235
236 static inline struct blob_attr *
237 blob_put_int16(struct blob_buf *buf, int id, uint16_t val)
238 {
239         val = cpu_to_be16(val);
240         return blob_put(buf, id, &val, sizeof(val));
241 }
242
243 static inline struct blob_attr *
244 blob_put_int32(struct blob_buf *buf, int id, uint32_t val)
245 {
246         val = cpu_to_be32(val);
247         return blob_put(buf, id, &val, sizeof(val));
248 }
249
250 static inline struct blob_attr *
251 blob_put_int64(struct blob_buf *buf, int id, uint64_t val)
252 {
253         val = cpu_to_be64(val);
254         return blob_put(buf, id, &val, sizeof(val));
255 }
256
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))
262
263
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))
269
270
271 #endif