4 * Copyright IBM, Corp. 2009
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.
14 #include "qemu/osdep.h"
15 #include "qemu-common.h"
16 #include "qapi/qmp/json-lexer.h"
18 #define MAX_TOKEN_SIZE (64ULL << 20)
21 * From RFC 8259 "The JavaScript Object Notation (JSON) Data
22 * Interchange Format", with [comments in brackets]:
24 * The set of tokens includes six structural characters, strings,
25 * numbers, and three literal names.
27 * These are the six structural characters:
29 * begin-array = ws %x5B ws ; [ left square bracket
30 * begin-object = ws %x7B ws ; { left curly bracket
31 * end-array = ws %x5D ws ; ] right square bracket
32 * end-object = ws %x7D ws ; } right curly bracket
33 * name-separator = ws %x3A ws ; : colon
34 * value-separator = ws %x2C ws ; , comma
36 * Insignificant whitespace is allowed before or after any of the six
37 * structural characters.
38 * [This lexer accepts it before or after any token, which is actually
39 * the same, as the grammar always has structural characters between
44 * %x09 / ; Horizontal tab
45 * %x0A / ; Line feed or New line
46 * %x0D ) ; Carriage return
48 * [...] three literal names:
50 * [This lexer accepts [a-z]+, and leaves rejecting unknown literal
51 * names to the parser.]
55 * number = [ minus ] int [ frac ] [ exp ]
56 * decimal-point = %x2E ; .
57 * digit1-9 = %x31-39 ; 1-9
58 * e = %x65 / %x45 ; e E
59 * exp = e [ minus / plus ] 1*DIGIT
60 * frac = decimal-point 1*DIGIT
61 * int = zero / ( digit1-9 *DIGIT )
67 * string = quotation-mark *char quotation-mark
71 * %x22 / ; " quotation mark U+0022
72 * %x5C / ; \ reverse solidus U+005C
73 * %x2F / ; / solidus U+002F
74 * %x62 / ; b backspace U+0008
75 * %x66 / ; f form feed U+000C
76 * %x6E / ; n line feed U+000A
77 * %x72 / ; r carriage return U+000D
78 * %x74 / ; t tab U+0009
79 * %x75 4HEXDIG ) ; uXXXX U+XXXX
81 * quotation-mark = %x22 ; "
82 * unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
83 * [This lexer accepts any non-control character after escape, and
84 * leaves rejecting invalid ones to the parser.]
87 * Extensions over RFC 8259:
88 * - Extra escape sequence in strings:
89 * 0x27 (apostrophe) is recognized after escape, too
90 * - Single-quoted strings:
91 * Like double-quoted strings, except they're delimited by %x27
92 * (apostrophe) instead of %x22 (quotation mark), and can't contain
93 * unescaped apostrophe, but can contain unescaped quotation mark.
95 * interpolation = %((l|ll|I64)[du]|[ipsf])
98 * - Input must be encoded in modified UTF-8.
99 * - Decoding and validating is left to the parser.
102 enum json_lexer_state {
103 IN_ERROR = 0, /* must really be 0, see json_lexer[] */
115 IN_NEG_NONZERO_NUMBER,
127 QEMU_BUILD_BUG_ON((int)JSON_MIN <= (int)IN_START);
129 #define TERMINAL(state) [0 ... 0x7F] = (state)
131 /* Return whether TERMINAL is a terminal state and the transition to it
132 from OLD_STATE required lookahead. This happens whenever the table
133 below uses the TERMINAL macro. */
134 #define TERMINAL_NEEDED_LOOKAHEAD(old_state, terminal) \
135 (terminal != IN_ERROR && json_lexer[(old_state)][0] == (terminal))
137 static const uint8_t json_lexer[][256] = {
138 /* Relies on default initialization to IN_ERROR! */
140 /* double quote string */
141 [IN_DQ_STRING_ESCAPE] = {
142 [0x20 ... 0xFD] = IN_DQ_STRING,
145 [0x20 ... 0xFD] = IN_DQ_STRING,
146 ['\\'] = IN_DQ_STRING_ESCAPE,
150 /* single quote string */
151 [IN_SQ_STRING_ESCAPE] = {
152 [0x20 ... 0xFD] = IN_SQ_STRING,
155 [0x20 ... 0xFD] = IN_SQ_STRING,
156 ['\\'] = IN_SQ_STRING_ESCAPE,
157 ['\''] = JSON_STRING,
162 TERMINAL(JSON_INTEGER),
163 ['0' ... '9'] = IN_ERROR,
169 TERMINAL(JSON_FLOAT),
170 ['0' ... '9'] = IN_DIGITS,
174 ['0' ... '9'] = IN_DIGITS,
180 ['0' ... '9'] = IN_DIGITS,
183 [IN_MANTISSA_DIGITS] = {
184 TERMINAL(JSON_FLOAT),
185 ['0' ... '9'] = IN_MANTISSA_DIGITS,
191 ['0' ... '9'] = IN_MANTISSA_DIGITS,
195 [IN_NONZERO_NUMBER] = {
196 TERMINAL(JSON_INTEGER),
197 ['0' ... '9'] = IN_NONZERO_NUMBER,
203 [IN_NEG_NONZERO_NUMBER] = {
205 ['1' ... '9'] = IN_NONZERO_NUMBER,
210 TERMINAL(JSON_KEYWORD),
211 ['a' ... 'z'] = IN_KEYWORD,
217 [' '] = IN_WHITESPACE,
218 ['\t'] = IN_WHITESPACE,
219 ['\r'] = IN_WHITESPACE,
220 ['\n'] = IN_WHITESPACE,
231 ['l'] = IN_ESCAPE_LL,
241 ['4'] = IN_ESCAPE_I64,
245 ['6'] = IN_ESCAPE_I6,
261 ['"'] = IN_DQ_STRING,
262 ['\''] = IN_SQ_STRING,
264 ['1' ... '9'] = IN_NONZERO_NUMBER,
265 ['-'] = IN_NEG_NONZERO_NUMBER,
268 ['['] = JSON_LSQUARE,
269 [']'] = JSON_RSQUARE,
272 ['a' ... 'z'] = IN_KEYWORD,
274 [' '] = IN_WHITESPACE,
275 ['\t'] = IN_WHITESPACE,
276 ['\r'] = IN_WHITESPACE,
277 ['\n'] = IN_WHITESPACE,
281 void json_lexer_init(JSONLexer *lexer, JSONLexerEmitter func)
284 lexer->state = IN_START;
285 lexer->token = g_string_sized_new(3);
286 lexer->x = lexer->y = 0;
289 static int json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush)
291 int char_consumed, new_state;
300 assert(lexer->state <= ARRAY_SIZE(json_lexer));
301 new_state = json_lexer[lexer->state][(uint8_t)ch];
302 char_consumed = !TERMINAL_NEEDED_LOOKAHEAD(lexer->state, new_state);
303 if (char_consumed && !flush) {
304 g_string_append_c(lexer->token, ch);
319 lexer->emit(lexer, lexer->token, new_state, lexer->x, lexer->y);
322 g_string_truncate(lexer->token, 0);
323 new_state = IN_START;
326 /* XXX: To avoid having previous bad input leaving the parser in an
327 * unresponsive state where we consume unpredictable amounts of
328 * subsequent "good" input, percolate this error state up to the
329 * tokenizer/parser by forcing a NULL object to be emitted, then
332 * Also note that this handling is required for reliable channel
333 * negotiation between QMP and the guest agent, since chr(0xFF)
334 * is placed at the beginning of certain events to ensure proper
335 * delivery when the channel is in an unknown state. chr(0xFF) is
336 * never a valid ASCII/UTF-8 sequence, so this should reliably
337 * induce an error/flush state.
339 lexer->emit(lexer, lexer->token, JSON_ERROR, lexer->x, lexer->y);
340 g_string_truncate(lexer->token, 0);
341 new_state = IN_START;
342 lexer->state = new_state;
347 lexer->state = new_state;
348 } while (!char_consumed && !flush);
350 /* Do not let a single token grow to an arbitrarily large size,
351 * this is a security consideration.
353 if (lexer->token->len > MAX_TOKEN_SIZE) {
354 lexer->emit(lexer, lexer->token, lexer->state, lexer->x, lexer->y);
355 g_string_truncate(lexer->token, 0);
356 lexer->state = IN_START;
362 int json_lexer_feed(JSONLexer *lexer, const char *buffer, size_t size)
366 for (i = 0; i < size; i++) {
369 err = json_lexer_feed_char(lexer, buffer[i], false);
378 int json_lexer_flush(JSONLexer *lexer)
380 return lexer->state == IN_START ? 0 : json_lexer_feed_char(lexer, 0, true);
383 void json_lexer_destroy(JSONLexer *lexer)
385 g_string_free(lexer->token, true);