]>
git.proxmox.com Git - mirror_qemu.git/blob - qobject/json-parser.c
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 "qapi/qmp/json-parser.h"
26 #include "qapi/qmp/json-lexer.h"
27 #include "qapi/qmp/json-streamer.h"
29 typedef struct JSONParserContext
36 #define BUG_ON(cond) assert(!(cond))
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
47 static QObject
*parse_value(JSONParserContext
*ctxt
, va_list *ap
);
52 static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext
*ctxt
,
53 JSONToken
*token
, const char *msg
, ...)
62 vsnprintf(message
, sizeof(message
), msg
, ap
);
64 error_setg(&ctxt
->err
, "JSON parse error, %s", message
);
70 * These helpers are used to unescape strings.
72 static void wchar_to_utf8(uint16_t wchar
, char *buffer
, size_t buffer_length
)
74 if (wchar
<= 0x007F) {
75 BUG_ON(buffer_length
< 2);
77 buffer
[0] = wchar
& 0x7F;
79 } else if (wchar
<= 0x07FF) {
80 BUG_ON(buffer_length
< 3);
82 buffer
[0] = 0xC0 | ((wchar
>> 6) & 0x1F);
83 buffer
[1] = 0x80 | (wchar
& 0x3F);
86 BUG_ON(buffer_length
< 4);
88 buffer
[0] = 0xE0 | ((wchar
>> 12) & 0x0F);
89 buffer
[1] = 0x80 | ((wchar
>> 6) & 0x3F);
90 buffer
[2] = 0x80 | (wchar
& 0x3F);
95 static int hex2decimal(char ch
)
97 if (ch
>= '0' && ch
<= '9') {
99 } else if (ch
>= 'a' && ch
<= 'f') {
100 return 10 + (ch
- 'a');
101 } else if (ch
>= 'A' && ch
<= 'F') {
102 return 10 + (ch
- 'A');
108 * parse_string(): Parse a JSON string
110 * From RFC 8259 "The JavaScript Object Notation (JSON) Data
111 * Interchange Format":
115 * %x22 / ; " quotation mark U+0022
116 * %x5C / ; \ reverse solidus U+005C
117 * %x2F / ; / solidus U+002F
118 * %x62 / ; b backspace U+0008
119 * %x66 / ; f form feed U+000C
120 * %x6E / ; n line feed U+000A
121 * %x72 / ; r carriage return U+000D
122 * %x74 / ; t tab U+0009
123 * %x75 4HEXDIG ) ; uXXXX U+XXXX
125 * quotation-mark = %x22 ; "
126 * unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
128 * Extensions over RFC 8259:
129 * - Extra escape sequence in strings:
130 * 0x27 (apostrophe) is recognized after escape, too
131 * - Single-quoted strings:
132 * Like double-quoted strings, except they're delimited by %x27
133 * (apostrophe) instead of %x22 (quotation mark), and can't contain
134 * unescaped apostrophe, but can contain unescaped quotation mark.
137 * - Encoding is modified UTF-8.
138 * - Invalid Unicode characters are rejected.
139 * - Control characters \x00..\x1F are rejected by the lexer.
141 static QString
*parse_string(JSONParserContext
*ctxt
, JSONToken
*token
)
143 const char *ptr
= token
->str
;
151 assert(*ptr
== '"' || *ptr
== '\'');
155 while (*ptr
!= quote
) {
161 qstring_append_chr(str
, '"');
164 qstring_append_chr(str
, '\'');
167 qstring_append_chr(str
, '\\');
170 qstring_append_chr(str
, '/');
173 qstring_append_chr(str
, '\b');
176 qstring_append_chr(str
, '\f');
179 qstring_append_chr(str
, '\n');
182 qstring_append_chr(str
, '\r');
185 qstring_append_chr(str
, '\t');
189 for (i
= 0; i
< 4; i
++) {
190 if (!qemu_isxdigit(*ptr
)) {
191 parse_error(ctxt
, token
,
192 "invalid hex escape sequence in string");
196 cp
|= hex2decimal(*ptr
);
200 wchar_to_utf8(cp
, utf8_buf
, sizeof(utf8_buf
));
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 assert(!g_queue_is_empty(ctxt
->buf
));
235 ctxt
->current
= g_queue_pop_head(ctxt
->buf
);
236 return ctxt
->current
;
239 static JSONToken
*parser_context_peek_token(JSONParserContext
*ctxt
)
241 assert(!g_queue_is_empty(ctxt
->buf
));
242 return g_queue_peek_head(ctxt
->buf
);
245 static JSONParserContext
*parser_context_new(GQueue
*tokens
)
247 JSONParserContext
*ctxt
;
253 ctxt
= g_malloc0(sizeof(JSONParserContext
));
259 /* to support error propagation, ctxt->err must be freed separately */
260 static void parser_context_free(JSONParserContext
*ctxt
)
263 while (!g_queue_is_empty(ctxt
->buf
)) {
264 parser_context_pop_token(ctxt
);
266 g_free(ctxt
->current
);
267 g_queue_free(ctxt
->buf
);
275 static int parse_pair(JSONParserContext
*ctxt
, QDict
*dict
, va_list *ap
)
279 JSONToken
*peek
, *token
;
281 peek
= parser_context_peek_token(ctxt
);
283 parse_error(ctxt
, NULL
, "premature EOI");
287 key
= qobject_to(QString
, parse_value(ctxt
, ap
));
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(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")) {
452 return QOBJECT(qnull());
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(qnum_from_int(va_arg(*ap
, int)));
475 } else if (!strcmp(token
->str
, "%ld")) {
476 return QOBJECT(qnum_from_int(va_arg(*ap
, long)));
477 } else if (!strcmp(token
->str
, "%lld") ||
478 !strcmp(token
->str
, "%I64d")) {
479 return QOBJECT(qnum_from_int(va_arg(*ap
, long long)));
480 } else if (!strcmp(token
->str
, "%u")) {
481 return QOBJECT(qnum_from_uint(va_arg(*ap
, unsigned int)));
482 } else if (!strcmp(token
->str
, "%lu")) {
483 return QOBJECT(qnum_from_uint(va_arg(*ap
, unsigned long)));
484 } else if (!strcmp(token
->str
, "%llu") ||
485 !strcmp(token
->str
, "%I64u")) {
486 return QOBJECT(qnum_from_uint(va_arg(*ap
, unsigned long long)));
487 } else if (!strcmp(token
->str
, "%s")) {
488 return QOBJECT(qstring_from_str(va_arg(*ap
, const char *)));
489 } else if (!strcmp(token
->str
, "%f")) {
490 return QOBJECT(qnum_from_double(va_arg(*ap
, double)));
495 static QObject
*parse_literal(JSONParserContext
*ctxt
)
499 token
= parser_context_pop_token(ctxt
);
502 switch (token
->type
) {
504 return QOBJECT(parse_string(ctxt
, token
));
507 * Represent JSON_INTEGER as QNUM_I64 if possible, else as
508 * QNUM_U64, else as QNUM_DOUBLE. Note that qemu_strtoi64()
509 * and qemu_strtou64() fail with ERANGE when it's not
512 * qnum_get_int() will then work for any signed 64-bit
513 * JSON_INTEGER, qnum_get_uint() for any unsigned 64-bit
514 * integer, and qnum_get_double() both for any JSON_INTEGER
515 * and any JSON_FLOAT (with precision loss for integers beyond
522 ret
= qemu_strtoi64(token
->str
, NULL
, 10, &value
);
524 return QOBJECT(qnum_from_int(value
));
526 assert(ret
== -ERANGE
);
528 if (token
->str
[0] != '-') {
529 ret
= qemu_strtou64(token
->str
, NULL
, 10, &uvalue
);
531 return QOBJECT(qnum_from_uint(uvalue
));
533 assert(ret
== -ERANGE
);
535 /* fall through to JSON_FLOAT */
538 /* FIXME dependent on locale; a pervasive issue in QEMU */
539 /* FIXME our lexer matches RFC 7159 in forbidding Inf or NaN,
540 * but those might be useful extensions beyond JSON */
541 return QOBJECT(qnum_from_double(strtod(token
->str
, NULL
)));
547 static QObject
*parse_value(JSONParserContext
*ctxt
, va_list *ap
)
551 token
= parser_context_peek_token(ctxt
);
553 parse_error(ctxt
, NULL
, "premature EOI");
557 switch (token
->type
) {
559 return parse_object(ctxt
, ap
);
561 return parse_array(ctxt
, ap
);
563 return parse_escape(ctxt
, ap
);
567 return parse_literal(ctxt
);
569 return parse_keyword(ctxt
);
571 parse_error(ctxt
, token
, "expecting value");
576 QObject
*json_parser_parse(GQueue
*tokens
, va_list *ap
)
578 return json_parser_parse_err(tokens
, ap
, NULL
);
581 QObject
*json_parser_parse_err(GQueue
*tokens
, va_list *ap
, Error
**errp
)
583 JSONParserContext
*ctxt
= parser_context_new(tokens
);
590 result
= parse_value(ctxt
, ap
);
592 error_propagate(errp
, ctxt
->err
);
594 parser_context_free(ctxt
);