]> git.proxmox.com Git - mirror_qemu.git/blame - qobject/json-streamer.c
qobject: Drop superfluous includes of qemu-common.h
[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"
84a56f38 15#include "qapi/error.h"
7b1b5d19 16#include "qapi/qmp/json-lexer.h"
62815d85 17#include "qapi/qmp/json-parser.h"
7b1b5d19 18#include "qapi/qmp/json-streamer.h"
d7ff3acb 19
29c75ddd 20#define MAX_TOKEN_SIZE (64ULL << 20)
df649835 21#define MAX_TOKEN_COUNT (2ULL << 20)
da09cfbf 22#define MAX_NESTING (1 << 10)
29c75ddd 23
95385fe9
PB
24static void json_message_free_tokens(JSONMessageParser *parser)
25{
a2731e08
MA
26 JSONToken *token;
27
28 while ((token = g_queue_pop_head(&parser->tokens))) {
29 g_free(token);
95385fe9
PB
30 }
31}
32
037f2440
MA
33void json_message_process_token(JSONLexer *lexer, GString *input,
34 JSONTokenType type, int x, int y)
d7ff3acb
AL
35{
36 JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
ff281a27 37 QObject *json = NULL;
62815d85 38 Error *err = NULL;
9bada897 39 JSONToken *token;
d7ff3acb 40
c5461660
MA
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;
269e57ae 54 case JSON_ERROR:
84a56f38 55 error_setg(&err, "JSON parse error, stray '%s'", input->str);
269e57ae 56 goto out_emit;
f9277915 57 case JSON_END_OF_INPUT:
a2731e08 58 if (g_queue_is_empty(&parser->tokens)) {
f9277915
MA
59 return;
60 }
a2731e08 61 json = json_parser_parse(&parser->tokens, parser->ap, &err);
f9277915 62 goto out_emit;
c5461660
MA
63 default:
64 break;
d7ff3acb
AL
65 }
66
da09cfbf
MA
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 }
a2731e08 75 if (g_queue_get_length(&parser->tokens) + 1 > MAX_TOKEN_COUNT) {
da09cfbf
MA
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
abe7c206 84 token = json_token(type, x, y, input);
d2ca7c0b 85 parser->token_size += input->len;
29c75ddd 86
a2731e08 87 g_queue_push_tail(&parser->tokens, token);
d7ff3acb 88
8d3265b3
MA
89 if ((parser->brace_count > 0 || parser->bracket_count > 0)
90 && parser->bracket_count >= 0 && parser->bracket_count >= 0) {
91 return;
ff281a27
MA
92 }
93
a2731e08 94 json = json_parser_parse(&parser->tokens, parser->ap, &err);
5e2dafeb 95
5e2dafeb 96out_emit:
5e2dafeb
MR
97 parser->brace_count = 0;
98 parser->bracket_count = 0;
ff281a27 99 json_message_free_tokens(parser);
5e2dafeb 100 parser->token_size = 0;
62815d85 101 parser->emit(parser->opaque, json, err);
d7ff3acb
AL
102}
103
104void json_message_parser_init(JSONMessageParser *parser,
62815d85
MA
105 void (*emit)(void *opaque, QObject *json,
106 Error *err),
107 void *opaque, va_list *ap)
d7ff3acb 108{
62815d85
MA
109 parser->emit = emit;
110 parser->opaque = opaque;
111 parser->ap = ap;
d7ff3acb
AL
112 parser->brace_count = 0;
113 parser->bracket_count = 0;
a2731e08 114 g_queue_init(&parser->tokens);
29c75ddd 115 parser->token_size = 0;
d7ff3acb 116
2cbd15aa 117 json_lexer_init(&parser->lexer, !!ap);
d7ff3acb
AL
118}
119
7c1e1d54 120void json_message_parser_feed(JSONMessageParser *parser,
d7ff3acb
AL
121 const char *buffer, size_t size)
122{
7c1e1d54 123 json_lexer_feed(&parser->lexer, buffer, size);
d7ff3acb
AL
124}
125
7c1e1d54 126void json_message_parser_flush(JSONMessageParser *parser)
d7ff3acb 127{
7c1e1d54 128 json_lexer_flush(&parser->lexer);
a2731e08 129 assert(g_queue_is_empty(&parser->tokens));
d7ff3acb
AL
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}