summaryrefslogtreecommitdiff
path: root/gio/gdbusaddress.c
diff options
context:
space:
mode:
authorSimon McVittie <simon.mcvittie@collabora.co.uk>2013-02-13 20:42:58 +0000
committerSimon McVittie <simon.mcvittie@collabora.co.uk>2013-02-13 20:42:58 +0000
commitd200208d2b8bad7babdd56f4ec6fcae1589e142b (patch)
treeeee9ad277e3655314a41f0c3e3e36681fed527e2 /gio/gdbusaddress.c
parent035cd81477ca9dc3061ff056c269fee28aab89c3 (diff)
downloadglib-d200208d2b8bad7babdd56f4ec6fcae1589e142b.tar.gz
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>
Diffstat (limited to 'gio/gdbusaddress.c')
-rw-r--r--gio/gdbusaddress.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/gio/gdbusaddress.c b/gio/gdbusaddress.c
index 8c1d31c76..cc9703c96 100644
--- a/gio/gdbusaddress.c
+++ b/gio/gdbusaddress.c
@@ -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);
+}