2 * Copyright © 2007, 2008 Ryan Lortie
3 * Copyright © 2010 Codethink Limited
5 * SPDX-License-Identifier: LGPL-2.1-or-later
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.
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.
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/>.
23 #include <glib/gvariant-core.h>
25 #include <glib/gvariant-internal.h>
26 #include <glib/gvariant-serialiser.h>
27 #include <glib/gtestutils.h>
28 #include <glib/gbitlock.h>
29 #include <glib/gatomic.h>
30 #include <glib/gbytes.h>
31 #include <glib/gslice.h>
32 #include <glib/gmem.h>
33 #include <glib/grefcount.h>
38 * This file includes the structure definition for GVariant and a small
39 * set of functions that are allowed to access the structure directly.
41 * This minimises the amount of code that can possibly touch a GVariant
42 * structure directly to a few simple fundamental operations. These few
43 * operations are written to be completely threadsafe with respect to
44 * all possible outside access. This means that we only need to be
45 * concerned about thread safety issues in this one small file.
47 * Most GVariant API functions are in gvariant.c.
53 * #GVariant is an opaque data structure and can only be accessed
54 * using the following functions.
59 /* see below for field member documentation */
61 GVariantTypeInfo *type_info;
80 gatomicrefcount ref_count;
86 * There are two primary forms of GVariant instances: "serialized form"
89 * "serialized form": A serialized GVariant instance stores its value in
90 * the GVariant serialization format. All
91 * basic-typed instances (ie: non-containers) are in
92 * serialized format, as are some containers.
94 * "tree form": Some containers are in "tree form". In this case,
95 * instead of containing the serialized data for the
96 * container, the instance contains an array of pointers to
97 * the child values of the container (thus forming a tree).
99 * It is possible for an instance to transition from tree form to
100 * serialized form. This happens, implicitly, if the serialized data is
101 * requested (eg: via g_variant_get_data()). Serialized form instances
102 * never transition into tree form.
105 * The fields of the structure are documented here:
107 * type_info: this is a reference to a GVariantTypeInfo describing the
108 * type of the instance. When the instance is freed, this
109 * reference must be released with g_variant_type_info_unref().
111 * The type_info field never changes during the life of the
112 * instance, so it can be accessed without a lock.
114 * size: this is the size of the serialized form for the instance, if it
115 * is known. If the instance is in serialized form then it is, by
116 * definition, known. If the instance is in tree form then it may
117 * be unknown (in which case it is -1). It is possible for the
118 * size to be known when in tree form if, for example, the user
119 * has called g_variant_get_size() without calling
120 * g_variant_get_data(). Additionally, even when the user calls
121 * g_variant_get_data() the size of the data must first be
122 * determined so that a large enough buffer can be allocated for
125 * Once the size is known, it can never become unknown again.
126 * g_variant_ensure_size() is used to ensure that the size is in
127 * the known state -- it calculates the size if needed. After
128 * that, the size field can be accessed without a lock.
130 * contents: a union containing either the information associated with
131 * holding a value in serialized form or holding a value in
134 * .serialised: Only valid when the instance is in serialized form.
136 * Since an instance can never transition away from
137 * serialized form, once these fields are set, they will
138 * never be changed. It is therefore valid to access
139 * them without holding a lock.
141 * .bytes: the #GBytes that contains the memory pointed to by
142 * .data, or %NULL if .data is %NULL. In the event that
143 * the instance was deserialized from another instance,
144 * then the bytes will be shared by both of them. When
145 * the instance is freed, this reference must be released
146 * with g_bytes_unref().
148 * .data: the serialized data (of size 'size') of the instance.
149 * This pointer should not be freed or modified in any way.
150 * #GBytes is responsible for memory management.
152 * This pointer may be %NULL in two cases:
154 * - if the serialized size of the instance is 0
156 * - if the instance is of a fixed-sized type and was
157 * deserialized out of a corrupted container such that
158 * the container contains too few bytes to point to the
159 * entire proper fixed-size of this instance. In this
160 * case, 'size' will still be equal to the proper fixed
161 * size, but this pointer will be %NULL. This is exactly
162 * the reason that g_variant_get_data() sometimes returns
163 * %NULL. For all other calls, the effect should be as
164 * if .data pointed to the appropriate number of nul
167 * .tree: Only valid when the instance is in tree form.
169 * Note that accesses from other threads could result in
170 * conversion of the instance from tree form to serialized form
171 * at any time. For this reason, the instance lock must always
172 * be held while performing any operations on 'contents.tree'.
174 * .children: the array of the child instances of this instance.
175 * When the instance is freed (or converted to serialized
176 * form) then each child must have g_variant_unref()
177 * called on it and the array must be freed using
180 * .n_children: the number of items in the .children array.
182 * state: a bitfield describing the state of the instance. It is a
183 * bitwise-or of the following STATE_* constants:
185 * STATE_LOCKED: the instance lock is held. This is the bit used by
188 * STATE_SERIALISED: the instance is in serialized form. If this
189 * flag is not set then the instance is in tree
192 * STATE_TRUSTED: for serialized form instances, this means that the
193 * serialized data is known to be in normal form (ie:
196 * For tree form instances, this means that all of the
197 * child instances in the contents.tree.children array
198 * are trusted. This means that if the container is
199 * serialized then the resulting data will be in
202 * If this flag is unset it does not imply that the
203 * data is corrupted. It merely means that we're not
204 * sure that it's valid. See g_variant_is_trusted().
206 * STATE_FLOATING: if this flag is set then the object has a floating
207 * reference. See g_variant_ref_sink().
209 * ref_count: the reference count of the instance
211 * depth: the depth of the GVariant in a hierarchy of nested containers,
212 * increasing with the level of nesting. The top-most GVariant has depth
213 * zero. This is used to avoid recursing too deeply and overflowing the
214 * stack when handling deeply nested untrusted serialized GVariants.
216 #define STATE_LOCKED 1
217 #define STATE_SERIALISED 2
218 #define STATE_TRUSTED 4
219 #define STATE_FLOATING 8
224 * @value: a #GVariant
226 * Locks @value for performing sensitive operations.
229 g_variant_lock (GVariant *value)
231 g_bit_lock (&value->state, 0);
236 * @value: a #GVariant
238 * Unlocks @value after performing sensitive operations.
241 g_variant_unlock (GVariant *value)
243 g_bit_unlock (&value->state, 0);
247 * g_variant_release_children:
248 * @value: a #GVariant
250 * Releases the reference held on each child in the 'children' array of
251 * @value and frees the array itself. @value must be in tree form.
253 * This is done when freeing a tree-form instance or converting it to
256 * The current thread must hold the lock on @value.
259 g_variant_release_children (GVariant *value)
263 g_assert (value->state & STATE_LOCKED);
264 g_assert (~value->state & STATE_SERIALISED);
266 for (i = 0; i < value->contents.tree.n_children; i++)
267 g_variant_unref (value->contents.tree.children[i]);
269 g_free (value->contents.tree.children);
272 /* This begins the main body of the recursive serializer.
274 * There are 3 functions here that work as a team with the serializer to
275 * get things done. g_variant_store() has a trivial role, but as a
276 * public API function, it has its definition elsewhere.
278 * Note that "serialization" of an instance does not mean that the
279 * instance is converted to serialized form -- it means that the
280 * serialized form of an instance is written to an external buffer.
281 * g_variant_ensure_serialised() (which is not part of this set of
282 * functions) is the function that is responsible for converting an
283 * instance to serialized form.
285 * We are only concerned here with container types since non-container
286 * instances are always in serialized form. For these instances,
287 * storing their serialized form merely involves a memcpy().
289 * Serialization is a two-step process. First, the size of the
290 * serialized data must be calculated so that an appropriately-sized
291 * buffer can be allocated. Second, the data is written into the
294 * Determining the size:
295 * The process of determining the size is triggered by a call to
296 * g_variant_ensure_size() on a container. This invokes the
297 * serializer code to determine the size. The serializer is passed
298 * g_variant_fill_gvs() as a callback.
300 * g_variant_fill_gvs() is called by the serializer on each child of
301 * the container which, in turn, calls g_variant_ensure_size() on
302 * itself and fills in the result of its own size calculation.
304 * The serializer uses the size information from the children to
305 * calculate the size needed for the entire container.
308 * After the buffer has been allocated, g_variant_serialise() is
309 * called on the container. This invokes the serializer code to write
310 * the bytes to the container. The serializer is, again, passed
311 * g_variant_fill_gvs() as a callback.
313 * This time, when g_variant_fill_gvs() is called for each child, the
314 * child is given a pointer to a sub-region of the allocated buffer
315 * where it should write its data. This is done by calling
316 * g_variant_store(). In the event that the instance is in serialized
317 * form this means a memcpy() of the serialized data into the
318 * allocated buffer. In the event that the instance is in tree form
319 * this means a recursive call back into g_variant_serialise().
322 * The forward declaration here allows corecursion via callback:
324 static void g_variant_fill_gvs (GVariantSerialised *, gpointer);
327 * g_variant_ensure_size:
328 * @value: a #GVariant
330 * Ensures that the ->size field of @value is filled in properly. This
331 * must be done as a precursor to any serialization of the value in
332 * order to know how large of a buffer is needed to store the data.
334 * The current thread must hold the lock on @value.
337 g_variant_ensure_size (GVariant *value)
339 g_assert (value->state & STATE_LOCKED);
341 if (value->size == (gsize) -1)
346 children = (gpointer *) value->contents.tree.children;
347 n_children = value->contents.tree.n_children;
348 value->size = g_variant_serialiser_needed_size (value->type_info,
350 children, n_children);
355 * g_variant_serialise:
356 * @value: a #GVariant
357 * @data: an appropriately-sized buffer
359 * Serializes @value into @data. @value must be in tree form.
361 * No change is made to @value.
363 * The current thread must hold the lock on @value.
366 g_variant_serialise (GVariant *value,
369 GVariantSerialised serialised = { 0, };
373 g_assert (~value->state & STATE_SERIALISED);
374 g_assert (value->state & STATE_LOCKED);
376 serialised.type_info = value->type_info;
377 serialised.size = value->size;
378 serialised.data = data;
379 serialised.depth = value->depth;
381 children = (gpointer *) value->contents.tree.children;
382 n_children = value->contents.tree.n_children;
384 g_variant_serialiser_serialise (serialised, g_variant_fill_gvs,
385 children, n_children);
389 * g_variant_fill_gvs:
390 * @serialised: a pointer to a #GVariantSerialised
391 * @data: a #GVariant instance
393 * This is the callback that is passed by a tree-form container instance
394 * to the serializer. This callback gets called on each child of the
395 * container. Each child is responsible for performing the following
398 * - reporting its type
400 * - reporting its serialized size (requires knowing the size first)
402 * - possibly storing its serialized form into the provided buffer
405 g_variant_fill_gvs (GVariantSerialised *serialised,
408 GVariant *value = data;
410 g_variant_lock (value);
411 g_variant_ensure_size (value);
412 g_variant_unlock (value);
414 if (serialised->type_info == NULL)
415 serialised->type_info = value->type_info;
416 g_assert (serialised->type_info == value->type_info);
418 if (serialised->size == 0)
419 serialised->size = value->size;
420 g_assert (serialised->size == value->size);
421 serialised->depth = value->depth;
423 if (serialised->data)
424 /* g_variant_store() is a public API, so it
425 * it will reacquire the lock if it needs to.
427 g_variant_store (value, serialised->data);
430 /* this ends the main body of the recursive serializer */
433 * g_variant_ensure_serialised:
434 * @value: a #GVariant
436 * Ensures that @value is in serialized form.
438 * If @value is in tree form then this function ensures that the
439 * serialized size is known and then allocates a buffer of that size and
440 * serializes the instance into the buffer. The 'children' array is
441 * then released and the instance is set to serialized form based on the
442 * contents of the buffer.
444 * The current thread must hold the lock on @value.
447 g_variant_ensure_serialised (GVariant *value)
449 g_assert (value->state & STATE_LOCKED);
451 if (~value->state & STATE_SERIALISED)
456 g_variant_ensure_size (value);
457 data = g_malloc (value->size);
458 g_variant_serialise (value, data);
460 g_variant_release_children (value);
462 bytes = g_bytes_new_take (data, value->size);
463 value->contents.serialised.data = g_bytes_get_data (bytes, NULL);
464 value->contents.serialised.bytes = bytes;
465 value->state |= STATE_SERIALISED;
471 * @type: the type of the new instance
472 * @serialised: if the instance will be in serialised form
473 * @trusted: if the instance will be trusted
475 * Allocates a #GVariant instance and does some common work (such as
476 * looking up and filling in the type info), setting the state field,
477 * and setting the ref_count to 1.
479 * Returns: a new #GVariant with a floating reference
482 g_variant_alloc (const GVariantType *type,
488 value = g_slice_new (GVariant);
489 value->type_info = g_variant_type_info_get (type);
490 value->state = (serialised ? STATE_SERIALISED : 0) |
491 (trusted ? STATE_TRUSTED : 0) |
493 value->size = (gssize) -1;
494 g_atomic_ref_count_init (&value->ref_count);
501 * g_variant_new_from_bytes:
502 * @type: a #GVariantType
504 * @trusted: if the contents of @bytes are trusted
506 * Constructs a new serialized-mode #GVariant instance. This is the
507 * inner interface for creation of new serialized values that gets
508 * called from various functions in gvariant.c.
510 * A reference is taken on @bytes.
512 * The data in @bytes must be aligned appropriately for the @type being loaded.
513 * Otherwise this function will internally create a copy of the memory (since
514 * GLib 2.60) or (in older versions) fail and exit the process.
516 * Returns: (transfer none): a new #GVariant with a floating reference
521 g_variant_new_from_bytes (const GVariantType *type,
528 GBytes *owned_bytes = NULL;
529 GVariantSerialised serialised;
531 value = g_variant_alloc (type, TRUE, trusted);
533 g_variant_type_info_query (value->type_info,
536 /* Ensure the alignment is correct. This is a huge performance hit if it’s
537 * not correct, but that’s better than aborting if a caller provides data
538 * with the wrong alignment (which is likely to happen very occasionally, and
539 * only cause an abort on some architectures — so is unlikely to be caught
540 * in testing). Callers can always actively ensure they use the correct
541 * alignment to avoid the performance hit. */
542 serialised.type_info = value->type_info;
543 serialised.data = (guchar *) g_bytes_get_data (bytes, &serialised.size);
544 serialised.depth = 0;
546 if (!g_variant_serialised_check (serialised))
548 #ifdef HAVE_POSIX_MEMALIGN
549 gpointer aligned_data = NULL;
550 gsize aligned_size = g_bytes_get_size (bytes);
552 /* posix_memalign() requires the alignment to be a multiple of
553 * sizeof(void*), and a power of 2. See g_variant_type_info_query() for
554 * details on the alignment format. */
555 if (posix_memalign (&aligned_data, MAX (sizeof (void *), alignment + 1),
557 g_error ("posix_memalign failed");
559 if (aligned_size != 0)
560 memcpy (aligned_data, g_bytes_get_data (bytes, NULL), aligned_size);
562 bytes = owned_bytes = g_bytes_new_with_free_func (aligned_data,
567 /* NOTE: there may be platforms that lack posix_memalign() and also
568 * have malloc() that returns non-8-aligned. if so, we need to try
571 bytes = owned_bytes = g_bytes_new (g_bytes_get_data (bytes, NULL),
572 g_bytes_get_size (bytes));
576 value->contents.serialised.bytes = g_bytes_ref (bytes);
578 if (size && g_bytes_get_size (bytes) != size)
580 /* Creating a fixed-sized GVariant with a bytes of the wrong
583 * We should do the equivalent of pulling a fixed-sized child out
584 * of a brozen container (ie: data is NULL size is equal to the correct
587 value->contents.serialised.data = NULL;
592 value->contents.serialised.data = g_bytes_get_data (bytes, &value->size);
595 g_clear_pointer (&owned_bytes, g_bytes_unref);
603 * g_variant_new_from_children:
604 * @type: a #GVariantType
605 * @children: an array of #GVariant pointers. Consumed.
606 * @n_children: the length of @children
607 * @trusted: %TRUE if every child in @children in trusted
609 * Constructs a new tree-mode #GVariant instance. This is the inner
610 * interface for creation of new serialized values that gets called from
611 * various functions in gvariant.c.
613 * @children is consumed by this function. g_free() will be called on
614 * it some time later.
616 * Returns: a new #GVariant with a floating reference
619 g_variant_new_from_children (const GVariantType *type,
626 value = g_variant_alloc (type, FALSE, trusted);
627 value->contents.tree.children = children;
628 value->contents.tree.n_children = n_children;
634 * g_variant_get_type_info:
635 * @value: a #GVariant
637 * Returns the #GVariantTypeInfo corresponding to the type of @value. A
638 * reference is not added, so the return value is only good for the
639 * duration of the life of @value.
641 * Returns: the #GVariantTypeInfo for @value
644 g_variant_get_type_info (GVariant *value)
646 return value->type_info;
650 * g_variant_is_trusted:
651 * @value: a #GVariant
653 * Determines if @value is trusted by #GVariant to contain only
654 * fully-valid data. All values constructed solely via #GVariant APIs
655 * are trusted, but values containing data read in from other sources
656 * are usually not trusted.
658 * The main advantage of trusted data is that certain checks can be
659 * skipped. For example, we don't need to check that a string is
660 * properly nul-terminated or that an object path is actually a
661 * properly-formatted object path.
663 * Returns: if @value is trusted
666 g_variant_is_trusted (GVariant *value)
668 return (value->state & STATE_TRUSTED) != 0;
672 * g_variant_get_depth:
673 * @value: a #GVariant
675 * Gets the nesting depth of a #GVariant. This is 0 for a #GVariant with no
678 * Returns: nesting depth of @value
681 g_variant_get_depth (GVariant *value)
690 * @value: a #GVariant
692 * Decreases the reference count of @value. When its reference count
693 * drops to 0, the memory used by the variant is freed.
698 g_variant_unref (GVariant *value)
700 g_return_if_fail (value != NULL);
702 if (g_atomic_ref_count_dec (&value->ref_count))
704 if G_UNLIKELY (value->state & STATE_LOCKED)
705 g_critical ("attempting to free a locked GVariant instance. "
706 "This should never happen.");
708 value->state |= STATE_LOCKED;
710 g_variant_type_info_unref (value->type_info);
712 if (value->state & STATE_SERIALISED)
713 g_bytes_unref (value->contents.serialised.bytes);
715 g_variant_release_children (value);
717 memset (value, 0, sizeof (GVariant));
718 g_slice_free (GVariant, value);
724 * @value: a #GVariant
726 * Increases the reference count of @value.
728 * Returns: the same @value
733 g_variant_ref (GVariant *value)
735 g_return_val_if_fail (value != NULL, NULL);
737 g_atomic_ref_count_inc (&value->ref_count);
743 * g_variant_ref_sink:
744 * @value: a #GVariant
746 * #GVariant uses a floating reference count system. All functions with
747 * names starting with `g_variant_new_` return floating
750 * Calling g_variant_ref_sink() on a #GVariant with a floating reference
751 * will convert the floating reference into a full reference. Calling
752 * g_variant_ref_sink() on a non-floating #GVariant results in an
753 * additional normal reference being added.
755 * In other words, if the @value is floating, then this call "assumes
756 * ownership" of the floating reference, converting it to a normal
757 * reference. If the @value is not floating, then this call adds a
758 * new normal reference increasing the reference count by one.
760 * All calls that result in a #GVariant instance being inserted into a
761 * container will call g_variant_ref_sink() on the instance. This means
762 * that if the value was just created (and has only its floating
763 * reference) then the container will assume sole ownership of the value
764 * at that point and the caller will not need to unreference it. This
765 * makes certain common styles of programming much easier while still
766 * maintaining normal refcounting semantics in situations where values
769 * Returns: the same @value
774 g_variant_ref_sink (GVariant *value)
776 g_return_val_if_fail (value != NULL, NULL);
777 g_return_val_if_fail (!g_atomic_ref_count_compare (&value->ref_count, 0), NULL);
779 g_variant_lock (value);
781 if (~value->state & STATE_FLOATING)
782 g_variant_ref (value);
784 value->state &= ~STATE_FLOATING;
786 g_variant_unlock (value);
792 * g_variant_take_ref:
793 * @value: a #GVariant
795 * If @value is floating, sink it. Otherwise, do nothing.
797 * Typically you want to use g_variant_ref_sink() in order to
798 * automatically do the correct thing with respect to floating or
799 * non-floating references, but there is one specific scenario where
800 * this function is helpful.
802 * The situation where this function is helpful is when creating an API
803 * that allows the user to provide a callback function that returns a
804 * #GVariant. We certainly want to allow the user the flexibility to
805 * return a non-floating reference from this callback (for the case
806 * where the value that is being returned already exists).
808 * At the same time, the style of the #GVariant API makes it likely that
809 * for newly-created #GVariant instances, the user can be saved some
810 * typing if they are allowed to return a #GVariant with a floating
813 * Using this function on the return value of the user's callback allows
814 * the user to do whichever is more convenient for them. The caller
815 * will always receives exactly one full reference to the value: either
816 * the one that was returned in the first place, or a floating reference
817 * that has been converted to a full reference.
819 * This function has an odd interaction when combined with
820 * g_variant_ref_sink() running at the same time in another thread on
821 * the same #GVariant instance. If g_variant_ref_sink() runs first then
822 * the result will be that the floating reference is converted to a hard
823 * reference. If g_variant_take_ref() runs first then the result will
824 * be that the floating reference is converted to a hard reference and
825 * an additional reference on top of that one is added. It is best to
826 * avoid this situation.
828 * Returns: the same @value
831 g_variant_take_ref (GVariant *value)
833 g_return_val_if_fail (value != NULL, NULL);
834 g_return_val_if_fail (!g_atomic_ref_count_compare (&value->ref_count, 0), NULL);
836 g_atomic_int_and (&value->state, ~STATE_FLOATING);
842 * g_variant_is_floating:
843 * @value: a #GVariant
845 * Checks whether @value has a floating reference count.
847 * This function should only ever be used to assert that a given variant
848 * is or is not floating, or for debug purposes. To acquire a reference
849 * to a variant that might be floating, always use g_variant_ref_sink()
850 * or g_variant_take_ref().
852 * See g_variant_ref_sink() for more information about floating reference
855 * Returns: whether @value is floating
860 g_variant_is_floating (GVariant *value)
862 g_return_val_if_fail (value != NULL, FALSE);
864 return (value->state & STATE_FLOATING) != 0;
868 * g_variant_get_size:
869 * @value: a #GVariant instance
871 * Determines the number of bytes that would be required to store @value
872 * with g_variant_store().
874 * If @value has a fixed-sized type then this function always returned
877 * In the case that @value is already in serialized form or the size has
878 * already been calculated (ie: this function has been called before)
879 * then this function is O(1). Otherwise, the size is calculated, an
880 * operation which is approximately O(n) in the number of values
883 * Returns: the serialized size of @value
888 g_variant_get_size (GVariant *value)
890 g_variant_lock (value);
891 g_variant_ensure_size (value);
892 g_variant_unlock (value);
898 * g_variant_get_data:
899 * @value: a #GVariant instance
901 * Returns a pointer to the serialized form of a #GVariant instance.
902 * The returned data may not be in fully-normalised form if read from an
903 * untrusted source. The returned data must not be freed; it remains
904 * valid for as long as @value exists.
906 * If @value is a fixed-sized value that was deserialized from a
907 * corrupted serialized container then %NULL may be returned. In this
908 * case, the proper thing to do is typically to use the appropriate
909 * number of nul bytes in place of @value. If @value is not fixed-sized
910 * then %NULL is never returned.
912 * In the case that @value is already in serialized form, this function
913 * is O(1). If the value is not already in serialized form,
914 * serialization occurs implicitly and is approximately O(n) in the size
917 * To deserialize the data returned by this function, in addition to the
918 * serialized data, you must know the type of the #GVariant, and (if the
919 * machine might be different) the endianness of the machine that stored
920 * it. As a result, file formats or network messages that incorporate
921 * serialized #GVariants must include this information either
922 * implicitly (for instance "the file always contains a
923 * %G_VARIANT_TYPE_VARIANT and it is always in little-endian order") or
924 * explicitly (by storing the type and/or endianness in addition to the
927 * Returns: (transfer none): the serialized form of @value, or %NULL
932 g_variant_get_data (GVariant *value)
934 g_variant_lock (value);
935 g_variant_ensure_serialised (value);
936 g_variant_unlock (value);
938 return value->contents.serialised.data;
942 * g_variant_get_data_as_bytes:
943 * @value: a #GVariant
945 * Returns a pointer to the serialized form of a #GVariant instance.
946 * The semantics of this function are exactly the same as
947 * g_variant_get_data(), except that the returned #GBytes holds
948 * a reference to the variant data.
950 * Returns: (transfer full): A new #GBytes representing the variant data
955 g_variant_get_data_as_bytes (GVariant *value)
957 const gchar *bytes_data;
962 g_variant_lock (value);
963 g_variant_ensure_serialised (value);
964 g_variant_unlock (value);
966 bytes_data = g_bytes_get_data (value->contents.serialised.bytes, &bytes_size);
967 data = value->contents.serialised.data;
972 g_assert (size == 0);
976 if (data == bytes_data && size == bytes_size)
977 return g_bytes_ref (value->contents.serialised.bytes);
979 return g_bytes_new_from_bytes (value->contents.serialised.bytes,
980 data - bytes_data, size);
985 * g_variant_n_children:
986 * @value: a container #GVariant
988 * Determines the number of children in a container #GVariant instance.
989 * This includes variants, maybes, arrays, tuples and dictionary
990 * entries. It is an error to call this function on any other type of
993 * For variants, the return value is always 1. For values with maybe
994 * types, it is always zero or one. For arrays, it is the length of the
995 * array. For tuples it is the number of tuple items (which depends
996 * only on the type). For dictionary entries, it is always 2
998 * This function is O(1).
1000 * Returns: the number of children in the container
1005 g_variant_n_children (GVariant *value)
1009 g_variant_lock (value);
1011 if (value->state & STATE_SERIALISED)
1013 GVariantSerialised serialised = {
1015 (gpointer) value->contents.serialised.data,
1020 n_children = g_variant_serialised_n_children (serialised);
1023 n_children = value->contents.tree.n_children;
1025 g_variant_unlock (value);
1031 * g_variant_get_child_value:
1032 * @value: a container #GVariant
1033 * @index_: the index of the child to fetch
1035 * Reads a child item out of a container #GVariant instance. This
1036 * includes variants, maybes, arrays, tuples and dictionary
1037 * entries. It is an error to call this function on any other type of
1040 * It is an error if @index_ is greater than the number of child items
1041 * in the container. See g_variant_n_children().
1043 * The returned value is never floating. You should free it with
1044 * g_variant_unref() when you're done with it.
1046 * Note that values borrowed from the returned child are not guaranteed to
1047 * still be valid after the child is freed even if you still hold a reference
1048 * to @value, if @value has not been serialized at the time this function is
1049 * called. To avoid this, you can serialize @value by calling
1050 * g_variant_get_data() and optionally ignoring the return value.
1052 * There may be implementation specific restrictions on deeply nested values,
1053 * which would result in the unit tuple being returned as the child value,
1054 * instead of further nested children. #GVariant is guaranteed to handle
1055 * nesting up to at least 64 levels.
1057 * This function is O(1).
1059 * Returns: (transfer full): the child at the specified index
1064 g_variant_get_child_value (GVariant *value,
1067 g_return_val_if_fail (index_ < g_variant_n_children (value), NULL);
1068 g_return_val_if_fail (value->depth < G_MAXSIZE, NULL);
1070 if (~g_atomic_int_get (&value->state) & STATE_SERIALISED)
1072 g_variant_lock (value);
1074 if (~value->state & STATE_SERIALISED)
1078 child = g_variant_ref (value->contents.tree.children[index_]);
1079 g_variant_unlock (value);
1084 g_variant_unlock (value);
1088 GVariantSerialised serialised = {
1090 (gpointer) value->contents.serialised.data,
1094 GVariantSerialised s_child;
1097 /* get the serializer to extract the serialized data for the child
1098 * from the serialized data for the container
1100 s_child = g_variant_serialised_get_child (serialised, index_);
1102 /* Check whether this would cause nesting too deep. If so, return a fake
1103 * child. The only situation we expect this to happen in is with a variant,
1104 * as all other deeply-nested types have a static type, and hence should
1105 * have been rejected earlier. In the case of a variant whose nesting plus
1106 * the depth of its child is too great, return a unit variant () instead of
1107 * the real child. */
1108 if (!(value->state & STATE_TRUSTED) &&
1109 g_variant_type_info_query_depth (s_child.type_info) >=
1110 G_VARIANT_MAX_RECURSION_DEPTH - value->depth)
1112 g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE_VARIANT));
1113 return g_variant_new_tuple (NULL, 0);
1116 /* create a new serialized instance out of it */
1117 child = g_slice_new (GVariant);
1118 child->type_info = s_child.type_info;
1119 child->state = (value->state & STATE_TRUSTED) |
1121 child->size = s_child.size;
1122 g_atomic_ref_count_init (&child->ref_count);
1123 child->depth = value->depth + 1;
1124 child->contents.serialised.bytes =
1125 g_bytes_ref (value->contents.serialised.bytes);
1126 child->contents.serialised.data = s_child.data;
1134 * @value: the #GVariant to store
1135 * @data: (not nullable): the location to store the serialized data at
1137 * Stores the serialized form of @value at @data. @data should be
1138 * large enough. See g_variant_get_size().
1140 * The stored data is in machine native byte order but may not be in
1141 * fully-normalised form if read from an untrusted source. See
1142 * g_variant_get_normal_form() for a solution.
1144 * As with g_variant_get_data(), to be able to deserialize the
1145 * serialized variant successfully, its type and (if the destination
1146 * machine might be different) its endianness must also be available.
1148 * This function is approximately O(n) in the size of @data.
1153 g_variant_store (GVariant *value,
1156 g_variant_lock (value);
1158 if (value->state & STATE_SERIALISED)
1160 if (value->contents.serialised.data != NULL)
1161 memcpy (data, value->contents.serialised.data, value->size);
1163 memset (data, 0, value->size);
1166 g_variant_serialise (value, data);
1168 g_variant_unlock (value);
1172 * g_variant_is_normal_form:
1173 * @value: a #GVariant instance
1175 * Checks if @value is in normal form.
1177 * The main reason to do this is to detect if a given chunk of
1178 * serialized data is in normal form: load the data into a #GVariant
1179 * using g_variant_new_from_data() and then use this function to
1182 * If @value is found to be in normal form then it will be marked as
1183 * being trusted. If the value was already marked as being trusted then
1184 * this function will immediately return %TRUE.
1186 * There may be implementation specific restrictions on deeply nested values.
1187 * GVariant is guaranteed to handle nesting up to at least 64 levels.
1189 * Returns: %TRUE if @value is in normal form
1194 g_variant_is_normal_form (GVariant *value)
1196 if (value->state & STATE_TRUSTED)
1199 g_variant_lock (value);
1201 if (value->depth >= G_VARIANT_MAX_RECURSION_DEPTH)
1204 if (value->state & STATE_SERIALISED)
1206 GVariantSerialised serialised = {
1208 (gpointer) value->contents.serialised.data,
1213 if (g_variant_serialised_is_normal (serialised))
1214 value->state |= STATE_TRUSTED;
1218 gboolean normal = TRUE;
1221 for (i = 0; i < value->contents.tree.n_children; i++)
1222 normal &= g_variant_is_normal_form (value->contents.tree.children[i]);
1225 value->state |= STATE_TRUSTED;
1228 g_variant_unlock (value);
1230 return (value->state & STATE_TRUSTED) != 0;