gdbus: make hexencode() a shared function to avoid duplication

https://bugzilla.gnome.org/show_bug.cgi?id=794170
This commit is contained in:
Руслан Ижбулатов
2018-03-09 09:05:22 +00:00
committed by Philip Withnall
parent cc7ab04b33
commit aab83f7475
4 changed files with 42 additions and 71 deletions

View File

@@ -365,12 +365,6 @@ _my_g_input_stream_read_line_safe (GInputStream *i,
/* ---------------------------------------------------------------------------------------------------- */
static void
append_nibble (GString *s, gint val)
{
g_string_append_c (s, val >= 10 ? ('a' + val - 10) : ('0' + val));
}
static gchar *
hexdecode (const gchar *str,
gsize *out_len,
@@ -417,31 +411,6 @@ hexdecode (const gchar *str,
return ret;
}
static gchar *
hexencode (const gchar *str,
gsize str_len)
{
guint n;
GString *s;
s = g_string_new (NULL);
for (n = 0; n < str_len; n++)
{
gint val;
gint upper_nibble;
gint lower_nibble;
val = ((const guchar *) str)[n];
upper_nibble = val >> 4;
lower_nibble = val & 0x0f;
append_nibble (s, upper_nibble);
append_nibble (s, lower_nibble);
}
return g_string_free (s, FALSE);
}
/* ---------------------------------------------------------------------------------------------------- */
static GDBusAuthMechanism *
@@ -548,7 +517,7 @@ client_choose_mech_and_send_initial_response (GDBusAuth *auth,
if (initial_response != NULL)
{
//g_printerr ("initial_response = '%s'\n", initial_response);
encoded = hexencode (initial_response, initial_response_len);
encoded = _g_dbus_hexencode (initial_response, initial_response_len);
s = g_strdup_printf ("AUTH %s %s\r\n",
_g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype),
encoded);
@@ -840,7 +809,7 @@ _g_dbus_auth_run_client (GDBusAuth *auth,
gsize data_len;
gchar *encoded_data;
data = _g_dbus_auth_mechanism_client_data_send (mech, &data_len);
encoded_data = hexencode (data, data_len);
encoded_data = _g_dbus_hexencode (data, data_len);
s = g_strdup_printf ("DATA %s\r\n", encoded_data);
g_free (encoded_data);
g_free (data);
@@ -1215,7 +1184,7 @@ _g_dbus_auth_run_server (GDBusAuth *auth,
{
gchar *encoded_data;
encoded_data = hexencode (data, data_len);
encoded_data = _g_dbus_hexencode (data, data_len);
s = g_strdup_printf ("DATA %s\r\n", encoded_data);
g_free (encoded_data);
g_free (data);

View File

@@ -307,42 +307,6 @@ out:
/* ---------------------------------------------------------------------------------------------------- */
static void
append_nibble (GString *s, gint val)
{
g_string_append_c (s, val >= 10 ? ('a' + val - 10) : ('0' + val));
}
static gchar *
hexencode (const gchar *str,
gssize len)
{
guint n;
GString *s;
if (len == -1)
len = strlen (str);
s = g_string_new (NULL);
for (n = 0; n < len; n++)
{
gint val;
gint upper_nibble;
gint lower_nibble;
val = ((const guchar *) str)[n];
upper_nibble = val >> 4;
lower_nibble = val & 0x0f;
append_nibble (s, upper_nibble);
append_nibble (s, lower_nibble);
}
return g_string_free (s, FALSE);
}
/* ---------------------------------------------------------------------------------------------------- */
/* looks up an entry in the keyring */
static gchar *
keyring_lookup_entry (const gchar *cookie_context,
@@ -836,7 +800,7 @@ keyring_generate_entry (const gchar *cookie_context,
gchar *raw_cookie;
*out_id = max_line_id + 1;
raw_cookie = random_blob (32);
*out_cookie = hexencode (raw_cookie, 32);
*out_cookie = _g_dbus_hexencode (raw_cookie, 32);
g_free (raw_cookie);
g_string_append_printf (new_contents,

View File

@@ -2232,3 +2232,38 @@ _g_signal_accumulator_false_handled (GSignalInvocationHint *ihint,
return continue_emission;
}
/* ---------------------------------------------------------------------------------------------------- */
static void
append_nibble (GString *s, gint val)
{
g_string_append_c (s, val >= 10 ? ('a' + val - 10) : ('0' + val));
}
/* ---------------------------------------------------------------------------------------------------- */
gchar *
_g_dbus_hexencode (const gchar *str,
gsize str_len)
{
gsize n;
GString *s;
s = g_string_new (NULL);
for (n = 0; n < str_len; n++)
{
gint val;
gint upper_nibble;
gint lower_nibble;
val = ((const guchar *) str)[n];
upper_nibble = val >> 4;
lower_nibble = val & 0x0f;
append_nibble (s, upper_nibble);
append_nibble (s, lower_nibble);
}
return g_string_free (s, FALSE);
}

View File

@@ -141,6 +141,9 @@ void _g_dbus_object_proxy_add_interface (GDBusObjectProxy *proxy,
void _g_dbus_object_proxy_remove_interface (GDBusObjectProxy *proxy,
const gchar *interface_name);
gchar *_g_dbus_hexencode (const gchar *str,
gsize str_len);
/* Implemented in gdbusconnection.c */
GDBusConnection *_g_bus_get_singleton_if_exists (GBusType bus_type);