Commit b599e3e5 authored by Dan Williams's avatar Dan Williams

libnm-util: simplify nm_utils_ssid_to_utf8()

It's always used with a GByteArray anyway, as are most
functions in nm-utils.h.  Even better, we can skip the
memcpy since it turns out to be pointless.
parent ebc46813
...@@ -103,7 +103,7 @@ show_access_point_info (NMAccessPoint *ap) ...@@ -103,7 +103,7 @@ show_access_point_info (NMAccessPoint *ap)
strength = nm_access_point_get_strength (ap); strength = nm_access_point_get_strength (ap);
/* Convert to strings */ /* Convert to strings */
ssid_str = nm_utils_ssid_to_utf8 ((const char *) ssid->data, ssid->len); ssid_str = nm_utils_ssid_to_utf8 (ssid);
freq_str = g_strdup_printf ("%u MHz", freq); freq_str = g_strdup_printf ("%u MHz", freq);
bitrate_str = g_strdup_printf ("%u MB/s", bitrate/1000); bitrate_str = g_strdup_printf ("%u MB/s", bitrate/1000);
strength_str = g_strdup_printf ("%u", strength); strength_str = g_strdup_printf ("%u", strength);
...@@ -167,7 +167,7 @@ show_wifi_device_info (NMDevice *device) ...@@ -167,7 +167,7 @@ show_wifi_device_info (NMDevice *device)
if (nm_device_get_state (device) == NM_DEVICE_STATE_ACTIVATED) { if (nm_device_get_state (device) == NM_DEVICE_STATE_ACTIVATED) {
if ((active_ap = nm_device_wifi_get_active_access_point (NM_DEVICE_WIFI (device)))) { if ((active_ap = nm_device_wifi_get_active_access_point (NM_DEVICE_WIFI (device)))) {
active_ssid = nm_access_point_get_ssid (active_ap); active_ssid = nm_access_point_get_ssid (active_ap);
active_ssid_str = nm_utils_ssid_to_utf8 ((const char *) active_ssid->data, active_ssid->len); active_ssid_str = nm_utils_ssid_to_utf8 (active_ssid);
} }
} }
......
...@@ -214,45 +214,6 @@ get_encodings_for_lang (const char *lang, ...@@ -214,45 +214,6 @@ get_encodings_for_lang (const char *lang,
return success; 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 */ /* init, deinit for libnm_util */
static gboolean initialized = FALSE; static gboolean initialized = FALSE;
...@@ -304,8 +265,7 @@ nm_utils_deinit (void) ...@@ -304,8 +265,7 @@ nm_utils_deinit (void)
/** /**
* nm_utils_ssid_to_utf8: * nm_utils_ssid_to_utf8:
* @ssid: pointer to a buffer containing the SSID data * @ssid: a byte array 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 * WiFi SSIDs are byte arrays, they are _not_ strings. Thus, an SSID may
* contain embedded NULLs and other unprintable characters. Often it is * contain embedded NULLs and other unprintable characters. Often it is
...@@ -330,23 +290,46 @@ nm_utils_deinit (void) ...@@ -330,23 +290,46 @@ nm_utils_deinit (void)
* Again, this function should be used for debugging and display purposes * Again, this function should be used for debugging and display purposes
* _only_. * _only_.
* *
* Returns: an allocated string containing a UTF-8 representation of the * Returns: (transfer full): an allocated string containing a UTF-8
* SSID, which must be freed by the caller using g_free(). Returns NULL * representation of the SSID, which must be freed by the caller using g_free().
* on errors. * Returns NULL on errors.
**/ **/
char * char *
nm_utils_ssid_to_utf8 (const char *ssid, guint32 len) nm_utils_ssid_to_utf8 (const GByteArray *ssid)
{ {
char *converted = NULL, *buf; char *converted = NULL;
gsize buflen = MIN (IW_ESSID_MAX_SIZE, (gsize) len); char *lang, *e1 = NULL, *e2 = NULL, *e3 = NULL;
g_return_val_if_fail (ssid != NULL, NULL); g_return_val_if_fail (ssid != NULL, NULL);
/* New buffer to ensure NULL-termination of SSID */ if (g_utf8_validate ((const gchar *) ssid->data, ssid->len, NULL))
buf = g_malloc0 (IW_ESSID_MAX_SIZE + 1); return g_strndup ((const gchar *) ssid->data, ssid->len);
memcpy (buf, ssid, buflen);
converted = string_to_utf8 (buf, buflen); /* LANG may be a good encoding hint */
g_free (buf); 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);
}
return converted; return converted;
} }
......
...@@ -159,7 +159,7 @@ gboolean nm_utils_same_ssid (const GByteArray * ssid1, ...@@ -159,7 +159,7 @@ gboolean nm_utils_same_ssid (const GByteArray * ssid1,
const GByteArray * ssid2, const GByteArray * ssid2,
gboolean ignore_trailing_null); gboolean ignore_trailing_null);
char *nm_utils_ssid_to_utf8 (const char *ssid, guint32 len); char *nm_utils_ssid_to_utf8 (const GByteArray *ssid);
GHashTable *nm_utils_gvalue_hash_dup (GHashTable *hash); GHashTable *nm_utils_gvalue_hash_dup (GHashTable *hash);
......
...@@ -1468,7 +1468,7 @@ real_complete_connection (NMDevice *device, ...@@ -1468,7 +1468,7 @@ real_complete_connection (NMDevice *device,
} }
g_assert (ssid); g_assert (ssid);
str_ssid = nm_utils_ssid_to_utf8 ((const char *) ssid, ssid->len); str_ssid = nm_utils_ssid_to_utf8 (ssid);
format = g_strdup_printf ("%s %%d", str_ssid); format = g_strdup_printf ("%s %%d", str_ssid);
nm_utils_complete_generic (connection, nm_utils_complete_generic (connection,
......
...@@ -2907,7 +2907,7 @@ wireless_connection_from_ifcfg (const char *file, ...@@ -2907,7 +2907,7 @@ wireless_connection_from_ifcfg (const char *file,
ssid = nm_setting_wireless_get_ssid (NM_SETTING_WIRELESS (wireless_setting)); ssid = nm_setting_wireless_get_ssid (NM_SETTING_WIRELESS (wireless_setting));
if (ssid) if (ssid)
printable_ssid = nm_utils_ssid_to_utf8 ((const char *) ssid->data, ssid->len); printable_ssid = nm_utils_ssid_to_utf8 (ssid);
else else
printable_ssid = g_strdup_printf ("unmanaged"); printable_ssid = g_strdup_printf ("unmanaged");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment