Merge the main public API of GVariant
[platform/upstream/glib.git] / glib / gvariant.c
1 /*
2  * Copyright © 2007, 2008 Ryan Lortie
3  * Copyright © 2010 Codethink Limited
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the licence, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Ryan Lortie <desrt@desrt.ca>
21  */
22
23 #include "config.h"
24
25 #include <glib/gvariant-serialiser.h>
26 #include <glib/gvariant-core.h>
27 #include <glib/gtestutils.h>
28 #include <glib/gstrfuncs.h>
29 #include <glib/ghash.h>
30 #include <glib/gmem.h>
31
32 #include <string.h>
33
34 #include "galias.h"
35
36 /**
37  * SECTION: gvariant
38  * @title: GVariant
39  * @short_description: strongly typed value datatype
40  * @see_also: GVariantType
41  *
42  * #GVariant is a variant datatype; it stores a value along with
43  * information about the type of that value.  The range of possible
44  * values is determined by the type.  The type system used by #GVariant
45  * is #GVariantType.
46  *
47  * #GVariant instances always have a type and a value (which are given
48  * at construction time).  The type and value of a #GVariant instance
49  * can never change other than by the #GVariant itself being
50  * destroyed.  A #GVariant can not contain a pointer.
51  *
52  * #GVariant is reference counted using g_variant_ref() and
53  * g_variant_unref().  #GVariant also has floating reference counts --
54  * see g_variant_ref_sink().
55  *
56  * #GVariant is completely threadsafe.  A #GVariant instance can be
57  * concurrently accessed in any way from any number of threads without
58  * problems.
59  *
60  * #GVariant is heavily optimised for dealing with data in serialised
61  * form.  It works particularly well with data located in memory-mapped
62  * files.  It can perform nearly all deserialisation operations in a
63  * small constant time, usually touching only a single memory page.
64  * Serialised #GVariant data can also be sent over the network.
65  *
66  * #GVariant is largely compatible with DBus.  Almost all types of
67  * #GVariant instances can be sent over DBus.  See #GVariantType for
68  * exceptions.
69  *
70  * For convenience to C programmers, #GVariant features powerful
71  * varargs-based value construction and destruction.  This feature is
72  * designed to be embedded in other libraries.
73  *
74  * There is a Python-inspired text language for describing #GVariant
75  * values.  #GVariant includes a printer for this language and a parser
76  * with type inferencing.
77  *
78  * <refsect2>
79  *  <title>Memory Use</title>
80  *  <para>
81  *   #GVariant tries to be quite efficient with respect to memory use.
82  *   This section gives a rough idea of how much memory is used by the
83  *   current implementation.  The information here is subject to change
84  *   in the future.
85  *  </para>
86  *  <para>
87  *   The memory allocated by #GVariant can be grouped into 4 broad
88  *   purposes: memory for serialised data, memory for the type
89  *   information cache, buffer management memory and memory for the
90  *   #GVariant structure itself.
91  *  </para>
92  *  <refsect3>
93  *   <title>Serialised Data Memory</title>
94  *   <para>
95  *    This is the memory that is used for storing GVariant data in
96  *    serialised form.  This is what would be sent over the network or
97  *    what would end up on disk.
98  *   </para>
99  *   <para>
100  *    The amount of memory required to store a boolean is 1 byte.  16,
101  *    32 and 64 bit integers and double precision floating point numbers
102  *    use their "natural" size.  Strings (including object path and
103  *    signature strings) are stored with a nul terminator, and as such
104  *    use the length of the string plus 1 byte.
105  *   </para>
106  *   <para>
107  *    Maybe types use no space at all to represent the null value and
108  *    use the same amount of space (sometimes plus one byte) as the
109  *    equivalent non-maybe-typed value to represent the non-null case.
110  *   </para>
111  *   <para>
112  *    Arrays use the amount of space required to store each of their
113  *    members, concatenated.  Additionally, if the items stored in an
114  *    array are not of a fixed-size (ie: strings, other arrays, etc)
115  *    then an additional framing offset is stored for each item.  The
116  *    size of this offset is either 1, 2 or 4 bytes depending on the
117  *    overall size of the container.  Additionally, extra padding bytes
118  *    are added as required for alignment of child values.
119  *   </para>
120  *   <para>
121  *    Tuples (including dictionary entries) use the amount of space
122  *    required to store each of their members, concatenated, plus one
123  *    framing offset (as per arrays) for each non-fixed-sized item in
124  *    the tuple, except for the last one.  Additionally, extra padding
125  *    bytes are added as required for alignment of child values.
126  *   </para>
127  *   <para>
128  *    Variants use the same amount of space as the item inside of the
129  *    variant, plus 1 byte, plus the length of the type string for the
130  *    item inside the variant.
131  *   </para>
132  *   <para>
133  *    As an example, consider a dictionary mapping strings to variants.
134  *    In the case that the dictionary is empty, 0 bytes are required for
135  *    the serialisation.
136  *   </para>
137  *   <para>
138  *    If we add an item "width" that maps to the int32 value of 500 then
139  *    we will use 4 byte to store the int32 (so 6 for the variant
140  *    containing it) and 6 bytes for the string.  The variant must be
141  *    aligned to 8 after the 6 bytes of the string, so that's 2 extra
142  *    bytes.  6 (string) + 2 (padding) + 6 (variant) is 14 bytes used
143  *    for the dictionary entry.  An additional 1 byte is added to the
144  *    array as a framing offset making a total of 15 bytes.
145  *   </para>
146  *   <para>
147  *    If we add another entry, "title" that maps to a nullable string
148  *    that happens to have a value of null, then we use 0 bytes for the
149  *    null value (and 3 bytes for the variant to contain it along with
150  *    its type string) plus 6 bytes for the string.  Again, we need 2
151  *    padding bytes.  That makes a total of 6 + 2 + 3 = 11 bytes.
152  *   </para>
153  *   <para>
154  *    We now require extra padding between the two items in the array.
155  *    After the 14 bytes of the first item, that's 2 bytes required.  We
156  *    now require 2 framing offsets for an extra two bytes.  14 + 2 + 11
157  *    + 2 = 29 bytes to encode the entire two-item dictionary.
158  *   </para>
159  *  </refsect3>
160  *  <refsect3>
161  *   <title>Type Information Cache</title>
162  *   <para>
163  *    For each GVariant type that currently exists in the program a type
164  *    information structure is kept in the type information cache.  The
165  *    type information structure is required for rapid deserialisation.
166  *   </para>
167  *   <para>
168  *    Continuing with the above example, if a #GVariant exists with the
169  *    type "a{sv}" then a type information struct will exist for
170  *    "a{sv}", "{sv}", "s", and "v".  Multiple uses of the same type
171  *    will share the same type information.  Additionally, all
172  *    single-digit types are stored in read-only static memory and do
173  *    not contribute to the writable memory footprint of a program using
174  *    #GVariant.
175  *   </para>
176  *   <para>
177  *    Aside from the type information structures stored in read-only
178  *    memory, there are two forms of type information.  One is used for
179  *    container types where there is a single element type: arrays and
180  *    maybe types.  The other is used for container types where there
181  *    are multiple element types: tuples and dictionary entries.
182  *   </para>
183  *   <para>
184  *    Array type info structures are 6 * sizeof (void *), plus the
185  *    memory required to store the type string itself.  This means that
186  *    on 32bit systems, the cache entry for "a{sv}" would require 30
187  *    bytes of memory (plus malloc overhead).
188  *   </para>
189  *   <para>
190  *    Tuple type info structures are 6 * sizeof (void *), plus 4 *
191  *    sizeof (void *) for each item in the tuple, plus the memory
192  *    required to store the type string itself.  A 2-item tuple, for
193  *    example, would have a type information structure that consumed
194  *    writable memory in the size of 14 * sizeof (void *) (plus type
195  *    string)  This means that on 32bit systems, the cache entry for
196  *    "{sv}" would require 61 bytes of memory (plus malloc overhead).
197  *   </para>
198  *   <para>
199  *    This means that in total, for our "a{sv}" example, 91 bytes of
200  *    type information would be allocated.
201  *   </para>
202  *   <para>
203  *    The type information cache, additionally, uses a #GHashTable to
204  *    store and lookup the cached items and stores a pointer to this
205  *    hash table in static storage.  The hash table is freed when there
206  *    are zero items in the type cache.
207  *   </para>
208  *   <para>
209  *    Although these sizes may seem large it is important to remember
210  *    that a program will probably only have a very small number of
211  *    different types of values in it and that only one type information
212  *    structure is required for many different values of the same type.
213  *   </para>
214  *  </refsect3>
215  *  <refsect3>
216  *   <title>Buffer Management Memory</title>
217  *   <para>
218  *    #GVariant uses an internal buffer management structure to deal
219  *    with the various different possible sources of serialised data
220  *    that it uses.  The buffer is responsible for ensuring that the
221  *    correct call is made when the data is no longer in use by
222  *    #GVariant.  This may involve a g_free() or a g_slice_free() or
223  *    even g_mapped_file_unref().
224  *   </para>
225  *   <para>
226  *    One buffer management structure is used for each chunk of
227  *    serialised data.  The size of the buffer management structure is 4
228  *    * (void *).  On 32bit systems, that's 16 bytes.
229  *   </para>
230  *  </refsect3>
231  *  <refsect3>
232  *   <title>GVariant structure</title>
233  *   <para>
234  *    The size of a #GVariant structure is 6 * (void *).  On 32 bit
235  *    systems, that's 24 bytes.
236  *   </para>
237  *   <para>
238  *    #GVariant structures only exist if they are explicitly created
239  *    with API calls.  For example, if a #GVariant is constructed out of
240  *    serialised data for the example given above (with the dictionary)
241  *    then although there are 9 individual values that comprise the
242  *    entire dictionary (two keys, two values, two variants containing
243  *    the values, two dictionary entries, plus the dictionary itself),
244  *    only 1 #GVariant instance exists -- the one refering to the
245  *    dictionary.
246  *   </para>
247  *   <para>
248  *    If calls are made to start accessing the other values then
249  *    #GVariant instances will exist for those values only for as long
250  *    as they are in use (ie: until you call g_variant_unref()).  The
251  *    type information is shared.  The serialised data and the buffer
252  *    management structure for that serialised data is shared by the
253  *    child.
254  *   </para>
255  *  </refsect3>
256  *  <refsect3>
257  *   <title>Summary</title>
258  *   <para>
259  *    To put the entire example together, for our dictionary mapping
260  *    strings to variants (with two entries, as given above), we are
261  *    using 91 bytes of memory for type information, 29 byes of memory
262  *    for the serialised data, 16 bytes for buffer management and 24
263  *    bytes for the #GVariant instance, or a total of 160 bytes, plus
264  *    malloc overhead.  If we were to use g_variant_get_child_value() to
265  *    access the two dictionary entries, we would use an additional 48
266  *    bytes.  If we were to have other dictionaries of the same type, we
267  *    would use more memory for the serialised data and buffer
268  *    management for those dictionaries, but the type information would
269  *    be shared.
270  *   </para>
271  *  </refsect3>
272  * </refsect2>
273  */
274
275 /* definition of GVariant structure is in gvariant-core.c */
276
277 /* this is a g_return_val_if_fail() for making
278  * sure a (GVariant *) has the required type.
279  */
280 #define TYPE_CHECK(value, TYPE, val) \
281   if G_UNLIKELY (!g_variant_is_of_type (value, TYPE)) {           \
282     g_return_if_fail_warning (G_LOG_DOMAIN, __PRETTY_FUNCTION__,  \
283                               "g_variant_is_of_type (" #value     \
284                               ", " #TYPE ")");                    \
285     return val;                                                   \
286   }
287
288 /* < private >
289  * g_variant_new_from_trusted:
290  * @type: the #GVariantType
291  * @data: the data to use
292  * @size: the size of @data
293  * @returns: a new floating #GVariant
294  *
295  * Constructs a new trusted #GVariant instance from the provided data.
296  * This is used to implement g_variant_new_* for all the basic types.
297  */
298 static GVariant *
299 g_variant_new_from_trusted (const GVariantType *type,
300                             gconstpointer       data,
301                             gsize               size)
302 {
303   GVariant *value;
304   GBuffer *buffer;
305
306   buffer = g_buffer_new_from_data (data, size);
307   value = g_variant_new_from_buffer (type, buffer, TRUE);
308   g_buffer_unref (buffer);
309
310   return value;
311 }
312
313 /**
314  * g_variant_new_boolean:
315  * @boolean: a #gboolean value
316  * @returns: a new boolean #GVariant instance
317  *
318  * Creates a new boolean #GVariant instance -- either %TRUE or %FALSE.
319  *
320  * Since: 2.24
321  **/
322 GVariant *
323 g_variant_new_boolean (gboolean value)
324 {
325   guchar v = value;
326
327   return g_variant_new_from_trusted (G_VARIANT_TYPE_BOOLEAN, &v, 1);
328 }
329
330 /**
331  * g_variant_get_boolean:
332  * @value: a boolean #GVariant instance
333  * @returns: %TRUE or %FALSE
334  *
335  * Returns the boolean value of @value.
336  *
337  * It is an error to call this function with a @value of any type
338  * other than %G_VARIANT_TYPE_BOOLEAN.
339  *
340  * Since: 2.24
341  **/
342 gboolean
343 g_variant_get_boolean (GVariant *value)
344 {
345   const guchar *data;
346
347   TYPE_CHECK (value, G_VARIANT_TYPE_BOOLEAN, FALSE);
348
349   data = g_variant_get_data (value);
350
351   return data != NULL ? *data != 0 : FALSE;
352 }
353
354 /* the constructors and accessors for byte, int{16,32,64}, handles and
355  * doubles all look pretty much exactly the same, so we reduce
356  * copy/pasting here.
357  */
358 #define NUMERIC_TYPE(TYPE, type, ctype) \
359   GVariant *g_variant_new_##type (ctype value) {                \
360     return g_variant_new_from_trusted (G_VARIANT_TYPE_##TYPE,   \
361                                        &value, sizeof value);   \
362   }                                                             \
363   ctype g_variant_get_##type (GVariant *value) {                \
364     const ctype *data;                                          \
365     TYPE_CHECK (value, G_VARIANT_TYPE_ ## TYPE, 0);             \
366     data = g_variant_get_data (value);                          \
367     return data != NULL ? *data : 0;                            \
368   }
369
370
371 /**
372  * g_variant_new_byte:
373  * @byte: a #guint8 value
374  * @returns: a new byte #GVariant instance
375  *
376  * Creates a new byte #GVariant instance.
377  *
378  * Since: 2.24
379  **/
380 /**
381  * g_variant_get_byte:
382  * @value: a byte #GVariant instance
383  * @returns: a #guchar
384  *
385  * Returns the byte value of @value.
386  *
387  * It is an error to call this function with a @value of any type
388  * other than %G_VARIANT_TYPE_BYTE.
389  *
390  * Since: 2.24
391  **/
392 NUMERIC_TYPE (BYTE, byte, guchar)
393
394 /**
395  * g_variant_new_int16:
396  * @int16: a #gint16 value
397  * @returns: a new int16 #GVariant instance
398  *
399  * Creates a new int16 #GVariant instance.
400  *
401  * Since: 2.24
402  **/
403 /**
404  * g_variant_get_int16:
405  * @value: a int16 #GVariant instance
406  * @returns: a #gint16
407  *
408  * Returns the 16-bit signed integer value of @value.
409  *
410  * It is an error to call this function with a @value of any type
411  * other than %G_VARIANT_TYPE_INT16.
412  *
413  * Since: 2.24
414  **/
415 NUMERIC_TYPE (INT16, int16, gint16)
416
417 /**
418  * g_variant_new_uint16:
419  * @uint16: a #guint16 value
420  * @returns: a new uint16 #GVariant instance
421  *
422  * Creates a new uint16 #GVariant instance.
423  *
424  * Since: 2.24
425  **/
426 /**
427  * g_variant_get_uint16:
428  * @value: a uint16 #GVariant instance
429  * @returns: a #guint16
430  *
431  * Returns the 16-bit unsigned integer value of @value.
432  *
433  * It is an error to call this function with a @value of any type
434  * other than %G_VARIANT_TYPE_UINT16.
435  *
436  * Since: 2.24
437  **/
438 NUMERIC_TYPE (UINT16, uint16, guint16)
439
440 /**
441  * g_variant_new_int32:
442  * @int32: a #gint32 value
443  * @returns: a new int32 #GVariant instance
444  *
445  * Creates a new int32 #GVariant instance.
446  *
447  * Since: 2.24
448  **/
449 /**
450  * g_variant_get_int32:
451  * @value: a int32 #GVariant instance
452  * @returns: a #gint32
453  *
454  * Returns the 32-bit signed integer value of @value.
455  *
456  * It is an error to call this function with a @value of any type
457  * other than %G_VARIANT_TYPE_INT32.
458  *
459  * Since: 2.24
460  **/
461 NUMERIC_TYPE (INT32, int32, gint32)
462
463 /**
464  * g_variant_new_uint32:
465  * @uint32: a #guint32 value
466  * @returns: a new uint32 #GVariant instance
467  *
468  * Creates a new uint32 #GVariant instance.
469  *
470  * Since: 2.24
471  **/
472 /**
473  * g_variant_get_uint32:
474  * @value: a uint32 #GVariant instance
475  * @returns: a #guint32
476  *
477  * Returns the 32-bit unsigned integer value of @value.
478  *
479  * It is an error to call this function with a @value of any type
480  * other than %G_VARIANT_TYPE_UINT32.
481  *
482  * Since: 2.24
483  **/
484 NUMERIC_TYPE (UINT32, uint32, guint32)
485
486 /**
487  * g_variant_new_int64:
488  * @int64: a #gint64 value
489  * @returns: a new int64 #GVariant instance
490  *
491  * Creates a new int64 #GVariant instance.
492  *
493  * Since: 2.24
494  **/
495 /**
496  * g_variant_get_int64:
497  * @value: a int64 #GVariant instance
498  * @returns: a #gint64
499  *
500  * Returns the 64-bit signed integer value of @value.
501  *
502  * It is an error to call this function with a @value of any type
503  * other than %G_VARIANT_TYPE_INT64.
504  *
505  * Since: 2.24
506  **/
507 NUMERIC_TYPE (INT64, int64, gint64)
508
509 /**
510  * g_variant_new_uint64:
511  * @uint64: a #guint64 value
512  * @returns: a new uint64 #GVariant instance
513  *
514  * Creates a new uint64 #GVariant instance.
515  *
516  * Since: 2.24
517  **/
518 /**
519  * g_variant_get_uint64:
520  * @value: a uint64 #GVariant instance
521  * @returns: a #guint64
522  *
523  * Returns the 64-bit unsigned integer value of @value.
524  *
525  * It is an error to call this function with a @value of any type
526  * other than %G_VARIANT_TYPE_UINT64.
527  *
528  * Since: 2.24
529  **/
530 NUMERIC_TYPE (UINT64, uint64, guint64)
531
532 /**
533  * g_variant_new_handle:
534  * @handle: a #gint32 value
535  * @returns: a new handle #GVariant instance
536  *
537  * Creates a new handle #GVariant instance.
538  *
539  * By convention, handles are indexes into an array of file descriptors
540  * that are sent alongside a DBus message.  If you're not interacting
541  * with DBus, you probably don't need them.
542  *
543  * Since: 2.24
544  **/
545 /**
546  * g_variant_get_handle:
547  * @value: a handle #GVariant instance
548  * @returns: a #gint32
549  *
550  * Returns the 32-bit signed integer value of @value.
551  *
552  * It is an error to call this function with a @value of any type other
553  * than %G_VARIANT_TYPE_HANDLE.
554  *
555  * By convention, handles are indexes into an array of file descriptors
556  * that are sent alongside a DBus message.  If you're not interacting
557  * with DBus, you probably don't need them.
558  *
559  * Since: 2.24
560  **/
561 NUMERIC_TYPE (HANDLE, handle, gint32)
562
563 /**
564  * g_variant_new_double:
565  * @floating: a #gdouble floating point value
566  * @returns: a new double #GVariant instance
567  *
568  * Creates a new double #GVariant instance.
569  *
570  * Since: 2.24
571  **/
572 /**
573  * g_variant_get_double:
574  * @value: a double #GVariant instance
575  * @returns: a #gdouble
576  *
577  * Returns the double precision floating point value of @value.
578  *
579  * It is an error to call this function with a @value of any type
580  * other than %G_VARIANT_TYPE_DOUBLE.
581  *
582  * Since: 2.24
583  **/
584 NUMERIC_TYPE (DOUBLE, double, gdouble)
585
586 /**
587  * g_variant_get_type:
588  * @value: a #GVariant
589  * @returns: a #GVariantType
590  *
591  * Determines the type of @value.
592  *
593  * The return value is valid for the lifetime of @value and must not
594  * be freed.
595  *
596  * Since: 2.24
597  **/
598 const GVariantType *
599 g_variant_get_type (GVariant *value)
600 {
601   GVariantTypeInfo *type_info;
602
603   g_return_val_if_fail (value != NULL, NULL);
604
605   type_info = g_variant_get_type_info (value);
606
607   return (GVariantType *) g_variant_type_info_get_type_string (type_info);
608 }
609
610 /**
611  * g_variant_get_type_string:
612  * @value: a #GVariant
613  * @returns: the type string for the type of @value
614  *
615  * Returns the type string of @value.  Unlike the result of calling
616  * g_variant_type_peek_string(), this string is nul-terminated.  This
617  * string belongs to #GVariant and must not be freed.
618  *
619  * Since: 2.24
620  **/
621 const gchar *
622 g_variant_get_type_string (GVariant *value)
623 {
624   GVariantTypeInfo *type_info;
625
626   g_return_val_if_fail (value != NULL, NULL);
627
628   type_info = g_variant_get_type_info (value);
629
630   return g_variant_type_info_get_type_string (type_info);
631 }
632
633 /**
634  * g_variant_is_of_type:
635  * @value: a #GVariant instance
636  * @type: a #GVariantType
637  * @returns: %TRUE if the type of @value matches @type
638  *
639  * Checks if a value has a type matching the provided type.
640  *
641  * Since: 2.24
642  **/
643 gboolean
644 g_variant_is_of_type (GVariant           *value,
645                       const GVariantType *type)
646 {
647   return g_variant_type_is_subtype_of (g_variant_get_type (value), type);
648 }
649
650 /**
651  * g_variant_is_container:
652  * @value: a #GVariant instance
653  * @returns: %TRUE if @value is a container
654  *
655  * Checks if @value is a container.
656  */
657 gboolean
658 g_variant_is_container (GVariant *value)
659 {
660   return g_variant_type_is_container (g_variant_get_type (value));
661 }
662
663 /**
664  * g_variant_new_maybe:
665  * @child_type: the #GVariantType of the child
666  * @child: the child value, or %NULL
667  * @returns: a new #GVariant maybe instance
668  *
669  * Depending on if @value is %NULL, either wraps @value inside of a
670  * maybe container or creates a Nothing instance for the given @type.
671  *
672  * At least one of @type and @value must be non-%NULL.  If @type is
673  * non-%NULL then it must be a definite type.  If they are both
674  * non-%NULL then @type must be the type of @value.
675  *
676  * Since: 2.24
677  **/
678 GVariant *
679 g_variant_new_maybe (const GVariantType *child_type,
680                      GVariant           *child)
681 {
682   GVariantType *maybe_type;
683   GVariant *value;
684
685   g_return_val_if_fail (child_type == NULL || g_variant_type_is_definite
686                         (child_type), 0);
687   g_return_val_if_fail (child_type != NULL || child != NULL, NULL);
688   g_return_val_if_fail (child_type == NULL || child == NULL ||
689                         g_variant_is_of_type (child, child_type),
690                         NULL);
691
692   if (child_type == NULL)
693     child_type = g_variant_get_type (child);
694
695   maybe_type = g_variant_type_new_maybe (child_type);
696
697   if (child != NULL)
698     {
699       GVariant **children;
700       gboolean trusted;
701
702       children = g_new (GVariant *, 1);
703       children[0] = g_variant_ref_sink (child);
704       trusted = g_variant_is_trusted (children[0]);
705
706       value = g_variant_new_from_children (maybe_type, children, 1, trusted);
707     }
708   else
709     value = g_variant_new_from_children (maybe_type, NULL, 0, TRUE);
710
711   g_variant_type_free (maybe_type);
712
713   return value;
714 }
715
716 /**
717  * g_variant_get_maybe:
718  * @value: a maybe-typed value
719  * @returns: the contents of @value, or %NULL
720  *
721  * Given a maybe-typed #GVariant instance, extract its value.  If the
722  * value is Nothing, then this function returns %NULL.
723  *
724  * Since: 2.24
725  **/
726 GVariant *
727 g_variant_get_maybe (GVariant *value)
728 {
729   TYPE_CHECK (value, G_VARIANT_TYPE_MAYBE, NULL);
730
731   if (g_variant_n_children (value))
732     return g_variant_get_child_value (value, 0);
733
734   return NULL;
735 }
736
737 /**
738  * g_variant_new_variant:
739  * @value: a #GVariance instance
740  * @returns: a new variant #GVariant instance
741  *
742  * Boxes @value.  The result is a #GVariant instance representing a
743  * variant containing the original value.
744  *
745  * Since: 2.24
746  **/
747 GVariant *
748 g_variant_new_variant (GVariant *value)
749 {
750   g_return_val_if_fail (value != NULL, NULL);
751
752   return g_variant_new_from_children (G_VARIANT_TYPE_VARIANT,
753                                       g_memdup (&value, sizeof value),
754                                       1, g_variant_is_trusted (value));
755 }
756
757 /**
758  * g_variant_get_variant:
759  * @value: a variant #GVariance instance
760  * @returns: the item contained in the variant
761  *
762  * Unboxes @value.  The result is the #GVariant instance that was
763  * contained in @value.
764  *
765  * Since: 2.24
766  **/
767 GVariant *
768 g_variant_get_variant (GVariant *value)
769 {
770   TYPE_CHECK (value, G_VARIANT_TYPE_VARIANT, NULL);
771
772   return g_variant_get_child_value (value, 0);
773 }
774
775 /**
776  * g_variant_new_string:
777  * @string: a normal C nul-terminated string
778  * @returns: a new string #GVariant instance
779  *
780  * Creates a string #GVariant with the contents of @string.
781  *
782  * Since: 2.24
783  **/
784 GVariant *
785 g_variant_new_string (const gchar *string)
786 {
787   g_return_val_if_fail (string != NULL, NULL);
788
789   return g_variant_new_from_trusted (G_VARIANT_TYPE_STRING,
790                                      string, strlen (string) + 1);
791 }
792
793 /**
794  * g_variant_new_object_path:
795  * @object_path: a normal C nul-terminated string
796  * @returns: a new object path #GVariant instance
797  *
798  * Creates a DBus object path #GVariant with the contents of @string.
799  * @string must be a valid DBus object path.  Use
800  * g_variant_is_object_path() if you're not sure.
801  *
802  * Since: 2.24
803  **/
804 GVariant *
805 g_variant_new_object_path (const gchar *object_path)
806 {
807   g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
808
809   return g_variant_new_from_trusted (G_VARIANT_TYPE_OBJECT_PATH,
810                                      object_path, strlen (object_path) + 1);
811 }
812
813 /**
814  * g_variant_is_object_path:
815  * @string: a normal C nul-terminated string
816  * @returns: %TRUE if @string is a DBus object path
817  *
818  * Determines if a given string is a valid DBus object path.  You
819  * should ensure that a string is a valid DBus object path before
820  * passing it to g_variant_new_object_path().
821  *
822  * A valid object path starts with '/' followed by zero or more
823  * sequences of characters separated by '/' characters.  Each sequence
824  * must contain only the characters "[A-Z][a-z][0-9]_".  No sequence
825  * (including the one following the final '/' character) may be empty.
826  *
827  * Since: 2.24
828  **/
829 gboolean
830 g_variant_is_object_path (const gchar *string)
831 {
832   g_return_val_if_fail (string != NULL, FALSE);
833
834   return g_variant_serialiser_is_object_path (string, strlen (string) + 1);
835 }
836
837
838 /**
839  * g_variant_new_signature:
840  * @signature: a normal C nul-terminated string
841  * @returns: a new signature #GVariant instance
842  *
843  * Creates a DBus type signature #GVariant with the contents of
844  * @string.  @string must be a valid DBus type signature.  Use
845  * g_variant_is_signature() if you're not sure.
846  *
847  * Since: 2.24
848  **/
849 GVariant *
850 g_variant_new_signature (const gchar *signature)
851 {
852   g_return_val_if_fail (g_variant_is_signature (signature), NULL);
853
854   return g_variant_new_from_trusted (G_VARIANT_TYPE_SIGNATURE,
855                                      signature, strlen (signature) + 1);
856 }
857
858 /**
859  * g_variant_is_signature:
860  * @string: a normal C nul-terminated string
861  * @returns: %TRUE if @string is a DBus type signature
862  *
863  * Determines if a given string is a valid DBus type signature.  You
864  * should ensure that a string is a valid DBus object path before
865  * passing it to g_variant_new_signature().
866  *
867  * DBus type signatures consist of zero or more definite #GVariantType
868  * strings in sequence.
869  *
870  * Since: 2.24
871  **/
872 gboolean
873 g_variant_is_signature (const gchar *string)
874 {
875   g_return_val_if_fail (string != NULL, FALSE);
876
877   return g_variant_serialiser_is_signature (string, strlen (string) + 1);
878 }
879
880 /**
881  * g_variant_get_string:
882  * @value: a string #GVariant instance
883  * @length: a pointer to a #gsize, to store the length
884  * @returns: the constant string
885  *
886  * Returns the string value of a #GVariant instance with a string
887  * type.  This includes the types %G_VARIANT_TYPE_STRING,
888  * %G_VARIANT_TYPE_OBJECT_PATH and %G_VARIANT_TYPE_SIGNATURE.
889  *
890  * If @length is non-%NULL then the length of the string (in bytes) is
891  * returned there.  For trusted values, this information is already
892  * known.  For untrusted values, a strlen() will be performed.
893  *
894  * It is an error to call this function with a @value of any type
895  * other than those three.
896  *
897  * The return value remains valid as long as @value exists.
898  *
899  * Since: 2.24
900  **/
901 const gchar *
902 g_variant_get_string (GVariant *value,
903                       gsize    *length)
904 {
905   g_return_val_if_fail (value != NULL, NULL);
906   g_return_val_if_fail (
907     g_variant_is_of_type (value, G_VARIANT_TYPE_STRING) ||
908     g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH) ||
909     g_variant_is_of_type (value, G_VARIANT_TYPE_SIGNATURE), NULL);
910   gconstpointer data = g_variant_get_data (value);
911   gsize size = g_variant_get_size (value);
912
913   if (!g_variant_is_trusted (value))
914     {
915       switch (g_variant_classify (value))
916         {
917         case G_VARIANT_CLASS_STRING:
918           if (g_variant_serialiser_is_string (data, size))
919             break;
920
921           data = "";
922           size = 1;
923           break;
924
925         case G_VARIANT_CLASS_OBJECT_PATH:
926           if (g_variant_serialiser_is_object_path (data, size))
927             break;
928
929           data = "/";
930           size = 2;
931           break;
932
933         case G_VARIANT_CLASS_SIGNATURE:
934           if (g_variant_serialiser_is_signature (data, size))
935             break;
936
937           data = "";
938           size = 1;
939           break;
940
941         default:
942           g_assert_not_reached ();
943         }
944     }
945
946   if (length)
947     *length = size - 1;
948
949   return data;
950 }
951
952 /**
953  * g_variant_dup_string:
954  * @value: a string #GVariant instance
955  * @length: a pointer to a #gsize, to store the length
956  * @returns: a newly allocated string
957  *
958  * Similar to g_variant_get_string() except that instead of returning
959  * a constant string, the string is duplicated.
960  *
961  * The return value must be freed using g_free().
962  *
963  * Since: 2.24
964  **/
965 gchar *
966 g_variant_dup_string (GVariant *value,
967                       gsize    *length)
968 {
969   return g_strdup (g_variant_get_string (value, length));
970 }
971
972 /**
973  * g_variant_new_strv:
974  * @strv: an array of strings
975  * @length: the length of @strv, or -1
976  * @returns: a new floating #GVariant instance
977  *
978  * Constructs an array of strings #GVariant from the given array of
979  * strings.
980  *
981  * If @length is not -1 then it gives the maximum length of @strv.  In
982  * any case, a %NULL pointer in @strv is taken as a terminator.
983  *
984  * Since: 2.24
985  **/
986 GVariant *
987 g_variant_new_strv (const gchar * const *strv,
988                     gssize               length)
989 {
990   GVariant **strings;
991   gsize i;
992
993   g_return_val_if_fail (length == 0 || strv != NULL, NULL);
994
995   if (length < 0)
996     for (length = 0; strv[length]; length++);
997
998   strings = g_new (GVariant *, length);
999   for (i = 0; i < length; i++)
1000     strings[i] = g_variant_new_string (strv[i]);
1001
1002   return g_variant_new_from_children (G_VARIANT_TYPE ("as"),
1003                                       strings, length, TRUE);
1004 }
1005
1006 /**
1007  * g_variant_get_strv:
1008  * @value: an array of strings #GVariant
1009  * @length: the length of the result, or %NULL
1010  * @returns: an array of constant strings
1011  *
1012  * Gets the contents of an array of strings #GVariant.  This call
1013  * makes a shallow copy; the return result should be released with
1014  * g_free(), but the individual strings must not be modified.
1015  *
1016  * If @length is non-%NULL then the number of elements in the result
1017  * is stored there.  In any case, the resulting array will be
1018  * %NULL-terminated.
1019  *
1020  * For an empty array, @length will be set to 0 and a pointer to a
1021  * %NULL pointer will be returned.
1022  *
1023  * Since: 2.24
1024  **/
1025 const gchar **
1026 g_variant_get_strv (GVariant *value,
1027                     gsize    *length)
1028 {
1029   const gchar **strv;
1030   gsize n;
1031   gsize i;
1032
1033   TYPE_CHECK (value, G_VARIANT_TYPE ("as"), NULL);
1034
1035   g_variant_get_data (value);
1036   n = g_variant_n_children (value);
1037   strv = g_new (const gchar *, n + 1);
1038
1039   for (i = 0; i < n; i++)
1040     {
1041       GVariant *string;
1042
1043       string = g_variant_get_child_value (value, i);
1044       strv[i] = g_variant_get_string (string, NULL);
1045       g_variant_unref (string);
1046     }
1047   strv[i] = NULL;
1048
1049   if (length)
1050     *length = n;
1051
1052   return strv;
1053 }
1054
1055 /**
1056  * g_variant_dup_strv:
1057  * @value: an array of strings #GVariant
1058  * @length: the length of the result, or %NULL
1059  * @returns: an array of constant strings
1060  *
1061  * Gets the contents of an array of strings #GVariant.  This call
1062  * makes a deep copy; the return result should be released with
1063  * g_strfreev().
1064  *
1065  * If @length is non-%NULL then the number of elements in the result
1066  * is stored there.  In any case, the resulting array will be
1067  * %NULL-terminated.
1068  *
1069  * For an empty array, @length will be set to 0 and a pointer to a
1070  * %NULL pointer will be returned.
1071  *
1072  * Since: 2.24
1073  **/
1074 gchar **
1075 g_variant_dup_strv (GVariant *value,
1076                     gsize    *length)
1077 {
1078   gchar **strv;
1079   gsize n;
1080   gsize i;
1081
1082   TYPE_CHECK (value, G_VARIANT_TYPE ("as"), NULL);
1083
1084   n = g_variant_n_children (value);
1085   strv = g_new (gchar *, n + 1);
1086
1087   for (i = 0; i < n; i++)
1088     {
1089       GVariant *string;
1090
1091       string = g_variant_get_child_value (value, i);
1092       strv[i] = g_variant_dup_string (string, NULL);
1093       g_variant_unref (string);
1094     }
1095   strv[i] = NULL;
1096
1097   if (length)
1098     *length = n;
1099
1100   return strv;
1101 }
1102
1103 /**
1104  * g_variant_new_array:
1105  * @child_type: the element type of the new array
1106  * @children: an array of #GVariant pointers, the children
1107  * @n_children: the length of @children
1108  * @returns: a new #GVariant array
1109  *
1110  * Creates a new #GVariant array from @children.
1111  *
1112  * @child_type must be non-%NULL if @n_children is zero.  Otherwise, the
1113  * child type is determined by inspecting the first element of the
1114  * @children array.  If @child_type is non-%NULL then it must be a
1115  * definite type.
1116  *
1117  * The items of the array are taken from the @children array.  No entry
1118  * in the @children array may be %NULL.
1119  *
1120  * All items in the array must have the same type, which must be the
1121  * same as @child_type, if given.
1122  *
1123  * Since: 2.24
1124  **/
1125 GVariant *
1126 g_variant_new_array (const GVariantType *child_type,
1127                      GVariant * const   *children,
1128                      gsize               n_children)
1129 {
1130   GVariantType *array_type;
1131   GVariant **my_children;
1132   gboolean trusted;
1133   GVariant *value;
1134   gsize i;
1135
1136   g_return_val_if_fail (n_children > 0 || child_type != NULL, NULL);
1137   g_return_val_if_fail (n_children == 0 || children != NULL, NULL);
1138   g_return_val_if_fail (child_type == NULL ||
1139                         g_variant_type_is_definite (child_type), NULL);
1140
1141   my_children = g_new (GVariant *, n_children);
1142   trusted = TRUE;
1143
1144   if (child_type == NULL)
1145     child_type = g_variant_get_type (children[0]);
1146   array_type = g_variant_type_new_array (child_type);
1147
1148   for (i = 0; i < n_children; i++)
1149     {
1150       TYPE_CHECK (children[i], child_type, NULL);
1151       my_children[i] = g_variant_ref_sink (children[i]);
1152       trusted &= g_variant_is_trusted (children[i]);
1153     }
1154
1155   value = g_variant_new_from_children (array_type, my_children,
1156                                        n_children, trusted);
1157   g_variant_type_free (array_type);
1158
1159   return value;
1160 }
1161
1162 /**
1163  * g_variant_new_tuple:
1164  * @children: the items to make the tuple out of
1165  * @n_children: the length of @children
1166  * @returns: a new #GVariant tuple
1167  *
1168  * Creates a new tuple #GVariant out of the items in @children.  The
1169  * type is determined from the types of @children.  No entry in the
1170  * @children array may be %NULL.
1171  *
1172  * If @n_children is 0 then the unit tuple is constructed.
1173  *
1174  * Since: 2.24
1175  **/
1176 GVariant *
1177 g_variant_new_tuple (GVariant * const *children,
1178                      gsize             n_children)
1179 {
1180   const GVariantType **types;
1181   GVariantType *tuple_type;
1182   GVariant **my_children;
1183   gboolean trusted;
1184   GVariant *value;
1185   gsize i;
1186
1187   g_return_val_if_fail (n_children == 0 || children != NULL, NULL);
1188
1189   types = g_new (const GVariantType *, n_children);
1190   my_children = g_new (GVariant *, n_children);
1191   trusted = TRUE;
1192
1193   for (i = 0; i < n_children; i++)
1194     {
1195       types[i] = g_variant_get_type (children[i]);
1196       my_children[i] = g_variant_ref_sink (children[i]);
1197       trusted &= g_variant_is_trusted (children[i]);
1198     }
1199
1200   tuple_type = g_variant_type_new_tuple (types, n_children);
1201   value = g_variant_new_from_children (tuple_type, my_children,
1202                                        n_children, trusted);
1203   g_variant_type_free (tuple_type);
1204   g_free (types);
1205
1206   return value;
1207 }
1208
1209 /**
1210  * g_variant_new_dict_entry:
1211  * @key: a basic #GVariant, the key
1212  * @value: a #GVariant, the value
1213  * @returns: a new dictionary entry #GVariant
1214  *
1215  * Creates a new dictionary entry #GVariant.  @key and @value must be
1216  * non-%NULL.
1217  *
1218  * @key must be a value of a basic type (ie: not a container).
1219  *
1220  * Since: 2.24
1221  **/
1222 GVariant *
1223 g_variant_new_dict_entry (GVariant *key,
1224                           GVariant *value)
1225 {
1226   GVariantType *dict_type;
1227   GVariant **children;
1228   gboolean trusted;
1229
1230   g_return_val_if_fail (key != NULL && value != NULL, NULL);
1231   g_return_val_if_fail (!g_variant_is_container (key), NULL);
1232
1233   children = g_new (GVariant *, 2);
1234   children[0] = g_variant_ref_sink (key);
1235   children[1] = g_variant_ref_sink (value);
1236   trusted = g_variant_is_trusted (key) && g_variant_is_trusted (value);
1237
1238   dict_type = g_variant_type_new_dict_entry (g_variant_get_type (key),
1239                                              g_variant_get_type (value));
1240   value = g_variant_new_from_children (dict_type, children, 2, trusted);
1241   g_variant_type_free (dict_type);
1242
1243   return value;
1244 }
1245
1246 /**
1247  * g_variant_get_fixed_array:
1248  * @value: a #GVariant array with fixed-sized elements
1249  * @n_elements: a pointer to the location to store the number of items
1250  * @element_size: the size of each element
1251  * @returns: a pointer to the fixed array
1252  *
1253  * Provides access to the serialised data for an array of fixed-sized
1254  * items.
1255  *
1256  * @value must be an array with fixed-sized elements.  Numeric types are
1257  * fixed-size as are tuples containing only other fixed-sized types.
1258  *
1259  * @element_size must be the size of a single element in the array.  For
1260  * example, if calling this function for an array of 32 bit integers,
1261  * you might say <code>sizeof (gint32)</code>.  This value isn't used
1262  * except for the purpose of a double-check that the form of the
1263  * seralised data matches the caller's expectation.
1264  *
1265  * @n_elements, which must be non-%NULL is set equal to the number of
1266  * items in the array.
1267  *
1268  * Since: 2.24
1269  **/
1270 gconstpointer
1271 g_variant_get_fixed_array (GVariant *value,
1272                            gsize    *n_elements,
1273                            gsize     element_size)
1274 {
1275   GVariantTypeInfo *array_info;
1276   gsize array_element_size;
1277   gconstpointer data;
1278   gsize size;
1279
1280   TYPE_CHECK (value, G_VARIANT_TYPE_ARRAY, NULL);
1281
1282   g_return_val_if_fail (n_elements != NULL, NULL);
1283   g_return_val_if_fail (element_size > 0, NULL);
1284
1285   array_info = g_variant_get_type_info (value);
1286   g_variant_type_info_query_element (array_info, NULL, &array_element_size);
1287
1288   g_return_val_if_fail (array_element_size, NULL);
1289
1290   if G_UNLIKELY (array_element_size != element_size)
1291     {
1292       if (array_element_size)
1293         g_critical ("g_variant_get_fixed_array: assertion "
1294                     "`g_variant_array_has_fixed_size (value, element_size)' "
1295                     "failed: array size %"G_GSIZE_FORMAT" does not match "
1296                     "given element_size %"G_GSIZE_FORMAT".",
1297                     array_element_size, element_size);
1298       else
1299         g_critical ("g_variant_get_fixed_array: assertion "
1300                     "`g_variant_array_has_fixed_size (value, element_size)' "
1301                     "failed: array does not have fixed size.");
1302     }
1303
1304   data = g_variant_get_data (value);
1305   size = g_variant_get_size (value);
1306
1307   if (size % element_size)
1308     *n_elements = 0;
1309   else
1310     *n_elements = size / element_size;
1311
1312   if (*n_elements)
1313     return data;
1314
1315   return NULL;
1316 }
1317
1318 /**
1319  * g_variant_classify:
1320  * @value: a #GVariant
1321  * @returns: the #GVariantClass of @value
1322  *
1323  * Classifies @value according to its top-level type.
1324  *
1325  * Since: 2.24
1326  **/
1327 /**
1328  * GVariantClass:
1329  * @G_VARIANT_CLASS_BOOLEAN: The #GVariant is a boolean.
1330  * @G_VARIANT_CLASS_BYTE: The #GVariant is a byte.
1331  * @G_VARIANT_CLASS_INT16: The #GVariant is a signed 16 bit integer.
1332  * @G_VARIANT_CLASS_UINT16: The #GVariant is an unsigned 16 bit integer.
1333  * @G_VARIANT_CLASS_INT32: The #GVariant is a signed 32 bit integer.
1334  * @G_VARIANT_CLASS_UINT32: The #GVariant is an unsigned 32 bit integer.
1335  * @G_VARIANT_CLASS_INT64: The #GVariant is a signed 64 bit integer.
1336  * @G_VARIANT_CLASS_UINT64: The #GVariant is an unsigned 64 bit integer.
1337  * @G_VARIANT_CLASS_HANDLE: The #GVariant is a file handle index.
1338  * @G_VARIANT_CLASS_DOUBLE: The #GVariant is a double precision floating 
1339  *                          point value.
1340  * @G_VARIANT_CLASS_STRING: The #GVariant is a normal string.
1341  * @G_VARIANT_CLASS_OBJECT_PATH: The #GVariant is a DBus object path 
1342  *                               string.
1343  * @G_VARIANT_CLASS_SIGNATURE: The #GVariant is a DBus signature string.
1344  * @G_VARIANT_CLASS_VARIANT: The #GVariant is a variant.
1345  * @G_VARIANT_CLASS_MAYBE: The #GVariant is a maybe-typed value.
1346  * @G_VARIANT_CLASS_ARRAY: The #GVariant is an array.
1347  * @G_VARIANT_CLASS_TUPLE: The #GVariant is a tuple.
1348  * @G_VARIANT_CLASS_DICT_ENTRY: The #GVariant is a dictionary entry.
1349  *
1350  * The range of possible top-level types of #GVariant instances.
1351  *
1352  * Since: 2.24
1353  **/
1354 GVariantClass
1355 g_variant_classify (GVariant *value)
1356 {
1357   g_return_val_if_fail (value != NULL, 0);
1358
1359   return *g_variant_get_type_string (value);
1360 }
1361
1362 /**
1363  * g_variant_print_string:
1364  * @value: a #GVariant
1365  * @string: a #GString, or %NULL
1366  * @type_annotate: %TRUE if type information should be included in
1367  *                 the output
1368  * @returns: a #GString containing the string
1369  *
1370  * Behaves as g_variant_print(), but operates on a #GString.
1371  *
1372  * If @string is non-%NULL then it is appended to and returned.  Else,
1373  * a new empty #GString is allocated and it is returned.
1374  *
1375  * Since: 2.24
1376  **/
1377 GString *
1378 g_variant_print_string (GVariant *value,
1379                         GString  *string,
1380                         gboolean  type_annotate)
1381 {
1382   if G_UNLIKELY (string == NULL)
1383     string = g_string_new (NULL);
1384
1385   switch (g_variant_classify (value))
1386     {
1387     case G_VARIANT_CLASS_MAYBE:
1388       if (type_annotate)
1389         g_string_append_printf (string, "@%s ",
1390                                 g_variant_get_type_string (value));
1391
1392       if (g_variant_n_children (value))
1393         {
1394           gchar *printed_child;
1395           GVariant *element;
1396
1397           /* Nested maybes:
1398            *
1399            * Consider the case of the type "mmi".  In this case we could
1400            * write "Just Just 4", but "4" alone is totally unambiguous,
1401            * so we try to drop "Just" where possible.
1402            *
1403            * We have to be careful not to always drop "Just", though,
1404            * since "Nothing" needs to be distinguishable from "Just
1405            * Nothing".  The case where we need to ensure we keep the
1406            * "Just" is actually exactly the case where we have a nested
1407            * Nothing.
1408            *
1409            * Instead of searching for that nested Nothing, we just print
1410            * the contained value into a separate string and see if we
1411            * end up with "Nothing" at the end of it.  If so, we need to
1412            * add "Just" at our level.
1413            */
1414           element = g_variant_get_child_value (value, 0);
1415           printed_child = g_variant_print (element, FALSE);
1416           g_variant_unref (element);
1417
1418           if (g_str_has_suffix (printed_child, "Nothing"))
1419             g_string_append (string, "Just ");
1420           g_string_append (string, printed_child);
1421           g_free (printed_child);
1422         }
1423       else
1424         g_string_append (string, "Nothing");
1425
1426       break;
1427
1428     case G_VARIANT_CLASS_ARRAY:
1429       /* it's an array so the first character of the type string is 'a'
1430        *
1431        * if the first two characters are 'a{' then it's an array of
1432        * dictionary entries (ie: a dictionary) so we print that
1433        * differently.
1434        */
1435       if (g_variant_get_type_string (value)[1] == '{')
1436         /* dictionary */
1437         {
1438           const gchar *comma = "";
1439           gsize n, i;
1440
1441           if ((n = g_variant_n_children (value)) == 0)
1442             {
1443               if (type_annotate)
1444                 g_string_append_printf (string, "@%s ",
1445                                         g_variant_get_type_string (value));
1446               g_string_append (string, "{}");
1447               break;
1448             }
1449
1450           g_string_append_c (string, '{');
1451           for (i = 0; i < n; i++)
1452             {
1453               GVariant *entry, *key, *val;
1454
1455               g_string_append (string, comma);
1456               comma = ", ";
1457
1458               entry = g_variant_get_child_value (value, i);
1459               key = g_variant_get_child_value (entry, 0);
1460               val = g_variant_get_child_value (entry, 1);
1461               g_variant_unref (entry);
1462
1463               g_variant_print_string (key, string, type_annotate);
1464               g_variant_unref (key);
1465               g_string_append (string, ": ");
1466               g_variant_print_string (val, string, type_annotate);
1467               g_variant_unref (val);
1468               type_annotate = FALSE;
1469             }
1470           g_string_append_c (string, '}');
1471         }
1472       else
1473         /* normal (non-dictionary) array */
1474         {
1475           const gchar *comma = "";
1476           gsize n, i;
1477
1478           if ((n = g_variant_n_children (value)) == 0)
1479             {
1480               if (type_annotate)
1481                 g_string_append_printf (string, "@%s ",
1482                                         g_variant_get_type_string (value));
1483               g_string_append (string, "[]");
1484               break;
1485             }
1486
1487           g_string_append_c (string, '[');
1488           for (i = 0; i < n; i++)
1489             {
1490               GVariant *element;
1491
1492               g_string_append (string, comma);
1493               comma = ", ";
1494
1495               element = g_variant_get_child_value (value, i);
1496
1497               g_variant_print_string (element, string, type_annotate);
1498               g_variant_unref (element);
1499               type_annotate = FALSE;
1500             }
1501           g_string_append_c (string, ']');
1502         }
1503
1504       break;
1505
1506     case G_VARIANT_CLASS_TUPLE:
1507       {
1508         gsize n, i;
1509
1510         n = g_variant_n_children (value);
1511
1512         g_string_append_c (string, '(');
1513         for (i = 0; i < n; i++)
1514           {
1515             GVariant *element;
1516
1517             element = g_variant_get_child_value (value, i);
1518             g_variant_print_string (element, string, type_annotate);
1519             g_string_append (string, ", ");
1520           }
1521
1522         /* for >1 item:  remove final ", "
1523          * for 1 item:   remove final " ", but leave the ","
1524          * for 0 items:  there is only "(", so remove nothing
1525          */
1526         g_string_truncate (string, string->len - (n > 0) - (n > 1));
1527         g_string_append_c (string, ')');
1528       }
1529       break;
1530
1531     case G_VARIANT_CLASS_DICT_ENTRY:
1532       {
1533         GVariant *element;
1534
1535         g_string_append_c (string, '{');
1536
1537         element = g_variant_get_child_value (value, 0);
1538         g_variant_print_string (element, string, type_annotate);
1539         g_variant_unref (element);
1540
1541         g_string_append (string, ", ");
1542
1543         element = g_variant_get_child_value (value, 1);
1544         g_variant_print_string (element, string, type_annotate);
1545         g_variant_unref (element);
1546
1547         g_string_append_c (string, '}');
1548       }
1549       break;
1550
1551     case G_VARIANT_CLASS_VARIANT:
1552       {
1553         GVariant *child = g_variant_get_variant (value);
1554
1555         /* Always annotate types in nested variants, because they are
1556          * (by nature) of variable type.
1557          */
1558         g_string_append_c (string, '<');
1559         g_variant_print_string (child, string, TRUE);
1560         g_string_append_c (string, '>');
1561
1562         g_variant_unref (child);
1563       }
1564       break;
1565
1566     case G_VARIANT_CLASS_BOOLEAN:
1567       if (g_variant_get_boolean (value))
1568         g_string_append (string, "true");
1569       else
1570         g_string_append (string, "false");
1571       break;
1572
1573     case G_VARIANT_CLASS_STRING:
1574       {
1575         const gchar *str = g_variant_get_string (value, NULL);
1576         gchar *escaped = g_strescape (str, NULL);
1577
1578         g_string_append_printf (string, "\"%s\"", escaped);
1579
1580         g_free (escaped);
1581       }
1582       break;
1583
1584     case G_VARIANT_CLASS_BYTE:
1585       if (type_annotate)
1586         g_string_append (string, "byte ");
1587       g_string_append_printf (string, "0x%02x",
1588                               g_variant_get_byte (value));
1589       break;
1590
1591     case G_VARIANT_CLASS_INT16:
1592       if (type_annotate)
1593         g_string_append (string, "int16 ");
1594       g_string_append_printf (string, "%"G_GINT16_FORMAT,
1595                               g_variant_get_int16 (value));
1596       break;
1597
1598     case G_VARIANT_CLASS_UINT16:
1599       if (type_annotate)
1600         g_string_append (string, "uint16 ");
1601       g_string_append_printf (string, "%"G_GUINT16_FORMAT,
1602                               g_variant_get_uint16 (value));
1603       break;
1604
1605     case G_VARIANT_CLASS_INT32:
1606       /* Never annotate this type because it is the default for numbers
1607        * (and this is a *pretty* printer)
1608        */
1609       g_string_append_printf (string, "%"G_GINT32_FORMAT,
1610                               g_variant_get_int32 (value));
1611       break;
1612
1613     case G_VARIANT_CLASS_HANDLE:
1614       if (type_annotate)
1615         g_string_append (string, "handle ");
1616       g_string_append_printf (string, "%"G_GINT32_FORMAT,
1617                               g_variant_get_handle (value));
1618       break;
1619
1620     case G_VARIANT_CLASS_UINT32:
1621       if (type_annotate)
1622         g_string_append (string, "uint32 ");
1623       g_string_append_printf (string, "%"G_GUINT32_FORMAT,
1624                               g_variant_get_uint32 (value));
1625       break;
1626
1627     case G_VARIANT_CLASS_INT64:
1628       if (type_annotate)
1629         g_string_append (string, "int64 ");
1630       g_string_append_printf (string, "%"G_GINT64_FORMAT,
1631                               g_variant_get_int64 (value));
1632       break;
1633
1634     case G_VARIANT_CLASS_UINT64:
1635       if (type_annotate)
1636         g_string_append (string, "uint64 ");
1637       g_string_append_printf (string, "%"G_GUINT64_FORMAT,
1638                               g_variant_get_uint64 (value));
1639       break;
1640
1641     case G_VARIANT_CLASS_DOUBLE:
1642       {
1643         gchar buffer[100];
1644         gint i;
1645
1646         g_ascii_dtostr (buffer, sizeof buffer, g_variant_get_double (value));
1647
1648         for (i = 0; buffer[i]; i++)
1649           if (buffer[i] == '.' || buffer[i] == 'e' ||
1650               buffer[i] == 'n' || buffer[i] == 'N')
1651             break;
1652
1653         /* if there is no '.' or 'e' in the float then add one */
1654         if (buffer[i] == '\0')
1655           {
1656             buffer[i++] = '.';
1657             buffer[i++] = '0';
1658             buffer[i++] = '\0';
1659           }
1660
1661         g_string_append (string, buffer);
1662       }
1663       break;
1664
1665     case G_VARIANT_CLASS_OBJECT_PATH:
1666       if (type_annotate)
1667         g_string_append (string, "objectpath ");
1668       g_string_append_printf (string, "\"%s\"",
1669                               g_variant_get_string (value, NULL));
1670       break;
1671
1672     case G_VARIANT_CLASS_SIGNATURE:
1673       if (type_annotate)
1674         g_string_append (string, "signature ");
1675       g_string_append_printf (string, "\"%s\"",
1676                               g_variant_get_string (value, NULL));
1677       break;
1678
1679     default:
1680       g_assert_not_reached ();
1681   }
1682
1683   return string;
1684 }
1685
1686 /**
1687  * g_variant_print:
1688  * @value: a #GVariant
1689  * @type_annotate: %TRUE if type information should be included in
1690  *                 the output
1691  * @returns: a newly-allocated string holding the result.
1692  *
1693  * Pretty-prints @value in the format understood by g_variant_parse().
1694  *
1695  * If @type_annotate is %TRUE, then type information is included in
1696  * the output.
1697  */
1698 gchar *
1699 g_variant_print (GVariant *value,
1700                  gboolean  type_annotate)
1701 {
1702   return g_string_free (g_variant_print_string (value, NULL, type_annotate),
1703                         FALSE);
1704 };
1705
1706 /**
1707  * g_variant_hash:
1708  * @value: a basic #GVariant value as a #gconstpointer
1709  * @returns: a hash value corresponding to @value
1710  *
1711  * Generates a hash value for a #GVariant instance.
1712  *
1713  * The output of this function is guaranteed to be the same for a given
1714  * value only per-process.  It may change between different processor
1715  * architectures or even different versions of GLib.  Do not use this
1716  * function as a basis for building protocols or file formats.
1717  *
1718  * The type of @value is #gconstpointer only to allow use of this
1719  * function with #GHashTable.  @value must be a #GVariant.
1720  *
1721  * Since: 2.24
1722  **/
1723 guint
1724 g_variant_hash (gconstpointer value_)
1725 {
1726   GVariant *value = (GVariant *) value_;
1727
1728   switch (g_variant_classify (value))
1729     {
1730     case G_VARIANT_CLASS_STRING:
1731     case G_VARIANT_CLASS_OBJECT_PATH:
1732     case G_VARIANT_CLASS_SIGNATURE:
1733       return g_str_hash (g_variant_get_string (value, NULL));
1734
1735     case G_VARIANT_CLASS_BOOLEAN:
1736       /* this is a very odd thing to hash... */
1737       return g_variant_get_boolean (value);
1738
1739     case G_VARIANT_CLASS_BYTE:
1740       return g_variant_get_byte (value);
1741
1742     case G_VARIANT_CLASS_INT16:
1743     case G_VARIANT_CLASS_UINT16:
1744       {
1745         const guint16 *ptr;
1746
1747         ptr = g_variant_get_data (value);
1748
1749         if (ptr)
1750           return *ptr;
1751         else
1752           return 0;
1753       }
1754
1755     case G_VARIANT_CLASS_INT32:
1756     case G_VARIANT_CLASS_UINT32:
1757     case G_VARIANT_CLASS_HANDLE:
1758       {
1759         const guint *ptr;
1760
1761         ptr = g_variant_get_data (value);
1762
1763         if (ptr)
1764           return *ptr;
1765         else
1766           return 0;
1767       }
1768
1769     case G_VARIANT_CLASS_INT64:
1770     case G_VARIANT_CLASS_UINT64:
1771     case G_VARIANT_CLASS_DOUBLE:
1772       /* need a separate case for these guys because otherwise
1773        * performance could be quite bad on big endian systems
1774        */
1775       {
1776         const guint *ptr;
1777
1778         ptr = g_variant_get_data (value);
1779
1780         if (ptr)
1781           return ptr[0] + ptr[1];
1782         else
1783           return 0;
1784       }
1785
1786     default:
1787       g_return_val_if_fail (!g_variant_is_container (value), 0);
1788       g_assert_not_reached ();
1789     }
1790 }
1791
1792 /**
1793  * g_variant_equal:
1794  * @one: a #GVariant instance
1795  * @two: a #GVariant instance
1796  * @returns: %TRUE if @one and @two are equal
1797  *
1798  * Checks if @one and @two have the same type and value.
1799  *
1800  * The types of @one and @two are #gconstpointer only to allow use of
1801  * this function with #GHashTable.  They must each be a #GVariant.
1802  *
1803  * Since: 2.24
1804  **/
1805 gboolean
1806 g_variant_equal (gconstpointer one,
1807                  gconstpointer two)
1808 {
1809   gboolean equal;
1810
1811   g_return_val_if_fail (one != NULL && two != NULL, FALSE);
1812
1813   if (g_variant_get_type_info ((GVariant *) one) !=
1814       g_variant_get_type_info ((GVariant *) two))
1815     return FALSE;
1816
1817   /* if both values are trusted to be in their canonical serialised form
1818    * then a simple memcmp() of their serialised data will answer the
1819    * question.
1820    *
1821    * if not, then this might generate a false negative (since it is
1822    * possible for two different byte sequences to represent the same
1823    * value).  for now we solve this by pretty-printing both values and
1824    * comparing the result.
1825    */
1826   if (g_variant_is_trusted ((GVariant *) one) &&
1827       g_variant_is_trusted ((GVariant *) two))
1828     {
1829       gconstpointer data_one, data_two;
1830       gsize size_one, size_two;
1831
1832       size_one = g_variant_get_size ((GVariant *) one);
1833       size_two = g_variant_get_size ((GVariant *) two);
1834
1835       if (size_one != size_two)
1836         return FALSE;
1837
1838       data_one = g_variant_get_data ((GVariant *) one);
1839       data_two = g_variant_get_data ((GVariant *) two);
1840
1841       equal = memcmp (data_one, data_two, size_one) == 0;
1842     }
1843   else
1844     {
1845       gchar *strone, *strtwo;
1846
1847       strone = g_variant_print ((GVariant *) one, FALSE);
1848       strtwo = g_variant_print ((GVariant *) two, FALSE);
1849       equal = strcmp (strone, strtwo);
1850       g_free (strone);
1851       g_free (strtwo);
1852     }
1853
1854   return equal;
1855 }
1856
1857 #define __G_VARIANT_C__
1858 #include "galiasdef.c"