+ return *g_variant_get_type_string (value);
+}
+
+/* Pretty printer {{{1 */
+/* This function is not introspectable because if @string is NULL,
+ @returns is (transfer full), otherwise it is (transfer none), which
+ is not supported by GObjectIntrospection */
+/**
+ * g_variant_print_string: (skip)
+ * @value: a #GVariant
+ * @string: (allow-none) (default NULL): a #GString, or %NULL
+ * @type_annotate: %TRUE if type information should be included in
+ * the output
+ *
+ * Behaves as g_variant_print(), but operates on a #GString.
+ *
+ * If @string is non-%NULL then it is appended to and returned. Else,
+ * a new empty #GString is allocated and it is returned.
+ *
+ * Returns: a #GString containing the string
+ *
+ * Since: 2.24
+ **/
+GString *
+g_variant_print_string (GVariant *value,
+ GString *string,
+ gboolean type_annotate)
+{
+ if G_UNLIKELY (string == NULL)
+ string = g_string_new (NULL);
+
+ switch (g_variant_classify (value))
+ {
+ case G_VARIANT_CLASS_MAYBE:
+ if (type_annotate)
+ g_string_append_printf (string, "@%s ",
+ g_variant_get_type_string (value));
+
+ if (g_variant_n_children (value))
+ {
+ gchar *printed_child;
+ GVariant *element;
+
+ /* Nested maybes:
+ *
+ * Consider the case of the type "mmi". In this case we could
+ * write "just just 4", but "4" alone is totally unambiguous,
+ * so we try to drop "just" where possible.
+ *
+ * We have to be careful not to always drop "just", though,
+ * since "nothing" needs to be distinguishable from "just
+ * nothing". The case where we need to ensure we keep the
+ * "just" is actually exactly the case where we have a nested
+ * Nothing.
+ *
+ * Instead of searching for that nested Nothing, we just print
+ * the contained value into a separate string and see if we
+ * end up with "nothing" at the end of it. If so, we need to
+ * add "just" at our level.
+ */
+ element = g_variant_get_child_value (value, 0);
+ printed_child = g_variant_print (element, FALSE);
+ g_variant_unref (element);
+
+ if (g_str_has_suffix (printed_child, "nothing"))
+ g_string_append (string, "just ");
+ g_string_append (string, printed_child);
+ g_free (printed_child);
+ }
+ else
+ g_string_append (string, "nothing");
+
+ break;
+
+ case G_VARIANT_CLASS_ARRAY:
+ /* it's an array so the first character of the type string is 'a'
+ *
+ * if the first two characters are 'ay' then it's a bytestring.
+ * under certain conditions we print those as strings.
+ */
+ if (g_variant_get_type_string (value)[1] == 'y')
+ {
+ const gchar *str;
+ gsize size;
+ gsize i;
+
+ /* first determine if it is a byte string.
+ * that's when there's a single nul character: at the end.
+ */
+ str = g_variant_get_data (value);
+ size = g_variant_get_size (value);
+
+ for (i = 0; i < size; i++)
+ if (str[i] == '\0')
+ break;
+
+ /* first nul byte is the last byte -> it's a byte string. */
+ if (i == size - 1)
+ {
+ gchar *escaped = g_strescape (str, NULL);
+
+ /* use double quotes only if a ' is in the string */
+ if (strchr (str, '\''))
+ g_string_append_printf (string, "b\"%s\"", escaped);
+ else
+ g_string_append_printf (string, "b'%s'", escaped);
+
+ g_free (escaped);
+ break;
+ }
+
+ else
+ /* fall through and handle normally... */;
+ }
+
+ /*
+ * if the first two characters are 'a{' then it's an array of
+ * dictionary entries (ie: a dictionary) so we print that
+ * differently.
+ */
+ if (g_variant_get_type_string (value)[1] == '{')
+ /* dictionary */
+ {
+ const gchar *comma = "";
+ gsize n, i;
+
+ if ((n = g_variant_n_children (value)) == 0)
+ {
+ if (type_annotate)
+ g_string_append_printf (string, "@%s ",
+ g_variant_get_type_string (value));
+ g_string_append (string, "{}");
+ break;
+ }
+
+ g_string_append_c (string, '{');
+ for (i = 0; i < n; i++)
+ {
+ GVariant *entry, *key, *val;
+
+ g_string_append (string, comma);
+ comma = ", ";
+
+ entry = g_variant_get_child_value (value, i);
+ key = g_variant_get_child_value (entry, 0);
+ val = g_variant_get_child_value (entry, 1);
+ g_variant_unref (entry);
+
+ g_variant_print_string (key, string, type_annotate);
+ g_variant_unref (key);
+ g_string_append (string, ": ");
+ g_variant_print_string (val, string, type_annotate);
+ g_variant_unref (val);
+ type_annotate = FALSE;
+ }
+ g_string_append_c (string, '}');
+ }
+ else
+ /* normal (non-dictionary) array */
+ {
+ const gchar *comma = "";
+ gsize n, i;
+
+ if ((n = g_variant_n_children (value)) == 0)
+ {
+ if (type_annotate)
+ g_string_append_printf (string, "@%s ",
+ g_variant_get_type_string (value));
+ g_string_append (string, "[]");
+ break;
+ }
+
+ g_string_append_c (string, '[');
+ for (i = 0; i < n; i++)
+ {
+ GVariant *element;
+
+ g_string_append (string, comma);
+ comma = ", ";
+
+ element = g_variant_get_child_value (value, i);
+
+ g_variant_print_string (element, string, type_annotate);
+ g_variant_unref (element);
+ type_annotate = FALSE;
+ }
+ g_string_append_c (string, ']');
+ }
+
+ break;
+
+ case G_VARIANT_CLASS_TUPLE:
+ {
+ gsize n, i;
+
+ n = g_variant_n_children (value);
+
+ g_string_append_c (string, '(');
+ for (i = 0; i < n; i++)
+ {
+ GVariant *element;
+
+ element = g_variant_get_child_value (value, i);
+ g_variant_print_string (element, string, type_annotate);
+ g_string_append (string, ", ");
+ g_variant_unref (element);
+ }
+
+ /* for >1 item: remove final ", "
+ * for 1 item: remove final " ", but leave the ","
+ * for 0 items: there is only "(", so remove nothing
+ */
+ g_string_truncate (string, string->len - (n > 0) - (n > 1));
+ g_string_append_c (string, ')');
+ }
+ break;
+
+ case G_VARIANT_CLASS_DICT_ENTRY:
+ {
+ GVariant *element;
+
+ g_string_append_c (string, '{');
+
+ element = g_variant_get_child_value (value, 0);
+ g_variant_print_string (element, string, type_annotate);
+ g_variant_unref (element);
+
+ g_string_append (string, ", ");
+
+ element = g_variant_get_child_value (value, 1);
+ g_variant_print_string (element, string, type_annotate);
+ g_variant_unref (element);
+
+ g_string_append_c (string, '}');
+ }
+ break;
+
+ case G_VARIANT_CLASS_VARIANT:
+ {
+ GVariant *child = g_variant_get_variant (value);
+
+ /* Always annotate types in nested variants, because they are
+ * (by nature) of variable type.
+ */
+ g_string_append_c (string, '<');
+ g_variant_print_string (child, string, TRUE);
+ g_string_append_c (string, '>');
+
+ g_variant_unref (child);
+ }
+ break;
+
+ case G_VARIANT_CLASS_BOOLEAN:
+ if (g_variant_get_boolean (value))
+ g_string_append (string, "true");
+ else
+ g_string_append (string, "false");
+ break;
+
+ case G_VARIANT_CLASS_STRING:
+ {
+ const gchar *str = g_variant_get_string (value, NULL);
+ gunichar quote = strchr (str, '\'') ? '"' : '\'';
+
+ g_string_append_c (string, quote);
+
+ while (*str)
+ {
+ gunichar c = g_utf8_get_char (str);
+
+ if (c == quote || c == '\\')
+ g_string_append_c (string, '\\');
+
+ if (g_unichar_isprint (c))
+ g_string_append_unichar (string, c);
+
+ else
+ {
+ g_string_append_c (string, '\\');
+ if (c < 0x10000)
+ switch (c)
+ {
+ case '\a':
+ g_string_append_c (string, 'a');
+ break;
+
+ case '\b':
+ g_string_append_c (string, 'b');
+ break;
+
+ case '\f':
+ g_string_append_c (string, 'f');
+ break;
+
+ case '\n':
+ g_string_append_c (string, 'n');
+ break;
+
+ case '\r':
+ g_string_append_c (string, 'r');
+ break;
+
+ case '\t':
+ g_string_append_c (string, 't');
+ break;
+
+ case '\v':
+ g_string_append_c (string, 'v');
+ break;
+
+ default:
+ g_string_append_printf (string, "u%04x", c);
+ break;
+ }
+ else
+ g_string_append_printf (string, "U%08x", c);
+ }
+
+ str = g_utf8_next_char (str);
+ }
+
+ g_string_append_c (string, quote);
+ }
+ break;
+
+ case G_VARIANT_CLASS_BYTE:
+ if (type_annotate)
+ g_string_append (string, "byte ");
+ g_string_append_printf (string, "0x%02x",
+ g_variant_get_byte (value));
+ break;
+
+ case G_VARIANT_CLASS_INT16:
+ if (type_annotate)
+ g_string_append (string, "int16 ");
+ g_string_append_printf (string, "%"G_GINT16_FORMAT,
+ g_variant_get_int16 (value));
+ break;
+
+ case G_VARIANT_CLASS_UINT16:
+ if (type_annotate)
+ g_string_append (string, "uint16 ");
+ g_string_append_printf (string, "%"G_GUINT16_FORMAT,
+ g_variant_get_uint16 (value));
+ break;
+
+ case G_VARIANT_CLASS_INT32:
+ /* Never annotate this type because it is the default for numbers
+ * (and this is a *pretty* printer)
+ */
+ g_string_append_printf (string, "%"G_GINT32_FORMAT,
+ g_variant_get_int32 (value));
+ break;
+
+ case G_VARIANT_CLASS_HANDLE:
+ if (type_annotate)
+ g_string_append (string, "handle ");
+ g_string_append_printf (string, "%"G_GINT32_FORMAT,
+ g_variant_get_handle (value));
+ break;
+
+ case G_VARIANT_CLASS_UINT32:
+ if (type_annotate)
+ g_string_append (string, "uint32 ");
+ g_string_append_printf (string, "%"G_GUINT32_FORMAT,
+ g_variant_get_uint32 (value));
+ break;
+
+ case G_VARIANT_CLASS_INT64:
+ if (type_annotate)
+ g_string_append (string, "int64 ");
+ g_string_append_printf (string, "%"G_GINT64_FORMAT,
+ g_variant_get_int64 (value));
+ break;
+
+ case G_VARIANT_CLASS_UINT64:
+ if (type_annotate)
+ g_string_append (string, "uint64 ");
+ g_string_append_printf (string, "%"G_GUINT64_FORMAT,
+ g_variant_get_uint64 (value));
+ break;
+
+ case G_VARIANT_CLASS_DOUBLE:
+ {
+ gchar buffer[100];
+ gint i;
+
+ g_ascii_dtostr (buffer, sizeof buffer, g_variant_get_double (value));
+
+ for (i = 0; buffer[i]; i++)
+ if (buffer[i] == '.' || buffer[i] == 'e' ||
+ buffer[i] == 'n' || buffer[i] == 'N')
+ break;
+
+ /* if there is no '.' or 'e' in the float then add one */
+ if (buffer[i] == '\0')
+ {
+ buffer[i++] = '.';
+ buffer[i++] = '0';
+ buffer[i++] = '\0';
+ }
+
+ g_string_append (string, buffer);
+ }
+ break;
+
+ case G_VARIANT_CLASS_OBJECT_PATH:
+ if (type_annotate)
+ g_string_append (string, "objectpath ");
+ g_string_append_printf (string, "\'%s\'",
+ g_variant_get_string (value, NULL));
+ break;
+
+ case G_VARIANT_CLASS_SIGNATURE:
+ if (type_annotate)
+ g_string_append (string, "signature ");
+ g_string_append_printf (string, "\'%s\'",
+ g_variant_get_string (value, NULL));
+ break;
+
+ default:
+ g_assert_not_reached ();
+ }
+
+ return string;
+}
+
+/**
+ * g_variant_print:
+ * @value: a #GVariant
+ * @type_annotate: %TRUE if type information should be included in
+ * the output
+ *
+ * Pretty-prints @value in the format understood by g_variant_parse().
+ *
+ * The format is described <link linkend='gvariant-text'>here</link>.
+ *
+ * If @type_annotate is %TRUE, then type information is included in
+ * the output.
+ *
+ * Returns: (transfer full): a newly-allocated string holding the result.
+ *
+ * Since: 2.24
+ */
+gchar *
+g_variant_print (GVariant *value,
+ gboolean type_annotate)
+{
+ return g_string_free (g_variant_print_string (value, NULL, type_annotate),
+ FALSE);
+};
+
+/* Hash, Equal, Compare {{{1 */
+/**
+ * g_variant_hash:
+ * @value: (type GVariant): a basic #GVariant value as a #gconstpointer
+ *
+ * Generates a hash value for a #GVariant instance.
+ *
+ * The output of this function is guaranteed to be the same for a given
+ * value only per-process. It may change between different processor
+ * architectures or even different versions of GLib. Do not use this
+ * function as a basis for building protocols or file formats.
+ *
+ * The type of @value is #gconstpointer only to allow use of this
+ * function with #GHashTable. @value must be a #GVariant.
+ *
+ * Returns: a hash value corresponding to @value
+ *
+ * Since: 2.24
+ **/
+guint
+g_variant_hash (gconstpointer value_)
+{
+ GVariant *value = (GVariant *) value_;
+
+ switch (g_variant_classify (value))
+ {
+ case G_VARIANT_CLASS_STRING:
+ case G_VARIANT_CLASS_OBJECT_PATH:
+ case G_VARIANT_CLASS_SIGNATURE:
+ return g_str_hash (g_variant_get_string (value, NULL));
+
+ case G_VARIANT_CLASS_BOOLEAN:
+ /* this is a very odd thing to hash... */
+ return g_variant_get_boolean (value);
+
+ case G_VARIANT_CLASS_BYTE:
+ return g_variant_get_byte (value);
+
+ case G_VARIANT_CLASS_INT16:
+ case G_VARIANT_CLASS_UINT16:
+ {
+ const guint16 *ptr;
+
+ ptr = g_variant_get_data (value);
+
+ if (ptr)
+ return *ptr;
+ else
+ return 0;
+ }
+
+ case G_VARIANT_CLASS_INT32:
+ case G_VARIANT_CLASS_UINT32:
+ case G_VARIANT_CLASS_HANDLE:
+ {
+ const guint *ptr;
+
+ ptr = g_variant_get_data (value);
+
+ if (ptr)
+ return *ptr;
+ else
+ return 0;
+ }
+
+ case G_VARIANT_CLASS_INT64:
+ case G_VARIANT_CLASS_UINT64:
+ case G_VARIANT_CLASS_DOUBLE:
+ /* need a separate case for these guys because otherwise
+ * performance could be quite bad on big endian systems
+ */
+ {
+ const guint *ptr;
+
+ ptr = g_variant_get_data (value);
+
+ if (ptr)
+ return ptr[0] + ptr[1];
+ else
+ return 0;
+ }
+
+ default:
+ g_return_val_if_fail (!g_variant_is_container (value), 0);
+ g_assert_not_reached ();
+ }
+}
+
+/**
+ * g_variant_equal:
+ * @one: (type GVariant): a #GVariant instance
+ * @two: (type GVariant): a #GVariant instance
+ *
+ * Checks if @one and @two have the same type and value.
+ *
+ * The types of @one and @two are #gconstpointer only to allow use of
+ * this function with #GHashTable. They must each be a #GVariant.
+ *
+ * Returns: %TRUE if @one and @two are equal
+ *
+ * Since: 2.24
+ **/
+gboolean
+g_variant_equal (gconstpointer one,
+ gconstpointer two)
+{
+ gboolean equal;
+
+ g_return_val_if_fail (one != NULL && two != NULL, FALSE);
+
+ if (g_variant_get_type_info ((GVariant *) one) !=
+ g_variant_get_type_info ((GVariant *) two))
+ return FALSE;
+
+ /* if both values are trusted to be in their canonical serialised form
+ * then a simple memcmp() of their serialised data will answer the
+ * question.
+ *
+ * if not, then this might generate a false negative (since it is
+ * possible for two different byte sequences to represent the same
+ * value). for now we solve this by pretty-printing both values and
+ * comparing the result.
+ */
+ if (g_variant_is_trusted ((GVariant *) one) &&
+ g_variant_is_trusted ((GVariant *) two))
+ {
+ gconstpointer data_one, data_two;
+ gsize size_one, size_two;
+
+ size_one = g_variant_get_size ((GVariant *) one);
+ size_two = g_variant_get_size ((GVariant *) two);
+
+ if (size_one != size_two)
+ return FALSE;
+
+ data_one = g_variant_get_data ((GVariant *) one);
+ data_two = g_variant_get_data ((GVariant *) two);
+
+ equal = memcmp (data_one, data_two, size_one) == 0;
+ }
+ else
+ {
+ gchar *strone, *strtwo;
+
+ strone = g_variant_print ((GVariant *) one, FALSE);
+ strtwo = g_variant_print ((GVariant *) two, FALSE);
+ equal = strcmp (strone, strtwo) == 0;
+ g_free (strone);
+ g_free (strtwo);
+ }
+
+ return equal;
+}
+
+/**
+ * g_variant_compare:
+ * @one: (type GVariant): a basic-typed #GVariant instance
+ * @two: (type GVariant): a #GVariant instance of the same type
+ *
+ * Compares @one and @two.
+ *
+ * The types of @one and @two are #gconstpointer only to allow use of
+ * this function with #GTree, #GPtrArray, etc. They must each be a
+ * #GVariant.
+ *
+ * Comparison is only defined for basic types (ie: booleans, numbers,
+ * strings). For booleans, %FALSE is less than %TRUE. Numbers are
+ * ordered in the usual way. Strings are in ASCII lexographical order.
+ *
+ * It is a programmer error to attempt to compare container values or
+ * two values that have types that are not exactly equal. For example,
+ * you cannot compare a 32-bit signed integer with a 32-bit unsigned
+ * integer. Also note that this function is not particularly
+ * well-behaved when it comes to comparison of doubles; in particular,
+ * the handling of incomparable values (ie: NaN) is undefined.
+ *
+ * If you only require an equality comparison, g_variant_equal() is more
+ * general.
+ *
+ * Returns: negative value if a < b;
+ * zero if a = b;
+ * positive value if a > b.
+ *
+ * Since: 2.26
+ **/
+gint
+g_variant_compare (gconstpointer one,
+ gconstpointer two)
+{
+ GVariant *a = (GVariant *) one;
+ GVariant *b = (GVariant *) two;
+
+ g_return_val_if_fail (g_variant_classify (a) == g_variant_classify (b), 0);
+
+ switch (g_variant_classify (a))
+ {
+ case G_VARIANT_CLASS_BOOLEAN:
+ return g_variant_get_boolean (a) -
+ g_variant_get_boolean (b);
+
+ case G_VARIANT_CLASS_BYTE:
+ return ((gint) g_variant_get_byte (a)) -
+ ((gint) g_variant_get_byte (b));
+
+ case G_VARIANT_CLASS_INT16:
+ return ((gint) g_variant_get_int16 (a)) -
+ ((gint) g_variant_get_int16 (b));
+
+ case G_VARIANT_CLASS_UINT16:
+ return ((gint) g_variant_get_uint16 (a)) -
+ ((gint) g_variant_get_uint16 (b));
+
+ case G_VARIANT_CLASS_INT32:
+ {
+ gint32 a_val = g_variant_get_int32 (a);
+ gint32 b_val = g_variant_get_int32 (b);
+
+ return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1;
+ }
+
+ case G_VARIANT_CLASS_UINT32:
+ {
+ guint32 a_val = g_variant_get_uint32 (a);
+ guint32 b_val = g_variant_get_uint32 (b);
+
+ return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1;
+ }
+
+ case G_VARIANT_CLASS_INT64:
+ {
+ gint64 a_val = g_variant_get_int64 (a);
+ gint64 b_val = g_variant_get_int64 (b);
+
+ return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1;
+ }
+
+ case G_VARIANT_CLASS_UINT64:
+ {
+ guint64 a_val = g_variant_get_uint64 (a);
+ guint64 b_val = g_variant_get_uint64 (b);
+
+ return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1;
+ }
+
+ case G_VARIANT_CLASS_DOUBLE:
+ {
+ gdouble a_val = g_variant_get_double (a);
+ gdouble b_val = g_variant_get_double (b);
+
+ return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1;
+ }
+
+ case G_VARIANT_CLASS_STRING:
+ case G_VARIANT_CLASS_OBJECT_PATH:
+ case G_VARIANT_CLASS_SIGNATURE:
+ return strcmp (g_variant_get_string (a, NULL),
+ g_variant_get_string (b, NULL));
+
+ default:
+ g_return_val_if_fail (!g_variant_is_container (a), 0);
+ g_assert_not_reached ();
+ }
+}
+
+/* GVariantIter {{{1 */
+/**
+ * GVariantIter: (skip)
+ *
+ * #GVariantIter is an opaque data structure and can only be accessed
+ * using the following functions.
+ **/
+struct stack_iter
+{
+ GVariant *value;
+ gssize n, i;
+
+ const gchar *loop_format;
+
+ gsize padding[3];
+ gsize magic;
+};
+
+G_STATIC_ASSERT (sizeof (struct stack_iter) <= sizeof (GVariantIter));
+
+struct heap_iter
+{
+ struct stack_iter iter;
+
+ GVariant *value_ref;
+ gsize magic;
+};
+
+#define GVSI(i) ((struct stack_iter *) (i))
+#define GVHI(i) ((struct heap_iter *) (i))
+#define GVSI_MAGIC ((gsize) 3579507750u)
+#define GVHI_MAGIC ((gsize) 1450270775u)
+#define is_valid_iter(i) (i != NULL && \
+ GVSI(i)->magic == GVSI_MAGIC)
+#define is_valid_heap_iter(i) (GVHI(i)->magic == GVHI_MAGIC && \
+ is_valid_iter(i))
+
+/**
+ * g_variant_iter_new:
+ * @value: a container #GVariant
+ *
+ * Creates a heap-allocated #GVariantIter for iterating over the items
+ * in @value.
+ *
+ * Use g_variant_iter_free() to free the return value when you no longer
+ * need it.
+ *
+ * A reference is taken to @value and will be released only when
+ * g_variant_iter_free() is called.
+ *
+ * Returns: (transfer full): a new heap-allocated #GVariantIter
+ *
+ * Since: 2.24
+ **/
+GVariantIter *
+g_variant_iter_new (GVariant *value)
+{
+ GVariantIter *iter;
+
+ iter = (GVariantIter *) g_slice_new (struct heap_iter);
+ GVHI(iter)->value_ref = g_variant_ref (value);
+ GVHI(iter)->magic = GVHI_MAGIC;
+
+ g_variant_iter_init (iter, value);
+
+ return iter;
+}
+
+/**
+ * g_variant_iter_init: (skip)
+ * @iter: a pointer to a #GVariantIter
+ * @value: a container #GVariant
+ *
+ * Initialises (without allocating) a #GVariantIter. @iter may be
+ * completely uninitialised prior to this call; its old value is
+ * ignored.
+ *
+ * The iterator remains valid for as long as @value exists, and need not
+ * be freed in any way.
+ *
+ * Returns: the number of items in @value
+ *
+ * Since: 2.24
+ **/
+gsize
+g_variant_iter_init (GVariantIter *iter,
+ GVariant *value)
+{
+ GVSI(iter)->magic = GVSI_MAGIC;
+ GVSI(iter)->value = value;
+ GVSI(iter)->n = g_variant_n_children (value);
+ GVSI(iter)->i = -1;
+ GVSI(iter)->loop_format = NULL;
+
+ return GVSI(iter)->n;
+}
+
+/**
+ * g_variant_iter_copy:
+ * @iter: a #GVariantIter
+ *
+ * Creates a new heap-allocated #GVariantIter to iterate over the
+ * container that was being iterated over by @iter. Iteration begins on
+ * the new iterator from the current position of the old iterator but
+ * the two copies are independent past that point.
+ *
+ * Use g_variant_iter_free() to free the return value when you no longer
+ * need it.
+ *
+ * A reference is taken to the container that @iter is iterating over
+ * and will be releated only when g_variant_iter_free() is called.
+ *
+ * Returns: (transfer full): a new heap-allocated #GVariantIter
+ *
+ * Since: 2.24
+ **/
+GVariantIter *
+g_variant_iter_copy (GVariantIter *iter)
+{
+ GVariantIter *copy;
+
+ g_return_val_if_fail (is_valid_iter (iter), 0);
+
+ copy = g_variant_iter_new (GVSI(iter)->value);
+ GVSI(copy)->i = GVSI(iter)->i;
+
+ return copy;
+}
+
+/**
+ * g_variant_iter_n_children:
+ * @iter: a #GVariantIter
+ *
+ * Queries the number of child items in the container that we are
+ * iterating over. This is the total number of items -- not the number
+ * of items remaining.
+ *
+ * This function might be useful for preallocation of arrays.
+ *
+ * Returns: the number of children in the container
+ *
+ * Since: 2.24
+ **/
+gsize
+g_variant_iter_n_children (GVariantIter *iter)
+{
+ g_return_val_if_fail (is_valid_iter (iter), 0);
+
+ return GVSI(iter)->n;
+}
+
+/**
+ * g_variant_iter_free:
+ * @iter: (transfer full): a heap-allocated #GVariantIter
+ *
+ * Frees a heap-allocated #GVariantIter. Only call this function on
+ * iterators that were returned by g_variant_iter_new() or
+ * g_variant_iter_copy().
+ *
+ * Since: 2.24
+ **/
+void
+g_variant_iter_free (GVariantIter *iter)
+{
+ g_return_if_fail (is_valid_heap_iter (iter));
+
+ g_variant_unref (GVHI(iter)->value_ref);
+ GVHI(iter)->magic = 0;
+
+ g_slice_free (struct heap_iter, GVHI(iter));
+}
+
+/**
+ * g_variant_iter_next_value:
+ * @iter: a #GVariantIter
+ *
+ * Gets the next item in the container. If no more items remain then
+ * %NULL is returned.
+ *
+ * Use g_variant_unref() to drop your reference on the return value when
+ * you no longer need it.
+ *
+ * <example>
+ * <title>Iterating with g_variant_iter_next_value()</title>
+ * <programlisting>
+ * /<!-- -->* recursively iterate a container *<!-- -->/
+ * void
+ * iterate_container_recursive (GVariant *container)
+ * {
+ * GVariantIter iter;
+ * GVariant *child;
+ *
+ * g_variant_iter_init (&iter, container);
+ * while ((child = g_variant_iter_next_value (&iter)))
+ * {
+ * g_print ("type '%s'\n", g_variant_get_type_string (child));
+ *
+ * if (g_variant_is_container (child))
+ * iterate_container_recursive (child);
+ *
+ * g_variant_unref (child);
+ * }
+ * }
+ * </programlisting>
+ * </example>
+ *
+ * Returns: (allow-none) (transfer full): a #GVariant, or %NULL
+ *
+ * Since: 2.24
+ **/
+GVariant *
+g_variant_iter_next_value (GVariantIter *iter)
+{
+ g_return_val_if_fail (is_valid_iter (iter), FALSE);
+
+ if G_UNLIKELY (GVSI(iter)->i >= GVSI(iter)->n)
+ {
+ g_critical ("g_variant_iter_next_value: must not be called again "
+ "after NULL has already been returned.");
+ return NULL;
+ }
+
+ GVSI(iter)->i++;
+
+ if (GVSI(iter)->i < GVSI(iter)->n)
+ return g_variant_get_child_value (GVSI(iter)->value, GVSI(iter)->i);
+
+ return NULL;
+}
+
+/* GVariantBuilder {{{1 */
+/**
+ * GVariantBuilder:
+ *
+ * A utility type for constructing container-type #GVariant instances.
+ *
+ * This is an opaque structure and may only be accessed using the
+ * following functions.
+ *
+ * #GVariantBuilder is not threadsafe in any way. Do not attempt to
+ * access it from more than one thread.
+ **/
+
+struct stack_builder
+{
+ GVariantBuilder *parent;
+ GVariantType *type;
+
+ /* type constraint explicitly specified by 'type'.
+ * for tuple types, this moves along as we add more items.
+ */
+ const GVariantType *expected_type;
+
+ /* type constraint implied by previous array item.
+ */
+ const GVariantType *prev_item_type;
+
+ /* constraints on the number of children. max = -1 for unlimited. */
+ gsize min_items;
+ gsize max_items;
+
+ /* dynamically-growing pointer array */
+ GVariant **children;
+ gsize allocated_children;
+ gsize offset;
+
+ /* set to '1' if all items in the container will have the same type
+ * (ie: maybe, array, variant) '0' if not (ie: tuple, dict entry)
+ */
+ guint uniform_item_types : 1;
+
+ /* set to '1' initially and changed to '0' if an untrusted value is
+ * added
+ */
+ guint trusted : 1;
+
+ gsize magic;
+};
+
+G_STATIC_ASSERT (sizeof (struct stack_builder) <= sizeof (GVariantBuilder));
+
+struct heap_builder
+{
+ GVariantBuilder builder;
+ gsize magic;
+
+ gint ref_count;
+};
+
+#define GVSB(b) ((struct stack_builder *) (b))
+#define GVHB(b) ((struct heap_builder *) (b))
+#define GVSB_MAGIC ((gsize) 1033660112u)
+#define GVHB_MAGIC ((gsize) 3087242682u)
+#define is_valid_builder(b) (b != NULL && \
+ GVSB(b)->magic == GVSB_MAGIC)
+#define is_valid_heap_builder(b) (GVHB(b)->magic == GVHB_MAGIC)
+
+/**
+ * g_variant_builder_new:
+ * @type: a container type
+ *
+ * Allocates and initialises a new #GVariantBuilder.
+ *
+ * You should call g_variant_builder_unref() on the return value when it
+ * is no longer needed. The memory will not be automatically freed by
+ * any other call.
+ *
+ * In most cases it is easier to place a #GVariantBuilder directly on
+ * the stack of the calling function and initialise it with
+ * g_variant_builder_init().
+ *
+ * Returns: (transfer full): a #GVariantBuilder
+ *
+ * Since: 2.24
+ **/
+GVariantBuilder *
+g_variant_builder_new (const GVariantType *type)
+{
+ GVariantBuilder *builder;
+
+ builder = (GVariantBuilder *) g_slice_new (struct heap_builder);
+ g_variant_builder_init (builder, type);
+ GVHB(builder)->magic = GVHB_MAGIC;
+ GVHB(builder)->ref_count = 1;
+
+ return builder;
+}
+
+/**
+ * g_variant_builder_unref:
+ * @builder: (transfer full): a #GVariantBuilder allocated by g_variant_builder_new()
+ *
+ * Decreases the reference count on @builder.
+ *
+ * In the event that there are no more references, releases all memory
+ * associated with the #GVariantBuilder.
+ *
+ * Don't call this on stack-allocated #GVariantBuilder instances or bad
+ * things will happen.
+ *
+ * Since: 2.24
+ **/
+void
+g_variant_builder_unref (GVariantBuilder *builder)
+{
+ g_return_if_fail (is_valid_heap_builder (builder));
+
+ if (--GVHB(builder)->ref_count)
+ return;
+
+ g_variant_builder_clear (builder);
+ GVHB(builder)->magic = 0;
+
+ g_slice_free (struct heap_builder, GVHB(builder));
+}
+
+/**
+ * g_variant_builder_ref:
+ * @builder: a #GVariantBuilder allocated by g_variant_builder_new()
+ *
+ * Increases the reference count on @builder.
+ *
+ * Don't call this on stack-allocated #GVariantBuilder instances or bad
+ * things will happen.
+ *
+ * Returns: (transfer full): a new reference to @builder
+ *
+ * Since: 2.24
+ **/
+GVariantBuilder *
+g_variant_builder_ref (GVariantBuilder *builder)
+{
+ g_return_val_if_fail (is_valid_heap_builder (builder), NULL);
+
+ GVHB(builder)->ref_count++;
+
+ return builder;
+}
+
+/**
+ * g_variant_builder_clear: (skip)
+ * @builder: a #GVariantBuilder
+ *
+ * Releases all memory associated with a #GVariantBuilder without
+ * freeing the #GVariantBuilder structure itself.
+ *
+ * It typically only makes sense to do this on a stack-allocated
+ * #GVariantBuilder if you want to abort building the value part-way
+ * through. This function need not be called if you call
+ * g_variant_builder_end() and it also doesn't need to be called on
+ * builders allocated with g_variant_builder_new (see
+ * g_variant_builder_unref() for that).
+ *
+ * This function leaves the #GVariantBuilder structure set to all-zeros.
+ * It is valid to call this function on either an initialised
+ * #GVariantBuilder or one that is set to all-zeros but it is not valid
+ * to call this function on uninitialised memory.
+ *
+ * Since: 2.24
+ **/
+void
+g_variant_builder_clear (GVariantBuilder *builder)
+{
+ gsize i;
+
+ if (GVSB(builder)->magic == 0)
+ /* all-zeros case */
+ return;
+
+ g_return_if_fail (is_valid_builder (builder));
+
+ g_variant_type_free (GVSB(builder)->type);
+
+ for (i = 0; i < GVSB(builder)->offset; i++)
+ g_variant_unref (GVSB(builder)->children[i]);
+
+ g_free (GVSB(builder)->children);
+
+ if (GVSB(builder)->parent)
+ {
+ g_variant_builder_clear (GVSB(builder)->parent);
+ g_slice_free (GVariantBuilder, GVSB(builder)->parent);
+ }
+
+ memset (builder, 0, sizeof (GVariantBuilder));
+}
+
+/**
+ * g_variant_builder_init: (skip)
+ * @builder: a #GVariantBuilder
+ * @type: a container type
+ *
+ * Initialises a #GVariantBuilder structure.
+ *
+ * @type must be non-%NULL. It specifies the type of container to
+ * construct. It can be an indefinite type such as
+ * %G_VARIANT_TYPE_ARRAY or a definite type such as "as" or "(ii)".
+ * Maybe, array, tuple, dictionary entry and variant-typed values may be
+ * constructed.
+ *
+ * After the builder is initialised, values are added using
+ * g_variant_builder_add_value() or g_variant_builder_add().
+ *
+ * After all the child values are added, g_variant_builder_end() frees
+ * the memory associated with the builder and returns the #GVariant that
+ * was created.
+ *
+ * This function completely ignores the previous contents of @builder.
+ * On one hand this means that it is valid to pass in completely
+ * uninitialised memory. On the other hand, this means that if you are
+ * initialising over top of an existing #GVariantBuilder you need to
+ * first call g_variant_builder_clear() in order to avoid leaking
+ * memory.
+ *
+ * You must not call g_variant_builder_ref() or
+ * g_variant_builder_unref() on a #GVariantBuilder that was initialised
+ * with this function. If you ever pass a reference to a
+ * #GVariantBuilder outside of the control of your own code then you
+ * should assume that the person receiving that reference may try to use
+ * reference counting; you should use g_variant_builder_new() instead of
+ * this function.
+ *
+ * Since: 2.24
+ **/
+void
+g_variant_builder_init (GVariantBuilder *builder,
+ const GVariantType *type)
+{
+ g_return_if_fail (type != NULL);
+ g_return_if_fail (g_variant_type_is_container (type));
+
+ memset (builder, 0, sizeof (GVariantBuilder));
+
+ GVSB(builder)->type = g_variant_type_copy (type);
+ GVSB(builder)->magic = GVSB_MAGIC;
+ GVSB(builder)->trusted = TRUE;
+
+ switch (*(const gchar *) type)
+ {
+ case G_VARIANT_CLASS_VARIANT:
+ GVSB(builder)->uniform_item_types = TRUE;
+ GVSB(builder)->allocated_children = 1;
+ GVSB(builder)->expected_type = NULL;
+ GVSB(builder)->min_items = 1;
+ GVSB(builder)->max_items = 1;
+ break;
+
+ case G_VARIANT_CLASS_ARRAY:
+ GVSB(builder)->uniform_item_types = TRUE;
+ GVSB(builder)->allocated_children = 8;
+ GVSB(builder)->expected_type =
+ g_variant_type_element (GVSB(builder)->type);
+ GVSB(builder)->min_items = 0;
+ GVSB(builder)->max_items = -1;
+ break;
+
+ case G_VARIANT_CLASS_MAYBE:
+ GVSB(builder)->uniform_item_types = TRUE;
+ GVSB(builder)->allocated_children = 1;
+ GVSB(builder)->expected_type =
+ g_variant_type_element (GVSB(builder)->type);
+ GVSB(builder)->min_items = 0;
+ GVSB(builder)->max_items = 1;
+ break;
+
+ case G_VARIANT_CLASS_DICT_ENTRY:
+ GVSB(builder)->uniform_item_types = FALSE;
+ GVSB(builder)->allocated_children = 2;
+ GVSB(builder)->expected_type =
+ g_variant_type_key (GVSB(builder)->type);
+ GVSB(builder)->min_items = 2;
+ GVSB(builder)->max_items = 2;
+ break;
+
+ case 'r': /* G_VARIANT_TYPE_TUPLE was given */
+ GVSB(builder)->uniform_item_types = FALSE;
+ GVSB(builder)->allocated_children = 8;
+ GVSB(builder)->expected_type = NULL;
+ GVSB(builder)->min_items = 0;
+ GVSB(builder)->max_items = -1;
+ break;
+
+ case G_VARIANT_CLASS_TUPLE: /* a definite tuple type was given */
+ GVSB(builder)->allocated_children = g_variant_type_n_items (type);
+ GVSB(builder)->expected_type =
+ g_variant_type_first (GVSB(builder)->type);
+ GVSB(builder)->min_items = GVSB(builder)->allocated_children;
+ GVSB(builder)->max_items = GVSB(builder)->allocated_children;
+ GVSB(builder)->uniform_item_types = FALSE;
+ break;
+
+ default:
+ g_assert_not_reached ();
+ }
+
+ GVSB(builder)->children = g_new (GVariant *,
+ GVSB(builder)->allocated_children);
+}
+
+static void
+g_variant_builder_make_room (struct stack_builder *builder)
+{
+ if (builder->offset == builder->allocated_children)
+ {
+ builder->allocated_children *= 2;
+ builder->children = g_renew (GVariant *, builder->children,
+ builder->allocated_children);
+ }
+}
+
+/**
+ * g_variant_builder_add_value:
+ * @builder: a #GVariantBuilder
+ * @value: a #GVariant
+ *
+ * Adds @value to @builder.
+ *
+ * It is an error to call this function in any way that would create an
+ * inconsistent value to be constructed. Some examples of this are
+ * putting different types of items into an array, putting the wrong
+ * types or number of items in a tuple, putting more than one value into
+ * a variant, etc.
+ *
+ * If @value is a floating reference (see g_variant_ref_sink()),
+ * the @builder instance takes ownership of @value.
+ *
+ * Since: 2.24
+ **/
+void
+g_variant_builder_add_value (GVariantBuilder *builder,
+ GVariant *value)
+{
+ g_return_if_fail (is_valid_builder (builder));
+ g_return_if_fail (GVSB(builder)->offset < GVSB(builder)->max_items);
+ g_return_if_fail (!GVSB(builder)->expected_type ||
+ g_variant_is_of_type (value,
+ GVSB(builder)->expected_type));
+ g_return_if_fail (!GVSB(builder)->prev_item_type ||
+ g_variant_is_of_type (value,
+ GVSB(builder)->prev_item_type));
+
+ GVSB(builder)->trusted &= g_variant_is_trusted (value);
+
+ if (!GVSB(builder)->uniform_item_types)
+ {
+ /* advance our expected type pointers */
+ if (GVSB(builder)->expected_type)
+ GVSB(builder)->expected_type =
+ g_variant_type_next (GVSB(builder)->expected_type);
+
+ if (GVSB(builder)->prev_item_type)
+ GVSB(builder)->prev_item_type =
+ g_variant_type_next (GVSB(builder)->prev_item_type);
+ }
+ else
+ GVSB(builder)->prev_item_type = g_variant_get_type (value);
+
+ g_variant_builder_make_room (GVSB(builder));
+
+ GVSB(builder)->children[GVSB(builder)->offset++] =
+ g_variant_ref_sink (value);
+}
+
+/**
+ * g_variant_builder_open:
+ * @builder: a #GVariantBuilder
+ * @type: a #GVariantType
+ *
+ * Opens a subcontainer inside the given @builder. When done adding
+ * items to the subcontainer, g_variant_builder_close() must be called.
+ *
+ * It is an error to call this function in any way that would cause an
+ * inconsistent value to be constructed (ie: adding too many values or
+ * a value of an incorrect type).
+ *
+ * Since: 2.24
+ **/
+void
+g_variant_builder_open (GVariantBuilder *builder,
+ const GVariantType *type)
+{
+ GVariantBuilder *parent;
+
+ g_return_if_fail (is_valid_builder (builder));
+ g_return_if_fail (GVSB(builder)->offset < GVSB(builder)->max_items);
+ g_return_if_fail (!GVSB(builder)->expected_type ||
+ g_variant_type_is_subtype_of (type,
+ GVSB(builder)->expected_type));
+ g_return_if_fail (!GVSB(builder)->prev_item_type ||
+ g_variant_type_is_subtype_of (GVSB(builder)->prev_item_type,
+ type));
+
+ parent = g_slice_dup (GVariantBuilder, builder);
+ g_variant_builder_init (builder, type);
+ GVSB(builder)->parent = parent;
+
+ /* push the prev_item_type down into the subcontainer */
+ if (GVSB(parent)->prev_item_type)
+ {
+ if (!GVSB(builder)->uniform_item_types)
+ /* tuples and dict entries */
+ GVSB(builder)->prev_item_type =
+ g_variant_type_first (GVSB(parent)->prev_item_type);
+
+ else if (!g_variant_type_is_variant (GVSB(builder)->type))
+ /* maybes and arrays */
+ GVSB(builder)->prev_item_type =
+ g_variant_type_element (GVSB(parent)->prev_item_type);
+ }
+}
+
+/**
+ * g_variant_builder_close:
+ * @builder: a #GVariantBuilder
+ *
+ * Closes the subcontainer inside the given @builder that was opened by
+ * the most recent call to g_variant_builder_open().
+ *
+ * It is an error to call this function in any way that would create an
+ * inconsistent value to be constructed (ie: too few values added to the
+ * subcontainer).
+ *
+ * Since: 2.24
+ **/
+void
+g_variant_builder_close (GVariantBuilder *builder)
+{
+ GVariantBuilder *parent;
+
+ g_return_if_fail (is_valid_builder (builder));
+ g_return_if_fail (GVSB(builder)->parent != NULL);
+
+ parent = GVSB(builder)->parent;
+ GVSB(builder)->parent = NULL;
+
+ g_variant_builder_add_value (parent, g_variant_builder_end (builder));
+ *builder = *parent;
+
+ g_slice_free (GVariantBuilder, parent);
+}
+
+/*< private >
+ * g_variant_make_maybe_type:
+ * @element: a #GVariant
+ *
+ * Return the type of a maybe containing @element.
+ */
+static GVariantType *
+g_variant_make_maybe_type (GVariant *element)
+{
+ return g_variant_type_new_maybe (g_variant_get_type (element));
+}
+
+/*< private >
+ * g_variant_make_array_type:
+ * @element: a #GVariant
+ *
+ * Return the type of an array containing @element.
+ */
+static GVariantType *
+g_variant_make_array_type (GVariant *element)
+{
+ return g_variant_type_new_array (g_variant_get_type (element));
+}
+
+/**
+ * g_variant_builder_end:
+ * @builder: a #GVariantBuilder
+ *
+ * Ends the builder process and returns the constructed value.
+ *
+ * It is not permissible to use @builder in any way after this call
+ * except for reference counting operations (in the case of a
+ * heap-allocated #GVariantBuilder) or by reinitialising it with
+ * g_variant_builder_init() (in the case of stack-allocated).
+ *
+ * It is an error to call this function in any way that would create an
+ * inconsistent value to be constructed (ie: insufficient number of
+ * items added to a container with a specific number of children
+ * required). It is also an error to call this function if the builder
+ * was created with an indefinite array or maybe type and no children
+ * have been added; in this case it is impossible to infer the type of
+ * the empty array.
+ *
+ * Returns: (transfer none): a new, floating, #GVariant
+ *
+ * Since: 2.24
+ **/
+GVariant *
+g_variant_builder_end (GVariantBuilder *builder)
+{
+ GVariantType *my_type;
+ GVariant *value;
+
+ g_return_val_if_fail (is_valid_builder (builder), NULL);
+ g_return_val_if_fail (GVSB(builder)->offset >= GVSB(builder)->min_items,
+ NULL);
+ g_return_val_if_fail (!GVSB(builder)->uniform_item_types ||
+ GVSB(builder)->prev_item_type != NULL ||
+ g_variant_type_is_definite (GVSB(builder)->type),
+ NULL);
+
+ if (g_variant_type_is_definite (GVSB(builder)->type))
+ my_type = g_variant_type_copy (GVSB(builder)->type);
+
+ else if (g_variant_type_is_maybe (GVSB(builder)->type))
+ my_type = g_variant_make_maybe_type (GVSB(builder)->children[0]);
+
+ else if (g_variant_type_is_array (GVSB(builder)->type))
+ my_type = g_variant_make_array_type (GVSB(builder)->children[0]);
+
+ else if (g_variant_type_is_tuple (GVSB(builder)->type))
+ my_type = g_variant_make_tuple_type (GVSB(builder)->children,
+ GVSB(builder)->offset);
+
+ else if (g_variant_type_is_dict_entry (GVSB(builder)->type))
+ my_type = g_variant_make_dict_entry_type (GVSB(builder)->children[0],
+ GVSB(builder)->children[1]);
+ else
+ g_assert_not_reached ();
+
+ value = g_variant_new_from_children (my_type,
+ g_renew (GVariant *,
+ GVSB(builder)->children,
+ GVSB(builder)->offset),
+ GVSB(builder)->offset,
+ GVSB(builder)->trusted);
+ GVSB(builder)->children = NULL;
+ GVSB(builder)->offset = 0;
+
+ g_variant_builder_clear (builder);
+ g_variant_type_free (my_type);
+
+ return value;
+}
+
+/* Format strings {{{1 */
+/*< private >
+ * g_variant_format_string_scan:
+ * @string: a string that may be prefixed with a format string
+ * @limit: (allow-none) (default NULL): a pointer to the end of @string,
+ * or %NULL
+ * @endptr: (allow-none) (default NULL): location to store the end pointer,
+ * or %NULL
+ *
+ * Checks the string pointed to by @string for starting with a properly
+ * formed #GVariant varargs format string. If no valid format string is
+ * found then %FALSE is returned.
+ *
+ * If @string does start with a valid format string then %TRUE is
+ * returned. If @endptr is non-%NULL then it is updated to point to the
+ * first character after the format string.
+ *
+ * If @limit is non-%NULL then @limit (and any charater after it) will
+ * not be accessed and the effect is otherwise equivalent to if the
+ * character at @limit were nul.
+ *
+ * See the section on <link linkend='gvariant-format-strings'>GVariant
+ * Format Strings</link>.
+ *
+ * Returns: %TRUE if there was a valid format string
+ *
+ * Since: 2.24
+ */
+gboolean
+g_variant_format_string_scan (const gchar *string,
+ const gchar *limit,
+ const gchar **endptr)
+{
+#define next_char() (string == limit ? '\0' : *string++)
+#define peek_char() (string == limit ? '\0' : *string)
+ char c;
+
+ switch (next_char())
+ {
+ case 'b': case 'y': case 'n': case 'q': case 'i': case 'u':
+ case 'x': case 't': case 'h': case 'd': case 's': case 'o':
+ case 'g': case 'v': case '*': case '?': case 'r':
+ break;
+
+ case 'm':
+ return g_variant_format_string_scan (string, limit, endptr);
+
+ case 'a':
+ case '@':
+ return g_variant_type_string_scan (string, limit, endptr);
+
+ case '(':
+ while (peek_char() != ')')
+ if (!g_variant_format_string_scan (string, limit, &string))
+ return FALSE;
+
+ next_char(); /* consume ')' */
+ break;
+
+ case '{':
+ c = next_char();
+
+ if (c == '&')
+ {
+ c = next_char ();
+
+ if (c != 's' && c != 'o' && c != 'g')
+ return FALSE;
+ }
+ else
+ {
+ if (c == '@')
+ c = next_char ();
+
+ /* ISO/IEC 9899:1999 (C99) §7.21.5.2:
+ * The terminating null character is considered to be
+ * part of the string.
+ */
+ if (c != '\0' && strchr ("bynqiuxthdsog?", c) == NULL)
+ return FALSE;
+ }
+
+ if (!g_variant_format_string_scan (string, limit, &string))
+ return FALSE;
+
+ if (next_char() != '}')
+ return FALSE;
+
+ break;
+
+ case '^':
+ if ((c = next_char()) == 'a')
+ {
+ if ((c = next_char()) == '&')
+ {
+ if ((c = next_char()) == 'a')
+ {
+ if ((c = next_char()) == 'y')
+ break; /* '^a&ay' */
+ }
+
+ else if (c == 's' || c == 'o')
+ break; /* '^a&s', '^a&o' */
+ }
+
+ else if (c == 'a')
+ {
+ if ((c = next_char()) == 'y')
+ break; /* '^aay' */
+ }
+
+ else if (c == 's' || c == 'o')
+ break; /* '^as', '^ao' */
+
+ else if (c == 'y')
+ break; /* '^ay' */
+ }
+ else if (c == '&')
+ {
+ if ((c = next_char()) == 'a')
+ {
+ if ((c = next_char()) == 'y')
+ break; /* '^&ay' */
+ }
+ }
+
+ return FALSE;
+
+ case '&':
+ c = next_char();
+
+ if (c != 's' && c != 'o' && c != 'g')
+ return FALSE;
+
+ break;
+
+ default:
+ return FALSE;
+ }
+
+ if (endptr != NULL)
+ *endptr = string;
+
+#undef next_char
+#undef peek_char
+
+ return TRUE;
+}
+
+/**
+ * g_variant_check_format_string:
+ * @value: a #GVariant
+ * @format_string: a valid #GVariant format string
+ * @copy_only: %TRUE to ensure the format string makes deep copies
+ *
+ * Checks if calling g_variant_get() with @format_string on @value would
+ * be valid from a type-compatibility standpoint. @format_string is
+ * assumed to be a valid format string (from a syntactic standpoint).
+ *
+ * If @copy_only is %TRUE then this function additionally checks that it
+ * would be safe to call g_variant_unref() on @value immediately after
+ * the call to g_variant_get() without invalidating the result. This is
+ * only possible if deep copies are made (ie: there are no pointers to
+ * the data inside of the soon-to-be-freed #GVariant instance). If this
+ * check fails then a g_critical() is printed and %FALSE is returned.
+ *
+ * This function is meant to be used by functions that wish to provide
+ * varargs accessors to #GVariant values of uncertain values (eg:
+ * g_variant_lookup() or g_menu_model_get_item_attribute()).
+ *
+ * Returns: %TRUE if @format_string is safe to use
+ *
+ * Since: 2.34
+ */
+gboolean
+g_variant_check_format_string (GVariant *value,
+ const gchar *format_string,
+ gboolean copy_only)
+{
+ const gchar *original_format = format_string;
+ const gchar *type_string;
+
+ /* Interesting factoid: assuming a format string is valid, it can be
+ * converted to a type string by removing all '@' '&' and '^'
+ * characters.
+ *
+ * Instead of doing that, we can just skip those characters when
+ * comparing it to the type string of @value.
+ *
+ * For the copy-only case we can just drop the '&' from the list of
+ * characters to skip over. A '&' will never appear in a type string
+ * so we know that it won't be possible to return %TRUE if it is in a
+ * format string.
+ */
+ type_string = g_variant_get_type_string (value);
+
+ while (*type_string || *format_string)
+ {
+ gchar format = *format_string++;
+
+ switch (format)
+ {
+ case '&':
+ if G_UNLIKELY (copy_only)
+ {
+ /* for the love of all that is good, please don't mark this string for translation... */
+ g_critical ("g_variant_check_format_string() is being called by a function with a GVariant varargs "
+ "interface to validate the passed format string for type safety. The passed format "
+ "(%s) contains a '&' character which would result in a pointer being returned to the "
+ "data inside of a GVariant instance that may no longer exist by the time the function "
+ "returns. Modify your code to use a format string without '&'.", original_format);
+ return FALSE;
+ }
+
+ /* fall through */
+ case '^':
+ case '@':
+ /* ignore these 2 (or 3) */
+ continue;
+
+ case '?':
+ /* attempt to consume one of 'bynqiuxthdsog' */
+ {
+ char s = *type_string++;
+
+ if (s == '\0' || strchr ("bynqiuxthdsog", s) == NULL)
+ return FALSE;
+ }
+ continue;
+
+ case 'r':
+ /* ensure it's a tuple */
+ if (*type_string != '(')
+ return FALSE;
+
+ /* fall through */
+ case '*':
+ /* consume a full type string for the '*' or 'r' */
+ if (!g_variant_type_string_scan (type_string, NULL, &type_string))
+ return FALSE;
+
+ continue;
+
+ default:
+ /* attempt to consume exactly one character equal to the format */
+ if (format != *type_string++)
+ return FALSE;
+ }
+ }
+
+ return TRUE;
+}
+
+/*< private >
+ * g_variant_format_string_scan_type:
+ * @string: a string that may be prefixed with a format string
+ * @limit: (allow-none) (default NULL): a pointer to the end of @string,
+ * or %NULL
+ * @endptr: (allow-none) (default NULL): location to store the end pointer,
+ * or %NULL
+ *
+ * If @string starts with a valid format string then this function will
+ * return the type that the format string corresponds to. Otherwise
+ * this function returns %NULL.
+ *
+ * Use g_variant_type_free() to free the return value when you no longer
+ * need it.
+ *
+ * This function is otherwise exactly like
+ * g_variant_format_string_scan().
+ *
+ * Returns: (allow-none): a #GVariantType if there was a valid format string
+ *
+ * Since: 2.24
+ */
+GVariantType *
+g_variant_format_string_scan_type (const gchar *string,
+ const gchar *limit,
+ const gchar **endptr)
+{
+ const gchar *my_end;
+ gchar *dest;
+ gchar *new;
+
+ if (endptr == NULL)
+ endptr = &my_end;
+
+ if (!g_variant_format_string_scan (string, limit, endptr))
+ return NULL;
+
+ dest = new = g_malloc (*endptr - string + 1);
+ while (string != *endptr)
+ {
+ if (*string != '@' && *string != '&' && *string != '^')
+ *dest++ = *string;
+ string++;
+ }
+ *dest = '\0';
+
+ return (GVariantType *) G_VARIANT_TYPE (new);
+}
+
+static gboolean
+valid_format_string (const gchar *format_string,
+ gboolean single,
+ GVariant *value)
+{
+ const gchar *endptr;
+ GVariantType *type;
+
+ type = g_variant_format_string_scan_type (format_string, NULL, &endptr);
+
+ if G_UNLIKELY (type == NULL || (single && *endptr != '\0'))
+ {
+ if (single)
+ g_critical ("`%s' is not a valid GVariant format string",
+ format_string);
+ else
+ g_critical ("`%s' does not have a valid GVariant format "
+ "string as a prefix", format_string);
+
+ if (type != NULL)
+ g_variant_type_free (type);
+
+ return FALSE;
+ }
+
+ if G_UNLIKELY (value && !g_variant_is_of_type (value, type))
+ {
+ gchar *fragment;
+ gchar *typestr;
+
+ fragment = g_strndup (format_string, endptr - format_string);
+ typestr = g_variant_type_dup_string (type);
+
+ g_critical ("the GVariant format string `%s' has a type of "
+ "`%s' but the given value has a type of `%s'",
+ fragment, typestr, g_variant_get_type_string (value));
+
+ g_variant_type_free (type);
+
+ return FALSE;
+ }
+
+ g_variant_type_free (type);
+
+ return TRUE;
+}
+
+/* Variable Arguments {{{1 */
+/* We consider 2 main classes of format strings:
+ *
+ * - recursive format strings
+ * these are ones that result in recursion and the collection of
+ * possibly more than one argument. Maybe types, tuples,
+ * dictionary entries.
+ *
+ * - leaf format string
+ * these result in the collection of a single argument.
+ *
+ * Leaf format strings are further subdivided into two categories:
+ *
+ * - single non-null pointer ("nnp")
+ * these either collect or return a single non-null pointer.
+ *
+ * - other
+ * these collect or return something else (bool, number, etc).
+ *
+ * Based on the above, the varargs handling code is split into 4 main parts:
+ *
+ * - nnp handling code
+ * - leaf handling code (which may invoke nnp code)
+ * - generic handling code (may be recursive, may invoke leaf code)
+ * - user-facing API (which invokes the generic code)
+ *
+ * Each section implements some of the following functions:
+ *
+ * - skip:
+ * collect the arguments for the format string as if
+ * g_variant_new() had been called, but do nothing with them. used
+ * for skipping over arguments when constructing a Nothing maybe
+ * type.
+ *
+ * - new:
+ * create a GVariant *
+ *
+ * - get:
+ * unpack a GVariant *
+ *
+ * - free (nnp only):
+ * free a previously allocated item
+ */
+
+static gboolean
+g_variant_format_string_is_leaf (const gchar *str)
+{
+ return str[0] != 'm' && str[0] != '(' && str[0] != '{';
+}
+
+static gboolean
+g_variant_format_string_is_nnp (const gchar *str)
+{
+ return str[0] == 'a' || str[0] == 's' || str[0] == 'o' || str[0] == 'g' ||
+ str[0] == '^' || str[0] == '@' || str[0] == '*' || str[0] == '?' ||
+ str[0] == 'r' || str[0] == 'v' || str[0] == '&';
+}
+
+/* Single non-null pointer ("nnp") {{{2 */
+static void
+g_variant_valist_free_nnp (const gchar *str,
+ gpointer ptr)
+{
+ switch (*str)
+ {
+ case 'a':
+ g_variant_iter_free (ptr);
+ break;
+
+ case '^':
+ if (str[2] != '&') /* '^as', '^ao' */
+ g_strfreev (ptr);
+ else /* '^a&s', '^a&o' */
+ g_free (ptr);
+ break;
+
+ case 's':
+ case 'o':
+ case 'g':
+ g_free (ptr);
+ break;
+
+ case '@':
+ case '*':
+ case '?':
+ case 'v':
+ g_variant_unref (ptr);
+ break;
+
+ case '&':
+ break;
+
+ default:
+ g_assert_not_reached ();
+ }