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