blob: 2a5561c917adfe1bc534d2eaf01f3866cccb92ad [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 Armbruster2ce4ee62018-08-31 09:58:40 +0200103 IN_RECOVERY = 1,
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 Armbruster2ce4ee62018-08-31 09:58:40 +0200123QEMU_BUILD_BUG_ON(JSON_ERROR != 0);
124QEMU_BUILD_BUG_ON(IN_RECOVERY != JSON_ERROR + 1);
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200125QEMU_BUILD_BUG_ON((int)JSON_MIN <= (int)IN_START_INTERP);
Markus Armbrusterc0ee3af2018-08-31 09:58:38 +0200126QEMU_BUILD_BUG_ON(JSON_MAX >= 0x80);
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200127QEMU_BUILD_BUG_ON(IN_START_INTERP != IN_START + 1);
Markus Armbrusterb8d3b1d2015-11-25 22:23:25 +0100128
Markus Armbrusterc0ee3af2018-08-31 09:58:38 +0200129#define LOOKAHEAD 0x80
130#define TERMINAL(state) [0 ... 0xFF] = ((state) | LOOKAHEAD)
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200131
Anthony Liguori5ab85582009-11-11 10:39:14 -0600132static const uint8_t json_lexer[][256] = {
Markus Armbrusterb8d3b1d2015-11-25 22:23:25 +0100133 /* Relies on default initialization to IN_ERROR! */
134
Markus Armbruster0f07a5d2018-08-31 09:58:39 +0200135 /* error recovery */
136 [IN_RECOVERY] = {
137 /*
138 * Skip characters until a structural character, an ASCII
139 * control character other than '\t', or impossible UTF-8
140 * bytes '\xFE', '\xFF'. Structural characters and line
141 * endings are promising resynchronization points. Clients
142 * may use the others to force the JSON parser into known-good
143 * state; see docs/interop/qmp-spec.txt.
144 */
145 [0 ... 0x1F] = IN_START | LOOKAHEAD,
146 [0x20 ... 0xFD] = IN_RECOVERY,
147 [0xFE ... 0xFF] = IN_START | LOOKAHEAD,
148 ['\t'] = IN_RECOVERY,
149 ['['] = IN_START | LOOKAHEAD,
150 [']'] = IN_START | LOOKAHEAD,
151 ['{'] = IN_START | LOOKAHEAD,
152 ['}'] = IN_START | LOOKAHEAD,
153 [':'] = IN_START | LOOKAHEAD,
154 [','] = IN_START | LOOKAHEAD,
155 },
156
Anthony Liguori5ab85582009-11-11 10:39:14 -0600157 /* double quote string */
Anthony Liguori5ab85582009-11-11 10:39:14 -0600158 [IN_DQ_STRING_ESCAPE] = {
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200159 [0x20 ... 0xFD] = IN_DQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600160 },
161 [IN_DQ_STRING] = {
Markus Armbrusterde930f42018-08-23 18:39:51 +0200162 [0x20 ... 0xFD] = IN_DQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600163 ['\\'] = IN_DQ_STRING_ESCAPE,
Paolo Bonzini28e91a62010-05-24 09:39:53 +0200164 ['"'] = JSON_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600165 },
166
167 /* single quote string */
Anthony Liguori5ab85582009-11-11 10:39:14 -0600168 [IN_SQ_STRING_ESCAPE] = {
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200169 [0x20 ... 0xFD] = IN_SQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600170 },
171 [IN_SQ_STRING] = {
Markus Armbrusterde930f42018-08-23 18:39:51 +0200172 [0x20 ... 0xFD] = IN_SQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600173 ['\\'] = IN_SQ_STRING_ESCAPE,
Paolo Bonzini28e91a62010-05-24 09:39:53 +0200174 ['\''] = JSON_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600175 },
176
177 /* Zero */
178 [IN_ZERO] = {
179 TERMINAL(JSON_INTEGER),
Markus Armbruster2ce4ee62018-08-31 09:58:40 +0200180 ['0' ... '9'] = JSON_ERROR,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600181 ['.'] = IN_MANTISSA,
182 },
183
184 /* Float */
Markus Armbruster4d400662018-08-23 18:40:09 +0200185 [IN_EXP_DIGITS] = {
Anthony Liguori5ab85582009-11-11 10:39:14 -0600186 TERMINAL(JSON_FLOAT),
Markus Armbruster4d400662018-08-23 18:40:09 +0200187 ['0' ... '9'] = IN_EXP_DIGITS,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600188 },
189
Markus Armbruster4d400662018-08-23 18:40:09 +0200190 [IN_EXP_SIGN] = {
191 ['0' ... '9'] = IN_EXP_DIGITS,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600192 },
193
194 [IN_EXP_E] = {
Markus Armbruster4d400662018-08-23 18:40:09 +0200195 ['-'] = IN_EXP_SIGN,
196 ['+'] = IN_EXP_SIGN,
197 ['0' ... '9'] = IN_EXP_DIGITS,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600198 },
199
200 [IN_MANTISSA_DIGITS] = {
201 TERMINAL(JSON_FLOAT),
202 ['0' ... '9'] = IN_MANTISSA_DIGITS,
203 ['e'] = IN_EXP_E,
204 ['E'] = IN_EXP_E,
205 },
206
207 [IN_MANTISSA] = {
208 ['0' ... '9'] = IN_MANTISSA_DIGITS,
209 },
210
211 /* Number */
Markus Armbruster4d400662018-08-23 18:40:09 +0200212 [IN_DIGITS] = {
Anthony Liguori5ab85582009-11-11 10:39:14 -0600213 TERMINAL(JSON_INTEGER),
Markus Armbruster4d400662018-08-23 18:40:09 +0200214 ['0' ... '9'] = IN_DIGITS,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600215 ['e'] = IN_EXP_E,
216 ['E'] = IN_EXP_E,
217 ['.'] = IN_MANTISSA,
218 },
219
Markus Armbruster4d400662018-08-23 18:40:09 +0200220 [IN_SIGN] = {
Anthony Liguori5ab85582009-11-11 10:39:14 -0600221 ['0'] = IN_ZERO,
Markus Armbruster4d400662018-08-23 18:40:09 +0200222 ['1' ... '9'] = IN_DIGITS,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600223 },
224
225 /* keywords */
226 [IN_KEYWORD] = {
227 TERMINAL(JSON_KEYWORD),
228 ['a' ... 'z'] = IN_KEYWORD,
229 },
230
231 /* whitespace */
232 [IN_WHITESPACE] = {
233 TERMINAL(JSON_SKIP),
234 [' '] = IN_WHITESPACE,
235 ['\t'] = IN_WHITESPACE,
236 ['\r'] = IN_WHITESPACE,
237 ['\n'] = IN_WHITESPACE,
Eric Blakeff5394a2016-06-09 20:48:06 -0600238 },
Anthony Liguori5ab85582009-11-11 10:39:14 -0600239
Markus Armbruster61030282018-08-23 18:40:04 +0200240 /* interpolation */
Markus Armbruster61030282018-08-23 18:40:04 +0200241 [IN_INTERP] = {
Markus Armbrusterf7617d42018-08-23 18:40:07 +0200242 TERMINAL(JSON_INTERP),
243 ['A' ... 'Z'] = IN_INTERP,
244 ['a' ... 'z'] = IN_INTERP,
245 ['0' ... '9'] = IN_INTERP,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600246 },
247
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200248 /*
249 * Two start states:
250 * - IN_START recognizes JSON tokens with our string extensions
251 * - IN_START_INTERP additionally recognizes interpolation.
252 */
253 [IN_START ... IN_START_INTERP] = {
Anthony Liguori5ab85582009-11-11 10:39:14 -0600254 ['"'] = IN_DQ_STRING,
255 ['\''] = IN_SQ_STRING,
256 ['0'] = IN_ZERO,
Markus Armbruster4d400662018-08-23 18:40:09 +0200257 ['1' ... '9'] = IN_DIGITS,
258 ['-'] = IN_SIGN,
Markus Armbrusterc5461662015-11-25 22:23:26 +0100259 ['{'] = JSON_LCURLY,
260 ['}'] = JSON_RCURLY,
261 ['['] = JSON_LSQUARE,
262 [']'] = JSON_RSQUARE,
263 [','] = JSON_COMMA,
264 [':'] = JSON_COLON,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600265 ['a' ... 'z'] = IN_KEYWORD,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600266 [' '] = IN_WHITESPACE,
267 ['\t'] = IN_WHITESPACE,
268 ['\r'] = IN_WHITESPACE,
269 ['\n'] = IN_WHITESPACE,
270 },
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200271 [IN_START_INTERP]['%'] = IN_INTERP,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600272};
273
Markus Armbrusterc0ee3af2018-08-31 09:58:38 +0200274static inline uint8_t next_state(JSONLexer *lexer, char ch, bool flush,
275 bool *char_consumed)
276{
277 uint8_t next;
278
279 assert(lexer->state <= ARRAY_SIZE(json_lexer));
280 next = json_lexer[lexer->state][(uint8_t)ch];
281 *char_consumed = !flush && !(next & LOOKAHEAD);
282 return next & ~LOOKAHEAD;
283}
284
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200285void json_lexer_init(JSONLexer *lexer, bool enable_interpolation)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600286{
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200287 lexer->start_state = lexer->state = enable_interpolation
288 ? IN_START_INTERP : IN_START;
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100289 lexer->token = g_string_sized_new(3);
Luiz Capitulino03308f62010-05-17 17:50:01 -0300290 lexer->x = lexer->y = 0;
Anthony Liguori5ab85582009-11-11 10:39:14 -0600291}
292
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200293static void json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600294{
Markus Armbruster852dfa72018-08-31 09:58:37 +0200295 int new_state;
296 bool char_consumed = false;
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200297
Anthony Liguori5ab85582009-11-11 10:39:14 -0600298 lexer->x++;
299 if (ch == '\n') {
300 lexer->x = 0;
301 lexer->y++;
302 }
303
Markus Armbruster852dfa72018-08-31 09:58:37 +0200304 while (flush ? lexer->state != lexer->start_state : !char_consumed) {
Markus Armbrusterc0ee3af2018-08-31 09:58:38 +0200305 new_state = next_state(lexer, ch, flush, &char_consumed);
Markus Armbruster852dfa72018-08-31 09:58:37 +0200306 if (char_consumed) {
Markus Armbrusterc0ee3af2018-08-31 09:58:38 +0200307 assert(!flush);
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100308 g_string_append_c(lexer->token, ch);
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200309 }
Anthony Liguori5ab85582009-11-11 10:39:14 -0600310
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200311 switch (new_state) {
Markus Armbrusterc5461662015-11-25 22:23:26 +0100312 case JSON_LCURLY:
313 case JSON_RCURLY:
314 case JSON_LSQUARE:
315 case JSON_RSQUARE:
316 case JSON_COLON:
317 case JSON_COMMA:
Markus Armbruster61030282018-08-23 18:40:04 +0200318 case JSON_INTERP:
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200319 case JSON_INTEGER:
320 case JSON_FLOAT:
321 case JSON_KEYWORD:
322 case JSON_STRING:
Markus Armbruster037f2442018-08-23 18:40:00 +0200323 json_message_process_token(lexer, lexer->token, new_state,
324 lexer->x, lexer->y);
Stefan Weil0b0404b2012-01-09 18:29:51 +0100325 /* fall through */
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200326 case JSON_SKIP:
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100327 g_string_truncate(lexer->token, 0);
Markus Armbruster0f07a5d2018-08-31 09:58:39 +0200328 /* fall through */
329 case IN_START:
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200330 new_state = lexer->start_state;
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200331 break;
Markus Armbruster2ce4ee62018-08-31 09:58:40 +0200332 case JSON_ERROR:
Markus Armbruster037f2442018-08-23 18:40:00 +0200333 json_message_process_token(lexer, lexer->token, JSON_ERROR,
334 lexer->x, lexer->y);
Markus Armbruster0f07a5d2018-08-31 09:58:39 +0200335 new_state = IN_RECOVERY;
336 /* fall through */
337 case IN_RECOVERY:
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100338 g_string_truncate(lexer->token, 0);
Markus Armbruster0f07a5d2018-08-31 09:58:39 +0200339 break;
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200340 default:
341 break;
342 }
343 lexer->state = new_state;
Markus Armbruster852dfa72018-08-31 09:58:37 +0200344 }
Anthony Liguori325601b2011-06-01 12:14:52 -0500345
346 /* Do not let a single token grow to an arbitrarily large size,
347 * this is a security consideration.
348 */
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100349 if (lexer->token->len > MAX_TOKEN_SIZE) {
Markus Armbruster037f2442018-08-23 18:40:00 +0200350 json_message_process_token(lexer, lexer->token, lexer->state,
351 lexer->x, lexer->y);
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100352 g_string_truncate(lexer->token, 0);
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200353 lexer->state = lexer->start_state;
Anthony Liguori325601b2011-06-01 12:14:52 -0500354 }
Anthony Liguori5ab85582009-11-11 10:39:14 -0600355}
356
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200357void json_lexer_feed(JSONLexer *lexer, const char *buffer, size_t size)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600358{
359 size_t i;
360
361 for (i = 0; i < size; i++) {
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200362 json_lexer_feed_char(lexer, buffer[i], false);
Anthony Liguori5ab85582009-11-11 10:39:14 -0600363 }
Anthony Liguori5ab85582009-11-11 10:39:14 -0600364}
365
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200366void json_lexer_flush(JSONLexer *lexer)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600367{
Markus Armbruster852dfa72018-08-31 09:58:37 +0200368 json_lexer_feed_char(lexer, 0, true);
369 assert(lexer->state == lexer->start_state);
Markus Armbrusterf9277912018-08-23 18:40:12 +0200370 json_message_process_token(lexer, lexer->token, JSON_END_OF_INPUT,
371 lexer->x, lexer->y);
Anthony Liguori5ab85582009-11-11 10:39:14 -0600372}
373
374void json_lexer_destroy(JSONLexer *lexer)
375{
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100376 g_string_free(lexer->token, true);
Anthony Liguori5ab85582009-11-11 10:39:14 -0600377}