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 "qemu-common.h"
17 #include "qapi/qmp/qstring.h"
18 #include "qapi/qmp/qint.h"
19 #include "qapi/qmp/qdict.h"
20 #include "qapi/qmp/qlist.h"
21 #include "qapi/qmp/qfloat.h"
22 #include "qapi/qmp/qbool.h"
23 #include "qapi/qmp/json-parser.h"
24 #include "qapi/qmp/json-lexer.h"
25 #include "qapi/qmp/json-streamer.h"
27 typedef struct JSONParserContext
34 #define BUG_ON(cond) assert(!(cond))
39 * 0) make errors meaningful again
40 * 1) add geometry information to tokens
41 * 3) should we return a parsed size?
42 * 4) deal with premature EOI
45 static QObject
*parse_value(JSONParserContext
*ctxt
, va_list *ap
);
50 static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext
*ctxt
,
51 JSONToken
*token
, const char *msg
, ...)
56 vsnprintf(message
, sizeof(message
), msg
, ap
);
59 error_free(ctxt
->err
);
62 error_setg(&ctxt
->err
, "JSON parse error, %s", message
);
68 * These helpers are used to unescape strings.
70 static void wchar_to_utf8(uint16_t wchar
, char *buffer
, size_t buffer_length
)
72 if (wchar
<= 0x007F) {
73 BUG_ON(buffer_length
< 2);
75 buffer
[0] = wchar
& 0x7F;
77 } else if (wchar
<= 0x07FF) {
78 BUG_ON(buffer_length
< 3);
80 buffer
[0] = 0xC0 | ((wchar
>> 6) & 0x1F);
81 buffer
[1] = 0x80 | (wchar
& 0x3F);
84 BUG_ON(buffer_length
< 4);
86 buffer
[0] = 0xE0 | ((wchar
>> 12) & 0x0F);
87 buffer
[1] = 0x80 | ((wchar
>> 6) & 0x3F);
88 buffer
[2] = 0x80 | (wchar
& 0x3F);
93 static int hex2decimal(char ch
)
95 if (ch
>= '0' && ch
<= '9') {
97 } else if (ch
>= 'a' && ch
<= 'f') {
98 return 10 + (ch
- 'a');
99 } else if (ch
>= 'A' && ch
<= 'F') {
100 return 10 + (ch
- 'A');
107 * parse_string(): Parse a json string and return a QObject
116 * any-Unicode-character-
129 static QString
*qstring_from_escaped_str(JSONParserContext
*ctxt
,
132 const char *ptr
= token
->str
;
134 int double_quote
= 1;
145 ((double_quote
&& *ptr
!= '"') || (!double_quote
&& *ptr
!= '\''))) {
151 qstring_append(str
, "\"");
155 qstring_append(str
, "'");
159 qstring_append(str
, "\\");
163 qstring_append(str
, "/");
167 qstring_append(str
, "\b");
171 qstring_append(str
, "\f");
175 qstring_append(str
, "\n");
179 qstring_append(str
, "\r");
183 qstring_append(str
, "\t");
187 uint16_t unicode_char
= 0;
193 for (i
= 0; i
< 4; i
++) {
194 if (qemu_isxdigit(*ptr
)) {
195 unicode_char
|= hex2decimal(*ptr
) << ((3 - i
) * 4);
197 parse_error(ctxt
, token
,
198 "invalid hex escape sequence in string");
204 wchar_to_utf8(unicode_char
, utf8_char
, sizeof(utf8_char
));
205 qstring_append(str
, utf8_char
);
208 parse_error(ctxt
, token
, "invalid escape sequence in string");
217 qstring_append(str
, dummy
);
228 /* Note: the token object returned by parser_context_peek_token or
229 * parser_context_pop_token is deleted as soon as parser_context_pop_token
232 static JSONToken
*parser_context_pop_token(JSONParserContext
*ctxt
)
234 g_free(ctxt
->current
);
235 assert(!g_queue_is_empty(ctxt
->buf
));
236 ctxt
->current
= g_queue_pop_head(ctxt
->buf
);
237 return ctxt
->current
;
240 static JSONToken
*parser_context_peek_token(JSONParserContext
*ctxt
)
242 assert(!g_queue_is_empty(ctxt
->buf
));
243 return g_queue_peek_head(ctxt
->buf
);
246 static JSONParserContext
*parser_context_new(GQueue
*tokens
)
248 JSONParserContext
*ctxt
;
254 ctxt
= g_malloc0(sizeof(JSONParserContext
));
260 /* to support error propagation, ctxt->err must be freed separately */
261 static void parser_context_free(JSONParserContext
*ctxt
)
264 while (!g_queue_is_empty(ctxt
->buf
)) {
265 parser_context_pop_token(ctxt
);
267 g_free(ctxt
->current
);
268 g_queue_free(ctxt
->buf
);
276 static int parse_pair(JSONParserContext
*ctxt
, QDict
*dict
, va_list *ap
)
278 QObject
*key
= NULL
, *value
;
279 JSONToken
*peek
, *token
;
281 peek
= parser_context_peek_token(ctxt
);
283 parse_error(ctxt
, NULL
, "premature EOI");
287 key
= parse_value(ctxt
, ap
);
288 if (!key
|| qobject_type(key
) != QTYPE_QSTRING
) {
289 parse_error(ctxt
, peek
, "key is not a string in object");
293 token
= parser_context_pop_token(ctxt
);
295 parse_error(ctxt
, NULL
, "premature EOI");
299 if (token
->type
!= JSON_COLON
) {
300 parse_error(ctxt
, token
, "missing : in object pair");
304 value
= parse_value(ctxt
, ap
);
306 parse_error(ctxt
, token
, "Missing value in dict");
310 qdict_put_obj(dict
, qstring_get_str(qobject_to_qstring(key
)), value
);
322 static QObject
*parse_object(JSONParserContext
*ctxt
, va_list *ap
)
325 JSONToken
*token
, *peek
;
327 token
= parser_context_pop_token(ctxt
);
328 assert(token
&& token
->type
== JSON_LCURLY
);
332 peek
= parser_context_peek_token(ctxt
);
334 parse_error(ctxt
, NULL
, "premature EOI");
338 if (peek
->type
!= JSON_RCURLY
) {
339 if (parse_pair(ctxt
, dict
, ap
) == -1) {
343 token
= parser_context_pop_token(ctxt
);
345 parse_error(ctxt
, NULL
, "premature EOI");
349 while (token
->type
!= JSON_RCURLY
) {
350 if (token
->type
!= JSON_COMMA
) {
351 parse_error(ctxt
, token
, "expected separator in dict");
355 if (parse_pair(ctxt
, dict
, ap
) == -1) {
359 token
= parser_context_pop_token(ctxt
);
361 parse_error(ctxt
, NULL
, "premature EOI");
366 (void)parser_context_pop_token(ctxt
);
369 return QOBJECT(dict
);
376 static QObject
*parse_array(JSONParserContext
*ctxt
, va_list *ap
)
379 JSONToken
*token
, *peek
;
381 token
= parser_context_pop_token(ctxt
);
382 assert(token
&& token
->type
== JSON_LSQUARE
);
386 peek
= parser_context_peek_token(ctxt
);
388 parse_error(ctxt
, NULL
, "premature EOI");
392 if (peek
->type
!= JSON_RSQUARE
) {
395 obj
= parse_value(ctxt
, ap
);
397 parse_error(ctxt
, token
, "expecting value");
401 qlist_append_obj(list
, obj
);
403 token
= parser_context_pop_token(ctxt
);
405 parse_error(ctxt
, NULL
, "premature EOI");
409 while (token
->type
!= JSON_RSQUARE
) {
410 if (token
->type
!= JSON_COMMA
) {
411 parse_error(ctxt
, token
, "expected separator in list");
415 obj
= parse_value(ctxt
, ap
);
417 parse_error(ctxt
, token
, "expecting value");
421 qlist_append_obj(list
, obj
);
423 token
= parser_context_pop_token(ctxt
);
425 parse_error(ctxt
, NULL
, "premature EOI");
430 (void)parser_context_pop_token(ctxt
);
433 return QOBJECT(list
);
440 static QObject
*parse_keyword(JSONParserContext
*ctxt
)
444 token
= parser_context_pop_token(ctxt
);
445 assert(token
&& token
->type
== JSON_KEYWORD
);
447 if (!strcmp(token
->str
, "true")) {
448 return QOBJECT(qbool_from_bool(true));
449 } else if (!strcmp(token
->str
, "false")) {
450 return QOBJECT(qbool_from_bool(false));
451 } else if (!strcmp(token
->str
, "null")) {
454 parse_error(ctxt
, token
, "invalid keyword '%s'", token
->str
);
458 static QObject
*parse_escape(JSONParserContext
*ctxt
, va_list *ap
)
466 token
= parser_context_pop_token(ctxt
);
467 assert(token
&& token
->type
== JSON_ESCAPE
);
469 if (!strcmp(token
->str
, "%p")) {
470 return va_arg(*ap
, QObject
*);
471 } else if (!strcmp(token
->str
, "%i")) {
472 return QOBJECT(qbool_from_bool(va_arg(*ap
, int)));
473 } else if (!strcmp(token
->str
, "%d")) {
474 return QOBJECT(qint_from_int(va_arg(*ap
, int)));
475 } else if (!strcmp(token
->str
, "%ld")) {
476 return QOBJECT(qint_from_int(va_arg(*ap
, long)));
477 } else if (!strcmp(token
->str
, "%lld") ||
478 !strcmp(token
->str
, "%I64d")) {
479 return QOBJECT(qint_from_int(va_arg(*ap
, long long)));
480 } else if (!strcmp(token
->str
, "%s")) {
481 return QOBJECT(qstring_from_str(va_arg(*ap
, const char *)));
482 } else if (!strcmp(token
->str
, "%f")) {
483 return QOBJECT(qfloat_from_double(va_arg(*ap
, double)));
488 static QObject
*parse_literal(JSONParserContext
*ctxt
)
492 token
= parser_context_pop_token(ctxt
);
495 switch (token
->type
) {
497 return QOBJECT(qstring_from_escaped_str(ctxt
, token
));
499 /* A possibility exists that this is a whole-valued float where the
500 * fractional part was left out due to being 0 (.0). It's not a big
501 * deal to treat these as ints in the parser, so long as users of the
502 * resulting QObject know to expect a QInt in place of a QFloat in
505 * However, in some cases these values will overflow/underflow a
506 * QInt/int64 container, thus we should assume these are to be handled
507 * as QFloats/doubles rather than silently changing their values.
509 * strtoll() indicates these instances by setting errno to ERANGE
513 errno
= 0; /* strtoll doesn't set errno on success */
514 value
= strtoll(token
->str
, NULL
, 10);
515 if (errno
!= ERANGE
) {
516 return QOBJECT(qint_from_int(value
));
518 /* fall through to JSON_FLOAT */
521 /* FIXME dependent on locale; a pervasive issue in QEMU */
522 /* FIXME our lexer matches RFC 7159 in forbidding Inf or NaN,
523 * but those might be useful extensions beyond JSON */
524 return QOBJECT(qfloat_from_double(strtod(token
->str
, NULL
)));
530 static QObject
*parse_value(JSONParserContext
*ctxt
, va_list *ap
)
534 token
= parser_context_peek_token(ctxt
);
536 parse_error(ctxt
, NULL
, "premature EOI");
540 switch (token
->type
) {
542 return parse_object(ctxt
, ap
);
544 return parse_array(ctxt
, ap
);
546 return parse_escape(ctxt
, ap
);
550 return parse_literal(ctxt
);
552 return parse_keyword(ctxt
);
554 parse_error(ctxt
, token
, "expecting value");
559 QObject
*json_parser_parse(GQueue
*tokens
, va_list *ap
)
561 return json_parser_parse_err(tokens
, ap
, NULL
);
564 QObject
*json_parser_parse_err(GQueue
*tokens
, va_list *ap
, Error
**errp
)
566 JSONParserContext
*ctxt
= parser_context_new(tokens
);
573 result
= parse_value(ctxt
, ap
);
575 error_propagate(errp
, ctxt
->err
);
577 parser_context_free(ctxt
);