308bef7bc6b052bf708308b6789e96b514c88bbb
[project/libubox.git] / blobmsg.c
1 /*
2 * Copyright (C) 2010-2012 Felix Fietkau <nbd@openwrt.org>
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16 #include "blobmsg.h"
17
18 static const int blob_type[__BLOBMSG_TYPE_LAST] = {
19 [BLOBMSG_TYPE_INT8] = BLOB_ATTR_INT8,
20 [BLOBMSG_TYPE_INT16] = BLOB_ATTR_INT16,
21 [BLOBMSG_TYPE_INT32] = BLOB_ATTR_INT32,
22 [BLOBMSG_TYPE_INT64] = BLOB_ATTR_INT64,
23 [BLOBMSG_TYPE_DOUBLE] = BLOB_ATTR_DOUBLE,
24 [BLOBMSG_TYPE_STRING] = BLOB_ATTR_STRING,
25 [BLOBMSG_TYPE_UNSPEC] = BLOB_ATTR_BINARY,
26 };
27
28 bool blobmsg_check_attr(const struct blob_attr *attr, bool name)
29 {
30 return blobmsg_check_attr_len(attr, name, blob_raw_len(attr));
31 }
32
33 static const struct blobmsg_hdr* blobmsg_hdr_from_blob(const struct blob_attr *attr, size_t len)
34 {
35 if (len < sizeof(struct blob_attr) + sizeof(struct blobmsg_hdr))
36 return NULL;
37
38 return blob_data(attr);
39 }
40
41 static bool blobmsg_hdr_valid_namelen(const struct blobmsg_hdr *hdr, size_t len)
42 {
43 if (len < sizeof(struct blob_attr) + sizeof(struct blobmsg_hdr) + blobmsg_namelen(hdr) + 1)
44 return false;
45
46 return true;
47 }
48
49 static bool blobmsg_check_name(const struct blob_attr *attr, size_t len, bool name)
50 {
51 const struct blobmsg_hdr *hdr;
52 uint16_t namelen;
53
54 hdr = blobmsg_hdr_from_blob(attr, len);
55 if (!hdr)
56 return false;
57
58 if (name && !hdr->namelen)
59 return false;
60
61 namelen = blobmsg_namelen(hdr);
62 if (blob_len(attr) < (size_t)blobmsg_hdrlen(namelen))
63 return false;
64
65 if (hdr->name[namelen] != 0)
66 return false;
67
68 return true;
69 }
70
71 static const char* blobmsg_check_data(const struct blob_attr *attr, size_t len, size_t *data_len)
72 {
73 char *limit = (char *) attr + len;
74 const char *data;
75
76 *data_len = blobmsg_data_len(attr);
77 if (*data_len > blob_raw_len(attr))
78 return NULL;
79
80 data = blobmsg_data(attr);
81 if (data + *data_len > limit)
82 return NULL;
83
84 return data;
85 }
86
87 bool blobmsg_check_attr_len(const struct blob_attr *attr, bool name, size_t len)
88 {
89 const char *data;
90 size_t data_len;
91 int id;
92
93 if (!blobmsg_check_name(attr, len, name))
94 return false;
95
96 id = blob_id(attr);
97 if (id > BLOBMSG_TYPE_LAST)
98 return false;
99
100 if (!blob_type[id])
101 return true;
102
103 data = blobmsg_check_data(attr, len, &data_len);
104 if (!data)
105 return false;
106
107 return blob_check_type(data, data_len, blob_type[id]);
108 }
109
110 int blobmsg_check_array(const struct blob_attr *attr, int type)
111 {
112 return blobmsg_check_array_len(attr, type, blob_raw_len(attr));
113 }
114
115 int blobmsg_check_array_len(const struct blob_attr *attr, int type,
116 size_t blob_len)
117 {
118 struct blob_attr *cur;
119 size_t rem;
120 bool name;
121 int size = 0;
122
123 if (type > BLOBMSG_TYPE_LAST)
124 return -1;
125
126 if (!blobmsg_check_attr_len(attr, false, blob_len))
127 return -1;
128
129 switch (blobmsg_type(attr)) {
130 case BLOBMSG_TYPE_TABLE:
131 name = true;
132 break;
133 case BLOBMSG_TYPE_ARRAY:
134 name = false;
135 break;
136 default:
137 return -1;
138 }
139
140 blobmsg_for_each_attr(cur, attr, rem) {
141 if (type != BLOBMSG_TYPE_UNSPEC && blobmsg_type(cur) != type)
142 return -1;
143
144 if (!blobmsg_check_attr_len(cur, name, rem))
145 return -1;
146
147 size++;
148 }
149
150 return size;
151 }
152
153 bool blobmsg_check_attr_list(const struct blob_attr *attr, int type)
154 {
155 return blobmsg_check_array(attr, type) >= 0;
156 }
157
158 bool blobmsg_check_attr_list_len(const struct blob_attr *attr, int type, size_t len)
159 {
160 return blobmsg_check_array_len(attr, type, len) >= 0;
161 }
162
163 int blobmsg_parse_array(const struct blobmsg_policy *policy, int policy_len,
164 struct blob_attr **tb, void *data, unsigned int len)
165 {
166 struct blob_attr *attr;
167 int i = 0;
168
169 memset(tb, 0, policy_len * sizeof(*tb));
170 __blob_for_each_attr(attr, data, len) {
171 if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
172 blob_id(attr) != policy[i].type)
173 continue;
174
175 if (!blobmsg_check_attr_len(attr, false, len))
176 return -1;
177
178 if (tb[i])
179 continue;
180
181 tb[i++] = attr;
182 if (i == policy_len)
183 break;
184 }
185
186 return 0;
187 }
188
189 int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
190 struct blob_attr **tb, void *data, unsigned int len)
191 {
192 const struct blobmsg_hdr *hdr;
193 struct blob_attr *attr;
194 uint8_t *pslen;
195 int i;
196
197 memset(tb, 0, policy_len * sizeof(*tb));
198 if (!data || !len)
199 return -EINVAL;
200 pslen = alloca(policy_len);
201 for (i = 0; i < policy_len; i++) {
202 if (!policy[i].name)
203 continue;
204
205 pslen[i] = strlen(policy[i].name);
206 }
207
208 __blob_for_each_attr(attr, data, len) {
209 hdr = blobmsg_hdr_from_blob(attr, len);
210 if (!hdr)
211 return -1;
212
213 if (!blobmsg_hdr_valid_namelen(hdr, len))
214 return -1;
215
216 for (i = 0; i < policy_len; i++) {
217 if (!policy[i].name)
218 continue;
219
220 if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
221 blob_id(attr) != policy[i].type)
222 continue;
223
224 if (blobmsg_namelen(hdr) != pslen[i])
225 continue;
226
227 if (!blobmsg_check_attr_len(attr, true, len))
228 return -1;
229
230 if (tb[i])
231 continue;
232
233 if (strcmp(policy[i].name, (char *) hdr->name) != 0)
234 continue;
235
236 tb[i] = attr;
237 }
238 }
239
240 return 0;
241 }
242
243
244 static struct blob_attr *
245 blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len, void **data)
246 {
247 struct blob_attr *attr;
248 struct blobmsg_hdr *hdr;
249 int attrlen, namelen;
250 char *pad_start, *pad_end;
251
252 if (!name)
253 name = "";
254
255 namelen = strlen(name);
256 attrlen = blobmsg_hdrlen(namelen) + payload_len;
257 attr = blob_new(buf, type, attrlen);
258 if (!attr)
259 return NULL;
260
261 attr->id_len |= be32_to_cpu(BLOB_ATTR_EXTENDED);
262 hdr = blob_data(attr);
263 hdr->namelen = cpu_to_be16(namelen);
264
265 memcpy(hdr->name, name, namelen);
266 hdr->name[namelen] = '\0';
267
268 pad_end = *data = blobmsg_data(attr);
269 pad_start = (char *) &hdr->name[namelen];
270 if (pad_start < pad_end)
271 memset(pad_start, 0, pad_end - pad_start);
272
273 return attr;
274 }
275
276 static inline int
277 attr_to_offset(struct blob_buf *buf, struct blob_attr *attr)
278 {
279 return (char *)attr - (char *) buf->buf + BLOB_COOKIE;
280 }
281
282
283 void *
284 blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array)
285 {
286 struct blob_attr *head;
287 int type = array ? BLOBMSG_TYPE_ARRAY : BLOBMSG_TYPE_TABLE;
288 unsigned long offset = attr_to_offset(buf, buf->head);
289 void *data;
290
291 if (!name)
292 name = "";
293
294 head = blobmsg_new(buf, type, name, 0, &data);
295 if (!head)
296 return NULL;
297 blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blobmsg_hdrlen(strlen(name)));
298 buf->head = head;
299 return (void *)offset;
300 }
301
302 __attribute__((format(printf, 3, 0)))
303 int blobmsg_vprintf(struct blob_buf *buf, const char *name, const char *format, va_list arg)
304 {
305 va_list arg2;
306 char cbuf;
307 char *sbuf;
308 int len, ret;
309
310 va_copy(arg2, arg);
311 len = vsnprintf(&cbuf, sizeof(cbuf), format, arg2);
312 va_end(arg2);
313
314 if (len < 0)
315 return -1;
316
317 sbuf = blobmsg_alloc_string_buffer(buf, name, len + 1);
318 if (!sbuf)
319 return -1;
320
321 ret = vsnprintf(sbuf, len + 1, format, arg);
322 if (ret < 0)
323 return -1;
324
325 blobmsg_add_string_buffer(buf);
326
327 return ret;
328 }
329
330 __attribute__((format(printf, 3, 4)))
331 int blobmsg_printf(struct blob_buf *buf, const char *name, const char *format, ...)
332 {
333 va_list ap;
334 int ret;
335
336 va_start(ap, format);
337 ret = blobmsg_vprintf(buf, name, format, ap);
338 va_end(ap);
339
340 return ret;
341 }
342
343 void *
344 blobmsg_alloc_string_buffer(struct blob_buf *buf, const char *name, unsigned int maxlen)
345 {
346 struct blob_attr *attr;
347 void *data_dest;
348
349 attr = blobmsg_new(buf, BLOBMSG_TYPE_STRING, name, maxlen, &data_dest);
350 if (!attr)
351 return NULL;
352
353 blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blob_pad_len(attr));
354 blob_set_raw_len(attr, blob_raw_len(attr) - maxlen);
355
356 return data_dest;
357 }
358
359 void *
360 blobmsg_realloc_string_buffer(struct blob_buf *buf, unsigned int maxlen)
361 {
362 struct blob_attr *attr = blob_next(buf->head);
363 int offset = attr_to_offset(buf, blob_next(buf->head)) + blob_pad_len(attr) - BLOB_COOKIE;
364 int required = maxlen - (buf->buflen - offset);
365
366 if (required <= 0)
367 goto out;
368
369 if (!blob_buf_grow(buf, required))
370 return NULL;
371 attr = blob_next(buf->head);
372
373 out:
374 return blobmsg_data(attr);
375 }
376
377 void
378 blobmsg_add_string_buffer(struct blob_buf *buf)
379 {
380 struct blob_attr *attr;
381 int len, attrlen;
382
383 attr = blob_next(buf->head);
384 len = strlen(blobmsg_data(attr)) + 1;
385
386 attrlen = blob_raw_len(attr) + len;
387 blob_set_raw_len(attr, attrlen);
388 blob_fill_pad(attr);
389
390 blob_set_raw_len(buf->head, blob_raw_len(buf->head) + blob_pad_len(attr));
391 }
392
393 int
394 blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
395 const void *data, unsigned int len)
396 {
397 struct blob_attr *attr;
398 void *data_dest;
399
400 attr = blobmsg_new(buf, type, name, len, &data_dest);
401 if (!attr)
402 return -1;
403
404 if (len > 0)
405 memcpy(data_dest, data, len);
406
407 return 0;
408 }