blob: f7a3e782bdd320a3aea8f4e52e579ff28326a53c [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 Bonzini95385fe2015-11-25 22:23:31 +010025static void json_message_free_tokens(JSONMessageParser *parser)
26{
27 if (parser->tokens) {
28 g_queue_free(parser->tokens);
29 parser->tokens = NULL;
30 }
31}
32
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +010033static void json_message_process_token(JSONLexer *lexer, GString *input,
34 JSONTokenType type, int x, int y)
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060035{
36 JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
37 QDict *dict;
38
Markus Armbrusterc5461662015-11-25 22:23:26 +010039 switch (type) {
40 case JSON_LCURLY:
41 parser->brace_count++;
42 break;
43 case JSON_RCURLY:
44 parser->brace_count--;
45 break;
46 case JSON_LSQUARE:
47 parser->bracket_count++;
48 break;
49 case JSON_RSQUARE:
50 parser->bracket_count--;
51 break;
52 default:
53 break;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060054 }
55
56 dict = qdict_new();
Luiz Capitulino2e89c062010-05-19 17:17:05 -030057 qdict_put(dict, "type", qint_from_int(type));
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +010058 qdict_put(dict, "token", qstring_from_str(input->str));
Luiz Capitulino2e89c062010-05-19 17:17:05 -030059 qdict_put(dict, "x", qint_from_int(x));
60 qdict_put(dict, "y", qint_from_int(y));
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060061
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +010062 parser->token_size += input->len;
Anthony Liguori29c75dd2011-06-01 12:14:53 -050063
Paolo Bonzini95385fe2015-11-25 22:23:31 +010064 g_queue_push_tail(parser->tokens, dict);
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060065
Michael Roth5e2dafe2011-06-01 12:14:59 -050066 if (type == JSON_ERROR) {
67 goto out_emit_bad;
68 } else if (parser->brace_count < 0 ||
Anthony Liguori55f83012011-06-01 12:14:51 -050069 parser->bracket_count < 0 ||
70 (parser->brace_count == 0 &&
71 parser->bracket_count == 0)) {
Michael Roth5e2dafe2011-06-01 12:14:59 -050072 goto out_emit;
Anthony Liguori29c75dd2011-06-01 12:14:53 -050073 } else if (parser->token_size > MAX_TOKEN_SIZE ||
Markus Armbruster4f2d31f2015-11-25 22:23:22 +010074 parser->bracket_count + parser->brace_count > MAX_NESTING) {
Anthony Liguori29c75dd2011-06-01 12:14:53 -050075 /* Security consideration, we limit total memory allocated per object
76 * and the maximum recursion depth that a message can force.
77 */
Markus Armbruster07531132015-11-25 22:23:23 +010078 goto out_emit_bad;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060079 }
Michael Roth5e2dafe2011-06-01 12:14:59 -050080
81 return;
82
83out_emit_bad:
Markus Armbruster07531132015-11-25 22:23:23 +010084 /*
85 * Clear out token list and tell the parser to emit an error
Michael Roth5e2dafe2011-06-01 12:14:59 -050086 * indication by passing it a NULL list
87 */
Paolo Bonzini95385fe2015-11-25 22:23:31 +010088 json_message_free_tokens(parser);
Michael Roth5e2dafe2011-06-01 12:14:59 -050089out_emit:
90 /* send current list of tokens to parser and reset tokenizer */
91 parser->brace_count = 0;
92 parser->bracket_count = 0;
Paolo Bonzini95385fe2015-11-25 22:23:31 +010093 /* parser->emit takes ownership of parser->tokens. */
Michael Roth5e2dafe2011-06-01 12:14:59 -050094 parser->emit(parser, parser->tokens);
Paolo Bonzini95385fe2015-11-25 22:23:31 +010095 parser->tokens = g_queue_new();
Michael Roth5e2dafe2011-06-01 12:14:59 -050096 parser->token_size = 0;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060097}
98
99void json_message_parser_init(JSONMessageParser *parser,
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100100 void (*func)(JSONMessageParser *, GQueue *))
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600101{
102 parser->emit = func;
103 parser->brace_count = 0;
104 parser->bracket_count = 0;
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100105 parser->tokens = g_queue_new();
Anthony Liguori29c75dd2011-06-01 12:14:53 -0500106 parser->token_size = 0;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600107
108 json_lexer_init(&parser->lexer, json_message_process_token);
109}
110
111int json_message_parser_feed(JSONMessageParser *parser,
112 const char *buffer, size_t size)
113{
114 return json_lexer_feed(&parser->lexer, buffer, size);
115}
116
117int json_message_parser_flush(JSONMessageParser *parser)
118{
119 return json_lexer_flush(&parser->lexer);
120}
121
122void json_message_parser_destroy(JSONMessageParser *parser)
123{
124 json_lexer_destroy(&parser->lexer);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100125 json_message_free_tokens(parser);
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600126}