blob: 8f4badc6d9957eab2e1385799855fff23d2ba501 [file] [log] [blame]
Anthony Liguori4a5fcab2009-11-11 10:39:23 -06001/*
Eric Blake6e8e5cb2016-01-29 06:48:37 -07002 * JSON Parser
Anthony Liguori4a5fcab2009-11-11 10:39:23 -06003 *
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
Peter Maydellf2ad72b2016-01-29 17:50:01 +000014#include "qemu/osdep.h"
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +040015#include "qemu/cutils.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010016#include "qapi/error.h"
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060017#include "qemu-common.h"
Markus Armbruster6b673952018-02-01 12:18:35 +010018#include "qapi/qmp/qbool.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010019#include "qapi/qmp/qnull.h"
20#include "qapi/qmp/qnum.h"
Markus Armbruster6b673952018-02-01 12:18:35 +010021#include "qapi/qmp/qstring.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010022#include "qapi/qmp/json-parser.h"
23#include "qapi/qmp/json-lexer.h"
Paolo Bonzini9bada892015-11-25 22:23:32 +010024#include "qapi/qmp/json-streamer.h"
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060025
26typedef struct JSONParserContext
27{
Anthony Liguorief749d02011-06-01 12:14:50 -050028 Error *err;
Paolo Bonzini9bada892015-11-25 22:23:32 +010029 JSONToken *current;
Paolo Bonzini95385fe2015-11-25 22:23:31 +010030 GQueue *buf;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060031} JSONParserContext;
32
33#define BUG_ON(cond) assert(!(cond))
34
35/**
36 * TODO
37 *
38 * 0) make errors meaningful again
39 * 1) add geometry information to tokens
40 * 3) should we return a parsed size?
41 * 4) deal with premature EOI
42 */
43
Michael Roth65c0f1e2012-08-15 13:45:43 -050044static QObject *parse_value(JSONParserContext *ctxt, va_list *ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060045
46/**
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060047 * Error handler
48 */
Stefan Weil8b7968f2010-09-23 21:28:05 +020049static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext *ctxt,
Paolo Bonzini9bada892015-11-25 22:23:32 +010050 JSONToken *token, const char *msg, ...)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060051{
Amos Kongc96c84a2010-03-24 23:12:05 +080052 va_list ap;
Anthony Liguorief749d02011-06-01 12:14:50 -050053 char message[1024];
Amos Kongc96c84a2010-03-24 23:12:05 +080054 va_start(ap, msg);
Anthony Liguorief749d02011-06-01 12:14:50 -050055 vsnprintf(message, sizeof(message), msg, ap);
Amos Kongc96c84a2010-03-24 23:12:05 +080056 va_end(ap);
Anthony Liguorief749d02011-06-01 12:14:50 -050057 if (ctxt->err) {
58 error_free(ctxt->err);
59 ctxt->err = NULL;
60 }
Cole Robinsonf231b882014-03-21 19:42:26 -040061 error_setg(&ctxt->err, "JSON parse error, %s", message);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060062}
63
64/**
65 * String helpers
66 *
67 * These helpers are used to unescape strings.
68 */
69static void wchar_to_utf8(uint16_t wchar, char *buffer, size_t buffer_length)
70{
71 if (wchar <= 0x007F) {
72 BUG_ON(buffer_length < 2);
73
74 buffer[0] = wchar & 0x7F;
75 buffer[1] = 0;
76 } else if (wchar <= 0x07FF) {
77 BUG_ON(buffer_length < 3);
78
79 buffer[0] = 0xC0 | ((wchar >> 6) & 0x1F);
80 buffer[1] = 0x80 | (wchar & 0x3F);
81 buffer[2] = 0;
82 } else {
83 BUG_ON(buffer_length < 4);
84
85 buffer[0] = 0xE0 | ((wchar >> 12) & 0x0F);
86 buffer[1] = 0x80 | ((wchar >> 6) & 0x3F);
87 buffer[2] = 0x80 | (wchar & 0x3F);
88 buffer[3] = 0;
89 }
90}
91
92static int hex2decimal(char ch)
93{
94 if (ch >= '0' && ch <= '9') {
95 return (ch - '0');
96 } else if (ch >= 'a' && ch <= 'f') {
97 return 10 + (ch - 'a');
98 } else if (ch >= 'A' && ch <= 'F') {
99 return 10 + (ch - 'A');
100 }
101
102 return -1;
103}
104
105/**
106 * parse_string(): Parse a json string and return a QObject
107 *
108 * string
109 * ""
110 * " chars "
111 * chars
112 * char
113 * char chars
114 * char
115 * any-Unicode-character-
116 * except-"-or-\-or-
117 * control-character
118 * \"
119 * \\
120 * \/
121 * \b
122 * \f
123 * \n
124 * \r
125 * \t
126 * \u four-hex-digits
127 */
Paolo Bonzini9bada892015-11-25 22:23:32 +0100128static QString *qstring_from_escaped_str(JSONParserContext *ctxt,
129 JSONToken *token)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600130{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100131 const char *ptr = token->str;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600132 QString *str;
133 int double_quote = 1;
134
135 if (*ptr == '"') {
136 double_quote = 1;
137 } else {
138 double_quote = 0;
139 }
140 ptr++;
141
142 str = qstring_new();
143 while (*ptr &&
144 ((double_quote && *ptr != '"') || (!double_quote && *ptr != '\''))) {
145 if (*ptr == '\\') {
146 ptr++;
147
148 switch (*ptr) {
149 case '"':
150 qstring_append(str, "\"");
151 ptr++;
152 break;
153 case '\'':
154 qstring_append(str, "'");
155 ptr++;
156 break;
157 case '\\':
158 qstring_append(str, "\\");
159 ptr++;
160 break;
161 case '/':
162 qstring_append(str, "/");
163 ptr++;
164 break;
165 case 'b':
166 qstring_append(str, "\b");
167 ptr++;
168 break;
Luiz Capitulinobd032692010-05-19 17:06:15 -0300169 case 'f':
170 qstring_append(str, "\f");
171 ptr++;
172 break;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600173 case 'n':
174 qstring_append(str, "\n");
175 ptr++;
176 break;
177 case 'r':
178 qstring_append(str, "\r");
179 ptr++;
180 break;
181 case 't':
182 qstring_append(str, "\t");
183 ptr++;
184 break;
185 case 'u': {
186 uint16_t unicode_char = 0;
187 char utf8_char[4];
188 int i = 0;
189
190 ptr++;
191
192 for (i = 0; i < 4; i++) {
193 if (qemu_isxdigit(*ptr)) {
194 unicode_char |= hex2decimal(*ptr) << ((3 - i) * 4);
195 } else {
196 parse_error(ctxt, token,
197 "invalid hex escape sequence in string");
198 goto out;
199 }
200 ptr++;
201 }
202
203 wchar_to_utf8(unicode_char, utf8_char, sizeof(utf8_char));
204 qstring_append(str, utf8_char);
205 } break;
206 default:
207 parse_error(ctxt, token, "invalid escape sequence in string");
208 goto out;
209 }
210 } else {
211 char dummy[2];
212
213 dummy[0] = *ptr++;
214 dummy[1] = 0;
215
216 qstring_append(str, dummy);
217 }
218 }
219
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600220 return str;
221
222out:
223 QDECREF(str);
224 return NULL;
225}
226
Paolo Bonzini9bada892015-11-25 22:23:32 +0100227/* Note: the token object returned by parser_context_peek_token or
228 * parser_context_pop_token is deleted as soon as parser_context_pop_token
229 * is called again.
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100230 */
Paolo Bonzini9bada892015-11-25 22:23:32 +0100231static JSONToken *parser_context_pop_token(JSONParserContext *ctxt)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500232{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100233 g_free(ctxt->current);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100234 assert(!g_queue_is_empty(ctxt->buf));
235 ctxt->current = g_queue_pop_head(ctxt->buf);
236 return ctxt->current;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500237}
238
Paolo Bonzini9bada892015-11-25 22:23:32 +0100239static JSONToken *parser_context_peek_token(JSONParserContext *ctxt)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500240{
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100241 assert(!g_queue_is_empty(ctxt->buf));
242 return g_queue_peek_head(ctxt->buf);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500243}
244
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100245static JSONParserContext *parser_context_new(GQueue *tokens)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500246{
247 JSONParserContext *ctxt;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500248
249 if (!tokens) {
250 return NULL;
251 }
252
Michael Roth65c0f1e2012-08-15 13:45:43 -0500253 ctxt = g_malloc0(sizeof(JSONParserContext));
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100254 ctxt->buf = tokens;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500255
256 return ctxt;
257}
258
259/* to support error propagation, ctxt->err must be freed separately */
260static void parser_context_free(JSONParserContext *ctxt)
261{
Michael Roth65c0f1e2012-08-15 13:45:43 -0500262 if (ctxt) {
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100263 while (!g_queue_is_empty(ctxt->buf)) {
264 parser_context_pop_token(ctxt);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500265 }
Paolo Bonzini9bada892015-11-25 22:23:32 +0100266 g_free(ctxt->current);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100267 g_queue_free(ctxt->buf);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500268 g_free(ctxt);
269 }
270}
271
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600272/**
273 * Parsing rules
274 */
Michael Roth65c0f1e2012-08-15 13:45:43 -0500275static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600276{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100277 QObject *key = NULL, *value;
278 JSONToken *peek, *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600279
Michael Roth65c0f1e2012-08-15 13:45:43 -0500280 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500281 if (peek == NULL) {
282 parse_error(ctxt, NULL, "premature EOI");
283 goto out;
284 }
285
Michael Roth65c0f1e2012-08-15 13:45:43 -0500286 key = parse_value(ctxt, ap);
Kevin Wolfd758d902010-02-24 16:17:58 +0100287 if (!key || qobject_type(key) != QTYPE_QSTRING) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600288 parse_error(ctxt, peek, "key is not a string in object");
289 goto out;
290 }
291
Michael Roth65c0f1e2012-08-15 13:45:43 -0500292 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500293 if (token == NULL) {
294 parse_error(ctxt, NULL, "premature EOI");
295 goto out;
296 }
297
Paolo Bonzini9bada892015-11-25 22:23:32 +0100298 if (token->type != JSON_COLON) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600299 parse_error(ctxt, token, "missing : in object pair");
300 goto out;
301 }
302
Michael Roth65c0f1e2012-08-15 13:45:43 -0500303 value = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600304 if (value == NULL) {
305 parse_error(ctxt, token, "Missing value in dict");
306 goto out;
307 }
308
309 qdict_put_obj(dict, qstring_get_str(qobject_to_qstring(key)), value);
310
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600311 qobject_decref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600312
313 return 0;
314
315out:
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600316 qobject_decref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600317
318 return -1;
319}
320
Michael Roth65c0f1e2012-08-15 13:45:43 -0500321static QObject *parse_object(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600322{
323 QDict *dict = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100324 JSONToken *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600325
Michael Roth65c0f1e2012-08-15 13:45:43 -0500326 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100327 assert(token && token->type == JSON_LCURLY);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600328
329 dict = qdict_new();
330
Michael Roth65c0f1e2012-08-15 13:45:43 -0500331 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500332 if (peek == NULL) {
333 parse_error(ctxt, NULL, "premature EOI");
334 goto out;
335 }
336
Paolo Bonzini9bada892015-11-25 22:23:32 +0100337 if (peek->type != JSON_RCURLY) {
Michael Roth65c0f1e2012-08-15 13:45:43 -0500338 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600339 goto out;
340 }
341
Michael Roth65c0f1e2012-08-15 13:45:43 -0500342 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500343 if (token == NULL) {
344 parse_error(ctxt, NULL, "premature EOI");
345 goto out;
346 }
347
Paolo Bonzini9bada892015-11-25 22:23:32 +0100348 while (token->type != JSON_RCURLY) {
349 if (token->type != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600350 parse_error(ctxt, token, "expected separator in dict");
351 goto out;
352 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600353
Michael Roth65c0f1e2012-08-15 13:45:43 -0500354 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600355 goto out;
356 }
357
Michael Roth65c0f1e2012-08-15 13:45:43 -0500358 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500359 if (token == NULL) {
360 parse_error(ctxt, NULL, "premature EOI");
361 goto out;
362 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600363 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600364 } else {
Gongleia491af42014-06-10 17:20:24 +0800365 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600366 }
367
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600368 return QOBJECT(dict);
369
370out:
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600371 QDECREF(dict);
372 return NULL;
373}
374
Michael Roth65c0f1e2012-08-15 13:45:43 -0500375static QObject *parse_array(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600376{
377 QList *list = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100378 JSONToken *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600379
Michael Roth65c0f1e2012-08-15 13:45:43 -0500380 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100381 assert(token && token->type == JSON_LSQUARE);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600382
383 list = qlist_new();
384
Michael Roth65c0f1e2012-08-15 13:45:43 -0500385 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500386 if (peek == NULL) {
387 parse_error(ctxt, NULL, "premature EOI");
388 goto out;
389 }
390
Paolo Bonzini9bada892015-11-25 22:23:32 +0100391 if (peek->type != JSON_RSQUARE) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600392 QObject *obj;
393
Michael Roth65c0f1e2012-08-15 13:45:43 -0500394 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600395 if (obj == NULL) {
396 parse_error(ctxt, token, "expecting value");
397 goto out;
398 }
399
400 qlist_append_obj(list, obj);
401
Michael Roth65c0f1e2012-08-15 13:45:43 -0500402 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500403 if (token == NULL) {
404 parse_error(ctxt, NULL, "premature EOI");
405 goto out;
406 }
407
Paolo Bonzini9bada892015-11-25 22:23:32 +0100408 while (token->type != JSON_RSQUARE) {
409 if (token->type != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600410 parse_error(ctxt, token, "expected separator in list");
411 goto out;
412 }
413
Michael Roth65c0f1e2012-08-15 13:45:43 -0500414 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600415 if (obj == NULL) {
416 parse_error(ctxt, token, "expecting value");
417 goto out;
418 }
419
420 qlist_append_obj(list, obj);
421
Michael Roth65c0f1e2012-08-15 13:45:43 -0500422 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500423 if (token == NULL) {
424 parse_error(ctxt, NULL, "premature EOI");
425 goto out;
426 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600427 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600428 } else {
Gongleia491af42014-06-10 17:20:24 +0800429 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600430 }
431
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600432 return QOBJECT(list);
433
434out:
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600435 QDECREF(list);
436 return NULL;
437}
438
Michael Roth65c0f1e2012-08-15 13:45:43 -0500439static QObject *parse_keyword(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600440{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100441 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600442
Michael Roth65c0f1e2012-08-15 13:45:43 -0500443 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100444 assert(token && token->type == JSON_KEYWORD);
Markus Armbruster50e2a462015-11-25 22:23:27 +0100445
Paolo Bonzini9bada892015-11-25 22:23:32 +0100446 if (!strcmp(token->str, "true")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100447 return QOBJECT(qbool_from_bool(true));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100448 } else if (!strcmp(token->str, "false")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100449 return QOBJECT(qbool_from_bool(false));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100450 } else if (!strcmp(token->str, "null")) {
Markus Armbruster006ca092017-06-26 13:52:24 +0200451 return QOBJECT(qnull());
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600452 }
Paolo Bonzini9bada892015-11-25 22:23:32 +0100453 parse_error(ctxt, token, "invalid keyword '%s'", token->str);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600454 return NULL;
455}
456
Michael Roth65c0f1e2012-08-15 13:45:43 -0500457static QObject *parse_escape(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600458{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100459 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600460
461 if (ap == NULL) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100462 return NULL;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600463 }
464
Michael Roth65c0f1e2012-08-15 13:45:43 -0500465 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100466 assert(token && token->type == JSON_ESCAPE);
Markus Armbruster6b9606f2015-11-25 22:23:28 +0100467
Paolo Bonzini9bada892015-11-25 22:23:32 +0100468 if (!strcmp(token->str, "%p")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100469 return va_arg(*ap, QObject *);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100470 } else if (!strcmp(token->str, "%i")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100471 return QOBJECT(qbool_from_bool(va_arg(*ap, int)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100472 } else if (!strcmp(token->str, "%d")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400473 return QOBJECT(qnum_from_int(va_arg(*ap, int)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100474 } else if (!strcmp(token->str, "%ld")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400475 return QOBJECT(qnum_from_int(va_arg(*ap, long)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100476 } else if (!strcmp(token->str, "%lld") ||
477 !strcmp(token->str, "%I64d")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400478 return QOBJECT(qnum_from_int(va_arg(*ap, long long)));
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400479 } else if (!strcmp(token->str, "%u")) {
480 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned int)));
481 } else if (!strcmp(token->str, "%lu")) {
482 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned long)));
483 } else if (!strcmp(token->str, "%llu") ||
484 !strcmp(token->str, "%I64u")) {
485 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned long long)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100486 } else if (!strcmp(token->str, "%s")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100487 return QOBJECT(qstring_from_str(va_arg(*ap, const char *)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100488 } else if (!strcmp(token->str, "%f")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400489 return QOBJECT(qnum_from_double(va_arg(*ap, double)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600490 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600491 return NULL;
492}
493
Michael Roth65c0f1e2012-08-15 13:45:43 -0500494static QObject *parse_literal(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600495{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100496 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600497
Michael Roth65c0f1e2012-08-15 13:45:43 -0500498 token = parser_context_pop_token(ctxt);
Markus Armbrusterd538b252015-11-25 22:23:30 +0100499 assert(token);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500500
Paolo Bonzini9bada892015-11-25 22:23:32 +0100501 switch (token->type) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600502 case JSON_STRING:
Markus Armbrusterd538b252015-11-25 22:23:30 +0100503 return QOBJECT(qstring_from_escaped_str(ctxt, token));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500504 case JSON_INTEGER: {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400505 /*
506 * Represent JSON_INTEGER as QNUM_I64 if possible, else as
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400507 * QNUM_U64, else as QNUM_DOUBLE. Note that qemu_strtoi64()
508 * and qemu_strtou64() fail with ERANGE when it's not
509 * possible.
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500510 *
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400511 * qnum_get_int() will then work for any signed 64-bit
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400512 * JSON_INTEGER, qnum_get_uint() for any unsigned 64-bit
513 * integer, and qnum_get_double() both for any JSON_INTEGER
514 * and any JSON_FLOAT (with precision loss for integers beyond
515 * 53 bits)
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500516 */
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400517 int ret;
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500518 int64_t value;
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400519 uint64_t uvalue;
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500520
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400521 ret = qemu_strtoi64(token->str, NULL, 10, &value);
522 if (!ret) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400523 return QOBJECT(qnum_from_int(value));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500524 }
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400525 assert(ret == -ERANGE);
526
527 if (token->str[0] != '-') {
528 ret = qemu_strtou64(token->str, NULL, 10, &uvalue);
529 if (!ret) {
530 return QOBJECT(qnum_from_uint(uvalue));
531 }
532 assert(ret == -ERANGE);
533 }
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500534 /* fall through to JSON_FLOAT */
535 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600536 case JSON_FLOAT:
Eric Blake6e8e5cb2016-01-29 06:48:37 -0700537 /* FIXME dependent on locale; a pervasive issue in QEMU */
538 /* FIXME our lexer matches RFC 7159 in forbidding Inf or NaN,
539 * but those might be useful extensions beyond JSON */
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400540 return QOBJECT(qnum_from_double(strtod(token->str, NULL)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600541 default:
Markus Armbrusterd538b252015-11-25 22:23:30 +0100542 abort();
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600543 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600544}
545
Michael Roth65c0f1e2012-08-15 13:45:43 -0500546static QObject *parse_value(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600547{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100548 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600549
Markus Armbrusterd538b252015-11-25 22:23:30 +0100550 token = parser_context_peek_token(ctxt);
551 if (token == NULL) {
552 parse_error(ctxt, NULL, "premature EOI");
553 return NULL;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600554 }
555
Paolo Bonzini9bada892015-11-25 22:23:32 +0100556 switch (token->type) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100557 case JSON_LCURLY:
558 return parse_object(ctxt, ap);
559 case JSON_LSQUARE:
560 return parse_array(ctxt, ap);
561 case JSON_ESCAPE:
562 return parse_escape(ctxt, ap);
563 case JSON_INTEGER:
564 case JSON_FLOAT:
565 case JSON_STRING:
566 return parse_literal(ctxt);
567 case JSON_KEYWORD:
568 return parse_keyword(ctxt);
569 default:
570 parse_error(ctxt, token, "expecting value");
571 return NULL;
572 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600573}
574
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100575QObject *json_parser_parse(GQueue *tokens, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600576{
Anthony Liguorief749d02011-06-01 12:14:50 -0500577 return json_parser_parse_err(tokens, ap, NULL);
578}
579
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100580QObject *json_parser_parse_err(GQueue *tokens, va_list *ap, Error **errp)
Anthony Liguorief749d02011-06-01 12:14:50 -0500581{
Michael Roth65c0f1e2012-08-15 13:45:43 -0500582 JSONParserContext *ctxt = parser_context_new(tokens);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600583 QObject *result;
584
Michael Roth65c0f1e2012-08-15 13:45:43 -0500585 if (!ctxt) {
Michael Rothc1990eb2011-06-01 12:15:00 -0500586 return NULL;
587 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600588
Michael Roth65c0f1e2012-08-15 13:45:43 -0500589 result = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600590
Michael Roth65c0f1e2012-08-15 13:45:43 -0500591 error_propagate(errp, ctxt->err);
592
593 parser_context_free(ctxt);
Anthony Liguorief749d02011-06-01 12:14:50 -0500594
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600595 return result;
596}