From 0a71eb0c8d1a86e30e2ac69dbaec9ccd3275a0f4 Mon Sep 17 00:00:00 2001 From: Simon McVittie <smcv@collabora.com> Date: Mon, 27 Jul 2020 18:12:49 +0100 Subject: [PATCH] glib-backports: Add a backport of g_dbus_address_escape_value Originally written by me, with trivial syntactic edits to the doc-comment by Matthias Clasen and Philip Withnall. We'll need this if we want to do D-Bus IPC on a socket given as a path. Signed-off-by: Simon McVittie <smcv@collabora.com> --- src/glib-backports.c | 51 +++++++++++++++++++++++++++++++++++++++++++- src/glib-backports.h | 5 +++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/glib-backports.c b/src/glib-backports.c index 477c421a1..8ab71affc 100644 --- a/src/glib-backports.c +++ b/src/glib-backports.c @@ -4,7 +4,7 @@ * Copyright 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald * Copyright 1997-2000 GLib team * Copyright 2000 Red Hat, Inc. - * Copyright 2019 Collabora Ltd. + * Copyright 2013-2019 Collabora Ltd. * g_execvpe implementation based on GNU libc execvp: * Copyright 1991, 92, 95, 96, 97, 98, 99 Free Software Foundation, Inc. * @@ -205,3 +205,52 @@ my_g_ptr_array_insert (GPtrArray *arr, } } #endif + +#if !GLIB_CHECK_VERSION (2, 36, 0) +/** + * 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 `/run/bus-for-:0`, + * this function would return `/run/bus-for-%3A0`, + * which could be used in a D-Bus address like + * `unix:nonce-tcp:host=127.0.0.1,port=42,noncefile=/run/bus-for-%3A0`. + * + * Returns: (transfer full): a copy of @string with all + * non-optionally-escaped bytes escaped + * + * Since: 2.36 + */ +gchar * +my_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); +} +#endif diff --git a/src/glib-backports.h b/src/glib-backports.h index 70aba6013..939852b5e 100644 --- a/src/glib-backports.h +++ b/src/glib-backports.h @@ -71,3 +71,8 @@ void my_g_ptr_array_insert (GPtrArray *arr, #if !GLIB_CHECK_VERSION (2, 42, 0) #define G_OPTION_FLAG_NONE 0 #endif + +#if !GLIB_CHECK_VERSION(2, 36, 0) +#define g_dbus_address_escape_value my_g_dbus_address_escape_value +gchar *my_g_dbus_address_escape_value (const gchar *string); +#endif -- GitLab