-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
78 changed files
with
690 additions
and
591 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,96 @@ | ||
#include <jni.h> | ||
#include "react-native-fast-rsa.h" | ||
#include <android/log.h> | ||
#include <jni.h> | ||
#include <librsa_bridge.h> | ||
|
||
extern "C" | ||
JNIEXPORT void JNICALL | ||
Java_com_fastrsa_FastRsaModule_initialize(JNIEnv *env, jobject thiz, | ||
jlong jsi_ptr) { | ||
__android_log_print(ANDROID_LOG_VERBOSE, "react-native-fast-rsa", | ||
"Initializing"); | ||
fastRSA::install(*reinterpret_cast<facebook::jsi::Runtime *>(jsi_ptr)); | ||
} | ||
#include "react-native-fast-rsa.h" | ||
|
||
extern "C" | ||
JNIEXPORT void JNICALL | ||
Java_com_fastrsa_FastRsaModule_destruct(JNIEnv *env, jobject thiz) { | ||
fastRSA::cleanup(); | ||
} | ||
extern "C" | ||
JNIEXPORT jbyteArray JNICALL | ||
Java_com_fastrsa_FastRsaModule_callNative(JNIEnv *env, jobject thiz, | ||
jstring name, jbyteArray payload) { | ||
extern "C" JNIEXPORT void JNICALL | ||
Java_com_fastrsa_FastRsaModule_initialize(JNIEnv* env, | ||
jobject /* thiz */, | ||
jlong jsContext) { | ||
if (jsContext == 0) { | ||
__android_log_print(ANDROID_LOG_ERROR, "react-native-fast-rsa", "Failed to initialize: jsContext is null"); | ||
jclass Exception = env->FindClass("java/lang/IllegalArgumentException"); | ||
env->ThrowNew(Exception, "JSI context is null"); | ||
return; | ||
} | ||
|
||
auto nameConstChar = env->GetStringUTFChars(name, nullptr); | ||
auto payloadBytes = env->GetByteArrayElements(payload, nullptr); | ||
auto size = env->GetArrayLength(payload); | ||
__android_log_print(ANDROID_LOG_VERBOSE, "react-native-fast-rsa", "Initializing JSI bindings"); | ||
|
||
auto nameChar = const_cast<char *>(nameConstChar); | ||
auto response = RSABridgeCall(nameChar, payloadBytes, size); | ||
try { | ||
auto* runtime = reinterpret_cast<facebook::jsi::Runtime*>(jsContext); | ||
|
||
env->ReleaseStringUTFChars(name, nameConstChar); | ||
env->ReleaseByteArrayElements(payload, payloadBytes, 0); | ||
|
||
if (response->error != nullptr) { | ||
auto error = response->error; | ||
free(response); | ||
jclass Exception = env->FindClass("java/lang/Exception"); | ||
env->ThrowNew(Exception, error); | ||
return nullptr; | ||
} | ||
|
||
auto result = env->NewByteArray(response->size); | ||
env->SetByteArrayRegion(result, 0, response->size, (jbyte*) response->message); | ||
free(response); | ||
return result; | ||
fastRSA::install(*runtime); | ||
|
||
__android_log_print(ANDROID_LOG_INFO, "react-native-fast-rsa", "JSI bindings successfully installed"); | ||
} catch (const std::exception& e) { | ||
__android_log_print(ANDROID_LOG_ERROR, "react-native-fast-rsa", "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-rsa", "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_fastrsa_FastRsaModule_destruct(JNIEnv* env, jobject thiz) { | ||
fastRSA::cleanup(); | ||
} | ||
|
||
extern "C" JNIEXPORT jbyteArray JNICALL | ||
Java_com_fastrsa_FastRsaModule_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; | ||
} | ||
|
||
extern "C" | ||
JNIEXPORT jbyteArray JNICALL | ||
Java_com_fastrsa_FastRsaModule_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); | ||
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; | ||
} | ||
|
||
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 = | ||
RSABridgeCall(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 = fastRSA::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; | ||
} | ||
|
||
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; | ||
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; | ||
} | ||
|
||
} | ||
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; | ||
} | ||
env->SetByteArrayRegion(result, 0, response->size, reinterpret_cast<jbyte*>(response->message)); | ||
free(response); | ||
|
||
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 @@ | ||
FastRsa_kotlinVersion=1.7.0 | ||
FastRsa_minSdkVersion=21 | ||
FastRsa_targetSdkVersion=31 | ||
FastRsa_compileSdkVersion=31 | ||
FastRsa_ndkversion=21.4.7075529 | ||
FastRsa_targetSdkVersion=33 | ||
FastRsa_compileSdkVersion=33 | ||
FastRsa_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.