]> git.proxmox.com Git - mirror_qemu.git/blob - qobject/qjson.c
json: Assert json_parser_parse() consumes all tokens on success
[mirror_qemu.git] / qobject / qjson.c
1 /*
2 * QObject JSON integration
3 *
4 * Copyright IBM, Corp. 2009
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
11 *
12 */
13
14 #include "qemu/osdep.h"
15 #include "qapi/error.h"
16 #include "qapi/qmp/json-streamer.h"
17 #include "qapi/qmp/qjson.h"
18 #include "qapi/qmp/qbool.h"
19 #include "qapi/qmp/qdict.h"
20 #include "qapi/qmp/qlist.h"
21 #include "qapi/qmp/qnum.h"
22 #include "qapi/qmp/qstring.h"
23 #include "qemu/unicode.h"
24
25 typedef struct JSONParsingState
26 {
27 JSONMessageParser parser;
28 QObject *result;
29 Error *err;
30 } JSONParsingState;
31
32 static void consume_json(void *opaque, QObject *json, Error *err)
33 {
34 JSONParsingState *s = opaque;
35
36 assert(!json != !err);
37 assert(!s->result || !s->err);
38
39 if (s->result) {
40 qobject_unref(s->result);
41 s->result = NULL;
42 error_setg(&s->err, "Expecting at most one JSON value");
43 }
44 if (s->err) {
45 qobject_unref(json);
46 error_free(err);
47 return;
48 }
49 s->result = json;
50 s->err = err;
51 }
52
53 /*
54 * Parse @string as JSON value.
55 * If @ap is non-null, interpolate %-escapes.
56 * Takes ownership of %p arguments.
57 * On success, return the JSON value.
58 * On failure, store an error through @errp and return NULL.
59 * Ownership of %p arguments becomes indeterminate then. To avoid
60 * leaks, callers passing %p must terminate on error, e.g. by passing
61 * &error_abort.
62 */
63 static QObject *qobject_from_jsonv(const char *string, va_list *ap,
64 Error **errp)
65 {
66 JSONParsingState state = {};
67
68 json_message_parser_init(&state.parser, consume_json, &state, ap);
69 json_message_parser_feed(&state.parser, string, strlen(string));
70 json_message_parser_flush(&state.parser);
71 json_message_parser_destroy(&state.parser);
72
73 error_propagate(errp, state.err);
74 return state.result;
75 }
76
77 QObject *qobject_from_json(const char *string, Error **errp)
78 {
79 return qobject_from_jsonv(string, NULL, errp);
80 }
81
82 /*
83 * Parse @string as JSON value with %-escapes interpolated.
84 * Abort on error. Do not use with untrusted @string.
85 * Return the resulting QObject. It is never null.
86 */
87 QObject *qobject_from_vjsonf_nofail(const char *string, va_list ap)
88 {
89 va_list ap_copy;
90 QObject *obj;
91
92 /* va_copy() is needed when va_list is an array type */
93 va_copy(ap_copy, ap);
94 obj = qobject_from_jsonv(string, &ap_copy, &error_abort);
95 va_end(ap_copy);
96
97 assert(obj);
98 return obj;
99 }
100
101 /*
102 * Parse @string as JSON value with %-escapes interpolated.
103 * Abort on error. Do not use with untrusted @string.
104 * Return the resulting QObject. It is never null.
105 */
106 QObject *qobject_from_jsonf_nofail(const char *string, ...)
107 {
108 QObject *obj;
109 va_list ap;
110
111 va_start(ap, string);
112 obj = qobject_from_vjsonf_nofail(string, ap);
113 va_end(ap);
114
115 return obj;
116 }
117
118 /*
119 * Parse @string as JSON object with %-escapes interpolated.
120 * Abort on error. Do not use with untrusted @string.
121 * Return the resulting QDict. It is never null.
122 */
123 QDict *qdict_from_vjsonf_nofail(const char *string, va_list ap)
124 {
125 QDict *qdict;
126
127 qdict = qobject_to(QDict, qobject_from_vjsonf_nofail(string, ap));
128 assert(qdict);
129 return qdict;
130 }
131
132 /*
133 * Parse @string as JSON object with %-escapes interpolated.
134 * Abort on error. Do not use with untrusted @string.
135 * Return the resulting QDict. It is never null.
136 */
137 QDict *qdict_from_jsonf_nofail(const char *string, ...)
138 {
139 QDict *qdict;
140 va_list ap;
141
142 va_start(ap, string);
143 qdict = qdict_from_vjsonf_nofail(string, ap);
144 va_end(ap);
145 return qdict;
146 }
147
148 typedef struct ToJsonIterState
149 {
150 int indent;
151 int pretty;
152 int count;
153 QString *str;
154 } ToJsonIterState;
155
156 static void to_json(const QObject *obj, QString *str, int pretty, int indent);
157
158 static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)
159 {
160 ToJsonIterState *s = opaque;
161 QString *qkey;
162 int j;
163
164 if (s->count) {
165 qstring_append(s->str, s->pretty ? "," : ", ");
166 }
167
168 if (s->pretty) {
169 qstring_append(s->str, "\n");
170 for (j = 0 ; j < s->indent ; j++)
171 qstring_append(s->str, " ");
172 }
173
174 qkey = qstring_from_str(key);
175 to_json(QOBJECT(qkey), s->str, s->pretty, s->indent);
176 qobject_unref(qkey);
177
178 qstring_append(s->str, ": ");
179 to_json(obj, s->str, s->pretty, s->indent);
180 s->count++;
181 }
182
183 static void to_json_list_iter(QObject *obj, void *opaque)
184 {
185 ToJsonIterState *s = opaque;
186 int j;
187
188 if (s->count) {
189 qstring_append(s->str, s->pretty ? "," : ", ");
190 }
191
192 if (s->pretty) {
193 qstring_append(s->str, "\n");
194 for (j = 0 ; j < s->indent ; j++)
195 qstring_append(s->str, " ");
196 }
197
198 to_json(obj, s->str, s->pretty, s->indent);
199 s->count++;
200 }
201
202 static void to_json(const QObject *obj, QString *str, int pretty, int indent)
203 {
204 switch (qobject_type(obj)) {
205 case QTYPE_QNULL:
206 qstring_append(str, "null");
207 break;
208 case QTYPE_QNUM: {
209 QNum *val = qobject_to(QNum, obj);
210 char *buffer = qnum_to_string(val);
211 qstring_append(str, buffer);
212 g_free(buffer);
213 break;
214 }
215 case QTYPE_QSTRING: {
216 QString *val = qobject_to(QString, obj);
217 const char *ptr;
218 int cp;
219 char buf[16];
220 char *end;
221
222 ptr = qstring_get_str(val);
223 qstring_append(str, "\"");
224
225 for (; *ptr; ptr = end) {
226 cp = mod_utf8_codepoint(ptr, 6, &end);
227 switch (cp) {
228 case '\"':
229 qstring_append(str, "\\\"");
230 break;
231 case '\\':
232 qstring_append(str, "\\\\");
233 break;
234 case '\b':
235 qstring_append(str, "\\b");
236 break;
237 case '\f':
238 qstring_append(str, "\\f");
239 break;
240 case '\n':
241 qstring_append(str, "\\n");
242 break;
243 case '\r':
244 qstring_append(str, "\\r");
245 break;
246 case '\t':
247 qstring_append(str, "\\t");
248 break;
249 default:
250 if (cp < 0) {
251 cp = 0xFFFD; /* replacement character */
252 }
253 if (cp > 0xFFFF) {
254 /* beyond BMP; need a surrogate pair */
255 snprintf(buf, sizeof(buf), "\\u%04X\\u%04X",
256 0xD800 + ((cp - 0x10000) >> 10),
257 0xDC00 + ((cp - 0x10000) & 0x3FF));
258 } else if (cp < 0x20 || cp >= 0x7F) {
259 snprintf(buf, sizeof(buf), "\\u%04X", cp);
260 } else {
261 buf[0] = cp;
262 buf[1] = 0;
263 }
264 qstring_append(str, buf);
265 }
266 };
267
268 qstring_append(str, "\"");
269 break;
270 }
271 case QTYPE_QDICT: {
272 ToJsonIterState s;
273 QDict *val = qobject_to(QDict, obj);
274
275 s.count = 0;
276 s.str = str;
277 s.indent = indent + 1;
278 s.pretty = pretty;
279 qstring_append(str, "{");
280 qdict_iter(val, to_json_dict_iter, &s);
281 if (pretty) {
282 int j;
283 qstring_append(str, "\n");
284 for (j = 0 ; j < indent ; j++)
285 qstring_append(str, " ");
286 }
287 qstring_append(str, "}");
288 break;
289 }
290 case QTYPE_QLIST: {
291 ToJsonIterState s;
292 QList *val = qobject_to(QList, obj);
293
294 s.count = 0;
295 s.str = str;
296 s.indent = indent + 1;
297 s.pretty = pretty;
298 qstring_append(str, "[");
299 qlist_iter(val, (void *)to_json_list_iter, &s);
300 if (pretty) {
301 int j;
302 qstring_append(str, "\n");
303 for (j = 0 ; j < indent ; j++)
304 qstring_append(str, " ");
305 }
306 qstring_append(str, "]");
307 break;
308 }
309 case QTYPE_QBOOL: {
310 QBool *val = qobject_to(QBool, obj);
311
312 if (qbool_get_bool(val)) {
313 qstring_append(str, "true");
314 } else {
315 qstring_append(str, "false");
316 }
317 break;
318 }
319 default:
320 abort();
321 }
322 }
323
324 QString *qobject_to_json(const QObject *obj)
325 {
326 QString *str = qstring_new();
327
328 to_json(obj, str, 0, 0);
329
330 return str;
331 }
332
333 QString *qobject_to_json_pretty(const QObject *obj)
334 {
335 QString *str = qstring_new();
336
337 to_json(obj, str, 1, 0);
338
339 return str;
340 }