pidgin: 595994cb: How about we just use a GString instead ...

markdoliner at pidgin.im markdoliner at pidgin.im
Thu Aug 13 22:54:05 EDT 2009


-----------------------------------------------------------------
Revision: 595994cbd9222742a876670d7503f564822c34c1
Ancestor: e61443cb17f6d03c10dddfe54c499d057a9ca362
Author: markdoliner at pidgin.im
Date: 2009-08-13T20:16:23
Branch: im.pidgin.pidgin
URL: http://d.pidgin.im/viewmtn/revision/info/595994cbd9222742a876670d7503f564822c34c1

Modified files:
        libpurple/util.c

ChangeLog: 

How about we just use a GString instead of this ridiculous
RECOVER_MARKED_TEXT macro?

-------------- next part --------------
============================================================
--- libpurple/util.c	a42b048d94b6975d4b6b5e84903f901021a99644
+++ libpurple/util.c	c2aa0df87c360230a718f41b5877275fee28612a
@@ -405,7 +405,7 @@ purple_mime_decode_field(const char *str
 	encoded_word_state_t state = state_start;
 	const char *cur, *mark;
 	const char *charset0 = NULL, *encoding0 = NULL, *encoded_text0 = NULL;
-	char *n, *new;
+	GString *new;
 
 	/* token can be any CHAR (supposedly ISO8859-1/ISO2022), not just ASCII */
 	#define token_char_p(c) \
@@ -415,16 +415,9 @@ purple_mime_decode_field(const char *str
 	#define encoded_text_char_p(c) \
 		((c & 0x80) == 0 && c != '?' && c != ' ' && isgraph(c))
 
-	#define RECOVER_MARKED_TEXT strncpy(n, mark, cur - mark + 1); \
-		n += cur - mark + 1
-
 	g_return_val_if_fail(str != NULL, NULL);
 
-	/* NOTE: Assuming that we need just strlen(str)+1 *may* be wrong.
-	 * It would be wrong if one byte (in some unknown encoding) could
-	 * expand to >=4 bytes of UTF-8; I don't know if there are such things.
-	 */
-	n = new = g_malloc(strlen(str) + 1);
+	new = g_string_new(NULL);
 
 	/* Here we will be looking for encoded words and if they seem to be
 	 * valid then decode them.
@@ -437,7 +430,7 @@ purple_mime_decode_field(const char *str
 			if (*cur == '?') {
 				state = state_question1;
 			} else {
-				RECOVER_MARKED_TEXT;
+				g_string_append_len(new, mark, cur - mark + 1);
 				state = state_start;
 			}
 			break;
@@ -446,7 +439,7 @@ purple_mime_decode_field(const char *str
 				charset0 = cur;
 				state = state_charset;
 			} else { /* This should never happen */
-				RECOVER_MARKED_TEXT;
+				g_string_append_len(new, mark, cur - mark + 1);
 				state = state_start;
 			}
 			break;
@@ -454,7 +447,7 @@ purple_mime_decode_field(const char *str
 			if (*cur == '?') {
 				state = state_question2;
 			} else if (!token_char_p(*cur)) { /* This should never happen */
-				RECOVER_MARKED_TEXT;
+				g_string_append_len(new, mark, cur - mark + 1);
 				state = state_start;
 			}
 			break;
@@ -463,7 +456,7 @@ purple_mime_decode_field(const char *str
 				encoding0 = cur;
 				state = state_encoding;
 			} else { /* This should never happen */
-				RECOVER_MARKED_TEXT;
+				g_string_append_len(new, mark, cur - mark + 1);
 				state = state_start;
 			}
 			break;
@@ -471,7 +464,7 @@ purple_mime_decode_field(const char *str
 			if (*cur == '?') {
 				state = state_question3;
 			} else if (!token_char_p(*cur)) { /* This should never happen */
-				RECOVER_MARKED_TEXT;
+				g_string_append_len(new, mark, cur - mark + 1);
 				state = state_start;
 			}
 			break;
@@ -483,7 +476,7 @@ purple_mime_decode_field(const char *str
 				encoded_text0 = cur;
 				state = state_question4;
 			} else { /* This should never happen */
-				RECOVER_MARKED_TEXT;
+				g_string_append_len(new, mark, cur - mark + 1);
 				state = state_start;
 			}
 			break;
@@ -491,7 +484,7 @@ purple_mime_decode_field(const char *str
 			if (*cur == '?') {
 				state = state_question4;
 			} else if (!encoded_text_char_p(*cur)) {
-				RECOVER_MARKED_TEXT;
+				g_string_append_len(new, mark, cur - mark + 1);
 				state = state_start;
 			}
 			break;
@@ -513,7 +506,7 @@ purple_mime_decode_field(const char *str
 					char *converted = g_convert((const gchar *)decoded, dec_len, "utf-8", charset, NULL, &len, NULL);
 
 					if (converted) {
-						n = strncpy(n, converted, len) + len;
+						g_string_append_len(new, converted, len);
 						g_free(converted);
 					}
 					g_free(decoded);
@@ -523,7 +516,7 @@ purple_mime_decode_field(const char *str
 				g_free(encoded_text);
 				state = state_equal2; /* Restart the FSM */
 			} else { /* This should never happen */
-				RECOVER_MARKED_TEXT;
+				g_string_append_len(new, mark, cur - mark + 1);
 				state = state_start;
 			}
 			break;
@@ -533,19 +526,16 @@ purple_mime_decode_field(const char *str
 				state = state_equal1;
 			} else {
 				/* Some unencoded text. */
-				*n = *cur;
-				n += 1;
+				g_string_append_c(new, *cur);
 			}
 			break;
 		} /* switch */
 	} /* for */
 
-	if (state != state_start) {
-		RECOVER_MARKED_TEXT;
-	}
-	*n = '\0';
+	if (state != state_start)
+		g_string_append_len(new, mark, cur - mark + 1);
 
-	return new;
+	return g_string_free(new, FALSE);;
 }
 
 


More information about the Commits mailing list