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