mirror of
https://github.com/M66B/FairEmail.git
synced 2026-01-03 11:28:41 +01:00
Conditionally convert address header/subject charset
This commit is contained in:
@@ -741,6 +741,38 @@ public class Helper {
|
||||
return false;
|
||||
}
|
||||
|
||||
static boolean isUTF8(String text) {
|
||||
// Get extended ASCII characters
|
||||
byte[] octets = text.getBytes(StandardCharsets.ISO_8859_1);
|
||||
|
||||
for (int i = 0; i < octets.length; i++) {
|
||||
int bytes;
|
||||
if ((octets[i] & 0b10000000) == 0b00000000)
|
||||
bytes = 1;
|
||||
else if ((octets[i] & 0b11100000) == 0b11000000)
|
||||
bytes = 2;
|
||||
else if ((octets[i] & 0b11110000) == 0b11100000)
|
||||
bytes = 3;
|
||||
else if ((octets[i] & 0b11111000) == 0b11110000)
|
||||
bytes = 4;
|
||||
else if ((octets[i] & 0b11111100) == 0b11111000)
|
||||
bytes = 5;
|
||||
else if ((octets[i] & 0b11111110) == 0b11111100)
|
||||
bytes = 6;
|
||||
else
|
||||
return false;
|
||||
|
||||
if (i + bytes > octets.length)
|
||||
return false;
|
||||
|
||||
while (--bytes > 0)
|
||||
if ((octets[++i] & 0b11000000) != 0b10000000)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Files
|
||||
|
||||
static String sanitizeFilename(String name) {
|
||||
|
||||
@@ -854,7 +854,10 @@ public class MessageHelper {
|
||||
if (header == null)
|
||||
return null;
|
||||
|
||||
header = new String(header.getBytes(StandardCharsets.ISO_8859_1));
|
||||
if (!Helper.isUTF8(header)) {
|
||||
Log.w("Converting header '" + name + "' to ISO8859-1");
|
||||
header = new String(header.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.ISO_8859_1);
|
||||
}
|
||||
Address[] addresses = InternetAddress.parseHeader(header, false);
|
||||
|
||||
for (Address address : addresses) {
|
||||
@@ -995,7 +998,10 @@ public class MessageHelper {
|
||||
if (subject == null)
|
||||
return null;
|
||||
|
||||
subject = new String(subject.getBytes(StandardCharsets.ISO_8859_1));
|
||||
if (!Helper.isUTF8(subject)) {
|
||||
Log.w("Converting subject to ISO8859-1");
|
||||
subject = new String(subject.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.ISO_8859_1);
|
||||
}
|
||||
subject = subject.replaceAll("\\?=\\r?\\n\\s+=\\?", "\\?==\\?");
|
||||
subject = MimeUtility.unfold(subject);
|
||||
subject = decodeMime(subject);
|
||||
|
||||
Reference in New Issue
Block a user