jshn: add functionality to read big JSON
[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         pslen = alloca(policy_len);
139         for (i = 0; i < policy_len; i++) {
140                 if (!policy[i].name)
141                         continue;
142
143                 pslen[i] = strlen(policy[i].name);
144         }
145
146         __blob_for_each_attr(attr, data, len) {
147                 hdr = blob_data(attr);
148                 for (i = 0; i < policy_len; i++) {
149                         if (!policy[i].name)
150                                 continue;
151
152                         if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
153                             blob_id(attr) != policy[i].type)
154                                 continue;
155
156                         if (blobmsg_namelen(hdr) != pslen[i])
157                                 continue;
158
159                         if (!blobmsg_check_attr(attr, true))
160                                 return -1;
161
162                         if (tb[i])
163                                 continue;
164
165                         if (strcmp(policy[i].name, (char *) hdr->name) != 0)
166                                 continue;
167
168                         tb[i] = attr;
169                 }
170         }
171
172         return 0;
173 }
174
175
176 static struct blob_attr *
177 blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len, void **data)
178 {
179         struct blob_attr *attr;
180         struct blobmsg_hdr *hdr;
181         int attrlen, namelen;
182         char *pad_start, *pad_end;
183
184         if (!name)
185                 name = "";
186
187         namelen = strlen(name);
188         attrlen = blobmsg_hdrlen(namelen) + payload_len;
189         attr = blob_new(buf, type, attrlen);
190         if (!attr)
191                 return NULL;
192
193         attr->id_len |= be32_to_cpu(BLOB_ATTR_EXTENDED);
194         hdr = blob_data(attr);
195         hdr->namelen = cpu_to_be16(namelen);
196         strcpy((char *) hdr->name, (const char *)name);
197         pad_end = *data = blobmsg_data(attr);
198         pad_start = (char *) &hdr->name[namelen];
199         if (pad_start < pad_end)
200                 memset(pad_start, 0, pad_end - pad_start);
201
202         return attr;
203 }
204
205 static inline int
206 attr_to_offset(struct blob_buf *buf, struct blob_attr *attr)
207 {
208         return (char *)attr - (char *) buf->buf + BLOB_COOKIE;
209 }
210
211
212 void *
213 blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array)
214 {
215         struct blob_attr *head;
216         int type = array ? BLOBMSG_TYPE_ARRAY : BLOBMSG_TYPE_TABLE;
217         unsigned long offset = attr_to_offset(buf, buf->head);
218         void *data;
219
220         if (!name)
221                 name = "";
222
223         head = blobmsg_new(buf, type, name, 0, &data);
224         if (!head)
225                 return NULL;
226         blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blobmsg_hdrlen(strlen(name)));
227         buf->head = head;
228         return (void *)offset;
229 }
230
231 int
232 blobmsg_vprintf(struct blob_buf *buf, const char *name, const char *format, va_list arg)
233 {
234         va_list arg2;
235         char cbuf;
236         char *sbuf;
237         int len, ret;
238
239         va_copy(arg2, arg);
240         len = vsnprintf(&cbuf, sizeof(cbuf), format, arg2);
241         va_end(arg2);
242
243         sbuf = blobmsg_alloc_string_buffer(buf, name, len + 1);
244         if (!sbuf)
245                 return -1;
246         ret = vsprintf(sbuf, format, arg);
247         blobmsg_add_string_buffer(buf);
248
249         return ret;
250 }
251
252 int
253 blobmsg_printf(struct blob_buf *buf, const char *name, const char *format, ...)
254 {
255         va_list ap;
256         int ret;
257
258         va_start(ap, format);
259         ret = blobmsg_vprintf(buf, name, format, ap);
260         va_end(ap);
261
262         return ret;
263 }
264
265 void *
266 blobmsg_alloc_string_buffer(struct blob_buf *buf, const char *name, unsigned int maxlen)
267 {
268         struct blob_attr *attr;
269         void *data_dest;
270
271         attr = blobmsg_new(buf, BLOBMSG_TYPE_STRING, name, maxlen, &data_dest);
272         if (!attr)
273                 return NULL;
274
275         blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blob_pad_len(attr));
276         blob_set_raw_len(attr, blob_raw_len(attr) - maxlen);
277
278         return data_dest;
279 }
280
281 void *
282 blobmsg_realloc_string_buffer(struct blob_buf *buf, unsigned int maxlen)
283 {
284         struct blob_attr *attr = blob_next(buf->head);
285         int offset = attr_to_offset(buf, blob_next(buf->head)) + blob_pad_len(attr) - BLOB_COOKIE;
286         int required = maxlen - (buf->buflen - offset);
287
288         if (required <= 0)
289                 goto out;
290
291         if (!blob_buf_grow(buf, required))
292                 return NULL;
293         attr = blob_next(buf->head);
294
295 out:
296         return blobmsg_data(attr);
297 }
298
299 void
300 blobmsg_add_string_buffer(struct blob_buf *buf)
301 {
302         struct blob_attr *attr;
303         int len, attrlen;
304
305         attr = blob_next(buf->head);
306         len = strlen(blobmsg_data(attr)) + 1;
307
308         attrlen = blob_raw_len(attr) + len;
309         blob_set_raw_len(attr, attrlen);
310         blob_fill_pad(attr);
311
312         blob_set_raw_len(buf->head, blob_raw_len(buf->head) + blob_pad_len(attr));
313 }
314
315 int
316 blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
317                   const void *data, unsigned int len)
318 {
319         struct blob_attr *attr;
320         void *data_dest;
321
322         attr = blobmsg_new(buf, type, name, len, &data_dest);
323         if (!attr)
324                 return -1;
325
326         if (len > 0)
327                 memcpy(data_dest, data, len);
328
329         return 0;
330 }