fix(android): transcode mpv strings as standard UTF-8 across JNI

Emoji and other supplementary-plane characters in titles, file names, and
subtitle paths were corrupted on the Android MPV path, and malformed bytes
from mpv logs could abort under CheckJNI. NewStringUTF/GetStringUTFChars
speak JNI's modified UTF-8, not the standard UTF-8 mpv produces and
consumes; the native lead-byte filter and the Kotlin surrogate scrubber
ran on the wrong side of that conversion and could not recover it.

Both directions now go through UTF-16 (NewString/GetStringChars) with a
small JNI-free transcoder that replaces malformed input with U+FFFD, the
same policy as shared/cpp/sanitize_utf8.h on desktop. The header is
covered by the Android host native test harness.
This commit is contained in:
edde746
2026-09-03 11:36:29 +02:00
parent 54efd4fe73
commit c70f88cee0
9 changed files with 240 additions and 55 deletions
+18
View File
@@ -5,6 +5,24 @@
#include <cstdlib>
#include "utf8_convert.h"
jstring new_java_string(JNIEnv* env, const char* utf8) {
if (!utf8) return NULL;
const std::u16string u16 = plezy::utf8::ToUtf16(utf8);
return env->NewString(reinterpret_cast<const jchar*>(u16.data()), static_cast<jsize>(u16.size()));
}
std::string java_string_to_utf8(JNIEnv* env, jstring jstr) {
if (!jstr) return std::string();
const jsize len = env->GetStringLength(jstr);
const jchar* chars = env->GetStringChars(jstr, NULL);
if (!chars) return std::string();
std::string out = plezy::utf8::FromUtf16(reinterpret_cast<const char16_t*>(chars), static_cast<size_t>(len));
env->ReleaseStringChars(jstr, chars);
return out;
}
bool acquire_jni_env(JavaVM* vm, JNIEnv** env) {
int ret = vm->GetEnv((void**)env, JNI_VERSION_1_6);
if (ret == JNI_EDETACHED)