mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-09-05 23:48:44 +02:00
g_dbus_address_escape_value: add
This is a GLib reimplementation of dbus_address_escape_value(). It's useful if you want to construct a D-Bus address from pieces: for instance, if you have a listening Unix socket whose path is known, and you want to connect a D-Bus peer to it. Bug: https://bugzilla.gnome.org/show_bug.cgi?id=693673 Reviewed-by: Colin Walters <walters@verbum.org> [amended to add Since: 2.36 as per review] Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
This commit is contained in:
@@ -1586,3 +1586,50 @@ g_dbus_address_get_for_bus_sync (GBusType bus_type,
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_dbus_address_escape_value:
|
||||
* @string: an unescaped string to be included in a D-Bus address
|
||||
* as the value in a key-value pair
|
||||
*
|
||||
* Escape @string so it can appear in a D-Bus address as the value
|
||||
* part of a key-value pair.
|
||||
*
|
||||
* For instance, if @string is <code>/run/bus-for-:0</code>,
|
||||
* this function would return <code>/run/bus-for-%3A0</code>,
|
||||
* which could be used in a D-Bus address like
|
||||
* <code>unix:nonce-tcp:host=127.0.0.1,port=42,noncefile=/run/bus-for-%3A0</code>.
|
||||
*
|
||||
* Returns: (transfer full): a copy of @string with all
|
||||
* non-optionally-escaped bytes escaped
|
||||
*
|
||||
* Since: 2.36
|
||||
*/
|
||||
gchar *
|
||||
g_dbus_address_escape_value (const gchar *string)
|
||||
{
|
||||
GString *s;
|
||||
gsize i;
|
||||
|
||||
g_return_val_if_fail (string != NULL, NULL);
|
||||
|
||||
/* There will often not be anything needing escaping at all. */
|
||||
s = g_string_sized_new (strlen (string));
|
||||
|
||||
/* D-Bus address escaping is mostly the same as URI escaping... */
|
||||
g_string_append_uri_escaped (s, string, "\\/", FALSE);
|
||||
|
||||
/* ... but '~' is an unreserved character in URIs, but a
|
||||
* non-optionally-escaped character in D-Bus addresses. */
|
||||
for (i = 0; i < s->len; i++)
|
||||
{
|
||||
if (G_UNLIKELY (s->str[i] == '~'))
|
||||
{
|
||||
s->str[i] = '%';
|
||||
g_string_insert (s, i + 1, "7E");
|
||||
i += 2;
|
||||
}
|
||||
}
|
||||
|
||||
return g_string_free (s, FALSE);
|
||||
}
|
||||
|
Reference in New Issue
Block a user