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