xref: /openbmc/qemu/qobject/qdict.c (revision 4a09d0bb)
1 /*
2  * QDict Module
3  *
4  * Copyright (C) 2009 Red Hat Inc.
5  *
6  * Authors:
7  *  Luiz Capitulino <lcapitulino@redhat.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 #include "qemu/osdep.h"
14 #include "qapi/qmp/qint.h"
15 #include "qapi/qmp/qfloat.h"
16 #include "qapi/qmp/qdict.h"
17 #include "qapi/qmp/qbool.h"
18 #include "qapi/qmp/qstring.h"
19 #include "qapi/qmp/qobject.h"
20 #include "qapi/error.h"
21 #include "qemu/queue.h"
22 #include "qemu-common.h"
23 #include "qemu/cutils.h"
24 
25 /**
26  * qdict_new(): Create a new QDict
27  *
28  * Return strong reference.
29  */
30 QDict *qdict_new(void)
31 {
32     QDict *qdict;
33 
34     qdict = g_malloc0(sizeof(*qdict));
35     qobject_init(QOBJECT(qdict), QTYPE_QDICT);
36 
37     return qdict;
38 }
39 
40 /**
41  * qobject_to_qdict(): Convert a QObject into a QDict
42  */
43 QDict *qobject_to_qdict(const QObject *obj)
44 {
45     if (!obj || qobject_type(obj) != QTYPE_QDICT) {
46         return NULL;
47     }
48     return container_of(obj, QDict, base);
49 }
50 
51 /**
52  * tdb_hash(): based on the hash agorithm from gdbm, via tdb
53  * (from module-init-tools)
54  */
55 static unsigned int tdb_hash(const char *name)
56 {
57     unsigned value;	/* Used to compute the hash value.  */
58     unsigned   i;	/* Used to cycle through random values. */
59 
60     /* Set the initial value from the key size. */
61     for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
62         value = (value + (((const unsigned char *)name)[i] << (i*5 % 24)));
63 
64     return (1103515243 * value + 12345);
65 }
66 
67 /**
68  * alloc_entry(): allocate a new QDictEntry
69  */
70 static QDictEntry *alloc_entry(const char *key, QObject *value)
71 {
72     QDictEntry *entry;
73 
74     entry = g_malloc0(sizeof(*entry));
75     entry->key = g_strdup(key);
76     entry->value = value;
77 
78     return entry;
79 }
80 
81 /**
82  * qdict_entry_value(): Return qdict entry value
83  *
84  * Return weak reference.
85  */
86 QObject *qdict_entry_value(const QDictEntry *entry)
87 {
88     return entry->value;
89 }
90 
91 /**
92  * qdict_entry_key(): Return qdict entry key
93  *
94  * Return a *pointer* to the string, it has to be duplicated before being
95  * stored.
96  */
97 const char *qdict_entry_key(const QDictEntry *entry)
98 {
99     return entry->key;
100 }
101 
102 /**
103  * qdict_find(): List lookup function
104  */
105 static QDictEntry *qdict_find(const QDict *qdict,
106                               const char *key, unsigned int bucket)
107 {
108     QDictEntry *entry;
109 
110     QLIST_FOREACH(entry, &qdict->table[bucket], next)
111         if (!strcmp(entry->key, key))
112             return entry;
113 
114     return NULL;
115 }
116 
117 /**
118  * qdict_put_obj(): Put a new QObject into the dictionary
119  *
120  * Insert the pair 'key:value' into 'qdict', if 'key' already exists
121  * its 'value' will be replaced.
122  *
123  * This is done by freeing the reference to the stored QObject and
124  * storing the new one in the same entry.
125  *
126  * NOTE: ownership of 'value' is transferred to the QDict
127  */
128 void qdict_put_obj(QDict *qdict, const char *key, QObject *value)
129 {
130     unsigned int bucket;
131     QDictEntry *entry;
132 
133     bucket = tdb_hash(key) % QDICT_BUCKET_MAX;
134     entry = qdict_find(qdict, key, bucket);
135     if (entry) {
136         /* replace key's value */
137         qobject_decref(entry->value);
138         entry->value = value;
139     } else {
140         /* allocate a new entry */
141         entry = alloc_entry(key, value);
142         QLIST_INSERT_HEAD(&qdict->table[bucket], entry, next);
143         qdict->size++;
144     }
145 }
146 
147 /**
148  * qdict_get(): Lookup for a given 'key'
149  *
150  * Return a weak reference to the QObject associated with 'key' if
151  * 'key' is present in the dictionary, NULL otherwise.
152  */
153 QObject *qdict_get(const QDict *qdict, const char *key)
154 {
155     QDictEntry *entry;
156 
157     entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_BUCKET_MAX);
158     return (entry == NULL ? NULL : entry->value);
159 }
160 
161 /**
162  * qdict_haskey(): Check if 'key' exists
163  *
164  * Return 1 if 'key' exists in the dict, 0 otherwise
165  */
166 int qdict_haskey(const QDict *qdict, const char *key)
167 {
168     unsigned int bucket = tdb_hash(key) % QDICT_BUCKET_MAX;
169     return (qdict_find(qdict, key, bucket) == NULL ? 0 : 1);
170 }
171 
172 /**
173  * qdict_size(): Return the size of the dictionary
174  */
175 size_t qdict_size(const QDict *qdict)
176 {
177     return qdict->size;
178 }
179 
180 /**
181  * qdict_get_obj(): Get a QObject of a specific type
182  */
183 static QObject *qdict_get_obj(const QDict *qdict, const char *key, QType type)
184 {
185     QObject *obj;
186 
187     obj = qdict_get(qdict, key);
188     assert(obj != NULL);
189     assert(qobject_type(obj) == type);
190 
191     return obj;
192 }
193 
194 /**
195  * qdict_get_double(): Get an number mapped by 'key'
196  *
197  * This function assumes that 'key' exists and it stores a
198  * QFloat or QInt object.
199  *
200  * Return number mapped by 'key'.
201  */
202 double qdict_get_double(const QDict *qdict, const char *key)
203 {
204     QObject *obj = qdict_get(qdict, key);
205 
206     assert(obj);
207     switch (qobject_type(obj)) {
208     case QTYPE_QFLOAT:
209         return qfloat_get_double(qobject_to_qfloat(obj));
210     case QTYPE_QINT:
211         return qint_get_int(qobject_to_qint(obj));
212     default:
213         abort();
214     }
215 }
216 
217 /**
218  * qdict_get_int(): Get an integer mapped by 'key'
219  *
220  * This function assumes that 'key' exists and it stores a
221  * QInt object.
222  *
223  * Return integer mapped by 'key'.
224  */
225 int64_t qdict_get_int(const QDict *qdict, const char *key)
226 {
227     return qint_get_int(qobject_to_qint(qdict_get(qdict, key)));
228 }
229 
230 /**
231  * qdict_get_bool(): Get a bool mapped by 'key'
232  *
233  * This function assumes that 'key' exists and it stores a
234  * QBool object.
235  *
236  * Return bool mapped by 'key'.
237  */
238 bool qdict_get_bool(const QDict *qdict, const char *key)
239 {
240     return qbool_get_bool(qobject_to_qbool(qdict_get(qdict, key)));
241 }
242 
243 /**
244  * qdict_get_qlist(): Get the QList mapped by 'key'
245  *
246  * This function assumes that 'key' exists and it stores a
247  * QList object.
248  *
249  * Return QList mapped by 'key'.
250  */
251 QList *qdict_get_qlist(const QDict *qdict, const char *key)
252 {
253     return qobject_to_qlist(qdict_get_obj(qdict, key, QTYPE_QLIST));
254 }
255 
256 /**
257  * qdict_get_qdict(): Get the QDict mapped by 'key'
258  *
259  * This function assumes that 'key' exists and it stores a
260  * QDict object.
261  *
262  * Return QDict mapped by 'key'.
263  */
264 QDict *qdict_get_qdict(const QDict *qdict, const char *key)
265 {
266     return qobject_to_qdict(qdict_get(qdict, key));
267 }
268 
269 /**
270  * qdict_get_str(): Get a pointer to the stored string mapped
271  * by 'key'
272  *
273  * This function assumes that 'key' exists and it stores a
274  * QString object.
275  *
276  * Return pointer to the string mapped by 'key'.
277  */
278 const char *qdict_get_str(const QDict *qdict, const char *key)
279 {
280     return qstring_get_str(qobject_to_qstring(qdict_get(qdict, key)));
281 }
282 
283 /**
284  * qdict_get_try_int(): Try to get integer mapped by 'key'
285  *
286  * Return integer mapped by 'key', if it is not present in
287  * the dictionary or if the stored object is not of QInt type
288  * 'def_value' will be returned.
289  */
290 int64_t qdict_get_try_int(const QDict *qdict, const char *key,
291                           int64_t def_value)
292 {
293     QInt *qint = qobject_to_qint(qdict_get(qdict, key));
294 
295     return qint ? qint_get_int(qint) : def_value;
296 }
297 
298 /**
299  * qdict_get_try_bool(): Try to get a bool mapped by 'key'
300  *
301  * Return bool mapped by 'key', if it is not present in the
302  * dictionary or if the stored object is not of QBool type
303  * 'def_value' will be returned.
304  */
305 bool qdict_get_try_bool(const QDict *qdict, const char *key, bool def_value)
306 {
307     QBool *qbool = qobject_to_qbool(qdict_get(qdict, key));
308 
309     return qbool ? qbool_get_bool(qbool) : def_value;
310 }
311 
312 /**
313  * qdict_get_try_str(): Try to get a pointer to the stored string
314  * mapped by 'key'
315  *
316  * Return a pointer to the string mapped by 'key', if it is not present
317  * in the dictionary or if the stored object is not of QString type
318  * NULL will be returned.
319  */
320 const char *qdict_get_try_str(const QDict *qdict, const char *key)
321 {
322     QString *qstr = qobject_to_qstring(qdict_get(qdict, key));
323 
324     return qstr ? qstring_get_str(qstr) : NULL;
325 }
326 
327 /**
328  * qdict_iter(): Iterate over all the dictionary's stored values.
329  *
330  * This function allows the user to provide an iterator, which will be
331  * called for each stored value in the dictionary.
332  */
333 void qdict_iter(const QDict *qdict,
334                 void (*iter)(const char *key, QObject *obj, void *opaque),
335                 void *opaque)
336 {
337     int i;
338     QDictEntry *entry;
339 
340     for (i = 0; i < QDICT_BUCKET_MAX; i++) {
341         QLIST_FOREACH(entry, &qdict->table[i], next)
342             iter(entry->key, entry->value, opaque);
343     }
344 }
345 
346 static QDictEntry *qdict_next_entry(const QDict *qdict, int first_bucket)
347 {
348     int i;
349 
350     for (i = first_bucket; i < QDICT_BUCKET_MAX; i++) {
351         if (!QLIST_EMPTY(&qdict->table[i])) {
352             return QLIST_FIRST(&qdict->table[i]);
353         }
354     }
355 
356     return NULL;
357 }
358 
359 /**
360  * qdict_first(): Return first qdict entry for iteration.
361  */
362 const QDictEntry *qdict_first(const QDict *qdict)
363 {
364     return qdict_next_entry(qdict, 0);
365 }
366 
367 /**
368  * qdict_next(): Return next qdict entry in an iteration.
369  */
370 const QDictEntry *qdict_next(const QDict *qdict, const QDictEntry *entry)
371 {
372     QDictEntry *ret;
373 
374     ret = QLIST_NEXT(entry, next);
375     if (!ret) {
376         unsigned int bucket = tdb_hash(entry->key) % QDICT_BUCKET_MAX;
377         ret = qdict_next_entry(qdict, bucket + 1);
378     }
379 
380     return ret;
381 }
382 
383 /**
384  * qdict_clone_shallow(): Clones a given QDict. Its entries are not copied, but
385  * another reference is added.
386  */
387 QDict *qdict_clone_shallow(const QDict *src)
388 {
389     QDict *dest;
390     QDictEntry *entry;
391     int i;
392 
393     dest = qdict_new();
394 
395     for (i = 0; i < QDICT_BUCKET_MAX; i++) {
396         QLIST_FOREACH(entry, &src->table[i], next) {
397             qobject_incref(entry->value);
398             qdict_put_obj(dest, entry->key, entry->value);
399         }
400     }
401 
402     return dest;
403 }
404 
405 /**
406  * qentry_destroy(): Free all the memory allocated by a QDictEntry
407  */
408 static void qentry_destroy(QDictEntry *e)
409 {
410     assert(e != NULL);
411     assert(e->key != NULL);
412     assert(e->value != NULL);
413 
414     qobject_decref(e->value);
415     g_free(e->key);
416     g_free(e);
417 }
418 
419 /**
420  * qdict_del(): Delete a 'key:value' pair from the dictionary
421  *
422  * This will destroy all data allocated by this entry.
423  */
424 void qdict_del(QDict *qdict, const char *key)
425 {
426     QDictEntry *entry;
427 
428     entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_BUCKET_MAX);
429     if (entry) {
430         QLIST_REMOVE(entry, next);
431         qentry_destroy(entry);
432         qdict->size--;
433     }
434 }
435 
436 /**
437  * qdict_destroy_obj(): Free all the memory allocated by a QDict
438  */
439 void qdict_destroy_obj(QObject *obj)
440 {
441     int i;
442     QDict *qdict;
443 
444     assert(obj != NULL);
445     qdict = qobject_to_qdict(obj);
446 
447     for (i = 0; i < QDICT_BUCKET_MAX; i++) {
448         QDictEntry *entry = QLIST_FIRST(&qdict->table[i]);
449         while (entry) {
450             QDictEntry *tmp = QLIST_NEXT(entry, next);
451             QLIST_REMOVE(entry, next);
452             qentry_destroy(entry);
453             entry = tmp;
454         }
455     }
456 
457     g_free(qdict);
458 }
459 
460 /**
461  * qdict_copy_default(): If no entry mapped by 'key' exists in 'dst' yet, the
462  * value of 'key' in 'src' is copied there (and the refcount increased
463  * accordingly).
464  */
465 void qdict_copy_default(QDict *dst, QDict *src, const char *key)
466 {
467     QObject *val;
468 
469     if (qdict_haskey(dst, key)) {
470         return;
471     }
472 
473     val = qdict_get(src, key);
474     if (val) {
475         qobject_incref(val);
476         qdict_put_obj(dst, key, val);
477     }
478 }
479 
480 /**
481  * qdict_set_default_str(): If no entry mapped by 'key' exists in 'dst' yet, a
482  * new QString initialised by 'val' is put there.
483  */
484 void qdict_set_default_str(QDict *dst, const char *key, const char *val)
485 {
486     if (qdict_haskey(dst, key)) {
487         return;
488     }
489 
490     qdict_put(dst, key, qstring_from_str(val));
491 }
492 
493 static void qdict_flatten_qdict(QDict *qdict, QDict *target,
494                                 const char *prefix);
495 
496 static void qdict_flatten_qlist(QList *qlist, QDict *target, const char *prefix)
497 {
498     QObject *value;
499     const QListEntry *entry;
500     char *new_key;
501     int i;
502 
503     /* This function is never called with prefix == NULL, i.e., it is always
504      * called from within qdict_flatten_q(list|dict)(). Therefore, it does not
505      * need to remove list entries during the iteration (the whole list will be
506      * deleted eventually anyway from qdict_flatten_qdict()). */
507     assert(prefix);
508 
509     entry = qlist_first(qlist);
510 
511     for (i = 0; entry; entry = qlist_next(entry), i++) {
512         value = qlist_entry_obj(entry);
513         new_key = g_strdup_printf("%s.%i", prefix, i);
514 
515         if (qobject_type(value) == QTYPE_QDICT) {
516             qdict_flatten_qdict(qobject_to_qdict(value), target, new_key);
517         } else if (qobject_type(value) == QTYPE_QLIST) {
518             qdict_flatten_qlist(qobject_to_qlist(value), target, new_key);
519         } else {
520             /* All other types are moved to the target unchanged. */
521             qobject_incref(value);
522             qdict_put_obj(target, new_key, value);
523         }
524 
525         g_free(new_key);
526     }
527 }
528 
529 static void qdict_flatten_qdict(QDict *qdict, QDict *target, const char *prefix)
530 {
531     QObject *value;
532     const QDictEntry *entry, *next;
533     char *new_key;
534     bool delete;
535 
536     entry = qdict_first(qdict);
537 
538     while (entry != NULL) {
539 
540         next = qdict_next(qdict, entry);
541         value = qdict_entry_value(entry);
542         new_key = NULL;
543         delete = false;
544 
545         if (prefix) {
546             new_key = g_strdup_printf("%s.%s", prefix, entry->key);
547         }
548 
549         if (qobject_type(value) == QTYPE_QDICT) {
550             /* Entries of QDicts are processed recursively, the QDict object
551              * itself disappears. */
552             qdict_flatten_qdict(qobject_to_qdict(value), target,
553                                 new_key ? new_key : entry->key);
554             delete = true;
555         } else if (qobject_type(value) == QTYPE_QLIST) {
556             qdict_flatten_qlist(qobject_to_qlist(value), target,
557                                 new_key ? new_key : entry->key);
558             delete = true;
559         } else if (prefix) {
560             /* All other objects are moved to the target unchanged. */
561             qobject_incref(value);
562             qdict_put_obj(target, new_key, value);
563             delete = true;
564         }
565 
566         g_free(new_key);
567 
568         if (delete) {
569             qdict_del(qdict, entry->key);
570 
571             /* Restart loop after modifying the iterated QDict */
572             entry = qdict_first(qdict);
573             continue;
574         }
575 
576         entry = next;
577     }
578 }
579 
580 /**
581  * qdict_flatten(): For each nested QDict with key x, all fields with key y
582  * are moved to this QDict and their key is renamed to "x.y". For each nested
583  * QList with key x, the field at index y is moved to this QDict with the key
584  * "x.y" (i.e., the reverse of what qdict_array_split() does).
585  * This operation is applied recursively for nested QDicts and QLists.
586  */
587 void qdict_flatten(QDict *qdict)
588 {
589     qdict_flatten_qdict(qdict, qdict, NULL);
590 }
591 
592 /* extract all the src QDict entries starting by start into dst */
593 void qdict_extract_subqdict(QDict *src, QDict **dst, const char *start)
594 
595 {
596     const QDictEntry *entry, *next;
597     const char *p;
598 
599     *dst = qdict_new();
600     entry = qdict_first(src);
601 
602     while (entry != NULL) {
603         next = qdict_next(src, entry);
604         if (strstart(entry->key, start, &p)) {
605             qobject_incref(entry->value);
606             qdict_put_obj(*dst, p, entry->value);
607             qdict_del(src, entry->key);
608         }
609         entry = next;
610     }
611 }
612 
613 static int qdict_count_prefixed_entries(const QDict *src, const char *start)
614 {
615     const QDictEntry *entry;
616     int count = 0;
617 
618     for (entry = qdict_first(src); entry; entry = qdict_next(src, entry)) {
619         if (strstart(entry->key, start, NULL)) {
620             if (count == INT_MAX) {
621                 return -ERANGE;
622             }
623             count++;
624         }
625     }
626 
627     return count;
628 }
629 
630 /**
631  * qdict_array_split(): This function moves array-like elements of a QDict into
632  * a new QList. Every entry in the original QDict with a key "%u" or one
633  * prefixed "%u.", where %u designates an unsigned integer starting at 0 and
634  * incrementally counting up, will be moved to a new QDict at index %u in the
635  * output QList with the key prefix removed, if that prefix is "%u.". If the
636  * whole key is just "%u", the whole QObject will be moved unchanged without
637  * creating a new QDict. The function terminates when there is no entry in the
638  * QDict with a prefix directly (incrementally) following the last one; it also
639  * returns if there are both entries with "%u" and "%u." for the same index %u.
640  * Example: {"0.a": 42, "0.b": 23, "1.x": 0, "4.y": 1, "o.o": 7, "2": 66}
641  *      (or {"1.x": 0, "4.y": 1, "0.a": 42, "o.o": 7, "0.b": 23, "2": 66})
642  *       => [{"a": 42, "b": 23}, {"x": 0}, 66]
643  *      and {"4.y": 1, "o.o": 7} (remainder of the old QDict)
644  */
645 void qdict_array_split(QDict *src, QList **dst)
646 {
647     unsigned i;
648 
649     *dst = qlist_new();
650 
651     for (i = 0; i < UINT_MAX; i++) {
652         QObject *subqobj;
653         bool is_subqdict;
654         QDict *subqdict;
655         char indexstr[32], prefix[32];
656         size_t snprintf_ret;
657 
658         snprintf_ret = snprintf(indexstr, 32, "%u", i);
659         assert(snprintf_ret < 32);
660 
661         subqobj = qdict_get(src, indexstr);
662 
663         snprintf_ret = snprintf(prefix, 32, "%u.", i);
664         assert(snprintf_ret < 32);
665 
666         /* Overflow is the same as positive non-zero results */
667         is_subqdict = qdict_count_prefixed_entries(src, prefix);
668 
669         // There may be either a single subordinate object (named "%u") or
670         // multiple objects (each with a key prefixed "%u."), but not both.
671         if (!subqobj == !is_subqdict) {
672             break;
673         }
674 
675         if (is_subqdict) {
676             qdict_extract_subqdict(src, &subqdict, prefix);
677             assert(qdict_size(subqdict) > 0);
678         } else {
679             qobject_incref(subqobj);
680             qdict_del(src, indexstr);
681         }
682 
683         qlist_append_obj(*dst, subqobj ?: QOBJECT(subqdict));
684     }
685 }
686 
687 /**
688  * qdict_split_flat_key:
689  * @key: the key string to split
690  * @prefix: non-NULL pointer to hold extracted prefix
691  * @suffix: non-NULL pointer to remaining suffix
692  *
693  * Given a flattened key such as 'foo.0.bar', split it into two parts
694  * at the first '.' separator. Allows double dot ('..') to escape the
695  * normal separator.
696  *
697  * e.g.
698  *    'foo.0.bar' -> prefix='foo' and suffix='0.bar'
699  *    'foo..0.bar' -> prefix='foo.0' and suffix='bar'
700  *
701  * The '..' sequence will be unescaped in the returned 'prefix'
702  * string. The 'suffix' string will be left in escaped format, so it
703  * can be fed back into the qdict_split_flat_key() key as the input
704  * later.
705  *
706  * The caller is responsible for freeing the string returned in @prefix
707  * using g_free().
708  */
709 static void qdict_split_flat_key(const char *key, char **prefix,
710                                  const char **suffix)
711 {
712     const char *separator;
713     size_t i, j;
714 
715     /* Find first '.' separator, but if there is a pair '..'
716      * that acts as an escape, so skip over '..' */
717     separator = NULL;
718     do {
719         if (separator) {
720             separator += 2;
721         } else {
722             separator = key;
723         }
724         separator = strchr(separator, '.');
725     } while (separator && separator[1] == '.');
726 
727     if (separator) {
728         *prefix = g_strndup(key, separator - key);
729         *suffix = separator + 1;
730     } else {
731         *prefix = g_strdup(key);
732         *suffix = NULL;
733     }
734 
735     /* Unescape the '..' sequence into '.' */
736     for (i = 0, j = 0; (*prefix)[i] != '\0'; i++, j++) {
737         if ((*prefix)[i] == '.') {
738             assert((*prefix)[i + 1] == '.');
739             i++;
740         }
741         (*prefix)[j] = (*prefix)[i];
742     }
743     (*prefix)[j] = '\0';
744 }
745 
746 /**
747  * qdict_is_list:
748  * @maybe_list: dict to check if keys represent list elements.
749  *
750  * Determine whether all keys in @maybe_list are valid list elements.
751  * If @maybe_list is non-zero in length and all the keys look like
752  * valid list indexes, this will return 1. If @maybe_list is zero
753  * length or all keys are non-numeric then it will return 0 to indicate
754  * it is a normal qdict. If there is a mix of numeric and non-numeric
755  * keys, or the list indexes are non-contiguous, an error is reported.
756  *
757  * Returns: 1 if a valid list, 0 if a dict, -1 on error
758  */
759 static int qdict_is_list(QDict *maybe_list, Error **errp)
760 {
761     const QDictEntry *ent;
762     ssize_t len = 0;
763     ssize_t max = -1;
764     int is_list = -1;
765     int64_t val;
766 
767     for (ent = qdict_first(maybe_list); ent != NULL;
768          ent = qdict_next(maybe_list, ent)) {
769 
770         if (qemu_strtoll(ent->key, NULL, 10, &val) == 0) {
771             if (is_list == -1) {
772                 is_list = 1;
773             } else if (!is_list) {
774                 error_setg(errp,
775                            "Cannot mix list and non-list keys");
776                 return -1;
777             }
778             len++;
779             if (val > max) {
780                 max = val;
781             }
782         } else {
783             if (is_list == -1) {
784                 is_list = 0;
785             } else if (is_list) {
786                 error_setg(errp,
787                            "Cannot mix list and non-list keys");
788                 return -1;
789             }
790         }
791     }
792 
793     if (is_list == -1) {
794         assert(!qdict_size(maybe_list));
795         is_list = 0;
796     }
797 
798     /* NB this isn't a perfect check - e.g. it won't catch
799      * a list containing '1', '+1', '01', '3', but that
800      * does not matter - we've still proved that the
801      * input is a list. It is up the caller to do a
802      * stricter check if desired */
803     if (len != (max + 1)) {
804         error_setg(errp, "List indices are not contiguous, "
805                    "saw %zd elements but %zd largest index",
806                    len, max);
807         return -1;
808     }
809 
810     return is_list;
811 }
812 
813 /**
814  * qdict_crumple:
815  * @src: the original flat dictionary (only scalar values) to crumple
816  *
817  * Takes a flat dictionary whose keys use '.' separator to indicate
818  * nesting, and values are scalars, and crumples it into a nested
819  * structure.
820  *
821  * To include a literal '.' in a key name, it must be escaped as '..'
822  *
823  * For example, an input of:
824  *
825  * { 'foo.0.bar': 'one', 'foo.0.wizz': '1',
826  *   'foo.1.bar': 'two', 'foo.1.wizz': '2' }
827  *
828  * will result in an output of:
829  *
830  * {
831  *   'foo': [
832  *      { 'bar': 'one', 'wizz': '1' },
833  *      { 'bar': 'two', 'wizz': '2' }
834  *   ],
835  * }
836  *
837  * The following scenarios in the input dict will result in an
838  * error being returned:
839  *
840  *  - Any values in @src are non-scalar types
841  *  - If keys in @src imply that a particular level is both a
842  *    list and a dict. e.g., "foo.0.bar" and "foo.eek.bar".
843  *  - If keys in @src imply that a particular level is a list,
844  *    but the indices are non-contiguous. e.g. "foo.0.bar" and
845  *    "foo.2.bar" without any "foo.1.bar" present.
846  *  - If keys in @src represent list indexes, but are not in
847  *    the "%zu" format. e.g. "foo.+0.bar"
848  *
849  * Returns: either a QDict or QList for the nested data structure, or NULL
850  * on error
851  */
852 QObject *qdict_crumple(const QDict *src, Error **errp)
853 {
854     const QDictEntry *ent;
855     QDict *two_level, *multi_level = NULL;
856     QObject *dst = NULL, *child;
857     size_t i;
858     char *prefix = NULL;
859     const char *suffix = NULL;
860     int is_list;
861 
862     two_level = qdict_new();
863 
864     /* Step 1: split our totally flat dict into a two level dict */
865     for (ent = qdict_first(src); ent != NULL; ent = qdict_next(src, ent)) {
866         if (qobject_type(ent->value) == QTYPE_QDICT ||
867             qobject_type(ent->value) == QTYPE_QLIST) {
868             error_setg(errp, "Value %s is not a scalar",
869                        ent->key);
870             goto error;
871         }
872 
873         qdict_split_flat_key(ent->key, &prefix, &suffix);
874 
875         child = qdict_get(two_level, prefix);
876         if (suffix) {
877             if (child) {
878                 if (qobject_type(child) != QTYPE_QDICT) {
879                     error_setg(errp, "Key %s prefix is already set as a scalar",
880                                prefix);
881                     goto error;
882                 }
883             } else {
884                 child = QOBJECT(qdict_new());
885                 qdict_put_obj(two_level, prefix, child);
886             }
887             qobject_incref(ent->value);
888             qdict_put_obj(qobject_to_qdict(child), suffix, ent->value);
889         } else {
890             if (child) {
891                 error_setg(errp, "Key %s prefix is already set as a dict",
892                            prefix);
893                 goto error;
894             }
895             qobject_incref(ent->value);
896             qdict_put_obj(two_level, prefix, ent->value);
897         }
898 
899         g_free(prefix);
900         prefix = NULL;
901     }
902 
903     /* Step 2: optionally process the two level dict recursively
904      * into a multi-level dict */
905     multi_level = qdict_new();
906     for (ent = qdict_first(two_level); ent != NULL;
907          ent = qdict_next(two_level, ent)) {
908 
909         if (qobject_type(ent->value) == QTYPE_QDICT) {
910             child = qdict_crumple(qobject_to_qdict(ent->value), errp);
911             if (!child) {
912                 goto error;
913             }
914 
915             qdict_put_obj(multi_level, ent->key, child);
916         } else {
917             qobject_incref(ent->value);
918             qdict_put_obj(multi_level, ent->key, ent->value);
919         }
920     }
921     QDECREF(two_level);
922     two_level = NULL;
923 
924     /* Step 3: detect if we need to turn our dict into list */
925     is_list = qdict_is_list(multi_level, errp);
926     if (is_list < 0) {
927         goto error;
928     }
929 
930     if (is_list) {
931         dst = QOBJECT(qlist_new());
932 
933         for (i = 0; i < qdict_size(multi_level); i++) {
934             char *key = g_strdup_printf("%zu", i);
935 
936             child = qdict_get(multi_level, key);
937             g_free(key);
938 
939             if (!child) {
940                 error_setg(errp, "Missing list index %zu", i);
941                 goto error;
942             }
943 
944             qobject_incref(child);
945             qlist_append_obj(qobject_to_qlist(dst), child);
946         }
947         QDECREF(multi_level);
948         multi_level = NULL;
949     } else {
950         dst = QOBJECT(multi_level);
951     }
952 
953     return dst;
954 
955  error:
956     g_free(prefix);
957     QDECREF(multi_level);
958     QDECREF(two_level);
959     qobject_decref(dst);
960     return NULL;
961 }
962 
963 /**
964  * qdict_array_entries(): Returns the number of direct array entries if the
965  * sub-QDict of src specified by the prefix in subqdict (or src itself for
966  * prefix == "") is valid as an array, i.e. the length of the created list if
967  * the sub-QDict would become empty after calling qdict_array_split() on it. If
968  * the array is not valid, -EINVAL is returned.
969  */
970 int qdict_array_entries(QDict *src, const char *subqdict)
971 {
972     const QDictEntry *entry;
973     unsigned i;
974     unsigned entries = 0;
975     size_t subqdict_len = strlen(subqdict);
976 
977     assert(!subqdict_len || subqdict[subqdict_len - 1] == '.');
978 
979     /* qdict_array_split() loops until UINT_MAX, but as we want to return
980      * negative errors, we only have a signed return value here. Any additional
981      * entries will lead to -EINVAL. */
982     for (i = 0; i < INT_MAX; i++) {
983         QObject *subqobj;
984         int subqdict_entries;
985         char *prefix = g_strdup_printf("%s%u.", subqdict, i);
986 
987         subqdict_entries = qdict_count_prefixed_entries(src, prefix);
988 
989         /* Remove ending "." */
990         prefix[strlen(prefix) - 1] = 0;
991         subqobj = qdict_get(src, prefix);
992 
993         g_free(prefix);
994 
995         if (subqdict_entries < 0) {
996             return subqdict_entries;
997         }
998 
999         /* There may be either a single subordinate object (named "%u") or
1000          * multiple objects (each with a key prefixed "%u."), but not both. */
1001         if (subqobj && subqdict_entries) {
1002             return -EINVAL;
1003         } else if (!subqobj && !subqdict_entries) {
1004             break;
1005         }
1006 
1007         entries += subqdict_entries ? subqdict_entries : 1;
1008     }
1009 
1010     /* Consider everything handled that isn't part of the given sub-QDict */
1011     for (entry = qdict_first(src); entry; entry = qdict_next(src, entry)) {
1012         if (!strstart(qdict_entry_key(entry), subqdict, NULL)) {
1013             entries++;
1014         }
1015     }
1016 
1017     /* Anything left in the sub-QDict that wasn't handled? */
1018     if (qdict_size(src) != entries) {
1019         return -EINVAL;
1020     }
1021 
1022     return i;
1023 }
1024 
1025 /**
1026  * qdict_join(): Absorb the src QDict into the dest QDict, that is, move all
1027  * elements from src to dest.
1028  *
1029  * If an element from src has a key already present in dest, it will not be
1030  * moved unless overwrite is true.
1031  *
1032  * If overwrite is true, the conflicting values in dest will be discarded and
1033  * replaced by the corresponding values from src.
1034  *
1035  * Therefore, with overwrite being true, the src QDict will always be empty when
1036  * this function returns. If overwrite is false, the src QDict will be empty
1037  * iff there were no conflicts.
1038  */
1039 void qdict_join(QDict *dest, QDict *src, bool overwrite)
1040 {
1041     const QDictEntry *entry, *next;
1042 
1043     entry = qdict_first(src);
1044     while (entry) {
1045         next = qdict_next(src, entry);
1046 
1047         if (overwrite || !qdict_haskey(dest, entry->key)) {
1048             qobject_incref(entry->value);
1049             qdict_put_obj(dest, entry->key, entry->value);
1050             qdict_del(src, entry->key);
1051         }
1052 
1053         entry = next;
1054     }
1055 }
1056