Author: olegk
Date: Wed Jan 29 14:18:49 2014
New Revision: 1562450
URL: http://svn.apache.org/r1562450
Log:
MIME4J-236: invalid handling of soft line breaks in strict mode
Modified:
james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/codec/QuotedPrintableInputStream.java
james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableInputStreamTest.java
Modified:
james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/codec/QuotedPrintableInputStream.java
URL:
http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/codec/QuotedPrintableInputStream.java?rev=1562450&r1=1562449&r2=1562450&view=diff
==============================================================================
---
james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/codec/QuotedPrintableInputStream.java
(original)
+++
james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/codec/QuotedPrintableInputStream.java
Wed Jan 29 14:18:49 2014
@@ -185,12 +185,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) {
@@ -235,6 +237,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/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableInputStreamTest.java
URL:
http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableInputStreamTest.java?rev=1562450&r1=1562449&r2=1562450&view=diff
==============================================================================
---
james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableInputStreamTest.java
(original)
+++
james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableInputStreamTest.java
Wed Jan 29 14:18:49 2014
@@ -72,6 +72,19 @@ public class QuotedPrintableInputStreamT
}
@Test
+ 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";
+ InputStream bis = InputStreams.createAscii(input);
+ QuotedPrintableInputStream decoder = new
QuotedPrintableInputStream(bis, DecodeMonitor.STRICT);
+ Assert.assertEquals(expected, readText(decoder));
+ }
+
+ @Test
public void testInvalidCR() throws IOException,
UnsupportedEncodingException {
InputStream bis = InputStreams.createAscii("Invalid=\rCR\rHard line
\r\n");
QuotedPrintableInputStream decoder = new
QuotedPrintableInputStream(bis);
@@ -80,6 +93,17 @@ public class QuotedPrintableInputStreamT
}
@Test
+ public void testInvalidCRStrictMode() throws IOException {
+ InputStream bis = InputStreams.createAscii("Invalid=\rCR\rHard line
\r\n");
+ QuotedPrintableInputStream decoder = new
QuotedPrintableInputStream(bis, DecodeMonitor.STRICT);
+ try {
+ readText(decoder);
+ Assert.fail("IOException should have been thrown");
+ } catch (IOException expected) {
+ }
+ }
+
+ @Test
public void testSoftBreakLoneLFDecode() throws IOException,
UnsupportedEncodingException {
InputStream bis = InputStreams.createAscii("Soft line =\nHard line
\r\n");
QuotedPrintableInputStream decoder = new
QuotedPrintableInputStream(bis);