LCOV - code coverage report
Current view: top level - glib/glib - gvarianttype.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 272 274 99.3 %
Date: 2024-04-16 05:15:53 Functions: 34 34 100.0 %
Branches: 108 117 92.3 %

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

Generated by: LCOV version 1.14