2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/tl/tl_jni_object.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2020-11-22 22:18:01 +01:00
|
|
|
#include "td/utils/ExitGuard.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
2018-01-22 10:51:04 +01:00
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-01-22 10:51:04 +01:00
|
|
|
|
|
|
|
#include <memory>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
namespace jni {
|
|
|
|
|
|
|
|
thread_local bool parse_error;
|
|
|
|
|
|
|
|
static jclass BooleanClass;
|
|
|
|
static jclass IntegerClass;
|
|
|
|
static jclass LongClass;
|
|
|
|
static jclass DoubleClass;
|
|
|
|
static jclass StringClass;
|
|
|
|
static jclass ObjectClass;
|
|
|
|
jclass ArrayKeyboardButtonClass;
|
|
|
|
jclass ArrayInlineKeyboardButtonClass;
|
2019-02-07 00:58:27 +01:00
|
|
|
jclass ArrayPageBlockTableCellClass;
|
2018-12-31 20:04:05 +01:00
|
|
|
jmethodID GetConstructorID;
|
|
|
|
jmethodID BooleanGetValueMethodID;
|
|
|
|
jmethodID IntegerGetValueMethodID;
|
|
|
|
jmethodID LongGetValueMethodID;
|
|
|
|
jmethodID DoubleGetValueMethodID;
|
|
|
|
|
2018-01-22 10:51:04 +01:00
|
|
|
static void fatal_error(JNIEnv *env, CSlice error) {
|
|
|
|
LOG(ERROR) << error;
|
|
|
|
env->FatalError(error.c_str());
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
jclass get_jclass(JNIEnv *env, const char *class_name) {
|
|
|
|
jclass clazz = env->FindClass(class_name);
|
|
|
|
if (!clazz) {
|
2018-01-22 10:51:04 +01:00
|
|
|
fatal_error(env, PSLICE() << "Can't find class [" << class_name << "]");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
jclass clazz_global = (jclass)env->NewGlobalRef(clazz);
|
|
|
|
|
|
|
|
env->DeleteLocalRef(clazz);
|
|
|
|
|
|
|
|
if (!clazz_global) {
|
2018-01-22 10:51:04 +01:00
|
|
|
fatal_error(env, PSLICE() << "Can't create global reference to [" << class_name << "]");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return clazz_global;
|
|
|
|
}
|
|
|
|
|
2018-01-22 10:51:04 +01:00
|
|
|
jmethodID get_method_id(JNIEnv *env, jclass clazz, const char *name, const char *signature) {
|
|
|
|
jmethodID res = env->GetMethodID(clazz, name, signature);
|
|
|
|
if (!res) {
|
|
|
|
fatal_error(env, PSLICE() << "Can't find method [" << name << "] with signature [" << signature << "]");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-22 10:51:04 +01:00
|
|
|
return res;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-01-22 10:51:04 +01:00
|
|
|
jfieldID get_field_id(JNIEnv *env, jclass clazz, const char *name, const char *signature) {
|
|
|
|
jfieldID res = env->GetFieldID(clazz, name, signature);
|
|
|
|
if (!res) {
|
|
|
|
fatal_error(env, PSLICE() << "Can't find field [" << name << "] with signature [" << signature << "]");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-22 10:51:04 +01:00
|
|
|
return res;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-01-22 10:51:04 +01:00
|
|
|
void register_native_method(JNIEnv *env, jclass clazz, std::string name, std::string signature, void *function_ptr) {
|
|
|
|
JNINativeMethod native_method{&name[0], &signature[0], function_ptr};
|
|
|
|
if (env->RegisterNatives(clazz, &native_method, 1) != 0) {
|
|
|
|
fatal_error(env, PSLICE() << "RegisterNatives failed for " << name << " with signature " << signature);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-01-28 11:20:43 +01:00
|
|
|
std::unique_ptr<JNIEnv, JvmThreadDetacher> get_jni_env(JavaVM *java_vm, jint jni_version) {
|
|
|
|
JNIEnv *env = nullptr;
|
2020-11-22 22:18:01 +01:00
|
|
|
if (!ExitGuard::is_exited() && java_vm->GetEnv(reinterpret_cast<void **>(&env), jni_version) == JNI_EDETACHED) {
|
2018-02-01 23:44:51 +01:00
|
|
|
#ifdef JDK1_2 // if not Android JNI
|
2018-01-28 11:20:43 +01:00
|
|
|
auto p_env = reinterpret_cast<void **>(&env);
|
|
|
|
#else
|
|
|
|
auto p_env = &env;
|
|
|
|
#endif
|
2020-10-11 13:53:12 +02:00
|
|
|
if (java_vm->AttachCurrentThread(p_env, nullptr) != JNI_OK) {
|
|
|
|
java_vm = nullptr;
|
|
|
|
env = nullptr;
|
|
|
|
}
|
2018-01-28 11:20:43 +01:00
|
|
|
} else {
|
|
|
|
java_vm = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::unique_ptr<JNIEnv, JvmThreadDetacher>(env, JvmThreadDetacher(java_vm));
|
|
|
|
}
|
|
|
|
|
2018-01-22 10:51:04 +01:00
|
|
|
void init_vars(JNIEnv *env, const char *td_api_java_package) {
|
2018-12-31 20:04:05 +01:00
|
|
|
BooleanClass = get_jclass(env, "java/lang/Boolean");
|
|
|
|
IntegerClass = get_jclass(env, "java/lang/Integer");
|
|
|
|
LongClass = get_jclass(env, "java/lang/Long");
|
|
|
|
DoubleClass = get_jclass(env, "java/lang/Double");
|
|
|
|
StringClass = get_jclass(env, "java/lang/String");
|
|
|
|
ObjectClass = get_jclass(env, (PSLICE() << td_api_java_package << "/TdApi$Object").c_str());
|
|
|
|
ArrayKeyboardButtonClass =
|
|
|
|
get_jclass(env, (PSLICE() << "[L" << td_api_java_package << "/TdApi$KeyboardButton;").c_str());
|
|
|
|
ArrayInlineKeyboardButtonClass =
|
|
|
|
get_jclass(env, (PSLICE() << "[L" << td_api_java_package << "/TdApi$InlineKeyboardButton;").c_str());
|
2019-02-07 00:58:27 +01:00
|
|
|
ArrayPageBlockTableCellClass =
|
|
|
|
get_jclass(env, (PSLICE() << "[L" << td_api_java_package << "/TdApi$PageBlockTableCell;").c_str());
|
2018-12-31 20:04:05 +01:00
|
|
|
GetConstructorID = get_method_id(env, ObjectClass, "getConstructor", "()I");
|
|
|
|
BooleanGetValueMethodID = get_method_id(env, BooleanClass, "booleanValue", "()Z");
|
|
|
|
IntegerGetValueMethodID = get_method_id(env, IntegerClass, "intValue", "()I");
|
|
|
|
LongGetValueMethodID = get_method_id(env, LongClass, "longValue", "()J");
|
|
|
|
DoubleGetValueMethodID = get_method_id(env, DoubleClass, "doubleValue", "()D");
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t get_utf8_from_utf16_length(const jchar *p, jsize len) {
|
|
|
|
size_t result = 0;
|
|
|
|
for (jsize i = 0; i < len; i++) {
|
2021-10-17 21:56:40 +02:00
|
|
|
uint32 cur = p[i];
|
2018-12-31 20:04:05 +01:00
|
|
|
if ((cur & 0xF800) == 0xD800) {
|
|
|
|
if (i < len) {
|
2021-10-17 21:56:40 +02:00
|
|
|
uint32 next = p[++i];
|
2018-12-31 20:04:05 +01:00
|
|
|
if ((next & 0xFC00) == 0xDC00 && (cur & 0x400) == 0) {
|
|
|
|
result += 4;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-28 07:18:17 +01:00
|
|
|
// TODO wrong UTF-16, it is possible
|
2018-12-31 20:04:05 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
result += 1 + (cur >= 0x80) + (cur >= 0x800);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void utf16_to_utf8(const jchar *p, jsize len, char *res) {
|
|
|
|
for (jsize i = 0; i < len; i++) {
|
2021-10-17 21:56:40 +02:00
|
|
|
uint32 cur = p[i];
|
|
|
|
// TODO conversion uint32 -> signed char is implementation defined
|
2018-12-31 20:04:05 +01:00
|
|
|
if (cur <= 0x7f) {
|
|
|
|
*res++ = static_cast<char>(cur);
|
|
|
|
} else if (cur <= 0x7ff) {
|
|
|
|
*res++ = static_cast<char>(0xc0 | (cur >> 6));
|
|
|
|
*res++ = static_cast<char>(0x80 | (cur & 0x3f));
|
|
|
|
} else if ((cur & 0xF800) != 0xD800) {
|
|
|
|
*res++ = static_cast<char>(0xe0 | (cur >> 12));
|
|
|
|
*res++ = static_cast<char>(0x80 | ((cur >> 6) & 0x3f));
|
|
|
|
*res++ = static_cast<char>(0x80 | (cur & 0x3f));
|
|
|
|
} else {
|
2018-02-17 00:54:44 +01:00
|
|
|
// correctness is already checked
|
2021-10-17 21:56:40 +02:00
|
|
|
uint32 next = p[++i];
|
|
|
|
uint32 val = ((cur - 0xD800) << 10) + next - 0xDC00 + 0x10000;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
*res++ = static_cast<char>(0xf0 | (val >> 18));
|
|
|
|
*res++ = static_cast<char>(0x80 | ((val >> 12) & 0x3f));
|
|
|
|
*res++ = static_cast<char>(0x80 | ((val >> 6) & 0x3f));
|
|
|
|
*res++ = static_cast<char>(0x80 | (val & 0x3f));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static jsize get_utf16_from_utf8_length(const char *p, size_t len, jsize *surrogates) {
|
|
|
|
// UTF-8 correctness is supposed
|
|
|
|
jsize result = 0;
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
result += ((p[i] & 0xc0) != 0x80);
|
|
|
|
*surrogates += ((p[i] & 0xf8) == 0xf0);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void utf8_to_utf16(const char *p, size_t len, jchar *res) {
|
|
|
|
// UTF-8 correctness is supposed
|
|
|
|
for (size_t i = 0; i < len;) {
|
2021-10-17 21:56:40 +02:00
|
|
|
uint32 a = static_cast<unsigned char>(p[i++]);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (a >= 0x80) {
|
2021-10-17 21:56:40 +02:00
|
|
|
uint32 b = static_cast<unsigned char>(p[i++]);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (a >= 0xe0) {
|
2021-10-17 21:56:40 +02:00
|
|
|
uint32 c = static_cast<unsigned char>(p[i++]);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (a >= 0xf0) {
|
2021-10-17 21:56:40 +02:00
|
|
|
uint32 d = static_cast<unsigned char>(p[i++]);
|
|
|
|
uint32 val = ((a & 0x07) << 18) + ((b & 0x3f) << 12) + ((c & 0x3f) << 6) + (d & 0x3f) - 0x10000;
|
2018-12-31 20:04:05 +01:00
|
|
|
*res++ = static_cast<jchar>(0xD800 + (val >> 10));
|
|
|
|
*res++ = static_cast<jchar>(0xDC00 + (val & 0x3ff));
|
|
|
|
} else {
|
|
|
|
*res++ = static_cast<jchar>(((a & 0x0f) << 12) + ((b & 0x3f) << 6) + (c & 0x3f));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*res++ = static_cast<jchar>(((a & 0x1f) << 6) + (b & 0x3f));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*res++ = static_cast<jchar>(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string fetch_string(JNIEnv *env, jobject o, jfieldID id) {
|
|
|
|
jstring s = (jstring)env->GetObjectField(o, id);
|
|
|
|
if (s == nullptr) {
|
|
|
|
// treat null as an empty string
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
std::string res = from_jstring(env, s);
|
|
|
|
env->DeleteLocalRef(s);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string from_jstring(JNIEnv *env, jstring s) {
|
|
|
|
if (!s) {
|
2021-10-20 00:27:02 +02:00
|
|
|
return std::string();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
jsize s_len = env->GetStringLength(s);
|
|
|
|
const jchar *p = env->GetStringChars(s, nullptr);
|
|
|
|
if (p == nullptr) {
|
|
|
|
parse_error = true;
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
size_t len = get_utf8_from_utf16_length(p, s_len);
|
|
|
|
std::string res(len, '\0');
|
|
|
|
if (len) {
|
|
|
|
utf16_to_utf8(p, s_len, &res[0]);
|
|
|
|
}
|
|
|
|
env->ReleaseStringChars(s, p);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
jstring to_jstring(JNIEnv *env, const std::string &s) {
|
|
|
|
jsize surrogates = 0;
|
|
|
|
jsize unicode_len = get_utf16_from_utf8_length(s.c_str(), s.size(), &surrogates);
|
|
|
|
if (surrogates == 0) {
|
|
|
|
// TODO '\0'
|
|
|
|
return env->NewStringUTF(s.c_str());
|
|
|
|
}
|
|
|
|
jsize result_len = surrogates + unicode_len;
|
|
|
|
if (result_len <= 256) {
|
|
|
|
jchar result[256];
|
|
|
|
utf8_to_utf16(s.c_str(), s.size(), result);
|
|
|
|
return env->NewString(result, result_len);
|
|
|
|
}
|
|
|
|
|
2018-01-22 10:51:04 +01:00
|
|
|
auto result = std::make_unique<jchar[]>(result_len);
|
|
|
|
utf8_to_utf16(s.c_str(), s.size(), result.get());
|
|
|
|
return env->NewString(result.get(), result_len);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string from_bytes(JNIEnv *env, jbyteArray arr) {
|
|
|
|
std::string b;
|
|
|
|
if (arr != nullptr) {
|
|
|
|
jsize length = env->GetArrayLength(arr);
|
2018-01-15 14:47:24 +01:00
|
|
|
if (length != 0) {
|
|
|
|
b.resize(narrow_cast<size_t>(length));
|
|
|
|
env->GetByteArrayRegion(arr, 0, length, reinterpret_cast<jbyte *>(&b[0]));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
env->DeleteLocalRef(arr);
|
|
|
|
}
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
jbyteArray to_bytes(JNIEnv *env, const std::string &b) {
|
|
|
|
static_assert(sizeof(char) == sizeof(jbyte), "Mismatched jbyte size");
|
2021-10-20 00:27:02 +02:00
|
|
|
auto length = narrow_cast<jsize>(b.size());
|
2018-12-31 20:04:05 +01:00
|
|
|
jbyteArray arr = env->NewByteArray(length);
|
2018-01-15 14:47:24 +01:00
|
|
|
if (arr != nullptr && length != 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
env->SetByteArrayRegion(arr, 0, length, reinterpret_cast<const jbyte *>(b.data()));
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
jintArray store_vector(JNIEnv *env, const std::vector<std::int32_t> &v) {
|
|
|
|
static_assert(sizeof(std::int32_t) == sizeof(jint), "Mismatched jint size");
|
2021-10-20 00:27:02 +02:00
|
|
|
auto length = narrow_cast<jsize>(v.size());
|
2018-12-31 20:04:05 +01:00
|
|
|
jintArray arr = env->NewIntArray(length);
|
2018-01-15 14:47:24 +01:00
|
|
|
if (arr != nullptr && length != 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
env->SetIntArrayRegion(arr, 0, length, reinterpret_cast<const jint *>(&v[0]));
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
jlongArray store_vector(JNIEnv *env, const std::vector<std::int64_t> &v) {
|
|
|
|
static_assert(sizeof(std::int64_t) == sizeof(jlong), "Mismatched jlong size");
|
2021-10-20 00:27:02 +02:00
|
|
|
auto length = narrow_cast<jsize>(v.size());
|
2018-12-31 20:04:05 +01:00
|
|
|
jlongArray arr = env->NewLongArray(length);
|
2018-01-15 14:47:24 +01:00
|
|
|
if (arr != nullptr && length != 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
env->SetLongArrayRegion(arr, 0, length, reinterpret_cast<const jlong *>(&v[0]));
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
jdoubleArray store_vector(JNIEnv *env, const std::vector<double> &v) {
|
|
|
|
static_assert(sizeof(double) == sizeof(jdouble), "Mismatched jdouble size");
|
2021-10-20 00:27:02 +02:00
|
|
|
auto length = narrow_cast<jsize>(v.size());
|
2018-12-31 20:04:05 +01:00
|
|
|
jdoubleArray arr = env->NewDoubleArray(length);
|
2018-01-15 14:47:24 +01:00
|
|
|
if (arr != nullptr && length != 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
env->SetDoubleArrayRegion(arr, 0, length, reinterpret_cast<const jdouble *>(&v[0]));
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
jobjectArray store_vector(JNIEnv *env, const std::vector<std::string> &v) {
|
2021-10-20 00:27:02 +02:00
|
|
|
auto length = narrow_cast<jsize>(v.size());
|
2018-12-31 20:04:05 +01:00
|
|
|
jobjectArray arr = env->NewObjectArray(length, StringClass, 0);
|
2018-01-15 14:47:24 +01:00
|
|
|
if (arr != nullptr) {
|
2018-12-31 20:04:05 +01:00
|
|
|
for (jsize i = 0; i < length; i++) {
|
|
|
|
jstring str = to_jstring(env, v[i]);
|
|
|
|
if (str) {
|
|
|
|
env->SetObjectArrayElement(arr, i, str);
|
|
|
|
env->DeleteLocalRef(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2018-01-15 14:47:24 +01:00
|
|
|
std::vector<std::int32_t> fetch_vector(JNIEnv *env, jintArray arr) {
|
|
|
|
std::vector<std::int32_t> result;
|
|
|
|
if (arr != nullptr) {
|
|
|
|
jsize length = env->GetArrayLength(arr);
|
|
|
|
if (length != 0) {
|
|
|
|
result.resize(length);
|
|
|
|
env->GetIntArrayRegion(arr, 0, length, reinterpret_cast<jint *>(&result[0]));
|
|
|
|
}
|
|
|
|
env->DeleteLocalRef(arr);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::int64_t> fetch_vector(JNIEnv *env, jlongArray arr) {
|
|
|
|
std::vector<std::int64_t> result;
|
|
|
|
if (arr != nullptr) {
|
|
|
|
jsize length = env->GetArrayLength(arr);
|
|
|
|
if (length != 0) {
|
|
|
|
result.resize(length);
|
|
|
|
env->GetLongArrayRegion(arr, 0, length, reinterpret_cast<jlong *>(&result[0]));
|
|
|
|
}
|
|
|
|
env->DeleteLocalRef(arr);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<double> fetch_vector(JNIEnv *env, jdoubleArray arr) {
|
|
|
|
std::vector<double> result;
|
|
|
|
if (arr != nullptr) {
|
|
|
|
jsize length = env->GetArrayLength(arr);
|
|
|
|
if (length != 0) {
|
|
|
|
result.resize(length);
|
|
|
|
env->GetDoubleArrayRegion(arr, 0, length, reinterpret_cast<jdouble *>(&result[0]));
|
|
|
|
}
|
|
|
|
env->DeleteLocalRef(arr);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace jni
|
|
|
|
} // namespace td
|