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