Return-Path: X-Original-To: apmail-james-mime4j-dev-archive@minotaur.apache.org Delivered-To: apmail-james-mime4j-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 51C20105C6 for ; Wed, 29 Jan 2014 14:20:13 +0000 (UTC) Received: (qmail 76585 invoked by uid 500); 29 Jan 2014 14:20:12 -0000 Delivered-To: apmail-james-mime4j-dev-archive@james.apache.org Received: (qmail 76550 invoked by uid 500); 29 Jan 2014 14:20:12 -0000 Mailing-List: contact mime4j-dev-help@james.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: mime4j-dev@james.apache.org Delivered-To: mailing list mime4j-dev@james.apache.org Received: (qmail 76540 invoked by uid 99); 29 Jan 2014 14:20:12 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 29 Jan 2014 14:20:12 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 29 Jan 2014 14:20:10 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id B43F82388993; Wed, 29 Jan 2014 14:19:50 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1562451 - in /james/mime4j/branches/apache-mime4j-0.7/core/src: main/java/org/apache/james/mime4j/codec/QuotedPrintableInputStream.java test/java/org/apache/james/mime4j/codec/QuotedPrintableInputStreamTest.java Date: Wed, 29 Jan 2014 14:19:50 -0000 To: mime4j-dev@james.apache.org From: olegk@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20140129141950.B43F82388993@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 = "\r\n" + + "\r\n"; + String expected = "\r\n" + + "\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);