You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2019/07/10 08:54:50 UTC

[james-mime4j] 02/02: MIME4J-273 Fix encoded word splitting point for EncodeQ method as well.

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

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-mime4j.git

commit 6ec1135e813fc845875cfae3c1055ad534a418b9
Author: Bowen Dai <bw...@gmail.com>
AuthorDate: Fri Apr 27 10:12:32 2018 -0700

    MIME4J-273 Fix encoded word splitting point for EncodeQ method as well.
---
 .../java/org/apache/james/mime4j/codec/EncoderUtil.java  |  5 +++--
 .../org/apache/james/mime4j/codec/EncoderUtilTest.java   | 16 +++++++++++++++-
 .../java/org/apache/james/mime4j/field/FieldsTest.java   |  4 ++--
 3 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/core/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java b/core/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java
index f29a04f..e883646 100644
--- a/core/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java
+++ b/core/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java
@@ -566,12 +566,13 @@ public class EncoderUtil {
         if (totalLength <= ENCODED_WORD_MAX_LENGTH - usedCharacters) {
             return prefix + encodeQ(bytes, usage) + ENC_WORD_SUFFIX;
         } else {
-            String part1 = text.substring(0, text.length() / 2);
+            int splitOffset = text.offsetByCodePoints(text.length() / 2, -1);
+            String part1 = text.substring(0, splitOffset);
             byte[] bytes1 = encode(part1, charset);
             String word1 = encodeQ(prefix, part1, usage, usedCharacters,
                     charset, bytes1);
 
-            String part2 = text.substring(text.length() / 2);
+            String part2 = text.substring(splitOffset);
             byte[] bytes2 = encode(part2, charset);
             String word2 = encodeQ(prefix, part2, usage, 0, charset, bytes2);
 
diff --git a/core/src/test/java/org/apache/james/mime4j/codec/EncoderUtilTest.java b/core/src/test/java/org/apache/james/mime4j/codec/EncoderUtilTest.java
index ca90a75..deda6be 100644
--- a/core/src/test/java/org/apache/james/mime4j/codec/EncoderUtilTest.java
+++ b/core/src/test/java/org/apache/james/mime4j/codec/EncoderUtilTest.java
@@ -142,7 +142,7 @@ public class EncoderUtilTest {
     }
     
     @Test
-    public void testEncodeEncodedWordSplitForUnicode() throws Exception {
+    public void testEncodeBEncodedWordSplitForUnicode() throws Exception {
         StringBuilder sb = new StringBuilder("z");
         for (int i = 0; i < 10; i++) {
             // Append unicode character 𝕫 10 times.
@@ -156,6 +156,20 @@ public class EncoderUtilTest {
     }
 
     @Test
+    public void testEncodeQEncodedWordSplitForUnicode() throws Exception {
+        StringBuilder sb = new StringBuilder("z");
+        for (int i = 0; i < 6; i++) {
+            // Append unicode character 𝕫 6 times.
+            sb.append("\uD835\uDD6b");
+        }
+
+        String expected = "=?UTF-8?Q?z=F0=9D=95=AB=F0=9D=95=AB?= " +
+                "=?UTF-8?Q?=F0=9D=95=AB=F0=9D=95=AB=F0=9D=95=AB=F0=9D=95=AB?=";
+        Assert.assertEquals(expected, EncoderUtil.encodeEncodedWord(sb.toString(),
+                Usage.TEXT_TOKEN, 10, null, Encoding.Q));
+    }
+
+    @Test
     public void testEncodeEncodedWord() throws Exception {
         Assert.assertEquals("=?US-ASCII?Q??=", EncoderUtil.encodeEncodedWord("",
                 Usage.TEXT_TOKEN, 0, null, Encoding.Q));
diff --git a/dom/src/test/java/org/apache/james/mime4j/field/FieldsTest.java b/dom/src/test/java/org/apache/james/mime4j/field/FieldsTest.java
index 07254f3..60eac64 100644
--- a/dom/src/test/java/org/apache/james/mime4j/field/FieldsTest.java
+++ b/dom/src/test/java/org/apache/james/mime4j/field/FieldsTest.java
@@ -251,8 +251,8 @@ public class FieldsTest {
                 seventyEight)));
 
         String seventyNine = seventyEight + "9";
-        String expected = "Subject: =?US-ASCII?Q?1234567890123456789012345678901234?="
-                + "\r\n =?US-ASCII?Q?56789012345678901234567890123456789?=";
+        String expected = "Subject: =?US-ASCII?Q?123456789012345678901234567890123?="
+                + "\r\n =?US-ASCII?Q?456789012345678901234567890123456789?=";
         Assert.assertEquals(expected, decode(Fields.subject(seventyNine)));
     }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org