1
2
3
4
5
6
7
8
9
10#include "qemu/osdep.h"
11#include "block/qdict.h"
12#include "qapi/qmp/qbool.h"
13#include "qapi/qmp/qlist.h"
14#include "qapi/qmp/qnum.h"
15#include "qapi/qmp/qstring.h"
16#include "qapi/qobject-input-visitor.h"
17#include "qemu/cutils.h"
18#include "qapi/error.h"
19
20
21
22
23
24
25void qdict_copy_default(QDict *dst, QDict *src, const char *key)
26{
27 QObject *val;
28
29 if (qdict_haskey(dst, key)) {
30 return;
31 }
32
33 val = qdict_get(src, key);
34 if (val) {
35 qdict_put_obj(dst, key, qobject_ref(val));
36 }
37}
38
39
40
41
42
43void qdict_set_default_str(QDict *dst, const char *key, const char *val)
44{
45 if (qdict_haskey(dst, key)) {
46 return;
47 }
48
49 qdict_put_str(dst, key, val);
50}
51
52static void qdict_flatten_qdict(QDict *qdict, QDict *target,
53 const char *prefix);
54
55static void qdict_flatten_qlist(QList *qlist, QDict *target, const char *prefix)
56{
57 QObject *value;
58 const QListEntry *entry;
59 QDict *dict_val;
60 QList *list_val;
61 char *new_key;
62 int i;
63
64
65
66
67
68 assert(prefix);
69
70 entry = qlist_first(qlist);
71
72 for (i = 0; entry; entry = qlist_next(entry), i++) {
73 value = qlist_entry_obj(entry);
74 dict_val = qobject_to(QDict, value);
75 list_val = qobject_to(QList, value);
76 new_key = g_strdup_printf("%s.%i", prefix, i);
77
78
79
80
81
82 if (dict_val && qdict_size(dict_val)) {
83 qdict_flatten_qdict(dict_val, target, new_key);
84 } else if (list_val && !qlist_empty(list_val)) {
85 qdict_flatten_qlist(list_val, target, new_key);
86 } else {
87 qdict_put_obj(target, new_key, qobject_ref(value));
88 }
89
90 g_free(new_key);
91 }
92}
93
94static void qdict_flatten_qdict(QDict *qdict, QDict *target, const char *prefix)
95{
96 QObject *value;
97 const QDictEntry *entry, *next;
98 QDict *dict_val;
99 QList *list_val;
100 char *key, *new_key;
101
102 entry = qdict_first(qdict);
103
104 while (entry != NULL) {
105 next = qdict_next(qdict, entry);
106 value = qdict_entry_value(entry);
107 dict_val = qobject_to(QDict, value);
108 list_val = qobject_to(QList, value);
109
110 if (prefix) {
111 key = new_key = g_strdup_printf("%s.%s", prefix, entry->key);
112 } else {
113 key = entry->key;
114 new_key = NULL;
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128
129 if (dict_val && qdict_size(dict_val)) {
130 qdict_flatten_qdict(dict_val, target, key);
131 if (target == qdict) {
132 qdict_del(qdict, entry->key);
133 }
134 } else if (list_val && !qlist_empty(list_val)) {
135 qdict_flatten_qlist(list_val, target, key);
136 if (target == qdict) {
137 qdict_del(qdict, entry->key);
138 }
139 } else if (target != qdict) {
140 qdict_put_obj(target, key, qobject_ref(value));
141 }
142
143 g_free(new_key);
144 entry = next;
145 }
146}
147
148
149
150
151
152
153
154
155
156void qdict_flatten(QDict *qdict)
157{
158 qdict_flatten_qdict(qdict, qdict, NULL);
159}
160
161
162
163void qdict_extract_subqdict(QDict *src, QDict **dst, const char *start)
164
165{
166 const QDictEntry *entry, *next;
167 const char *p;
168
169 if (dst) {
170 *dst = qdict_new();
171 }
172 entry = qdict_first(src);
173
174 while (entry != NULL) {
175 next = qdict_next(src, entry);
176 if (strstart(entry->key, start, &p)) {
177 if (dst) {
178 qdict_put_obj(*dst, p, qobject_ref(entry->value));
179 }
180 qdict_del(src, entry->key);
181 }
182 entry = next;
183 }
184}
185
186static int qdict_count_prefixed_entries(const QDict *src, const char *start)
187{
188 const QDictEntry *entry;
189 int count = 0;
190
191 for (entry = qdict_first(src); entry; entry = qdict_next(src, entry)) {
192 if (strstart(entry->key, start, NULL)) {
193 if (count == INT_MAX) {
194 return -ERANGE;
195 }
196 count++;
197 }
198 }
199
200 return count;
201}
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218void qdict_array_split(QDict *src, QList **dst)
219{
220 unsigned i;
221
222 *dst = qlist_new();
223
224 for (i = 0; i < UINT_MAX; i++) {
225 QObject *subqobj;
226 bool is_subqdict;
227 QDict *subqdict;
228 char indexstr[32], prefix[32];
229 size_t snprintf_ret;
230
231 snprintf_ret = snprintf(indexstr, 32, "%u", i);
232 assert(snprintf_ret < 32);
233
234 subqobj = qdict_get(src, indexstr);
235
236 snprintf_ret = snprintf(prefix, 32, "%u.", i);
237 assert(snprintf_ret < 32);
238
239
240 is_subqdict = qdict_count_prefixed_entries(src, prefix);
241
242
243
244
245
246
247 if (!subqobj == !is_subqdict) {
248 break;
249 }
250
251 if (is_subqdict) {
252 qdict_extract_subqdict(src, &subqdict, prefix);
253 assert(qdict_size(subqdict) > 0);
254 qlist_append_obj(*dst, QOBJECT(subqdict));
255 } else {
256 qobject_ref(subqobj);
257 qdict_del(src, indexstr);
258 qlist_append_obj(*dst, subqobj);
259 }
260 }
261}
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285static void qdict_split_flat_key(const char *key, char **prefix,
286 const char **suffix)
287{
288 const char *separator;
289 size_t i, j;
290
291
292
293 separator = NULL;
294 do {
295 if (separator) {
296 separator += 2;
297 } else {
298 separator = key;
299 }
300 separator = strchr(separator, '.');
301 } while (separator && separator[1] == '.');
302
303 if (separator) {
304 *prefix = g_strndup(key, separator - key);
305 *suffix = separator + 1;
306 } else {
307 *prefix = g_strdup(key);
308 *suffix = NULL;
309 }
310
311
312 for (i = 0, j = 0; (*prefix)[i] != '\0'; i++, j++) {
313 if ((*prefix)[i] == '.') {
314 assert((*prefix)[i + 1] == '.');
315 i++;
316 }
317 (*prefix)[j] = (*prefix)[i];
318 }
319 (*prefix)[j] = '\0';
320}
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335static int qdict_is_list(QDict *maybe_list, Error **errp)
336{
337 const QDictEntry *ent;
338 ssize_t len = 0;
339 ssize_t max = -1;
340 int is_list = -1;
341 int64_t val;
342
343 for (ent = qdict_first(maybe_list); ent != NULL;
344 ent = qdict_next(maybe_list, ent)) {
345 int is_index = !qemu_strtoi64(ent->key, NULL, 10, &val);
346
347 if (is_list == -1) {
348 is_list = is_index;
349 }
350
351 if (is_index != is_list) {
352 error_setg(errp, "Cannot mix list and non-list keys");
353 return -1;
354 }
355
356 if (is_index) {
357 len++;
358 if (val > max) {
359 max = val;
360 }
361 }
362 }
363
364 if (is_list == -1) {
365 assert(!qdict_size(maybe_list));
366 is_list = 0;
367 }
368
369
370
371
372
373
374 if (len != (max + 1)) {
375 error_setg(errp, "List indices are not contiguous, "
376 "saw %zd elements but %zd largest index",
377 len, max);
378 return -1;
379 }
380
381 return is_list;
382}
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423QObject *qdict_crumple(const QDict *src, Error **errp)
424{
425 const QDictEntry *ent;
426 QDict *two_level, *multi_level = NULL, *child_dict;
427 QDict *dict_val;
428 QList *list_val;
429 QObject *dst = NULL, *child;
430 size_t i;
431 char *prefix = NULL;
432 const char *suffix = NULL;
433 int is_list;
434
435 two_level = qdict_new();
436
437
438 for (ent = qdict_first(src); ent != NULL; ent = qdict_next(src, ent)) {
439 dict_val = qobject_to(QDict, ent->value);
440 list_val = qobject_to(QList, ent->value);
441 if ((dict_val && qdict_size(dict_val))
442 || (list_val && !qlist_empty(list_val))) {
443 error_setg(errp, "Value %s is not flat", ent->key);
444 goto error;
445 }
446
447 qdict_split_flat_key(ent->key, &prefix, &suffix);
448 child = qdict_get(two_level, prefix);
449 child_dict = qobject_to(QDict, child);
450
451 if (child) {
452
453
454
455
456
457 if (!child_dict || !suffix) {
458 error_setg(errp, "Cannot mix scalar and non-scalar keys");
459 goto error;
460 }
461 }
462
463 if (suffix) {
464 if (!child_dict) {
465 child_dict = qdict_new();
466 qdict_put(two_level, prefix, child_dict);
467 }
468 qdict_put_obj(child_dict, suffix, qobject_ref(ent->value));
469 } else {
470 qdict_put_obj(two_level, prefix, qobject_ref(ent->value));
471 }
472
473 g_free(prefix);
474 prefix = NULL;
475 }
476
477
478
479 multi_level = qdict_new();
480 for (ent = qdict_first(two_level); ent != NULL;
481 ent = qdict_next(two_level, ent)) {
482 dict_val = qobject_to(QDict, ent->value);
483 if (dict_val && qdict_size(dict_val)) {
484 child = qdict_crumple(dict_val, errp);
485 if (!child) {
486 goto error;
487 }
488
489 qdict_put_obj(multi_level, ent->key, child);
490 } else {
491 qdict_put_obj(multi_level, ent->key, qobject_ref(ent->value));
492 }
493 }
494 qobject_unref(two_level);
495 two_level = NULL;
496
497
498 is_list = qdict_is_list(multi_level, errp);
499 if (is_list < 0) {
500 goto error;
501 }
502
503 if (is_list) {
504 dst = QOBJECT(qlist_new());
505
506 for (i = 0; i < qdict_size(multi_level); i++) {
507 char *key = g_strdup_printf("%zu", i);
508
509 child = qdict_get(multi_level, key);
510 g_free(key);
511
512 if (!child) {
513 error_setg(errp, "Missing list index %zu", i);
514 goto error;
515 }
516
517 qlist_append_obj(qobject_to(QList, dst), qobject_ref(child));
518 }
519 qobject_unref(multi_level);
520 multi_level = NULL;
521 } else {
522 dst = QOBJECT(multi_level);
523 }
524
525 return dst;
526
527 error:
528 g_free(prefix);
529 qobject_unref(multi_level);
530 qobject_unref(two_level);
531 qobject_unref(dst);
532 return NULL;
533}
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548static QObject *qdict_crumple_for_keyval_qiv(QDict *src, Error **errp)
549{
550 QDict *tmp = NULL;
551 char *buf;
552 const char *s;
553 const QDictEntry *ent;
554 QObject *dst;
555
556 for (ent = qdict_first(src); ent; ent = qdict_next(src, ent)) {
557 buf = NULL;
558 switch (qobject_type(ent->value)) {
559 case QTYPE_QNULL:
560 case QTYPE_QSTRING:
561 continue;
562 case QTYPE_QNUM:
563 s = buf = qnum_to_string(qobject_to(QNum, ent->value));
564 break;
565 case QTYPE_QDICT:
566 case QTYPE_QLIST:
567
568 continue;
569 case QTYPE_QBOOL:
570 s = qbool_get_bool(qobject_to(QBool, ent->value))
571 ? "on" : "off";
572 break;
573 default:
574 abort();
575 }
576
577 if (!tmp) {
578 tmp = qdict_clone_shallow(src);
579 }
580 qdict_put_str(tmp, ent->key, s);
581 g_free(buf);
582 }
583
584 dst = qdict_crumple(tmp ?: src, errp);
585 qobject_unref(tmp);
586 return dst;
587}
588
589
590
591
592
593
594
595
596int qdict_array_entries(QDict *src, const char *subqdict)
597{
598 const QDictEntry *entry;
599 unsigned i;
600 unsigned entries = 0;
601 size_t subqdict_len = strlen(subqdict);
602
603 assert(!subqdict_len || subqdict[subqdict_len - 1] == '.');
604
605
606
607
608 for (i = 0; i < INT_MAX; i++) {
609 QObject *subqobj;
610 int subqdict_entries;
611 char *prefix = g_strdup_printf("%s%u.", subqdict, i);
612
613 subqdict_entries = qdict_count_prefixed_entries(src, prefix);
614
615
616 prefix[strlen(prefix) - 1] = 0;
617 subqobj = qdict_get(src, prefix);
618
619 g_free(prefix);
620
621 if (subqdict_entries < 0) {
622 return subqdict_entries;
623 }
624
625
626
627 if (subqobj && subqdict_entries) {
628 return -EINVAL;
629 } else if (!subqobj && !subqdict_entries) {
630 break;
631 }
632
633 entries += subqdict_entries ? subqdict_entries : 1;
634 }
635
636
637 for (entry = qdict_first(src); entry; entry = qdict_next(src, entry)) {
638 if (!strstart(qdict_entry_key(entry), subqdict, NULL)) {
639 entries++;
640 }
641 }
642
643
644 if (qdict_size(src) != entries) {
645 return -EINVAL;
646 }
647
648 return i;
649}
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665void qdict_join(QDict *dest, QDict *src, bool overwrite)
666{
667 const QDictEntry *entry, *next;
668
669 entry = qdict_first(src);
670 while (entry) {
671 next = qdict_next(src, entry);
672
673 if (overwrite || !qdict_haskey(dest, entry->key)) {
674 qdict_put_obj(dest, entry->key, qobject_ref(entry->value));
675 qdict_del(src, entry->key);
676 }
677
678 entry = next;
679 }
680}
681
682
683
684
685
686
687
688
689
690
691
692
693bool qdict_rename_keys(QDict *qdict, const QDictRenames *renames, Error **errp)
694{
695 QObject *qobj;
696
697 while (renames->from) {
698 if (qdict_haskey(qdict, renames->from)) {
699 if (qdict_haskey(qdict, renames->to)) {
700 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
701 "same time", renames->to, renames->from);
702 return false;
703 }
704
705 qobj = qdict_get(qdict, renames->from);
706 qdict_put_obj(qdict, renames->to, qobject_ref(qobj));
707 qdict_del(qdict, renames->from);
708 }
709
710 renames++;
711 }
712 return true;
713}
714
715
716
717
718
719
720
721
722
723
724Visitor *qobject_input_visitor_new_flat_confused(QDict *qdict,
725 Error **errp)
726{
727 QObject *crumpled;
728 Visitor *v;
729
730 crumpled = qdict_crumple_for_keyval_qiv(qdict, errp);
731 if (!crumpled) {
732 return NULL;
733 }
734
735 v = qobject_input_visitor_new_keyval(crumpled);
736 qobject_unref(crumpled);
737 return v;
738}
739