]> git.ipfire.org Git - thirdparty/qemu.git/blob - qobject/json-streamer.c
qobject: Drop superfluous includes of qemu-common.h
[thirdparty/qemu.git] / qobject / json-streamer.c
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
14 #include "qemu/osdep.h"
15 #include "qapi/error.h"
16 #include "qapi/qmp/json-lexer.h"
17 #include "qapi/qmp/json-parser.h"
18 #include "qapi/qmp/json-streamer.h"
19
20 #define MAX_TOKEN_SIZE (64ULL << 20)
21 #define MAX_TOKEN_COUNT (2ULL << 20)
22 #define MAX_NESTING (1 << 10)
23
24 static void json_message_free_tokens(JSONMessageParser *parser)
25 {
26 JSONToken *token;
27
28 while ((token = g_queue_pop_head(&parser->tokens))) {
29 g_free(token);
30 }
31 }
32
33 void json_message_process_token(JSONLexer *lexer, GString *input,
34 JSONTokenType type, int x, int y)
35 {
36 JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
37 QObject *json = NULL;
38 Error *err = NULL;
39 JSONToken *token;
40
41 switch (type) {
42 case JSON_LCURLY:
43 parser->brace_count++;
44 break;
45 case JSON_RCURLY:
46 parser->brace_count--;
47 break;
48 case JSON_LSQUARE:
49 parser->bracket_count++;
50 break;
51 case JSON_RSQUARE:
52 parser->bracket_count--;
53 break;
54 case JSON_ERROR:
55 error_setg(&err, "JSON parse error, stray '%s'", input->str);
56 goto out_emit;
57 case JSON_END_OF_INPUT:
58 if (g_queue_is_empty(&parser->tokens)) {
59 return;
60 }
61 json = json_parser_parse(&parser->tokens, parser->ap, &err);
62 goto out_emit;
63 default:
64 break;
65 }
66
67 /*
68 * Security consideration, we limit total memory allocated per object
69 * and the maximum recursion depth that a message can force.
70 */
71 if (parser->token_size + input->len + 1 > MAX_TOKEN_SIZE) {
72 error_setg(&err, "JSON token size limit exceeded");
73 goto out_emit;
74 }
75 if (g_queue_get_length(&parser->tokens) + 1 > MAX_TOKEN_COUNT) {
76 error_setg(&err, "JSON token count limit exceeded");
77 goto out_emit;
78 }
79 if (parser->bracket_count + parser->brace_count > MAX_NESTING) {
80 error_setg(&err, "JSON nesting depth limit exceeded");
81 goto out_emit;
82 }
83
84 token = json_token(type, x, y, input);
85 parser->token_size += input->len;
86
87 g_queue_push_tail(&parser->tokens, token);
88
89 if ((parser->brace_count > 0 || parser->bracket_count > 0)
90 && parser->bracket_count >= 0 && parser->bracket_count >= 0) {
91 return;
92 }
93
94 json = json_parser_parse(&parser->tokens, parser->ap, &err);
95
96 out_emit:
97 parser->brace_count = 0;
98 parser->bracket_count = 0;
99 json_message_free_tokens(parser);
100 parser->token_size = 0;
101 parser->emit(parser->opaque, json, err);
102 }
103
104 void json_message_parser_init(JSONMessageParser *parser,
105 void (*emit)(void *opaque, QObject *json,
106 Error *err),
107 void *opaque, va_list *ap)
108 {
109 parser->emit = emit;
110 parser->opaque = opaque;
111 parser->ap = ap;
112 parser->brace_count = 0;
113 parser->bracket_count = 0;
114 g_queue_init(&parser->tokens);
115 parser->token_size = 0;
116
117 json_lexer_init(&parser->lexer, !!ap);
118 }
119
120 void json_message_parser_feed(JSONMessageParser *parser,
121 const char *buffer, size_t size)
122 {
123 json_lexer_feed(&parser->lexer, buffer, size);
124 }
125
126 void json_message_parser_flush(JSONMessageParser *parser)
127 {
128 json_lexer_flush(&parser->lexer);
129 assert(g_queue_is_empty(&parser->tokens));
130 }
131
132 void json_message_parser_destroy(JSONMessageParser *parser)
133 {
134 json_lexer_destroy(&parser->lexer);
135 json_message_free_tokens(parser);
136 }