[Patch] Validate input in vpnc importer



Here's a patch to check if the pcf file values contain non-utf8
characters and tries quite hard to produce valid utf8 from it. First,
using the current locale and ISO-8859-15 (usable for majority of western
european languages) if that fails.

Tambet
? nm-vpnc-pcf-import-utf8.diff
? po/cs.gmo
? po/de.gmo
? po/es.gmo
? po/fi.gmo
? po/fr.gmo
? po/hu.gmo
? po/it.gmo
? po/ja.gmo
? po/pl.gmo
? po/pt_BR.gmo
? po/zh_CN.gmo
? po/zh_TW.gmo
Index: properties/pcf-file.c
===================================================================
RCS file: /cvs/gnome/NetworkManager/vpn-daemons/vpnc/properties/pcf-file.c,v
retrieving revision 1.1.2.1
diff -u -r1.1.2.1 pcf-file.c
--- properties/pcf-file.c	1 Nov 2006 16:33:28 -0000	1.1.2.1
+++ properties/pcf-file.c	13 Nov 2006 11:12:09 -0000
@@ -15,6 +15,32 @@
 	}
 }
 
+/* Stolen from gaim */
+
+static char *
+pcf_utf8_try_convert (const char *str)
+{
+	char *utf8;
+	gsize converted;
+
+	if (!str)
+		return NULL;
+
+	if (g_utf8_validate (str, -1, NULL))
+		return g_strdup (str);
+
+	utf8 = g_locale_to_utf8 (str, -1, NULL, NULL, NULL);
+	if (utf8)
+		return utf8;
+
+	utf8 = g_convert (str, -1, "UTF-8", "ISO-8859-15", &converted, NULL, NULL);
+	if (utf8 && converted == strlen (str))
+		return utf8;
+
+	g_free (utf8);
+	return NULL;
+}
+
 /*
   The main reader loop here is based on the simple .ini file
   parser from avahi/avahi-daemon/ini-file-parser.c
@@ -87,7 +113,7 @@
             *(e++) = 0;
 
 			entry = g_new (PcfEntry, 1);
-			entry->value = g_strdup (e);
+			entry->value = pcf_utf8_try_convert (e);
 
 			if (*s == '!') {
 				entry->key = g_utf8_strdown (s+1, -1);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]