diff options
Diffstat (limited to 'libnm-util/nm-utils.c')
| -rw-r--r-- | libnm-util/nm-utils.c | 170 |
1 files changed, 87 insertions, 83 deletions
diff --git a/libnm-util/nm-utils.c b/libnm-util/nm-utils.c index daa977cc..7f40fc7e 100644 --- a/libnm-util/nm-utils.c +++ b/libnm-util/nm-utils.c @@ -24,7 +24,6 @@ * (C) Copyright 2005 - 2010 Red Hat, Inc. */ -#include "config.h" #include <string.h> #include <stdio.h> #include <stdlib.h> @@ -215,6 +214,45 @@ get_encodings_for_lang (const char *lang, return success; } +static char * +string_to_utf8 (const char *str, gsize len) +{ + char *converted = NULL; + char *lang, *e1 = NULL, *e2 = NULL, *e3 = NULL; + + g_return_val_if_fail (str != NULL, NULL); + + if (g_utf8_validate (str, len, NULL)) + return g_strdup (str); + + /* LANG may be a good encoding hint */ + g_get_charset ((const char **)(&e1)); + if ((lang = getenv ("LANG"))) { + char * dot; + + lang = g_ascii_strdown (lang, -1); + if ((dot = strchr (lang, '.'))) + *dot = '\0'; + + get_encodings_for_lang (lang, &e1, &e2, &e3); + g_free (lang); + } + + converted = g_convert (str, len, "UTF-8", e1, NULL, NULL, NULL); + if (!converted && e2) + converted = g_convert (str, len, "UTF-8", e2, NULL, NULL, NULL); + + if (!converted && e3) + converted = g_convert (str, len, "UTF-8", e3, NULL, NULL, NULL); + + if (!converted) { + converted = g_convert_with_fallback (str, len, "UTF-8", e1, + "?", NULL, NULL, NULL); + } + + return converted; +} + /* init, deinit for libnm_util */ static gboolean initialized = FALSE; @@ -265,7 +303,8 @@ nm_utils_deinit (void) /** * nm_utils_ssid_to_utf8: - * @ssid: a byte array containing the SSID data + * @ssid: pointer to a buffer containing the SSID data + * @len: length of the SSID data in @ssid * * WiFi SSIDs are byte arrays, they are _not_ strings. Thus, an SSID may * contain embedded NULLs and other unprintable characters. Often it is @@ -290,46 +329,23 @@ nm_utils_deinit (void) * Again, this function should be used for debugging and display purposes * _only_. * - * Returns: (transfer full): an allocated string containing a UTF-8 - * representation of the SSID, which must be freed by the caller using g_free(). - * Returns NULL on errors. + * Returns: an allocated string containing a UTF-8 representation of the + * SSID, which must be freed by the caller using g_free(). Returns NULL + * on errors. **/ char * -nm_utils_ssid_to_utf8 (const GByteArray *ssid) +nm_utils_ssid_to_utf8 (const char *ssid, guint32 len) { - char *converted = NULL; - char *lang, *e1 = NULL, *e2 = NULL, *e3 = NULL; + char *converted = NULL, *buf; + gsize buflen = MIN (IW_ESSID_MAX_SIZE, (gsize) len); g_return_val_if_fail (ssid != NULL, NULL); - if (g_utf8_validate ((const gchar *) ssid->data, ssid->len, NULL)) - return g_strndup ((const gchar *) ssid->data, ssid->len); - - /* LANG may be a good encoding hint */ - g_get_charset ((const char **)(&e1)); - if ((lang = getenv ("LANG"))) { - char * dot; - - lang = g_ascii_strdown (lang, -1); - if ((dot = strchr (lang, '.'))) - *dot = '\0'; - - get_encodings_for_lang (lang, &e1, &e2, &e3); - g_free (lang); - } - - converted = g_convert ((const gchar *) ssid->data, ssid->len, "UTF-8", e1, NULL, NULL, NULL); - if (!converted && e2) - converted = g_convert ((const gchar *) ssid->data, ssid->len, "UTF-8", e2, NULL, NULL, NULL); - - if (!converted && e3) - converted = g_convert ((const gchar *) ssid->data, ssid->len, "UTF-8", e3, NULL, NULL, NULL); - - if (!converted) { - converted = g_convert_with_fallback ((const gchar *) ssid->data, ssid->len, - "UTF-8", e1, "?", NULL, NULL, NULL); - } - + /* New buffer to ensure NULL-termination of SSID */ + buf = g_malloc0 (IW_ESSID_MAX_SIZE + 1); + memcpy (buf, ssid, buflen); + converted = string_to_utf8 (buf, buflen); + g_free (buf); return converted; } @@ -470,7 +486,7 @@ value_dup (gpointer key, gpointer val, gpointer user_data) * * Utility function to duplicate a hash table of GValues. * - * Returns: (transfer container) (element-type utf8 GObject.Value): a newly allocated duplicated #GHashTable, caller must free the + * Returns: a newly allocated duplicated #GHashTable, caller must free the * returned hash with g_hash_table_unref() or g_hash_table_destroy() **/ GHashTable * @@ -642,8 +658,8 @@ nm_utils_convert_uint_array_to_string (const GValue *src_value, GValue *dest_val memset (buf, 0, sizeof (buf)); addr.s_addr = g_array_index (array, guint32, i++); if (!inet_ntop (AF_INET, &addr, buf, INET_ADDRSTRLEN)) - g_warning ("%s: error converting IP4 address 0x%X", - __func__, ntohl (addr.s_addr)); + nm_warning ("%s: error converting IP4 address 0x%X", + __func__, ntohl (addr.s_addr)); g_string_append_printf (printable, "%u (%s)", addr.s_addr, buf); } g_string_append_c (printable, ']'); @@ -684,8 +700,8 @@ nm_utils_convert_ip4_addr_route_struct_array_to_string (const GValue *src_value, memset (buf, 0, sizeof (buf)); addr.s_addr = g_array_index (array, guint32, 0); if (!inet_ntop (AF_INET, &addr, buf, INET_ADDRSTRLEN)) - g_warning ("%s: error converting IP4 address 0x%X", - __func__, ntohl (addr.s_addr)); + nm_warning ("%s: error converting IP4 address 0x%X", + __func__, ntohl (addr.s_addr)); if (is_addr) g_string_append_printf (printable, "ip = %s", buf); else @@ -702,8 +718,8 @@ nm_utils_convert_ip4_addr_route_struct_array_to_string (const GValue *src_value, memset (buf, 0, sizeof (buf)); addr.s_addr = g_array_index (array, guint32, 2); if (!inet_ntop (AF_INET, &addr, buf, INET_ADDRSTRLEN)) - g_warning ("%s: error converting IP4 address 0x%X", - __func__, ntohl (addr.s_addr)); + nm_warning ("%s: error converting IP4 address 0x%X", + __func__, ntohl (addr.s_addr)); if (is_addr) g_string_append_printf (printable, "gw = %s", buf); else @@ -819,8 +835,8 @@ nm_utils_inet6_ntop (struct in6_addr *addr, char *buf) g_string_append_printf (ip6_str, "%02X", addr->s6_addr[0]); for (i = 1; i < 16; i++) g_string_append_printf (ip6_str, " %02X", addr->s6_addr[i]); - g_warning ("%s: error converting IP6 address %s", - __func__, ip6_str->str); + nm_warning ("%s: error converting IP6 address %s", + __func__, ip6_str->str); g_string_free (ip6_str, TRUE); return FALSE; } @@ -1342,7 +1358,7 @@ nm_utils_security_valid (NMUtilsSecurityType type, * this serialization is not guaranteed to be stable and the #GArray may be * extended in the future. * - * Returns: (transfer full) (element-type NetworkManager.IP4Address): a newly allocated #GSList of #NMIP4Address objects + * Returns: a newly allocated #GSList of #NMIP4Address objects **/ GSList * nm_utils_ip4_addresses_from_gvalue (const GValue *value) @@ -1357,7 +1373,7 @@ nm_utils_ip4_addresses_from_gvalue (const GValue *value) NMIP4Address *addr; if (array->len < 3) { - g_warning ("Ignoring invalid IP4 address"); + nm_warning ("Ignoring invalid IP4 address"); continue; } @@ -1424,7 +1440,7 @@ nm_utils_ip4_addresses_to_gvalue (GSList *list, GValue *value) * format of this serialization is not guaranteed to be stable and may be * extended in the future. * - * Returns: (transfer full) (element-type NetworkManager.IP4Route): a newly allocated #GSList of #NMIP4Route objects + * Returns: a newly allocated #GSList of #NMIP4Route objects **/ GSList * nm_utils_ip4_routes_from_gvalue (const GValue *value) @@ -1439,7 +1455,7 @@ nm_utils_ip4_routes_from_gvalue (const GValue *value) NMIP4Route *route; if (array->len < 4) { - g_warning ("Ignoring invalid IP4 route"); + nm_warning ("Ignoring invalid IP4 route"); continue; } @@ -1587,7 +1603,7 @@ nm_utils_ip4_get_default_prefix (guint32 ip) * this serialization is not guaranteed to be stable and the #GValueArray may be * extended in the future. * - * Returns: (transfer full) (element-type NetworkManager.IP6Address): a newly allocated #GSList of #NMIP6Address objects + * Returns: a newly allocated #GSList of #NMIP6Address objects **/ GSList * nm_utils_ip6_addresses_from_gvalue (const GValue *value) @@ -1607,27 +1623,27 @@ nm_utils_ip6_addresses_from_gvalue (const GValue *value) guint32 prefix; if (elements->n_values < 2 || elements->n_values > 3) { - g_warning ("%s: ignoring invalid IP6 address structure", __func__); + nm_warning ("%s: ignoring invalid IP6 address structure", __func__); continue; } if ( (G_VALUE_TYPE (g_value_array_get_nth (elements, 0)) != DBUS_TYPE_G_UCHAR_ARRAY) || (G_VALUE_TYPE (g_value_array_get_nth (elements, 1)) != G_TYPE_UINT)) { - g_warning ("%s: ignoring invalid IP6 address structure", __func__); + nm_warning ("%s: ignoring invalid IP6 address structure", __func__); continue; } /* Check optional 3rd element (gateway) */ if ( elements->n_values == 3 && (G_VALUE_TYPE (g_value_array_get_nth (elements, 2)) != DBUS_TYPE_G_UCHAR_ARRAY)) { - g_warning ("%s: ignoring invalid IP6 address structure", __func__); + nm_warning ("%s: ignoring invalid IP6 address structure", __func__); continue; } tmp = g_value_array_get_nth (elements, 0); ba_addr = g_value_get_boxed (tmp); if (ba_addr->len != 16) { - g_warning ("%s: ignoring invalid IP6 address of length %d", + nm_warning ("%s: ignoring invalid IP6 address of length %d", __func__, ba_addr->len); continue; } @@ -1635,7 +1651,7 @@ nm_utils_ip6_addresses_from_gvalue (const GValue *value) tmp = g_value_array_get_nth (elements, 1); prefix = g_value_get_uint (tmp); if (prefix > 128) { - g_warning ("%s: ignoring invalid IP6 prefix %d", + nm_warning ("%s: ignoring invalid IP6 prefix %d", __func__, prefix); continue; } @@ -1644,7 +1660,7 @@ nm_utils_ip6_addresses_from_gvalue (const GValue *value) tmp = g_value_array_get_nth (elements, 2); ba_gw = g_value_get_boxed (tmp); if (ba_gw->len != 16) { - g_warning ("%s: ignoring invalid IP6 gateway address of length %d", + nm_warning ("%s: ignoring invalid IP6 gateway address of length %d", __func__, ba_gw->len); continue; } @@ -1730,7 +1746,7 @@ nm_utils_ip6_addresses_to_gvalue (GSList *list, GValue *value) * into a GSList of #NMIP6Route objects. The specific format of this serialization * is not guaranteed to be stable and may be extended in the future. * - * Returns: (transfer full) (element-type NetworkManager.IP6Route): a newly allocated #GSList of #NMIP6Route objects + * Returns: a newly allocated #GSList of #NMIP6Route objects **/ GSList * nm_utils_ip6_routes_from_gvalue (const GValue *value) @@ -1751,13 +1767,13 @@ nm_utils_ip6_routes_from_gvalue (const GValue *value) || (G_VALUE_TYPE (g_value_array_get_nth (route_values, 1)) != G_TYPE_UINT) || (G_VALUE_TYPE (g_value_array_get_nth (route_values, 2)) != DBUS_TYPE_G_UCHAR_ARRAY) || (G_VALUE_TYPE (g_value_array_get_nth (route_values, 3)) != G_TYPE_UINT)) { - g_warning ("Ignoring invalid IP6 route"); + nm_warning ("Ignoring invalid IP6 route"); continue; } dest = g_value_get_boxed (g_value_array_get_nth (route_values, 0)); if (dest->len != 16) { - g_warning ("%s: ignoring invalid IP6 dest address of length %d", + nm_warning ("%s: ignoring invalid IP6 dest address of length %d", __func__, dest->len); continue; } @@ -1766,7 +1782,7 @@ nm_utils_ip6_routes_from_gvalue (const GValue *value) next_hop = g_value_get_boxed (g_value_array_get_nth (route_values, 2)); if (next_hop->len != 16) { - g_warning ("%s: ignoring invalid IP6 next_hop address of length %d", + nm_warning ("%s: ignoring invalid IP6 next_hop address of length %d", __func__, next_hop->len); continue; } @@ -1846,18 +1862,6 @@ nm_utils_ip6_routes_to_gvalue (GSList *list, GValue *value) g_value_take_boxed (value, routes); } -/* FIXME: the Posix namespace does not exist, and thus neither does - the in6_addr struct. Marking (skip) for now */ -/** - * nm_utils_ip6_dns_from_gvalue: (skip): - * @value: a #GValue - * - * Converts a #GValue containing a #GPtrArray of IP6 DNS, represented as - * #GByteArray<!-- -->s into a #GSList of #in6_addr<!-- -->s. - * - * Returns: (transfer full) (element-type Posix.in6_addr): a #GSList of IP6 - * addresses. - */ GSList * nm_utils_ip6_dns_from_gvalue (const GValue *value) { @@ -1871,8 +1875,8 @@ nm_utils_ip6_dns_from_gvalue (const GValue *value) struct in6_addr *addr; if (bytearray->len != 16) { - g_warning ("%s: ignoring invalid IP6 address of length %d", - __func__, bytearray->len); + nm_warning ("%s: ignoring invalid IP6 address of length %d", + __func__, bytearray->len); continue; } @@ -1939,9 +1943,9 @@ nm_utils_uuid_generate_from_string (const char *s) char *buf = NULL; if (!nm_utils_init (&error)) { - g_warning ("error initializing crypto: (%d) %s", - error ? error->code : 0, - error ? error->message : "unknown"); + nm_warning ("error initializing crypto: (%d) %s", + error ? error->code : 0, + error ? error->message : "unknown"); if (error) g_error_free (error); return NULL; @@ -1949,9 +1953,9 @@ nm_utils_uuid_generate_from_string (const char *s) uuid = g_malloc0 (sizeof (*uuid)); if (!crypto_md5_hash (NULL, 0, s, strlen (s), (char *) uuid, sizeof (*uuid), &error)) { - g_warning ("error generating UUID: (%d) %s", - error ? error->code : 0, - error ? error->message : "unknown"); + nm_warning ("error generating UUID: (%d) %s", + error ? error->code : 0, + error ? error->message : "unknown"); if (error) g_error_free (error); goto out; @@ -2041,8 +2045,8 @@ utils_bin2hexstr (const char *bytes, int len, int final_len) /** * nm_utils_rsa_key_encrypt: * @data: RSA private key data to be encrypted - * @in_password: (allow-none): existing password to use, if any - * @out_password: (out) (allow-none): if @in_password was NULL, a random password will be generated + * @in_password: existing password to use, if any + * @out_password: if @in_password was NULL, a random password will be generated * and returned in this argument * @error: detailed error information on return, if an error occurred * @@ -2050,7 +2054,7 @@ utils_bin2hexstr (const char *bytes, int len, int final_len) * a password if no password was given) and converts the data to PEM format * suitable for writing to a file. * - * Returns: (transfer full): on success, PEM-formatted data suitable for writing to a PEM-formatted + * Returns: on success, PEM-formatted data suitable for writing to a PEM-formatted * certificate/private key file. **/ GByteArray * |