Author: olegk
Date: Wed Jan 29 14:19:50 2014
New Revision: 1562451

URL: http://svn.apache.org/r1562451
Log:
MIME4J-236: invalid handling of soft line breaks in strict mode

Modified:
    
james/mime4j/branches/apache-mime4j-0.7/core/src/main/java/org/apache/james/mime4j/codec/QuotedPrintableInputStream.java
    
james/mime4j/branches/apache-mime4j-0.7/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableInputStreamTest.java

Modified: 
james/mime4j/branches/apache-mime4j-0.7/core/src/main/java/org/apache/james/mime4j/codec/QuotedPrintableInputStream.java
URL: 
http://svn.apache.org/viewvc/james/mime4j/branches/apache-mime4j-0.7/core/src/main/java/org/apache/james/mime4j/codec/QuotedPrintableInputStream.java?rev=1562451&r1=1562450&r2=1562451&view=diff
==============================================================================
--- 
james/mime4j/branches/apache-mime4j-0.7/core/src/main/java/org/apache/james/mime4j/codec/QuotedPrintableInputStream.java
 (original)
+++ 
james/mime4j/branches/apache-mime4j-0.7/core/src/main/java/org/apache/james/mime4j/codec/QuotedPrintableInputStream.java
 Wed Jan 29 14:19:50 2014
@@ -186,12 +186,14 @@ public class QuotedPrintableInputStream 
                 int b = encoded[pos++] & 0xFF;
 
                 if (lastWasCR && b != LF) {
-                    if (monitor.warn("Found CR without LF", "Leaving it as 
is"))
+                    if (monitor.warn("Found CR without LF", "Leaving it as 
is")) {
                         throw new IOException("Found CR without LF");
+                    }
                     index = transfer(CR, buffer, index, to, false);
                 } else if (!lastWasCR && b == LF) {
-                    if (monitor.warn("Found LF without CR", "Translating to 
CRLF"))
+                    if (monitor.warn("Found LF without CR", "Translating to 
CRLF")) {
                         throw new IOException("Found LF without CR");
+                    }
                 }
 
                 if (b == CR) {
@@ -236,6 +238,15 @@ public class QuotedPrintableInputStream 
                         }
                     } else if (Character.isWhitespace((char) b2)) {
                         // soft line break
+                        int b3 = peek(0);
+                        if (!(b2 == CR && b3 == LF)) {
+                            if (monitor.warn("Found non-standard soft line 
break", "Translating to soft line break")) {
+                                throw new IOException("Non-standard soft line 
break");
+                            }
+                        }
+                        if (b3 == LF) {
+                            lastWasCR = b2 == CR;
+                        }
                         index = transfer(-1, buffer, index, to, true);
                         if (b2 != LF) {
                             blanks.append(b);

Modified: 
james/mime4j/branches/apache-mime4j-0.7/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableInputStreamTest.java
URL: 
http://svn.apache.org/viewvc/james/mime4j/branches/apache-mime4j-0.7/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableInputStreamTest.java?rev=1562451&r1=1562450&r2=1562451&view=diff
==============================================================================
--- 
james/mime4j/branches/apache-mime4j-0.7/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableInputStreamTest.java
 (original)
+++ 
james/mime4j/branches/apache-mime4j-0.7/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableInputStreamTest.java
 Wed Jan 29 14:19:50 2014
@@ -61,13 +61,34 @@ public class QuotedPrintableInputStreamT
         assertEquals("Soft line   Hard line\r\n", new String(read(decoder), 
"ISO8859-1"));
     }
 
-    public void testInvalidCR() throws IOException, 
UnsupportedEncodingException {
+    public void testSoftBreakStrictMode() throws IOException {
+        String input = "<?xml version=3D\"1.0\" standalone=3D\"no\"?>\r\n" +
+                "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" 
\"http://www.w3.org/Graphics/=\r\n"; +
+                "SVG/1.1/DTD/svg11.dtd\" >\r\n";
+        String expected = "<?xml version=\"1.0\" standalone=\"no\"?>\r\n" +
+                "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" 
\"http://www.w3.org/Graphics/"; +
+                "SVG/1.1/DTD/svg11.dtd\" >\r\n";
+        ByteArrayInputStream bis = new 
ByteArrayInputStream(input.getBytes("US-ASCII"));
+        QuotedPrintableInputStream decoder = new 
QuotedPrintableInputStream(bis, DecodeMonitor.STRICT);
+        assertEquals(expected, new String(read(decoder), "ISO8859-1"));
+    }
+
+    public void testInvalidCR() throws IOException {
         ByteArrayInputStream bis = new 
ByteArrayInputStream("Invalid=\rCR\rHard line   \r\n".getBytes("US-ASCII"));
         QuotedPrintableInputStream decoder = new 
QuotedPrintableInputStream(bis);
-        // TODO is this what we really expect from decoding a stream including 
CR with no LF?
         assertEquals("Invalid=\rCR\rHard line\r\n", new String(read(decoder), 
"ISO8859-1"));
     }
 
+    public void testInvalidCRStrictMode() throws IOException {
+        ByteArrayInputStream bis = new 
ByteArrayInputStream("Invalid=\rCR\rHard line   \r\n".getBytes("US-ASCII"));
+        QuotedPrintableInputStream decoder = new 
QuotedPrintableInputStream(bis, DecodeMonitor.STRICT);
+        try {
+            read(decoder);
+            fail("IOException should have been thrown");
+        } catch (IOException expected) {
+        }
+    }
+
     public void testSoftBreakLoneLFDecode() throws IOException, 
UnsupportedEncodingException {
         ByteArrayInputStream bis = new ByteArrayInputStream("Soft line   
=\nHard line   \r\n".getBytes("US-ASCII"));
         QuotedPrintableInputStream decoder = new 
QuotedPrintableInputStream(bis);


Reply via email to