1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82#include "qemu/osdep.h"
83#include "qapi/error.h"
84#include "qapi/qmp/qdict.h"
85#include "qapi/qmp/qlist.h"
86#include "qapi/qmp/qstring.h"
87#include "qemu/cutils.h"
88#include "qemu/option.h"
89
90
91
92
93
94
95
96
97
98
99
100
101
102static int key_to_index(const char *key, const char **end)
103{
104 int ret;
105 unsigned long index;
106
107 if (*key < '0' || *key > '9') {
108 return -EINVAL;
109 }
110 ret = qemu_strtoul(key, end, 10, &index);
111 if (ret) {
112 return ret == -ERANGE ? INT_MAX : ret;
113 }
114 return index <= INT_MAX ? index : INT_MAX;
115}
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134static QObject *keyval_parse_put(QDict *cur,
135 const char *key_in_cur, QString *value,
136 const char *key, const char *key_cursor,
137 Error **errp)
138{
139 QObject *old, *new;
140
141 old = qdict_get(cur, key_in_cur);
142 if (old) {
143 if (qobject_type(old) != (value ? QTYPE_QSTRING : QTYPE_QDICT)) {
144 error_setg(errp, "Parameters '%.*s.*' used inconsistently",
145 (int)(key_cursor - key), key);
146 qobject_unref(value);
147 return NULL;
148 }
149 if (!value) {
150 return old;
151 }
152 new = QOBJECT(value);
153 } else {
154 new = value ? QOBJECT(value) : QOBJECT(qdict_new());
155 }
156 qdict_put_obj(cur, key_in_cur, new);
157 return new;
158}
159
160
161
162
163
164
165
166
167
168static const char *keyval_parse_one(QDict *qdict, const char *params,
169 const char *implied_key,
170 Error **errp)
171{
172 const char *key, *key_end, *s, *end;
173 size_t len;
174 char key_in_cur[128];
175 QDict *cur;
176 int ret;
177 QObject *next;
178 QString *val;
179
180 key = params;
181 len = strcspn(params, "=,");
182 if (implied_key && len && key[len] != '=') {
183
184 key = implied_key;
185 len = strlen(implied_key);
186 }
187 key_end = key + len;
188
189
190
191
192
193 cur = qdict;
194 s = key;
195 for (;;) {
196
197 if (s != key && key_to_index(s, &end) >= 0) {
198 len = end - s;
199 } else {
200 ret = parse_qapi_name(s, false);
201 len = ret < 0 ? 0 : ret;
202 }
203 assert(s + len <= key_end);
204 if (!len || (s + len < key_end && s[len] != '.')) {
205 assert(key != implied_key);
206 error_setg(errp, "Invalid parameter '%.*s'",
207 (int)(key_end - key), key);
208 return NULL;
209 }
210 if (len >= sizeof(key_in_cur)) {
211 assert(key != implied_key);
212 error_setg(errp, "Parameter%s '%.*s' is too long",
213 s != key || s + len != key_end ? " fragment" : "",
214 (int)len, s);
215 return NULL;
216 }
217
218 if (s != key) {
219 next = keyval_parse_put(cur, key_in_cur, NULL,
220 key, s - 1, errp);
221 if (!next) {
222 return NULL;
223 }
224 cur = qobject_to(QDict, next);
225 assert(cur);
226 }
227
228 memcpy(key_in_cur, s, len);
229 key_in_cur[len] = 0;
230 s += len;
231
232 if (*s != '.') {
233 break;
234 }
235 s++;
236 }
237
238 if (key == implied_key) {
239 assert(!*s);
240 s = params;
241 } else {
242 if (*s != '=') {
243 error_setg(errp, "Expected '=' after parameter '%.*s'",
244 (int)(s - key), key);
245 return NULL;
246 }
247 s++;
248 }
249
250 val = qstring_new();
251 for (;;) {
252 if (!*s) {
253 break;
254 } else if (*s == ',') {
255 s++;
256 if (*s != ',') {
257 break;
258 }
259 }
260 qstring_append_chr(val, *s++);
261 }
262
263 if (!keyval_parse_put(cur, key_in_cur, val, key, key_end, errp)) {
264 return NULL;
265 }
266 return s;
267}
268
269static char *reassemble_key(GSList *key)
270{
271 GString *s = g_string_new("");
272 GSList *p;
273
274 for (p = key; p; p = p->next) {
275 g_string_prepend_c(s, '.');
276 g_string_prepend(s, (char *)p->data);
277 }
278
279 return g_string_free(s, FALSE);
280}
281
282
283
284
285
286
287
288
289static QObject *keyval_listify(QDict *cur, GSList *key_of_cur, Error **errp)
290{
291 GSList key_node;
292 bool has_index, has_member;
293 const QDictEntry *ent;
294 QDict *qdict;
295 QObject *val;
296 char *key;
297 size_t nelt;
298 QObject **elt;
299 int index, max_index, i;
300 QList *list;
301
302 key_node.next = key_of_cur;
303
304
305
306
307
308 has_index = false;
309 has_member = false;
310 for (ent = qdict_first(cur); ent; ent = qdict_next(cur, ent)) {
311 if (key_to_index(ent->key, NULL) >= 0) {
312 has_index = true;
313 } else {
314 has_member = true;
315 }
316
317 qdict = qobject_to(QDict, ent->value);
318 if (!qdict) {
319 continue;
320 }
321
322 key_node.data = ent->key;
323 val = keyval_listify(qdict, &key_node, errp);
324 if (!val) {
325 return NULL;
326 }
327 if (val != ent->value) {
328 qdict_put_obj(cur, ent->key, val);
329 }
330 }
331
332 if (has_index && has_member) {
333 key = reassemble_key(key_of_cur);
334 error_setg(errp, "Parameters '%s*' used inconsistently", key);
335 g_free(key);
336 return NULL;
337 }
338 if (!has_index) {
339 return QOBJECT(cur);
340 }
341
342
343 nelt = qdict_size(cur) + 1;
344 elt = g_new0(QObject *, nelt);
345 max_index = -1;
346 for (ent = qdict_first(cur); ent; ent = qdict_next(cur, ent)) {
347 index = key_to_index(ent->key, NULL);
348 assert(index >= 0);
349 if (index > max_index) {
350 max_index = index;
351 }
352
353
354
355
356
357 if ((size_t)index >= nelt - 1) {
358 continue;
359 }
360
361 elt[index] = ent->value;
362 }
363
364
365
366
367
368
369
370 list = qlist_new();
371 assert(!elt[nelt-1]);
372 for (i = 0; i < MIN(nelt, max_index + 1); i++) {
373 if (!elt[i]) {
374 key = reassemble_key(key_of_cur);
375 error_setg(errp, "Parameter '%s%d' missing", key, i);
376 g_free(key);
377 g_free(elt);
378 qobject_unref(list);
379 return NULL;
380 }
381 qobject_ref(elt[i]);
382 qlist_append_obj(list, elt[i]);
383 }
384
385 g_free(elt);
386 return QOBJECT(list);
387}
388
389
390
391
392
393
394
395
396QDict *keyval_parse(const char *params, const char *implied_key,
397 Error **errp)
398{
399 QDict *qdict = qdict_new();
400 QObject *listified;
401 const char *s;
402
403 s = params;
404 while (*s) {
405 s = keyval_parse_one(qdict, s, implied_key, errp);
406 if (!s) {
407 qobject_unref(qdict);
408 return NULL;
409 }
410 implied_key = NULL;
411 }
412
413 listified = keyval_listify(qdict, NULL, errp);
414 if (!listified) {
415 qobject_unref(qdict);
416 return NULL;
417 }
418 assert(listified == QOBJECT(qdict));
419 return qdict;
420}
421