26604b1be6d9c0c6fb5bef34cb5bc5ce6c13826c
[project/libubox.git] / blobmsg.c
1 /*
2  * blobmsg - library for generating/parsing structured blob messages
3  *
4  * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License version 2.1
8  * as published by the Free Software Foundation
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include "blobmsg.h"
17
18 struct strbuf {
19         int len;
20         int pos;
21         char *buf;
22
23         blobmsg_json_format_t custom_format;
24         void *priv;
25 };
26
27 static bool blobmsg_puts(struct strbuf *s, const char *c, int len)
28 {
29         if (len <= 0)
30                 return true;
31
32         if (s->pos + len >= s->len) {
33                 s->len += 16;
34                 s->buf = realloc(s->buf, s->len);
35                 if (!s->buf)
36                         return false;
37         }
38         memcpy(s->buf + s->pos, c, len);
39         s->pos += len;
40         return true;
41 }
42
43 static void blobmsg_format_string(struct strbuf *s, const char *str)
44 {
45         const char *p, *last = str, *end = str + strlen(str);
46         char buf[8] = "\\u00";
47
48         blobmsg_puts(s, "\"", 1);
49         for (p = str; *p; p++) {
50                 char escape = '\0';
51                 int len;
52
53                 switch(*p) {
54                 case '\b':
55                         escape = 'b';
56                         break;
57                 case '\n':
58                         escape = 'n';
59                         break;
60                 case '\t':
61                         escape = 't';
62                         break;
63                 case '\r':
64                         escape = 'r';
65                         break;
66                 case '"':
67                 case '\\':
68                 case '/':
69                         escape = *p;
70                         break;
71                 default:
72                         if (*p < ' ')
73                                 escape = 'u';
74                         break;
75                 }
76
77                 if (!escape)
78                         continue;
79
80                 if (p > last)
81                         blobmsg_puts(s, last, p - last);
82                 last = p + 1;
83                 buf[1] = escape;
84
85                 if (escape == 'u') {
86                         sprintf(buf + 4, "%02x", (unsigned char) *p);
87                         len = 4;
88                 } else {
89                         len = 2;
90                 }
91                 blobmsg_puts(s, buf, len);
92         }
93
94         blobmsg_puts(s, last, end - last);
95         blobmsg_puts(s, "\"", 1);
96 }
97
98 static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array);
99
100 static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, bool array, bool head)
101 {
102         const char *data_str;
103         char buf[32];
104         void *data;
105         int len;
106
107         if (!array && blobmsg_name(attr)[0]) {
108                 blobmsg_format_string(s, blobmsg_name(attr));
109                 blobmsg_puts(s, ":", 1);
110         }
111         if (head) {
112                 data = blob_data(attr);
113                 len = blob_len(attr);
114         } else {
115                 data = blobmsg_data(attr);
116                 len = blobmsg_data_len(attr);
117
118                 if (s->custom_format) {
119                         data_str = s->custom_format(s->priv, attr);
120                         if (data_str)
121                                 goto out;
122                 }
123         }
124
125         data_str = buf;
126         switch(blob_id(attr)) {
127         case BLOBMSG_TYPE_INT8:
128                 sprintf(buf, "%d", *(uint8_t *)data);
129                 break;
130         case BLOBMSG_TYPE_INT16:
131                 sprintf(buf, "%d", *(uint16_t *)data);
132                 break;
133         case BLOBMSG_TYPE_INT32:
134                 sprintf(buf, "%d", *(uint32_t *)data);
135                 break;
136         case BLOBMSG_TYPE_INT64:
137                 sprintf(buf, "%lld", *(uint64_t *)data);
138                 break;
139         case BLOBMSG_TYPE_STRING:
140                 blobmsg_format_string(s, data);
141                 return;
142         case BLOBMSG_TYPE_ARRAY:
143                 blobmsg_format_json_list(s, data, len, true);
144                 return;
145         case BLOBMSG_TYPE_TABLE:
146                 blobmsg_format_json_list(s, data, len, false);
147                 return;
148         }
149
150 out:
151         blobmsg_puts(s, data_str, strlen(data_str));
152 }
153
154 static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array)
155 {
156         struct blob_attr *pos;
157         bool first = true;
158         int rem = len;
159
160         blobmsg_puts(s, (array ? "[ " : "{ "), 2);
161         __blob_for_each_attr(pos, attr, rem) {
162                 if (!first)
163                         blobmsg_puts(s, ", ", 2);
164
165                 blobmsg_format_element(s, pos, array, false);
166                 first = false;
167         }
168         blobmsg_puts(s, (array ? " ]" : " }"), 2);
169 }
170
171 char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, blobmsg_json_format_t cb, void *priv)
172 {
173         struct strbuf s;
174
175         s.len = blob_len(attr);
176         s.buf = malloc(s.len);
177         s.pos = 0;
178         s.custom_format = cb;
179         s.priv = priv;
180
181         if (list)
182                 blobmsg_format_json_list(&s, blob_data(attr), blob_len(attr), false);
183         else
184                 blobmsg_format_element(&s, attr, false, false);
185
186         if (!s.len)
187                 return NULL;
188
189         s.buf = realloc(s.buf, s.pos + 1);
190         return s.buf;
191 }
192
193 static const int blob_type[__BLOBMSG_TYPE_LAST] = {
194         [BLOBMSG_TYPE_INT8] = BLOB_ATTR_INT8,
195         [BLOBMSG_TYPE_INT16] = BLOB_ATTR_INT16,
196         [BLOBMSG_TYPE_INT32] = BLOB_ATTR_INT32,
197         [BLOBMSG_TYPE_INT64] = BLOB_ATTR_INT64,
198         [BLOBMSG_TYPE_STRING] = BLOB_ATTR_STRING,
199 };
200
201 bool blobmsg_check_attr(const struct blob_attr *attr, bool name)
202 {
203         const struct blobmsg_hdr *hdr;
204         const char *data;
205         int id, len;
206
207         if (blob_len(attr) < sizeof(struct blobmsg_hdr))
208                 return false;
209
210         hdr = (void *) attr->data;
211         if (!hdr->namelen && name)
212                 return false;
213
214         if (hdr->namelen > blob_len(attr) - sizeof(struct blobmsg_hdr))
215                 return false;
216
217         if (hdr->name[hdr->namelen] != 0)
218                 return false;
219
220         id = blob_id(attr);
221         len = blobmsg_data_len(attr);
222         data = blobmsg_data(attr);
223
224         if (!id || id > BLOBMSG_TYPE_LAST)
225                 return false;
226
227         if (!blob_type[id])
228                 return true;
229
230         return blob_check_type(data, len, blob_type[id]);
231 }
232
233 int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
234                   struct blob_attr **tb, void *data, int len)
235 {
236         struct blobmsg_hdr *hdr;
237         struct blob_attr *attr;
238         uint8_t *pslen;
239         int i;
240
241         memset(tb, 0, policy_len * sizeof(*tb));
242         pslen = alloca(policy_len);
243         for (i = 0; i < policy_len; i++) {
244                 if (!policy[i].name)
245                         continue;
246
247                 pslen[i] = strlen(policy[i].name);
248         }
249
250         __blob_for_each_attr(attr, data, len) {
251                 hdr = blob_data(attr);
252                 for (i = 0; i < policy_len; i++) {
253                         if (!policy[i].name)
254                                 continue;
255
256                         if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
257                             blob_id(attr) != policy[i].type)
258                                 continue;
259
260                         if (hdr->namelen != pslen[i])
261                                 continue;
262
263                         if (!blobmsg_check_attr(attr, true))
264                                 return -1;
265
266                         if (tb[i])
267                                 return -1;
268
269                         if (strcmp(policy[i].name, (char *) hdr->name) != 0)
270                                 continue;
271
272                         tb[i] = attr;
273                 }
274         }
275
276         return 0;
277 }
278
279
280 static struct blob_attr *
281 blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len, void **data)
282 {
283         struct blob_attr *attr;
284         struct blobmsg_hdr *hdr;
285         int attrlen, namelen;
286
287         if (!name)
288                 name = "";
289
290         namelen = strlen(name);
291         attrlen = blobmsg_hdrlen(namelen) + payload_len;
292         attr = blob_new(buf, type, attrlen);
293         if (!attr)
294                 return NULL;
295
296         hdr = blob_data(attr);
297         hdr->namelen = namelen;
298         strcpy((char *) hdr->name, (const char *)name);
299         *data = blobmsg_data(attr);
300
301         return attr;
302 }
303
304 static inline int
305 attr_to_offset(struct blob_buf *buf, struct blob_attr *attr)
306 {
307         return (char *)attr - (char *) buf->buf;
308 }
309
310
311 void *
312 blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array)
313 {
314         struct blob_attr *head = buf->head;
315         int type = array ? BLOBMSG_TYPE_ARRAY : BLOBMSG_TYPE_TABLE;
316         unsigned long offset = attr_to_offset(buf, buf->head);
317         void *data;
318
319         if (!name)
320                 name = "";
321
322         head = blobmsg_new(buf, type, name, 0, &data);
323         blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blobmsg_hdrlen(strlen(name)));
324         buf->head = head;
325         return (void *)offset;
326 }
327
328 void *
329 blobmsg_alloc_string_buffer(struct blob_buf *buf, const char *name, int maxlen)
330 {
331         struct blob_attr *attr;
332         void *data_dest;
333
334         attr = blobmsg_new(buf, BLOBMSG_TYPE_STRING, name, maxlen, &data_dest);
335         if (!attr)
336                 return NULL;
337
338         data_dest = blobmsg_data(attr);
339         blob_set_raw_len(buf->head, blob_pad_len(buf->head) - blob_pad_len(attr));
340         blob_set_raw_len(attr, blob_raw_len(attr) - maxlen);
341
342         return data_dest;
343 }
344
345 void
346 blobmsg_add_string_buffer(struct blob_buf *buf)
347 {
348         struct blob_attr *attr;
349         int len, attrlen;
350
351         attr = blob_next(buf->head);
352         len = strlen(blobmsg_data(attr)) + 1;
353
354         attrlen = blob_raw_len(attr) + len;
355         blob_set_raw_len(attr, attrlen);
356         blob_set_raw_len(buf->head, blob_raw_len(buf->head) + blob_pad_len(attr));
357 }
358
359 int
360 blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
361                   const void *data, int len)
362 {
363         struct blob_attr *attr;
364         void *data_dest;
365
366         if (type == BLOBMSG_TYPE_ARRAY ||
367             type == BLOBMSG_TYPE_TABLE)
368                 return -1;
369
370         attr = blobmsg_new(buf, type, name, len, &data_dest);
371         if (!attr)
372                 return -1;
373
374         if (len > 0)
375                 memcpy(data_dest, data, len);
376
377         return 0;
378 }