From d26eb776a9212431e70233f62ba36b611c2af2c9 Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 28 Jun 2022 20:59:29 +0200 Subject: [PATCH] Check charset of fragmented subject --- .../java/eu/faircode/email/CharsetHelper.java | 24 +++++++++---------- .../java/eu/faircode/email/MessageHelper.java | 5 ++++ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/CharsetHelper.java b/app/src/main/java/eu/faircode/email/CharsetHelper.java index 145639d8d3..14a4a8f400 100644 --- a/app/src/main/java/eu/faircode/email/CharsetHelper.java +++ b/app/src/main/java/eu/faircode/email/CharsetHelper.java @@ -30,6 +30,7 @@ import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.nio.charset.CodingErrorAction; import java.nio.charset.StandardCharsets; +import java.nio.charset.UnsupportedCharsetException; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -76,24 +77,23 @@ public class CharsetHelper { } static boolean isUTF8(byte[] octets) { - CharsetDecoder utf8Decoder = StandardCharsets.UTF_8.newDecoder() - .onMalformedInput(CodingErrorAction.REPORT) - .onUnmappableCharacter(CodingErrorAction.REPORT); - try { - utf8Decoder.decode(ByteBuffer.wrap(octets)); - return true; - } catch (CharacterCodingException ex) { - Log.w(ex); - return false; - } + return isValid(octets, StandardCharsets.UTF_8); } static boolean isUTF16(byte[] octets) { - CharsetDecoder utf8Decoder = StandardCharsets.UTF_16.newDecoder() + return isValid(octets, StandardCharsets.UTF_16); + } + + static Boolean isValid(byte[] octets, String charset) { + return isValid(octets, Charset.forName(charset)); + } + + static boolean isValid(byte[] octets, Charset charset) { + CharsetDecoder decoder = charset.newDecoder() .onMalformedInput(CodingErrorAction.REPORT) .onUnmappableCharacter(CodingErrorAction.REPORT); try { - utf8Decoder.decode(ByteBuffer.wrap(octets)); + decoder.decode(ByteBuffer.wrap(octets)); return true; } catch (CharacterCodingException ex) { Log.w(ex); diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index db7b725320..1398f826d2 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -2880,6 +2880,11 @@ public class MessageHelper { try { byte[] b1 = decodeWord(p1.text, p1.encoding, p1.charset); byte[] b2 = decodeWord(p2.text, p2.encoding, p2.charset); + if (CharsetHelper.isValid(b1, p1.charset) && CharsetHelper.isValid(b2, p2.charset)) { + p++; + continue; + } + byte[] b = new byte[b1.length + b2.length]; System.arraycopy(b1, 0, b, 0, b1.length); System.arraycopy(b2, 0, b, b1.length, b2.length);