blob: ec3aec726f4ce3a9c5f0d10de71189b6b57b842a [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"
Markus Armbruster86cdf9e2018-08-23 18:40:20 +020015#include "json-parser-int.h"
Anthony Liguori5ab85582009-11-11 10:39:14 -060016
Anthony Liguori325601b2011-06-01 12:14:52 -050017#define MAX_TOKEN_SIZE (64ULL << 20)
18
Anthony Liguori5ab85582009-11-11 10:39:14 -060019/*
Markus Armbrustereddc0a72018-08-23 18:39:46 +020020 * From RFC 8259 "The JavaScript Object Notation (JSON) Data
21 * Interchange Format", with [comments in brackets]:
Eric Blakeff5394a2016-06-09 20:48:06 -060022 *
Markus Armbrustereddc0a72018-08-23 18:39:46 +020023 * The set of tokens includes six structural characters, strings,
24 * numbers, and three literal names.
Eric Blakeff5394a2016-06-09 20:48:06 -060025 *
Markus Armbrustereddc0a72018-08-23 18:39:46 +020026 * These are the six structural characters:
Eric Blakeff5394a2016-06-09 20:48:06 -060027 *
Markus Armbrustereddc0a72018-08-23 18:39:46 +020028 * begin-array = ws %x5B ws ; [ left square bracket
29 * begin-object = ws %x7B ws ; { left curly bracket
30 * end-array = ws %x5D ws ; ] right square bracket
31 * end-object = ws %x7D ws ; } right curly bracket
32 * name-separator = ws %x3A ws ; : colon
33 * value-separator = ws %x2C ws ; , comma
Eric Blakeff5394a2016-06-09 20:48:06 -060034 *
Markus Armbrustereddc0a72018-08-23 18:39:46 +020035 * Insignificant whitespace is allowed before or after any of the six
36 * structural characters.
37 * [This lexer accepts it before or after any token, which is actually
38 * the same, as the grammar always has structural characters between
39 * other tokens.]
Eric Blakeff5394a2016-06-09 20:48:06 -060040 *
Markus Armbrustereddc0a72018-08-23 18:39:46 +020041 * ws = *(
42 * %x20 / ; Space
43 * %x09 / ; Horizontal tab
44 * %x0A / ; Line feed or New line
45 * %x0D ) ; Carriage return
Anthony Liguori5ab85582009-11-11 10:39:14 -060046 *
Markus Armbrustereddc0a72018-08-23 18:39:46 +020047 * [...] three literal names:
48 * false null true
49 * [This lexer accepts [a-z]+, and leaves rejecting unknown literal
50 * names to the parser.]
51 *
52 * [Numbers:]
53 *
54 * number = [ minus ] int [ frac ] [ exp ]
55 * decimal-point = %x2E ; .
56 * digit1-9 = %x31-39 ; 1-9
57 * e = %x65 / %x45 ; e E
58 * exp = e [ minus / plus ] 1*DIGIT
59 * frac = decimal-point 1*DIGIT
60 * int = zero / ( digit1-9 *DIGIT )
61 * minus = %x2D ; -
62 * plus = %x2B ; +
63 * zero = %x30 ; 0
64 *
65 * [Strings:]
66 * string = quotation-mark *char quotation-mark
67 *
68 * char = unescaped /
69 * escape (
70 * %x22 / ; " quotation mark U+0022
71 * %x5C / ; \ reverse solidus U+005C
72 * %x2F / ; / solidus U+002F
73 * %x62 / ; b backspace U+0008
74 * %x66 / ; f form feed U+000C
75 * %x6E / ; n line feed U+000A
76 * %x72 / ; r carriage return U+000D
77 * %x74 / ; t tab U+0009
78 * %x75 4HEXDIG ) ; uXXXX U+XXXX
79 * escape = %x5C ; \
80 * quotation-mark = %x22 ; "
81 * unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
Markus Armbrusterb2da4a42018-08-23 18:39:53 +020082 * [This lexer accepts any non-control character after escape, and
83 * leaves rejecting invalid ones to the parser.]
Markus Armbrustereddc0a72018-08-23 18:39:46 +020084 *
85 *
86 * Extensions over RFC 8259:
87 * - Extra escape sequence in strings:
88 * 0x27 (apostrophe) is recognized after escape, too
89 * - Single-quoted strings:
90 * Like double-quoted strings, except they're delimited by %x27
91 * (apostrophe) instead of %x22 (quotation mark), and can't contain
92 * unescaped apostrophe, but can contain unescaped quotation mark.
Markus Armbruster2cbd15a2018-08-23 18:40:05 +020093 * - Interpolation, if enabled:
Markus Armbrusterf7617d42018-08-23 18:40:07 +020094 * The lexer accepts %[A-Za-z0-9]*, and leaves rejecting invalid
95 * ones to the parser.
Markus Armbrustereddc0a72018-08-23 18:39:46 +020096 *
97 * Note:
Markus Armbruster4b1c0cd2018-08-23 18:39:52 +020098 * - Input must be encoded in modified UTF-8.
Markus Armbrustereddc0a72018-08-23 18:39:46 +020099 * - Decoding and validating is left to the parser.
Anthony Liguori5ab85582009-11-11 10:39:14 -0600100 */
101
102enum json_lexer_state {
Markus Armbrusterb8d3b1d2015-11-25 22:23:25 +0100103 IN_ERROR = 0, /* must really be 0, see json_lexer[] */
Anthony Liguori5ab85582009-11-11 10:39:14 -0600104 IN_DQ_STRING_ESCAPE,
105 IN_DQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600106 IN_SQ_STRING_ESCAPE,
107 IN_SQ_STRING,
108 IN_ZERO,
Markus Armbruster4d400662018-08-23 18:40:09 +0200109 IN_EXP_DIGITS,
110 IN_EXP_SIGN,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600111 IN_EXP_E,
112 IN_MANTISSA,
113 IN_MANTISSA_DIGITS,
Markus Armbruster4d400662018-08-23 18:40:09 +0200114 IN_DIGITS,
115 IN_SIGN,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600116 IN_KEYWORD,
Markus Armbruster61030282018-08-23 18:40:04 +0200117 IN_INTERP,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600118 IN_WHITESPACE,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600119 IN_START,
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200120 IN_START_INTERP, /* must be IN_START + 1 */
Anthony Liguori5ab85582009-11-11 10:39:14 -0600121};
122
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200123QEMU_BUILD_BUG_ON((int)JSON_MIN <= (int)IN_START_INTERP);
124QEMU_BUILD_BUG_ON(IN_START_INTERP != IN_START + 1);
Markus Armbrusterb8d3b1d2015-11-25 22:23:25 +0100125
Markus Armbruster2a960422018-08-31 09:58:36 +0200126#define TERMINAL(state) [0 ... 0xFF] = (state)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600127
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200128/* Return whether TERMINAL is a terminal state and the transition to it
129 from OLD_STATE required lookahead. This happens whenever the table
130 below uses the TERMINAL macro. */
131#define TERMINAL_NEEDED_LOOKAHEAD(old_state, terminal) \
Markus Armbrustera2ec6be2018-08-23 18:39:44 +0200132 (terminal != IN_ERROR && json_lexer[(old_state)][0] == (terminal))
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200133
Anthony Liguori5ab85582009-11-11 10:39:14 -0600134static const uint8_t json_lexer[][256] = {
Markus Armbrusterb8d3b1d2015-11-25 22:23:25 +0100135 /* Relies on default initialization to IN_ERROR! */
136
Anthony Liguori5ab85582009-11-11 10:39:14 -0600137 /* double quote string */
Anthony Liguori5ab85582009-11-11 10:39:14 -0600138 [IN_DQ_STRING_ESCAPE] = {
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200139 [0x20 ... 0xFD] = IN_DQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600140 },
141 [IN_DQ_STRING] = {
Markus Armbrusterde930f42018-08-23 18:39:51 +0200142 [0x20 ... 0xFD] = IN_DQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600143 ['\\'] = IN_DQ_STRING_ESCAPE,
Paolo Bonzini28e91a62010-05-24 09:39:53 +0200144 ['"'] = JSON_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600145 },
146
147 /* single quote string */
Anthony Liguori5ab85582009-11-11 10:39:14 -0600148 [IN_SQ_STRING_ESCAPE] = {
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200149 [0x20 ... 0xFD] = IN_SQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600150 },
151 [IN_SQ_STRING] = {
Markus Armbrusterde930f42018-08-23 18:39:51 +0200152 [0x20 ... 0xFD] = IN_SQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600153 ['\\'] = IN_SQ_STRING_ESCAPE,
Paolo Bonzini28e91a62010-05-24 09:39:53 +0200154 ['\''] = JSON_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600155 },
156
157 /* Zero */
158 [IN_ZERO] = {
159 TERMINAL(JSON_INTEGER),
Blue Swirl33d05392011-03-27 09:07:54 +0000160 ['0' ... '9'] = IN_ERROR,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600161 ['.'] = IN_MANTISSA,
162 },
163
164 /* Float */
Markus Armbruster4d400662018-08-23 18:40:09 +0200165 [IN_EXP_DIGITS] = {
Anthony Liguori5ab85582009-11-11 10:39:14 -0600166 TERMINAL(JSON_FLOAT),
Markus Armbruster4d400662018-08-23 18:40:09 +0200167 ['0' ... '9'] = IN_EXP_DIGITS,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600168 },
169
Markus Armbruster4d400662018-08-23 18:40:09 +0200170 [IN_EXP_SIGN] = {
171 ['0' ... '9'] = IN_EXP_DIGITS,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600172 },
173
174 [IN_EXP_E] = {
Markus Armbruster4d400662018-08-23 18:40:09 +0200175 ['-'] = IN_EXP_SIGN,
176 ['+'] = IN_EXP_SIGN,
177 ['0' ... '9'] = IN_EXP_DIGITS,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600178 },
179
180 [IN_MANTISSA_DIGITS] = {
181 TERMINAL(JSON_FLOAT),
182 ['0' ... '9'] = IN_MANTISSA_DIGITS,
183 ['e'] = IN_EXP_E,
184 ['E'] = IN_EXP_E,
185 },
186
187 [IN_MANTISSA] = {
188 ['0' ... '9'] = IN_MANTISSA_DIGITS,
189 },
190
191 /* Number */
Markus Armbruster4d400662018-08-23 18:40:09 +0200192 [IN_DIGITS] = {
Anthony Liguori5ab85582009-11-11 10:39:14 -0600193 TERMINAL(JSON_INTEGER),
Markus Armbruster4d400662018-08-23 18:40:09 +0200194 ['0' ... '9'] = IN_DIGITS,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600195 ['e'] = IN_EXP_E,
196 ['E'] = IN_EXP_E,
197 ['.'] = IN_MANTISSA,
198 },
199
Markus Armbruster4d400662018-08-23 18:40:09 +0200200 [IN_SIGN] = {
Anthony Liguori5ab85582009-11-11 10:39:14 -0600201 ['0'] = IN_ZERO,
Markus Armbruster4d400662018-08-23 18:40:09 +0200202 ['1' ... '9'] = IN_DIGITS,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600203 },
204
205 /* keywords */
206 [IN_KEYWORD] = {
207 TERMINAL(JSON_KEYWORD),
208 ['a' ... 'z'] = IN_KEYWORD,
209 },
210
211 /* whitespace */
212 [IN_WHITESPACE] = {
213 TERMINAL(JSON_SKIP),
214 [' '] = IN_WHITESPACE,
215 ['\t'] = IN_WHITESPACE,
216 ['\r'] = IN_WHITESPACE,
217 ['\n'] = IN_WHITESPACE,
Eric Blakeff5394a2016-06-09 20:48:06 -0600218 },
Anthony Liguori5ab85582009-11-11 10:39:14 -0600219
Markus Armbruster61030282018-08-23 18:40:04 +0200220 /* interpolation */
Markus Armbruster61030282018-08-23 18:40:04 +0200221 [IN_INTERP] = {
Markus Armbrusterf7617d42018-08-23 18:40:07 +0200222 TERMINAL(JSON_INTERP),
223 ['A' ... 'Z'] = IN_INTERP,
224 ['a' ... 'z'] = IN_INTERP,
225 ['0' ... '9'] = IN_INTERP,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600226 },
227
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200228 /*
229 * Two start states:
230 * - IN_START recognizes JSON tokens with our string extensions
231 * - IN_START_INTERP additionally recognizes interpolation.
232 */
233 [IN_START ... IN_START_INTERP] = {
Anthony Liguori5ab85582009-11-11 10:39:14 -0600234 ['"'] = IN_DQ_STRING,
235 ['\''] = IN_SQ_STRING,
236 ['0'] = IN_ZERO,
Markus Armbruster4d400662018-08-23 18:40:09 +0200237 ['1' ... '9'] = IN_DIGITS,
238 ['-'] = IN_SIGN,
Markus Armbrusterc5461662015-11-25 22:23:26 +0100239 ['{'] = JSON_LCURLY,
240 ['}'] = JSON_RCURLY,
241 ['['] = JSON_LSQUARE,
242 [']'] = JSON_RSQUARE,
243 [','] = JSON_COMMA,
244 [':'] = JSON_COLON,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600245 ['a' ... 'z'] = IN_KEYWORD,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600246 [' '] = IN_WHITESPACE,
247 ['\t'] = IN_WHITESPACE,
248 ['\r'] = IN_WHITESPACE,
249 ['\n'] = IN_WHITESPACE,
250 },
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200251 [IN_START_INTERP]['%'] = IN_INTERP,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600252};
253
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200254void json_lexer_init(JSONLexer *lexer, bool enable_interpolation)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600255{
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200256 lexer->start_state = lexer->state = enable_interpolation
257 ? IN_START_INTERP : IN_START;
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100258 lexer->token = g_string_sized_new(3);
Luiz Capitulino03308f62010-05-17 17:50:01 -0300259 lexer->x = lexer->y = 0;
Anthony Liguori5ab85582009-11-11 10:39:14 -0600260}
261
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200262static void json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600263{
Markus Armbruster852dfa72018-08-31 09:58:37 +0200264 int new_state;
265 bool char_consumed = false;
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200266
Anthony Liguori5ab85582009-11-11 10:39:14 -0600267 lexer->x++;
268 if (ch == '\n') {
269 lexer->x = 0;
270 lexer->y++;
271 }
272
Markus Armbruster852dfa72018-08-31 09:58:37 +0200273 while (flush ? lexer->state != lexer->start_state : !char_consumed) {
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];
Markus Armbruster852dfa72018-08-31 09:58:37 +0200276 char_consumed = !flush
277 && !TERMINAL_NEEDED_LOOKAHEAD(lexer->state, new_state);
278 if (char_consumed) {
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100279 g_string_append_c(lexer->token, ch);
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200280 }
Anthony Liguori5ab85582009-11-11 10:39:14 -0600281
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200282 switch (new_state) {
Markus Armbrusterc5461662015-11-25 22:23:26 +0100283 case JSON_LCURLY:
284 case JSON_RCURLY:
285 case JSON_LSQUARE:
286 case JSON_RSQUARE:
287 case JSON_COLON:
288 case JSON_COMMA:
Markus Armbruster61030282018-08-23 18:40:04 +0200289 case JSON_INTERP:
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200290 case JSON_INTEGER:
291 case JSON_FLOAT:
292 case JSON_KEYWORD:
293 case JSON_STRING:
Markus Armbruster037f2442018-08-23 18:40:00 +0200294 json_message_process_token(lexer, lexer->token, new_state,
295 lexer->x, lexer->y);
Stefan Weil0b0404b2012-01-09 18:29:51 +0100296 /* fall through */
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200297 case JSON_SKIP:
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100298 g_string_truncate(lexer->token, 0);
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200299 new_state = lexer->start_state;
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200300 break;
Blue Swirl33d05392011-03-27 09:07:54 +0000301 case IN_ERROR:
Michael Rothb011f612011-06-01 12:14:58 -0500302 /* 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
Markus Armbruster84a56f32018-08-23 18:40:06 +0200305 * parser by emitting a JSON_ERROR token, then reset lexer state.
Michael Rothb011f612011-06-01 12:14:58 -0500306 *
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.
313 */
Markus Armbruster037f2442018-08-23 18:40:00 +0200314 json_message_process_token(lexer, lexer->token, JSON_ERROR,
315 lexer->x, lexer->y);
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100316 g_string_truncate(lexer->token, 0);
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200317 lexer->state = lexer->start_state;
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200318 return;
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200319 default:
320 break;
321 }
322 lexer->state = new_state;
Markus Armbruster852dfa72018-08-31 09:58:37 +0200323 }
Anthony Liguori325601b2011-06-01 12:14:52 -0500324
325 /* Do not let a single token grow to an arbitrarily large size,
326 * this is a security consideration.
327 */
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100328 if (lexer->token->len > MAX_TOKEN_SIZE) {
Markus Armbruster037f2442018-08-23 18:40:00 +0200329 json_message_process_token(lexer, lexer->token, lexer->state,
330 lexer->x, lexer->y);
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100331 g_string_truncate(lexer->token, 0);
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200332 lexer->state = lexer->start_state;
Anthony Liguori325601b2011-06-01 12:14:52 -0500333 }
Anthony Liguori5ab85582009-11-11 10:39:14 -0600334}
335
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200336void json_lexer_feed(JSONLexer *lexer, const char *buffer, size_t size)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600337{
338 size_t i;
339
340 for (i = 0; i < size; i++) {
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200341 json_lexer_feed_char(lexer, buffer[i], false);
Anthony Liguori5ab85582009-11-11 10:39:14 -0600342 }
Anthony Liguori5ab85582009-11-11 10:39:14 -0600343}
344
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200345void json_lexer_flush(JSONLexer *lexer)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600346{
Markus Armbruster852dfa72018-08-31 09:58:37 +0200347 json_lexer_feed_char(lexer, 0, true);
348 assert(lexer->state == lexer->start_state);
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}