]>
git.proxmox.com Git - mirror_qemu.git/blob - qobject/json-lexer.c
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"
18 #define MAX_TOKEN_SIZE (64ULL << 20)
21 * From RFC 8259 "The JavaScript Object Notation (JSON) Data
22 * Interchange Format", with [comments in brackets]:
24 * The set of tokens includes six structural characters, strings,
25 * numbers, and three literal names.
27 * These are the six structural characters:
29 * 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
36 * 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
44 * %x09 / ; Horizontal tab
45 * %x0A / ; Line feed or New line
46 * %x0D ) ; Carriage return
48 * [...] three literal names:
50 * [This lexer accepts [a-z]+, and leaves rejecting unknown literal
51 * names to the parser.]
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 )
67 * string = quotation-mark *char quotation-mark
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
81 * quotation-mark = %x22 ; "
82 * unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
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.
93 * interpolation = %((l|ll|I64)[du]|[ipsf])
96 * - Input must be encoded in UTF-8.
97 * - Decoding and validating is left to the parser.
100 enum json_lexer_state
{
101 IN_ERROR
= 0, /* must really be 0, see json_lexer[] */
121 IN_NEG_NONZERO_NUMBER
,
133 QEMU_BUILD_BUG_ON((int)JSON_MIN
<= (int)IN_START
);
135 #define TERMINAL(state) [0 ... 0x7F] = (state)
137 /* 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) \
141 (terminal != IN_ERROR && json_lexer[(old_state)][0] == (terminal))
143 static const uint8_t json_lexer
[][256] = {
144 /* Relies on default initialization to IN_ERROR! */
146 /* double quote string */
148 ['0' ... '9'] = IN_DQ_STRING
,
149 ['a' ... 'f'] = IN_DQ_STRING
,
150 ['A' ... 'F'] = IN_DQ_STRING
,
153 ['0' ... '9'] = IN_DQ_UCODE3
,
154 ['a' ... 'f'] = IN_DQ_UCODE3
,
155 ['A' ... 'F'] = IN_DQ_UCODE3
,
158 ['0' ... '9'] = IN_DQ_UCODE2
,
159 ['a' ... 'f'] = IN_DQ_UCODE2
,
160 ['A' ... 'F'] = IN_DQ_UCODE2
,
163 ['0' ... '9'] = IN_DQ_UCODE1
,
164 ['a' ... 'f'] = IN_DQ_UCODE1
,
165 ['A' ... 'F'] = IN_DQ_UCODE1
,
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
,
173 ['/'] = IN_DQ_STRING
,
174 ['\\'] = IN_DQ_STRING
,
175 ['\''] = IN_DQ_STRING
,
176 ['\"'] = IN_DQ_STRING
,
177 ['u'] = IN_DQ_UCODE0
,
180 [0x20 ... 0xFD] = IN_DQ_STRING
,
181 ['\\'] = IN_DQ_STRING_ESCAPE
,
185 /* single quote string */
187 ['0' ... '9'] = IN_SQ_STRING
,
188 ['a' ... 'f'] = IN_SQ_STRING
,
189 ['A' ... 'F'] = IN_SQ_STRING
,
192 ['0' ... '9'] = IN_SQ_UCODE3
,
193 ['a' ... 'f'] = IN_SQ_UCODE3
,
194 ['A' ... 'F'] = IN_SQ_UCODE3
,
197 ['0' ... '9'] = IN_SQ_UCODE2
,
198 ['a' ... 'f'] = IN_SQ_UCODE2
,
199 ['A' ... 'F'] = IN_SQ_UCODE2
,
202 ['0' ... '9'] = IN_SQ_UCODE1
,
203 ['a' ... 'f'] = IN_SQ_UCODE1
,
204 ['A' ... 'F'] = IN_SQ_UCODE1
,
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
,
212 ['/'] = IN_SQ_STRING
,
213 ['\\'] = IN_SQ_STRING
,
214 ['\''] = IN_SQ_STRING
,
215 ['\"'] = IN_SQ_STRING
,
216 ['u'] = IN_SQ_UCODE0
,
219 [0x20 ... 0xFD] = IN_SQ_STRING
,
220 ['\\'] = IN_SQ_STRING_ESCAPE
,
221 ['\''] = JSON_STRING
,
226 TERMINAL(JSON_INTEGER
),
227 ['0' ... '9'] = IN_ERROR
,
233 TERMINAL(JSON_FLOAT
),
234 ['0' ... '9'] = IN_DIGITS
,
238 ['0' ... '9'] = IN_DIGITS
,
244 ['0' ... '9'] = IN_DIGITS
,
247 [IN_MANTISSA_DIGITS
] = {
248 TERMINAL(JSON_FLOAT
),
249 ['0' ... '9'] = IN_MANTISSA_DIGITS
,
255 ['0' ... '9'] = IN_MANTISSA_DIGITS
,
259 [IN_NONZERO_NUMBER
] = {
260 TERMINAL(JSON_INTEGER
),
261 ['0' ... '9'] = IN_NONZERO_NUMBER
,
267 [IN_NEG_NONZERO_NUMBER
] = {
269 ['1' ... '9'] = IN_NONZERO_NUMBER
,
274 TERMINAL(JSON_KEYWORD
),
275 ['a' ... 'z'] = IN_KEYWORD
,
281 [' '] = IN_WHITESPACE
,
282 ['\t'] = IN_WHITESPACE
,
283 ['\r'] = IN_WHITESPACE
,
284 ['\n'] = IN_WHITESPACE
,
295 ['l'] = IN_ESCAPE_LL
,
305 ['4'] = IN_ESCAPE_I64
,
309 ['6'] = IN_ESCAPE_I6
,
325 ['"'] = IN_DQ_STRING
,
326 ['\''] = IN_SQ_STRING
,
328 ['1' ... '9'] = IN_NONZERO_NUMBER
,
329 ['-'] = IN_NEG_NONZERO_NUMBER
,
332 ['['] = JSON_LSQUARE
,
333 [']'] = JSON_RSQUARE
,
336 ['a' ... 'z'] = IN_KEYWORD
,
338 [' '] = IN_WHITESPACE
,
339 ['\t'] = IN_WHITESPACE
,
340 ['\r'] = IN_WHITESPACE
,
341 ['\n'] = IN_WHITESPACE
,
345 void json_lexer_init(JSONLexer
*lexer
, JSONLexerEmitter func
)
348 lexer
->state
= IN_START
;
349 lexer
->token
= g_string_sized_new(3);
350 lexer
->x
= lexer
->y
= 0;
353 static int json_lexer_feed_char(JSONLexer
*lexer
, char ch
, bool flush
)
355 int char_consumed
, new_state
;
364 assert(lexer
->state
<= ARRAY_SIZE(json_lexer
));
365 new_state
= json_lexer
[lexer
->state
][(uint8_t)ch
];
366 char_consumed
= !TERMINAL_NEEDED_LOOKAHEAD(lexer
->state
, new_state
);
367 if (char_consumed
&& !flush
) {
368 g_string_append_c(lexer
->token
, ch
);
383 lexer
->emit(lexer
, lexer
->token
, new_state
, lexer
->x
, lexer
->y
);
386 g_string_truncate(lexer
->token
, 0);
387 new_state
= IN_START
;
390 /* 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
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.
403 lexer
->emit(lexer
, lexer
->token
, JSON_ERROR
, lexer
->x
, lexer
->y
);
404 g_string_truncate(lexer
->token
, 0);
405 new_state
= IN_START
;
406 lexer
->state
= new_state
;
411 lexer
->state
= new_state
;
412 } while (!char_consumed
&& !flush
);
414 /* Do not let a single token grow to an arbitrarily large size,
415 * this is a security consideration.
417 if (lexer
->token
->len
> MAX_TOKEN_SIZE
) {
418 lexer
->emit(lexer
, lexer
->token
, lexer
->state
, lexer
->x
, lexer
->y
);
419 g_string_truncate(lexer
->token
, 0);
420 lexer
->state
= IN_START
;
426 int json_lexer_feed(JSONLexer
*lexer
, const char *buffer
, size_t size
)
430 for (i
= 0; i
< size
; i
++) {
433 err
= json_lexer_feed_char(lexer
, buffer
[i
], false);
442 int json_lexer_flush(JSONLexer
*lexer
)
444 return lexer
->state
== IN_START
? 0 : json_lexer_feed_char(lexer
, 0, true);
447 void json_lexer_destroy(JSONLexer
*lexer
)
449 g_string_free(lexer
->token
, true);