From 4c63bd31890ce00467c054e44115519aa45025f8 Mon Sep 17 00:00:00 2001 From: Ralf Habacker Date: Tue, 9 Feb 2016 15:23:39 +0100 Subject: [PATCH] Add new functions _dbus_hash_table_to_array() and _dbus_hash_table_from_array() from related activation code. These functions are required for dbus-run-session. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=92899 --- bus/activation.c | 91 +--------------------------------------- dbus/dbus-hash.c | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ dbus/dbus-hash.h | 8 ++++ 3 files changed, 136 insertions(+), 89 deletions(-) diff --git a/bus/activation.c b/bus/activation.c index 67b353f..55e25fd 100644 --- a/bus/activation.c +++ b/bus/activation.c @@ -751,50 +751,7 @@ populate_environment (BusActivation *activation) if (environment == NULL) return FALSE; - if (!_dbus_string_init (&key)) - { - dbus_free_string_array (environment); - return FALSE; - } - - if (!_dbus_string_init (&value)) - { - _dbus_string_free (&key); - dbus_free_string_array (environment); - return FALSE; - } - - for (i = 0; environment[i] != NULL; i++) - { - if (!_dbus_string_append (&key, environment[i])) - break; - - if (_dbus_string_split_on_byte (&key, '=', &value)) - { - char *hash_key, *hash_value; - - if (!_dbus_string_steal_data (&key, &hash_key)) - break; - - if (!_dbus_string_steal_data (&value, &hash_value)) - break; - - if (!_dbus_hash_table_insert_string (activation->environment, - hash_key, hash_value)) - break; - } - _dbus_string_set_length (&key, 0); - _dbus_string_set_length (&value, 0); - } - - if (environment[i] != NULL) - goto out; - - retval = TRUE; -out: - - _dbus_string_free (&key); - _dbus_string_free (&value); + retval = _dbus_hash_table_from_array (activation->environment, environment, '='); dbus_free_string_array (environment); return retval; @@ -1611,51 +1568,7 @@ activation_find_entry (BusActivation *activation, static char ** bus_activation_get_environment (BusActivation *activation) { - char **environment; - int i, length; - DBusString entry; - DBusHashIter iter; - - length = _dbus_hash_table_get_n_entries (activation->environment); - - environment = dbus_new0 (char *, length + 1); - - if (environment == NULL) - return NULL; - - i = 0; - _dbus_hash_iter_init (activation->environment, &iter); - - if (!_dbus_string_init (&entry)) - { - dbus_free_string_array (environment); - return NULL; - } - - while (_dbus_hash_iter_next (&iter)) - { - const char *key, *value; - - key = (const char *) _dbus_hash_iter_get_string_key (&iter); - value = (const char *) _dbus_hash_iter_get_value (&iter); - - if (!_dbus_string_append_printf (&entry, "%s=%s", key, value)) - break; - - if (!_dbus_string_steal_data (&entry, environment + i)) - break; - i++; - } - - _dbus_string_free (&entry); - - if (i != length) - { - dbus_free_string_array (environment); - environment = NULL; - } - - return environment; + return _dbus_hash_table_to_array (activation->environment, '='); } dbus_bool_t diff --git a/dbus/dbus-hash.c b/dbus/dbus-hash.c index 8f7d04b..26ac4c2 100644 --- a/dbus/dbus-hash.c +++ b/dbus/dbus-hash.c @@ -1828,4 +1828,130 @@ _dbus_hash_test (void) return ret; } +/** + * Imports a string array into a hash table + * The hash table needs to be initialized as string hash table. + * + * @param table the hash table + * @param array the string array to import + * @param delimiter the delimiter to separate key and value + * @return #TRUE on success. + * @return #FALSE on failure. + */ + +dbus_bool_t +_dbus_hash_table_from_array (DBusHashTable *table, char **array, char delimiter) +{ + DBusString key; + DBusString value; + int i; + dbus_bool_t retval = FALSE; + + if (array == NULL) + return FALSE; + + if (!_dbus_string_init (&key)) + { + return FALSE; + } + + if (!_dbus_string_init (&value)) + { + _dbus_string_free (&key); + return FALSE; + } + + for (i = 0; array[i] != NULL; i++) + { + if (!_dbus_string_append (&key, array[i])) + break; + + if (_dbus_string_split_on_byte (&key, delimiter, &value)) + { + char *hash_key, *hash_value; + + if (!_dbus_string_steal_data (&key, &hash_key)) + break; + + if (!_dbus_string_steal_data (&value, &hash_value)) + break; + + if (!_dbus_hash_table_insert_string (table, + hash_key, hash_value)) + break; + } + _dbus_string_set_length (&key, 0); + _dbus_string_set_length (&value, 0); + } + + if (array[i] != NULL) + goto out; + + retval = TRUE; +out: + + _dbus_string_free (&key); + _dbus_string_free (&value); + + return retval; +} + +/** + * Creates a string array from a hash table + * + * @param table the hash table + * @param delimiter the delimiter to join key and value + * @return pointer to created string array + * @return #FALSE on failure. + */ +char ** +_dbus_hash_table_to_array (DBusHashTable *table, char delimiter) +{ + int i, length; + DBusString entry; + DBusHashIter iter; + char **array; + + length = _dbus_hash_table_get_n_entries (table); + + array = dbus_new0 (char *, length + 1); + + if (array == NULL) + return NULL; + + i = 0; + _dbus_hash_iter_init (table, &iter); + + if (!_dbus_string_init (&entry)) + { + dbus_free_string_array (array); + return NULL; + } + + while (_dbus_hash_iter_next (&iter)) + { + const char *key, *value; + + key = (const char *) _dbus_hash_iter_get_string_key (&iter); + value = (const char *) _dbus_hash_iter_get_value (&iter); + + if (!_dbus_string_append_printf (&entry, "%s%c%s", key, delimiter, value)) + break; + + if (!_dbus_string_steal_data (&entry, array + i)) + break; + i++; + } + + _dbus_string_free (&entry); + + if (i != length) + { + dbus_free_string_array (array); + array = NULL; + } + + return array; +} + #endif /* DBUS_ENABLE_EMBEDDED_TESTS */ diff --git a/dbus/dbus-hash.h b/dbus/dbus-hash.h index 2898f51..93f717a 100644 --- a/dbus/dbus-hash.h +++ b/dbus/dbus-hash.h @@ -133,6 +133,14 @@ dbus_bool_t _dbus_hash_table_insert_uintptr (DBusHashTable *table, DBUS_PRIVATE_EXPORT int _dbus_hash_table_get_n_entries (DBusHashTable *table); +DBUS_PRIVATE_EXPORT +char ** _dbus_hash_table_to_array (DBusHashTable *table, + char delimiter); +DBUS_PRIVATE_EXPORT +dbus_bool_t _dbus_hash_table_from_array (DBusHashTable *table, + char **array, + char delimiter); + /* Preallocation */ /** A preallocated hash entry */ -- 1.8.4.5