blob: a4db4b832e0898c15f49952cfc0409dce4d5e392 [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)
Markus Armbrusterdf649832015-11-25 22:23:33 +010019#define MAX_TOKEN_COUNT (2ULL << 20)
Anthony Liguori29c75dd2011-06-01 12:14:53 -050020#define MAX_NESTING (1ULL << 10)
21
Paolo Bonzini95385fe2015-11-25 22:23:31 +010022static void json_message_free_tokens(JSONMessageParser *parser)
23{
24 if (parser->tokens) {
25 g_queue_free(parser->tokens);
26 parser->tokens = NULL;
27 }
28}
29
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +010030static void json_message_process_token(JSONLexer *lexer, GString *input,
31 JSONTokenType type, int x, int y)
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060032{
33 JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
Paolo Bonzini9bada892015-11-25 22:23:32 +010034 JSONToken *token;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060035
Markus Armbrusterc5461662015-11-25 22:23:26 +010036 switch (type) {
37 case JSON_LCURLY:
38 parser->brace_count++;
39 break;
40 case JSON_RCURLY:
41 parser->brace_count--;
42 break;
43 case JSON_LSQUARE:
44 parser->bracket_count++;
45 break;
46 case JSON_RSQUARE:
47 parser->bracket_count--;
48 break;
49 default:
50 break;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060051 }
52
Paolo Bonzini9bada892015-11-25 22:23:32 +010053 token = g_malloc(sizeof(JSONToken) + input->len + 1);
54 token->type = type;
55 memcpy(token->str, input->str, input->len);
56 token->str[input->len] = 0;
57 token->x = x;
58 token->y = y;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060059
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +010060 parser->token_size += input->len;
Anthony Liguori29c75dd2011-06-01 12:14:53 -050061
Paolo Bonzini9bada892015-11-25 22:23:32 +010062 g_queue_push_tail(parser->tokens, token);
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060063
Michael Roth5e2dafe2011-06-01 12:14:59 -050064 if (type == JSON_ERROR) {
65 goto out_emit_bad;
66 } else if (parser->brace_count < 0 ||
Anthony Liguori55f83012011-06-01 12:14:51 -050067 parser->bracket_count < 0 ||
68 (parser->brace_count == 0 &&
69 parser->bracket_count == 0)) {
Michael Roth5e2dafe2011-06-01 12:14:59 -050070 goto out_emit;
Anthony Liguori29c75dd2011-06-01 12:14:53 -050071 } else if (parser->token_size > MAX_TOKEN_SIZE ||
Markus Armbrusterdf649832015-11-25 22:23:33 +010072 g_queue_get_length(parser->tokens) > MAX_TOKEN_COUNT ||
Markus Armbruster4f2d31f2015-11-25 22:23:22 +010073 parser->bracket_count + parser->brace_count > MAX_NESTING) {
Anthony Liguori29c75dd2011-06-01 12:14:53 -050074 /* Security consideration, we limit total memory allocated per object
75 * and the maximum recursion depth that a message can force.
76 */
Markus Armbruster07531132015-11-25 22:23:23 +010077 goto out_emit_bad;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060078 }
Michael Roth5e2dafe2011-06-01 12:14:59 -050079
80 return;
81
82out_emit_bad:
Markus Armbruster07531132015-11-25 22:23:23 +010083 /*
84 * Clear out token list and tell the parser to emit an error
Michael Roth5e2dafe2011-06-01 12:14:59 -050085 * indication by passing it a NULL list
86 */
Paolo Bonzini95385fe2015-11-25 22:23:31 +010087 json_message_free_tokens(parser);
Michael Roth5e2dafe2011-06-01 12:14:59 -050088out_emit:
89 /* send current list of tokens to parser and reset tokenizer */
90 parser->brace_count = 0;
91 parser->bracket_count = 0;
Paolo Bonzini95385fe2015-11-25 22:23:31 +010092 /* parser->emit takes ownership of parser->tokens. */
Michael Roth5e2dafe2011-06-01 12:14:59 -050093 parser->emit(parser, parser->tokens);
Paolo Bonzini95385fe2015-11-25 22:23:31 +010094 parser->tokens = g_queue_new();
Michael Roth5e2dafe2011-06-01 12:14:59 -050095 parser->token_size = 0;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060096}
97
98void json_message_parser_init(JSONMessageParser *parser,
Paolo Bonzini95385fe2015-11-25 22:23:31 +010099 void (*func)(JSONMessageParser *, GQueue *))
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600100{
101 parser->emit = func;
102 parser->brace_count = 0;
103 parser->bracket_count = 0;
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100104 parser->tokens = g_queue_new();
Anthony Liguori29c75dd2011-06-01 12:14:53 -0500105 parser->token_size = 0;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600106
107 json_lexer_init(&parser->lexer, json_message_process_token);
108}
109
110int json_message_parser_feed(JSONMessageParser *parser,
111 const char *buffer, size_t size)
112{
113 return json_lexer_feed(&parser->lexer, buffer, size);
114}
115
116int json_message_parser_flush(JSONMessageParser *parser)
117{
118 return json_lexer_flush(&parser->lexer);
119}
120
121void json_message_parser_destroy(JSONMessageParser *parser)
122{
123 json_lexer_destroy(&parser->lexer);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100124 json_message_free_tokens(parser);
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600125}