feat: add in libsession-util dependency to app and release string utf chars after use

This commit is contained in:
0x330a 2022-12-16 16:00:38 +11:00
parent 7d040dca26
commit bd2a4fcfd6
No known key found for this signature in database
GPG Key ID: 267811D6E6A2698C
4 changed files with 22 additions and 6 deletions

View File

@ -101,6 +101,7 @@ dependencies {
}
implementation project(":libsignal")
implementation project(":libsession")
implementation project(":libsession-util")
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlinxJsonVersion"
implementation "com.github.oxen-io.session-android-curve-25519:curve25519-java:$curve25519Version"
implementation project(":liblazysodium")

View File

@ -6,7 +6,9 @@ JNIEXPORT jobject JNICALL
Java_network_loki_messenger_libsession_1util_Contacts_get(JNIEnv *env, jobject thiz,
jstring session_id) {
auto contacts = ptrToContacts(env, thiz);
auto contact = contacts->get(env->GetStringUTFChars(session_id, nullptr));
auto session_id_chars = env->GetStringUTFChars(session_id, nullptr);
auto contact = contacts->get(session_id_chars);
env->ReleaseStringUTFChars(session_id, session_id_chars);
if (!contact) return nullptr;
jobject j_contact = serialize_contact(env, contact.value());
return j_contact;
@ -17,7 +19,9 @@ JNIEXPORT jobject JNICALL
Java_network_loki_messenger_libsession_1util_Contacts_getOrCreate(JNIEnv *env, jobject thiz,
jstring session_id) {
auto contacts = ptrToContacts(env, thiz);
auto contact = contacts->get_or_create(env->GetStringUTFChars(session_id, nullptr));
auto session_id_chars = env->GetStringUTFChars(session_id, nullptr);
auto contact = contacts->get_or_create(session_id_chars);
env->ReleaseStringUTFChars(session_id, session_id_chars);
return serialize_contact(env, contact);
}
@ -35,7 +39,11 @@ JNIEXPORT jboolean JNICALL
Java_network_loki_messenger_libsession_1util_Contacts_erase(JNIEnv *env, jobject thiz,
jstring session_id) {
auto contacts = ptrToContacts(env, thiz);
return contacts->erase(env->GetStringUTFChars(session_id, nullptr));
auto session_id_chars = env->GetStringUTFChars(session_id, nullptr);
bool result = contacts->erase(session_id_chars);
env->ReleaseStringUTFChars(session_id, session_id_chars);
return result;
}
extern "C"
#pragma clang diagnostic push

View File

@ -40,7 +40,9 @@ Java_network_loki_messenger_libsession_1util_UserProfile_setName(
jobject thiz,
jstring newName) {
auto profile = ptrToProfile(env, thiz);
profile->set_name(env->GetStringUTFChars(newName, nullptr));
auto name_chars = env->GetStringUTFChars(newName, nullptr);
profile->set_name(name_chars);
env->ReleaseStringUTFChars(newName, name_chars);
}
JNIEXPORT jstring JNICALL
@ -76,6 +78,7 @@ Java_network_loki_messenger_libsession_1util_UserProfile_setPic(JNIEnv *env, job
auto url = env->GetStringUTFChars(pic.first, nullptr);
auto key = util::ustring_from_bytes(env, pic.second);
profile->set_profile_pic(url, key);
env->ReleaseStringUTFChars(pic.first, url);
}
}

View File

@ -65,8 +65,12 @@ Java_network_loki_messenger_libsession_1util_util_Sodium_ed25519PkToCurve25519(J
jbyteArray pk) {
auto ed_pk = util::ustring_from_bytes(env, pk);
std::array<unsigned char, 32> curve_pk; // NOLINT(cppcoreguidelines-pro-type-member-init)
crypto_sign_ed25519_pk_to_curve25519(curve_pk.data(), ed_pk.data());
int success = crypto_sign_ed25519_pk_to_curve25519(curve_pk.data(), ed_pk.data());
if (success != 0) {
jclass exception = env->FindClass("java/lang/Exception");
env->ThrowNew(exception, "Invalid crypto_sign_ed25519_pk_to_curve25519 operation");
return nullptr;
}
jbyteArray curve_pk_jarray = util::bytes_from_ustring(env, session::ustring_view {curve_pk.data(), curve_pk.size()});
return curve_pk_jarray;
}