blob: 30dfb9dc4117268ee336e22780d85da62cf443fa [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"
19#include "qapi/qmp/qstring.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010020#include "qapi/qmp/json-parser.h"
21#include "qapi/qmp/json-lexer.h"
Paolo Bonzini9bada892015-11-25 22:23:32 +010022#include "qapi/qmp/json-streamer.h"
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060023
24typedef struct JSONParserContext
25{
Anthony Liguorief749d02011-06-01 12:14:50 -050026 Error *err;
Paolo Bonzini9bada892015-11-25 22:23:32 +010027 JSONToken *current;
Paolo Bonzini95385fe2015-11-25 22:23:31 +010028 GQueue *buf;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060029} JSONParserContext;
30
31#define BUG_ON(cond) assert(!(cond))
32
33/**
34 * TODO
35 *
36 * 0) make errors meaningful again
37 * 1) add geometry information to tokens
38 * 3) should we return a parsed size?
39 * 4) deal with premature EOI
40 */
41
Michael Roth65c0f1e2012-08-15 13:45:43 -050042static QObject *parse_value(JSONParserContext *ctxt, va_list *ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060043
44/**
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060045 * Error handler
46 */
Stefan Weil8b7968f2010-09-23 21:28:05 +020047static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext *ctxt,
Paolo Bonzini9bada892015-11-25 22:23:32 +010048 JSONToken *token, const char *msg, ...)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060049{
Amos Kongc96c84a2010-03-24 23:12:05 +080050 va_list ap;
Anthony Liguorief749d02011-06-01 12:14:50 -050051 char message[1024];
Amos Kongc96c84a2010-03-24 23:12:05 +080052 va_start(ap, msg);
Anthony Liguorief749d02011-06-01 12:14:50 -050053 vsnprintf(message, sizeof(message), msg, ap);
Amos Kongc96c84a2010-03-24 23:12:05 +080054 va_end(ap);
Anthony Liguorief749d02011-06-01 12:14:50 -050055 if (ctxt->err) {
56 error_free(ctxt->err);
57 ctxt->err = NULL;
58 }
Cole Robinsonf231b882014-03-21 19:42:26 -040059 error_setg(&ctxt->err, "JSON parse error, %s", message);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060060}
61
62/**
63 * String helpers
64 *
65 * These helpers are used to unescape strings.
66 */
67static void wchar_to_utf8(uint16_t wchar, char *buffer, size_t buffer_length)
68{
69 if (wchar <= 0x007F) {
70 BUG_ON(buffer_length < 2);
71
72 buffer[0] = wchar & 0x7F;
73 buffer[1] = 0;
74 } else if (wchar <= 0x07FF) {
75 BUG_ON(buffer_length < 3);
76
77 buffer[0] = 0xC0 | ((wchar >> 6) & 0x1F);
78 buffer[1] = 0x80 | (wchar & 0x3F);
79 buffer[2] = 0;
80 } else {
81 BUG_ON(buffer_length < 4);
82
83 buffer[0] = 0xE0 | ((wchar >> 12) & 0x0F);
84 buffer[1] = 0x80 | ((wchar >> 6) & 0x3F);
85 buffer[2] = 0x80 | (wchar & 0x3F);
86 buffer[3] = 0;
87 }
88}
89
90static int hex2decimal(char ch)
91{
92 if (ch >= '0' && ch <= '9') {
93 return (ch - '0');
94 } else if (ch >= 'a' && ch <= 'f') {
95 return 10 + (ch - 'a');
96 } else if (ch >= 'A' && ch <= 'F') {
97 return 10 + (ch - 'A');
98 }
99
100 return -1;
101}
102
103/**
104 * parse_string(): Parse a json string and return a QObject
105 *
106 * string
107 * ""
108 * " chars "
109 * chars
110 * char
111 * char chars
112 * char
113 * any-Unicode-character-
114 * except-"-or-\-or-
115 * control-character
116 * \"
117 * \\
118 * \/
119 * \b
120 * \f
121 * \n
122 * \r
123 * \t
124 * \u four-hex-digits
125 */
Paolo Bonzini9bada892015-11-25 22:23:32 +0100126static QString *qstring_from_escaped_str(JSONParserContext *ctxt,
127 JSONToken *token)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600128{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100129 const char *ptr = token->str;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600130 QString *str;
131 int double_quote = 1;
132
133 if (*ptr == '"') {
134 double_quote = 1;
135 } else {
136 double_quote = 0;
137 }
138 ptr++;
139
140 str = qstring_new();
141 while (*ptr &&
142 ((double_quote && *ptr != '"') || (!double_quote && *ptr != '\''))) {
143 if (*ptr == '\\') {
144 ptr++;
145
146 switch (*ptr) {
147 case '"':
148 qstring_append(str, "\"");
149 ptr++;
150 break;
151 case '\'':
152 qstring_append(str, "'");
153 ptr++;
154 break;
155 case '\\':
156 qstring_append(str, "\\");
157 ptr++;
158 break;
159 case '/':
160 qstring_append(str, "/");
161 ptr++;
162 break;
163 case 'b':
164 qstring_append(str, "\b");
165 ptr++;
166 break;
Luiz Capitulinobd032692010-05-19 17:06:15 -0300167 case 'f':
168 qstring_append(str, "\f");
169 ptr++;
170 break;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600171 case 'n':
172 qstring_append(str, "\n");
173 ptr++;
174 break;
175 case 'r':
176 qstring_append(str, "\r");
177 ptr++;
178 break;
179 case 't':
180 qstring_append(str, "\t");
181 ptr++;
182 break;
183 case 'u': {
184 uint16_t unicode_char = 0;
185 char utf8_char[4];
186 int i = 0;
187
188 ptr++;
189
190 for (i = 0; i < 4; i++) {
191 if (qemu_isxdigit(*ptr)) {
192 unicode_char |= hex2decimal(*ptr) << ((3 - i) * 4);
193 } else {
194 parse_error(ctxt, token,
195 "invalid hex escape sequence in string");
196 goto out;
197 }
198 ptr++;
199 }
200
201 wchar_to_utf8(unicode_char, utf8_char, sizeof(utf8_char));
202 qstring_append(str, utf8_char);
203 } break;
204 default:
205 parse_error(ctxt, token, "invalid escape sequence in string");
206 goto out;
207 }
208 } else {
209 char dummy[2];
210
211 dummy[0] = *ptr++;
212 dummy[1] = 0;
213
214 qstring_append(str, dummy);
215 }
216 }
217
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600218 return str;
219
220out:
221 QDECREF(str);
222 return NULL;
223}
224
Paolo Bonzini9bada892015-11-25 22:23:32 +0100225/* Note: the token object returned by parser_context_peek_token or
226 * parser_context_pop_token is deleted as soon as parser_context_pop_token
227 * is called again.
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100228 */
Paolo Bonzini9bada892015-11-25 22:23:32 +0100229static JSONToken *parser_context_pop_token(JSONParserContext *ctxt)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500230{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100231 g_free(ctxt->current);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100232 assert(!g_queue_is_empty(ctxt->buf));
233 ctxt->current = g_queue_pop_head(ctxt->buf);
234 return ctxt->current;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500235}
236
Paolo Bonzini9bada892015-11-25 22:23:32 +0100237static JSONToken *parser_context_peek_token(JSONParserContext *ctxt)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500238{
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100239 assert(!g_queue_is_empty(ctxt->buf));
240 return g_queue_peek_head(ctxt->buf);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500241}
242
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100243static JSONParserContext *parser_context_new(GQueue *tokens)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500244{
245 JSONParserContext *ctxt;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500246
247 if (!tokens) {
248 return NULL;
249 }
250
Michael Roth65c0f1e2012-08-15 13:45:43 -0500251 ctxt = g_malloc0(sizeof(JSONParserContext));
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100252 ctxt->buf = tokens;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500253
254 return ctxt;
255}
256
257/* to support error propagation, ctxt->err must be freed separately */
258static void parser_context_free(JSONParserContext *ctxt)
259{
Michael Roth65c0f1e2012-08-15 13:45:43 -0500260 if (ctxt) {
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100261 while (!g_queue_is_empty(ctxt->buf)) {
262 parser_context_pop_token(ctxt);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500263 }
Paolo Bonzini9bada892015-11-25 22:23:32 +0100264 g_free(ctxt->current);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100265 g_queue_free(ctxt->buf);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500266 g_free(ctxt);
267 }
268}
269
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600270/**
271 * Parsing rules
272 */
Michael Roth65c0f1e2012-08-15 13:45:43 -0500273static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600274{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100275 QObject *key = NULL, *value;
276 JSONToken *peek, *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600277
Michael Roth65c0f1e2012-08-15 13:45:43 -0500278 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500279 if (peek == NULL) {
280 parse_error(ctxt, NULL, "premature EOI");
281 goto out;
282 }
283
Michael Roth65c0f1e2012-08-15 13:45:43 -0500284 key = parse_value(ctxt, ap);
Kevin Wolfd758d902010-02-24 16:17:58 +0100285 if (!key || qobject_type(key) != QTYPE_QSTRING) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600286 parse_error(ctxt, peek, "key is not a string in object");
287 goto out;
288 }
289
Michael Roth65c0f1e2012-08-15 13:45:43 -0500290 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500291 if (token == NULL) {
292 parse_error(ctxt, NULL, "premature EOI");
293 goto out;
294 }
295
Paolo Bonzini9bada892015-11-25 22:23:32 +0100296 if (token->type != JSON_COLON) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600297 parse_error(ctxt, token, "missing : in object pair");
298 goto out;
299 }
300
Michael Roth65c0f1e2012-08-15 13:45:43 -0500301 value = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600302 if (value == NULL) {
303 parse_error(ctxt, token, "Missing value in dict");
304 goto out;
305 }
306
307 qdict_put_obj(dict, qstring_get_str(qobject_to_qstring(key)), value);
308
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600309 qobject_decref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600310
311 return 0;
312
313out:
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600314 qobject_decref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600315
316 return -1;
317}
318
Michael Roth65c0f1e2012-08-15 13:45:43 -0500319static QObject *parse_object(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600320{
321 QDict *dict = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100322 JSONToken *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600323
Michael Roth65c0f1e2012-08-15 13:45:43 -0500324 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100325 assert(token && token->type == JSON_LCURLY);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600326
327 dict = qdict_new();
328
Michael Roth65c0f1e2012-08-15 13:45:43 -0500329 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500330 if (peek == NULL) {
331 parse_error(ctxt, NULL, "premature EOI");
332 goto out;
333 }
334
Paolo Bonzini9bada892015-11-25 22:23:32 +0100335 if (peek->type != JSON_RCURLY) {
Michael Roth65c0f1e2012-08-15 13:45:43 -0500336 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600337 goto out;
338 }
339
Michael Roth65c0f1e2012-08-15 13:45:43 -0500340 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500341 if (token == NULL) {
342 parse_error(ctxt, NULL, "premature EOI");
343 goto out;
344 }
345
Paolo Bonzini9bada892015-11-25 22:23:32 +0100346 while (token->type != JSON_RCURLY) {
347 if (token->type != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600348 parse_error(ctxt, token, "expected separator in dict");
349 goto out;
350 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600351
Michael Roth65c0f1e2012-08-15 13:45:43 -0500352 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600353 goto out;
354 }
355
Michael Roth65c0f1e2012-08-15 13:45:43 -0500356 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500357 if (token == NULL) {
358 parse_error(ctxt, NULL, "premature EOI");
359 goto out;
360 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600361 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600362 } else {
Gongleia491af42014-06-10 17:20:24 +0800363 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600364 }
365
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600366 return QOBJECT(dict);
367
368out:
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600369 QDECREF(dict);
370 return NULL;
371}
372
Michael Roth65c0f1e2012-08-15 13:45:43 -0500373static QObject *parse_array(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600374{
375 QList *list = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100376 JSONToken *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600377
Michael Roth65c0f1e2012-08-15 13:45:43 -0500378 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100379 assert(token && token->type == JSON_LSQUARE);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600380
381 list = qlist_new();
382
Michael Roth65c0f1e2012-08-15 13:45:43 -0500383 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500384 if (peek == NULL) {
385 parse_error(ctxt, NULL, "premature EOI");
386 goto out;
387 }
388
Paolo Bonzini9bada892015-11-25 22:23:32 +0100389 if (peek->type != JSON_RSQUARE) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600390 QObject *obj;
391
Michael Roth65c0f1e2012-08-15 13:45:43 -0500392 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600393 if (obj == NULL) {
394 parse_error(ctxt, token, "expecting value");
395 goto out;
396 }
397
398 qlist_append_obj(list, obj);
399
Michael Roth65c0f1e2012-08-15 13:45:43 -0500400 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500401 if (token == NULL) {
402 parse_error(ctxt, NULL, "premature EOI");
403 goto out;
404 }
405
Paolo Bonzini9bada892015-11-25 22:23:32 +0100406 while (token->type != JSON_RSQUARE) {
407 if (token->type != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600408 parse_error(ctxt, token, "expected separator in list");
409 goto out;
410 }
411
Michael Roth65c0f1e2012-08-15 13:45:43 -0500412 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600413 if (obj == NULL) {
414 parse_error(ctxt, token, "expecting value");
415 goto out;
416 }
417
418 qlist_append_obj(list, obj);
419
Michael Roth65c0f1e2012-08-15 13:45:43 -0500420 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500421 if (token == NULL) {
422 parse_error(ctxt, NULL, "premature EOI");
423 goto out;
424 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600425 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600426 } else {
Gongleia491af42014-06-10 17:20:24 +0800427 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600428 }
429
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600430 return QOBJECT(list);
431
432out:
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600433 QDECREF(list);
434 return NULL;
435}
436
Michael Roth65c0f1e2012-08-15 13:45:43 -0500437static QObject *parse_keyword(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600438{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100439 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600440
Michael Roth65c0f1e2012-08-15 13:45:43 -0500441 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100442 assert(token && token->type == JSON_KEYWORD);
Markus Armbruster50e2a462015-11-25 22:23:27 +0100443
Paolo Bonzini9bada892015-11-25 22:23:32 +0100444 if (!strcmp(token->str, "true")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100445 return QOBJECT(qbool_from_bool(true));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100446 } else if (!strcmp(token->str, "false")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100447 return QOBJECT(qbool_from_bool(false));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100448 } else if (!strcmp(token->str, "null")) {
Markus Armbruster006ca092017-06-26 13:52:24 +0200449 return QOBJECT(qnull());
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600450 }
Paolo Bonzini9bada892015-11-25 22:23:32 +0100451 parse_error(ctxt, token, "invalid keyword '%s'", token->str);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600452 return NULL;
453}
454
Michael Roth65c0f1e2012-08-15 13:45:43 -0500455static QObject *parse_escape(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600456{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100457 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600458
459 if (ap == NULL) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100460 return NULL;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600461 }
462
Michael Roth65c0f1e2012-08-15 13:45:43 -0500463 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100464 assert(token && token->type == JSON_ESCAPE);
Markus Armbruster6b9606f2015-11-25 22:23:28 +0100465
Paolo Bonzini9bada892015-11-25 22:23:32 +0100466 if (!strcmp(token->str, "%p")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100467 return va_arg(*ap, QObject *);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100468 } else if (!strcmp(token->str, "%i")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100469 return QOBJECT(qbool_from_bool(va_arg(*ap, int)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100470 } else if (!strcmp(token->str, "%d")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400471 return QOBJECT(qnum_from_int(va_arg(*ap, int)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100472 } else if (!strcmp(token->str, "%ld")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400473 return QOBJECT(qnum_from_int(va_arg(*ap, long)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100474 } else if (!strcmp(token->str, "%lld") ||
475 !strcmp(token->str, "%I64d")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400476 return QOBJECT(qnum_from_int(va_arg(*ap, long long)));
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400477 } else if (!strcmp(token->str, "%u")) {
478 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned int)));
479 } else if (!strcmp(token->str, "%lu")) {
480 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned long)));
481 } else if (!strcmp(token->str, "%llu") ||
482 !strcmp(token->str, "%I64u")) {
483 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned long long)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100484 } else if (!strcmp(token->str, "%s")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100485 return QOBJECT(qstring_from_str(va_arg(*ap, const char *)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100486 } else if (!strcmp(token->str, "%f")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400487 return QOBJECT(qnum_from_double(va_arg(*ap, double)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600488 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600489 return NULL;
490}
491
Michael Roth65c0f1e2012-08-15 13:45:43 -0500492static QObject *parse_literal(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600493{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100494 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600495
Michael Roth65c0f1e2012-08-15 13:45:43 -0500496 token = parser_context_pop_token(ctxt);
Markus Armbrusterd538b252015-11-25 22:23:30 +0100497 assert(token);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500498
Paolo Bonzini9bada892015-11-25 22:23:32 +0100499 switch (token->type) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600500 case JSON_STRING:
Markus Armbrusterd538b252015-11-25 22:23:30 +0100501 return QOBJECT(qstring_from_escaped_str(ctxt, token));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500502 case JSON_INTEGER: {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400503 /*
504 * Represent JSON_INTEGER as QNUM_I64 if possible, else as
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400505 * QNUM_U64, else as QNUM_DOUBLE. Note that qemu_strtoi64()
506 * and qemu_strtou64() fail with ERANGE when it's not
507 * possible.
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500508 *
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400509 * qnum_get_int() will then work for any signed 64-bit
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400510 * JSON_INTEGER, qnum_get_uint() for any unsigned 64-bit
511 * integer, and qnum_get_double() both for any JSON_INTEGER
512 * and any JSON_FLOAT (with precision loss for integers beyond
513 * 53 bits)
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500514 */
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400515 int ret;
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500516 int64_t value;
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400517 uint64_t uvalue;
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500518
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400519 ret = qemu_strtoi64(token->str, NULL, 10, &value);
520 if (!ret) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400521 return QOBJECT(qnum_from_int(value));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500522 }
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400523 assert(ret == -ERANGE);
524
525 if (token->str[0] != '-') {
526 ret = qemu_strtou64(token->str, NULL, 10, &uvalue);
527 if (!ret) {
528 return QOBJECT(qnum_from_uint(uvalue));
529 }
530 assert(ret == -ERANGE);
531 }
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500532 /* fall through to JSON_FLOAT */
533 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600534 case JSON_FLOAT:
Eric Blake6e8e5cb2016-01-29 06:48:37 -0700535 /* FIXME dependent on locale; a pervasive issue in QEMU */
536 /* FIXME our lexer matches RFC 7159 in forbidding Inf or NaN,
537 * but those might be useful extensions beyond JSON */
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400538 return QOBJECT(qnum_from_double(strtod(token->str, NULL)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600539 default:
Markus Armbrusterd538b252015-11-25 22:23:30 +0100540 abort();
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600541 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600542}
543
Michael Roth65c0f1e2012-08-15 13:45:43 -0500544static QObject *parse_value(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600545{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100546 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600547
Markus Armbrusterd538b252015-11-25 22:23:30 +0100548 token = parser_context_peek_token(ctxt);
549 if (token == NULL) {
550 parse_error(ctxt, NULL, "premature EOI");
551 return NULL;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600552 }
553
Paolo Bonzini9bada892015-11-25 22:23:32 +0100554 switch (token->type) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100555 case JSON_LCURLY:
556 return parse_object(ctxt, ap);
557 case JSON_LSQUARE:
558 return parse_array(ctxt, ap);
559 case JSON_ESCAPE:
560 return parse_escape(ctxt, ap);
561 case JSON_INTEGER:
562 case JSON_FLOAT:
563 case JSON_STRING:
564 return parse_literal(ctxt);
565 case JSON_KEYWORD:
566 return parse_keyword(ctxt);
567 default:
568 parse_error(ctxt, token, "expecting value");
569 return NULL;
570 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600571}
572
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100573QObject *json_parser_parse(GQueue *tokens, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600574{
Anthony Liguorief749d02011-06-01 12:14:50 -0500575 return json_parser_parse_err(tokens, ap, NULL);
576}
577
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100578QObject *json_parser_parse_err(GQueue *tokens, va_list *ap, Error **errp)
Anthony Liguorief749d02011-06-01 12:14:50 -0500579{
Michael Roth65c0f1e2012-08-15 13:45:43 -0500580 JSONParserContext *ctxt = parser_context_new(tokens);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600581 QObject *result;
582
Michael Roth65c0f1e2012-08-15 13:45:43 -0500583 if (!ctxt) {
Michael Rothc1990eb2011-06-01 12:15:00 -0500584 return NULL;
585 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600586
Michael Roth65c0f1e2012-08-15 13:45:43 -0500587 result = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600588
Michael Roth65c0f1e2012-08-15 13:45:43 -0500589 error_propagate(errp, ctxt->err);
590
591 parser_context_free(ctxt);
Anthony Liguorief749d02011-06-01 12:14:50 -0500592
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600593 return result;
594}