blob: e87230d681d3b94ea17cf0504ade4a7ba19ff439 [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
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060014#include "qemu-common.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010015#include "qapi/qmp/json-lexer.h"
16#include "qapi/qmp/json-streamer.h"
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060017
Anthony Liguori29c75dd2011-06-01 12:14:53 -050018#define MAX_TOKEN_SIZE (64ULL << 20)
19#define MAX_NESTING (1ULL << 10)
20
Paolo Bonzini95385fe2015-11-25 22:23:31 +010021static void json_message_free_tokens(JSONMessageParser *parser)
22{
23 if (parser->tokens) {
24 g_queue_free(parser->tokens);
25 parser->tokens = NULL;
26 }
27}
28
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +010029static void json_message_process_token(JSONLexer *lexer, GString *input,
30 JSONTokenType type, int x, int y)
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060031{
32 JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
Paolo Bonzini9bada892015-11-25 22:23:32 +010033 JSONToken *token;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060034
Markus Armbrusterc5461662015-11-25 22:23:26 +010035 switch (type) {
36 case JSON_LCURLY:
37 parser->brace_count++;
38 break;
39 case JSON_RCURLY:
40 parser->brace_count--;
41 break;
42 case JSON_LSQUARE:
43 parser->bracket_count++;
44 break;
45 case JSON_RSQUARE:
46 parser->bracket_count--;
47 break;
48 default:
49 break;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060050 }
51
Paolo Bonzini9bada892015-11-25 22:23:32 +010052 token = g_malloc(sizeof(JSONToken) + input->len + 1);
53 token->type = type;
54 memcpy(token->str, input->str, input->len);
55 token->str[input->len] = 0;
56 token->x = x;
57 token->y = y;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060058
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +010059 parser->token_size += input->len;
Anthony Liguori29c75dd2011-06-01 12:14:53 -050060
Paolo Bonzini9bada892015-11-25 22:23:32 +010061 g_queue_push_tail(parser->tokens, token);
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060062
Michael Roth5e2dafe2011-06-01 12:14:59 -050063 if (type == JSON_ERROR) {
64 goto out_emit_bad;
65 } else if (parser->brace_count < 0 ||
Anthony Liguori55f83012011-06-01 12:14:51 -050066 parser->bracket_count < 0 ||
67 (parser->brace_count == 0 &&
68 parser->bracket_count == 0)) {
Michael Roth5e2dafe2011-06-01 12:14:59 -050069 goto out_emit;
Anthony Liguori29c75dd2011-06-01 12:14:53 -050070 } else if (parser->token_size > MAX_TOKEN_SIZE ||
Markus Armbruster4f2d31f2015-11-25 22:23:22 +010071 parser->bracket_count + parser->brace_count > MAX_NESTING) {
Anthony Liguori29c75dd2011-06-01 12:14:53 -050072 /* Security consideration, we limit total memory allocated per object
73 * and the maximum recursion depth that a message can force.
74 */
Markus Armbruster07531132015-11-25 22:23:23 +010075 goto out_emit_bad;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060076 }
Michael Roth5e2dafe2011-06-01 12:14:59 -050077
78 return;
79
80out_emit_bad:
Markus Armbruster07531132015-11-25 22:23:23 +010081 /*
82 * Clear out token list and tell the parser to emit an error
Michael Roth5e2dafe2011-06-01 12:14:59 -050083 * indication by passing it a NULL list
84 */
Paolo Bonzini95385fe2015-11-25 22:23:31 +010085 json_message_free_tokens(parser);
Michael Roth5e2dafe2011-06-01 12:14:59 -050086out_emit:
87 /* send current list of tokens to parser and reset tokenizer */
88 parser->brace_count = 0;
89 parser->bracket_count = 0;
Paolo Bonzini95385fe2015-11-25 22:23:31 +010090 /* parser->emit takes ownership of parser->tokens. */
Michael Roth5e2dafe2011-06-01 12:14:59 -050091 parser->emit(parser, parser->tokens);
Paolo Bonzini95385fe2015-11-25 22:23:31 +010092 parser->tokens = g_queue_new();
Michael Roth5e2dafe2011-06-01 12:14:59 -050093 parser->token_size = 0;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060094}
95
96void json_message_parser_init(JSONMessageParser *parser,
Paolo Bonzini95385fe2015-11-25 22:23:31 +010097 void (*func)(JSONMessageParser *, GQueue *))
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060098{
99 parser->emit = func;
100 parser->brace_count = 0;
101 parser->bracket_count = 0;
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100102 parser->tokens = g_queue_new();
Anthony Liguori29c75dd2011-06-01 12:14:53 -0500103 parser->token_size = 0;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600104
105 json_lexer_init(&parser->lexer, json_message_process_token);
106}
107
108int json_message_parser_feed(JSONMessageParser *parser,
109 const char *buffer, size_t size)
110{
111 return json_lexer_feed(&parser->lexer, buffer, size);
112}
113
114int json_message_parser_flush(JSONMessageParser *parser)
115{
116 return json_lexer_flush(&parser->lexer);
117}
118
119void json_message_parser_destroy(JSONMessageParser *parser)
120{
121 json_lexer_destroy(&parser->lexer);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100122 json_message_free_tokens(parser);
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600123}