]> git.proxmox.com Git - mirror_qemu.git/blame - qobject/json-streamer.c
Merge remote-tracking branch 'remotes/ehabkost/tags/x86-pull-request' into staging
[mirror_qemu.git] / qobject / json-streamer.c
CommitLineData
d7ff3acb
AL
1/*
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
f2ad72b3 14#include "qemu/osdep.h"
d7ff3acb 15#include "qemu-common.h"
7b1b5d19
PB
16#include "qapi/qmp/json-lexer.h"
17#include "qapi/qmp/json-streamer.h"
d7ff3acb 18
29c75ddd 19#define MAX_TOKEN_SIZE (64ULL << 20)
df649835 20#define MAX_TOKEN_COUNT (2ULL << 20)
29c75ddd
AL
21#define MAX_NESTING (1ULL << 10)
22
ba4dba54
EB
23static void json_message_free_token(void *token, void *opaque)
24{
25 g_free(token);
26}
27
95385fe9
PB
28static void json_message_free_tokens(JSONMessageParser *parser)
29{
30 if (parser->tokens) {
ba4dba54 31 g_queue_foreach(parser->tokens, json_message_free_token, NULL);
95385fe9
PB
32 g_queue_free(parser->tokens);
33 parser->tokens = NULL;
34 }
35}
36
d2ca7c0b
PB
37static void json_message_process_token(JSONLexer *lexer, GString *input,
38 JSONTokenType type, int x, int y)
d7ff3acb
AL
39{
40 JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
9bada897 41 JSONToken *token;
a942d8fa 42 GQueue *tokens;
d7ff3acb 43
c5461660
MA
44 switch (type) {
45 case JSON_LCURLY:
46 parser->brace_count++;
47 break;
48 case JSON_RCURLY:
49 parser->brace_count--;
50 break;
51 case JSON_LSQUARE:
52 parser->bracket_count++;
53 break;
54 case JSON_RSQUARE:
55 parser->bracket_count--;
56 break;
57 default:
58 break;
d7ff3acb
AL
59 }
60
9bada897
PB
61 token = g_malloc(sizeof(JSONToken) + input->len + 1);
62 token->type = type;
63 memcpy(token->str, input->str, input->len);
64 token->str[input->len] = 0;
65 token->x = x;
66 token->y = y;
d7ff3acb 67
d2ca7c0b 68 parser->token_size += input->len;
29c75ddd 69
9bada897 70 g_queue_push_tail(parser->tokens, token);
d7ff3acb 71
5e2dafeb
MR
72 if (type == JSON_ERROR) {
73 goto out_emit_bad;
74 } else if (parser->brace_count < 0 ||
55f8301f
AL
75 parser->bracket_count < 0 ||
76 (parser->brace_count == 0 &&
77 parser->bracket_count == 0)) {
5e2dafeb 78 goto out_emit;
29c75ddd 79 } else if (parser->token_size > MAX_TOKEN_SIZE ||
df649835 80 g_queue_get_length(parser->tokens) > MAX_TOKEN_COUNT ||
4f2d31fb 81 parser->bracket_count + parser->brace_count > MAX_NESTING) {
29c75ddd
AL
82 /* Security consideration, we limit total memory allocated per object
83 * and the maximum recursion depth that a message can force.
84 */
0753113a 85 goto out_emit_bad;
5e2dafeb
MR
86 }
87
88 return;
89
90out_emit_bad:
0753113a
MA
91 /*
92 * Clear out token list and tell the parser to emit an error
5e2dafeb
MR
93 * indication by passing it a NULL list
94 */
95385fe9 95 json_message_free_tokens(parser);
5e2dafeb
MR
96out_emit:
97 /* send current list of tokens to parser and reset tokenizer */
98 parser->brace_count = 0;
99 parser->bracket_count = 0;
a942d8fa
PB
100 /* parser->emit takes ownership of parser->tokens. Remove our own
101 * reference to parser->tokens before handing it out to parser->emit.
102 */
103 tokens = parser->tokens;
95385fe9 104 parser->tokens = g_queue_new();
a942d8fa 105 parser->emit(parser, tokens);
5e2dafeb 106 parser->token_size = 0;
d7ff3acb
AL
107}
108
109void json_message_parser_init(JSONMessageParser *parser,
95385fe9 110 void (*func)(JSONMessageParser *, GQueue *))
d7ff3acb
AL
111{
112 parser->emit = func;
113 parser->brace_count = 0;
114 parser->bracket_count = 0;
95385fe9 115 parser->tokens = g_queue_new();
29c75ddd 116 parser->token_size = 0;
d7ff3acb
AL
117
118 json_lexer_init(&parser->lexer, json_message_process_token);
119}
120
121int json_message_parser_feed(JSONMessageParser *parser,
122 const char *buffer, size_t size)
123{
124 return json_lexer_feed(&parser->lexer, buffer, size);
125}
126
127int json_message_parser_flush(JSONMessageParser *parser)
128{
129 return json_lexer_flush(&parser->lexer);
130}
131
132void json_message_parser_destroy(JSONMessageParser *parser)
133{
134 json_lexer_destroy(&parser->lexer);
95385fe9 135 json_message_free_tokens(parser);
d7ff3acb 136}