|
|
|
@ -21,7 +21,6 @@ package eu.faircode.email;
|
|
|
|
|
|
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.nio.charset.UnsupportedCharsetException;
|
|
|
|
|
|
|
|
|
|
class CharsetHelper {
|
|
|
|
|
private static final int MAX_SAMPLE_SIZE = 8192;
|
|
|
|
@ -64,66 +63,6 @@ class CharsetHelper {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static boolean isISO8859(String text) {
|
|
|
|
|
// https://en.wikipedia.org/wiki/ISO/IEC_8859-1
|
|
|
|
|
int c;
|
|
|
|
|
byte[] octets = text.getBytes(StandardCharsets.ISO_8859_1);
|
|
|
|
|
for (byte b : octets) {
|
|
|
|
|
c = b & 0xFF;
|
|
|
|
|
if (c < 32)
|
|
|
|
|
return false;
|
|
|
|
|
if (c >= 127 && c < 160)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static boolean isISO2022JP(String text) {
|
|
|
|
|
// https://en.wikipedia.org/wiki/ISO/IEC_2022
|
|
|
|
|
// https://www.sljfaq.org/afaq/encodings.html#encodings-ISO-2022-JP
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Charset.forName("ISO-2022-JP");
|
|
|
|
|
} catch (UnsupportedCharsetException ex) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int c;
|
|
|
|
|
int escapes = 0;
|
|
|
|
|
boolean escaped = false;
|
|
|
|
|
boolean parenthesis = false;
|
|
|
|
|
boolean dollar = false;
|
|
|
|
|
byte[] octets = text.getBytes(StandardCharsets.ISO_8859_1);
|
|
|
|
|
for (byte b : octets) {
|
|
|
|
|
c = b & 0xFF;
|
|
|
|
|
|
|
|
|
|
if (c > 0x7F)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (escaped) {
|
|
|
|
|
escaped = false;
|
|
|
|
|
if (c == '(')
|
|
|
|
|
parenthesis = true;
|
|
|
|
|
else if (c == '$')
|
|
|
|
|
dollar = true;
|
|
|
|
|
} else if (parenthesis) {
|
|
|
|
|
parenthesis = false;
|
|
|
|
|
if (c == 'B' || c == 'J')
|
|
|
|
|
escapes++;
|
|
|
|
|
} else if (dollar) {
|
|
|
|
|
dollar = false;
|
|
|
|
|
if (c == '@' || c == 'B')
|
|
|
|
|
escapes++;
|
|
|
|
|
} else if (c == 0x1B)
|
|
|
|
|
escaped = true;
|
|
|
|
|
|
|
|
|
|
if (escapes >= 3)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Charset detect(String text) {
|
|
|
|
|
try {
|
|
|
|
|
byte[] octets = text.getBytes(StandardCharsets.ISO_8859_1);
|
|
|
|
|