This is an automated email from the ASF dual-hosted git repository.

garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git

commit 57a79f56b6f50cbf0d32ab036190964351a4dcd6
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Nov 14 07:26:57 2026 -0500

    Clarify QuotedPrintableCodec strict mode and expand boundary tests
    
    Document trailing-whitespace escaping, soft line breaks, and the
    existing
    CR/LF encoding behavior without claiming full text-body RFC compliance.
    
    Add coverage for custom printable sets, whitespace pairs, null input,
    CRLF/NUL, and the three-byte boundary.
    
    See PR #455⁠.
---
 .../commons/codec/net/QuotedPrintableCodec.java    | 32 +++++----
 .../codec/net/QuotedPrintableCodecTest.java        | 79 +++++++++++++++-------
 2 files changed, 72 insertions(+), 39 deletions(-)

diff --git 
a/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java 
b/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
index f0eb2d7f..e3f36870 100644
--- a/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
+++ b/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
@@ -50,9 +50,13 @@ import org.apache.commons.codec.binary.StringUtils;
  * </p>
  * <ul>
  * <li>{@code strict=false}: only rules #1 and #2 are implemented</li>
- * <li>{@code strict=true}: all rules #1 through #5 are implemented</li>
+ * <li>{@code strict=true}: rules #1 and #2 plus trailing-whitespace escaping 
(rule #3) and soft line breaks (rule #5) are implemented</li>
  * </ul>
  * <p>
+ * Both modes encode CR and LF bytes as {@code =0D} and {@code =0A}, 
respectively, as required for binary data. They do not preserve canonical text
+ * line breaks as required by rule #4 for MIME text bodies. Strict mode does 
not impose a minimum input length; empty input produces empty output.
+ * </p>
+ * <p>
  * Originally, this class only supported the non-strict mode, but the codec in 
this partial form could already be used for certain applications that do not
  * require quoted-printable line formatting (rules #3, #4, #5), for instance Q 
codec. The strict mode has been added in 1.10.
  * Decoding is independent of this parameter; see {@link 
#decodeQuotedPrintable(byte[])} for its behavior.
@@ -187,13 +191,13 @@ public class QuotedPrintableCodec implements 
BinaryEncoder, BinaryDecoder, Strin
     /**
      * Encodes an array of bytes into an array of quoted-printable 7-bit 
characters. Unsafe characters are escaped.
      * <p>
-     * Depending on the selection of the {@code strict} parameter, this 
function either implements the full ruleset or only a subset of quoted-printable
-     * encoding specification (rule #1 and rule #2) as defined in RFC 1521 and 
is suitable for encoding binary data and unformatted text.
+     * Encodes binary data and unformatted text using rules #1 and #2 of RFC 
1521. Strict mode additionally escapes trailing whitespace and inserts
+     * soft line breaks. CR and LF bytes are escaped in both modes; canonical 
text line breaks are not preserved. See the class documentation.
      * </p>
      *
      * @param printable bitset of characters deemed quoted-printable.
      * @param bytes     array of bytes to be encoded.
-     * @param strict    if {@code true} the full ruleset is used, otherwise 
only rule #1 and rule #2.
+     * @param strict    if {@code true}, trailing whitespace is escaped and 
soft line breaks are inserted as needed.
      * @return array of bytes containing quoted-printable data.
      * @since 1.10
      */
@@ -329,7 +333,7 @@ public class QuotedPrintableCodec implements BinaryEncoder, 
BinaryDecoder, Strin
     /**
      * Constructs a new instance for the selection of the strict mode.
      *
-     * @param strict if {@code true}, soft line breaks will be used.
+     * @param strict if {@code true}, trailing whitespace is escaped and soft 
line breaks are inserted as needed.
      * @since 1.10
      */
     public QuotedPrintableCodec(final boolean strict) {
@@ -350,7 +354,7 @@ public class QuotedPrintableCodec implements BinaryEncoder, 
BinaryDecoder, Strin
      * Constructs a new instance for the selection of a default Charset and 
strict mode.
      *
      * @param charset The default string Charset to use.
-     * @param strict  if {@code true}, soft line breaks will be used.
+     * @param strict  if {@code true}, trailing whitespace is escaped and soft 
line breaks are inserted as needed.
      * @since 1.10
      */
     public QuotedPrintableCodec(final Charset charset, final boolean strict) {
@@ -459,8 +463,8 @@ public class QuotedPrintableCodec implements BinaryEncoder, 
BinaryDecoder, Strin
     /**
      * Encodes an array of bytes into an array of quoted-printable 7-bit 
characters. Unsafe characters are escaped.
      * <p>
-     * Depending on the selection of the {@code strict} parameter, this 
function either implements the full ruleset or only a subset of quoted-printable
-     * encoding specification (rule #1 and rule #2) as defined in RFC 1521 and 
is suitable for encoding binary data and unformatted text.
+     * Encodes binary data and unformatted text using rules #1 and #2 of RFC 
1521. Strict mode additionally escapes trailing whitespace and inserts
+     * soft line breaks. CR and LF bytes are escaped in both modes; canonical 
text line breaks are not preserved. See the class documentation.
      * </p>
      *
      * @param bytes array of bytes to be encoded.
@@ -495,8 +499,8 @@ public class QuotedPrintableCodec implements BinaryEncoder, 
BinaryDecoder, Strin
     /**
      * Encodes a string into its quoted-printable form using the default 
string Charset. Unsafe characters are escaped.
      * <p>
-     * Depending on the selection of the {@code strict} parameter, this 
function either implements the full ruleset or only a subset of quoted-printable
-     * encoding specification (rule #1 and rule #2) as defined in RFC 1521 and 
is suitable for encoding binary data and unformatted text.
+     * Encodes binary data and unformatted text using rules #1 and #2 of RFC 
1521. Strict mode additionally escapes trailing whitespace and inserts
+     * soft line breaks. CR and LF bytes are escaped in both modes; canonical 
text line breaks are not preserved. See the class documentation.
      * </p>
      *
      * @param sourceStr string to convert to quoted-printable form.
@@ -513,8 +517,8 @@ public class QuotedPrintableCodec implements BinaryEncoder, 
BinaryDecoder, Strin
     /**
      * Encodes a string into its quoted-printable form using the specified 
Charset. Unsafe characters are escaped.
      * <p>
-     * Depending on the selection of the {@code strict} parameter, this 
function either implements the full ruleset or only a subset of quoted-printable
-     * encoding specification (rule #1 and rule #2) as defined in RFC 1521 and 
is suitable for encoding binary data and unformatted text.
+     * Encodes binary data and unformatted text using rules #1 and #2 of RFC 
1521. Strict mode additionally escapes trailing whitespace and inserts
+     * soft line breaks. CR and LF bytes are escaped in both modes; canonical 
text line breaks are not preserved. See the class documentation.
      * </p>
      *
      * @param sourceStr     string to convert to quoted-printable form.
@@ -532,8 +536,8 @@ public class QuotedPrintableCodec implements BinaryEncoder, 
BinaryDecoder, Strin
     /**
      * Encodes a string into its quoted-printable form using the specified 
Charset. Unsafe characters are escaped.
      * <p>
-     * Depending on the selection of the {@code strict} parameter, this 
function either implements the full ruleset or only a subset of quoted-printable
-     * encoding specification (rule #1 and rule #2) as defined in RFC 1521 and 
is suitable for encoding binary data and unformatted text.
+     * Encodes binary data and unformatted text using rules #1 and #2 of RFC 
1521. Strict mode additionally escapes trailing whitespace and inserts
+     * soft line breaks. CR and LF bytes are escaped in both modes; canonical 
text line breaks are not preserved. See the class documentation.
      * </p>
      *
      * @param sourceStr     string to convert to quoted-printable form.
diff --git 
a/src/test/java/org/apache/commons/codec/net/QuotedPrintableCodecTest.java 
b/src/test/java/org/apache/commons/codec/net/QuotedPrintableCodecTest.java
index 599aa895..aed1a63c 100644
--- a/src/test/java/org/apache/commons/codec/net/QuotedPrintableCodecTest.java
+++ b/src/test/java/org/apache/commons/codec/net/QuotedPrintableCodecTest.java
@@ -25,6 +25,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.nio.charset.UnsupportedCharsetException;
+import java.util.BitSet;
 import java.util.stream.Stream;
 
 import org.apache.commons.codec.CharEncoding;
@@ -46,24 +47,18 @@ class QuotedPrintableCodecTest {
 
     static final int[] RUSSIAN_STUFF_UNICODE = { 0x412, 0x441, 0x435, 0x43C, 
0x5F, 0x43F, 0x440, 0x438, 0x432, 0x435, 0x442 };
 
-    private static Stream<Arguments> softLineBreakBoundaryCases() {
-        // @formatter:off
-        return Stream.of(
-                Arguments.of("=\r\n", ""),
-                Arguments.of("=\r\nA=\r\n", "A"),
-                Arguments.of("A=\r\n=\r\nB", "AB"),
-                Arguments.of("A=\r\n\r\nB", "A\r\nB"),
-                Arguments.of("A\r\n=\r\nB", "A\r\nB"),
-                Arguments.of("A=0D=0AB", "A\r\nB"));
-        // @formatter:on
-    }
-
-    private static Stream<Arguments> shortInputCases() {
+    private static Stream<Arguments> shortInputBoundaryCases() {
         // @formatter:off
         return Stream.of(
                 Arguments.of("", "", ""),
                 Arguments.of("a", "a", "a"),
                 Arguments.of("ab", "ab", "ab"),
+                Arguments.of("abc", "abc", "abc"),
+                Arguments.of("ab ", "ab=20", "ab "),
+                Arguments.of("  ", " =20", "  "),
+                Arguments.of("\t\t", "\t=09", "\t\t"),
+                Arguments.of("\r\n", "=0D=0A", "=0D=0A"),
+                Arguments.of("\0", "=00", "=00"),
                 Arguments.of(" ", "=20", " "),
                 Arguments.of("a ", "a=20", "a "),
                 Arguments.of(" a", " a", " a"),
@@ -73,6 +68,18 @@ class QuotedPrintableCodecTest {
         // @formatter:on
     }
 
+    private static Stream<Arguments> softLineBreakBoundaryCases() {
+        // @formatter:off
+        return Stream.of(
+                Arguments.of("=\r\n", ""),
+                Arguments.of("=\r\nA=\r\n", "A"),
+                Arguments.of("A=\r\n=\r\nB", "AB"),
+                Arguments.of("A=\r\n\r\nB", "A\r\nB"),
+                Arguments.of("A\r\n=\r\nB", "A\r\nB"),
+                Arguments.of("A=0D=0AB", "A\r\nB"));
+        // @formatter:on
+    }
+
     private String constructString(final int[] unicodeChars) {
         final StringBuilder buffer = new StringBuilder();
         if (unicodeChars != null) {
@@ -252,6 +259,34 @@ class QuotedPrintableCodecTest {
         assertEquals(plain, qpcodec.decode(encoded), "Safe chars 
quoted-printable decoding test");
     }
 
+    @ParameterizedTest
+    @MethodSource("shortInputBoundaryCases")
+    void testShortInputBoundaryStrictEncoding(final String plain, final String 
strictExpected, final String lenientExpected) throws Exception {
+        final QuotedPrintableCodec strictCodec = new 
QuotedPrintableCodec(true);
+        final QuotedPrintableCodec lenientCodec = new 
QuotedPrintableCodec(false);
+        final byte[] plainBytes = plain.getBytes(StandardCharsets.UTF_8);
+        final byte[] strictBytes = 
strictExpected.getBytes(StandardCharsets.US_ASCII);
+        assertEquals(strictExpected, strictCodec.encode(plain));
+        assertEquals(plain, strictCodec.decode(strictCodec.encode(plain)));
+        assertArrayEquals(strictBytes, strictCodec.encode(plainBytes));
+        assertArrayEquals(strictBytes, 
QuotedPrintableCodec.encodeQuotedPrintable(null, plainBytes, true));
+        assertEquals(lenientExpected, lenientCodec.encode(plain));
+    }
+
+    @Test
+    void testShortInputStrictEncodingWithCustomPrintable() {
+        final BitSet printable = new BitSet();
+        printable.set('b');
+        printable.set(' ');
+        printable.set('\t');
+        assertArrayEquals(StringUtils.getBytesUsAscii("=61b"),
+                QuotedPrintableCodec.encodeQuotedPrintable(printable, 
StringUtils.getBytesUsAscii("ab"), true));
+        assertArrayEquals(StringUtils.getBytesUsAscii("b=20"),
+                QuotedPrintableCodec.encodeQuotedPrintable(printable, 
StringUtils.getBytesUsAscii("b "), true));
+        assertArrayEquals(StringUtils.getBytesUsAscii("b=09"),
+                QuotedPrintableCodec.encodeQuotedPrintable(printable, 
StringUtils.getBytesUsAscii("b\t"), true));
+    }
+
     @ParameterizedTest
     @MethodSource("softLineBreakBoundaryCases")
     void testSoftLineBreakBoundaries(final String input, final String 
expected) throws Exception {
@@ -285,18 +320,12 @@ class QuotedPrintableCodecTest {
         assertEquals(qpdata, qpcodec.encode(decoded));
     }
 
-    @ParameterizedTest
-    @MethodSource("shortInputCases")
-    void testShortInputStrictEncoding(final String plain, final String 
strictExpected, final String lenientExpected) throws Exception {
-        final QuotedPrintableCodec strictCodec = new 
QuotedPrintableCodec(true);
-        final QuotedPrintableCodec lenientCodec = new 
QuotedPrintableCodec(false);
-        final byte[] plainBytes = plain.getBytes(StandardCharsets.UTF_8);
-        final byte[] strictBytes = 
strictExpected.getBytes(StandardCharsets.US_ASCII);
-        assertEquals(strictExpected, strictCodec.encode(plain));
-        assertEquals(plain, strictCodec.decode(strictCodec.encode(plain)));
-        assertArrayEquals(strictBytes, strictCodec.encode(plainBytes));
-        assertArrayEquals(strictBytes, 
QuotedPrintableCodec.encodeQuotedPrintable(null, plainBytes, true));
-        assertEquals(lenientExpected, lenientCodec.encode(plain));
+    @Test
+    void testStrictEncodeNull() throws Exception {
+        final QuotedPrintableCodec codec = new QuotedPrintableCodec(true);
+        assertNull(codec.encode((String) null));
+        assertNull(codec.encode((byte[]) null));
+        assertNull(QuotedPrintableCodec.encodeQuotedPrintable(null, null, 
true));
     }
 
     @Test

Reply via email to