LCOV - code coverage report
Current view: top level - glib - gvarianttype.c (source / functions) Coverage Total Hit
Test: unnamed Lines: 99.3 % 267 265
Test Date: 2026-01-20 05:15:58 Functions: 100.0 % 34 34
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Copyright © 2007, 2008 Ryan Lortie
       3                 :             :  * Copyright © 2009, 2010 Codethink Limited
       4                 :             :  *
       5                 :             :  * SPDX-License-Identifier: LGPL-2.1-or-later
       6                 :             :  *
       7                 :             :  * This library is free software; you can redistribute it and/or
       8                 :             :  * modify it under the terms of the GNU Lesser General Public
       9                 :             :  * License as published by the Free Software Foundation; either
      10                 :             :  * version 2.1 of the License, or (at your option) any later version.
      11                 :             :  *
      12                 :             :  * This library is distributed in the hope that it will be useful,
      13                 :             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15                 :             :  * Lesser General Public License for more details.
      16                 :             :  *
      17                 :             :  * You should have received a copy of the GNU Lesser General Public
      18                 :             :  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
      19                 :             :  *
      20                 :             :  * Author: Ryan Lortie <desrt@desrt.ca>
      21                 :             :  */
      22                 :             : 
      23                 :             : #include "config.h"
      24                 :             : 
      25                 :             : #include "gvarianttype-private.h"
      26                 :             : 
      27                 :             : #include <glib/gtestutils.h>
      28                 :             : #include <glib/gstrfuncs.h>
      29                 :             : #include <glib/gvariant-internal.h>
      30                 :             : 
      31                 :             : #include <string.h>
      32                 :             : 
      33                 :             : 
      34                 :             : /**
      35                 :             :  * GVariantType:
      36                 :             :  *
      37                 :             :  * A type in the [type@GLib.Variant] type system.
      38                 :             :  *
      39                 :             :  * [type@GLib.Variant] types are represented as strings, but have a strict
      40                 :             :  * syntax described below. All [type@GLib.VariantType]s passed to GLib must be
      41                 :             :  * valid, and they are typically expected to be static (i.e. not provided by
      42                 :             :  * user input) as they determine how binary [type@GLib.Variant] data is
      43                 :             :  * interpreted.
      44                 :             :  *
      45                 :             :  * To convert a static string to a [type@GLib.VariantType] in C, use the
      46                 :             :  * [func@GLib.VARIANT_TYPE] casting macro. When GLib is compiled with checks
      47                 :             :  * enabled, it will validate the type. To check if an arbitrary string is a
      48                 :             :  * valid [type@GLib.VariantType], use [func@GLib.VariantType.string_is_valid].
      49                 :             :  *
      50                 :             :  * ## GVariant Type System
      51                 :             :  *
      52                 :             :  * This section introduces the [type@GLib.Variant] type system. It is based, in
      53                 :             :  * large part, on the D-Bus type system, with two major changes and
      54                 :             :  * some minor lifting of restrictions. The
      55                 :             :  * [D-Bus specification](http://dbus.freedesktop.org/doc/dbus-specification.html),
      56                 :             :  * therefore, provides a significant amount of
      57                 :             :  * information that is useful when working with [type@GLib.Variant].
      58                 :             :  *
      59                 :             :  * The first major change with respect to the D-Bus type system is the
      60                 :             :  * introduction of maybe (or ‘nullable’) types.  Any type in [type@GLib.Variant]
      61                 :             :  * can be converted to a maybe type, in which case, `nothing` (or `null`)
      62                 :             :  * becomes a valid value.  Maybe types have been added by introducing the
      63                 :             :  * character `m` to type strings.
      64                 :             :  *
      65                 :             :  * The second major change is that the [type@GLib.Variant] type system supports
      66                 :             :  * the concept of ‘indefinite types’ — types that are less specific than
      67                 :             :  * the normal types found in D-Bus.  For example, it is possible to speak
      68                 :             :  * of ‘an array of any type’ in [type@GLib.Variant], where the D-Bus type system
      69                 :             :  * would require you to speak of ‘an array of integers’ or ‘an array of
      70                 :             :  * strings’.  Indefinite types have been added by introducing the
      71                 :             :  * characters `*`, `?` and `r` to type strings.
      72                 :             :  *
      73                 :             :  * Finally, all arbitrary restrictions relating to the complexity of
      74                 :             :  * types are lifted along with the restriction that dictionary entries
      75                 :             :  * may only appear nested inside of arrays.
      76                 :             :  *
      77                 :             :  * Just as in D-Bus, [type@GLib.Variant] types are described with strings (‘type
      78                 :             :  * strings’).  Subject to the differences mentioned above, these strings
      79                 :             :  * are of the same form as those found in D-Bus.  Note, however: D-Bus
      80                 :             :  * always works in terms of messages and therefore individual type
      81                 :             :  * strings appear nowhere in its interface.  Instead, ‘signatures’
      82                 :             :  * are a concatenation of the strings of the type of each argument in a
      83                 :             :  * message.  [type@GLib.Variant] deals with single values directly so
      84                 :             :  * [type@GLib.Variant] type strings always describe the type of exactly one
      85                 :             :  * value.  This means that a D-Bus signature string is generally not a valid
      86                 :             :  * [type@GLib.Variant] type string — except in the case that it is the signature
      87                 :             :  * of a message containing exactly one argument.
      88                 :             :  *
      89                 :             :  * An indefinite type is similar in spirit to what may be called an
      90                 :             :  * abstract type in other type systems.  No value can exist that has an
      91                 :             :  * indefinite type as its type, but values can exist that have types
      92                 :             :  * that are subtypes of indefinite types.  That is to say,
      93                 :             :  * [method@GLib.Variant.get_type] will never return an indefinite type, but
      94                 :             :  * calling [method@GLib.Variant.is_of_type] with an indefinite type may return
      95                 :             :  * true.  For example, you cannot have a value that represents ‘an
      96                 :             :  * array of no particular type’, but you can have an ‘array of integers’
      97                 :             :  * which certainly matches the type of ‘an array of no particular type’,
      98                 :             :  * since ‘array of integers’ is a subtype of ‘array of no particular
      99                 :             :  * type’.
     100                 :             :  *
     101                 :             :  * This is similar to how instances of abstract classes may not
     102                 :             :  * directly exist in other type systems, but instances of their
     103                 :             :  * non-abstract subtypes may.  For example, in GTK, no object that has
     104                 :             :  * the type of [`GtkWidget`](https://docs.gtk.org/gtk4/class.Widget.html) can
     105                 :             :  * exist (since `GtkWidget` is an abstract class), but a [`GtkWindow`](https://docs.gtk.org/gtk4/class.Window.html)
     106                 :             :  * can certainly be instantiated, and you would say that a `GtkWindow` is a
     107                 :             :  * `GtkWidget` (since `GtkWindow` is a subclass of `GtkWidget`).
     108                 :             :  *
     109                 :             :  * Two types may not be compared by value; use [method@GLib.VariantType.equal]
     110                 :             :  * or [method@GLib.VariantType.is_subtype_of]  May be copied using
     111                 :             :  * [method@GLib.VariantType.copy] and freed using [method@GLib.VariantType.free].
     112                 :             :  *
     113                 :             :  * ## GVariant Type Strings
     114                 :             :  *
     115                 :             :  * A [type@GLib.Variant] type string can be any of the following:
     116                 :             :  *
     117                 :             :  * - any basic type string (listed below)
     118                 :             :  * - `v`, `r` or `*`
     119                 :             :  * - one of the characters `a` or `m`, followed by another type string
     120                 :             :  * - the character `(`, followed by a concatenation of zero or more other
     121                 :             :  *   type strings, followed by the character `)`
     122                 :             :  * - the character `{`, followed by a basic type string (see below),
     123                 :             :  *   followed by another type string, followed by the character `}`
     124                 :             :  *
     125                 :             :  * A basic type string describes a basic type (as per
     126                 :             :  * [method@GLib.VariantType.is_basic]) and is always a single character in
     127                 :             :  * length. The valid basic type strings are `b`, `y`, `n`, `q`, `i`, `u`, `x`,
     128                 :             :  * `t`, `h`, `d`, `s`, `o`, `g` and `?`.
     129                 :             :  *
     130                 :             :  * The above definition is recursive to arbitrary depth. `aaaaai` and
     131                 :             :  * `(ui(nq((y)))s)` are both valid type strings, as is
     132                 :             :  * `a(aa(ui)(qna{ya(yd)}))`. In order to not hit memory limits,
     133                 :             :  * [type@GLib.Variant] imposes a limit on recursion depth of 65 nested
     134                 :             :  * containers. This is the limit in the D-Bus specification (64) plus one to
     135                 :             :  * allow a [`GDBusMessage`](../gio/class.DBusMessage.html) to be nested in
     136                 :             :  * a top-level tuple.
     137                 :             :  *
     138                 :             :  * The meaning of each of the characters is as follows:
     139                 :             :  *
     140                 :             :  * - `b`: the type string of `G_VARIANT_TYPE_BOOLEAN`; a boolean value.
     141                 :             :  * - `y`: the type string of `G_VARIANT_TYPE_BYTE`; a byte.
     142                 :             :  * - `n`: the type string of `G_VARIANT_TYPE_INT16`; a signed 16 bit integer.
     143                 :             :  * - `q`: the type string of `G_VARIANT_TYPE_UINT16`; an unsigned 16 bit integer.
     144                 :             :  * - `i`: the type string of `G_VARIANT_TYPE_INT32`; a signed 32 bit integer.
     145                 :             :  * - `u`: the type string of `G_VARIANT_TYPE_UINT32`; an unsigned 32 bit integer.
     146                 :             :  * - `x`: the type string of `G_VARIANT_TYPE_INT64`; a signed 64 bit integer.
     147                 :             :  * - `t`: the type string of `G_VARIANT_TYPE_UINT64`; an unsigned 64 bit integer.
     148                 :             :  * - `h`: the type string of `G_VARIANT_TYPE_HANDLE`; a signed 32 bit value
     149                 :             :  *   that, by convention, is used as an index into an array of file
     150                 :             :  *   descriptors that are sent alongside a D-Bus message.
     151                 :             :  * - `d`: the type string of `G_VARIANT_TYPE_DOUBLE`; a double precision
     152                 :             :  *   floating point value.
     153                 :             :  * - `s`: the type string of `G_VARIANT_TYPE_STRING`; a string.
     154                 :             :  * - `o`: the type string of `G_VARIANT_TYPE_OBJECT_PATH`; a string in the form
     155                 :             :  *   of a D-Bus object path.
     156                 :             :  * - `g`: the type string of `G_VARIANT_TYPE_SIGNATURE`; a string in the form of
     157                 :             :  *   a D-Bus type signature.
     158                 :             :  * - `?`: the type string of `G_VARIANT_TYPE_BASIC`; an indefinite type that
     159                 :             :  *   is a supertype of any of the basic types.
     160                 :             :  * - `v`: the type string of `G_VARIANT_TYPE_VARIANT`; a container type that
     161                 :             :  *   contain any other type of value.
     162                 :             :  * - `a`: used as a prefix on another type string to mean an array of that
     163                 :             :  *   type; the type string `ai`, for example, is the type of an array of
     164                 :             :  *   signed 32-bit integers.
     165                 :             :  * - `m`: used as a prefix on another type string to mean a ‘maybe’, or
     166                 :             :  *   ‘nullable’, version of that type; the type string `ms`, for example,
     167                 :             :  *   is the type of a value that maybe contains a string, or maybe contains
     168                 :             :  *   nothing.
     169                 :             :  * - `()`: used to enclose zero or more other concatenated type strings to
     170                 :             :  *   create a tuple type; the type string `(is)`, for example, is the type of
     171                 :             :  *   a pair of an integer and a string.
     172                 :             :  * - `r`: the type string of `G_VARIANT_TYPE_TUPLE`; an indefinite type that is
     173                 :             :  *   a supertype of any tuple type, regardless of the number of items.
     174                 :             :  * - `{}`: used to enclose a basic type string concatenated with another type
     175                 :             :  *   string to create a dictionary entry type, which usually appears inside of
     176                 :             :  *   an array to form a dictionary; the type string `a{sd}`, for example, is
     177                 :             :  *   the type of a dictionary that maps strings to double precision floating
     178                 :             :  *   point values.
     179                 :             :  *
     180                 :             :  *   The first type (the basic type) is the key type and the second type is
     181                 :             :  *   the value type. The reason that the first type is restricted to being a
     182                 :             :  *   basic type is so that it can easily be hashed.
     183                 :             :  * - `*`: the type string of `G_VARIANT_TYPE_ANY`; the indefinite type that is
     184                 :             :  *   a supertype of all types.  Note that, as with all type strings, this
     185                 :             :  *   character represents exactly one type. It cannot be used inside of tuples
     186                 :             :  *   to mean ‘any number of items’.
     187                 :             :  *
     188                 :             :  * Any type string of a container that contains an indefinite type is,
     189                 :             :  * itself, an indefinite type. For example, the type string `a*`
     190                 :             :  * (corresponding to `G_VARIANT_TYPE_ARRAY`) is an indefinite type
     191                 :             :  * that is a supertype of every array type. `(*s)` is a supertype
     192                 :             :  * of all tuples that contain exactly two items where the second
     193                 :             :  * item is a string.
     194                 :             :  *
     195                 :             :  * `a{?*}` is an indefinite type that is a supertype of all arrays
     196                 :             :  * containing dictionary entries where the key is any basic type and
     197                 :             :  * the value is any type at all.  This is, by definition, a dictionary,
     198                 :             :  * so this type string corresponds to `G_VARIANT_TYPE_DICTIONARY`. Note
     199                 :             :  * that, due to the restriction that the key of a dictionary entry must
     200                 :             :  * be a basic type, `{**}` is not a valid type string.
     201                 :             :  *
     202                 :             :  * Since: 2.24
     203                 :             :  */
     204                 :             : 
     205                 :             : 
     206                 :             : static gboolean
     207                 :    88734570 : g_variant_type_check (const GVariantType *type)
     208                 :             : {
     209                 :    88734570 :   if (type == NULL)
     210                 :           0 :     return FALSE;
     211                 :             : 
     212                 :             : #if 0
     213                 :             :   return g_variant_type_string_scan ((const gchar *) type, NULL, NULL);
     214                 :             : #else
     215                 :    88734570 :   return TRUE;
     216                 :             : #endif
     217                 :             : }
     218                 :             : 
     219                 :             : static gboolean
     220                 :    60779836 : variant_type_string_scan_internal (const gchar  *string,
     221                 :             :                                    const gchar  *limit,
     222                 :             :                                    const gchar **endptr,
     223                 :             :                                    gsize        *depth,
     224                 :             :                                    gsize         depth_limit)
     225                 :             : {
     226                 :    60779836 :   gsize max_depth = 0, child_depth;
     227                 :             : 
     228                 :    60779836 :   g_return_val_if_fail (string != NULL, FALSE);
     229                 :             : 
     230                 :    60779836 :   if (string == limit || *string == '\0')
     231                 :         888 :     return FALSE;
     232                 :             : 
     233                 :    60778948 :   switch (*string++)
     234                 :             :     {
     235                 :      256414 :     case '(':
     236                 :     2419155 :       while (string == limit || *string != ')')
     237                 :             :         {
     238                 :     4327037 :           if (depth_limit == 0 ||
     239                 :     2163518 :               !variant_type_string_scan_internal (string, limit, &string,
     240                 :             :                                                   &child_depth,
     241                 :             :                                                   depth_limit - 1))
     242                 :         778 :             return FALSE;
     243                 :             : 
     244                 :     2162741 :           max_depth = MAX (max_depth, child_depth + 1);
     245                 :             :         }
     246                 :             : 
     247                 :      255636 :       string++;
     248                 :      255636 :       break;
     249                 :             : 
     250                 :      429416 :     case '{':
     251                 :      429416 :       if (depth_limit == 0 ||
     252                 :      429416 :           string == limit || *string == '\0' ||                                  /* { */
     253                 :      858684 :           !strchr ("bynqihuxtdsog?", *string++) ||                               /* key */
     254                 :      429280 :           !variant_type_string_scan_internal (string, limit, &string,
     255                 :      429114 :                                               &child_depth, depth_limit - 1) ||  /* value */
     256                 :      429114 :           string == limit || *string++ != '}')                                   /* } */
     257                 :         437 :         return FALSE;
     258                 :             : 
     259                 :      428979 :       max_depth = MAX (max_depth, child_depth + 1);
     260                 :      428979 :       break;
     261                 :             : 
     262                 :      863343 :     case 'm': case 'a':
     263                 :     1726685 :       if (depth_limit == 0 ||
     264                 :      863342 :           !variant_type_string_scan_internal (string, limit, &string,
     265                 :             :                                               &child_depth, depth_limit - 1))
     266                 :         514 :         return FALSE;
     267                 :             : 
     268                 :      862829 :       max_depth = MAX (max_depth, child_depth + 1);
     269                 :      862829 :       break;
     270                 :             : 
     271                 :    59228714 :     case 'b': case 'y': case 'n': case 'q': case 'i': case 'u':
     272                 :             :     case 'x': case 't': case 'd': case 's': case 'o': case 'g':
     273                 :             :     case 'v': case 'r': case '*': case '?': case 'h':
     274                 :    59228714 :       max_depth = MAX (max_depth, 1);
     275                 :    59228714 :       break;
     276                 :             : 
     277                 :        1061 :     default:
     278                 :        1061 :       return FALSE;
     279                 :             :     }
     280                 :             : 
     281                 :    60776158 :   if (endptr != NULL)
     282                 :    60776158 :     *endptr = string;
     283                 :    60776158 :   if (depth != NULL)
     284                 :     4080201 :     *depth = max_depth;
     285                 :             : 
     286                 :    60776158 :   return TRUE;
     287                 :             : }
     288                 :             : 
     289                 :             : /**
     290                 :             :  * g_variant_type_string_scan:
     291                 :             :  * @string: a pointer to any string
     292                 :             :  * @limit: (nullable): the end of @string
     293                 :             :  * @endptr: (out) (optional): location to store the end pointer
     294                 :             :  *
     295                 :             :  * Scan for a single complete and valid GVariant type string in @string.
     296                 :             :  *
     297                 :             :  * The memory pointed to by @limit (or bytes beyond it) is never
     298                 :             :  * accessed.
     299                 :             :  *
     300                 :             :  * If a valid type string is found, @endptr is updated to point to the
     301                 :             :  * first character past the end of the string that was found and %TRUE
     302                 :             :  * is returned.
     303                 :             :  *
     304                 :             :  * If there is no valid type string starting at @string, or if the type
     305                 :             :  * string does not end before @limit then %FALSE is returned.
     306                 :             :  *
     307                 :             :  * For the simple case of checking if a string is a valid type string,
     308                 :             :  * see [func@GLib.VariantType.string_is_valid].
     309                 :             :  *
     310                 :             :  * Returns: true if a valid type string was found
     311                 :             :  * Since: 2.24
     312                 :             :  **/
     313                 :             : gboolean
     314                 :    56698179 : g_variant_type_string_scan (const gchar  *string,
     315                 :             :                             const gchar  *limit,
     316                 :             :                             const gchar **endptr)
     317                 :             : {
     318                 :    56698179 :   return variant_type_string_scan_internal (string, limit, endptr, NULL,
     319                 :             :                                             G_VARIANT_MAX_RECURSION_DEPTH);
     320                 :             : }
     321                 :             : 
     322                 :             : /* < private >
     323                 :             :  * g_variant_type_string_get_depth_:
     324                 :             :  * @type_string: a pointer to any string
     325                 :             :  *
     326                 :             :  * Get the maximum depth of the nested types in @type_string. A basic type will
     327                 :             :  * return depth 1, and a container type will return a greater value. The depth
     328                 :             :  * of a tuple is 1 plus the depth of its deepest child type.
     329                 :             :  *
     330                 :             :  * If @type_string is not a valid [type@GLib.Variant] type string, `0` will be returned.
     331                 :             :  *
     332                 :             :  * Returns: depth of @type_string, or `0` on error
     333                 :             :  * Since: 2.60
     334                 :             :  */
     335                 :             : gsize
     336                 :      625517 : g_variant_type_string_get_depth_ (const gchar *type_string)
     337                 :             : {
     338                 :             :   const gchar *endptr;
     339                 :      625517 :   gsize depth = 0;
     340                 :             : 
     341                 :      625517 :   g_return_val_if_fail (type_string != NULL, 0);
     342                 :             : 
     343                 :      625517 :   if (!variant_type_string_scan_internal (type_string, NULL, &endptr, &depth,
     344                 :      625517 :                                           G_VARIANT_MAX_RECURSION_DEPTH) ||
     345                 :      625517 :       *endptr != '\0')
     346                 :           0 :     return 0;
     347                 :             : 
     348                 :      625517 :   return depth;
     349                 :             : }
     350                 :             : 
     351                 :             : /**
     352                 :             :  * g_variant_type_string_is_valid:
     353                 :             :  * @type_string: a pointer to any string
     354                 :             :  *
     355                 :             :  * Checks if @type_string is a valid
     356                 :             :  * [GVariant type string](./struct.VariantType.html#gvariant-type-strings).
     357                 :             :  *
     358                 :             :  * This call is equivalent to calling [func@GLib.VariantType.string_scan] and
     359                 :             :  * confirming that the following character is a nul terminator.
     360                 :             :  *
     361                 :             :  * Returns: true if @type_string is exactly one valid type string
     362                 :             :  * Since 2.24
     363                 :             :  **/
     364                 :             : gboolean
     365                 :      789694 : g_variant_type_string_is_valid (const gchar *type_string)
     366                 :             : {
     367                 :             :   const gchar *endptr;
     368                 :             : 
     369                 :      789694 :   g_return_val_if_fail (type_string != NULL, FALSE);
     370                 :             : 
     371                 :      789694 :   if (!g_variant_type_string_scan (type_string, NULL, &endptr))
     372                 :        1952 :     return FALSE;
     373                 :             : 
     374                 :      787742 :   return *endptr == '\0';
     375                 :             : }
     376                 :             : 
     377                 :             : /**
     378                 :             :  * g_variant_type_free:
     379                 :             :  * @type: (nullable): type to free
     380                 :             :  *
     381                 :             :  * Frees a [type@GLib.VariantType] that was allocated with
     382                 :             :  * [method@GLib.VariantType.copy], [ctor@GLib.VariantType.new] or one of the
     383                 :             :  * container type constructor functions.
     384                 :             :  *
     385                 :             :  * In the case that @type is `NULL`, this function does nothing.
     386                 :             :  *
     387                 :             :  * Since 2.24
     388                 :             :  **/
     389                 :             : void
     390                 :     3836207 : g_variant_type_free (GVariantType *type)
     391                 :             : {
     392                 :     3836207 :   g_return_if_fail (type == NULL || g_variant_type_check (type));
     393                 :             : 
     394                 :     3836207 :   g_free (type);
     395                 :             : }
     396                 :             : 
     397                 :             : /**
     398                 :             :  * g_variant_type_copy:
     399                 :             :  * @type: (not nullable): type to copy
     400                 :             :  *
     401                 :             :  * Makes a copy of a [type@GLib.VariantType].
     402                 :             :  *
     403                 :             :  * It is appropriate to call [method@GLib.VariantType.free] on the return value.
     404                 :             :  * @type may not be `NULL`.
     405                 :             :  *
     406                 :             :  * Returns: (transfer full): a new [type@GLib.VariantType]
     407                 :             :  * Since 2.24
     408                 :             :  **/
     409                 :             : GVariantType *
     410                 :      430665 : g_variant_type_copy (const GVariantType *type)
     411                 :             : {
     412                 :             :   gsize length;
     413                 :             :   gchar *new;
     414                 :             : 
     415                 :      430665 :   g_return_val_if_fail (g_variant_type_check (type), NULL);
     416                 :             : 
     417                 :      430665 :   length = g_variant_type_get_string_length (type);
     418                 :      430665 :   new = g_malloc (length + 1);
     419                 :             : 
     420                 :      430665 :   memcpy (new, type, length);
     421                 :      430665 :   new[length] = '\0';
     422                 :             : 
     423                 :      430665 :   return (GVariantType *) new;
     424                 :             : }
     425                 :             : 
     426                 :             : /**
     427                 :             :  * g_variant_type_new:
     428                 :             :  * @type_string: a valid [GVariant type string](./struct.VariantType.html#gvariant-type-strings)
     429                 :             :  *
     430                 :             :  * Creates a new [type@GLib.VariantType] corresponding to the type string given
     431                 :             :  * by @type_string.
     432                 :             :  *
     433                 :             :  * It is appropriate to call [method@GLib.VariantType.free] on the return value.
     434                 :             :  *
     435                 :             :  * It is a programmer error to call this function with an invalid type
     436                 :             :  * string.  Use [func@GLib.VariantType.string_is_valid] if you are unsure.
     437                 :             :  *
     438                 :             :  * Returns: (transfer full): a new [type@GLib.VariantType]
     439                 :             :  * Since: 2.24
     440                 :             :  */
     441                 :             : GVariantType *
     442                 :      175761 : g_variant_type_new (const gchar *type_string)
     443                 :             : {
     444                 :      175761 :   g_return_val_if_fail (type_string != NULL, NULL);
     445                 :             : 
     446                 :      175761 :   return g_variant_type_copy (G_VARIANT_TYPE (type_string));
     447                 :             : }
     448                 :             : 
     449                 :             : /**
     450                 :             :  * g_variant_type_get_string_length:
     451                 :             :  * @type: type to measure
     452                 :             :  *
     453                 :             :  * Returns the length of the type string corresponding to the given @type.
     454                 :             :  *
     455                 :             :  * This function must be used to determine the valid extent of
     456                 :             :  * the memory region returned by [method@GLib.VariantType.peek_string].
     457                 :             :  *
     458                 :             :  * Returns: the length of the corresponding type string
     459                 :             :  * Since 2.24
     460                 :             :  **/
     461                 :             : gsize
     462                 :     8694567 : g_variant_type_get_string_length (const GVariantType *type)
     463                 :             : {
     464                 :     8694567 :   const gchar *type_string = (const gchar *) type;
     465                 :     8694567 :   gint brackets = 0;
     466                 :     8694567 :   gsize index = 0;
     467                 :             : 
     468                 :     8694567 :   g_return_val_if_fail (g_variant_type_check (type), 0);
     469                 :             : 
     470                 :             :   do
     471                 :             :     {
     472                 :    31306739 :       while (type_string[index] == 'a' || type_string[index] == 'm')
     473                 :     3395498 :         index++;
     474                 :             : 
     475                 :    27911241 :       if (type_string[index] == '(' || type_string[index] == '{')
     476                 :     3223235 :         brackets++;
     477                 :             : 
     478                 :    24688006 :       else if (type_string[index] == ')' || type_string[index] == '}')
     479                 :     3223235 :         brackets--;
     480                 :             : 
     481                 :    27911241 :       index++;
     482                 :             :     }
     483                 :    27911241 :   while (brackets);
     484                 :             : 
     485                 :     8694567 :   return index;
     486                 :             : }
     487                 :             : 
     488                 :             : /*
     489                 :             :   This function is not introspectable, it returns something that
     490                 :             :   is not an array and neither a string
     491                 :             : */
     492                 :             : /**
     493                 :             :  * g_variant_type_peek_string: (skip)
     494                 :             :  * @type: type to peek at
     495                 :             :  *
     496                 :             :  * Returns the type string corresponding to the given @type.
     497                 :             :  *
     498                 :             :  * The result is not nul-terminated; in order to determine its length you
     499                 :             :  * must call [method@GLib.VariantType.get_string_length].
     500                 :             :  *
     501                 :             :  * To get a nul-terminated string, see [method@GLib.VariantType.dup_string].
     502                 :             :  *
     503                 :             :  * Returns: the corresponding type string (not nul-terminated)
     504                 :             :  * Since 2.24
     505                 :             :  **/
     506                 :             : const gchar *
     507                 :    37115506 : g_variant_type_peek_string (const GVariantType *type)
     508                 :             : {
     509                 :    37115506 :   g_return_val_if_fail (g_variant_type_check (type), NULL);
     510                 :             : 
     511                 :    37115506 :   return (const gchar *) type;
     512                 :             : }
     513                 :             : 
     514                 :             : /**
     515                 :             :  * g_variant_type_dup_string:
     516                 :             :  * @type: type to copy
     517                 :             :  *
     518                 :             :  * Returns a newly-allocated copy of the type string corresponding to @type.
     519                 :             :  *
     520                 :             :  * The returned string is nul-terminated.  It is appropriate to call
     521                 :             :  * [func@GLib.free] on the return value.
     522                 :             :  *
     523                 :             :  * Returns: (transfer full): the corresponding type string
     524                 :             :  * Since 2.24
     525                 :             :  **/
     526                 :             : gchar *
     527                 :       32667 : g_variant_type_dup_string (const GVariantType *type)
     528                 :             : {
     529                 :       32667 :   g_return_val_if_fail (g_variant_type_check (type), NULL);
     530                 :             : 
     531                 :       32667 :   return g_strndup (g_variant_type_peek_string (type),
     532                 :             :                     g_variant_type_get_string_length (type));
     533                 :             : }
     534                 :             : 
     535                 :             : /**
     536                 :             :  * g_variant_type_is_definite:
     537                 :             :  * @type: type to check
     538                 :             :  *
     539                 :             :  * Determines if the given @type is definite (ie: not indefinite).
     540                 :             :  *
     541                 :             :  * A type is definite if its type string does not contain any indefinite
     542                 :             :  * type characters (`*`, `?`, or `r`).
     543                 :             :  *
     544                 :             :  * A [type@GLib.Variant] instance may not have an indefinite type, so calling
     545                 :             :  * this function on the result of [method@GLib.Variant.get_type] will always
     546                 :             :  * result in true being returned.  Calling this function on an
     547                 :             :  * indefinite type like `G_VARIANT_TYPE_ARRAY`, however, will result in
     548                 :             :  * `FALSE` being returned.
     549                 :             :  *
     550                 :             :  * Returns: true if @type is definite
     551                 :             :  * Since 2.24
     552                 :             :  **/
     553                 :             : gboolean
     554                 :      355842 : g_variant_type_is_definite (const GVariantType *type)
     555                 :             : {
     556                 :             :   const gchar *type_string;
     557                 :             :   gsize type_length;
     558                 :             :   gsize i;
     559                 :             : 
     560                 :      355842 :   g_return_val_if_fail (g_variant_type_check (type), FALSE);
     561                 :             : 
     562                 :      355842 :   type_length = g_variant_type_get_string_length (type);
     563                 :      355842 :   type_string = g_variant_type_peek_string (type);
     564                 :             : 
     565                 :     1131115 :   for (i = 0; i < type_length; i++)
     566                 :      872680 :     if (type_string[i] == '*' ||
     567                 :      867808 :         type_string[i] == '?' ||
     568                 :      788526 :         type_string[i] == 'r')
     569                 :       97407 :       return FALSE;
     570                 :             : 
     571                 :      258435 :   return TRUE;
     572                 :             : }
     573                 :             : 
     574                 :             : /**
     575                 :             :  * g_variant_type_is_container:
     576                 :             :  * @type: type to check
     577                 :             :  *
     578                 :             :  * Determines if the given @type is a container type.
     579                 :             :  *
     580                 :             :  * Container types are any array, maybe, tuple, or dictionary
     581                 :             :  * entry types plus the variant type.
     582                 :             :  *
     583                 :             :  * This function returns true for any indefinite type for which every
     584                 :             :  * definite subtype is a container — `G_VARIANT_TYPE_ARRAY`, for
     585                 :             :  * example.
     586                 :             :  *
     587                 :             :  * Returns: true if @type is a container type
     588                 :             :  * Since 2.24
     589                 :             :  **/
     590                 :             : gboolean
     591                 :      575766 : g_variant_type_is_container (const GVariantType *type)
     592                 :             : {
     593                 :             :   gchar first_char;
     594                 :             : 
     595                 :      575766 :   g_return_val_if_fail (g_variant_type_check (type), FALSE);
     596                 :             : 
     597                 :      575766 :   first_char = g_variant_type_peek_string (type)[0];
     598                 :      575766 :   switch (first_char)
     599                 :             :   {
     600                 :      224566 :     case 'a':
     601                 :             :     case 'm':
     602                 :             :     case 'r':
     603                 :             :     case '(':
     604                 :             :     case '{':
     605                 :             :     case 'v':
     606                 :      224566 :       return TRUE;
     607                 :             : 
     608                 :      351200 :     default:
     609                 :      351200 :       return FALSE;
     610                 :             :   }
     611                 :             : }
     612                 :             : 
     613                 :             : /**
     614                 :             :  * g_variant_type_is_basic:
     615                 :             :  * @type: type to check
     616                 :             :  *
     617                 :             :  * Determines if the given @type is a basic type.
     618                 :             :  *
     619                 :             :  * Basic types are booleans, bytes, integers, doubles, strings, object
     620                 :             :  * paths and signatures.
     621                 :             :  *
     622                 :             :  * Only a basic type may be used as the key of a dictionary entry.
     623                 :             :  *
     624                 :             :  * This function returns `FALSE` for all indefinite types except
     625                 :             :  * `G_VARIANT_TYPE_BASIC`.
     626                 :             :  *
     627                 :             :  * Returns: true if @type is a basic type
     628                 :             :  * Since 2.24
     629                 :             :  **/
     630                 :             : gboolean
     631                 :      122540 : g_variant_type_is_basic (const GVariantType *type)
     632                 :             : {
     633                 :             :   gchar first_char;
     634                 :             : 
     635                 :      122540 :   g_return_val_if_fail (g_variant_type_check (type), FALSE);
     636                 :             : 
     637                 :      122540 :   first_char = g_variant_type_peek_string (type)[0];
     638                 :      122540 :   switch (first_char)
     639                 :             :   {
     640                 :      104704 :     case 'b':
     641                 :             :     case 'y':
     642                 :             :     case 'n':
     643                 :             :     case 'q':
     644                 :             :     case 'i':
     645                 :             :     case 'h':
     646                 :             :     case 'u':
     647                 :             :     case 't':
     648                 :             :     case 'x':
     649                 :             :     case 'd':
     650                 :             :     case 's':
     651                 :             :     case 'o':
     652                 :             :     case 'g':
     653                 :             :     case '?':
     654                 :      104704 :       return TRUE;
     655                 :             : 
     656                 :       17836 :     default:
     657                 :       17836 :       return FALSE;
     658                 :             :   }
     659                 :             : }
     660                 :             : 
     661                 :             : /**
     662                 :             :  * g_variant_type_is_maybe:
     663                 :             :  * @type: type to check
     664                 :             :  *
     665                 :             :  * Determines if the given @type is a ‘maybe’ type.
     666                 :             :  *
     667                 :             :  * This is true if the type string for @type starts with an `m`.
     668                 :             :  *
     669                 :             :  * This function returns true for any indefinite type for which every
     670                 :             :  * definite subtype is a ‘maybe’ type — `G_VARIANT_TYPE_MAYBE`, for
     671                 :             :  * example.
     672                 :             :  *
     673                 :             :  * Returns: true if @type is a ‘maybe’ type
     674                 :             :  * Since 2.24
     675                 :             :  **/
     676                 :             : gboolean
     677                 :     1263248 : g_variant_type_is_maybe (const GVariantType *type)
     678                 :             : {
     679                 :     1263248 :   g_return_val_if_fail (g_variant_type_check (type), FALSE);
     680                 :             : 
     681                 :     1263248 :   return g_variant_type_peek_string (type)[0] == 'm';
     682                 :             : }
     683                 :             : 
     684                 :             : /**
     685                 :             :  * g_variant_type_is_array:
     686                 :             :  * @type: type to check
     687                 :             :  *
     688                 :             :  * Determines if the given @type is an array type.
     689                 :             :  *
     690                 :             :  * This is true if the type string for @type starts with an `a`.
     691                 :             :  *
     692                 :             :  * This function returns true for any indefinite type for which every
     693                 :             :  * definite subtype is an array type — `G_VARIANT_TYPE_ARRAY`, for
     694                 :             :  * example.
     695                 :             :  *
     696                 :             :  * Returns: true if @type is an array type
     697                 :             :  * Since 2.24
     698                 :             :  **/
     699                 :             : gboolean
     700                 :     1251467 : g_variant_type_is_array (const GVariantType *type)
     701                 :             : {
     702                 :     1251467 :   g_return_val_if_fail (g_variant_type_check (type), FALSE);
     703                 :             : 
     704                 :     1251467 :   return g_variant_type_peek_string (type)[0] == 'a';
     705                 :             : }
     706                 :             : 
     707                 :             : /**
     708                 :             :  * g_variant_type_is_tuple:
     709                 :             :  * @type: type to check
     710                 :             :  *
     711                 :             :  * Determines if the given @type is a tuple type.
     712                 :             :  *
     713                 :             :  * This is true if the type string for @type starts with a `(` or if @type is
     714                 :             :  * `G_VARIANT_TYPE_TUPLE`.
     715                 :             :  *
     716                 :             :  * This function returns true for any indefinite type for which every
     717                 :             :  * definite subtype is a tuple type — `G_VARIANT_TYPE_TUPLE`, for
     718                 :             :  * example.
     719                 :             :  *
     720                 :             :  * Returns: true if @type is a tuple type
     721                 :             :  * Since 2.24
     722                 :             :  **/
     723                 :             : gboolean
     724                 :     1427323 : g_variant_type_is_tuple (const GVariantType *type)
     725                 :             : {
     726                 :             :   gchar type_char;
     727                 :             : 
     728                 :     1427323 :   g_return_val_if_fail (g_variant_type_check (type), FALSE);
     729                 :             : 
     730                 :     1427323 :   type_char = g_variant_type_peek_string (type)[0];
     731                 :     1427323 :   return type_char == 'r' || type_char == '(';
     732                 :             : }
     733                 :             : 
     734                 :             : /**
     735                 :             :  * g_variant_type_is_dict_entry:
     736                 :             :  * @type: type to check
     737                 :             :  *
     738                 :             :  * Determines if the given @type is a dictionary entry type.
     739                 :             :  *
     740                 :             :  * This is true if the type string for @type starts with a `{`.
     741                 :             :  *
     742                 :             :  * This function returns true for any indefinite type for which every
     743                 :             :  * definite subtype is a dictionary entry type —
     744                 :             :  * `G_VARIANT_TYPE_DICT_ENTRY`, for example.
     745                 :             :  *
     746                 :             :  * Returns: true if @type is a dictionary entry type
     747                 :             :  * Since 2.24
     748                 :             :  **/
     749                 :             : gboolean
     750                 :     1464085 : g_variant_type_is_dict_entry (const GVariantType *type)
     751                 :             : {
     752                 :     1464085 :   g_return_val_if_fail (g_variant_type_check (type), FALSE);
     753                 :             : 
     754                 :     1464085 :   return g_variant_type_peek_string (type)[0] == '{';
     755                 :             : }
     756                 :             : 
     757                 :             : /**
     758                 :             :  * g_variant_type_is_variant:
     759                 :             :  * @type: type to check
     760                 :             :  *
     761                 :             :  * Determines if the given @type is the variant type.
     762                 :             :  *
     763                 :             :  * Returns: true if @type is the variant type
     764                 :             :  * Since 2.24
     765                 :             :  **/
     766                 :             : gboolean
     767                 :      225498 : g_variant_type_is_variant (const GVariantType *type)
     768                 :             : {
     769                 :      225498 :   g_return_val_if_fail (g_variant_type_check (type), FALSE);
     770                 :             : 
     771                 :      225498 :   return g_variant_type_peek_string (type)[0] == 'v';
     772                 :             : }
     773                 :             : 
     774                 :             : /**
     775                 :             :  * g_variant_type_hash:
     776                 :             :  * @type: (type GVariantType): type to hash
     777                 :             :  *
     778                 :             :  * Hashes @type.
     779                 :             :  *
     780                 :             :  * The argument type of @type is only `gconstpointer` to allow use with
     781                 :             :  * [type@GLib.HashTable] without function pointer casting.  A valid
     782                 :             :  * [type@GLib.VariantType] must be provided.
     783                 :             :  *
     784                 :             :  * Returns: the hash value
     785                 :             :  * Since 2.24
     786                 :             :  **/
     787                 :             : guint
     788                 :        4006 : g_variant_type_hash (gconstpointer type)
     789                 :             : {
     790                 :        4006 :   g_return_val_if_fail (g_variant_type_check (type), 0);
     791                 :             : 
     792                 :        4006 :   return _g_variant_type_hash (type);
     793                 :             : }
     794                 :             : 
     795                 :             : /**
     796                 :             :  * g_variant_type_equal:
     797                 :             :  * @type1: (type GVariantType): type to compare
     798                 :             :  * @type2: (type GVariantType): another type to compare
     799                 :             :  *
     800                 :             :  * Compares @type1 and @type2 for equality.
     801                 :             :  *
     802                 :             :  * Only returns true if the types are exactly equal.  Even if one type
     803                 :             :  * is an indefinite type and the other is a subtype of it, false will
     804                 :             :  * be returned if they are not exactly equal.  If you want to check for
     805                 :             :  * subtypes, use [method@GLib.VariantType.is_subtype_of].
     806                 :             :  *
     807                 :             :  * The argument types of @type1 and @type2 are only `gconstpointer` to
     808                 :             :  * allow use with [type@GLib.HashTable] without function pointer casting.  For
     809                 :             :  * both arguments, a valid [type@GLib.VariantType] must be provided.
     810                 :             :  *
     811                 :             :  * Returns: true if @type1 and @type2 are exactly equal
     812                 :             :  * Since 2.24
     813                 :             :  **/
     814                 :             : gboolean
     815                 :     6559179 : g_variant_type_equal (gconstpointer type1,
     816                 :             :                       gconstpointer type2)
     817                 :             : {
     818                 :     6559179 :   g_return_val_if_fail (g_variant_type_check (type1), FALSE);
     819                 :     6559179 :   g_return_val_if_fail (g_variant_type_check (type2), FALSE);
     820                 :             : 
     821                 :     6559179 :   return _g_variant_type_equal (type1, type2);
     822                 :             : }
     823                 :             : 
     824                 :             : /**
     825                 :             :  * g_variant_type_is_subtype_of:
     826                 :             :  * @type: type to check
     827                 :             :  * @supertype: type of potential supertype
     828                 :             :  *
     829                 :             :  * Checks if @type is a subtype of @supertype.
     830                 :             :  *
     831                 :             :  * This function returns true if @type is a subtype of @supertype.  All
     832                 :             :  * types are considered to be subtypes of themselves.  Aside from that,
     833                 :             :  * only indefinite types can have subtypes.
     834                 :             :  *
     835                 :             :  * Returns: true if @type is a subtype of @supertype
     836                 :             :  * Since 2.24
     837                 :             :  **/
     838                 :             : gboolean
     839                 :     7373321 : g_variant_type_is_subtype_of (const GVariantType *type,
     840                 :             :                               const GVariantType *supertype)
     841                 :             : {
     842                 :             :   const gchar *supertype_string;
     843                 :             :   const gchar *supertype_end;
     844                 :             :   const gchar *type_string;
     845                 :             : 
     846                 :     7373321 :   g_return_val_if_fail (g_variant_type_check (type), FALSE);
     847                 :     7373321 :   g_return_val_if_fail (g_variant_type_check (supertype), FALSE);
     848                 :             : 
     849                 :     7373321 :   supertype_string = g_variant_type_peek_string (supertype);
     850                 :     7373321 :   type_string = g_variant_type_peek_string (type);
     851                 :             : 
     852                 :             :   /* fast path for the basic determinate types */
     853                 :     7373321 :   if (type_string[0] == supertype_string[0])
     854                 :             :     {
     855                 :     6619674 :       switch (type_string[0])
     856                 :             :         {
     857                 :     4730278 :         case 'b': case 'y':
     858                 :             :         case 'n': case 'q':
     859                 :             :         case 'i': case 'h': case 'u':
     860                 :             :         case 't': case 'x':
     861                 :             :         case 's': case 'o': case 'g':
     862                 :             :         case 'd':
     863                 :     4730278 :           return TRUE;
     864                 :             : 
     865                 :     1889396 :         default:
     866                 :     1889396 :           break;
     867                 :             :         }
     868                 :             :     }
     869                 :             : 
     870                 :     2643043 :   supertype_end = supertype_string +
     871                 :     2643043 :                   g_variant_type_get_string_length (supertype);
     872                 :             : 
     873                 :             :   /* we know that type and supertype are both well-formed, so it's
     874                 :             :    * safe to treat this merely as a text processing problem.
     875                 :             :    */
     876                 :    11319747 :   while (supertype_string < supertype_end)
     877                 :             :     {
     878                 :     9192873 :       char supertype_char = *supertype_string++;
     879                 :             : 
     880                 :     9192873 :       if (supertype_char == *type_string)
     881                 :     8422298 :         type_string++;
     882                 :             : 
     883                 :      770575 :       else if (*type_string == ')')
     884                 :           1 :         return FALSE;
     885                 :             : 
     886                 :             :       else
     887                 :             :         {
     888                 :      770574 :           const GVariantType *target_type = (GVariantType *) type_string;
     889                 :             : 
     890                 :      770574 :           switch (supertype_char)
     891                 :             :             {
     892                 :       79150 :             case 'r':
     893                 :       79150 :               if (!g_variant_type_is_tuple (target_type))
     894                 :         160 :                 return FALSE;
     895                 :       78990 :               break;
     896                 :             : 
     897                 :       96629 :             case '*':
     898                 :       96629 :               break;
     899                 :             : 
     900                 :       78819 :             case '?':
     901                 :       78819 :               if (!g_variant_type_is_basic (target_type))
     902                 :          32 :                 return FALSE;
     903                 :       78787 :               break;
     904                 :             : 
     905                 :      515976 :             default:
     906                 :      515976 :               return FALSE;
     907                 :             :             }
     908                 :             : 
     909                 :      254406 :           type_string += g_variant_type_get_string_length (target_type);
     910                 :             :         }
     911                 :             :     }
     912                 :             : 
     913                 :     2126874 :   return TRUE;
     914                 :             : }
     915                 :             : 
     916                 :             : /**
     917                 :             :  * g_variant_type_element:
     918                 :             :  * @type: an array or ‘maybe’ type
     919                 :             :  *
     920                 :             :  * Determines the element type of an array or ‘maybe’ type.
     921                 :             :  *
     922                 :             :  * This function may only be used with array or ‘maybe’ types.
     923                 :             :  *
     924                 :             :  * Returns: (transfer none): the element type of @type
     925                 :             :  * Since 2.24
     926                 :             :  **/
     927                 :             : const GVariantType *
     928                 :      360839 : g_variant_type_element (const GVariantType *type)
     929                 :             : {
     930                 :             :   const gchar *type_string;
     931                 :             : 
     932                 :      360839 :   g_return_val_if_fail (g_variant_type_check (type), NULL);
     933                 :             : 
     934                 :      360839 :   type_string = g_variant_type_peek_string (type);
     935                 :             : 
     936                 :      360839 :   g_assert (type_string[0] == 'a' || type_string[0] == 'm');
     937                 :             : 
     938                 :      360839 :   return (const GVariantType *) &type_string[1];
     939                 :             : }
     940                 :             : 
     941                 :             : /**
     942                 :             :  * g_variant_type_first:
     943                 :             :  * @type: a tuple or dictionary entry type
     944                 :             :  *
     945                 :             :  * Determines the first item type of a tuple or dictionary entry
     946                 :             :  * type.
     947                 :             :  *
     948                 :             :  * This function may only be used with tuple or dictionary entry types,
     949                 :             :  * but must not be used with the generic tuple type
     950                 :             :  * `G_VARIANT_TYPE_TUPLE`.
     951                 :             :  *
     952                 :             :  * In the case of a dictionary entry type, this returns the type of
     953                 :             :  * the key.
     954                 :             :  *
     955                 :             :  * `NULL` is returned in case of @type being `G_VARIANT_TYPE_UNIT`.
     956                 :             :  *
     957                 :             :  * This call, together with [method@GLib.VariantType.next] provides an iterator
     958                 :             :  * interface over tuple and dictionary entry types.
     959                 :             :  *
     960                 :             :  * Returns: (transfer none) (nullable): the first item type of @type, or `NULL`
     961                 :             :  *   if the type has no item types
     962                 :             :  * Since 2.24
     963                 :             :  **/
     964                 :             : const GVariantType *
     965                 :      646904 : g_variant_type_first (const GVariantType *type)
     966                 :             : {
     967                 :             :   const gchar *type_string;
     968                 :             : 
     969                 :      646904 :   g_return_val_if_fail (g_variant_type_check (type), NULL);
     970                 :             : 
     971                 :      646904 :   type_string = g_variant_type_peek_string (type);
     972                 :      646904 :   g_assert (type_string[0] == '(' || type_string[0] == '{');
     973                 :             : 
     974                 :      646904 :   if (type_string[1] == ')')
     975                 :        6408 :     return NULL;
     976                 :             : 
     977                 :      640496 :   return (const GVariantType *) &type_string[1];
     978                 :             : }
     979                 :             : 
     980                 :             : /**
     981                 :             :  * g_variant_type_next:
     982                 :             :  * @type: a type from a previous call
     983                 :             :  *
     984                 :             :  * Determines the next item type of a tuple or dictionary entry
     985                 :             :  * type.
     986                 :             :  *
     987                 :             :  * @type must be the result of a previous call to
     988                 :             :  * [method@GLib.VariantType.first] or [method@GLib.VariantType.next].
     989                 :             :  *
     990                 :             :  * If called on the key type of a dictionary entry then this call
     991                 :             :  * returns the value type.  If called on the value type of a dictionary
     992                 :             :  * entry then this call returns `NULL`.
     993                 :             :  *
     994                 :             :  * For tuples, `NULL` is returned when @type is the last item in the tuple.
     995                 :             :  *
     996                 :             :  * Returns: (transfer none) (nullable): the next type after @type, or `NULL` if
     997                 :             :  *   there are no further types
     998                 :             :  * Since 2.24
     999                 :             :  **/
    1000                 :             : const GVariantType *
    1001                 :     4024349 : g_variant_type_next (const GVariantType *type)
    1002                 :             : {
    1003                 :             :   const gchar *type_string;
    1004                 :             : 
    1005                 :     4024349 :   g_return_val_if_fail (g_variant_type_check (type), NULL);
    1006                 :             : 
    1007                 :     4024349 :   type_string = g_variant_type_peek_string (type);
    1008                 :     4024349 :   type_string += g_variant_type_get_string_length (type);
    1009                 :             : 
    1010                 :     4024349 :   if (*type_string == ')' || *type_string == '}')
    1011                 :      642205 :     return NULL;
    1012                 :             : 
    1013                 :     3382144 :   return (const GVariantType *) type_string;
    1014                 :             : }
    1015                 :             : 
    1016                 :             : /**
    1017                 :             :  * g_variant_type_n_items:
    1018                 :             :  * @type: a tuple or dictionary entry type
    1019                 :             :  *
    1020                 :             :  * Determines the number of items contained in a tuple or
    1021                 :             :  * dictionary entry type.
    1022                 :             :  *
    1023                 :             :  * This function may only be used with tuple or dictionary entry types,
    1024                 :             :  * but must not be used with the generic tuple type
    1025                 :             :  * `G_VARIANT_TYPE_TUPLE`.
    1026                 :             :  *
    1027                 :             :  * In the case of a dictionary entry type, this function will always
    1028                 :             :  * return `2`.
    1029                 :             :  *
    1030                 :             :  * Returns: the number of items in @type
    1031                 :             :  * Since 2.24
    1032                 :             :  **/
    1033                 :             : gsize
    1034                 :      260637 : g_variant_type_n_items (const GVariantType *type)
    1035                 :             : {
    1036                 :      260637 :   gsize count = 0;
    1037                 :             : 
    1038                 :      260637 :   g_return_val_if_fail (g_variant_type_check (type), 0);
    1039                 :             : 
    1040                 :      260637 :   for (type = g_variant_type_first (type);
    1041                 :     2059740 :        type;
    1042                 :     1799103 :        type = g_variant_type_next (type))
    1043                 :     1799103 :     count++;
    1044                 :             : 
    1045                 :      260637 :   return count;
    1046                 :             : }
    1047                 :             : 
    1048                 :             : /**
    1049                 :             :  * g_variant_type_key:
    1050                 :             :  * @type: a dictionary entry type
    1051                 :             :  *
    1052                 :             :  * Determines the key type of a dictionary entry type.
    1053                 :             :  *
    1054                 :             :  * This function may only be used with a dictionary entry type.  Other
    1055                 :             :  * than the additional restriction, this call is equivalent to
    1056                 :             :  * [method@GLib.VariantType.first].
    1057                 :             :  *
    1058                 :             :  * Returns: (transfer none): the key type of the dictionary entry
    1059                 :             :  * Since 2.24
    1060                 :             :  **/
    1061                 :             : const GVariantType *
    1062                 :      386924 : g_variant_type_key (const GVariantType *type)
    1063                 :             : {
    1064                 :             :   const gchar *type_string;
    1065                 :             : 
    1066                 :      386924 :   g_return_val_if_fail (g_variant_type_check (type), NULL);
    1067                 :             : 
    1068                 :      386924 :   type_string = g_variant_type_peek_string (type);
    1069                 :      386924 :   g_assert (type_string[0] == '{');
    1070                 :             : 
    1071                 :      386924 :   return (const GVariantType *) &type_string[1];
    1072                 :             : }
    1073                 :             : 
    1074                 :             : /**
    1075                 :             :  * g_variant_type_value:
    1076                 :             :  * @type: a dictionary entry type
    1077                 :             :  *
    1078                 :             :  * Determines the value type of a dictionary entry type.
    1079                 :             :  *
    1080                 :             :  * This function may only be used with a dictionary entry type.
    1081                 :             :  *
    1082                 :             :  * Returns: (transfer none): the value type of the dictionary entry
    1083                 :             :  * Since 2.24
    1084                 :             :  **/
    1085                 :             : const GVariantType *
    1086                 :      150756 : g_variant_type_value (const GVariantType *type)
    1087                 :             : {
    1088                 :             : #ifndef G_DISABLE_ASSERT
    1089                 :             :   const gchar *type_string;
    1090                 :             : #endif
    1091                 :             : 
    1092                 :      150756 :   g_return_val_if_fail (g_variant_type_check (type), NULL);
    1093                 :             : 
    1094                 :             : #ifndef G_DISABLE_ASSERT
    1095                 :      150756 :   type_string = g_variant_type_peek_string (type);
    1096                 :      150756 :   g_assert (type_string[0] == '{');
    1097                 :             : #endif
    1098                 :             : 
    1099                 :      150756 :   return g_variant_type_next (g_variant_type_key (type));
    1100                 :             : }
    1101                 :             : 
    1102                 :             : /**
    1103                 :             :  * g_variant_type_new_tuple:
    1104                 :             :  * @items: (array length=length): an array of types, one for each item
    1105                 :             :  * @length: the length of @items, or `-1`
    1106                 :             :  *
    1107                 :             :  * Constructs a new tuple type, from @items.
    1108                 :             :  *
    1109                 :             :  * @length is the number of items in @items, or `-1` to indicate that
    1110                 :             :  * @items is `NULL`-terminated.
    1111                 :             :  *
    1112                 :             :  * It is appropriate to call [method@GLib.VariantType.free] on the return value.
    1113                 :             :  *
    1114                 :             :  * Returns: (transfer full): a new tuple type
    1115                 :             :  * Since 2.24
    1116                 :             :  **/
    1117                 :             : static GVariantType *
    1118                 :          82 : g_variant_type_new_tuple_slow (const GVariantType * const *items,
    1119                 :             :                                gint                        length)
    1120                 :             : {
    1121                 :             :   /* the "slow" version is needed in case the static buffer of 1024
    1122                 :             :    * bytes is exceeded when running the normal version.  this will
    1123                 :             :    * happen only with very unusually large types, so it can be slow.
    1124                 :             :    */
    1125                 :             :   GString *string;
    1126                 :             :   gint i;
    1127                 :             : 
    1128                 :          82 :   string = g_string_new ("(");
    1129                 :        1258 :   for (i = 0; i < length; i++)
    1130                 :             :     {
    1131                 :             :       const GVariantType *type;
    1132                 :             :       gsize size;
    1133                 :             : 
    1134                 :        1176 :       g_return_val_if_fail (g_variant_type_check (items[i]), NULL);
    1135                 :             : 
    1136                 :        1176 :       type = items[i];
    1137                 :        1176 :       size = g_variant_type_get_string_length (type);
    1138                 :        1176 :       g_string_append_len (string, (const gchar *) type, size);
    1139                 :             :     }
    1140                 :             :   g_string_append_c (string, ')');
    1141                 :             : 
    1142                 :          82 :   return (GVariantType *) g_string_free (string, FALSE);
    1143                 :             : }
    1144                 :             : 
    1145                 :             : GVariantType *
    1146                 :       54615 : g_variant_type_new_tuple (const GVariantType * const *items,
    1147                 :             :                           gint                        length)
    1148                 :             : {
    1149                 :             :   char buffer[1024];
    1150                 :             :   gsize offset;
    1151                 :             :   gsize i;
    1152                 :             :   gsize length_unsigned;
    1153                 :             : 
    1154                 :       54615 :   g_return_val_if_fail (length == 0 || items != NULL, NULL);
    1155                 :             : 
    1156                 :       54615 :   if (length < 0)
    1157                 :      211010 :     for (length_unsigned = 0; items[length_unsigned] != NULL; length_unsigned++);
    1158                 :             :   else
    1159                 :       34687 :     length_unsigned = (gsize) length;
    1160                 :             : 
    1161                 :       54615 :   offset = 0;
    1162                 :       54615 :   buffer[offset++] = '(';
    1163                 :             : 
    1164                 :      463732 :   for (i = 0; i < length_unsigned; i++)
    1165                 :             :     {
    1166                 :             :       const GVariantType *type;
    1167                 :             :       gsize size;
    1168                 :             : 
    1169                 :      409199 :       g_return_val_if_fail (g_variant_type_check (items[i]), NULL);
    1170                 :             : 
    1171                 :      409199 :       type = items[i];
    1172                 :      409199 :       size = g_variant_type_get_string_length (type);
    1173                 :             : 
    1174                 :      409199 :       if (offset + size >= sizeof buffer) /* leave room for ')' */
    1175                 :          82 :         return g_variant_type_new_tuple_slow (items, length_unsigned);
    1176                 :             : 
    1177                 :      409117 :       memcpy (&buffer[offset], type, size);
    1178                 :      409117 :       offset += size;
    1179                 :             :     }
    1180                 :             : 
    1181                 :       54533 :   g_assert (offset < sizeof buffer);
    1182                 :       54533 :   buffer[offset++] = ')';
    1183                 :             : 
    1184                 :       54533 :   return (GVariantType *) g_memdup2 (buffer, offset);
    1185                 :             : }
    1186                 :             : 
    1187                 :             : /**
    1188                 :             :  * g_variant_type_new_array: (constructor)
    1189                 :             :  * @element: an element type
    1190                 :             :  *
    1191                 :             :  * Constructs the type corresponding to an array of elements of the
    1192                 :             :  * type @type.
    1193                 :             :  *
    1194                 :             :  * It is appropriate to call [method@GLib.VariantType.first] on the return value.
    1195                 :             :  *
    1196                 :             :  * Returns: (transfer full): a new array type
    1197                 :             :  * Since 2.24
    1198                 :             :  **/
    1199                 :             : GVariantType *
    1200                 :       21880 : g_variant_type_new_array (const GVariantType *element)
    1201                 :             : {
    1202                 :             :   gsize size;
    1203                 :             :   gchar *new;
    1204                 :             : 
    1205                 :       21880 :   g_return_val_if_fail (g_variant_type_check (element), NULL);
    1206                 :             : 
    1207                 :       21880 :   size = g_variant_type_get_string_length (element);
    1208                 :       21880 :   new = g_malloc (size + 1);
    1209                 :             : 
    1210                 :       21880 :   new[0] = 'a';
    1211                 :       21880 :   memcpy (new + 1, element, size);
    1212                 :             : 
    1213                 :       21880 :   return (GVariantType *) new;
    1214                 :             : }
    1215                 :             : 
    1216                 :             : /**
    1217                 :             :  * g_variant_type_new_maybe: (constructor)
    1218                 :             :  * @element: an element type
    1219                 :             :  *
    1220                 :             :  * Constructs the type corresponding to a ‘maybe’ instance containing
    1221                 :             :  * type @type or `Nothing`.
    1222                 :             :  *
    1223                 :             :  * It is appropriate to call [method@GLib.VariantType.free] on the return value.
    1224                 :             :  *
    1225                 :             :  * Returns: (transfer full): a new ‘maybe’ type
    1226                 :             :  * Since 2.24
    1227                 :             :  **/
    1228                 :             : GVariantType *
    1229                 :       20784 : g_variant_type_new_maybe (const GVariantType *element)
    1230                 :             : {
    1231                 :             :   gsize size;
    1232                 :             :   gchar *new;
    1233                 :             : 
    1234                 :       20784 :   g_return_val_if_fail (g_variant_type_check (element), NULL);
    1235                 :             : 
    1236                 :       20784 :   size = g_variant_type_get_string_length (element);
    1237                 :       20784 :   new = g_malloc (size + 1);
    1238                 :             : 
    1239                 :       20784 :   new[0] = 'm';
    1240                 :       20784 :   memcpy (new + 1, element, size);
    1241                 :             : 
    1242                 :       20784 :   return (GVariantType *) new;
    1243                 :             : }
    1244                 :             : 
    1245                 :             : /**
    1246                 :             :  * g_variant_type_new_dict_entry: (constructor)
    1247                 :             :  * @key: a basic type to use for the key
    1248                 :             :  * @value: a type to use for the value
    1249                 :             :  *
    1250                 :             :  * Constructs the type corresponding to a dictionary entry with a key
    1251                 :             :  * of type @key and a value of type @value.
    1252                 :             :  *
    1253                 :             :  * It is appropriate to call [method@GLib.VariantType.free] on the return value.
    1254                 :             :  *
    1255                 :             :  * Returns: (transfer full): a new dictionary entry type
    1256                 :             :  * Since 2.24
    1257                 :             :  **/
    1258                 :             : GVariantType *
    1259                 :      250278 : g_variant_type_new_dict_entry (const GVariantType *key,
    1260                 :             :                                const GVariantType *value)
    1261                 :             : {
    1262                 :             :   gsize keysize, valsize;
    1263                 :             :   gchar *new;
    1264                 :             : 
    1265                 :      250278 :   g_return_val_if_fail (g_variant_type_check (key), NULL);
    1266                 :      250278 :   g_return_val_if_fail (g_variant_type_check (value), NULL);
    1267                 :             : 
    1268                 :      250278 :   keysize = g_variant_type_get_string_length (key);
    1269                 :      250278 :   valsize = g_variant_type_get_string_length (value);
    1270                 :             : 
    1271                 :      250278 :   new = g_malloc (1 + keysize + valsize + 1);
    1272                 :             : 
    1273                 :      250278 :   new[0] = '{';
    1274                 :      250278 :   memcpy (new + 1, key, keysize);
    1275                 :      250278 :   memcpy (new + 1 + keysize, value, valsize);
    1276                 :      250278 :   new[1 + keysize + valsize] = '}';
    1277                 :             : 
    1278                 :      250278 :   return (GVariantType *) new;
    1279                 :             : }
    1280                 :             : 
    1281                 :             : /* private */
    1282                 :             : const GVariantType *
    1283                 :      265680 : g_variant_type_checked_ (const gchar *type_string)
    1284                 :             : {
    1285                 :      265680 :   g_return_val_if_fail (g_variant_type_string_is_valid (type_string), NULL);
    1286                 :      265680 :   return (const GVariantType *) type_string;
    1287                 :             : }
        

Generated by: LCOV version 2.0-1