8a773df5fb134e4c3e6e0bb272cfaaef88246895
[platform/upstream/glib.git] / glib / gvarianttype.c
1 /*
2  * Copyright © 2007, 2008 Ryan Lortie
3  * Copyright © 2009, 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 "gvarianttype.h"
24
25 #include <glib/gtestutils.h>
26
27 #include <string.h>
28
29 #include "galias.h"
30
31 /**
32  * SECTION: gvarianttype
33  * @title: GVariantType
34  * @short_description: introduction to the GVariant type system
35  * @see_also: #GVariantType, #GVariant
36  *
37  * This section introduces the GVariant type system.  It is based, in
38  * large part, on the DBus type system, with two major changes and some minor
39  * lifting of restrictions.  The <ulink
40  * url='http://dbus.freedesktop.org/doc/dbus-specification.html'>DBus
41  * specification</ulink>, therefore, provides a significant amount of
42  * information that is useful when working with GVariant.
43  *
44  * The first major change with respect to the DBus type system is the
45  * introduction of maybe (or "nullable") types.  Any type in GVariant can be
46  * converted to a maybe type, in which case, "nothing" (or "null") becomes a
47  * valid value.  Maybe types have been added by introducing the
48  * character "<literal>m</literal>" to type strings.
49  *
50  * The second major change is that the GVariant type system supports the
51  * concept of "indefinite types" -- types that are less specific than
52  * the normal types found in DBus.  For example, it is possible to speak
53  * of "an array of any type" in GVariant, where the DBus type system
54  * would require you to speak of "an array of integers" or "an array of
55  * strings".  Indefinite types have been added by introducing the
56  * characters "<literal>*</literal>", "<literal>?</literal>" and
57  * "<literal>r</literal>" to type strings.
58  *
59  * Finally, all arbitrary restrictions relating to the complexity of
60  * types are lifted along with the restriction that dictionary entries
61  * may only appear nested inside of arrays.
62  *
63  * Just as in DBus, GVariant types are described with strings ("type
64  * strings").  Subject to the differences mentioned above, these strings
65  * are of the same form as those found in DBus.  Note, however: DBus
66  * always works in terms of messages and therefore individual type
67  * strings appear nowhere in its interface.  Instead, "signatures"
68  * are a concatenation of the strings of the type of each argument in a
69  * message.  GVariant deals with single values directly so GVariant type
70  * strings always describe the type of exactly one value.  This means
71  * that a DBus signature string is generally not a valid GVariant type
72  * string -- except in the case that it is the signature of a message
73  * containing exactly one argument.
74  *
75  * An indefinite type is similar in spirit to what may be called an
76  * abstract type in other type systems.  No value can exist that has an
77  * indefinite type as its type, but values can exist that have types
78  * that are subtypes of indefinite types.  That is to say,
79  * g_variant_get_type() will never return an indefinite type, but
80  * calling g_variant_is_a() with an indefinite type may return %TRUE.
81  * For example, you can not have a value that represents "an array of no
82  * particular type", but you can have an "array of integers" which
83  * certainly matches the type of "an array of no particular type", since
84  * "array of integers" is a subtype of "array of no particular type".
85  *
86  * This is similar to how instances of abstract classes may not
87  * directly exist in other type systems, but instances of their
88  * non-abstract subtypes may.  For example, in GTK, no object that has
89  * the type of #GtkBin can exist (since #GtkBin is an abstract class),
90  * but a #GtkWindow can certainly be instantiated, and you would say
91  * that the #GtkWindow is a #GtkBin (since #GtkWindow is a subclass of
92  * #GtkBin).
93  *
94  * A detailed description of GVariant type strings is given here:
95  *
96  * <refsect2 id='gvariant-typestrings'>
97  *  <title>GVariant Type Strings</title>
98  *  <para>
99  *   A GVariant type string can be any of the following:
100  *  </para>
101  *  <itemizedlist>
102  *   <listitem>
103  *    <para>
104  *     any basic type string (listed below)
105  *    </para>
106  *   </listitem>
107  *   <listitem>
108  *    <para>
109  *     "<literal>v</literal>", "<literal>r</literal>" or
110  *     "<literal>*</literal>"
111  *    </para>
112  *   </listitem>
113  *   <listitem>
114  *    <para>
115  *     one of the characters '<literal>a</literal>' or
116  *     '<literal>m</literal>', followed by another type string
117  *    </para>
118  *   </listitem>
119  *   <listitem>
120  *    <para>
121  *     the character '<literal>(</literal>', followed by a concatenation
122  *     of zero or more other type strings, followed by the character
123  *     '<literal>)</literal>'
124  *    </para>
125  *   </listitem>
126  *   <listitem>
127  *    <para>
128  *     the character '<literal>{</literal>', followed by a basic type
129  *     string (see below), followed by another type string, followed by
130  *     the character '<literal>}</literal>'
131  *    </para>
132  *   </listitem>
133  *  </itemizedlist>
134  *  <para>
135  *   A basic type string describes a basic type (as per
136  *   g_variant_type_is_basic()) and is always a single
137  *   character in length.  The valid basic type strings are
138  *   "<literal>b</literal>", "<literal>y</literal>",
139  *   "<literal>n</literal>", "<literal>q</literal>",
140  *   "<literal>i</literal>", "<literal>u</literal>",
141  *   "<literal>x</literal>", "<literal>t</literal>",
142  *   "<literal>h</literal>", "<literal>d</literal>",
143  *   "<literal>s</literal>", "<literal>o</literal>",
144  *   "<literal>g</literal>" and "<literal>?</literal>".
145  *  </para>
146  *  <para>
147  *   The above definition is recursive to arbitrary depth.
148  *   "<literal>aaaaai</literal>" and "<literal>(ui(nq((y)))s)</literal>"
149  *   are both valid type strings, as is
150  *   "<literal>a(aa(ui)(qna{ya(yd)}))</literal>".
151  *  </para>
152  *  <para>
153  *   The meaning of each of the characters is as follows:
154  *  </para>
155  *  <informaltable>
156  *   <tgroup cols='2'>
157  *    <tbody>
158  *     <row>
159  *      <entry>
160  *       <para>
161  *        <emphasis role='strong'>Character</emphasis>
162  *       </para>
163  *      </entry>
164  *      <entry>
165  *       <para>
166  *        <emphasis role='strong'>Meaning</emphasis>
167  *       </para>
168  *      </entry>
169  *     </row>
170  *     <row>
171  *      <entry>
172  *       <para>
173  *        <literal>b</literal>
174  *       </para>
175  *      </entry>
176  *      <entry>
177  *       <para>
178  *        the type string of %G_VARIANT_TYPE_BOOLEAN; a boolean value.
179  *       </para>
180  *      </entry>
181  *     </row>
182  *     <row>
183  *      <entry>
184  *       <para>
185  *        <literal>y</literal>
186  *       </para>
187  *      </entry>
188  *      <entry>
189  *       <para>
190  *        the type string of %G_VARIANT_TYPE_BYTE; a byte.
191  *       </para>
192  *      </entry>
193  *     </row>
194  *     <row>
195  *      <entry>
196  *       <para>
197  *        <literal>n</literal>
198  *       </para>
199  *      </entry>
200  *      <entry>
201  *       <para>
202  *        the type string of %G_VARIANT_TYPE_INT16; a signed 16 bit
203  *        integer.
204  *       </para>
205  *      </entry>
206  *     </row>
207  *     <row>
208  *      <entry>
209  *       <para>
210  *        <literal>q</literal>
211  *       </para>
212  *      </entry>
213  *      <entry>
214  *       <para>
215  *        the type string of %G_VARIANT_TYPE_UINT16; an unsigned 16 bit
216  *        integer.
217  *       </para>
218  *      </entry>
219  *     </row>
220  *     <row>
221  *      <entry>
222  *       <para>
223  *        <literal>i</literal>
224  *       </para>
225  *      </entry>
226  *      <entry>
227  *       <para>
228  *        the type string of %G_VARIANT_TYPE_INT32; a signed 32 bit
229  *        integer.
230  *       </para>
231  *      </entry>
232  *     </row>
233  *     <row>
234  *      <entry>
235  *       <para>
236  *        <literal>u</literal>
237  *       </para>
238  *      </entry>
239  *      <entry>
240  *       <para>
241  *        the type string of %G_VARIANT_TYPE_UINT32; an unsigned 32 bit
242  *        integer.
243  *       </para>
244  *      </entry>
245  *     </row>
246  *     <row>
247  *      <entry>
248  *       <para>
249  *        <literal>x</literal>
250  *       </para>
251  *      </entry>
252  *      <entry>
253  *       <para>
254  *        the type string of %G_VARIANT_TYPE_INT64; a signed 64 bit
255  *        integer.
256  *       </para>
257  *      </entry>
258  *     </row>
259  *     <row>
260  *      <entry>
261  *       <para>
262  *        <literal>t</literal>
263  *       </para>
264  *      </entry>
265  *      <entry>
266  *       <para>
267  *        the type string of %G_VARIANT_TYPE_UINT64; an unsigned 64 bit
268  *        integer.
269  *       </para>
270  *      </entry>
271  *     </row>
272  *     <row>
273  *      <entry>
274  *       <para>
275  *        <literal>h</literal>
276  *       </para>
277  *      </entry>
278  *      <entry>
279  *       <para>
280  *        the type string of %G_VARIANT_TYPE_HANDLE; a signed 32 bit
281  *        value that, by convention, is used as an index into an array
282  *        of file descriptors that are sent alongside a DBus message.
283  *       </para>
284  *      </entry>
285  *     </row>
286  *     <row>
287  *      <entry>
288  *       <para>
289  *        <literal>d</literal>
290  *       </para>
291  *      </entry>
292  *      <entry>
293  *       <para>
294  *        the type string of %G_VARIANT_TYPE_DOUBLE; a double precision
295  *        floating point value.
296  *       </para>
297  *      </entry>
298  *     </row>
299  *     <row>
300  *      <entry>
301  *       <para>
302  *        <literal>s</literal>
303  *       </para>
304  *      </entry>
305  *      <entry>
306  *       <para>
307  *        the type string of %G_VARIANT_TYPE_STRING; a string.
308  *       </para>
309  *      </entry>
310  *     </row>
311  *     <row>
312  *      <entry>
313  *       <para>
314  *        <literal>o</literal>
315  *       </para>
316  *      </entry>
317  *      <entry>
318  *       <para>
319  *        the type string of %G_VARIANT_TYPE_OBJECT_PATH; a string in
320  *        the form of a DBus object path.
321  *       </para>
322  *      </entry>
323  *     </row>
324  *     <row>
325  *      <entry>
326  *       <para>
327  *        <literal>g</literal>
328  *       </para>
329  *      </entry>
330  *      <entry>
331  *       <para>
332  *        the type string of %G_VARIANT_TYPE_STRING; a string in the
333  *        form of a DBus type signature.
334  *       </para>
335  *      </entry>
336  *     </row>
337  *     <row>
338  *      <entry>
339  *       <para>
340  *        <literal>?</literal>
341  *       </para>
342  *      </entry>
343  *      <entry>
344  *       <para>
345  *        the type string of %G_VARIANT_TYPE_BASIC; an indefinite type
346  *        that is a supertype of any of the basic types.
347  *       </para>
348  *      </entry>
349  *     </row>
350  *     <row>
351  *      <entry>
352  *       <para>
353  *        <literal>v</literal>
354  *       </para>
355  *      </entry>
356  *      <entry>
357  *       <para>
358  *        the type string of %G_VARIANT_TYPE_VARIANT; a container type
359  *        that contain any other type of value.
360  *       </para>
361  *      </entry>
362  *     </row>
363  *     <row>
364  *      <entry>
365  *       <para>
366  *        <literal>a</literal>
367  *       </para>
368  *      </entry>
369  *      <entry>
370  *       <para>
371  *        used as a prefix on another type string to mean an array of
372  *        that type; the type string "<literal>ai</literal>", for
373  *        example, is the type of an array of 32 bit signed integers.
374  *       </para>
375  *      </entry>
376  *     </row>
377  *     <row>
378  *      <entry>
379  *       <para>
380  *        <literal>m</literal>
381  *       </para>
382  *      </entry>
383  *      <entry>
384  *       <para>
385  *        used as a prefix on another type string to mean a "maybe", or
386  *        "nullable", version of that type; the type string
387  *        "<literal>ms</literal>", for example, is the type of a value
388  *        that maybe contains a string, or maybe contains nothing.
389  *       </para>
390  *      </entry>
391  *     </row>
392  *     <row>
393  *      <entry>
394  *       <para>
395  *        <literal>()</literal>
396  *       </para>
397  *      </entry>
398  *      <entry>
399  *       <para>
400  *        used to enclose zero or more other concatenated type strings
401  *        to create a tuple type; the type string
402  *        "<literal>(is)</literal>", for example, is the type of a pair
403  *        of an integer and a string.
404  *       </para>
405  *      </entry>
406  *     </row>
407  *     <row>
408  *      <entry>
409  *       <para>
410  *        <literal>r</literal>
411  *       </para>
412  *      </entry>
413  *      <entry>
414  *       <para>
415  *        the type string of %G_VARIANT_TYPE_TUPLE; an indefinite type
416  *        that is a supertype of any tuple type, regardless of the
417  *        number of items.
418  *       </para>
419  *      </entry>
420  *     </row>
421  *     <row>
422  *      <entry>
423  *       <para>
424  *        <literal>{}</literal>
425  *       </para>
426  *      </entry>
427  *      <entry>
428  *       <para>
429  *        used to enclose a basic type string concatenated with another
430  *        type string to create a dictionary entry type, which usually
431  *        appears inside of an array to form a dictionary; the type
432  *        string "<literal>a{sd}</literal>", for example, is the type of
433  *        a dictionary that maps strings to double precision floating
434  *        point values.
435  *       </para>
436  *       <para>
437  *        The first type (the basic type) is the key type and the second
438  *        type is the value type.  The reason that the first type is
439  *        restricted to being a basic type is so that it can easily be
440  *        hashed.
441  *       </para>
442  *      </entry>
443  *     </row>
444  *     <row>
445  *      <entry>
446  *       <para>
447  *        <literal>*</literal>
448  *       </para>
449  *      </entry>
450  *      <entry>
451  *       <para>
452  *        the type string of %G_VARIANT_TYPE_ANY; the indefinite type
453  *        that is a supertype of all types.  Note that, as with all type
454  *        strings, this character represents exactly one type.  It
455  *        cannot be used inside of tuples to mean "any number of items".
456  *       </para>
457  *      </entry>
458  *     </row>
459  *    </tbody>
460  *   </tgroup>
461  *  </informaltable>
462  *  <para>
463  *   Any type string of a container that contains an indefinite type is,
464  *   itself, an indefinite type.  For example, the type string
465  *   "<literal>a*</literal>" (corresponding to %G_VARIANT_TYPE_ARRAY) is
466  *   an indefinite type that is a supertype of every array type.
467  *   "<literal>(*s)</literal>" is a supertype of all tuples that
468  *   contain exactly two items where the second item is a string.
469  *  </para>
470  *  <para>
471  *   "<literal>a{?*}</literal>" is an indefinite type that is a
472  *   supertype of all arrays containing dictionary entries where the key
473  *   is any basic type and the value is any type at all.  This is, by
474  *   definition, a dictionary, so this type string corresponds to
475  *   %G_VARIANT_TYPE_DICTIONARY.  Note that, due to the restriction that
476  *   the key of a dictionary entry must be a basic type,
477  *   "<literal>{**}</literal>" is not a valid type string.
478  *  </para>
479  * </refsect2>
480  */
481
482
483 static gboolean
484 g_variant_type_check (const GVariantType *type)
485 {
486   const gchar *type_string;
487
488   if (type == NULL)
489     return FALSE;
490
491   type_string = (const gchar *) type;
492 #ifndef G_DISABLE_CHECKS
493   return g_variant_type_string_scan (type_string, NULL, NULL);
494 #else
495   return TRUE;
496 #endif
497 }
498
499 /**
500  * g_variant_type_string_scan:
501  * @string: a pointer to any string
502  * @limit: the end of @string, or %NULL
503  * @endptr: location to store the end pointer, or %NULL
504  * @returns: %TRUE if a valid type string was found
505  *
506  * Scan for a single complete and valid GVariant type string in @string.
507  * The memory pointed to by @limit (or bytes beyond it) is never
508  * accessed.
509  *
510  * If a valid type string is found, @endptr is updated to point to the
511  * first character past the end of the string that was found and %TRUE
512  * is returned.
513  *
514  * If there is no valid type string starting at @string, or if the type
515  * string does not end before @limit then %FALSE is returned.
516  *
517  * For the simple case of checking if a string is a valid type string,
518  * see g_variant_type_string_is_valid().
519  *
520  * Since: 2.24
521  **/
522 gboolean
523 g_variant_type_string_scan (const gchar  *string,
524                             const gchar  *limit,
525                             const gchar **endptr)
526 {
527   g_return_val_if_fail (string != NULL, FALSE);
528
529   if (string == limit || *string == '\0')
530     return FALSE;
531
532   switch (*string++)
533     {
534     case '(':
535       while (string == limit || *string != ')')
536         if (!g_variant_type_string_scan (string, limit, &string))
537           return FALSE;
538
539       string++;
540       break;
541
542     case '{':
543       if (string == limit || *string == '\0' ||                    /* { */
544           !strchr ("bynqihuxtdsog?", *string++) ||                 /* key */
545           !g_variant_type_string_scan (string, limit, &string) ||  /* value */
546           string == limit || *string++ != '}')                     /* } */
547         return FALSE;
548
549       break;
550
551     case 'm': case 'a':
552       return g_variant_type_string_scan (string, limit, endptr);
553
554     case 'b': case 'y': case 'n': case 'q': case 'i': case 'u':
555     case 'x': case 't': case 'd': case 's': case 'o': case 'g':
556     case 'v': case 'r': case '*': case '?': case 'h':
557       break;
558
559     default:
560       return FALSE;
561     }
562
563   if (endptr != NULL)
564     *endptr = string;
565
566   return TRUE;
567 }
568
569 /**
570  * g_variant_type_string_is_valid:
571  * @type_string: a pointer to any string
572  * @returns: %TRUE if @type_string is exactly one valid type string
573  *
574  * Checks if @type_string is a valid GVariant type string.  This call is
575  * equivalent to calling g_variant_type_string_scan() and confirming
576  * that the following character is a nul terminator.
577  *
578  * Since 2.24
579  **/
580 gboolean
581 g_variant_type_string_is_valid (const gchar *type_string)
582 {
583   gchar *endptr;
584
585   g_return_val_if_fail (type_string != NULL, FALSE);
586
587   if (!g_variant_type_string_scan (type_string, NULL, &endptr))
588     return FALSE;
589
590   return *endptr == '\0';
591 }
592
593 /**
594  * g_variant_type_free:
595  * @type: a #GVariantType, or %NULL
596  *
597  * Frees a #GVariantType that was allocated with
598  * g_variant_type_copy(), g_variant_type_new() or one of the container
599  * type constructor functions.
600  *
601  * In the case that @type is %NULL, this function does nothing.
602  *
603  * Since 2.24
604  **/
605 void
606 g_variant_type_free (GVariantType *type)
607 {
608   g_return_if_fail (type == NULL || g_variant_type_check (type));
609
610   g_free (type);
611 }
612
613 /**
614  * g_variant_type_copy:
615  * @type: a #GVariantType
616  * @returns: a new #GVariantType
617  *
618  * Makes a copy of a #GVariantType.  It is appropriate to call
619  * g_variant_type_free() on the return value.  @type may not be %NULL.
620  *
621  * Since 2.24
622  **/
623 GVariantType *
624 g_variant_type_copy (const GVariantType *type)
625 {
626   gsize length;
627   gchar *new;
628
629   g_return_val_if_fail (g_variant_type_check (type), NULL);
630
631   length = g_variant_type_get_string_length (type);
632   new = g_malloc (length + 1);
633
634   memcpy (new, type, length);
635   new[length] = '\0';
636
637   return (GVariantType *) new;
638 }
639
640 /**
641  * g_variant_type_new:
642  * @type_string: a valid GVariant type string
643  * @returns: a new #GVariantType
644  *
645  * Creates a new #GVariantType corresponding to the type string given
646  * by @type_string.  It is appropriate to call g_variant_type_free() on
647  * the return value.
648  *
649  * It is a programmer error to call this function with an invalid type
650  * string.  Use g_variant_type_string_is_valid() if you are unsure.
651  *
652  * Since: 2.24
653  */
654 GVariantType *
655 g_variant_type_new (const gchar *type_string)
656 {
657   g_return_val_if_fail (type_string != NULL, NULL);
658
659   return g_variant_type_copy (G_VARIANT_TYPE (type_string));
660 }
661
662 /**
663  * g_variant_type_get_string_length:
664  * @type: a #GVariantType
665  * @returns: the length of the corresponding type string
666  *
667  * Returns the length of the type string corresponding to the given
668  * @type.  This function must be used to determine the valid extent of
669  * the memory region returned by g_variant_type_peek_string().
670  *
671  * Since 2.24
672  **/
673 gsize
674 g_variant_type_get_string_length (const GVariantType *type)
675 {
676   const gchar *type_string = (const gchar *) type;
677   gint brackets = 0;
678   gsize index = 0;
679
680   g_return_val_if_fail (g_variant_type_check (type), 0);
681
682   do
683     {
684       while (type_string[index] == 'a' || type_string[index] == 'm')
685         index++;
686
687       if (type_string[index] == '(' || type_string[index] == '{')
688         brackets++;
689
690       else if (type_string[index] == ')' || type_string[index] == '}')
691         brackets--;
692
693       index++;
694     }
695   while (brackets);
696
697   return index;
698 }
699
700 /**
701  * g_variant_type_peek_string:
702  * @type: a #GVariantType
703  * @returns: the corresponding type string (not nul-terminated)
704  *
705  * Returns the type string corresponding to the given @type.  The
706  * result is not nul-terminated; in order to determine its length you
707  * must call g_variant_type_get_string_length().
708  *
709  * To get a nul-terminated string, see g_variant_type_dup_string().
710  *
711  * Since 2.24
712  **/
713 const gchar *
714 g_variant_type_peek_string (const GVariantType *type)
715 {
716   g_return_val_if_fail (g_variant_type_check (type), NULL);
717
718   return (const gchar *) type;
719 }
720
721 /**
722  * g_variant_type_dup_string:
723  * @type: a #GVariantType
724  * @returns: the corresponding type string
725  *
726  * Returns a newly-allocated copy of the type string corresponding to
727  * @type.  The returned string is nul-terminated.  It is appropriate to
728  * call g_free() on the return value.
729  *
730  * Since 2.24
731  **/
732 gchar *
733 g_variant_type_dup_string (const GVariantType *type)
734 {
735   g_return_val_if_fail (g_variant_type_check (type), NULL);
736
737   return g_strndup (g_variant_type_peek_string (type),
738                     g_variant_type_get_string_length (type));
739 }
740
741 /**
742  * g_variant_type_is_definite:
743  * @type: a #GVariantType
744  * @returns: %TRUE if @type is definite
745  *
746  * Determines if the given @type is definite (ie: not indefinite).
747  *
748  * A type is definite if its type string does not contain any indefinite
749  * type characters ('*', '?', or 'r').
750  *
751  * A #GVariant instance may not have an indefinite type, so calling
752  * this function on the result of g_variant_get_type() will always
753  * result in %TRUE being returned.  Calling this function on an
754  * indefinite type like %G_VARIANT_TYPE_ARRAY, however, will result in
755  * %FALSE being returned.
756  *
757  * Since 2.24
758  **/
759 gboolean
760 g_variant_type_is_definite (const GVariantType *type)
761 {
762   const gchar *type_string;
763   gsize type_length;
764   gsize i;
765
766   g_return_val_if_fail (g_variant_type_check (type), FALSE);
767
768   type_length = g_variant_type_get_string_length (type);
769   type_string = g_variant_type_peek_string (type);
770
771   for (i = 0; i < type_length; i++)
772     if (type_string[i] == '*' ||
773         type_string[i] == '?' ||
774         type_string[i] == 'r')
775       return FALSE;
776
777   return TRUE;
778 }
779
780 /**
781  * g_variant_type_is_container:
782  * @type: a #GVariantType
783  * @returns: %TRUE if @type is a container type
784  *
785  * Determines if the given @type is a container type.
786  *
787  * Container types are any array, maybe, tuple, or dictionary
788  * entry types plus the variant type.
789  *
790  * This function returns %TRUE for any indefinite type for which every
791  * definite subtype is a container -- %G_VARIANT_TYPE_ARRAY, for
792  * example.
793  *
794  * Since 2.24
795  **/
796 gboolean
797 g_variant_type_is_container (const GVariantType *type)
798 {
799   gchar first_char;
800
801   g_return_val_if_fail (g_variant_type_check (type), FALSE);
802
803   first_char = g_variant_type_peek_string (type)[0];
804   switch (first_char)
805   {
806     case 'a':
807     case 'm':
808     case 'r':
809     case '(':
810     case '{':
811     case 'v':
812       return TRUE;
813
814     default:
815       return FALSE;
816   }
817 }
818
819 /**
820  * g_variant_type_is_basic:
821  * @type: a #GVariantType
822  * @returns: %TRUE if @type is a basic type
823  *
824  * Determines if the given @type is a basic type.
825  *
826  * Basic types are booleans, bytes, integers, doubles, strings, object
827  * paths and signatures.
828  *
829  * Only a basic type may be used as the key of a dictionary entry.
830  *
831  * This function returns %FALSE for all indefinite types except
832  * %G_VARIANT_TYPE_BASIC.
833  *
834  * Since 2.24
835  **/
836 gboolean
837 g_variant_type_is_basic (const GVariantType *type)
838 {
839   gchar first_char;
840
841   g_return_val_if_fail (g_variant_type_check (type), FALSE);
842
843   first_char = g_variant_type_peek_string (type)[0];
844   switch (first_char)
845   {
846     case 'b':
847     case 'y':
848     case 'n':
849     case 'q':
850     case 'i':
851     case 'h':
852     case 'u':
853     case 't':
854     case 'x':
855     case 'd':
856     case 's':
857     case 'o':
858     case 'g':
859     case '?':
860       return TRUE;
861
862     default:
863       return FALSE;
864   }
865 }
866
867 /**
868  * g_variant_type_is_maybe:
869  * @type: a #GVariantType
870  * @returns: %TRUE if @type is a maybe type
871  *
872  * Determines if the given @type is a maybe type.  This is true if the
873  * type string for @type starts with an 'm'.
874  *
875  * This function returns %TRUE for any indefinite type for which every
876  * definite subtype is a maybe type -- %G_VARIANT_TYPE_MAYBE, for
877  * example.
878  *
879  * Since 2.24
880  **/
881 gboolean
882 g_variant_type_is_maybe (const GVariantType *type)
883 {
884   g_return_val_if_fail (g_variant_type_check (type), FALSE);
885
886   return g_variant_type_peek_string (type)[0] == 'm';
887 }
888
889 /**
890  * g_variant_type_is_array:
891  * @type: a #GVariantType
892  * @returns: %TRUE if @type is an array type
893  *
894  * Determines if the given @type is an array type.  This is true if the
895  * type string for @type starts with an 'a'.
896  *
897  * This function returns %TRUE for any indefinite type for which every
898  * definite subtype is an array type -- %G_VARIANT_TYPE_ARRAY, for
899  * example.
900  *
901  * Since 2.24
902  **/
903 gboolean
904 g_variant_type_is_array (const GVariantType *type)
905 {
906   g_return_val_if_fail (g_variant_type_check (type), FALSE);
907
908   return g_variant_type_peek_string (type)[0] == 'a';
909 }
910
911 /**
912  * g_variant_type_is_tuple:
913  * @type: a #GVariantType
914  * @returns: %TRUE if @type is a tuple type
915  *
916  * Determines if the given @type is a tuple type.  This is true if the
917  * type string for @type starts with a '(' or if @type is
918  * %G_VARIANT_TYPE_TUPLE.
919  *
920  * This function returns %TRUE for any indefinite type for which every
921  * definite subtype is a tuple type -- %G_VARIANT_TYPE_TUPLE, for
922  * example.
923  *
924  * Since 2.24
925  **/
926 gboolean
927 g_variant_type_is_tuple (const GVariantType *type)
928 {
929   gchar type_char;
930
931   g_return_val_if_fail (g_variant_type_check (type), FALSE);
932
933   type_char = g_variant_type_peek_string (type)[0];
934   return type_char == 'r' || type_char == '(';
935 }
936
937 /**
938  * g_variant_type_is_dict_entry:
939  * @type: a #GVariantType
940  * @returns: %TRUE if @type is a dictionary entry type
941  *
942  * Determines if the given @type is a dictionary entry type.  This is
943  * true if the type string for @type starts with a '{'.
944  *
945  * This function returns %TRUE for any indefinite type for which every
946  * definite subtype is a dictionary entry type --
947  * %G_VARIANT_TYPE_DICT_ENTRY, for example.
948  *
949  * Since 2.24
950  **/
951 gboolean
952 g_variant_type_is_dict_entry (const GVariantType *type)
953 {
954   g_return_val_if_fail (g_variant_type_check (type), FALSE);
955
956   return g_variant_type_peek_string (type)[0] == '{';
957 }
958
959 /**
960  * g_variant_type_hash:
961  * @type: a #GVariantType
962  * @returns: the hash value
963  *
964  * Hashes @type.
965  *
966  * The argument type of @type is only #gconstpointer to allow use with
967  * #GHashTable without function pointer casting.  A valid
968  * #GVariantType must be provided.
969  *
970  * Since 2.24
971  **/
972 guint
973 g_variant_type_hash (gconstpointer type)
974 {
975   const gchar *type_string;
976   guint value = 0;
977   gsize length;
978   gsize i;
979
980   g_return_val_if_fail (g_variant_type_check (type), 0);
981
982   type_string = g_variant_type_peek_string (type);
983   length = g_variant_type_get_string_length (type);
984
985   for (i = 0; i < length; i++)
986     value = (value << 5) - value + type_string[i];
987
988   return value;
989 }
990
991 /**
992  * g_variant_type_equal:
993  * @type1: a #GVariantType
994  * @type2: a #GVariantType
995  * @returns: %TRUE if @type1 and @type2 are exactly equal
996  *
997  * Compares @type1 and @type2 for equality.
998  *
999  * Only returns %TRUE if the types are exactly equal.  Even if one type
1000  * is an indefinite type and the other is a subtype of it, %FALSE will
1001  * be returned if they are not exactly equal.  If you want to check for
1002  * subtypes, use g_variant_type_is_subtype_of().
1003  *
1004  * The argument types of @type1 and @type2 are only #gconstpointer to
1005  * allow use with #GHashTable without function pointer casting.  For
1006  * both arguments, a valid #GVariantType must be provided.
1007  *
1008  * Since 2.24
1009  **/
1010 gboolean
1011 g_variant_type_equal (gconstpointer type1,
1012                       gconstpointer type2)
1013 {
1014   const gchar *string1, *string2;
1015   gsize size1, size2;
1016
1017   g_return_val_if_fail (g_variant_type_check (type1), FALSE);
1018   g_return_val_if_fail (g_variant_type_check (type2), FALSE);
1019
1020   if (type1 == type2)
1021     return TRUE;
1022
1023   size1 = g_variant_type_get_string_length (type1);
1024   size2 = g_variant_type_get_string_length (type2);
1025
1026   if (size1 != size2)
1027     return FALSE;
1028
1029   string1 = g_variant_type_peek_string (type1);
1030   string2 = g_variant_type_peek_string (type2);
1031
1032   return memcmp (string1, string2, size1) == 0;
1033 }
1034
1035 /**
1036  * g_variant_type_is_subtype_of:
1037  * @type: a #GVariantType
1038  * @supertype: a #GVariantType
1039  * @returns: %TRUE if @type is a subtype of @supertype
1040  *
1041  * Checks if @type is a subtype of @supertype.
1042  *
1043  * This function returns %TRUE if @type is a subtype of @supertype.  All
1044  * types are considered to be subtypes of themselves.  Aside from that,
1045  * only indefinite types can have subtypes.
1046  *
1047  * Since 2.24
1048  **/
1049 gboolean
1050 g_variant_type_is_subtype_of (const GVariantType *type,
1051                               const GVariantType *supertype)
1052 {
1053   const gchar *supertype_string;
1054   const gchar *supertype_end;
1055   const gchar *type_string;
1056
1057   g_return_val_if_fail (g_variant_type_check (type), FALSE);
1058   g_return_val_if_fail (g_variant_type_check (supertype), FALSE);
1059
1060   supertype_string = g_variant_type_peek_string (supertype);
1061   type_string = g_variant_type_peek_string (type);
1062
1063   supertype_end = supertype_string +
1064                   g_variant_type_get_string_length (supertype);
1065
1066   /* we know that type and supertype are both well-formed, so it's
1067    * safe to treat this merely as a text processing problem.
1068    */
1069   while (supertype_string < supertype_end)
1070     {
1071       char supertype_char = *supertype_string++;
1072
1073       if (supertype_char == *type_string)
1074         type_string++;
1075
1076       else if (*type_string == ')')
1077         return FALSE;
1078
1079       else
1080         {
1081           const GVariantType *target_type = (GVariantType *) type_string;
1082
1083           switch (supertype_char)
1084             {
1085             case 'r':
1086               if (!g_variant_type_is_tuple (target_type))
1087                 return FALSE;
1088               break;
1089
1090             case '*':
1091               break;
1092
1093             case '?':
1094               if (!g_variant_type_is_basic (target_type))
1095                 return FALSE;
1096               break;
1097
1098             default:
1099               return FALSE;
1100             }
1101
1102           type_string += g_variant_type_get_string_length (target_type);
1103         }
1104     }
1105
1106   return TRUE;
1107 }
1108
1109 /**
1110  * g_variant_type_element:
1111  * @type: an array or maybe #GVariantType
1112  * @returns: the element type of @type
1113  *
1114  * Determines the element type of an array or maybe type.
1115  *
1116  * This function may only be used with array or maybe types.
1117  *
1118  * Since 2.24
1119  **/
1120 const GVariantType *
1121 g_variant_type_element (const GVariantType *type)
1122 {
1123   const gchar *type_string;
1124
1125   g_return_val_if_fail (g_variant_type_check (type), NULL);
1126
1127   type_string = g_variant_type_peek_string (type);
1128
1129   g_assert (type_string[0] == 'a' || type_string[0] == 'm');
1130
1131   return (const GVariantType *) &type_string[1];
1132 }
1133
1134 /**
1135  * g_variant_type_first:
1136  * @type: a tuple or dictionary entry #GVariantType
1137  * @returns: the first item type of @type, or %NULL
1138  *
1139  * Determines the first item type of a tuple or dictionary entry
1140  * type.
1141  *
1142  * This function may only be used with tuple or dictionary entry types,
1143  * but must not be used with the generic tuple type
1144  * %G_VARIANT_TYPE_TUPLE.
1145  *
1146  * In the case of a dictionary entry type, this returns the type of
1147  * the key.
1148  *
1149  * %NULL is returned in case of @type being %G_VARIANT_TYPE_UNIT.
1150  *
1151  * This call, together with g_variant_type_next() provides an iterator
1152  * interface over tuple and dictionary entry types.
1153  *
1154  * Since 2.24
1155  **/
1156 const GVariantType *
1157 g_variant_type_first (const GVariantType *type)
1158 {
1159   const gchar *type_string;
1160
1161   g_return_val_if_fail (g_variant_type_check (type), NULL);
1162
1163   type_string = g_variant_type_peek_string (type);
1164   g_assert (type_string[0] == '(' || type_string[0] == '{');
1165
1166   if (type_string[1] == ')')
1167     return NULL;
1168
1169   return (const GVariantType *) &type_string[1];
1170 }
1171
1172 /**
1173  * g_variant_type_next:
1174  * @type: a #GVariantType from a previous call
1175  * @returns: the next #GVariantType after @type, or %NULL
1176  *
1177  * Determines the next item type of a tuple or dictionary entry
1178  * type.
1179  *
1180  * @type must be the result of a previous call to
1181  * g_variant_type_first() or g_variant_type_next().
1182  *
1183  * If called on the key type of a dictionary entry then this call
1184  * returns the value type.  If called on the value type of a dictionary
1185  * entry then this call returns %NULL.
1186  *
1187  * For tuples, %NULL is returned when @type is the last item in a tuple.
1188  *
1189  * Since 2.24
1190  **/
1191 const GVariantType *
1192 g_variant_type_next (const GVariantType *type)
1193 {
1194   const gchar *type_string;
1195
1196   g_return_val_if_fail (g_variant_type_check (type), NULL);
1197
1198   type_string = g_variant_type_peek_string (type);
1199   type_string += g_variant_type_get_string_length (type);
1200
1201   if (*type_string == ')' || *type_string == '}')
1202     return NULL;
1203
1204   return (const GVariantType *) type_string;
1205 }
1206
1207 /**
1208  * g_variant_type_n_items:
1209  * @type: a tuple or dictionary entry #GVariantType
1210  * @returns: the number of items in @type
1211  *
1212  * Determines the number of items contained in a tuple or
1213  * dictionary entry type.
1214  *
1215  * This function may only be used with tuple or dictionary entry types,
1216  * but must not be used with the generic tuple type
1217  * %G_VARIANT_TYPE_TUPLE.
1218  *
1219  * In the case of a dictionary entry type, this function will always
1220  * return 2.
1221  *
1222  * Since 2.24
1223  **/
1224 gsize
1225 g_variant_type_n_items (const GVariantType *type)
1226 {
1227   gsize count = 0;
1228
1229   g_return_val_if_fail (g_variant_type_check (type), 0);
1230
1231   for (type = g_variant_type_first (type);
1232        type;
1233        type = g_variant_type_next (type))
1234     count++;
1235
1236   return count;
1237 }
1238
1239 /**
1240  * g_variant_type_key:
1241  * @type: a dictionary entry #GVariantType
1242  * @returns: the key type of the dictionary entry
1243  *
1244  * Determines the key type of a dictionary entry type.
1245  *
1246  * This function may only be used with a dictionary entry type.  Other
1247  * than the additional restriction, this call is equivalent to
1248  * g_variant_type_first().
1249  *
1250  * Since 2.24
1251  **/
1252 const GVariantType *
1253 g_variant_type_key (const GVariantType *type)
1254 {
1255   const gchar *type_string;
1256
1257   g_return_val_if_fail (g_variant_type_check (type), NULL);
1258
1259   type_string = g_variant_type_peek_string (type);
1260   g_assert (type_string[0] == '{');
1261
1262   return (const GVariantType *) &type_string[1];
1263 }
1264
1265 /**
1266  * g_variant_type_value:
1267  * @type: a dictionary entry #GVariantType
1268  * @returns: the value type of the dictionary entry
1269  *
1270  * Determines the value type of a dictionary entry type.
1271  *
1272  * This function may only be used with a dictionary entry type.
1273  *
1274  * Since 2.24
1275  **/
1276 const GVariantType *
1277 g_variant_type_value (const GVariantType *type)
1278 {
1279   const gchar *type_string;
1280
1281   g_return_val_if_fail (g_variant_type_check (type), NULL);
1282
1283   type_string = g_variant_type_peek_string (type);
1284   g_assert (type_string[0] == '{');
1285
1286   return g_variant_type_next (g_variant_type_key (type));
1287 }
1288
1289 /**
1290  * g_variant_type_new_tuple:
1291  * @items: an array of #GVariantTypes, one for each item
1292  * @length: the length of @items, or -1
1293  * @returns: a new tuple #GVariantType
1294  *
1295  * Constructs a new tuple type, from @items.
1296  *
1297  * @length is the number of items in @items, or -1 to indicate that
1298  * @items is %NULL-terminated.
1299  *
1300  * It is appropriate to call g_variant_type_free() on the return value.
1301  *
1302  * Since 2.24
1303  **/
1304 static GVariantType *
1305 g_variant_type_new_tuple_slow (const GVariantType * const *items,
1306                                gint                        length)
1307 {
1308   /* the "slow" version is needed in case the static buffer of 1024
1309    * bytes is exceeded when running the normal version.  this will
1310    * happen only in truly insane code, so it can be slow.
1311    */
1312   GString *string;
1313   gsize i;
1314
1315   string = g_string_new ("(");
1316   for (i = 0; i < length; i++)
1317     {
1318       const GVariantType *type;
1319       gsize size;
1320
1321       g_return_val_if_fail (g_variant_type_check (items[i]), NULL);
1322
1323       type = items[i];
1324       size = g_variant_type_get_string_length (type);
1325       g_string_append_len (string, (const gchar *) type, size);
1326     }
1327   g_string_append_c (string, ')');
1328
1329   return (GVariantType *) g_string_free (string, FALSE);
1330 }
1331
1332 GVariantType *
1333 g_variant_type_new_tuple (const GVariantType * const *items,
1334                           gint                        length)
1335 {
1336   char buffer[1024];
1337   gsize offset;
1338   gsize i;
1339
1340   g_return_val_if_fail (length == 0 || items != NULL, NULL);
1341
1342   if (length < 0)
1343     for (length = 0; items[length] != NULL; length++);
1344
1345   offset = 0;
1346   buffer[offset++] = '(';
1347
1348   for (i = 0; i < length; i++)
1349     {
1350       const GVariantType *type;
1351       gsize size;
1352
1353       g_return_val_if_fail (g_variant_type_check (items[i]), NULL);
1354
1355       type = items[i];
1356       size = g_variant_type_get_string_length (type);
1357
1358       if (offset + size >= sizeof buffer) /* leave room for ')' */
1359         return g_variant_type_new_tuple_slow (items, length);
1360
1361       memcpy (&buffer[offset], type, size);
1362       offset += size;
1363     }
1364
1365   g_assert (offset < sizeof buffer);
1366   buffer[offset++] = ')';
1367
1368   return (GVariantType *) g_memdup (buffer, offset);
1369 }
1370
1371 /**
1372  * g_variant_type_new_array:
1373  * @element: a #GVariantType
1374  * @returns: a new array #GVariantType
1375  *
1376  * Constructs the type corresponding to an array of elements of the
1377  * type @type.
1378  *
1379  * It is appropriate to call g_variant_type_free() on the return value.
1380  *
1381  * Since 2.24
1382  **/
1383 GVariantType *
1384 g_variant_type_new_array (const GVariantType *element)
1385 {
1386   gsize size;
1387   gchar *new;
1388
1389   g_return_val_if_fail (g_variant_type_check (element), NULL);
1390
1391   size = g_variant_type_get_string_length (element);
1392   new = g_malloc (size + 1);
1393
1394   new[0] = 'a';
1395   memcpy (new + 1, element, size);
1396
1397   return (GVariantType *) new;
1398 }
1399
1400 /**
1401  * g_variant_type_new_maybe:
1402  * @element: a #GVariantType
1403  * @returns: a new maybe #GVariantType
1404  *
1405  * Constructs the type corresponding to a maybe instance containing
1406  * type @type or Nothing.
1407  *
1408  * It is appropriate to call g_variant_type_free() on the return value.
1409  *
1410  * Since 2.24
1411  **/
1412 GVariantType *
1413 g_variant_type_new_maybe (const GVariantType *element)
1414 {
1415   gsize size;
1416   gchar *new;
1417
1418   g_return_val_if_fail (g_variant_type_check (element), NULL);
1419
1420   size = g_variant_type_get_string_length (element);
1421   new = g_malloc (size + 1);
1422
1423   new[0] = 'm';
1424   memcpy (new + 1, element, size);
1425
1426   return (GVariantType *) new;
1427 }
1428
1429 /**
1430  * g_variant_type_new_dict_entry:
1431  * @key: a basic #GVariantType
1432  * @value: a #GVariantType
1433  * @returns: a new dictionary entry #GVariantType
1434  *
1435  * Constructs the type corresponding to a dictionary entry with a key
1436  * of type @key and a value of type @value.
1437  *
1438  * It is appropriate to call g_variant_type_free() on the return value.
1439  *
1440  * Since 2.24
1441  **/
1442 GVariantType *
1443 g_variant_type_new_dict_entry (const GVariantType *key,
1444                                const GVariantType *value)
1445 {
1446   gsize keysize, valsize;
1447   gchar *new;
1448
1449   g_return_val_if_fail (g_variant_type_check (key), NULL);
1450   g_return_val_if_fail (g_variant_type_check (value), NULL);
1451
1452   keysize = g_variant_type_get_string_length (key);
1453   valsize = g_variant_type_get_string_length (value);
1454
1455   new = g_malloc (1 + keysize + valsize + 1);
1456
1457   new[0] = '{';
1458   memcpy (new + 1, key, keysize);
1459   memcpy (new + 1 + keysize, value, valsize);
1460   new[1 + keysize + valsize] = '}';
1461
1462   return (GVariantType *) new;
1463 }
1464
1465 /* private */
1466 const GVariantType *
1467 g_variant_type_checked_ (const gchar *type_string)
1468 {
1469   g_return_val_if_fail (g_variant_type_string_is_valid (type_string), NULL);
1470   return (const GVariantType *) type_string;
1471 }
1472
1473 #define __G_VARIANT_TYPE_C__
1474 #include "galiasdef.c"