4 * Copyright IBM, Corp. 2009
7 * Anthony Liguori <aliguori@us.ibm.com>
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"
17 #include "qapi/qmp/json-streamer.h"
19 #define MAX_TOKEN_SIZE (64ULL << 20)
22 * From RFC 8259 "The JavaScript Object Notation (JSON) Data
23 * Interchange Format", with [comments in brackets]:
25 * The set of tokens includes six structural characters, strings,
26 * numbers, and three literal names.
28 * These are the six structural characters:
30 * begin-array = ws %x5B ws ; [ left square bracket
31 * begin-object = ws %x7B ws ; { left curly bracket
32 * end-array = ws %x5D ws ; ] right square bracket
33 * end-object = ws %x7D ws ; } right curly bracket
34 * name-separator = ws %x3A ws ; : colon
35 * value-separator = ws %x2C ws ; , comma
37 * Insignificant whitespace is allowed before or after any of the six
38 * structural characters.
39 * [This lexer accepts it before or after any token, which is actually
40 * the same, as the grammar always has structural characters between
45 * %x09 / ; Horizontal tab
46 * %x0A / ; Line feed or New line
47 * %x0D ) ; Carriage return
49 * [...] three literal names:
51 * [This lexer accepts [a-z]+, and leaves rejecting unknown literal
52 * names to the parser.]
56 * number = [ minus ] int [ frac ] [ exp ]
57 * decimal-point = %x2E ; .
58 * digit1-9 = %x31-39 ; 1-9
59 * e = %x65 / %x45 ; e E
60 * exp = e [ minus / plus ] 1*DIGIT
61 * frac = decimal-point 1*DIGIT
62 * int = zero / ( digit1-9 *DIGIT )
68 * string = quotation-mark *char quotation-mark
72 * %x22 / ; " quotation mark U+0022
73 * %x5C / ; \ reverse solidus U+005C
74 * %x2F / ; / solidus U+002F
75 * %x62 / ; b backspace U+0008
76 * %x66 / ; f form feed U+000C
77 * %x6E / ; n line feed U+000A
78 * %x72 / ; r carriage return U+000D
79 * %x74 / ; t tab U+0009
80 * %x75 4HEXDIG ) ; uXXXX U+XXXX
82 * quotation-mark = %x22 ; "
83 * unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
84 * [This lexer accepts any non-control character after escape, and
85 * leaves rejecting invalid ones to the parser.]
88 * Extensions over RFC 8259:
89 * - Extra escape sequence in strings:
90 * 0x27 (apostrophe) is recognized after escape, too
91 * - Single-quoted strings:
92 * Like double-quoted strings, except they're delimited by %x27
93 * (apostrophe) instead of %x22 (quotation mark), and can't contain
94 * unescaped apostrophe, but can contain unescaped quotation mark.
95 * - Interpolation, if enabled:
96 * The lexer accepts %[A-Za-z0-9]*, and leaves rejecting invalid
100 * - Input must be encoded in modified UTF-8.
101 * - Decoding and validating is left to the parser.
104 enum json_lexer_state
{
105 IN_ERROR
= 0, /* must really be 0, see json_lexer[] */
122 IN_START_INTERP
, /* must be IN_START + 1 */
125 QEMU_BUILD_BUG_ON((int)JSON_MIN
<= (int)IN_START_INTERP
);
126 QEMU_BUILD_BUG_ON(IN_START_INTERP
!= IN_START
+ 1);
128 #define TERMINAL(state) [0 ... 0x7F] = (state)
130 /* Return whether TERMINAL is a terminal state and the transition to it
131 from OLD_STATE required lookahead. This happens whenever the table
132 below uses the TERMINAL macro. */
133 #define TERMINAL_NEEDED_LOOKAHEAD(old_state, terminal) \
134 (terminal != IN_ERROR && json_lexer[(old_state)][0] == (terminal))
136 static const uint8_t json_lexer
[][256] = {
137 /* Relies on default initialization to IN_ERROR! */
139 /* double quote string */
140 [IN_DQ_STRING_ESCAPE
] = {
141 [0x20 ... 0xFD] = IN_DQ_STRING
,
144 [0x20 ... 0xFD] = IN_DQ_STRING
,
145 ['\\'] = IN_DQ_STRING_ESCAPE
,
149 /* single quote string */
150 [IN_SQ_STRING_ESCAPE
] = {
151 [0x20 ... 0xFD] = IN_SQ_STRING
,
154 [0x20 ... 0xFD] = IN_SQ_STRING
,
155 ['\\'] = IN_SQ_STRING_ESCAPE
,
156 ['\''] = JSON_STRING
,
161 TERMINAL(JSON_INTEGER
),
162 ['0' ... '9'] = IN_ERROR
,
168 TERMINAL(JSON_FLOAT
),
169 ['0' ... '9'] = IN_EXP_DIGITS
,
173 ['0' ... '9'] = IN_EXP_DIGITS
,
179 ['0' ... '9'] = IN_EXP_DIGITS
,
182 [IN_MANTISSA_DIGITS
] = {
183 TERMINAL(JSON_FLOAT
),
184 ['0' ... '9'] = IN_MANTISSA_DIGITS
,
190 ['0' ... '9'] = IN_MANTISSA_DIGITS
,
195 TERMINAL(JSON_INTEGER
),
196 ['0' ... '9'] = IN_DIGITS
,
204 ['1' ... '9'] = IN_DIGITS
,
209 TERMINAL(JSON_KEYWORD
),
210 ['a' ... 'z'] = IN_KEYWORD
,
216 [' '] = IN_WHITESPACE
,
217 ['\t'] = IN_WHITESPACE
,
218 ['\r'] = IN_WHITESPACE
,
219 ['\n'] = IN_WHITESPACE
,
224 TERMINAL(JSON_INTERP
),
225 ['A' ... 'Z'] = IN_INTERP
,
226 ['a' ... 'z'] = IN_INTERP
,
227 ['0' ... '9'] = IN_INTERP
,
232 * - IN_START recognizes JSON tokens with our string extensions
233 * - IN_START_INTERP additionally recognizes interpolation.
235 [IN_START
... IN_START_INTERP
] = {
236 ['"'] = IN_DQ_STRING
,
237 ['\''] = IN_SQ_STRING
,
239 ['1' ... '9'] = IN_DIGITS
,
243 ['['] = JSON_LSQUARE
,
244 [']'] = JSON_RSQUARE
,
247 ['a' ... 'z'] = IN_KEYWORD
,
248 [' '] = IN_WHITESPACE
,
249 ['\t'] = IN_WHITESPACE
,
250 ['\r'] = IN_WHITESPACE
,
251 ['\n'] = IN_WHITESPACE
,
253 [IN_START_INTERP
]['%'] = IN_INTERP
,
256 void json_lexer_init(JSONLexer
*lexer
, bool enable_interpolation
)
258 lexer
->start_state
= lexer
->state
= enable_interpolation
259 ? IN_START_INTERP
: IN_START
;
260 lexer
->token
= g_string_sized_new(3);
261 lexer
->x
= lexer
->y
= 0;
264 static void json_lexer_feed_char(JSONLexer
*lexer
, char ch
, bool flush
)
266 int char_consumed
, new_state
;
275 assert(lexer
->state
<= ARRAY_SIZE(json_lexer
));
276 new_state
= json_lexer
[lexer
->state
][(uint8_t)ch
];
277 char_consumed
= !TERMINAL_NEEDED_LOOKAHEAD(lexer
->state
, new_state
);
278 if (char_consumed
&& !flush
) {
279 g_string_append_c(lexer
->token
, ch
);
294 json_message_process_token(lexer
, lexer
->token
, new_state
,
298 g_string_truncate(lexer
->token
, 0);
299 new_state
= lexer
->start_state
;
302 /* XXX: To avoid having previous bad input leaving the parser in an
303 * unresponsive state where we consume unpredictable amounts of
304 * subsequent "good" input, percolate this error state up to the
305 * parser by emitting a JSON_ERROR token, then reset lexer state.
307 * Also note that this handling is required for reliable channel
308 * negotiation between QMP and the guest agent, since chr(0xFF)
309 * is placed at the beginning of certain events to ensure proper
310 * delivery when the channel is in an unknown state. chr(0xFF) is
311 * never a valid ASCII/UTF-8 sequence, so this should reliably
312 * induce an error/flush state.
314 json_message_process_token(lexer
, lexer
->token
, JSON_ERROR
,
316 g_string_truncate(lexer
->token
, 0);
317 lexer
->state
= lexer
->start_state
;
322 lexer
->state
= new_state
;
323 } while (!char_consumed
&& !flush
);
325 /* Do not let a single token grow to an arbitrarily large size,
326 * this is a security consideration.
328 if (lexer
->token
->len
> MAX_TOKEN_SIZE
) {
329 json_message_process_token(lexer
, lexer
->token
, lexer
->state
,
331 g_string_truncate(lexer
->token
, 0);
332 lexer
->state
= lexer
->start_state
;
336 void json_lexer_feed(JSONLexer
*lexer
, const char *buffer
, size_t size
)
340 for (i
= 0; i
< size
; i
++) {
341 json_lexer_feed_char(lexer
, buffer
[i
], false);
345 void json_lexer_flush(JSONLexer
*lexer
)
347 if (lexer
->state
!= lexer
->start_state
) {
348 json_lexer_feed_char(lexer
, 0, true);
350 json_message_process_token(lexer
, lexer
->token
, JSON_END_OF_INPUT
,
354 void json_lexer_destroy(JSONLexer
*lexer
)
356 g_string_free(lexer
->token
, true);