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