changeset 10700:9505695acff8

[gaim-migrate @ 12273] Fixed URL encoding (and filename encoding) to work with UTF-8 strings committer: Tailor Script <tailor@pidgin.im>
author Daniel Atallah <daniel.atallah@gmail.com>
date Fri, 18 Mar 2005 19:54:23 +0000
parents c8b4bf3bf9e5
children e4d893b12624
files src/util.c
diffstat 1 files changed, 39 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/src/util.c	Wed Mar 16 20:38:56 2005 +0000
+++ b/src/util.c	Fri Mar 18 19:54:23 2005 +0000
@@ -3097,19 +3097,29 @@
 const char *
 gaim_url_encode(const char *str)
 {
+	const char *iter;
 	static char buf[BUF_LEN];
+	char utf_char[6];
 	guint i, j = 0;
 
 	g_return_val_if_fail(str != NULL, NULL);
-
-	for (i = 0; i < strlen(str) && j < (BUF_LEN - 1); i++) {
-		if (isalnum(str[i]))
-			buf[j++] = str[i];
-		else {
-			if (j > (BUF_LEN - 4))
-				break;
-			sprintf(buf + j, "%%%02x", (unsigned char)str[i]);
-			j += 3;
+	g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
+
+	iter = str;
+	for (; *iter && j < (BUF_LEN - 1) ; iter = g_utf8_next_char(iter)) {
+		gunichar c = g_utf8_get_char(iter);
+		/* If the character is an ASCII character and is alphanumeric,
+		 * or one of the specified values, no need to escape */
+		if (c < 256 && isalnum(c)) {
+			buf[j++] = c;
+		} else {
+			int bytes = g_unichar_to_utf8(c, utf_char);
+			for (i = 0; i < bytes; i++) {
+				if (j > (BUF_LEN - 4))
+					break;
+				sprintf(buf + j, "%%%02x", utf_char[i] & 0xff);
+				j += 3;
+			}
 		}
 	}
 
@@ -3462,20 +3472,30 @@
 const char *
 gaim_escape_filename(const char *str)
 {
+	const char *iter;
 	static char buf[BUF_LEN];
+	char utf_char[6];
 	guint i, j = 0;
 
 	g_return_val_if_fail(str != NULL, NULL);
-
-	for (i = 0; i < strlen(str) && j < (BUF_LEN - 1); i++) {
-		if (isalnum(str[i]) || str[i] == '@' || str[i] == '-' ||
-				str[i] == '_' || str[i] == '.' || str[i] == '#')
-			buf[j++] = str[i];
-		else {
-			if (j > (BUF_LEN - 4))
-				break;
-			sprintf(buf + j, "%%%02x", (unsigned char)str[i]);
-			j += 3;
+	g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
+
+	iter = str;
+	for (; *iter && j < (BUF_LEN - 1) ; iter = g_utf8_next_char(iter)) {
+		gunichar c = g_utf8_get_char(iter);
+		/* If the character is an ASCII character and is alphanumeric,
+		 * or one of the specified values, no need to escape */
+		if (c < 256 && (isalnum(c) || c == '@' || c == '-' ||
+				c == '_' || c == '.' || c == '#')) {
+			buf[j++] = c;
+		} else {
+			int bytes = g_unichar_to_utf8(c, utf_char);
+			for (i = 0; i < bytes; i++) {
+				if (j > (BUF_LEN - 4))
+					break;
+				sprintf(buf + j, "%%%02x", utf_char[i] & 0xff);
+				j += 3;
+			}
 		}
 	}