xref: /openbmc/qemu/qobject/json-lexer.c (revision 7c1e1d5481fe8d2e757469179f9ccd14e8838ed1)
1a372823aSPaolo Bonzini /*
2a372823aSPaolo Bonzini  * JSON lexer
3a372823aSPaolo Bonzini  *
4a372823aSPaolo Bonzini  * Copyright IBM, Corp. 2009
5a372823aSPaolo Bonzini  *
6a372823aSPaolo Bonzini  * Authors:
7a372823aSPaolo Bonzini  *  Anthony Liguori   <aliguori@us.ibm.com>
8a372823aSPaolo Bonzini  *
9a372823aSPaolo Bonzini  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10a372823aSPaolo Bonzini  * See the COPYING.LIB file in the top-level directory.
11a372823aSPaolo Bonzini  *
12a372823aSPaolo Bonzini  */
13a372823aSPaolo Bonzini 
14f2ad72b3SPeter Maydell #include "qemu/osdep.h"
15a372823aSPaolo Bonzini #include "qemu-common.h"
16a372823aSPaolo Bonzini #include "qapi/qmp/json-lexer.h"
17a372823aSPaolo Bonzini 
18a372823aSPaolo Bonzini #define MAX_TOKEN_SIZE (64ULL << 20)
19a372823aSPaolo Bonzini 
20a372823aSPaolo Bonzini /*
21eddc0a7fSMarkus Armbruster  * From RFC 8259 "The JavaScript Object Notation (JSON) Data
22eddc0a7fSMarkus Armbruster  * Interchange Format", with [comments in brackets]:
23ff5394adSEric Blake  *
24eddc0a7fSMarkus Armbruster  * The set of tokens includes six structural characters, strings,
25eddc0a7fSMarkus Armbruster  * numbers, and three literal names.
26ff5394adSEric Blake  *
27eddc0a7fSMarkus Armbruster  * These are the six structural characters:
28ff5394adSEric Blake  *
29eddc0a7fSMarkus Armbruster  *    begin-array     = ws %x5B ws  ; [ left square bracket
30eddc0a7fSMarkus Armbruster  *    begin-object    = ws %x7B ws  ; { left curly bracket
31eddc0a7fSMarkus Armbruster  *    end-array       = ws %x5D ws  ; ] right square bracket
32eddc0a7fSMarkus Armbruster  *    end-object      = ws %x7D ws  ; } right curly bracket
33eddc0a7fSMarkus Armbruster  *    name-separator  = ws %x3A ws  ; : colon
34eddc0a7fSMarkus Armbruster  *    value-separator = ws %x2C ws  ; , comma
35ff5394adSEric Blake  *
36eddc0a7fSMarkus Armbruster  * Insignificant whitespace is allowed before or after any of the six
37eddc0a7fSMarkus Armbruster  * structural characters.
38eddc0a7fSMarkus Armbruster  * [This lexer accepts it before or after any token, which is actually
39eddc0a7fSMarkus Armbruster  * the same, as the grammar always has structural characters between
40eddc0a7fSMarkus Armbruster  * other tokens.]
41ff5394adSEric Blake  *
42eddc0a7fSMarkus Armbruster  *    ws = *(
43eddc0a7fSMarkus Armbruster  *           %x20 /              ; Space
44eddc0a7fSMarkus Armbruster  *           %x09 /              ; Horizontal tab
45eddc0a7fSMarkus Armbruster  *           %x0A /              ; Line feed or New line
46eddc0a7fSMarkus Armbruster  *           %x0D )              ; Carriage return
47a372823aSPaolo Bonzini  *
48eddc0a7fSMarkus Armbruster  * [...] three literal names:
49eddc0a7fSMarkus Armbruster  *    false null true
50eddc0a7fSMarkus Armbruster  *  [This lexer accepts [a-z]+, and leaves rejecting unknown literal
51eddc0a7fSMarkus Armbruster  *  names to the parser.]
52eddc0a7fSMarkus Armbruster  *
53eddc0a7fSMarkus Armbruster  * [Numbers:]
54eddc0a7fSMarkus Armbruster  *
55eddc0a7fSMarkus Armbruster  *    number = [ minus ] int [ frac ] [ exp ]
56eddc0a7fSMarkus Armbruster  *    decimal-point = %x2E       ; .
57eddc0a7fSMarkus Armbruster  *    digit1-9 = %x31-39         ; 1-9
58eddc0a7fSMarkus Armbruster  *    e = %x65 / %x45            ; e E
59eddc0a7fSMarkus Armbruster  *    exp = e [ minus / plus ] 1*DIGIT
60eddc0a7fSMarkus Armbruster  *    frac = decimal-point 1*DIGIT
61eddc0a7fSMarkus Armbruster  *    int = zero / ( digit1-9 *DIGIT )
62eddc0a7fSMarkus Armbruster  *    minus = %x2D               ; -
63eddc0a7fSMarkus Armbruster  *    plus = %x2B                ; +
64eddc0a7fSMarkus Armbruster  *    zero = %x30                ; 0
65eddc0a7fSMarkus Armbruster  *
66eddc0a7fSMarkus Armbruster  * [Strings:]
67eddc0a7fSMarkus Armbruster  *    string = quotation-mark *char quotation-mark
68eddc0a7fSMarkus Armbruster  *
69eddc0a7fSMarkus Armbruster  *    char = unescaped /
70eddc0a7fSMarkus Armbruster  *        escape (
71eddc0a7fSMarkus Armbruster  *            %x22 /          ; "    quotation mark  U+0022
72eddc0a7fSMarkus Armbruster  *            %x5C /          ; \    reverse solidus U+005C
73eddc0a7fSMarkus Armbruster  *            %x2F /          ; /    solidus         U+002F
74eddc0a7fSMarkus Armbruster  *            %x62 /          ; b    backspace       U+0008
75eddc0a7fSMarkus Armbruster  *            %x66 /          ; f    form feed       U+000C
76eddc0a7fSMarkus Armbruster  *            %x6E /          ; n    line feed       U+000A
77eddc0a7fSMarkus Armbruster  *            %x72 /          ; r    carriage return U+000D
78eddc0a7fSMarkus Armbruster  *            %x74 /          ; t    tab             U+0009
79eddc0a7fSMarkus Armbruster  *            %x75 4HEXDIG )  ; uXXXX                U+XXXX
80eddc0a7fSMarkus Armbruster  *    escape = %x5C              ; \
81eddc0a7fSMarkus Armbruster  *    quotation-mark = %x22      ; "
82eddc0a7fSMarkus Armbruster  *    unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
83b2da4a4dSMarkus Armbruster  *    [This lexer accepts any non-control character after escape, and
84b2da4a4dSMarkus Armbruster  *    leaves rejecting invalid ones to the parser.]
85eddc0a7fSMarkus Armbruster  *
86eddc0a7fSMarkus Armbruster  *
87eddc0a7fSMarkus Armbruster  * Extensions over RFC 8259:
88eddc0a7fSMarkus Armbruster  * - Extra escape sequence in strings:
89eddc0a7fSMarkus Armbruster  *   0x27 (apostrophe) is recognized after escape, too
90eddc0a7fSMarkus Armbruster  * - Single-quoted strings:
91eddc0a7fSMarkus Armbruster  *   Like double-quoted strings, except they're delimited by %x27
92eddc0a7fSMarkus Armbruster  *   (apostrophe) instead of %x22 (quotation mark), and can't contain
93eddc0a7fSMarkus Armbruster  *   unescaped apostrophe, but can contain unescaped quotation mark.
94eddc0a7fSMarkus Armbruster  * - Interpolation:
95eddc0a7fSMarkus Armbruster  *   interpolation = %((l|ll|I64)[du]|[ipsf])
96eddc0a7fSMarkus Armbruster  *
97eddc0a7fSMarkus Armbruster  * Note:
984b1c0cd7SMarkus Armbruster  * - Input must be encoded in modified UTF-8.
99eddc0a7fSMarkus Armbruster  * - Decoding and validating is left to the parser.
100a372823aSPaolo Bonzini  */
101a372823aSPaolo Bonzini 
102a372823aSPaolo Bonzini enum json_lexer_state {
103b8d3b1daSMarkus Armbruster     IN_ERROR = 0,               /* must really be 0, see json_lexer[] */
104a372823aSPaolo Bonzini     IN_DQ_STRING_ESCAPE,
105a372823aSPaolo Bonzini     IN_DQ_STRING,
106a372823aSPaolo Bonzini     IN_SQ_STRING_ESCAPE,
107a372823aSPaolo Bonzini     IN_SQ_STRING,
108a372823aSPaolo Bonzini     IN_ZERO,
109a372823aSPaolo Bonzini     IN_DIGITS,
110a372823aSPaolo Bonzini     IN_DIGIT,
111a372823aSPaolo Bonzini     IN_EXP_E,
112a372823aSPaolo Bonzini     IN_MANTISSA,
113a372823aSPaolo Bonzini     IN_MANTISSA_DIGITS,
114a372823aSPaolo Bonzini     IN_NONZERO_NUMBER,
115a372823aSPaolo Bonzini     IN_NEG_NONZERO_NUMBER,
116a372823aSPaolo Bonzini     IN_KEYWORD,
117a372823aSPaolo Bonzini     IN_ESCAPE,
118a372823aSPaolo Bonzini     IN_ESCAPE_L,
119a372823aSPaolo Bonzini     IN_ESCAPE_LL,
120a372823aSPaolo Bonzini     IN_ESCAPE_I,
121a372823aSPaolo Bonzini     IN_ESCAPE_I6,
122a372823aSPaolo Bonzini     IN_ESCAPE_I64,
123a372823aSPaolo Bonzini     IN_WHITESPACE,
124a372823aSPaolo Bonzini     IN_START,
125a372823aSPaolo Bonzini };
126a372823aSPaolo Bonzini 
127b8d3b1daSMarkus Armbruster QEMU_BUILD_BUG_ON((int)JSON_MIN <= (int)IN_START);
128b8d3b1daSMarkus Armbruster 
129a372823aSPaolo Bonzini #define TERMINAL(state) [0 ... 0x7F] = (state)
130a372823aSPaolo Bonzini 
131a372823aSPaolo Bonzini /* Return whether TERMINAL is a terminal state and the transition to it
132a372823aSPaolo Bonzini    from OLD_STATE required lookahead.  This happens whenever the table
133a372823aSPaolo Bonzini    below uses the TERMINAL macro.  */
134a372823aSPaolo Bonzini #define TERMINAL_NEEDED_LOOKAHEAD(old_state, terminal) \
135a2ec6be7SMarkus Armbruster     (terminal != IN_ERROR && json_lexer[(old_state)][0] == (terminal))
136a372823aSPaolo Bonzini 
137a372823aSPaolo Bonzini static const uint8_t json_lexer[][256] =  {
138b8d3b1daSMarkus Armbruster     /* Relies on default initialization to IN_ERROR! */
139b8d3b1daSMarkus Armbruster 
140a372823aSPaolo Bonzini     /* double quote string */
141a372823aSPaolo Bonzini     [IN_DQ_STRING_ESCAPE] = {
142b2da4a4dSMarkus Armbruster         [0x20 ... 0xFD] = IN_DQ_STRING,
143a372823aSPaolo Bonzini     },
144a372823aSPaolo Bonzini     [IN_DQ_STRING] = {
145de930f45SMarkus Armbruster         [0x20 ... 0xFD] = IN_DQ_STRING,
146a372823aSPaolo Bonzini         ['\\'] = IN_DQ_STRING_ESCAPE,
147a372823aSPaolo Bonzini         ['"'] = JSON_STRING,
148a372823aSPaolo Bonzini     },
149a372823aSPaolo Bonzini 
150a372823aSPaolo Bonzini     /* single quote string */
151a372823aSPaolo Bonzini     [IN_SQ_STRING_ESCAPE] = {
152b2da4a4dSMarkus Armbruster         [0x20 ... 0xFD] = IN_SQ_STRING,
153a372823aSPaolo Bonzini     },
154a372823aSPaolo Bonzini     [IN_SQ_STRING] = {
155de930f45SMarkus Armbruster         [0x20 ... 0xFD] = IN_SQ_STRING,
156a372823aSPaolo Bonzini         ['\\'] = IN_SQ_STRING_ESCAPE,
157a372823aSPaolo Bonzini         ['\''] = JSON_STRING,
158a372823aSPaolo Bonzini     },
159a372823aSPaolo Bonzini 
160a372823aSPaolo Bonzini     /* Zero */
161a372823aSPaolo Bonzini     [IN_ZERO] = {
162a372823aSPaolo Bonzini         TERMINAL(JSON_INTEGER),
163a372823aSPaolo Bonzini         ['0' ... '9'] = IN_ERROR,
164a372823aSPaolo Bonzini         ['.'] = IN_MANTISSA,
165a372823aSPaolo Bonzini     },
166a372823aSPaolo Bonzini 
167a372823aSPaolo Bonzini     /* Float */
168a372823aSPaolo Bonzini     [IN_DIGITS] = {
169a372823aSPaolo Bonzini         TERMINAL(JSON_FLOAT),
170a372823aSPaolo Bonzini         ['0' ... '9'] = IN_DIGITS,
171a372823aSPaolo Bonzini     },
172a372823aSPaolo Bonzini 
173a372823aSPaolo Bonzini     [IN_DIGIT] = {
174a372823aSPaolo Bonzini         ['0' ... '9'] = IN_DIGITS,
175a372823aSPaolo Bonzini     },
176a372823aSPaolo Bonzini 
177a372823aSPaolo Bonzini     [IN_EXP_E] = {
178a372823aSPaolo Bonzini         ['-'] = IN_DIGIT,
179a372823aSPaolo Bonzini         ['+'] = IN_DIGIT,
180a372823aSPaolo Bonzini         ['0' ... '9'] = IN_DIGITS,
181a372823aSPaolo Bonzini     },
182a372823aSPaolo Bonzini 
183a372823aSPaolo Bonzini     [IN_MANTISSA_DIGITS] = {
184a372823aSPaolo Bonzini         TERMINAL(JSON_FLOAT),
185a372823aSPaolo Bonzini         ['0' ... '9'] = IN_MANTISSA_DIGITS,
186a372823aSPaolo Bonzini         ['e'] = IN_EXP_E,
187a372823aSPaolo Bonzini         ['E'] = IN_EXP_E,
188a372823aSPaolo Bonzini     },
189a372823aSPaolo Bonzini 
190a372823aSPaolo Bonzini     [IN_MANTISSA] = {
191a372823aSPaolo Bonzini         ['0' ... '9'] = IN_MANTISSA_DIGITS,
192a372823aSPaolo Bonzini     },
193a372823aSPaolo Bonzini 
194a372823aSPaolo Bonzini     /* Number */
195a372823aSPaolo Bonzini     [IN_NONZERO_NUMBER] = {
196a372823aSPaolo Bonzini         TERMINAL(JSON_INTEGER),
197a372823aSPaolo Bonzini         ['0' ... '9'] = IN_NONZERO_NUMBER,
198a372823aSPaolo Bonzini         ['e'] = IN_EXP_E,
199a372823aSPaolo Bonzini         ['E'] = IN_EXP_E,
200a372823aSPaolo Bonzini         ['.'] = IN_MANTISSA,
201a372823aSPaolo Bonzini     },
202a372823aSPaolo Bonzini 
203a372823aSPaolo Bonzini     [IN_NEG_NONZERO_NUMBER] = {
204a372823aSPaolo Bonzini         ['0'] = IN_ZERO,
205a372823aSPaolo Bonzini         ['1' ... '9'] = IN_NONZERO_NUMBER,
206a372823aSPaolo Bonzini     },
207a372823aSPaolo Bonzini 
208a372823aSPaolo Bonzini     /* keywords */
209a372823aSPaolo Bonzini     [IN_KEYWORD] = {
210a372823aSPaolo Bonzini         TERMINAL(JSON_KEYWORD),
211a372823aSPaolo Bonzini         ['a' ... 'z'] = IN_KEYWORD,
212a372823aSPaolo Bonzini     },
213a372823aSPaolo Bonzini 
214a372823aSPaolo Bonzini     /* whitespace */
215a372823aSPaolo Bonzini     [IN_WHITESPACE] = {
216a372823aSPaolo Bonzini         TERMINAL(JSON_SKIP),
217a372823aSPaolo Bonzini         [' '] = IN_WHITESPACE,
218a372823aSPaolo Bonzini         ['\t'] = IN_WHITESPACE,
219a372823aSPaolo Bonzini         ['\r'] = IN_WHITESPACE,
220a372823aSPaolo Bonzini         ['\n'] = IN_WHITESPACE,
221a372823aSPaolo Bonzini     },
222a372823aSPaolo Bonzini 
223a372823aSPaolo Bonzini     /* escape */
224a372823aSPaolo Bonzini     [IN_ESCAPE_LL] = {
225a372823aSPaolo Bonzini         ['d'] = JSON_ESCAPE,
2262bc7cfeaSMarc-André Lureau         ['u'] = JSON_ESCAPE,
227a372823aSPaolo Bonzini     },
228a372823aSPaolo Bonzini 
229a372823aSPaolo Bonzini     [IN_ESCAPE_L] = {
230a372823aSPaolo Bonzini         ['d'] = JSON_ESCAPE,
231a372823aSPaolo Bonzini         ['l'] = IN_ESCAPE_LL,
2322bc7cfeaSMarc-André Lureau         ['u'] = JSON_ESCAPE,
233a372823aSPaolo Bonzini     },
234a372823aSPaolo Bonzini 
235a372823aSPaolo Bonzini     [IN_ESCAPE_I64] = {
236a372823aSPaolo Bonzini         ['d'] = JSON_ESCAPE,
2372bc7cfeaSMarc-André Lureau         ['u'] = JSON_ESCAPE,
238a372823aSPaolo Bonzini     },
239a372823aSPaolo Bonzini 
240a372823aSPaolo Bonzini     [IN_ESCAPE_I6] = {
241a372823aSPaolo Bonzini         ['4'] = IN_ESCAPE_I64,
242a372823aSPaolo Bonzini     },
243a372823aSPaolo Bonzini 
244a372823aSPaolo Bonzini     [IN_ESCAPE_I] = {
245a372823aSPaolo Bonzini         ['6'] = IN_ESCAPE_I6,
246a372823aSPaolo Bonzini     },
247a372823aSPaolo Bonzini 
248a372823aSPaolo Bonzini     [IN_ESCAPE] = {
249a372823aSPaolo Bonzini         ['d'] = JSON_ESCAPE,
250a372823aSPaolo Bonzini         ['i'] = JSON_ESCAPE,
251a372823aSPaolo Bonzini         ['p'] = JSON_ESCAPE,
252a372823aSPaolo Bonzini         ['s'] = JSON_ESCAPE,
2532bc7cfeaSMarc-André Lureau         ['u'] = JSON_ESCAPE,
254a372823aSPaolo Bonzini         ['f'] = JSON_ESCAPE,
255a372823aSPaolo Bonzini         ['l'] = IN_ESCAPE_L,
256a372823aSPaolo Bonzini         ['I'] = IN_ESCAPE_I,
257a372823aSPaolo Bonzini     },
258a372823aSPaolo Bonzini 
259a372823aSPaolo Bonzini     /* top level rule */
260a372823aSPaolo Bonzini     [IN_START] = {
261a372823aSPaolo Bonzini         ['"'] = IN_DQ_STRING,
262a372823aSPaolo Bonzini         ['\''] = IN_SQ_STRING,
263a372823aSPaolo Bonzini         ['0'] = IN_ZERO,
264a372823aSPaolo Bonzini         ['1' ... '9'] = IN_NONZERO_NUMBER,
265a372823aSPaolo Bonzini         ['-'] = IN_NEG_NONZERO_NUMBER,
266c5461660SMarkus Armbruster         ['{'] = JSON_LCURLY,
267c5461660SMarkus Armbruster         ['}'] = JSON_RCURLY,
268c5461660SMarkus Armbruster         ['['] = JSON_LSQUARE,
269c5461660SMarkus Armbruster         [']'] = JSON_RSQUARE,
270c5461660SMarkus Armbruster         [','] = JSON_COMMA,
271c5461660SMarkus Armbruster         [':'] = JSON_COLON,
272a372823aSPaolo Bonzini         ['a' ... 'z'] = IN_KEYWORD,
273a372823aSPaolo Bonzini         ['%'] = IN_ESCAPE,
274a372823aSPaolo Bonzini         [' '] = IN_WHITESPACE,
275a372823aSPaolo Bonzini         ['\t'] = IN_WHITESPACE,
276a372823aSPaolo Bonzini         ['\r'] = IN_WHITESPACE,
277a372823aSPaolo Bonzini         ['\n'] = IN_WHITESPACE,
278a372823aSPaolo Bonzini     },
279a372823aSPaolo Bonzini };
280a372823aSPaolo Bonzini 
281a372823aSPaolo Bonzini void json_lexer_init(JSONLexer *lexer, JSONLexerEmitter func)
282a372823aSPaolo Bonzini {
283a372823aSPaolo Bonzini     lexer->emit = func;
284a372823aSPaolo Bonzini     lexer->state = IN_START;
285d2ca7c0bSPaolo Bonzini     lexer->token = g_string_sized_new(3);
286a372823aSPaolo Bonzini     lexer->x = lexer->y = 0;
287a372823aSPaolo Bonzini }
288a372823aSPaolo Bonzini 
289*7c1e1d54SMarc-André Lureau static void json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush)
290a372823aSPaolo Bonzini {
291a372823aSPaolo Bonzini     int char_consumed, new_state;
292a372823aSPaolo Bonzini 
293a372823aSPaolo Bonzini     lexer->x++;
294a372823aSPaolo Bonzini     if (ch == '\n') {
295a372823aSPaolo Bonzini         lexer->x = 0;
296a372823aSPaolo Bonzini         lexer->y++;
297a372823aSPaolo Bonzini     }
298a372823aSPaolo Bonzini 
299a372823aSPaolo Bonzini     do {
300b8d3b1daSMarkus Armbruster         assert(lexer->state <= ARRAY_SIZE(json_lexer));
301a372823aSPaolo Bonzini         new_state = json_lexer[lexer->state][(uint8_t)ch];
302a372823aSPaolo Bonzini         char_consumed = !TERMINAL_NEEDED_LOOKAHEAD(lexer->state, new_state);
303a2ec6be7SMarkus Armbruster         if (char_consumed && !flush) {
304d2ca7c0bSPaolo Bonzini             g_string_append_c(lexer->token, ch);
305a372823aSPaolo Bonzini         }
306a372823aSPaolo Bonzini 
307a372823aSPaolo Bonzini         switch (new_state) {
308c5461660SMarkus Armbruster         case JSON_LCURLY:
309c5461660SMarkus Armbruster         case JSON_RCURLY:
310c5461660SMarkus Armbruster         case JSON_LSQUARE:
311c5461660SMarkus Armbruster         case JSON_RSQUARE:
312c5461660SMarkus Armbruster         case JSON_COLON:
313c5461660SMarkus Armbruster         case JSON_COMMA:
314a372823aSPaolo Bonzini         case JSON_ESCAPE:
315a372823aSPaolo Bonzini         case JSON_INTEGER:
316a372823aSPaolo Bonzini         case JSON_FLOAT:
317a372823aSPaolo Bonzini         case JSON_KEYWORD:
318a372823aSPaolo Bonzini         case JSON_STRING:
319a372823aSPaolo Bonzini             lexer->emit(lexer, lexer->token, new_state, lexer->x, lexer->y);
320a372823aSPaolo Bonzini             /* fall through */
321a372823aSPaolo Bonzini         case JSON_SKIP:
322d2ca7c0bSPaolo Bonzini             g_string_truncate(lexer->token, 0);
323a372823aSPaolo Bonzini             new_state = IN_START;
324a372823aSPaolo Bonzini             break;
325a372823aSPaolo Bonzini         case IN_ERROR:
326a372823aSPaolo Bonzini             /* XXX: To avoid having previous bad input leaving the parser in an
327a372823aSPaolo Bonzini              * unresponsive state where we consume unpredictable amounts of
328a372823aSPaolo Bonzini              * subsequent "good" input, percolate this error state up to the
329a372823aSPaolo Bonzini              * tokenizer/parser by forcing a NULL object to be emitted, then
330a372823aSPaolo Bonzini              * reset state.
331a372823aSPaolo Bonzini              *
332a372823aSPaolo Bonzini              * Also note that this handling is required for reliable channel
333a372823aSPaolo Bonzini              * negotiation between QMP and the guest agent, since chr(0xFF)
334a372823aSPaolo Bonzini              * is placed at the beginning of certain events to ensure proper
335a372823aSPaolo Bonzini              * delivery when the channel is in an unknown state. chr(0xFF) is
336a372823aSPaolo Bonzini              * never a valid ASCII/UTF-8 sequence, so this should reliably
337a372823aSPaolo Bonzini              * induce an error/flush state.
338a372823aSPaolo Bonzini              */
339a372823aSPaolo Bonzini             lexer->emit(lexer, lexer->token, JSON_ERROR, lexer->x, lexer->y);
340d2ca7c0bSPaolo Bonzini             g_string_truncate(lexer->token, 0);
341a372823aSPaolo Bonzini             new_state = IN_START;
342a372823aSPaolo Bonzini             lexer->state = new_state;
343*7c1e1d54SMarc-André Lureau             return;
344a372823aSPaolo Bonzini         default:
345a372823aSPaolo Bonzini             break;
346a372823aSPaolo Bonzini         }
347a372823aSPaolo Bonzini         lexer->state = new_state;
348a372823aSPaolo Bonzini     } while (!char_consumed && !flush);
349a372823aSPaolo Bonzini 
350a372823aSPaolo Bonzini     /* Do not let a single token grow to an arbitrarily large size,
351a372823aSPaolo Bonzini      * this is a security consideration.
352a372823aSPaolo Bonzini      */
353d2ca7c0bSPaolo Bonzini     if (lexer->token->len > MAX_TOKEN_SIZE) {
354a372823aSPaolo Bonzini         lexer->emit(lexer, lexer->token, lexer->state, lexer->x, lexer->y);
355d2ca7c0bSPaolo Bonzini         g_string_truncate(lexer->token, 0);
356a372823aSPaolo Bonzini         lexer->state = IN_START;
357a372823aSPaolo Bonzini     }
358a372823aSPaolo Bonzini }
359a372823aSPaolo Bonzini 
360*7c1e1d54SMarc-André Lureau void json_lexer_feed(JSONLexer *lexer, const char *buffer, size_t size)
361a372823aSPaolo Bonzini {
362a372823aSPaolo Bonzini     size_t i;
363a372823aSPaolo Bonzini 
364a372823aSPaolo Bonzini     for (i = 0; i < size; i++) {
365*7c1e1d54SMarc-André Lureau         json_lexer_feed_char(lexer, buffer[i], false);
366a372823aSPaolo Bonzini     }
367a372823aSPaolo Bonzini }
368a372823aSPaolo Bonzini 
369*7c1e1d54SMarc-André Lureau void json_lexer_flush(JSONLexer *lexer)
370a372823aSPaolo Bonzini {
371*7c1e1d54SMarc-André Lureau     if (lexer->state != IN_START) {
372*7c1e1d54SMarc-André Lureau         json_lexer_feed_char(lexer, 0, true);
373*7c1e1d54SMarc-André Lureau     }
374a372823aSPaolo Bonzini }
375a372823aSPaolo Bonzini 
376a372823aSPaolo Bonzini void json_lexer_destroy(JSONLexer *lexer)
377a372823aSPaolo Bonzini {
378d2ca7c0bSPaolo Bonzini     g_string_free(lexer->token, true);
379a372823aSPaolo Bonzini }
380