Check charset of fragmented subject

This commit is contained in:
M66B 2022-06-28 20:59:29 +02:00
parent 61f256f04d
commit d26eb776a9
2 changed files with 17 additions and 12 deletions

View File

@ -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);

View File

@ -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);