From 04fd4c5a1301545419fb72533c57e4253085bdc0 Mon Sep 17 00:00:00 2001 From: pedroGitt Date: Mon, 14 Nov 2016 11:56:37 +0100 Subject: Fix the decrypt issue (crash on V5.1.1) when the message to decrypt contains emojis: - add an internal specific JNI function (javaCStringToUtf8()) to perform the UTF-8 conversion - the SDK is configured to enable/disable the use of javaCStringToUtf8() --- .../olm-sdk/src/main/jni/olm_jni_helper.cpp | 45 +++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) (limited to 'java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp') diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp index 239fc68..561010d 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp @@ -270,4 +270,47 @@ jstring serializeDataWithKey(JNIEnv *env, jobject thiz, } return pickledDataRetValue; -} \ No newline at end of file +} + + +/** +* Convert a C string into a UTF-8 format string. +* The conversion is performed in JAVA side to workaround the issue in NewStringUTF(). +* The problem is described here: https://github.com/eclipsesource/J2V8/issues/142 +*/ +jstring javaCStringToUtf8(JNIEnv *env, uint8_t *aCStringMsgPtr, size_t aMsgLength) +{ + jstring convertedRetValue = 0; + jbyteArray tempByteArray = NULL; + + if((NULL == aCStringMsgPtr) || (NULL == env)) + { + LOGE("## javaCStringToUtf8(): failure - invalid parameters (null)"); + } + else if(NULL == (tempByteArray=env->NewByteArray(aMsgLength))) + { + LOGE("## javaCStringToUtf8(): failure - return byte array OOM"); + } + else + { + env->SetByteArrayRegion(tempByteArray, 0, aMsgLength, (const jbyte*)aCStringMsgPtr); + + // UTF-8 conversion from JAVA + jstring strEncode = (env)->NewStringUTF("UTF-8"); + jclass jClass = env->FindClass("java/lang/String"); + jmethodID cstor = env->GetMethodID(jClass, "", "([BLjava/lang/String;)V"); + + if((0!=jClass) && (0!=jClass) && (0!=strEncode)) + { + convertedRetValue = (jstring) env->NewObject(jClass, cstor, tempByteArray, strEncode); + LOGD(" ## javaCStringToUtf8(): succeed"); + env->DeleteLocalRef(tempByteArray); + } + else + { + LOGE(" ## javaCStringToUtf8(): failure - invalid Java references"); + } + } + + return convertedRetValue; +} -- cgit v1.2.3