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 : : * 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 : 88046507 : g_variant_type_check (const GVariantType *type)
195 : : {
196 : 88046507 : 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 : 88046507 : return TRUE;
203 : : #endif
204 : : }
205 : :
206 : : static gboolean
207 : 125516070 : 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 : 125516070 : gsize max_depth = 0, child_depth;
214 : :
215 : 125516070 : g_return_val_if_fail (string != NULL, FALSE);
216 : :
217 : 125516070 : if (string == limit || *string == '\0')
218 : 915 : return FALSE;
219 : :
220 : 125515155 : switch (*string++)
221 : : {
222 : 218552 : case '(':
223 : 1934969 : while (string == limit || *string != ')')
224 : : {
225 : 3434371 : if (depth_limit == 0 ||
226 : 1717185 : !variant_type_string_scan_internal (string, limit, &string,
227 : : &child_depth,
228 : : depth_limit - 1))
229 : 769 : return FALSE;
230 : :
231 : 1716417 : max_depth = MAX (max_depth, child_depth + 1);
232 : : }
233 : :
234 : 217783 : string++;
235 : 217783 : break;
236 : :
237 : 380565 : case '{':
238 : 380565 : if (depth_limit == 0 ||
239 : 380565 : string == limit || *string == '\0' || /* { */
240 : 760964 : !strchr ("bynqihuxtdsog?", *string++) || /* key */
241 : 380417 : !variant_type_string_scan_internal (string, limit, &string,
242 : 380234 : &child_depth, depth_limit - 1) || /* value */
243 : 380234 : string == limit || *string++ != '}') /* } */
244 : 443 : return FALSE;
245 : :
246 : 380122 : max_depth = MAX (max_depth, child_depth + 1);
247 : 380122 : break;
248 : :
249 : 218229 : case 'm': case 'a':
250 : 436457 : if (depth_limit == 0 ||
251 : 218228 : !variant_type_string_scan_internal (string, limit, &string,
252 : : &child_depth, depth_limit - 1))
253 : 495 : return FALSE;
254 : :
255 : 217734 : max_depth = MAX (max_depth, child_depth + 1);
256 : 217734 : break;
257 : :
258 : 124696820 : 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 : 124696820 : max_depth = MAX (max_depth, 1);
262 : 124696820 : break;
263 : :
264 : 989 : default:
265 : 989 : return FALSE;
266 : : }
267 : :
268 : 125512459 : if (endptr != NULL)
269 : 125512459 : *endptr = string;
270 : 125512459 : if (depth != NULL)
271 : 2533790 : *depth = max_depth;
272 : :
273 : 125512459 : 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
280 : : * @endptr: (out) (optional): location to store the end pointer
281 : : *
282 : : * Scan for a single complete and valid GVariant type string in @string.
283 : : *
284 : : * The memory pointed to by @limit (or bytes beyond it) is never
285 : : * accessed.
286 : : *
287 : : * If a valid type string is found, @endptr is updated to point to the
288 : : * first character past the end of the string that was found and %TRUE
289 : : * is returned.
290 : : *
291 : : * If there is no valid type string starting at @string, or if the type
292 : : * string does not end before @limit then %FALSE is returned.
293 : : *
294 : : * For the simple case of checking if a string is a valid type string,
295 : : * see [func@GLib.VariantType.string_is_valid].
296 : : *
297 : : * Returns: true if a valid type string was found
298 : : * Since: 2.24
299 : : **/
300 : : gboolean
301 : 122980835 : g_variant_type_string_scan (const gchar *string,
302 : : const gchar *limit,
303 : : const gchar **endptr)
304 : : {
305 : 122980835 : 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 [type@GLib.Variant] 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 : 219405 : g_variant_type_string_get_depth_ (const gchar *type_string)
324 : : {
325 : : const gchar *endptr;
326 : 219405 : gsize depth = 0;
327 : :
328 : 219405 : g_return_val_if_fail (type_string != NULL, 0);
329 : :
330 : 219405 : if (!variant_type_string_scan_internal (type_string, NULL, &endptr, &depth,
331 : 219405 : G_VARIANT_MAX_RECURSION_DEPTH) ||
332 : 219405 : *endptr != '\0')
333 : 0 : return 0;
334 : :
335 : 219405 : 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
343 : : * [GVariant type string](./struct.VariantType.html#gvariant-type-strings).
344 : : *
345 : : * This call is equivalent to calling [func@GLib.VariantType.string_scan] and
346 : : * confirming that the following character is a nul terminator.
347 : : *
348 : : * Returns: true if @type_string is exactly one valid type string
349 : : * Since 2.24
350 : : **/
351 : : gboolean
352 : 753583 : g_variant_type_string_is_valid (const gchar *type_string)
353 : : {
354 : : const gchar *endptr;
355 : :
356 : 753583 : g_return_val_if_fail (type_string != NULL, FALSE);
357 : :
358 : 753583 : if (!g_variant_type_string_scan (type_string, NULL, &endptr))
359 : 1952 : return FALSE;
360 : :
361 : 751631 : return *endptr == '\0';
362 : : }
363 : :
364 : : /**
365 : : * g_variant_type_free:
366 : : * @type: (nullable): type to free
367 : : *
368 : : * Frees a [type@GLib.VariantType] that was allocated with
369 : : * [method@GLib.VariantType.copy], [ctor@GLib.VariantType.new] or one of the
370 : : * container type constructor functions.
371 : : *
372 : : * In the case that @type is `NULL`, this function does nothing.
373 : : *
374 : : * Since 2.24
375 : : **/
376 : : void
377 : 3026941 : g_variant_type_free (GVariantType *type)
378 : : {
379 : 3026941 : g_return_if_fail (type == NULL || g_variant_type_check (type));
380 : :
381 : 3026941 : g_free (type);
382 : : }
383 : :
384 : : /**
385 : : * g_variant_type_copy:
386 : : * @type: (not nullable): type to copy
387 : : *
388 : : * Makes a copy of a [type@GLib.VariantType].
389 : : *
390 : : * It is appropriate to call [method@GLib.VariantType.free] on the return value.
391 : : * @type may not be `NULL`.
392 : : *
393 : : * Returns: (transfer full): a new [type@GLib.VariantType]
394 : : * Since 2.24
395 : : **/
396 : : GVariantType *
397 : 401196 : g_variant_type_copy (const GVariantType *type)
398 : : {
399 : : gsize length;
400 : : gchar *new;
401 : :
402 : 401196 : g_return_val_if_fail (g_variant_type_check (type), NULL);
403 : :
404 : 401196 : length = g_variant_type_get_string_length (type);
405 : 401196 : new = g_malloc (length + 1);
406 : :
407 : 401196 : memcpy (new, type, length);
408 : 401196 : new[length] = '\0';
409 : :
410 : 401196 : return (GVariantType *) new;
411 : : }
412 : :
413 : : /**
414 : : * g_variant_type_new:
415 : : * @type_string: a valid [GVariant type string](./struct.VariantType.html#gvariant-type-strings)
416 : : *
417 : : * Creates a new [type@GLib.VariantType] corresponding to the type string given
418 : : * by @type_string.
419 : : *
420 : : * It is appropriate to call [method@GLib.VariantType.free] on the return value.
421 : : *
422 : : * It is a programmer error to call this function with an invalid type
423 : : * string. Use [func@GLib.VariantType.string_is_valid] if you are unsure.
424 : : *
425 : : * Returns: (transfer full): a new [type@GLib.VariantType]
426 : : * Since: 2.24
427 : : */
428 : : GVariantType *
429 : 165882 : g_variant_type_new (const gchar *type_string)
430 : : {
431 : 165882 : g_return_val_if_fail (type_string != NULL, NULL);
432 : :
433 : 165882 : return g_variant_type_copy (G_VARIANT_TYPE (type_string));
434 : : }
435 : :
436 : : /**
437 : : * g_variant_type_get_string_length:
438 : : * @type: type to measure
439 : : *
440 : : * Returns the length of the type string corresponding to the given @type.
441 : : *
442 : : * This function must be used to determine the valid extent of
443 : : * the memory region returned by [method@GLib.VariantType.peek_string].
444 : : *
445 : : * Returns: the length of the corresponding type string
446 : : * Since 2.24
447 : : **/
448 : : gsize
449 : 9003748 : g_variant_type_get_string_length (const GVariantType *type)
450 : : {
451 : 9003748 : const gchar *type_string = (const gchar *) type;
452 : 9003748 : gint brackets = 0;
453 : 9003748 : gsize index = 0;
454 : :
455 : 9003748 : g_return_val_if_fail (g_variant_type_check (type), 0);
456 : :
457 : : do
458 : : {
459 : 29243237 : while (type_string[index] == 'a' || type_string[index] == 'm')
460 : 2155932 : index++;
461 : :
462 : 27087305 : if (type_string[index] == '(' || type_string[index] == '{')
463 : 3029134 : brackets++;
464 : :
465 : 24058171 : else if (type_string[index] == ')' || type_string[index] == '}')
466 : 3029134 : brackets--;
467 : :
468 : 27087305 : index++;
469 : : }
470 : 27087305 : while (brackets);
471 : :
472 : 9003748 : return index;
473 : : }
474 : :
475 : : /*
476 : : This function is not introspectable, it returns something that
477 : : is not an array and neither a string
478 : : */
479 : : /**
480 : : * g_variant_type_peek_string: (skip)
481 : : * @type: type to peek at
482 : : *
483 : : * Returns the type string corresponding to the given @type.
484 : : *
485 : : * The result is not nul-terminated; in order to determine its length you
486 : : * must call [method@GLib.VariantType.get_string_length].
487 : : *
488 : : * To get a nul-terminated string, see [method@GLib.VariantType.dup_string].
489 : : *
490 : : * Returns: the corresponding type string (not nul-terminated)
491 : : * Since 2.24
492 : : **/
493 : : const gchar *
494 : 35162280 : g_variant_type_peek_string (const GVariantType *type)
495 : : {
496 : 35162280 : g_return_val_if_fail (g_variant_type_check (type), NULL);
497 : :
498 : 35162280 : return (const gchar *) type;
499 : : }
500 : :
501 : : /**
502 : : * g_variant_type_dup_string:
503 : : * @type: type to copy
504 : : *
505 : : * Returns a newly-allocated copy of the type string corresponding to @type.
506 : : *
507 : : * The returned string is nul-terminated. It is appropriate to call
508 : : * [func@GLib.free] on the return value.
509 : : *
510 : : * Returns: (transfer full): the corresponding type string
511 : : * Since 2.24
512 : : **/
513 : : gchar *
514 : 34043 : g_variant_type_dup_string (const GVariantType *type)
515 : : {
516 : 34043 : g_return_val_if_fail (g_variant_type_check (type), NULL);
517 : :
518 : 34043 : return g_strndup (g_variant_type_peek_string (type),
519 : : g_variant_type_get_string_length (type));
520 : : }
521 : :
522 : : /**
523 : : * g_variant_type_is_definite:
524 : : * @type: type to check
525 : : *
526 : : * Determines if the given @type is definite (ie: not indefinite).
527 : : *
528 : : * A type is definite if its type string does not contain any indefinite
529 : : * type characters (`*`, `?`, or `r`).
530 : : *
531 : : * A [type@GLib.Variant] instance may not have an indefinite type, so calling
532 : : * this function on the result of [method@GLib.Variant.get_type] will always
533 : : * result in true being returned. Calling this function on an
534 : : * indefinite type like `G_VARIANT_TYPE_ARRAY`, however, will result in
535 : : * `FALSE` being returned.
536 : : *
537 : : * Returns: true if @type is definite
538 : : * Since 2.24
539 : : **/
540 : : gboolean
541 : 270265 : g_variant_type_is_definite (const GVariantType *type)
542 : : {
543 : : const gchar *type_string;
544 : : gsize type_length;
545 : : gsize i;
546 : :
547 : 270265 : g_return_val_if_fail (g_variant_type_check (type), FALSE);
548 : :
549 : 270265 : type_length = g_variant_type_get_string_length (type);
550 : 270265 : type_string = g_variant_type_peek_string (type);
551 : :
552 : 992619 : for (i = 0; i < type_length; i++)
553 : 811263 : if (type_string[i] == '*' ||
554 : 806210 : type_string[i] == '?' ||
555 : 734517 : type_string[i] == 'r')
556 : 88909 : return FALSE;
557 : :
558 : 181356 : return TRUE;
559 : : }
560 : :
561 : : /**
562 : : * g_variant_type_is_container:
563 : : * @type: type to check
564 : : *
565 : : * Determines if the given @type is a container type.
566 : : *
567 : : * Container types are any array, maybe, tuple, or dictionary
568 : : * entry types plus the variant type.
569 : : *
570 : : * This function returns true for any indefinite type for which every
571 : : * definite subtype is a container — `G_VARIANT_TYPE_ARRAY`, for
572 : : * example.
573 : : *
574 : : * Returns: true if @type is a container type
575 : : * Since 2.24
576 : : **/
577 : : gboolean
578 : 426923 : g_variant_type_is_container (const GVariantType *type)
579 : : {
580 : : gchar first_char;
581 : :
582 : 426923 : g_return_val_if_fail (g_variant_type_check (type), FALSE);
583 : :
584 : 426923 : first_char = g_variant_type_peek_string (type)[0];
585 : 426923 : switch (first_char)
586 : : {
587 : 211875 : case 'a':
588 : : case 'm':
589 : : case 'r':
590 : : case '(':
591 : : case '{':
592 : : case 'v':
593 : 211875 : return TRUE;
594 : :
595 : 215048 : default:
596 : 215048 : return FALSE;
597 : : }
598 : : }
599 : :
600 : : /**
601 : : * g_variant_type_is_basic:
602 : : * @type: type to check
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 : : * Since 2.24
616 : : **/
617 : : gboolean
618 : 119959 : g_variant_type_is_basic (const GVariantType *type)
619 : : {
620 : : gchar first_char;
621 : :
622 : 119959 : g_return_val_if_fail (g_variant_type_check (type), FALSE);
623 : :
624 : 119959 : first_char = g_variant_type_peek_string (type)[0];
625 : 119959 : switch (first_char)
626 : : {
627 : 100000 : case 'b':
628 : : case 'y':
629 : : case 'n':
630 : : case 'q':
631 : : case 'i':
632 : : case 'h':
633 : : case 'u':
634 : : case 't':
635 : : case 'x':
636 : : case 'd':
637 : : case 's':
638 : : case 'o':
639 : : case 'g':
640 : : case '?':
641 : 100000 : return TRUE;
642 : :
643 : 19959 : default:
644 : 19959 : return FALSE;
645 : : }
646 : : }
647 : :
648 : : /**
649 : : * g_variant_type_is_maybe:
650 : : * @type: type to check
651 : : *
652 : : * Determines if the given @type is a ‘maybe’ type.
653 : : *
654 : : * This is true if the 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 : : * Since 2.24
662 : : **/
663 : : gboolean
664 : 1660810 : g_variant_type_is_maybe (const GVariantType *type)
665 : : {
666 : 1660810 : g_return_val_if_fail (g_variant_type_check (type), FALSE);
667 : :
668 : 1660810 : return g_variant_type_peek_string (type)[0] == 'm';
669 : : }
670 : :
671 : : /**
672 : : * g_variant_type_is_array:
673 : : * @type: type to check
674 : : *
675 : : * Determines if the given @type is an array type.
676 : : *
677 : : * This is true if the 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 : : * Since 2.24
685 : : **/
686 : : gboolean
687 : 1219768 : g_variant_type_is_array (const GVariantType *type)
688 : : {
689 : 1219768 : g_return_val_if_fail (g_variant_type_check (type), FALSE);
690 : :
691 : 1219768 : return g_variant_type_peek_string (type)[0] == 'a';
692 : : }
693 : :
694 : : /**
695 : : * g_variant_type_is_tuple:
696 : : * @type: type to check
697 : : *
698 : : * Determines if the given @type is a tuple type.
699 : : *
700 : : * This is true if the 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 : : * Since 2.24
709 : : **/
710 : : gboolean
711 : 1371620 : g_variant_type_is_tuple (const GVariantType *type)
712 : : {
713 : : gchar type_char;
714 : :
715 : 1371620 : g_return_val_if_fail (g_variant_type_check (type), FALSE);
716 : :
717 : 1371620 : type_char = g_variant_type_peek_string (type)[0];
718 : 1371620 : return type_char == 'r' || type_char == '(';
719 : : }
720 : :
721 : : /**
722 : : * g_variant_type_is_dict_entry:
723 : : * @type: type to check
724 : : *
725 : : * Determines if the given @type is a dictionary entry type.
726 : : *
727 : : * This is 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 : : * Since 2.24
735 : : **/
736 : : gboolean
737 : 1392481 : g_variant_type_is_dict_entry (const GVariantType *type)
738 : : {
739 : 1392481 : g_return_val_if_fail (g_variant_type_check (type), FALSE);
740 : :
741 : 1392481 : return g_variant_type_peek_string (type)[0] == '{';
742 : : }
743 : :
744 : : /**
745 : : * g_variant_type_is_variant:
746 : : * @type: type to check
747 : : *
748 : : * Determines if the given @type is the variant type.
749 : : *
750 : : * Returns: true if @type is the variant type
751 : : * Since 2.24
752 : : **/
753 : : gboolean
754 : 208568 : g_variant_type_is_variant (const GVariantType *type)
755 : : {
756 : 208568 : g_return_val_if_fail (g_variant_type_check (type), FALSE);
757 : :
758 : 208568 : return g_variant_type_peek_string (type)[0] == 'v';
759 : : }
760 : :
761 : : /**
762 : : * g_variant_type_hash:
763 : : * @type: (type GVariantType): type to hash
764 : : *
765 : : * Hashes @type.
766 : : *
767 : : * The argument type of @type is only `gconstpointer` to allow use with
768 : : * [type@GLib.HashTable] without function pointer casting. A valid
769 : : * [type@GLib.VariantType] must be provided.
770 : : *
771 : : * Returns: the hash value
772 : : * Since 2.24
773 : : **/
774 : : guint
775 : 4006 : g_variant_type_hash (gconstpointer type)
776 : : {
777 : 4006 : g_return_val_if_fail (g_variant_type_check (type), 0);
778 : :
779 : 4006 : return _g_variant_type_hash (type);
780 : : }
781 : :
782 : : /**
783 : : * g_variant_type_equal:
784 : : * @type1: (type GVariantType): type to compare
785 : : * @type2: (type GVariantType): another type to compare
786 : : *
787 : : * Compares @type1 and @type2 for equality.
788 : : *
789 : : * Only returns true if the types are exactly equal. Even if one type
790 : : * is an indefinite type and the other is a subtype of it, false will
791 : : * be returned if they are not exactly equal. If you want to check for
792 : : * subtypes, use [method@GLib.VariantType.is_subtype_of].
793 : : *
794 : : * The argument types of @type1 and @type2 are only `gconstpointer` to
795 : : * allow use with [type@GLib.HashTable] without function pointer casting. For
796 : : * both arguments, a valid [type@GLib.VariantType] must be provided.
797 : : *
798 : : * Returns: true if @type1 and @type2 are exactly equal
799 : : * Since 2.24
800 : : **/
801 : : gboolean
802 : 7448882 : g_variant_type_equal (gconstpointer type1,
803 : : gconstpointer type2)
804 : : {
805 : 7448882 : g_return_val_if_fail (g_variant_type_check (type1), FALSE);
806 : 7448882 : g_return_val_if_fail (g_variant_type_check (type2), FALSE);
807 : :
808 : 7448882 : return _g_variant_type_equal (type1, type2);
809 : : }
810 : :
811 : : /**
812 : : * g_variant_type_is_subtype_of:
813 : : * @type: type to check
814 : : * @supertype: type of potential supertype
815 : : *
816 : : * Checks if @type is a subtype of @supertype.
817 : : *
818 : : * This function returns true if @type is a subtype of @supertype. All
819 : : * types are considered to be subtypes of themselves. Aside from that,
820 : : * only indefinite types can have subtypes.
821 : : *
822 : : * Returns: true if @type is a subtype of @supertype
823 : : * Since 2.24
824 : : **/
825 : : gboolean
826 : 7018064 : g_variant_type_is_subtype_of (const GVariantType *type,
827 : : const GVariantType *supertype)
828 : : {
829 : : const gchar *supertype_string;
830 : : const gchar *supertype_end;
831 : : const gchar *type_string;
832 : :
833 : 7018064 : g_return_val_if_fail (g_variant_type_check (type), FALSE);
834 : 7018064 : g_return_val_if_fail (g_variant_type_check (supertype), FALSE);
835 : :
836 : 7018064 : supertype_string = g_variant_type_peek_string (supertype);
837 : 7018064 : type_string = g_variant_type_peek_string (type);
838 : :
839 : : /* fast path for the basic determinate types */
840 : 7018064 : if (type_string[0] == supertype_string[0])
841 : : {
842 : 4992810 : switch (type_string[0])
843 : : {
844 : 3884075 : case 'b': case 'y':
845 : : case 'n': case 'q':
846 : : case 'i': case 'h': case 'u':
847 : : case 't': case 'x':
848 : : case 's': case 'o': case 'g':
849 : : case 'd':
850 : 3884075 : return TRUE;
851 : :
852 : 1108735 : default:
853 : 1108735 : break;
854 : : }
855 : : }
856 : :
857 : 3133989 : supertype_end = supertype_string +
858 : 3133989 : g_variant_type_get_string_length (supertype);
859 : :
860 : : /* we know that type and supertype are both well-formed, so it's
861 : : * safe to treat this merely as a text processing problem.
862 : : */
863 : 8801809 : while (supertype_string < supertype_end)
864 : : {
865 : 7481568 : char supertype_char = *supertype_string++;
866 : :
867 : 7481568 : if (supertype_char == *type_string)
868 : 5439620 : type_string++;
869 : :
870 : 2041948 : else if (*type_string == ')')
871 : 1 : return FALSE;
872 : :
873 : : else
874 : : {
875 : 2041947 : const GVariantType *target_type = (GVariantType *) type_string;
876 : :
877 : 2041947 : switch (supertype_char)
878 : : {
879 : 74043 : case 'r':
880 : 74043 : if (!g_variant_type_is_tuple (target_type))
881 : 173 : return FALSE;
882 : 73870 : break;
883 : :
884 : 83073 : case '*':
885 : 83073 : break;
886 : :
887 : 71297 : case '?':
888 : 71297 : if (!g_variant_type_is_basic (target_type))
889 : 40 : return FALSE;
890 : 71257 : break;
891 : :
892 : 1813534 : default:
893 : 1813534 : return FALSE;
894 : : }
895 : :
896 : 228200 : type_string += g_variant_type_get_string_length (target_type);
897 : : }
898 : : }
899 : :
900 : 1320241 : return TRUE;
901 : : }
902 : :
903 : : /**
904 : : * g_variant_type_element:
905 : : * @type: an array or ‘maybe’ type
906 : : *
907 : : * Determines the element type of an array or ‘maybe’ type.
908 : : *
909 : : * This function may only be used with array or ‘maybe’ types.
910 : : *
911 : : * Returns: (transfer none): the element type of @type
912 : : * Since 2.24
913 : : **/
914 : : const GVariantType *
915 : 403102 : g_variant_type_element (const GVariantType *type)
916 : : {
917 : : const gchar *type_string;
918 : :
919 : 403102 : g_return_val_if_fail (g_variant_type_check (type), NULL);
920 : :
921 : 403102 : type_string = g_variant_type_peek_string (type);
922 : :
923 : 403102 : g_assert (type_string[0] == 'a' || type_string[0] == 'm');
924 : :
925 : 403102 : return (const GVariantType *) &type_string[1];
926 : : }
927 : :
928 : : /**
929 : : * g_variant_type_first:
930 : : * @type: a tuple or dictionary entry type
931 : : *
932 : : * Determines the first item type of a tuple or dictionary entry
933 : : * type.
934 : : *
935 : : * This function may only be used with tuple or dictionary entry types,
936 : : * but must not be used with the generic tuple type
937 : : * `G_VARIANT_TYPE_TUPLE`.
938 : : *
939 : : * In the case of a dictionary entry type, this returns the type of
940 : : * the key.
941 : : *
942 : : * `NULL` is returned in case of @type being `G_VARIANT_TYPE_UNIT`.
943 : : *
944 : : * This call, together with [method@GLib.VariantType.next] provides an iterator
945 : : * interface over tuple and dictionary entry types.
946 : : *
947 : : * Returns: (transfer none) (nullable): the first item type of @type, or `NULL`
948 : : * if the type has no item types
949 : : * Since 2.24
950 : : **/
951 : : const GVariantType *
952 : 660123 : g_variant_type_first (const GVariantType *type)
953 : : {
954 : : const gchar *type_string;
955 : :
956 : 660123 : g_return_val_if_fail (g_variant_type_check (type), NULL);
957 : :
958 : 660123 : type_string = g_variant_type_peek_string (type);
959 : 660123 : g_assert (type_string[0] == '(' || type_string[0] == '{');
960 : :
961 : 660123 : if (type_string[1] == ')')
962 : 5882 : return NULL;
963 : :
964 : 654241 : return (const GVariantType *) &type_string[1];
965 : : }
966 : :
967 : : /**
968 : : * g_variant_type_next:
969 : : * @type: a type from a previous call
970 : : *
971 : : * Determines the next item type of a tuple or dictionary entry
972 : : * type.
973 : : *
974 : : * @type must be the result of a previous call to
975 : : * [method@GLib.VariantType.first] or [method@GLib.VariantType.next].
976 : : *
977 : : * If called on the key type of a dictionary entry then this call
978 : : * returns the value type. If called on the value type of a dictionary
979 : : * entry then this call returns `NULL`.
980 : : *
981 : : * For tuples, `NULL` is returned when @type is the last item in the tuple.
982 : : *
983 : : * Returns: (transfer none) (nullable): the next type after @type, or `NULL` if
984 : : * there are no further types
985 : : * Since 2.24
986 : : **/
987 : : const GVariantType *
988 : 4002507 : g_variant_type_next (const GVariantType *type)
989 : : {
990 : : const gchar *type_string;
991 : :
992 : 4002507 : g_return_val_if_fail (g_variant_type_check (type), NULL);
993 : :
994 : 4002507 : type_string = g_variant_type_peek_string (type);
995 : 4002507 : type_string += g_variant_type_get_string_length (type);
996 : :
997 : 4002507 : if (*type_string == ')' || *type_string == '}')
998 : 655935 : return NULL;
999 : :
1000 : 3346572 : return (const GVariantType *) type_string;
1001 : : }
1002 : :
1003 : : /**
1004 : : * g_variant_type_n_items:
1005 : : * @type: a tuple or dictionary entry type
1006 : : *
1007 : : * Determines the number of items contained in a tuple or
1008 : : * dictionary entry type.
1009 : : *
1010 : : * This function may only be used with tuple or dictionary entry types,
1011 : : * but must not be used with the generic tuple type
1012 : : * `G_VARIANT_TYPE_TUPLE`.
1013 : : *
1014 : : * In the case of a dictionary entry type, this function will always
1015 : : * return `2`.
1016 : : *
1017 : : * Returns: the number of items in @type
1018 : : * Since 2.24
1019 : : **/
1020 : : gsize
1021 : 267279 : g_variant_type_n_items (const GVariantType *type)
1022 : : {
1023 : 267279 : gsize count = 0;
1024 : :
1025 : 267279 : g_return_val_if_fail (g_variant_type_check (type), 0);
1026 : :
1027 : 267279 : for (type = g_variant_type_first (type);
1028 : 2049342 : type;
1029 : 1782063 : type = g_variant_type_next (type))
1030 : 1782063 : count++;
1031 : :
1032 : 267279 : return count;
1033 : : }
1034 : :
1035 : : /**
1036 : : * g_variant_type_key:
1037 : : * @type: a dictionary entry type
1038 : : *
1039 : : * Determines the key type of a dictionary entry type.
1040 : : *
1041 : : * This function may only be used with a dictionary entry type. Other
1042 : : * than the additional restriction, this call is equivalent to
1043 : : * [method@GLib.VariantType.first].
1044 : : *
1045 : : * Returns: (transfer none): the key type of the dictionary entry
1046 : : * Since 2.24
1047 : : **/
1048 : : const GVariantType *
1049 : 363025 : g_variant_type_key (const GVariantType *type)
1050 : : {
1051 : : const gchar *type_string;
1052 : :
1053 : 363025 : g_return_val_if_fail (g_variant_type_check (type), NULL);
1054 : :
1055 : 363025 : type_string = g_variant_type_peek_string (type);
1056 : 363025 : g_assert (type_string[0] == '{');
1057 : :
1058 : 363025 : return (const GVariantType *) &type_string[1];
1059 : : }
1060 : :
1061 : : /**
1062 : : * g_variant_type_value:
1063 : : * @type: a dictionary entry type
1064 : : *
1065 : : * Determines the value type of a dictionary entry type.
1066 : : *
1067 : : * This function may only be used with a dictionary entry type.
1068 : : *
1069 : : * Returns: (transfer none): the value type of the dictionary entry
1070 : : * Since 2.24
1071 : : **/
1072 : : const GVariantType *
1073 : 142237 : g_variant_type_value (const GVariantType *type)
1074 : : {
1075 : : #ifndef G_DISABLE_ASSERT
1076 : : const gchar *type_string;
1077 : : #endif
1078 : :
1079 : 142237 : g_return_val_if_fail (g_variant_type_check (type), NULL);
1080 : :
1081 : : #ifndef G_DISABLE_ASSERT
1082 : 142237 : type_string = g_variant_type_peek_string (type);
1083 : 142237 : g_assert (type_string[0] == '{');
1084 : : #endif
1085 : :
1086 : 142237 : return g_variant_type_next (g_variant_type_key (type));
1087 : : }
1088 : :
1089 : : /**
1090 : : * g_variant_type_new_tuple:
1091 : : * @items: (array length=length): an array of types, one for each item
1092 : : * @length: the length of @items, or `-1`
1093 : : *
1094 : : * Constructs a new tuple type, from @items.
1095 : : *
1096 : : * @length is the number of items in @items, or `-1` to indicate that
1097 : : * @items is `NULL`-terminated.
1098 : : *
1099 : : * It is appropriate to call [method@GLib.VariantType.free] on the return value.
1100 : : *
1101 : : * Returns: (transfer full): a new tuple type
1102 : : * Since 2.24
1103 : : **/
1104 : : static GVariantType *
1105 : 82 : g_variant_type_new_tuple_slow (const GVariantType * const *items,
1106 : : gint length)
1107 : : {
1108 : : /* the "slow" version is needed in case the static buffer of 1024
1109 : : * bytes is exceeded when running the normal version. this will
1110 : : * happen only with very unusually large types, so it can be slow.
1111 : : */
1112 : : GString *string;
1113 : : gint i;
1114 : :
1115 : 82 : string = g_string_new ("(");
1116 : 1266 : for (i = 0; i < length; i++)
1117 : : {
1118 : : const GVariantType *type;
1119 : : gsize size;
1120 : :
1121 : 1184 : g_return_val_if_fail (g_variant_type_check (items[i]), NULL);
1122 : :
1123 : 1184 : type = items[i];
1124 : 1184 : size = g_variant_type_get_string_length (type);
1125 : 1184 : g_string_append_len (string, (const gchar *) type, size);
1126 : : }
1127 : : g_string_append_c (string, ')');
1128 : :
1129 : 82 : return (GVariantType *) g_string_free (string, FALSE);
1130 : : }
1131 : :
1132 : : GVariantType *
1133 : 57289 : g_variant_type_new_tuple (const GVariantType * const *items,
1134 : : gint length)
1135 : : {
1136 : : char buffer[1024];
1137 : : gsize offset;
1138 : : gsize i;
1139 : : gsize length_unsigned;
1140 : :
1141 : 57289 : g_return_val_if_fail (length == 0 || items != NULL, NULL);
1142 : :
1143 : 57289 : if (length < 0)
1144 : 211213 : for (length_unsigned = 0; items[length_unsigned] != NULL; length_unsigned++);
1145 : : else
1146 : 37187 : length_unsigned = (gsize) length;
1147 : :
1148 : 57289 : offset = 0;
1149 : 57289 : buffer[offset++] = '(';
1150 : :
1151 : 480740 : for (i = 0; i < length_unsigned; i++)
1152 : : {
1153 : : const GVariantType *type;
1154 : : gsize size;
1155 : :
1156 : 423533 : g_return_val_if_fail (g_variant_type_check (items[i]), NULL);
1157 : :
1158 : 423533 : type = items[i];
1159 : 423533 : size = g_variant_type_get_string_length (type);
1160 : :
1161 : 423533 : if (offset + size >= sizeof buffer) /* leave room for ')' */
1162 : 82 : return g_variant_type_new_tuple_slow (items, length_unsigned);
1163 : :
1164 : 423451 : memcpy (&buffer[offset], type, size);
1165 : 423451 : offset += size;
1166 : : }
1167 : :
1168 : 57207 : g_assert (offset < sizeof buffer);
1169 : 57207 : buffer[offset++] = ')';
1170 : :
1171 : 57207 : return (GVariantType *) g_memdup2 (buffer, offset);
1172 : : }
1173 : :
1174 : : /**
1175 : : * g_variant_type_new_array: (constructor)
1176 : : * @element: an element type
1177 : : *
1178 : : * Constructs the type corresponding to an array of elements of the
1179 : : * type @type.
1180 : : *
1181 : : * It is appropriate to call [method@GLib.VariantType.first] on the return value.
1182 : : *
1183 : : * Returns: (transfer full): a new array type
1184 : : * Since 2.24
1185 : : **/
1186 : : GVariantType *
1187 : 24693 : g_variant_type_new_array (const GVariantType *element)
1188 : : {
1189 : : gsize size;
1190 : : gchar *new;
1191 : :
1192 : 24693 : g_return_val_if_fail (g_variant_type_check (element), NULL);
1193 : :
1194 : 24693 : size = g_variant_type_get_string_length (element);
1195 : 24693 : new = g_malloc (size + 1);
1196 : :
1197 : 24693 : new[0] = 'a';
1198 : 24693 : memcpy (new + 1, element, size);
1199 : :
1200 : 24693 : return (GVariantType *) new;
1201 : : }
1202 : :
1203 : : /**
1204 : : * g_variant_type_new_maybe: (constructor)
1205 : : * @element: an element type
1206 : : *
1207 : : * Constructs the type corresponding to a ‘maybe’ instance containing
1208 : : * type @type or `Nothing`.
1209 : : *
1210 : : * It is appropriate to call [method@GLib.VariantType.free] on the return value.
1211 : : *
1212 : : * Returns: (transfer full): a new ‘maybe’ type
1213 : : * Since 2.24
1214 : : **/
1215 : : GVariantType *
1216 : 20840 : g_variant_type_new_maybe (const GVariantType *element)
1217 : : {
1218 : : gsize size;
1219 : : gchar *new;
1220 : :
1221 : 20840 : g_return_val_if_fail (g_variant_type_check (element), NULL);
1222 : :
1223 : 20840 : size = g_variant_type_get_string_length (element);
1224 : 20840 : new = g_malloc (size + 1);
1225 : :
1226 : 20840 : new[0] = 'm';
1227 : 20840 : memcpy (new + 1, element, size);
1228 : :
1229 : 20840 : return (GVariantType *) new;
1230 : : }
1231 : :
1232 : : /**
1233 : : * g_variant_type_new_dict_entry: (constructor)
1234 : : * @key: a basic type to use for the key
1235 : : * @value: a type to use for the value
1236 : : *
1237 : : * Constructs the type corresponding to a dictionary entry with a key
1238 : : * of type @key and a value of type @value.
1239 : : *
1240 : : * It is appropriate to call [method@GLib.VariantType.free] on the return value.
1241 : : *
1242 : : * Returns: (transfer full): a new dictionary entry type
1243 : : * Since 2.24
1244 : : **/
1245 : : GVariantType *
1246 : 231649 : g_variant_type_new_dict_entry (const GVariantType *key,
1247 : : const GVariantType *value)
1248 : : {
1249 : : gsize keysize, valsize;
1250 : : gchar *new;
1251 : :
1252 : 231649 : g_return_val_if_fail (g_variant_type_check (key), NULL);
1253 : 231649 : g_return_val_if_fail (g_variant_type_check (value), NULL);
1254 : :
1255 : 231649 : keysize = g_variant_type_get_string_length (key);
1256 : 231649 : valsize = g_variant_type_get_string_length (value);
1257 : :
1258 : 231649 : new = g_malloc (1 + keysize + valsize + 1);
1259 : :
1260 : 231649 : new[0] = '{';
1261 : 231649 : memcpy (new + 1, key, keysize);
1262 : 231649 : memcpy (new + 1 + keysize, value, valsize);
1263 : 231649 : new[1 + keysize + valsize] = '}';
1264 : :
1265 : 231649 : return (GVariantType *) new;
1266 : : }
1267 : :
1268 : : /* private */
1269 : : const GVariantType *
1270 : 254152 : g_variant_type_checked_ (const gchar *type_string)
1271 : : {
1272 : 254152 : g_return_val_if_fail (g_variant_type_string_is_valid (type_string), NULL);
1273 : 254152 : return (const GVariantType *) type_string;
1274 : : }
|