2009-11-11 17:39:23 +01:00
|
|
|
/*
|
2016-01-29 14:48:37 +01:00
|
|
|
* JSON Parser
|
2009-11-11 17:39:23 +01:00
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2009
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
|
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-29 18:50:01 +01:00
|
|
|
#include "qemu/osdep.h"
|
2017-06-07 18:36:02 +02:00
|
|
|
#include "qemu/cutils.h"
|
2018-08-23 18:39:49 +02:00
|
|
|
#include "qemu/unicode.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 09:01:28 +01:00
|
|
|
#include "qapi/error.h"
|
2009-11-11 17:39:23 +01:00
|
|
|
#include "qemu-common.h"
|
2018-02-01 12:18:35 +01:00
|
|
|
#include "qapi/qmp/qbool.h"
|
2018-02-01 12:18:39 +01:00
|
|
|
#include "qapi/qmp/qdict.h"
|
2018-02-01 12:18:38 +01:00
|
|
|
#include "qapi/qmp/qlist.h"
|
2018-02-01 12:18:36 +01:00
|
|
|
#include "qapi/qmp/qnull.h"
|
|
|
|
#include "qapi/qmp/qnum.h"
|
2018-02-01 12:18:35 +01:00
|
|
|
#include "qapi/qmp/qstring.h"
|
2018-08-23 18:40:20 +02:00
|
|
|
#include "json-parser-int.h"
|
2009-11-11 17:39:23 +01:00
|
|
|
|
2018-08-23 18:40:18 +02:00
|
|
|
struct JSONToken {
|
|
|
|
JSONTokenType type;
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
char str[];
|
|
|
|
};
|
|
|
|
|
2009-11-11 17:39:23 +01:00
|
|
|
typedef struct JSONParserContext
|
|
|
|
{
|
2011-06-01 19:14:50 +02:00
|
|
|
Error *err;
|
2015-11-25 22:23:32 +01:00
|
|
|
JSONToken *current;
|
2015-11-25 22:23:31 +01:00
|
|
|
GQueue *buf;
|
2018-08-23 18:40:22 +02:00
|
|
|
va_list *ap;
|
2009-11-11 17:39:23 +01:00
|
|
|
} JSONParserContext;
|
|
|
|
|
|
|
|
#define BUG_ON(cond) assert(!(cond))
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO
|
|
|
|
*
|
|
|
|
* 0) make errors meaningful again
|
|
|
|
* 1) add geometry information to tokens
|
|
|
|
* 3) should we return a parsed size?
|
|
|
|
* 4) deal with premature EOI
|
|
|
|
*/
|
|
|
|
|
2018-08-23 18:40:22 +02:00
|
|
|
static QObject *parse_value(JSONParserContext *ctxt);
|
2009-11-11 17:39:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Error handler
|
|
|
|
*/
|
2010-09-23 21:28:05 +02:00
|
|
|
static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext *ctxt,
|
2015-11-25 22:23:32 +01:00
|
|
|
JSONToken *token, const char *msg, ...)
|
2009-11-11 17:39:23 +01:00
|
|
|
{
|
2010-03-24 16:12:05 +01:00
|
|
|
va_list ap;
|
2011-06-01 19:14:50 +02:00
|
|
|
char message[1024];
|
2018-08-23 18:39:50 +02:00
|
|
|
|
|
|
|
if (ctxt->err) {
|
|
|
|
return;
|
|
|
|
}
|
2010-03-24 16:12:05 +01:00
|
|
|
va_start(ap, msg);
|
2011-06-01 19:14:50 +02:00
|
|
|
vsnprintf(message, sizeof(message), msg, ap);
|
2010-03-24 16:12:05 +01:00
|
|
|
va_end(ap);
|
2014-03-22 00:42:26 +01:00
|
|
|
error_setg(&ctxt->err, "JSON parse error, %s", message);
|
2009-11-11 17:39:23 +01:00
|
|
|
}
|
|
|
|
|
2018-08-23 18:39:56 +02:00
|
|
|
static int cvt4hex(const char *s)
|
2009-11-11 17:39:23 +01:00
|
|
|
{
|
2018-08-23 18:39:56 +02:00
|
|
|
int cp, i;
|
|
|
|
|
|
|
|
cp = 0;
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
if (!qemu_isxdigit(s[i])) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cp <<= 4;
|
|
|
|
if (s[i] >= '0' && s[i] <= '9') {
|
|
|
|
cp |= s[i] - '0';
|
|
|
|
} else if (s[i] >= 'a' && s[i] <= 'f') {
|
|
|
|
cp |= 10 + s[i] - 'a';
|
|
|
|
} else if (s[i] >= 'A' && s[i] <= 'F') {
|
|
|
|
cp |= 10 + s[i] - 'A';
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
2009-11-11 17:39:23 +01:00
|
|
|
}
|
2018-08-23 18:39:56 +02:00
|
|
|
return cp;
|
2009-11-11 17:39:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-23 18:39:53 +02:00
|
|
|
* parse_string(): Parse a JSON string
|
2009-11-11 17:39:23 +01:00
|
|
|
*
|
2018-08-23 18:39:53 +02:00
|
|
|
* From RFC 8259 "The JavaScript Object Notation (JSON) Data
|
|
|
|
* Interchange Format":
|
|
|
|
*
|
|
|
|
* char = unescaped /
|
|
|
|
* escape (
|
|
|
|
* %x22 / ; " quotation mark U+0022
|
|
|
|
* %x5C / ; \ reverse solidus U+005C
|
|
|
|
* %x2F / ; / solidus U+002F
|
|
|
|
* %x62 / ; b backspace U+0008
|
|
|
|
* %x66 / ; f form feed U+000C
|
|
|
|
* %x6E / ; n line feed U+000A
|
|
|
|
* %x72 / ; r carriage return U+000D
|
|
|
|
* %x74 / ; t tab U+0009
|
|
|
|
* %x75 4HEXDIG ) ; uXXXX U+XXXX
|
|
|
|
* escape = %x5C ; \
|
|
|
|
* quotation-mark = %x22 ; "
|
|
|
|
* unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
|
|
|
|
*
|
|
|
|
* Extensions over RFC 8259:
|
|
|
|
* - Extra escape sequence in strings:
|
|
|
|
* 0x27 (apostrophe) is recognized after escape, too
|
|
|
|
* - Single-quoted strings:
|
|
|
|
* Like double-quoted strings, except they're delimited by %x27
|
|
|
|
* (apostrophe) instead of %x22 (quotation mark), and can't contain
|
|
|
|
* unescaped apostrophe, but can contain unescaped quotation mark.
|
|
|
|
*
|
|
|
|
* Note:
|
|
|
|
* - Encoding is modified UTF-8.
|
|
|
|
* - Invalid Unicode characters are rejected.
|
|
|
|
* - Control characters \x00..\x1F are rejected by the lexer.
|
2009-11-11 17:39:23 +01:00
|
|
|
*/
|
2018-08-23 18:39:53 +02:00
|
|
|
static QString *parse_string(JSONParserContext *ctxt, JSONToken *token)
|
2009-11-11 17:39:23 +01:00
|
|
|
{
|
2015-11-25 22:23:32 +01:00
|
|
|
const char *ptr = token->str;
|
2009-11-11 17:39:23 +01:00
|
|
|
QString *str;
|
2018-08-23 18:39:47 +02:00
|
|
|
char quote;
|
2018-08-23 18:39:56 +02:00
|
|
|
const char *beg;
|
|
|
|
int cp, trailing;
|
2018-08-23 18:39:49 +02:00
|
|
|
char *end;
|
|
|
|
ssize_t len;
|
|
|
|
char utf8_buf[5];
|
2009-11-11 17:39:23 +01:00
|
|
|
|
2018-08-23 18:39:47 +02:00
|
|
|
assert(*ptr == '"' || *ptr == '\'');
|
|
|
|
quote = *ptr++;
|
2009-11-11 17:39:23 +01:00
|
|
|
str = qstring_new();
|
2018-08-23 18:39:47 +02:00
|
|
|
|
|
|
|
while (*ptr != quote) {
|
|
|
|
assert(*ptr);
|
json: Improve safety of qobject_from_jsonf_nofail() & friends
The JSON parser optionally supports interpolation. This is used to
build QObjects by parsing string templates. The templates are C
literals, so parse errors (such as invalid interpolation
specifications) are actually programming errors. Consequently, the
functions providing parsing with interpolation
(qobject_from_jsonf_nofail(), qobject_from_vjsonf_nofail(),
qdict_from_jsonf_nofail(), qdict_from_vjsonf_nofail()) pass
&error_abort to the parser.
However, there's another, more dangerous kind of programming error:
since we use va_arg() to get the value to interpolate, behavior is
undefined when the variable argument isn't consistent with the
interpolation specification.
The same problem exists with printf()-like functions, and the solution
is to have the compiler check consistency. This is what
GCC_FMT_ATTR() is about.
To enable this type checking for interpolation as well, we carefully
chose our interpolation specifications to match printf conversion
specifications, and decorate functions parsing templates with
GCC_FMT_ATTR().
Note that this only protects against undefined behavior due to type
errors. It can't protect against use of invalid interpolation
specifications that happen to be valid printf conversion
specifications.
However, there's still a gaping hole in the type checking: GCC
recognizes '%' as start of printf conversion specification anywhere in
the template, but the parser recognizes it only outside JSON strings.
For instance, if someone were to pass a "{ '%s': %d }" template, GCC
would require a char * and an int argument, but the parser would
va_arg() only an int argument, resulting in undefined behavior.
Avoid undefined behavior by catching the programming error at run
time: have the parser recognize and reject '%' in JSON strings.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20180823164025.12553-57-armbru@redhat.com>
2018-08-23 18:40:23 +02:00
|
|
|
switch (*ptr) {
|
|
|
|
case '\\':
|
2018-08-23 18:39:56 +02:00
|
|
|
beg = ptr++;
|
2018-08-23 18:39:47 +02:00
|
|
|
switch (*ptr++) {
|
2009-11-11 17:39:23 +01:00
|
|
|
case '"':
|
2018-08-23 18:39:54 +02:00
|
|
|
qstring_append_chr(str, '"');
|
2009-11-11 17:39:23 +01:00
|
|
|
break;
|
|
|
|
case '\'':
|
2018-08-23 18:39:54 +02:00
|
|
|
qstring_append_chr(str, '\'');
|
2009-11-11 17:39:23 +01:00
|
|
|
break;
|
|
|
|
case '\\':
|
2018-08-23 18:39:54 +02:00
|
|
|
qstring_append_chr(str, '\\');
|
2009-11-11 17:39:23 +01:00
|
|
|
break;
|
|
|
|
case '/':
|
2018-08-23 18:39:54 +02:00
|
|
|
qstring_append_chr(str, '/');
|
2009-11-11 17:39:23 +01:00
|
|
|
break;
|
|
|
|
case 'b':
|
2018-08-23 18:39:54 +02:00
|
|
|
qstring_append_chr(str, '\b');
|
2009-11-11 17:39:23 +01:00
|
|
|
break;
|
2010-05-19 22:06:15 +02:00
|
|
|
case 'f':
|
2018-08-23 18:39:54 +02:00
|
|
|
qstring_append_chr(str, '\f');
|
2010-05-19 22:06:15 +02:00
|
|
|
break;
|
2009-11-11 17:39:23 +01:00
|
|
|
case 'n':
|
2018-08-23 18:39:54 +02:00
|
|
|
qstring_append_chr(str, '\n');
|
2009-11-11 17:39:23 +01:00
|
|
|
break;
|
|
|
|
case 'r':
|
2018-08-23 18:39:54 +02:00
|
|
|
qstring_append_chr(str, '\r');
|
2009-11-11 17:39:23 +01:00
|
|
|
break;
|
|
|
|
case 't':
|
2018-08-23 18:39:54 +02:00
|
|
|
qstring_append_chr(str, '\t');
|
2009-11-11 17:39:23 +01:00
|
|
|
break;
|
2018-08-23 18:39:54 +02:00
|
|
|
case 'u':
|
2018-08-23 18:39:56 +02:00
|
|
|
cp = cvt4hex(ptr);
|
|
|
|
ptr += 4;
|
|
|
|
|
|
|
|
/* handle surrogate pairs */
|
|
|
|
if (cp >= 0xD800 && cp <= 0xDBFF
|
|
|
|
&& ptr[0] == '\\' && ptr[1] == 'u') {
|
|
|
|
/* leading surrogate followed by \u */
|
|
|
|
cp = 0x10000 + ((cp & 0x3FF) << 10);
|
|
|
|
trailing = cvt4hex(ptr + 2);
|
|
|
|
if (trailing >= 0xDC00 && trailing <= 0xDFFF) {
|
|
|
|
/* followed by trailing surrogate */
|
|
|
|
cp |= trailing & 0x3FF;
|
|
|
|
ptr += 6;
|
|
|
|
} else {
|
|
|
|
cp = -1; /* invalid */
|
2009-11-11 17:39:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 18:39:55 +02:00
|
|
|
if (mod_utf8_encode(utf8_buf, sizeof(utf8_buf), cp) < 0) {
|
|
|
|
parse_error(ctxt, token,
|
2018-08-23 18:39:56 +02:00
|
|
|
"%.*s is not a valid Unicode character",
|
|
|
|
(int)(ptr - beg), beg);
|
2018-08-23 18:39:55 +02:00
|
|
|
goto out;
|
|
|
|
}
|
2018-08-23 18:39:54 +02:00
|
|
|
qstring_append(str, utf8_buf);
|
|
|
|
break;
|
2009-11-11 17:39:23 +01:00
|
|
|
default:
|
|
|
|
parse_error(ctxt, token, "invalid escape sequence in string");
|
|
|
|
goto out;
|
|
|
|
}
|
json: Improve safety of qobject_from_jsonf_nofail() & friends
The JSON parser optionally supports interpolation. This is used to
build QObjects by parsing string templates. The templates are C
literals, so parse errors (such as invalid interpolation
specifications) are actually programming errors. Consequently, the
functions providing parsing with interpolation
(qobject_from_jsonf_nofail(), qobject_from_vjsonf_nofail(),
qdict_from_jsonf_nofail(), qdict_from_vjsonf_nofail()) pass
&error_abort to the parser.
However, there's another, more dangerous kind of programming error:
since we use va_arg() to get the value to interpolate, behavior is
undefined when the variable argument isn't consistent with the
interpolation specification.
The same problem exists with printf()-like functions, and the solution
is to have the compiler check consistency. This is what
GCC_FMT_ATTR() is about.
To enable this type checking for interpolation as well, we carefully
chose our interpolation specifications to match printf conversion
specifications, and decorate functions parsing templates with
GCC_FMT_ATTR().
Note that this only protects against undefined behavior due to type
errors. It can't protect against use of invalid interpolation
specifications that happen to be valid printf conversion
specifications.
However, there's still a gaping hole in the type checking: GCC
recognizes '%' as start of printf conversion specification anywhere in
the template, but the parser recognizes it only outside JSON strings.
For instance, if someone were to pass a "{ '%s': %d }" template, GCC
would require a char * and an int argument, but the parser would
va_arg() only an int argument, resulting in undefined behavior.
Avoid undefined behavior by catching the programming error at run
time: have the parser recognize and reject '%' in JSON strings.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20180823164025.12553-57-armbru@redhat.com>
2018-08-23 18:40:23 +02:00
|
|
|
break;
|
|
|
|
case '%':
|
2018-08-23 18:40:24 +02:00
|
|
|
if (ctxt->ap && ptr[1] != '%') {
|
json: Improve safety of qobject_from_jsonf_nofail() & friends
The JSON parser optionally supports interpolation. This is used to
build QObjects by parsing string templates. The templates are C
literals, so parse errors (such as invalid interpolation
specifications) are actually programming errors. Consequently, the
functions providing parsing with interpolation
(qobject_from_jsonf_nofail(), qobject_from_vjsonf_nofail(),
qdict_from_jsonf_nofail(), qdict_from_vjsonf_nofail()) pass
&error_abort to the parser.
However, there's another, more dangerous kind of programming error:
since we use va_arg() to get the value to interpolate, behavior is
undefined when the variable argument isn't consistent with the
interpolation specification.
The same problem exists with printf()-like functions, and the solution
is to have the compiler check consistency. This is what
GCC_FMT_ATTR() is about.
To enable this type checking for interpolation as well, we carefully
chose our interpolation specifications to match printf conversion
specifications, and decorate functions parsing templates with
GCC_FMT_ATTR().
Note that this only protects against undefined behavior due to type
errors. It can't protect against use of invalid interpolation
specifications that happen to be valid printf conversion
specifications.
However, there's still a gaping hole in the type checking: GCC
recognizes '%' as start of printf conversion specification anywhere in
the template, but the parser recognizes it only outside JSON strings.
For instance, if someone were to pass a "{ '%s': %d }" template, GCC
would require a char * and an int argument, but the parser would
va_arg() only an int argument, resulting in undefined behavior.
Avoid undefined behavior by catching the programming error at run
time: have the parser recognize and reject '%' in JSON strings.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20180823164025.12553-57-armbru@redhat.com>
2018-08-23 18:40:23 +02:00
|
|
|
parse_error(ctxt, token, "can't interpolate into string");
|
|
|
|
goto out;
|
|
|
|
}
|
2018-08-23 18:40:24 +02:00
|
|
|
ptr++;
|
json: Improve safety of qobject_from_jsonf_nofail() & friends
The JSON parser optionally supports interpolation. This is used to
build QObjects by parsing string templates. The templates are C
literals, so parse errors (such as invalid interpolation
specifications) are actually programming errors. Consequently, the
functions providing parsing with interpolation
(qobject_from_jsonf_nofail(), qobject_from_vjsonf_nofail(),
qdict_from_jsonf_nofail(), qdict_from_vjsonf_nofail()) pass
&error_abort to the parser.
However, there's another, more dangerous kind of programming error:
since we use va_arg() to get the value to interpolate, behavior is
undefined when the variable argument isn't consistent with the
interpolation specification.
The same problem exists with printf()-like functions, and the solution
is to have the compiler check consistency. This is what
GCC_FMT_ATTR() is about.
To enable this type checking for interpolation as well, we carefully
chose our interpolation specifications to match printf conversion
specifications, and decorate functions parsing templates with
GCC_FMT_ATTR().
Note that this only protects against undefined behavior due to type
errors. It can't protect against use of invalid interpolation
specifications that happen to be valid printf conversion
specifications.
However, there's still a gaping hole in the type checking: GCC
recognizes '%' as start of printf conversion specification anywhere in
the template, but the parser recognizes it only outside JSON strings.
For instance, if someone were to pass a "{ '%s': %d }" template, GCC
would require a char * and an int argument, but the parser would
va_arg() only an int argument, resulting in undefined behavior.
Avoid undefined behavior by catching the programming error at run
time: have the parser recognize and reject '%' in JSON strings.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20180823164025.12553-57-armbru@redhat.com>
2018-08-23 18:40:23 +02:00
|
|
|
/* fall through */
|
|
|
|
default:
|
2018-08-23 18:39:49 +02:00
|
|
|
cp = mod_utf8_codepoint(ptr, 6, &end);
|
2018-08-23 18:39:52 +02:00
|
|
|
if (cp < 0) {
|
2018-08-23 18:39:49 +02:00
|
|
|
parse_error(ctxt, token, "invalid UTF-8 sequence in string");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
ptr = end;
|
|
|
|
len = mod_utf8_encode(utf8_buf, sizeof(utf8_buf), cp);
|
|
|
|
assert(len >= 0);
|
|
|
|
qstring_append(str, utf8_buf);
|
2009-11-11 17:39:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
|
|
|
|
out:
|
2018-04-19 17:01:43 +02:00
|
|
|
qobject_unref(str);
|
2009-11-11 17:39:23 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-25 22:23:32 +01:00
|
|
|
/* Note: the token object returned by parser_context_peek_token or
|
|
|
|
* parser_context_pop_token is deleted as soon as parser_context_pop_token
|
|
|
|
* is called again.
|
2015-11-25 22:23:31 +01:00
|
|
|
*/
|
2015-11-25 22:23:32 +01:00
|
|
|
static JSONToken *parser_context_pop_token(JSONParserContext *ctxt)
|
2012-08-15 20:45:43 +02:00
|
|
|
{
|
2015-11-25 22:23:32 +01:00
|
|
|
g_free(ctxt->current);
|
2015-11-25 22:23:31 +01:00
|
|
|
ctxt->current = g_queue_pop_head(ctxt->buf);
|
|
|
|
return ctxt->current;
|
2012-08-15 20:45:43 +02:00
|
|
|
}
|
|
|
|
|
2015-11-25 22:23:32 +01:00
|
|
|
static JSONToken *parser_context_peek_token(JSONParserContext *ctxt)
|
2012-08-15 20:45:43 +02:00
|
|
|
{
|
2015-11-25 22:23:31 +01:00
|
|
|
return g_queue_peek_head(ctxt->buf);
|
2012-08-15 20:45:43 +02:00
|
|
|
}
|
|
|
|
|
2009-11-11 17:39:23 +01:00
|
|
|
/**
|
|
|
|
* Parsing rules
|
|
|
|
*/
|
2018-08-23 18:40:22 +02:00
|
|
|
static int parse_pair(JSONParserContext *ctxt, QDict *dict)
|
2009-11-11 17:39:23 +01:00
|
|
|
{
|
2018-03-10 23:14:36 +01:00
|
|
|
QObject *value;
|
|
|
|
QString *key = NULL;
|
2015-11-25 22:23:32 +01:00
|
|
|
JSONToken *peek, *token;
|
2009-11-11 17:39:23 +01:00
|
|
|
|
2012-08-15 20:45:43 +02:00
|
|
|
peek = parser_context_peek_token(ctxt);
|
2011-06-01 19:14:55 +02:00
|
|
|
if (peek == NULL) {
|
|
|
|
parse_error(ctxt, NULL, "premature EOI");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2018-08-23 18:40:22 +02:00
|
|
|
key = qobject_to(QString, parse_value(ctxt));
|
2018-03-10 23:14:36 +01:00
|
|
|
if (!key) {
|
2009-11-11 17:39:23 +01:00
|
|
|
parse_error(ctxt, peek, "key is not a string in object");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-08-15 20:45:43 +02:00
|
|
|
token = parser_context_pop_token(ctxt);
|
2011-06-01 19:14:55 +02:00
|
|
|
if (token == NULL) {
|
|
|
|
parse_error(ctxt, NULL, "premature EOI");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-11-25 22:23:32 +01:00
|
|
|
if (token->type != JSON_COLON) {
|
2009-11-11 17:39:23 +01:00
|
|
|
parse_error(ctxt, token, "missing : in object pair");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2018-08-23 18:40:22 +02:00
|
|
|
value = parse_value(ctxt);
|
2009-11-11 17:39:23 +01:00
|
|
|
if (value == NULL) {
|
|
|
|
parse_error(ctxt, token, "Missing value in dict");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2018-03-10 23:14:36 +01:00
|
|
|
qdict_put_obj(dict, qstring_get_str(key), value);
|
2009-11-11 17:39:23 +01:00
|
|
|
|
2018-04-19 17:01:43 +02:00
|
|
|
qobject_unref(key);
|
2009-11-11 17:39:23 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out:
|
2018-04-19 17:01:43 +02:00
|
|
|
qobject_unref(key);
|
2009-11-11 17:39:23 +01:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-08-23 18:40:22 +02:00
|
|
|
static QObject *parse_object(JSONParserContext *ctxt)
|
2009-11-11 17:39:23 +01:00
|
|
|
{
|
|
|
|
QDict *dict = NULL;
|
2015-11-25 22:23:32 +01:00
|
|
|
JSONToken *token, *peek;
|
2009-11-11 17:39:23 +01:00
|
|
|
|
2012-08-15 20:45:43 +02:00
|
|
|
token = parser_context_pop_token(ctxt);
|
2015-11-25 22:23:32 +01:00
|
|
|
assert(token && token->type == JSON_LCURLY);
|
2009-11-11 17:39:23 +01:00
|
|
|
|
|
|
|
dict = qdict_new();
|
|
|
|
|
2012-08-15 20:45:43 +02:00
|
|
|
peek = parser_context_peek_token(ctxt);
|
2011-06-01 19:14:55 +02:00
|
|
|
if (peek == NULL) {
|
|
|
|
parse_error(ctxt, NULL, "premature EOI");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-11-25 22:23:32 +01:00
|
|
|
if (peek->type != JSON_RCURLY) {
|
2018-08-23 18:40:22 +02:00
|
|
|
if (parse_pair(ctxt, dict) == -1) {
|
2009-11-11 17:39:23 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-08-15 20:45:43 +02:00
|
|
|
token = parser_context_pop_token(ctxt);
|
2011-06-01 19:14:55 +02:00
|
|
|
if (token == NULL) {
|
|
|
|
parse_error(ctxt, NULL, "premature EOI");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-11-25 22:23:32 +01:00
|
|
|
while (token->type != JSON_RCURLY) {
|
|
|
|
if (token->type != JSON_COMMA) {
|
2009-11-11 17:39:23 +01:00
|
|
|
parse_error(ctxt, token, "expected separator in dict");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2018-08-23 18:40:22 +02:00
|
|
|
if (parse_pair(ctxt, dict) == -1) {
|
2009-11-11 17:39:23 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-08-15 20:45:43 +02:00
|
|
|
token = parser_context_pop_token(ctxt);
|
2011-06-01 19:14:55 +02:00
|
|
|
if (token == NULL) {
|
|
|
|
parse_error(ctxt, NULL, "premature EOI");
|
|
|
|
goto out;
|
|
|
|
}
|
2009-11-11 17:39:23 +01:00
|
|
|
}
|
|
|
|
} else {
|
2014-06-10 11:20:24 +02:00
|
|
|
(void)parser_context_pop_token(ctxt);
|
2009-11-11 17:39:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return QOBJECT(dict);
|
|
|
|
|
|
|
|
out:
|
2018-04-19 17:01:43 +02:00
|
|
|
qobject_unref(dict);
|
2009-11-11 17:39:23 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-08-23 18:40:22 +02:00
|
|
|
static QObject *parse_array(JSONParserContext *ctxt)
|
2009-11-11 17:39:23 +01:00
|
|
|
{
|
|
|
|
QList *list = NULL;
|
2015-11-25 22:23:32 +01:00
|
|
|
JSONToken *token, *peek;
|
2009-11-11 17:39:23 +01:00
|
|
|
|
2012-08-15 20:45:43 +02:00
|
|
|
token = parser_context_pop_token(ctxt);
|
2015-11-25 22:23:32 +01:00
|
|
|
assert(token && token->type == JSON_LSQUARE);
|
2009-11-11 17:39:23 +01:00
|
|
|
|
|
|
|
list = qlist_new();
|
|
|
|
|
2012-08-15 20:45:43 +02:00
|
|
|
peek = parser_context_peek_token(ctxt);
|
2011-06-01 19:14:55 +02:00
|
|
|
if (peek == NULL) {
|
|
|
|
parse_error(ctxt, NULL, "premature EOI");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-11-25 22:23:32 +01:00
|
|
|
if (peek->type != JSON_RSQUARE) {
|
2009-11-11 17:39:23 +01:00
|
|
|
QObject *obj;
|
|
|
|
|
2018-08-23 18:40:22 +02:00
|
|
|
obj = parse_value(ctxt);
|
2009-11-11 17:39:23 +01:00
|
|
|
if (obj == NULL) {
|
|
|
|
parse_error(ctxt, token, "expecting value");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
qlist_append_obj(list, obj);
|
|
|
|
|
2012-08-15 20:45:43 +02:00
|
|
|
token = parser_context_pop_token(ctxt);
|
2011-06-01 19:14:55 +02:00
|
|
|
if (token == NULL) {
|
|
|
|
parse_error(ctxt, NULL, "premature EOI");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-11-25 22:23:32 +01:00
|
|
|
while (token->type != JSON_RSQUARE) {
|
|
|
|
if (token->type != JSON_COMMA) {
|
2009-11-11 17:39:23 +01:00
|
|
|
parse_error(ctxt, token, "expected separator in list");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2018-08-23 18:40:22 +02:00
|
|
|
obj = parse_value(ctxt);
|
2009-11-11 17:39:23 +01:00
|
|
|
if (obj == NULL) {
|
|
|
|
parse_error(ctxt, token, "expecting value");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
qlist_append_obj(list, obj);
|
|
|
|
|
2012-08-15 20:45:43 +02:00
|
|
|
token = parser_context_pop_token(ctxt);
|
2011-06-01 19:14:55 +02:00
|
|
|
if (token == NULL) {
|
|
|
|
parse_error(ctxt, NULL, "premature EOI");
|
|
|
|
goto out;
|
|
|
|
}
|
2009-11-11 17:39:23 +01:00
|
|
|
}
|
|
|
|
} else {
|
2014-06-10 11:20:24 +02:00
|
|
|
(void)parser_context_pop_token(ctxt);
|
2009-11-11 17:39:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return QOBJECT(list);
|
|
|
|
|
|
|
|
out:
|
2018-04-19 17:01:43 +02:00
|
|
|
qobject_unref(list);
|
2009-11-11 17:39:23 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-15 20:45:43 +02:00
|
|
|
static QObject *parse_keyword(JSONParserContext *ctxt)
|
2009-11-11 17:39:23 +01:00
|
|
|
{
|
2015-11-25 22:23:32 +01:00
|
|
|
JSONToken *token;
|
2009-11-11 17:39:23 +01:00
|
|
|
|
2012-08-15 20:45:43 +02:00
|
|
|
token = parser_context_pop_token(ctxt);
|
2015-11-25 22:23:32 +01:00
|
|
|
assert(token && token->type == JSON_KEYWORD);
|
2015-11-25 22:23:27 +01:00
|
|
|
|
2015-11-25 22:23:32 +01:00
|
|
|
if (!strcmp(token->str, "true")) {
|
2015-11-25 22:23:30 +01:00
|
|
|
return QOBJECT(qbool_from_bool(true));
|
2015-11-25 22:23:32 +01:00
|
|
|
} else if (!strcmp(token->str, "false")) {
|
2015-11-25 22:23:30 +01:00
|
|
|
return QOBJECT(qbool_from_bool(false));
|
2015-11-25 22:23:32 +01:00
|
|
|
} else if (!strcmp(token->str, "null")) {
|
2017-06-26 13:52:24 +02:00
|
|
|
return QOBJECT(qnull());
|
2009-11-11 17:39:23 +01:00
|
|
|
}
|
2015-11-25 22:23:32 +01:00
|
|
|
parse_error(ctxt, token, "invalid keyword '%s'", token->str);
|
2009-11-11 17:39:23 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-08-23 18:40:22 +02:00
|
|
|
static QObject *parse_interpolation(JSONParserContext *ctxt)
|
2009-11-11 17:39:23 +01:00
|
|
|
{
|
2015-11-25 22:23:32 +01:00
|
|
|
JSONToken *token;
|
2009-11-11 17:39:23 +01:00
|
|
|
|
2012-08-15 20:45:43 +02:00
|
|
|
token = parser_context_pop_token(ctxt);
|
2018-08-23 18:40:04 +02:00
|
|
|
assert(token && token->type == JSON_INTERP);
|
2015-11-25 22:23:28 +01:00
|
|
|
|
2015-11-25 22:23:32 +01:00
|
|
|
if (!strcmp(token->str, "%p")) {
|
2018-08-23 18:40:22 +02:00
|
|
|
return va_arg(*ctxt->ap, QObject *);
|
2015-11-25 22:23:32 +01:00
|
|
|
} else if (!strcmp(token->str, "%i")) {
|
2018-08-23 18:40:22 +02:00
|
|
|
return QOBJECT(qbool_from_bool(va_arg(*ctxt->ap, int)));
|
2015-11-25 22:23:32 +01:00
|
|
|
} else if (!strcmp(token->str, "%d")) {
|
2018-08-23 18:40:22 +02:00
|
|
|
return QOBJECT(qnum_from_int(va_arg(*ctxt->ap, int)));
|
2015-11-25 22:23:32 +01:00
|
|
|
} else if (!strcmp(token->str, "%ld")) {
|
2018-08-23 18:40:22 +02:00
|
|
|
return QOBJECT(qnum_from_int(va_arg(*ctxt->ap, long)));
|
2018-08-23 18:40:08 +02:00
|
|
|
} else if (!strcmp(token->str, "%lld")) {
|
2018-08-23 18:40:22 +02:00
|
|
|
return QOBJECT(qnum_from_int(va_arg(*ctxt->ap, long long)));
|
2018-08-23 18:40:08 +02:00
|
|
|
} else if (!strcmp(token->str, "%" PRId64)) {
|
2018-08-23 18:40:22 +02:00
|
|
|
return QOBJECT(qnum_from_int(va_arg(*ctxt->ap, int64_t)));
|
2017-06-07 18:36:02 +02:00
|
|
|
} else if (!strcmp(token->str, "%u")) {
|
2018-08-23 18:40:22 +02:00
|
|
|
return QOBJECT(qnum_from_uint(va_arg(*ctxt->ap, unsigned int)));
|
2017-06-07 18:36:02 +02:00
|
|
|
} else if (!strcmp(token->str, "%lu")) {
|
2018-08-23 18:40:22 +02:00
|
|
|
return QOBJECT(qnum_from_uint(va_arg(*ctxt->ap, unsigned long)));
|
2018-08-23 18:40:08 +02:00
|
|
|
} else if (!strcmp(token->str, "%llu")) {
|
2018-08-23 18:40:22 +02:00
|
|
|
return QOBJECT(qnum_from_uint(va_arg(*ctxt->ap, unsigned long long)));
|
2018-08-23 18:40:08 +02:00
|
|
|
} else if (!strcmp(token->str, "%" PRIu64)) {
|
2018-08-23 18:40:22 +02:00
|
|
|
return QOBJECT(qnum_from_uint(va_arg(*ctxt->ap, uint64_t)));
|
2015-11-25 22:23:32 +01:00
|
|
|
} else if (!strcmp(token->str, "%s")) {
|
2018-08-23 18:40:22 +02:00
|
|
|
return QOBJECT(qstring_from_str(va_arg(*ctxt->ap, const char *)));
|
2015-11-25 22:23:32 +01:00
|
|
|
} else if (!strcmp(token->str, "%f")) {
|
2018-08-23 18:40:22 +02:00
|
|
|
return QOBJECT(qnum_from_double(va_arg(*ctxt->ap, double)));
|
2009-11-11 17:39:23 +01:00
|
|
|
}
|
2018-08-23 18:40:07 +02:00
|
|
|
parse_error(ctxt, token, "invalid interpolation '%s'", token->str);
|
2009-11-11 17:39:23 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-15 20:45:43 +02:00
|
|
|
static QObject *parse_literal(JSONParserContext *ctxt)
|
2009-11-11 17:39:23 +01:00
|
|
|
{
|
2015-11-25 22:23:32 +01:00
|
|
|
JSONToken *token;
|
2009-11-11 17:39:23 +01:00
|
|
|
|
2012-08-15 20:45:43 +02:00
|
|
|
token = parser_context_pop_token(ctxt);
|
2015-11-25 22:23:30 +01:00
|
|
|
assert(token);
|
2011-06-01 19:14:55 +02:00
|
|
|
|
2015-11-25 22:23:32 +01:00
|
|
|
switch (token->type) {
|
2009-11-11 17:39:23 +01:00
|
|
|
case JSON_STRING:
|
2018-08-23 18:39:53 +02:00
|
|
|
return QOBJECT(parse_string(ctxt, token));
|
2013-05-11 00:46:05 +02:00
|
|
|
case JSON_INTEGER: {
|
2017-06-07 18:35:58 +02:00
|
|
|
/*
|
|
|
|
* Represent JSON_INTEGER as QNUM_I64 if possible, else as
|
2017-06-07 18:36:02 +02:00
|
|
|
* QNUM_U64, else as QNUM_DOUBLE. Note that qemu_strtoi64()
|
|
|
|
* and qemu_strtou64() fail with ERANGE when it's not
|
|
|
|
* possible.
|
2013-05-11 00:46:05 +02:00
|
|
|
*
|
2017-06-07 18:35:58 +02:00
|
|
|
* qnum_get_int() will then work for any signed 64-bit
|
2017-06-07 18:36:02 +02:00
|
|
|
* JSON_INTEGER, qnum_get_uint() for any unsigned 64-bit
|
|
|
|
* integer, and qnum_get_double() both for any JSON_INTEGER
|
|
|
|
* and any JSON_FLOAT (with precision loss for integers beyond
|
|
|
|
* 53 bits)
|
2013-05-11 00:46:05 +02:00
|
|
|
*/
|
2017-06-07 18:36:02 +02:00
|
|
|
int ret;
|
2013-05-11 00:46:05 +02:00
|
|
|
int64_t value;
|
2017-06-07 18:36:02 +02:00
|
|
|
uint64_t uvalue;
|
2013-05-11 00:46:05 +02:00
|
|
|
|
2017-06-07 18:36:02 +02:00
|
|
|
ret = qemu_strtoi64(token->str, NULL, 10, &value);
|
|
|
|
if (!ret) {
|
2017-06-07 18:35:58 +02:00
|
|
|
return QOBJECT(qnum_from_int(value));
|
2013-05-11 00:46:05 +02:00
|
|
|
}
|
2017-06-07 18:36:02 +02:00
|
|
|
assert(ret == -ERANGE);
|
|
|
|
|
|
|
|
if (token->str[0] != '-') {
|
|
|
|
ret = qemu_strtou64(token->str, NULL, 10, &uvalue);
|
|
|
|
if (!ret) {
|
|
|
|
return QOBJECT(qnum_from_uint(uvalue));
|
|
|
|
}
|
|
|
|
assert(ret == -ERANGE);
|
|
|
|
}
|
2013-05-11 00:46:05 +02:00
|
|
|
/* fall through to JSON_FLOAT */
|
|
|
|
}
|
2009-11-11 17:39:23 +01:00
|
|
|
case JSON_FLOAT:
|
2016-01-29 14:48:37 +01:00
|
|
|
/* FIXME dependent on locale; a pervasive issue in QEMU */
|
2018-08-23 18:40:25 +02:00
|
|
|
/* FIXME our lexer matches RFC 8259 in forbidding Inf or NaN,
|
2016-01-29 14:48:37 +01:00
|
|
|
* but those might be useful extensions beyond JSON */
|
2017-06-07 18:35:58 +02:00
|
|
|
return QOBJECT(qnum_from_double(strtod(token->str, NULL)));
|
2009-11-11 17:39:23 +01:00
|
|
|
default:
|
2015-11-25 22:23:30 +01:00
|
|
|
abort();
|
2009-11-11 17:39:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 18:40:22 +02:00
|
|
|
static QObject *parse_value(JSONParserContext *ctxt)
|
2009-11-11 17:39:23 +01:00
|
|
|
{
|
2015-11-25 22:23:32 +01:00
|
|
|
JSONToken *token;
|
2009-11-11 17:39:23 +01:00
|
|
|
|
2015-11-25 22:23:30 +01:00
|
|
|
token = parser_context_peek_token(ctxt);
|
|
|
|
if (token == NULL) {
|
|
|
|
parse_error(ctxt, NULL, "premature EOI");
|
|
|
|
return NULL;
|
2009-11-11 17:39:23 +01:00
|
|
|
}
|
|
|
|
|
2015-11-25 22:23:32 +01:00
|
|
|
switch (token->type) {
|
2015-11-25 22:23:30 +01:00
|
|
|
case JSON_LCURLY:
|
2018-08-23 18:40:22 +02:00
|
|
|
return parse_object(ctxt);
|
2015-11-25 22:23:30 +01:00
|
|
|
case JSON_LSQUARE:
|
2018-08-23 18:40:22 +02:00
|
|
|
return parse_array(ctxt);
|
2018-08-23 18:40:04 +02:00
|
|
|
case JSON_INTERP:
|
2018-08-23 18:40:22 +02:00
|
|
|
return parse_interpolation(ctxt);
|
2015-11-25 22:23:30 +01:00
|
|
|
case JSON_INTEGER:
|
|
|
|
case JSON_FLOAT:
|
|
|
|
case JSON_STRING:
|
|
|
|
return parse_literal(ctxt);
|
|
|
|
case JSON_KEYWORD:
|
|
|
|
return parse_keyword(ctxt);
|
|
|
|
default:
|
|
|
|
parse_error(ctxt, token, "expecting value");
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-11-11 17:39:23 +01:00
|
|
|
}
|
|
|
|
|
2018-08-23 18:40:18 +02:00
|
|
|
JSONToken *json_token(JSONTokenType type, int x, int y, GString *tokstr)
|
|
|
|
{
|
|
|
|
JSONToken *token = g_malloc(sizeof(JSONToken) + tokstr->len + 1);
|
|
|
|
|
|
|
|
token->type = type;
|
|
|
|
memcpy(token->str, tokstr->str, tokstr->len);
|
|
|
|
token->str[tokstr->len] = 0;
|
|
|
|
token->x = x;
|
|
|
|
token->y = y;
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2018-08-23 18:40:01 +02:00
|
|
|
QObject *json_parser_parse(GQueue *tokens, va_list *ap, Error **errp)
|
2009-11-11 17:39:23 +01:00
|
|
|
{
|
2018-08-23 18:40:22 +02:00
|
|
|
JSONParserContext ctxt = { .buf = tokens, .ap = ap };
|
2009-11-11 17:39:23 +01:00
|
|
|
QObject *result;
|
|
|
|
|
2018-08-23 18:40:22 +02:00
|
|
|
result = parse_value(&ctxt);
|
2018-08-23 18:40:13 +02:00
|
|
|
assert(ctxt.err || g_queue_is_empty(ctxt.buf));
|
2012-08-15 20:45:43 +02:00
|
|
|
|
2018-08-23 18:39:59 +02:00
|
|
|
error_propagate(errp, ctxt.err);
|
2009-11-11 17:39:23 +01:00
|
|
|
|
2018-08-23 18:39:59 +02:00
|
|
|
while (!g_queue_is_empty(ctxt.buf)) {
|
|
|
|
parser_context_pop_token(&ctxt);
|
|
|
|
}
|
|
|
|
g_free(ctxt.current);
|
2011-06-01 19:14:50 +02:00
|
|
|
|
2009-11-11 17:39:23 +01:00
|
|
|
return result;
|
|
|
|
}
|