Coverage Report

Created: 2025-11-16 07:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/glib-2.80.0/glib/gvariant-core.c
Line
Count
Source
1
/*
2
 * Copyright © 2007, 2008 Ryan Lortie
3
 * Copyright © 2010 Codethink Limited
4
 * Copyright © 2022 Endless OS Foundation, LLC
5
 *
6
 * SPDX-License-Identifier: LGPL-2.1-or-later
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "config.h"
23
24
#include <glib/gvariant-core.h>
25
26
#include <glib/gvariant-internal.h>
27
#include <glib/gvariant-serialiser.h>
28
#include <glib/gtestutils.h>
29
#include <glib/gbitlock.h>
30
#include <glib/gatomic.h>
31
#include <glib/gbytes.h>
32
#include <glib/gslice.h>
33
#include <glib/gmem.h>
34
#include <glib/grefcount.h>
35
#include <string.h>
36
37
#include "glib_trace.h"
38
39
/*
40
 * This file includes the structure definition for GVariant and a small
41
 * set of functions that are allowed to access the structure directly.
42
 *
43
 * This minimises the amount of code that can possibly touch a GVariant
44
 * structure directly to a few simple fundamental operations.  These few
45
 * operations are written to be completely threadsafe with respect to
46
 * all possible outside access.  This means that we only need to be
47
 * concerned about thread safety issues in this one small file.
48
 *
49
 * Most GVariant API functions are in gvariant.c.
50
 */
51
52
struct _GVariant
53
/* see below for field member documentation */
54
{
55
  GVariantTypeInfo *type_info;
56
  gsize size;
57
58
  union
59
  {
60
    struct
61
    {
62
      GBytes *bytes;
63
      gconstpointer data;
64
      gsize ordered_offsets_up_to;
65
      gsize checked_offsets_up_to;
66
    } serialised;
67
68
    struct
69
    {
70
      GVariant **children;
71
      gsize n_children;
72
    } tree;
73
  } contents;
74
75
  gint state;
76
  gatomicrefcount ref_count;
77
  gsize depth;
78
};
79
80
/* struct GVariant:
81
 *
82
 * There are two primary forms of GVariant instances: "serialized form"
83
 * and "tree form".
84
 *
85
 * "serialized form": A serialized GVariant instance stores its value in
86
 *                    the GVariant serialization format.  All
87
 *                    basic-typed instances (ie: non-containers) are in
88
 *                    serialized format, as are some containers.
89
 *
90
 * "tree form": Some containers are in "tree form".  In this case,
91
 *              instead of containing the serialized data for the
92
 *              container, the instance contains an array of pointers to
93
 *              the child values of the container (thus forming a tree).
94
 *
95
 * It is possible for an instance to transition from tree form to
96
 * serialized form.  This happens, implicitly, if the serialized data is
97
 * requested (eg: via g_variant_get_data()).  Serialized form instances
98
 * never transition into tree form.
99
 *
100
 *
101
 * The fields of the structure are documented here:
102
 *
103
 * type_info: this is a reference to a GVariantTypeInfo describing the
104
 *            type of the instance.  When the instance is freed, this
105
 *            reference must be released with g_variant_type_info_unref().
106
 *
107
 *            The type_info field never changes during the life of the
108
 *            instance, so it can be accessed without a lock.
109
 *
110
 * size: this is the size of the serialized form for the instance, if it
111
 *       is known.  If the instance is in serialized form then it is, by
112
 *       definition, known.  If the instance is in tree form then it may
113
 *       be unknown (in which case it is -1).  It is possible for the
114
 *       size to be known when in tree form if, for example, the user
115
 *       has called g_variant_get_size() without calling
116
 *       g_variant_get_data().  Additionally, even when the user calls
117
 *       g_variant_get_data() the size of the data must first be
118
 *       determined so that a large enough buffer can be allocated for
119
 *       the data.
120
 *
121
 *       Once the size is known, it can never become unknown again.
122
 *       g_variant_ensure_size() is used to ensure that the size is in
123
 *       the known state -- it calculates the size if needed.  After
124
 *       that, the size field can be accessed without a lock.
125
 *
126
 * contents: a union containing either the information associated with
127
 *           holding a value in serialized form or holding a value in
128
 *           tree form.
129
 *
130
 *   .serialised: Only valid when the instance is in serialized form.
131
 *
132
 *                Since an instance can never transition away from
133
 *                serialized form, once these fields are set, they will
134
 *                never be changed.  It is therefore valid to access
135
 *                them without holding a lock.
136
 *
137
 *     .bytes:  the #GBytes that contains the memory pointed to by
138
 *              .data, or %NULL if .data is %NULL.  In the event that
139
 *              the instance was deserialized from another instance,
140
 *              then the bytes will be shared by both of them.  When
141
 *              the instance is freed, this reference must be released
142
 *              with g_bytes_unref().
143
 *
144
 *     .data: the serialized data (of size 'size') of the instance.
145
 *            This pointer should not be freed or modified in any way.
146
 *            #GBytes is responsible for memory management.
147
 *
148
 *            This pointer may be %NULL in two cases:
149
 *
150
 *              - if the serialized size of the instance is 0
151
 *
152
 *              - if the instance is of a fixed-sized type and was
153
 *                deserialized out of a corrupted container such that
154
 *                the container contains too few bytes to point to the
155
 *                entire proper fixed-size of this instance.  In this
156
 *                case, 'size' will still be equal to the proper fixed
157
 *                size, but this pointer will be %NULL.  This is exactly
158
 *                the reason that g_variant_get_data() sometimes returns
159
 *                %NULL.  For all other calls, the effect should be as
160
 *                if .data pointed to the appropriate number of nul
161
 *                bytes.
162
 *
163
 *     .ordered_offsets_up_to: If ordered_offsets_up_to == n this means that all
164
 *                             the frame offsets up to and including the frame
165
 *                             offset determining the end of element n are in
166
 *                             order. This guarantees that the bytes of element
167
 *                             n don't overlap with any previous element.
168
 *
169
 *                             For trusted data this is set to G_MAXSIZE and we
170
 *                             don't check that the frame offsets are in order.
171
 *
172
 *                             Note: This doesn't imply the offsets are good in
173
 *                             any way apart from their ordering.  In particular
174
 *                             offsets may be out of bounds for this value or
175
 *                             may imply that the data overlaps the frame
176
 *                             offsets themselves.
177
 *
178
 *                             This field is only relevant for arrays of non
179
 *                             fixed width types and for tuples.
180
 *
181
 *     .checked_offsets_up_to: Similarly to .ordered_offsets_up_to, this stores
182
 *                             the index of the highest element, n, whose frame
183
 *                             offsets (and all the preceding frame offsets)
184
 *                             have been checked for validity.
185
 *
186
 *                             It is always the case that
187
 *                             .checked_offsets_up_to ≥ .ordered_offsets_up_to.
188
 *
189
 *                             If .checked_offsets_up_to == .ordered_offsets_up_to,
190
 *                             then a bad offset has not been found so far.
191
 *
192
 *                             If .checked_offsets_up_to > .ordered_offsets_up_to,
193
 *                             then a bad offset has been found at
194
 *                             (.ordered_offsets_up_to + 1).
195
 *
196
 *                             This field is only relevant for arrays of non
197
 *                             fixed width types and for tuples.
198
 *
199
 *   .tree: Only valid when the instance is in tree form.
200
 *
201
 *          Note that accesses from other threads could result in
202
 *          conversion of the instance from tree form to serialized form
203
 *          at any time.  For this reason, the instance lock must always
204
 *          be held while performing any operations on 'contents.tree'.
205
 *
206
 *     .children: the array of the child instances of this instance.
207
 *                When the instance is freed (or converted to serialized
208
 *                form) then each child must have g_variant_unref()
209
 *                called on it and the array must be freed using
210
 *                g_free().
211
 *
212
 *     .n_children: the number of items in the .children array.
213
 *
214
 * state: a bitfield describing the state of the instance.  It is a
215
 *        bitwise-or of the following STATE_* constants:
216
 *
217
 *    STATE_LOCKED: the instance lock is held.  This is the bit used by
218
 *                  g_bit_lock().
219
 *
220
 *    STATE_SERIALISED: the instance is in serialized form.  If this
221
 *                      flag is not set then the instance is in tree
222
 *                      form.
223
 *
224
 *    STATE_TRUSTED: for serialized form instances, this means that the
225
 *                   serialized data is known to be in normal form (ie:
226
 *                   not corrupted).
227
 *
228
 *                   For tree form instances, this means that all of the
229
 *                   child instances in the contents.tree.children array
230
 *                   are trusted.  This means that if the container is
231
 *                   serialized then the resulting data will be in
232
 *                   normal form.
233
 *
234
 *                   If this flag is unset it does not imply that the
235
 *                   data is corrupted.  It merely means that we're not
236
 *                   sure that it's valid.  See g_variant_is_trusted().
237
 *
238
 *    STATE_FLOATING: if this flag is set then the object has a floating
239
 *                    reference.  See g_variant_ref_sink().
240
 *
241
 * ref_count: the reference count of the instance
242
 *
243
 * depth: the depth of the GVariant in a hierarchy of nested containers,
244
 *        increasing with the level of nesting. The top-most GVariant has depth
245
 *        zero.  This is used to avoid recursing too deeply and overflowing the
246
 *        stack when handling deeply nested untrusted serialized GVariants.
247
 */
248
0
#define STATE_LOCKED     1
249
0
#define STATE_SERIALISED 2
250
0
#define STATE_TRUSTED    4
251
0
#define STATE_FLOATING   8
252
253
/* -- private -- */
254
/* < private >
255
 * g_variant_lock:
256
 * @value: a #GVariant
257
 *
258
 * Locks @value for performing sensitive operations.
259
 */
260
static void
261
g_variant_lock (GVariant *value)
262
0
{
263
0
  g_bit_lock (&value->state, 0);
264
0
}
265
266
/* < private >
267
 * g_variant_unlock:
268
 * @value: a #GVariant
269
 *
270
 * Unlocks @value after performing sensitive operations.
271
 */
272
static void
273
g_variant_unlock (GVariant *value)
274
0
{
275
0
  g_bit_unlock (&value->state, 0);
276
0
}
277
278
/* < private >
279
 * g_variant_release_children:
280
 * @value: a #GVariant
281
 *
282
 * Releases the reference held on each child in the 'children' array of
283
 * @value and frees the array itself.  @value must be in tree form.
284
 *
285
 * This is done when freeing a tree-form instance or converting it to
286
 * serialized form.
287
 *
288
 * The current thread must hold the lock on @value.
289
 */
290
static void
291
g_variant_release_children (GVariant *value)
292
0
{
293
0
  gsize i;
294
295
0
  g_assert (value->state & STATE_LOCKED);
296
0
  g_assert (~value->state & STATE_SERIALISED);
297
298
0
  for (i = 0; i < value->contents.tree.n_children; i++)
299
0
    g_variant_unref (value->contents.tree.children[i]);
300
301
0
  g_free (value->contents.tree.children);
302
0
}
303
304
/* This begins the main body of the recursive serializer.
305
 *
306
 * There are 3 functions here that work as a team with the serializer to
307
 * get things done.  g_variant_store() has a trivial role, but as a
308
 * public API function, it has its definition elsewhere.
309
 *
310
 * Note that "serialization" of an instance does not mean that the
311
 * instance is converted to serialized form -- it means that the
312
 * serialized form of an instance is written to an external buffer.
313
 * g_variant_ensure_serialised() (which is not part of this set of
314
 * functions) is the function that is responsible for converting an
315
 * instance to serialized form.
316
 *
317
 * We are only concerned here with container types since non-container
318
 * instances are always in serialized form.  For these instances,
319
 * storing their serialized form merely involves a memcpy().
320
 *
321
 * Serialization is a two-step process.  First, the size of the
322
 * serialized data must be calculated so that an appropriately-sized
323
 * buffer can be allocated.  Second, the data is written into the
324
 * buffer.
325
 *
326
 * Determining the size:
327
 *   The process of determining the size is triggered by a call to
328
 *   g_variant_ensure_size() on a container.  This invokes the
329
 *   serializer code to determine the size.  The serializer is passed
330
 *   g_variant_fill_gvs() as a callback.
331
 *
332
 *   g_variant_fill_gvs() is called by the serializer on each child of
333
 *   the container which, in turn, calls g_variant_ensure_size() on
334
 *   itself and fills in the result of its own size calculation.
335
 *
336
 *   The serializer uses the size information from the children to
337
 *   calculate the size needed for the entire container.
338
 *
339
 * Writing the data:
340
 *   After the buffer has been allocated, g_variant_serialise() is
341
 *   called on the container.  This invokes the serializer code to write
342
 *   the bytes to the container.  The serializer is, again, passed
343
 *   g_variant_fill_gvs() as a callback.
344
 *
345
 *   This time, when g_variant_fill_gvs() is called for each child, the
346
 *   child is given a pointer to a sub-region of the allocated buffer
347
 *   where it should write its data.  This is done by calling
348
 *   g_variant_store().  In the event that the instance is in serialized
349
 *   form this means a memcpy() of the serialized data into the
350
 *   allocated buffer.  In the event that the instance is in tree form
351
 *   this means a recursive call back into g_variant_serialise().
352
 *
353
 *
354
 * The forward declaration here allows corecursion via callback:
355
 */
356
static void g_variant_fill_gvs (GVariantSerialised *, gpointer);
357
358
/* < private >
359
 * g_variant_ensure_size:
360
 * @value: a #GVariant
361
 *
362
 * Ensures that the ->size field of @value is filled in properly.  This
363
 * must be done as a precursor to any serialization of the value in
364
 * order to know how large of a buffer is needed to store the data.
365
 *
366
 * The current thread must hold the lock on @value.
367
 */
368
static void
369
g_variant_ensure_size (GVariant *value)
370
0
{
371
0
  g_assert (value->state & STATE_LOCKED);
372
373
0
  if (value->size == (gsize) -1)
374
0
    {
375
0
      gpointer *children;
376
0
      gsize n_children;
377
378
0
      children = (gpointer *) value->contents.tree.children;
379
0
      n_children = value->contents.tree.n_children;
380
0
      value->size = g_variant_serialiser_needed_size (value->type_info,
381
0
                                                      g_variant_fill_gvs,
382
0
                                                      children, n_children);
383
0
    }
384
0
}
385
386
/* < private >
387
 * g_variant_to_serialised:
388
 * @value: a #GVariant
389
 *
390
 * Gets a GVariantSerialised for a GVariant in state STATE_SERIALISED.
391
 */
392
inline static GVariantSerialised
393
g_variant_to_serialised (GVariant *value)
394
0
{
395
0
  g_assert (value->state & STATE_SERIALISED);
396
0
  {
397
0
    GVariantSerialised serialised = {
398
0
      value->type_info,
399
0
      (gpointer) value->contents.serialised.data,
400
0
      value->size,
401
0
      value->depth,
402
0
      value->contents.serialised.ordered_offsets_up_to,
403
0
      value->contents.serialised.checked_offsets_up_to,
404
0
    };
405
0
    return serialised;
406
0
  }
407
0
}
408
409
/* < private >
410
 * g_variant_serialise:
411
 * @value: a #GVariant
412
 * @data: an appropriately-sized buffer
413
 *
414
 * Serializes @value into @data.  @value must be in tree form.
415
 *
416
 * No change is made to @value.
417
 *
418
 * The current thread must hold the lock on @value.
419
 */
420
static void
421
g_variant_serialise (GVariant *value,
422
                     gpointer  data)
423
0
{
424
0
  GVariantSerialised serialised = { 0, };
425
0
  gpointer *children;
426
0
  gsize n_children;
427
428
0
  g_assert (~value->state & STATE_SERIALISED);
429
0
  g_assert (value->state & STATE_LOCKED);
430
431
0
  serialised.type_info = value->type_info;
432
0
  serialised.size = value->size;
433
0
  serialised.data = data;
434
0
  serialised.depth = value->depth;
435
0
  serialised.ordered_offsets_up_to = 0;
436
0
  serialised.checked_offsets_up_to = 0;
437
438
0
  children = (gpointer *) value->contents.tree.children;
439
0
  n_children = value->contents.tree.n_children;
440
441
0
  g_variant_serialiser_serialise (serialised, g_variant_fill_gvs,
442
0
                                  children, n_children);
443
0
}
444
445
/* < private >
446
 * g_variant_fill_gvs:
447
 * @serialised: a pointer to a #GVariantSerialised
448
 * @data: a #GVariant instance
449
 *
450
 * This is the callback that is passed by a tree-form container instance
451
 * to the serializer.  This callback gets called on each child of the
452
 * container.  Each child is responsible for performing the following
453
 * actions:
454
 *
455
 *  - reporting its type
456
 *
457
 *  - reporting its serialized size (requires knowing the size first)
458
 *
459
 *  - possibly storing its serialized form into the provided buffer
460
 */
461
static void
462
g_variant_fill_gvs (GVariantSerialised *serialised,
463
                    gpointer            data)
464
0
{
465
0
  GVariant *value = data;
466
467
0
  g_variant_lock (value);
468
0
  g_variant_ensure_size (value);
469
0
  g_variant_unlock (value);
470
471
0
  if (serialised->type_info == NULL)
472
0
    serialised->type_info = value->type_info;
473
0
  g_assert (serialised->type_info == value->type_info);
474
475
0
  if (serialised->size == 0)
476
0
    serialised->size = value->size;
477
0
  g_assert (serialised->size == value->size);
478
0
  serialised->depth = value->depth;
479
480
0
  if (value->state & STATE_SERIALISED)
481
0
    {
482
0
      serialised->ordered_offsets_up_to = value->contents.serialised.ordered_offsets_up_to;
483
0
      serialised->checked_offsets_up_to = value->contents.serialised.checked_offsets_up_to;
484
0
    }
485
0
  else
486
0
    {
487
0
      serialised->ordered_offsets_up_to = 0;
488
0
      serialised->checked_offsets_up_to = 0;
489
0
    }
490
491
0
  if (serialised->data)
492
    /* g_variant_store() is a public API, so it
493
     * it will reacquire the lock if it needs to.
494
     */
495
0
    g_variant_store (value, serialised->data);
496
0
}
497
498
/* this ends the main body of the recursive serializer */
499
500
/* < private >
501
 * g_variant_ensure_serialised:
502
 * @value: a #GVariant
503
 *
504
 * Ensures that @value is in serialized form.
505
 *
506
 * If @value is in tree form then this function ensures that the
507
 * serialized size is known and then allocates a buffer of that size and
508
 * serializes the instance into the buffer.  The 'children' array is
509
 * then released and the instance is set to serialized form based on the
510
 * contents of the buffer.
511
 *
512
 * The current thread must hold the lock on @value.
513
 */
514
static void
515
g_variant_ensure_serialised (GVariant *value)
516
0
{
517
0
  g_assert (value->state & STATE_LOCKED);
518
519
0
  if (~value->state & STATE_SERIALISED)
520
0
    {
521
0
      GBytes *bytes;
522
0
      gpointer data;
523
524
0
      TRACE(GLIB_VARIANT_START_SERIALISE(value, value->type_info));
525
0
      g_variant_ensure_size (value);
526
0
      data = g_malloc (value->size);
527
0
      g_variant_serialise (value, data);
528
529
0
      g_variant_release_children (value);
530
531
0
      bytes = g_bytes_new_take (data, value->size);
532
0
      value->contents.serialised.data = g_bytes_get_data (bytes, NULL);
533
0
      value->contents.serialised.bytes = bytes;
534
0
      value->contents.serialised.ordered_offsets_up_to = G_MAXSIZE;
535
0
      value->contents.serialised.checked_offsets_up_to = G_MAXSIZE;
536
0
      value->state |= STATE_SERIALISED;
537
0
      TRACE(GLIB_VARIANT_END_SERIALISE(value, value->type_info));
538
0
    }
539
0
}
540
541
/* < private >
542
 * g_variant_alloc:
543
 * @type: the type of the new instance
544
 * @serialised: if the instance will be in serialised form
545
 * @trusted: if the instance will be trusted
546
 *
547
 * Allocates a #GVariant instance and does some common work (such as
548
 * looking up and filling in the type info), setting the state field,
549
 * and setting the ref_count to 1.
550
 *
551
 * Returns: a new #GVariant with a floating reference
552
 */
553
static GVariant *
554
g_variant_alloc (const GVariantType *type,
555
                 gboolean            serialised,
556
                 gboolean            trusted)
557
0
{
558
0
  GVariant *value;
559
560
0
  value = g_slice_new (GVariant);
561
0
  value->type_info = g_variant_type_info_get (type);
562
0
  value->state = (serialised ? STATE_SERIALISED : 0) |
563
0
                 (trusted ? STATE_TRUSTED : 0) |
564
0
                 STATE_FLOATING;
565
0
  value->size = (gssize) -1;
566
0
  g_atomic_ref_count_init (&value->ref_count);
567
0
  value->depth = 0;
568
569
0
  return value;
570
0
}
571
572
/**
573
 * g_variant_new_from_bytes:
574
 * @type: a #GVariantType
575
 * @bytes: a #GBytes
576
 * @trusted: if the contents of @bytes are trusted
577
 *
578
 * Constructs a new serialized-mode #GVariant instance.  This is the
579
 * inner interface for creation of new serialized values that gets
580
 * called from various functions in gvariant.c.
581
 *
582
 * A reference is taken on @bytes.
583
 *
584
 * The data in @bytes must be aligned appropriately for the @type being loaded.
585
 * Otherwise this function will internally create a copy of the memory (since
586
 * GLib 2.60) or (in older versions) fail and exit the process.
587
 *
588
 * Returns: (transfer none): a new #GVariant with a floating reference
589
 *
590
 * Since: 2.36
591
 */
592
GVariant *
593
g_variant_new_from_bytes (const GVariantType *type,
594
                          GBytes             *bytes,
595
                          gboolean            trusted)
596
0
{
597
0
  GVariant *value;
598
0
  guint alignment;
599
0
  gsize size;
600
0
  GBytes *owned_bytes = NULL;
601
0
  GVariantSerialised serialised;
602
603
0
  value = g_variant_alloc (type, TRUE, trusted);
604
605
0
  g_variant_type_info_query (value->type_info,
606
0
                             &alignment, &size);
607
608
  /* Ensure the alignment is correct. This is a huge performance hit if it’s
609
   * not correct, but that’s better than aborting if a caller provides data
610
   * with the wrong alignment (which is likely to happen very occasionally, and
611
   * only cause an abort on some architectures — so is unlikely to be caught
612
   * in testing). Callers can always actively ensure they use the correct
613
   * alignment to avoid the performance hit. */
614
0
  serialised.type_info = value->type_info;
615
0
  serialised.data = (guchar *) g_bytes_get_data (bytes, &serialised.size);
616
0
  serialised.depth = 0;
617
0
  serialised.ordered_offsets_up_to = trusted ? G_MAXSIZE : 0;
618
0
  serialised.checked_offsets_up_to = trusted ? G_MAXSIZE : 0;
619
620
0
  if (!g_variant_serialised_check (serialised))
621
0
    {
622
0
#ifdef HAVE_POSIX_MEMALIGN
623
0
      gpointer aligned_data = NULL;
624
0
      gsize aligned_size = g_bytes_get_size (bytes);
625
626
      /* posix_memalign() requires the alignment to be a multiple of
627
       * sizeof(void*), and a power of 2. See g_variant_type_info_query() for
628
       * details on the alignment format.
629
       *
630
       * While calling posix_memalign() with aligned_size==0 is safe on glibc,
631
       * POSIX specifies that the behaviour is implementation-defined, so avoid
632
       * that and leave aligned_data==NULL in that case.
633
       * See https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_memalign.html */
634
0
      if (aligned_size != 0 &&
635
0
          posix_memalign (&aligned_data, MAX (sizeof (void *), alignment + 1),
636
0
                          aligned_size) != 0)
637
0
        g_error ("posix_memalign failed");
638
639
0
      if (aligned_size != 0)
640
0
        memcpy (aligned_data, g_bytes_get_data (bytes, NULL), aligned_size);
641
642
0
      bytes = owned_bytes = g_bytes_new_with_free_func (aligned_data,
643
0
                                                        aligned_size,
644
0
                                                        free, aligned_data);
645
0
      aligned_data = NULL;
646
#else
647
      /* NOTE: there may be platforms that lack posix_memalign() and also
648
       * have malloc() that returns non-8-aligned.  if so, we need to try
649
       * harder here.
650
       */
651
      bytes = owned_bytes = g_bytes_new (g_bytes_get_data (bytes, NULL),
652
                                         g_bytes_get_size (bytes));
653
#endif
654
0
    }
655
656
0
  value->contents.serialised.bytes = g_bytes_ref (bytes);
657
658
0
  if (size && g_bytes_get_size (bytes) != size)
659
0
    {
660
      /* Creating a fixed-sized GVariant with a bytes of the wrong
661
       * size.
662
       *
663
       * We should do the equivalent of pulling a fixed-sized child out
664
       * of a brozen container (ie: data is NULL size is equal to the correct
665
       * fixed size).
666
       */
667
0
      value->contents.serialised.data = NULL;
668
0
      value->size = size;
669
0
    }
670
0
  else
671
0
    {
672
0
      value->contents.serialised.data = g_bytes_get_data (bytes, &value->size);
673
0
    }
674
675
0
  value->contents.serialised.ordered_offsets_up_to = trusted ? G_MAXSIZE : 0;
676
0
  value->contents.serialised.checked_offsets_up_to = trusted ? G_MAXSIZE : 0;
677
678
0
  g_clear_pointer (&owned_bytes, g_bytes_unref);
679
680
0
  TRACE(GLIB_VARIANT_FROM_BUFFER(value, value->type_info, value->ref_count, value->state));
681
682
0
  return value;
683
0
}
684
685
/* -- internal -- */
686
687
/* < internal >
688
 * g_variant_new_from_children:
689
 * @type: a #GVariantType
690
 * @children: an array of #GVariant pointers.  Consumed.
691
 * @n_children: the length of @children
692
 * @trusted: %TRUE if every child in @children is trusted
693
 *
694
 * Constructs a new tree-mode #GVariant instance.  This is the inner
695
 * interface for creation of new serialized values that gets called from
696
 * various functions in gvariant.c.
697
 *
698
 * @children is consumed by this function.  g_free() will be called on
699
 * it some time later.
700
 *
701
 * Returns: a new #GVariant with a floating reference
702
 */
703
GVariant *
704
g_variant_new_from_children (const GVariantType  *type,
705
                             GVariant           **children,
706
                             gsize                n_children,
707
                             gboolean             trusted)
708
0
{
709
0
  GVariant *value;
710
711
0
  value = g_variant_alloc (type, FALSE, trusted);
712
0
  value->contents.tree.children = children;
713
0
  value->contents.tree.n_children = n_children;
714
0
  TRACE(GLIB_VARIANT_FROM_CHILDREN(value, value->type_info, value->ref_count, value->state));
715
716
0
  return value;
717
0
}
718
719
/* < internal >
720
 * g_variant_get_type_info:
721
 * @value: a #GVariant
722
 *
723
 * Returns the #GVariantTypeInfo corresponding to the type of @value.  A
724
 * reference is not added, so the return value is only good for the
725
 * duration of the life of @value.
726
 *
727
 * Returns: the #GVariantTypeInfo for @value
728
 */
729
GVariantTypeInfo *
730
g_variant_get_type_info (GVariant *value)
731
0
{
732
0
  return value->type_info;
733
0
}
734
735
/* < internal >
736
 * g_variant_is_trusted:
737
 * @value: a #GVariant
738
 *
739
 * Determines if @value is trusted by #GVariant to contain only
740
 * fully-valid data.  All values constructed solely via #GVariant APIs
741
 * are trusted, but values containing data read in from other sources
742
 * are usually not trusted.
743
 *
744
 * The main advantage of trusted data is that certain checks can be
745
 * skipped.  For example, we don't need to check that a string is
746
 * properly nul-terminated or that an object path is actually a
747
 * properly-formatted object path.
748
 *
749
 * Returns: if @value is trusted
750
 */
751
gboolean
752
g_variant_is_trusted (GVariant *value)
753
0
{
754
0
  return (value->state & STATE_TRUSTED) != 0;
755
0
}
756
757
/* < internal >
758
 * g_variant_get_depth:
759
 * @value: a #GVariant
760
 *
761
 * Gets the nesting depth of a #GVariant. This is 0 for a #GVariant with no
762
 * children.
763
 *
764
 * Returns: nesting depth of @value
765
 */
766
gsize
767
g_variant_get_depth (GVariant *value)
768
0
{
769
0
  return value->depth;
770
0
}
771
772
/* -- public -- */
773
774
/**
775
 * g_variant_unref:
776
 * @value: a #GVariant
777
 *
778
 * Decreases the reference count of @value.  When its reference count
779
 * drops to 0, the memory used by the variant is freed.
780
 *
781
 * Since: 2.24
782
 **/
783
void
784
g_variant_unref (GVariant *value)
785
0
{
786
0
  g_return_if_fail (value != NULL);
787
788
0
  TRACE(GLIB_VARIANT_UNREF(value, value->type_info, value->ref_count, value->state));
789
790
0
  if (g_atomic_ref_count_dec (&value->ref_count))
791
0
    {
792
0
      if G_UNLIKELY (value->state & STATE_LOCKED)
793
0
        g_critical ("attempting to free a locked GVariant instance.  "
794
0
                    "This should never happen.");
795
796
0
      value->state |= STATE_LOCKED;
797
798
0
      g_variant_type_info_unref (value->type_info);
799
800
0
      if (value->state & STATE_SERIALISED)
801
0
        g_bytes_unref (value->contents.serialised.bytes);
802
0
      else
803
0
        g_variant_release_children (value);
804
805
0
      memset (value, 0, sizeof (GVariant));
806
0
      g_slice_free (GVariant, value);
807
0
    }
808
0
}
809
810
/**
811
 * g_variant_ref:
812
 * @value: a #GVariant
813
 *
814
 * Increases the reference count of @value.
815
 *
816
 * Returns: the same @value
817
 *
818
 * Since: 2.24
819
 **/
820
GVariant *
821
g_variant_ref (GVariant *value)
822
0
{
823
0
  g_return_val_if_fail (value != NULL, NULL);
824
825
0
  TRACE(GLIB_VARIANT_REF(value, value->type_info, value->ref_count, value->state));
826
827
0
  g_atomic_ref_count_inc (&value->ref_count);
828
829
0
  return value;
830
0
}
831
832
/**
833
 * g_variant_ref_sink:
834
 * @value: a #GVariant
835
 *
836
 * #GVariant uses a floating reference count system.  All functions with
837
 * names starting with `g_variant_new_` return floating
838
 * references.
839
 *
840
 * Calling g_variant_ref_sink() on a #GVariant with a floating reference
841
 * will convert the floating reference into a full reference.  Calling
842
 * g_variant_ref_sink() on a non-floating #GVariant results in an
843
 * additional normal reference being added.
844
 *
845
 * In other words, if the @value is floating, then this call "assumes
846
 * ownership" of the floating reference, converting it to a normal
847
 * reference.  If the @value is not floating, then this call adds a
848
 * new normal reference increasing the reference count by one.
849
 *
850
 * All calls that result in a #GVariant instance being inserted into a
851
 * container will call g_variant_ref_sink() on the instance.  This means
852
 * that if the value was just created (and has only its floating
853
 * reference) then the container will assume sole ownership of the value
854
 * at that point and the caller will not need to unreference it.  This
855
 * makes certain common styles of programming much easier while still
856
 * maintaining normal refcounting semantics in situations where values
857
 * are not floating.
858
 *
859
 * Returns: the same @value
860
 *
861
 * Since: 2.24
862
 **/
863
GVariant *
864
g_variant_ref_sink (GVariant *value)
865
0
{
866
0
  g_return_val_if_fail (value != NULL, NULL);
867
0
  g_return_val_if_fail (!g_atomic_ref_count_compare (&value->ref_count, 0), NULL);
868
869
0
  g_variant_lock (value);
870
871
0
  TRACE(GLIB_VARIANT_REF_SINK(value, value->type_info, value->ref_count, value->state, value->state & STATE_FLOATING));
872
873
0
  if (~value->state & STATE_FLOATING)
874
0
    g_variant_ref (value);
875
0
  else
876
0
    value->state &= ~STATE_FLOATING;
877
878
0
  g_variant_unlock (value);
879
880
0
  return value;
881
0
}
882
883
/**
884
 * g_variant_take_ref:
885
 * @value: a #GVariant
886
 *
887
 * If @value is floating, sink it.  Otherwise, do nothing.
888
 *
889
 * Typically you want to use g_variant_ref_sink() in order to
890
 * automatically do the correct thing with respect to floating or
891
 * non-floating references, but there is one specific scenario where
892
 * this function is helpful.
893
 *
894
 * The situation where this function is helpful is when creating an API
895
 * that allows the user to provide a callback function that returns a
896
 * #GVariant.  We certainly want to allow the user the flexibility to
897
 * return a non-floating reference from this callback (for the case
898
 * where the value that is being returned already exists).
899
 *
900
 * At the same time, the style of the #GVariant API makes it likely that
901
 * for newly-created #GVariant instances, the user can be saved some
902
 * typing if they are allowed to return a #GVariant with a floating
903
 * reference.
904
 *
905
 * Using this function on the return value of the user's callback allows
906
 * the user to do whichever is more convenient for them.  The caller
907
 * will always receives exactly one full reference to the value: either
908
 * the one that was returned in the first place, or a floating reference
909
 * that has been converted to a full reference.
910
 *
911
 * This function has an odd interaction when combined with
912
 * g_variant_ref_sink() running at the same time in another thread on
913
 * the same #GVariant instance.  If g_variant_ref_sink() runs first then
914
 * the result will be that the floating reference is converted to a hard
915
 * reference.  If g_variant_take_ref() runs first then the result will
916
 * be that the floating reference is converted to a hard reference and
917
 * an additional reference on top of that one is added.  It is best to
918
 * avoid this situation.
919
 *
920
 * Returns: the same @value
921
 **/
922
GVariant *
923
g_variant_take_ref (GVariant *value)
924
0
{
925
0
  g_return_val_if_fail (value != NULL, NULL);
926
0
  g_return_val_if_fail (!g_atomic_ref_count_compare (&value->ref_count, 0), NULL);
927
928
0
  TRACE(GLIB_VARIANT_TAKE_REF(value, value->type_info, value->ref_count, value->state, value->state & STATE_FLOATING));
929
0
  g_atomic_int_and (&value->state, ~STATE_FLOATING);
930
931
0
  return value;
932
0
}
933
934
/**
935
 * g_variant_is_floating:
936
 * @value: a #GVariant
937
 *
938
 * Checks whether @value has a floating reference count.
939
 *
940
 * This function should only ever be used to assert that a given variant
941
 * is or is not floating, or for debug purposes. To acquire a reference
942
 * to a variant that might be floating, always use g_variant_ref_sink()
943
 * or g_variant_take_ref().
944
 *
945
 * See g_variant_ref_sink() for more information about floating reference
946
 * counts.
947
 *
948
 * Returns: whether @value is floating
949
 *
950
 * Since: 2.26
951
 **/
952
gboolean
953
g_variant_is_floating (GVariant *value)
954
0
{
955
0
  g_return_val_if_fail (value != NULL, FALSE);
956
957
0
  return (value->state & STATE_FLOATING) != 0;
958
0
}
959
960
/**
961
 * g_variant_get_size:
962
 * @value: a #GVariant instance
963
 *
964
 * Determines the number of bytes that would be required to store @value
965
 * with g_variant_store().
966
 *
967
 * If @value has a fixed-sized type then this function always returned
968
 * that fixed size.
969
 *
970
 * In the case that @value is already in serialized form or the size has
971
 * already been calculated (ie: this function has been called before)
972
 * then this function is O(1).  Otherwise, the size is calculated, an
973
 * operation which is approximately O(n) in the number of values
974
 * involved.
975
 *
976
 * Returns: the serialized size of @value
977
 *
978
 * Since: 2.24
979
 **/
980
gsize
981
g_variant_get_size (GVariant *value)
982
0
{
983
0
  g_variant_lock (value);
984
0
  g_variant_ensure_size (value);
985
0
  g_variant_unlock (value);
986
987
0
  return value->size;
988
0
}
989
990
/**
991
 * g_variant_get_data:
992
 * @value: a #GVariant instance
993
 *
994
 * Returns a pointer to the serialized form of a #GVariant instance.
995
 * The returned data may not be in fully-normalised form if read from an
996
 * untrusted source.  The returned data must not be freed; it remains
997
 * valid for as long as @value exists.
998
 *
999
 * If @value is a fixed-sized value that was deserialized from a
1000
 * corrupted serialized container then %NULL may be returned.  In this
1001
 * case, the proper thing to do is typically to use the appropriate
1002
 * number of nul bytes in place of @value.  If @value is not fixed-sized
1003
 * then %NULL is never returned.
1004
 *
1005
 * In the case that @value is already in serialized form, this function
1006
 * is O(1).  If the value is not already in serialized form,
1007
 * serialization occurs implicitly and is approximately O(n) in the size
1008
 * of the result.
1009
 *
1010
 * To deserialize the data returned by this function, in addition to the
1011
 * serialized data, you must know the type of the #GVariant, and (if the
1012
 * machine might be different) the endianness of the machine that stored
1013
 * it. As a result, file formats or network messages that incorporate
1014
 * serialized #GVariants must include this information either
1015
 * implicitly (for instance "the file always contains a
1016
 * %G_VARIANT_TYPE_VARIANT and it is always in little-endian order") or
1017
 * explicitly (by storing the type and/or endianness in addition to the
1018
 * serialized data).
1019
 *
1020
 * Returns: (transfer none): the serialized form of @value, or %NULL
1021
 *
1022
 * Since: 2.24
1023
 **/
1024
gconstpointer
1025
g_variant_get_data (GVariant *value)
1026
0
{
1027
0
  g_variant_lock (value);
1028
0
  g_variant_ensure_serialised (value);
1029
0
  g_variant_unlock (value);
1030
1031
0
  return value->contents.serialised.data;
1032
0
}
1033
1034
/**
1035
 * g_variant_get_data_as_bytes:
1036
 * @value: a #GVariant
1037
 *
1038
 * Returns a pointer to the serialized form of a #GVariant instance.
1039
 * The semantics of this function are exactly the same as
1040
 * g_variant_get_data(), except that the returned #GBytes holds
1041
 * a reference to the variant data.
1042
 *
1043
 * Returns: (transfer full): A new #GBytes representing the variant data
1044
 *
1045
 * Since: 2.36
1046
 */ 
1047
GBytes *
1048
g_variant_get_data_as_bytes (GVariant *value)
1049
0
{
1050
0
  const gchar *bytes_data;
1051
0
  const gchar *data;
1052
0
  gsize bytes_size;
1053
0
  gsize size;
1054
1055
0
  g_variant_lock (value);
1056
0
  g_variant_ensure_serialised (value);
1057
0
  g_variant_unlock (value);
1058
1059
0
  bytes_data = g_bytes_get_data (value->contents.serialised.bytes, &bytes_size);
1060
0
  data = value->contents.serialised.data;
1061
0
  size = value->size;
1062
1063
0
  if (data == NULL)
1064
0
    {
1065
0
      g_assert (size == 0);
1066
0
      data = bytes_data;
1067
0
    }
1068
1069
0
  if (data == bytes_data && size == bytes_size)
1070
0
    return g_bytes_ref (value->contents.serialised.bytes);
1071
0
  else
1072
0
    return g_bytes_new_from_bytes (value->contents.serialised.bytes,
1073
0
                                   data - bytes_data, size);
1074
0
}
1075
1076
1077
/**
1078
 * g_variant_n_children:
1079
 * @value: a container #GVariant
1080
 *
1081
 * Determines the number of children in a container #GVariant instance.
1082
 * This includes variants, maybes, arrays, tuples and dictionary
1083
 * entries.  It is an error to call this function on any other type of
1084
 * #GVariant.
1085
 *
1086
 * For variants, the return value is always 1.  For values with maybe
1087
 * types, it is always zero or one.  For arrays, it is the length of the
1088
 * array.  For tuples it is the number of tuple items (which depends
1089
 * only on the type).  For dictionary entries, it is always 2
1090
 *
1091
 * This function is O(1).
1092
 *
1093
 * Returns: the number of children in the container
1094
 *
1095
 * Since: 2.24
1096
 **/
1097
gsize
1098
g_variant_n_children (GVariant *value)
1099
0
{
1100
0
  gsize n_children;
1101
1102
0
  g_variant_lock (value);
1103
1104
0
  if (value->state & STATE_SERIALISED)
1105
0
    n_children = g_variant_serialised_n_children (
1106
0
        g_variant_to_serialised (value));
1107
0
  else
1108
0
    n_children = value->contents.tree.n_children;
1109
1110
0
  g_variant_unlock (value);
1111
1112
0
  return n_children;
1113
0
}
1114
1115
/**
1116
 * g_variant_get_child_value:
1117
 * @value: a container #GVariant
1118
 * @index_: the index of the child to fetch
1119
 *
1120
 * Reads a child item out of a container #GVariant instance.  This
1121
 * includes variants, maybes, arrays, tuples and dictionary
1122
 * entries.  It is an error to call this function on any other type of
1123
 * #GVariant.
1124
 *
1125
 * It is an error if @index_ is greater than the number of child items
1126
 * in the container.  See g_variant_n_children().
1127
 *
1128
 * The returned value is never floating.  You should free it with
1129
 * g_variant_unref() when you're done with it.
1130
 *
1131
 * Note that values borrowed from the returned child are not guaranteed to
1132
 * still be valid after the child is freed even if you still hold a reference
1133
 * to @value, if @value has not been serialized at the time this function is
1134
 * called. To avoid this, you can serialize @value by calling
1135
 * g_variant_get_data() and optionally ignoring the return value.
1136
 *
1137
 * There may be implementation specific restrictions on deeply nested values,
1138
 * which would result in the unit tuple being returned as the child value,
1139
 * instead of further nested children. #GVariant is guaranteed to handle
1140
 * nesting up to at least 64 levels.
1141
 *
1142
 * This function is O(1).
1143
 *
1144
 * Returns: (transfer full): the child at the specified index
1145
 *
1146
 * Since: 2.24
1147
 **/
1148
GVariant *
1149
g_variant_get_child_value (GVariant *value,
1150
                           gsize     index_)
1151
0
{
1152
0
  g_return_val_if_fail (value->depth < G_MAXSIZE, NULL);
1153
1154
0
  if (~g_atomic_int_get (&value->state) & STATE_SERIALISED)
1155
0
    {
1156
      /* g_variant_serialised_get_child() does its own checks on index_ */
1157
0
      g_return_val_if_fail (index_ < g_variant_n_children (value), NULL);
1158
1159
0
      g_variant_lock (value);
1160
1161
0
      if (~value->state & STATE_SERIALISED)
1162
0
        {
1163
0
          GVariant *child;
1164
1165
0
          child = g_variant_ref (value->contents.tree.children[index_]);
1166
0
          g_variant_unlock (value);
1167
1168
0
          return child;
1169
0
        }
1170
1171
0
      g_variant_unlock (value);
1172
0
    }
1173
1174
0
  {
1175
0
    GVariantSerialised serialised = g_variant_to_serialised (value);
1176
0
    GVariantSerialised s_child;
1177
0
    GVariant *child;
1178
1179
    /* get the serializer to extract the serialized data for the child
1180
     * from the serialized data for the container
1181
     */
1182
0
    s_child = g_variant_serialised_get_child (serialised, index_);
1183
1184
    /* Update the cached ordered_offsets_up_to, since @serialised will be thrown away when this function exits */
1185
0
    value->contents.serialised.ordered_offsets_up_to = MAX (value->contents.serialised.ordered_offsets_up_to, serialised.ordered_offsets_up_to);
1186
0
    value->contents.serialised.checked_offsets_up_to = MAX (value->contents.serialised.checked_offsets_up_to, serialised.checked_offsets_up_to);
1187
1188
    /* Check whether this would cause nesting too deep. If so, return a fake
1189
     * child. The only situation we expect this to happen in is with a variant,
1190
     * as all other deeply-nested types have a static type, and hence should
1191
     * have been rejected earlier. In the case of a variant whose nesting plus
1192
     * the depth of its child is too great, return a unit variant () instead of
1193
     * the real child. */
1194
0
    if (!(value->state & STATE_TRUSTED) &&
1195
0
        g_variant_type_info_query_depth (s_child.type_info) >=
1196
0
        G_VARIANT_MAX_RECURSION_DEPTH - value->depth)
1197
0
      {
1198
0
        g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE_VARIANT));
1199
0
        g_variant_type_info_unref (s_child.type_info);
1200
0
        return g_variant_new_tuple (NULL, 0);
1201
0
      }
1202
1203
    /* create a new serialized instance out of it */
1204
0
    child = g_slice_new (GVariant);
1205
0
    child->type_info = s_child.type_info;
1206
0
    child->state = (value->state & STATE_TRUSTED) |
1207
0
                   STATE_SERIALISED;
1208
0
    child->size = s_child.size;
1209
0
    g_atomic_ref_count_init (&child->ref_count);
1210
0
    child->depth = value->depth + 1;
1211
0
    child->contents.serialised.bytes =
1212
0
      g_bytes_ref (value->contents.serialised.bytes);
1213
0
    child->contents.serialised.data = s_child.data;
1214
0
    child->contents.serialised.ordered_offsets_up_to = (value->state & STATE_TRUSTED) ? G_MAXSIZE : s_child.ordered_offsets_up_to;
1215
0
    child->contents.serialised.checked_offsets_up_to = (value->state & STATE_TRUSTED) ? G_MAXSIZE : s_child.checked_offsets_up_to;
1216
1217
0
    TRACE(GLIB_VARIANT_FROM_PARENT(child, child->type_info, child->ref_count, child->state, value));
1218
1219
0
    return child;
1220
0
  }
1221
0
}
1222
1223
/**
1224
 * g_variant_maybe_get_child_value:
1225
 * @value: a container #GVariant
1226
 * @index_: the index of the child to fetch
1227
 *
1228
 * Reads a child item out of a container #GVariant instance, if it is in normal
1229
 * form. If it is not in normal form, return %NULL.
1230
 *
1231
 * This function behaves the same as g_variant_get_child_value(), except that it
1232
 * returns %NULL if the child is not in normal form. g_variant_get_child_value()
1233
 * would instead return a new default value of the correct type.
1234
 *
1235
 * This is intended to be used internally to avoid unnecessary #GVariant
1236
 * allocations.
1237
 *
1238
 * The returned value is never floating.  You should free it with
1239
 * g_variant_unref() when you're done with it.
1240
 *
1241
 * This function is O(1).
1242
 *
1243
 * Returns: (transfer full): the child at the specified index
1244
 *
1245
 * Since: 2.74
1246
 */
1247
GVariant *
1248
g_variant_maybe_get_child_value (GVariant *value,
1249
                                 gsize     index_)
1250
0
{
1251
0
  g_return_val_if_fail (value->depth < G_MAXSIZE, NULL);
1252
1253
0
  if (~g_atomic_int_get (&value->state) & STATE_SERIALISED)
1254
0
    {
1255
      /* g_variant_serialised_get_child() does its own checks on index_ */
1256
0
      g_return_val_if_fail (index_ < g_variant_n_children (value), NULL);
1257
1258
0
      g_variant_lock (value);
1259
1260
0
      if (~value->state & STATE_SERIALISED)
1261
0
        {
1262
0
          GVariant *child;
1263
1264
0
          child = g_variant_ref (value->contents.tree.children[index_]);
1265
0
          g_variant_unlock (value);
1266
1267
0
          return child;
1268
0
        }
1269
1270
0
      g_variant_unlock (value);
1271
0
    }
1272
1273
0
  {
1274
0
    GVariantSerialised serialised = g_variant_to_serialised (value);
1275
0
    GVariantSerialised s_child;
1276
1277
    /* get the serializer to extract the serialized data for the child
1278
     * from the serialized data for the container
1279
     */
1280
0
    s_child = g_variant_serialised_get_child (serialised, index_);
1281
1282
0
    if (!(value->state & STATE_TRUSTED) && s_child.data == NULL)
1283
0
      {
1284
0
        g_variant_type_info_unref (s_child.type_info);
1285
0
        return NULL;
1286
0
      }
1287
1288
0
    g_variant_type_info_unref (s_child.type_info);
1289
0
    return g_variant_get_child_value (value, index_);
1290
0
  }
1291
0
}
1292
1293
/**
1294
 * g_variant_store:
1295
 * @value: the #GVariant to store
1296
 * @data: (not nullable): the location to store the serialized data at
1297
 *
1298
 * Stores the serialized form of @value at @data.  @data should be
1299
 * large enough.  See g_variant_get_size().
1300
 *
1301
 * The stored data is in machine native byte order but may not be in
1302
 * fully-normalised form if read from an untrusted source.  See
1303
 * g_variant_get_normal_form() for a solution.
1304
 *
1305
 * As with g_variant_get_data(), to be able to deserialize the
1306
 * serialized variant successfully, its type and (if the destination
1307
 * machine might be different) its endianness must also be available.
1308
 *
1309
 * This function is approximately O(n) in the size of @data.
1310
 *
1311
 * Since: 2.24
1312
 **/
1313
void
1314
g_variant_store (GVariant *value,
1315
                 gpointer  data)
1316
0
{
1317
0
  g_variant_lock (value);
1318
1319
0
  if (value->state & STATE_SERIALISED)
1320
0
    {
1321
0
      if (value->contents.serialised.data != NULL)
1322
0
        memcpy (data, value->contents.serialised.data, value->size);
1323
0
      else
1324
0
        memset (data, 0, value->size);
1325
0
    }
1326
0
  else
1327
0
    g_variant_serialise (value, data);
1328
1329
0
  g_variant_unlock (value);
1330
0
}
1331
1332
/**
1333
 * g_variant_is_normal_form:
1334
 * @value: a #GVariant instance
1335
 *
1336
 * Checks if @value is in normal form.
1337
 *
1338
 * The main reason to do this is to detect if a given chunk of
1339
 * serialized data is in normal form: load the data into a #GVariant
1340
 * using g_variant_new_from_data() and then use this function to
1341
 * check.
1342
 *
1343
 * If @value is found to be in normal form then it will be marked as
1344
 * being trusted.  If the value was already marked as being trusted then
1345
 * this function will immediately return %TRUE.
1346
 *
1347
 * There may be implementation specific restrictions on deeply nested values.
1348
 * GVariant is guaranteed to handle nesting up to at least 64 levels.
1349
 *
1350
 * Returns: %TRUE if @value is in normal form
1351
 *
1352
 * Since: 2.24
1353
 **/
1354
gboolean
1355
g_variant_is_normal_form (GVariant *value)
1356
0
{
1357
0
  if (value->state & STATE_TRUSTED)
1358
0
    return TRUE;
1359
1360
0
  g_variant_lock (value);
1361
1362
0
  if (value->depth >= G_VARIANT_MAX_RECURSION_DEPTH)
1363
0
    return FALSE;
1364
1365
0
  if (value->state & STATE_SERIALISED)
1366
0
    {
1367
0
      if (g_variant_serialised_is_normal (g_variant_to_serialised (value)))
1368
0
        value->state |= STATE_TRUSTED;
1369
0
    }
1370
0
  else
1371
0
    {
1372
0
      gboolean normal = TRUE;
1373
0
      gsize i;
1374
1375
0
      for (i = 0; i < value->contents.tree.n_children; i++)
1376
0
        normal &= g_variant_is_normal_form (value->contents.tree.children[i]);
1377
1378
0
      if (normal)
1379
0
        value->state |= STATE_TRUSTED;
1380
0
    }
1381
1382
0
  g_variant_unlock (value);
1383
1384
0
  return (value->state & STATE_TRUSTED) != 0;
1385
0
}