Conditionally upgrade HTML to UTF8

pull/199/head
M66B 3 years ago
parent 62a501298b
commit 697bda090b

@ -1876,14 +1876,14 @@ public class MessageHelper {
if ((TextUtils.isEmpty(charset) || charset.equalsIgnoreCase(StandardCharsets.US_ASCII.name()))) if ((TextUtils.isEmpty(charset) || charset.equalsIgnoreCase(StandardCharsets.US_ASCII.name())))
charset = null; charset = null;
if (h.isPlainText()) { Charset cs = null;
Charset cs = null; try {
try { if (charset != null)
if (charset != null) cs = Charset.forName(charset);
cs = Charset.forName(charset); } catch (UnsupportedCharsetException ignored) {
} catch (UnsupportedCharsetException ignored) { }
}
if (h.isPlainText()) {
if (charset == null || StandardCharsets.ISO_8859_1.equals(cs)) { if (charset == null || StandardCharsets.ISO_8859_1.equals(cs)) {
Charset detected = CharsetHelper.detect(result); Charset detected = CharsetHelper.detect(result);
if (StandardCharsets.ISO_8859_1.equals(cs) && if (StandardCharsets.ISO_8859_1.equals(cs) &&
@ -1907,26 +1907,27 @@ public class MessageHelper {
result = HtmlHelper.flow(result); result = HtmlHelper.flow(result);
result = "<div x-plain=\"true\">" + HtmlHelper.formatPre(result) + "</div>"; result = "<div x-plain=\"true\">" + HtmlHelper.formatPre(result) + "</div>";
} else if (h.isHtml()) { } else if (h.isHtml()) {
if (charset == null) { // Conditionally upgrade to UTF8
if (CharsetHelper.isUTF8(result)) if ((cs == null ||
result = new String(result.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); StandardCharsets.US_ASCII.equals(cs) ||
} else { StandardCharsets.ISO_8859_1.equals(cs)) &&
// Fix incorrect UTF16 CharsetHelper.isUTF8(result))
try { result = new String(result.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
Charset c = Charset.forName(charset);
if (CHARSET16.contains(c)) { // Fix incorrect UTF16
Charset detected = CharsetHelper.detect(result); try {
if (!CHARSET16.contains(detected)) if (CHARSET16.contains(cs)) {
Log.e(new Throwable("Charset=" + c + " detected=" + detected)); Charset detected = CharsetHelper.detect(result);
if (StandardCharsets.US_ASCII.equals(detected) || if (!CHARSET16.contains(detected))
StandardCharsets.UTF_8.equals(detected)) { Log.e(new Throwable("Charset=" + cs + " detected=" + detected));
charset = null; if (StandardCharsets.US_ASCII.equals(detected) ||
result = new String(result.getBytes(c), detected); StandardCharsets.UTF_8.equals(detected)) {
} charset = null;
result = new String(result.getBytes(cs), detected);
} }
} catch (Throwable ex) {
Log.w(ex);
} }
} catch (Throwable ex) {
Log.w(ex);
} }
if (charset == null) { if (charset == null) {

Loading…
Cancel
Save