Coverage Report

Created: 2025-07-18 06:08

/src/tinysparql/subprojects/glib-2.80.3/glib/gvariant.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright © 2007, 2008 Ryan Lortie
3
 * Copyright © 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
/* Prologue {{{1 */
24
25
#include "config.h"
26
27
#include <glib/gvariant-serialiser.h>
28
#include "gvariant-internal.h"
29
#include <glib/gvariant-core.h>
30
#include <glib/gtestutils.h>
31
#include <glib/gstrfuncs.h>
32
#include <glib/gslice.h>
33
#include <glib/ghash.h>
34
#include <glib/gmem.h>
35
36
#include <string.h>
37
38
/**
39
 * GVariant:
40
 *
41
 * `GVariant` is a variant datatype; it can contain one or more values
42
 * along with information about the type of the values.
43
 *
44
 * A `GVariant` may contain simple types, like an integer, or a boolean value;
45
 * or complex types, like an array of two strings, or a dictionary of key
46
 * value pairs. A `GVariant` is also immutable: once it’s been created neither
47
 * its type nor its content can be modified further.
48
 *
49
 * `GVariant` is useful whenever data needs to be serialized, for example when
50
 * sending method parameters in D-Bus, or when saving settings using
51
 * [`GSettings`](../gio/class.Settings.html).
52
 *
53
 * When creating a new `GVariant`, you pass the data you want to store in it
54
 * along with a string representing the type of data you wish to pass to it.
55
 *
56
 * For instance, if you want to create a `GVariant` holding an integer value you
57
 * can use:
58
 *
59
 * ```c
60
 * GVariant *v = g_variant_new ("u", 40);
61
 * ```
62
 *
63
 * The string `u` in the first argument tells `GVariant` that the data passed to
64
 * the constructor (`40`) is going to be an unsigned integer.
65
 *
66
 * More advanced examples of `GVariant` in use can be found in documentation for
67
 * [`GVariant` format strings](gvariant-format-strings.html#pointers).
68
 *
69
 * The range of possible values is determined by the type.
70
 *
71
 * The type system used by `GVariant` is [type@GLib.VariantType].
72
 *
73
 * `GVariant` instances always have a type and a value (which are given
74
 * at construction time).  The type and value of a `GVariant` instance
75
 * can never change other than by the `GVariant` itself being
76
 * destroyed.  A `GVariant` cannot contain a pointer.
77
 *
78
 * `GVariant` is reference counted using [method@GLib.Variant.ref] and
79
 * [method@GLib.Variant.unref].  `GVariant` also has floating reference counts —
80
 * see [method@GLib.Variant.ref_sink].
81
 *
82
 * `GVariant` is completely threadsafe.  A `GVariant` instance can be
83
 * concurrently accessed in any way from any number of threads without
84
 * problems.
85
 *
86
 * `GVariant` is heavily optimised for dealing with data in serialized
87
 * form.  It works particularly well with data located in memory-mapped
88
 * files.  It can perform nearly all deserialization operations in a
89
 * small constant time, usually touching only a single memory page.
90
 * Serialized `GVariant` data can also be sent over the network.
91
 *
92
 * `GVariant` is largely compatible with D-Bus.  Almost all types of
93
 * `GVariant` instances can be sent over D-Bus.  See [type@GLib.VariantType] for
94
 * exceptions.  (However, `GVariant`’s serialization format is not the same
95
 * as the serialization format of a D-Bus message body: use
96
 * [GDBusMessage](../gio/class.DBusMessage.html), in the GIO library, for those.)
97
 *
98
 * For space-efficiency, the `GVariant` serialization format does not
99
 * automatically include the variant’s length, type or endianness,
100
 * which must either be implied from context (such as knowledge that a
101
 * particular file format always contains a little-endian
102
 * `G_VARIANT_TYPE_VARIANT` which occupies the whole length of the file)
103
 * or supplied out-of-band (for instance, a length, type and/or endianness
104
 * indicator could be placed at the beginning of a file, network message
105
 * or network stream).
106
 *
107
 * A `GVariant`’s size is limited mainly by any lower level operating
108
 * system constraints, such as the number of bits in `gsize`.  For
109
 * example, it is reasonable to have a 2GB file mapped into memory
110
 * with [struct@GLib.MappedFile], and call [ctor@GLib.Variant.new_from_data] on
111
 * it.
112
 *
113
 * For convenience to C programmers, `GVariant` features powerful
114
 * varargs-based value construction and destruction.  This feature is
115
 * designed to be embedded in other libraries.
116
 *
117
 * There is a Python-inspired text language for describing `GVariant`
118
 * values.  `GVariant` includes a printer for this language and a parser
119
 * with type inferencing.
120
 *
121
 * ## Memory Use
122
 *
123
 * `GVariant` tries to be quite efficient with respect to memory use.
124
 * This section gives a rough idea of how much memory is used by the
125
 * current implementation.  The information here is subject to change
126
 * in the future.
127
 *
128
 * The memory allocated by `GVariant` can be grouped into 4 broad
129
 * purposes: memory for serialized data, memory for the type
130
 * information cache, buffer management memory and memory for the
131
 * `GVariant` structure itself.
132
 *
133
 * ## Serialized Data Memory
134
 *
135
 * This is the memory that is used for storing `GVariant` data in
136
 * serialized form.  This is what would be sent over the network or
137
 * what would end up on disk, not counting any indicator of the
138
 * endianness, or of the length or type of the top-level variant.
139
 *
140
 * The amount of memory required to store a boolean is 1 byte. 16,
141
 * 32 and 64 bit integers and double precision floating point numbers
142
 * use their ‘natural’ size.  Strings (including object path and
143
 * signature strings) are stored with a nul terminator, and as such
144
 * use the length of the string plus 1 byte.
145
 *
146
 * ‘Maybe’ types use no space at all to represent the null value and
147
 * use the same amount of space (sometimes plus one byte) as the
148
 * equivalent non-maybe-typed value to represent the non-null case.
149
 *
150
 * Arrays use the amount of space required to store each of their
151
 * members, concatenated.  Additionally, if the items stored in an
152
 * array are not of a fixed-size (ie: strings, other arrays, etc)
153
 * then an additional framing offset is stored for each item.  The
154
 * size of this offset is either 1, 2 or 4 bytes depending on the
155
 * overall size of the container.  Additionally, extra padding bytes
156
 * are added as required for alignment of child values.
157
 *
158
 * Tuples (including dictionary entries) use the amount of space
159
 * required to store each of their members, concatenated, plus one
160
 * framing offset (as per arrays) for each non-fixed-sized item in
161
 * the tuple, except for the last one.  Additionally, extra padding
162
 * bytes are added as required for alignment of child values.
163
 *
164
 * Variants use the same amount of space as the item inside of the
165
 * variant, plus 1 byte, plus the length of the type string for the
166
 * item inside the variant.
167
 *
168
 * As an example, consider a dictionary mapping strings to variants.
169
 * In the case that the dictionary is empty, 0 bytes are required for
170
 * the serialization.
171
 *
172
 * If we add an item ‘width’ that maps to the int32 value of 500 then
173
 * we will use 4 bytes to store the int32 (so 6 for the variant
174
 * containing it) and 6 bytes for the string.  The variant must be
175
 * aligned to 8 after the 6 bytes of the string, so that’s 2 extra
176
 * bytes.  6 (string) + 2 (padding) + 6 (variant) is 14 bytes used
177
 * for the dictionary entry.  An additional 1 byte is added to the
178
 * array as a framing offset making a total of 15 bytes.
179
 *
180
 * If we add another entry, ‘title’ that maps to a nullable string
181
 * that happens to have a value of null, then we use 0 bytes for the
182
 * null value (and 3 bytes for the variant to contain it along with
183
 * its type string) plus 6 bytes for the string.  Again, we need 2
184
 * padding bytes.  That makes a total of 6 + 2 + 3 = 11 bytes.
185
 *
186
 * We now require extra padding between the two items in the array.
187
 * After the 14 bytes of the first item, that’s 2 bytes required.
188
 * We now require 2 framing offsets for an extra two
189
 * bytes. 14 + 2 + 11 + 2 = 29 bytes to encode the entire two-item
190
 * dictionary.
191
 *
192
 * ## Type Information Cache
193
 *
194
 * For each `GVariant` type that currently exists in the program a type
195
 * information structure is kept in the type information cache.  The
196
 * type information structure is required for rapid deserialization.
197
 *
198
 * Continuing with the above example, if a `GVariant` exists with the
199
 * type `a{sv}` then a type information struct will exist for
200
 * `a{sv}`, `{sv}`, `s`, and `v`.  Multiple uses of the same type
201
 * will share the same type information.  Additionally, all
202
 * single-digit types are stored in read-only static memory and do
203
 * not contribute to the writable memory footprint of a program using
204
 * `GVariant`.
205
 *
206
 * Aside from the type information structures stored in read-only
207
 * memory, there are two forms of type information.  One is used for
208
 * container types where there is a single element type: arrays and
209
 * maybe types.  The other is used for container types where there
210
 * are multiple element types: tuples and dictionary entries.
211
 *
212
 * Array type info structures are `6 * sizeof (void *)`, plus the
213
 * memory required to store the type string itself.  This means that
214
 * on 32-bit systems, the cache entry for `a{sv}` would require 30
215
 * bytes of memory (plus allocation overhead).
216
 *
217
 * Tuple type info structures are `6 * sizeof (void *)`, plus `4 *
218
 * sizeof (void *)` for each item in the tuple, plus the memory
219
 * required to store the type string itself.  A 2-item tuple, for
220
 * example, would have a type information structure that consumed
221
 * writable memory in the size of `14 * sizeof (void *)` (plus type
222
 * string)  This means that on 32-bit systems, the cache entry for
223
 * `{sv}` would require 61 bytes of memory (plus allocation overhead).
224
 *
225
 * This means that in total, for our `a{sv}` example, 91 bytes of
226
 * type information would be allocated.
227
 * 
228
 * The type information cache, additionally, uses a [struct@GLib.HashTable] to
229
 * store and look up the cached items and stores a pointer to this
230
 * hash table in static storage.  The hash table is freed when there
231
 * are zero items in the type cache.
232
 *
233
 * Although these sizes may seem large it is important to remember
234
 * that a program will probably only have a very small number of
235
 * different types of values in it and that only one type information
236
 * structure is required for many different values of the same type.
237
 *
238
 * ## Buffer Management Memory
239
 *
240
 * `GVariant` uses an internal buffer management structure to deal
241
 * with the various different possible sources of serialized data
242
 * that it uses.  The buffer is responsible for ensuring that the
243
 * correct call is made when the data is no longer in use by
244
 * `GVariant`.  This may involve a [func@GLib.free] or
245
 * even [method@GLib.MappedFile.unref].
246
 *
247
 * One buffer management structure is used for each chunk of
248
 * serialized data.  The size of the buffer management structure
249
 * is `4 * (void *)`.  On 32-bit systems, that’s 16 bytes.
250
 *
251
 * ## GVariant structure
252
 *
253
 * The size of a `GVariant` structure is `6 * (void *)`.  On 32-bit
254
 * systems, that’s 24 bytes.
255
 *
256
 * `GVariant` structures only exist if they are explicitly created
257
 * with API calls.  For example, if a `GVariant` is constructed out of
258
 * serialized data for the example given above (with the dictionary)
259
 * then although there are 9 individual values that comprise the
260
 * entire dictionary (two keys, two values, two variants containing
261
 * the values, two dictionary entries, plus the dictionary itself),
262
 * only 1 `GVariant` instance exists — the one referring to the
263
 * dictionary.
264
 *
265
 * If calls are made to start accessing the other values then
266
 * `GVariant` instances will exist for those values only for as long
267
 * as they are in use (ie: until you call [method@GLib.Variant.unref]).  The
268
 * type information is shared.  The serialized data and the buffer
269
 * management structure for that serialized data is shared by the
270
 * child.
271
 *
272
 * ## Summary
273
 *
274
 * To put the entire example together, for our dictionary mapping
275
 * strings to variants (with two entries, as given above), we are
276
 * using 91 bytes of memory for type information, 29 bytes of memory
277
 * for the serialized data, 16 bytes for buffer management and 24
278
 * bytes for the `GVariant` instance, or a total of 160 bytes, plus
279
 * allocation overhead.  If we were to use [method@GLib.Variant.get_child_value]
280
 * to access the two dictionary entries, we would use an additional 48
281
 * bytes.  If we were to have other dictionaries of the same type, we
282
 * would use more memory for the serialized data and buffer
283
 * management for those dictionaries, but the type information would
284
 * be shared.
285
 *
286
 * Since: 2.24
287
 */
288
289
/* definition of GVariant structure is in gvariant-core.c */
290
291
/* this is a g_return_val_if_fail() for making
292
 * sure a (GVariant *) has the required type.
293
 */
294
#define TYPE_CHECK(value, TYPE, val) \
295
60
  if G_UNLIKELY (!g_variant_is_of_type (value, TYPE)) {           \
296
0
    g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC,            \
297
0
                              "g_variant_is_of_type (" #value     \
298
0
                              ", " #TYPE ")");                    \
299
0
    return val;                                                   \
300
0
  }
301
302
/* Numeric Type Constructor/Getters {{{1 */
303
/* < private >
304
 * g_variant_new_from_trusted:
305
 * @type: the #GVariantType
306
 * @data: the data to use
307
 * @size: the size of @data
308
 *
309
 * Constructs a new trusted #GVariant instance from the provided data.
310
 * This is used to implement g_variant_new_* for all the basic types.
311
 *
312
 * Note: @data must be backed by memory that is aligned appropriately for the
313
 * @type being loaded. Otherwise this function will internally create a copy of
314
 * the memory (since GLib 2.60) or (in older versions) fail and exit the
315
 * process.
316
 *
317
 * Returns: a new floating #GVariant
318
 */
319
static GVariant *
320
g_variant_new_from_trusted (const GVariantType *type,
321
                            gconstpointer       data,
322
                            gsize               size)
323
0
{
324
0
  GVariant *value;
325
0
  GBytes *bytes;
326
327
0
  bytes = g_bytes_new (data, size);
328
0
  value = g_variant_new_from_bytes (type, bytes, TRUE);
329
0
  g_bytes_unref (bytes);
330
331
0
  return value;
332
0
}
333
334
/**
335
 * g_variant_new_boolean:
336
 * @value: a #gboolean value
337
 *
338
 * Creates a new boolean #GVariant instance -- either %TRUE or %FALSE.
339
 *
340
 * Returns: (transfer none): a floating reference to a new boolean #GVariant instance
341
 *
342
 * Since: 2.24
343
 **/
344
GVariant *
345
g_variant_new_boolean (gboolean value)
346
0
{
347
0
  guchar v = value;
348
349
0
  return g_variant_new_from_trusted (G_VARIANT_TYPE_BOOLEAN, &v, 1);
350
0
}
351
352
/**
353
 * g_variant_get_boolean:
354
 * @value: a boolean #GVariant instance
355
 *
356
 * Returns the boolean value of @value.
357
 *
358
 * It is an error to call this function with a @value of any type
359
 * other than %G_VARIANT_TYPE_BOOLEAN.
360
 *
361
 * Returns: %TRUE or %FALSE
362
 *
363
 * Since: 2.24
364
 **/
365
gboolean
366
g_variant_get_boolean (GVariant *value)
367
0
{
368
0
  const guchar *data;
369
370
0
  TYPE_CHECK (value, G_VARIANT_TYPE_BOOLEAN, FALSE);
371
372
0
  data = g_variant_get_data (value);
373
374
0
  return data != NULL ? *data != 0 : FALSE;
375
0
}
376
377
/* the constructors and accessors for byte, int{16,32,64}, handles and
378
 * doubles all look pretty much exactly the same, so we reduce
379
 * copy/pasting here.
380
 */
381
#define NUMERIC_TYPE(TYPE, type, ctype) \
382
0
  GVariant *g_variant_new_##type (ctype value) {                \
383
0
    return g_variant_new_from_trusted (G_VARIANT_TYPE_##TYPE,   \
384
0
                                       &value, sizeof value);   \
385
0
  }                                                             \
Unexecuted instantiation: g_variant_new_byte
Unexecuted instantiation: g_variant_new_int16
Unexecuted instantiation: g_variant_new_uint16
Unexecuted instantiation: g_variant_new_int32
Unexecuted instantiation: g_variant_new_uint32
Unexecuted instantiation: g_variant_new_int64
Unexecuted instantiation: g_variant_new_uint64
Unexecuted instantiation: g_variant_new_handle
Unexecuted instantiation: g_variant_new_double
386
40
  ctype g_variant_get_##type (GVariant *value) {                \
387
40
    const ctype *data;                                          \
388
40
    TYPE_CHECK (value, G_VARIANT_TYPE_ ## TYPE, 0);             \
389
40
    data = g_variant_get_data (value);                          \
390
40
    return data != NULL ? *data : 0;                            \
391
40
  }
Unexecuted instantiation: g_variant_get_byte
Unexecuted instantiation: g_variant_get_int16
Unexecuted instantiation: g_variant_get_uint16
Unexecuted instantiation: g_variant_get_int32
g_variant_get_uint32
Line
Count
Source
386
40
  ctype g_variant_get_##type (GVariant *value) {                \
387
40
    const ctype *data;                                          \
388
40
    TYPE_CHECK (value, G_VARIANT_TYPE_ ## TYPE, 0);             \
389
40
    data = g_variant_get_data (value);                          \
390
40
    return data != NULL ? *data : 0;                            \
391
40
  }
Unexecuted instantiation: g_variant_get_int64
Unexecuted instantiation: g_variant_get_uint64
Unexecuted instantiation: g_variant_get_handle
Unexecuted instantiation: g_variant_get_double
392
393
394
/**
395
 * g_variant_new_byte:
396
 * @value: a #guint8 value
397
 *
398
 * Creates a new byte #GVariant instance.
399
 *
400
 * Returns: (transfer none): a floating reference to a new byte #GVariant instance
401
 *
402
 * Since: 2.24
403
 **/
404
/**
405
 * g_variant_get_byte:
406
 * @value: a byte #GVariant instance
407
 *
408
 * Returns the byte value of @value.
409
 *
410
 * It is an error to call this function with a @value of any type
411
 * other than %G_VARIANT_TYPE_BYTE.
412
 *
413
 * Returns: a #guint8
414
 *
415
 * Since: 2.24
416
 **/
417
NUMERIC_TYPE (BYTE, byte, guint8)
418
419
/**
420
 * g_variant_new_int16:
421
 * @value: a #gint16 value
422
 *
423
 * Creates a new int16 #GVariant instance.
424
 *
425
 * Returns: (transfer none): a floating reference to a new int16 #GVariant instance
426
 *
427
 * Since: 2.24
428
 **/
429
/**
430
 * g_variant_get_int16:
431
 * @value: an int16 #GVariant instance
432
 *
433
 * Returns the 16-bit signed integer value of @value.
434
 *
435
 * It is an error to call this function with a @value of any type
436
 * other than %G_VARIANT_TYPE_INT16.
437
 *
438
 * Returns: a #gint16
439
 *
440
 * Since: 2.24
441
 **/
442
NUMERIC_TYPE (INT16, int16, gint16)
443
444
/**
445
 * g_variant_new_uint16:
446
 * @value: a #guint16 value
447
 *
448
 * Creates a new uint16 #GVariant instance.
449
 *
450
 * Returns: (transfer none): a floating reference to a new uint16 #GVariant instance
451
 *
452
 * Since: 2.24
453
 **/
454
/**
455
 * g_variant_get_uint16:
456
 * @value: a uint16 #GVariant instance
457
 *
458
 * Returns the 16-bit unsigned integer value of @value.
459
 *
460
 * It is an error to call this function with a @value of any type
461
 * other than %G_VARIANT_TYPE_UINT16.
462
 *
463
 * Returns: a #guint16
464
 *
465
 * Since: 2.24
466
 **/
467
NUMERIC_TYPE (UINT16, uint16, guint16)
468
469
/**
470
 * g_variant_new_int32:
471
 * @value: a #gint32 value
472
 *
473
 * Creates a new int32 #GVariant instance.
474
 *
475
 * Returns: (transfer none): a floating reference to a new int32 #GVariant instance
476
 *
477
 * Since: 2.24
478
 **/
479
/**
480
 * g_variant_get_int32:
481
 * @value: an int32 #GVariant instance
482
 *
483
 * Returns the 32-bit signed integer value of @value.
484
 *
485
 * It is an error to call this function with a @value of any type
486
 * other than %G_VARIANT_TYPE_INT32.
487
 *
488
 * Returns: a #gint32
489
 *
490
 * Since: 2.24
491
 **/
492
NUMERIC_TYPE (INT32, int32, gint32)
493
494
/**
495
 * g_variant_new_uint32:
496
 * @value: a #guint32 value
497
 *
498
 * Creates a new uint32 #GVariant instance.
499
 *
500
 * Returns: (transfer none): a floating reference to a new uint32 #GVariant instance
501
 *
502
 * Since: 2.24
503
 **/
504
/**
505
 * g_variant_get_uint32:
506
 * @value: a uint32 #GVariant instance
507
 *
508
 * Returns the 32-bit unsigned integer value of @value.
509
 *
510
 * It is an error to call this function with a @value of any type
511
 * other than %G_VARIANT_TYPE_UINT32.
512
 *
513
 * Returns: a #guint32
514
 *
515
 * Since: 2.24
516
 **/
517
NUMERIC_TYPE (UINT32, uint32, guint32)
518
519
/**
520
 * g_variant_new_int64:
521
 * @value: a #gint64 value
522
 *
523
 * Creates a new int64 #GVariant instance.
524
 *
525
 * Returns: (transfer none): a floating reference to a new int64 #GVariant instance
526
 *
527
 * Since: 2.24
528
 **/
529
/**
530
 * g_variant_get_int64:
531
 * @value: an int64 #GVariant instance
532
 *
533
 * Returns the 64-bit signed integer value of @value.
534
 *
535
 * It is an error to call this function with a @value of any type
536
 * other than %G_VARIANT_TYPE_INT64.
537
 *
538
 * Returns: a #gint64
539
 *
540
 * Since: 2.24
541
 **/
542
NUMERIC_TYPE (INT64, int64, gint64)
543
544
/**
545
 * g_variant_new_uint64:
546
 * @value: a #guint64 value
547
 *
548
 * Creates a new uint64 #GVariant instance.
549
 *
550
 * Returns: (transfer none): a floating reference to a new uint64 #GVariant instance
551
 *
552
 * Since: 2.24
553
 **/
554
/**
555
 * g_variant_get_uint64:
556
 * @value: a uint64 #GVariant instance
557
 *
558
 * Returns the 64-bit unsigned integer value of @value.
559
 *
560
 * It is an error to call this function with a @value of any type
561
 * other than %G_VARIANT_TYPE_UINT64.
562
 *
563
 * Returns: a #guint64
564
 *
565
 * Since: 2.24
566
 **/
567
NUMERIC_TYPE (UINT64, uint64, guint64)
568
569
/**
570
 * g_variant_new_handle:
571
 * @value: a #gint32 value
572
 *
573
 * Creates a new handle #GVariant instance.
574
 *
575
 * By convention, handles are indexes into an array of file descriptors
576
 * that are sent alongside a D-Bus message.  If you're not interacting
577
 * with D-Bus, you probably don't need them.
578
 *
579
 * Returns: (transfer none): a floating reference to a new handle #GVariant instance
580
 *
581
 * Since: 2.24
582
 **/
583
/**
584
 * g_variant_get_handle:
585
 * @value: a handle #GVariant instance
586
 *
587
 * Returns the 32-bit signed integer value of @value.
588
 *
589
 * It is an error to call this function with a @value of any type other
590
 * than %G_VARIANT_TYPE_HANDLE.
591
 *
592
 * By convention, handles are indexes into an array of file descriptors
593
 * that are sent alongside a D-Bus message.  If you're not interacting
594
 * with D-Bus, you probably don't need them.
595
 *
596
 * Returns: a #gint32
597
 *
598
 * Since: 2.24
599
 **/
600
NUMERIC_TYPE (HANDLE, handle, gint32)
601
602
/**
603
 * g_variant_new_double:
604
 * @value: a #gdouble floating point value
605
 *
606
 * Creates a new double #GVariant instance.
607
 *
608
 * Returns: (transfer none): a floating reference to a new double #GVariant instance
609
 *
610
 * Since: 2.24
611
 **/
612
/**
613
 * g_variant_get_double:
614
 * @value: a double #GVariant instance
615
 *
616
 * Returns the double precision floating point value of @value.
617
 *
618
 * It is an error to call this function with a @value of any type
619
 * other than %G_VARIANT_TYPE_DOUBLE.
620
 *
621
 * Returns: a #gdouble
622
 *
623
 * Since: 2.24
624
 **/
625
NUMERIC_TYPE (DOUBLE, double, gdouble)
626
627
/* Container type Constructor / Deconstructors {{{1 */
628
/**
629
 * g_variant_new_maybe:
630
 * @child_type: (nullable): the #GVariantType of the child, or %NULL
631
 * @child: (nullable): the child value, or %NULL
632
 *
633
 * Depending on if @child is %NULL, either wraps @child inside of a
634
 * maybe container or creates a Nothing instance for the given @type.
635
 *
636
 * At least one of @child_type and @child must be non-%NULL.
637
 * If @child_type is non-%NULL then it must be a definite type.
638
 * If they are both non-%NULL then @child_type must be the type
639
 * of @child.
640
 *
641
 * If @child is a floating reference (see g_variant_ref_sink()), the new
642
 * instance takes ownership of @child.
643
 *
644
 * Returns: (transfer none): a floating reference to a new #GVariant maybe instance
645
 *
646
 * Since: 2.24
647
 **/
648
GVariant *
649
g_variant_new_maybe (const GVariantType *child_type,
650
                     GVariant           *child)
651
0
{
652
0
  GVariantType *maybe_type;
653
0
  GVariant *value;
654
655
0
  g_return_val_if_fail (child_type == NULL || g_variant_type_is_definite
656
0
                        (child_type), 0);
657
0
  g_return_val_if_fail (child_type != NULL || child != NULL, NULL);
658
0
  g_return_val_if_fail (child_type == NULL || child == NULL ||
659
0
                        g_variant_is_of_type (child, child_type),
660
0
                        NULL);
661
662
0
  if (child_type == NULL)
663
0
    child_type = g_variant_get_type (child);
664
665
0
  maybe_type = g_variant_type_new_maybe (child_type);
666
667
0
  if (child != NULL)
668
0
    {
669
0
      GVariant **children;
670
0
      gboolean trusted;
671
672
0
      children = g_new (GVariant *, 1);
673
0
      children[0] = g_variant_ref_sink (child);
674
0
      trusted = g_variant_is_trusted (children[0]);
675
676
0
      value = g_variant_new_from_children (maybe_type, children, 1, trusted);
677
0
    }
678
0
  else
679
0
    value = g_variant_new_from_children (maybe_type, NULL, 0, TRUE);
680
681
0
  g_variant_type_free (maybe_type);
682
683
0
  return value;
684
0
}
685
686
/**
687
 * g_variant_get_maybe:
688
 * @value: a maybe-typed value
689
 *
690
 * Given a maybe-typed #GVariant instance, extract its value.  If the
691
 * value is Nothing, then this function returns %NULL.
692
 *
693
 * Returns: (nullable) (transfer full): the contents of @value, or %NULL
694
 *
695
 * Since: 2.24
696
 **/
697
GVariant *
698
g_variant_get_maybe (GVariant *value)
699
0
{
700
0
  TYPE_CHECK (value, G_VARIANT_TYPE_MAYBE, NULL);
701
702
0
  if (g_variant_n_children (value))
703
0
    return g_variant_get_child_value (value, 0);
704
705
0
  return NULL;
706
0
}
707
708
/**
709
 * g_variant_new_variant: (constructor)
710
 * @value: a #GVariant instance
711
 *
712
 * Boxes @value.  The result is a #GVariant instance representing a
713
 * variant containing the original value.
714
 *
715
 * If @child is a floating reference (see g_variant_ref_sink()), the new
716
 * instance takes ownership of @child.
717
 *
718
 * Returns: (transfer none): a floating reference to a new variant #GVariant instance
719
 *
720
 * Since: 2.24
721
 **/
722
GVariant *
723
g_variant_new_variant (GVariant *value)
724
0
{
725
0
  g_return_val_if_fail (value != NULL, NULL);
726
727
0
  g_variant_ref_sink (value);
728
729
0
  return g_variant_new_from_children (G_VARIANT_TYPE_VARIANT,
730
0
                                      g_memdup2 (&value, sizeof value),
731
0
                                      1, g_variant_is_trusted (value));
732
0
}
733
734
/**
735
 * g_variant_get_variant:
736
 * @value: a variant #GVariant instance
737
 *
738
 * Unboxes @value.  The result is the #GVariant instance that was
739
 * contained in @value.
740
 *
741
 * Returns: (transfer full): the item contained in the variant
742
 *
743
 * Since: 2.24
744
 **/
745
GVariant *
746
g_variant_get_variant (GVariant *value)
747
20
{
748
20
  TYPE_CHECK (value, G_VARIANT_TYPE_VARIANT, NULL);
749
750
20
  return g_variant_get_child_value (value, 0);
751
20
}
752
753
/**
754
 * g_variant_new_array:
755
 * @child_type: (nullable): the element type of the new array
756
 * @children: (nullable) (array length=n_children): an array of
757
 *            #GVariant pointers, the children
758
 * @n_children: the length of @children
759
 *
760
 * Creates a new #GVariant array from @children.
761
 *
762
 * @child_type must be non-%NULL if @n_children is zero.  Otherwise, the
763
 * child type is determined by inspecting the first element of the
764
 * @children array.  If @child_type is non-%NULL then it must be a
765
 * definite type.
766
 *
767
 * The items of the array are taken from the @children array.  No entry
768
 * in the @children array may be %NULL.
769
 *
770
 * All items in the array must have the same type, which must be the
771
 * same as @child_type, if given.
772
 *
773
 * If the @children are floating references (see g_variant_ref_sink()), the
774
 * new instance takes ownership of them as if via g_variant_ref_sink().
775
 *
776
 * Returns: (transfer none): a floating reference to a new #GVariant array
777
 *
778
 * Since: 2.24
779
 **/
780
GVariant *
781
g_variant_new_array (const GVariantType *child_type,
782
                     GVariant * const   *children,
783
                     gsize               n_children)
784
0
{
785
0
  GVariantType *array_type;
786
0
  GVariant **my_children;
787
0
  gboolean trusted;
788
0
  GVariant *value;
789
0
  gsize i;
790
791
0
  g_return_val_if_fail (n_children > 0 || child_type != NULL, NULL);
792
0
  g_return_val_if_fail (n_children == 0 || children != NULL, NULL);
793
0
  g_return_val_if_fail (child_type == NULL ||
794
0
                        g_variant_type_is_definite (child_type), NULL);
795
796
0
  my_children = g_new (GVariant *, n_children);
797
0
  trusted = TRUE;
798
799
0
  if (child_type == NULL)
800
0
    child_type = g_variant_get_type (children[0]);
801
0
  array_type = g_variant_type_new_array (child_type);
802
803
0
  for (i = 0; i < n_children; i++)
804
0
    {
805
0
      gboolean is_of_child_type = g_variant_is_of_type (children[i], child_type);
806
0
      if G_UNLIKELY (!is_of_child_type)
807
0
        {
808
0
          while (i != 0)
809
0
            g_variant_unref (my_children[--i]);
810
0
          g_free (my_children);
811
0
          g_return_val_if_fail (is_of_child_type, NULL);
812
0
        }
813
0
      my_children[i] = g_variant_ref_sink (children[i]);
814
0
      trusted &= g_variant_is_trusted (children[i]);
815
0
    }
816
817
0
  value = g_variant_new_from_children (array_type, my_children,
818
0
                                       n_children, trusted);
819
0
  g_variant_type_free (array_type);
820
821
0
  return value;
822
0
}
823
824
/*< private >
825
 * g_variant_make_tuple_type:
826
 * @children: (array length=n_children): an array of GVariant *
827
 * @n_children: the length of @children
828
 *
829
 * Return the type of a tuple containing @children as its items.
830
 **/
831
static GVariantType *
832
g_variant_make_tuple_type (GVariant * const *children,
833
                           gsize             n_children)
834
0
{
835
0
  const GVariantType **types;
836
0
  GVariantType *type;
837
0
  gsize i;
838
839
0
  types = g_new (const GVariantType *, n_children);
840
841
0
  for (i = 0; i < n_children; i++)
842
0
    types[i] = g_variant_get_type (children[i]);
843
844
0
  type = g_variant_type_new_tuple (types, n_children);
845
0
  g_free (types);
846
847
0
  return type;
848
0
}
849
850
/**
851
 * g_variant_new_tuple:
852
 * @children: (array length=n_children): the items to make the tuple out of
853
 * @n_children: the length of @children
854
 *
855
 * Creates a new tuple #GVariant out of the items in @children.  The
856
 * type is determined from the types of @children.  No entry in the
857
 * @children array may be %NULL.
858
 *
859
 * If @n_children is 0 then the unit tuple is constructed.
860
 *
861
 * If the @children are floating references (see g_variant_ref_sink()), the
862
 * new instance takes ownership of them as if via g_variant_ref_sink().
863
 *
864
 * Returns: (transfer none): a floating reference to a new #GVariant tuple
865
 *
866
 * Since: 2.24
867
 **/
868
GVariant *
869
g_variant_new_tuple (GVariant * const *children,
870
                     gsize             n_children)
871
0
{
872
0
  GVariantType *tuple_type;
873
0
  GVariant **my_children;
874
0
  gboolean trusted;
875
0
  GVariant *value;
876
0
  gsize i;
877
878
0
  g_return_val_if_fail (n_children == 0 || children != NULL, NULL);
879
880
0
  my_children = g_new (GVariant *, n_children);
881
0
  trusted = TRUE;
882
883
0
  for (i = 0; i < n_children; i++)
884
0
    {
885
0
      my_children[i] = g_variant_ref_sink (children[i]);
886
0
      trusted &= g_variant_is_trusted (children[i]);
887
0
    }
888
889
0
  tuple_type = g_variant_make_tuple_type (children, n_children);
890
0
  value = g_variant_new_from_children (tuple_type, my_children,
891
0
                                       n_children, trusted);
892
0
  g_variant_type_free (tuple_type);
893
894
0
  return value;
895
0
}
896
897
/*< private >
898
 * g_variant_make_dict_entry_type:
899
 * @key: a #GVariant, the key
900
 * @val: a #GVariant, the value
901
 *
902
 * Return the type of a dictionary entry containing @key and @val as its
903
 * children.
904
 **/
905
static GVariantType *
906
g_variant_make_dict_entry_type (GVariant *key,
907
                                GVariant *val)
908
0
{
909
0
  return g_variant_type_new_dict_entry (g_variant_get_type (key),
910
0
                                        g_variant_get_type (val));
911
0
}
912
913
/**
914
 * g_variant_new_dict_entry: (constructor)
915
 * @key: a basic #GVariant, the key
916
 * @value: a #GVariant, the value
917
 *
918
 * Creates a new dictionary entry #GVariant. @key and @value must be
919
 * non-%NULL. @key must be a value of a basic type (ie: not a container).
920
 *
921
 * If the @key or @value are floating references (see g_variant_ref_sink()),
922
 * the new instance takes ownership of them as if via g_variant_ref_sink().
923
 *
924
 * Returns: (transfer none): a floating reference to a new dictionary entry #GVariant
925
 *
926
 * Since: 2.24
927
 **/
928
GVariant *
929
g_variant_new_dict_entry (GVariant *key,
930
                          GVariant *value)
931
0
{
932
0
  GVariantType *dict_type;
933
0
  GVariant **children;
934
0
  gboolean trusted;
935
936
0
  g_return_val_if_fail (key != NULL && value != NULL, NULL);
937
0
  g_return_val_if_fail (!g_variant_is_container (key), NULL);
938
939
0
  children = g_new (GVariant *, 2);
940
0
  children[0] = g_variant_ref_sink (key);
941
0
  children[1] = g_variant_ref_sink (value);
942
0
  trusted = g_variant_is_trusted (key) && g_variant_is_trusted (value);
943
944
0
  dict_type = g_variant_make_dict_entry_type (key, value);
945
0
  value = g_variant_new_from_children (dict_type, children, 2, trusted);
946
0
  g_variant_type_free (dict_type);
947
948
0
  return value;
949
0
}
950
951
/**
952
 * g_variant_lookup: (skip)
953
 * @dictionary: a dictionary #GVariant
954
 * @key: the key to look up in the dictionary
955
 * @format_string: a GVariant format string
956
 * @...: the arguments to unpack the value into
957
 *
958
 * Looks up a value in a dictionary #GVariant.
959
 *
960
 * This function is a wrapper around g_variant_lookup_value() and
961
 * g_variant_get().  In the case that %NULL would have been returned,
962
 * this function returns %FALSE.  Otherwise, it unpacks the returned
963
 * value and returns %TRUE.
964
 *
965
 * @format_string determines the C types that are used for unpacking
966
 * the values and also determines if the values are copied or borrowed,
967
 * see the section on
968
 * [`GVariant` format strings](gvariant-format-strings.html#pointers).
969
 *
970
 * This function is currently implemented with a linear scan.  If you
971
 * plan to do many lookups then #GVariantDict may be more efficient.
972
 *
973
 * Returns: %TRUE if a value was unpacked
974
 *
975
 * Since: 2.28
976
 */
977
gboolean
978
g_variant_lookup (GVariant    *dictionary,
979
                  const gchar *key,
980
                  const gchar *format_string,
981
                  ...)
982
0
{
983
0
  GVariantType *type;
984
0
  GVariant *value;
985
986
  /* flatten */
987
0
  g_variant_get_data (dictionary);
988
989
0
  type = g_variant_format_string_scan_type (format_string, NULL, NULL);
990
0
  value = g_variant_lookup_value (dictionary, key, type);
991
0
  g_variant_type_free (type);
992
993
0
  if (value)
994
0
    {
995
0
      va_list ap;
996
997
0
      va_start (ap, format_string);
998
0
      g_variant_get_va (value, format_string, NULL, &ap);
999
0
      g_variant_unref (value);
1000
0
      va_end (ap);
1001
1002
0
      return TRUE;
1003
0
    }
1004
1005
0
  else
1006
0
    return FALSE;
1007
0
}
1008
1009
/**
1010
 * g_variant_lookup_value:
1011
 * @dictionary: a dictionary #GVariant
1012
 * @key: the key to look up in the dictionary
1013
 * @expected_type: (nullable): a #GVariantType, or %NULL
1014
 *
1015
 * Looks up a value in a dictionary #GVariant.
1016
 *
1017
 * This function works with dictionaries of the type a{s*} (and equally
1018
 * well with type a{o*}, but we only further discuss the string case
1019
 * for sake of clarity).
1020
 *
1021
 * In the event that @dictionary has the type a{sv}, the @expected_type
1022
 * string specifies what type of value is expected to be inside of the
1023
 * variant. If the value inside the variant has a different type then
1024
 * %NULL is returned. In the event that @dictionary has a value type other
1025
 * than v then @expected_type must directly match the value type and it is
1026
 * used to unpack the value directly or an error occurs.
1027
 *
1028
 * In either case, if @key is not found in @dictionary, %NULL is returned.
1029
 *
1030
 * If the key is found and the value has the correct type, it is
1031
 * returned.  If @expected_type was specified then any non-%NULL return
1032
 * value will have this type.
1033
 *
1034
 * This function is currently implemented with a linear scan.  If you
1035
 * plan to do many lookups then #GVariantDict may be more efficient.
1036
 *
1037
 * Returns: (transfer full): the value of the dictionary key, or %NULL
1038
 *
1039
 * Since: 2.28
1040
 */
1041
GVariant *
1042
g_variant_lookup_value (GVariant           *dictionary,
1043
                        const gchar        *key,
1044
                        const GVariantType *expected_type)
1045
0
{
1046
0
  GVariantIter iter;
1047
0
  GVariant *entry;
1048
0
  GVariant *value;
1049
1050
0
  g_return_val_if_fail (g_variant_is_of_type (dictionary,
1051
0
                                              G_VARIANT_TYPE ("a{s*}")) ||
1052
0
                        g_variant_is_of_type (dictionary,
1053
0
                                              G_VARIANT_TYPE ("a{o*}")),
1054
0
                        NULL);
1055
1056
0
  g_variant_iter_init (&iter, dictionary);
1057
1058
0
  while ((entry = g_variant_iter_next_value (&iter)))
1059
0
    {
1060
0
      GVariant *entry_key;
1061
0
      gboolean matches;
1062
1063
0
      entry_key = g_variant_get_child_value (entry, 0);
1064
0
      matches = strcmp (g_variant_get_string (entry_key, NULL), key) == 0;
1065
0
      g_variant_unref (entry_key);
1066
1067
0
      if (matches)
1068
0
        break;
1069
1070
0
      g_variant_unref (entry);
1071
0
    }
1072
1073
0
  if (entry == NULL)
1074
0
    return NULL;
1075
1076
0
  value = g_variant_get_child_value (entry, 1);
1077
0
  g_variant_unref (entry);
1078
1079
0
  if (g_variant_is_of_type (value, G_VARIANT_TYPE_VARIANT))
1080
0
    {
1081
0
      GVariant *tmp;
1082
1083
0
      tmp = g_variant_get_variant (value);
1084
0
      g_variant_unref (value);
1085
1086
0
      if (expected_type && !g_variant_is_of_type (tmp, expected_type))
1087
0
        {
1088
0
          g_variant_unref (tmp);
1089
0
          tmp = NULL;
1090
0
        }
1091
1092
0
      value = tmp;
1093
0
    }
1094
1095
0
  g_return_val_if_fail (expected_type == NULL || value == NULL ||
1096
0
                        g_variant_is_of_type (value, expected_type), NULL);
1097
1098
0
  return value;
1099
0
}
1100
1101
/**
1102
 * g_variant_get_fixed_array:
1103
 * @value: a #GVariant array with fixed-sized elements
1104
 * @n_elements: (out): a pointer to the location to store the number of items
1105
 * @element_size: the size of each element
1106
 *
1107
 * Provides access to the serialized data for an array of fixed-sized
1108
 * items.
1109
 *
1110
 * @value must be an array with fixed-sized elements.  Numeric types are
1111
 * fixed-size, as are tuples containing only other fixed-sized types.
1112
 *
1113
 * @element_size must be the size of a single element in the array,
1114
 * as given by the section on
1115
 * [serialized data memory][gvariant-serialized-data-memory].
1116
 *
1117
 * In particular, arrays of these fixed-sized types can be interpreted
1118
 * as an array of the given C type, with @element_size set to the size
1119
 * the appropriate type:
1120
 * - %G_VARIANT_TYPE_INT16 (etc.): #gint16 (etc.)
1121
 * - %G_VARIANT_TYPE_BOOLEAN: #guchar (not #gboolean!)
1122
 * - %G_VARIANT_TYPE_BYTE: #guint8
1123
 * - %G_VARIANT_TYPE_HANDLE: #guint32
1124
 * - %G_VARIANT_TYPE_DOUBLE: #gdouble
1125
 *
1126
 * For example, if calling this function for an array of 32-bit integers,
1127
 * you might say `sizeof(gint32)`. This value isn't used except for the purpose
1128
 * of a double-check that the form of the serialized data matches the caller's
1129
 * expectation.
1130
 *
1131
 * @n_elements, which must be non-%NULL, is set equal to the number of
1132
 * items in the array.
1133
 *
1134
 * Returns: (array length=n_elements) (transfer none): a pointer to
1135
 *     the fixed array
1136
 *
1137
 * Since: 2.24
1138
 **/
1139
gconstpointer
1140
g_variant_get_fixed_array (GVariant *value,
1141
                           gsize    *n_elements,
1142
                           gsize     element_size)
1143
0
{
1144
0
  GVariantTypeInfo *array_info;
1145
0
  gsize array_element_size;
1146
0
  gconstpointer data;
1147
0
  gsize size;
1148
1149
0
  TYPE_CHECK (value, G_VARIANT_TYPE_ARRAY, NULL);
1150
1151
0
  g_return_val_if_fail (n_elements != NULL, NULL);
1152
0
  g_return_val_if_fail (element_size > 0, NULL);
1153
1154
0
  array_info = g_variant_get_type_info (value);
1155
0
  g_variant_type_info_query_element (array_info, NULL, &array_element_size);
1156
1157
0
  g_return_val_if_fail (array_element_size, NULL);
1158
1159
0
  if G_UNLIKELY (array_element_size != element_size)
1160
0
    {
1161
0
      if (array_element_size)
1162
0
        g_critical ("g_variant_get_fixed_array: assertion "
1163
0
                    "'g_variant_array_has_fixed_size (value, element_size)' "
1164
0
                    "failed: array size %"G_GSIZE_FORMAT" does not match "
1165
0
                    "given element_size %"G_GSIZE_FORMAT".",
1166
0
                    array_element_size, element_size);
1167
0
      else
1168
0
        g_critical ("g_variant_get_fixed_array: assertion "
1169
0
                    "'g_variant_array_has_fixed_size (value, element_size)' "
1170
0
                    "failed: array does not have fixed size.");
1171
0
    }
1172
1173
0
  data = g_variant_get_data (value);
1174
0
  size = g_variant_get_size (value);
1175
1176
0
  if (size % element_size)
1177
0
    *n_elements = 0;
1178
0
  else
1179
0
    *n_elements = size / element_size;
1180
1181
0
  if (*n_elements)
1182
0
    return data;
1183
1184
0
  return NULL;
1185
0
}
1186
1187
/**
1188
 * g_variant_new_fixed_array:
1189
 * @element_type: the #GVariantType of each element
1190
 * @elements: a pointer to the fixed array of contiguous elements
1191
 * @n_elements: the number of elements
1192
 * @element_size: the size of each element
1193
 *
1194
 * Constructs a new array #GVariant instance, where the elements are
1195
 * of @element_type type.
1196
 *
1197
 * @elements must be an array with fixed-sized elements.  Numeric types are
1198
 * fixed-size as are tuples containing only other fixed-sized types.
1199
 *
1200
 * @element_size must be the size of a single element in the array.
1201
 * For example, if calling this function for an array of 32-bit integers,
1202
 * you might say sizeof(gint32). This value isn't used except for the purpose
1203
 * of a double-check that the form of the serialized data matches the caller's
1204
 * expectation.
1205
 *
1206
 * @n_elements must be the length of the @elements array.
1207
 *
1208
 * Returns: (transfer none): a floating reference to a new array #GVariant instance
1209
 *
1210
 * Since: 2.32
1211
 **/
1212
GVariant *
1213
g_variant_new_fixed_array (const GVariantType  *element_type,
1214
                           gconstpointer        elements,
1215
                           gsize                n_elements,
1216
                           gsize                element_size)
1217
0
{
1218
0
  GVariantType *array_type;
1219
0
  gsize array_element_size;
1220
0
  GVariantTypeInfo *array_info;
1221
0
  GVariant *value;
1222
0
  gpointer data;
1223
1224
0
  g_return_val_if_fail (g_variant_type_is_definite (element_type), NULL);
1225
0
  g_return_val_if_fail (element_size > 0, NULL);
1226
1227
0
  array_type = g_variant_type_new_array (element_type);
1228
0
  array_info = g_variant_type_info_get (array_type);
1229
0
  g_variant_type_info_query_element (array_info, NULL, &array_element_size);
1230
0
  if G_UNLIKELY (array_element_size != element_size)
1231
0
    {
1232
0
      if (array_element_size)
1233
0
        g_critical ("g_variant_new_fixed_array: array size %" G_GSIZE_FORMAT
1234
0
                    " does not match given element_size %" G_GSIZE_FORMAT ".",
1235
0
                    array_element_size, element_size);
1236
0
      else
1237
0
        g_critical ("g_variant_get_fixed_array: array does not have fixed size.");
1238
0
      return NULL;
1239
0
    }
1240
1241
0
  data = g_memdup2 (elements, n_elements * element_size);
1242
0
  value = g_variant_new_from_data (array_type, data,
1243
0
                                   n_elements * element_size,
1244
0
                                   FALSE, g_free, data);
1245
1246
0
  g_variant_type_free (array_type);
1247
0
  g_variant_type_info_unref (array_info);
1248
1249
0
  return value;
1250
0
}
1251
1252
/* String type constructor/getters/validation {{{1 */
1253
/**
1254
 * g_variant_new_string:
1255
 * @string: a normal UTF-8 nul-terminated string
1256
 *
1257
 * Creates a string #GVariant with the contents of @string.
1258
 *
1259
 * @string must be valid UTF-8, and must not be %NULL. To encode
1260
 * potentially-%NULL strings, use g_variant_new() with `ms` as the
1261
 * [format string][gvariant-format-strings-maybe-types].
1262
 *
1263
 * Returns: (transfer none): a floating reference to a new string #GVariant instance
1264
 *
1265
 * Since: 2.24
1266
 **/
1267
GVariant *
1268
g_variant_new_string (const gchar *string)
1269
0
{
1270
0
  g_return_val_if_fail (string != NULL, NULL);
1271
0
  g_return_val_if_fail (g_utf8_validate (string, -1, NULL), NULL);
1272
1273
0
  return g_variant_new_from_trusted (G_VARIANT_TYPE_STRING,
1274
0
                                     string, strlen (string) + 1);
1275
0
}
1276
1277
/**
1278
 * g_variant_new_take_string: (skip)
1279
 * @string: a normal UTF-8 nul-terminated string
1280
 *
1281
 * Creates a string #GVariant with the contents of @string.
1282
 *
1283
 * @string must be valid UTF-8, and must not be %NULL. To encode
1284
 * potentially-%NULL strings, use this with g_variant_new_maybe().
1285
 *
1286
 * After this call, @string belongs to the #GVariant and may no longer be
1287
 * modified by the caller. The memory of @data has to be dynamically
1288
 * allocated and will eventually be freed with g_free().
1289
 *
1290
 * You must not modify or access @string in any other way after passing
1291
 * it to this function.  It is even possible that @string is immediately
1292
 * freed.
1293
 *
1294
 * Returns: (transfer none): a floating reference to a new string
1295
 *   #GVariant instance
1296
 *
1297
 * Since: 2.38
1298
 **/
1299
GVariant *
1300
g_variant_new_take_string (gchar *string)
1301
0
{
1302
0
  GVariant *value;
1303
0
  GBytes *bytes;
1304
1305
0
  g_return_val_if_fail (string != NULL, NULL);
1306
0
  g_return_val_if_fail (g_utf8_validate (string, -1, NULL), NULL);
1307
1308
0
  bytes = g_bytes_new_take (string, strlen (string) + 1);
1309
0
  value = g_variant_new_from_bytes (G_VARIANT_TYPE_STRING, bytes, TRUE);
1310
0
  g_bytes_unref (bytes);
1311
1312
0
  return value;
1313
0
}
1314
1315
/**
1316
 * g_variant_new_printf: (skip)
1317
 * @format_string: a printf-style format string
1318
 * @...: arguments for @format_string
1319
 *
1320
 * Creates a string-type GVariant using printf formatting.
1321
 *
1322
 * This is similar to calling g_strdup_printf() and then
1323
 * g_variant_new_string() but it saves a temporary variable and an
1324
 * unnecessary copy.
1325
 *
1326
 * Returns: (transfer none): a floating reference to a new string
1327
 *   #GVariant instance
1328
 *
1329
 * Since: 2.38
1330
 **/
1331
GVariant *
1332
g_variant_new_printf (const gchar *format_string,
1333
                      ...)
1334
0
{
1335
0
  GVariant *value;
1336
0
  GBytes *bytes;
1337
0
  gchar *string;
1338
0
  va_list ap;
1339
1340
0
  g_return_val_if_fail (format_string != NULL, NULL);
1341
1342
0
  va_start (ap, format_string);
1343
0
  string = g_strdup_vprintf (format_string, ap);
1344
0
  va_end (ap);
1345
1346
0
  bytes = g_bytes_new_take (string, strlen (string) + 1);
1347
0
  value = g_variant_new_from_bytes (G_VARIANT_TYPE_STRING, bytes, TRUE);
1348
0
  g_bytes_unref (bytes);
1349
1350
0
  return value;
1351
0
}
1352
1353
/**
1354
 * g_variant_new_object_path:
1355
 * @object_path: a normal C nul-terminated string
1356
 *
1357
 * Creates a D-Bus object path #GVariant with the contents of @object_path.
1358
 * @object_path must be a valid D-Bus object path.  Use
1359
 * g_variant_is_object_path() if you're not sure.
1360
 *
1361
 * Returns: (transfer none): a floating reference to a new object path #GVariant instance
1362
 *
1363
 * Since: 2.24
1364
 **/
1365
GVariant *
1366
g_variant_new_object_path (const gchar *object_path)
1367
0
{
1368
0
  g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1369
1370
0
  return g_variant_new_from_trusted (G_VARIANT_TYPE_OBJECT_PATH,
1371
0
                                     object_path, strlen (object_path) + 1);
1372
0
}
1373
1374
/**
1375
 * g_variant_is_object_path:
1376
 * @string: a normal C nul-terminated string
1377
 *
1378
 * Determines if a given string is a valid D-Bus object path.  You
1379
 * should ensure that a string is a valid D-Bus object path before
1380
 * passing it to g_variant_new_object_path().
1381
 *
1382
 * A valid object path starts with `/` followed by zero or more
1383
 * sequences of characters separated by `/` characters.  Each sequence
1384
 * must contain only the characters `[A-Z][a-z][0-9]_`.  No sequence
1385
 * (including the one following the final `/` character) may be empty.
1386
 *
1387
 * Returns: %TRUE if @string is a D-Bus object path
1388
 *
1389
 * Since: 2.24
1390
 **/
1391
gboolean
1392
g_variant_is_object_path (const gchar *string)
1393
0
{
1394
0
  g_return_val_if_fail (string != NULL, FALSE);
1395
1396
0
  return g_variant_serialiser_is_object_path (string, strlen (string) + 1);
1397
0
}
1398
1399
/**
1400
 * g_variant_new_signature:
1401
 * @signature: a normal C nul-terminated string
1402
 *
1403
 * Creates a D-Bus type signature #GVariant with the contents of
1404
 * @string.  @string must be a valid D-Bus type signature.  Use
1405
 * g_variant_is_signature() if you're not sure.
1406
 *
1407
 * Returns: (transfer none): a floating reference to a new signature #GVariant instance
1408
 *
1409
 * Since: 2.24
1410
 **/
1411
GVariant *
1412
g_variant_new_signature (const gchar *signature)
1413
0
{
1414
0
  g_return_val_if_fail (g_variant_is_signature (signature), NULL);
1415
1416
0
  return g_variant_new_from_trusted (G_VARIANT_TYPE_SIGNATURE,
1417
0
                                     signature, strlen (signature) + 1);
1418
0
}
1419
1420
/**
1421
 * g_variant_is_signature:
1422
 * @string: a normal C nul-terminated string
1423
 *
1424
 * Determines if a given string is a valid D-Bus type signature.  You
1425
 * should ensure that a string is a valid D-Bus type signature before
1426
 * passing it to g_variant_new_signature().
1427
 *
1428
 * D-Bus type signatures consist of zero or more definite #GVariantType
1429
 * strings in sequence.
1430
 *
1431
 * Returns: %TRUE if @string is a D-Bus type signature
1432
 *
1433
 * Since: 2.24
1434
 **/
1435
gboolean
1436
g_variant_is_signature (const gchar *string)
1437
0
{
1438
0
  g_return_val_if_fail (string != NULL, FALSE);
1439
1440
0
  return g_variant_serialiser_is_signature (string, strlen (string) + 1);
1441
0
}
1442
1443
/**
1444
 * g_variant_get_string:
1445
 * @value: a string #GVariant instance
1446
 * @length: (optional) (default 0) (out): a pointer to a #gsize,
1447
 *          to store the length
1448
 *
1449
 * Returns the string value of a #GVariant instance with a string
1450
 * type.  This includes the types %G_VARIANT_TYPE_STRING,
1451
 * %G_VARIANT_TYPE_OBJECT_PATH and %G_VARIANT_TYPE_SIGNATURE.
1452
 *
1453
 * The string will always be UTF-8 encoded, will never be %NULL, and will never
1454
 * contain nul bytes.
1455
 *
1456
 * If @length is non-%NULL then the length of the string (in bytes) is
1457
 * returned there.  For trusted values, this information is already
1458
 * known.  Untrusted values will be validated and, if valid, a strlen() will be
1459
 * performed. If invalid, a default value will be returned — for
1460
 * %G_VARIANT_TYPE_OBJECT_PATH, this is `"/"`, and for other types it is the
1461
 * empty string.
1462
 *
1463
 * It is an error to call this function with a @value of any type
1464
 * other than those three.
1465
 *
1466
 * The return value remains valid as long as @value exists.
1467
 *
1468
 * Returns: (transfer none): the constant string, UTF-8 encoded
1469
 *
1470
 * Since: 2.24
1471
 **/
1472
const gchar *
1473
g_variant_get_string (GVariant *value,
1474
                      gsize    *length)
1475
0
{
1476
0
  gconstpointer data;
1477
0
  gsize size;
1478
1479
0
  g_return_val_if_fail (value != NULL, NULL);
1480
0
  g_return_val_if_fail (
1481
0
    g_variant_is_of_type (value, G_VARIANT_TYPE_STRING) ||
1482
0
    g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH) ||
1483
0
    g_variant_is_of_type (value, G_VARIANT_TYPE_SIGNATURE), NULL);
1484
1485
0
  data = g_variant_get_data (value);
1486
0
  size = g_variant_get_size (value);
1487
1488
0
  if (!g_variant_is_trusted (value))
1489
0
    {
1490
0
      switch (g_variant_classify (value))
1491
0
        {
1492
0
        case G_VARIANT_CLASS_STRING:
1493
0
          if (g_variant_serialiser_is_string (data, size))
1494
0
            break;
1495
1496
0
          data = "";
1497
0
          size = 1;
1498
0
          break;
1499
1500
0
        case G_VARIANT_CLASS_OBJECT_PATH:
1501
0
          if (g_variant_serialiser_is_object_path (data, size))
1502
0
            break;
1503
1504
0
          data = "/";
1505
0
          size = 2;
1506
0
          break;
1507
1508
0
        case G_VARIANT_CLASS_SIGNATURE:
1509
0
          if (g_variant_serialiser_is_signature (data, size))
1510
0
            break;
1511
1512
0
          data = "";
1513
0
          size = 1;
1514
0
          break;
1515
1516
0
        default:
1517
0
          g_assert_not_reached ();
1518
0
        }
1519
0
    }
1520
1521
0
  if (length)
1522
0
    *length = size - 1;
1523
1524
0
  return data;
1525
0
}
1526
1527
/**
1528
 * g_variant_dup_string:
1529
 * @value: a string #GVariant instance
1530
 * @length: (out): a pointer to a #gsize, to store the length
1531
 *
1532
 * Similar to g_variant_get_string() except that instead of returning
1533
 * a constant string, the string is duplicated.
1534
 *
1535
 * The string will always be UTF-8 encoded.
1536
 *
1537
 * The return value must be freed using g_free().
1538
 *
1539
 * Returns: (transfer full): a newly allocated string, UTF-8 encoded
1540
 *
1541
 * Since: 2.24
1542
 **/
1543
gchar *
1544
g_variant_dup_string (GVariant *value,
1545
                      gsize    *length)
1546
0
{
1547
0
  return g_strdup (g_variant_get_string (value, length));
1548
0
}
1549
1550
/**
1551
 * g_variant_new_strv:
1552
 * @strv: (array length=length) (element-type utf8): an array of strings
1553
 * @length: the length of @strv, or -1
1554
 *
1555
 * Constructs an array of strings #GVariant from the given array of
1556
 * strings.
1557
 *
1558
 * If @length is -1 then @strv is %NULL-terminated.
1559
 *
1560
 * Returns: (transfer none): a new floating #GVariant instance
1561
 *
1562
 * Since: 2.24
1563
 **/
1564
GVariant *
1565
g_variant_new_strv (const gchar * const *strv,
1566
                    gssize               length)
1567
0
{
1568
0
  GVariant **strings;
1569
0
  gsize i, length_unsigned;
1570
1571
0
  g_return_val_if_fail (length == 0 || strv != NULL, NULL);
1572
1573
0
  if (length < 0)
1574
0
    length = g_strv_length ((gchar **) strv);
1575
0
  length_unsigned = length;
1576
1577
0
  strings = g_new (GVariant *, length_unsigned);
1578
0
  for (i = 0; i < length_unsigned; i++)
1579
0
    strings[i] = g_variant_ref_sink (g_variant_new_string (strv[i]));
1580
1581
0
  return g_variant_new_from_children (G_VARIANT_TYPE_STRING_ARRAY,
1582
0
                                      strings, length_unsigned, TRUE);
1583
0
}
1584
1585
/**
1586
 * g_variant_get_strv:
1587
 * @value: an array of strings #GVariant
1588
 * @length: (out) (optional): the length of the result, or %NULL
1589
 *
1590
 * Gets the contents of an array of strings #GVariant.  This call
1591
 * makes a shallow copy; the return result should be released with
1592
 * g_free(), but the individual strings must not be modified.
1593
 *
1594
 * If @length is non-%NULL then the number of elements in the result
1595
 * is stored there.  In any case, the resulting array will be
1596
 * %NULL-terminated.
1597
 *
1598
 * For an empty array, @length will be set to 0 and a pointer to a
1599
 * %NULL pointer will be returned.
1600
 *
1601
 * Returns: (array length=length zero-terminated=1) (transfer container): an array of constant strings
1602
 *
1603
 * Since: 2.24
1604
 **/
1605
const gchar **
1606
g_variant_get_strv (GVariant *value,
1607
                    gsize    *length)
1608
0
{
1609
0
  const gchar **strv;
1610
0
  gsize n;
1611
0
  gsize i;
1612
1613
0
  TYPE_CHECK (value, G_VARIANT_TYPE_STRING_ARRAY, NULL);
1614
1615
0
  g_variant_get_data (value);
1616
0
  n = g_variant_n_children (value);
1617
0
  strv = g_new (const gchar *, n + 1);
1618
1619
0
  for (i = 0; i < n; i++)
1620
0
    {
1621
0
      GVariant *string;
1622
1623
0
      string = g_variant_get_child_value (value, i);
1624
0
      strv[i] = g_variant_get_string (string, NULL);
1625
0
      g_variant_unref (string);
1626
0
    }
1627
0
  strv[i] = NULL;
1628
1629
0
  if (length)
1630
0
    *length = n;
1631
1632
0
  return strv;
1633
0
}
1634
1635
/**
1636
 * g_variant_dup_strv:
1637
 * @value: an array of strings #GVariant
1638
 * @length: (out) (optional): the length of the result, or %NULL
1639
 *
1640
 * Gets the contents of an array of strings #GVariant.  This call
1641
 * makes a deep copy; the return result should be released with
1642
 * g_strfreev().
1643
 *
1644
 * If @length is non-%NULL then the number of elements in the result
1645
 * is stored there.  In any case, the resulting array will be
1646
 * %NULL-terminated.
1647
 *
1648
 * For an empty array, @length will be set to 0 and a pointer to a
1649
 * %NULL pointer will be returned.
1650
 *
1651
 * Returns: (array length=length zero-terminated=1) (transfer full): an array of strings
1652
 *
1653
 * Since: 2.24
1654
 **/
1655
gchar **
1656
g_variant_dup_strv (GVariant *value,
1657
                    gsize    *length)
1658
0
{
1659
0
  gchar **strv;
1660
0
  gsize n;
1661
0
  gsize i;
1662
1663
0
  TYPE_CHECK (value, G_VARIANT_TYPE_STRING_ARRAY, NULL);
1664
1665
0
  n = g_variant_n_children (value);
1666
0
  strv = g_new (gchar *, n + 1);
1667
1668
0
  for (i = 0; i < n; i++)
1669
0
    {
1670
0
      GVariant *string;
1671
1672
0
      string = g_variant_get_child_value (value, i);
1673
0
      strv[i] = g_variant_dup_string (string, NULL);
1674
0
      g_variant_unref (string);
1675
0
    }
1676
0
  strv[i] = NULL;
1677
1678
0
  if (length)
1679
0
    *length = n;
1680
1681
0
  return strv;
1682
0
}
1683
1684
/**
1685
 * g_variant_new_objv:
1686
 * @strv: (array length=length) (element-type utf8): an array of strings
1687
 * @length: the length of @strv, or -1
1688
 *
1689
 * Constructs an array of object paths #GVariant from the given array of
1690
 * strings.
1691
 *
1692
 * Each string must be a valid #GVariant object path; see
1693
 * g_variant_is_object_path().
1694
 *
1695
 * If @length is -1 then @strv is %NULL-terminated.
1696
 *
1697
 * Returns: (transfer none): a new floating #GVariant instance
1698
 *
1699
 * Since: 2.30
1700
 **/
1701
GVariant *
1702
g_variant_new_objv (const gchar * const *strv,
1703
                    gssize               length)
1704
0
{
1705
0
  GVariant **strings;
1706
0
  gsize i, length_unsigned;
1707
1708
0
  g_return_val_if_fail (length == 0 || strv != NULL, NULL);
1709
1710
0
  if (length < 0)
1711
0
    length = g_strv_length ((gchar **) strv);
1712
0
  length_unsigned = length;
1713
1714
0
  strings = g_new (GVariant *, length_unsigned);
1715
0
  for (i = 0; i < length_unsigned; i++)
1716
0
    strings[i] = g_variant_ref_sink (g_variant_new_object_path (strv[i]));
1717
1718
0
  return g_variant_new_from_children (G_VARIANT_TYPE_OBJECT_PATH_ARRAY,
1719
0
                                      strings, length_unsigned, TRUE);
1720
0
}
1721
1722
/**
1723
 * g_variant_get_objv:
1724
 * @value: an array of object paths #GVariant
1725
 * @length: (out) (optional): the length of the result, or %NULL
1726
 *
1727
 * Gets the contents of an array of object paths #GVariant.  This call
1728
 * makes a shallow copy; the return result should be released with
1729
 * g_free(), but the individual strings must not be modified.
1730
 *
1731
 * If @length is non-%NULL then the number of elements in the result
1732
 * is stored there.  In any case, the resulting array will be
1733
 * %NULL-terminated.
1734
 *
1735
 * For an empty array, @length will be set to 0 and a pointer to a
1736
 * %NULL pointer will be returned.
1737
 *
1738
 * Returns: (array length=length zero-terminated=1) (transfer container): an array of constant strings
1739
 *
1740
 * Since: 2.30
1741
 **/
1742
const gchar **
1743
g_variant_get_objv (GVariant *value,
1744
                    gsize    *length)
1745
0
{
1746
0
  const gchar **strv;
1747
0
  gsize n;
1748
0
  gsize i;
1749
1750
0
  TYPE_CHECK (value, G_VARIANT_TYPE_OBJECT_PATH_ARRAY, NULL);
1751
1752
0
  g_variant_get_data (value);
1753
0
  n = g_variant_n_children (value);
1754
0
  strv = g_new (const gchar *, n + 1);
1755
1756
0
  for (i = 0; i < n; i++)
1757
0
    {
1758
0
      GVariant *string;
1759
1760
0
      string = g_variant_get_child_value (value, i);
1761
0
      strv[i] = g_variant_get_string (string, NULL);
1762
0
      g_variant_unref (string);
1763
0
    }
1764
0
  strv[i] = NULL;
1765
1766
0
  if (length)
1767
0
    *length = n;
1768
1769
0
  return strv;
1770
0
}
1771
1772
/**
1773
 * g_variant_dup_objv:
1774
 * @value: an array of object paths #GVariant
1775
 * @length: (out) (optional): the length of the result, or %NULL
1776
 *
1777
 * Gets the contents of an array of object paths #GVariant.  This call
1778
 * makes a deep copy; the return result should be released with
1779
 * g_strfreev().
1780
 *
1781
 * If @length is non-%NULL then the number of elements in the result
1782
 * is stored there.  In any case, the resulting array will be
1783
 * %NULL-terminated.
1784
 *
1785
 * For an empty array, @length will be set to 0 and a pointer to a
1786
 * %NULL pointer will be returned.
1787
 *
1788
 * Returns: (array length=length zero-terminated=1) (transfer full): an array of strings
1789
 *
1790
 * Since: 2.30
1791
 **/
1792
gchar **
1793
g_variant_dup_objv (GVariant *value,
1794
                    gsize    *length)
1795
0
{
1796
0
  gchar **strv;
1797
0
  gsize n;
1798
0
  gsize i;
1799
1800
0
  TYPE_CHECK (value, G_VARIANT_TYPE_OBJECT_PATH_ARRAY, NULL);
1801
1802
0
  n = g_variant_n_children (value);
1803
0
  strv = g_new (gchar *, n + 1);
1804
1805
0
  for (i = 0; i < n; i++)
1806
0
    {
1807
0
      GVariant *string;
1808
1809
0
      string = g_variant_get_child_value (value, i);
1810
0
      strv[i] = g_variant_dup_string (string, NULL);
1811
0
      g_variant_unref (string);
1812
0
    }
1813
0
  strv[i] = NULL;
1814
1815
0
  if (length)
1816
0
    *length = n;
1817
1818
0
  return strv;
1819
0
}
1820
1821
1822
/**
1823
 * g_variant_new_bytestring:
1824
 * @string: (array zero-terminated=1) (element-type guint8): a normal
1825
 *          nul-terminated string in no particular encoding
1826
 *
1827
 * Creates an array-of-bytes #GVariant with the contents of @string.
1828
 * This function is just like g_variant_new_string() except that the
1829
 * string need not be valid UTF-8.
1830
 *
1831
 * The nul terminator character at the end of the string is stored in
1832
 * the array.
1833
 *
1834
 * Returns: (transfer none): a floating reference to a new bytestring #GVariant instance
1835
 *
1836
 * Since: 2.26
1837
 **/
1838
GVariant *
1839
g_variant_new_bytestring (const gchar *string)
1840
0
{
1841
0
  g_return_val_if_fail (string != NULL, NULL);
1842
1843
0
  return g_variant_new_from_trusted (G_VARIANT_TYPE_BYTESTRING,
1844
0
                                     string, strlen (string) + 1);
1845
0
}
1846
1847
/**
1848
 * g_variant_get_bytestring:
1849
 * @value: an array-of-bytes #GVariant instance
1850
 *
1851
 * Returns the string value of a #GVariant instance with an
1852
 * array-of-bytes type.  The string has no particular encoding.
1853
 *
1854
 * If the array does not end with a nul terminator character, the empty
1855
 * string is returned.  For this reason, you can always trust that a
1856
 * non-%NULL nul-terminated string will be returned by this function.
1857
 *
1858
 * If the array contains a nul terminator character somewhere other than
1859
 * the last byte then the returned string is the string, up to the first
1860
 * such nul character.
1861
 *
1862
 * g_variant_get_fixed_array() should be used instead if the array contains
1863
 * arbitrary data that could not be nul-terminated or could contain nul bytes.
1864
 *
1865
 * It is an error to call this function with a @value that is not an
1866
 * array of bytes.
1867
 *
1868
 * The return value remains valid as long as @value exists.
1869
 *
1870
 * Returns: (transfer none) (array zero-terminated=1) (element-type guint8):
1871
 *          the constant string
1872
 *
1873
 * Since: 2.26
1874
 **/
1875
const gchar *
1876
g_variant_get_bytestring (GVariant *value)
1877
0
{
1878
0
  const gchar *string;
1879
0
  gsize size;
1880
1881
0
  TYPE_CHECK (value, G_VARIANT_TYPE_BYTESTRING, NULL);
1882
1883
  /* Won't be NULL since this is an array type */
1884
0
  string = g_variant_get_data (value);
1885
0
  size = g_variant_get_size (value);
1886
1887
0
  if (size && string[size - 1] == '\0')
1888
0
    return string;
1889
0
  else
1890
0
    return "";
1891
0
}
1892
1893
/**
1894
 * g_variant_dup_bytestring:
1895
 * @value: an array-of-bytes #GVariant instance
1896
 * @length: (out) (optional) (default NULL): a pointer to a #gsize, to store
1897
 *          the length (not including the nul terminator)
1898
 *
1899
 * Similar to g_variant_get_bytestring() except that instead of
1900
 * returning a constant string, the string is duplicated.
1901
 *
1902
 * The return value must be freed using g_free().
1903
 *
1904
 * Returns: (transfer full) (array zero-terminated=1 length=length) (element-type guint8):
1905
 *          a newly allocated string
1906
 *
1907
 * Since: 2.26
1908
 **/
1909
gchar *
1910
g_variant_dup_bytestring (GVariant *value,
1911
                          gsize    *length)
1912
0
{
1913
0
  const gchar *original = g_variant_get_bytestring (value);
1914
0
  gsize size;
1915
1916
  /* don't crash in case get_bytestring() had an assert failure */
1917
0
  if (original == NULL)
1918
0
    return NULL;
1919
1920
0
  size = strlen (original);
1921
1922
0
  if (length)
1923
0
    *length = size;
1924
1925
0
  return g_memdup2 (original, size + 1);
1926
0
}
1927
1928
/**
1929
 * g_variant_new_bytestring_array:
1930
 * @strv: (array length=length): an array of strings
1931
 * @length: the length of @strv, or -1
1932
 *
1933
 * Constructs an array of bytestring #GVariant from the given array of
1934
 * strings.
1935
 *
1936
 * If @length is -1 then @strv is %NULL-terminated.
1937
 *
1938
 * Returns: (transfer none): a new floating #GVariant instance
1939
 *
1940
 * Since: 2.26
1941
 **/
1942
GVariant *
1943
g_variant_new_bytestring_array (const gchar * const *strv,
1944
                                gssize               length)
1945
0
{
1946
0
  GVariant **strings;
1947
0
  gsize i, length_unsigned;
1948
1949
0
  g_return_val_if_fail (length == 0 || strv != NULL, NULL);
1950
1951
0
  if (length < 0)
1952
0
    length = g_strv_length ((gchar **) strv);
1953
0
  length_unsigned = length;
1954
1955
0
  strings = g_new (GVariant *, length_unsigned);
1956
0
  for (i = 0; i < length_unsigned; i++)
1957
0
    strings[i] = g_variant_ref_sink (g_variant_new_bytestring (strv[i]));
1958
1959
0
  return g_variant_new_from_children (G_VARIANT_TYPE_BYTESTRING_ARRAY,
1960
0
                                      strings, length_unsigned, TRUE);
1961
0
}
1962
1963
/**
1964
 * g_variant_get_bytestring_array:
1965
 * @value: an array of array of bytes #GVariant ('aay')
1966
 * @length: (out) (optional): the length of the result, or %NULL
1967
 *
1968
 * Gets the contents of an array of array of bytes #GVariant.  This call
1969
 * makes a shallow copy; the return result should be released with
1970
 * g_free(), but the individual strings must not be modified.
1971
 *
1972
 * If @length is non-%NULL then the number of elements in the result is
1973
 * stored there.  In any case, the resulting array will be
1974
 * %NULL-terminated.
1975
 *
1976
 * For an empty array, @length will be set to 0 and a pointer to a
1977
 * %NULL pointer will be returned.
1978
 *
1979
 * Returns: (array length=length) (transfer container): an array of constant strings
1980
 *
1981
 * Since: 2.26
1982
 **/
1983
const gchar **
1984
g_variant_get_bytestring_array (GVariant *value,
1985
                                gsize    *length)
1986
0
{
1987
0
  const gchar **strv;
1988
0
  gsize n;
1989
0
  gsize i;
1990
1991
0
  TYPE_CHECK (value, G_VARIANT_TYPE_BYTESTRING_ARRAY, NULL);
1992
1993
0
  g_variant_get_data (value);
1994
0
  n = g_variant_n_children (value);
1995
0
  strv = g_new (const gchar *, n + 1);
1996
1997
0
  for (i = 0; i < n; i++)
1998
0
    {
1999
0
      GVariant *string;
2000
2001
0
      string = g_variant_get_child_value (value, i);
2002
0
      strv[i] = g_variant_get_bytestring (string);
2003
0
      g_variant_unref (string);
2004
0
    }
2005
0
  strv[i] = NULL;
2006
2007
0
  if (length)
2008
0
    *length = n;
2009
2010
0
  return strv;
2011
0
}
2012
2013
/**
2014
 * g_variant_dup_bytestring_array:
2015
 * @value: an array of array of bytes #GVariant ('aay')
2016
 * @length: (out) (optional): the length of the result, or %NULL
2017
 *
2018
 * Gets the contents of an array of array of bytes #GVariant.  This call
2019
 * makes a deep copy; the return result should be released with
2020
 * g_strfreev().
2021
 *
2022
 * If @length is non-%NULL then the number of elements in the result is
2023
 * stored there.  In any case, the resulting array will be
2024
 * %NULL-terminated.
2025
 *
2026
 * For an empty array, @length will be set to 0 and a pointer to a
2027
 * %NULL pointer will be returned.
2028
 *
2029
 * Returns: (array length=length) (transfer full): an array of strings
2030
 *
2031
 * Since: 2.26
2032
 **/
2033
gchar **
2034
g_variant_dup_bytestring_array (GVariant *value,
2035
                                gsize    *length)
2036
0
{
2037
0
  gchar **strv;
2038
0
  gsize n;
2039
0
  gsize i;
2040
2041
0
  TYPE_CHECK (value, G_VARIANT_TYPE_BYTESTRING_ARRAY, NULL);
2042
2043
0
  g_variant_get_data (value);
2044
0
  n = g_variant_n_children (value);
2045
0
  strv = g_new (gchar *, n + 1);
2046
2047
0
  for (i = 0; i < n; i++)
2048
0
    {
2049
0
      GVariant *string;
2050
2051
0
      string = g_variant_get_child_value (value, i);
2052
0
      strv[i] = g_variant_dup_bytestring (string, NULL);
2053
0
      g_variant_unref (string);
2054
0
    }
2055
0
  strv[i] = NULL;
2056
2057
0
  if (length)
2058
0
    *length = n;
2059
2060
0
  return strv;
2061
0
}
2062
2063
/* Type checking and querying {{{1 */
2064
/**
2065
 * g_variant_get_type:
2066
 * @value: a #GVariant
2067
 *
2068
 * Determines the type of @value.
2069
 *
2070
 * The return value is valid for the lifetime of @value and must not
2071
 * be freed.
2072
 *
2073
 * Returns: a #GVariantType
2074
 *
2075
 * Since: 2.24
2076
 **/
2077
const GVariantType *
2078
g_variant_get_type (GVariant *value)
2079
100
{
2080
100
  GVariantTypeInfo *type_info;
2081
2082
100
  g_return_val_if_fail (value != NULL, NULL);
2083
2084
100
  type_info = g_variant_get_type_info (value);
2085
2086
100
  return (GVariantType *) g_variant_type_info_get_type_string (type_info);
2087
100
}
2088
2089
/**
2090
 * g_variant_get_type_string:
2091
 * @value: a #GVariant
2092
 *
2093
 * Returns the type string of @value.  Unlike the result of calling
2094
 * g_variant_type_peek_string(), this string is nul-terminated.  This
2095
 * string belongs to #GVariant and must not be freed.
2096
 *
2097
 * Returns: the type string for the type of @value
2098
 *
2099
 * Since: 2.24
2100
 **/
2101
const gchar *
2102
g_variant_get_type_string (GVariant *value)
2103
0
{
2104
0
  GVariantTypeInfo *type_info;
2105
2106
0
  g_return_val_if_fail (value != NULL, NULL);
2107
2108
0
  type_info = g_variant_get_type_info (value);
2109
2110
0
  return g_variant_type_info_get_type_string (type_info);
2111
0
}
2112
2113
/**
2114
 * g_variant_is_of_type:
2115
 * @value: a #GVariant instance
2116
 * @type: a #GVariantType
2117
 *
2118
 * Checks if a value has a type matching the provided type.
2119
 *
2120
 * Returns: %TRUE if the type of @value matches @type
2121
 *
2122
 * Since: 2.24
2123
 **/
2124
gboolean
2125
g_variant_is_of_type (GVariant           *value,
2126
                      const GVariantType *type)
2127
100
{
2128
100
  return g_variant_type_is_subtype_of (g_variant_get_type (value), type);
2129
100
}
2130
2131
/**
2132
 * g_variant_is_container:
2133
 * @value: a #GVariant instance
2134
 *
2135
 * Checks if @value is a container.
2136
 *
2137
 * Returns: %TRUE if @value is a container
2138
 *
2139
 * Since: 2.24
2140
 */
2141
gboolean
2142
g_variant_is_container (GVariant *value)
2143
0
{
2144
0
  return g_variant_type_is_container (g_variant_get_type (value));
2145
0
}
2146
2147
2148
/**
2149
 * g_variant_classify:
2150
 * @value: a #GVariant
2151
 *
2152
 * Classifies @value according to its top-level type.
2153
 *
2154
 * Returns: the #GVariantClass of @value
2155
 *
2156
 * Since: 2.24
2157
 **/
2158
/**
2159
 * GVariantClass:
2160
 * @G_VARIANT_CLASS_BOOLEAN: The #GVariant is a boolean.
2161
 * @G_VARIANT_CLASS_BYTE: The #GVariant is a byte.
2162
 * @G_VARIANT_CLASS_INT16: The #GVariant is a signed 16 bit integer.
2163
 * @G_VARIANT_CLASS_UINT16: The #GVariant is an unsigned 16 bit integer.
2164
 * @G_VARIANT_CLASS_INT32: The #GVariant is a signed 32 bit integer.
2165
 * @G_VARIANT_CLASS_UINT32: The #GVariant is an unsigned 32 bit integer.
2166
 * @G_VARIANT_CLASS_INT64: The #GVariant is a signed 64 bit integer.
2167
 * @G_VARIANT_CLASS_UINT64: The #GVariant is an unsigned 64 bit integer.
2168
 * @G_VARIANT_CLASS_HANDLE: The #GVariant is a file handle index.
2169
 * @G_VARIANT_CLASS_DOUBLE: The #GVariant is a double precision floating 
2170
 *                          point value.
2171
 * @G_VARIANT_CLASS_STRING: The #GVariant is a normal string.
2172
 * @G_VARIANT_CLASS_OBJECT_PATH: The #GVariant is a D-Bus object path 
2173
 *                               string.
2174
 * @G_VARIANT_CLASS_SIGNATURE: The #GVariant is a D-Bus signature string.
2175
 * @G_VARIANT_CLASS_VARIANT: The #GVariant is a variant.
2176
 * @G_VARIANT_CLASS_MAYBE: The #GVariant is a maybe-typed value.
2177
 * @G_VARIANT_CLASS_ARRAY: The #GVariant is an array.
2178
 * @G_VARIANT_CLASS_TUPLE: The #GVariant is a tuple.
2179
 * @G_VARIANT_CLASS_DICT_ENTRY: The #GVariant is a dictionary entry.
2180
 *
2181
 * The range of possible top-level types of #GVariant instances.
2182
 *
2183
 * Since: 2.24
2184
 **/
2185
GVariantClass
2186
g_variant_classify (GVariant *value)
2187
0
{
2188
0
  g_return_val_if_fail (value != NULL, 0);
2189
2190
0
  return *g_variant_get_type_string (value);
2191
0
}
2192
2193
/* Pretty printer {{{1 */
2194
/* This function is not introspectable because if @string is NULL,
2195
   @returns is (transfer full), otherwise it is (transfer none), which
2196
   is not supported by GObjectIntrospection */
2197
/**
2198
 * g_variant_print_string: (skip)
2199
 * @value: a #GVariant
2200
 * @string: (nullable) (default NULL): a #GString, or %NULL
2201
 * @type_annotate: %TRUE if type information should be included in
2202
 *                 the output
2203
 *
2204
 * Behaves as g_variant_print(), but operates on a #GString.
2205
 *
2206
 * If @string is non-%NULL then it is appended to and returned.  Else,
2207
 * a new empty #GString is allocated and it is returned.
2208
 *
2209
 * Returns: a #GString containing the string
2210
 *
2211
 * Since: 2.24
2212
 **/
2213
GString *
2214
g_variant_print_string (GVariant *value,
2215
                        GString  *string,
2216
                        gboolean  type_annotate)
2217
0
{
2218
0
  const gchar *value_type_string = g_variant_get_type_string (value);
2219
2220
0
  if G_UNLIKELY (string == NULL)
2221
0
    string = g_string_new (NULL);
2222
2223
0
  switch (value_type_string[0])
2224
0
    {
2225
0
    case G_VARIANT_CLASS_MAYBE:
2226
0
      if (type_annotate)
2227
0
        g_string_append_printf (string, "@%s ", value_type_string);
2228
2229
0
      if (g_variant_n_children (value))
2230
0
        {
2231
0
          const GVariantType *base_type;
2232
0
          guint i, depth;
2233
0
          GVariant *element = NULL;
2234
2235
          /* Nested maybes:
2236
           *
2237
           * Consider the case of the type "mmi".  In this case we could
2238
           * write "just just 4", but "4" alone is totally unambiguous,
2239
           * so we try to drop "just" where possible.
2240
           *
2241
           * We have to be careful not to always drop "just", though,
2242
           * since "nothing" needs to be distinguishable from "just
2243
           * nothing".  The case where we need to ensure we keep the
2244
           * "just" is actually exactly the case where we have a nested
2245
           * Nothing.
2246
           *
2247
           * Search for the nested Nothing, to save a lot of recursion if there
2248
           * are multiple levels of maybes.
2249
           */
2250
0
          for (depth = 0, base_type = g_variant_get_type (value);
2251
0
               g_variant_type_is_maybe (base_type);
2252
0
               depth++, base_type = g_variant_type_element (base_type));
2253
2254
0
          element = g_variant_ref (value);
2255
0
          for (i = 0; i < depth && element != NULL; i++)
2256
0
            {
2257
0
              GVariant *new_element = g_variant_n_children (element) ? g_variant_get_child_value (element, 0) : NULL;
2258
0
              g_variant_unref (element);
2259
0
              element = g_steal_pointer (&new_element);
2260
0
            }
2261
2262
0
          if (element == NULL)
2263
0
            {
2264
              /* One of the maybes was Nothing, so print out the right number of
2265
               * justs. */
2266
0
              for (; i > 1; i--)
2267
0
                g_string_append (string, "just ");
2268
0
              g_string_append (string, "nothing");
2269
0
            }
2270
0
          else
2271
0
            {
2272
              /* There are no Nothings, so print out the child with no prefixes. */
2273
0
              g_variant_print_string (element, string, FALSE);
2274
0
            }
2275
2276
0
          g_clear_pointer (&element, g_variant_unref);
2277
0
        }
2278
0
      else
2279
0
        g_string_append (string, "nothing");
2280
2281
0
      break;
2282
2283
0
    case G_VARIANT_CLASS_ARRAY:
2284
      /* it's an array so the first character of the type string is 'a'
2285
       *
2286
       * if the first two characters are 'ay' then it's a bytestring.
2287
       * under certain conditions we print those as strings.
2288
       */
2289
0
      if (value_type_string[1] == 'y')
2290
0
        {
2291
0
          const gchar *str;
2292
0
          gsize size;
2293
0
          gsize i;
2294
2295
          /* first determine if it is a byte string.
2296
           * that's when there's a single nul character: at the end.
2297
           */
2298
0
          str = g_variant_get_data (value);
2299
0
          size = g_variant_get_size (value);
2300
2301
0
          for (i = 0; i < size; i++)
2302
0
            if (str[i] == '\0')
2303
0
              break;
2304
2305
          /* first nul byte is the last byte -> it's a byte string. */
2306
0
          if (i == size - 1)
2307
0
            {
2308
0
              gchar *escaped = g_strescape (str, NULL);
2309
2310
              /* use double quotes only if a ' is in the string */
2311
0
              if (strchr (str, '\''))
2312
0
                g_string_append_printf (string, "b\"%s\"", escaped);
2313
0
              else
2314
0
                g_string_append_printf (string, "b'%s'", escaped);
2315
2316
0
              g_free (escaped);
2317
0
              break;
2318
0
            }
2319
2320
0
          else
2321
0
            {
2322
              /* fall through and handle normally... */
2323
0
            }
2324
0
        }
2325
2326
      /*
2327
       * if the first two characters are 'a{' then it's an array of
2328
       * dictionary entries (ie: a dictionary) so we print that
2329
       * differently.
2330
       */
2331
0
      if (value_type_string[1] == '{')
2332
        /* dictionary */
2333
0
        {
2334
0
          const gchar *comma = "";
2335
0
          gsize n, i;
2336
2337
0
          if ((n = g_variant_n_children (value)) == 0)
2338
0
            {
2339
0
              if (type_annotate)
2340
0
                g_string_append_printf (string, "@%s ", value_type_string);
2341
0
              g_string_append (string, "{}");
2342
0
              break;
2343
0
            }
2344
2345
0
          g_string_append_c (string, '{');
2346
0
          for (i = 0; i < n; i++)
2347
0
            {
2348
0
              GVariant *entry, *key, *val;
2349
2350
0
              g_string_append (string, comma);
2351
0
              comma = ", ";
2352
2353
0
              entry = g_variant_get_child_value (value, i);
2354
0
              key = g_variant_get_child_value (entry, 0);
2355
0
              val = g_variant_get_child_value (entry, 1);
2356
0
              g_variant_unref (entry);
2357
2358
0
              g_variant_print_string (key, string, type_annotate);
2359
0
              g_variant_unref (key);
2360
0
              g_string_append (string, ": ");
2361
0
              g_variant_print_string (val, string, type_annotate);
2362
0
              g_variant_unref (val);
2363
0
              type_annotate = FALSE;
2364
0
            }
2365
0
          g_string_append_c (string, '}');
2366
0
        }
2367
0
      else
2368
        /* normal (non-dictionary) array */
2369
0
        {
2370
0
          const gchar *comma = "";
2371
0
          gsize n, i;
2372
2373
0
          if ((n = g_variant_n_children (value)) == 0)
2374
0
            {
2375
0
              if (type_annotate)
2376
0
                g_string_append_printf (string, "@%s ", value_type_string);
2377
0
              g_string_append (string, "[]");
2378
0
              break;
2379
0
            }
2380
2381
0
          g_string_append_c (string, '[');
2382
0
          for (i = 0; i < n; i++)
2383
0
            {
2384
0
              GVariant *element;
2385
2386
0
              g_string_append (string, comma);
2387
0
              comma = ", ";
2388
2389
0
              element = g_variant_get_child_value (value, i);
2390
2391
0
              g_variant_print_string (element, string, type_annotate);
2392
0
              g_variant_unref (element);
2393
0
              type_annotate = FALSE;
2394
0
            }
2395
0
          g_string_append_c (string, ']');
2396
0
        }
2397
2398
0
      break;
2399
2400
0
    case G_VARIANT_CLASS_TUPLE:
2401
0
      {
2402
0
        gsize n, i;
2403
2404
0
        n = g_variant_n_children (value);
2405
2406
0
        g_string_append_c (string, '(');
2407
0
        for (i = 0; i < n; i++)
2408
0
          {
2409
0
            GVariant *element;
2410
2411
0
            element = g_variant_get_child_value (value, i);
2412
0
            g_variant_print_string (element, string, type_annotate);
2413
0
            g_string_append (string, ", ");
2414
0
            g_variant_unref (element);
2415
0
          }
2416
2417
        /* for >1 item:  remove final ", "
2418
         * for 1 item:   remove final " ", but leave the ","
2419
         * for 0 items:  there is only "(", so remove nothing
2420
         */
2421
0
        g_string_truncate (string, string->len - (n > 0) - (n > 1));
2422
0
        g_string_append_c (string, ')');
2423
0
      }
2424
0
      break;
2425
2426
0
    case G_VARIANT_CLASS_DICT_ENTRY:
2427
0
      {
2428
0
        GVariant *element;
2429
2430
0
        g_string_append_c (string, '{');
2431
2432
0
        element = g_variant_get_child_value (value, 0);
2433
0
        g_variant_print_string (element, string, type_annotate);
2434
0
        g_variant_unref (element);
2435
2436
0
        g_string_append (string, ", ");
2437
2438
0
        element = g_variant_get_child_value (value, 1);
2439
0
        g_variant_print_string (element, string, type_annotate);
2440
0
        g_variant_unref (element);
2441
2442
0
        g_string_append_c (string, '}');
2443
0
      }
2444
0
      break;
2445
2446
0
    case G_VARIANT_CLASS_VARIANT:
2447
0
      {
2448
0
        GVariant *child = g_variant_get_variant (value);
2449
2450
        /* Always annotate types in nested variants, because they are
2451
         * (by nature) of variable type.
2452
         */
2453
0
        g_string_append_c (string, '<');
2454
0
        g_variant_print_string (child, string, TRUE);
2455
0
        g_string_append_c (string, '>');
2456
2457
0
        g_variant_unref (child);
2458
0
      }
2459
0
      break;
2460
2461
0
    case G_VARIANT_CLASS_BOOLEAN:
2462
0
      if (g_variant_get_boolean (value))
2463
0
        g_string_append (string, "true");
2464
0
      else
2465
0
        g_string_append (string, "false");
2466
0
      break;
2467
2468
0
    case G_VARIANT_CLASS_STRING:
2469
0
      {
2470
0
        const gchar *str = g_variant_get_string (value, NULL);
2471
0
        gunichar quote = strchr (str, '\'') ? '"' : '\'';
2472
2473
0
        g_string_append_c (string, quote);
2474
2475
0
        while (*str)
2476
0
          {
2477
0
            gunichar c = g_utf8_get_char (str);
2478
2479
0
            if (c == quote || c == '\\')
2480
0
              g_string_append_c (string, '\\');
2481
2482
0
            if (g_unichar_isprint (c))
2483
0
              g_string_append_unichar (string, c);
2484
2485
0
            else
2486
0
              {
2487
0
                g_string_append_c (string, '\\');
2488
0
                if (c < 0x10000)
2489
0
                  switch (c)
2490
0
                    {
2491
0
                    case '\a':
2492
0
                      g_string_append_c (string, 'a');
2493
0
                      break;
2494
2495
0
                    case '\b':
2496
0
                      g_string_append_c (string, 'b');
2497
0
                      break;
2498
2499
0
                    case '\f':
2500
0
                      g_string_append_c (string, 'f');
2501
0
                      break;
2502
2503
0
                    case '\n':
2504
0
                      g_string_append_c (string, 'n');
2505
0
                      break;
2506
2507
0
                    case '\r':
2508
0
                      g_string_append_c (string, 'r');
2509
0
                      break;
2510
2511
0
                    case '\t':
2512
0
                      g_string_append_c (string, 't');
2513
0
                      break;
2514
2515
0
                    case '\v':
2516
0
                      g_string_append_c (string, 'v');
2517
0
                      break;
2518
2519
0
                    default:
2520
0
                      g_string_append_printf (string, "u%04x", c);
2521
0
                      break;
2522
0
                    }
2523
0
                 else
2524
0
                   g_string_append_printf (string, "U%08x", c);
2525
0
              }
2526
2527
0
            str = g_utf8_next_char (str);
2528
0
          }
2529
2530
0
        g_string_append_c (string, quote);
2531
0
      }
2532
0
      break;
2533
2534
0
    case G_VARIANT_CLASS_BYTE:
2535
0
      if (type_annotate)
2536
0
        g_string_append (string, "byte ");
2537
0
      g_string_append_printf (string, "0x%02x",
2538
0
                              g_variant_get_byte (value));
2539
0
      break;
2540
2541
0
    case G_VARIANT_CLASS_INT16:
2542
0
      if (type_annotate)
2543
0
        g_string_append (string, "int16 ");
2544
0
      g_string_append_printf (string, "%"G_GINT16_FORMAT,
2545
0
                              g_variant_get_int16 (value));
2546
0
      break;
2547
2548
0
    case G_VARIANT_CLASS_UINT16:
2549
0
      if (type_annotate)
2550
0
        g_string_append (string, "uint16 ");
2551
0
      g_string_append_printf (string, "%"G_GUINT16_FORMAT,
2552
0
                              g_variant_get_uint16 (value));
2553
0
      break;
2554
2555
0
    case G_VARIANT_CLASS_INT32:
2556
      /* Never annotate this type because it is the default for numbers
2557
       * (and this is a *pretty* printer)
2558
       */
2559
0
      g_string_append_printf (string, "%"G_GINT32_FORMAT,
2560
0
                              g_variant_get_int32 (value));
2561
0
      break;
2562
2563
0
    case G_VARIANT_CLASS_HANDLE:
2564
0
      if (type_annotate)
2565
0
        g_string_append (string, "handle ");
2566
0
      g_string_append_printf (string, "%"G_GINT32_FORMAT,
2567
0
                              g_variant_get_handle (value));
2568
0
      break;
2569
2570
0
    case G_VARIANT_CLASS_UINT32:
2571
0
      if (type_annotate)
2572
0
        g_string_append (string, "uint32 ");
2573
0
      g_string_append_printf (string, "%"G_GUINT32_FORMAT,
2574
0
                              g_variant_get_uint32 (value));
2575
0
      break;
2576
2577
0
    case G_VARIANT_CLASS_INT64:
2578
0
      if (type_annotate)
2579
0
        g_string_append (string, "int64 ");
2580
0
      g_string_append_printf (string, "%"G_GINT64_FORMAT,
2581
0
                              g_variant_get_int64 (value));
2582
0
      break;
2583
2584
0
    case G_VARIANT_CLASS_UINT64:
2585
0
      if (type_annotate)
2586
0
        g_string_append (string, "uint64 ");
2587
0
      g_string_append_printf (string, "%"G_GUINT64_FORMAT,
2588
0
                              g_variant_get_uint64 (value));
2589
0
      break;
2590
2591
0
    case G_VARIANT_CLASS_DOUBLE:
2592
0
      {
2593
0
        gchar buffer[100];
2594
0
        gint i;
2595
2596
0
        g_ascii_dtostr (buffer, sizeof buffer, g_variant_get_double (value));
2597
2598
0
        for (i = 0; buffer[i]; i++)
2599
0
          if (buffer[i] == '.' || buffer[i] == 'e' ||
2600
0
              buffer[i] == 'n' || buffer[i] == 'N')
2601
0
            break;
2602
2603
        /* if there is no '.' or 'e' in the float then add one */
2604
0
        if (buffer[i] == '\0')
2605
0
          {
2606
0
            buffer[i++] = '.';
2607
0
            buffer[i++] = '0';
2608
0
            buffer[i++] = '\0';
2609
0
          }
2610
2611
0
        g_string_append (string, buffer);
2612
0
      }
2613
0
      break;
2614
2615
0
    case G_VARIANT_CLASS_OBJECT_PATH:
2616
0
      if (type_annotate)
2617
0
        g_string_append (string, "objectpath ");
2618
0
      g_string_append_printf (string, "\'%s\'",
2619
0
                              g_variant_get_string (value, NULL));
2620
0
      break;
2621
2622
0
    case G_VARIANT_CLASS_SIGNATURE:
2623
0
      if (type_annotate)
2624
0
        g_string_append (string, "signature ");
2625
0
      g_string_append_printf (string, "\'%s\'",
2626
0
                              g_variant_get_string (value, NULL));
2627
0
      break;
2628
2629
0
    default:
2630
0
      g_assert_not_reached ();
2631
0
  }
2632
2633
0
  return string;
2634
0
}
2635
2636
/**
2637
 * g_variant_print:
2638
 * @value: a #GVariant
2639
 * @type_annotate: %TRUE if type information should be included in
2640
 *                 the output
2641
 *
2642
 * Pretty-prints @value in the format understood by g_variant_parse().
2643
 *
2644
 * The format is described [here][gvariant-text].
2645
 *
2646
 * If @type_annotate is %TRUE, then type information is included in
2647
 * the output.
2648
 *
2649
 * Returns: (transfer full): a newly-allocated string holding the result.
2650
 *
2651
 * Since: 2.24
2652
 */
2653
gchar *
2654
g_variant_print (GVariant *value,
2655
                 gboolean  type_annotate)
2656
0
{
2657
0
  return g_string_free (g_variant_print_string (value, NULL, type_annotate),
2658
0
                        FALSE);
2659
0
}
2660
2661
/* Hash, Equal, Compare {{{1 */
2662
/**
2663
 * g_variant_hash:
2664
 * @value: (type GVariant): a basic #GVariant value as a #gconstpointer
2665
 *
2666
 * Generates a hash value for a #GVariant instance.
2667
 *
2668
 * The output of this function is guaranteed to be the same for a given
2669
 * value only per-process.  It may change between different processor
2670
 * architectures or even different versions of GLib.  Do not use this
2671
 * function as a basis for building protocols or file formats.
2672
 *
2673
 * The type of @value is #gconstpointer only to allow use of this
2674
 * function with #GHashTable.  @value must be a #GVariant.
2675
 *
2676
 * Returns: a hash value corresponding to @value
2677
 *
2678
 * Since: 2.24
2679
 **/
2680
guint
2681
g_variant_hash (gconstpointer value_)
2682
0
{
2683
0
  GVariant *value = (GVariant *) value_;
2684
2685
0
  switch (g_variant_classify (value))
2686
0
    {
2687
0
    case G_VARIANT_CLASS_STRING:
2688
0
    case G_VARIANT_CLASS_OBJECT_PATH:
2689
0
    case G_VARIANT_CLASS_SIGNATURE:
2690
0
      return g_str_hash (g_variant_get_string (value, NULL));
2691
2692
0
    case G_VARIANT_CLASS_BOOLEAN:
2693
      /* this is a very odd thing to hash... */
2694
0
      return g_variant_get_boolean (value);
2695
2696
0
    case G_VARIANT_CLASS_BYTE:
2697
0
      return g_variant_get_byte (value);
2698
2699
0
    case G_VARIANT_CLASS_INT16:
2700
0
    case G_VARIANT_CLASS_UINT16:
2701
0
      {
2702
0
        const guint16 *ptr;
2703
2704
0
        ptr = g_variant_get_data (value);
2705
2706
0
        if (ptr)
2707
0
          return *ptr;
2708
0
        else
2709
0
          return 0;
2710
0
      }
2711
2712
0
    case G_VARIANT_CLASS_INT32:
2713
0
    case G_VARIANT_CLASS_UINT32:
2714
0
    case G_VARIANT_CLASS_HANDLE:
2715
0
      {
2716
0
        const guint *ptr;
2717
2718
0
        ptr = g_variant_get_data (value);
2719
2720
0
        if (ptr)
2721
0
          return *ptr;
2722
0
        else
2723
0
          return 0;
2724
0
      }
2725
2726
0
    case G_VARIANT_CLASS_INT64:
2727
0
    case G_VARIANT_CLASS_UINT64:
2728
0
    case G_VARIANT_CLASS_DOUBLE:
2729
      /* need a separate case for these guys because otherwise
2730
       * performance could be quite bad on big endian systems
2731
       */
2732
0
      {
2733
0
        const guint *ptr;
2734
2735
0
        ptr = g_variant_get_data (value);
2736
2737
0
        if (ptr)
2738
0
          return ptr[0] + ptr[1];
2739
0
        else
2740
0
          return 0;
2741
0
      }
2742
2743
0
    default:
2744
0
      g_return_val_if_fail (!g_variant_is_container (value), 0);
2745
0
      g_assert_not_reached ();
2746
0
    }
2747
0
}
2748
2749
/**
2750
 * g_variant_equal:
2751
 * @one: (type GVariant): a #GVariant instance
2752
 * @two: (type GVariant): a #GVariant instance
2753
 *
2754
 * Checks if @one and @two have the same type and value.
2755
 *
2756
 * The types of @one and @two are #gconstpointer only to allow use of
2757
 * this function with #GHashTable.  They must each be a #GVariant.
2758
 *
2759
 * Returns: %TRUE if @one and @two are equal
2760
 *
2761
 * Since: 2.24
2762
 **/
2763
gboolean
2764
g_variant_equal (gconstpointer one,
2765
                 gconstpointer two)
2766
0
{
2767
0
  gboolean equal;
2768
2769
0
  g_return_val_if_fail (one != NULL && two != NULL, FALSE);
2770
2771
0
  if (g_variant_get_type_info ((GVariant *) one) !=
2772
0
      g_variant_get_type_info ((GVariant *) two))
2773
0
    return FALSE;
2774
2775
  /* if both values are trusted to be in their canonical serialized form
2776
   * then a simple memcmp() of their serialized data will answer the
2777
   * question.
2778
   *
2779
   * if not, then this might generate a false negative (since it is
2780
   * possible for two different byte sequences to represent the same
2781
   * value).  for now we solve this by pretty-printing both values and
2782
   * comparing the result.
2783
   */
2784
0
  if (g_variant_is_trusted ((GVariant *) one) &&
2785
0
      g_variant_is_trusted ((GVariant *) two))
2786
0
    {
2787
0
      gconstpointer data_one, data_two;
2788
0
      gsize size_one, size_two;
2789
2790
0
      size_one = g_variant_get_size ((GVariant *) one);
2791
0
      size_two = g_variant_get_size ((GVariant *) two);
2792
2793
0
      if (size_one != size_two)
2794
0
        return FALSE;
2795
2796
0
      data_one = g_variant_get_data ((GVariant *) one);
2797
0
      data_two = g_variant_get_data ((GVariant *) two);
2798
2799
0
      if (size_one)
2800
0
        equal = memcmp (data_one, data_two, size_one) == 0;
2801
0
      else
2802
0
        equal = TRUE;
2803
0
    }
2804
0
  else
2805
0
    {
2806
0
      gchar *strone, *strtwo;
2807
2808
0
      strone = g_variant_print ((GVariant *) one, FALSE);
2809
0
      strtwo = g_variant_print ((GVariant *) two, FALSE);
2810
0
      equal = strcmp (strone, strtwo) == 0;
2811
0
      g_free (strone);
2812
0
      g_free (strtwo);
2813
0
    }
2814
2815
0
  return equal;
2816
0
}
2817
2818
/**
2819
 * g_variant_compare:
2820
 * @one: (type GVariant): a basic-typed #GVariant instance
2821
 * @two: (type GVariant): a #GVariant instance of the same type
2822
 *
2823
 * Compares @one and @two.
2824
 *
2825
 * The types of @one and @two are #gconstpointer only to allow use of
2826
 * this function with #GTree, #GPtrArray, etc.  They must each be a
2827
 * #GVariant.
2828
 *
2829
 * Comparison is only defined for basic types (ie: booleans, numbers,
2830
 * strings).  For booleans, %FALSE is less than %TRUE.  Numbers are
2831
 * ordered in the usual way.  Strings are in ASCII lexographical order.
2832
 *
2833
 * It is a programmer error to attempt to compare container values or
2834
 * two values that have types that are not exactly equal.  For example,
2835
 * you cannot compare a 32-bit signed integer with a 32-bit unsigned
2836
 * integer.  Also note that this function is not particularly
2837
 * well-behaved when it comes to comparison of doubles; in particular,
2838
 * the handling of incomparable values (ie: NaN) is undefined.
2839
 *
2840
 * If you only require an equality comparison, g_variant_equal() is more
2841
 * general.
2842
 *
2843
 * Returns: negative value if a < b;
2844
 *          zero if a = b;
2845
 *          positive value if a > b.
2846
 *
2847
 * Since: 2.26
2848
 **/
2849
gint
2850
g_variant_compare (gconstpointer one,
2851
                   gconstpointer two)
2852
0
{
2853
0
  GVariant *a = (GVariant *) one;
2854
0
  GVariant *b = (GVariant *) two;
2855
2856
0
  g_return_val_if_fail (g_variant_classify (a) == g_variant_classify (b), 0);
2857
2858
0
  switch (g_variant_classify (a))
2859
0
    {
2860
0
    case G_VARIANT_CLASS_BOOLEAN:
2861
0
      return g_variant_get_boolean (a) -
2862
0
             g_variant_get_boolean (b);
2863
2864
0
    case G_VARIANT_CLASS_BYTE:
2865
0
      return ((gint) g_variant_get_byte (a)) -
2866
0
             ((gint) g_variant_get_byte (b));
2867
2868
0
    case G_VARIANT_CLASS_INT16:
2869
0
      return ((gint) g_variant_get_int16 (a)) -
2870
0
             ((gint) g_variant_get_int16 (b));
2871
2872
0
    case G_VARIANT_CLASS_UINT16:
2873
0
      return ((gint) g_variant_get_uint16 (a)) -
2874
0
             ((gint) g_variant_get_uint16 (b));
2875
2876
0
    case G_VARIANT_CLASS_INT32:
2877
0
      {
2878
0
        gint32 a_val = g_variant_get_int32 (a);
2879
0
        gint32 b_val = g_variant_get_int32 (b);
2880
2881
0
        return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1;
2882
0
      }
2883
2884
0
    case G_VARIANT_CLASS_UINT32:
2885
0
      {
2886
0
        guint32 a_val = g_variant_get_uint32 (a);
2887
0
        guint32 b_val = g_variant_get_uint32 (b);
2888
2889
0
        return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1;
2890
0
      }
2891
2892
0
    case G_VARIANT_CLASS_INT64:
2893
0
      {
2894
0
        gint64 a_val = g_variant_get_int64 (a);
2895
0
        gint64 b_val = g_variant_get_int64 (b);
2896
2897
0
        return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1;
2898
0
      }
2899
2900
0
    case G_VARIANT_CLASS_UINT64:
2901
0
      {
2902
0
        guint64 a_val = g_variant_get_uint64 (a);
2903
0
        guint64 b_val = g_variant_get_uint64 (b);
2904
2905
0
        return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1;
2906
0
      }
2907
2908
0
    case G_VARIANT_CLASS_DOUBLE:
2909
0
      {
2910
0
        gdouble a_val = g_variant_get_double (a);
2911
0
        gdouble b_val = g_variant_get_double (b);
2912
2913
0
        return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1;
2914
0
      }
2915
2916
0
    case G_VARIANT_CLASS_STRING:
2917
0
    case G_VARIANT_CLASS_OBJECT_PATH:
2918
0
    case G_VARIANT_CLASS_SIGNATURE:
2919
0
      return strcmp (g_variant_get_string (a, NULL),
2920
0
                     g_variant_get_string (b, NULL));
2921
2922
0
    default:
2923
0
      g_return_val_if_fail (!g_variant_is_container (a), 0);
2924
0
      g_assert_not_reached ();
2925
0
    }
2926
0
}
2927
2928
/* GVariantIter {{{1 */
2929
/**
2930
 * GVariantIter: (skip)
2931
 *
2932
 * #GVariantIter is an opaque data structure and can only be accessed
2933
 * using the following functions.
2934
 **/
2935
struct stack_iter
2936
{
2937
  GVariant *value;
2938
  gssize n, i;
2939
2940
  const gchar *loop_format;
2941
2942
  gsize padding[3];
2943
  gsize magic;
2944
};
2945
2946
G_STATIC_ASSERT (sizeof (struct stack_iter) <= sizeof (GVariantIter));
2947
2948
struct heap_iter
2949
{
2950
  struct stack_iter iter;
2951
2952
  GVariant *value_ref;
2953
  gsize magic;
2954
};
2955
2956
G_STATIC_ASSERT (sizeof (struct heap_iter) <= sizeof (GVariantIter));
2957
2958
0
#define GVSI(i)                 ((struct stack_iter *) (i))
2959
0
#define GVHI(i)                 ((struct heap_iter *) (i))
2960
0
#define GVSI_MAGIC              ((gsize) 3579507750u)
2961
0
#define GVHI_MAGIC              ((gsize) 1450270775u)
2962
#define is_valid_iter(i)        (i != NULL && \
2963
                                 GVSI(i)->magic == GVSI_MAGIC)
2964
#define is_valid_heap_iter(i)   (is_valid_iter(i) && \
2965
                                 GVHI(i)->magic == GVHI_MAGIC)
2966
2967
/**
2968
 * g_variant_iter_new:
2969
 * @value: a container #GVariant
2970
 *
2971
 * Creates a heap-allocated #GVariantIter for iterating over the items
2972
 * in @value.
2973
 *
2974
 * Use g_variant_iter_free() to free the return value when you no longer
2975
 * need it.
2976
 *
2977
 * A reference is taken to @value and will be released only when
2978
 * g_variant_iter_free() is called.
2979
 *
2980
 * Returns: (transfer full): a new heap-allocated #GVariantIter
2981
 *
2982
 * Since: 2.24
2983
 **/
2984
GVariantIter *
2985
g_variant_iter_new (GVariant *value)
2986
0
{
2987
0
  GVariantIter *iter;
2988
2989
0
  iter = (GVariantIter *) g_slice_new (struct heap_iter);
2990
0
  GVHI(iter)->value_ref = g_variant_ref (value);
2991
0
  GVHI(iter)->magic = GVHI_MAGIC;
2992
2993
0
  g_variant_iter_init (iter, value);
2994
2995
0
  return iter;
2996
0
}
2997
2998
/**
2999
 * g_variant_iter_init: (skip)
3000
 * @iter: a pointer to a #GVariantIter
3001
 * @value: a container #GVariant
3002
 *
3003
 * Initialises (without allocating) a #GVariantIter.  @iter may be
3004
 * completely uninitialised prior to this call; its old value is
3005
 * ignored.
3006
 *
3007
 * The iterator remains valid for as long as @value exists, and need not
3008
 * be freed in any way.
3009
 *
3010
 * Returns: the number of items in @value
3011
 *
3012
 * Since: 2.24
3013
 **/
3014
gsize
3015
g_variant_iter_init (GVariantIter *iter,
3016
                     GVariant     *value)
3017
0
{
3018
0
  GVSI(iter)->magic = GVSI_MAGIC;
3019
0
  GVSI(iter)->value = value;
3020
0
  GVSI(iter)->n = g_variant_n_children (value);
3021
0
  GVSI(iter)->i = -1;
3022
0
  GVSI(iter)->loop_format = NULL;
3023
3024
0
  return GVSI(iter)->n;
3025
0
}
3026
3027
/**
3028
 * g_variant_iter_copy:
3029
 * @iter: a #GVariantIter
3030
 *
3031
 * Creates a new heap-allocated #GVariantIter to iterate over the
3032
 * container that was being iterated over by @iter.  Iteration begins on
3033
 * the new iterator from the current position of the old iterator but
3034
 * the two copies are independent past that point.
3035
 *
3036
 * Use g_variant_iter_free() to free the return value when you no longer
3037
 * need it.
3038
 *
3039
 * A reference is taken to the container that @iter is iterating over
3040
 * and will be related only when g_variant_iter_free() is called.
3041
 *
3042
 * Returns: (transfer full): a new heap-allocated #GVariantIter
3043
 *
3044
 * Since: 2.24
3045
 **/
3046
GVariantIter *
3047
g_variant_iter_copy (GVariantIter *iter)
3048
0
{
3049
0
  GVariantIter *copy;
3050
3051
0
  g_return_val_if_fail (is_valid_iter (iter), 0);
3052
3053
0
  copy = g_variant_iter_new (GVSI(iter)->value);
3054
0
  GVSI(copy)->i = GVSI(iter)->i;
3055
3056
0
  return copy;
3057
0
}
3058
3059
/**
3060
 * g_variant_iter_n_children:
3061
 * @iter: a #GVariantIter
3062
 *
3063
 * Queries the number of child items in the container that we are
3064
 * iterating over.  This is the total number of items -- not the number
3065
 * of items remaining.
3066
 *
3067
 * This function might be useful for preallocation of arrays.
3068
 *
3069
 * Returns: the number of children in the container
3070
 *
3071
 * Since: 2.24
3072
 **/
3073
gsize
3074
g_variant_iter_n_children (GVariantIter *iter)
3075
0
{
3076
0
  g_return_val_if_fail (is_valid_iter (iter), 0);
3077
3078
0
  return GVSI(iter)->n;
3079
0
}
3080
3081
/**
3082
 * g_variant_iter_free:
3083
 * @iter: (transfer full): a heap-allocated #GVariantIter
3084
 *
3085
 * Frees a heap-allocated #GVariantIter.  Only call this function on
3086
 * iterators that were returned by g_variant_iter_new() or
3087
 * g_variant_iter_copy().
3088
 *
3089
 * Since: 2.24
3090
 **/
3091
void
3092
g_variant_iter_free (GVariantIter *iter)
3093
0
{
3094
0
  g_return_if_fail (is_valid_heap_iter (iter));
3095
3096
0
  g_variant_unref (GVHI(iter)->value_ref);
3097
0
  GVHI(iter)->magic = 0;
3098
3099
0
  g_slice_free (struct heap_iter, GVHI(iter));
3100
0
}
3101
3102
/**
3103
 * g_variant_iter_next_value:
3104
 * @iter: a #GVariantIter
3105
 *
3106
 * Gets the next item in the container.  If no more items remain then
3107
 * %NULL is returned.
3108
 *
3109
 * Use g_variant_unref() to drop your reference on the return value when
3110
 * you no longer need it.
3111
 *
3112
 * Here is an example for iterating with g_variant_iter_next_value():
3113
 * |[<!-- language="C" --> 
3114
 *   // recursively iterate a container
3115
 *   void
3116
 *   iterate_container_recursive (GVariant *container)
3117
 *   {
3118
 *     GVariantIter iter;
3119
 *     GVariant *child;
3120
 *
3121
 *     g_variant_iter_init (&iter, container);
3122
 *     while ((child = g_variant_iter_next_value (&iter)))
3123
 *       {
3124
 *         g_print ("type '%s'\n", g_variant_get_type_string (child));
3125
 *
3126
 *         if (g_variant_is_container (child))
3127
 *           iterate_container_recursive (child);
3128
 *
3129
 *         g_variant_unref (child);
3130
 *       }
3131
 *   }
3132
 * ]|
3133
 *
3134
 * Returns: (nullable) (transfer full): a #GVariant, or %NULL
3135
 *
3136
 * Since: 2.24
3137
 **/
3138
GVariant *
3139
g_variant_iter_next_value (GVariantIter *iter)
3140
0
{
3141
0
  g_return_val_if_fail (is_valid_iter (iter), FALSE);
3142
3143
0
  if G_UNLIKELY (GVSI(iter)->i >= GVSI(iter)->n)
3144
0
    {
3145
0
      g_critical ("g_variant_iter_next_value: must not be called again "
3146
0
                  "after NULL has already been returned.");
3147
0
      return NULL;
3148
0
    }
3149
3150
0
  GVSI(iter)->i++;
3151
3152
0
  if (GVSI(iter)->i < GVSI(iter)->n)
3153
0
    return g_variant_get_child_value (GVSI(iter)->value, GVSI(iter)->i);
3154
3155
0
  return NULL;
3156
0
}
3157
3158
/* GVariantBuilder {{{1 */
3159
/**
3160
 * GVariantBuilder:
3161
 *
3162
 * A utility type for constructing container-type #GVariant instances.
3163
 *
3164
 * This is an opaque structure and may only be accessed using the
3165
 * following functions.
3166
 *
3167
 * #GVariantBuilder is not threadsafe in any way.  Do not attempt to
3168
 * access it from more than one thread.
3169
 **/
3170
3171
struct stack_builder
3172
{
3173
  GVariantBuilder *parent;
3174
  GVariantType *type;
3175
3176
  /* type constraint explicitly specified by 'type'.
3177
   * for tuple types, this moves along as we add more items.
3178
   */
3179
  const GVariantType *expected_type;
3180
3181
  /* type constraint implied by previous array item.
3182
   */
3183
  const GVariantType *prev_item_type;
3184
3185
  /* constraints on the number of children.  max = -1 for unlimited. */
3186
  gsize min_items;
3187
  gsize max_items;
3188
3189
  /* dynamically-growing pointer array */
3190
  GVariant **children;
3191
  gsize allocated_children;
3192
  gsize offset;
3193
3194
  /* set to '1' if all items in the container will have the same type
3195
   * (ie: maybe, array, variant) '0' if not (ie: tuple, dict entry)
3196
   */
3197
  guint uniform_item_types : 1;
3198
3199
  /* set to '1' initially and changed to '0' if an untrusted value is
3200
   * added
3201
   */
3202
  guint trusted : 1;
3203
3204
  gsize magic;
3205
};
3206
3207
G_STATIC_ASSERT (sizeof (struct stack_builder) <= sizeof (GVariantBuilder));
3208
3209
struct heap_builder
3210
{
3211
  GVariantBuilder builder;
3212
  gsize magic;
3213
3214
  gint ref_count;
3215
};
3216
3217
0
#define GVSB(b)                  ((struct stack_builder *) (b))
3218
0
#define GVHB(b)                  ((struct heap_builder *) (b))
3219
0
#define GVSB_MAGIC               ((gsize) 1033660112u)
3220
0
#define GVSB_MAGIC_PARTIAL       ((gsize) 2942751021u)
3221
0
#define GVHB_MAGIC               ((gsize) 3087242682u)
3222
0
#define is_valid_builder(b)      (GVSB(b)->magic == GVSB_MAGIC)
3223
#define is_valid_heap_builder(b) (GVHB(b)->magic == GVHB_MAGIC)
3224
3225
/* Just to make sure that by adding a union to GVariantBuilder, we
3226
 * didn't accidentally change ABI. */
3227
G_STATIC_ASSERT (sizeof (GVariantBuilder) == sizeof (guintptr[16]));
3228
3229
static gboolean
3230
ensure_valid_builder (GVariantBuilder *builder)
3231
0
{
3232
0
  if (builder == NULL)
3233
0
    return FALSE;
3234
0
  else if (is_valid_builder (builder))
3235
0
    return TRUE;
3236
0
  if (builder->u.s.partial_magic == GVSB_MAGIC_PARTIAL)
3237
0
    {
3238
0
      static GVariantBuilder cleared_builder;
3239
3240
      /* Make sure that only first two fields were set and the rest is
3241
       * zeroed to avoid messing up the builder that had parent
3242
       * address equal to GVSB_MAGIC_PARTIAL. */
3243
0
      if (memcmp (cleared_builder.u.s.y, builder->u.s.y, sizeof cleared_builder.u.s.y))
3244
0
        return FALSE;
3245
3246
0
      g_variant_builder_init (builder, builder->u.s.type);
3247
0
    }
3248
0
  return is_valid_builder (builder);
3249
0
}
3250
3251
/* return_if_invalid_builder (b) is like
3252
 * g_return_if_fail (ensure_valid_builder (b)), except that
3253
 * the side effects of ensure_valid_builder are evaluated
3254
 * regardless of whether G_DISABLE_CHECKS is defined or not. */
3255
0
#define return_if_invalid_builder(b) G_STMT_START {                \
3256
0
  gboolean valid_builder G_GNUC_UNUSED = ensure_valid_builder (b); \
3257
0
  g_return_if_fail (valid_builder);                                \
3258
0
} G_STMT_END
3259
3260
/* return_val_if_invalid_builder (b, val) is like
3261
 * g_return_val_if_fail (ensure_valid_builder (b), val), except that
3262
 * the side effects of ensure_valid_builder are evaluated
3263
 * regardless of whether G_DISABLE_CHECKS is defined or not. */
3264
0
#define return_val_if_invalid_builder(b, val) G_STMT_START {       \
3265
0
  gboolean valid_builder G_GNUC_UNUSED = ensure_valid_builder (b); \
3266
0
  g_return_val_if_fail (valid_builder, val);                       \
3267
0
} G_STMT_END
3268
3269
/**
3270
 * g_variant_builder_new:
3271
 * @type: a container type
3272
 *
3273
 * Allocates and initialises a new #GVariantBuilder.
3274
 *
3275
 * You should call g_variant_builder_unref() on the return value when it
3276
 * is no longer needed.  The memory will not be automatically freed by
3277
 * any other call.
3278
 *
3279
 * In most cases it is easier to place a #GVariantBuilder directly on
3280
 * the stack of the calling function and initialise it with
3281
 * g_variant_builder_init().
3282
 *
3283
 * Returns: (transfer full): a #GVariantBuilder
3284
 *
3285
 * Since: 2.24
3286
 **/
3287
GVariantBuilder *
3288
g_variant_builder_new (const GVariantType *type)
3289
0
{
3290
0
  GVariantBuilder *builder;
3291
3292
0
  builder = (GVariantBuilder *) g_slice_new (struct heap_builder);
3293
0
  g_variant_builder_init (builder, type);
3294
0
  GVHB(builder)->magic = GVHB_MAGIC;
3295
0
  GVHB(builder)->ref_count = 1;
3296
3297
0
  return builder;
3298
0
}
3299
3300
/**
3301
 * g_variant_builder_unref:
3302
 * @builder: (transfer full): a #GVariantBuilder allocated by g_variant_builder_new()
3303
 *
3304
 * Decreases the reference count on @builder.
3305
 *
3306
 * In the event that there are no more references, releases all memory
3307
 * associated with the #GVariantBuilder.
3308
 *
3309
 * Don't call this on stack-allocated #GVariantBuilder instances or bad
3310
 * things will happen.
3311
 *
3312
 * Since: 2.24
3313
 **/
3314
void
3315
g_variant_builder_unref (GVariantBuilder *builder)
3316
0
{
3317
0
  g_return_if_fail (is_valid_heap_builder (builder));
3318
3319
0
  if (--GVHB(builder)->ref_count)
3320
0
    return;
3321
3322
0
  g_variant_builder_clear (builder);
3323
0
  GVHB(builder)->magic = 0;
3324
3325
0
  g_slice_free (struct heap_builder, GVHB(builder));
3326
0
}
3327
3328
/**
3329
 * g_variant_builder_ref:
3330
 * @builder: a #GVariantBuilder allocated by g_variant_builder_new()
3331
 *
3332
 * Increases the reference count on @builder.
3333
 *
3334
 * Don't call this on stack-allocated #GVariantBuilder instances or bad
3335
 * things will happen.
3336
 *
3337
 * Returns: (transfer full): a new reference to @builder
3338
 *
3339
 * Since: 2.24
3340
 **/
3341
GVariantBuilder *
3342
g_variant_builder_ref (GVariantBuilder *builder)
3343
0
{
3344
0
  g_return_val_if_fail (is_valid_heap_builder (builder), NULL);
3345
3346
0
  GVHB(builder)->ref_count++;
3347
3348
0
  return builder;
3349
0
}
3350
3351
/**
3352
 * g_variant_builder_clear: (skip)
3353
 * @builder: a #GVariantBuilder
3354
 *
3355
 * Releases all memory associated with a #GVariantBuilder without
3356
 * freeing the #GVariantBuilder structure itself.
3357
 *
3358
 * It typically only makes sense to do this on a stack-allocated
3359
 * #GVariantBuilder if you want to abort building the value part-way
3360
 * through.  This function need not be called if you call
3361
 * g_variant_builder_end() and it also doesn't need to be called on
3362
 * builders allocated with g_variant_builder_new() (see
3363
 * g_variant_builder_unref() for that).
3364
 *
3365
 * This function leaves the #GVariantBuilder structure set to all-zeros.
3366
 * It is valid to call this function on either an initialised
3367
 * #GVariantBuilder or one that is set to all-zeros but it is not valid
3368
 * to call this function on uninitialised memory.
3369
 *
3370
 * Since: 2.24
3371
 **/
3372
void
3373
g_variant_builder_clear (GVariantBuilder *builder)
3374
0
{
3375
0
  gsize i;
3376
3377
0
  if (GVSB(builder)->magic == 0)
3378
    /* all-zeros or partial case */
3379
0
    return;
3380
3381
0
  return_if_invalid_builder (builder);
3382
3383
0
  g_variant_type_free (GVSB(builder)->type);
3384
3385
0
  for (i = 0; i < GVSB(builder)->offset; i++)
3386
0
    g_variant_unref (GVSB(builder)->children[i]);
3387
3388
0
  g_free (GVSB(builder)->children);
3389
3390
0
  if (GVSB(builder)->parent)
3391
0
    {
3392
0
      g_variant_builder_clear (GVSB(builder)->parent);
3393
0
      g_slice_free (GVariantBuilder, GVSB(builder)->parent);
3394
0
    }
3395
3396
0
  memset (builder, 0, sizeof (GVariantBuilder));
3397
0
}
3398
3399
/**
3400
 * g_variant_builder_init: (skip)
3401
 * @builder: a #GVariantBuilder
3402
 * @type: a container type
3403
 *
3404
 * Initialises a #GVariantBuilder structure.
3405
 *
3406
 * @type must be non-%NULL.  It specifies the type of container to
3407
 * construct.  It can be an indefinite type such as
3408
 * %G_VARIANT_TYPE_ARRAY or a definite type such as "as" or "(ii)".
3409
 * Maybe, array, tuple, dictionary entry and variant-typed values may be
3410
 * constructed.
3411
 *
3412
 * After the builder is initialised, values are added using
3413
 * g_variant_builder_add_value() or g_variant_builder_add().
3414
 *
3415
 * After all the child values are added, g_variant_builder_end() frees
3416
 * the memory associated with the builder and returns the #GVariant that
3417
 * was created.
3418
 *
3419
 * This function completely ignores the previous contents of @builder.
3420
 * On one hand this means that it is valid to pass in completely
3421
 * uninitialised memory.  On the other hand, this means that if you are
3422
 * initialising over top of an existing #GVariantBuilder you need to
3423
 * first call g_variant_builder_clear() in order to avoid leaking
3424
 * memory.
3425
 *
3426
 * You must not call g_variant_builder_ref() or
3427
 * g_variant_builder_unref() on a #GVariantBuilder that was initialised
3428
 * with this function.  If you ever pass a reference to a
3429
 * #GVariantBuilder outside of the control of your own code then you
3430
 * should assume that the person receiving that reference may try to use
3431
 * reference counting; you should use g_variant_builder_new() instead of
3432
 * this function.
3433
 *
3434
 * Since: 2.24
3435
 **/
3436
void
3437
g_variant_builder_init (GVariantBuilder    *builder,
3438
                        const GVariantType *type)
3439
0
{
3440
0
  g_return_if_fail (type != NULL);
3441
0
  g_return_if_fail (g_variant_type_is_container (type));
3442
3443
0
  memset (builder, 0, sizeof (GVariantBuilder));
3444
3445
0
  GVSB(builder)->type = g_variant_type_copy (type);
3446
0
  GVSB(builder)->magic = GVSB_MAGIC;
3447
0
  GVSB(builder)->trusted = TRUE;
3448
3449
0
  switch (*(const gchar *) type)
3450
0
    {
3451
0
    case G_VARIANT_CLASS_VARIANT:
3452
0
      GVSB(builder)->uniform_item_types = TRUE;
3453
0
      GVSB(builder)->allocated_children = 1;
3454
0
      GVSB(builder)->expected_type = NULL;
3455
0
      GVSB(builder)->min_items = 1;
3456
0
      GVSB(builder)->max_items = 1;
3457
0
      break;
3458
3459
0
    case G_VARIANT_CLASS_ARRAY:
3460
0
      GVSB(builder)->uniform_item_types = TRUE;
3461
0
      GVSB(builder)->allocated_children = 8;
3462
0
      GVSB(builder)->expected_type =
3463
0
        g_variant_type_element (GVSB(builder)->type);
3464
0
      GVSB(builder)->min_items = 0;
3465
0
      GVSB(builder)->max_items = -1;
3466
0
      break;
3467
3468
0
    case G_VARIANT_CLASS_MAYBE:
3469
0
      GVSB(builder)->uniform_item_types = TRUE;
3470
0
      GVSB(builder)->allocated_children = 1;
3471
0
      GVSB(builder)->expected_type =
3472
0
        g_variant_type_element (GVSB(builder)->type);
3473
0
      GVSB(builder)->min_items = 0;
3474
0
      GVSB(builder)->max_items = 1;
3475
0
      break;
3476
3477
0
    case G_VARIANT_CLASS_DICT_ENTRY:
3478
0
      GVSB(builder)->uniform_item_types = FALSE;
3479
0
      GVSB(builder)->allocated_children = 2;
3480
0
      GVSB(builder)->expected_type =
3481
0
        g_variant_type_key (GVSB(builder)->type);
3482
0
      GVSB(builder)->min_items = 2;
3483
0
      GVSB(builder)->max_items = 2;
3484
0
      break;
3485
3486
0
    case 'r': /* G_VARIANT_TYPE_TUPLE was given */
3487
0
      GVSB(builder)->uniform_item_types = FALSE;
3488
0
      GVSB(builder)->allocated_children = 8;
3489
0
      GVSB(builder)->expected_type = NULL;
3490
0
      GVSB(builder)->min_items = 0;
3491
0
      GVSB(builder)->max_items = -1;
3492
0
      break;
3493
3494
0
    case G_VARIANT_CLASS_TUPLE: /* a definite tuple type was given */
3495
0
      GVSB(builder)->allocated_children = g_variant_type_n_items (type);
3496
0
      GVSB(builder)->expected_type =
3497
0
        g_variant_type_first (GVSB(builder)->type);
3498
0
      GVSB(builder)->min_items = GVSB(builder)->allocated_children;
3499
0
      GVSB(builder)->max_items = GVSB(builder)->allocated_children;
3500
0
      GVSB(builder)->uniform_item_types = FALSE;
3501
0
      break;
3502
3503
0
    default:
3504
0
      g_assert_not_reached ();
3505
0
   }
3506
3507
0
#ifdef G_ANALYZER_ANALYZING
3508
  /* Static analysers can’t couple the code in g_variant_builder_init() to the
3509
   * code in g_variant_builder_end() by GVariantType, so end up assuming that
3510
   * @offset and @children mismatch and that uninitialised memory is accessed
3511
   * from @children. At runtime, this is caught by the preconditions at the top
3512
   * of g_variant_builder_end(). Help the analyser by zero-initialising the
3513
   * memory to avoid a false positive. */
3514
0
  GVSB(builder)->children = g_new0 (GVariant *,
3515
0
                                    GVSB(builder)->allocated_children);
3516
#else
3517
  GVSB(builder)->children = g_new (GVariant *,
3518
                                   GVSB(builder)->allocated_children);
3519
#endif
3520
0
}
3521
3522
static void
3523
g_variant_builder_make_room (struct stack_builder *builder)
3524
0
{
3525
0
  if (builder->offset == builder->allocated_children)
3526
0
    {
3527
0
      builder->allocated_children *= 2;
3528
0
      builder->children = g_renew (GVariant *, builder->children,
3529
0
                                   builder->allocated_children);
3530
0
    }
3531
0
}
3532
3533
/**
3534
 * g_variant_builder_add_value:
3535
 * @builder: a #GVariantBuilder
3536
 * @value: a #GVariant
3537
 *
3538
 * Adds @value to @builder.
3539
 *
3540
 * It is an error to call this function in any way that would create an
3541
 * inconsistent value to be constructed.  Some examples of this are
3542
 * putting different types of items into an array, putting the wrong
3543
 * types or number of items in a tuple, putting more than one value into
3544
 * a variant, etc.
3545
 *
3546
 * If @value is a floating reference (see g_variant_ref_sink()),
3547
 * the @builder instance takes ownership of @value.
3548
 *
3549
 * Since: 2.24
3550
 **/
3551
void
3552
g_variant_builder_add_value (GVariantBuilder *builder,
3553
                             GVariant        *value)
3554
0
{
3555
0
  return_if_invalid_builder (builder);
3556
0
  g_return_if_fail (GVSB(builder)->offset < GVSB(builder)->max_items);
3557
0
  g_return_if_fail (!GVSB(builder)->expected_type ||
3558
0
                    g_variant_is_of_type (value,
3559
0
                                          GVSB(builder)->expected_type));
3560
0
  g_return_if_fail (!GVSB(builder)->prev_item_type ||
3561
0
                    g_variant_is_of_type (value,
3562
0
                                          GVSB(builder)->prev_item_type));
3563
3564
0
  GVSB(builder)->trusted &= g_variant_is_trusted (value);
3565
3566
0
  if (!GVSB(builder)->uniform_item_types)
3567
0
    {
3568
      /* advance our expected type pointers */
3569
0
      if (GVSB(builder)->expected_type)
3570
0
        GVSB(builder)->expected_type =
3571
0
          g_variant_type_next (GVSB(builder)->expected_type);
3572
3573
0
      if (GVSB(builder)->prev_item_type)
3574
0
        GVSB(builder)->prev_item_type =
3575
0
          g_variant_type_next (GVSB(builder)->prev_item_type);
3576
0
    }
3577
0
  else
3578
0
    GVSB(builder)->prev_item_type = g_variant_get_type (value);
3579
3580
0
  g_variant_builder_make_room (GVSB(builder));
3581
3582
0
  GVSB(builder)->children[GVSB(builder)->offset++] =
3583
0
    g_variant_ref_sink (value);
3584
0
}
3585
3586
/**
3587
 * g_variant_builder_open:
3588
 * @builder: a #GVariantBuilder
3589
 * @type: the #GVariantType of the container
3590
 *
3591
 * Opens a subcontainer inside the given @builder.  When done adding
3592
 * items to the subcontainer, g_variant_builder_close() must be called. @type
3593
 * is the type of the container: so to build a tuple of several values, @type
3594
 * must include the tuple itself.
3595
 *
3596
 * It is an error to call this function in any way that would cause an
3597
 * inconsistent value to be constructed (ie: adding too many values or
3598
 * a value of an incorrect type).
3599
 *
3600
 * Example of building a nested variant:
3601
 * |[<!-- language="C" -->
3602
 * GVariantBuilder builder;
3603
 * guint32 some_number = get_number ();
3604
 * g_autoptr (GHashTable) some_dict = get_dict ();
3605
 * GHashTableIter iter;
3606
 * const gchar *key;
3607
 * const GVariant *value;
3608
 * g_autoptr (GVariant) output = NULL;
3609
 *
3610
 * g_variant_builder_init (&builder, G_VARIANT_TYPE ("(ua{sv})"));
3611
 * g_variant_builder_add (&builder, "u", some_number);
3612
 * g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{sv}"));
3613
 *
3614
 * g_hash_table_iter_init (&iter, some_dict);
3615
 * while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value))
3616
 *   {
3617
 *     g_variant_builder_open (&builder, G_VARIANT_TYPE ("{sv}"));
3618
 *     g_variant_builder_add (&builder, "s", key);
3619
 *     g_variant_builder_add (&builder, "v", value);
3620
 *     g_variant_builder_close (&builder);
3621
 *   }
3622
 *
3623
 * g_variant_builder_close (&builder);
3624
 *
3625
 * output = g_variant_builder_end (&builder);
3626
 * ]|
3627
 *
3628
 * Since: 2.24
3629
 **/
3630
void
3631
g_variant_builder_open (GVariantBuilder    *builder,
3632
                        const GVariantType *type)
3633
0
{
3634
0
  GVariantBuilder *parent;
3635
3636
0
  return_if_invalid_builder (builder);
3637
0
  g_return_if_fail (GVSB(builder)->offset < GVSB(builder)->max_items);
3638
0
  g_return_if_fail (!GVSB(builder)->expected_type ||
3639
0
                    g_variant_type_is_subtype_of (type,
3640
0
                                                  GVSB(builder)->expected_type));
3641
0
  g_return_if_fail (!GVSB(builder)->prev_item_type ||
3642
0
                    g_variant_type_is_subtype_of (GVSB(builder)->prev_item_type,
3643
0
                                                  type));
3644
3645
0
  parent = g_slice_dup (GVariantBuilder, builder);
3646
0
  g_variant_builder_init (builder, type);
3647
0
  GVSB(builder)->parent = parent;
3648
3649
  /* push the prev_item_type down into the subcontainer */
3650
0
  if (GVSB(parent)->prev_item_type)
3651
0
    {
3652
0
      if (!GVSB(builder)->uniform_item_types)
3653
        /* tuples and dict entries */
3654
0
        GVSB(builder)->prev_item_type =
3655
0
          g_variant_type_first (GVSB(parent)->prev_item_type);
3656
3657
0
      else if (!g_variant_type_is_variant (GVSB(builder)->type))
3658
        /* maybes and arrays */
3659
0
        GVSB(builder)->prev_item_type =
3660
0
          g_variant_type_element (GVSB(parent)->prev_item_type);
3661
0
    }
3662
0
}
3663
3664
/**
3665
 * g_variant_builder_close:
3666
 * @builder: a #GVariantBuilder
3667
 *
3668
 * Closes the subcontainer inside the given @builder that was opened by
3669
 * the most recent call to g_variant_builder_open().
3670
 *
3671
 * It is an error to call this function in any way that would create an
3672
 * inconsistent value to be constructed (ie: too few values added to the
3673
 * subcontainer).
3674
 *
3675
 * Since: 2.24
3676
 **/
3677
void
3678
g_variant_builder_close (GVariantBuilder *builder)
3679
0
{
3680
0
  GVariantBuilder *parent;
3681
3682
0
  return_if_invalid_builder (builder);
3683
0
  g_return_if_fail (GVSB(builder)->parent != NULL);
3684
3685
0
  parent = GVSB(builder)->parent;
3686
0
  GVSB(builder)->parent = NULL;
3687
3688
0
  g_variant_builder_add_value (parent, g_variant_builder_end (builder));
3689
0
  *builder = *parent;
3690
3691
0
  g_slice_free (GVariantBuilder, parent);
3692
0
}
3693
3694
/*< private >
3695
 * g_variant_make_maybe_type:
3696
 * @element: a #GVariant
3697
 *
3698
 * Return the type of a maybe containing @element.
3699
 */
3700
static GVariantType *
3701
g_variant_make_maybe_type (GVariant *element)
3702
0
{
3703
0
  return g_variant_type_new_maybe (g_variant_get_type (element));
3704
0
}
3705
3706
/*< private >
3707
 * g_variant_make_array_type:
3708
 * @element: a #GVariant
3709
 *
3710
 * Return the type of an array containing @element.
3711
 */
3712
static GVariantType *
3713
g_variant_make_array_type (GVariant *element)
3714
0
{
3715
0
  return g_variant_type_new_array (g_variant_get_type (element));
3716
0
}
3717
3718
/**
3719
 * g_variant_builder_end:
3720
 * @builder: a #GVariantBuilder
3721
 *
3722
 * Ends the builder process and returns the constructed value.
3723
 *
3724
 * It is not permissible to use @builder in any way after this call
3725
 * except for reference counting operations (in the case of a
3726
 * heap-allocated #GVariantBuilder) or by reinitialising it with
3727
 * g_variant_builder_init() (in the case of stack-allocated). This
3728
 * means that for the stack-allocated builders there is no need to
3729
 * call g_variant_builder_clear() after the call to
3730
 * g_variant_builder_end().
3731
 *
3732
 * It is an error to call this function in any way that would create an
3733
 * inconsistent value to be constructed (ie: insufficient number of
3734
 * items added to a container with a specific number of children
3735
 * required).  It is also an error to call this function if the builder
3736
 * was created with an indefinite array or maybe type and no children
3737
 * have been added; in this case it is impossible to infer the type of
3738
 * the empty array.
3739
 *
3740
 * Returns: (transfer none): a new, floating, #GVariant
3741
 *
3742
 * Since: 2.24
3743
 **/
3744
GVariant *
3745
g_variant_builder_end (GVariantBuilder *builder)
3746
0
{
3747
0
  GVariantType *my_type;
3748
0
  GVariant *value;
3749
3750
0
  return_val_if_invalid_builder (builder, NULL);
3751
0
  g_return_val_if_fail (GVSB(builder)->offset >= GVSB(builder)->min_items,
3752
0
                        NULL);
3753
0
  g_return_val_if_fail (!GVSB(builder)->uniform_item_types ||
3754
0
                        GVSB(builder)->prev_item_type != NULL ||
3755
0
                        g_variant_type_is_definite (GVSB(builder)->type),
3756
0
                        NULL);
3757
3758
0
  if (g_variant_type_is_definite (GVSB(builder)->type))
3759
0
    my_type = g_variant_type_copy (GVSB(builder)->type);
3760
3761
0
  else if (g_variant_type_is_maybe (GVSB(builder)->type))
3762
0
    my_type = g_variant_make_maybe_type (GVSB(builder)->children[0]);
3763
3764
0
  else if (g_variant_type_is_array (GVSB(builder)->type))
3765
0
    my_type = g_variant_make_array_type (GVSB(builder)->children[0]);
3766
3767
0
  else if (g_variant_type_is_tuple (GVSB(builder)->type))
3768
0
    my_type = g_variant_make_tuple_type (GVSB(builder)->children,
3769
0
                                         GVSB(builder)->offset);
3770
3771
0
  else if (g_variant_type_is_dict_entry (GVSB(builder)->type))
3772
0
    my_type = g_variant_make_dict_entry_type (GVSB(builder)->children[0],
3773
0
                                              GVSB(builder)->children[1]);
3774
0
  else
3775
0
    g_assert_not_reached ();
3776
3777
0
  value = g_variant_new_from_children (my_type,
3778
0
                                       g_renew (GVariant *,
3779
0
                                                GVSB(builder)->children,
3780
0
                                                GVSB(builder)->offset),
3781
0
                                       GVSB(builder)->offset,
3782
0
                                       GVSB(builder)->trusted);
3783
0
  GVSB(builder)->children = NULL;
3784
0
  GVSB(builder)->offset = 0;
3785
3786
0
  g_variant_builder_clear (builder);
3787
0
  g_variant_type_free (my_type);
3788
3789
0
  return value;
3790
0
}
3791
3792
/* GVariantDict {{{1 */
3793
3794
/**
3795
 * GVariantDict:
3796
 *
3797
 * #GVariantDict is a mutable interface to #GVariant dictionaries.
3798
 *
3799
 * It can be used for doing a sequence of dictionary lookups in an
3800
 * efficient way on an existing #GVariant dictionary or it can be used
3801
 * to construct new dictionaries with a hashtable-like interface.  It
3802
 * can also be used for taking existing dictionaries and modifying them
3803
 * in order to create new ones.
3804
 *
3805
 * #GVariantDict can only be used with %G_VARIANT_TYPE_VARDICT
3806
 * dictionaries.
3807
 *
3808
 * It is possible to use #GVariantDict allocated on the stack or on the
3809
 * heap.  When using a stack-allocated #GVariantDict, you begin with a
3810
 * call to g_variant_dict_init() and free the resources with a call to
3811
 * g_variant_dict_clear().
3812
 *
3813
 * Heap-allocated #GVariantDict follows normal refcounting rules: you
3814
 * allocate it with g_variant_dict_new() and use g_variant_dict_ref()
3815
 * and g_variant_dict_unref().
3816
 *
3817
 * g_variant_dict_end() is used to convert the #GVariantDict back into a
3818
 * dictionary-type #GVariant.  When used with stack-allocated instances,
3819
 * this also implicitly frees all associated memory, but for
3820
 * heap-allocated instances, you must still call g_variant_dict_unref()
3821
 * afterwards.
3822
 *
3823
 * You will typically want to use a heap-allocated #GVariantDict when
3824
 * you expose it as part of an API.  For most other uses, the
3825
 * stack-allocated form will be more convenient.
3826
 *
3827
 * Consider the following two examples that do the same thing in each
3828
 * style: take an existing dictionary and look up the "count" uint32
3829
 * key, adding 1 to it if it is found, or returning an error if the
3830
 * key is not found.  Each returns the new dictionary as a floating
3831
 * #GVariant.
3832
 *
3833
 * ## Using a stack-allocated GVariantDict
3834
 *
3835
 * |[<!-- language="C" -->
3836
 *   GVariant *
3837
 *   add_to_count (GVariant  *orig,
3838
 *                 GError   **error)
3839
 *   {
3840
 *     GVariantDict dict;
3841
 *     guint32 count;
3842
 *
3843
 *     g_variant_dict_init (&dict, orig);
3844
 *     if (!g_variant_dict_lookup (&dict, "count", "u", &count))
3845
 *       {
3846
 *         g_set_error (...);
3847
 *         g_variant_dict_clear (&dict);
3848
 *         return NULL;
3849
 *       }
3850
 *
3851
 *     g_variant_dict_insert (&dict, "count", "u", count + 1);
3852
 *
3853
 *     return g_variant_dict_end (&dict);
3854
 *   }
3855
 * ]|
3856
 *
3857
 * ## Using heap-allocated GVariantDict
3858
 *
3859
 * |[<!-- language="C" -->
3860
 *   GVariant *
3861
 *   add_to_count (GVariant  *orig,
3862
 *                 GError   **error)
3863
 *   {
3864
 *     GVariantDict *dict;
3865
 *     GVariant *result;
3866
 *     guint32 count;
3867
 *
3868
 *     dict = g_variant_dict_new (orig);
3869
 *
3870
 *     if (g_variant_dict_lookup (dict, "count", "u", &count))
3871
 *       {
3872
 *         g_variant_dict_insert (dict, "count", "u", count + 1);
3873
 *         result = g_variant_dict_end (dict);
3874
 *       }
3875
 *     else
3876
 *       {
3877
 *         g_set_error (...);
3878
 *         result = NULL;
3879
 *       }
3880
 *
3881
 *     g_variant_dict_unref (dict);
3882
 *
3883
 *     return result;
3884
 *   }
3885
 * ]|
3886
 *
3887
 * Since: 2.40
3888
 **/
3889
struct stack_dict
3890
{
3891
  GHashTable *values;
3892
  gsize magic;
3893
};
3894
3895
G_STATIC_ASSERT (sizeof (struct stack_dict) <= sizeof (GVariantDict));
3896
3897
struct heap_dict
3898
{
3899
  struct stack_dict dict;
3900
  gint ref_count;
3901
  gsize magic;
3902
};
3903
3904
0
#define GVSD(d)                 ((struct stack_dict *) (d))
3905
0
#define GVHD(d)                 ((struct heap_dict *) (d))
3906
0
#define GVSD_MAGIC              ((gsize) 2579507750u)
3907
0
#define GVSD_MAGIC_PARTIAL      ((gsize) 3488698669u)
3908
0
#define GVHD_MAGIC              ((gsize) 2450270775u)
3909
0
#define is_valid_dict(d)        (GVSD(d)->magic == GVSD_MAGIC)
3910
#define is_valid_heap_dict(d)   (GVHD(d)->magic == GVHD_MAGIC)
3911
3912
/* Just to make sure that by adding a union to GVariantDict, we didn't
3913
 * accidentally change ABI. */
3914
G_STATIC_ASSERT (sizeof (GVariantDict) == sizeof (guintptr[16]));
3915
3916
static gboolean
3917
ensure_valid_dict (GVariantDict *dict)
3918
0
{
3919
0
  if (dict == NULL)
3920
0
    return FALSE;
3921
0
  else if (is_valid_dict (dict))
3922
0
    return TRUE;
3923
0
  if (dict->u.s.partial_magic == GVSD_MAGIC_PARTIAL)
3924
0
    {
3925
0
      static GVariantDict cleared_dict;
3926
3927
      /* Make sure that only first two fields were set and the rest is
3928
       * zeroed to avoid messing up the builder that had parent
3929
       * address equal to GVSB_MAGIC_PARTIAL. */
3930
0
      if (memcmp (cleared_dict.u.s.y, dict->u.s.y, sizeof cleared_dict.u.s.y))
3931
0
        return FALSE;
3932
3933
0
      g_variant_dict_init (dict, dict->u.s.asv);
3934
0
    }
3935
0
  return is_valid_dict (dict);
3936
0
}
3937
3938
/* return_if_invalid_dict (d) is like
3939
 * g_return_if_fail (ensure_valid_dict (d)), except that
3940
 * the side effects of ensure_valid_dict are evaluated
3941
 * regardless of whether G_DISABLE_CHECKS is defined or not. */
3942
0
#define return_if_invalid_dict(d) G_STMT_START {                \
3943
0
  gboolean valid_dict G_GNUC_UNUSED = ensure_valid_dict (d);    \
3944
0
  g_return_if_fail (valid_dict);                                \
3945
0
} G_STMT_END
3946
3947
/* return_val_if_invalid_dict (d, val) is like
3948
 * g_return_val_if_fail (ensure_valid_dict (d), val), except that
3949
 * the side effects of ensure_valid_dict are evaluated
3950
 * regardless of whether G_DISABLE_CHECKS is defined or not. */
3951
0
#define return_val_if_invalid_dict(d, val) G_STMT_START {       \
3952
0
  gboolean valid_dict G_GNUC_UNUSED = ensure_valid_dict (d);    \
3953
0
  g_return_val_if_fail (valid_dict, val);                       \
3954
0
} G_STMT_END
3955
3956
/**
3957
 * g_variant_dict_new:
3958
 * @from_asv: (nullable): the #GVariant with which to initialise the
3959
 *   dictionary
3960
 *
3961
 * Allocates and initialises a new #GVariantDict.
3962
 *
3963
 * You should call g_variant_dict_unref() on the return value when it
3964
 * is no longer needed.  The memory will not be automatically freed by
3965
 * any other call.
3966
 *
3967
 * In some cases it may be easier to place a #GVariantDict directly on
3968
 * the stack of the calling function and initialise it with
3969
 * g_variant_dict_init().  This is particularly useful when you are
3970
 * using #GVariantDict to construct a #GVariant.
3971
 *
3972
 * Returns: (transfer full): a #GVariantDict
3973
 *
3974
 * Since: 2.40
3975
 **/
3976
GVariantDict *
3977
g_variant_dict_new (GVariant *from_asv)
3978
0
{
3979
0
  GVariantDict *dict;
3980
3981
  /* We actually want to treat the allocation as a `struct heap_dict`, but the
3982
   * compiler will warn if it’s not at least as big as `struct GVariantDict`. */
3983
0
  G_STATIC_ASSERT (sizeof (GVariantDict) >= sizeof (struct heap_dict));
3984
3985
0
  dict = g_malloc (sizeof (GVariantDict));
3986
0
  g_variant_dict_init (dict, from_asv);
3987
0
  GVHD(dict)->magic = GVHD_MAGIC;
3988
0
  GVHD(dict)->ref_count = 1;
3989
3990
0
  return dict;
3991
0
}
3992
3993
/**
3994
 * g_variant_dict_init: (skip)
3995
 * @dict: a #GVariantDict
3996
 * @from_asv: (nullable): the initial value for @dict
3997
 *
3998
 * Initialises a #GVariantDict structure.
3999
 *
4000
 * If @from_asv is given, it is used to initialise the dictionary.
4001
 *
4002
 * This function completely ignores the previous contents of @dict.  On
4003
 * one hand this means that it is valid to pass in completely
4004
 * uninitialised memory.  On the other hand, this means that if you are
4005
 * initialising over top of an existing #GVariantDict you need to first
4006
 * call g_variant_dict_clear() in order to avoid leaking memory.
4007
 *
4008
 * You must not call g_variant_dict_ref() or g_variant_dict_unref() on a
4009
 * #GVariantDict that was initialised with this function.  If you ever
4010
 * pass a reference to a #GVariantDict outside of the control of your
4011
 * own code then you should assume that the person receiving that
4012
 * reference may try to use reference counting; you should use
4013
 * g_variant_dict_new() instead of this function.
4014
 *
4015
 * Since: 2.40
4016
 **/
4017
void
4018
g_variant_dict_init (GVariantDict *dict,
4019
                     GVariant     *from_asv)
4020
0
{
4021
0
  GVariantIter iter;
4022
0
  gchar *key;
4023
0
  GVariant *value;
4024
4025
0
  GVSD(dict)->values = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_variant_unref);
4026
0
  GVSD(dict)->magic = GVSD_MAGIC;
4027
4028
0
  if (from_asv)
4029
0
    {
4030
0
      g_variant_iter_init (&iter, from_asv);
4031
0
      while (g_variant_iter_next (&iter, "{sv}", &key, &value))
4032
0
        g_hash_table_insert (GVSD(dict)->values, key, value);
4033
0
    }
4034
0
}
4035
4036
/**
4037
 * g_variant_dict_lookup:
4038
 * @dict: a #GVariantDict
4039
 * @key: the key to look up in the dictionary
4040
 * @format_string: a GVariant format string
4041
 * @...: the arguments to unpack the value into
4042
 *
4043
 * Looks up a value in a #GVariantDict.
4044
 *
4045
 * This function is a wrapper around g_variant_dict_lookup_value() and
4046
 * g_variant_get().  In the case that %NULL would have been returned,
4047
 * this function returns %FALSE and does not modify the values of the arguments
4048
 * passed in to @....  Otherwise, it unpacks the returned
4049
 * value and returns %TRUE.
4050
 *
4051
 * @format_string determines the C types that are used for unpacking the
4052
 * values and also determines if the values are copied or borrowed, see the
4053
 * section on [`GVariant` format strings](gvariant-format-strings.html#pointers).
4054
 *
4055
 * Returns: %TRUE if a value was unpacked
4056
 *
4057
 * Since: 2.40
4058
 **/
4059
gboolean
4060
g_variant_dict_lookup (GVariantDict *dict,
4061
                       const gchar  *key,
4062
                       const gchar  *format_string,
4063
                       ...)
4064
0
{
4065
0
  GVariant *value;
4066
0
  va_list ap;
4067
4068
0
  return_val_if_invalid_dict (dict, FALSE);
4069
0
  g_return_val_if_fail (key != NULL, FALSE);
4070
0
  g_return_val_if_fail (format_string != NULL, FALSE);
4071
4072
0
  value = g_hash_table_lookup (GVSD(dict)->values, key);
4073
4074
0
  if (value == NULL || !g_variant_check_format_string (value, format_string, FALSE))
4075
0
    return FALSE;
4076
4077
0
  va_start (ap, format_string);
4078
0
  g_variant_get_va (value, format_string, NULL, &ap);
4079
0
  va_end (ap);
4080
4081
0
  return TRUE;
4082
0
}
4083
4084
/**
4085
 * g_variant_dict_lookup_value:
4086
 * @dict: a #GVariantDict
4087
 * @key: the key to look up in the dictionary
4088
 * @expected_type: (nullable): a #GVariantType, or %NULL
4089
 *
4090
 * Looks up a value in a #GVariantDict.
4091
 *
4092
 * If @key is not found in @dictionary, %NULL is returned.
4093
 *
4094
 * The @expected_type string specifies what type of value is expected.
4095
 * If the value associated with @key has a different type then %NULL is
4096
 * returned.
4097
 *
4098
 * If the key is found and the value has the correct type, it is
4099
 * returned.  If @expected_type was specified then any non-%NULL return
4100
 * value will have this type.
4101
 *
4102
 * Returns: (transfer full) (nullable): the value of the dictionary key, or %NULL
4103
 *
4104
 * Since: 2.40
4105
 **/
4106
GVariant *
4107
g_variant_dict_lookup_value (GVariantDict       *dict,
4108
                             const gchar        *key,
4109
                             const GVariantType *expected_type)
4110
0
{
4111
0
  GVariant *result;
4112
4113
0
  return_val_if_invalid_dict (dict, NULL);
4114
0
  g_return_val_if_fail (key != NULL, NULL);
4115
4116
0
  result = g_hash_table_lookup (GVSD(dict)->values, key);
4117
4118
0
  if (result && (!expected_type || g_variant_is_of_type (result, expected_type)))
4119
0
    return g_variant_ref (result);
4120
4121
0
  return NULL;
4122
0
}
4123
4124
/**
4125
 * g_variant_dict_contains:
4126
 * @dict: a #GVariantDict
4127
 * @key: the key to look up in the dictionary
4128
 *
4129
 * Checks if @key exists in @dict.
4130
 *
4131
 * Returns: %TRUE if @key is in @dict
4132
 *
4133
 * Since: 2.40
4134
 **/
4135
gboolean
4136
g_variant_dict_contains (GVariantDict *dict,
4137
                         const gchar  *key)
4138
0
{
4139
0
  return_val_if_invalid_dict (dict, FALSE);
4140
0
  g_return_val_if_fail (key != NULL, FALSE);
4141
4142
0
  return g_hash_table_contains (GVSD(dict)->values, key);
4143
0
}
4144
4145
/**
4146
 * g_variant_dict_insert:
4147
 * @dict: a #GVariantDict
4148
 * @key: the key to insert a value for
4149
 * @format_string: a #GVariant varargs format string
4150
 * @...: arguments, as per @format_string
4151
 *
4152
 * Inserts a value into a #GVariantDict.
4153
 *
4154
 * This call is a convenience wrapper that is exactly equivalent to
4155
 * calling g_variant_new() followed by g_variant_dict_insert_value().
4156
 *
4157
 * Since: 2.40
4158
 **/
4159
void
4160
g_variant_dict_insert (GVariantDict *dict,
4161
                       const gchar  *key,
4162
                       const gchar  *format_string,
4163
                       ...)
4164
0
{
4165
0
  va_list ap;
4166
4167
0
  return_if_invalid_dict (dict);
4168
0
  g_return_if_fail (key != NULL);
4169
0
  g_return_if_fail (format_string != NULL);
4170
4171
0
  va_start (ap, format_string);
4172
0
  g_variant_dict_insert_value (dict, key, g_variant_new_va (format_string, NULL, &ap));
4173
0
  va_end (ap);
4174
0
}
4175
4176
/**
4177
 * g_variant_dict_insert_value:
4178
 * @dict: a #GVariantDict
4179
 * @key: the key to insert a value for
4180
 * @value: the value to insert
4181
 *
4182
 * Inserts (or replaces) a key in a #GVariantDict.
4183
 *
4184
 * @value is consumed if it is floating.
4185
 *
4186
 * Since: 2.40
4187
 **/
4188
void
4189
g_variant_dict_insert_value (GVariantDict *dict,
4190
                             const gchar  *key,
4191
                             GVariant     *value)
4192
0
{
4193
0
  return_if_invalid_dict (dict);
4194
0
  g_return_if_fail (key != NULL);
4195
0
  g_return_if_fail (value != NULL);
4196
4197
0
  g_hash_table_insert (GVSD(dict)->values, g_strdup (key), g_variant_ref_sink (value));
4198
0
}
4199
4200
/**
4201
 * g_variant_dict_remove:
4202
 * @dict: a #GVariantDict
4203
 * @key: the key to remove
4204
 *
4205
 * Removes a key and its associated value from a #GVariantDict.
4206
 *
4207
 * Returns: %TRUE if the key was found and removed
4208
 *
4209
 * Since: 2.40
4210
 **/
4211
gboolean
4212
g_variant_dict_remove (GVariantDict *dict,
4213
                       const gchar  *key)
4214
0
{
4215
0
  return_val_if_invalid_dict (dict, FALSE);
4216
0
  g_return_val_if_fail (key != NULL, FALSE);
4217
4218
0
  return g_hash_table_remove (GVSD(dict)->values, key);
4219
0
}
4220
4221
/**
4222
 * g_variant_dict_clear:
4223
 * @dict: a #GVariantDict
4224
 *
4225
 * Releases all memory associated with a #GVariantDict without freeing
4226
 * the #GVariantDict structure itself.
4227
 *
4228
 * It typically only makes sense to do this on a stack-allocated
4229
 * #GVariantDict if you want to abort building the value part-way
4230
 * through.  This function need not be called if you call
4231
 * g_variant_dict_end() and it also doesn't need to be called on dicts
4232
 * allocated with g_variant_dict_new (see g_variant_dict_unref() for
4233
 * that).
4234
 *
4235
 * It is valid to call this function on either an initialised
4236
 * #GVariantDict or one that was previously cleared by an earlier call
4237
 * to g_variant_dict_clear() but it is not valid to call this function
4238
 * on uninitialised memory.
4239
 *
4240
 * Since: 2.40
4241
 **/
4242
void
4243
g_variant_dict_clear (GVariantDict *dict)
4244
0
{
4245
0
  if (GVSD(dict)->magic == 0)
4246
    /* all-zeros case */
4247
0
    return;
4248
4249
0
  return_if_invalid_dict (dict);
4250
4251
0
  g_hash_table_unref (GVSD(dict)->values);
4252
0
  GVSD(dict)->values = NULL;
4253
4254
0
  GVSD(dict)->magic = 0;
4255
0
}
4256
4257
/**
4258
 * g_variant_dict_end:
4259
 * @dict: a #GVariantDict
4260
 *
4261
 * Returns the current value of @dict as a #GVariant of type
4262
 * %G_VARIANT_TYPE_VARDICT, clearing it in the process.
4263
 *
4264
 * It is not permissible to use @dict in any way after this call except
4265
 * for reference counting operations (in the case of a heap-allocated
4266
 * #GVariantDict) or by reinitialising it with g_variant_dict_init() (in
4267
 * the case of stack-allocated).
4268
 *
4269
 * Returns: (transfer none): a new, floating, #GVariant
4270
 *
4271
 * Since: 2.40
4272
 **/
4273
GVariant *
4274
g_variant_dict_end (GVariantDict *dict)
4275
0
{
4276
0
  GVariantBuilder builder;
4277
0
  GHashTableIter iter;
4278
0
  gpointer key, value;
4279
4280
0
  return_val_if_invalid_dict (dict, NULL);
4281
4282
0
  g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
4283
4284
0
  g_hash_table_iter_init (&iter, GVSD(dict)->values);
4285
0
  while (g_hash_table_iter_next (&iter, &key, &value))
4286
0
    g_variant_builder_add (&builder, "{sv}", (const gchar *) key, (GVariant *) value);
4287
4288
0
  g_variant_dict_clear (dict);
4289
4290
0
  return g_variant_builder_end (&builder);
4291
0
}
4292
4293
/**
4294
 * g_variant_dict_ref:
4295
 * @dict: a heap-allocated #GVariantDict
4296
 *
4297
 * Increases the reference count on @dict.
4298
 *
4299
 * Don't call this on stack-allocated #GVariantDict instances or bad
4300
 * things will happen.
4301
 *
4302
 * Returns: (transfer full): a new reference to @dict
4303
 *
4304
 * Since: 2.40
4305
 **/
4306
GVariantDict *
4307
g_variant_dict_ref (GVariantDict *dict)
4308
0
{
4309
0
  g_return_val_if_fail (is_valid_heap_dict (dict), NULL);
4310
4311
0
  GVHD(dict)->ref_count++;
4312
4313
0
  return dict;
4314
0
}
4315
4316
/**
4317
 * g_variant_dict_unref:
4318
 * @dict: (transfer full): a heap-allocated #GVariantDict
4319
 *
4320
 * Decreases the reference count on @dict.
4321
 *
4322
 * In the event that there are no more references, releases all memory
4323
 * associated with the #GVariantDict.
4324
 *
4325
 * Don't call this on stack-allocated #GVariantDict instances or bad
4326
 * things will happen.
4327
 *
4328
 * Since: 2.40
4329
 **/
4330
void
4331
g_variant_dict_unref (GVariantDict *dict)
4332
0
{
4333
0
  g_return_if_fail (is_valid_heap_dict (dict));
4334
4335
0
  if (--GVHD(dict)->ref_count == 0)
4336
0
    {
4337
0
      g_variant_dict_clear (dict);
4338
0
      g_free_sized (dict, sizeof (GVariantDict));
4339
0
    }
4340
0
}
4341
4342
4343
/* Format strings {{{1 */
4344
/*< private >
4345
 * g_variant_format_string_scan:
4346
 * @string: a string that may be prefixed with a format string
4347
 * @limit: (nullable) (default NULL): a pointer to the end of @string,
4348
 *         or %NULL
4349
 * @endptr: (nullable) (default NULL): location to store the end pointer,
4350
 *          or %NULL
4351
 *
4352
 * Checks the string pointed to by @string for starting with a properly
4353
 * formed #GVariant varargs format string.  If no valid format string is
4354
 * found then %FALSE is returned.
4355
 *
4356
 * If @string does start with a valid format string then %TRUE is
4357
 * returned.  If @endptr is non-%NULL then it is updated to point to the
4358
 * first character after the format string.
4359
 *
4360
 * If @limit is non-%NULL then @limit (and any character after it) will
4361
 * not be accessed and the effect is otherwise equivalent to if the
4362
 * character at @limit were nul.
4363
 *
4364
 * See the section on [GVariant format strings][gvariant-format-strings].
4365
 *
4366
 * Returns: %TRUE if there was a valid format string
4367
 *
4368
 * Since: 2.24
4369
 */
4370
gboolean
4371
g_variant_format_string_scan (const gchar  *string,
4372
                              const gchar  *limit,
4373
                              const gchar **endptr)
4374
160
{
4375
200
#define next_char() (string == limit ? '\0' : *(string++))
4376
160
#define peek_char() (string == limit ? '\0' : *string)
4377
160
  char c;
4378
4379
160
  switch (next_char())
4380
160
    {
4381
80
    case 'b': case 'y': case 'n': case 'q': case 'i': case 'u':
4382
80
    case 'x': case 't': case 'h': case 'd': case 's': case 'o':
4383
80
    case 'g': case 'v': case '*': case '?': case 'r':
4384
80
      break;
4385
4386
0
    case 'm':
4387
0
      return g_variant_format_string_scan (string, limit, endptr);
4388
4389
0
    case 'a':
4390
40
    case '@':
4391
40
      return g_variant_type_string_scan (string, limit, endptr);
4392
4393
40
    case '(':
4394
160
      while (peek_char() != ')')
4395
120
        if (!g_variant_format_string_scan (string, limit, &string))
4396
0
          return FALSE;
4397
4398
40
      next_char(); /* consume ')' */
4399
40
      break;
4400
4401
0
    case '{':
4402
0
      c = next_char();
4403
4404
0
      if (c == '&')
4405
0
        {
4406
0
          c = next_char ();
4407
4408
0
          if (c != 's' && c != 'o' && c != 'g')
4409
0
            return FALSE;
4410
0
        }
4411
0
      else
4412
0
        {
4413
0
          if (c == '@')
4414
0
            c = next_char ();
4415
4416
          /* ISO/IEC 9899:1999 (C99) §7.21.5.2:
4417
           *    The terminating null character is considered to be
4418
           *    part of the string.
4419
           */
4420
0
          if (c != '\0' && strchr ("bynqiuxthdsog?", c) == NULL)
4421
0
            return FALSE;
4422
0
        }
4423
4424
0
      if (!g_variant_format_string_scan (string, limit, &string))
4425
0
        return FALSE;
4426
4427
0
      if (next_char() != '}')
4428
0
        return FALSE;
4429
4430
0
      break;
4431
4432
0
    case '^':
4433
0
      if ((c = next_char()) == 'a')
4434
0
        {
4435
0
          if ((c = next_char()) == '&')
4436
0
            {
4437
0
              if ((c = next_char()) == 'a')
4438
0
                {
4439
0
                  if ((c = next_char()) == 'y')
4440
0
                    break;      /* '^a&ay' */
4441
0
                }
4442
4443
0
              else if (c == 's' || c == 'o')
4444
0
                break;          /* '^a&s', '^a&o' */
4445
0
            }
4446
4447
0
          else if (c == 'a')
4448
0
            {
4449
0
              if ((c = next_char()) == 'y')
4450
0
                break;          /* '^aay' */
4451
0
            }
4452
4453
0
          else if (c == 's' || c == 'o')
4454
0
            break;              /* '^as', '^ao' */
4455
4456
0
          else if (c == 'y')
4457
0
            break;              /* '^ay' */
4458
0
        }
4459
0
      else if (c == '&')
4460
0
        {
4461
0
          if ((c = next_char()) == 'a')
4462
0
            {
4463
0
              if ((c = next_char()) == 'y')
4464
0
                break;          /* '^&ay' */
4465
0
            }
4466
0
        }
4467
4468
0
      return FALSE;
4469
4470
0
    case '&':
4471
0
      c = next_char();
4472
4473
0
      if (c != 's' && c != 'o' && c != 'g')
4474
0
        return FALSE;
4475
4476
0
      break;
4477
4478
0
    default:
4479
0
      return FALSE;
4480
160
    }
4481
4482
120
  if (endptr != NULL)
4483
120
    *endptr = string;
4484
4485
120
#undef next_char
4486
120
#undef peek_char
4487
4488
120
  return TRUE;
4489
160
}
4490
4491
/**
4492
 * g_variant_check_format_string:
4493
 * @value: a #GVariant
4494
 * @format_string: a valid #GVariant format string
4495
 * @copy_only: %TRUE to ensure the format string makes deep copies
4496
 *
4497
 * Checks if calling g_variant_get() with @format_string on @value would
4498
 * be valid from a type-compatibility standpoint.  @format_string is
4499
 * assumed to be a valid format string (from a syntactic standpoint).
4500
 *
4501
 * If @copy_only is %TRUE then this function additionally checks that it
4502
 * would be safe to call g_variant_unref() on @value immediately after
4503
 * the call to g_variant_get() without invalidating the result.  This is
4504
 * only possible if deep copies are made (ie: there are no pointers to
4505
 * the data inside of the soon-to-be-freed #GVariant instance).  If this
4506
 * check fails then a g_critical() is printed and %FALSE is returned.
4507
 *
4508
 * This function is meant to be used by functions that wish to provide
4509
 * varargs accessors to #GVariant values of uncertain values (eg:
4510
 * g_variant_lookup() or g_menu_model_get_item_attribute()).
4511
 *
4512
 * Returns: %TRUE if @format_string is safe to use
4513
 *
4514
 * Since: 2.34
4515
 */
4516
gboolean
4517
g_variant_check_format_string (GVariant    *value,
4518
                               const gchar *format_string,
4519
                               gboolean     copy_only)
4520
0
{
4521
0
  const gchar *original_format = format_string;
4522
0
  const gchar *type_string;
4523
4524
  /* Interesting factoid: assuming a format string is valid, it can be
4525
   * converted to a type string by removing all '@' '&' and '^'
4526
   * characters.
4527
   *
4528
   * Instead of doing that, we can just skip those characters when
4529
   * comparing it to the type string of @value.
4530
   *
4531
   * For the copy-only case we can just drop the '&' from the list of
4532
   * characters to skip over.  A '&' will never appear in a type string
4533
   * so we know that it won't be possible to return %TRUE if it is in a
4534
   * format string.
4535
   */
4536
0
  type_string = g_variant_get_type_string (value);
4537
4538
0
  while (*type_string || *format_string)
4539
0
    {
4540
0
      gchar format = *format_string++;
4541
4542
0
      switch (format)
4543
0
        {
4544
0
        case '&':
4545
0
          if G_UNLIKELY (copy_only)
4546
0
            {
4547
              /* for the love of all that is good, please don't mark this string for translation... */
4548
0
              g_critical ("g_variant_check_format_string() is being called by a function with a GVariant varargs "
4549
0
                          "interface to validate the passed format string for type safety.  The passed format "
4550
0
                          "(%s) contains a '&' character which would result in a pointer being returned to the "
4551
0
                          "data inside of a GVariant instance that may no longer exist by the time the function "
4552
0
                          "returns.  Modify your code to use a format string without '&'.", original_format);
4553
0
              return FALSE;
4554
0
            }
4555
4556
0
          G_GNUC_FALLTHROUGH;
4557
0
        case '^':
4558
0
        case '@':
4559
          /* ignore these 2 (or 3) */
4560
0
          continue;
4561
4562
0
        case '?':
4563
          /* attempt to consume one of 'bynqiuxthdsog' */
4564
0
          {
4565
0
            char s = *type_string++;
4566
4567
0
            if (s == '\0' || strchr ("bynqiuxthdsog", s) == NULL)
4568
0
              return FALSE;
4569
0
          }
4570
0
          continue;
4571
4572
0
        case 'r':
4573
          /* ensure it's a tuple */
4574
0
          if (*type_string != '(')
4575
0
            return FALSE;
4576
4577
0
          G_GNUC_FALLTHROUGH;
4578
0
        case '*':
4579
          /* consume a full type string for the '*' or 'r' */
4580
0
          if (!g_variant_type_string_scan (type_string, NULL, &type_string))
4581
0
            return FALSE;
4582
4583
0
          continue;
4584
4585
0
        default:
4586
          /* attempt to consume exactly one character equal to the format */
4587
0
          if (format != *type_string++)
4588
0
            return FALSE;
4589
0
        }
4590
0
    }
4591
4592
0
  return TRUE;
4593
0
}
4594
4595
/*< private >
4596
 * g_variant_format_string_scan_type:
4597
 * @string: a string that may be prefixed with a format string
4598
 * @limit: (nullable) (default NULL): a pointer to the end of @string,
4599
 *         or %NULL
4600
 * @endptr: (nullable) (default NULL): location to store the end pointer,
4601
 *          or %NULL
4602
 *
4603
 * If @string starts with a valid format string then this function will
4604
 * return the type that the format string corresponds to.  Otherwise
4605
 * this function returns %NULL.
4606
 *
4607
 * Use g_variant_type_free() to free the return value when you no longer
4608
 * need it.
4609
 *
4610
 * This function is otherwise exactly like
4611
 * g_variant_format_string_scan().
4612
 *
4613
 * Returns: (nullable): a #GVariantType if there was a valid format string
4614
 *
4615
 * Since: 2.24
4616
 */
4617
GVariantType *
4618
g_variant_format_string_scan_type (const gchar  *string,
4619
                                   const gchar  *limit,
4620
                                   const gchar **endptr)
4621
40
{
4622
40
  const gchar *my_end;
4623
40
  gchar *dest;
4624
40
  gchar *new;
4625
4626
40
  if (endptr == NULL)
4627
0
    endptr = &my_end;
4628
4629
40
  if (!g_variant_format_string_scan (string, limit, endptr))
4630
0
    return NULL;
4631
4632
40
  dest = new = g_malloc (*endptr - string + 1);
4633
320
  while (string != *endptr)
4634
280
    {
4635
280
      if (*string != '@' && *string != '&' && *string != '^')
4636
240
        *dest++ = *string;
4637
280
      string++;
4638
280
    }
4639
40
  *dest = '\0';
4640
4641
40
  return (GVariantType *) G_VARIANT_TYPE (new);
4642
40
}
4643
4644
static gboolean
4645
valid_format_string (const gchar *format_string,
4646
                     gboolean     single,
4647
                     GVariant    *value)
4648
40
{
4649
40
  const gchar *endptr;
4650
40
  GVariantType *type;
4651
4652
40
  type = g_variant_format_string_scan_type (format_string, NULL, &endptr);
4653
4654
40
  if G_UNLIKELY (type == NULL || (single && *endptr != '\0'))
4655
0
    {
4656
0
      if (single)
4657
0
        g_critical ("'%s' is not a valid GVariant format string",
4658
0
                    format_string);
4659
0
      else
4660
0
        g_critical ("'%s' does not have a valid GVariant format "
4661
0
                    "string as a prefix", format_string);
4662
4663
0
      if (type != NULL)
4664
0
        g_variant_type_free (type);
4665
4666
0
      return FALSE;
4667
0
    }
4668
4669
40
  if G_UNLIKELY (value && !g_variant_is_of_type (value, type))
4670
0
    {
4671
0
      gchar *fragment;
4672
0
      gchar *typestr;
4673
4674
0
      fragment = g_strndup (format_string, endptr - format_string);
4675
0
      typestr = g_variant_type_dup_string (type);
4676
4677
0
      g_critical ("the GVariant format string '%s' has a type of "
4678
0
                  "'%s' but the given value has a type of '%s'",
4679
0
                  fragment, typestr, g_variant_get_type_string (value));
4680
4681
0
      g_variant_type_free (type);
4682
0
      g_free (fragment);
4683
0
      g_free (typestr);
4684
4685
0
      return FALSE;
4686
0
    }
4687
4688
40
  g_variant_type_free (type);
4689
4690
40
  return TRUE;
4691
40
}
4692
4693
/* Variable Arguments {{{1 */
4694
/* We consider 2 main classes of format strings:
4695
 *
4696
 *   - recursive format strings
4697
 *      these are ones that result in recursion and the collection of
4698
 *      possibly more than one argument.  Maybe types, tuples,
4699
 *      dictionary entries.
4700
 *
4701
 *   - leaf format string
4702
 *      these result in the collection of a single argument.
4703
 *
4704
 * Leaf format strings are further subdivided into two categories:
4705
 *
4706
 *   - single non-null pointer ("nnp")
4707
 *      these either collect or return a single non-null pointer.
4708
 *
4709
 *   - other
4710
 *      these collect or return something else (bool, number, etc).
4711
 *
4712
 * Based on the above, the varargs handling code is split into 4 main parts:
4713
 *
4714
 *   - nnp handling code
4715
 *   - leaf handling code (which may invoke nnp code)
4716
 *   - generic handling code (may be recursive, may invoke leaf code)
4717
 *   - user-facing API (which invokes the generic code)
4718
 *
4719
 * Each section implements some of the following functions:
4720
 *
4721
 *   - skip:
4722
 *      collect the arguments for the format string as if
4723
 *      g_variant_new() had been called, but do nothing with them.  used
4724
 *      for skipping over arguments when constructing a Nothing maybe
4725
 *      type.
4726
 *
4727
 *   - new:
4728
 *      create a GVariant *
4729
 *
4730
 *   - get:
4731
 *      unpack a GVariant *
4732
 *
4733
 *   - free (nnp only):
4734
 *      free a previously allocated item
4735
 */
4736
4737
static gboolean
4738
g_variant_format_string_is_leaf (const gchar *str)
4739
80
{
4740
80
  return str[0] != 'm' && str[0] != '(' && str[0] != '{';
4741
80
}
4742
4743
static gboolean
4744
g_variant_format_string_is_nnp (const gchar *str)
4745
60
{
4746
60
  return str[0] == 'a' || str[0] == 's' || str[0] == 'o' || str[0] == 'g' ||
4747
60
         str[0] == '^' || str[0] == '@' || str[0] == '*' || str[0] == '?' ||
4748
60
         str[0] == 'r' || str[0] == 'v' || str[0] == '&';
4749
60
}
4750
4751
/* Single non-null pointer ("nnp") {{{2 */
4752
static void
4753
g_variant_valist_free_nnp (const gchar *str,
4754
                           gpointer     ptr)
4755
0
{
4756
0
  switch (*str)
4757
0
    {
4758
0
    case 'a':
4759
0
      g_variant_iter_free (ptr);
4760
0
      break;
4761
4762
0
    case '^':
4763
0
      if (g_str_has_suffix (str, "y"))
4764
0
        {
4765
0
          if (str[2] != 'a') /* '^a&ay', '^ay' */
4766
0
            g_free (ptr);
4767
0
          else if (str[1] == 'a') /* '^aay' */
4768
0
            g_strfreev (ptr);
4769
0
          break; /* '^&ay' */
4770
0
        }
4771
0
      else if (str[2] != '&') /* '^as', '^ao' */
4772
0
        g_strfreev (ptr);
4773
0
      else                      /* '^a&s', '^a&o' */
4774
0
        g_free (ptr);
4775
0
      break;
4776
4777
0
    case 's':
4778
0
    case 'o':
4779
0
    case 'g':
4780
0
      g_free (ptr);
4781
0
      break;
4782
4783
0
    case '@':
4784
0
    case '*':
4785
0
    case '?':
4786
0
    case 'v':
4787
0
      g_variant_unref (ptr);
4788
0
      break;
4789
4790
0
    case '&':
4791
0
      break;
4792
4793
0
    default:
4794
0
      g_assert_not_reached ();
4795
0
    }
4796
0
}
4797
4798
static gchar
4799
g_variant_scan_convenience (const gchar **str,
4800
                            gboolean     *constant,
4801
                            guint        *arrays)
4802
0
{
4803
0
  *constant = FALSE;
4804
0
  *arrays = 0;
4805
4806
0
  for (;;)
4807
0
    {
4808
0
      char c = *(*str)++;
4809
4810
0
      if (c == '&')
4811
0
        *constant = TRUE;
4812
4813
0
      else if (c == 'a')
4814
0
        (*arrays)++;
4815
4816
0
      else
4817
0
        return c;
4818
0
    }
4819
0
}
4820
4821
static GVariant *
4822
g_variant_valist_new_nnp (const gchar **str,
4823
                          gpointer      ptr)
4824
0
{
4825
0
  if (**str == '&')
4826
0
    (*str)++;
4827
4828
0
  switch (*(*str)++)
4829
0
    {
4830
0
    case 'a':
4831
0
      if (ptr != NULL)
4832
0
        {
4833
0
          const GVariantType *type;
4834
0
          GVariant *value;
4835
4836
0
          value = g_variant_builder_end (ptr);
4837
0
          type = g_variant_get_type (value);
4838
4839
0
          if G_UNLIKELY (!g_variant_type_is_array (type))
4840
0
            g_error ("g_variant_new: expected array GVariantBuilder but "
4841
0
                     "the built value has type '%s'",
4842
0
                     g_variant_get_type_string (value));
4843
4844
0
          type = g_variant_type_element (type);
4845
4846
0
          if G_UNLIKELY (!g_variant_type_is_subtype_of (type, (GVariantType *) *str))
4847
0
            {
4848
0
              gchar *type_string = g_variant_type_dup_string ((GVariantType *) *str);
4849
0
              g_error ("g_variant_new: expected GVariantBuilder array element "
4850
0
                       "type '%s' but the built value has element type '%s'",
4851
0
                       type_string, g_variant_get_type_string (value) + 1);
4852
0
              g_free (type_string);
4853
0
            }
4854
4855
0
          g_variant_type_string_scan (*str, NULL, str);
4856
4857
0
          return value;
4858
0
        }
4859
0
      else
4860
4861
        /* special case: NULL pointer for empty array */
4862
0
        {
4863
0
          const GVariantType *type = (GVariantType *) *str;
4864
4865
0
          g_variant_type_string_scan (*str, NULL, str);
4866
4867
0
          if G_UNLIKELY (!g_variant_type_is_definite (type))
4868
0
            g_error ("g_variant_new: NULL pointer given with indefinite "
4869
0
                     "array type; unable to determine which type of empty "
4870
0
                     "array to construct.");
4871
4872
0
          return g_variant_new_array (type, NULL, 0);
4873
0
        }
4874
4875
0
    case 's':
4876
0
      {
4877
0
        GVariant *value;
4878
4879
0
        value = g_variant_new_string (ptr);
4880
4881
0
        if (value == NULL)
4882
0
          value = g_variant_new_string ("[Invalid UTF-8]");
4883
4884
0
        return value;
4885
0
      }
4886
4887
0
    case 'o':
4888
0
      return g_variant_new_object_path (ptr);
4889
4890
0
    case 'g':
4891
0
      return g_variant_new_signature (ptr);
4892
4893
0
    case '^':
4894
0
      {
4895
0
        gboolean constant;
4896
0
        guint arrays;
4897
0
        gchar type;
4898
4899
0
        type = g_variant_scan_convenience (str, &constant, &arrays);
4900
4901
0
        if (type == 's')
4902
0
          return g_variant_new_strv (ptr, -1);
4903
4904
0
        if (type == 'o')
4905
0
          return g_variant_new_objv (ptr, -1);
4906
4907
0
        if (arrays > 1)
4908
0
          return g_variant_new_bytestring_array (ptr, -1);
4909
4910
0
        return g_variant_new_bytestring (ptr);
4911
0
      }
4912
4913
0
    case '@':
4914
0
      if G_UNLIKELY (!g_variant_is_of_type (ptr, (GVariantType *) *str))
4915
0
        {
4916
0
          gchar *type_string = g_variant_type_dup_string ((GVariantType *) *str);
4917
0
          g_error ("g_variant_new: expected GVariant of type '%s' but "
4918
0
                   "received value has type '%s'",
4919
0
                   type_string, g_variant_get_type_string (ptr));
4920
0
          g_free (type_string);
4921
0
        }
4922
4923
0
      g_variant_type_string_scan (*str, NULL, str);
4924
4925
0
      return ptr;
4926
4927
0
    case '*':
4928
0
      return ptr;
4929
4930
0
    case '?':
4931
0
      if G_UNLIKELY (!g_variant_type_is_basic (g_variant_get_type (ptr)))
4932
0
        g_error ("g_variant_new: format string '?' expects basic-typed "
4933
0
                 "GVariant, but received value has type '%s'",
4934
0
                 g_variant_get_type_string (ptr));
4935
4936
0
      return ptr;
4937
4938
0
    case 'r':
4939
0
      if G_UNLIKELY (!g_variant_type_is_tuple (g_variant_get_type (ptr)))
4940
0
        g_error ("g_variant_new: format string 'r' expects tuple-typed "
4941
0
                 "GVariant, but received value has type '%s'",
4942
0
                 g_variant_get_type_string (ptr));
4943
4944
0
      return ptr;
4945
4946
0
    case 'v':
4947
0
      return g_variant_new_variant (ptr);
4948
4949
0
    default:
4950
0
      g_assert_not_reached ();
4951
0
    }
4952
0
}
4953
4954
static gpointer
4955
g_variant_valist_get_nnp (const gchar **str,
4956
                          GVariant     *value)
4957
20
{
4958
20
  switch (*(*str)++)
4959
20
    {
4960
0
    case 'a':
4961
0
      g_variant_type_string_scan (*str, NULL, str);
4962
0
      return g_variant_iter_new (value);
4963
4964
0
    case '&':
4965
0
      (*str)++;
4966
0
      return (gchar *) g_variant_get_string (value, NULL);
4967
4968
0
    case 's':
4969
0
    case 'o':
4970
0
    case 'g':
4971
0
      return g_variant_dup_string (value, NULL);
4972
4973
0
    case '^':
4974
0
      {
4975
0
        gboolean constant;
4976
0
        guint arrays;
4977
0
        gchar type;
4978
4979
0
        type = g_variant_scan_convenience (str, &constant, &arrays);
4980
4981
0
        if (type == 's')
4982
0
          {
4983
0
            if (constant)
4984
0
              return g_variant_get_strv (value, NULL);
4985
0
            else
4986
0
              return g_variant_dup_strv (value, NULL);
4987
0
          }
4988
4989
0
        else if (type == 'o')
4990
0
          {
4991
0
            if (constant)
4992
0
              return g_variant_get_objv (value, NULL);
4993
0
            else
4994
0
              return g_variant_dup_objv (value, NULL);
4995
0
          }
4996
4997
0
        else if (arrays > 1)
4998
0
          {
4999
0
            if (constant)
5000
0
              return g_variant_get_bytestring_array (value, NULL);
5001
0
            else
5002
0
              return g_variant_dup_bytestring_array (value, NULL);
5003
0
          }
5004
5005
0
        else
5006
0
          {
5007
0
            if (constant)
5008
0
              return (gchar *) g_variant_get_bytestring (value);
5009
0
            else
5010
0
              return g_variant_dup_bytestring (value, NULL);
5011
0
          }
5012
0
      }
5013
5014
20
    case '@':
5015
20
      g_variant_type_string_scan (*str, NULL, str);
5016
20
      G_GNUC_FALLTHROUGH;
5017
5018
20
    case '*':
5019
20
    case '?':
5020
20
    case 'r':
5021
20
      return g_variant_ref (value);
5022
5023
0
    case 'v':
5024
0
      return g_variant_get_variant (value);
5025
5026
0
    default:
5027
0
      g_assert_not_reached ();
5028
20
    }
5029
20
}
5030
5031
/* Leaves {{{2 */
5032
static void
5033
g_variant_valist_skip_leaf (const gchar **str,
5034
                            va_list      *app)
5035
0
{
5036
0
  if (g_variant_format_string_is_nnp (*str))
5037
0
    {
5038
0
      g_variant_format_string_scan (*str, NULL, str);
5039
0
      va_arg (*app, gpointer);
5040
0
      return;
5041
0
    }
5042
5043
0
  switch (*(*str)++)
5044
0
    {
5045
0
    case 'b':
5046
0
    case 'y':
5047
0
    case 'n':
5048
0
    case 'q':
5049
0
    case 'i':
5050
0
    case 'u':
5051
0
    case 'h':
5052
0
      va_arg (*app, int);
5053
0
      return;
5054
5055
0
    case 'x':
5056
0
    case 't':
5057
0
      va_arg (*app, guint64);
5058
0
      return;
5059
5060
0
    case 'd':
5061
0
      va_arg (*app, gdouble);
5062
0
      return;
5063
5064
0
    default:
5065
0
      g_assert_not_reached ();
5066
0
    }
5067
0
}
5068
5069
static GVariant *
5070
g_variant_valist_new_leaf (const gchar **str,
5071
                           va_list      *app)
5072
0
{
5073
0
  if (g_variant_format_string_is_nnp (*str))
5074
0
    return g_variant_valist_new_nnp (str, va_arg (*app, gpointer));
5075
5076
0
  switch (*(*str)++)
5077
0
    {
5078
0
    case 'b':
5079
0
      return g_variant_new_boolean (va_arg (*app, gboolean));
5080
5081
0
    case 'y':
5082
0
      return g_variant_new_byte (va_arg (*app, guint));
5083
5084
0
    case 'n':
5085
0
      return g_variant_new_int16 (va_arg (*app, gint));
5086
5087
0
    case 'q':
5088
0
      return g_variant_new_uint16 (va_arg (*app, guint));
5089
5090
0
    case 'i':
5091
0
      return g_variant_new_int32 (va_arg (*app, gint));
5092
5093
0
    case 'u':
5094
0
      return g_variant_new_uint32 (va_arg (*app, guint));
5095
5096
0
    case 'x':
5097
0
      return g_variant_new_int64 (va_arg (*app, gint64));
5098
5099
0
    case 't':
5100
0
      return g_variant_new_uint64 (va_arg (*app, guint64));
5101
5102
0
    case 'h':
5103
0
      return g_variant_new_handle (va_arg (*app, gint));
5104
5105
0
    case 'd':
5106
0
      return g_variant_new_double (va_arg (*app, gdouble));
5107
5108
0
    default:
5109
0
      g_assert_not_reached ();
5110
0
    }
5111
0
}
5112
5113
/* The code below assumes this */
5114
G_STATIC_ASSERT (sizeof (gboolean) == sizeof (guint32));
5115
G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64));
5116
5117
static void
5118
g_variant_valist_get_leaf (const gchar **str,
5119
                           GVariant     *value,
5120
                           gboolean      free,
5121
                           va_list      *app)
5122
60
{
5123
60
  gpointer ptr = va_arg (*app, gpointer);
5124
5125
60
  if (ptr == NULL)
5126
0
    {
5127
0
      g_variant_format_string_scan (*str, NULL, str);
5128
0
      return;
5129
0
    }
5130
5131
60
  if (g_variant_format_string_is_nnp (*str))
5132
20
    {
5133
20
      gpointer *nnp = (gpointer *) ptr;
5134
5135
20
      if (free && *nnp != NULL)
5136
0
        g_variant_valist_free_nnp (*str, *nnp);
5137
5138
20
      *nnp = NULL;
5139
5140
20
      if (value != NULL)
5141
20
        *nnp = g_variant_valist_get_nnp (str, value);
5142
0
      else
5143
0
        g_variant_format_string_scan (*str, NULL, str);
5144
5145
20
      return;
5146
20
    }
5147
5148
40
  if (value != NULL)
5149
40
    {
5150
40
      switch (*(*str)++)
5151
40
        {
5152
0
        case 'b':
5153
0
          *(gboolean *) ptr = g_variant_get_boolean (value);
5154
0
          return;
5155
5156
0
        case 'y':
5157
0
          *(guint8 *) ptr = g_variant_get_byte (value);
5158
0
          return;
5159
5160
0
        case 'n':
5161
0
          *(gint16 *) ptr = g_variant_get_int16 (value);
5162
0
          return;
5163
5164
0
        case 'q':
5165
0
          *(guint16 *) ptr = g_variant_get_uint16 (value);
5166
0
          return;
5167
5168
0
        case 'i':
5169
0
          *(gint32 *) ptr = g_variant_get_int32 (value);
5170
0
          return;
5171
5172
40
        case 'u':
5173
40
          *(guint32 *) ptr = g_variant_get_uint32 (value);
5174
40
          return;
5175
5176
0
        case 'x':
5177
0
          *(gint64 *) ptr = g_variant_get_int64 (value);
5178
0
          return;
5179
5180
0
        case 't':
5181
0
          *(guint64 *) ptr = g_variant_get_uint64 (value);
5182
0
          return;
5183
5184
0
        case 'h':
5185
0
          *(gint32 *) ptr = g_variant_get_handle (value);
5186
0
          return;
5187
5188
0
        case 'd':
5189
0
          *(gdouble *) ptr = g_variant_get_double (value);
5190
0
          return;
5191
40
        }
5192
40
    }
5193
0
  else
5194
0
    {
5195
0
      switch (*(*str)++)
5196
0
        {
5197
0
        case 'y':
5198
0
          *(guint8 *) ptr = 0;
5199
0
          return;
5200
5201
0
        case 'n':
5202
0
        case 'q':
5203
0
          *(guint16 *) ptr = 0;
5204
0
          return;
5205
5206
0
        case 'i':
5207
0
        case 'u':
5208
0
        case 'h':
5209
0
        case 'b':
5210
0
          *(guint32 *) ptr = 0;
5211
0
          return;
5212
5213
0
        case 'x':
5214
0
        case 't':
5215
0
        case 'd':
5216
0
          *(guint64 *) ptr = 0;
5217
0
          return;
5218
0
        }
5219
0
    }
5220
5221
40
  g_assert_not_reached ();
5222
0
}
5223
5224
/* Generic (recursive) {{{2 */
5225
static void
5226
g_variant_valist_skip (const gchar **str,
5227
                       va_list      *app)
5228
0
{
5229
0
  if (g_variant_format_string_is_leaf (*str))
5230
0
    g_variant_valist_skip_leaf (str, app);
5231
5232
0
  else if (**str == 'm') /* maybe */
5233
0
    {
5234
0
      (*str)++;
5235
5236
0
      if (!g_variant_format_string_is_nnp (*str))
5237
0
        va_arg (*app, gboolean);
5238
5239
0
      g_variant_valist_skip (str, app);
5240
0
    }
5241
0
  else /* tuple, dictionary entry */
5242
0
    {
5243
0
      g_assert (**str == '(' || **str == '{');
5244
0
      (*str)++;
5245
0
      while (**str != ')' && **str != '}')
5246
0
        g_variant_valist_skip (str, app);
5247
0
      (*str)++;
5248
0
    }
5249
0
}
5250
5251
static GVariant *
5252
g_variant_valist_new (const gchar **str,
5253
                      va_list      *app)
5254
0
{
5255
0
  if (g_variant_format_string_is_leaf (*str))
5256
0
    return g_variant_valist_new_leaf (str, app);
5257
5258
0
  if (**str == 'm') /* maybe */
5259
0
    {
5260
0
      GVariantType *type = NULL;
5261
0
      GVariant *value = NULL;
5262
5263
0
      (*str)++;
5264
5265
0
      if (g_variant_format_string_is_nnp (*str))
5266
0
        {
5267
0
          gpointer nnp = va_arg (*app, gpointer);
5268
5269
0
          if (nnp != NULL)
5270
0
            value = g_variant_valist_new_nnp (str, nnp);
5271
0
          else
5272
0
            type = g_variant_format_string_scan_type (*str, NULL, str);
5273
0
        }
5274
0
      else
5275
0
        {
5276
0
          gboolean just = va_arg (*app, gboolean);
5277
5278
0
          if (just)
5279
0
            value = g_variant_valist_new (str, app);
5280
0
          else
5281
0
            {
5282
0
              type = g_variant_format_string_scan_type (*str, NULL, NULL);
5283
0
              g_variant_valist_skip (str, app);
5284
0
            }
5285
0
        }
5286
5287
0
      value = g_variant_new_maybe (type, value);
5288
5289
0
      if (type != NULL)
5290
0
        g_variant_type_free (type);
5291
5292
0
      return value;
5293
0
    }
5294
0
  else /* tuple, dictionary entry */
5295
0
    {
5296
0
      GVariantBuilder b;
5297
5298
0
      if (**str == '(')
5299
0
        g_variant_builder_init (&b, G_VARIANT_TYPE_TUPLE);
5300
0
      else
5301
0
        {
5302
0
          g_assert (**str == '{');
5303
0
          g_variant_builder_init (&b, G_VARIANT_TYPE_DICT_ENTRY);
5304
0
        }
5305
5306
0
      (*str)++; /* '(' */
5307
0
      while (**str != ')' && **str != '}')
5308
0
        g_variant_builder_add_value (&b, g_variant_valist_new (str, app));
5309
0
      (*str)++; /* ')' */
5310
5311
0
      return g_variant_builder_end (&b);
5312
0
    }
5313
0
}
5314
5315
static void
5316
g_variant_valist_get (const gchar **str,
5317
                      GVariant     *value,
5318
                      gboolean      free,
5319
                      va_list      *app)
5320
80
{
5321
80
  if (g_variant_format_string_is_leaf (*str))
5322
60
    g_variant_valist_get_leaf (str, value, free, app);
5323
5324
20
  else if (**str == 'm')
5325
0
    {
5326
0
      (*str)++;
5327
5328
0
      if (value != NULL)
5329
0
        value = g_variant_get_maybe (value);
5330
5331
0
      if (!g_variant_format_string_is_nnp (*str))
5332
0
        {
5333
0
          gboolean *ptr = va_arg (*app, gboolean *);
5334
5335
0
          if (ptr != NULL)
5336
0
            *ptr = value != NULL;
5337
0
        }
5338
5339
0
      g_variant_valist_get (str, value, free, app);
5340
5341
0
      if (value != NULL)
5342
0
        g_variant_unref (value);
5343
0
    }
5344
5345
20
  else /* tuple, dictionary entry */
5346
20
    {
5347
20
      gint index = 0;
5348
5349
20
      g_assert (**str == '(' || **str == '{');
5350
5351
20
      (*str)++;
5352
80
      while (**str != ')' && **str != '}')
5353
60
        {
5354
60
          if (value != NULL)
5355
60
            {
5356
60
              GVariant *child = g_variant_get_child_value (value, index++);
5357
60
              g_variant_valist_get (str, child, free, app);
5358
60
              g_variant_unref (child);
5359
60
            }
5360
0
          else
5361
0
            g_variant_valist_get (str, NULL, free, app);
5362
60
        }
5363
20
      (*str)++;
5364
20
    }
5365
80
}
5366
5367
/* User-facing API {{{2 */
5368
/**
5369
 * g_variant_new: (skip)
5370
 * @format_string: a #GVariant format string
5371
 * @...: arguments, as per @format_string
5372
 *
5373
 * Creates a new #GVariant instance.
5374
 *
5375
 * Think of this function as an analogue to g_strdup_printf().
5376
 *
5377
 * The type of the created instance and the arguments that are expected
5378
 * by this function are determined by @format_string. See the section on
5379
 * [GVariant format strings][gvariant-format-strings]. Please note that
5380
 * the syntax of the format string is very likely to be extended in the
5381
 * future.
5382
 *
5383
 * The first character of the format string must not be '*' '?' '@' or
5384
 * 'r'; in essence, a new #GVariant must always be constructed by this
5385
 * function (and not merely passed through it unmodified).
5386
 *
5387
 * Note that the arguments must be of the correct width for their types
5388
 * specified in @format_string. This can be achieved by casting them. See
5389
 * the [GVariant varargs documentation][gvariant-varargs].
5390
 *
5391
 * |[<!-- language="C" -->
5392
 * MyFlags some_flags = FLAG_ONE | FLAG_TWO;
5393
 * const gchar *some_strings[] = { "a", "b", "c", NULL };
5394
 * GVariant *new_variant;
5395
 *
5396
 * new_variant = g_variant_new ("(t^as)",
5397
 *                              // This cast is required.
5398
 *                              (guint64) some_flags,
5399
 *                              some_strings);
5400
 * ]|
5401
 *
5402
 * Returns: a new floating #GVariant instance
5403
 *
5404
 * Since: 2.24
5405
 **/
5406
GVariant *
5407
g_variant_new (const gchar *format_string,
5408
               ...)
5409
0
{
5410
0
  GVariant *value;
5411
0
  va_list ap;
5412
5413
0
  g_return_val_if_fail (valid_format_string (format_string, TRUE, NULL) &&
5414
0
                        format_string[0] != '?' && format_string[0] != '@' &&
5415
0
                        format_string[0] != '*' && format_string[0] != 'r',
5416
0
                        NULL);
5417
5418
0
  va_start (ap, format_string);
5419
0
  value = g_variant_new_va (format_string, NULL, &ap);
5420
0
  va_end (ap);
5421
5422
0
  return value;
5423
0
}
5424
5425
/**
5426
 * g_variant_new_va: (skip)
5427
 * @format_string: a string that is prefixed with a format string
5428
 * @endptr: (nullable) (default NULL): location to store the end pointer,
5429
 *          or %NULL
5430
 * @app: a pointer to a #va_list
5431
 *
5432
 * This function is intended to be used by libraries based on
5433
 * #GVariant that want to provide g_variant_new()-like functionality
5434
 * to their users.
5435
 *
5436
 * The API is more general than g_variant_new() to allow a wider range
5437
 * of possible uses.
5438
 *
5439
 * @format_string must still point to a valid format string, but it only
5440
 * needs to be nul-terminated if @endptr is %NULL.  If @endptr is
5441
 * non-%NULL then it is updated to point to the first character past the
5442
 * end of the format string.
5443
 *
5444
 * @app is a pointer to a #va_list.  The arguments, according to
5445
 * @format_string, are collected from this #va_list and the list is left
5446
 * pointing to the argument following the last.
5447
 *
5448
 * Note that the arguments in @app must be of the correct width for their
5449
 * types specified in @format_string when collected into the #va_list.
5450
 * See the [GVariant varargs documentation][gvariant-varargs].
5451
 *
5452
 * These two generalisations allow mixing of multiple calls to
5453
 * g_variant_new_va() and g_variant_get_va() within a single actual
5454
 * varargs call by the user.
5455
 *
5456
 * The return value will be floating if it was a newly created GVariant
5457
 * instance (for example, if the format string was "(ii)").  In the case
5458
 * that the format_string was '*', '?', 'r', or a format starting with
5459
 * '@' then the collected #GVariant pointer will be returned unmodified,
5460
 * without adding any additional references.
5461
 *
5462
 * In order to behave correctly in all cases it is necessary for the
5463
 * calling function to g_variant_ref_sink() the return result before
5464
 * returning control to the user that originally provided the pointer.
5465
 * At this point, the caller will have their own full reference to the
5466
 * result.  This can also be done by adding the result to a container,
5467
 * or by passing it to another g_variant_new() call.
5468
 *
5469
 * Returns: a new, usually floating, #GVariant
5470
 *
5471
 * Since: 2.24
5472
 **/
5473
GVariant *
5474
g_variant_new_va (const gchar  *format_string,
5475
                  const gchar **endptr,
5476
                  va_list      *app)
5477
0
{
5478
0
  GVariant *value;
5479
5480
0
  g_return_val_if_fail (valid_format_string (format_string, !endptr, NULL),
5481
0
                        NULL);
5482
0
  g_return_val_if_fail (app != NULL, NULL);
5483
5484
0
  value = g_variant_valist_new (&format_string, app);
5485
5486
0
  if (endptr != NULL)
5487
0
    *endptr = format_string;
5488
5489
0
  return value;
5490
0
}
5491
5492
/**
5493
 * g_variant_get: (skip)
5494
 * @value: a #GVariant instance
5495
 * @format_string: a #GVariant format string
5496
 * @...: arguments, as per @format_string
5497
 *
5498
 * Deconstructs a #GVariant instance.
5499
 *
5500
 * Think of this function as an analogue to scanf().
5501
 *
5502
 * The arguments that are expected by this function are entirely
5503
 * determined by @format_string.  @format_string also restricts the
5504
 * permissible types of @value.  It is an error to give a value with
5505
 * an incompatible type.  See the section on
5506
 * [GVariant format strings][gvariant-format-strings].
5507
 * Please note that the syntax of the format string is very likely to be
5508
 * extended in the future.
5509
 *
5510
 * @format_string determines the C types that are used for unpacking
5511
 * the values and also determines if the values are copied or borrowed,
5512
 * see the section on
5513
 * [`GVariant` format strings](gvariant-format-strings.html#pointers).
5514
 *
5515
 * Since: 2.24
5516
 **/
5517
void
5518
g_variant_get (GVariant    *value,
5519
               const gchar *format_string,
5520
               ...)
5521
20
{
5522
20
  va_list ap;
5523
5524
20
  g_return_if_fail (value != NULL);
5525
20
  g_return_if_fail (valid_format_string (format_string, TRUE, value));
5526
5527
  /* if any direct-pointer-access formats are in use, flatten first */
5528
20
  if (strchr (format_string, '&'))
5529
0
    g_variant_get_data (value);
5530
5531
20
  va_start (ap, format_string);
5532
20
  g_variant_get_va (value, format_string, NULL, &ap);
5533
20
  va_end (ap);
5534
20
}
5535
5536
/**
5537
 * g_variant_get_va: (skip)
5538
 * @value: a #GVariant
5539
 * @format_string: a string that is prefixed with a format string
5540
 * @endptr: (nullable) (default NULL): location to store the end pointer,
5541
 *          or %NULL
5542
 * @app: a pointer to a #va_list
5543
 *
5544
 * This function is intended to be used by libraries based on #GVariant
5545
 * that want to provide g_variant_get()-like functionality to their
5546
 * users.
5547
 *
5548
 * The API is more general than g_variant_get() to allow a wider range
5549
 * of possible uses.
5550
 *
5551
 * @format_string must still point to a valid format string, but it only
5552
 * need to be nul-terminated if @endptr is %NULL.  If @endptr is
5553
 * non-%NULL then it is updated to point to the first character past the
5554
 * end of the format string.
5555
 *
5556
 * @app is a pointer to a #va_list.  The arguments, according to
5557
 * @format_string, are collected from this #va_list and the list is left
5558
 * pointing to the argument following the last.
5559
 *
5560
 * These two generalisations allow mixing of multiple calls to
5561
 * g_variant_new_va() and g_variant_get_va() within a single actual
5562
 * varargs call by the user.
5563
 *
5564
 * @format_string determines the C types that are used for unpacking
5565
 * the values and also determines if the values are copied or borrowed,
5566
 * see the section on
5567
 * [`GVariant` format strings](gvariant-format-strings.html#pointers).
5568
 *
5569
 * Since: 2.24
5570
 **/
5571
void
5572
g_variant_get_va (GVariant     *value,
5573
                  const gchar  *format_string,
5574
                  const gchar **endptr,
5575
                  va_list      *app)
5576
20
{
5577
20
  g_return_if_fail (valid_format_string (format_string, !endptr, value));
5578
20
  g_return_if_fail (value != NULL);
5579
20
  g_return_if_fail (app != NULL);
5580
5581
  /* if any direct-pointer-access formats are in use, flatten first */
5582
20
  if (strchr (format_string, '&'))
5583
0
    g_variant_get_data (value);
5584
5585
20
  g_variant_valist_get (&format_string, value, FALSE, app);
5586
5587
20
  if (endptr != NULL)
5588
0
    *endptr = format_string;
5589
20
}
5590
5591
/* Varargs-enabled Utility Functions {{{1 */
5592
5593
/**
5594
 * g_variant_builder_add: (skip)
5595
 * @builder: a #GVariantBuilder
5596
 * @format_string: a #GVariant varargs format string
5597
 * @...: arguments, as per @format_string
5598
 *
5599
 * Adds to a #GVariantBuilder.
5600
 *
5601
 * This call is a convenience wrapper that is exactly equivalent to
5602
 * calling g_variant_new() followed by g_variant_builder_add_value().
5603
 *
5604
 * Note that the arguments must be of the correct width for their types
5605
 * specified in @format_string. This can be achieved by casting them. See
5606
 * the [GVariant varargs documentation][gvariant-varargs].
5607
 *
5608
 * This function might be used as follows:
5609
 *
5610
 * |[<!-- language="C" --> 
5611
 * GVariant *
5612
 * make_pointless_dictionary (void)
5613
 * {
5614
 *   GVariantBuilder builder;
5615
 *   int i;
5616
 *
5617
 *   g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
5618
 *   for (i = 0; i < 16; i++)
5619
 *     {
5620
 *       gchar buf[3];
5621
 *
5622
 *       sprintf (buf, "%d", i);
5623
 *       g_variant_builder_add (&builder, "{is}", i, buf);
5624
 *     }
5625
 *
5626
 *   return g_variant_builder_end (&builder);
5627
 * }
5628
 * ]|
5629
 *
5630
 * Since: 2.24
5631
 */
5632
void
5633
g_variant_builder_add (GVariantBuilder *builder,
5634
                       const gchar     *format_string,
5635
                       ...)
5636
0
{
5637
0
  GVariant *variant;
5638
0
  va_list ap;
5639
5640
0
  va_start (ap, format_string);
5641
0
  variant = g_variant_new_va (format_string, NULL, &ap);
5642
0
  va_end (ap);
5643
5644
0
  g_variant_builder_add_value (builder, variant);
5645
0
}
5646
5647
/**
5648
 * g_variant_get_child: (skip)
5649
 * @value: a container #GVariant
5650
 * @index_: the index of the child to deconstruct
5651
 * @format_string: a #GVariant format string
5652
 * @...: arguments, as per @format_string
5653
 *
5654
 * Reads a child item out of a container #GVariant instance and
5655
 * deconstructs it according to @format_string.  This call is
5656
 * essentially a combination of g_variant_get_child_value() and
5657
 * g_variant_get().
5658
 *
5659
 * @format_string determines the C types that are used for unpacking
5660
 * the values and also determines if the values are copied or borrowed,
5661
 * see the section on
5662
 * [`GVariant` format strings](gvariant-format-strings.html#pointers).
5663
 *
5664
 * Since: 2.24
5665
 **/
5666
void
5667
g_variant_get_child (GVariant    *value,
5668
                     gsize        index_,
5669
                     const gchar *format_string,
5670
                     ...)
5671
0
{
5672
0
  GVariant *child;
5673
0
  va_list ap;
5674
5675
  /* if any direct-pointer-access formats are in use, flatten first */
5676
0
  if (strchr (format_string, '&'))
5677
0
    g_variant_get_data (value);
5678
5679
0
  child = g_variant_get_child_value (value, index_);
5680
0
  g_return_if_fail (valid_format_string (format_string, TRUE, child));
5681
5682
0
  va_start (ap, format_string);
5683
0
  g_variant_get_va (child, format_string, NULL, &ap);
5684
0
  va_end (ap);
5685
5686
0
  g_variant_unref (child);
5687
0
}
5688
5689
/**
5690
 * g_variant_iter_next: (skip)
5691
 * @iter: a #GVariantIter
5692
 * @format_string: a GVariant format string
5693
 * @...: the arguments to unpack the value into
5694
 *
5695
 * Gets the next item in the container and unpacks it into the variable
5696
 * argument list according to @format_string, returning %TRUE.
5697
 *
5698
 * If no more items remain then %FALSE is returned.
5699
 *
5700
 * All of the pointers given on the variable arguments list of this
5701
 * function are assumed to point at uninitialised memory.  It is the
5702
 * responsibility of the caller to free all of the values returned by
5703
 * the unpacking process.
5704
 *
5705
 * Here is an example for memory management with g_variant_iter_next():
5706
 * |[<!-- language="C" --> 
5707
 *   // Iterates a dictionary of type 'a{sv}'
5708
 *   void
5709
 *   iterate_dictionary (GVariant *dictionary)
5710
 *   {
5711
 *     GVariantIter iter;
5712
 *     GVariant *value;
5713
 *     gchar *key;
5714
 *
5715
 *     g_variant_iter_init (&iter, dictionary);
5716
 *     while (g_variant_iter_next (&iter, "{sv}", &key, &value))
5717
 *       {
5718
 *         g_print ("Item '%s' has type '%s'\n", key,
5719
 *                  g_variant_get_type_string (value));
5720
 *
5721
 *         // must free data for ourselves
5722
 *         g_variant_unref (value);
5723
 *         g_free (key);
5724
 *       }
5725
 *   }
5726
 * ]|
5727
 *
5728
 * For a solution that is likely to be more convenient to C programmers
5729
 * when dealing with loops, see g_variant_iter_loop().
5730
 *
5731
 * @format_string determines the C types that are used for unpacking
5732
 * the values and also determines if the values are copied or borrowed.
5733
 *
5734
 * See the section on
5735
 * [`GVariant` format strings](gvariant-format-strings.html#pointers).
5736
 *
5737
 * Returns: %TRUE if a value was unpacked, or %FALSE if there as no value
5738
 *
5739
 * Since: 2.24
5740
 **/
5741
gboolean
5742
g_variant_iter_next (GVariantIter *iter,
5743
                     const gchar  *format_string,
5744
                     ...)
5745
0
{
5746
0
  GVariant *value;
5747
5748
0
  value = g_variant_iter_next_value (iter);
5749
5750
0
  g_return_val_if_fail (valid_format_string (format_string, TRUE, value),
5751
0
                        FALSE);
5752
5753
0
  if (value != NULL)
5754
0
    {
5755
0
      va_list ap;
5756
5757
0
      va_start (ap, format_string);
5758
0
      g_variant_valist_get (&format_string, value, FALSE, &ap);
5759
0
      va_end (ap);
5760
5761
0
      g_variant_unref (value);
5762
0
    }
5763
5764
0
  return value != NULL;
5765
0
}
5766
5767
/**
5768
 * g_variant_iter_loop: (skip)
5769
 * @iter: a #GVariantIter
5770
 * @format_string: a GVariant format string
5771
 * @...: the arguments to unpack the value into
5772
 *
5773
 * Gets the next item in the container and unpacks it into the variable
5774
 * argument list according to @format_string, returning %TRUE.
5775
 *
5776
 * If no more items remain then %FALSE is returned.
5777
 *
5778
 * On the first call to this function, the pointers appearing on the
5779
 * variable argument list are assumed to point at uninitialised memory.
5780
 * On the second and later calls, it is assumed that the same pointers
5781
 * will be given and that they will point to the memory as set by the
5782
 * previous call to this function.  This allows the previous values to
5783
 * be freed, as appropriate.
5784
 *
5785
 * This function is intended to be used with a while loop as
5786
 * demonstrated in the following example.  This function can only be
5787
 * used when iterating over an array.  It is only valid to call this
5788
 * function with a string constant for the format string and the same
5789
 * string constant must be used each time.  Mixing calls to this
5790
 * function and g_variant_iter_next() or g_variant_iter_next_value() on
5791
 * the same iterator causes undefined behavior.
5792
 *
5793
 * If you break out of a such a while loop using g_variant_iter_loop() then
5794
 * you must free or unreference all the unpacked values as you would with
5795
 * g_variant_get(). Failure to do so will cause a memory leak.
5796
 *
5797
 * Here is an example for memory management with g_variant_iter_loop():
5798
 * |[<!-- language="C" --> 
5799
 *   // Iterates a dictionary of type 'a{sv}'
5800
 *   void
5801
 *   iterate_dictionary (GVariant *dictionary)
5802
 *   {
5803
 *     GVariantIter iter;
5804
 *     GVariant *value;
5805
 *     gchar *key;
5806
 *
5807
 *     g_variant_iter_init (&iter, dictionary);
5808
 *     while (g_variant_iter_loop (&iter, "{sv}", &key, &value))
5809
 *       {
5810
 *         g_print ("Item '%s' has type '%s'\n", key,
5811
 *                  g_variant_get_type_string (value));
5812
 *
5813
 *         // no need to free 'key' and 'value' here
5814
 *         // unless breaking out of this loop
5815
 *       }
5816
 *   }
5817
 * ]|
5818
 *
5819
 * For most cases you should use g_variant_iter_next().
5820
 *
5821
 * This function is really only useful when unpacking into #GVariant or
5822
 * #GVariantIter in order to allow you to skip the call to
5823
 * g_variant_unref() or g_variant_iter_free().
5824
 *
5825
 * For example, if you are only looping over simple integer and string
5826
 * types, g_variant_iter_next() is definitely preferred.  For string
5827
 * types, use the '&' prefix to avoid allocating any memory at all (and
5828
 * thereby avoiding the need to free anything as well).
5829
 *
5830
 * @format_string determines the C types that are used for unpacking
5831
 * the values and also determines if the values are copied or borrowed.
5832
 *
5833
 * See the section on
5834
 * [`GVariant` format strings](gvariant-format-strings.html#pointers).
5835
 *
5836
 * Returns: %TRUE if a value was unpacked, or %FALSE if there was no
5837
 *          value
5838
 *
5839
 * Since: 2.24
5840
 **/
5841
gboolean
5842
g_variant_iter_loop (GVariantIter *iter,
5843
                     const gchar  *format_string,
5844
                     ...)
5845
0
{
5846
0
  gboolean first_time = GVSI(iter)->loop_format == NULL;
5847
0
  GVariant *value;
5848
0
  va_list ap;
5849
5850
0
  g_return_val_if_fail (first_time ||
5851
0
                        format_string == GVSI(iter)->loop_format,
5852
0
                        FALSE);
5853
5854
0
  if (first_time)
5855
0
    {
5856
0
      TYPE_CHECK (GVSI(iter)->value, G_VARIANT_TYPE_ARRAY, FALSE);
5857
0
      GVSI(iter)->loop_format = format_string;
5858
5859
0
      if (strchr (format_string, '&'))
5860
0
        g_variant_get_data (GVSI(iter)->value);
5861
0
    }
5862
5863
0
  value = g_variant_iter_next_value (iter);
5864
5865
0
  g_return_val_if_fail (!first_time ||
5866
0
                        valid_format_string (format_string, TRUE, value),
5867
0
                        FALSE);
5868
5869
0
  va_start (ap, format_string);
5870
0
  g_variant_valist_get (&format_string, value, !first_time, &ap);
5871
0
  va_end (ap);
5872
5873
0
  if (value != NULL)
5874
0
    g_variant_unref (value);
5875
5876
0
  return value != NULL;
5877
0
}
5878
5879
/* Serialized data {{{1 */
5880
static GVariant *
5881
g_variant_deep_copy (GVariant *value,
5882
                     gboolean  byteswap)
5883
0
{
5884
0
  switch (g_variant_classify (value))
5885
0
    {
5886
0
    case G_VARIANT_CLASS_MAYBE:
5887
0
    case G_VARIANT_CLASS_TUPLE:
5888
0
    case G_VARIANT_CLASS_DICT_ENTRY:
5889
0
    case G_VARIANT_CLASS_VARIANT:
5890
0
      {
5891
0
        GVariantBuilder builder;
5892
0
        gsize i, n_children;
5893
5894
0
        g_variant_builder_init (&builder, g_variant_get_type (value));
5895
5896
0
        for (i = 0, n_children = g_variant_n_children (value); i < n_children; i++)
5897
0
          {
5898
0
            GVariant *child = g_variant_get_child_value (value, i);
5899
0
            g_variant_builder_add_value (&builder, g_variant_deep_copy (child, byteswap));
5900
0
            g_variant_unref (child);
5901
0
          }
5902
5903
0
        return g_variant_builder_end (&builder);
5904
0
      }
5905
5906
0
    case G_VARIANT_CLASS_ARRAY:
5907
0
      {
5908
0
        GVariantBuilder builder;
5909
0
        gsize i, n_children;
5910
0
        GVariant *first_invalid_child_deep_copy = NULL;
5911
5912
        /* Arrays are in theory treated the same as maybes, tuples, dict entries
5913
         * and variants, and could be another case in the above block of code.
5914
         *
5915
         * However, they have the property that when dealing with non-normal
5916
         * data (which is the only time g_variant_deep_copy() is currently
5917
         * called) in a variable-sized array, the code above can easily end up
5918
         * creating many default child values in order to return an array which
5919
         * is of the right length and type, but without containing non-normal
5920
         * data. This can happen if the offset table for the array is malformed.
5921
         *
5922
         * In this case, the code above would end up allocating the same default
5923
         * value for each one of the child indexes beyond the first malformed
5924
         * entry in the offset table. This can end up being a lot of identical
5925
         * allocations of default values, particularly if the non-normal array
5926
         * is crafted maliciously.
5927
         *
5928
         * Avoid that problem by returning a new reference to the same default
5929
         * value for every child after the first invalid one. This results in
5930
         * returning an equivalent array, in normal form and trusted — but with
5931
         * significantly fewer memory allocations.
5932
         *
5933
         * See https://gitlab.gnome.org/GNOME/glib/-/issues/2540 */
5934
5935
0
        g_variant_builder_init (&builder, g_variant_get_type (value));
5936
5937
0
        for (i = 0, n_children = g_variant_n_children (value); i < n_children; i++)
5938
0
          {
5939
            /* Try maybe_get_child_value() first; if it returns NULL, this child
5940
             * is non-normal. get_child_value() would have constructed and
5941
             * returned a default value in that case. */
5942
0
            GVariant *child = g_variant_maybe_get_child_value (value, i);
5943
5944
0
            if (child != NULL)
5945
0
              {
5946
                /* Non-normal children may not always be contiguous, as they may
5947
                 * be non-normal for reasons other than invalid offset table
5948
                 * entries. As they are all the same type, they will all have
5949
                 * the same default value though, so keep that around. */
5950
0
                g_variant_builder_add_value (&builder, g_variant_deep_copy (child, byteswap));
5951
0
              }
5952
0
            else if (child == NULL && first_invalid_child_deep_copy != NULL)
5953
0
              {
5954
0
                g_variant_builder_add_value (&builder, first_invalid_child_deep_copy);
5955
0
              }
5956
0
            else if (child == NULL)
5957
0
              {
5958
0
                child = g_variant_get_child_value (value, i);
5959
0
                first_invalid_child_deep_copy = g_variant_ref_sink (g_variant_deep_copy (child, byteswap));
5960
0
                g_variant_builder_add_value (&builder, first_invalid_child_deep_copy);
5961
0
              }
5962
5963
0
            g_clear_pointer (&child, g_variant_unref);
5964
0
          }
5965
5966
0
        g_clear_pointer (&first_invalid_child_deep_copy, g_variant_unref);
5967
5968
0
        return g_variant_builder_end (&builder);
5969
0
      }
5970
5971
0
    case G_VARIANT_CLASS_BOOLEAN:
5972
0
      return g_variant_new_boolean (g_variant_get_boolean (value));
5973
5974
0
    case G_VARIANT_CLASS_BYTE:
5975
0
      return g_variant_new_byte (g_variant_get_byte (value));
5976
5977
0
    case G_VARIANT_CLASS_INT16:
5978
0
      if (byteswap)
5979
0
        return g_variant_new_int16 (GUINT16_SWAP_LE_BE (g_variant_get_int16 (value)));
5980
0
      else
5981
0
        return g_variant_new_int16 (g_variant_get_int16 (value));
5982
5983
0
    case G_VARIANT_CLASS_UINT16:
5984
0
      if (byteswap)
5985
0
        return g_variant_new_uint16 (GUINT16_SWAP_LE_BE (g_variant_get_uint16 (value)));
5986
0
      else
5987
0
        return g_variant_new_uint16 (g_variant_get_uint16 (value));
5988
5989
0
    case G_VARIANT_CLASS_INT32:
5990
0
      if (byteswap)
5991
0
        return g_variant_new_int32 (GUINT32_SWAP_LE_BE (g_variant_get_int32 (value)));
5992
0
      else
5993
0
        return g_variant_new_int32 (g_variant_get_int32 (value));
5994
5995
0
    case G_VARIANT_CLASS_UINT32:
5996
0
      if (byteswap)
5997
0
        return g_variant_new_uint32 (GUINT32_SWAP_LE_BE (g_variant_get_uint32 (value)));
5998
0
      else
5999
0
        return g_variant_new_uint32 (g_variant_get_uint32 (value));
6000
6001
0
    case G_VARIANT_CLASS_INT64:
6002
0
      if (byteswap)
6003
0
        return g_variant_new_int64 (GUINT64_SWAP_LE_BE (g_variant_get_int64 (value)));
6004
0
      else
6005
0
        return g_variant_new_int64 (g_variant_get_int64 (value));
6006
6007
0
    case G_VARIANT_CLASS_UINT64:
6008
0
      if (byteswap)
6009
0
        return g_variant_new_uint64 (GUINT64_SWAP_LE_BE (g_variant_get_uint64 (value)));
6010
0
      else
6011
0
        return g_variant_new_uint64 (g_variant_get_uint64 (value));
6012
6013
0
    case G_VARIANT_CLASS_HANDLE:
6014
0
      if (byteswap)
6015
0
        return g_variant_new_handle (GUINT32_SWAP_LE_BE (g_variant_get_handle (value)));
6016
0
      else
6017
0
        return g_variant_new_handle (g_variant_get_handle (value));
6018
6019
0
    case G_VARIANT_CLASS_DOUBLE:
6020
0
      if (byteswap)
6021
0
        {
6022
          /* We have to convert the double to a uint64 here using a union,
6023
           * because a cast will round it numerically. */
6024
0
          union
6025
0
            {
6026
0
              guint64 u64;
6027
0
              gdouble dbl;
6028
0
            } u1, u2;
6029
0
          u1.dbl = g_variant_get_double (value);
6030
0
          u2.u64 = GUINT64_SWAP_LE_BE (u1.u64);
6031
0
          return g_variant_new_double (u2.dbl);
6032
0
        }
6033
0
      else
6034
0
        return g_variant_new_double (g_variant_get_double (value));
6035
6036
0
    case G_VARIANT_CLASS_STRING:
6037
0
      return g_variant_new_string (g_variant_get_string (value, NULL));
6038
6039
0
    case G_VARIANT_CLASS_OBJECT_PATH:
6040
0
      return g_variant_new_object_path (g_variant_get_string (value, NULL));
6041
6042
0
    case G_VARIANT_CLASS_SIGNATURE:
6043
0
      return g_variant_new_signature (g_variant_get_string (value, NULL));
6044
0
    }
6045
6046
0
  g_assert_not_reached ();
6047
0
}
6048
6049
/**
6050
 * g_variant_get_normal_form:
6051
 * @value: a #GVariant
6052
 *
6053
 * Gets a #GVariant instance that has the same value as @value and is
6054
 * trusted to be in normal form.
6055
 *
6056
 * If @value is already trusted to be in normal form then a new
6057
 * reference to @value is returned.
6058
 *
6059
 * If @value is not already trusted, then it is scanned to check if it
6060
 * is in normal form.  If it is found to be in normal form then it is
6061
 * marked as trusted and a new reference to it is returned.
6062
 *
6063
 * If @value is found not to be in normal form then a new trusted
6064
 * #GVariant is created with the same value as @value. The non-normal parts of
6065
 * @value will be replaced with default values which are guaranteed to be in
6066
 * normal form.
6067
 *
6068
 * It makes sense to call this function if you've received #GVariant
6069
 * data from untrusted sources and you want to ensure your serialized
6070
 * output is definitely in normal form.
6071
 *
6072
 * If @value is already in normal form, a new reference will be returned
6073
 * (which will be floating if @value is floating). If it is not in normal form,
6074
 * the newly created #GVariant will be returned with a single non-floating
6075
 * reference. Typically, g_variant_take_ref() should be called on the return
6076
 * value from this function to guarantee ownership of a single non-floating
6077
 * reference to it.
6078
 *
6079
 * Returns: (transfer full): a trusted #GVariant
6080
 *
6081
 * Since: 2.24
6082
 **/
6083
GVariant *
6084
g_variant_get_normal_form (GVariant *value)
6085
0
{
6086
0
  GVariant *trusted;
6087
6088
0
  if (g_variant_is_normal_form (value))
6089
0
    return g_variant_ref (value);
6090
6091
0
  trusted = g_variant_deep_copy (value, FALSE);
6092
0
  g_assert (g_variant_is_trusted (trusted));
6093
6094
0
  return g_variant_ref_sink (trusted);
6095
0
}
6096
6097
/**
6098
 * g_variant_byteswap:
6099
 * @value: a #GVariant
6100
 *
6101
 * Performs a byteswapping operation on the contents of @value.  The
6102
 * result is that all multi-byte numeric data contained in @value is
6103
 * byteswapped.  That includes 16, 32, and 64bit signed and unsigned
6104
 * integers as well as file handles and double precision floating point
6105
 * values.
6106
 *
6107
 * This function is an identity mapping on any value that does not
6108
 * contain multi-byte numeric data.  That include strings, booleans,
6109
 * bytes and containers containing only these things (recursively).
6110
 *
6111
 * While this function can safely handle untrusted, non-normal data, it is
6112
 * recommended to check whether the input is in normal form beforehand, using
6113
 * g_variant_is_normal_form(), and to reject non-normal inputs if your
6114
 * application can be strict about what inputs it rejects.
6115
 *
6116
 * The returned value is always in normal form and is marked as trusted.
6117
 * A full, not floating, reference is returned.
6118
 *
6119
 * Returns: (transfer full): the byteswapped form of @value
6120
 *
6121
 * Since: 2.24
6122
 **/
6123
GVariant *
6124
g_variant_byteswap (GVariant *value)
6125
0
{
6126
0
  GVariantTypeInfo *type_info;
6127
0
  guint alignment;
6128
0
  GVariant *new;
6129
6130
0
  type_info = g_variant_get_type_info (value);
6131
6132
0
  g_variant_type_info_query (type_info, &alignment, NULL);
6133
6134
0
  if (alignment && g_variant_is_normal_form (value))
6135
0
    {
6136
      /* (potentially) contains multi-byte numeric data, but is also already in
6137
       * normal form so we can use a faster byteswapping codepath on the
6138
       * serialised data */
6139
0
      GVariantSerialised serialised = { 0, };
6140
0
      GBytes *bytes;
6141
6142
0
      serialised.type_info = g_variant_get_type_info (value);
6143
0
      serialised.size = g_variant_get_size (value);
6144
0
      serialised.data = g_malloc (serialised.size);
6145
0
      serialised.depth = g_variant_get_depth (value);
6146
0
      serialised.ordered_offsets_up_to = G_MAXSIZE;  /* operating on the normal form */
6147
0
      serialised.checked_offsets_up_to = G_MAXSIZE;
6148
0
      g_variant_store (value, serialised.data);
6149
6150
0
      g_variant_serialised_byteswap (serialised);
6151
6152
0
      bytes = g_bytes_new_take (serialised.data, serialised.size);
6153
0
      new = g_variant_ref_sink (g_variant_new_from_bytes (g_variant_get_type (value), bytes, TRUE));
6154
0
      g_bytes_unref (bytes);
6155
0
    }
6156
0
  else if (alignment)
6157
    /* (potentially) contains multi-byte numeric data */
6158
0
    new = g_variant_ref_sink (g_variant_deep_copy (value, TRUE));
6159
0
  else
6160
    /* contains no multi-byte data */
6161
0
    new = g_variant_get_normal_form (value);
6162
6163
0
  g_assert (g_variant_is_trusted (new));
6164
6165
0
  return g_steal_pointer (&new);
6166
0
}
6167
6168
/**
6169
 * g_variant_new_from_data:
6170
 * @type: a definite #GVariantType
6171
 * @data: (array length=size) (element-type guint8): the serialized data
6172
 * @size: the size of @data
6173
 * @trusted: %TRUE if @data is definitely in normal form
6174
 * @notify: (scope async): function to call when @data is no longer needed
6175
 * @user_data: data for @notify
6176
 *
6177
 * Creates a new #GVariant instance from serialized data.
6178
 *
6179
 * @type is the type of #GVariant instance that will be constructed.
6180
 * The interpretation of @data depends on knowing the type.
6181
 *
6182
 * @data is not modified by this function and must remain valid with an
6183
 * unchanging value until such a time as @notify is called with
6184
 * @user_data.  If the contents of @data change before that time then
6185
 * the result is undefined.
6186
 *
6187
 * If @data is trusted to be serialized data in normal form then
6188
 * @trusted should be %TRUE.  This applies to serialized data created
6189
 * within this process or read from a trusted location on the disk (such
6190
 * as a file installed in /usr/lib alongside your application).  You
6191
 * should set trusted to %FALSE if @data is read from the network, a
6192
 * file in the user's home directory, etc.
6193
 *
6194
 * If @data was not stored in this machine's native endianness, any multi-byte
6195
 * numeric values in the returned variant will also be in non-native
6196
 * endianness. g_variant_byteswap() can be used to recover the original values.
6197
 *
6198
 * @notify will be called with @user_data when @data is no longer
6199
 * needed.  The exact time of this call is unspecified and might even be
6200
 * before this function returns.
6201
 *
6202
 * Note: @data must be backed by memory that is aligned appropriately for the
6203
 * @type being loaded. Otherwise this function will internally create a copy of
6204
 * the memory (since GLib 2.60) or (in older versions) fail and exit the
6205
 * process.
6206
 *
6207
 * Returns: (transfer none): a new floating #GVariant of type @type
6208
 *
6209
 * Since: 2.24
6210
 **/
6211
GVariant *
6212
g_variant_new_from_data (const GVariantType *type,
6213
                         gconstpointer       data,
6214
                         gsize               size,
6215
                         gboolean            trusted,
6216
                         GDestroyNotify      notify,
6217
                         gpointer            user_data)
6218
0
{
6219
0
  GVariant *value;
6220
0
  GBytes *bytes;
6221
6222
0
  g_return_val_if_fail (g_variant_type_is_definite (type), NULL);
6223
0
  g_return_val_if_fail (data != NULL || size == 0, NULL);
6224
6225
0
  if (notify)
6226
0
    bytes = g_bytes_new_with_free_func (data, size, notify, user_data);
6227
0
  else
6228
0
    bytes = g_bytes_new_static (data, size);
6229
6230
0
  value = g_variant_new_from_bytes (type, bytes, trusted);
6231
0
  g_bytes_unref (bytes);
6232
6233
0
  return value;
6234
0
}
6235
6236
/* Epilogue {{{1 */
6237
/* vim:set foldmethod=marker: */