diff libpurple/util.c @ 27101:bf9db4c67679

Add purple_utf8_strip_unprintables and use it on outgoing XMPP messages. We will no longer send messages which contain entities considered invalid in XML 1.0 (i.e.  and other ASCII control characters). Closes #5768.
author Paul Aurich <paul@darkrain42.org>
date Wed, 03 Jun 2009 19:09:16 +0000
parents af4a4ebc6441
children c59918579c3a
line wrap: on
line diff
--- a/libpurple/util.c	Wed Jun 03 17:09:18 2009 +0000
+++ b/libpurple/util.c	Wed Jun 03 19:09:16 2009 +0000
@@ -4424,6 +4424,34 @@
 	return g_string_free(workstr, FALSE);
 }
 
+gchar *
+purple_utf8_strip_unprintables(const gchar *str)
+{
+	gchar *workstr, *iter;
+
+	g_return_val_if_fail(str != NULL, NULL);
+	g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
+
+	workstr = iter = g_new(gchar, strlen(str) + 1);
+	while (*str) {
+		gunichar c = g_utf8_get_char(str);
+		const gchar *next = g_utf8_next_char(str);
+		size_t len = next - str;
+
+		if (g_unichar_isprint(c)) {
+			memcpy(iter, str, len);
+			iter += len;
+		}
+
+		str = next;
+	}
+
+	/* nul-terminate the new string */
+	*iter = '\0';
+
+	return workstr;
+}
+
 /*
  * This function is copied from g_strerror() but changed to use
  * gai_strerror().