|
|
|
@ -24,11 +24,21 @@ void log_android(int prio, const char *fmt, ...) {
|
|
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
|
JNIEXPORT jobject JNICALL
|
|
|
|
|
|
|
|
|
|
Java_eu_faircode_email_CharsetHelper_jni_1detect_1charset(
|
|
|
|
|
JNIEnv *env, jclass type,
|
|
|
|
|
jbyteArray _octets) {
|
|
|
|
|
jbyteArray _octets, jstring _ref, jstring _lang) {
|
|
|
|
|
int len = env->GetArrayLength(_octets);
|
|
|
|
|
jbyte *octets = env->GetByteArrayElements(_octets, nullptr);
|
|
|
|
|
const char *ref = env->GetStringUTFChars(_ref, 0);
|
|
|
|
|
const char *lang = env->GetStringUTFChars(_lang, 0);
|
|
|
|
|
|
|
|
|
|
// ISO-8859-1 is unknown
|
|
|
|
|
Encoding encoding_hint;
|
|
|
|
|
EncodingFromName(ref, &encoding_hint);
|
|
|
|
|
|
|
|
|
|
Language language_hint;
|
|
|
|
|
LanguageFromCode(lang, &language_hint);
|
|
|
|
|
|
|
|
|
|
// https://github.com/google/compact_enc_det
|
|
|
|
|
|
|
|
|
@ -38,19 +48,25 @@ Java_eu_faircode_email_CharsetHelper_jni_1detect_1charset(
|
|
|
|
|
Encoding encoding = CompactEncDet::DetectEncoding(
|
|
|
|
|
(const char *) octets, len,
|
|
|
|
|
nullptr, nullptr, nullptr,
|
|
|
|
|
UNKNOWN_ENCODING,
|
|
|
|
|
UNKNOWN_LANGUAGE,
|
|
|
|
|
encoding_hint,
|
|
|
|
|
language_hint,
|
|
|
|
|
CompactEncDet::EMAIL_CORPUS,
|
|
|
|
|
false,
|
|
|
|
|
&bytes_consumed,
|
|
|
|
|
&is_reliable);
|
|
|
|
|
// TODO: PreferredWebOutputEncoding?
|
|
|
|
|
const char *name = MimeEncodingName(encoding);
|
|
|
|
|
|
|
|
|
|
log_android(ANDROID_LOG_DEBUG, "detect=%d/%s bytes=%d reliable=%d",
|
|
|
|
|
encoding, name, bytes_consumed, is_reliable);
|
|
|
|
|
log_android(ANDROID_LOG_DEBUG,
|
|
|
|
|
"MMM detect=%d/%s bytes=%d reliable=%d"
|
|
|
|
|
" ref=%s/%s lang=%s/%s",
|
|
|
|
|
encoding, name, bytes_consumed, is_reliable,
|
|
|
|
|
EncodingName(encoding_hint), ref, LanguageCode(language_hint), lang);
|
|
|
|
|
|
|
|
|
|
// https://developer.android.com/training/articles/perf-jni#primitive-arrays
|
|
|
|
|
env->ReleaseByteArrayElements(_octets, octets, JNI_ABORT);
|
|
|
|
|
env->ReleaseStringUTFChars(_ref, ref);
|
|
|
|
|
env->ReleaseStringUTFChars(_lang, lang);
|
|
|
|
|
|
|
|
|
|
jclass cls = env->FindClass("eu/faircode/email/CharsetHelper$DetectResult");
|
|
|
|
|
jmethodID ctor = env->GetMethodID(cls, "<init>", "(Ljava/lang/String;IIZ)V");
|
|
|
|
|