master
c 671 lines 21.2 KB
Raw
1 /*
2 * JSON Parser
3 *
4 * Copyright IBM, Corp. 2009
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
11 *
12 */
13
14 #include "qemu/osdep.h"
15 #include "qemu/ctype.h"
16 #include "qemu/cutils.h"
17 #include "qemu/unicode.h"
18 #include "qapi/error.h"
19 #include "qobject/qbool.h"
20 #include "qobject/qdict.h"
21 #include "qobject/qlist.h"
22 #include "qobject/qnull.h"
23 #include "qobject/qnum.h"
24 #include "qobject/qstring.h"
25 #include "json-parser-int.h"
26
27 /*
28 * The JSON parser is a push parser, returning a completed top-level
29 * object, an error, or NULL (if the object is incomplete and no error
30 * happened) after every token. Therefore it has an explicit
31 * representation of its parser stack; each stack entry consists of a
32 * parser state and a QObject:
33 * - a QList, for an array that is being added to
34 * - a QDict, for a dictionary that is being added to
35 * - a QString, for the key of the next pair that will be added to a QDict
36 *
37 * The stack represents an arbitrary nesting of arrays and dictionaries
38 * (whose next key has been parsed); it can also have a dictionary whose
39 * next key has not been parsed, but that can only happen at the top level.
40 * Because of this, the stack contents are always of the form
41 * "(QList | QDict QString)* QDict?".
42 *
43 * An empty stack represents the beginning of the parsing process, with
44 * start state BEFORE_VALUE.
45 */
46
47 typedef enum JSONParserState {
48 AFTER_LCURLY,
49 AFTER_LSQUARE,
50 BEFORE_KEY,
51 BEFORE_VALUE,
52 END_OF_KEY,
53 END_OF_VALUE,
54 } JSONParserState;
55
56 typedef struct JSONParserStackEntry {
57 /*
58 * State when the container is completed or, for the top of the stack,
59 * entry state for the next token.
60 */
61 JSONParserState state;
62
63 /*
64 * A QString with the last parsed key, or a QList/QDict for the current
65 * container.
66 */
67 QObject *partial;
68 } JSONParserStackEntry;
69
70 /*
71 * This is the JSON grammar that's parsed, with the state transition and
72 * action at each point of the grammar. While this is not a formal
73 * description, "-> action" represents the pseudocode of the action
74 * and "-> STATE" sets the top stack entry's state to STATE.
75 *
76 * The state alone is enough to tell you what to parse; the state plus
77 * the type of the top of stack tells you which action to take.
78 *
79 * // The initial state is BEFORE_VALUE.
80 * input := value -> END_OF_VALUE -> return parsed value
81 * (input | END_OF_INPUT)
82 *
83 * // entered on BEFORE_VALUE; after any of these rules are processed, the
84 * // parser has completed a QObject and is in the END_OF_VALUE state.
85 * //
86 * // When the parser reaches the END_OF_VALUE state, it examines the
87 * // top of the stack to see if it's coming from "input" (stack empty),
88 * // "array_items" (TOS is a QList) or "dict_pairs" (TOS is a QString; the
89 * // item below will be a QDict). It then proceeds with the corresponding
90 * // actions, which will be one of:
91 * // - return parsed value
92 * // - add value to QList
93 * // - pop QString with the key, add key/value to the QDict
94 * value := literal -> END_OF_VALUE
95 * | '[' -> push empty QList -> AFTER_LSQUARE
96 * after_lsquare -> END_OF_VALUE
97 * | '{' -> push empty QDict -> AFTER_LCURLY
98 * after_lcurly -> END_OF_VALUE
99 *
100 * // non-recursive values, entered on BEFORE_VALUE
101 * literal := INTEGER -> END_OF_VALUE
102 * | FLOAT -> END_OF_VALUE
103 * | KEYWORD -> END_OF_VALUE
104 * | STRING -> END_OF_VALUE
105 * | INTERP -> END_OF_VALUE
106 *
107 * // entered on AFTER_LSQUARE
108 * after_lsquare := ']' -> pop completed QList -> END_OF_VALUE
109 * | ϵ -> BEFORE_VALUE
110 * array_items -> END_OF_VALUE
111 *
112 * // entered on BEFORE_VALUE, with TOS being a QList
113 * array_items := value -> add value to QList -> END_OF_VALUE
114 * (']' -> pop completed QList -> END_OF_VALUE
115 * | ',' -> BEFORE_VALUE
116 * array_items) -> END_OF_VALUE
117 *
118 * // entered on AFTER_LCURLY
119 * after_lcurly := '}' -> pop completed QDict -> END_OF_VALUE
120 * | ϵ -> BEFORE_KEY
121 * dict_pairs -> END_OF_VALUE
122 *
123 * // entered on BEFORE_KEY, with TOS being a QDict
124 * dict_pairs := (STRING | INTERP) -> push QString -> END_OF_KEY
125 * ':' -> BEFORE_VALUE
126 * value -> pop QString + add pair to QDict -> END_OF_VALUE
127 * ('}' -> pop completed QDict -> END_OF_VALUE
128 * | ',' -> BEFORE_KEY
129 * dict_pairs) -> END_OF_VALUE
130 *
131 * Parse errors ignore the token. json_parser_reset() can be
132 * called to restart parsing from scratch, with an empty stack.
133 */
134
135 #define BUG_ON(cond) assert(!(cond))
136
137 static inline JSONParserStackEntry *current_entry(JSONParserContext *ctxt)
138 {
139 return g_queue_peek_tail(ctxt->stack);
140 }
141
142 static void push_entry(JSONParserContext *ctxt, QObject *partial,
143 JSONParserState state)
144 {
145 JSONParserStackEntry *entry = g_new(JSONParserStackEntry, 1);
146 entry->partial = partial;
147 entry->state = state;
148 g_queue_push_tail(ctxt->stack, entry);
149 }
150
151 /* Drop the top entry and return the new top entry. */
152 static JSONParserStackEntry *pop_entry(JSONParserContext *ctxt)
153 {
154 JSONParserStackEntry *entry = g_queue_pop_tail(ctxt->stack);
155 g_free(entry);
156 return current_entry(ctxt);
157 }
158
159 /**
160 * Error handler
161 */
162 static void G_GNUC_PRINTF(3, 4) parse_error(JSONParserContext *ctxt,
163 const JSONToken *token,
164 const char *msg, ...)
165 {
166 va_list ap;
167 char message[1024];
168
169 if (ctxt->err) {
170 return;
171 }
172 va_start(ap, msg);
173 vsnprintf(message, sizeof(message), msg, ap);
174 va_end(ap);
175 error_setg(&ctxt->err, "%d:%d: JSON parse error, %s",
176 token->y, token->x, message);
177 }
178
179 static int cvt4hex(const char *s)
180 {
181 int cp, i;
182
183 cp = 0;
184 for (i = 0; i < 4; i++) {
185 if (!qemu_isxdigit(s[i])) {
186 return -1;
187 }
188 cp <<= 4;
189 if (s[i] >= '0' && s[i] <= '9') {
190 cp |= s[i] - '0';
191 } else if (s[i] >= 'a' && s[i] <= 'f') {
192 cp |= 10 + s[i] - 'a';
193 } else if (s[i] >= 'A' && s[i] <= 'F') {
194 cp |= 10 + s[i] - 'A';
195 } else {
196 return -1;
197 }
198 }
199 return cp;
200 }
201
202 /**
203 * parse_string(): Parse a JSON string
204 *
205 * From RFC 8259 "The JavaScript Object Notation (JSON) Data
206 * Interchange Format":
207 *
208 * char = unescaped /
209 * escape (
210 * %x22 / ; " quotation mark U+0022
211 * %x5C / ; \ reverse solidus U+005C
212 * %x2F / ; / solidus U+002F
213 * %x62 / ; b backspace U+0008
214 * %x66 / ; f form feed U+000C
215 * %x6E / ; n line feed U+000A
216 * %x72 / ; r carriage return U+000D
217 * %x74 / ; t tab U+0009
218 * %x75 4HEXDIG ) ; uXXXX U+XXXX
219 * escape = %x5C ; \
220 * quotation-mark = %x22 ; "
221 * unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
222 *
223 * Extensions over RFC 8259:
224 * - Extra escape sequence in strings:
225 * 0x27 (apostrophe) is recognized after escape, too
226 * - Single-quoted strings:
227 * Like double-quoted strings, except they're delimited by %x27
228 * (apostrophe) instead of %x22 (quotation mark), and can't contain
229 * unescaped apostrophe, but can contain unescaped quotation mark.
230 *
231 * Note:
232 * - Encoding is modified UTF-8.
233 * - Invalid Unicode characters are rejected.
234 * - Control characters \x00..\x1F are rejected by the lexer.
235 */
236 static QString *parse_string(JSONParserContext *ctxt, const JSONToken *token)
237 {
238 const char *ptr = token->str;
239 GString *str;
240 char quote;
241 const char *beg;
242 int cp, trailing;
243 char *end;
244 ssize_t len;
245 char utf8_buf[5];
246
247 assert(*ptr == '"' || *ptr == '\'');
248 quote = *ptr++;
249 str = g_string_new(NULL);
250
251 while (*ptr != quote) {
252 assert(*ptr);
253 switch (*ptr) {
254 case '\\':
255 beg = ptr++;
256 switch (*ptr++) {
257 case '"':
258 g_string_append_c(str, '"');
259 break;
260 case '\'':
261 g_string_append_c(str, '\'');
262 break;
263 case '\\':
264 g_string_append_c(str, '\\');
265 break;
266 case '/':
267 g_string_append_c(str, '/');
268 break;
269 case 'b':
270 g_string_append_c(str, '\b');
271 break;
272 case 'f':
273 g_string_append_c(str, '\f');
274 break;
275 case 'n':
276 g_string_append_c(str, '\n');
277 break;
278 case 'r':
279 g_string_append_c(str, '\r');
280 break;
281 case 't':
282 g_string_append_c(str, '\t');
283 break;
284 case 'u':
285 cp = cvt4hex(ptr);
286 ptr += 4;
287
288 /* handle surrogate pairs */
289 if (cp >= 0xD800 && cp <= 0xDBFF
290 && ptr[0] == '\\' && ptr[1] == 'u') {
291 /* leading surrogate followed by \u */
292 cp = 0x10000 + ((cp & 0x3FF) << 10);
293 trailing = cvt4hex(ptr + 2);
294 if (trailing >= 0xDC00 && trailing <= 0xDFFF) {
295 /* followed by trailing surrogate */
296 cp |= trailing & 0x3FF;
297 ptr += 6;
298 } else {
299 cp = -1; /* invalid */
300 }
301 }
302
303 if (mod_utf8_encode(utf8_buf, sizeof(utf8_buf), cp) < 0) {
304 parse_error(ctxt, token,
305 "%.*s is not a valid Unicode character",
306 (int)(ptr - beg), beg);
307 goto out;
308 }
309 g_string_append(str, utf8_buf);
310 break;
311 default:
312 parse_error(ctxt, token, "invalid escape sequence in string");
313 goto out;
314 }
315 break;
316 case '%':
317 if (ctxt->ap) {
318 if (ptr[1] != '%') {
319 parse_error(ctxt, token, "can't interpolate into string");
320 goto out;
321 }
322 ptr++;
323 }
324 /* fall through */
325 default:
326 cp = mod_utf8_codepoint(ptr, 6, &end);
327 if (cp < 0) {
328 parse_error(ctxt, token, "invalid UTF-8 sequence in string");
329 goto out;
330 }
331 ptr = end;
332 len = mod_utf8_encode(utf8_buf, sizeof(utf8_buf), cp);
333 assert(len >= 0);
334 g_string_append(str, utf8_buf);
335 }
336 }
337
338 return qstring_from_gstring(str);
339
340 out:
341 g_string_free(str, true);
342 return NULL;
343 }
344
345 /* Terminals */
346
347 static QObject *parse_keyword(JSONParserContext *ctxt, const JSONToken *token)
348 {
349 assert(token && token->type == JSON_KEYWORD);
350
351 if (!strcmp(token->str, "true")) {
352 return QOBJECT(qbool_from_bool(true));
353 } else if (!strcmp(token->str, "false")) {
354 return QOBJECT(qbool_from_bool(false));
355 } else if (!strcmp(token->str, "null")) {
356 return QOBJECT(qnull());
357 }
358 parse_error(ctxt, token, "invalid keyword '%s'", token->str);
359 return NULL;
360 }
361
362 static QObject *parse_interpolation(JSONParserContext *ctxt,
363 const JSONToken *token)
364 {
365 assert(token && token->type == JSON_INTERP);
366
367 if (!strcmp(token->str, "%p")) {
368 return va_arg(*ctxt->ap, QObject *);
369 } else if (!strcmp(token->str, "%i")) {
370 return QOBJECT(qbool_from_bool(va_arg(*ctxt->ap, int)));
371 } else if (!strcmp(token->str, "%d")) {
372 return QOBJECT(qnum_from_int(va_arg(*ctxt->ap, int)));
373 } else if (!strcmp(token->str, "%ld")) {
374 return QOBJECT(qnum_from_int(va_arg(*ctxt->ap, long)));
375 } else if (!strcmp(token->str, "%lld")) {
376 return QOBJECT(qnum_from_int(va_arg(*ctxt->ap, long long)));
377 } else if (!strcmp(token->str, "%" PRId64)) {
378 return QOBJECT(qnum_from_int(va_arg(*ctxt->ap, int64_t)));
379 } else if (!strcmp(token->str, "%u")) {
380 return QOBJECT(qnum_from_uint(va_arg(*ctxt->ap, unsigned int)));
381 } else if (!strcmp(token->str, "%lu")) {
382 return QOBJECT(qnum_from_uint(va_arg(*ctxt->ap, unsigned long)));
383 } else if (!strcmp(token->str, "%llu")) {
384 return QOBJECT(qnum_from_uint(va_arg(*ctxt->ap, unsigned long long)));
385 } else if (!strcmp(token->str, "%" PRIu64)) {
386 return QOBJECT(qnum_from_uint(va_arg(*ctxt->ap, uint64_t)));
387 } else if (!strcmp(token->str, "%s")) {
388 return QOBJECT(qstring_from_str(va_arg(*ctxt->ap, const char *)));
389 } else if (!strcmp(token->str, "%f")) {
390 return QOBJECT(qnum_from_double(va_arg(*ctxt->ap, double)));
391 }
392 parse_error(ctxt, token, "invalid interpolation '%s'", token->str);
393 return NULL;
394 }
395
396 static QObject *parse_literal(JSONParserContext *ctxt, const JSONToken *token)
397 {
398 assert(token);
399
400 switch (token->type) {
401 case JSON_STRING:
402 return QOBJECT(parse_string(ctxt, token));
403 case JSON_INTEGER: {
404 /*
405 * Represent JSON_INTEGER as QNUM_I64 if possible, else as
406 * QNUM_U64, else as QNUM_DOUBLE. Note that qemu_strtoi64()
407 * and qemu_strtou64() fail with ERANGE when it's not
408 * possible.
409 *
410 * qnum_get_int() will then work for any signed 64-bit
411 * JSON_INTEGER, qnum_get_uint() for any unsigned 64-bit
412 * integer, and qnum_get_double() both for any JSON_INTEGER
413 * and any JSON_FLOAT (with precision loss for integers beyond
414 * 53 bits)
415 */
416 int ret;
417 int64_t value;
418 uint64_t uvalue;
419
420 ret = qemu_strtoi64(token->str, NULL, 10, &value);
421 if (!ret) {
422 return QOBJECT(qnum_from_int(value));
423 }
424 assert(ret == -ERANGE);
425
426 if (token->str[0] != '-') {
427 ret = qemu_strtou64(token->str, NULL, 10, &uvalue);
428 if (!ret) {
429 return QOBJECT(qnum_from_uint(uvalue));
430 }
431 assert(ret == -ERANGE);
432 }
433 }
434 /* fall through to JSON_FLOAT */
435 case JSON_FLOAT:
436 /* FIXME dependent on locale; a pervasive issue in QEMU */
437 /* FIXME our lexer matches RFC 8259 in forbidding Inf or NaN,
438 * but those might be useful extensions beyond JSON */
439 return QOBJECT(qnum_from_double(strtod(token->str, NULL)));
440 default:
441 abort();
442 }
443 }
444
445 /* Parsing state machine */
446
447 static QObject *parse_begin_value(JSONParserContext *ctxt,
448 const JSONToken *token)
449 {
450 switch (token->type) {
451 case JSON_LCURLY:
452 push_entry(ctxt, QOBJECT(qdict_new()), AFTER_LCURLY);
453 return NULL;
454 case JSON_LSQUARE:
455 push_entry(ctxt, QOBJECT(qlist_new()), AFTER_LSQUARE);
456 return NULL;
457 case JSON_INTERP:
458 return parse_interpolation(ctxt, token);
459 case JSON_INTEGER:
460 case JSON_FLOAT:
461 case JSON_STRING:
462 return parse_literal(ctxt, token);
463 case JSON_KEYWORD:
464 return parse_keyword(ctxt, token);
465 default:
466 parse_error(ctxt, token, "expecting value");
467 return NULL;
468 }
469 }
470
471 static QObject *parse_token(JSONParserContext *ctxt, const JSONToken *token)
472 {
473 JSONParserStackEntry *entry;
474 JSONParserState state;
475 QString *key;
476 QObject *key_obj = NULL, *value = NULL;
477
478 entry = current_entry(ctxt);
479 state = entry ? entry->state : BEFORE_VALUE;
480 switch (state) {
481 case AFTER_LCURLY:
482 /* Grab '}' for empty object or fall through to BEFORE_KEY */
483 assert(qobject_type(entry->partial) == QTYPE_QDICT);
484 if (token->type == JSON_RCURLY) {
485 value = entry->partial;
486 entry = pop_entry(ctxt);
487 break;
488 }
489 entry->state = BEFORE_KEY;
490 /* fall through */
491
492 case BEFORE_KEY:
493 /* Expecting object key */
494 assert(qobject_type(entry->partial) == QTYPE_QDICT);
495 if (token->type != JSON_STRING && token->type != JSON_INTERP) {
496 parse_error(ctxt, token, "expecting key");
497 return NULL;
498 }
499
500 key_obj = parse_begin_value(ctxt, token);
501 if (!key_obj) {
502 /* Parse error already reported */
503 } else if (qobject_type(key_obj) != QTYPE_QSTRING) {
504 /* An interpolation was valid syntactically but not %s */
505 parse_error(ctxt, token, "key is not a string in object");
506 } else {
507 /* Store key in a special entry on the stack */
508 push_entry(ctxt, key_obj, END_OF_KEY);
509 }
510 return NULL;
511
512 case END_OF_KEY:
513 /* Expecting ':' after key */
514 assert(qobject_type(entry->partial) == QTYPE_QSTRING);
515 if (token->type == JSON_COLON) {
516 entry->state = BEFORE_VALUE;
517 } else {
518 parse_error(ctxt, token, "expecting ':'");
519 }
520 return NULL;
521
522 case AFTER_LSQUARE:
523 /* Grab ']' for empty array or fall through to BEFORE_VALUE */
524 assert(qobject_type(entry->partial) == QTYPE_QLIST);
525 if (token->type == JSON_RSQUARE) {
526 value = entry->partial;
527 entry = pop_entry(ctxt);
528 break;
529 }
530 entry->state = BEFORE_VALUE;
531 /* fall through */
532
533 case BEFORE_VALUE:
534 /* Expecting value */
535 assert(!entry || qobject_type(entry->partial) != QTYPE_QDICT);
536 value = parse_begin_value(ctxt, token);
537 if (!value) {
538 /* Error or '['/'{' */
539 return NULL;
540 }
541 /* Return value or insert it into a container */
542 break;
543
544 case END_OF_VALUE:
545 /* Grab ',' or ']' for array; ',' or '}' for object */
546 if (qobject_to(QList, entry->partial)) {
547 /* Array */
548 if (token->type != JSON_RSQUARE) {
549 if (token->type == JSON_COMMA) {
550 entry->state = BEFORE_VALUE;
551 } else {
552 parse_error(ctxt, token, "expected ',' or ']'");
553 }
554 return NULL;
555 }
556 } else if (qobject_to(QDict, entry->partial)) {
557 /* Object */
558 if (token->type != JSON_RCURLY) {
559 if (token->type == JSON_COMMA) {
560 entry->state = BEFORE_KEY;
561 } else {
562 parse_error(ctxt, token, "expected ',' or '}'");
563 }
564 return NULL;
565 }
566 } else {
567 g_assert_not_reached();
568 }
569
570 /* Got ']' or '}'; return full value or insert into parent container */
571 value = entry->partial;
572 entry = pop_entry(ctxt);
573 break;
574 }
575
576 assert(value);
577 if (entry == NULL) {
578 /* Parse stack now empty, the top-level value is complete. */
579 return value;
580 }
581
582 /*
583 * Parse stack is not empty and entry->partial is the top of stack.
584 * It's a QString with the key (and a QDict is below it) if we're
585 * parsing an object, or a QList if we're parsing an array.
586 */
587 key = qobject_to(QString, entry->partial);
588 if (key) {
589 const char *key_str;
590 QDict *dict;
591
592 /* Pop off key, and store (key, value) in QDict. */
593 entry = pop_entry(ctxt);
594 dict = qobject_to(QDict, entry->partial);
595 assert(dict);
596 key_str = qstring_get_str(key);
597 if (qdict_haskey(dict, key_str)) {
598 parse_error(ctxt, token, "duplicate key");
599 qobject_unref(value);
600 return NULL;
601 }
602 qdict_put_obj(dict, key_str, value);
603 qobject_unref(key);
604 } else {
605 /* Array, just store value in the QList. */
606 qlist_append_obj(qobject_to(QList, entry->partial), value);
607 }
608
609 entry->state = END_OF_VALUE;
610 return NULL;
611 }
612
613
614 void json_parser_reset(JSONParserContext *ctxt)
615 {
616 JSONParserStackEntry *entry;
617
618 ctxt->err = NULL;
619 while ((entry = g_queue_pop_tail(ctxt->stack)) != NULL) {
620 qobject_unref(entry->partial);
621 g_free(entry);
622 }
623 }
624
625 void json_parser_init(JSONParserContext *ctxt, va_list *ap)
626 {
627 ctxt->stack = g_queue_new();
628 ctxt->ap = ap;
629 json_parser_reset(ctxt);
630 }
631
632 void json_parser_destroy(JSONParserContext *ctxt)
633 {
634 json_parser_reset(ctxt);
635 g_queue_free(ctxt->stack);
636 ctxt->stack = NULL;
637 }
638
639 /*
640 * Advance the parser based on the token that is passed.
641 * Return the finished top-level value if the token completes it, else
642 * NULL.
643 * Once an error is returned, the function must not be called again
644 * without first resetting the parser.
645 */
646 QObject *json_parser_feed(JSONParserContext *ctxt, const JSONToken *token,
647 Error **errp)
648 {
649 QObject *result = NULL;
650
651 assert(!ctxt->err);
652 switch (token->type) {
653 case JSON_ERROR:
654 parse_error(ctxt, token, "stray '%s'", token->str);
655 break;
656
657 case JSON_END_OF_INPUT:
658 /* Check for premature end of input */
659 if (!g_queue_is_empty(ctxt->stack)) {
660 parse_error(ctxt, token, "premature end of input");
661 }
662 break;
663
664 default:
665 result = parse_token(ctxt, token);
666 break;
667 }
668
669 error_propagate(errp, ctxt->err);
670 return result;
671 }