From f3fa8cb0651e6c2f61aeaef2a6157fb7fa77f58c Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 6 Apr 2020 09:46:42 +0200 Subject: [PATCH] Fixing header encoding in some more cases --- .../main/java/eu/faircode/email/Helper.java | 13 +++++++++ .../java/eu/faircode/email/MessageHelper.java | 27 +++++++++++++------ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/Helper.java b/app/src/main/java/eu/faircode/email/Helper.java index dffdcd50a4..7401d8b09f 100644 --- a/app/src/main/java/eu/faircode/email/Helper.java +++ b/app/src/main/java/eu/faircode/email/Helper.java @@ -856,6 +856,19 @@ public class Helper { return true; } + static boolean isISO8859(String text) { + // https://en.wikipedia.org/wiki/ISO/IEC_8859-1 + byte[] octets = text.getBytes(StandardCharsets.ISO_8859_1); + for (byte b : octets) { + int c = b & 0xFF; + if (c < 32) + return false; + if (c >= 127 && c < 160) + return false; + } + return true; + } + // Files static String sanitizeFilename(String name) { diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index e07ea1be85..dbb9ccaf52 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -995,15 +995,29 @@ public class MessageHelper { return null; } + private String fixEncoding(String name, String header) { + if (header.trim().startsWith("=?")) + return header; + + if (Helper.isUTF8(header)) { + if (!Helper.isISO8859(header)) { + Log.w("Converting " + name + " to UTF-8"); + return new String(header.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); + } + } else { + Log.w("Converting " + name + " to ISO8859-1"); + return new String(header.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.ISO_8859_1); + } + + return header; + } + private Address[] getAddressHeader(String name) throws MessagingException { String header = imessage.getHeader(name, ","); if (header == null) return null; - 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); - } + header = fixEncoding(name, header); Address[] addresses = InternetAddress.parseHeader(header, false); for (Address address : addresses) { @@ -1144,10 +1158,7 @@ public class MessageHelper { if (subject == null) return null; - 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 = fixEncoding("subject", subject); subject = subject.replaceAll("\\?=\\r?\\n\\s+=\\?", "\\?==\\?"); subject = MimeUtility.unfold(subject); subject = decodeMime(subject);