blob: 7292f3a38fb51c434c55ef74b9601c945b0f50dd [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"
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +010015#include "qapi/qmp/qstring.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010016#include "qapi/qmp/qint.h"
17#include "qapi/qmp/qdict.h"
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060018#include "qemu-common.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010019#include "qapi/qmp/json-lexer.h"
20#include "qapi/qmp/json-streamer.h"
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060021
Anthony Liguori29c75dd2011-06-01 12:14:53 -050022#define MAX_TOKEN_SIZE (64ULL << 20)
23#define MAX_NESTING (1ULL << 10)
24
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +010025static void json_message_process_token(JSONLexer *lexer, GString *input,
26 JSONTokenType type, int x, int y)
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060027{
28 JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
29 QDict *dict;
30
Markus Armbrusterc5461662015-11-25 22:23:26 +010031 switch (type) {
32 case JSON_LCURLY:
33 parser->brace_count++;
34 break;
35 case JSON_RCURLY:
36 parser->brace_count--;
37 break;
38 case JSON_LSQUARE:
39 parser->bracket_count++;
40 break;
41 case JSON_RSQUARE:
42 parser->bracket_count--;
43 break;
44 default:
45 break;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060046 }
47
48 dict = qdict_new();
Luiz Capitulino2e89c062010-05-19 17:17:05 -030049 qdict_put(dict, "type", qint_from_int(type));
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +010050 qdict_put(dict, "token", qstring_from_str(input->str));
Luiz Capitulino2e89c062010-05-19 17:17:05 -030051 qdict_put(dict, "x", qint_from_int(x));
52 qdict_put(dict, "y", qint_from_int(y));
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060053
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +010054 parser->token_size += input->len;
Anthony Liguori29c75dd2011-06-01 12:14:53 -050055
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060056 qlist_append(parser->tokens, dict);
57
Michael Roth5e2dafe2011-06-01 12:14:59 -050058 if (type == JSON_ERROR) {
59 goto out_emit_bad;
60 } else if (parser->brace_count < 0 ||
Anthony Liguori55f83012011-06-01 12:14:51 -050061 parser->bracket_count < 0 ||
62 (parser->brace_count == 0 &&
63 parser->bracket_count == 0)) {
Michael Roth5e2dafe2011-06-01 12:14:59 -050064 goto out_emit;
Anthony Liguori29c75dd2011-06-01 12:14:53 -050065 } else if (parser->token_size > MAX_TOKEN_SIZE ||
Markus Armbruster4f2d31f2015-11-25 22:23:22 +010066 parser->bracket_count + parser->brace_count > MAX_NESTING) {
Anthony Liguori29c75dd2011-06-01 12:14:53 -050067 /* Security consideration, we limit total memory allocated per object
68 * and the maximum recursion depth that a message can force.
69 */
Markus Armbruster07531132015-11-25 22:23:23 +010070 goto out_emit_bad;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060071 }
Michael Roth5e2dafe2011-06-01 12:14:59 -050072
73 return;
74
75out_emit_bad:
Markus Armbruster07531132015-11-25 22:23:23 +010076 /*
77 * Clear out token list and tell the parser to emit an error
Michael Roth5e2dafe2011-06-01 12:14:59 -050078 * 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}