From d4c5de92109ff740bd1e2ac21f95a543f2f78ec6 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 14 Oct 2020 22:36:27 +0200 Subject: [PATCH] Limit compact_enc_det sample size --- .../java/eu/faircode/email/CharsetHelper.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/CharsetHelper.java b/app/src/main/java/eu/faircode/email/CharsetHelper.java index 873c849dc4..938a346161 100644 --- a/app/src/main/java/eu/faircode/email/CharsetHelper.java +++ b/app/src/main/java/eu/faircode/email/CharsetHelper.java @@ -24,6 +24,8 @@ import java.nio.charset.StandardCharsets; import java.nio.charset.UnsupportedCharsetException; class CharsetHelper { + private static final int MAX_DETECT_SIZE = 8192; + static { System.loadLibrary("compact_enc_det"); } @@ -125,8 +127,19 @@ class CharsetHelper { static Charset detect(String text) { try { byte[] octets = text.getBytes(StandardCharsets.ISO_8859_1); - String detected = jni_detect(octets); - Log.i("compact_enc_det=" + detected); + + byte[] sample; + if (octets.length < MAX_DETECT_SIZE) + sample = octets; + else { + sample = new byte[MAX_DETECT_SIZE]; + System.arraycopy(octets, 0, sample, 0, MAX_DETECT_SIZE); + } + + Log.i("compact_enc_det sample=" + sample.length); + String detected = jni_detect(sample); + Log.i("compact_enc_det result=" + detected); + return Charset.forName(detected); } catch (Throwable ex) { Log.w(ex);