blob: 7164390cf5a8ddaadd54e3bf2f15a15ecc52ae55 [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
Peter Maydellf2ad72b2016-01-29 17:50:01 +000014#include "qemu/osdep.h"
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060015#include "qemu-common.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010016#include "qapi/qmp/json-lexer.h"
17#include "qapi/qmp/json-streamer.h"
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060018
Anthony Liguori29c75dd2011-06-01 12:14:53 -050019#define MAX_TOKEN_SIZE (64ULL << 20)
Markus Armbrusterdf649832015-11-25 22:23:33 +010020#define MAX_TOKEN_COUNT (2ULL << 20)
Anthony Liguori29c75dd2011-06-01 12:14:53 -050021#define MAX_NESTING (1ULL << 10)
22
Eric Blakeba4dba52016-05-18 15:46:52 -060023static void json_message_free_token(void *token, void *opaque)
24{
25 g_free(token);
26}
27
Paolo Bonzini95385fe2015-11-25 22:23:31 +010028static void json_message_free_tokens(JSONMessageParser *parser)
29{
30 if (parser->tokens) {
Eric Blakeba4dba52016-05-18 15:46:52 -060031 g_queue_foreach(parser->tokens, json_message_free_token, NULL);
Paolo Bonzini95385fe2015-11-25 22:23:31 +010032 g_queue_free(parser->tokens);
33 parser->tokens = NULL;
34 }
35}
36
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +010037static void json_message_process_token(JSONLexer *lexer, GString *input,
38 JSONTokenType type, int x, int y)
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060039{
40 JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
Paolo Bonzini9bada892015-11-25 22:23:32 +010041 JSONToken *token;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060042
Markus Armbrusterc5461662015-11-25 22:23:26 +010043 switch (type) {
44 case JSON_LCURLY:
45 parser->brace_count++;
46 break;
47 case JSON_RCURLY:
48 parser->brace_count--;
49 break;
50 case JSON_LSQUARE:
51 parser->bracket_count++;
52 break;
53 case JSON_RSQUARE:
54 parser->bracket_count--;
55 break;
56 default:
57 break;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060058 }
59
Paolo Bonzini9bada892015-11-25 22:23:32 +010060 token = g_malloc(sizeof(JSONToken) + input->len + 1);
61 token->type = type;
62 memcpy(token->str, input->str, input->len);
63 token->str[input->len] = 0;
64 token->x = x;
65 token->y = y;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060066
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +010067 parser->token_size += input->len;
Anthony Liguori29c75dd2011-06-01 12:14:53 -050068
Paolo Bonzini9bada892015-11-25 22:23:32 +010069 g_queue_push_tail(parser->tokens, token);
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060070
Michael Roth5e2dafe2011-06-01 12:14:59 -050071 if (type == JSON_ERROR) {
72 goto out_emit_bad;
73 } else if (parser->brace_count < 0 ||
Anthony Liguori55f83012011-06-01 12:14:51 -050074 parser->bracket_count < 0 ||
75 (parser->brace_count == 0 &&
76 parser->bracket_count == 0)) {
Michael Roth5e2dafe2011-06-01 12:14:59 -050077 goto out_emit;
Anthony Liguori29c75dd2011-06-01 12:14:53 -050078 } else if (parser->token_size > MAX_TOKEN_SIZE ||
Markus Armbrusterdf649832015-11-25 22:23:33 +010079 g_queue_get_length(parser->tokens) > MAX_TOKEN_COUNT ||
Markus Armbruster4f2d31f2015-11-25 22:23:22 +010080 parser->bracket_count + parser->brace_count > MAX_NESTING) {
Anthony Liguori29c75dd2011-06-01 12:14:53 -050081 /* Security consideration, we limit total memory allocated per object
82 * and the maximum recursion depth that a message can force.
83 */
Markus Armbruster07531132015-11-25 22:23:23 +010084 goto out_emit_bad;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060085 }
Michael Roth5e2dafe2011-06-01 12:14:59 -050086
87 return;
88
89out_emit_bad:
Markus Armbruster07531132015-11-25 22:23:23 +010090 /*
91 * Clear out token list and tell the parser to emit an error
Michael Roth5e2dafe2011-06-01 12:14:59 -050092 * indication by passing it a NULL list
93 */
Paolo Bonzini95385fe2015-11-25 22:23:31 +010094 json_message_free_tokens(parser);
Michael Roth5e2dafe2011-06-01 12:14:59 -050095out_emit:
96 /* send current list of tokens to parser and reset tokenizer */
97 parser->brace_count = 0;
98 parser->bracket_count = 0;
Paolo Bonzini95385fe2015-11-25 22:23:31 +010099 /* parser->emit takes ownership of parser->tokens. */
Michael Roth5e2dafe2011-06-01 12:14:59 -0500100 parser->emit(parser, parser->tokens);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100101 parser->tokens = g_queue_new();
Michael Roth5e2dafe2011-06-01 12:14:59 -0500102 parser->token_size = 0;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600103}
104
105void json_message_parser_init(JSONMessageParser *parser,
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100106 void (*func)(JSONMessageParser *, GQueue *))
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600107{
108 parser->emit = func;
109 parser->brace_count = 0;
110 parser->bracket_count = 0;
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100111 parser->tokens = g_queue_new();
Anthony Liguori29c75dd2011-06-01 12:14:53 -0500112 parser->token_size = 0;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600113
114 json_lexer_init(&parser->lexer, json_message_process_token);
115}
116
117int json_message_parser_feed(JSONMessageParser *parser,
118 const char *buffer, size_t size)
119{
120 return json_lexer_feed(&parser->lexer, buffer, size);
121}
122
123int json_message_parser_flush(JSONMessageParser *parser)
124{
125 return json_lexer_flush(&parser->lexer);
126}
127
128void json_message_parser_destroy(JSONMessageParser *parser)
129{
130 json_lexer_destroy(&parser->lexer);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100131 json_message_free_tokens(parser);
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600132}