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"
15 #include "qemu/cutils.h"
16 #include "qemu/unicode.h"
17 #include "qapi/error.h"
18 #include "qemu-common.h"
19 #include "qapi/qmp/qbool.h"
20 #include "qapi/qmp/qdict.h"
21 #include "qapi/qmp/qlist.h"
22 #include "qapi/qmp/qnull.h"
23 #include "qapi/qmp/qnum.h"
24 #include "qapi/qmp/qstring.h"
25 #include "json-parser-int.h"
34 typedef struct JSONParserContext
41 #define BUG_ON(cond) assert(!(cond))
46 * 0) make errors meaningful again
47 * 1) add geometry information to tokens
48 * 3) should we return a parsed size?
49 * 4) deal with premature EOI
52 static QObject
*parse_value(JSONParserContext
*ctxt
, va_list *ap
);
57 static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext
*ctxt
,
58 JSONToken
*token
, const char *msg
, ...)
67 vsnprintf(message
, sizeof(message
), msg
, ap
);
69 error_setg(&ctxt
->err
, "JSON parse error, %s", message
);
72 static int cvt4hex(const char *s
)
77 for (i
= 0; i
< 4; i
++) {
78 if (!qemu_isxdigit(s
[i
])) {
82 if (s
[i
] >= '0' && s
[i
] <= '9') {
84 } else if (s
[i
] >= 'a' && s
[i
] <= 'f') {
85 cp
|= 10 + s
[i
] - 'a';
86 } else if (s
[i
] >= 'A' && s
[i
] <= 'F') {
87 cp
|= 10 + s
[i
] - 'A';
96 * parse_string(): Parse a JSON string
98 * From RFC 8259 "The JavaScript Object Notation (JSON) Data
99 * Interchange Format":
103 * %x22 / ; " quotation mark U+0022
104 * %x5C / ; \ reverse solidus U+005C
105 * %x2F / ; / solidus U+002F
106 * %x62 / ; b backspace U+0008
107 * %x66 / ; f form feed U+000C
108 * %x6E / ; n line feed U+000A
109 * %x72 / ; r carriage return U+000D
110 * %x74 / ; t tab U+0009
111 * %x75 4HEXDIG ) ; uXXXX U+XXXX
113 * quotation-mark = %x22 ; "
114 * unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
116 * Extensions over RFC 8259:
117 * - Extra escape sequence in strings:
118 * 0x27 (apostrophe) is recognized after escape, too
119 * - Single-quoted strings:
120 * Like double-quoted strings, except they're delimited by %x27
121 * (apostrophe) instead of %x22 (quotation mark), and can't contain
122 * unescaped apostrophe, but can contain unescaped quotation mark.
125 * - Encoding is modified UTF-8.
126 * - Invalid Unicode characters are rejected.
127 * - Control characters \x00..\x1F are rejected by the lexer.
129 static QString
*parse_string(JSONParserContext
*ctxt
, JSONToken
*token
)
131 const char *ptr
= token
->str
;
140 assert(*ptr
== '"' || *ptr
== '\'');
144 while (*ptr
!= quote
) {
150 qstring_append_chr(str
, '"');
153 qstring_append_chr(str
, '\'');
156 qstring_append_chr(str
, '\\');
159 qstring_append_chr(str
, '/');
162 qstring_append_chr(str
, '\b');
165 qstring_append_chr(str
, '\f');
168 qstring_append_chr(str
, '\n');
171 qstring_append_chr(str
, '\r');
174 qstring_append_chr(str
, '\t');
180 /* handle surrogate pairs */
181 if (cp
>= 0xD800 && cp
<= 0xDBFF
182 && ptr
[0] == '\\' && ptr
[1] == 'u') {
183 /* leading surrogate followed by \u */
184 cp
= 0x10000 + ((cp
& 0x3FF) << 10);
185 trailing
= cvt4hex(ptr
+ 2);
186 if (trailing
>= 0xDC00 && trailing
<= 0xDFFF) {
187 /* followed by trailing surrogate */
188 cp
|= trailing
& 0x3FF;
191 cp
= -1; /* invalid */
195 if (mod_utf8_encode(utf8_buf
, sizeof(utf8_buf
), cp
) < 0) {
196 parse_error(ctxt
, token
,
197 "%.*s is not a valid Unicode character",
198 (int)(ptr
- beg
), beg
);
201 qstring_append(str
, utf8_buf
);
204 parse_error(ctxt
, token
, "invalid escape sequence in string");
208 cp
= mod_utf8_codepoint(ptr
, 6, &end
);
210 parse_error(ctxt
, token
, "invalid UTF-8 sequence in string");
214 len
= mod_utf8_encode(utf8_buf
, sizeof(utf8_buf
), cp
);
216 qstring_append(str
, utf8_buf
);
227 /* Note: the token object returned by parser_context_peek_token or
228 * parser_context_pop_token is deleted as soon as parser_context_pop_token
231 static JSONToken
*parser_context_pop_token(JSONParserContext
*ctxt
)
233 g_free(ctxt
->current
);
234 ctxt
->current
= g_queue_pop_head(ctxt
->buf
);
235 return ctxt
->current
;
238 static JSONToken
*parser_context_peek_token(JSONParserContext
*ctxt
)
240 return g_queue_peek_head(ctxt
->buf
);
246 static int parse_pair(JSONParserContext
*ctxt
, QDict
*dict
, va_list *ap
)
250 JSONToken
*peek
, *token
;
252 peek
= parser_context_peek_token(ctxt
);
254 parse_error(ctxt
, NULL
, "premature EOI");
258 key
= qobject_to(QString
, parse_value(ctxt
, ap
));
260 parse_error(ctxt
, peek
, "key is not a string in object");
264 token
= parser_context_pop_token(ctxt
);
266 parse_error(ctxt
, NULL
, "premature EOI");
270 if (token
->type
!= JSON_COLON
) {
271 parse_error(ctxt
, token
, "missing : in object pair");
275 value
= parse_value(ctxt
, ap
);
277 parse_error(ctxt
, token
, "Missing value in dict");
281 qdict_put_obj(dict
, qstring_get_str(key
), value
);
293 static QObject
*parse_object(JSONParserContext
*ctxt
, va_list *ap
)
296 JSONToken
*token
, *peek
;
298 token
= parser_context_pop_token(ctxt
);
299 assert(token
&& token
->type
== JSON_LCURLY
);
303 peek
= parser_context_peek_token(ctxt
);
305 parse_error(ctxt
, NULL
, "premature EOI");
309 if (peek
->type
!= JSON_RCURLY
) {
310 if (parse_pair(ctxt
, dict
, ap
) == -1) {
314 token
= parser_context_pop_token(ctxt
);
316 parse_error(ctxt
, NULL
, "premature EOI");
320 while (token
->type
!= JSON_RCURLY
) {
321 if (token
->type
!= JSON_COMMA
) {
322 parse_error(ctxt
, token
, "expected separator in dict");
326 if (parse_pair(ctxt
, dict
, ap
) == -1) {
330 token
= parser_context_pop_token(ctxt
);
332 parse_error(ctxt
, NULL
, "premature EOI");
337 (void)parser_context_pop_token(ctxt
);
340 return QOBJECT(dict
);
347 static QObject
*parse_array(JSONParserContext
*ctxt
, va_list *ap
)
350 JSONToken
*token
, *peek
;
352 token
= parser_context_pop_token(ctxt
);
353 assert(token
&& token
->type
== JSON_LSQUARE
);
357 peek
= parser_context_peek_token(ctxt
);
359 parse_error(ctxt
, NULL
, "premature EOI");
363 if (peek
->type
!= JSON_RSQUARE
) {
366 obj
= parse_value(ctxt
, ap
);
368 parse_error(ctxt
, token
, "expecting value");
372 qlist_append_obj(list
, obj
);
374 token
= parser_context_pop_token(ctxt
);
376 parse_error(ctxt
, NULL
, "premature EOI");
380 while (token
->type
!= JSON_RSQUARE
) {
381 if (token
->type
!= JSON_COMMA
) {
382 parse_error(ctxt
, token
, "expected separator in list");
386 obj
= parse_value(ctxt
, ap
);
388 parse_error(ctxt
, token
, "expecting value");
392 qlist_append_obj(list
, obj
);
394 token
= parser_context_pop_token(ctxt
);
396 parse_error(ctxt
, NULL
, "premature EOI");
401 (void)parser_context_pop_token(ctxt
);
404 return QOBJECT(list
);
411 static QObject
*parse_keyword(JSONParserContext
*ctxt
)
415 token
= parser_context_pop_token(ctxt
);
416 assert(token
&& token
->type
== JSON_KEYWORD
);
418 if (!strcmp(token
->str
, "true")) {
419 return QOBJECT(qbool_from_bool(true));
420 } else if (!strcmp(token
->str
, "false")) {
421 return QOBJECT(qbool_from_bool(false));
422 } else if (!strcmp(token
->str
, "null")) {
423 return QOBJECT(qnull());
425 parse_error(ctxt
, token
, "invalid keyword '%s'", token
->str
);
429 static QObject
*parse_interpolation(JSONParserContext
*ctxt
, va_list *ap
)
433 token
= parser_context_pop_token(ctxt
);
434 assert(token
&& token
->type
== JSON_INTERP
);
436 if (!strcmp(token
->str
, "%p")) {
437 return va_arg(*ap
, QObject
*);
438 } else if (!strcmp(token
->str
, "%i")) {
439 return QOBJECT(qbool_from_bool(va_arg(*ap
, int)));
440 } else if (!strcmp(token
->str
, "%d")) {
441 return QOBJECT(qnum_from_int(va_arg(*ap
, int)));
442 } else if (!strcmp(token
->str
, "%ld")) {
443 return QOBJECT(qnum_from_int(va_arg(*ap
, long)));
444 } else if (!strcmp(token
->str
, "%lld")) {
445 return QOBJECT(qnum_from_int(va_arg(*ap
, long long)));
446 } else if (!strcmp(token
->str
, "%" PRId64
)) {
447 return QOBJECT(qnum_from_int(va_arg(*ap
, int64_t)));
448 } else if (!strcmp(token
->str
, "%u")) {
449 return QOBJECT(qnum_from_uint(va_arg(*ap
, unsigned int)));
450 } else if (!strcmp(token
->str
, "%lu")) {
451 return QOBJECT(qnum_from_uint(va_arg(*ap
, unsigned long)));
452 } else if (!strcmp(token
->str
, "%llu")) {
453 return QOBJECT(qnum_from_uint(va_arg(*ap
, unsigned long long)));
454 } else if (!strcmp(token
->str
, "%" PRIu64
)) {
455 return QOBJECT(qnum_from_uint(va_arg(*ap
, uint64_t)));
456 } else if (!strcmp(token
->str
, "%s")) {
457 return QOBJECT(qstring_from_str(va_arg(*ap
, const char *)));
458 } else if (!strcmp(token
->str
, "%f")) {
459 return QOBJECT(qnum_from_double(va_arg(*ap
, double)));
461 parse_error(ctxt
, token
, "invalid interpolation '%s'", token
->str
);
465 static QObject
*parse_literal(JSONParserContext
*ctxt
)
469 token
= parser_context_pop_token(ctxt
);
472 switch (token
->type
) {
474 return QOBJECT(parse_string(ctxt
, token
));
477 * Represent JSON_INTEGER as QNUM_I64 if possible, else as
478 * QNUM_U64, else as QNUM_DOUBLE. Note that qemu_strtoi64()
479 * and qemu_strtou64() fail with ERANGE when it's not
482 * qnum_get_int() will then work for any signed 64-bit
483 * JSON_INTEGER, qnum_get_uint() for any unsigned 64-bit
484 * integer, and qnum_get_double() both for any JSON_INTEGER
485 * and any JSON_FLOAT (with precision loss for integers beyond
492 ret
= qemu_strtoi64(token
->str
, NULL
, 10, &value
);
494 return QOBJECT(qnum_from_int(value
));
496 assert(ret
== -ERANGE
);
498 if (token
->str
[0] != '-') {
499 ret
= qemu_strtou64(token
->str
, NULL
, 10, &uvalue
);
501 return QOBJECT(qnum_from_uint(uvalue
));
503 assert(ret
== -ERANGE
);
505 /* fall through to JSON_FLOAT */
508 /* FIXME dependent on locale; a pervasive issue in QEMU */
509 /* FIXME our lexer matches RFC 7159 in forbidding Inf or NaN,
510 * but those might be useful extensions beyond JSON */
511 return QOBJECT(qnum_from_double(strtod(token
->str
, NULL
)));
517 static QObject
*parse_value(JSONParserContext
*ctxt
, va_list *ap
)
521 token
= parser_context_peek_token(ctxt
);
523 parse_error(ctxt
, NULL
, "premature EOI");
527 switch (token
->type
) {
529 return parse_object(ctxt
, ap
);
531 return parse_array(ctxt
, ap
);
533 return parse_interpolation(ctxt
, ap
);
537 return parse_literal(ctxt
);
539 return parse_keyword(ctxt
);
541 parse_error(ctxt
, token
, "expecting value");
546 JSONToken
*json_token(JSONTokenType type
, int x
, int y
, GString
*tokstr
)
548 JSONToken
*token
= g_malloc(sizeof(JSONToken
) + tokstr
->len
+ 1);
551 memcpy(token
->str
, tokstr
->str
, tokstr
->len
);
552 token
->str
[tokstr
->len
] = 0;
558 QObject
*json_parser_parse(GQueue
*tokens
, va_list *ap
, Error
**errp
)
560 JSONParserContext ctxt
= { .buf
= tokens
};
563 result
= parse_value(&ctxt
, ap
);
564 assert(ctxt
.err
|| g_queue_is_empty(ctxt
.buf
));
566 error_propagate(errp
, ctxt
.err
);
568 while (!g_queue_is_empty(ctxt
.buf
)) {
569 parser_context_pop_token(&ctxt
);
571 g_free(ctxt
.current
);