71d4a36a647cb7ac7bac01c6f79833fd3614d4a8
[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 static uint16_t
29 blobmsg_namelen(const struct blobmsg_hdr *hdr)
30 {
31 return be16_to_cpu(hdr->namelen);
32 }
33
34 bool blobmsg_check_attr(const struct blob_attr *attr, bool name)
35 {
36 const struct blobmsg_hdr *hdr;
37 const char *data;
38 size_t len;
39 int id;
40
41 if (blob_len(attr) < sizeof(struct blobmsg_hdr))
42 return false;
43
44 hdr = (void *) attr->data;
45 if (!hdr->namelen && name)
46 return false;
47
48 if (blobmsg_namelen(hdr) > blob_len(attr) - sizeof(struct blobmsg_hdr))
49 return false;
50
51 if (hdr->name[blobmsg_namelen(hdr)] != 0)
52 return false;
53
54 id = blob_id(attr);
55 len = blobmsg_data_len(attr);
56 if (len > blob_raw_len(attr))
57 return false;
58
59 data = blobmsg_data(attr);
60
61 if (id > BLOBMSG_TYPE_LAST)
62 return false;
63
64 if (!blob_type[id])
65 return true;
66
67 return blob_check_type(data, len, blob_type[id]);
68 }
69
70 int blobmsg_check_array(const struct blob_attr *attr, int type)
71 {
72 struct blob_attr *cur;
73 bool name;
74 size_t rem;
75 int size = 0;
76
77 switch (blobmsg_type(attr)) {
78 case BLOBMSG_TYPE_TABLE:
79 name = true;
80 break;
81 case BLOBMSG_TYPE_ARRAY:
82 name = false;
83 break;
84 default:
85 return -1;
86 }
87
88 blobmsg_for_each_attr(cur, attr, rem) {
89 if (type != BLOBMSG_TYPE_UNSPEC && blobmsg_type(cur) != type)
90 return -1;
91
92 if (!blobmsg_check_attr(cur, name))
93 return -1;
94
95 size++;
96 }
97
98 return size;
99 }
100
101 bool blobmsg_check_attr_list(const struct blob_attr *attr, int type)
102 {
103 return blobmsg_check_array(attr, type) >= 0;
104 }
105
106 int blobmsg_parse_array(const struct blobmsg_policy *policy, int policy_len,
107 struct blob_attr **tb, void *data, unsigned int len)
108 {
109 struct blob_attr *attr;
110 int i = 0;
111
112 memset(tb, 0, policy_len * sizeof(*tb));
113 __blob_for_each_attr(attr, data, len) {
114 if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
115 blob_id(attr) != policy[i].type)
116 continue;
117
118 if (!blobmsg_check_attr(attr, false))
119 return -1;
120
121 if (tb[i])
122 continue;
123
124 tb[i++] = attr;
125 if (i == policy_len)
126 break;
127 }
128
129 return 0;
130 }
131
132
133 int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
134 struct blob_attr **tb, void *data, unsigned int len)
135 {
136 struct blobmsg_hdr *hdr;
137 struct blob_attr *attr;
138 uint8_t *pslen;
139 int i;
140
141 memset(tb, 0, policy_len * sizeof(*tb));
142 if (!data || !len)
143 return -EINVAL;
144 pslen = alloca(policy_len);
145 for (i = 0; i < policy_len; i++) {
146 if (!policy[i].name)
147 continue;
148
149 pslen[i] = strlen(policy[i].name);
150 }
151
152 __blob_for_each_attr(attr, data, len) {
153 hdr = blob_data(attr);
154 for (i = 0; i < policy_len; i++) {
155 if (!policy[i].name)
156 continue;
157
158 if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
159 blob_id(attr) != policy[i].type)
160 continue;
161
162 if (blobmsg_namelen(hdr) != pslen[i])
163 continue;
164
165 if (!blobmsg_check_attr(attr, true))
166 return -1;
167
168 if (tb[i])
169 continue;
170
171 if (strcmp(policy[i].name, (char *) hdr->name) != 0)
172 continue;
173
174 tb[i] = attr;
175 }
176 }
177
178 return 0;
179 }
180
181
182 static struct blob_attr *
183 blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len, void **data)
184 {
185 struct blob_attr *attr;
186 struct blobmsg_hdr *hdr;
187 int attrlen, namelen;
188 char *pad_start, *pad_end;
189
190 if (!name)
191 name = "";
192
193 namelen = strlen(name);
194 attrlen = blobmsg_hdrlen(namelen) + payload_len;
195 attr = blob_new(buf, type, attrlen);
196 if (!attr)
197 return NULL;
198
199 attr->id_len |= be32_to_cpu(BLOB_ATTR_EXTENDED);
200 hdr = blob_data(attr);
201 hdr->namelen = cpu_to_be16(namelen);
202 strcpy((char *) hdr->name, (const char *)name);
203 pad_end = *data = blobmsg_data(attr);
204 pad_start = (char *) &hdr->name[namelen];
205 if (pad_start < pad_end)
206 memset(pad_start, 0, pad_end - pad_start);
207
208 return attr;
209 }
210
211 static inline int
212 attr_to_offset(struct blob_buf *buf, struct blob_attr *attr)
213 {
214 return (char *)attr - (char *) buf->buf + BLOB_COOKIE;
215 }
216
217
218 void *
219 blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array)
220 {
221 struct blob_attr *head;
222 int type = array ? BLOBMSG_TYPE_ARRAY : BLOBMSG_TYPE_TABLE;
223 unsigned long offset = attr_to_offset(buf, buf->head);
224 void *data;
225
226 if (!name)
227 name = "";
228
229 head = blobmsg_new(buf, type, name, 0, &data);
230 if (!head)
231 return NULL;
232 blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blobmsg_hdrlen(strlen(name)));
233 buf->head = head;
234 return (void *)offset;
235 }
236
237 __attribute__((format(printf, 3, 0)))
238 int blobmsg_vprintf(struct blob_buf *buf, const char *name, const char *format, va_list arg)
239 {
240 va_list arg2;
241 char cbuf;
242 char *sbuf;
243 int len, ret;
244
245 va_copy(arg2, arg);
246 len = vsnprintf(&cbuf, sizeof(cbuf), format, arg2);
247 va_end(arg2);
248
249 sbuf = blobmsg_alloc_string_buffer(buf, name, len + 1);
250 if (!sbuf)
251 return -1;
252 ret = vsprintf(sbuf, format, arg);
253 blobmsg_add_string_buffer(buf);
254
255 return ret;
256 }
257
258 __attribute__((format(printf, 3, 4)))
259 int blobmsg_printf(struct blob_buf *buf, const char *name, const char *format, ...)
260 {
261 va_list ap;
262 int ret;
263
264 va_start(ap, format);
265 ret = blobmsg_vprintf(buf, name, format, ap);
266 va_end(ap);
267
268 return ret;
269 }
270
271 void *
272 blobmsg_alloc_string_buffer(struct blob_buf *buf, const char *name, unsigned int maxlen)
273 {
274 struct blob_attr *attr;
275 void *data_dest;
276
277 attr = blobmsg_new(buf, BLOBMSG_TYPE_STRING, name, maxlen, &data_dest);
278 if (!attr)
279 return NULL;
280
281 blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blob_pad_len(attr));
282 blob_set_raw_len(attr, blob_raw_len(attr) - maxlen);
283
284 return data_dest;
285 }
286
287 void *
288 blobmsg_realloc_string_buffer(struct blob_buf *buf, unsigned int maxlen)
289 {
290 struct blob_attr *attr = blob_next(buf->head);
291 int offset = attr_to_offset(buf, blob_next(buf->head)) + blob_pad_len(attr) - BLOB_COOKIE;
292 int required = maxlen - (buf->buflen - offset);
293
294 if (required <= 0)
295 goto out;
296
297 if (!blob_buf_grow(buf, required))
298 return NULL;
299 attr = blob_next(buf->head);
300
301 out:
302 return blobmsg_data(attr);
303 }
304
305 void
306 blobmsg_add_string_buffer(struct blob_buf *buf)
307 {
308 struct blob_attr *attr;
309 int len, attrlen;
310
311 attr = blob_next(buf->head);
312 len = strlen(blobmsg_data(attr)) + 1;
313
314 attrlen = blob_raw_len(attr) + len;
315 blob_set_raw_len(attr, attrlen);
316 blob_fill_pad(attr);
317
318 blob_set_raw_len(buf->head, blob_raw_len(buf->head) + blob_pad_len(attr));
319 }
320
321 int
322 blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
323 const void *data, unsigned int len)
324 {
325 struct blob_attr *attr;
326 void *data_dest;
327
328 attr = blobmsg_new(buf, type, name, len, &data_dest);
329 if (!attr)
330 return -1;
331
332 if (len > 0)
333 memcpy(data_dest, data, len);
334
335 return 0;
336 }