blob: 06ec67dc45bef6ffa46f8dccc091c19065edd5d9 [file] [log] [blame]
Anthony Liguori5ab85582009-11-11 10:39:14 -06001/*
2 * JSON lexer
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 Maydellf2ad72b2016-01-29 17:50:01 +000014#include "qemu/osdep.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010015#include "qapi/qmp/json-lexer.h"
Markus Armbruster037f2442018-08-23 18:40:00 +020016#include "qapi/qmp/json-streamer.h"
Anthony Liguori5ab85582009-11-11 10:39:14 -060017
Anthony Liguori325601b2011-06-01 12:14:52 -050018#define MAX_TOKEN_SIZE (64ULL << 20)
19
Anthony Liguori5ab85582009-11-11 10:39:14 -060020/*
Markus Armbrustereddc0a72018-08-23 18:39:46 +020021 * From RFC 8259 "The JavaScript Object Notation (JSON) Data
22 * Interchange Format", with [comments in brackets]:
Eric Blakeff5394a2016-06-09 20:48:06 -060023 *
Markus Armbrustereddc0a72018-08-23 18:39:46 +020024 * The set of tokens includes six structural characters, strings,
25 * numbers, and three literal names.
Eric Blakeff5394a2016-06-09 20:48:06 -060026 *
Markus Armbrustereddc0a72018-08-23 18:39:46 +020027 * These are the six structural characters:
Eric Blakeff5394a2016-06-09 20:48:06 -060028 *
Markus Armbrustereddc0a72018-08-23 18:39:46 +020029 * 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
Eric Blakeff5394a2016-06-09 20:48:06 -060035 *
Markus Armbrustereddc0a72018-08-23 18:39:46 +020036 * 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
40 * other tokens.]
Eric Blakeff5394a2016-06-09 20:48:06 -060041 *
Markus Armbrustereddc0a72018-08-23 18:39:46 +020042 * ws = *(
43 * %x20 / ; Space
44 * %x09 / ; Horizontal tab
45 * %x0A / ; Line feed or New line
46 * %x0D ) ; Carriage return
Anthony Liguori5ab85582009-11-11 10:39:14 -060047 *
Markus Armbrustereddc0a72018-08-23 18:39:46 +020048 * [...] three literal names:
49 * false null true
50 * [This lexer accepts [a-z]+, and leaves rejecting unknown literal
51 * names to the parser.]
52 *
53 * [Numbers:]
54 *
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 )
62 * minus = %x2D ; -
63 * plus = %x2B ; +
64 * zero = %x30 ; 0
65 *
66 * [Strings:]
67 * string = quotation-mark *char quotation-mark
68 *
69 * char = unescaped /
70 * escape (
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
80 * escape = %x5C ; \
81 * quotation-mark = %x22 ; "
82 * unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
Markus Armbrusterb2da4a42018-08-23 18:39:53 +020083 * [This lexer accepts any non-control character after escape, and
84 * leaves rejecting invalid ones to the parser.]
Markus Armbrustereddc0a72018-08-23 18:39:46 +020085 *
86 *
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.
Markus Armbruster2cbd15a2018-08-23 18:40:05 +020094 * - Interpolation, if enabled:
Markus Armbrusterf7617d42018-08-23 18:40:07 +020095 * The lexer accepts %[A-Za-z0-9]*, and leaves rejecting invalid
96 * ones to the parser.
Markus Armbrustereddc0a72018-08-23 18:39:46 +020097 *
98 * Note:
Markus Armbruster4b1c0cd2018-08-23 18:39:52 +020099 * - Input must be encoded in modified UTF-8.
Markus Armbrustereddc0a72018-08-23 18:39:46 +0200100 * - Decoding and validating is left to the parser.
Anthony Liguori5ab85582009-11-11 10:39:14 -0600101 */
102
103enum json_lexer_state {
Markus Armbrusterb8d3b1d2015-11-25 22:23:25 +0100104 IN_ERROR = 0, /* must really be 0, see json_lexer[] */
Anthony Liguori5ab85582009-11-11 10:39:14 -0600105 IN_DQ_STRING_ESCAPE,
106 IN_DQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600107 IN_SQ_STRING_ESCAPE,
108 IN_SQ_STRING,
109 IN_ZERO,
Markus Armbruster4d400662018-08-23 18:40:09 +0200110 IN_EXP_DIGITS,
111 IN_EXP_SIGN,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600112 IN_EXP_E,
113 IN_MANTISSA,
114 IN_MANTISSA_DIGITS,
Markus Armbruster4d400662018-08-23 18:40:09 +0200115 IN_DIGITS,
116 IN_SIGN,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600117 IN_KEYWORD,
Markus Armbruster61030282018-08-23 18:40:04 +0200118 IN_INTERP,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600119 IN_WHITESPACE,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600120 IN_START,
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200121 IN_START_INTERP, /* must be IN_START + 1 */
Anthony Liguori5ab85582009-11-11 10:39:14 -0600122};
123
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200124QEMU_BUILD_BUG_ON((int)JSON_MIN <= (int)IN_START_INTERP);
125QEMU_BUILD_BUG_ON(IN_START_INTERP != IN_START + 1);
Markus Armbrusterb8d3b1d2015-11-25 22:23:25 +0100126
Anthony Liguori5ab85582009-11-11 10:39:14 -0600127#define TERMINAL(state) [0 ... 0x7F] = (state)
128
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200129/* Return whether TERMINAL is a terminal state and the transition to it
130 from OLD_STATE required lookahead. This happens whenever the table
131 below uses the TERMINAL macro. */
132#define TERMINAL_NEEDED_LOOKAHEAD(old_state, terminal) \
Markus Armbrustera2ec6be2018-08-23 18:39:44 +0200133 (terminal != IN_ERROR && json_lexer[(old_state)][0] == (terminal))
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200134
Anthony Liguori5ab85582009-11-11 10:39:14 -0600135static const uint8_t json_lexer[][256] = {
Markus Armbrusterb8d3b1d2015-11-25 22:23:25 +0100136 /* Relies on default initialization to IN_ERROR! */
137
Anthony Liguori5ab85582009-11-11 10:39:14 -0600138 /* double quote string */
Anthony Liguori5ab85582009-11-11 10:39:14 -0600139 [IN_DQ_STRING_ESCAPE] = {
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200140 [0x20 ... 0xFD] = IN_DQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600141 },
142 [IN_DQ_STRING] = {
Markus Armbrusterde930f42018-08-23 18:39:51 +0200143 [0x20 ... 0xFD] = IN_DQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600144 ['\\'] = IN_DQ_STRING_ESCAPE,
Paolo Bonzini28e91a62010-05-24 09:39:53 +0200145 ['"'] = JSON_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600146 },
147
148 /* single quote string */
Anthony Liguori5ab85582009-11-11 10:39:14 -0600149 [IN_SQ_STRING_ESCAPE] = {
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200150 [0x20 ... 0xFD] = IN_SQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600151 },
152 [IN_SQ_STRING] = {
Markus Armbrusterde930f42018-08-23 18:39:51 +0200153 [0x20 ... 0xFD] = IN_SQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600154 ['\\'] = IN_SQ_STRING_ESCAPE,
Paolo Bonzini28e91a62010-05-24 09:39:53 +0200155 ['\''] = JSON_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600156 },
157
158 /* Zero */
159 [IN_ZERO] = {
160 TERMINAL(JSON_INTEGER),
Blue Swirl33d05392011-03-27 09:07:54 +0000161 ['0' ... '9'] = IN_ERROR,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600162 ['.'] = IN_MANTISSA,
163 },
164
165 /* Float */
Markus Armbruster4d400662018-08-23 18:40:09 +0200166 [IN_EXP_DIGITS] = {
Anthony Liguori5ab85582009-11-11 10:39:14 -0600167 TERMINAL(JSON_FLOAT),
Markus Armbruster4d400662018-08-23 18:40:09 +0200168 ['0' ... '9'] = IN_EXP_DIGITS,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600169 },
170
Markus Armbruster4d400662018-08-23 18:40:09 +0200171 [IN_EXP_SIGN] = {
172 ['0' ... '9'] = IN_EXP_DIGITS,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600173 },
174
175 [IN_EXP_E] = {
Markus Armbruster4d400662018-08-23 18:40:09 +0200176 ['-'] = IN_EXP_SIGN,
177 ['+'] = IN_EXP_SIGN,
178 ['0' ... '9'] = IN_EXP_DIGITS,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600179 },
180
181 [IN_MANTISSA_DIGITS] = {
182 TERMINAL(JSON_FLOAT),
183 ['0' ... '9'] = IN_MANTISSA_DIGITS,
184 ['e'] = IN_EXP_E,
185 ['E'] = IN_EXP_E,
186 },
187
188 [IN_MANTISSA] = {
189 ['0' ... '9'] = IN_MANTISSA_DIGITS,
190 },
191
192 /* Number */
Markus Armbruster4d400662018-08-23 18:40:09 +0200193 [IN_DIGITS] = {
Anthony Liguori5ab85582009-11-11 10:39:14 -0600194 TERMINAL(JSON_INTEGER),
Markus Armbruster4d400662018-08-23 18:40:09 +0200195 ['0' ... '9'] = IN_DIGITS,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600196 ['e'] = IN_EXP_E,
197 ['E'] = IN_EXP_E,
198 ['.'] = IN_MANTISSA,
199 },
200
Markus Armbruster4d400662018-08-23 18:40:09 +0200201 [IN_SIGN] = {
Anthony Liguori5ab85582009-11-11 10:39:14 -0600202 ['0'] = IN_ZERO,
Markus Armbruster4d400662018-08-23 18:40:09 +0200203 ['1' ... '9'] = IN_DIGITS,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600204 },
205
206 /* keywords */
207 [IN_KEYWORD] = {
208 TERMINAL(JSON_KEYWORD),
209 ['a' ... 'z'] = IN_KEYWORD,
210 },
211
212 /* whitespace */
213 [IN_WHITESPACE] = {
214 TERMINAL(JSON_SKIP),
215 [' '] = IN_WHITESPACE,
216 ['\t'] = IN_WHITESPACE,
217 ['\r'] = IN_WHITESPACE,
218 ['\n'] = IN_WHITESPACE,
Eric Blakeff5394a2016-06-09 20:48:06 -0600219 },
Anthony Liguori5ab85582009-11-11 10:39:14 -0600220
Markus Armbruster61030282018-08-23 18:40:04 +0200221 /* interpolation */
Markus Armbruster61030282018-08-23 18:40:04 +0200222 [IN_INTERP] = {
Markus Armbrusterf7617d42018-08-23 18:40:07 +0200223 TERMINAL(JSON_INTERP),
224 ['A' ... 'Z'] = IN_INTERP,
225 ['a' ... 'z'] = IN_INTERP,
226 ['0' ... '9'] = IN_INTERP,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600227 },
228
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200229 /*
230 * Two start states:
231 * - IN_START recognizes JSON tokens with our string extensions
232 * - IN_START_INTERP additionally recognizes interpolation.
233 */
234 [IN_START ... IN_START_INTERP] = {
Anthony Liguori5ab85582009-11-11 10:39:14 -0600235 ['"'] = IN_DQ_STRING,
236 ['\''] = IN_SQ_STRING,
237 ['0'] = IN_ZERO,
Markus Armbruster4d400662018-08-23 18:40:09 +0200238 ['1' ... '9'] = IN_DIGITS,
239 ['-'] = IN_SIGN,
Markus Armbrusterc5461662015-11-25 22:23:26 +0100240 ['{'] = JSON_LCURLY,
241 ['}'] = JSON_RCURLY,
242 ['['] = JSON_LSQUARE,
243 [']'] = JSON_RSQUARE,
244 [','] = JSON_COMMA,
245 [':'] = JSON_COLON,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600246 ['a' ... 'z'] = IN_KEYWORD,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600247 [' '] = IN_WHITESPACE,
248 ['\t'] = IN_WHITESPACE,
249 ['\r'] = IN_WHITESPACE,
250 ['\n'] = IN_WHITESPACE,
251 },
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200252 [IN_START_INTERP]['%'] = IN_INTERP,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600253};
254
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200255void json_lexer_init(JSONLexer *lexer, bool enable_interpolation)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600256{
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200257 lexer->start_state = lexer->state = enable_interpolation
258 ? IN_START_INTERP : IN_START;
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100259 lexer->token = g_string_sized_new(3);
Luiz Capitulino03308f62010-05-17 17:50:01 -0300260 lexer->x = lexer->y = 0;
Anthony Liguori5ab85582009-11-11 10:39:14 -0600261}
262
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200263static void json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600264{
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200265 int char_consumed, new_state;
266
Anthony Liguori5ab85582009-11-11 10:39:14 -0600267 lexer->x++;
268 if (ch == '\n') {
269 lexer->x = 0;
270 lexer->y++;
271 }
272
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200273 do {
Markus Armbrusterb8d3b1d2015-11-25 22:23:25 +0100274 assert(lexer->state <= ARRAY_SIZE(json_lexer));
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200275 new_state = json_lexer[lexer->state][(uint8_t)ch];
276 char_consumed = !TERMINAL_NEEDED_LOOKAHEAD(lexer->state, new_state);
Markus Armbrustera2ec6be2018-08-23 18:39:44 +0200277 if (char_consumed && !flush) {
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100278 g_string_append_c(lexer->token, ch);
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200279 }
Anthony Liguori5ab85582009-11-11 10:39:14 -0600280
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200281 switch (new_state) {
Markus Armbrusterc5461662015-11-25 22:23:26 +0100282 case JSON_LCURLY:
283 case JSON_RCURLY:
284 case JSON_LSQUARE:
285 case JSON_RSQUARE:
286 case JSON_COLON:
287 case JSON_COMMA:
Markus Armbruster61030282018-08-23 18:40:04 +0200288 case JSON_INTERP:
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200289 case JSON_INTEGER:
290 case JSON_FLOAT:
291 case JSON_KEYWORD:
292 case JSON_STRING:
Markus Armbruster037f2442018-08-23 18:40:00 +0200293 json_message_process_token(lexer, lexer->token, new_state,
294 lexer->x, lexer->y);
Stefan Weil0b0404b2012-01-09 18:29:51 +0100295 /* fall through */
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200296 case JSON_SKIP:
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100297 g_string_truncate(lexer->token, 0);
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200298 new_state = lexer->start_state;
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200299 break;
Blue Swirl33d05392011-03-27 09:07:54 +0000300 case IN_ERROR:
Michael Rothb011f612011-06-01 12:14:58 -0500301 /* XXX: To avoid having previous bad input leaving the parser in an
302 * unresponsive state where we consume unpredictable amounts of
303 * subsequent "good" input, percolate this error state up to the
Markus Armbruster84a56f32018-08-23 18:40:06 +0200304 * parser by emitting a JSON_ERROR token, then reset lexer state.
Michael Rothb011f612011-06-01 12:14:58 -0500305 *
306 * Also note that this handling is required for reliable channel
307 * negotiation between QMP and the guest agent, since chr(0xFF)
308 * is placed at the beginning of certain events to ensure proper
309 * delivery when the channel is in an unknown state. chr(0xFF) is
310 * never a valid ASCII/UTF-8 sequence, so this should reliably
311 * induce an error/flush state.
312 */
Markus Armbruster037f2442018-08-23 18:40:00 +0200313 json_message_process_token(lexer, lexer->token, JSON_ERROR,
314 lexer->x, lexer->y);
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100315 g_string_truncate(lexer->token, 0);
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200316 lexer->state = lexer->start_state;
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200317 return;
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200318 default:
319 break;
320 }
321 lexer->state = new_state;
Michael Rothbd3924a2011-06-01 12:14:57 -0500322 } while (!char_consumed && !flush);
Anthony Liguori325601b2011-06-01 12:14:52 -0500323
324 /* Do not let a single token grow to an arbitrarily large size,
325 * this is a security consideration.
326 */
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100327 if (lexer->token->len > MAX_TOKEN_SIZE) {
Markus Armbruster037f2442018-08-23 18:40:00 +0200328 json_message_process_token(lexer, lexer->token, lexer->state,
329 lexer->x, lexer->y);
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100330 g_string_truncate(lexer->token, 0);
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200331 lexer->state = lexer->start_state;
Anthony Liguori325601b2011-06-01 12:14:52 -0500332 }
Anthony Liguori5ab85582009-11-11 10:39:14 -0600333}
334
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200335void json_lexer_feed(JSONLexer *lexer, const char *buffer, size_t size)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600336{
337 size_t i;
338
339 for (i = 0; i < size; i++) {
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200340 json_lexer_feed_char(lexer, buffer[i], false);
Anthony Liguori5ab85582009-11-11 10:39:14 -0600341 }
Anthony Liguori5ab85582009-11-11 10:39:14 -0600342}
343
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200344void json_lexer_flush(JSONLexer *lexer)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600345{
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200346 if (lexer->state != lexer->start_state) {
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200347 json_lexer_feed_char(lexer, 0, true);
348 }
Markus Armbrusterf9277912018-08-23 18:40:12 +0200349 json_message_process_token(lexer, lexer->token, JSON_END_OF_INPUT,
350 lexer->x, lexer->y);
Anthony Liguori5ab85582009-11-11 10:39:14 -0600351}
352
353void json_lexer_destroy(JSONLexer *lexer)
354{
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100355 g_string_free(lexer->token, true);
Anthony Liguori5ab85582009-11-11 10:39:14 -0600356}