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