aeea1c188e832add95c77953f3b6040c33ca6a7a
[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 "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
33 /**
34  * SECTION:gvarianttype
35  * @title: GVariantType
36  * @short_description: introduction to the GVariant type system
37  * @see_also: #GVariantType, #GVariant
38  *
39  * This section introduces the GVariant type system.  It is based, in
40  * large part, on the D-Bus type system, with two major changes and some minor
41  * lifting of restrictions.  The <ulink
42  * url='http://dbus.freedesktop.org/doc/dbus-specification.html'>DBus
43  * specification</ulink>, therefore, provides a significant amount of
44  * information that is useful when working with GVariant.
45  *
46  * The first major change with respect to the D-Bus type system is the
47  * introduction of maybe (or "nullable") types.  Any type in GVariant can be
48  * converted to a maybe type, in which case, "nothing" (or "null") becomes a
49  * valid value.  Maybe types have been added by introducing the
50  * character "<literal>m</literal>" to type strings.
51  *
52  * The second major change is that the GVariant type system supports the
53  * concept of "indefinite types" -- types that are less specific than
54  * the normal types found in D-Bus.  For example, it is possible to speak
55  * of "an array of any type" in GVariant, where the D-Bus type system
56  * would require you to speak of "an array of integers" or "an array of
57  * strings".  Indefinite types have been added by introducing the
58  * characters "<literal>*</literal>", "<literal>?</literal>" and
59  * "<literal>r</literal>" to type strings.
60  *
61  * Finally, all arbitrary restrictions relating to the complexity of
62  * types are lifted along with the restriction that dictionary entries
63  * may only appear nested inside of arrays.
64  *
65  * Just as in D-Bus, GVariant types are described with strings ("type
66  * strings").  Subject to the differences mentioned above, these strings
67  * are of the same form as those found in DBus.  Note, however: D-Bus
68  * always works in terms of messages and therefore individual type
69  * strings appear nowhere in its interface.  Instead, "signatures"
70  * are a concatenation of the strings of the type of each argument in a
71  * message.  GVariant deals with single values directly so GVariant type
72  * strings always describe the type of exactly one value.  This means
73  * that a D-Bus signature string is generally not a valid GVariant type
74  * string -- except in the case that it is the signature of a message
75  * containing exactly one argument.
76  *
77  * An indefinite type is similar in spirit to what may be called an
78  * abstract type in other type systems.  No value can exist that has an
79  * indefinite type as its type, but values can exist that have types
80  * that are subtypes of indefinite types.  That is to say,
81  * g_variant_get_type() will never return an indefinite type, but
82  * calling g_variant_is_of_type() with an indefinite type may return
83  * %TRUE.  For example, you cannot have a value that represents "an
84  * array of no particular type", but you can have an "array of integers"
85  * which certainly matches the type of "an array of no particular type",
86  * since "array of integers" is a subtype of "array of no particular
87  * 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 D-Bus 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 D-Bus 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 D-Bus 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: (allow-none): the end of @string, or %NULL
506  * @endptr: (out) (allow-none): location to store the end pointer, or %NULL
507  *
508  * Scan for a single complete and valid GVariant type string in @string.
509  * The memory pointed to by @limit (or bytes beyond it) is never
510  * accessed.
511  *
512  * If a valid type string is found, @endptr is updated to point to the
513  * first character past the end of the string that was found and %TRUE
514  * is returned.
515  *
516  * If there is no valid type string starting at @string, or if the type
517  * string does not end before @limit then %FALSE is returned.
518  *
519  * For the simple case of checking if a string is a valid type string,
520  * see g_variant_type_string_is_valid().
521  *
522  * Returns: %TRUE if a valid type string was found
523  *
524  * Since: 2.24
525  **/
526 gboolean
527 g_variant_type_string_scan (const gchar  *string,
528                             const gchar  *limit,
529                             const gchar **endptr)
530 {
531   g_return_val_if_fail (string != NULL, FALSE);
532
533   if (string == limit || *string == '\0')
534     return FALSE;
535
536   switch (*string++)
537     {
538     case '(':
539       while (string == limit || *string != ')')
540         if (!g_variant_type_string_scan (string, limit, &string))
541           return FALSE;
542
543       string++;
544       break;
545
546     case '{':
547       if (string == limit || *string == '\0' ||                    /* { */
548           !strchr ("bynqihuxtdsog?", *string++) ||                 /* key */
549           !g_variant_type_string_scan (string, limit, &string) ||  /* value */
550           string == limit || *string++ != '}')                     /* } */
551         return FALSE;
552
553       break;
554
555     case 'm': case 'a':
556       return g_variant_type_string_scan (string, limit, endptr);
557
558     case 'b': case 'y': case 'n': case 'q': case 'i': case 'u':
559     case 'x': case 't': case 'd': case 's': case 'o': case 'g':
560     case 'v': case 'r': case '*': case '?': case 'h':
561       break;
562
563     default:
564       return FALSE;
565     }
566
567   if (endptr != NULL)
568     *endptr = string;
569
570   return TRUE;
571 }
572
573 /**
574  * g_variant_type_string_is_valid:
575  * @type_string: a pointer to any 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  * Returns: %TRUE if @type_string is exactly one valid type string
582  *
583  * Since 2.24
584  **/
585 gboolean
586 g_variant_type_string_is_valid (const gchar *type_string)
587 {
588   const gchar *endptr;
589
590   g_return_val_if_fail (type_string != NULL, FALSE);
591
592   if (!g_variant_type_string_scan (type_string, NULL, &endptr))
593     return FALSE;
594
595   return *endptr == '\0';
596 }
597
598 /**
599  * g_variant_type_free:
600  * @type: (allow-none): a #GVariantType, or %NULL
601  *
602  * Frees a #GVariantType that was allocated with
603  * g_variant_type_copy(), g_variant_type_new() or one of the container
604  * type constructor functions.
605  *
606  * In the case that @type is %NULL, this function does nothing.
607  *
608  * Since 2.24
609  **/
610 void
611 g_variant_type_free (GVariantType *type)
612 {
613   g_return_if_fail (type == NULL || g_variant_type_check (type));
614
615   g_free (type);
616 }
617
618 /**
619  * g_variant_type_copy:
620  * @type: a #GVariantType
621  *
622  * Makes a copy of a #GVariantType.  It is appropriate to call
623  * g_variant_type_free() on the return value.  @type may not be %NULL.
624  *
625  * Returns: (transfer full): a new #GVariantType
626  *
627  * Since 2.24
628  **/
629 GVariantType *
630 g_variant_type_copy (const GVariantType *type)
631 {
632   gsize length;
633   gchar *new;
634
635   g_return_val_if_fail (g_variant_type_check (type), NULL);
636
637   length = g_variant_type_get_string_length (type);
638   new = g_malloc (length + 1);
639
640   memcpy (new, type, length);
641   new[length] = '\0';
642
643   return (GVariantType *) new;
644 }
645
646 /**
647  * g_variant_type_new:
648  * @type_string: a valid GVariant type string
649  *
650  * Creates a new #GVariantType corresponding to the type string given
651  * by @type_string.  It is appropriate to call g_variant_type_free() on
652  * the return value.
653  *
654  * It is a programmer error to call this function with an invalid type
655  * string.  Use g_variant_type_string_is_valid() if you are unsure.
656  *
657  * Returns: (transfer full): a new #GVariantType
658  *
659  * Since: 2.24
660  */
661 GVariantType *
662 g_variant_type_new (const gchar *type_string)
663 {
664   g_return_val_if_fail (type_string != NULL, NULL);
665
666   return g_variant_type_copy (G_VARIANT_TYPE (type_string));
667 }
668
669 /**
670  * g_variant_type_get_string_length:
671  * @type: a #GVariantType
672  *
673  * Returns the length of the type string corresponding to the given
674  * @type.  This function must be used to determine the valid extent of
675  * the memory region returned by g_variant_type_peek_string().
676  *
677  * Returns: the length of the corresponding type string
678  *
679  * Since 2.24
680  **/
681 gsize
682 g_variant_type_get_string_length (const GVariantType *type)
683 {
684   const gchar *type_string = (const gchar *) type;
685   gint brackets = 0;
686   gsize index = 0;
687
688   g_return_val_if_fail (g_variant_type_check (type), 0);
689
690   do
691     {
692       while (type_string[index] == 'a' || type_string[index] == 'm')
693         index++;
694
695       if (type_string[index] == '(' || type_string[index] == '{')
696         brackets++;
697
698       else if (type_string[index] == ')' || type_string[index] == '}')
699         brackets--;
700
701       index++;
702     }
703   while (brackets);
704
705   return index;
706 }
707
708 /*
709   This function is not introspectable, it returns something that
710   is not an array and neither a string
711 */
712 /**
713  * g_variant_type_peek_string: (skip)
714  * @type: a #GVariantType
715  *
716  * Returns the type string corresponding to the given @type.  The
717  * result is not nul-terminated; in order to determine its length you
718  * must call g_variant_type_get_string_length().
719  *
720  * To get a nul-terminated string, see g_variant_type_dup_string().
721  *
722  * Returns: the corresponding type string (not nul-terminated)
723  *
724  * Since 2.24
725  **/
726 const gchar *
727 g_variant_type_peek_string (const GVariantType *type)
728 {
729   g_return_val_if_fail (g_variant_type_check (type), NULL);
730
731   return (const gchar *) type;
732 }
733
734 /**
735  * g_variant_type_dup_string:
736  * @type: a #GVariantType
737  *
738  * Returns a newly-allocated copy of the type string corresponding to
739  * @type.  The returned string is nul-terminated.  It is appropriate to
740  * call g_free() on the return value.
741  *
742  * Returns: (transfer full): the corresponding type string
743  *
744  * Since 2.24
745  **/
746 gchar *
747 g_variant_type_dup_string (const GVariantType *type)
748 {
749   g_return_val_if_fail (g_variant_type_check (type), NULL);
750
751   return g_strndup (g_variant_type_peek_string (type),
752                     g_variant_type_get_string_length (type));
753 }
754
755 /**
756  * g_variant_type_is_definite:
757  * @type: a #GVariantType
758  *
759  * Determines if the given @type is definite (ie: not indefinite).
760  *
761  * A type is definite if its type string does not contain any indefinite
762  * type characters ('*', '?', or 'r').
763  *
764  * A #GVariant instance may not have an indefinite type, so calling
765  * this function on the result of g_variant_get_type() will always
766  * result in %TRUE being returned.  Calling this function on an
767  * indefinite type like %G_VARIANT_TYPE_ARRAY, however, will result in
768  * %FALSE being returned.
769  *
770  * Returns: %TRUE if @type is definite
771  *
772  * Since 2.24
773  **/
774 gboolean
775 g_variant_type_is_definite (const GVariantType *type)
776 {
777   const gchar *type_string;
778   gsize type_length;
779   gsize i;
780
781   g_return_val_if_fail (g_variant_type_check (type), FALSE);
782
783   type_length = g_variant_type_get_string_length (type);
784   type_string = g_variant_type_peek_string (type);
785
786   for (i = 0; i < type_length; i++)
787     if (type_string[i] == '*' ||
788         type_string[i] == '?' ||
789         type_string[i] == 'r')
790       return FALSE;
791
792   return TRUE;
793 }
794
795 /**
796  * g_variant_type_is_container:
797  * @type: a #GVariantType
798  *
799  * Determines if the given @type is a container type.
800  *
801  * Container types are any array, maybe, tuple, or dictionary
802  * entry types plus the variant type.
803  *
804  * This function returns %TRUE for any indefinite type for which every
805  * definite subtype is a container -- %G_VARIANT_TYPE_ARRAY, for
806  * example.
807  *
808  * Returns: %TRUE if @type is a container type
809  *
810  * Since 2.24
811  **/
812 gboolean
813 g_variant_type_is_container (const GVariantType *type)
814 {
815   gchar first_char;
816
817   g_return_val_if_fail (g_variant_type_check (type), FALSE);
818
819   first_char = g_variant_type_peek_string (type)[0];
820   switch (first_char)
821   {
822     case 'a':
823     case 'm':
824     case 'r':
825     case '(':
826     case '{':
827     case 'v':
828       return TRUE;
829
830     default:
831       return FALSE;
832   }
833 }
834
835 /**
836  * g_variant_type_is_basic:
837  * @type: a #GVariantType
838  *
839  * Determines if the given @type is a basic type.
840  *
841  * Basic types are booleans, bytes, integers, doubles, strings, object
842  * paths and signatures.
843  *
844  * Only a basic type may be used as the key of a dictionary entry.
845  *
846  * This function returns %FALSE for all indefinite types except
847  * %G_VARIANT_TYPE_BASIC.
848  *
849  * Returns: %TRUE if @type is a basic type
850  *
851  * Since 2.24
852  **/
853 gboolean
854 g_variant_type_is_basic (const GVariantType *type)
855 {
856   gchar first_char;
857
858   g_return_val_if_fail (g_variant_type_check (type), FALSE);
859
860   first_char = g_variant_type_peek_string (type)[0];
861   switch (first_char)
862   {
863     case 'b':
864     case 'y':
865     case 'n':
866     case 'q':
867     case 'i':
868     case 'h':
869     case 'u':
870     case 't':
871     case 'x':
872     case 'd':
873     case 's':
874     case 'o':
875     case 'g':
876     case '?':
877       return TRUE;
878
879     default:
880       return FALSE;
881   }
882 }
883
884 /**
885  * g_variant_type_is_maybe:
886  * @type: a #GVariantType
887  *
888  * Determines if the given @type is a maybe type.  This is true if the
889  * type string for @type starts with an 'm'.
890  *
891  * This function returns %TRUE for any indefinite type for which every
892  * definite subtype is a maybe type -- %G_VARIANT_TYPE_MAYBE, for
893  * example.
894  *
895  * Returns: %TRUE if @type is a maybe type
896  *
897  * Since 2.24
898  **/
899 gboolean
900 g_variant_type_is_maybe (const GVariantType *type)
901 {
902   g_return_val_if_fail (g_variant_type_check (type), FALSE);
903
904   return g_variant_type_peek_string (type)[0] == 'm';
905 }
906
907 /**
908  * g_variant_type_is_array:
909  * @type: a #GVariantType
910  *
911  * Determines if the given @type is an array type.  This is true if the
912  * type string for @type starts with an 'a'.
913  *
914  * This function returns %TRUE for any indefinite type for which every
915  * definite subtype is an array type -- %G_VARIANT_TYPE_ARRAY, for
916  * example.
917  *
918  * Returns: %TRUE if @type is an array type
919  *
920  * Since 2.24
921  **/
922 gboolean
923 g_variant_type_is_array (const GVariantType *type)
924 {
925   g_return_val_if_fail (g_variant_type_check (type), FALSE);
926
927   return g_variant_type_peek_string (type)[0] == 'a';
928 }
929
930 /**
931  * g_variant_type_is_tuple:
932  * @type: a #GVariantType
933  *
934  * Determines if the given @type is a tuple type.  This is true if the
935  * type string for @type starts with a '(' or if @type is
936  * %G_VARIANT_TYPE_TUPLE.
937  *
938  * This function returns %TRUE for any indefinite type for which every
939  * definite subtype is a tuple type -- %G_VARIANT_TYPE_TUPLE, for
940  * example.
941  *
942  * Returns: %TRUE if @type is a tuple type
943  *
944  * Since 2.24
945  **/
946 gboolean
947 g_variant_type_is_tuple (const GVariantType *type)
948 {
949   gchar type_char;
950
951   g_return_val_if_fail (g_variant_type_check (type), FALSE);
952
953   type_char = g_variant_type_peek_string (type)[0];
954   return type_char == 'r' || type_char == '(';
955 }
956
957 /**
958  * g_variant_type_is_dict_entry:
959  * @type: a #GVariantType
960  *
961  * Determines if the given @type is a dictionary entry type.  This is
962  * true if the type string for @type starts with a '{'.
963  *
964  * This function returns %TRUE for any indefinite type for which every
965  * definite subtype is a dictionary entry type --
966  * %G_VARIANT_TYPE_DICT_ENTRY, for example.
967  *
968  * Returns: %TRUE if @type is a dictionary entry type
969  *
970  * Since 2.24
971  **/
972 gboolean
973 g_variant_type_is_dict_entry (const GVariantType *type)
974 {
975   g_return_val_if_fail (g_variant_type_check (type), FALSE);
976
977   return g_variant_type_peek_string (type)[0] == '{';
978 }
979
980 /**
981  * g_variant_type_is_variant:
982  * @type: a #GVariantType
983  *
984  * Determines if the given @type is the variant type.
985  *
986  * Returns: %TRUE if @type is the variant type
987  *
988  * Since 2.24
989  **/
990 gboolean
991 g_variant_type_is_variant (const GVariantType *type)
992 {
993   g_return_val_if_fail (g_variant_type_check (type), FALSE);
994
995   return g_variant_type_peek_string (type)[0] == 'v';
996 }
997
998 /**
999  * g_variant_type_hash:
1000  * @type: (type GVariantType): a #GVariantType
1001  *
1002  * Hashes @type.
1003  *
1004  * The argument type of @type is only #gconstpointer to allow use with
1005  * #GHashTable without function pointer casting.  A valid
1006  * #GVariantType must be provided.
1007  *
1008  * Returns: the hash value
1009  *
1010  * Since 2.24
1011  **/
1012 guint
1013 g_variant_type_hash (gconstpointer type)
1014 {
1015   const gchar *type_string;
1016   guint value = 0;
1017   gsize length;
1018   gsize i;
1019
1020   g_return_val_if_fail (g_variant_type_check (type), 0);
1021
1022   type_string = g_variant_type_peek_string (type);
1023   length = g_variant_type_get_string_length (type);
1024
1025   for (i = 0; i < length; i++)
1026     value = (value << 5) - value + type_string[i];
1027
1028   return value;
1029 }
1030
1031 /**
1032  * g_variant_type_equal:
1033  * @type1: (type GVariantType): a #GVariantType
1034  * @type2: (type GVariantType): a #GVariantType
1035  *
1036  * Compares @type1 and @type2 for equality.
1037  *
1038  * Only returns %TRUE if the types are exactly equal.  Even if one type
1039  * is an indefinite type and the other is a subtype of it, %FALSE will
1040  * be returned if they are not exactly equal.  If you want to check for
1041  * subtypes, use g_variant_type_is_subtype_of().
1042  *
1043  * The argument types of @type1 and @type2 are only #gconstpointer to
1044  * allow use with #GHashTable without function pointer casting.  For
1045  * both arguments, a valid #GVariantType must be provided.
1046  *
1047  * Returns: %TRUE if @type1 and @type2 are exactly equal
1048  *
1049  * Since 2.24
1050  **/
1051 gboolean
1052 g_variant_type_equal (gconstpointer type1,
1053                       gconstpointer type2)
1054 {
1055   const gchar *string1, *string2;
1056   gsize size1, size2;
1057
1058   g_return_val_if_fail (g_variant_type_check (type1), FALSE);
1059   g_return_val_if_fail (g_variant_type_check (type2), FALSE);
1060
1061   if (type1 == type2)
1062     return TRUE;
1063
1064   size1 = g_variant_type_get_string_length (type1);
1065   size2 = g_variant_type_get_string_length (type2);
1066
1067   if (size1 != size2)
1068     return FALSE;
1069
1070   string1 = g_variant_type_peek_string (type1);
1071   string2 = g_variant_type_peek_string (type2);
1072
1073   return memcmp (string1, string2, size1) == 0;
1074 }
1075
1076 /**
1077  * g_variant_type_is_subtype_of:
1078  * @type: a #GVariantType
1079  * @supertype: a #GVariantType
1080  *
1081  * Checks if @type is a subtype of @supertype.
1082  *
1083  * This function returns %TRUE if @type is a subtype of @supertype.  All
1084  * types are considered to be subtypes of themselves.  Aside from that,
1085  * only indefinite types can have subtypes.
1086  *
1087  * Returns: %TRUE if @type is a subtype of @supertype
1088  *
1089  * Since 2.24
1090  **/
1091 gboolean
1092 g_variant_type_is_subtype_of (const GVariantType *type,
1093                               const GVariantType *supertype)
1094 {
1095   const gchar *supertype_string;
1096   const gchar *supertype_end;
1097   const gchar *type_string;
1098
1099   g_return_val_if_fail (g_variant_type_check (type), FALSE);
1100   g_return_val_if_fail (g_variant_type_check (supertype), FALSE);
1101
1102   supertype_string = g_variant_type_peek_string (supertype);
1103   type_string = g_variant_type_peek_string (type);
1104
1105   supertype_end = supertype_string +
1106                   g_variant_type_get_string_length (supertype);
1107
1108   /* we know that type and supertype are both well-formed, so it's
1109    * safe to treat this merely as a text processing problem.
1110    */
1111   while (supertype_string < supertype_end)
1112     {
1113       char supertype_char = *supertype_string++;
1114
1115       if (supertype_char == *type_string)
1116         type_string++;
1117
1118       else if (*type_string == ')')
1119         return FALSE;
1120
1121       else
1122         {
1123           const GVariantType *target_type = (GVariantType *) type_string;
1124
1125           switch (supertype_char)
1126             {
1127             case 'r':
1128               if (!g_variant_type_is_tuple (target_type))
1129                 return FALSE;
1130               break;
1131
1132             case '*':
1133               break;
1134
1135             case '?':
1136               if (!g_variant_type_is_basic (target_type))
1137                 return FALSE;
1138               break;
1139
1140             default:
1141               return FALSE;
1142             }
1143
1144           type_string += g_variant_type_get_string_length (target_type);
1145         }
1146     }
1147
1148   return TRUE;
1149 }
1150
1151 /**
1152  * g_variant_type_element:
1153  * @type: an array or maybe #GVariantType
1154  *
1155  * Determines the element type of an array or maybe type.
1156  *
1157  * This function may only be used with array or maybe types.
1158  *
1159  * Returns: (transfer none): the element type of @type
1160  *
1161  * Since 2.24
1162  **/
1163 const GVariantType *
1164 g_variant_type_element (const GVariantType *type)
1165 {
1166   const gchar *type_string;
1167
1168   g_return_val_if_fail (g_variant_type_check (type), NULL);
1169
1170   type_string = g_variant_type_peek_string (type);
1171
1172   g_assert (type_string[0] == 'a' || type_string[0] == 'm');
1173
1174   return (const GVariantType *) &type_string[1];
1175 }
1176
1177 /**
1178  * g_variant_type_first:
1179  * @type: a tuple or dictionary entry #GVariantType
1180  *
1181  * Determines the first item type of a tuple or dictionary entry
1182  * type.
1183  *
1184  * This function may only be used with tuple or dictionary entry types,
1185  * but must not be used with the generic tuple type
1186  * %G_VARIANT_TYPE_TUPLE.
1187  *
1188  * In the case of a dictionary entry type, this returns the type of
1189  * the key.
1190  *
1191  * %NULL is returned in case of @type being %G_VARIANT_TYPE_UNIT.
1192  *
1193  * This call, together with g_variant_type_next() provides an iterator
1194  * interface over tuple and dictionary entry types.
1195  *
1196  * Returns: (transfer none): the first item type of @type, or %NULL
1197  *
1198  * Since 2.24
1199  **/
1200 const GVariantType *
1201 g_variant_type_first (const GVariantType *type)
1202 {
1203   const gchar *type_string;
1204
1205   g_return_val_if_fail (g_variant_type_check (type), NULL);
1206
1207   type_string = g_variant_type_peek_string (type);
1208   g_assert (type_string[0] == '(' || type_string[0] == '{');
1209
1210   if (type_string[1] == ')')
1211     return NULL;
1212
1213   return (const GVariantType *) &type_string[1];
1214 }
1215
1216 /**
1217  * g_variant_type_next:
1218  * @type: a #GVariantType from a previous call
1219  *
1220  * Determines the next item type of a tuple or dictionary entry
1221  * type.
1222  *
1223  * @type must be the result of a previous call to
1224  * g_variant_type_first() or g_variant_type_next().
1225  *
1226  * If called on the key type of a dictionary entry then this call
1227  * returns the value type.  If called on the value type of a dictionary
1228  * entry then this call returns %NULL.
1229  *
1230  * For tuples, %NULL is returned when @type is the last item in a tuple.
1231  *
1232  * Returns: (transfer none): the next #GVariantType after @type, or %NULL
1233  *
1234  * Since 2.24
1235  **/
1236 const GVariantType *
1237 g_variant_type_next (const GVariantType *type)
1238 {
1239   const gchar *type_string;
1240
1241   g_return_val_if_fail (g_variant_type_check (type), NULL);
1242
1243   type_string = g_variant_type_peek_string (type);
1244   type_string += g_variant_type_get_string_length (type);
1245
1246   if (*type_string == ')' || *type_string == '}')
1247     return NULL;
1248
1249   return (const GVariantType *) type_string;
1250 }
1251
1252 /**
1253  * g_variant_type_n_items:
1254  * @type: a tuple or dictionary entry #GVariantType
1255  *
1256  * Determines the number of items contained in a tuple or
1257  * dictionary entry type.
1258  *
1259  * This function may only be used with tuple or dictionary entry types,
1260  * but must not be used with the generic tuple type
1261  * %G_VARIANT_TYPE_TUPLE.
1262  *
1263  * In the case of a dictionary entry type, this function will always
1264  * return 2.
1265  *
1266  * Returns: the number of items in @type
1267  *
1268  * Since 2.24
1269  **/
1270 gsize
1271 g_variant_type_n_items (const GVariantType *type)
1272 {
1273   gsize count = 0;
1274
1275   g_return_val_if_fail (g_variant_type_check (type), 0);
1276
1277   for (type = g_variant_type_first (type);
1278        type;
1279        type = g_variant_type_next (type))
1280     count++;
1281
1282   return count;
1283 }
1284
1285 /**
1286  * g_variant_type_key:
1287  * @type: a dictionary entry #GVariantType
1288  *
1289  * Determines the key type of a dictionary entry type.
1290  *
1291  * This function may only be used with a dictionary entry type.  Other
1292  * than the additional restriction, this call is equivalent to
1293  * g_variant_type_first().
1294  *
1295  * Returns: (transfer none): the key type of the dictionary entry
1296  *
1297  * Since 2.24
1298  **/
1299 const GVariantType *
1300 g_variant_type_key (const GVariantType *type)
1301 {
1302   const gchar *type_string;
1303
1304   g_return_val_if_fail (g_variant_type_check (type), NULL);
1305
1306   type_string = g_variant_type_peek_string (type);
1307   g_assert (type_string[0] == '{');
1308
1309   return (const GVariantType *) &type_string[1];
1310 }
1311
1312 /**
1313  * g_variant_type_value:
1314  * @type: a dictionary entry #GVariantType
1315  *
1316  * Determines the value type of a dictionary entry type.
1317  *
1318  * This function may only be used with a dictionary entry type.
1319  *
1320  * Returns: (transfer none): the value type of the dictionary entry
1321  *
1322  * Since 2.24
1323  **/
1324 const GVariantType *
1325 g_variant_type_value (const GVariantType *type)
1326 {
1327   const gchar *type_string;
1328
1329   g_return_val_if_fail (g_variant_type_check (type), NULL);
1330
1331   type_string = g_variant_type_peek_string (type);
1332   g_assert (type_string[0] == '{');
1333
1334   return g_variant_type_next (g_variant_type_key (type));
1335 }
1336
1337 /**
1338  * g_variant_type_new_tuple:
1339  * @items: (array length=length): an array of #GVariantTypes, one for each item
1340  * @length: the length of @items, or -1
1341  *
1342  * Constructs a new tuple type, from @items.
1343  *
1344  * @length is the number of items in @items, or -1 to indicate that
1345  * @items is %NULL-terminated.
1346  *
1347  * It is appropriate to call g_variant_type_free() on the return value.
1348  *
1349  * Returns: (transfer full): a new tuple #GVariantType
1350  *
1351  * Since 2.24
1352  **/
1353 static GVariantType *
1354 g_variant_type_new_tuple_slow (const GVariantType * const *items,
1355                                gint                        length)
1356 {
1357   /* the "slow" version is needed in case the static buffer of 1024
1358    * bytes is exceeded when running the normal version.  this will
1359    * happen only in truly insane code, so it can be slow.
1360    */
1361   GString *string;
1362   gsize i;
1363
1364   string = g_string_new ("(");
1365   for (i = 0; i < length; i++)
1366     {
1367       const GVariantType *type;
1368       gsize size;
1369
1370       g_return_val_if_fail (g_variant_type_check (items[i]), NULL);
1371
1372       type = items[i];
1373       size = g_variant_type_get_string_length (type);
1374       g_string_append_len (string, (const gchar *) type, size);
1375     }
1376   g_string_append_c (string, ')');
1377
1378   return (GVariantType *) g_string_free (string, FALSE);
1379 }
1380
1381 GVariantType *
1382 g_variant_type_new_tuple (const GVariantType * const *items,
1383                           gint                        length)
1384 {
1385   char buffer[1024];
1386   gsize offset;
1387   gsize i;
1388
1389   g_return_val_if_fail (length == 0 || items != NULL, NULL);
1390
1391   if (length < 0)
1392     for (length = 0; items[length] != NULL; length++);
1393
1394   offset = 0;
1395   buffer[offset++] = '(';
1396
1397   for (i = 0; i < length; i++)
1398     {
1399       const GVariantType *type;
1400       gsize size;
1401
1402       g_return_val_if_fail (g_variant_type_check (items[i]), NULL);
1403
1404       type = items[i];
1405       size = g_variant_type_get_string_length (type);
1406
1407       if (offset + size >= sizeof buffer) /* leave room for ')' */
1408         return g_variant_type_new_tuple_slow (items, length);
1409
1410       memcpy (&buffer[offset], type, size);
1411       offset += size;
1412     }
1413
1414   g_assert (offset < sizeof buffer);
1415   buffer[offset++] = ')';
1416
1417   return (GVariantType *) g_memdup (buffer, offset);
1418 }
1419
1420 /**
1421  * g_variant_type_new_array: (constructor)
1422  * @element: a #GVariantType
1423  *
1424  * Constructs the type corresponding to an array of elements of the
1425  * type @type.
1426  *
1427  * It is appropriate to call g_variant_type_free() on the return value.
1428  *
1429  * Returns: (transfer full): a new array #GVariantType
1430  *
1431  * Since 2.24
1432  **/
1433 GVariantType *
1434 g_variant_type_new_array (const GVariantType *element)
1435 {
1436   gsize size;
1437   gchar *new;
1438
1439   g_return_val_if_fail (g_variant_type_check (element), NULL);
1440
1441   size = g_variant_type_get_string_length (element);
1442   new = g_malloc (size + 1);
1443
1444   new[0] = 'a';
1445   memcpy (new + 1, element, size);
1446
1447   return (GVariantType *) new;
1448 }
1449
1450 /**
1451  * g_variant_type_new_maybe: (constructor)
1452  * @element: a #GVariantType
1453  *
1454  * Constructs the type corresponding to a maybe instance containing
1455  * type @type or Nothing.
1456  *
1457  * It is appropriate to call g_variant_type_free() on the return value.
1458  *
1459  * Returns: (transfer full): a new maybe #GVariantType
1460  *
1461  * Since 2.24
1462  **/
1463 GVariantType *
1464 g_variant_type_new_maybe (const GVariantType *element)
1465 {
1466   gsize size;
1467   gchar *new;
1468
1469   g_return_val_if_fail (g_variant_type_check (element), NULL);
1470
1471   size = g_variant_type_get_string_length (element);
1472   new = g_malloc (size + 1);
1473
1474   new[0] = 'm';
1475   memcpy (new + 1, element, size);
1476
1477   return (GVariantType *) new;
1478 }
1479
1480 /**
1481  * g_variant_type_new_dict_entry: (constructor)
1482  * @key: a basic #GVariantType
1483  * @value: a #GVariantType
1484  *
1485  * Constructs the type corresponding to a dictionary entry with a key
1486  * of type @key and a value of type @value.
1487  *
1488  * It is appropriate to call g_variant_type_free() on the return value.
1489  *
1490  * Returns: (transfer full): a new dictionary entry #GVariantType
1491  *
1492  * Since 2.24
1493  **/
1494 GVariantType *
1495 g_variant_type_new_dict_entry (const GVariantType *key,
1496                                const GVariantType *value)
1497 {
1498   gsize keysize, valsize;
1499   gchar *new;
1500
1501   g_return_val_if_fail (g_variant_type_check (key), NULL);
1502   g_return_val_if_fail (g_variant_type_check (value), NULL);
1503
1504   keysize = g_variant_type_get_string_length (key);
1505   valsize = g_variant_type_get_string_length (value);
1506
1507   new = g_malloc (1 + keysize + valsize + 1);
1508
1509   new[0] = '{';
1510   memcpy (new + 1, key, keysize);
1511   memcpy (new + 1 + keysize, value, valsize);
1512   new[1 + keysize + valsize] = '}';
1513
1514   return (GVariantType *) new;
1515 }
1516
1517 /* private */
1518 const GVariantType *
1519 g_variant_type_checked_ (const gchar *type_string)
1520 {
1521   g_return_val_if_fail (g_variant_type_string_is_valid (type_string), NULL);
1522   return (const GVariantType *) type_string;
1523 }