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