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