File: 91_from_cvs_valid_utf8_vcard.patch

package info (click to toggle)
evolution-data-server 1.6.3-5etch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 59,384 kB
  • ctags: 43,218
  • sloc: ansic: 319,315; tcl: 30,499; xml: 19,166; sh: 18,776; perl: 11,529; cpp: 8,259; java: 7,653; makefile: 6,448; awk: 1,338; yacc: 1,103; sed: 772; cs: 505; lex: 134; asm: 14
file content (70 lines) | stat: -rw-r--r-- 1,984 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
diff -ru evolution-data-server-1.6.2.orig/addressbook/libebook/e-vcard.c evolution-data-server-1.6.2/addressbook/libebook/e-vcard.c
--- evolution-data-server-1.6.2.orig/addressbook/libebook/e-vcard.c	2006-05-03 15:59:23.000000000 +0200
+++ evolution-data-server-1.6.2/addressbook/libebook/e-vcard.c	2006-05-30 20:02:38.000000000 +0200
@@ -541,6 +541,44 @@
 	return NULL;
 }
 
+/* Stolen from glib/glib/gconvert.c */
+static gchar *
+make_valid_utf8 (const gchar *name)
+{
+	GString *string;
+	const gchar *remainder, *invalid;
+	gint remaining_bytes, valid_bytes;
+
+	string = NULL;
+	remainder = name;
+	remaining_bytes = strlen (name);
+
+	while (remaining_bytes != 0) {
+		if (g_utf8_validate (remainder, remaining_bytes, &invalid))
+			break;
+		valid_bytes = invalid - remainder;
+
+		if (string == NULL)
+			string = g_string_sized_new (remaining_bytes);
+
+		g_string_append_len (string, remainder, valid_bytes);
+		/* append U+FFFD REPLACEMENT CHARACTER */
+		g_string_append (string, "\357\277\275");
+
+		remaining_bytes -= valid_bytes + 1;
+		remainder = invalid + 1;
+	}
+
+	if (string == NULL)
+		return g_strdup (name);
+	
+	g_string_append (string, remainder);
+
+	g_assert (g_utf8_validate (string->str, -1, NULL));
+
+	return g_string_free (string, FALSE);
+}
+
 /* we try to be as forgiving as we possibly can here - this isn't a
  * validator.  Almost nothing is considered a fatal error.  We always
  * try to return *something*.
@@ -548,17 +586,12 @@
 static void
 parse (EVCard *evc, const char *str)
 {
-	char *buf = g_strdup (str);
-	char *p, *end;
+	char *buf;
+	char *p;
 	EVCardAttribute *attr;
 
-	/* first validate the string is valid utf8 */
-	if (!g_utf8_validate (buf, -1, (const char **)&end)) {
-		/* if the string isn't valid, we parse as much as we can from it */
-		g_warning ("invalid utf8 passed to EVCard.  Limping along.");
-		*end = '\0';
-	}
-	
+	buf = make_valid_utf8 (str);
+
 	buf = fold_lines (buf);
 
 	d(printf ("BEFORE FOLDING:\n"));