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_STRING] = BLOB_ATTR_STRING,
27 blobmsg_namelen(const struct blobmsg_hdr *hdr)
29 return be16_to_cpu(hdr->namelen);
32 bool blobmsg_check_attr(const struct blob_attr *attr, bool name)
34 const struct blobmsg_hdr *hdr;
38 if (blob_len(attr) < sizeof(struct blobmsg_hdr))
41 hdr = (void *) attr->data;
42 if (!hdr->namelen && name)
45 if (blobmsg_namelen(hdr) > blob_len(attr) - sizeof(struct blobmsg_hdr))
48 if (hdr->name[blobmsg_namelen(hdr)] != 0)
52 len = blobmsg_data_len(attr);
53 data = blobmsg_data(attr);
55 if (!id || id > BLOBMSG_TYPE_LAST)
61 return blob_check_type(data, len, blob_type[id]);
64 bool blobmsg_check_attr_list(const struct blob_attr *attr, int type)
66 struct blob_attr *cur;
70 switch (blobmsg_type(attr)) {
71 case BLOBMSG_TYPE_TABLE:
74 case BLOBMSG_TYPE_ARRAY:
81 blobmsg_for_each_attr(cur, attr, rem) {
82 if (blobmsg_type(cur) != type)
85 if (!blobmsg_check_attr(cur, name))
92 int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
93 struct blob_attr **tb, void *data, int len)
95 struct blobmsg_hdr *hdr;
96 struct blob_attr *attr;
100 memset(tb, 0, policy_len * sizeof(*tb));
101 pslen = alloca(policy_len);
102 for (i = 0; i < policy_len; i++) {
106 pslen[i] = strlen(policy[i].name);
109 __blob_for_each_attr(attr, data, len) {
110 hdr = blob_data(attr);
111 for (i = 0; i < policy_len; i++) {
115 if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
116 blob_id(attr) != policy[i].type)
119 if (blobmsg_namelen(hdr) != pslen[i])
122 if (!blobmsg_check_attr(attr, true))
128 if (strcmp(policy[i].name, (char *) hdr->name) != 0)
139 static struct blob_attr *
140 blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len, void **data)
142 struct blob_attr *attr;
143 struct blobmsg_hdr *hdr;
144 int attrlen, namelen;
145 char *pad_start, *pad_end;
150 namelen = strlen(name);
151 attrlen = blobmsg_hdrlen(namelen) + payload_len;
152 attr = blob_new(buf, type, attrlen);
156 hdr = blob_data(attr);
157 hdr->namelen = cpu_to_be16(namelen);
158 strcpy((char *) hdr->name, (const char *)name);
159 pad_end = *data = blobmsg_data(attr);
160 pad_start = (char *) &hdr->name[namelen];
161 if (pad_start < pad_end)
162 memset(pad_start, 0, pad_end - pad_start);
168 attr_to_offset(struct blob_buf *buf, struct blob_attr *attr)
170 return (char *)attr - (char *) buf->buf;
175 blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array)
177 struct blob_attr *head = buf->head;
178 int type = array ? BLOBMSG_TYPE_ARRAY : BLOBMSG_TYPE_TABLE;
179 unsigned long offset = attr_to_offset(buf, buf->head);
185 head = blobmsg_new(buf, type, name, 0, &data);
186 blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blobmsg_hdrlen(strlen(name)));
188 return (void *)offset;
192 blobmsg_alloc_string_buffer(struct blob_buf *buf, const char *name, int maxlen)
194 struct blob_attr *attr;
197 attr = blobmsg_new(buf, BLOBMSG_TYPE_STRING, name, maxlen, &data_dest);
201 data_dest = blobmsg_data(attr);
202 blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blob_pad_len(attr));
203 blob_set_raw_len(attr, blob_raw_len(attr) - maxlen);
209 blobmsg_add_string_buffer(struct blob_buf *buf)
211 struct blob_attr *attr;
214 attr = blob_next(buf->head);
215 len = strlen(blobmsg_data(attr)) + 1;
217 attrlen = blob_raw_len(attr) + len;
218 blob_set_raw_len(attr, attrlen);
221 blob_set_raw_len(buf->head, blob_raw_len(buf->head) + blob_pad_len(attr));
225 blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
226 const void *data, int len)
228 struct blob_attr *attr;
231 attr = blobmsg_new(buf, type, name, len, &data_dest);
236 memcpy(data_dest, data, len);