-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
84 changed files
with
705 additions
and
596 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
BasedOnStyle: Google | ||
ColumnLimit: 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,94 @@ | ||
#include <jni.h> | ||
#include "react-native-fast-openpgp.h" | ||
#include <android/log.h> | ||
#include <jni.h> | ||
#include <libopenpgp_bridge.h> | ||
|
||
extern "C" | ||
JNIEXPORT void JNICALL | ||
Java_com_fastopenpgp_FastOpenpgpModule_initialize(JNIEnv *env, jobject thiz, | ||
jlong jsi_ptr) { | ||
__android_log_print(ANDROID_LOG_VERBOSE, "react-native-fast-openpgp", | ||
"Initializing"); | ||
fastOpenPGP::install(*reinterpret_cast<facebook::jsi::Runtime *>(jsi_ptr)); | ||
} | ||
|
||
extern "C" | ||
JNIEXPORT void JNICALL | ||
Java_com_fastopenpgp_FastOpenpgpModule_destruct(JNIEnv *env, jobject thiz) { | ||
fastOpenPGP::cleanup(); | ||
} | ||
extern "C" | ||
JNIEXPORT jbyteArray JNICALL | ||
Java_com_fastopenpgp_FastOpenpgpModule_callNative(JNIEnv *env, jobject thiz, | ||
jstring name, jbyteArray payload) { | ||
#include "react-native-fast-openpgp.h" | ||
|
||
auto nameConstChar = env->GetStringUTFChars(name, nullptr); | ||
auto payloadBytes = env->GetByteArrayElements(payload, nullptr); | ||
auto size = env->GetArrayLength(payload); | ||
extern "C" JNIEXPORT void JNICALL | ||
Java_com_fastopenpgp_FastOpenpgpModule_initialize(JNIEnv* env, | ||
jobject /* thiz */, | ||
jlong jsContext) { | ||
if (jsContext == 0) { | ||
__android_log_print(ANDROID_LOG_ERROR, "react-native-fast-openpgp", "Failed to initialize: jsContext is null"); | ||
jclass Exception = env->FindClass("java/lang/IllegalArgumentException"); | ||
env->ThrowNew(Exception, "JSI context is null"); | ||
return; | ||
} | ||
|
||
auto nameChar = const_cast<char *>(nameConstChar); | ||
auto response = OpenPGPBridgeCall(nameChar, payloadBytes, size); | ||
__android_log_print(ANDROID_LOG_VERBOSE, "react-native-fast-openpgp", "Initializing JSI bindings"); | ||
|
||
env->ReleaseStringUTFChars(name, nameConstChar); | ||
env->ReleaseByteArrayElements(payload, payloadBytes, 0); | ||
try { | ||
auto* runtime = reinterpret_cast<facebook::jsi::Runtime*>(jsContext); | ||
|
||
if (response->error != nullptr) { | ||
auto error = response->error; | ||
free(response); | ||
jclass Exception = env->FindClass("java/lang/Exception"); | ||
env->ThrowNew(Exception, error); | ||
return nullptr; | ||
} | ||
fastOpenPGP::install(*runtime); | ||
|
||
auto result = env->NewByteArray(response->size); | ||
env->SetByteArrayRegion(result, 0, response->size, (jbyte*) response->message); | ||
free(response); | ||
return result; | ||
__android_log_print(ANDROID_LOG_INFO, "react-native-fast-openpgp", "JSI bindings successfully installed"); | ||
} catch (const std::exception& e) { | ||
__android_log_print(ANDROID_LOG_ERROR, "react-native-fast-openpgp", "Exception during initialization: %s", e.what()); | ||
jclass Exception = env->FindClass("java/lang/RuntimeException"); | ||
env->ThrowNew(Exception, e.what()); | ||
} catch (...) { | ||
__android_log_print(ANDROID_LOG_ERROR, "react-native-fast-openpgp", "Unknown error during initialization"); | ||
jclass Exception = env->FindClass("java/lang/RuntimeException"); | ||
env->ThrowNew(Exception, "Unknown error occurred during JSI initialization"); | ||
} | ||
} | ||
extern "C" JNIEXPORT void JNICALL | ||
Java_com_fastopenpgp_FastOpenpgpModule_destruct(JNIEnv* env, jobject thiz) { | ||
fastOpenPGP::cleanup(); | ||
} | ||
extern "C" JNIEXPORT jbyteArray JNICALL | ||
Java_com_fastopenpgp_FastOpenpgpModule_callNative(JNIEnv* env, | ||
jobject thiz, | ||
jstring name, | ||
jbyteArray payload) { | ||
if (name == nullptr || payload == nullptr) { | ||
jclass Exception = env->FindClass("java/lang/NullPointerException"); | ||
env->ThrowNew(Exception, "Input parameters 'name' or 'payload' cannot be null"); | ||
return nullptr; | ||
} | ||
|
||
const char* nameConstChar = env->GetStringUTFChars(name, nullptr); | ||
if (nameConstChar == nullptr) { | ||
jclass Exception = env->FindClass("java/lang/OutOfMemoryError"); | ||
env->ThrowNew(Exception, "Failed to allocate memory for 'name'"); | ||
return nullptr; | ||
} | ||
|
||
extern "C" | ||
JNIEXPORT jbyteArray JNICALL | ||
Java_com_fastopenpgp_FastOpenpgpModule_callJSI(JNIEnv *env, jobject thiz, jlong jsi_ptr, | ||
jstring name, jbyteArray payload) { | ||
auto &runtime = *reinterpret_cast<jsi::Runtime *>(jsi_ptr); | ||
auto nameConstChar = env->GetStringUTFChars(name, nullptr); | ||
auto payloadBytes = env->GetByteArrayElements(payload, nullptr); | ||
auto size = env->GetArrayLength(payload); | ||
|
||
auto nameValue = jsi::String::createFromAscii(runtime, nameConstChar); | ||
jbyte* payloadBytes = env->GetByteArrayElements(payload, nullptr); | ||
if (payloadBytes == nullptr) { | ||
env->ReleaseStringUTFChars(name, nameConstChar); | ||
jclass Exception = env->FindClass("java/lang/OutOfMemoryError"); | ||
env->ThrowNew(Exception, "Failed to allocate memory for 'payload'"); | ||
return nullptr; | ||
} | ||
|
||
jsize size = env->GetArrayLength(payload); | ||
auto response = | ||
OpenPGPBridgeCall(const_cast<char*>(nameConstChar), payloadBytes, size); | ||
|
||
auto arrayBuffer = runtime.global().getPropertyAsFunction(runtime, "ArrayBuffer"); | ||
jsi::Object o = arrayBuffer.callAsConstructor(runtime, size).getObject(runtime); | ||
jsi::ArrayBuffer payloadValue = o.getArrayBuffer(runtime); | ||
memcpy(payloadValue.data(runtime), payloadBytes, size); | ||
env->ReleaseByteArrayElements(payload, payloadBytes, 0); | ||
// Release resources | ||
env->ReleaseStringUTFChars(name, nameConstChar); | ||
env->ReleaseByteArrayElements(payload, payloadBytes, JNI_ABORT); | ||
|
||
auto response = fastOpenPGP::call(runtime, nameValue, payloadValue); | ||
if (response->error != nullptr) { | ||
const char* error = response->error; | ||
free(response); | ||
jclass Exception = env->FindClass("java/lang/Exception"); | ||
env->ThrowNew(Exception, error); | ||
return nullptr; | ||
} | ||
|
||
jbyteArray result = env->NewByteArray(response->size); | ||
if (result == nullptr) { | ||
free(response); | ||
jclass Exception = env->FindClass("java/lang/OutOfMemoryError"); | ||
env->ThrowNew(Exception, "Failed to allocate memory for result"); | ||
return nullptr; | ||
} | ||
|
||
if (response.isString()) { | ||
auto error = response.asString(runtime); | ||
jclass Exception = env->FindClass("java/lang/Exception"); | ||
env->ThrowNew(Exception, error.utf8(runtime).c_str()); | ||
return nullptr; | ||
env->SetByteArrayRegion(result, 0, response->size, reinterpret_cast<jbyte*>(response->message)); | ||
free(response); | ||
|
||
} | ||
auto byteResult = response.asObject(runtime).getArrayBuffer(runtime); | ||
auto sizeResult = byteResult.size(runtime); | ||
auto result = env->NewByteArray(sizeResult); | ||
env->SetByteArrayRegion(result, 0, sizeResult, (jbyte*) byteResult.data(runtime)); | ||
return result; | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
FastOpenpgp_kotlinVersion=1.7.0 | ||
FastOpenpgp_minSdkVersion=21 | ||
FastOpenpgp_targetSdkVersion=31 | ||
FastOpenpgp_compileSdkVersion=31 | ||
FastOpenpgp_ndkversion=21.4.7075529 | ||
FastOpenpgp_targetSdkVersion=33 | ||
FastOpenpgp_compileSdkVersion=33 | ||
FastOpenpgp_ndkversion=23.1.7779620 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.