1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-25 01:06:04 +00:00

Limit compact_enc_det sample size

This commit is contained in:
M66B 2020-10-14 22:36:27 +02:00
parent 5caa95144c
commit d4c5de9210

View file

@ -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);