blob: 93fa2737e695a1fe811b8ee12142f57fd7eed227 [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"
Anthony Liguori5ab85582009-11-11 10:39:14 -060015#include "qemu-common.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010016#include "qapi/qmp/json-lexer.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
83 *
84 *
85 * Extensions over RFC 8259:
86 * - Extra escape sequence in strings:
87 * 0x27 (apostrophe) is recognized after escape, too
88 * - Single-quoted strings:
89 * Like double-quoted strings, except they're delimited by %x27
90 * (apostrophe) instead of %x22 (quotation mark), and can't contain
91 * unescaped apostrophe, but can contain unescaped quotation mark.
92 * - Interpolation:
93 * interpolation = %((l|ll|I64)[du]|[ipsf])
94 *
95 * Note:
96 * - Input must be encoded in UTF-8.
97 * - Decoding and validating is left to the parser.
Anthony Liguori5ab85582009-11-11 10:39:14 -060098 */
99
100enum json_lexer_state {
Markus Armbrusterb8d3b1d2015-11-25 22:23:25 +0100101 IN_ERROR = 0, /* must really be 0, see json_lexer[] */
Anthony Liguori5ab85582009-11-11 10:39:14 -0600102 IN_DQ_UCODE3,
103 IN_DQ_UCODE2,
104 IN_DQ_UCODE1,
105 IN_DQ_UCODE0,
106 IN_DQ_STRING_ESCAPE,
107 IN_DQ_STRING,
108 IN_SQ_UCODE3,
109 IN_SQ_UCODE2,
110 IN_SQ_UCODE1,
111 IN_SQ_UCODE0,
112 IN_SQ_STRING_ESCAPE,
113 IN_SQ_STRING,
114 IN_ZERO,
115 IN_DIGITS,
116 IN_DIGIT,
117 IN_EXP_E,
118 IN_MANTISSA,
119 IN_MANTISSA_DIGITS,
120 IN_NONZERO_NUMBER,
121 IN_NEG_NONZERO_NUMBER,
122 IN_KEYWORD,
123 IN_ESCAPE,
124 IN_ESCAPE_L,
125 IN_ESCAPE_LL,
Roy Tam2c0d4b32010-02-04 10:30:30 +0800126 IN_ESCAPE_I,
127 IN_ESCAPE_I6,
128 IN_ESCAPE_I64,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600129 IN_WHITESPACE,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600130 IN_START,
131};
132
Markus Armbrusterb8d3b1d2015-11-25 22:23:25 +0100133QEMU_BUILD_BUG_ON((int)JSON_MIN <= (int)IN_START);
134
Anthony Liguori5ab85582009-11-11 10:39:14 -0600135#define TERMINAL(state) [0 ... 0x7F] = (state)
136
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200137/* Return whether TERMINAL is a terminal state and the transition to it
138 from OLD_STATE required lookahead. This happens whenever the table
139 below uses the TERMINAL macro. */
140#define TERMINAL_NEEDED_LOOKAHEAD(old_state, terminal) \
Markus Armbrustera2ec6be2018-08-23 18:39:44 +0200141 (terminal != IN_ERROR && json_lexer[(old_state)][0] == (terminal))
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200142
Anthony Liguori5ab85582009-11-11 10:39:14 -0600143static const uint8_t json_lexer[][256] = {
Markus Armbrusterb8d3b1d2015-11-25 22:23:25 +0100144 /* Relies on default initialization to IN_ERROR! */
145
Anthony Liguori5ab85582009-11-11 10:39:14 -0600146 /* double quote string */
147 [IN_DQ_UCODE3] = {
148 ['0' ... '9'] = IN_DQ_STRING,
149 ['a' ... 'f'] = IN_DQ_STRING,
150 ['A' ... 'F'] = IN_DQ_STRING,
151 },
152 [IN_DQ_UCODE2] = {
153 ['0' ... '9'] = IN_DQ_UCODE3,
154 ['a' ... 'f'] = IN_DQ_UCODE3,
155 ['A' ... 'F'] = IN_DQ_UCODE3,
156 },
157 [IN_DQ_UCODE1] = {
158 ['0' ... '9'] = IN_DQ_UCODE2,
159 ['a' ... 'f'] = IN_DQ_UCODE2,
160 ['A' ... 'F'] = IN_DQ_UCODE2,
161 },
162 [IN_DQ_UCODE0] = {
163 ['0' ... '9'] = IN_DQ_UCODE1,
164 ['a' ... 'f'] = IN_DQ_UCODE1,
165 ['A' ... 'F'] = IN_DQ_UCODE1,
166 },
167 [IN_DQ_STRING_ESCAPE] = {
168 ['b'] = IN_DQ_STRING,
169 ['f'] = IN_DQ_STRING,
170 ['n'] = IN_DQ_STRING,
171 ['r'] = IN_DQ_STRING,
172 ['t'] = IN_DQ_STRING,
Luiz Capitulino1041ba72010-05-19 16:57:28 -0300173 ['/'] = IN_DQ_STRING,
174 ['\\'] = IN_DQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600175 ['\''] = IN_DQ_STRING,
176 ['\"'] = IN_DQ_STRING,
177 ['u'] = IN_DQ_UCODE0,
178 },
179 [IN_DQ_STRING] = {
Markus Armbrusterde930f42018-08-23 18:39:51 +0200180 [0x20 ... 0xFD] = IN_DQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600181 ['\\'] = IN_DQ_STRING_ESCAPE,
Paolo Bonzini28e91a62010-05-24 09:39:53 +0200182 ['"'] = JSON_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600183 },
184
185 /* single quote string */
186 [IN_SQ_UCODE3] = {
187 ['0' ... '9'] = IN_SQ_STRING,
188 ['a' ... 'f'] = IN_SQ_STRING,
189 ['A' ... 'F'] = IN_SQ_STRING,
190 },
191 [IN_SQ_UCODE2] = {
192 ['0' ... '9'] = IN_SQ_UCODE3,
193 ['a' ... 'f'] = IN_SQ_UCODE3,
194 ['A' ... 'F'] = IN_SQ_UCODE3,
195 },
196 [IN_SQ_UCODE1] = {
197 ['0' ... '9'] = IN_SQ_UCODE2,
198 ['a' ... 'f'] = IN_SQ_UCODE2,
199 ['A' ... 'F'] = IN_SQ_UCODE2,
200 },
201 [IN_SQ_UCODE0] = {
202 ['0' ... '9'] = IN_SQ_UCODE1,
203 ['a' ... 'f'] = IN_SQ_UCODE1,
204 ['A' ... 'F'] = IN_SQ_UCODE1,
205 },
206 [IN_SQ_STRING_ESCAPE] = {
207 ['b'] = IN_SQ_STRING,
208 ['f'] = IN_SQ_STRING,
209 ['n'] = IN_SQ_STRING,
210 ['r'] = IN_SQ_STRING,
211 ['t'] = IN_SQ_STRING,
Paolo Bonzinid5932332014-06-13 10:13:02 +0200212 ['/'] = IN_SQ_STRING,
213 ['\\'] = IN_SQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600214 ['\''] = IN_SQ_STRING,
215 ['\"'] = IN_SQ_STRING,
216 ['u'] = IN_SQ_UCODE0,
217 },
218 [IN_SQ_STRING] = {
Markus Armbrusterde930f42018-08-23 18:39:51 +0200219 [0x20 ... 0xFD] = IN_SQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600220 ['\\'] = IN_SQ_STRING_ESCAPE,
Paolo Bonzini28e91a62010-05-24 09:39:53 +0200221 ['\''] = JSON_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600222 },
223
224 /* Zero */
225 [IN_ZERO] = {
226 TERMINAL(JSON_INTEGER),
Blue Swirl33d05392011-03-27 09:07:54 +0000227 ['0' ... '9'] = IN_ERROR,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600228 ['.'] = IN_MANTISSA,
229 },
230
231 /* Float */
232 [IN_DIGITS] = {
233 TERMINAL(JSON_FLOAT),
234 ['0' ... '9'] = IN_DIGITS,
235 },
236
237 [IN_DIGIT] = {
238 ['0' ... '9'] = IN_DIGITS,
239 },
240
241 [IN_EXP_E] = {
242 ['-'] = IN_DIGIT,
243 ['+'] = IN_DIGIT,
244 ['0' ... '9'] = IN_DIGITS,
245 },
246
247 [IN_MANTISSA_DIGITS] = {
248 TERMINAL(JSON_FLOAT),
249 ['0' ... '9'] = IN_MANTISSA_DIGITS,
250 ['e'] = IN_EXP_E,
251 ['E'] = IN_EXP_E,
252 },
253
254 [IN_MANTISSA] = {
255 ['0' ... '9'] = IN_MANTISSA_DIGITS,
256 },
257
258 /* Number */
259 [IN_NONZERO_NUMBER] = {
260 TERMINAL(JSON_INTEGER),
261 ['0' ... '9'] = IN_NONZERO_NUMBER,
262 ['e'] = IN_EXP_E,
263 ['E'] = IN_EXP_E,
264 ['.'] = IN_MANTISSA,
265 },
266
267 [IN_NEG_NONZERO_NUMBER] = {
268 ['0'] = IN_ZERO,
269 ['1' ... '9'] = IN_NONZERO_NUMBER,
270 },
271
272 /* keywords */
273 [IN_KEYWORD] = {
274 TERMINAL(JSON_KEYWORD),
275 ['a' ... 'z'] = IN_KEYWORD,
276 },
277
278 /* whitespace */
279 [IN_WHITESPACE] = {
280 TERMINAL(JSON_SKIP),
281 [' '] = IN_WHITESPACE,
282 ['\t'] = IN_WHITESPACE,
283 ['\r'] = IN_WHITESPACE,
284 ['\n'] = IN_WHITESPACE,
Eric Blakeff5394a2016-06-09 20:48:06 -0600285 },
Anthony Liguori5ab85582009-11-11 10:39:14 -0600286
Anthony Liguori5ab85582009-11-11 10:39:14 -0600287 /* escape */
Anthony Liguori5ab85582009-11-11 10:39:14 -0600288 [IN_ESCAPE_LL] = {
Paolo Bonzini28e91a62010-05-24 09:39:53 +0200289 ['d'] = JSON_ESCAPE,
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400290 ['u'] = JSON_ESCAPE,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600291 },
292
293 [IN_ESCAPE_L] = {
Paolo Bonzini28e91a62010-05-24 09:39:53 +0200294 ['d'] = JSON_ESCAPE,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600295 ['l'] = IN_ESCAPE_LL,
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400296 ['u'] = JSON_ESCAPE,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600297 },
298
Roy Tam2c0d4b32010-02-04 10:30:30 +0800299 [IN_ESCAPE_I64] = {
Paolo Bonzini28e91a62010-05-24 09:39:53 +0200300 ['d'] = JSON_ESCAPE,
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400301 ['u'] = JSON_ESCAPE,
Roy Tam2c0d4b32010-02-04 10:30:30 +0800302 },
303
304 [IN_ESCAPE_I6] = {
305 ['4'] = IN_ESCAPE_I64,
306 },
307
308 [IN_ESCAPE_I] = {
309 ['6'] = IN_ESCAPE_I6,
310 },
311
Anthony Liguori5ab85582009-11-11 10:39:14 -0600312 [IN_ESCAPE] = {
Paolo Bonzini28e91a62010-05-24 09:39:53 +0200313 ['d'] = JSON_ESCAPE,
314 ['i'] = JSON_ESCAPE,
315 ['p'] = JSON_ESCAPE,
316 ['s'] = JSON_ESCAPE,
Marc-André Lureau2bc7cfe2017-06-07 20:36:02 +0400317 ['u'] = JSON_ESCAPE,
Paolo Bonzini28e91a62010-05-24 09:39:53 +0200318 ['f'] = JSON_ESCAPE,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600319 ['l'] = IN_ESCAPE_L,
Roy Tam2c0d4b32010-02-04 10:30:30 +0800320 ['I'] = IN_ESCAPE_I,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600321 },
322
323 /* top level rule */
324 [IN_START] = {
325 ['"'] = IN_DQ_STRING,
326 ['\''] = IN_SQ_STRING,
327 ['0'] = IN_ZERO,
328 ['1' ... '9'] = IN_NONZERO_NUMBER,
329 ['-'] = IN_NEG_NONZERO_NUMBER,
Markus Armbrusterc5461662015-11-25 22:23:26 +0100330 ['{'] = JSON_LCURLY,
331 ['}'] = JSON_RCURLY,
332 ['['] = JSON_LSQUARE,
333 [']'] = JSON_RSQUARE,
334 [','] = JSON_COMMA,
335 [':'] = JSON_COLON,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600336 ['a' ... 'z'] = IN_KEYWORD,
337 ['%'] = IN_ESCAPE,
338 [' '] = IN_WHITESPACE,
339 ['\t'] = IN_WHITESPACE,
340 ['\r'] = IN_WHITESPACE,
341 ['\n'] = IN_WHITESPACE,
342 },
343};
344
345void json_lexer_init(JSONLexer *lexer, JSONLexerEmitter func)
346{
347 lexer->emit = func;
348 lexer->state = IN_START;
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100349 lexer->token = g_string_sized_new(3);
Luiz Capitulino03308f62010-05-17 17:50:01 -0300350 lexer->x = lexer->y = 0;
Anthony Liguori5ab85582009-11-11 10:39:14 -0600351}
352
Michael Rothbd3924a2011-06-01 12:14:57 -0500353static int json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600354{
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200355 int char_consumed, new_state;
356
Anthony Liguori5ab85582009-11-11 10:39:14 -0600357 lexer->x++;
358 if (ch == '\n') {
359 lexer->x = 0;
360 lexer->y++;
361 }
362
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200363 do {
Markus Armbrusterb8d3b1d2015-11-25 22:23:25 +0100364 assert(lexer->state <= ARRAY_SIZE(json_lexer));
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200365 new_state = json_lexer[lexer->state][(uint8_t)ch];
366 char_consumed = !TERMINAL_NEEDED_LOOKAHEAD(lexer->state, new_state);
Markus Armbrustera2ec6be2018-08-23 18:39:44 +0200367 if (char_consumed && !flush) {
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100368 g_string_append_c(lexer->token, ch);
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200369 }
Anthony Liguori5ab85582009-11-11 10:39:14 -0600370
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200371 switch (new_state) {
Markus Armbrusterc5461662015-11-25 22:23:26 +0100372 case JSON_LCURLY:
373 case JSON_RCURLY:
374 case JSON_LSQUARE:
375 case JSON_RSQUARE:
376 case JSON_COLON:
377 case JSON_COMMA:
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200378 case JSON_ESCAPE:
379 case JSON_INTEGER:
380 case JSON_FLOAT:
381 case JSON_KEYWORD:
382 case JSON_STRING:
383 lexer->emit(lexer, lexer->token, new_state, lexer->x, lexer->y);
Stefan Weil0b0404b2012-01-09 18:29:51 +0100384 /* fall through */
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200385 case JSON_SKIP:
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100386 g_string_truncate(lexer->token, 0);
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200387 new_state = IN_START;
388 break;
Blue Swirl33d05392011-03-27 09:07:54 +0000389 case IN_ERROR:
Michael Rothb011f612011-06-01 12:14:58 -0500390 /* XXX: To avoid having previous bad input leaving the parser in an
391 * unresponsive state where we consume unpredictable amounts of
392 * subsequent "good" input, percolate this error state up to the
393 * tokenizer/parser by forcing a NULL object to be emitted, then
394 * reset state.
395 *
396 * Also note that this handling is required for reliable channel
397 * negotiation between QMP and the guest agent, since chr(0xFF)
398 * is placed at the beginning of certain events to ensure proper
399 * delivery when the channel is in an unknown state. chr(0xFF) is
400 * never a valid ASCII/UTF-8 sequence, so this should reliably
401 * induce an error/flush state.
402 */
403 lexer->emit(lexer, lexer->token, JSON_ERROR, lexer->x, lexer->y);
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100404 g_string_truncate(lexer->token, 0);
Anthony Liguori529a0ef2011-06-01 12:14:56 -0500405 new_state = IN_START;
Michael Rothb011f612011-06-01 12:14:58 -0500406 lexer->state = new_state;
407 return 0;
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200408 default:
409 break;
410 }
411 lexer->state = new_state;
Michael Rothbd3924a2011-06-01 12:14:57 -0500412 } while (!char_consumed && !flush);
Anthony Liguori325601b2011-06-01 12:14:52 -0500413
414 /* Do not let a single token grow to an arbitrarily large size,
415 * this is a security consideration.
416 */
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100417 if (lexer->token->len > MAX_TOKEN_SIZE) {
Anthony Liguori325601b2011-06-01 12:14:52 -0500418 lexer->emit(lexer, lexer->token, lexer->state, lexer->x, lexer->y);
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100419 g_string_truncate(lexer->token, 0);
Anthony Liguori325601b2011-06-01 12:14:52 -0500420 lexer->state = IN_START;
421 }
422
Anthony Liguori5ab85582009-11-11 10:39:14 -0600423 return 0;
424}
425
426int json_lexer_feed(JSONLexer *lexer, const char *buffer, size_t size)
427{
428 size_t i;
429
430 for (i = 0; i < size; i++) {
431 int err;
432
Michael Rothbd3924a2011-06-01 12:14:57 -0500433 err = json_lexer_feed_char(lexer, buffer[i], false);
Anthony Liguori5ab85582009-11-11 10:39:14 -0600434 if (err < 0) {
435 return err;
436 }
437 }
438
439 return 0;
440}
441
442int json_lexer_flush(JSONLexer *lexer)
443{
Michael Rothb011f612011-06-01 12:14:58 -0500444 return lexer->state == IN_START ? 0 : json_lexer_feed_char(lexer, 0, true);
Anthony Liguori5ab85582009-11-11 10:39:14 -0600445}
446
447void json_lexer_destroy(JSONLexer *lexer)
448{
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100449 g_string_free(lexer->token, true);
Anthony Liguori5ab85582009-11-11 10:39:14 -0600450}