Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 1 | /* |
| 2 | * JSON Parser |
| 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 | |
Amos Kong | c96c84a | 2010-03-24 23:12:05 +0800 | [diff] [blame] | 14 | #include <stdarg.h> |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 15 | |
| 16 | #include "qemu-common.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 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" |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 25 | |
| 26 | typedef struct JSONParserContext |
| 27 | { |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 28 | Error *err; |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 29 | struct { |
| 30 | QObject **buf; |
| 31 | size_t pos; |
| 32 | size_t count; |
| 33 | } tokens; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 34 | } JSONParserContext; |
| 35 | |
| 36 | #define BUG_ON(cond) assert(!(cond)) |
| 37 | |
| 38 | /** |
| 39 | * TODO |
| 40 | * |
| 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 |
| 45 | */ |
| 46 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 47 | static QObject *parse_value(JSONParserContext *ctxt, va_list *ap); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Token manipulators |
| 51 | * |
| 52 | * tokens are dictionaries that contain a type, a string value, and geometry information |
| 53 | * about a token identified by the lexer. These are routines that make working with |
| 54 | * these objects a bit easier. |
| 55 | */ |
| 56 | static const char *token_get_value(QObject *obj) |
| 57 | { |
| 58 | return qdict_get_str(qobject_to_qdict(obj), "token"); |
| 59 | } |
| 60 | |
| 61 | static JSONTokenType token_get_type(QObject *obj) |
| 62 | { |
| 63 | return qdict_get_int(qobject_to_qdict(obj), "type"); |
| 64 | } |
| 65 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 66 | /** |
| 67 | * Error handler |
| 68 | */ |
Stefan Weil | 8b7968f | 2010-09-23 21:28:05 +0200 | [diff] [blame] | 69 | static void GCC_FMT_ATTR(3, 4) parse_error(JSONParserContext *ctxt, |
| 70 | QObject *token, const char *msg, ...) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 71 | { |
Amos Kong | c96c84a | 2010-03-24 23:12:05 +0800 | [diff] [blame] | 72 | va_list ap; |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 73 | char message[1024]; |
Amos Kong | c96c84a | 2010-03-24 23:12:05 +0800 | [diff] [blame] | 74 | va_start(ap, msg); |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 75 | vsnprintf(message, sizeof(message), msg, ap); |
Amos Kong | c96c84a | 2010-03-24 23:12:05 +0800 | [diff] [blame] | 76 | va_end(ap); |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 77 | if (ctxt->err) { |
| 78 | error_free(ctxt->err); |
| 79 | ctxt->err = NULL; |
| 80 | } |
Cole Robinson | f231b88 | 2014-03-21 19:42:26 -0400 | [diff] [blame] | 81 | error_setg(&ctxt->err, "JSON parse error, %s", message); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
| 85 | * String helpers |
| 86 | * |
| 87 | * These helpers are used to unescape strings. |
| 88 | */ |
| 89 | static void wchar_to_utf8(uint16_t wchar, char *buffer, size_t buffer_length) |
| 90 | { |
| 91 | if (wchar <= 0x007F) { |
| 92 | BUG_ON(buffer_length < 2); |
| 93 | |
| 94 | buffer[0] = wchar & 0x7F; |
| 95 | buffer[1] = 0; |
| 96 | } else if (wchar <= 0x07FF) { |
| 97 | BUG_ON(buffer_length < 3); |
| 98 | |
| 99 | buffer[0] = 0xC0 | ((wchar >> 6) & 0x1F); |
| 100 | buffer[1] = 0x80 | (wchar & 0x3F); |
| 101 | buffer[2] = 0; |
| 102 | } else { |
| 103 | BUG_ON(buffer_length < 4); |
| 104 | |
| 105 | buffer[0] = 0xE0 | ((wchar >> 12) & 0x0F); |
| 106 | buffer[1] = 0x80 | ((wchar >> 6) & 0x3F); |
| 107 | buffer[2] = 0x80 | (wchar & 0x3F); |
| 108 | buffer[3] = 0; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | static int hex2decimal(char ch) |
| 113 | { |
| 114 | if (ch >= '0' && ch <= '9') { |
| 115 | return (ch - '0'); |
| 116 | } else if (ch >= 'a' && ch <= 'f') { |
| 117 | return 10 + (ch - 'a'); |
| 118 | } else if (ch >= 'A' && ch <= 'F') { |
| 119 | return 10 + (ch - 'A'); |
| 120 | } |
| 121 | |
| 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * parse_string(): Parse a json string and return a QObject |
| 127 | * |
| 128 | * string |
| 129 | * "" |
| 130 | * " chars " |
| 131 | * chars |
| 132 | * char |
| 133 | * char chars |
| 134 | * char |
| 135 | * any-Unicode-character- |
| 136 | * except-"-or-\-or- |
| 137 | * control-character |
| 138 | * \" |
| 139 | * \\ |
| 140 | * \/ |
| 141 | * \b |
| 142 | * \f |
| 143 | * \n |
| 144 | * \r |
| 145 | * \t |
| 146 | * \u four-hex-digits |
| 147 | */ |
| 148 | static QString *qstring_from_escaped_str(JSONParserContext *ctxt, QObject *token) |
| 149 | { |
| 150 | const char *ptr = token_get_value(token); |
| 151 | QString *str; |
| 152 | int double_quote = 1; |
| 153 | |
| 154 | if (*ptr == '"') { |
| 155 | double_quote = 1; |
| 156 | } else { |
| 157 | double_quote = 0; |
| 158 | } |
| 159 | ptr++; |
| 160 | |
| 161 | str = qstring_new(); |
| 162 | while (*ptr && |
| 163 | ((double_quote && *ptr != '"') || (!double_quote && *ptr != '\''))) { |
| 164 | if (*ptr == '\\') { |
| 165 | ptr++; |
| 166 | |
| 167 | switch (*ptr) { |
| 168 | case '"': |
| 169 | qstring_append(str, "\""); |
| 170 | ptr++; |
| 171 | break; |
| 172 | case '\'': |
| 173 | qstring_append(str, "'"); |
| 174 | ptr++; |
| 175 | break; |
| 176 | case '\\': |
| 177 | qstring_append(str, "\\"); |
| 178 | ptr++; |
| 179 | break; |
| 180 | case '/': |
| 181 | qstring_append(str, "/"); |
| 182 | ptr++; |
| 183 | break; |
| 184 | case 'b': |
| 185 | qstring_append(str, "\b"); |
| 186 | ptr++; |
| 187 | break; |
Luiz Capitulino | bd03269 | 2010-05-19 17:06:15 -0300 | [diff] [blame] | 188 | case 'f': |
| 189 | qstring_append(str, "\f"); |
| 190 | ptr++; |
| 191 | break; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 192 | case 'n': |
| 193 | qstring_append(str, "\n"); |
| 194 | ptr++; |
| 195 | break; |
| 196 | case 'r': |
| 197 | qstring_append(str, "\r"); |
| 198 | ptr++; |
| 199 | break; |
| 200 | case 't': |
| 201 | qstring_append(str, "\t"); |
| 202 | ptr++; |
| 203 | break; |
| 204 | case 'u': { |
| 205 | uint16_t unicode_char = 0; |
| 206 | char utf8_char[4]; |
| 207 | int i = 0; |
| 208 | |
| 209 | ptr++; |
| 210 | |
| 211 | for (i = 0; i < 4; i++) { |
| 212 | if (qemu_isxdigit(*ptr)) { |
| 213 | unicode_char |= hex2decimal(*ptr) << ((3 - i) * 4); |
| 214 | } else { |
| 215 | parse_error(ctxt, token, |
| 216 | "invalid hex escape sequence in string"); |
| 217 | goto out; |
| 218 | } |
| 219 | ptr++; |
| 220 | } |
| 221 | |
| 222 | wchar_to_utf8(unicode_char, utf8_char, sizeof(utf8_char)); |
| 223 | qstring_append(str, utf8_char); |
| 224 | } break; |
| 225 | default: |
| 226 | parse_error(ctxt, token, "invalid escape sequence in string"); |
| 227 | goto out; |
| 228 | } |
| 229 | } else { |
| 230 | char dummy[2]; |
| 231 | |
| 232 | dummy[0] = *ptr++; |
| 233 | dummy[1] = 0; |
| 234 | |
| 235 | qstring_append(str, dummy); |
| 236 | } |
| 237 | } |
| 238 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 239 | return str; |
| 240 | |
| 241 | out: |
| 242 | QDECREF(str); |
| 243 | return NULL; |
| 244 | } |
| 245 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 246 | static QObject *parser_context_pop_token(JSONParserContext *ctxt) |
| 247 | { |
| 248 | QObject *token; |
| 249 | g_assert(ctxt->tokens.pos < ctxt->tokens.count); |
| 250 | token = ctxt->tokens.buf[ctxt->tokens.pos]; |
| 251 | ctxt->tokens.pos++; |
| 252 | return token; |
| 253 | } |
| 254 | |
| 255 | /* Note: parser_context_{peek|pop}_token do not increment the |
| 256 | * token object's refcount. In both cases the references will continue |
| 257 | * to be tracked and cleaned up in parser_context_free(), so do not |
| 258 | * attempt to free the token object. |
| 259 | */ |
| 260 | static QObject *parser_context_peek_token(JSONParserContext *ctxt) |
| 261 | { |
| 262 | QObject *token; |
| 263 | g_assert(ctxt->tokens.pos < ctxt->tokens.count); |
| 264 | token = ctxt->tokens.buf[ctxt->tokens.pos]; |
| 265 | return token; |
| 266 | } |
| 267 | |
| 268 | static JSONParserContext parser_context_save(JSONParserContext *ctxt) |
| 269 | { |
| 270 | JSONParserContext saved_ctxt = {0}; |
| 271 | saved_ctxt.tokens.pos = ctxt->tokens.pos; |
| 272 | saved_ctxt.tokens.count = ctxt->tokens.count; |
| 273 | saved_ctxt.tokens.buf = ctxt->tokens.buf; |
| 274 | return saved_ctxt; |
| 275 | } |
| 276 | |
| 277 | static void parser_context_restore(JSONParserContext *ctxt, |
| 278 | JSONParserContext saved_ctxt) |
| 279 | { |
| 280 | ctxt->tokens.pos = saved_ctxt.tokens.pos; |
| 281 | ctxt->tokens.count = saved_ctxt.tokens.count; |
| 282 | ctxt->tokens.buf = saved_ctxt.tokens.buf; |
| 283 | } |
| 284 | |
| 285 | static void tokens_append_from_iter(QObject *obj, void *opaque) |
| 286 | { |
| 287 | JSONParserContext *ctxt = opaque; |
| 288 | g_assert(ctxt->tokens.pos < ctxt->tokens.count); |
| 289 | ctxt->tokens.buf[ctxt->tokens.pos++] = obj; |
| 290 | qobject_incref(obj); |
| 291 | } |
| 292 | |
| 293 | static JSONParserContext *parser_context_new(QList *tokens) |
| 294 | { |
| 295 | JSONParserContext *ctxt; |
| 296 | size_t count; |
| 297 | |
| 298 | if (!tokens) { |
| 299 | return NULL; |
| 300 | } |
| 301 | |
| 302 | count = qlist_size(tokens); |
| 303 | if (count == 0) { |
| 304 | return NULL; |
| 305 | } |
| 306 | |
| 307 | ctxt = g_malloc0(sizeof(JSONParserContext)); |
| 308 | ctxt->tokens.pos = 0; |
| 309 | ctxt->tokens.count = count; |
| 310 | ctxt->tokens.buf = g_malloc(count * sizeof(QObject *)); |
| 311 | qlist_iter(tokens, tokens_append_from_iter, ctxt); |
| 312 | ctxt->tokens.pos = 0; |
| 313 | |
| 314 | return ctxt; |
| 315 | } |
| 316 | |
| 317 | /* to support error propagation, ctxt->err must be freed separately */ |
| 318 | static void parser_context_free(JSONParserContext *ctxt) |
| 319 | { |
| 320 | int i; |
| 321 | if (ctxt) { |
| 322 | for (i = 0; i < ctxt->tokens.count; i++) { |
| 323 | qobject_decref(ctxt->tokens.buf[i]); |
| 324 | } |
| 325 | g_free(ctxt->tokens.buf); |
| 326 | g_free(ctxt); |
| 327 | } |
| 328 | } |
| 329 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 330 | /** |
| 331 | * Parsing rules |
| 332 | */ |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 333 | static int parse_pair(JSONParserContext *ctxt, QDict *dict, va_list *ap) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 334 | { |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 335 | QObject *key = NULL, *token = NULL, *value, *peek; |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 336 | JSONParserContext saved_ctxt = parser_context_save(ctxt); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 337 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 338 | peek = parser_context_peek_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 339 | if (peek == NULL) { |
| 340 | parse_error(ctxt, NULL, "premature EOI"); |
| 341 | goto out; |
| 342 | } |
| 343 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 344 | key = parse_value(ctxt, ap); |
Kevin Wolf | d758d90 | 2010-02-24 16:17:58 +0100 | [diff] [blame] | 345 | if (!key || qobject_type(key) != QTYPE_QSTRING) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 346 | parse_error(ctxt, peek, "key is not a string in object"); |
| 347 | goto out; |
| 348 | } |
| 349 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 350 | token = parser_context_pop_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 351 | if (token == NULL) { |
| 352 | parse_error(ctxt, NULL, "premature EOI"); |
| 353 | goto out; |
| 354 | } |
| 355 | |
Markus Armbruster | c546166 | 2015-11-25 22:23:26 +0100 | [diff] [blame] | 356 | if (token_get_type(token) != JSON_COLON) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 357 | parse_error(ctxt, token, "missing : in object pair"); |
| 358 | goto out; |
| 359 | } |
| 360 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 361 | value = parse_value(ctxt, ap); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 362 | if (value == NULL) { |
| 363 | parse_error(ctxt, token, "Missing value in dict"); |
| 364 | goto out; |
| 365 | } |
| 366 | |
| 367 | qdict_put_obj(dict, qstring_get_str(qobject_to_qstring(key)), value); |
| 368 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 369 | qobject_decref(key); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 370 | |
| 371 | return 0; |
| 372 | |
| 373 | out: |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 374 | parser_context_restore(ctxt, saved_ctxt); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 375 | qobject_decref(key); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 376 | |
| 377 | return -1; |
| 378 | } |
| 379 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 380 | static QObject *parse_object(JSONParserContext *ctxt, va_list *ap) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 381 | { |
| 382 | QDict *dict = NULL; |
| 383 | QObject *token, *peek; |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 384 | JSONParserContext saved_ctxt = parser_context_save(ctxt); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 385 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 386 | token = parser_context_pop_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 387 | if (token == NULL) { |
| 388 | goto out; |
| 389 | } |
| 390 | |
Markus Armbruster | c546166 | 2015-11-25 22:23:26 +0100 | [diff] [blame] | 391 | if (token_get_type(token) != JSON_LCURLY) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 392 | goto out; |
| 393 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 394 | |
| 395 | dict = qdict_new(); |
| 396 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 397 | peek = parser_context_peek_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 398 | if (peek == NULL) { |
| 399 | parse_error(ctxt, NULL, "premature EOI"); |
| 400 | goto out; |
| 401 | } |
| 402 | |
Markus Armbruster | c546166 | 2015-11-25 22:23:26 +0100 | [diff] [blame] | 403 | if (token_get_type(peek) != JSON_RCURLY) { |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 404 | if (parse_pair(ctxt, dict, ap) == -1) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 405 | goto out; |
| 406 | } |
| 407 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 408 | token = parser_context_pop_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 409 | if (token == NULL) { |
| 410 | parse_error(ctxt, NULL, "premature EOI"); |
| 411 | goto out; |
| 412 | } |
| 413 | |
Markus Armbruster | c546166 | 2015-11-25 22:23:26 +0100 | [diff] [blame] | 414 | while (token_get_type(token) != JSON_RCURLY) { |
| 415 | if (token_get_type(token) != JSON_COMMA) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 416 | parse_error(ctxt, token, "expected separator in dict"); |
| 417 | goto out; |
| 418 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 419 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 420 | if (parse_pair(ctxt, dict, ap) == -1) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 421 | goto out; |
| 422 | } |
| 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(dict); |
| 435 | |
| 436 | out: |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 437 | parser_context_restore(ctxt, saved_ctxt); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 438 | QDECREF(dict); |
| 439 | return NULL; |
| 440 | } |
| 441 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 442 | static QObject *parse_array(JSONParserContext *ctxt, va_list *ap) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 443 | { |
| 444 | QList *list = NULL; |
| 445 | QObject *token, *peek; |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 446 | JSONParserContext saved_ctxt = parser_context_save(ctxt); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 447 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 448 | token = parser_context_pop_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 449 | if (token == NULL) { |
| 450 | goto out; |
| 451 | } |
| 452 | |
Markus Armbruster | c546166 | 2015-11-25 22:23:26 +0100 | [diff] [blame] | 453 | if (token_get_type(token) != JSON_LSQUARE) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 454 | goto out; |
| 455 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 456 | |
| 457 | list = qlist_new(); |
| 458 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 459 | peek = parser_context_peek_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 460 | if (peek == NULL) { |
| 461 | parse_error(ctxt, NULL, "premature EOI"); |
| 462 | goto out; |
| 463 | } |
| 464 | |
Markus Armbruster | c546166 | 2015-11-25 22:23:26 +0100 | [diff] [blame] | 465 | if (token_get_type(peek) != JSON_RSQUARE) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 466 | QObject *obj; |
| 467 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 468 | obj = parse_value(ctxt, ap); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 469 | if (obj == NULL) { |
| 470 | parse_error(ctxt, token, "expecting value"); |
| 471 | goto out; |
| 472 | } |
| 473 | |
| 474 | qlist_append_obj(list, obj); |
| 475 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 476 | token = parser_context_pop_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 477 | if (token == NULL) { |
| 478 | parse_error(ctxt, NULL, "premature EOI"); |
| 479 | goto out; |
| 480 | } |
| 481 | |
Markus Armbruster | c546166 | 2015-11-25 22:23:26 +0100 | [diff] [blame] | 482 | while (token_get_type(token) != JSON_RSQUARE) { |
| 483 | if (token_get_type(token) != JSON_COMMA) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 484 | parse_error(ctxt, token, "expected separator in list"); |
| 485 | goto out; |
| 486 | } |
| 487 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 488 | obj = parse_value(ctxt, ap); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 489 | if (obj == NULL) { |
| 490 | parse_error(ctxt, token, "expecting value"); |
| 491 | goto out; |
| 492 | } |
| 493 | |
| 494 | qlist_append_obj(list, obj); |
| 495 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 496 | token = parser_context_pop_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 497 | if (token == NULL) { |
| 498 | parse_error(ctxt, NULL, "premature EOI"); |
| 499 | goto out; |
| 500 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 501 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 502 | } else { |
Gonglei | a491af4 | 2014-06-10 17:20:24 +0800 | [diff] [blame] | 503 | (void)parser_context_pop_token(ctxt); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 504 | } |
| 505 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 506 | return QOBJECT(list); |
| 507 | |
| 508 | out: |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 509 | parser_context_restore(ctxt, saved_ctxt); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 510 | QDECREF(list); |
| 511 | return NULL; |
| 512 | } |
| 513 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 514 | static QObject *parse_keyword(JSONParserContext *ctxt) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 515 | { |
| 516 | QObject *token, *ret; |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 517 | JSONParserContext saved_ctxt = parser_context_save(ctxt); |
Markus Armbruster | 50e2a46 | 2015-11-25 22:23:27 +0100 | [diff] [blame] | 518 | const char *val; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 519 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 520 | token = parser_context_pop_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 521 | if (token == NULL) { |
| 522 | goto out; |
| 523 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 524 | |
| 525 | if (token_get_type(token) != JSON_KEYWORD) { |
| 526 | goto out; |
| 527 | } |
| 528 | |
Markus Armbruster | 50e2a46 | 2015-11-25 22:23:27 +0100 | [diff] [blame] | 529 | val = token_get_value(token); |
| 530 | |
| 531 | if (!strcmp(val, "true")) { |
Eric Blake | fc48ffc | 2015-05-15 16:24:59 -0600 | [diff] [blame] | 532 | ret = QOBJECT(qbool_from_bool(true)); |
Markus Armbruster | 50e2a46 | 2015-11-25 22:23:27 +0100 | [diff] [blame] | 533 | } else if (!strcmp(val, "false")) { |
Eric Blake | fc48ffc | 2015-05-15 16:24:59 -0600 | [diff] [blame] | 534 | ret = QOBJECT(qbool_from_bool(false)); |
Markus Armbruster | 50e2a46 | 2015-11-25 22:23:27 +0100 | [diff] [blame] | 535 | } else if (!strcmp(val, "null")) { |
Eric Blake | e549e71 | 2015-04-29 15:35:06 -0600 | [diff] [blame] | 536 | ret = qnull(); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 537 | } else { |
Markus Armbruster | 50e2a46 | 2015-11-25 22:23:27 +0100 | [diff] [blame] | 538 | parse_error(ctxt, token, "invalid keyword '%s'", val); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 539 | goto out; |
| 540 | } |
| 541 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 542 | return ret; |
| 543 | |
| 544 | out: |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 545 | parser_context_restore(ctxt, saved_ctxt); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 546 | |
| 547 | return NULL; |
| 548 | } |
| 549 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 550 | static QObject *parse_escape(JSONParserContext *ctxt, va_list *ap) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 551 | { |
| 552 | QObject *token = NULL, *obj; |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 553 | JSONParserContext saved_ctxt = parser_context_save(ctxt); |
Markus Armbruster | 6b9606f | 2015-11-25 22:23:28 +0100 | [diff] [blame] | 554 | const char *val; |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 555 | |
| 556 | if (ap == NULL) { |
| 557 | goto out; |
| 558 | } |
| 559 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 560 | token = parser_context_pop_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 561 | if (token == NULL) { |
| 562 | goto out; |
| 563 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 564 | |
Markus Armbruster | 6b9606f | 2015-11-25 22:23:28 +0100 | [diff] [blame] | 565 | if (token_get_type(token) != JSON_ESCAPE) { |
| 566 | goto out; |
| 567 | } |
| 568 | |
| 569 | val = token_get_value(token); |
| 570 | |
| 571 | if (!strcmp(val, "%p")) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 572 | obj = va_arg(*ap, QObject *); |
Markus Armbruster | 6b9606f | 2015-11-25 22:23:28 +0100 | [diff] [blame] | 573 | } else if (!strcmp(val, "%i")) { |
Eric Blake | fc48ffc | 2015-05-15 16:24:59 -0600 | [diff] [blame] | 574 | obj = QOBJECT(qbool_from_bool(va_arg(*ap, int))); |
Markus Armbruster | 6b9606f | 2015-11-25 22:23:28 +0100 | [diff] [blame] | 575 | } else if (!strcmp(val, "%d")) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 576 | obj = QOBJECT(qint_from_int(va_arg(*ap, int))); |
Markus Armbruster | 6b9606f | 2015-11-25 22:23:28 +0100 | [diff] [blame] | 577 | } else if (!strcmp(val, "%ld")) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 578 | obj = QOBJECT(qint_from_int(va_arg(*ap, long))); |
Markus Armbruster | 6b9606f | 2015-11-25 22:23:28 +0100 | [diff] [blame] | 579 | } else if (!strcmp(val, "%lld") || |
| 580 | !strcmp(val, "%I64d")) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 581 | obj = QOBJECT(qint_from_int(va_arg(*ap, long long))); |
Markus Armbruster | 6b9606f | 2015-11-25 22:23:28 +0100 | [diff] [blame] | 582 | } else if (!strcmp(val, "%s")) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 583 | obj = QOBJECT(qstring_from_str(va_arg(*ap, const char *))); |
Markus Armbruster | 6b9606f | 2015-11-25 22:23:28 +0100 | [diff] [blame] | 584 | } else if (!strcmp(val, "%f")) { |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 585 | obj = QOBJECT(qfloat_from_double(va_arg(*ap, double))); |
| 586 | } else { |
| 587 | goto out; |
| 588 | } |
| 589 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 590 | return obj; |
| 591 | |
| 592 | out: |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 593 | parser_context_restore(ctxt, saved_ctxt); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 594 | |
| 595 | return NULL; |
| 596 | } |
| 597 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 598 | static QObject *parse_literal(JSONParserContext *ctxt) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 599 | { |
| 600 | QObject *token, *obj; |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 601 | JSONParserContext saved_ctxt = parser_context_save(ctxt); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 602 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 603 | token = parser_context_pop_token(ctxt); |
Anthony Liguori | 11e8a46 | 2011-06-01 12:14:55 -0500 | [diff] [blame] | 604 | if (token == NULL) { |
| 605 | goto out; |
| 606 | } |
| 607 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 608 | switch (token_get_type(token)) { |
| 609 | case JSON_STRING: |
| 610 | obj = QOBJECT(qstring_from_escaped_str(ctxt, token)); |
| 611 | break; |
Michael Roth | 3d5b3ec | 2013-05-10 17:46:05 -0500 | [diff] [blame] | 612 | case JSON_INTEGER: { |
| 613 | /* A possibility exists that this is a whole-valued float where the |
| 614 | * fractional part was left out due to being 0 (.0). It's not a big |
| 615 | * deal to treat these as ints in the parser, so long as users of the |
| 616 | * resulting QObject know to expect a QInt in place of a QFloat in |
| 617 | * cases like these. |
| 618 | * |
| 619 | * However, in some cases these values will overflow/underflow a |
| 620 | * QInt/int64 container, thus we should assume these are to be handled |
| 621 | * as QFloats/doubles rather than silently changing their values. |
| 622 | * |
| 623 | * strtoll() indicates these instances by setting errno to ERANGE |
| 624 | */ |
| 625 | int64_t value; |
| 626 | |
| 627 | errno = 0; /* strtoll doesn't set errno on success */ |
| 628 | value = strtoll(token_get_value(token), NULL, 10); |
| 629 | if (errno != ERANGE) { |
| 630 | obj = QOBJECT(qint_from_int(value)); |
| 631 | break; |
| 632 | } |
| 633 | /* fall through to JSON_FLOAT */ |
| 634 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 635 | case JSON_FLOAT: |
| 636 | /* FIXME dependent on locale */ |
| 637 | obj = QOBJECT(qfloat_from_double(strtod(token_get_value(token), NULL))); |
| 638 | break; |
| 639 | default: |
| 640 | goto out; |
| 641 | } |
| 642 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 643 | return obj; |
| 644 | |
| 645 | out: |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 646 | parser_context_restore(ctxt, saved_ctxt); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 647 | |
| 648 | return NULL; |
| 649 | } |
| 650 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 651 | static QObject *parse_value(JSONParserContext *ctxt, va_list *ap) |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 652 | { |
| 653 | QObject *obj; |
| 654 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 655 | obj = parse_object(ctxt, ap); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 656 | if (obj == NULL) { |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 657 | obj = parse_array(ctxt, ap); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 658 | } |
| 659 | if (obj == NULL) { |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 660 | obj = parse_escape(ctxt, ap); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 661 | } |
| 662 | if (obj == NULL) { |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 663 | obj = parse_keyword(ctxt); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 664 | } |
| 665 | if (obj == NULL) { |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 666 | obj = parse_literal(ctxt); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | return obj; |
| 670 | } |
| 671 | |
| 672 | QObject *json_parser_parse(QList *tokens, va_list *ap) |
| 673 | { |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 674 | return json_parser_parse_err(tokens, ap, NULL); |
| 675 | } |
| 676 | |
| 677 | QObject *json_parser_parse_err(QList *tokens, va_list *ap, Error **errp) |
| 678 | { |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 679 | JSONParserContext *ctxt = parser_context_new(tokens); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 680 | QObject *result; |
| 681 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 682 | if (!ctxt) { |
Michael Roth | c1990eb | 2011-06-01 12:15:00 -0500 | [diff] [blame] | 683 | return NULL; |
| 684 | } |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 685 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 686 | result = parse_value(ctxt, ap); |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 687 | |
Michael Roth | 65c0f1e | 2012-08-15 13:45:43 -0500 | [diff] [blame] | 688 | error_propagate(errp, ctxt->err); |
| 689 | |
| 690 | parser_context_free(ctxt); |
Anthony Liguori | ef749d0 | 2011-06-01 12:14:50 -0500 | [diff] [blame] | 691 | |
Anthony Liguori | 4a5fcab | 2009-11-11 10:39:23 -0600 | [diff] [blame] | 692 | return result; |
| 693 | } |