Added/improved charset mapping

pull/172/head
M66B 5 years ago
parent 9b2e4eb2ec
commit 6e7e890043

@ -1251,28 +1251,27 @@ public class MessageHelper {
try {
ContentType ct = new ContentType(part.getContentType());
String charset = ct.getParameter("charset");
// Fix common mistakes
if (charset != null) {
String charset = ct.getParameter("charset");
if (charset == null)
charset = StandardCharsets.ISO_8859_1.name();
else {
charset = charset.replace("\"", "");
if ("ASCII".equals(charset.toUpperCase()))
charset = "US-ASCII";
else if (charset.toLowerCase().endsWith("8859-16")) // not supported by Android
charset = null; // Use ISO8859-1 instead
}
if (TextUtils.isEmpty(charset) || "US-ASCII".equals(charset.toUpperCase())) {
// The first 127 characters are the same as in US-ASCII
result = new String(result.getBytes(StandardCharsets.ISO_8859_1));
} else {
// See UnknownCharsetProvider class
if ("US-ASCII".equals(Charset.forName(charset).name())) {
Log.w("Unsupported encoding charset=" + charset);
charset = MimeUtility.javaCharset(charset);
if (!Charset.isSupported(charset)) {
// x-binaryenc
// UseInqueCodePage
// none
// unknown-8bit
// X-UNKNOWN
Log.e("Unsupported encoding charset=" + charset);
warnings.add(context.getString(R.string.title_no_charset, charset));
result = new String(result.getBytes(StandardCharsets.ISO_8859_1));
charset = StandardCharsets.ISO_8859_1.name();
}
}
result = new String(result.getBytes(Charset.forName(charset)));
} catch (ParseException ex) {
Log.w(ex);
warnings.add(Log.formatThrowable(ex, false));

@ -1,48 +0,0 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2020 by Marcel Bokhorst (M66B)
*/
import android.text.TextUtils;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.charset.spi.CharsetProvider;
import java.util.Collections;
import java.util.Iterator;
public class UnknownCharsetProvider extends CharsetProvider {
@Override
public Iterator<Charset> charsets() {
return Collections.emptyIterator();
}
@Override
public Charset charsetForName(String name) {
// x-binaryenc
// UseInqueCodePage
// none
// unknown-8bit
// X-UNKNOWN
// https://javaee.github.io/javamail/FAQ#unsupen
if (!TextUtils.isEmpty(name))
Log.e("Unknown charset=" + name);
return StandardCharsets.US_ASCII;
}
}

@ -1,6 +1,17 @@
### JDK-to-MIME charset mapping table ####
### This should be the first mapping table ###
### FairEmail
8859_16 ISO-8859-1
iso8859_16 ISO-8859-1
ISO8859-16 ISO-8859-1
ascii ISO-8859-1
ASCII ISO-8859-1
### Inherited
8859_1 ISO-8859-1
iso8859_1 ISO-8859-1
ISO8859-1 ISO-8859-1

Loading…
Cancel
Save