2 * Copyright (C) 2010-2012 Felix Fietkau <nbd@openwrt.org>
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.
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.
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,
28 bool blobmsg_check_attr(const struct blob_attr *attr, bool name)
30 return blobmsg_check_attr_len(attr, name, blob_raw_len(attr));
33 static bool blobmsg_check_name(const struct blob_attr *attr, bool name)
35 const struct blobmsg_hdr *hdr;
38 if (!blob_is_extended(attr))
41 if (blob_len(attr) < sizeof(struct blobmsg_hdr))
44 hdr = (const struct blobmsg_hdr *)blob_data(attr);
45 if (name && !hdr->namelen)
48 namelen = blobmsg_namelen(hdr);
49 if (blob_len(attr) < (size_t)blobmsg_hdrlen(namelen))
52 if (hdr->name[namelen] != 0)
58 bool blobmsg_check_attr_len(const struct blob_attr *attr, bool name, size_t len)
64 if (len < sizeof(struct blob_attr))
67 data_len = blob_raw_len(attr);
68 if (data_len < sizeof(struct blob_attr) || data_len > len)
71 if (!blobmsg_check_name(attr, name))
75 if (id > BLOBMSG_TYPE_LAST)
81 data = blobmsg_data(attr);
82 data_len = blobmsg_data_len(attr);
84 return blob_check_type(data, data_len, blob_type[id]);
87 int blobmsg_check_array(const struct blob_attr *attr, int type)
89 return blobmsg_check_array_len(attr, type, blob_raw_len(attr));
92 int blobmsg_check_array_len(const struct blob_attr *attr, int type,
95 struct blob_attr *cur;
100 if (type > BLOBMSG_TYPE_LAST)
103 if (!blobmsg_check_attr_len(attr, false, blob_len))
106 switch (blobmsg_type(attr)) {
107 case BLOBMSG_TYPE_TABLE:
110 case BLOBMSG_TYPE_ARRAY:
117 blobmsg_for_each_attr(cur, attr, rem) {
118 if (type != BLOBMSG_TYPE_UNSPEC && blobmsg_type(cur) != type)
121 if (!blobmsg_check_attr_len(cur, name, rem))
130 bool blobmsg_check_attr_list(const struct blob_attr *attr, int type)
132 return blobmsg_check_array(attr, type) >= 0;
135 bool blobmsg_check_attr_list_len(const struct blob_attr *attr, int type, size_t len)
137 return blobmsg_check_array_len(attr, type, len) >= 0;
140 int blobmsg_parse_array(const struct blobmsg_policy *policy, int policy_len,
141 struct blob_attr **tb, void *data, unsigned int len)
143 struct blob_attr *attr;
146 memset(tb, 0, policy_len * sizeof(*tb));
147 __blob_for_each_attr(attr, data, len) {
148 if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
149 blob_id(attr) != policy[i].type)
152 if (!blobmsg_check_attr_len(attr, false, len))
166 int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
167 struct blob_attr **tb, void *data, unsigned int len)
169 const struct blobmsg_hdr *hdr;
170 struct blob_attr *attr;
174 memset(tb, 0, policy_len * sizeof(*tb));
177 pslen = alloca(policy_len);
178 for (i = 0; i < policy_len; i++) {
182 pslen[i] = strlen(policy[i].name);
185 __blob_for_each_attr(attr, data, len) {
186 if (!blobmsg_check_attr_len(attr, false, len))
189 if (!blob_is_extended(attr))
192 hdr = blob_data(attr);
193 for (i = 0; i < policy_len; i++) {
197 if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
198 blob_id(attr) != policy[i].type)
201 if (blobmsg_namelen(hdr) != pslen[i])
207 if (strcmp(policy[i].name, (char *) hdr->name) != 0)
218 static struct blob_attr *
219 blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len, void **data)
221 struct blob_attr *attr;
222 struct blobmsg_hdr *hdr;
223 int attrlen, namelen;
224 char *pad_start, *pad_end;
229 namelen = strlen(name);
230 attrlen = blobmsg_hdrlen(namelen) + payload_len;
231 attr = blob_new(buf, type, attrlen);
235 attr->id_len |= be32_to_cpu(BLOB_ATTR_EXTENDED);
236 hdr = blob_data(attr);
237 hdr->namelen = cpu_to_be16(namelen);
239 memcpy(hdr->name, name, namelen);
240 hdr->name[namelen] = '\0';
242 pad_end = *data = blobmsg_data(attr);
243 pad_start = (char *) &hdr->name[namelen];
244 if (pad_start < pad_end)
245 memset(pad_start, 0, pad_end - pad_start);
251 attr_to_offset(struct blob_buf *buf, struct blob_attr *attr)
253 return (char *)attr - (char *) buf->buf + BLOB_COOKIE;
258 blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array)
260 struct blob_attr *head;
261 int type = array ? BLOBMSG_TYPE_ARRAY : BLOBMSG_TYPE_TABLE;
262 unsigned long offset = attr_to_offset(buf, buf->head);
268 head = blobmsg_new(buf, type, name, 0, &data);
271 blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blobmsg_hdrlen(strlen(name)));
273 return (void *)offset;
276 __attribute__((format(printf, 3, 0)))
277 int blobmsg_vprintf(struct blob_buf *buf, const char *name, const char *format, va_list arg)
285 len = vsnprintf(&cbuf, sizeof(cbuf), format, arg2);
291 sbuf = blobmsg_alloc_string_buffer(buf, name, len + 1);
295 ret = vsnprintf(sbuf, len + 1, format, arg);
299 blobmsg_add_string_buffer(buf);
304 __attribute__((format(printf, 3, 4)))
305 int blobmsg_printf(struct blob_buf *buf, const char *name, const char *format, ...)
310 va_start(ap, format);
311 ret = blobmsg_vprintf(buf, name, format, ap);
318 blobmsg_alloc_string_buffer(struct blob_buf *buf, const char *name, unsigned int maxlen)
320 struct blob_attr *attr;
323 attr = blobmsg_new(buf, BLOBMSG_TYPE_STRING, name, maxlen, &data_dest);
327 blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blob_pad_len(attr));
328 blob_set_raw_len(attr, blob_raw_len(attr) - maxlen);
334 blobmsg_realloc_string_buffer(struct blob_buf *buf, unsigned int maxlen)
336 struct blob_attr *attr = blob_next(buf->head);
337 int offset = attr_to_offset(buf, blob_next(buf->head)) + blob_pad_len(attr) - BLOB_COOKIE;
338 int required = maxlen - (buf->buflen - offset);
343 if (!blob_buf_grow(buf, required))
345 attr = blob_next(buf->head);
348 return blobmsg_data(attr);
352 blobmsg_add_string_buffer(struct blob_buf *buf)
354 struct blob_attr *attr;
357 attr = blob_next(buf->head);
358 len = strlen(blobmsg_data(attr)) + 1;
360 attrlen = blob_raw_len(attr) + len;
361 blob_set_raw_len(attr, attrlen);
364 blob_set_raw_len(buf->head, blob_raw_len(buf->head) + blob_pad_len(attr));
368 blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
369 const void *data, unsigned int len)
371 struct blob_attr *attr;
374 attr = blobmsg_new(buf, type, name, len, &data_dest);
379 memcpy(data_dest, data, len);