[Commits] quoted-printable.c 1.3.2.17 1.3.2.18 quoted-printable.h 1.3.2.9 1.3.2.10

miras at claws-mail.org miras at claws-mail.org
Tue Jul 3 19:23:02 CEST 2012


Update of /home/claws-mail/claws/src/common
In directory srv:/tmp/cvs-serv31901/src/common

Modified Files:
      Tag: gtk2
	quoted-printable.c quoted-printable.h 
Log Message:
2012-07-03 [mir]	3.8.1cvs2

	* src/messageview.c
	* src/procmime.c
	* src/common/quoted-printable.c
	* src/common/quoted-printable.h
	    Patch fixes bug 2640.
	    Patch provided by madroach claws at gmerlin dot de

Index: quoted-printable.c
===================================================================
RCS file: /home/claws-mail/claws/src/common/quoted-printable.c,v
retrieving revision 1.3.2.17
retrieving revision 1.3.2.18
diff -u -d -r1.3.2.17 -r1.3.2.18
--- quoted-printable.c	27 May 2012 17:31:01 -0000	1.3.2.17
+++ quoted-printable.c	3 Jul 2012 17:23:00 -0000	1.3.2.18
@@ -22,66 +22,81 @@
 
 #include "utils.h"
 
+
 #define MAX_LINELEN	76
 
 #define IS_LBREAK(p) \
-	(*(p) == '\0' || *(p) == '\n' || (*(p) == '\r' && *((p) + 1) == '\n'))
-
-#define SOFT_LBREAK_IF_REQUIRED(n)					\
-	if (len + (n) > MAX_LINELEN ||					\
-	    (len + (n) == MAX_LINELEN && (!IS_LBREAK(inp + 1)))) {	\
-		*outp++ = '=';						\
-		*outp++ = '\n';						\
-		len = 0;						\
-	}
+	((p)[0] == '\n' ? 1 : ((p)[0] == '\r' && (p)[1] == '\n') ? 2 : 0)
 
-void qp_encode_line(gchar *out, const guchar *in)
+gint qp_encode(gboolean text, gchar *out, const guchar *in, gint len)
 {
-	const guchar *inp = in;
-	gchar *outp = out;
-	guchar ch;
-	gint len = 0;
-
-	while (*inp != '\0') {
-		ch = *inp;
+	/* counters of input/output characters */
+	gint inc = 0;
+	gint outc = 1; /* one character reserved for '=' soft line break */
 
-		if (IS_LBREAK(inp)) {
-			*outp++ = '\n';
-			len = 0;
-			if (*inp == '\r')
-				inp++;
-			inp++;
-		} else if (ch == '\t' || ch == ' ') {
-			if (IS_LBREAK(inp + 1)) {
-				SOFT_LBREAK_IF_REQUIRED(3);
-				*outp++ = '=';
-				get_hex_str(outp, ch);
-				outp += 2;
-				len += 3;
-				inp++;
-			} else {
-				SOFT_LBREAK_IF_REQUIRED(1);
-				*outp++ = *inp++;
-				len++;
+	while(inc < len) {
+		/* allow literal linebreaks in text */
+		if(text) {
+			if(IS_LBREAK(in)) {
+				/* inserting linebreaks is the job of our caller */
+				g_assert(outc <= MAX_LINELEN);
+				*out = '\0';
+				return inc + IS_LBREAK(in);
 			}
-		} else if ((ch >= 33 && ch <= 60) || (ch >= 62 && ch <= 126)) {
-			SOFT_LBREAK_IF_REQUIRED(1);
-			*outp++ = *inp++;
-			len++;
-		} else {
-			SOFT_LBREAK_IF_REQUIRED(3);
-			*outp++ = '=';
-			get_hex_str(outp, ch);
-			outp += 2;
-			len += 3;
-			inp++;
+			if(IS_LBREAK(in+1)) {
+				/* free the reserved character since no softbreak
+				 * will be needed after the current character */
+				outc--;
+				/* guard against whitespace before a literal linebreak */
+				if(*in == ' ' || *in == '\t') {
+					goto escape;
+				}
+			}
+		}
+		if(*in == '=') {
+			goto escape;
+		}
+		/* Cave: Whitespace is unconditionally output literally,
+		 * but according to the RFC it must not be output before a
+		 * linebreak. 
+		 * This requirement is obeyed by quoting all linebreaks
+		 * and therefore ending all lines with '='. */
+		else if((*in >= ' ' && *in <= '~') || *in == '\t') {
+			if(outc + 1 <= MAX_LINELEN) {
+				*out++ = *in++;
+				outc++;
+				inc++;
+			}
+			else break;
+		}
+		else {
+escape:
+			if(outc + 3 <= MAX_LINELEN) {
+				*out++ = '=';
+				outc++;
+				get_hex_str(out, *in);
+				out += 2;
+				outc += 2;
+				in++;
+				inc++;
+			}
+			else break;
 		}
 	}
+	g_assert(outc <= MAX_LINELEN);
+	*out++ = '=';
+	*out = '\0';
+	return inc;
+}
 
-	if (len > 0)
-		*outp++ = '\n';
+void qp_encode_line(gchar *out, const guchar *in) {
+	while (*in != '\0') {
+		in += qp_encode(TRUE, out, in, strlen(in));
 
-	*outp = '\0';
+		while(*out != '\0') out++;
+		*out++ = '\n';
+		*out++ = '\0';
+	}
 }
 
 gint qp_decode_line(gchar *str)

Index: quoted-printable.h
===================================================================
RCS file: /home/claws-mail/claws/src/common/quoted-printable.h,v
retrieving revision 1.3.2.9
retrieving revision 1.3.2.10
diff -u -d -r1.3.2.9 -r1.3.2.10
--- quoted-printable.h	27 May 2012 17:31:01 -0000	1.3.2.9
+++ quoted-printable.h	3 Jul 2012 17:23:00 -0000	1.3.2.10
@@ -22,6 +22,18 @@
 
 #include <glib.h>
 
+/* Processes at most 78 characters from in buffer,
+ * and stores one NULL-terminated line of at most 76 characters (excl. \0) of
+ * quoted-printable output without terminating newline characters in out buffer.
+ * Except when encoding text, every output line ends in a soft line break.
+ * Therefore the caller can chain multiple lines of encoded data resulting from
+ * sequential runs by glueing them together with line breaks.
+ * The number of processed input characters is returned. */
+gint qp_encode			(gboolean	text,
+				 gchar		*out,
+				 const guchar	*in,
+				 gint		len);
+/* Deprecated */
 void qp_encode_line		(gchar		*out,
 				 const guchar	*in);
 gint qp_decode_line		(gchar		*str);



More information about the Commits mailing list