2009-11-11 16:38:59 +00:00
|
|
|
/*
|
|
|
|
* JSON streaming support
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2009
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
|
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-29 17:50:01 +00:00
|
|
|
#include "qemu/osdep.h"
|
2009-11-11 16:38:59 +00:00
|
|
|
#include "qemu-common.h"
|
2012-12-17 17:19:43 +00:00
|
|
|
#include "qapi/qmp/json-lexer.h"
|
|
|
|
#include "qapi/qmp/json-streamer.h"
|
2009-11-11 16:38:59 +00:00
|
|
|
|
2011-06-01 17:14:53 +00:00
|
|
|
#define MAX_TOKEN_SIZE (64ULL << 20)
|
2015-11-25 21:23:33 +00:00
|
|
|
#define MAX_TOKEN_COUNT (2ULL << 20)
|
2011-06-01 17:14:53 +00:00
|
|
|
#define MAX_NESTING (1ULL << 10)
|
|
|
|
|
2016-05-18 21:46:52 +00:00
|
|
|
static void json_message_free_token(void *token, void *opaque)
|
|
|
|
{
|
|
|
|
g_free(token);
|
|
|
|
}
|
|
|
|
|
2015-11-25 21:23:31 +00:00
|
|
|
static void json_message_free_tokens(JSONMessageParser *parser)
|
|
|
|
{
|
|
|
|
if (parser->tokens) {
|
2016-05-18 21:46:52 +00:00
|
|
|
g_queue_foreach(parser->tokens, json_message_free_token, NULL);
|
2015-11-25 21:23:31 +00:00
|
|
|
g_queue_free(parser->tokens);
|
|
|
|
parser->tokens = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 16:40:00 +00:00
|
|
|
void json_message_process_token(JSONLexer *lexer, GString *input,
|
|
|
|
JSONTokenType type, int x, int y)
|
2009-11-11 16:38:59 +00:00
|
|
|
{
|
|
|
|
JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
|
2015-11-25 21:23:32 +00:00
|
|
|
JSONToken *token;
|
2016-07-04 12:40:59 +00:00
|
|
|
GQueue *tokens;
|
2009-11-11 16:38:59 +00:00
|
|
|
|
2015-11-25 21:23:26 +00:00
|
|
|
switch (type) {
|
|
|
|
case JSON_LCURLY:
|
|
|
|
parser->brace_count++;
|
|
|
|
break;
|
|
|
|
case JSON_RCURLY:
|
|
|
|
parser->brace_count--;
|
|
|
|
break;
|
|
|
|
case JSON_LSQUARE:
|
|
|
|
parser->bracket_count++;
|
|
|
|
break;
|
|
|
|
case JSON_RSQUARE:
|
|
|
|
parser->bracket_count--;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2009-11-11 16:38:59 +00:00
|
|
|
}
|
|
|
|
|
2015-11-25 21:23:32 +00:00
|
|
|
token = g_malloc(sizeof(JSONToken) + input->len + 1);
|
|
|
|
token->type = type;
|
|
|
|
memcpy(token->str, input->str, input->len);
|
|
|
|
token->str[input->len] = 0;
|
|
|
|
token->x = x;
|
|
|
|
token->y = y;
|
2009-11-11 16:38:59 +00:00
|
|
|
|
2015-11-25 21:23:29 +00:00
|
|
|
parser->token_size += input->len;
|
2011-06-01 17:14:53 +00:00
|
|
|
|
2015-11-25 21:23:32 +00:00
|
|
|
g_queue_push_tail(parser->tokens, token);
|
2009-11-11 16:38:59 +00:00
|
|
|
|
2011-06-01 17:14:59 +00:00
|
|
|
if (type == JSON_ERROR) {
|
|
|
|
goto out_emit_bad;
|
|
|
|
} else if (parser->brace_count < 0 ||
|
2011-06-01 17:14:51 +00:00
|
|
|
parser->bracket_count < 0 ||
|
|
|
|
(parser->brace_count == 0 &&
|
|
|
|
parser->bracket_count == 0)) {
|
2011-06-01 17:14:59 +00:00
|
|
|
goto out_emit;
|
2011-06-01 17:14:53 +00:00
|
|
|
} else if (parser->token_size > MAX_TOKEN_SIZE ||
|
2015-11-25 21:23:33 +00:00
|
|
|
g_queue_get_length(parser->tokens) > MAX_TOKEN_COUNT ||
|
2015-11-25 21:23:22 +00:00
|
|
|
parser->bracket_count + parser->brace_count > MAX_NESTING) {
|
2011-06-01 17:14:53 +00:00
|
|
|
/* Security consideration, we limit total memory allocated per object
|
|
|
|
* and the maximum recursion depth that a message can force.
|
|
|
|
*/
|
2015-11-25 21:23:23 +00:00
|
|
|
goto out_emit_bad;
|
2011-06-01 17:14:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
out_emit_bad:
|
2015-11-25 21:23:23 +00:00
|
|
|
/*
|
|
|
|
* Clear out token list and tell the parser to emit an error
|
2011-06-01 17:14:59 +00:00
|
|
|
* indication by passing it a NULL list
|
|
|
|
*/
|
2015-11-25 21:23:31 +00:00
|
|
|
json_message_free_tokens(parser);
|
2011-06-01 17:14:59 +00:00
|
|
|
out_emit:
|
|
|
|
/* send current list of tokens to parser and reset tokenizer */
|
|
|
|
parser->brace_count = 0;
|
|
|
|
parser->bracket_count = 0;
|
2016-07-04 12:40:59 +00:00
|
|
|
/* parser->emit takes ownership of parser->tokens. Remove our own
|
|
|
|
* reference to parser->tokens before handing it out to parser->emit.
|
|
|
|
*/
|
|
|
|
tokens = parser->tokens;
|
2015-11-25 21:23:31 +00:00
|
|
|
parser->tokens = g_queue_new();
|
2016-07-04 12:40:59 +00:00
|
|
|
parser->emit(parser, tokens);
|
2011-06-01 17:14:59 +00:00
|
|
|
parser->token_size = 0;
|
2009-11-11 16:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void json_message_parser_init(JSONMessageParser *parser,
|
2015-11-25 21:23:31 +00:00
|
|
|
void (*func)(JSONMessageParser *, GQueue *))
|
2009-11-11 16:38:59 +00:00
|
|
|
{
|
|
|
|
parser->emit = func;
|
|
|
|
parser->brace_count = 0;
|
|
|
|
parser->bracket_count = 0;
|
2015-11-25 21:23:31 +00:00
|
|
|
parser->tokens = g_queue_new();
|
2011-06-01 17:14:53 +00:00
|
|
|
parser->token_size = 0;
|
2009-11-11 16:38:59 +00:00
|
|
|
|
2018-08-23 16:40:00 +00:00
|
|
|
json_lexer_init(&parser->lexer);
|
2009-11-11 16:38:59 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 16:39:58 +00:00
|
|
|
void json_message_parser_feed(JSONMessageParser *parser,
|
2009-11-11 16:38:59 +00:00
|
|
|
const char *buffer, size_t size)
|
|
|
|
{
|
2018-08-23 16:39:58 +00:00
|
|
|
json_lexer_feed(&parser->lexer, buffer, size);
|
2009-11-11 16:38:59 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 16:39:58 +00:00
|
|
|
void json_message_parser_flush(JSONMessageParser *parser)
|
2009-11-11 16:38:59 +00:00
|
|
|
{
|
2018-08-23 16:39:58 +00:00
|
|
|
json_lexer_flush(&parser->lexer);
|
2009-11-11 16:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void json_message_parser_destroy(JSONMessageParser *parser)
|
|
|
|
{
|
|
|
|
json_lexer_destroy(&parser->lexer);
|
2015-11-25 21:23:31 +00:00
|
|
|
json_message_free_tokens(parser);
|
2009-11-11 16:38:59 +00:00
|
|
|
}
|