]> git.proxmox.com Git - mirror_qemu.git/blame - qobject/json-streamer.c
MAINTAINERS: Remove Blue Swirl leftovers
[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
95385fe9
PB
23static void json_message_free_tokens(JSONMessageParser *parser)
24{
25 if (parser->tokens) {
26 g_queue_free(parser->tokens);
27 parser->tokens = NULL;
28 }
29}
30
d2ca7c0b
PB
31static void json_message_process_token(JSONLexer *lexer, GString *input,
32 JSONTokenType type, int x, int y)
d7ff3acb
AL
33{
34 JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
9bada897 35 JSONToken *token;
d7ff3acb 36
c5461660
MA
37 switch (type) {
38 case JSON_LCURLY:
39 parser->brace_count++;
40 break;
41 case JSON_RCURLY:
42 parser->brace_count--;
43 break;
44 case JSON_LSQUARE:
45 parser->bracket_count++;
46 break;
47 case JSON_RSQUARE:
48 parser->bracket_count--;
49 break;
50 default:
51 break;
d7ff3acb
AL
52 }
53
9bada897
PB
54 token = g_malloc(sizeof(JSONToken) + input->len + 1);
55 token->type = type;
56 memcpy(token->str, input->str, input->len);
57 token->str[input->len] = 0;
58 token->x = x;
59 token->y = y;
d7ff3acb 60
d2ca7c0b 61 parser->token_size += input->len;
29c75ddd 62
9bada897 63 g_queue_push_tail(parser->tokens, token);
d7ff3acb 64
5e2dafeb
MR
65 if (type == JSON_ERROR) {
66 goto out_emit_bad;
67 } else if (parser->brace_count < 0 ||
55f8301f
AL
68 parser->bracket_count < 0 ||
69 (parser->brace_count == 0 &&
70 parser->bracket_count == 0)) {
5e2dafeb 71 goto out_emit;
29c75ddd 72 } else if (parser->token_size > MAX_TOKEN_SIZE ||
df649835 73 g_queue_get_length(parser->tokens) > MAX_TOKEN_COUNT ||
4f2d31fb 74 parser->bracket_count + parser->brace_count > MAX_NESTING) {
29c75ddd
AL
75 /* Security consideration, we limit total memory allocated per object
76 * and the maximum recursion depth that a message can force.
77 */
0753113a 78 goto out_emit_bad;
5e2dafeb
MR
79 }
80
81 return;
82
83out_emit_bad:
0753113a
MA
84 /*
85 * Clear out token list and tell the parser to emit an error
5e2dafeb
MR
86 * indication by passing it a NULL list
87 */
95385fe9 88 json_message_free_tokens(parser);
5e2dafeb
MR
89out_emit:
90 /* send current list of tokens to parser and reset tokenizer */
91 parser->brace_count = 0;
92 parser->bracket_count = 0;
95385fe9 93 /* parser->emit takes ownership of parser->tokens. */
5e2dafeb 94 parser->emit(parser, parser->tokens);
95385fe9 95 parser->tokens = g_queue_new();
5e2dafeb 96 parser->token_size = 0;
d7ff3acb
AL
97}
98
99void json_message_parser_init(JSONMessageParser *parser,
95385fe9 100 void (*func)(JSONMessageParser *, GQueue *))
d7ff3acb
AL
101{
102 parser->emit = func;
103 parser->brace_count = 0;
104 parser->bracket_count = 0;
95385fe9 105 parser->tokens = g_queue_new();
29c75ddd 106 parser->token_size = 0;
d7ff3acb
AL
107
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);
95385fe9 125 json_message_free_tokens(parser);
d7ff3acb 126}