blob: 055c7f0272a6c36618716a4f59f68293a8f92bac [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 Armbruster452fcdb2018-02-01 12:18:39 +010019#include "qapi/qmp/qdict.h"
Markus Armbruster47e6b292018-02-01 12:18:38 +010020#include "qapi/qmp/qlist.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010021#include "qapi/qmp/qnull.h"
22#include "qapi/qmp/qnum.h"
Markus Armbruster6b673952018-02-01 12:18:35 +010023#include "qapi/qmp/qstring.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010024#include "qapi/qmp/json-parser.h"
25#include "qapi/qmp/json-lexer.h"
Paolo Bonzini9bada892015-11-25 22:23:32 +010026#include "qapi/qmp/json-streamer.h"
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060027
28typedef struct JSONParserContext
29{
Anthony Liguorief749d02011-06-01 12:14:50 -050030 Error *err;
Paolo Bonzini9bada892015-11-25 22:23:32 +010031 JSONToken *current;
Paolo Bonzini95385fe2015-11-25 22:23:31 +010032 GQueue *buf;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060033} JSONParserContext;
34
35#define BUG_ON(cond) assert(!(cond))
36
37/**
38 * TODO
39 *
40 * 0) make errors meaningful again
41 * 1) add geometry information to tokens
42 * 3) should we return a parsed size?
43 * 4) deal with premature EOI
44 */
45
Michael Roth65c0f1e2012-08-15 13:45:43 -050046static QObject *parse_value(JSONParserContext *ctxt, va_list *ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060047
48/**
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060049 * Error handler
50 */
Stefan Weil8b7968f2010-09-23 21:28:05 +020051static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext *ctxt,
Paolo Bonzini9bada892015-11-25 22:23:32 +010052 JSONToken *token, const char *msg, ...)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060053{
Amos Kongc96c84a2010-03-24 23:12:05 +080054 va_list ap;
Anthony Liguorief749d02011-06-01 12:14:50 -050055 char message[1024];
Amos Kongc96c84a2010-03-24 23:12:05 +080056 va_start(ap, msg);
Anthony Liguorief749d02011-06-01 12:14:50 -050057 vsnprintf(message, sizeof(message), msg, ap);
Amos Kongc96c84a2010-03-24 23:12:05 +080058 va_end(ap);
Anthony Liguorief749d02011-06-01 12:14:50 -050059 if (ctxt->err) {
60 error_free(ctxt->err);
61 ctxt->err = NULL;
62 }
Cole Robinsonf231b882014-03-21 19:42:26 -040063 error_setg(&ctxt->err, "JSON parse error, %s", message);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060064}
65
66/**
67 * String helpers
68 *
69 * These helpers are used to unescape strings.
70 */
71static void wchar_to_utf8(uint16_t wchar, char *buffer, size_t buffer_length)
72{
73 if (wchar <= 0x007F) {
74 BUG_ON(buffer_length < 2);
75
76 buffer[0] = wchar & 0x7F;
77 buffer[1] = 0;
78 } else if (wchar <= 0x07FF) {
79 BUG_ON(buffer_length < 3);
80
81 buffer[0] = 0xC0 | ((wchar >> 6) & 0x1F);
82 buffer[1] = 0x80 | (wchar & 0x3F);
83 buffer[2] = 0;
84 } else {
85 BUG_ON(buffer_length < 4);
86
87 buffer[0] = 0xE0 | ((wchar >> 12) & 0x0F);
88 buffer[1] = 0x80 | ((wchar >> 6) & 0x3F);
89 buffer[2] = 0x80 | (wchar & 0x3F);
90 buffer[3] = 0;
91 }
92}
93
94static int hex2decimal(char ch)
95{
96 if (ch >= '0' && ch <= '9') {
97 return (ch - '0');
98 } else if (ch >= 'a' && ch <= 'f') {
99 return 10 + (ch - 'a');
100 } else if (ch >= 'A' && ch <= 'F') {
101 return 10 + (ch - 'A');
102 }
103
104 return -1;
105}
106
107/**
108 * parse_string(): Parse a json string and return a QObject
109 *
110 * string
111 * ""
112 * " chars "
113 * chars
114 * char
115 * char chars
116 * char
117 * any-Unicode-character-
118 * except-"-or-\-or-
119 * control-character
120 * \"
121 * \\
122 * \/
123 * \b
124 * \f
125 * \n
126 * \r
127 * \t
128 * \u four-hex-digits
129 */
Paolo Bonzini9bada892015-11-25 22:23:32 +0100130static QString *qstring_from_escaped_str(JSONParserContext *ctxt,
131 JSONToken *token)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600132{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100133 const char *ptr = token->str;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600134 QString *str;
135 int double_quote = 1;
136
137 if (*ptr == '"') {
138 double_quote = 1;
139 } else {
140 double_quote = 0;
141 }
142 ptr++;
143
144 str = qstring_new();
145 while (*ptr &&
146 ((double_quote && *ptr != '"') || (!double_quote && *ptr != '\''))) {
147 if (*ptr == '\\') {
148 ptr++;
149
150 switch (*ptr) {
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 '/':
164 qstring_append(str, "/");
165 ptr++;
166 break;
167 case 'b':
168 qstring_append(str, "\b");
169 ptr++;
170 break;
Luiz Capitulinobd032692010-05-19 17:06:15 -0300171 case 'f':
172 qstring_append(str, "\f");
173 ptr++;
174 break;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600175 case 'n':
176 qstring_append(str, "\n");
177 ptr++;
178 break;
179 case 'r':
180 qstring_append(str, "\r");
181 ptr++;
182 break;
183 case 't':
184 qstring_append(str, "\t");
185 ptr++;
186 break;
187 case 'u': {
188 uint16_t unicode_char = 0;
189 char utf8_char[4];
190 int i = 0;
191
192 ptr++;
193
194 for (i = 0; i < 4; i++) {
195 if (qemu_isxdigit(*ptr)) {
196 unicode_char |= hex2decimal(*ptr) << ((3 - i) * 4);
197 } else {
198 parse_error(ctxt, token,
199 "invalid hex escape sequence in string");
200 goto out;
201 }
202 ptr++;
203 }
204
205 wchar_to_utf8(unicode_char, utf8_char, sizeof(utf8_char));
206 qstring_append(str, utf8_char);
207 } break;
208 default:
209 parse_error(ctxt, token, "invalid escape sequence in string");
210 goto out;
211 }
212 } else {
213 char dummy[2];
214
215 dummy[0] = *ptr++;
216 dummy[1] = 0;
217
218 qstring_append(str, dummy);
219 }
220 }
221
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600222 return str;
223
224out:
225 QDECREF(str);
226 return NULL;
227}
228
Paolo Bonzini9bada892015-11-25 22:23:32 +0100229/* Note: the token object returned by parser_context_peek_token or
230 * parser_context_pop_token is deleted as soon as parser_context_pop_token
231 * is called again.
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100232 */
Paolo Bonzini9bada892015-11-25 22:23:32 +0100233static JSONToken *parser_context_pop_token(JSONParserContext *ctxt)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500234{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100235 g_free(ctxt->current);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100236 assert(!g_queue_is_empty(ctxt->buf));
237 ctxt->current = g_queue_pop_head(ctxt->buf);
238 return ctxt->current;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500239}
240
Paolo Bonzini9bada892015-11-25 22:23:32 +0100241static JSONToken *parser_context_peek_token(JSONParserContext *ctxt)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500242{
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100243 assert(!g_queue_is_empty(ctxt->buf));
244 return g_queue_peek_head(ctxt->buf);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500245}
246
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100247static JSONParserContext *parser_context_new(GQueue *tokens)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500248{
249 JSONParserContext *ctxt;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500250
251 if (!tokens) {
252 return NULL;
253 }
254
Michael Roth65c0f1e2012-08-15 13:45:43 -0500255 ctxt = g_malloc0(sizeof(JSONParserContext));
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100256 ctxt->buf = tokens;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500257
258 return ctxt;
259}
260
261/* to support error propagation, ctxt->err must be freed separately */
262static void parser_context_free(JSONParserContext *ctxt)
263{
Michael Roth65c0f1e2012-08-15 13:45:43 -0500264 if (ctxt) {
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100265 while (!g_queue_is_empty(ctxt->buf)) {
266 parser_context_pop_token(ctxt);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500267 }
Paolo Bonzini9bada892015-11-25 22:23:32 +0100268 g_free(ctxt->current);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100269 g_queue_free(ctxt->buf);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500270 g_free(ctxt);
271 }
272}
273
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600274/**
275 * Parsing rules
276 */
Michael Roth65c0f1e2012-08-15 13:45:43 -0500277static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600278{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100279 QObject *key = NULL, *value;
280 JSONToken *peek, *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600281
Michael Roth65c0f1e2012-08-15 13:45:43 -0500282 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500283 if (peek == NULL) {
284 parse_error(ctxt, NULL, "premature EOI");
285 goto out;
286 }
287
Michael Roth65c0f1e2012-08-15 13:45:43 -0500288 key = parse_value(ctxt, ap);
Kevin Wolfd758d902010-02-24 16:17:58 +0100289 if (!key || qobject_type(key) != QTYPE_QSTRING) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600290 parse_error(ctxt, peek, "key is not a string in object");
291 goto out;
292 }
293
Michael Roth65c0f1e2012-08-15 13:45:43 -0500294 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500295 if (token == NULL) {
296 parse_error(ctxt, NULL, "premature EOI");
297 goto out;
298 }
299
Paolo Bonzini9bada892015-11-25 22:23:32 +0100300 if (token->type != JSON_COLON) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600301 parse_error(ctxt, token, "missing : in object pair");
302 goto out;
303 }
304
Michael Roth65c0f1e2012-08-15 13:45:43 -0500305 value = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600306 if (value == NULL) {
307 parse_error(ctxt, token, "Missing value in dict");
308 goto out;
309 }
310
Max Reitz7dc847e2018-02-24 16:40:29 +0100311 qdict_put_obj(dict, qstring_get_str(qobject_to(QString, key)), value);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600312
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600313 qobject_decref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600314
315 return 0;
316
317out:
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600318 qobject_decref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600319
320 return -1;
321}
322
Michael Roth65c0f1e2012-08-15 13:45:43 -0500323static QObject *parse_object(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600324{
325 QDict *dict = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100326 JSONToken *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600327
Michael Roth65c0f1e2012-08-15 13:45:43 -0500328 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100329 assert(token && token->type == JSON_LCURLY);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600330
331 dict = qdict_new();
332
Michael Roth65c0f1e2012-08-15 13:45:43 -0500333 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500334 if (peek == NULL) {
335 parse_error(ctxt, NULL, "premature EOI");
336 goto out;
337 }
338
Paolo Bonzini9bada892015-11-25 22:23:32 +0100339 if (peek->type != JSON_RCURLY) {
Michael Roth65c0f1e2012-08-15 13:45:43 -0500340 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600341 goto out;
342 }
343
Michael Roth65c0f1e2012-08-15 13:45:43 -0500344 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500345 if (token == NULL) {
346 parse_error(ctxt, NULL, "premature EOI");
347 goto out;
348 }
349
Paolo Bonzini9bada892015-11-25 22:23:32 +0100350 while (token->type != JSON_RCURLY) {
351 if (token->type != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600352 parse_error(ctxt, token, "expected separator in dict");
353 goto out;
354 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600355
Michael Roth65c0f1e2012-08-15 13:45:43 -0500356 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600357 goto out;
358 }
359
Michael Roth65c0f1e2012-08-15 13:45:43 -0500360 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500361 if (token == NULL) {
362 parse_error(ctxt, NULL, "premature EOI");
363 goto out;
364 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600365 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600366 } else {
Gongleia491af42014-06-10 17:20:24 +0800367 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600368 }
369
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600370 return QOBJECT(dict);
371
372out:
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600373 QDECREF(dict);
374 return NULL;
375}
376
Michael Roth65c0f1e2012-08-15 13:45:43 -0500377static QObject *parse_array(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600378{
379 QList *list = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100380 JSONToken *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600381
Michael Roth65c0f1e2012-08-15 13:45:43 -0500382 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100383 assert(token && token->type == JSON_LSQUARE);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600384
385 list = qlist_new();
386
Michael Roth65c0f1e2012-08-15 13:45:43 -0500387 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500388 if (peek == NULL) {
389 parse_error(ctxt, NULL, "premature EOI");
390 goto out;
391 }
392
Paolo Bonzini9bada892015-11-25 22:23:32 +0100393 if (peek->type != JSON_RSQUARE) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600394 QObject *obj;
395
Michael Roth65c0f1e2012-08-15 13:45:43 -0500396 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600397 if (obj == NULL) {
398 parse_error(ctxt, token, "expecting value");
399 goto out;
400 }
401
402 qlist_append_obj(list, obj);
403
Michael Roth65c0f1e2012-08-15 13:45:43 -0500404 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500405 if (token == NULL) {
406 parse_error(ctxt, NULL, "premature EOI");
407 goto out;
408 }
409
Paolo Bonzini9bada892015-11-25 22:23:32 +0100410 while (token->type != JSON_RSQUARE) {
411 if (token->type != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600412 parse_error(ctxt, token, "expected separator in list");
413 goto out;
414 }
415
Michael Roth65c0f1e2012-08-15 13:45:43 -0500416 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600417 if (obj == NULL) {
418 parse_error(ctxt, token, "expecting value");
419 goto out;
420 }
421
422 qlist_append_obj(list, obj);
423
Michael Roth65c0f1e2012-08-15 13:45:43 -0500424 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500425 if (token == NULL) {
426 parse_error(ctxt, NULL, "premature EOI");
427 goto out;
428 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600429 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600430 } else {
Gongleia491af42014-06-10 17:20:24 +0800431 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600432 }
433
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600434 return QOBJECT(list);
435
436out:
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600437 QDECREF(list);
438 return NULL;
439}
440
Michael Roth65c0f1e2012-08-15 13:45:43 -0500441static QObject *parse_keyword(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600442{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100443 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600444
Michael Roth65c0f1e2012-08-15 13:45:43 -0500445 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100446 assert(token && token->type == JSON_KEYWORD);
Markus Armbruster50e2a462015-11-25 22:23:27 +0100447
Paolo Bonzini9bada892015-11-25 22:23:32 +0100448 if (!strcmp(token->str, "true")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100449 return QOBJECT(qbool_from_bool(true));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100450 } else if (!strcmp(token->str, "false")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100451 return QOBJECT(qbool_from_bool(false));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100452 } else if (!strcmp(token->str, "null")) {
Markus Armbruster006ca092017-06-26 13:52:24 +0200453 return QOBJECT(qnull());
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600454 }
Paolo Bonzini9bada892015-11-25 22:23:32 +0100455 parse_error(ctxt, token, "invalid keyword '%s'", token->str);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600456 return NULL;
457}
458
Michael Roth65c0f1e2012-08-15 13:45:43 -0500459static QObject *parse_escape(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600460{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100461 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600462
463 if (ap == NULL) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100464 return NULL;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600465 }
466
Michael Roth65c0f1e2012-08-15 13:45:43 -0500467 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100468 assert(token && token->type == JSON_ESCAPE);
Markus Armbruster6b9606f2015-11-25 22:23:28 +0100469
Paolo Bonzini9bada892015-11-25 22:23:32 +0100470 if (!strcmp(token->str, "%p")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100471 return va_arg(*ap, QObject *);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100472 } else if (!strcmp(token->str, "%i")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100473 return QOBJECT(qbool_from_bool(va_arg(*ap, int)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100474 } else if (!strcmp(token->str, "%d")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400475 return QOBJECT(qnum_from_int(va_arg(*ap, int)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100476 } else if (!strcmp(token->str, "%ld")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400477 return QOBJECT(qnum_from_int(va_arg(*ap, long)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100478 } else if (!strcmp(token->str, "%lld") ||
479 !strcmp(token->str, "%I64d")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400480 return QOBJECT(qnum_from_int(va_arg(*ap, long long)));
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400481 } else if (!strcmp(token->str, "%u")) {
482 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned int)));
483 } else if (!strcmp(token->str, "%lu")) {
484 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned long)));
485 } else if (!strcmp(token->str, "%llu") ||
486 !strcmp(token->str, "%I64u")) {
487 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned long long)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100488 } else if (!strcmp(token->str, "%s")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100489 return QOBJECT(qstring_from_str(va_arg(*ap, const char *)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100490 } else if (!strcmp(token->str, "%f")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400491 return QOBJECT(qnum_from_double(va_arg(*ap, double)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600492 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600493 return NULL;
494}
495
Michael Roth65c0f1e2012-08-15 13:45:43 -0500496static QObject *parse_literal(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600497{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100498 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600499
Michael Roth65c0f1e2012-08-15 13:45:43 -0500500 token = parser_context_pop_token(ctxt);
Markus Armbrusterd538b252015-11-25 22:23:30 +0100501 assert(token);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500502
Paolo Bonzini9bada892015-11-25 22:23:32 +0100503 switch (token->type) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600504 case JSON_STRING:
Markus Armbrusterd538b252015-11-25 22:23:30 +0100505 return QOBJECT(qstring_from_escaped_str(ctxt, token));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500506 case JSON_INTEGER: {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400507 /*
508 * Represent JSON_INTEGER as QNUM_I64 if possible, else as
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400509 * QNUM_U64, else as QNUM_DOUBLE. Note that qemu_strtoi64()
510 * and qemu_strtou64() fail with ERANGE when it's not
511 * possible.
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500512 *
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400513 * qnum_get_int() will then work for any signed 64-bit
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400514 * JSON_INTEGER, qnum_get_uint() for any unsigned 64-bit
515 * integer, and qnum_get_double() both for any JSON_INTEGER
516 * and any JSON_FLOAT (with precision loss for integers beyond
517 * 53 bits)
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500518 */
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400519 int ret;
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500520 int64_t value;
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400521 uint64_t uvalue;
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500522
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400523 ret = qemu_strtoi64(token->str, NULL, 10, &value);
524 if (!ret) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400525 return QOBJECT(qnum_from_int(value));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500526 }
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400527 assert(ret == -ERANGE);
528
529 if (token->str[0] != '-') {
530 ret = qemu_strtou64(token->str, NULL, 10, &uvalue);
531 if (!ret) {
532 return QOBJECT(qnum_from_uint(uvalue));
533 }
534 assert(ret == -ERANGE);
535 }
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500536 /* fall through to JSON_FLOAT */
537 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600538 case JSON_FLOAT:
Eric Blake6e8e5cb2016-01-29 06:48:37 -0700539 /* FIXME dependent on locale; a pervasive issue in QEMU */
540 /* FIXME our lexer matches RFC 7159 in forbidding Inf or NaN,
541 * but those might be useful extensions beyond JSON */
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400542 return QOBJECT(qnum_from_double(strtod(token->str, NULL)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600543 default:
Markus Armbrusterd538b252015-11-25 22:23:30 +0100544 abort();
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600545 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600546}
547
Michael Roth65c0f1e2012-08-15 13:45:43 -0500548static QObject *parse_value(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600549{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100550 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600551
Markus Armbrusterd538b252015-11-25 22:23:30 +0100552 token = parser_context_peek_token(ctxt);
553 if (token == NULL) {
554 parse_error(ctxt, NULL, "premature EOI");
555 return NULL;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600556 }
557
Paolo Bonzini9bada892015-11-25 22:23:32 +0100558 switch (token->type) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100559 case JSON_LCURLY:
560 return parse_object(ctxt, ap);
561 case JSON_LSQUARE:
562 return parse_array(ctxt, ap);
563 case JSON_ESCAPE:
564 return parse_escape(ctxt, ap);
565 case JSON_INTEGER:
566 case JSON_FLOAT:
567 case JSON_STRING:
568 return parse_literal(ctxt);
569 case JSON_KEYWORD:
570 return parse_keyword(ctxt);
571 default:
572 parse_error(ctxt, token, "expecting value");
573 return NULL;
574 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600575}
576
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100577QObject *json_parser_parse(GQueue *tokens, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600578{
Anthony Liguorief749d02011-06-01 12:14:50 -0500579 return json_parser_parse_err(tokens, ap, NULL);
580}
581
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100582QObject *json_parser_parse_err(GQueue *tokens, va_list *ap, Error **errp)
Anthony Liguorief749d02011-06-01 12:14:50 -0500583{
Michael Roth65c0f1e2012-08-15 13:45:43 -0500584 JSONParserContext *ctxt = parser_context_new(tokens);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600585 QObject *result;
586
Michael Roth65c0f1e2012-08-15 13:45:43 -0500587 if (!ctxt) {
Michael Rothc1990eb2011-06-01 12:15:00 -0500588 return NULL;
589 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600590
Michael Roth65c0f1e2012-08-15 13:45:43 -0500591 result = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600592
Michael Roth65c0f1e2012-08-15 13:45:43 -0500593 error_propagate(errp, ctxt->err);
594
595 parser_context_free(ctxt);
Anthony Liguorief749d02011-06-01 12:14:50 -0500596
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600597 return result;
598}