4 * Copyright IBM, Corp. 2009
7 * Anthony Liguori <aliguori@us.ibm.com>
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.
14 #include "qemu/osdep.h"
16 #include "qapi/error.h"
17 #include "qemu-common.h"
18 #include "qapi/qmp/qstring.h"
19 #include "qapi/qmp/qint.h"
20 #include "qapi/qmp/qdict.h"
21 #include "qapi/qmp/qlist.h"
22 #include "qapi/qmp/qfloat.h"
23 #include "qapi/qmp/qbool.h"
24 #include "qapi/qmp/json-parser.h"
25 #include "qapi/qmp/json-lexer.h"
26 #include "qapi/qmp/json-streamer.h"
28 typedef struct JSONParserContext
35 #define BUG_ON(cond) assert(!(cond))
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
46 static QObject
*parse_value(JSONParserContext
*ctxt
, va_list *ap
);
51 static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext
*ctxt
,
52 JSONToken
*token
, const char *msg
, ...)
57 vsnprintf(message
, sizeof(message
), msg
, ap
);
60 error_free(ctxt
->err
);
63 error_setg(&ctxt
->err
, "JSON parse error, %s", message
);
69 * These helpers are used to unescape strings.
71 static void wchar_to_utf8(uint16_t wchar
, char *buffer
, size_t buffer_length
)
73 if (wchar
<= 0x007F) {
74 BUG_ON(buffer_length
< 2);
76 buffer
[0] = wchar
& 0x7F;
78 } else if (wchar
<= 0x07FF) {
79 BUG_ON(buffer_length
< 3);
81 buffer
[0] = 0xC0 | ((wchar
>> 6) & 0x1F);
82 buffer
[1] = 0x80 | (wchar
& 0x3F);
85 BUG_ON(buffer_length
< 4);
87 buffer
[0] = 0xE0 | ((wchar
>> 12) & 0x0F);
88 buffer
[1] = 0x80 | ((wchar
>> 6) & 0x3F);
89 buffer
[2] = 0x80 | (wchar
& 0x3F);
94 static int hex2decimal(char ch
)
96 if (ch
>= '0' && ch
<= '9') {
98 } else if (ch
>= 'a' && ch
<= 'f') {
99 return 10 + (ch
- 'a');
100 } else if (ch
>= 'A' && ch
<= 'F') {
101 return 10 + (ch
- 'A');
108 * parse_string(): Parse a json string and return a QObject
117 * any-Unicode-character-
130 static QString
*qstring_from_escaped_str(JSONParserContext
*ctxt
,
133 const char *ptr
= token
->str
;
135 int double_quote
= 1;
146 ((double_quote
&& *ptr
!= '"') || (!double_quote
&& *ptr
!= '\''))) {
152 qstring_append(str
, "\"");
156 qstring_append(str
, "'");
160 qstring_append(str
, "\\");
164 qstring_append(str
, "/");
168 qstring_append(str
, "\b");
172 qstring_append(str
, "\f");
176 qstring_append(str
, "\n");
180 qstring_append(str
, "\r");
184 qstring_append(str
, "\t");
188 uint16_t unicode_char
= 0;
194 for (i
= 0; i
< 4; i
++) {
195 if (qemu_isxdigit(*ptr
)) {
196 unicode_char
|= hex2decimal(*ptr
) << ((3 - i
) * 4);
198 parse_error(ctxt
, token
,
199 "invalid hex escape sequence in string");
205 wchar_to_utf8(unicode_char
, utf8_char
, sizeof(utf8_char
));
206 qstring_append(str
, utf8_char
);
209 parse_error(ctxt
, token
, "invalid escape sequence in string");
218 qstring_append(str
, dummy
);
229 /* 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
233 static JSONToken
*parser_context_pop_token(JSONParserContext
*ctxt
)
235 g_free(ctxt
->current
);
236 assert(!g_queue_is_empty(ctxt
->buf
));
237 ctxt
->current
= g_queue_pop_head(ctxt
->buf
);
238 return ctxt
->current
;
241 static JSONToken
*parser_context_peek_token(JSONParserContext
*ctxt
)
243 assert(!g_queue_is_empty(ctxt
->buf
));
244 return g_queue_peek_head(ctxt
->buf
);
247 static JSONParserContext
*parser_context_new(GQueue
*tokens
)
249 JSONParserContext
*ctxt
;
255 ctxt
= g_malloc0(sizeof(JSONParserContext
));
261 /* to support error propagation, ctxt->err must be freed separately */
262 static void parser_context_free(JSONParserContext
*ctxt
)
265 while (!g_queue_is_empty(ctxt
->buf
)) {
266 parser_context_pop_token(ctxt
);
268 g_free(ctxt
->current
);
269 g_queue_free(ctxt
->buf
);
277 static int parse_pair(JSONParserContext
*ctxt
, QDict
*dict
, va_list *ap
)
279 QObject
*key
= NULL
, *value
;
280 JSONToken
*peek
, *token
;
282 peek
= parser_context_peek_token(ctxt
);
284 parse_error(ctxt
, NULL
, "premature EOI");
288 key
= parse_value(ctxt
, ap
);
289 if (!key
|| qobject_type(key
) != QTYPE_QSTRING
) {
290 parse_error(ctxt
, peek
, "key is not a string in object");
294 token
= parser_context_pop_token(ctxt
);
296 parse_error(ctxt
, NULL
, "premature EOI");
300 if (token
->type
!= JSON_COLON
) {
301 parse_error(ctxt
, token
, "missing : in object pair");
305 value
= parse_value(ctxt
, ap
);
307 parse_error(ctxt
, token
, "Missing value in dict");
311 qdict_put_obj(dict
, qstring_get_str(qobject_to_qstring(key
)), value
);
323 static QObject
*parse_object(JSONParserContext
*ctxt
, va_list *ap
)
326 JSONToken
*token
, *peek
;
328 token
= parser_context_pop_token(ctxt
);
329 assert(token
&& token
->type
== JSON_LCURLY
);
333 peek
= parser_context_peek_token(ctxt
);
335 parse_error(ctxt
, NULL
, "premature EOI");
339 if (peek
->type
!= JSON_RCURLY
) {
340 if (parse_pair(ctxt
, dict
, ap
) == -1) {
344 token
= parser_context_pop_token(ctxt
);
346 parse_error(ctxt
, NULL
, "premature EOI");
350 while (token
->type
!= JSON_RCURLY
) {
351 if (token
->type
!= JSON_COMMA
) {
352 parse_error(ctxt
, token
, "expected separator in dict");
356 if (parse_pair(ctxt
, dict
, ap
) == -1) {
360 token
= parser_context_pop_token(ctxt
);
362 parse_error(ctxt
, NULL
, "premature EOI");
367 (void)parser_context_pop_token(ctxt
);
370 return QOBJECT(dict
);
377 static QObject
*parse_array(JSONParserContext
*ctxt
, va_list *ap
)
380 JSONToken
*token
, *peek
;
382 token
= parser_context_pop_token(ctxt
);
383 assert(token
&& token
->type
== JSON_LSQUARE
);
387 peek
= parser_context_peek_token(ctxt
);
389 parse_error(ctxt
, NULL
, "premature EOI");
393 if (peek
->type
!= JSON_RSQUARE
) {
396 obj
= parse_value(ctxt
, ap
);
398 parse_error(ctxt
, token
, "expecting value");
402 qlist_append_obj(list
, obj
);
404 token
= parser_context_pop_token(ctxt
);
406 parse_error(ctxt
, NULL
, "premature EOI");
410 while (token
->type
!= JSON_RSQUARE
) {
411 if (token
->type
!= JSON_COMMA
) {
412 parse_error(ctxt
, token
, "expected separator in list");
416 obj
= parse_value(ctxt
, ap
);
418 parse_error(ctxt
, token
, "expecting value");
422 qlist_append_obj(list
, obj
);
424 token
= parser_context_pop_token(ctxt
);
426 parse_error(ctxt
, NULL
, "premature EOI");
431 (void)parser_context_pop_token(ctxt
);
434 return QOBJECT(list
);
441 static QObject
*parse_keyword(JSONParserContext
*ctxt
)
445 token
= parser_context_pop_token(ctxt
);
446 assert(token
&& token
->type
== JSON_KEYWORD
);
448 if (!strcmp(token
->str
, "true")) {
449 return QOBJECT(qbool_from_bool(true));
450 } else if (!strcmp(token
->str
, "false")) {
451 return QOBJECT(qbool_from_bool(false));
452 } else if (!strcmp(token
->str
, "null")) {
455 parse_error(ctxt
, token
, "invalid keyword '%s'", token
->str
);
459 static QObject
*parse_escape(JSONParserContext
*ctxt
, va_list *ap
)
467 token
= parser_context_pop_token(ctxt
);
468 assert(token
&& token
->type
== JSON_ESCAPE
);
470 if (!strcmp(token
->str
, "%p")) {
471 return va_arg(*ap
, QObject
*);
472 } else if (!strcmp(token
->str
, "%i")) {
473 return QOBJECT(qbool_from_bool(va_arg(*ap
, int)));
474 } else if (!strcmp(token
->str
, "%d")) {
475 return QOBJECT(qint_from_int(va_arg(*ap
, int)));
476 } else if (!strcmp(token
->str
, "%ld")) {
477 return QOBJECT(qint_from_int(va_arg(*ap
, long)));
478 } else if (!strcmp(token
->str
, "%lld") ||
479 !strcmp(token
->str
, "%I64d")) {
480 return QOBJECT(qint_from_int(va_arg(*ap
, long long)));
481 } else if (!strcmp(token
->str
, "%s")) {
482 return QOBJECT(qstring_from_str(va_arg(*ap
, const char *)));
483 } else if (!strcmp(token
->str
, "%f")) {
484 return QOBJECT(qfloat_from_double(va_arg(*ap
, double)));
489 static QObject
*parse_literal(JSONParserContext
*ctxt
)
493 token
= parser_context_pop_token(ctxt
);
496 switch (token
->type
) {
498 return QOBJECT(qstring_from_escaped_str(ctxt
, token
));
500 /* A possibility exists that this is a whole-valued float where the
501 * fractional part was left out due to being 0 (.0). It's not a big
502 * deal to treat these as ints in the parser, so long as users of the
503 * resulting QObject know to expect a QInt in place of a QFloat in
506 * However, in some cases these values will overflow/underflow a
507 * QInt/int64 container, thus we should assume these are to be handled
508 * as QFloats/doubles rather than silently changing their values.
510 * strtoll() indicates these instances by setting errno to ERANGE
514 errno
= 0; /* strtoll doesn't set errno on success */
515 value
= strtoll(token
->str
, NULL
, 10);
516 if (errno
!= ERANGE
) {
517 return QOBJECT(qint_from_int(value
));
519 /* fall through to JSON_FLOAT */
522 /* FIXME dependent on locale; a pervasive issue in QEMU */
523 /* FIXME our lexer matches RFC 7159 in forbidding Inf or NaN,
524 * but those might be useful extensions beyond JSON */
525 return QOBJECT(qfloat_from_double(strtod(token
->str
, NULL
)));
531 static QObject
*parse_value(JSONParserContext
*ctxt
, va_list *ap
)
535 token
= parser_context_peek_token(ctxt
);
537 parse_error(ctxt
, NULL
, "premature EOI");
541 switch (token
->type
) {
543 return parse_object(ctxt
, ap
);
545 return parse_array(ctxt
, ap
);
547 return parse_escape(ctxt
, ap
);
551 return parse_literal(ctxt
);
553 return parse_keyword(ctxt
);
555 parse_error(ctxt
, token
, "expecting value");
560 QObject
*json_parser_parse(GQueue
*tokens
, va_list *ap
)
562 return json_parser_parse_err(tokens
, ap
, NULL
);
565 QObject
*json_parser_parse_err(GQueue
*tokens
, va_list *ap
, Error
**errp
)
567 JSONParserContext
*ctxt
= parser_context_new(tokens
);
574 result
= parse_value(ctxt
, ap
);
576 error_propagate(errp
, ctxt
->err
);
578 parser_context_free(ctxt
);