ustream: Add format string checks to ustream_(v)printf()
[oweals/libubox.git] / blobmsg_json.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 <inttypes.h>
17 #include "blobmsg.h"
18 #include "blobmsg_json.h"
19
20 #ifdef JSONC
21         #include <json.h>
22 #else
23         #include <json/json.h>
24 #endif
25
26 bool blobmsg_add_object(struct blob_buf *b, json_object *obj)
27 {
28         json_object_object_foreach(obj, key, val) {
29                 if (!blobmsg_add_json_element(b, key, val))
30                         return false;
31         }
32         return true;
33 }
34
35 static bool blobmsg_add_array(struct blob_buf *b, struct array_list *a)
36 {
37         int i, len;
38
39         for (i = 0, len = array_list_length(a); i < len; i++) {
40                 if (!blobmsg_add_json_element(b, NULL, array_list_get_idx(a, i)))
41                         return false;
42         }
43
44         return true;
45 }
46
47 bool blobmsg_add_json_element(struct blob_buf *b, const char *name, json_object *obj)
48 {
49         bool ret = true;
50         void *c;
51
52         switch (json_object_get_type(obj)) {
53         case json_type_object:
54                 c = blobmsg_open_table(b, name);
55                 ret = blobmsg_add_object(b, obj);
56                 blobmsg_close_table(b, c);
57                 break;
58         case json_type_array:
59                 c = blobmsg_open_array(b, name);
60                 ret = blobmsg_add_array(b, json_object_get_array(obj));
61                 blobmsg_close_array(b, c);
62                 break;
63         case json_type_string:
64                 blobmsg_add_string(b, name, json_object_get_string(obj));
65                 break;
66         case json_type_boolean:
67                 blobmsg_add_u8(b, name, json_object_get_boolean(obj));
68                 break;
69         case json_type_int:
70                 blobmsg_add_u32(b, name, json_object_get_int(obj));
71                 break;
72         case json_type_double:
73                 blobmsg_add_double(b, name, json_object_get_double(obj));
74                 break;
75         case json_type_null:
76                 blobmsg_add_field(b, BLOBMSG_TYPE_UNSPEC, name, NULL, 0);
77                 break;
78         default:
79                 return false;
80         }
81         return ret;
82 }
83
84 static bool __blobmsg_add_json(struct blob_buf *b, json_object *obj)
85 {
86         bool ret = false;
87
88         if (!obj)
89                 return false;
90
91         if (json_object_get_type(obj) != json_type_object)
92                 goto out;
93
94         ret = blobmsg_add_object(b, obj);
95
96 out:
97         json_object_put(obj);
98         return ret;
99 }
100
101 bool blobmsg_add_json_from_file(struct blob_buf *b, const char *file)
102 {
103         return __blobmsg_add_json(b, json_object_from_file(file));
104 }
105
106 bool blobmsg_add_json_from_string(struct blob_buf *b, const char *str)
107 {
108         return __blobmsg_add_json(b, json_tokener_parse(str));
109 }
110
111
112 struct strbuf {
113         int len;
114         int pos;
115         char *buf;
116
117         blobmsg_json_format_t custom_format;
118         void *priv;
119         bool indent;
120         int indent_level;
121 };
122
123 static bool blobmsg_puts(struct strbuf *s, const char *c, int len)
124 {
125         size_t new_len;
126         char *new_buf;
127
128         if (len <= 0)
129                 return true;
130
131         if (s->pos + len >= s->len) {
132                 new_len = s->len + 16 + len;
133                 new_buf = realloc(s->buf, new_len);
134                 if (!new_buf)
135                         return false;
136
137                 s->len = new_len;
138                 s->buf = new_buf;
139         }
140
141         memcpy(s->buf + s->pos, c, len);
142         s->pos += len;
143         return true;
144 }
145
146 static void add_separator(struct strbuf *s)
147 {
148         const char *indent_chars = "\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
149         int len;
150
151         if (!s->indent)
152                 return;
153
154         len = s->indent_level + 1;
155         if (len > strlen(indent_chars))
156                 len = strlen(indent_chars);
157
158         blobmsg_puts(s, indent_chars, len);
159 }
160
161
162 static void blobmsg_format_string(struct strbuf *s, const char *str)
163 {
164         const unsigned char *p, *last, *end;
165         char buf[8] = "\\u00";
166
167         end = (unsigned char *) str + strlen(str);
168         blobmsg_puts(s, "\"", 1);
169         for (p = (unsigned char *) str, last = p; *p; p++) {
170                 char escape = '\0';
171                 int len;
172
173                 switch(*p) {
174                 case '\b':
175                         escape = 'b';
176                         break;
177                 case '\n':
178                         escape = 'n';
179                         break;
180                 case '\t':
181                         escape = 't';
182                         break;
183                 case '\r':
184                         escape = 'r';
185                         break;
186                 case '"':
187                 case '\\':
188                         escape = *p;
189                         break;
190                 default:
191                         if (*p < ' ')
192                                 escape = 'u';
193                         break;
194                 }
195
196                 if (!escape)
197                         continue;
198
199                 if (p > last)
200                         blobmsg_puts(s, (char *) last, p - last);
201                 last = p + 1;
202                 buf[1] = escape;
203
204                 if (escape == 'u') {
205                         sprintf(buf + 4, "%02x", (unsigned char) *p);
206                         len = 6;
207                 } else {
208                         len = 2;
209                 }
210                 blobmsg_puts(s, buf, len);
211         }
212
213         blobmsg_puts(s, (char *) last, end - last);
214         blobmsg_puts(s, "\"", 1);
215 }
216
217 static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array);
218
219 static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, bool without_name, bool head)
220 {
221         const char *data_str;
222         char buf[32];
223         void *data;
224         int len;
225
226         if (!blobmsg_check_attr(attr, false))
227                 return;
228
229         if (!without_name && blobmsg_name(attr)[0]) {
230                 blobmsg_format_string(s, blobmsg_name(attr));
231                 blobmsg_puts(s, ": ", s->indent ? 2 : 1);
232         }
233
234         data = blobmsg_data(attr);
235         len = blobmsg_data_len(attr);
236
237         if (!head && s->custom_format) {
238                 data_str = s->custom_format(s->priv, attr);
239                 if (data_str)
240                         goto out;
241         }
242
243         data_str = buf;
244         switch(blob_id(attr)) {
245         case BLOBMSG_TYPE_UNSPEC:
246                 sprintf(buf, "null");
247                 break;
248         case BLOBMSG_TYPE_BOOL:
249                 sprintf(buf, "%s", *(uint8_t *)data ? "true" : "false");
250                 break;
251         case BLOBMSG_TYPE_INT16:
252                 sprintf(buf, "%d", be16_to_cpu(*(uint16_t *)data));
253                 break;
254         case BLOBMSG_TYPE_INT32:
255                 sprintf(buf, "%d", (int32_t) be32_to_cpu(*(uint32_t *)data));
256                 break;
257         case BLOBMSG_TYPE_INT64:
258                 sprintf(buf, "%" PRId64, (int64_t) be64_to_cpu(*(uint64_t *)data));
259                 break;
260         case BLOBMSG_TYPE_DOUBLE:
261                 sprintf(buf, "%lf", blobmsg_get_double(attr));
262                 break;
263         case BLOBMSG_TYPE_STRING:
264                 blobmsg_format_string(s, data);
265                 return;
266         case BLOBMSG_TYPE_ARRAY:
267                 blobmsg_format_json_list(s, data, len, true);
268                 return;
269         case BLOBMSG_TYPE_TABLE:
270                 blobmsg_format_json_list(s, data, len, false);
271                 return;
272         }
273
274 out:
275         blobmsg_puts(s, data_str, strlen(data_str));
276 }
277
278 static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array)
279 {
280         struct blob_attr *pos;
281         bool first = true;
282         int rem = len;
283
284         blobmsg_puts(s, (array ? "[" : "{" ), 1);
285         s->indent_level++;
286         add_separator(s);
287         __blob_for_each_attr(pos, attr, rem) {
288                 if (!first) {
289                         blobmsg_puts(s, ",", 1);
290                         add_separator(s);
291                 }
292
293                 blobmsg_format_element(s, pos, array, false);
294                 first = false;
295         }
296         s->indent_level--;
297         add_separator(s);
298         blobmsg_puts(s, (array ? "]" : "}"), 1);
299 }
300
301 static void setup_strbuf(struct strbuf *s, struct blob_attr *attr, blobmsg_json_format_t cb, void *priv, int indent)
302 {
303         s->len = blob_len(attr);
304         s->buf = malloc(s->len);
305         s->pos = 0;
306         s->custom_format = cb;
307         s->priv = priv;
308         s->indent = false;
309
310         if (indent >= 0) {
311                 s->indent = true;
312                 s->indent_level = indent;
313         }
314 }
315
316 char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, blobmsg_json_format_t cb, void *priv, int indent)
317 {
318         struct strbuf s;
319         bool array;
320         char *ret;
321
322         setup_strbuf(&s, attr, cb, priv, indent);
323         if (!s.buf)
324                 return NULL;
325
326         array = blob_is_extended(attr) &&
327                 blobmsg_type(attr) == BLOBMSG_TYPE_ARRAY;
328
329         if (list)
330                 blobmsg_format_json_list(&s, blobmsg_data(attr), blobmsg_data_len(attr), array);
331         else
332                 blobmsg_format_element(&s, attr, false, false);
333
334         if (!s.len) {
335                 free(s.buf);
336                 return NULL;
337         }
338
339         ret = realloc(s.buf, s.pos + 1);
340         if (!ret) {
341                 free(s.buf);
342                 return NULL;
343         }
344
345         ret[s.pos] = 0;
346
347         return ret;
348 }
349
350 char *blobmsg_format_json_value_with_cb(struct blob_attr *attr, blobmsg_json_format_t cb, void *priv, int indent)
351 {
352         struct strbuf s;
353         char *ret;
354
355         setup_strbuf(&s, attr, cb, priv, indent);
356         if (!s.buf)
357                 return NULL;
358
359         blobmsg_format_element(&s, attr, true, false);
360
361         if (!s.len) {
362                 free(s.buf);
363                 return NULL;
364         }
365
366         ret = realloc(s.buf, s.pos + 1);
367         if (!ret) {
368                 free(s.buf);
369                 return NULL;
370         }
371
372         ret[s.pos] = 0;
373
374         return ret;
375 }