blob: 7437827c2471ab0270f092d8036bdb895c78616b [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 Armbrustere59f39d2018-08-23 18:39:49 +020016#include "qemu/unicode.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010017#include "qapi/error.h"
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060018#include "qemu-common.h"
Markus Armbruster6b673952018-02-01 12:18:35 +010019#include "qapi/qmp/qbool.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010020#include "qapi/qmp/qdict.h"
Markus Armbruster47e6b292018-02-01 12:18:38 +010021#include "qapi/qmp/qlist.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010022#include "qapi/qmp/qnull.h"
23#include "qapi/qmp/qnum.h"
Markus Armbruster6b673952018-02-01 12:18:35 +010024#include "qapi/qmp/qstring.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010025#include "qapi/qmp/json-parser.h"
26#include "qapi/qmp/json-lexer.h"
Paolo Bonzini9bada892015-11-25 22:23:32 +010027#include "qapi/qmp/json-streamer.h"
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060028
29typedef struct JSONParserContext
30{
Anthony Liguorief749d02011-06-01 12:14:50 -050031 Error *err;
Paolo Bonzini9bada892015-11-25 22:23:32 +010032 JSONToken *current;
Paolo Bonzini95385fe2015-11-25 22:23:31 +010033 GQueue *buf;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060034} JSONParserContext;
35
36#define BUG_ON(cond) assert(!(cond))
37
38/**
39 * TODO
40 *
41 * 0) make errors meaningful again
42 * 1) add geometry information to tokens
43 * 3) should we return a parsed size?
44 * 4) deal with premature EOI
45 */
46
Michael Roth65c0f1e2012-08-15 13:45:43 -050047static QObject *parse_value(JSONParserContext *ctxt, va_list *ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060048
49/**
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060050 * Error handler
51 */
Stefan Weil8b7968f2010-09-23 21:28:05 +020052static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext *ctxt,
Paolo Bonzini9bada892015-11-25 22:23:32 +010053 JSONToken *token, const char *msg, ...)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060054{
Amos Kongc96c84a2010-03-24 23:12:05 +080055 va_list ap;
Anthony Liguorief749d02011-06-01 12:14:50 -050056 char message[1024];
Markus Armbruster574bf162018-08-23 18:39:50 +020057
58 if (ctxt->err) {
59 return;
60 }
Amos Kongc96c84a2010-03-24 23:12:05 +080061 va_start(ap, msg);
Anthony Liguorief749d02011-06-01 12:14:50 -050062 vsnprintf(message, sizeof(message), msg, ap);
Amos Kongc96c84a2010-03-24 23:12:05 +080063 va_end(ap);
Cole Robinsonf231b882014-03-21 19:42:26 -040064 error_setg(&ctxt->err, "JSON parse error, %s", message);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -060065}
66
67/**
68 * String helpers
69 *
70 * These helpers are used to unescape strings.
71 */
72static void wchar_to_utf8(uint16_t wchar, char *buffer, size_t buffer_length)
73{
74 if (wchar <= 0x007F) {
75 BUG_ON(buffer_length < 2);
76
77 buffer[0] = wchar & 0x7F;
78 buffer[1] = 0;
79 } else if (wchar <= 0x07FF) {
80 BUG_ON(buffer_length < 3);
81
82 buffer[0] = 0xC0 | ((wchar >> 6) & 0x1F);
83 buffer[1] = 0x80 | (wchar & 0x3F);
84 buffer[2] = 0;
85 } else {
86 BUG_ON(buffer_length < 4);
87
88 buffer[0] = 0xE0 | ((wchar >> 12) & 0x0F);
89 buffer[1] = 0x80 | ((wchar >> 6) & 0x3F);
90 buffer[2] = 0x80 | (wchar & 0x3F);
91 buffer[3] = 0;
92 }
93}
94
95static int hex2decimal(char ch)
96{
97 if (ch >= '0' && ch <= '9') {
98 return (ch - '0');
99 } else if (ch >= 'a' && ch <= 'f') {
100 return 10 + (ch - 'a');
101 } else if (ch >= 'A' && ch <= 'F') {
102 return 10 + (ch - 'A');
103 }
104
105 return -1;
106}
107
108/**
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200109 * parse_string(): Parse a JSON string
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600110 *
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200111 * From RFC 8259 "The JavaScript Object Notation (JSON) Data
112 * Interchange Format":
113 *
114 * char = unescaped /
115 * escape (
116 * %x22 / ; " quotation mark U+0022
117 * %x5C / ; \ reverse solidus U+005C
118 * %x2F / ; / solidus U+002F
119 * %x62 / ; b backspace U+0008
120 * %x66 / ; f form feed U+000C
121 * %x6E / ; n line feed U+000A
122 * %x72 / ; r carriage return U+000D
123 * %x74 / ; t tab U+0009
124 * %x75 4HEXDIG ) ; uXXXX U+XXXX
125 * escape = %x5C ; \
126 * quotation-mark = %x22 ; "
127 * unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
128 *
129 * Extensions over RFC 8259:
130 * - Extra escape sequence in strings:
131 * 0x27 (apostrophe) is recognized after escape, too
132 * - Single-quoted strings:
133 * Like double-quoted strings, except they're delimited by %x27
134 * (apostrophe) instead of %x22 (quotation mark), and can't contain
135 * unescaped apostrophe, but can contain unescaped quotation mark.
136 *
137 * Note:
138 * - Encoding is modified UTF-8.
139 * - Invalid Unicode characters are rejected.
140 * - Control characters \x00..\x1F are rejected by the lexer.
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600141 */
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200142static QString *parse_string(JSONParserContext *ctxt, JSONToken *token)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600143{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100144 const char *ptr = token->str;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600145 QString *str;
Markus Armbruster00ea57f2018-08-23 18:39:47 +0200146 char quote;
Markus Armbrustere59f39d2018-08-23 18:39:49 +0200147 int cp;
148 char *end;
149 ssize_t len;
150 char utf8_buf[5];
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600151
Markus Armbruster00ea57f2018-08-23 18:39:47 +0200152 assert(*ptr == '"' || *ptr == '\'');
153 quote = *ptr++;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600154 str = qstring_new();
Markus Armbruster00ea57f2018-08-23 18:39:47 +0200155
156 while (*ptr != quote) {
157 assert(*ptr);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600158 if (*ptr == '\\') {
159 ptr++;
Markus Armbruster00ea57f2018-08-23 18:39:47 +0200160 switch (*ptr++) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600161 case '"':
162 qstring_append(str, "\"");
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600163 break;
164 case '\'':
165 qstring_append(str, "'");
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600166 break;
167 case '\\':
168 qstring_append(str, "\\");
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600169 break;
170 case '/':
171 qstring_append(str, "/");
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600172 break;
173 case 'b':
174 qstring_append(str, "\b");
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600175 break;
Luiz Capitulinobd032692010-05-19 17:06:15 -0300176 case 'f':
177 qstring_append(str, "\f");
Luiz Capitulinobd032692010-05-19 17:06:15 -0300178 break;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600179 case 'n':
180 qstring_append(str, "\n");
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600181 break;
182 case 'r':
183 qstring_append(str, "\r");
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600184 break;
185 case 't':
186 qstring_append(str, "\t");
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600187 break;
188 case 'u': {
189 uint16_t unicode_char = 0;
190 char utf8_char[4];
191 int i = 0;
192
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600193 for (i = 0; i < 4; i++) {
194 if (qemu_isxdigit(*ptr)) {
195 unicode_char |= hex2decimal(*ptr) << ((3 - i) * 4);
196 } else {
197 parse_error(ctxt, token,
198 "invalid hex escape sequence in string");
199 goto out;
200 }
201 ptr++;
202 }
203
204 wchar_to_utf8(unicode_char, utf8_char, sizeof(utf8_char));
205 qstring_append(str, utf8_char);
206 } break;
207 default:
208 parse_error(ctxt, token, "invalid escape sequence in string");
209 goto out;
210 }
211 } else {
Markus Armbrustere59f39d2018-08-23 18:39:49 +0200212 cp = mod_utf8_codepoint(ptr, 6, &end);
Markus Armbruster4b1c0cd2018-08-23 18:39:52 +0200213 if (cp < 0) {
Markus Armbrustere59f39d2018-08-23 18:39:49 +0200214 parse_error(ctxt, token, "invalid UTF-8 sequence in string");
215 goto out;
216 }
217 ptr = end;
218 len = mod_utf8_encode(utf8_buf, sizeof(utf8_buf), cp);
219 assert(len >= 0);
220 qstring_append(str, utf8_buf);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600221 }
222 }
223
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600224 return str;
225
226out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200227 qobject_unref(str);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600228 return NULL;
229}
230
Paolo Bonzini9bada892015-11-25 22:23:32 +0100231/* Note: the token object returned by parser_context_peek_token or
232 * parser_context_pop_token is deleted as soon as parser_context_pop_token
233 * is called again.
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100234 */
Paolo Bonzini9bada892015-11-25 22:23:32 +0100235static JSONToken *parser_context_pop_token(JSONParserContext *ctxt)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500236{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100237 g_free(ctxt->current);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100238 assert(!g_queue_is_empty(ctxt->buf));
239 ctxt->current = g_queue_pop_head(ctxt->buf);
240 return ctxt->current;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500241}
242
Paolo Bonzini9bada892015-11-25 22:23:32 +0100243static JSONToken *parser_context_peek_token(JSONParserContext *ctxt)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500244{
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100245 assert(!g_queue_is_empty(ctxt->buf));
246 return g_queue_peek_head(ctxt->buf);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500247}
248
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100249static JSONParserContext *parser_context_new(GQueue *tokens)
Michael Roth65c0f1e2012-08-15 13:45:43 -0500250{
251 JSONParserContext *ctxt;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500252
253 if (!tokens) {
254 return NULL;
255 }
256
Michael Roth65c0f1e2012-08-15 13:45:43 -0500257 ctxt = g_malloc0(sizeof(JSONParserContext));
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100258 ctxt->buf = tokens;
Michael Roth65c0f1e2012-08-15 13:45:43 -0500259
260 return ctxt;
261}
262
263/* to support error propagation, ctxt->err must be freed separately */
264static void parser_context_free(JSONParserContext *ctxt)
265{
Michael Roth65c0f1e2012-08-15 13:45:43 -0500266 if (ctxt) {
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100267 while (!g_queue_is_empty(ctxt->buf)) {
268 parser_context_pop_token(ctxt);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500269 }
Paolo Bonzini9bada892015-11-25 22:23:32 +0100270 g_free(ctxt->current);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100271 g_queue_free(ctxt->buf);
Michael Roth65c0f1e2012-08-15 13:45:43 -0500272 g_free(ctxt);
273 }
274}
275
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600276/**
277 * Parsing rules
278 */
Michael Roth65c0f1e2012-08-15 13:45:43 -0500279static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600280{
Max Reitz532fb532018-03-10 16:14:36 -0600281 QObject *value;
282 QString *key = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100283 JSONToken *peek, *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600284
Michael Roth65c0f1e2012-08-15 13:45:43 -0500285 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500286 if (peek == NULL) {
287 parse_error(ctxt, NULL, "premature EOI");
288 goto out;
289 }
290
Max Reitz532fb532018-03-10 16:14:36 -0600291 key = qobject_to(QString, parse_value(ctxt, ap));
292 if (!key) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600293 parse_error(ctxt, peek, "key is not a string in object");
294 goto out;
295 }
296
Michael Roth65c0f1e2012-08-15 13:45:43 -0500297 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500298 if (token == NULL) {
299 parse_error(ctxt, NULL, "premature EOI");
300 goto out;
301 }
302
Paolo Bonzini9bada892015-11-25 22:23:32 +0100303 if (token->type != JSON_COLON) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600304 parse_error(ctxt, token, "missing : in object pair");
305 goto out;
306 }
307
Michael Roth65c0f1e2012-08-15 13:45:43 -0500308 value = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600309 if (value == NULL) {
310 parse_error(ctxt, token, "Missing value in dict");
311 goto out;
312 }
313
Max Reitz532fb532018-03-10 16:14:36 -0600314 qdict_put_obj(dict, qstring_get_str(key), value);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600315
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200316 qobject_unref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600317
318 return 0;
319
320out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200321 qobject_unref(key);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600322
323 return -1;
324}
325
Michael Roth65c0f1e2012-08-15 13:45:43 -0500326static QObject *parse_object(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600327{
328 QDict *dict = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100329 JSONToken *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600330
Michael Roth65c0f1e2012-08-15 13:45:43 -0500331 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100332 assert(token && token->type == JSON_LCURLY);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600333
334 dict = qdict_new();
335
Michael Roth65c0f1e2012-08-15 13:45:43 -0500336 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500337 if (peek == NULL) {
338 parse_error(ctxt, NULL, "premature EOI");
339 goto out;
340 }
341
Paolo Bonzini9bada892015-11-25 22:23:32 +0100342 if (peek->type != JSON_RCURLY) {
Michael Roth65c0f1e2012-08-15 13:45:43 -0500343 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600344 goto out;
345 }
346
Michael Roth65c0f1e2012-08-15 13:45:43 -0500347 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500348 if (token == NULL) {
349 parse_error(ctxt, NULL, "premature EOI");
350 goto out;
351 }
352
Paolo Bonzini9bada892015-11-25 22:23:32 +0100353 while (token->type != JSON_RCURLY) {
354 if (token->type != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600355 parse_error(ctxt, token, "expected separator in dict");
356 goto out;
357 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600358
Michael Roth65c0f1e2012-08-15 13:45:43 -0500359 if (parse_pair(ctxt, dict, ap) == -1) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600360 goto out;
361 }
362
Michael Roth65c0f1e2012-08-15 13:45:43 -0500363 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500364 if (token == NULL) {
365 parse_error(ctxt, NULL, "premature EOI");
366 goto out;
367 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600368 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600369 } else {
Gongleia491af42014-06-10 17:20:24 +0800370 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600371 }
372
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600373 return QOBJECT(dict);
374
375out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200376 qobject_unref(dict);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600377 return NULL;
378}
379
Michael Roth65c0f1e2012-08-15 13:45:43 -0500380static QObject *parse_array(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600381{
382 QList *list = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +0100383 JSONToken *token, *peek;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600384
Michael Roth65c0f1e2012-08-15 13:45:43 -0500385 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100386 assert(token && token->type == JSON_LSQUARE);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600387
388 list = qlist_new();
389
Michael Roth65c0f1e2012-08-15 13:45:43 -0500390 peek = parser_context_peek_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500391 if (peek == NULL) {
392 parse_error(ctxt, NULL, "premature EOI");
393 goto out;
394 }
395
Paolo Bonzini9bada892015-11-25 22:23:32 +0100396 if (peek->type != JSON_RSQUARE) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600397 QObject *obj;
398
Michael Roth65c0f1e2012-08-15 13:45:43 -0500399 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600400 if (obj == NULL) {
401 parse_error(ctxt, token, "expecting value");
402 goto out;
403 }
404
405 qlist_append_obj(list, obj);
406
Michael Roth65c0f1e2012-08-15 13:45:43 -0500407 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500408 if (token == NULL) {
409 parse_error(ctxt, NULL, "premature EOI");
410 goto out;
411 }
412
Paolo Bonzini9bada892015-11-25 22:23:32 +0100413 while (token->type != JSON_RSQUARE) {
414 if (token->type != JSON_COMMA) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600415 parse_error(ctxt, token, "expected separator in list");
416 goto out;
417 }
418
Michael Roth65c0f1e2012-08-15 13:45:43 -0500419 obj = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600420 if (obj == NULL) {
421 parse_error(ctxt, token, "expecting value");
422 goto out;
423 }
424
425 qlist_append_obj(list, obj);
426
Michael Roth65c0f1e2012-08-15 13:45:43 -0500427 token = parser_context_pop_token(ctxt);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500428 if (token == NULL) {
429 parse_error(ctxt, NULL, "premature EOI");
430 goto out;
431 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600432 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600433 } else {
Gongleia491af42014-06-10 17:20:24 +0800434 (void)parser_context_pop_token(ctxt);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600435 }
436
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600437 return QOBJECT(list);
438
439out:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200440 qobject_unref(list);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600441 return NULL;
442}
443
Michael Roth65c0f1e2012-08-15 13:45:43 -0500444static QObject *parse_keyword(JSONParserContext *ctxt)
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
Michael Roth65c0f1e2012-08-15 13:45:43 -0500448 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100449 assert(token && token->type == JSON_KEYWORD);
Markus Armbruster50e2a462015-11-25 22:23:27 +0100450
Paolo Bonzini9bada892015-11-25 22:23:32 +0100451 if (!strcmp(token->str, "true")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100452 return QOBJECT(qbool_from_bool(true));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100453 } else if (!strcmp(token->str, "false")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100454 return QOBJECT(qbool_from_bool(false));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100455 } else if (!strcmp(token->str, "null")) {
Markus Armbruster006ca092017-06-26 13:52:24 +0200456 return QOBJECT(qnull());
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600457 }
Paolo Bonzini9bada892015-11-25 22:23:32 +0100458 parse_error(ctxt, token, "invalid keyword '%s'", token->str);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600459 return NULL;
460}
461
Michael Roth65c0f1e2012-08-15 13:45:43 -0500462static QObject *parse_escape(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600463{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100464 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600465
466 if (ap == NULL) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100467 return NULL;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600468 }
469
Michael Roth65c0f1e2012-08-15 13:45:43 -0500470 token = parser_context_pop_token(ctxt);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100471 assert(token && token->type == JSON_ESCAPE);
Markus Armbruster6b9606f2015-11-25 22:23:28 +0100472
Paolo Bonzini9bada892015-11-25 22:23:32 +0100473 if (!strcmp(token->str, "%p")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100474 return va_arg(*ap, QObject *);
Paolo Bonzini9bada892015-11-25 22:23:32 +0100475 } else if (!strcmp(token->str, "%i")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100476 return QOBJECT(qbool_from_bool(va_arg(*ap, int)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100477 } else if (!strcmp(token->str, "%d")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400478 return QOBJECT(qnum_from_int(va_arg(*ap, int)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100479 } else if (!strcmp(token->str, "%ld")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400480 return QOBJECT(qnum_from_int(va_arg(*ap, long)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100481 } else if (!strcmp(token->str, "%lld") ||
482 !strcmp(token->str, "%I64d")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400483 return QOBJECT(qnum_from_int(va_arg(*ap, long long)));
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400484 } else if (!strcmp(token->str, "%u")) {
485 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned int)));
486 } else if (!strcmp(token->str, "%lu")) {
487 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned long)));
488 } else if (!strcmp(token->str, "%llu") ||
489 !strcmp(token->str, "%I64u")) {
490 return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned long long)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100491 } else if (!strcmp(token->str, "%s")) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100492 return QOBJECT(qstring_from_str(va_arg(*ap, const char *)));
Paolo Bonzini9bada892015-11-25 22:23:32 +0100493 } else if (!strcmp(token->str, "%f")) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400494 return QOBJECT(qnum_from_double(va_arg(*ap, double)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600495 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600496 return NULL;
497}
498
Michael Roth65c0f1e2012-08-15 13:45:43 -0500499static QObject *parse_literal(JSONParserContext *ctxt)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600500{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100501 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600502
Michael Roth65c0f1e2012-08-15 13:45:43 -0500503 token = parser_context_pop_token(ctxt);
Markus Armbrusterd538b252015-11-25 22:23:30 +0100504 assert(token);
Anthony Liguori11e8a462011-06-01 12:14:55 -0500505
Paolo Bonzini9bada892015-11-25 22:23:32 +0100506 switch (token->type) {
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600507 case JSON_STRING:
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200508 return QOBJECT(parse_string(ctxt, token));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500509 case JSON_INTEGER: {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400510 /*
511 * Represent JSON_INTEGER as QNUM_I64 if possible, else as
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400512 * QNUM_U64, else as QNUM_DOUBLE. Note that qemu_strtoi64()
513 * and qemu_strtou64() fail with ERANGE when it's not
514 * possible.
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500515 *
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400516 * qnum_get_int() will then work for any signed 64-bit
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400517 * JSON_INTEGER, qnum_get_uint() for any unsigned 64-bit
518 * integer, and qnum_get_double() both for any JSON_INTEGER
519 * and any JSON_FLOAT (with precision loss for integers beyond
520 * 53 bits)
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500521 */
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400522 int ret;
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500523 int64_t value;
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400524 uint64_t uvalue;
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500525
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400526 ret = qemu_strtoi64(token->str, NULL, 10, &value);
527 if (!ret) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400528 return QOBJECT(qnum_from_int(value));
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500529 }
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400530 assert(ret == -ERANGE);
531
532 if (token->str[0] != '-') {
533 ret = qemu_strtou64(token->str, NULL, 10, &uvalue);
534 if (!ret) {
535 return QOBJECT(qnum_from_uint(uvalue));
536 }
537 assert(ret == -ERANGE);
538 }
Michael Roth3d5b3ec2013-05-10 17:46:05 -0500539 /* fall through to JSON_FLOAT */
540 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600541 case JSON_FLOAT:
Eric Blake6e8e5cb2016-01-29 06:48:37 -0700542 /* FIXME dependent on locale; a pervasive issue in QEMU */
543 /* FIXME our lexer matches RFC 7159 in forbidding Inf or NaN,
544 * but those might be useful extensions beyond JSON */
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400545 return QOBJECT(qnum_from_double(strtod(token->str, NULL)));
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600546 default:
Markus Armbrusterd538b252015-11-25 22:23:30 +0100547 abort();
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600548 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600549}
550
Michael Roth65c0f1e2012-08-15 13:45:43 -0500551static QObject *parse_value(JSONParserContext *ctxt, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600552{
Paolo Bonzini9bada892015-11-25 22:23:32 +0100553 JSONToken *token;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600554
Markus Armbrusterd538b252015-11-25 22:23:30 +0100555 token = parser_context_peek_token(ctxt);
556 if (token == NULL) {
557 parse_error(ctxt, NULL, "premature EOI");
558 return NULL;
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600559 }
560
Paolo Bonzini9bada892015-11-25 22:23:32 +0100561 switch (token->type) {
Markus Armbrusterd538b252015-11-25 22:23:30 +0100562 case JSON_LCURLY:
563 return parse_object(ctxt, ap);
564 case JSON_LSQUARE:
565 return parse_array(ctxt, ap);
566 case JSON_ESCAPE:
567 return parse_escape(ctxt, ap);
568 case JSON_INTEGER:
569 case JSON_FLOAT:
570 case JSON_STRING:
571 return parse_literal(ctxt);
572 case JSON_KEYWORD:
573 return parse_keyword(ctxt);
574 default:
575 parse_error(ctxt, token, "expecting value");
576 return NULL;
577 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600578}
579
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100580QObject *json_parser_parse(GQueue *tokens, va_list *ap)
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600581{
Anthony Liguorief749d02011-06-01 12:14:50 -0500582 return json_parser_parse_err(tokens, ap, NULL);
583}
584
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100585QObject *json_parser_parse_err(GQueue *tokens, va_list *ap, Error **errp)
Anthony Liguorief749d02011-06-01 12:14:50 -0500586{
Michael Roth65c0f1e2012-08-15 13:45:43 -0500587 JSONParserContext *ctxt = parser_context_new(tokens);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600588 QObject *result;
589
Michael Roth65c0f1e2012-08-15 13:45:43 -0500590 if (!ctxt) {
Michael Rothc1990eb2011-06-01 12:15:00 -0500591 return NULL;
592 }
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600593
Michael Roth65c0f1e2012-08-15 13:45:43 -0500594 result = parse_value(ctxt, ap);
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600595
Michael Roth65c0f1e2012-08-15 13:45:43 -0500596 error_propagate(errp, ctxt->err);
597
598 parser_context_free(ctxt);
Anthony Liguorief749d02011-06-01 12:14:50 -0500599
Anthony Liguori4a5fcab2009-11-11 10:39:23 -0600600 return result;
601}