blob: 4a161a17a1c760f937cf6baedf718ed5584529b0 [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"
15#include "qapi/qmp/qint.h"
16#include "qapi/qmp/qdict.h"
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060017#include "qemu-common.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010018#include "qapi/qmp/json-lexer.h"
19#include "qapi/qmp/json-streamer.h"
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060020
Anthony Liguori29c75dd2011-06-01 12:14:53 -050021#define MAX_TOKEN_SIZE (64ULL << 20)
22#define MAX_NESTING (1ULL << 10)
23
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060024static void json_message_process_token(JSONLexer *lexer, QString *token, JSONTokenType type, int x, int y)
25{
26 JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
27 QDict *dict;
28
Markus Armbrusterc5461662015-11-25 22:23:26 +010029 switch (type) {
30 case JSON_LCURLY:
31 parser->brace_count++;
32 break;
33 case JSON_RCURLY:
34 parser->brace_count--;
35 break;
36 case JSON_LSQUARE:
37 parser->bracket_count++;
38 break;
39 case JSON_RSQUARE:
40 parser->bracket_count--;
41 break;
42 default:
43 break;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060044 }
45
46 dict = qdict_new();
Luiz Capitulino2e89c062010-05-19 17:17:05 -030047 qdict_put(dict, "type", qint_from_int(type));
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060048 QINCREF(token);
Luiz Capitulino2e89c062010-05-19 17:17:05 -030049 qdict_put(dict, "token", token);
50 qdict_put(dict, "x", qint_from_int(x));
51 qdict_put(dict, "y", qint_from_int(y));
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060052
Anthony Liguori29c75dd2011-06-01 12:14:53 -050053 parser->token_size += token->length;
54
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060055 qlist_append(parser->tokens, dict);
56
Michael Roth5e2dafe2011-06-01 12:14:59 -050057 if (type == JSON_ERROR) {
58 goto out_emit_bad;
59 } else if (parser->brace_count < 0 ||
Anthony Liguori55f83012011-06-01 12:14:51 -050060 parser->bracket_count < 0 ||
61 (parser->brace_count == 0 &&
62 parser->bracket_count == 0)) {
Michael Roth5e2dafe2011-06-01 12:14:59 -050063 goto out_emit;
Anthony Liguori29c75dd2011-06-01 12:14:53 -050064 } else if (parser->token_size > MAX_TOKEN_SIZE ||
Markus Armbruster4f2d31f2015-11-25 22:23:22 +010065 parser->bracket_count + parser->brace_count > MAX_NESTING) {
Anthony Liguori29c75dd2011-06-01 12:14:53 -050066 /* Security consideration, we limit total memory allocated per object
67 * and the maximum recursion depth that a message can force.
68 */
Markus Armbruster07531132015-11-25 22:23:23 +010069 goto out_emit_bad;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060070 }
Michael Roth5e2dafe2011-06-01 12:14:59 -050071
72 return;
73
74out_emit_bad:
Markus Armbruster07531132015-11-25 22:23:23 +010075 /*
76 * Clear out token list and tell the parser to emit an error
Michael Roth5e2dafe2011-06-01 12:14:59 -050077 * indication by passing it a NULL list
78 */
79 QDECREF(parser->tokens);
80 parser->tokens = NULL;
81out_emit:
82 /* send current list of tokens to parser and reset tokenizer */
83 parser->brace_count = 0;
84 parser->bracket_count = 0;
85 parser->emit(parser, parser->tokens);
86 if (parser->tokens) {
87 QDECREF(parser->tokens);
88 }
89 parser->tokens = qlist_new();
90 parser->token_size = 0;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060091}
92
93void json_message_parser_init(JSONMessageParser *parser,
94 void (*func)(JSONMessageParser *, QList *))
95{
96 parser->emit = func;
97 parser->brace_count = 0;
98 parser->bracket_count = 0;
99 parser->tokens = qlist_new();
Anthony Liguori29c75dd2011-06-01 12:14:53 -0500100 parser->token_size = 0;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600101
102 json_lexer_init(&parser->lexer, json_message_process_token);
103}
104
105int json_message_parser_feed(JSONMessageParser *parser,
106 const char *buffer, size_t size)
107{
108 return json_lexer_feed(&parser->lexer, buffer, size);
109}
110
111int json_message_parser_flush(JSONMessageParser *parser)
112{
113 return json_lexer_flush(&parser->lexer);
114}
115
116void json_message_parser_destroy(JSONMessageParser *parser)
117{
118 json_lexer_destroy(&parser->lexer);
119 QDECREF(parser->tokens);
120}