2 * JSON streaming support
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 "qapi/qmp/qlist.h"
15 #include "qapi/qmp/qstring.h"
16 #include "qapi/qmp/qint.h"
17 #include "qapi/qmp/qdict.h"
18 #include "qemu-common.h"
19 #include "qapi/qmp/json-lexer.h"
20 #include "qapi/qmp/json-streamer.h"
22 #define MAX_TOKEN_SIZE (64ULL << 20)
23 #define MAX_NESTING (1ULL << 10)
25 static void json_message_process_token(JSONLexer
*lexer
, GString
*input
,
26 JSONTokenType type
, int x
, int y
)
28 JSONMessageParser
*parser
= container_of(lexer
, JSONMessageParser
, lexer
);
33 parser
->brace_count
++;
36 parser
->brace_count
--;
39 parser
->bracket_count
++;
42 parser
->bracket_count
--;
49 qdict_put(dict
, "type", qint_from_int(type
));
50 qdict_put(dict
, "token", qstring_from_str(input
->str
));
51 qdict_put(dict
, "x", qint_from_int(x
));
52 qdict_put(dict
, "y", qint_from_int(y
));
54 parser
->token_size
+= input
->len
;
56 qlist_append(parser
->tokens
, dict
);
58 if (type
== JSON_ERROR
) {
60 } else if (parser
->brace_count
< 0 ||
61 parser
->bracket_count
< 0 ||
62 (parser
->brace_count
== 0 &&
63 parser
->bracket_count
== 0)) {
65 } else if (parser
->token_size
> MAX_TOKEN_SIZE
||
66 parser
->bracket_count
+ parser
->brace_count
> MAX_NESTING
) {
67 /* Security consideration, we limit total memory allocated per object
68 * and the maximum recursion depth that a message can force.
77 * Clear out token list and tell the parser to emit an error
78 * indication by passing it a NULL list
80 QDECREF(parser
->tokens
);
81 parser
->tokens
= NULL
;
83 /* send current list of tokens to parser and reset tokenizer */
84 parser
->brace_count
= 0;
85 parser
->bracket_count
= 0;
86 parser
->emit(parser
, parser
->tokens
);
88 QDECREF(parser
->tokens
);
90 parser
->tokens
= qlist_new();
91 parser
->token_size
= 0;
94 void json_message_parser_init(JSONMessageParser
*parser
,
95 void (*func
)(JSONMessageParser
*, QList
*))
98 parser
->brace_count
= 0;
99 parser
->bracket_count
= 0;
100 parser
->tokens
= qlist_new();
101 parser
->token_size
= 0;
103 json_lexer_init(&parser
->lexer
, json_message_process_token
);
106 int json_message_parser_feed(JSONMessageParser
*parser
,
107 const char *buffer
, size_t size
)
109 return json_lexer_feed(&parser
->lexer
, buffer
, size
);
112 int json_message_parser_flush(JSONMessageParser
*parser
)
114 return json_lexer_flush(&parser
->lexer
);
117 void json_message_parser_destroy(JSONMessageParser
*parser
)
119 json_lexer_destroy(&parser
->lexer
);
120 QDECREF(parser
->tokens
);