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.
This commit is contained in:
Dan Williams 2011-02-23 11:24:58 -06:00
parent ebc4681368
commit b599e3e5dc
5 changed files with 40 additions and 57 deletions

View file

@ -103,7 +103,7 @@ show_access_point_info (NMAccessPoint *ap)
strength = nm_access_point_get_strength (ap);
/* 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);
bitrate_str = g_strdup_printf ("%u MB/s", bitrate/1000);
strength_str = g_strdup_printf ("%u", strength);
@ -167,7 +167,7 @@ show_wifi_device_info (NMDevice *device)
if (nm_device_get_state (device) == NM_DEVICE_STATE_ACTIVATED) {
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_str = nm_utils_ssid_to_utf8 ((const char *) active_ssid->data, active_ssid->len);
active_ssid_str = nm_utils_ssid_to_utf8 (active_ssid);
}
}

View file

@ -214,45 +214,6 @@ 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;
@ -304,8 +265,7 @@ nm_utils_deinit (void)
/**
* nm_utils_ssid_to_utf8:
* @ssid: pointer to a buffer containing the SSID data
* @len: length of the SSID data in @ssid
* @ssid: a byte array containing the SSID data
*
* WiFi SSIDs are byte arrays, they are _not_ strings. Thus, an SSID may
* contain embedded NULLs and other unprintable characters. Often it is
@ -330,23 +290,46 @@ nm_utils_deinit (void)
* Again, this function should be used for debugging and display purposes
* _only_.
*
* 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.
* 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.
**/
char *
nm_utils_ssid_to_utf8 (const char *ssid, guint32 len)
nm_utils_ssid_to_utf8 (const GByteArray *ssid)
{
char *converted = NULL, *buf;
gsize buflen = MIN (IW_ESSID_MAX_SIZE, (gsize) len);
char *converted = NULL;
char *lang, *e1 = NULL, *e2 = NULL, *e3 = NULL;
g_return_val_if_fail (ssid != 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);
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);
}
return converted;
}

View file

@ -159,7 +159,7 @@ gboolean nm_utils_same_ssid (const GByteArray * ssid1,
const GByteArray * ssid2,
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);

View file

@ -1468,7 +1468,7 @@ real_complete_connection (NMDevice *device,
}
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);
nm_utils_complete_generic (connection,

View file

@ -2907,7 +2907,7 @@ wireless_connection_from_ifcfg (const char *file,
ssid = nm_setting_wireless_get_ssid (NM_SETTING_WIRELESS (wireless_setting));
if (ssid)
printable_ssid = nm_utils_ssid_to_utf8 ((const char *) ssid->data, ssid->len);
printable_ssid = nm_utils_ssid_to_utf8 (ssid);
else
printable_ssid = g_strdup_printf ("unmanaged");