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