blob: dced2c77a19b2a83b9c9c4b2b6cc0252b906c21c [file] [log] [blame]
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -06001/*
2 * JSON streaming support
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
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010014#include "qapi/qmp/qlist.h"
15#include "qapi/qmp/qint.h"
16#include "qapi/qmp/qdict.h"
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060017#include "qemu-common.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010018#include "qapi/qmp/json-lexer.h"
19#include "qapi/qmp/json-streamer.h"
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060020
Anthony Liguori29c75dd2011-06-01 12:14:53 -050021#define MAX_TOKEN_SIZE (64ULL << 20)
22#define MAX_NESTING (1ULL << 10)
23
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060024static void json_message_process_token(JSONLexer *lexer, QString *token, JSONTokenType type, int x, int y)
25{
26 JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
27 QDict *dict;
28
29 if (type == JSON_OPERATOR) {
30 switch (qstring_get_str(token)[0]) {
31 case '{':
32 parser->brace_count++;
33 break;
34 case '}':
35 parser->brace_count--;
36 break;
37 case '[':
38 parser->bracket_count++;
39 break;
40 case ']':
41 parser->bracket_count--;
42 break;
43 default:
44 break;
45 }
46 }
47
48 dict = qdict_new();
Luiz Capitulino2e89c062010-05-19 17:17:05 -030049 qdict_put(dict, "type", qint_from_int(type));
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060050 QINCREF(token);
Luiz Capitulino2e89c062010-05-19 17:17:05 -030051 qdict_put(dict, "token", token);
52 qdict_put(dict, "x", qint_from_int(x));
53 qdict_put(dict, "y", qint_from_int(y));
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060054
Anthony Liguori29c75dd2011-06-01 12:14:53 -050055 parser->token_size += token->length;
56
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060057 qlist_append(parser->tokens, dict);
58
Michael Roth5e2dafe2011-06-01 12:14:59 -050059 if (type == JSON_ERROR) {
60 goto out_emit_bad;
61 } else if (parser->brace_count < 0 ||
Anthony Liguori55f83012011-06-01 12:14:51 -050062 parser->bracket_count < 0 ||
63 (parser->brace_count == 0 &&
64 parser->bracket_count == 0)) {
Michael Roth5e2dafe2011-06-01 12:14:59 -050065 goto out_emit;
Anthony Liguori29c75dd2011-06-01 12:14:53 -050066 } else if (parser->token_size > MAX_TOKEN_SIZE ||
Markus Armbruster4f2d31f2015-11-25 22:23:22 +010067 parser->bracket_count + parser->brace_count > MAX_NESTING) {
Anthony Liguori29c75dd2011-06-01 12:14:53 -050068 /* Security consideration, we limit total memory allocated per object
69 * and the maximum recursion depth that a message can force.
70 */
Michael Roth5e2dafe2011-06-01 12:14:59 -050071 goto out_emit;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060072 }
Michael Roth5e2dafe2011-06-01 12:14:59 -050073
74 return;
75
76out_emit_bad:
77 /* clear out token list and tell the parser to emit and error
78 * indication by passing it a NULL list
79 */
80 QDECREF(parser->tokens);
81 parser->tokens = NULL;
82out_emit:
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);
87 if (parser->tokens) {
88 QDECREF(parser->tokens);
89 }
90 parser->tokens = qlist_new();
91 parser->token_size = 0;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060092}
93
94void json_message_parser_init(JSONMessageParser *parser,
95 void (*func)(JSONMessageParser *, QList *))
96{
97 parser->emit = func;
98 parser->brace_count = 0;
99 parser->bracket_count = 0;
100 parser->tokens = qlist_new();
Anthony Liguori29c75dd2011-06-01 12:14:53 -0500101 parser->token_size = 0;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600102
103 json_lexer_init(&parser->lexer, json_message_process_token);
104}
105
106int json_message_parser_feed(JSONMessageParser *parser,
107 const char *buffer, size_t size)
108{
109 return json_lexer_feed(&parser->lexer, buffer, size);
110}
111
112int json_message_parser_flush(JSONMessageParser *parser)
113{
114 return json_lexer_flush(&parser->lexer);
115}
116
117void json_message_parser_destroy(JSONMessageParser *parser)
118{
119 json_lexer_destroy(&parser->lexer);
120 QDECREF(parser->tokens);
121}