summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2017-05-19 10:32:13 +0200
committerThomas Haller <thaller@redhat.com>2017-05-19 12:45:45 +0200
commite255ad2a03c84f806f9606b420fa12757bbd883f (patch)
tree1aebde89bacbc3eeed757b25a7429eeb023255c7
parent8df944c7e495d18bfecaf9d8316ef7783039c94b (diff)
downloadNetworkManager-th/sanitize-non-utf8-rh1443114-pt2.tar.gz
libnm: move fixup_desc_string() to nm-libnm-utils.cth/sanitize-non-utf8-rh1443114-pt2
-rw-r--r--libnm/nm-device.c141
-rw-r--r--libnm/nm-libnm-utils.c135
-rw-r--r--libnm/nm-libnm-utils.h2
-rw-r--r--libnm/tests/test-general.c35
4 files changed, 174 insertions, 139 deletions
diff --git a/libnm/nm-device.c b/libnm/nm-device.c
index 3a6052bd9c..969d08a620 100644
--- a/libnm/nm-device.c
+++ b/libnm/nm-device.c
@@ -26,6 +26,7 @@
#include <string.h>
#include <libudev.h>
+#include "nm-libnm-utils.h"
#include "nm-dbus-interface.h"
#include "nm-active-connection.h"
#include "nm-device-bt.h"
@@ -1431,142 +1432,6 @@ nm_device_get_vendor (NMDevice *device)
return priv->vendor;
}
-static char *
-fixup_desc_string (const char *desc)
-{
- static const char *const IGNORED_PHRASES[] = {
- "Multiprotocol MAC/baseband processor",
- "Wireless LAN Controller",
- "Wireless LAN Adapter",
- "Wireless Adapter",
- "Network Connection",
- "Wireless Cardbus Adapter",
- "Wireless CardBus Adapter",
- "54 Mbps Wireless PC Card",
- "Wireless PC Card",
- "Wireless PC",
- "PC Card with XJACK(r) Antenna",
- "Wireless cardbus",
- "Wireless LAN PC Card",
- "Technology Group Ltd.",
- "Communication S.p.A.",
- "Business Mobile Networks BV",
- "Mobile Broadband Minicard Composite Device",
- "Mobile Communications AB",
- "(PC-Suite Mode)",
- };
- static const char *const IGNORED_WORDS[] = {
- "Semiconductor",
- "Components",
- "Corporation",
- "Communications",
- "Company",
- "Corp.",
- "Corp",
- "Co.",
- "Inc.",
- "Inc",
- "Incorporated",
- "Ltd.",
- "Limited.",
- "Intel?",
- "chipset",
- "adapter",
- "[hex]",
- "NDIS",
- "Module",
- };
- char *desc_full;
- char *p, *q;
- int i;
-
- if (!desc || !desc[0])
- return NULL;
-
- /* restore original non-UTF-8-safe text. */
- desc_full = nm_utils_str_utf8safe_unescape_cp (desc);
-
- /* replace all invalid UTF-8 bytes with space. */
- p = desc_full;
- while (!g_utf8_validate (p, -1, (const char **) &q)) {
- /* the byte is invalid UTF-8. Replace it with space and proceed. */
- *q = ' ';
- p = q + 1;
- }
-
- /* replace '_', ',', and ASCII controll characters with space. */
- for (p = desc_full; p[0]; p++) {
- if ( NM_IN_SET (*p, '_', ',')
- || *p < ' ')
- *p = ' ';
- }
-
- /* Attempt to shorten ID by ignoring certain phrases */
- for (i = 0; i < G_N_ELEMENTS (IGNORED_PHRASES); i++) {
- p = strstr (desc_full, IGNORED_PHRASES[i]);
- if (p) {
- const char *eow = &p[strlen (IGNORED_PHRASES[i])];
-
- /* require that the phrase is delimited by space, or
- * at the beginning or end of the description. */
- if ( (p == desc_full || p[-1] == ' ')
- && NM_IN_SET (eow[0], '\0', ' '))
- memmove (p, eow, strlen (eow) + 1); /* +1 for the \0 */
- }
- }
-
- /* Attempt to shorten ID by ignoring certain individual words.
- * - word-split the description at spaces
- * - coalesce multiple spaces
- * - skip over IGNORED_WORDS */
- p = desc_full;
- q = desc_full;
- for (;;) {
- char *eow;
- gsize l;
-
- /* skip leading spaces. */
- while (p[0] == ' ')
- p++;
-
- if (!p[0])
- break;
-
- /* split leading word on first space */
- eow = strchr (p, ' ');
- if (eow)
- *eow = '\0';
-
- if (nm_utils_strv_find_first ((char **) IGNORED_WORDS,
- G_N_ELEMENTS (IGNORED_WORDS),
- p) >= 0)
- goto next;
-
- l = strlen (p);
- if (q != p) {
- if (q != desc_full)
- *q++ = ' ';
- memmove (q, p, l);
- }
- q += l;
-
-next:
- if (!eow)
- break;
- p = eow + 1;
- }
-
- *q++ = '\0';
-
- if (!desc_full[0]) {
- g_free (desc_full);
- return NULL;
- }
-
- nm_assert (g_utf8_validate (desc_full, -1, NULL));
- return desc_full;
-}
-
static void
ensure_description (NMDevice *device)
{
@@ -1574,7 +1439,7 @@ ensure_description (NMDevice *device)
GParamSpec *name_prop;
gs_free char *short_product = NULL;
- priv->short_vendor = nm_str_realloc (fixup_desc_string (nm_device_get_vendor (device)));
+ priv->short_vendor = nm_str_realloc (nm_utils_fixup_desc_string (nm_device_get_vendor (device)));
/* Grab device's preferred name, if any */
name_prop = g_object_class_find_property (G_OBJECT_GET_CLASS (G_OBJECT (device)), "name");
@@ -1586,7 +1451,7 @@ ensure_description (NMDevice *device)
}
if ( !priv->short_vendor
- || !(short_product = fixup_desc_string (nm_device_get_product (device)))) {
+ || !(short_product = nm_utils_fixup_desc_string (nm_device_get_product (device)))) {
priv->description = g_strdup (nm_device_get_iface (device) ?: "");
return;
}
diff --git a/libnm/nm-libnm-utils.c b/libnm/nm-libnm-utils.c
index 8cea276059..fbbfe2c552 100644
--- a/libnm/nm-libnm-utils.c
+++ b/libnm/nm-libnm-utils.c
@@ -25,3 +25,138 @@
/*****************************************************************************/
+char *
+nm_utils_fixup_desc_string (const char *desc)
+{
+ static const char *const IGNORED_PHRASES[] = {
+ "Multiprotocol MAC/baseband processor",
+ "Wireless LAN Controller",
+ "Wireless LAN Adapter",
+ "Wireless Adapter",
+ "Network Connection",
+ "Wireless Cardbus Adapter",
+ "Wireless CardBus Adapter",
+ "54 Mbps Wireless PC Card",
+ "Wireless PC Card",
+ "Wireless PC",
+ "PC Card with XJACK(r) Antenna",
+ "Wireless cardbus",
+ "Wireless LAN PC Card",
+ "Technology Group Ltd.",
+ "Communication S.p.A.",
+ "Business Mobile Networks BV",
+ "Mobile Broadband Minicard Composite Device",
+ "Mobile Communications AB",
+ "(PC-Suite Mode)",
+ };
+ static const char *const IGNORED_WORDS[] = {
+ "Semiconductor",
+ "Components",
+ "Corporation",
+ "Communications",
+ "Company",
+ "Corp.",
+ "Corp",
+ "Co.",
+ "Inc.",
+ "Inc",
+ "Incorporated",
+ "Ltd.",
+ "Limited.",
+ "Intel?",
+ "chipset",
+ "adapter",
+ "[hex]",
+ "NDIS",
+ "Module",
+ };
+ char *desc_full;
+ char *p, *q;
+ int i;
+
+ if (!desc || !desc[0])
+ return NULL;
+
+ /* restore original non-UTF-8-safe text. */
+ desc_full = nm_utils_str_utf8safe_unescape_cp (desc);
+
+ /* replace all invalid UTF-8 bytes with space. */
+ p = desc_full;
+ while (!g_utf8_validate (p, -1, (const char **) &q)) {
+ /* the byte is invalid UTF-8. Replace it with space and proceed. */
+ *q = ' ';
+ p = q + 1;
+ }
+
+ /* replace '_', ',', and ASCII controll characters with space. */
+ for (p = desc_full; p[0]; p++) {
+ if ( NM_IN_SET (*p, '_', ',')
+ || *p < ' ')
+ *p = ' ';
+ }
+
+ /* Attempt to shorten ID by ignoring certain phrases */
+ for (i = 0; i < G_N_ELEMENTS (IGNORED_PHRASES); i++) {
+ p = strstr (desc_full, IGNORED_PHRASES[i]);
+ if (p) {
+ const char *eow = &p[strlen (IGNORED_PHRASES[i])];
+
+ /* require that the phrase is delimited by space, or
+ * at the beginning or end of the description. */
+ if ( (p == desc_full || p[-1] == ' ')
+ && NM_IN_SET (eow[0], '\0', ' '))
+ memmove (p, eow, strlen (eow) + 1); /* +1 for the \0 */
+ }
+ }
+
+ /* Attempt to shorten ID by ignoring certain individual words.
+ * - word-split the description at spaces
+ * - coalesce multiple spaces
+ * - skip over IGNORED_WORDS */
+ p = desc_full;
+ q = desc_full;
+ for (;;) {
+ char *eow;
+ gsize l;
+
+ /* skip leading spaces. */
+ while (p[0] == ' ')
+ p++;
+
+ if (!p[0])
+ break;
+
+ /* split leading word on first space */
+ eow = strchr (p, ' ');
+ if (eow)
+ *eow = '\0';
+
+ if (nm_utils_strv_find_first ((char **) IGNORED_WORDS,
+ G_N_ELEMENTS (IGNORED_WORDS),
+ p) >= 0)
+ goto next;
+
+ l = strlen (p);
+ if (q != p) {
+ if (q != desc_full)
+ *q++ = ' ';
+ memmove (q, p, l);
+ }
+ q += l;
+
+next:
+ if (!eow)
+ break;
+ p = eow + 1;
+ }
+
+ *q++ = '\0';
+
+ if (!desc_full[0]) {
+ g_free (desc_full);
+ return NULL;
+ }
+
+ nm_assert (g_utf8_validate (desc_full, -1, NULL));
+ return desc_full;
+}
diff --git a/libnm/nm-libnm-utils.h b/libnm/nm-libnm-utils.h
index 356b2f9dab..4a5a361c51 100644
--- a/libnm/nm-libnm-utils.h
+++ b/libnm/nm-libnm-utils.h
@@ -21,6 +21,6 @@
#ifndef __NM_LIBNM_UTILS_H__
#define __NM_LIBNM_UTILS_H__
-
+char *nm_utils_fixup_desc_string (const char *desc);
#endif /* __NM_LIBNM_UTILS_H__ */
diff --git a/libnm/tests/test-general.c b/libnm/tests/test-general.c
index 2653cb913d..7e0b7bb740 100644
--- a/libnm/tests/test-general.c
+++ b/libnm/tests/test-general.c
@@ -20,15 +20,50 @@
#include "nm-default.h"
+#include "nm-libnm-utils.h"
+
#include "nm-utils/nm-test-utils.h"
/*****************************************************************************/
+static void
+do_test_fixup_desc_string (const char *desc, const char *expected)
+{
+ gs_free char *result = NULL;
+
+ result = nm_utils_fixup_desc_string (desc);
+ g_assert_cmpstr (result, ==, expected);
+}
+
+#define do_test_fixup_desc_string_same(desc) (do_test_fixup_desc_string (""desc"", ""desc""))
+
+static void
+test_fixup_desc_string (void)
+{
+ do_test_fixup_desc_string (NULL, NULL);
+ do_test_fixup_desc_string ("", NULL);
+ do_test_fixup_desc_string_same ("a");
+ do_test_fixup_desc_string_same ("a b");
+ do_test_fixup_desc_string ("a b ", "a b");
+ do_test_fixup_desc_string (" a bbc ", "a bbc");
+ do_test_fixup_desc_string (" a \xcc bbc ", "a bbc");
+ do_test_fixup_desc_string (" a\xcc bbc ", "a bbc");
+ do_test_fixup_desc_string (" a\xcc""bbc Wireless PC", "a bbc");
+ do_test_fixup_desc_string (" a\xcc""bbc Wireless PC ", "a bbc");
+ do_test_fixup_desc_string (" a\xcc""bbcWireless PC ", "a bbcWireless PC");
+ do_test_fixup_desc_string (" a\xcc""bbc Wireless PCx", "a bbc Wireless PCx");
+ do_test_fixup_desc_string (" a\xcc""bbc Inc Wireless PC ", "a bbc");
+}
+
+/*****************************************************************************/
+
NMTST_DEFINE ();
int main (int argc, char **argv)
{
nmtst_init (&argc, &argv, TRUE);
+ g_test_add_func ("/libnm/general/fixup_desc_string", test_fixup_desc_string);
+
return g_test_run ();
}