Blame glib/glib/gvariant-core.c

Packit db3073
/*
Packit db3073
 * Copyright © 2007, 2008 Ryan Lortie
Packit db3073
 * Copyright © 2010 Codethink Limited
Packit db3073
 *
Packit db3073
 * This library is free software; you can redistribute it and/or
Packit db3073
 * modify it under the terms of the GNU Lesser General Public
Packit db3073
 * License as published by the Free Software Foundation; either
Packit db3073
 * version 2 of the License, or (at your option) any later version.
Packit db3073
 *
Packit db3073
 * This library is distributed in the hope that it will be useful,
Packit db3073
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit db3073
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit db3073
 * Lesser General Public License for more details.
Packit db3073
 *
Packit db3073
 * You should have received a copy of the GNU Lesser General Public
Packit db3073
 * License along with this library; if not, write to the
Packit db3073
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit db3073
 * Boston, MA 02111-1307, USA.
Packit db3073
 */
Packit db3073
Packit db3073
#include <glib/gvariant-core.h>
Packit db3073
Packit db3073
#include <glib/gvariant-serialiser.h>
Packit db3073
#include <glib/gtestutils.h>
Packit db3073
#include <glib/gbitlock.h>
Packit db3073
#include <glib/gatomic.h>
Packit db3073
#include <glib/gbytes.h>
Packit db3073
#include <glib/gslice.h>
Packit db3073
#include <glib/gmem.h>
Packit db3073
#include <string.h>
Packit db3073
Packit db3073
Packit db3073
/*
Packit db3073
 * This file includes the structure definition for GVariant and a small
Packit db3073
 * set of functions that are allowed to access the structure directly.
Packit db3073
 *
Packit db3073
 * This minimises the amount of code that can possibly touch a GVariant
Packit db3073
 * structure directly to a few simple fundamental operations.  These few
Packit db3073
 * operations are written to be completely threadsafe with respect to
Packit db3073
 * all possible outside access.  This means that we only need to be
Packit db3073
 * concerned about thread safety issues in this one small file.
Packit db3073
 *
Packit db3073
 * Most GVariant API functions are in gvariant.c.
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * GVariant:
Packit db3073
 *
Packit db3073
 * #GVariant is an opaque data structure and can only be accessed
Packit db3073
 * using the following functions.
Packit db3073
 *
Packit db3073
 * Since: 2.24
Packit db3073
 **/
Packit db3073
struct _GVariant
Packit db3073
/* see below for field member documentation */
Packit db3073
{
Packit db3073
  GVariantTypeInfo *type_info;
Packit db3073
  gsize size;
Packit db3073
Packit db3073
  union
Packit db3073
  {
Packit db3073
    struct
Packit db3073
    {
Packit db3073
      GBytes *bytes;
Packit db3073
      gconstpointer data;
Packit db3073
    } serialised;
Packit db3073
Packit db3073
    struct
Packit db3073
    {
Packit db3073
      GVariant **children;
Packit db3073
      gsize n_children;
Packit db3073
    } tree;
Packit db3073
  } contents;
Packit db3073
Packit db3073
  gint state;
Packit db3073
  gint ref_count;
Packit db3073
};
Packit db3073
Packit db3073
/* struct GVariant:
Packit db3073
 *
Packit db3073
 * There are two primary forms of GVariant instances: "serialised form"
Packit db3073
 * and "tree form".
Packit db3073
 *
Packit db3073
 * "serialised form": A serialised GVariant instance stores its value in
Packit db3073
 *                    the GVariant serialisation format.  All
Packit db3073
 *                    basic-typed instances (ie: non-containers) are in
Packit db3073
 *                    serialised format, as are some containers.
Packit db3073
 *
Packit db3073
 * "tree form": Some containers are in "tree form".  In this case,
Packit db3073
 *              instead of containing the serialised data for the
Packit db3073
 *              container, the instance contains an array of pointers to
Packit db3073
 *              the child values of the container (thus forming a tree).
Packit db3073
 *
Packit db3073
 * It is possible for an instance to transition from tree form to
Packit db3073
 * serialised form.  This happens, implicitly, if the serialised data is
Packit db3073
 * requested (eg: via g_variant_get_data()).  Serialised form instances
Packit db3073
 * never transition into tree form.
Packit db3073
 *
Packit db3073
 *
Packit db3073
 * The fields of the structure are documented here:
Packit db3073
 *
Packit db3073
 * type_info: this is a reference to a GVariantTypeInfo describing the
Packit db3073
 *            type of the instance.  When the instance is freed, this
Packit db3073
 *            reference must be released with g_variant_type_info_unref().
Packit db3073
 *
Packit db3073
 *            The type_info field never changes during the life of the
Packit db3073
 *            instance, so it can be accessed without a lock.
Packit db3073
 *
Packit db3073
 * size: this is the size of the serialised form for the instance, if it
Packit db3073
 *       is known.  If the instance is in serialised form then it is, by
Packit db3073
 *       definition, known.  If the instance is in tree form then it may
Packit db3073
 *       be unknown (in which case it is -1).  It is possible for the
Packit db3073
 *       size to be known when in tree form if, for example, the user
Packit db3073
 *       has called g_variant_get_size() without calling
Packit db3073
 *       g_variant_get_data().  Additionally, even when the user calls
Packit db3073
 *       g_variant_get_data() the size of the data must first be
Packit db3073
 *       determined so that a large enough buffer can be allocated for
Packit db3073
 *       the data.
Packit db3073
 *
Packit db3073
 *       Once the size is known, it can never become unknown again.
Packit db3073
 *       g_variant_ensure_size() is used to ensure that the size is in
Packit db3073
 *       the known state -- it calculates the size if needed.  After
Packit db3073
 *       that, the size field can be accessed without a lock.
Packit db3073
 *
Packit db3073
 * contents: a union containing either the information associated with
Packit db3073
 *           holding a value in serialised form or holding a value in
Packit db3073
 *           tree form.
Packit db3073
 *
Packit db3073
 *   .serialised: Only valid when the instance is in serialised form.
Packit db3073
 *
Packit db3073
 *                Since an instance can never transition away from
Packit db3073
 *                serialised form, once these fields are set, they will
Packit db3073
 *                never be changed.  It is therefore valid to access
Packit db3073
 *                them without holding a lock.
Packit db3073
 *
Packit db3073
 *     .bytes:  the #GBytes that contains the memory pointed to by
Packit db3073
 *              .data, or %NULL if .data is %NULL.  In the event that
Packit db3073
 *              the instance was deserialised from another instance,
Packit db3073
 *              then the bytes will be shared by both of them.  When
Packit db3073
 *              the instance is freed, this reference must be released
Packit db3073
 *              with g_bytes_unref().
Packit db3073
 *
Packit db3073
 *     .data: the serialised data (of size 'size') of the instance.
Packit db3073
 *            This pointer should not be freed or modified in any way.
Packit db3073
 *            #GBytes is responsible for memory management.
Packit db3073
 *
Packit db3073
 *            This pointer may be %NULL in two cases:
Packit db3073
 *
Packit db3073
 *              - if the serialised size of the instance is 0
Packit db3073
 *
Packit db3073
 *              - if the instance is of a fixed-sized type and was
Packit db3073
 *                deserialised out of a corrupted container such that
Packit db3073
 *                the container contains too few bytes to point to the
Packit db3073
 *                entire proper fixed-size of this instance.  In this
Packit db3073
 *                case, 'size' will still be equal to the proper fixed
Packit db3073
 *                size, but this pointer will be %NULL.  This is exactly
Packit db3073
 *                the reason that g_variant_get_data() sometimes returns
Packit db3073
 *                %NULL.  For all other calls, the effect should be as
Packit db3073
 *                if .data pointed to the appropriate number of nul
Packit db3073
 *                bytes.
Packit db3073
 *
Packit db3073
 *   .tree: Only valid when the instance is in tree form.
Packit db3073
 *
Packit db3073
 *          Note that accesses from other threads could result in
Packit db3073
 *          conversion of the instance from tree form to serialised form
Packit db3073
 *          at any time.  For this reason, the instance lock must always
Packit db3073
 *          be held while performing any operations on 'contents.tree'.
Packit db3073
 *
Packit db3073
 *     .children: the array of the child instances of this instance.
Packit db3073
 *                When the instance is freed (or converted to serialised
Packit db3073
 *                form) then each child must have g_variant_unref()
Packit db3073
 *                called on it and the array must be freed using
Packit db3073
 *                g_free().
Packit db3073
 *
Packit db3073
 *     .n_children: the number of items in the .children array.
Packit db3073
 *
Packit db3073
 * state: a bitfield describing the state of the instance.  It is a
Packit db3073
 *        bitwise-or of the following STATE_* constants:
Packit db3073
 *
Packit db3073
 *    STATE_LOCKED: the instance lock is held.  This is the bit used by
Packit db3073
 *                  g_bit_lock().
Packit db3073
 *
Packit db3073
 *    STATE_SERIALISED: the instance is in serialised form.  If this
Packit db3073
 *                      flag is not set then the instance is in tree
Packit db3073
 *                      form.
Packit db3073
 *
Packit db3073
 *    STATE_TRUSTED: for serialised form instances, this means that the
Packit db3073
 *                   serialised data is known to be in normal form (ie:
Packit db3073
 *                   not corrupted).
Packit db3073
 *
Packit db3073
 *                   For tree form instances, this means that all of the
Packit db3073
 *                   child instances in the contents.tree.children array
Packit db3073
 *                   are trusted.  This means that if the container is
Packit db3073
 *                   serialised then the resulting data will be in
Packit db3073
 *                   normal form.
Packit db3073
 *
Packit db3073
 *                   If this flag is unset it does not imply that the
Packit db3073
 *                   data is corrupted.  It merely means that we're not
Packit db3073
 *                   sure that it's valid.  See g_variant_is_trusted().
Packit db3073
 *
Packit db3073
 *    STATE_FLOATING: if this flag is set then the object has a floating
Packit db3073
 *                    reference.  See g_variant_ref_sink().
Packit db3073
 *
Packit db3073
 * ref_count: the reference count of the instance
Packit db3073
 */
Packit db3073
#define STATE_LOCKED     1
Packit db3073
#define STATE_SERIALISED 2
Packit db3073
#define STATE_TRUSTED    4
Packit db3073
#define STATE_FLOATING   8
Packit db3073
Packit db3073
/* -- private -- */
Packit db3073
/* < private >
Packit db3073
 * g_variant_lock:
Packit db3073
 * @value: a #GVariant
Packit db3073
 *
Packit db3073
 * Locks @value for performing sensitive operations.
Packit db3073
 */
Packit db3073
static void
Packit db3073
g_variant_lock (GVariant *value)
Packit db3073
{
Packit db3073
  g_bit_lock (&value->state, 0);
Packit db3073
}
Packit db3073
Packit db3073
/* < private >
Packit db3073
 * g_variant_unlock:
Packit db3073
 * @value: a #GVariant
Packit db3073
 *
Packit db3073
 * Unlocks @value after performing sensitive operations.
Packit db3073
 */
Packit db3073
static void
Packit db3073
g_variant_unlock (GVariant *value)
Packit db3073
{
Packit db3073
  g_bit_unlock (&value->state, 0);
Packit db3073
}
Packit db3073
Packit db3073
/* < private >
Packit db3073
 * g_variant_release_children:
Packit db3073
 * @value: a #GVariant
Packit db3073
 *
Packit db3073
 * Releases the reference held on each child in the 'children' array of
Packit db3073
 * @value and frees the array itself.  @value must be in tree form.
Packit db3073
 *
Packit db3073
 * This is done when freeing a tree-form instance or converting it to
Packit db3073
 * serialised form.
Packit db3073
 *
Packit db3073
 * The current thread must hold the lock on @value.
Packit db3073
 */
Packit db3073
static void
Packit db3073
g_variant_release_children (GVariant *value)
Packit db3073
{
Packit db3073
  gsize i;
Packit db3073
Packit db3073
  g_assert (value->state & STATE_LOCKED);
Packit db3073
  g_assert (~value->state & STATE_SERIALISED);
Packit db3073
Packit db3073
  for (i = 0; i < value->contents.tree.n_children; i++)
Packit db3073
    g_variant_unref (value->contents.tree.children[i]);
Packit db3073
Packit db3073
  g_free (value->contents.tree.children);
Packit db3073
}
Packit db3073
Packit db3073
/* This begins the main body of the recursive serialiser.
Packit db3073
 *
Packit db3073
 * There are 3 functions here that work as a team with the serialiser to
Packit db3073
 * get things done.  g_variant_store() has a trivial role, but as a
Packit db3073
 * public API function, it has its definition elsewhere.
Packit db3073
 *
Packit db3073
 * Note that "serialisation" of an instance does not mean that the
Packit db3073
 * instance is converted to serialised form -- it means that the
Packit db3073
 * serialised form of an instance is written to an external buffer.
Packit db3073
 * g_variant_ensure_serialised() (which is not part of this set of
Packit db3073
 * functions) is the function that is responsible for converting an
Packit db3073
 * instance to serialised form.
Packit db3073
 *
Packit db3073
 * We are only concerned here with container types since non-container
Packit db3073
 * instances are always in serialised form.  For these instances,
Packit db3073
 * storing their serialised form merely involves a memcpy().
Packit db3073
 *
Packit db3073
 * Serialisation is a two-step process.  First, the size of the
Packit db3073
 * serialised data must be calculated so that an appropriately-sized
Packit db3073
 * buffer can be allocated.  Second, the data is written into the
Packit db3073
 * buffer.
Packit db3073
 *
Packit db3073
 * Determining the size:
Packit db3073
 *   The process of determining the size is triggered by a call to
Packit db3073
 *   g_variant_ensure_size() on a container.  This invokes the
Packit db3073
 *   serialiser code to determine the size.  The serialiser is passed
Packit db3073
 *   g_variant_fill_gvs() as a callback.
Packit db3073
 *
Packit db3073
 *   g_variant_fill_gvs() is called by the serialiser on each child of
Packit db3073
 *   the container which, in turn, calls g_variant_ensure_size() on
Packit db3073
 *   itself and fills in the result of its own size calculation.
Packit db3073
 *
Packit db3073
 *   The serialiser uses the size information from the children to
Packit db3073
 *   calculate the size needed for the entire container.
Packit db3073
 *
Packit db3073
 * Writing the data:
Packit db3073
 *   After the buffer has been allocated, g_variant_serialise() is
Packit db3073
 *   called on the container.  This invokes the serialiser code to write
Packit db3073
 *   the bytes to the container.  The serialiser is, again, passed
Packit db3073
 *   g_variant_fill_gvs() as a callback.
Packit db3073
 *
Packit db3073
 *   This time, when g_variant_fill_gvs() is called for each child, the
Packit db3073
 *   child is given a pointer to a sub-region of the allocated buffer
Packit db3073
 *   where it should write its data.  This is done by calling
Packit db3073
 *   g_variant_store().  In the event that the instance is in serialised
Packit db3073
 *   form this means a memcpy() of the serialised data into the
Packit db3073
 *   allocated buffer.  In the event that the instance is in tree form
Packit db3073
 *   this means a recursive call back into g_variant_serialise().
Packit db3073
 *
Packit db3073
 *
Packit db3073
 * The forward declaration here allows corecursion via callback:
Packit db3073
 */
Packit db3073
static void g_variant_fill_gvs (GVariantSerialised *, gpointer);
Packit db3073
Packit db3073
/* < private >
Packit db3073
 * g_variant_ensure_size:
Packit db3073
 * @value: a #GVariant
Packit db3073
 *
Packit db3073
 * Ensures that the ->size field of @value is filled in properly.  This
Packit db3073
 * must be done as a precursor to any serialisation of the value in
Packit db3073
 * order to know how large of a buffer is needed to store the data.
Packit db3073
 *
Packit db3073
 * The current thread must hold the lock on @value.
Packit db3073
 */
Packit db3073
static void
Packit db3073
g_variant_ensure_size (GVariant *value)
Packit db3073
{
Packit db3073
  g_assert (value->state & STATE_LOCKED);
Packit db3073
Packit db3073
  if (value->size == (gssize) -1)
Packit db3073
    {
Packit db3073
      gpointer *children;
Packit db3073
      gsize n_children;
Packit db3073
Packit db3073
      children = (gpointer *) value->contents.tree.children;
Packit db3073
      n_children = value->contents.tree.n_children;
Packit db3073
      value->size = g_variant_serialiser_needed_size (value->type_info,
Packit db3073
                                                      g_variant_fill_gvs,
Packit db3073
                                                      children, n_children);
Packit db3073
    }
Packit db3073
}
Packit db3073
Packit db3073
/* < private >
Packit db3073
 * g_variant_serialise:
Packit db3073
 * @value: a #GVariant
Packit db3073
 * @data: an appropriately-sized buffer
Packit db3073
 *
Packit db3073
 * Serialises @value into @data.  @value must be in tree form.
Packit db3073
 *
Packit db3073
 * No change is made to @value.
Packit db3073
 *
Packit db3073
 * The current thread must hold the lock on @value.
Packit db3073
 */
Packit db3073
static void
Packit db3073
g_variant_serialise (GVariant *value,
Packit db3073
                     gpointer  data)
Packit db3073
{
Packit db3073
  GVariantSerialised serialised = { 0, };
Packit db3073
  gpointer *children;
Packit db3073
  gsize n_children;
Packit db3073
Packit db3073
  g_assert (~value->state & STATE_SERIALISED);
Packit db3073
  g_assert (value->state & STATE_LOCKED);
Packit db3073
Packit db3073
  serialised.type_info = value->type_info;
Packit db3073
  serialised.size = value->size;
Packit db3073
  serialised.data = data;
Packit db3073
Packit db3073
  children = (gpointer *) value->contents.tree.children;
Packit db3073
  n_children = value->contents.tree.n_children;
Packit db3073
Packit db3073
  g_variant_serialiser_serialise (serialised, g_variant_fill_gvs,
Packit db3073
                                  children, n_children);
Packit db3073
}
Packit db3073
Packit db3073
/* < private >
Packit db3073
 * g_variant_fill_gvs:
Packit db3073
 * @serialised: a pointer to a #GVariantSerialised
Packit db3073
 * @data: a #GVariant instance
Packit db3073
 *
Packit db3073
 * This is the callback that is passed by a tree-form container instance
Packit db3073
 * to the serialiser.  This callback gets called on each child of the
Packit db3073
 * container.  Each child is responsible for performing the following
Packit db3073
 * actions:
Packit db3073
 *
Packit db3073
 *  - reporting its type
Packit db3073
 *
Packit db3073
 *  - reporting its serialised size (requires knowing the size first)
Packit db3073
 *
Packit db3073
 *  - possibly storing its serialised form into the provided buffer
Packit db3073
 */
Packit db3073
static void
Packit db3073
g_variant_fill_gvs (GVariantSerialised *serialised,
Packit db3073
                    gpointer            data)
Packit db3073
{
Packit db3073
  GVariant *value = data;
Packit db3073
Packit db3073
  g_variant_lock (value);
Packit db3073
  g_variant_ensure_size (value);
Packit db3073
  g_variant_unlock (value);
Packit db3073
Packit db3073
  if (serialised->type_info == NULL)
Packit db3073
    serialised->type_info = value->type_info;
Packit db3073
  g_assert (serialised->type_info == value->type_info);
Packit db3073
Packit db3073
  if (serialised->size == 0)
Packit db3073
    serialised->size = value->size;
Packit db3073
  g_assert (serialised->size == value->size);
Packit db3073
Packit db3073
  if (serialised->data)
Packit db3073
    /* g_variant_store() is a public API, so it
Packit db3073
     * it will reacquire the lock if it needs to.
Packit db3073
     */
Packit db3073
    g_variant_store (value, serialised->data);
Packit db3073
}
Packit db3073
Packit db3073
/* this ends the main body of the recursive serialiser */
Packit db3073
Packit db3073
/* < private >
Packit db3073
 * g_variant_ensure_serialised:
Packit db3073
 * @value: a #GVariant
Packit db3073
 *
Packit db3073
 * Ensures that @value is in serialised form.
Packit db3073
 *
Packit db3073
 * If @value is in tree form then this function ensures that the
Packit db3073
 * serialised size is known and then allocates a buffer of that size and
Packit db3073
 * serialises the instance into the buffer.  The 'children' array is
Packit db3073
 * then released and the instance is set to serialised form based on the
Packit db3073
 * contents of the buffer.
Packit db3073
 *
Packit db3073
 * The current thread must hold the lock on @value.
Packit db3073
 */
Packit db3073
static void
Packit db3073
g_variant_ensure_serialised (GVariant *value)
Packit db3073
{
Packit db3073
  g_assert (value->state & STATE_LOCKED);
Packit db3073
Packit db3073
  if (~value->state & STATE_SERIALISED)
Packit db3073
    {
Packit db3073
      GBytes *bytes;
Packit db3073
      gpointer data;
Packit db3073
Packit db3073
      g_variant_ensure_size (value);
Packit db3073
      data = g_malloc (value->size);
Packit db3073
      g_variant_serialise (value, data);
Packit db3073
Packit db3073
      g_variant_release_children (value);
Packit db3073
Packit db3073
      bytes = g_bytes_new_take (data, value->size);
Packit db3073
      value->contents.serialised.data = g_bytes_get_data (bytes, NULL);
Packit db3073
      value->contents.serialised.bytes = bytes;
Packit db3073
      value->state |= STATE_SERIALISED;
Packit db3073
    }
Packit db3073
}
Packit db3073
Packit db3073
/* < private >
Packit db3073
 * g_variant_alloc:
Packit db3073
 * @type: the type of the new instance
Packit db3073
 * @serialised: if the instance will be in serialised form
Packit db3073
 * @trusted: if the instance will be trusted
Packit db3073
 *
Packit db3073
 * Allocates a #GVariant instance and does some common work (such as
Packit db3073
 * looking up and filling in the type info), setting the state field,
Packit db3073
 * and setting the ref_count to 1.
Packit db3073
 *
Packit db3073
 * Returns: a new #GVariant with a floating reference
Packit db3073
 */
Packit db3073
static GVariant *
Packit db3073
g_variant_alloc (const GVariantType *type,
Packit db3073
                 gboolean            serialised,
Packit db3073
                 gboolean            trusted)
Packit db3073
{
Packit db3073
  GVariant *value;
Packit db3073
Packit db3073
  value = g_slice_new (GVariant);
Packit db3073
  value->type_info = g_variant_type_info_get (type);
Packit db3073
  value->state = (serialised ? STATE_SERIALISED : 0) |
Packit db3073
                 (trusted ? STATE_TRUSTED : 0) |
Packit db3073
                 STATE_FLOATING;
Packit db3073
  value->size = (gssize) -1;
Packit db3073
  value->ref_count = 1;
Packit db3073
Packit db3073
  return value;
Packit db3073
}
Packit db3073
Packit db3073
/* -- internal -- */
Packit db3073
/* < internal >
Packit db3073
 * g_variant_new_from_bytes:
Packit db3073
 * @type: a #GVariantType
Packit db3073
 * @bytes: a #GBytes
Packit db3073
 * @trusted: if the contents of @bytes are trusted
Packit db3073
 *
Packit db3073
 * Constructs a new serialised-mode #GVariant instance.  This is the
Packit db3073
 * inner interface for creation of new serialised values that gets
Packit db3073
 * called from various functions in gvariant.c.
Packit db3073
 *
Packit db3073
 * A reference is taken on @bytes.
Packit db3073
 *
Packit db3073
 * Returns: a new #GVariant with a floating reference
Packit db3073
 */
Packit db3073
GVariant *
Packit db3073
g_variant_new_from_bytes (const GVariantType *type,
Packit db3073
                          GBytes             *bytes,
Packit db3073
                          gboolean            trusted)
Packit db3073
{
Packit db3073
  GVariant *value;
Packit db3073
  guint alignment;
Packit db3073
  gsize size;
Packit db3073
Packit db3073
  value = g_variant_alloc (type, TRUE, trusted);
Packit db3073
Packit db3073
  value->contents.serialised.bytes = g_bytes_ref (bytes);
Packit db3073
Packit db3073
  g_variant_type_info_query (value->type_info,
Packit db3073
                             &alignment, &size);
Packit db3073
Packit db3073
  if (size && g_bytes_get_size (bytes) != size)
Packit db3073
    {
Packit db3073
      /* Creating a fixed-sized GVariant with a bytes of the wrong
Packit db3073
       * size.
Packit db3073
       *
Packit db3073
       * We should do the equivalent of pulling a fixed-sized child out
Packit db3073
       * of a brozen container (ie: data is NULL size is equal to the correct
Packit db3073
       * fixed size).
Packit db3073
       */
Packit db3073
      value->contents.serialised.data = NULL;
Packit db3073
      value->size = size;
Packit db3073
    }
Packit db3073
  else
Packit db3073
    {
Packit db3073
      value->contents.serialised.data = g_bytes_get_data (bytes, &value->size);
Packit db3073
    }
Packit db3073
Packit db3073
  return value;
Packit db3073
}
Packit db3073
Packit db3073
/* < internal >
Packit db3073
 * g_variant_new_from_children:
Packit db3073
 * @type: a #GVariantType
Packit db3073
 * @children: an array of #GVariant pointers.  Consumed.
Packit db3073
 * @n_children: the length of @children
Packit db3073
 * @trusted: %TRUE if every child in @children in trusted
Packit db3073
 *
Packit db3073
 * Constructs a new tree-mode #GVariant instance.  This is the inner
Packit db3073
 * interface for creation of new serialised values that gets called from
Packit db3073
 * various functions in gvariant.c.
Packit db3073
 *
Packit db3073
 * @children is consumed by this function.  g_free() will be called on
Packit db3073
 * it some time later.
Packit db3073
 *
Packit db3073
 * Returns: a new #GVariant with a floating reference
Packit db3073
 */
Packit db3073
GVariant *
Packit db3073
g_variant_new_from_children (const GVariantType  *type,
Packit db3073
                             GVariant           **children,
Packit db3073
                             gsize                n_children,
Packit db3073
                             gboolean             trusted)
Packit db3073
{
Packit db3073
  GVariant *value;
Packit db3073
Packit db3073
  value = g_variant_alloc (type, FALSE, trusted);
Packit db3073
  value->contents.tree.children = children;
Packit db3073
  value->contents.tree.n_children = n_children;
Packit db3073
Packit db3073
  return value;
Packit db3073
}
Packit db3073
Packit db3073
/* < internal >
Packit db3073
 * g_variant_get_type_info:
Packit db3073
 * @value: a #GVariant
Packit db3073
 *
Packit db3073
 * Returns the #GVariantTypeInfo corresponding to the type of @value.  A
Packit db3073
 * reference is not added, so the return value is only good for the
Packit db3073
 * duration of the life of @value.
Packit db3073
 *
Packit db3073
 * Returns: the #GVariantTypeInfo for @value
Packit db3073
 */
Packit db3073
GVariantTypeInfo *
Packit db3073
g_variant_get_type_info (GVariant *value)
Packit db3073
{
Packit db3073
  return value->type_info;
Packit db3073
}
Packit db3073
Packit db3073
/* < internal >
Packit db3073
 * g_variant_is_trusted:
Packit db3073
 * @value: a #GVariant
Packit db3073
 *
Packit db3073
 * Determines if @value is trusted by #GVariant to contain only
Packit db3073
 * fully-valid data.  All values constructed solely via #GVariant APIs
Packit db3073
 * are trusted, but values containing data read in from other sources
Packit db3073
 * are usually not trusted.
Packit db3073
 *
Packit db3073
 * The main advantage of trusted data is that certain checks can be
Packit db3073
 * skipped.  For example, we don't need to check that a string is
Packit db3073
 * properly nul-terminated or that an object path is actually a
Packit db3073
 * properly-formatted object path.
Packit db3073
 *
Packit db3073
 * Returns: if @value is trusted
Packit db3073
 */
Packit db3073
gboolean
Packit db3073
g_variant_is_trusted (GVariant *value)
Packit db3073
{
Packit db3073
  return (value->state & STATE_TRUSTED) != 0;
Packit db3073
}
Packit db3073
Packit db3073
/* -- public -- */
Packit db3073
Packit db3073
/**
Packit db3073
 * g_variant_unref:
Packit db3073
 * @value: a #GVariant
Packit db3073
 *
Packit db3073
 * Decreases the reference count of @value.  When its reference count
Packit db3073
 * drops to 0, the memory used by the variant is freed.
Packit db3073
 *
Packit db3073
 * Since: 2.24
Packit db3073
 **/
Packit db3073
void
Packit db3073
g_variant_unref (GVariant *value)
Packit db3073
{
Packit db3073
  g_return_if_fail (value != NULL);
Packit db3073
  g_return_if_fail (value->ref_count > 0);
Packit db3073
Packit db3073
  if (g_atomic_int_dec_and_test (&value->ref_count))
Packit db3073
    {
Packit db3073
      if G_UNLIKELY (value->state & STATE_LOCKED)
Packit db3073
        g_critical ("attempting to free a locked GVariant instance.  "
Packit db3073
                    "This should never happen.");
Packit db3073
Packit db3073
      value->state |= STATE_LOCKED;
Packit db3073
Packit db3073
      g_variant_type_info_unref (value->type_info);
Packit db3073
Packit db3073
      if (value->state & STATE_SERIALISED)
Packit db3073
        g_bytes_unref (value->contents.serialised.bytes);
Packit db3073
      else
Packit db3073
        g_variant_release_children (value);
Packit db3073
Packit db3073
      memset (value, 0, sizeof (GVariant));
Packit db3073
      g_slice_free (GVariant, value);
Packit db3073
    }
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_variant_ref:
Packit db3073
 * @value: a #GVariant
Packit db3073
 *
Packit db3073
 * Increases the reference count of @value.
Packit db3073
 *
Packit db3073
 * Returns: the same @value
Packit db3073
 *
Packit db3073
 * Since: 2.24
Packit db3073
 **/
Packit db3073
GVariant *
Packit db3073
g_variant_ref (GVariant *value)
Packit db3073
{
Packit db3073
  g_return_val_if_fail (value != NULL, NULL);
Packit db3073
  g_return_val_if_fail (value->ref_count > 0, NULL);
Packit db3073
Packit db3073
  g_atomic_int_inc (&value->ref_count);
Packit db3073
Packit db3073
  return value;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_variant_ref_sink:
Packit db3073
 * @value: a #GVariant
Packit db3073
 *
Packit db3073
 * #GVariant uses a floating reference count system.  All functions with
Packit db3073
 * names starting with <literal>g_variant_new_</literal> return floating
Packit db3073
 * references.
Packit db3073
 *
Packit db3073
 * Calling g_variant_ref_sink() on a #GVariant with a floating reference
Packit db3073
 * will convert the floating reference into a full reference.  Calling
Packit db3073
 * g_variant_ref_sink() on a non-floating #GVariant results in an
Packit db3073
 * additional normal reference being added.
Packit db3073
 *
Packit db3073
 * In other words, if the @value is floating, then this call "assumes
Packit db3073
 * ownership" of the floating reference, converting it to a normal
Packit db3073
 * reference.  If the @value is not floating, then this call adds a
Packit db3073
 * new normal reference increasing the reference count by one.
Packit db3073
 *
Packit db3073
 * All calls that result in a #GVariant instance being inserted into a
Packit db3073
 * container will call g_variant_ref_sink() on the instance.  This means
Packit db3073
 * that if the value was just created (and has only its floating
Packit db3073
 * reference) then the container will assume sole ownership of the value
Packit db3073
 * at that point and the caller will not need to unreference it.  This
Packit db3073
 * makes certain common styles of programming much easier while still
Packit db3073
 * maintaining normal refcounting semantics in situations where values
Packit db3073
 * are not floating.
Packit db3073
 *
Packit db3073
 * Returns: the same @value
Packit db3073
 *
Packit db3073
 * Since: 2.24
Packit db3073
 **/
Packit db3073
GVariant *
Packit db3073
g_variant_ref_sink (GVariant *value)
Packit db3073
{
Packit db3073
  g_return_val_if_fail (value != NULL, NULL);
Packit db3073
  g_return_val_if_fail (value->ref_count > 0, NULL);
Packit db3073
Packit db3073
  g_variant_lock (value);
Packit db3073
Packit db3073
  if (~value->state & STATE_FLOATING)
Packit db3073
    g_variant_ref (value);
Packit db3073
  else
Packit db3073
    value->state &= ~STATE_FLOATING;
Packit db3073
Packit db3073
  g_variant_unlock (value);
Packit db3073
Packit db3073
  return value;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_variant_take_ref:
Packit db3073
 * @value: a #GVariant
Packit db3073
 *
Packit db3073
 * If @value is floating, sink it.  Otherwise, do nothing.
Packit db3073
 *
Packit db3073
 * Typically you want to use g_variant_ref_sink() in order to
Packit db3073
 * automatically do the correct thing with respect to floating or
Packit db3073
 * non-floating references, but there is one specific scenario where
Packit db3073
 * this function is helpful.
Packit db3073
 *
Packit db3073
 * The situation where this function is helpful is when creating an API
Packit db3073
 * that allows the user to provide a callback function that returns a
Packit db3073
 * #GVariant.  We certainly want to allow the user the flexibility to
Packit db3073
 * return a non-floating reference from this callback (for the case
Packit db3073
 * where the value that is being returned already exists).
Packit db3073
 *
Packit db3073
 * At the same time, the style of the #GVariant API makes it likely that
Packit db3073
 * for newly-created #GVariant instances, the user can be saved some
Packit db3073
 * typing if they are allowed to return a #GVariant with a floating
Packit db3073
 * reference.
Packit db3073
 *
Packit db3073
 * Using this function on the return value of the user's callback allows
Packit db3073
 * the user to do whichever is more convenient for them.  The caller
Packit db3073
 * will alway receives exactly one full reference to the value: either
Packit db3073
 * the one that was returned in the first place, or a floating reference
Packit db3073
 * that has been converted to a full reference.
Packit db3073
 *
Packit db3073
 * This function has an odd interaction when combined with
Packit db3073
 * g_variant_ref_sink() running at the same time in another thread on
Packit db3073
 * the same #GVariant instance.  If g_variant_ref_sink() runs first then
Packit db3073
 * the result will be that the floating reference is converted to a hard
Packit db3073
 * reference.  If g_variant_take_ref() runs first then the result will
Packit db3073
 * be that the floating reference is converted to a hard reference and
Packit db3073
 * an additional reference on top of that one is added.  It is best to
Packit db3073
 * avoid this situation.
Packit db3073
 *
Packit db3073
 * Returns: the same @value
Packit db3073
 **/
Packit db3073
GVariant *
Packit db3073
g_variant_take_ref (GVariant *value)
Packit db3073
{
Packit db3073
  g_return_val_if_fail (value != NULL, NULL);
Packit db3073
  g_return_val_if_fail (value->ref_count > 0, NULL);
Packit db3073
Packit db3073
  g_atomic_int_and (&value->state, ~STATE_FLOATING);
Packit db3073
Packit db3073
  return value;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_variant_is_floating:
Packit db3073
 * @value: a #GVariant
Packit db3073
 *
Packit db3073
 * Checks whether @value has a floating reference count.
Packit db3073
 *
Packit db3073
 * This function should only ever be used to assert that a given variant
Packit db3073
 * is or is not floating, or for debug purposes. To acquire a reference
Packit db3073
 * to a variant that might be floating, always use g_variant_ref_sink()
Packit db3073
 * or g_variant_take_ref().
Packit db3073
 *
Packit db3073
 * See g_variant_ref_sink() for more information about floating reference
Packit db3073
 * counts.
Packit db3073
 *
Packit db3073
 * Returns: whether @value is floating
Packit db3073
 *
Packit db3073
 * Since: 2.26
Packit db3073
 **/
Packit db3073
gboolean
Packit db3073
g_variant_is_floating (GVariant *value)
Packit db3073
{
Packit db3073
  g_return_val_if_fail (value != NULL, FALSE);
Packit db3073
Packit db3073
  return (value->state & STATE_FLOATING) != 0;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_variant_get_size:
Packit db3073
 * @value: a #GVariant instance
Packit db3073
 *
Packit db3073
 * Determines the number of bytes that would be required to store @value
Packit db3073
 * with g_variant_store().
Packit db3073
 *
Packit db3073
 * If @value has a fixed-sized type then this function always returned
Packit db3073
 * that fixed size.
Packit db3073
 *
Packit db3073
 * In the case that @value is already in serialised form or the size has
Packit db3073
 * already been calculated (ie: this function has been called before)
Packit db3073
 * then this function is O(1).  Otherwise, the size is calculated, an
Packit db3073
 * operation which is approximately O(n) in the number of values
Packit db3073
 * involved.
Packit db3073
 *
Packit db3073
 * Returns: the serialised size of @value
Packit db3073
 *
Packit db3073
 * Since: 2.24
Packit db3073
 **/
Packit db3073
gsize
Packit db3073
g_variant_get_size (GVariant *value)
Packit db3073
{
Packit db3073
  g_variant_lock (value);
Packit db3073
  g_variant_ensure_size (value);
Packit db3073
  g_variant_unlock (value);
Packit db3073
Packit db3073
  return value->size;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_variant_get_data:
Packit db3073
 * @value: a #GVariant instance
Packit db3073
 *
Packit db3073
 * Returns a pointer to the serialised form of a #GVariant instance.
Packit db3073
 * The returned data may not be in fully-normalised form if read from an
Packit db3073
 * untrusted source.  The returned data must not be freed; it remains
Packit db3073
 * valid for as long as @value exists.
Packit db3073
 *
Packit db3073
 * If @value is a fixed-sized value that was deserialised from a
Packit db3073
 * corrupted serialised container then %NULL may be returned.  In this
Packit db3073
 * case, the proper thing to do is typically to use the appropriate
Packit db3073
 * number of nul bytes in place of @value.  If @value is not fixed-sized
Packit db3073
 * then %NULL is never returned.
Packit db3073
 *
Packit db3073
 * In the case that @value is already in serialised form, this function
Packit db3073
 * is O(1).  If the value is not already in serialised form,
Packit db3073
 * serialisation occurs implicitly and is approximately O(n) in the size
Packit db3073
 * of the result.
Packit db3073
 *
Packit db3073
 * To deserialise the data returned by this function, in addition to the
Packit db3073
 * serialised data, you must know the type of the #GVariant, and (if the
Packit db3073
 * machine might be different) the endianness of the machine that stored
Packit db3073
 * it. As a result, file formats or network messages that incorporate
Packit db3073
 * serialised #GVariants must include this information either
Packit db3073
 * implicitly (for instance "the file always contains a
Packit db3073
 * %G_VARIANT_TYPE_VARIANT and it is always in little-endian order") or
Packit db3073
 * explicitly (by storing the type and/or endianness in addition to the
Packit db3073
 * serialised data).
Packit db3073
 *
Packit db3073
 * Returns: (transfer none): the serialised form of @value, or %NULL
Packit db3073
 *
Packit db3073
 * Since: 2.24
Packit db3073
 **/
Packit db3073
gconstpointer
Packit db3073
g_variant_get_data (GVariant *value)
Packit db3073
{
Packit db3073
  g_variant_lock (value);
Packit db3073
  g_variant_ensure_serialised (value);
Packit db3073
  g_variant_unlock (value);
Packit db3073
Packit db3073
  return value->contents.serialised.data;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_variant_n_children:
Packit db3073
 * @value: a container #GVariant
Packit db3073
 *
Packit db3073
 * Determines the number of children in a container #GVariant instance.
Packit db3073
 * This includes variants, maybes, arrays, tuples and dictionary
Packit db3073
 * entries.  It is an error to call this function on any other type of
Packit db3073
 * #GVariant.
Packit db3073
 *
Packit db3073
 * For variants, the return value is always 1.  For values with maybe
Packit db3073
 * types, it is always zero or one.  For arrays, it is the length of the
Packit db3073
 * array.  For tuples it is the number of tuple items (which depends
Packit db3073
 * only on the type).  For dictionary entries, it is always 2
Packit db3073
 *
Packit db3073
 * This function is O(1).
Packit db3073
 *
Packit db3073
 * Returns: the number of children in the container
Packit db3073
 *
Packit db3073
 * Since: 2.24
Packit db3073
 **/
Packit db3073
gsize
Packit db3073
g_variant_n_children (GVariant *value)
Packit db3073
{
Packit db3073
  gsize n_children;
Packit db3073
Packit db3073
  g_variant_lock (value);
Packit db3073
Packit db3073
  if (value->state & STATE_SERIALISED)
Packit db3073
    {
Packit db3073
      GVariantSerialised serialised = {
Packit db3073
        value->type_info,
Packit db3073
        (gpointer) value->contents.serialised.data,
Packit db3073
        value->size
Packit db3073
      };
Packit db3073
Packit db3073
      n_children = g_variant_serialised_n_children (serialised);
Packit db3073
    }
Packit db3073
  else
Packit db3073
    n_children = value->contents.tree.n_children;
Packit db3073
Packit db3073
  g_variant_unlock (value);
Packit db3073
Packit db3073
  return n_children;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_variant_get_child_value:
Packit db3073
 * @value: a container #GVariant
Packit db3073
 * @index_: the index of the child to fetch
Packit db3073
 *
Packit db3073
 * Reads a child item out of a container #GVariant instance.  This
Packit db3073
 * includes variants, maybes, arrays, tuples and dictionary
Packit db3073
 * entries.  It is an error to call this function on any other type of
Packit db3073
 * #GVariant.
Packit db3073
 *
Packit db3073
 * It is an error if @index_ is greater than the number of child items
Packit db3073
 * in the container.  See g_variant_n_children().
Packit db3073
 *
Packit db3073
 * The returned value is never floating.  You should free it with
Packit db3073
 * g_variant_unref() when you're done with it.
Packit db3073
 *
Packit db3073
 * This function is O(1).
Packit db3073
 *
Packit db3073
 * Returns: (transfer full): the child at the specified index
Packit db3073
 *
Packit db3073
 * Since: 2.24
Packit db3073
 **/
Packit db3073
GVariant *
Packit db3073
g_variant_get_child_value (GVariant *value,
Packit db3073
                           gsize     index_)
Packit db3073
{
Packit db3073
  g_return_val_if_fail (index_ < g_variant_n_children (value), NULL);
Packit db3073
Packit db3073
  if (~g_atomic_int_get (&value->state) & STATE_SERIALISED)
Packit db3073
    {
Packit db3073
      g_variant_lock (value);
Packit db3073
Packit db3073
      if (~value->state & STATE_SERIALISED)
Packit db3073
        {
Packit db3073
          GVariant *child;
Packit db3073
Packit db3073
          child = g_variant_ref (value->contents.tree.children[index_]);
Packit db3073
          g_variant_unlock (value);
Packit db3073
Packit db3073
          return child;
Packit db3073
        }
Packit db3073
Packit db3073
      g_variant_unlock (value);
Packit db3073
    }
Packit db3073
Packit db3073
  {
Packit db3073
    GVariantSerialised serialised = {
Packit db3073
      value->type_info,
Packit db3073
      (gpointer) value->contents.serialised.data,
Packit db3073
      value->size
Packit db3073
    };
Packit db3073
    GVariantSerialised s_child;
Packit db3073
    GVariant *child;
Packit db3073
Packit db3073
    /* get the serialiser to extract the serialised data for the child
Packit db3073
     * from the serialised data for the container
Packit db3073
     */
Packit db3073
    s_child = g_variant_serialised_get_child (serialised, index_);
Packit db3073
Packit db3073
    /* create a new serialised instance out of it */
Packit db3073
    child = g_slice_new (GVariant);
Packit db3073
    child->type_info = s_child.type_info;
Packit db3073
    child->state = (value->state & STATE_TRUSTED) |
Packit db3073
                   STATE_SERIALISED;
Packit db3073
    child->size = s_child.size;
Packit db3073
    child->ref_count = 1;
Packit db3073
    child->contents.serialised.bytes =
Packit db3073
      g_bytes_ref (value->contents.serialised.bytes);
Packit db3073
    child->contents.serialised.data = s_child.data;
Packit db3073
Packit db3073
    return child;
Packit db3073
  }
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_variant_store:
Packit db3073
 * @value: the #GVariant to store
Packit db3073
 * @data: the location to store the serialised data at
Packit db3073
 *
Packit db3073
 * Stores the serialised form of @value at @data.  @data should be
Packit db3073
 * large enough.  See g_variant_get_size().
Packit db3073
 *
Packit db3073
 * The stored data is in machine native byte order but may not be in
Packit db3073
 * fully-normalised form if read from an untrusted source.  See
Packit db3073
 * g_variant_get_normal_form() for a solution.
Packit db3073
 *
Packit db3073
 * As with g_variant_get_data(), to be able to deserialise the
Packit db3073
 * serialised variant successfully, its type and (if the destination
Packit db3073
 * machine might be different) its endianness must also be available.
Packit db3073
 *
Packit db3073
 * This function is approximately O(n) in the size of @data.
Packit db3073
 *
Packit db3073
 * Since: 2.24
Packit db3073
 **/
Packit db3073
void
Packit db3073
g_variant_store (GVariant *value,
Packit db3073
                 gpointer  data)
Packit db3073
{
Packit db3073
  g_variant_lock (value);
Packit db3073
Packit db3073
  if (value->state & STATE_SERIALISED)
Packit db3073
    {
Packit db3073
      if (value->contents.serialised.data != NULL)
Packit db3073
        memcpy (data, value->contents.serialised.data, value->size);
Packit db3073
      else
Packit db3073
        memset (data, 0, value->size);
Packit db3073
    }
Packit db3073
  else
Packit db3073
    g_variant_serialise (value, data);
Packit db3073
Packit db3073
  g_variant_unlock (value);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_variant_is_normal_form:
Packit db3073
 * @value: a #GVariant instance
Packit db3073
 *
Packit db3073
 * Checks if @value is in normal form.
Packit db3073
 *
Packit db3073
 * The main reason to do this is to detect if a given chunk of
Packit db3073
 * serialised data is in normal form: load the data into a #GVariant
Packit db3073
 * using g_variant_new_from_data() and then use this function to
Packit db3073
 * check.
Packit db3073
 *
Packit db3073
 * If @value is found to be in normal form then it will be marked as
Packit db3073
 * being trusted.  If the value was already marked as being trusted then
Packit db3073
 * this function will immediately return %TRUE.
Packit db3073
 *
Packit db3073
 * Returns: %TRUE if @value is in normal form
Packit db3073
 *
Packit db3073
 * Since: 2.24
Packit db3073
 **/
Packit db3073
gboolean
Packit db3073
g_variant_is_normal_form (GVariant *value)
Packit db3073
{
Packit db3073
  if (value->state & STATE_TRUSTED)
Packit db3073
    return TRUE;
Packit db3073
Packit db3073
  g_variant_lock (value);
Packit db3073
Packit db3073
  if (value->state & STATE_SERIALISED)
Packit db3073
    {
Packit db3073
      GVariantSerialised serialised = {
Packit db3073
        value->type_info,
Packit db3073
        (gpointer) value->contents.serialised.data,
Packit db3073
        value->size
Packit db3073
      };
Packit db3073
Packit db3073
      if (g_variant_serialised_is_normal (serialised))
Packit db3073
        value->state |= STATE_TRUSTED;
Packit db3073
    }
Packit db3073
  else
Packit db3073
    {
Packit db3073
      gboolean normal = TRUE;
Packit db3073
      gsize i;
Packit db3073
Packit db3073
      for (i = 0; i < value->contents.tree.n_children; i++)
Packit db3073
        normal &= g_variant_is_normal_form (value->contents.tree.children[i]);
Packit db3073
Packit db3073
      if (normal)
Packit db3073
        value->state |= STATE_TRUSTED;
Packit db3073
    }
Packit db3073
Packit db3073
  g_variant_unlock (value);
Packit db3073
Packit db3073
  return (value->state & STATE_TRUSTED) != 0;
Packit db3073
}