GBytes *
g_variant_get_data_as_bytes (GVariant *value)
{
+ const gchar *bytes_data;
+ const gchar *data;
+ gsize bytes_size;
+ gsize size;
+
g_variant_lock (value);
g_variant_ensure_serialised (value);
g_variant_unlock (value);
- return g_bytes_ref (value->contents.serialised.bytes);
+ bytes_data = g_bytes_get_data (value->contents.serialised.bytes, &bytes_size);
+ data = value->contents.serialised.data;
+ size = value->size;
+
+ if (data == bytes_data && size == bytes_size)
+ return g_bytes_ref (value->contents.serialised.bytes);
+ else
+ return g_bytes_new_from_bytes (value->contents.serialised.bytes,
+ data - bytes_data, size);
}
test_gbytes (void)
{
GVariant *a;
+ GVariant *tuple;
GBytes *bytes;
GBytes *bytes2;
const guint8 values[5] = { 1, 2, 3, 4, 5 };
bytes = g_bytes_new (&values, 5);
g_assert (g_bytes_equal (bytes, bytes2));
+ g_bytes_unref (bytes);
+ g_bytes_unref (bytes2);
+
+ tuple = g_variant_new_parsed ("['foo', 'bar']");
+ bytes = g_variant_get_data_as_bytes (tuple); /* force serialisation */
+ a = g_variant_get_child_value (tuple, 1);
+ bytes2 = g_variant_get_data_as_bytes (a);
+ g_assert (!g_bytes_equal (bytes, bytes2));
g_bytes_unref (bytes);
g_bytes_unref (bytes2);
+ g_variant_unref (a);
+ g_variant_unref (tuple);
}
int