about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--rfc2047.c23
-rwxr-xr-xt/1701-mshow-regress.t26
2 files changed, 41 insertions, 8 deletions
diff --git a/rfc2047.c b/rfc2047.c
index b581976..3074b4d 100644
--- a/rfc2047.c
+++ b/rfc2047.c
@@ -82,16 +82,25 @@ blaze822_decode_b64(char *s, char *e, char **deco, size_t *decleno)
 	*deco = buf;
 
 	while (s + 4 <= e) {
-		while (s < e && isfws((unsigned char)*s))
-			s++;
-		if (s >= e)
-			break;
-
 		uint32_t v = 0;
 		unsigned char t = 0;
 
-		unsigned char c0 = s[0], c1 = s[1], c2 = s[2], c3 = s[3];
-		s += 4;
+#define SKIP_WS \
+		while (s < e && isfws((unsigned char)*s)) \
+			s++; \
+		if (s >= e) \
+			break;
+
+		SKIP_WS;
+		unsigned char c0 = *s++;
+		SKIP_WS;
+		unsigned char c1 = *s++;
+		SKIP_WS;
+		unsigned char c2 = *s++;
+		SKIP_WS;
+		unsigned char c3 = *s++;
+
+#undef SKIP_WS
 
 		if ((c0 | c1 | c2 | c3) > 127)
 			goto error;
diff --git a/t/1701-mshow-regress.t b/t/1701-mshow-regress.t
index ffb676d..96fe13f 100755
--- a/t/1701-mshow-regress.t
+++ b/t/1701-mshow-regress.t
@@ -1,7 +1,7 @@
 #!/bin/sh -e
 cd ${0%/*}
 . ./lib.sh
-plan 2
+plan 3
 
 # Mail with \n\n and \r\n\r\n
 cr=$(printf '\r')
@@ -27,3 +27,27 @@ EOF
 
 check 'mail has 3 attachments' 'mshow -t ./tmp | wc -l | grep 4'
 check 'mail attachment foo has size 35' 'mshow -t ./tmp | grep size=35.*name=\"foo\"'
+
+# Mail with linebreaks in base64 quartets
+cat <<EOF >tmp
+Subject: base64
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="----_=_2f8f1e2243b55f8618eaf0d9_=_"
+
+This is a multipart message in MIME format.
+
+------_=_2f8f1e2243b55f8618eaf0d9_=_
+Content-Disposition: attachment; filename=base64
+Content-Type: application/binary
+Content-Transfer-Encoding: base64
+
+dGhp
+cyBpc
+yBzb21
+lIGJhc2
+U2NAo=
+
+------_=_2f8f1e2243b55f8618eaf0d9_=_--
+EOF
+
+check 'mail decodes correctly' 'mshow -O ./tmp 2 | grep -q "this is some base64"'