blob: 164b86769b53b219e5bd0ae3814df8deb7755e86 [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;
Markus Armbruster00ea57f2018-08-23 18:39:47 +0200135 char quote;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600136
Markus Armbruster00ea57f2018-08-23 18:39:47 +0200137 assert(*ptr == '"' || *ptr == '\'');
138 quote = *ptr++;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600139 str = qstring_new();
Markus Armbruster00ea57f2018-08-23 18:39:47 +0200140
141 while (*ptr != quote) {
142 assert(*ptr);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600143 if (*ptr == '\\') {
144 ptr++;
Markus Armbruster00ea57f2018-08-23 18:39:47 +0200145 switch (*ptr++) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600146 case '"':
147 qstring_append(str, "\"");
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600148 break;
149 case '\'':
150 qstring_append(str, "'");
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600151 break;
152 case '\\':
153 qstring_append(str, "\\");
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600154 break;
155 case '/':
156 qstring_append(str, "/");
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600157 break;
158 case 'b':
159 qstring_append(str, "\b");
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600160 break;
Luiz Capitulinobd032692010-05-19 17:06:15 -0300161 case 'f':
162 qstring_append(str, "\f");
Luiz Capitulinobd032692010-05-19 17:06:15 -0300163 break;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600164 case 'n':
165 qstring_append(str, "\n");
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600166 break;
167 case 'r':
168 qstring_append(str, "\r");
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600169 break;
170 case 't':
171 qstring_append(str, "\t");
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600172 break;
173 case 'u': {
174 uint16_t unicode_char = 0;
175 char utf8_char[4];
176 int i = 0;
177
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600178 for (i = 0; i < 4; i++) {
179 if (qemu_isxdigit(*ptr)) {
180 unicode_char |= hex2decimal(*ptr) << ((3 - i) * 4);
181 } else {
182 parse_error(ctxt, token,
183 "invalid hex escape sequence in string");
184 goto out;
185 }
186 ptr++;
187 }
188
189 wchar_to_utf8(unicode_char, utf8_char, sizeof(utf8_char));
190 qstring_append(str, utf8_char);
191 } break;
192 default:
193 parse_error(ctxt, token, "invalid escape sequence in string");
194 goto out;
195 }
196 } else {
197 char dummy[2];
198
199 dummy[0] = *ptr++;
200 dummy[1] = 0;
201
202 qstring_append(str, dummy);
203 }
204 }
205
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600206 return str;
207
208out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200209 qobject_unref(str);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600210 return NULL;
211}
212
Paolo Bonzini9bada892015-11-25 22:23:32 +0100213/* Note: the token object returned by parser_context_peek_token or
214 * parser_context_pop_token is deleted as soon as parser_context_pop_token
215 * is called again.
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100216 */
Paolo Bonzini9bada892015-11-25 22:23:32 +0100217static JSONToken *parser_context_pop_token(JSONParserContext *ctxt)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500218{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100219 g_free(ctxt->current);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100220 assert(!g_queue_is_empty(ctxt->buf));
221 ctxt->current = g_queue_pop_head(ctxt->buf);
222 return ctxt->current;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500223}
224
Paolo Bonzini9bada892015-11-25 22:23:32 +0100225static JSONToken *parser_context_peek_token(JSONParserContext *ctxt)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500226{
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100227 assert(!g_queue_is_empty(ctxt->buf));
228 return g_queue_peek_head(ctxt->buf);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500229}
230
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100231static JSONParserContext *parser_context_new(GQueue *tokens)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500232{
233 JSONParserContext *ctxt;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500234
235 if (!tokens) {
236 return NULL;
237 }
238
Michael Roth65c0f1e2012-08-15 13:45:43 -0500239 ctxt = g_malloc0(sizeof(JSONParserContext));
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100240 ctxt->buf = tokens;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500241
242 return ctxt;
243}
244
245/* to support error propagation, ctxt->err must be freed separately */
246static void parser_context_free(JSONParserContext *ctxt)
247{
Michael Roth65c0f1e2012-08-15 13:45:43 -0500248 if (ctxt) {
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100249 while (!g_queue_is_empty(ctxt->buf)) {
250 parser_context_pop_token(ctxt);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500251 }
Paolo Bonzini9bada892015-11-25 22:23:32 +0100252 g_free(ctxt->current);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100253 g_queue_free(ctxt->buf);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500254 g_free(ctxt);
255 }
256}
257
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600258/**
259 * Parsing rules
260 */
Michael Roth65c0f1e2012-08-15 13:45:43 -0500261static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600262{
Max Reitz532fb532018-03-10 16:14:36 -0600263 QObject *value;
264 QString *key = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100265 JSONToken *peek, *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600266
Michael Roth65c0f1e2012-08-15 13:45:43 -0500267 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500268 if (peek == NULL) {
269 parse_error(ctxt, NULL, "premature EOI");
270 goto out;
271 }
272
Max Reitz532fb532018-03-10 16:14:36 -0600273 key = qobject_to(QString, parse_value(ctxt, ap));
274 if (!key) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600275 parse_error(ctxt, peek, "key is not a string in object");
276 goto out;
277 }
278
Michael Roth65c0f1e2012-08-15 13:45:43 -0500279 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500280 if (token == NULL) {
281 parse_error(ctxt, NULL, "premature EOI");
282 goto out;
283 }
284
Paolo Bonzini9bada892015-11-25 22:23:32 +0100285 if (token->type != JSON_COLON) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600286 parse_error(ctxt, token, "missing : in object pair");
287 goto out;
288 }
289
Michael Roth65c0f1e2012-08-15 13:45:43 -0500290 value = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600291 if (value == NULL) {
292 parse_error(ctxt, token, "Missing value in dict");
293 goto out;
294 }
295
Max Reitz532fb532018-03-10 16:14:36 -0600296 qdict_put_obj(dict, qstring_get_str(key), value);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600297
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200298 qobject_unref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600299
300 return 0;
301
302out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200303 qobject_unref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600304
305 return -1;
306}
307
Michael Roth65c0f1e2012-08-15 13:45:43 -0500308static QObject *parse_object(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600309{
310 QDict *dict = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100311 JSONToken *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600312
Michael Roth65c0f1e2012-08-15 13:45:43 -0500313 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100314 assert(token && token->type == JSON_LCURLY);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600315
316 dict = qdict_new();
317
Michael Roth65c0f1e2012-08-15 13:45:43 -0500318 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500319 if (peek == NULL) {
320 parse_error(ctxt, NULL, "premature EOI");
321 goto out;
322 }
323
Paolo Bonzini9bada892015-11-25 22:23:32 +0100324 if (peek->type != JSON_RCURLY) {
Michael Roth65c0f1e2012-08-15 13:45:43 -0500325 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600326 goto out;
327 }
328
Michael Roth65c0f1e2012-08-15 13:45:43 -0500329 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500330 if (token == NULL) {
331 parse_error(ctxt, NULL, "premature EOI");
332 goto out;
333 }
334
Paolo Bonzini9bada892015-11-25 22:23:32 +0100335 while (token->type != JSON_RCURLY) {
336 if (token->type != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600337 parse_error(ctxt, token, "expected separator in dict");
338 goto out;
339 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600340
Michael Roth65c0f1e2012-08-15 13:45:43 -0500341 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600342 goto out;
343 }
344
Michael Roth65c0f1e2012-08-15 13:45:43 -0500345 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500346 if (token == NULL) {
347 parse_error(ctxt, NULL, "premature EOI");
348 goto out;
349 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600350 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600351 } else {
Gongleia491af42014-06-10 17:20:24 +0800352 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600353 }
354
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600355 return QOBJECT(dict);
356
357out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200358 qobject_unref(dict);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600359 return NULL;
360}
361
Michael Roth65c0f1e2012-08-15 13:45:43 -0500362static QObject *parse_array(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600363{
364 QList *list = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100365 JSONToken *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600366
Michael Roth65c0f1e2012-08-15 13:45:43 -0500367 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100368 assert(token && token->type == JSON_LSQUARE);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600369
370 list = qlist_new();
371
Michael Roth65c0f1e2012-08-15 13:45:43 -0500372 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500373 if (peek == NULL) {
374 parse_error(ctxt, NULL, "premature EOI");
375 goto out;
376 }
377
Paolo Bonzini9bada892015-11-25 22:23:32 +0100378 if (peek->type != JSON_RSQUARE) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600379 QObject *obj;
380
Michael Roth65c0f1e2012-08-15 13:45:43 -0500381 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600382 if (obj == NULL) {
383 parse_error(ctxt, token, "expecting value");
384 goto out;
385 }
386
387 qlist_append_obj(list, obj);
388
Michael Roth65c0f1e2012-08-15 13:45:43 -0500389 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500390 if (token == NULL) {
391 parse_error(ctxt, NULL, "premature EOI");
392 goto out;
393 }
394
Paolo Bonzini9bada892015-11-25 22:23:32 +0100395 while (token->type != JSON_RSQUARE) {
396 if (token->type != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600397 parse_error(ctxt, token, "expected separator in list");
398 goto out;
399 }
400
Michael Roth65c0f1e2012-08-15 13:45:43 -0500401 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600402 if (obj == NULL) {
403 parse_error(ctxt, token, "expecting value");
404 goto out;
405 }
406
407 qlist_append_obj(list, obj);
408
Michael Roth65c0f1e2012-08-15 13:45:43 -0500409 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500410 if (token == NULL) {
411 parse_error(ctxt, NULL, "premature EOI");
412 goto out;
413 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600414 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600415 } else {
Gongleia491af42014-06-10 17:20:24 +0800416 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600417 }
418
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600419 return QOBJECT(list);
420
421out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200422 qobject_unref(list);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600423 return NULL;
424}
425
Michael Roth65c0f1e2012-08-15 13:45:43 -0500426static QObject *parse_keyword(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600427{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100428 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600429
Michael Roth65c0f1e2012-08-15 13:45:43 -0500430 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100431 assert(token && token->type == JSON_KEYWORD);
Markus Armbruster50e2a462015-11-25 22:23:27 +0100432
Paolo Bonzini9bada892015-11-25 22:23:32 +0100433 if (!strcmp(token->str, "true")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100434 return QOBJECT(qbool_from_bool(true));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100435 } else if (!strcmp(token->str, "false")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100436 return QOBJECT(qbool_from_bool(false));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100437 } else if (!strcmp(token->str, "null")) {
Markus Armbruster006ca092017-06-26 13:52:24 +0200438 return QOBJECT(qnull());
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600439 }
Paolo Bonzini9bada892015-11-25 22:23:32 +0100440 parse_error(ctxt, token, "invalid keyword '%s'", token->str);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600441 return NULL;
442}
443
Michael Roth65c0f1e2012-08-15 13:45:43 -0500444static QObject *parse_escape(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600445{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100446 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600447
448 if (ap == NULL) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100449 return NULL;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600450 }
451
Michael Roth65c0f1e2012-08-15 13:45:43 -0500452 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100453 assert(token && token->type == JSON_ESCAPE);
Markus Armbruster6b9606f2015-11-25 22:23:28 +0100454
Paolo Bonzini9bada892015-11-25 22:23:32 +0100455 if (!strcmp(token->str, "%p")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100456 return va_arg(*ap, QObject *);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100457 } else if (!strcmp(token->str, "%i")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100458 return QOBJECT(qbool_from_bool(va_arg(*ap, int)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100459 } else if (!strcmp(token->str, "%d")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400460 return QOBJECT(qnum_from_int(va_arg(*ap, int)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100461 } else if (!strcmp(token->str, "%ld")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400462 return QOBJECT(qnum_from_int(va_arg(*ap, long)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100463 } else if (!strcmp(token->str, "%lld") ||
464 !strcmp(token->str, "%I64d")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400465 return QOBJECT(qnum_from_int(va_arg(*ap, long long)));
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400466 } else if (!strcmp(token->str, "%u")) {
467 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned int)));
468 } else if (!strcmp(token->str, "%lu")) {
469 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned long)));
470 } else if (!strcmp(token->str, "%llu") ||
471 !strcmp(token->str, "%I64u")) {
472 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned long long)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100473 } else if (!strcmp(token->str, "%s")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100474 return QOBJECT(qstring_from_str(va_arg(*ap, const char *)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100475 } else if (!strcmp(token->str, "%f")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400476 return QOBJECT(qnum_from_double(va_arg(*ap, double)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600477 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600478 return NULL;
479}
480
Michael Roth65c0f1e2012-08-15 13:45:43 -0500481static QObject *parse_literal(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600482{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100483 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600484
Michael Roth65c0f1e2012-08-15 13:45:43 -0500485 token = parser_context_pop_token(ctxt);
Markus Armbrusterd538b252015-11-25 22:23:30 +0100486 assert(token);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500487
Paolo Bonzini9bada892015-11-25 22:23:32 +0100488 switch (token->type) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600489 case JSON_STRING:
Markus Armbrusterd538b252015-11-25 22:23:30 +0100490 return QOBJECT(qstring_from_escaped_str(ctxt, token));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500491 case JSON_INTEGER: {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400492 /*
493 * Represent JSON_INTEGER as QNUM_I64 if possible, else as
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400494 * QNUM_U64, else as QNUM_DOUBLE. Note that qemu_strtoi64()
495 * and qemu_strtou64() fail with ERANGE when it's not
496 * possible.
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500497 *
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400498 * qnum_get_int() will then work for any signed 64-bit
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400499 * JSON_INTEGER, qnum_get_uint() for any unsigned 64-bit
500 * integer, and qnum_get_double() both for any JSON_INTEGER
501 * and any JSON_FLOAT (with precision loss for integers beyond
502 * 53 bits)
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500503 */
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400504 int ret;
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500505 int64_t value;
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400506 uint64_t uvalue;
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500507
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400508 ret = qemu_strtoi64(token->str, NULL, 10, &value);
509 if (!ret) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400510 return QOBJECT(qnum_from_int(value));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500511 }
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400512 assert(ret == -ERANGE);
513
514 if (token->str[0] != '-') {
515 ret = qemu_strtou64(token->str, NULL, 10, &uvalue);
516 if (!ret) {
517 return QOBJECT(qnum_from_uint(uvalue));
518 }
519 assert(ret == -ERANGE);
520 }
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500521 /* fall through to JSON_FLOAT */
522 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600523 case JSON_FLOAT:
Eric Blake6e8e5cb2016-01-29 06:48:37 -0700524 /* FIXME dependent on locale; a pervasive issue in QEMU */
525 /* FIXME our lexer matches RFC 7159 in forbidding Inf or NaN,
526 * but those might be useful extensions beyond JSON */
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400527 return QOBJECT(qnum_from_double(strtod(token->str, NULL)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600528 default:
Markus Armbrusterd538b252015-11-25 22:23:30 +0100529 abort();
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600530 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600531}
532
Michael Roth65c0f1e2012-08-15 13:45:43 -0500533static QObject *parse_value(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600534{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100535 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600536
Markus Armbrusterd538b252015-11-25 22:23:30 +0100537 token = parser_context_peek_token(ctxt);
538 if (token == NULL) {
539 parse_error(ctxt, NULL, "premature EOI");
540 return NULL;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600541 }
542
Paolo Bonzini9bada892015-11-25 22:23:32 +0100543 switch (token->type) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100544 case JSON_LCURLY:
545 return parse_object(ctxt, ap);
546 case JSON_LSQUARE:
547 return parse_array(ctxt, ap);
548 case JSON_ESCAPE:
549 return parse_escape(ctxt, ap);
550 case JSON_INTEGER:
551 case JSON_FLOAT:
552 case JSON_STRING:
553 return parse_literal(ctxt);
554 case JSON_KEYWORD:
555 return parse_keyword(ctxt);
556 default:
557 parse_error(ctxt, token, "expecting value");
558 return NULL;
559 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600560}
561
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100562QObject *json_parser_parse(GQueue *tokens, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600563{
Anthony Liguorief749d02011-06-01 12:14:50 -0500564 return json_parser_parse_err(tokens, ap, NULL);
565}
566
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100567QObject *json_parser_parse_err(GQueue *tokens, va_list *ap, Error **errp)
Anthony Liguorief749d02011-06-01 12:14:50 -0500568{
Michael Roth65c0f1e2012-08-15 13:45:43 -0500569 JSONParserContext *ctxt = parser_context_new(tokens);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600570 QObject *result;
571
Michael Roth65c0f1e2012-08-15 13:45:43 -0500572 if (!ctxt) {
Michael Rothc1990eb2011-06-01 12:15:00 -0500573 return NULL;
574 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600575
Michael Roth65c0f1e2012-08-15 13:45:43 -0500576 result = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600577
Michael Roth65c0f1e2012-08-15 13:45:43 -0500578 error_propagate(errp, ctxt->err);
579
580 parser_context_free(ctxt);
Anthony Liguorief749d02011-06-01 12:14:50 -0500581
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600582 return result;
583}