summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDevashish Sharma <dsharma@src.gnome.org>2006-05-12 09:46:00 +0000
committerDevashish Sharma <dsharma@src.gnome.org>2006-05-12 09:46:00 +0000
commit527699f056e3ec0b431a7d7825b80c29eba14dad (patch)
tree89782cce8bb4cbd8f296408c40f2a0fa21cea45d
parent2d640ac44c85ae4c93ffb899adedb6d349c9950b (diff)
downloadevolution-data-server-527699f056e3ec0b431a7d7825b80c29eba14dad.tar.gz
Changes to vcard parsing so that it handles quoted printable characters correctly.
-rw-r--r--addressbook/ChangeLog9
-rw-r--r--addressbook/libebook/e-vcard.c162
-rw-r--r--addressbook/libebook/e-vcard.h4
-rw-r--r--addressbook/tests/vcard/12.vcf23
-rw-r--r--addressbook/tests/vcard/13.vcf23
-rw-r--r--addressbook/tests/vcard/README5
6 files changed, 150 insertions, 76 deletions
diff --git a/addressbook/ChangeLog b/addressbook/ChangeLog
index 671aff7db..b6bddac4e 100644
--- a/addressbook/ChangeLog
+++ b/addressbook/ChangeLog
@@ -1,3 +1,12 @@
+2006-05-12 Devashish Sharma <sdevashish@novell.com>
+
+ * libebook/e-vcard.c : Chages to vcard parsing so that it handles
+ quoted printable characters correctly.
+ * tests/vcard/12.vcf :
+ * tests/vcard/13.vcf :Added new test vcards.
+ Fixes Bug #334179.
+ Patch given by <ralf-engels@gmx.de>
+
2006-05-04 Ross Burton <ross@openedhand.com>
* libedata-book/e-book-backend-sexp.c:
diff --git a/addressbook/libebook/e-vcard.c b/addressbook/libebook/e-vcard.c
index a8e54fa05..1887fa57d 100644
--- a/addressbook/libebook/e-vcard.c
+++ b/addressbook/libebook/e-vcard.c
@@ -20,6 +20,10 @@
* Author: Chris Toshok (toshok@ximian.com)
*/
+/* This file implements the decoding of the v-card format
+ * http://www.imc.org/pdi/vcard-21.txt
+ */
+
#include <glib.h>
#include <stdio.h>
#include <string.h>
@@ -30,6 +34,9 @@
#define CRLF "\r\n"
+/** Encoding used in v-card
+ * Note: v-card spec defines additional 7BIT 8BIT and X- encoding
+ */
typedef enum {
EVC_ENCODING_RAW, /* no encoding */
EVC_ENCODING_BASE64, /* base64 */
@@ -127,49 +134,57 @@ e_vcard_get_type (void)
+/** Skip newline characters and return the next character.
+ * This function takes care of folding lines, skipping
+ * newline characters if found, taking care of equal characters
+ * and other strange things.
+ */
static char*
-fold_lines (char *buf)
+skip_newline (char *str, gboolean quoted_printable)
{
- GString *str = g_string_new ("");
- char *p = buf;
- char *next, *next2;
-
- /* we're pretty liberal with line folding here. We handle
- lines folded with \r\n<WS>... and \n\r<WS>... and
- \n<WS>... We also turn single \r's and \n's not followed by
- WS into \r\n's. */
- while (*p) {
- if (*p == '\r' || *p == '\n') {
- next = g_utf8_next_char (p);
- if (*next == '\n' || *next == '\r') {
- next2 = g_utf8_next_char (next);
- if (*next2 == ' ' || *next2 == '\t') {
- p = g_utf8_next_char (next2);
+ char *p;
+ char *next;
+ char *next2;
+ p = str;
+
+ /* -- swallow equal signs at end of line for quoted printable */
+ /* note: a quoted_printable linefolding is an equal sign followed by
+ one or more newline characters and optional a whitespace */
+ if (quoted_printable && *p == '=' ) {
+ next = g_utf8_next_char (p);
+ if (*next == '\r' || *next == '\n') {
+ p = g_utf8_next_char (next); /* swallow equal and newline */
+
+ if ((*p == '\r' || *p == '\n') && *p != *next ) {
+ p = g_utf8_next_char (p); /* swallow second newline */
+
+ if (*p == ' ' || *p == '\t') {
+ p = g_utf8_next_char (p); /* swallow whitespace */
}
- else {
- g_string_append (str, CRLF);
- p = g_utf8_next_char (next);
- }
- }
- else if (*next == ' ' || *next == '\t') {
- p = g_utf8_next_char (next);
- }
- else {
- g_string_append (str, CRLF);
- p = g_utf8_next_char (p);
}
+
}
- else {
- g_string_append_unichar (str, g_utf8_get_char (p));
- p = g_utf8_next_char (p);
+
+ /* -- swallow newline and (if present) following whitespaces */
+ } else if (*p == '\r' || *p == '\n') {
+
+ next = g_utf8_next_char (p);
+ if ((*next == '\n' || *next == '\r') && *p != *next) {
+
+ next2 = g_utf8_next_char (next);
+ if (*next2 == ' ' || *next2 == '\t') {
+ p = g_utf8_next_char (next2); /* we found a line folding */
+ }
+
+ } else if (*next == ' ' || *next == '\t') {
+ p = g_utf8_next_char (next); /* we found a line folding */
}
}
- g_free (buf);
-
- return g_string_free (str, FALSE);
+ return p;
}
+
/* skip forward until we hit the CRLF, or \0 */
static void
skip_to_next_line (char **p)
@@ -177,12 +192,12 @@ skip_to_next_line (char **p)
char *lp;
lp = *p;
- while (*lp != '\r' && *lp != '\0')
+ while (*lp != '\n' && *lp != '\r' && *lp != '\0')
lp = g_utf8_next_char (lp);
- if (*lp == '\r') {
- lp = g_utf8_next_char (lp); /* \n */
- lp = g_utf8_next_char (lp); /* start of the next line */
+ /* -- skip over the endline */
+ while( *lp == '\r' || *lp == '\n' ) {
+ lp = g_utf8_next_char (lp);
}
*p = lp;
@@ -223,20 +238,16 @@ read_attribute_value (EVCardAttribute *attr, char **p, gboolean quoted_printable
/* read in the value */
str = g_string_new ("");
- while (*lp != '\r' && *lp != '\0') {
+ for( lp = skip_newline( *p, quoted_printable );
+ *lp != '\n' && *lp != '\r' && *lp != '\0';
+ lp = skip_newline( lp, quoted_printable ) ) {
+
if (*lp == '=' && quoted_printable) {
char a, b;
if ((a = *(++lp)) == '\0') break;
if ((b = *(++lp)) == '\0') break;
- if (a == '\r' && b == '\n') {
- /* it was a = at the end of the line,
- * just ignore this and continue
- * parsing on the next line. yay for
- * 2 kinds of line folding
- */
- }
- else if (isxdigit(a) && isxdigit (b)) {
- gunichar c;
+ if (isxdigit(a) && isxdigit (b)) {
+ char c;
a = tolower (a);
b = tolower (b);
@@ -244,7 +255,7 @@ read_attribute_value (EVCardAttribute *attr, char **p, gboolean quoted_printable
c = (((a>='a'?a-'a'+10:a-'0')&0x0f) << 4)
| ((b>='a'?b-'a'+10:b-'0')&0x0f);
- g_string_append_unichar (str, c);
+ g_string_append_c (str, c); /* add decoded byte (this is not a unicode yet) */
}
else
{
@@ -253,8 +264,8 @@ read_attribute_value (EVCardAttribute *attr, char **p, gboolean quoted_printable
}
lp++;
- }
- else if (*lp == '\\') {
+
+ } else if (*lp == '\\') {
/* convert back to the non-escaped version of
the characters */
lp = g_utf8_next_char(lp);
@@ -264,7 +275,9 @@ read_attribute_value (EVCardAttribute *attr, char **p, gboolean quoted_printable
}
switch (*lp) {
case 'n': g_string_append_c (str, '\n'); break;
+ case 'N': g_string_append_c (str, '\n'); break;
case 'r': g_string_append_c (str, '\r'); break;
+ case 'R': g_string_append_c (str, '\r'); break;
case ';': g_string_append_c (str, ';'); break;
case ',': g_string_append_c (str, ','); break;
case '\\': g_string_append_c (str, '\\'); break;
@@ -292,10 +305,7 @@ read_attribute_value (EVCardAttribute *attr, char **p, gboolean quoted_printable
g_string_free (str, TRUE);
}
- if (*lp == '\r') {
- lp = g_utf8_next_char (lp); /* \n */
- lp = g_utf8_next_char (lp); /* start of the next line */
- }
+ skip_to_next_line( &lp );
*p = lp;
}
@@ -303,12 +313,16 @@ read_attribute_value (EVCardAttribute *attr, char **p, gboolean quoted_printable
static void
read_attribute_params (EVCardAttribute *attr, char **p, gboolean *quoted_printable)
{
- char *lp = *p;
+ char *lp;
GString *str;
EVCardAttributeParam *param = NULL;
gboolean in_quote = FALSE;
+
str = g_string_new ("");
- while (*lp != '\0') {
+ for( lp = skip_newline( *p, *quoted_printable );
+ *lp != '\n' && *lp != '\r' && *lp != '\0';
+ lp = skip_newline( lp, *quoted_printable ) ) {
+
if (*lp == '"') {
in_quote = !in_quote;
lp = g_utf8_next_char (lp);
@@ -331,13 +345,16 @@ read_attribute_params (EVCardAttribute *attr, char **p, gboolean *quoted_printab
}
else {
skip_until (&lp, ":;");
- if (*lp == '\r') {
- lp = g_utf8_next_char (lp); /* \n */
- lp = g_utf8_next_char (lp); /* start of the next line */
+ if (*lp == ';') {
+ lp = g_utf8_next_char (lp);
+
+ } else if (*lp == ':') {
+ /* do nothing */
+
+ } else {
+ skip_to_next_line( &lp );
break;
}
- else if (*lp == ';')
- lp = g_utf8_next_char (lp);
}
}
else if (*lp == ';' || *lp == ':' || *lp == ',') {
@@ -430,13 +447,9 @@ read_attribute_params (EVCardAttribute *attr, char **p, gboolean *quoted_printab
else {
g_warning ("invalid character found in parameter spec");
g_string_assign (str, "");
- skip_until (&lp, ":;");
+ /* skip_until (&lp, ":;"); */
- if (*lp == '\r') {
- lp = g_utf8_next_char (lp); /* \n */
- lp = g_utf8_next_char (lp); /* start of the next line */
- break;
- }
+ skip_to_next_line( &lp );
}
}
@@ -455,12 +468,15 @@ read_attribute (char **p)
char *attr_name = NULL;
EVCardAttribute *attr = NULL;
GString *str;
- char *lp = *p;
+ char *lp;
gboolean is_qp = FALSE;
/* first read in the group/name */
str = g_string_new ("");
- while (*lp != '\r' && *lp != '\0') {
+ for( lp = skip_newline( *p, is_qp );
+ *lp != '\n' && *lp != '\r' && *lp != '\0';
+ lp = skip_newline( lp, is_qp ) ) {
+
if (*lp == ':' || *lp == ';') {
if (str->len != 0) {
/* we've got a name, break out to the value/attribute parsing */
@@ -592,7 +608,7 @@ parse (EVCard *evc, const char *str)
buf = make_valid_utf8 (str);
- buf = fold_lines (buf);
+ //buf = fold_lines (buf);
d(printf ("BEFORE FOLDING:\n"));
d(printf (str));
@@ -638,7 +654,7 @@ parse (EVCard *evc, const char *str)
*
* Return value: A newly allocated, escaped string.
**/
-char*
+static char*
e_vcard_escape_string (const char *s)
{
GString *str;
@@ -683,7 +699,7 @@ e_vcard_escape_string (const char *s)
*
* Return value: A newly allocated, unescaped string.
**/
-char*
+static char*
e_vcard_unescape_string (const char *s)
{
GString *str;
@@ -1647,6 +1663,8 @@ e_vcard_attribute_get_value (EVCardAttribute *attr)
* Gets the value of a single-valued #EVCardAttribute, @attr, decoding
* it if necessary.
*
+ * Note: this function seems currently to be unused. Could be removed.
+ *
* Return value: A newly allocated #GString representing the value.
**/
GString*
diff --git a/addressbook/libebook/e-vcard.h b/addressbook/libebook/e-vcard.h
index 3791a1007..8ed58103d 100644
--- a/addressbook/libebook/e-vcard.h
+++ b/addressbook/libebook/e-vcard.h
@@ -186,10 +186,6 @@ GList* e_vcard_attribute_param_get_values (EVCardAttributeParam *param
/* special TYPE= parameter predicate (checks for TYPE=@typestr */
gboolean e_vcard_attribute_has_type (EVCardAttribute *attr, const char *typestr);
-/* Utility functions. */
-char* e_vcard_escape_string (const char *str);
-char* e_vcard_unescape_string (const char *str);
-
G_END_DECLS
#endif /* _EVCARD_H */
diff --git a/addressbook/tests/vcard/12.vcf b/addressbook/tests/vcard/12.vcf
new file mode 100644
index 000000000..d1c059841
--- /dev/null
+++ b/addressbook/tests/vcard/12.vcf
@@ -0,0 +1,23 @@
+BEGIN:VCARD
+VERSION:2.1
+N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=
+Br=C3=BCning;=
+Michael
+FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=
+Michael Br=C3=BCning
+TEL;CELL;WORK:+491622433834
+TEL;CELL;HOME:017623384942
+TEL;WORK:+4973117546691
+EMAIL;WORK:ext-Michael.Bruning@nokia.com
+ADR;HOME;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=
+;=
+;=
+M=C3=BCnsterplatz 21;=
+Ulm;=
+Baden-W=C3=BCrttemberg;=
+89073;=
+
+LABEL;HOME;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=
+M=C3=BCnsterplatz 21=0D=0A89073 Ulm, Baden-W=C3=BCrttemberg
+BDAY:19781229
+END:VCARD
diff --git a/addressbook/tests/vcard/13.vcf b/addressbook/tests/vcard/13.vcf
new file mode 100644
index 000000000..77831b84d
--- /dev/null
+++ b/addressbook/tests/vcard/13.vcf
@@ -0,0 +1,23 @@
+BEGIN:vCard
+VERSION:3.0
+FN:Ralf Engels
+ORG:Ralf Engels evil tests
+ADR;TYPE=WORK,POSTAL,PARCEL:;;Evil semicolon city\;
+ ;Semicolon\;way;NC;27613-3502;U.S.A.
+TEL;TYPE=VOICE,M
+ SG,WORK:+1-919-676-9515
+TEL;TYPE=
+ FAX,WORK:+1-919-676-9564
+EMAIL;TYPE=INTERNET
+ ,PREF:Frank_Dawson@Lotus.com
+EMAIL;TYPE=INTERNET:semicolon\;@points.net
+URL:http://home%32%20.earthlink.net/~fdawson
+ADR;HOME;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=
+;=
+;=
+Evil backslash \\ way;=
+Ulm;=
+Newlines\nCountry\Nevil;=
+89073;=
+
+END:vCard
diff --git a/addressbook/tests/vcard/README b/addressbook/tests/vcard/README
index 9baf674c1..46220132e 100644
--- a/addressbook/tests/vcard/README
+++ b/addressbook/tests/vcard/README
@@ -41,4 +41,9 @@ Test : Description
11.vcf : A vCard with invalid attribute parameter specs.
+12.vcf : A vCard with quoted parameter values that get corrupted if the
+ line folding mechanism does not correctly remove the equal sign
+
+13.vcf : A vCard with various escaped characters
+
----------------------