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