1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include "qemu/osdep.h"
16#include "qapi/error.h"
17#include "qapi/qmp-input-visitor.h"
18#include "qapi/visitor-impl.h"
19#include "qemu/queue.h"
20#include "qemu-common.h"
21#include "qapi/qmp/types.h"
22#include "qapi/qmp/qerror.h"
23
24#define QIV_STACK_SIZE 1024
25
26typedef struct StackObject
27{
28 QObject *obj;
29 const QListEntry *entry;
30 GHashTable *h;
31} StackObject;
32
33struct QmpInputVisitor
34{
35 Visitor visitor;
36 StackObject stack[QIV_STACK_SIZE];
37 int nb_stack;
38 bool strict;
39};
40
41static QmpInputVisitor *to_qiv(Visitor *v)
42{
43 return container_of(v, QmpInputVisitor, visitor);
44}
45
46static QObject *qmp_input_get_object(QmpInputVisitor *qiv,
47 const char *name,
48 bool consume)
49{
50 QObject *qobj = qiv->stack[qiv->nb_stack - 1].obj;
51
52 if (qobj) {
53 if (name && qobject_type(qobj) == QTYPE_QDICT) {
54 if (qiv->stack[qiv->nb_stack - 1].h && consume) {
55 g_hash_table_remove(qiv->stack[qiv->nb_stack - 1].h, name);
56 }
57 return qdict_get(qobject_to_qdict(qobj), name);
58 } else if (qiv->stack[qiv->nb_stack - 1].entry) {
59 return qlist_entry_obj(qiv->stack[qiv->nb_stack - 1].entry);
60 }
61 }
62
63 return qobj;
64}
65
66static void qdict_add_key(const char *key, QObject *obj, void *opaque)
67{
68 GHashTable *h = opaque;
69 g_hash_table_insert(h, (gpointer) key, NULL);
70}
71
72static void qmp_input_push(QmpInputVisitor *qiv, QObject *obj, Error **errp)
73{
74 GHashTable *h;
75
76 if (qiv->nb_stack >= QIV_STACK_SIZE) {
77 error_setg(errp, "An internal buffer overran");
78 return;
79 }
80
81 qiv->stack[qiv->nb_stack].obj = obj;
82 qiv->stack[qiv->nb_stack].entry = NULL;
83 qiv->stack[qiv->nb_stack].h = NULL;
84
85 if (qiv->strict && qobject_type(obj) == QTYPE_QDICT) {
86 h = g_hash_table_new(g_str_hash, g_str_equal);
87 qdict_iter(qobject_to_qdict(obj), qdict_add_key, h);
88 qiv->stack[qiv->nb_stack].h = h;
89 }
90
91 qiv->nb_stack++;
92}
93
94
95static void qmp_input_pop(QmpInputVisitor *qiv, Error **errp)
96{
97 assert(qiv->nb_stack > 0);
98
99 if (qiv->strict) {
100 GHashTable * const top_ht = qiv->stack[qiv->nb_stack - 1].h;
101 if (top_ht) {
102 GHashTableIter iter;
103 const char *key;
104
105 g_hash_table_iter_init(&iter, top_ht);
106 if (g_hash_table_iter_next(&iter, (void **)&key, NULL)) {
107 error_setg(errp, QERR_QMP_EXTRA_MEMBER, key);
108 }
109 g_hash_table_unref(top_ht);
110 }
111 }
112
113 qiv->nb_stack--;
114}
115
116static void qmp_input_start_struct(Visitor *v, const char *name, void **obj,
117 size_t size, Error **errp)
118{
119 QmpInputVisitor *qiv = to_qiv(v);
120 QObject *qobj = qmp_input_get_object(qiv, name, true);
121 Error *err = NULL;
122
123 if (!qobj || qobject_type(qobj) != QTYPE_QDICT) {
124 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
125 "QDict");
126 return;
127 }
128
129 qmp_input_push(qiv, qobj, &err);
130 if (err) {
131 error_propagate(errp, err);
132 return;
133 }
134
135 if (obj) {
136 *obj = g_malloc0(size);
137 }
138}
139
140static void qmp_input_end_struct(Visitor *v, Error **errp)
141{
142 QmpInputVisitor *qiv = to_qiv(v);
143
144 qmp_input_pop(qiv, errp);
145}
146
147static void qmp_input_start_list(Visitor *v, const char *name, Error **errp)
148{
149 QmpInputVisitor *qiv = to_qiv(v);
150 QObject *qobj = qmp_input_get_object(qiv, name, true);
151
152 if (!qobj || qobject_type(qobj) != QTYPE_QLIST) {
153 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
154 "list");
155 return;
156 }
157
158 qmp_input_push(qiv, qobj, errp);
159}
160
161static GenericList *qmp_input_next_list(Visitor *v, GenericList **list,
162 size_t size)
163{
164 QmpInputVisitor *qiv = to_qiv(v);
165 GenericList *entry;
166 StackObject *so = &qiv->stack[qiv->nb_stack - 1];
167 bool first;
168
169 if (so->entry == NULL) {
170 so->entry = qlist_first(qobject_to_qlist(so->obj));
171 first = true;
172 } else {
173 so->entry = qlist_next(so->entry);
174 first = false;
175 }
176
177 if (so->entry == NULL) {
178 return NULL;
179 }
180
181 entry = g_malloc0(size);
182 if (first) {
183 *list = entry;
184 } else {
185 (*list)->next = entry;
186 }
187
188 return entry;
189}
190
191static void qmp_input_end_list(Visitor *v)
192{
193 QmpInputVisitor *qiv = to_qiv(v);
194
195 qmp_input_pop(qiv, &error_abort);
196}
197
198static void qmp_input_start_alternate(Visitor *v, const char *name,
199 GenericAlternate **obj, size_t size,
200 bool promote_int, Error **errp)
201{
202 QmpInputVisitor *qiv = to_qiv(v);
203 QObject *qobj = qmp_input_get_object(qiv, name, false);
204
205 if (!qobj) {
206 *obj = NULL;
207 error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
208 return;
209 }
210 *obj = g_malloc0(size);
211 (*obj)->type = qobject_type(qobj);
212 if (promote_int && (*obj)->type == QTYPE_QINT) {
213 (*obj)->type = QTYPE_QFLOAT;
214 }
215}
216
217static void qmp_input_type_int64(Visitor *v, const char *name, int64_t *obj,
218 Error **errp)
219{
220 QmpInputVisitor *qiv = to_qiv(v);
221 QInt *qint = qobject_to_qint(qmp_input_get_object(qiv, name, true));
222
223 if (!qint) {
224 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
225 "integer");
226 return;
227 }
228
229 *obj = qint_get_int(qint);
230}
231
232static void qmp_input_type_uint64(Visitor *v, const char *name, uint64_t *obj,
233 Error **errp)
234{
235
236 QmpInputVisitor *qiv = to_qiv(v);
237 QInt *qint = qobject_to_qint(qmp_input_get_object(qiv, name, true));
238
239 if (!qint) {
240 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
241 "integer");
242 return;
243 }
244
245 *obj = qint_get_int(qint);
246}
247
248static void qmp_input_type_bool(Visitor *v, const char *name, bool *obj,
249 Error **errp)
250{
251 QmpInputVisitor *qiv = to_qiv(v);
252 QBool *qbool = qobject_to_qbool(qmp_input_get_object(qiv, name, true));
253
254 if (!qbool) {
255 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
256 "boolean");
257 return;
258 }
259
260 *obj = qbool_get_bool(qbool);
261}
262
263static void qmp_input_type_str(Visitor *v, const char *name, char **obj,
264 Error **errp)
265{
266 QmpInputVisitor *qiv = to_qiv(v);
267 QString *qstr = qobject_to_qstring(qmp_input_get_object(qiv, name, true));
268
269 if (!qstr) {
270 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
271 "string");
272 return;
273 }
274
275 *obj = g_strdup(qstring_get_str(qstr));
276}
277
278static void qmp_input_type_number(Visitor *v, const char *name, double *obj,
279 Error **errp)
280{
281 QmpInputVisitor *qiv = to_qiv(v);
282 QObject *qobj = qmp_input_get_object(qiv, name, true);
283 QInt *qint;
284 QFloat *qfloat;
285
286 qint = qobject_to_qint(qobj);
287 if (qint) {
288 *obj = qint_get_int(qobject_to_qint(qobj));
289 return;
290 }
291
292 qfloat = qobject_to_qfloat(qobj);
293 if (qfloat) {
294 *obj = qfloat_get_double(qobject_to_qfloat(qobj));
295 return;
296 }
297
298 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
299 "number");
300}
301
302static void qmp_input_type_any(Visitor *v, const char *name, QObject **obj,
303 Error **errp)
304{
305 QmpInputVisitor *qiv = to_qiv(v);
306 QObject *qobj = qmp_input_get_object(qiv, name, true);
307
308 qobject_incref(qobj);
309 *obj = qobj;
310}
311
312static void qmp_input_optional(Visitor *v, const char *name, bool *present)
313{
314 QmpInputVisitor *qiv = to_qiv(v);
315 QObject *qobj = qmp_input_get_object(qiv, name, true);
316
317 if (!qobj) {
318 *present = false;
319 return;
320 }
321
322 *present = true;
323}
324
325Visitor *qmp_input_get_visitor(QmpInputVisitor *v)
326{
327 return &v->visitor;
328}
329
330void qmp_input_visitor_cleanup(QmpInputVisitor *v)
331{
332 qobject_decref(v->stack[0].obj);
333 g_free(v);
334}
335
336QmpInputVisitor *qmp_input_visitor_new(QObject *obj)
337{
338 QmpInputVisitor *v;
339
340 v = g_malloc0(sizeof(*v));
341
342 v->visitor.start_struct = qmp_input_start_struct;
343 v->visitor.end_struct = qmp_input_end_struct;
344 v->visitor.start_list = qmp_input_start_list;
345 v->visitor.next_list = qmp_input_next_list;
346 v->visitor.end_list = qmp_input_end_list;
347 v->visitor.start_alternate = qmp_input_start_alternate;
348 v->visitor.type_enum = input_type_enum;
349 v->visitor.type_int64 = qmp_input_type_int64;
350 v->visitor.type_uint64 = qmp_input_type_uint64;
351 v->visitor.type_bool = qmp_input_type_bool;
352 v->visitor.type_str = qmp_input_type_str;
353 v->visitor.type_number = qmp_input_type_number;
354 v->visitor.type_any = qmp_input_type_any;
355 v->visitor.optional = qmp_input_optional;
356
357 qmp_input_push(v, obj, NULL);
358 qobject_incref(obj);
359
360 return v;
361}
362
363QmpInputVisitor *qmp_input_visitor_new_strict(QObject *obj)
364{
365 QmpInputVisitor *v;
366
367 v = qmp_input_visitor_new(obj);
368 v->strict = true;
369
370 return v;
371}
372