Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 1 | /* |
Eric Blake | 6e8e5cb | 2016-01-29 06:48:37 -0700 | [diff] [blame] | 2 | * JSON Parser |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 3 | * |
| 4 | * Copyright IBM, Corp. 2009 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Anthony Liguori <aliguori@us.ibm.com> |
| 8 | * |
| 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. |
| 11 | * |
| 12 | */ |
| 13 | |
Peter Maydell | f2ad72b | 2016-01-29 17:50:01 +0000 | [diff] [blame] | 14 | #include "qemu/osdep.h" |
Marc-André Lureau | 2bc7cfe | 2017-06-07 20:36:02 +0400 | [diff] [blame] | 15 | #include "qemu/cutils.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 16 | #include "qapi/error.h" |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 17 | #include "qemu-common.h" |
Markus Armbruster | 6b67395 | 2018-02-01 12:18:35 +0100 | [diff] [blame] | 18 | #include "qapi/qmp/qbool.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame^] | 19 | #include "qapi/qmp/qdict.h" |
Markus Armbruster | 47e6b29 | 2018-02-01 12:18:38 +0100 | [diff] [blame] | 20 | #include "qapi/qmp/qlist.h" |
Markus Armbruster | 15280c3 | 2018-02-01 12:18:36 +0100 | [diff] [blame] | 21 | #include "qapi/qmp/qnull.h" |
| 22 | #include "qapi/qmp/qnum.h" |
Markus Armbruster | 6b67395 | 2018-02-01 12:18:35 +0100 | [diff] [blame] | 23 | #include "qapi/qmp/qstring.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 24 | #include "qapi/qmp/json-parser.h" |
| 25 | #include "qapi/qmp/json-lexer.h" |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 26 | #include "qapi/qmp/json-streamer.h" |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 27 | |
| 28 | typedef struct JSONParserContext |
| 29 | { |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 30 | Error *err; |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 31 | JSONToken *current; |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 32 | GQueue *buf; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 33 | } JSONParserContext; |
| 34 | |
| 35 | #define BUG_ON(cond) assert(!(cond)) |
| 36 | |
| 37 | /** |
| 38 | * TODO |
| 39 | * |
| 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 |
| 44 | */ |
| 45 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 46 | static QObject *parse_value(JSONParserContext *ctxt, va_list *ap); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 47 | |
| 48 | /** |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 49 | * Error handler |
| 50 | */ |
Stefan Weil | 8b7968f | 2010-09-23 21:28:05 +0200 | [diff] [blame] | 51 | static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext *ctxt, |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 52 | JSONToken *token, const char *msg, ...) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 53 | { |
Amos Kong | c96c84a | 2010-03-24 23:12:05 +0800 | [diff] [blame] | 54 | va_list ap; |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 55 | char message[1024]; |
Amos Kong | c96c84a | 2010-03-24 23:12:05 +0800 | [diff] [blame] | 56 | va_start(ap, msg); |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 57 | vsnprintf(message, sizeof(message), msg, ap); |
Amos Kong | c96c84a | 2010-03-24 23:12:05 +0800 | [diff] [blame] | 58 | va_end(ap); |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 59 | if (ctxt->err) { |
| 60 | error_free(ctxt->err); |
| 61 | ctxt->err = NULL; |
| 62 | } |
Cole Robinson | f231b88 | 2014-03-21 19:42:26 -0400 | [diff] [blame] | 63 | error_setg(&ctxt->err, "JSON parse error, %s", message); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
| 67 | * String helpers |
| 68 | * |
| 69 | * These helpers are used to unescape strings. |
| 70 | */ |
| 71 | static void wchar_to_utf8(uint16_t wchar, char *buffer, size_t buffer_length) |
| 72 | { |
| 73 | if (wchar <= 0x007F) { |
| 74 | BUG_ON(buffer_length < 2); |
| 75 | |
| 76 | buffer[0] = wchar & 0x7F; |
| 77 | buffer[1] = 0; |
| 78 | } else if (wchar <= 0x07FF) { |
| 79 | BUG_ON(buffer_length < 3); |
| 80 | |
| 81 | buffer[0] = 0xC0 | ((wchar >> 6) & 0x1F); |
| 82 | buffer[1] = 0x80 | (wchar & 0x3F); |
| 83 | buffer[2] = 0; |
| 84 | } else { |
| 85 | BUG_ON(buffer_length < 4); |
| 86 | |
| 87 | buffer[0] = 0xE0 | ((wchar >> 12) & 0x0F); |
| 88 | buffer[1] = 0x80 | ((wchar >> 6) & 0x3F); |
| 89 | buffer[2] = 0x80 | (wchar & 0x3F); |
| 90 | buffer[3] = 0; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static int hex2decimal(char ch) |
| 95 | { |
| 96 | if (ch >= '0' && ch <= '9') { |
| 97 | return (ch - '0'); |
| 98 | } else if (ch >= 'a' && ch <= 'f') { |
| 99 | return 10 + (ch - 'a'); |
| 100 | } else if (ch >= 'A' && ch <= 'F') { |
| 101 | return 10 + (ch - 'A'); |
| 102 | } |
| 103 | |
| 104 | return -1; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * parse_string(): Parse a json string and return a QObject |
| 109 | * |
| 110 | * string |
| 111 | * "" |
| 112 | * " chars " |
| 113 | * chars |
| 114 | * char |
| 115 | * char chars |
| 116 | * char |
| 117 | * any-Unicode-character- |
| 118 | * except-"-or-\-or- |
| 119 | * control-character |
| 120 | * \" |
| 121 | * \\ |
| 122 | * \/ |
| 123 | * \b |
| 124 | * \f |
| 125 | * \n |
| 126 | * \r |
| 127 | * \t |
| 128 | * \u four-hex-digits |
| 129 | */ |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 130 | static QString *qstring_from_escaped_str(JSONParserContext *ctxt, |
| 131 | JSONToken *token) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 132 | { |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 133 | const char *ptr = token->str; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 134 | QString *str; |
| 135 | int double_quote = 1; |
| 136 | |
| 137 | if (*ptr == '"') { |
| 138 | double_quote = 1; |
| 139 | } else { |
| 140 | double_quote = 0; |
| 141 | } |
| 142 | ptr++; |
| 143 | |
| 144 | str = qstring_new(); |
| 145 | while (*ptr && |
| 146 | ((double_quote && *ptr != '"') || (!double_quote && *ptr != '\''))) { |
| 147 | if (*ptr == '\\') { |
| 148 | ptr++; |
| 149 | |
| 150 | switch (*ptr) { |
| 151 | case '"': |
| 152 | qstring_append(str, "\""); |
| 153 | ptr++; |
| 154 | break; |
| 155 | case '\'': |
| 156 | qstring_append(str, "'"); |
| 157 | ptr++; |
| 158 | break; |
| 159 | case '\\': |
| 160 | qstring_append(str, "\\"); |
| 161 | ptr++; |
| 162 | break; |
| 163 | case '/': |
| 164 | qstring_append(str, "/"); |
| 165 | ptr++; |
| 166 | break; |
| 167 | case 'b': |
| 168 | qstring_append(str, "\b"); |
| 169 | ptr++; |
| 170 | break; |
Luiz Capitulino | bd03269 | 2010-05-19 17:06:15 -0300 | [diff] [blame] | 171 | case 'f': |
| 172 | qstring_append(str, "\f"); |
| 173 | ptr++; |
| 174 | break; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 175 | case 'n': |
| 176 | qstring_append(str, "\n"); |
| 177 | ptr++; |
| 178 | break; |
| 179 | case 'r': |
| 180 | qstring_append(str, "\r"); |
| 181 | ptr++; |
| 182 | break; |
| 183 | case 't': |
| 184 | qstring_append(str, "\t"); |
| 185 | ptr++; |
| 186 | break; |
| 187 | case 'u': { |
| 188 | uint16_t unicode_char = 0; |
| 189 | char utf8_char[4]; |
| 190 | int i = 0; |
| 191 | |
| 192 | ptr++; |
| 193 | |
| 194 | for (i = 0; i < 4; i++) { |
| 195 | if (qemu_isxdigit(*ptr)) { |
| 196 | unicode_char |= hex2decimal(*ptr) << ((3 - i) * 4); |
| 197 | } else { |
| 198 | parse_error(ctxt, token, |
| 199 | "invalid hex escape sequence in string"); |
| 200 | goto out; |
| 201 | } |
| 202 | ptr++; |
| 203 | } |
| 204 | |
| 205 | wchar_to_utf8(unicode_char, utf8_char, sizeof(utf8_char)); |
| 206 | qstring_append(str, utf8_char); |
| 207 | } break; |
| 208 | default: |
| 209 | parse_error(ctxt, token, "invalid escape sequence in string"); |
| 210 | goto out; |
| 211 | } |
| 212 | } else { |
| 213 | char dummy[2]; |
| 214 | |
| 215 | dummy[0] = *ptr++; |
| 216 | dummy[1] = 0; |
| 217 | |
| 218 | qstring_append(str, dummy); |
| 219 | } |
| 220 | } |
| 221 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 222 | return str; |
| 223 | |
| 224 | out: |
| 225 | QDECREF(str); |
| 226 | return NULL; |
| 227 | } |
| 228 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 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 |
| 231 | * is called again. |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 232 | */ |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 233 | static JSONToken *parser_context_pop_token(JSONParserContext *ctxt) |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 234 | { |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 235 | g_free(ctxt->current); |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 236 | assert(!g_queue_is_empty(ctxt->buf)); |
| 237 | ctxt->current = g_queue_pop_head(ctxt->buf); |
| 238 | return ctxt->current; |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 239 | } |
| 240 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 241 | static JSONToken *parser_context_peek_token(JSONParserContext *ctxt) |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 242 | { |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 243 | assert(!g_queue_is_empty(ctxt->buf)); |
| 244 | return g_queue_peek_head(ctxt->buf); |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 245 | } |
| 246 | |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 247 | static JSONParserContext *parser_context_new(GQueue *tokens) |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 248 | { |
| 249 | JSONParserContext *ctxt; |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 250 | |
| 251 | if (!tokens) { |
| 252 | return NULL; |
| 253 | } |
| 254 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 255 | ctxt = g_malloc0(sizeof(JSONParserContext)); |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 256 | ctxt->buf = tokens; |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 257 | |
| 258 | return ctxt; |
| 259 | } |
| 260 | |
| 261 | /* to support error propagation, ctxt->err must be freed separately */ |
| 262 | static void parser_context_free(JSONParserContext *ctxt) |
| 263 | { |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 264 | if (ctxt) { |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 265 | while (!g_queue_is_empty(ctxt->buf)) { |
| 266 | parser_context_pop_token(ctxt); |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 267 | } |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 268 | g_free(ctxt->current); |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 269 | g_queue_free(ctxt->buf); |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 270 | g_free(ctxt); |
| 271 | } |
| 272 | } |
| 273 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 274 | /** |
| 275 | * Parsing rules |
| 276 | */ |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 277 | static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 278 | { |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 279 | QObject *key = NULL, *value; |
| 280 | JSONToken *peek, *token; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 281 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 282 | peek = parser_context_peek_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 283 | if (peek == NULL) { |
| 284 | parse_error(ctxt, NULL, "premature EOI"); |
| 285 | goto out; |
| 286 | } |
| 287 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 288 | key = parse_value(ctxt, ap); |
Kevin Wolf | d758d90 | 2010-02-24 16:17:58 +0100 | [diff] [blame] | 289 | if (!key || qobject_type(key) != QTYPE_QSTRING) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 290 | parse_error(ctxt, peek, "key is not a string in object"); |
| 291 | goto out; |
| 292 | } |
| 293 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 294 | token = parser_context_pop_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 295 | if (token == NULL) { |
| 296 | parse_error(ctxt, NULL, "premature EOI"); |
| 297 | goto out; |
| 298 | } |
| 299 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 300 | if (token->type != JSON_COLON) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 301 | parse_error(ctxt, token, "missing : in object pair"); |
| 302 | goto out; |
| 303 | } |
| 304 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 305 | value = parse_value(ctxt, ap); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 306 | if (value == NULL) { |
| 307 | parse_error(ctxt, token, "Missing value in dict"); |
| 308 | goto out; |
| 309 | } |
| 310 | |
| 311 | qdict_put_obj(dict, qstring_get_str(qobject_to_qstring(key)), value); |
| 312 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 313 | qobject_decref(key); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 314 | |
| 315 | return 0; |
| 316 | |
| 317 | out: |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 318 | qobject_decref(key); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 319 | |
| 320 | return -1; |
| 321 | } |
| 322 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 323 | static QObject *parse_object(JSONParserContext *ctxt, va_list *ap) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 324 | { |
| 325 | QDict *dict = NULL; |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 326 | JSONToken *token, *peek; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 327 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 328 | token = parser_context_pop_token(ctxt); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 329 | assert(token && token->type == JSON_LCURLY); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 330 | |
| 331 | dict = qdict_new(); |
| 332 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 333 | peek = parser_context_peek_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 334 | if (peek == NULL) { |
| 335 | parse_error(ctxt, NULL, "premature EOI"); |
| 336 | goto out; |
| 337 | } |
| 338 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 339 | if (peek->type != JSON_RCURLY) { |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 340 | if (parse_pair(ctxt, dict, ap) == -1) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 341 | goto out; |
| 342 | } |
| 343 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 344 | token = parser_context_pop_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 345 | if (token == NULL) { |
| 346 | parse_error(ctxt, NULL, "premature EOI"); |
| 347 | goto out; |
| 348 | } |
| 349 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 350 | while (token->type != JSON_RCURLY) { |
| 351 | if (token->type != JSON_COMMA) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 352 | parse_error(ctxt, token, "expected separator in dict"); |
| 353 | goto out; |
| 354 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 355 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 356 | if (parse_pair(ctxt, dict, ap) == -1) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 357 | goto out; |
| 358 | } |
| 359 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 360 | token = parser_context_pop_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 361 | if (token == NULL) { |
| 362 | parse_error(ctxt, NULL, "premature EOI"); |
| 363 | goto out; |
| 364 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 365 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 366 | } else { |
Gonglei | a491af4 | 2014-06-10 17:20:24 +0800 | [diff] [blame] | 367 | (void)parser_context_pop_token(ctxt); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 368 | } |
| 369 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 370 | return QOBJECT(dict); |
| 371 | |
| 372 | out: |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 373 | QDECREF(dict); |
| 374 | return NULL; |
| 375 | } |
| 376 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 377 | static QObject *parse_array(JSONParserContext *ctxt, va_list *ap) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 378 | { |
| 379 | QList *list = NULL; |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 380 | JSONToken *token, *peek; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 381 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 382 | token = parser_context_pop_token(ctxt); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 383 | assert(token && token->type == JSON_LSQUARE); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 384 | |
| 385 | list = qlist_new(); |
| 386 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 387 | peek = parser_context_peek_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 388 | if (peek == NULL) { |
| 389 | parse_error(ctxt, NULL, "premature EOI"); |
| 390 | goto out; |
| 391 | } |
| 392 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 393 | if (peek->type != JSON_RSQUARE) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 394 | QObject *obj; |
| 395 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 396 | obj = parse_value(ctxt, ap); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 397 | if (obj == NULL) { |
| 398 | parse_error(ctxt, token, "expecting value"); |
| 399 | goto out; |
| 400 | } |
| 401 | |
| 402 | qlist_append_obj(list, obj); |
| 403 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 404 | token = parser_context_pop_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 405 | if (token == NULL) { |
| 406 | parse_error(ctxt, NULL, "premature EOI"); |
| 407 | goto out; |
| 408 | } |
| 409 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 410 | while (token->type != JSON_RSQUARE) { |
| 411 | if (token->type != JSON_COMMA) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 412 | parse_error(ctxt, token, "expected separator in list"); |
| 413 | goto out; |
| 414 | } |
| 415 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 416 | obj = parse_value(ctxt, ap); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 417 | if (obj == NULL) { |
| 418 | parse_error(ctxt, token, "expecting value"); |
| 419 | goto out; |
| 420 | } |
| 421 | |
| 422 | qlist_append_obj(list, obj); |
| 423 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 424 | token = parser_context_pop_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 425 | if (token == NULL) { |
| 426 | parse_error(ctxt, NULL, "premature EOI"); |
| 427 | goto out; |
| 428 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 429 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 430 | } else { |
Gonglei | a491af4 | 2014-06-10 17:20:24 +0800 | [diff] [blame] | 431 | (void)parser_context_pop_token(ctxt); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 432 | } |
| 433 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 434 | return QOBJECT(list); |
| 435 | |
| 436 | out: |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 437 | QDECREF(list); |
| 438 | return NULL; |
| 439 | } |
| 440 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 441 | static QObject *parse_keyword(JSONParserContext *ctxt) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 442 | { |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 443 | JSONToken *token; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 444 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 445 | token = parser_context_pop_token(ctxt); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 446 | assert(token && token->type == JSON_KEYWORD); |
Markus Armbruster | 50e2a46 | 2015-11-25 22:23:27 +0100 | [diff] [blame] | 447 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 448 | if (!strcmp(token->str, "true")) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 449 | return QOBJECT(qbool_from_bool(true)); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 450 | } else if (!strcmp(token->str, "false")) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 451 | return QOBJECT(qbool_from_bool(false)); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 452 | } else if (!strcmp(token->str, "null")) { |
Markus Armbruster | 006ca09 | 2017-06-26 13:52:24 +0200 | [diff] [blame] | 453 | return QOBJECT(qnull()); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 454 | } |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 455 | parse_error(ctxt, token, "invalid keyword '%s'", token->str); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 456 | return NULL; |
| 457 | } |
| 458 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 459 | static QObject *parse_escape(JSONParserContext *ctxt, va_list *ap) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 460 | { |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 461 | JSONToken *token; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 462 | |
| 463 | if (ap == NULL) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 464 | return NULL; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 465 | } |
| 466 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 467 | token = parser_context_pop_token(ctxt); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 468 | assert(token && token->type == JSON_ESCAPE); |
Markus Armbruster | 6b9606f | 2015-11-25 22:23:28 +0100 | [diff] [blame] | 469 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 470 | if (!strcmp(token->str, "%p")) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 471 | return va_arg(*ap, QObject *); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 472 | } else if (!strcmp(token->str, "%i")) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 473 | return QOBJECT(qbool_from_bool(va_arg(*ap, int))); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 474 | } else if (!strcmp(token->str, "%d")) { |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 475 | return QOBJECT(qnum_from_int(va_arg(*ap, int))); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 476 | } else if (!strcmp(token->str, "%ld")) { |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 477 | return QOBJECT(qnum_from_int(va_arg(*ap, long))); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 478 | } else if (!strcmp(token->str, "%lld") || |
| 479 | !strcmp(token->str, "%I64d")) { |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 480 | return QOBJECT(qnum_from_int(va_arg(*ap, long long))); |
Marc-André Lureau | 2bc7cfe | 2017-06-07 20:36:02 +0400 | [diff] [blame] | 481 | } else if (!strcmp(token->str, "%u")) { |
| 482 | return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned int))); |
| 483 | } else if (!strcmp(token->str, "%lu")) { |
| 484 | return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned long))); |
| 485 | } else if (!strcmp(token->str, "%llu") || |
| 486 | !strcmp(token->str, "%I64u")) { |
| 487 | return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned long long))); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 488 | } else if (!strcmp(token->str, "%s")) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 489 | return QOBJECT(qstring_from_str(va_arg(*ap, const char *))); |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 490 | } else if (!strcmp(token->str, "%f")) { |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 491 | return QOBJECT(qnum_from_double(va_arg(*ap, double))); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 492 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 493 | return NULL; |
| 494 | } |
| 495 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 496 | static QObject *parse_literal(JSONParserContext *ctxt) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 497 | { |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 498 | JSONToken *token; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 499 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 500 | token = parser_context_pop_token(ctxt); |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 501 | assert(token); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 502 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 503 | switch (token->type) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 504 | case JSON_STRING: |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 505 | return QOBJECT(qstring_from_escaped_str(ctxt, token)); |
Michael Roth | 3d5b3ec | 2013-05-10 17:46:05 -0500 | [diff] [blame] | 506 | case JSON_INTEGER: { |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 507 | /* |
| 508 | * Represent JSON_INTEGER as QNUM_I64 if possible, else as |
Marc-André Lureau | 2bc7cfe | 2017-06-07 20:36:02 +0400 | [diff] [blame] | 509 | * QNUM_U64, else as QNUM_DOUBLE. Note that qemu_strtoi64() |
| 510 | * and qemu_strtou64() fail with ERANGE when it's not |
| 511 | * possible. |
Michael Roth | 3d5b3ec | 2013-05-10 17:46:05 -0500 | [diff] [blame] | 512 | * |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 513 | * qnum_get_int() will then work for any signed 64-bit |
Marc-André Lureau | 2bc7cfe | 2017-06-07 20:36:02 +0400 | [diff] [blame] | 514 | * JSON_INTEGER, qnum_get_uint() for any unsigned 64-bit |
| 515 | * integer, and qnum_get_double() both for any JSON_INTEGER |
| 516 | * and any JSON_FLOAT (with precision loss for integers beyond |
| 517 | * 53 bits) |
Michael Roth | 3d5b3ec | 2013-05-10 17:46:05 -0500 | [diff] [blame] | 518 | */ |
Marc-André Lureau | 2bc7cfe | 2017-06-07 20:36:02 +0400 | [diff] [blame] | 519 | int ret; |
Michael Roth | 3d5b3ec | 2013-05-10 17:46:05 -0500 | [diff] [blame] | 520 | int64_t value; |
Marc-André Lureau | 2bc7cfe | 2017-06-07 20:36:02 +0400 | [diff] [blame] | 521 | uint64_t uvalue; |
Michael Roth | 3d5b3ec | 2013-05-10 17:46:05 -0500 | [diff] [blame] | 522 | |
Marc-André Lureau | 2bc7cfe | 2017-06-07 20:36:02 +0400 | [diff] [blame] | 523 | ret = qemu_strtoi64(token->str, NULL, 10, &value); |
| 524 | if (!ret) { |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 525 | return QOBJECT(qnum_from_int(value)); |
Michael Roth | 3d5b3ec | 2013-05-10 17:46:05 -0500 | [diff] [blame] | 526 | } |
Marc-André Lureau | 2bc7cfe | 2017-06-07 20:36:02 +0400 | [diff] [blame] | 527 | assert(ret == -ERANGE); |
| 528 | |
| 529 | if (token->str[0] != '-') { |
| 530 | ret = qemu_strtou64(token->str, NULL, 10, &uvalue); |
| 531 | if (!ret) { |
| 532 | return QOBJECT(qnum_from_uint(uvalue)); |
| 533 | } |
| 534 | assert(ret == -ERANGE); |
| 535 | } |
Michael Roth | 3d5b3ec | 2013-05-10 17:46:05 -0500 | [diff] [blame] | 536 | /* fall through to JSON_FLOAT */ |
| 537 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 538 | case JSON_FLOAT: |
Eric Blake | 6e8e5cb | 2016-01-29 06:48:37 -0700 | [diff] [blame] | 539 | /* FIXME dependent on locale; a pervasive issue in QEMU */ |
| 540 | /* FIXME our lexer matches RFC 7159 in forbidding Inf or NaN, |
| 541 | * but those might be useful extensions beyond JSON */ |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 542 | return QOBJECT(qnum_from_double(strtod(token->str, NULL))); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 543 | default: |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 544 | abort(); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 545 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 546 | } |
| 547 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 548 | static QObject *parse_value(JSONParserContext *ctxt, va_list *ap) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 549 | { |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 550 | JSONToken *token; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 551 | |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 552 | token = parser_context_peek_token(ctxt); |
| 553 | if (token == NULL) { |
| 554 | parse_error(ctxt, NULL, "premature EOI"); |
| 555 | return NULL; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 556 | } |
| 557 | |
Paolo Bonzini | 9bada89 | 2015-11-25 22:23:32 +0100 | [diff] [blame] | 558 | switch (token->type) { |
Markus Armbruster | d538b25 | 2015-11-25 22:23:30 +0100 | [diff] [blame] | 559 | case JSON_LCURLY: |
| 560 | return parse_object(ctxt, ap); |
| 561 | case JSON_LSQUARE: |
| 562 | return parse_array(ctxt, ap); |
| 563 | case JSON_ESCAPE: |
| 564 | return parse_escape(ctxt, ap); |
| 565 | case JSON_INTEGER: |
| 566 | case JSON_FLOAT: |
| 567 | case JSON_STRING: |
| 568 | return parse_literal(ctxt); |
| 569 | case JSON_KEYWORD: |
| 570 | return parse_keyword(ctxt); |
| 571 | default: |
| 572 | parse_error(ctxt, token, "expecting value"); |
| 573 | return NULL; |
| 574 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 575 | } |
| 576 | |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 577 | QObject *json_parser_parse(GQueue *tokens, va_list *ap) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 578 | { |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 579 | return json_parser_parse_err(tokens, ap, NULL); |
| 580 | } |
| 581 | |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 582 | QObject *json_parser_parse_err(GQueue *tokens, va_list *ap, Error **errp) |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 583 | { |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 584 | JSONParserContext *ctxt = parser_context_new(tokens); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 585 | QObject *result; |
| 586 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 587 | if (!ctxt) { |
Michael Roth | c1990eb | 2011-06-01 12:15:00 -0500 | [diff] [blame] | 588 | return NULL; |
| 589 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 590 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 591 | result = parse_value(ctxt, ap); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 592 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 593 | error_propagate(errp, ctxt->err); |
| 594 | |
| 595 | parser_context_free(ctxt); |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 596 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 597 | return result; |
| 598 | } |