From 7bf7a7e4158df882028df510e8d76009f2640d96 Mon Sep 17 00:00:00 2001 From: ylecollen Date: Mon, 9 Jan 2017 13:55:58 +0100 Subject: use the same way to name the creation method i.e. createNewXX. Avoid the initWithXX. --- .../src/main/java/org/matrix/olm/OlmAccount.java | 57 ++++++------------- .../src/main/java/org/matrix/olm/OlmException.java | 1 + .../org/matrix/olm/OlmInboundGroupSession.java | 44 +++++++-------- .../src/main/java/org/matrix/olm/OlmManager.java | 2 - .../org/matrix/olm/OlmOutboundGroupSession.java | 45 +++++++-------- .../src/main/java/org/matrix/olm/OlmSession.java | 43 ++++---------- .../src/main/java/org/matrix/olm/OlmUtility.java | 11 +++- .../OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp | 66 +++++++++------------- .../OlmLibSdk/olm-sdk/src/main/jni/olm_account.h | 1 - .../src/main/jni/olm_inbound_group_session.cpp | 8 +++ .../OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp | 36 ++++-------- .../OlmLibSdk/olm-sdk/src/main/jni/olm_session.h | 1 - .../OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp | 9 +-- .../OlmLibSdk/olm-sdk/src/main/jni/olm_utility.h | 2 +- 14 files changed, 131 insertions(+), 195 deletions(-) diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java index 6737963..3e7d01d 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java @@ -17,6 +17,7 @@ package org.matrix.olm; +import android.text.TextUtils; import android.util.Log; import org.json.JSONObject; @@ -61,7 +62,7 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { private transient long mNativeId; public OlmAccount() throws OlmException { - initNewAccount(); + createNewAccount(); } /** @@ -85,43 +86,23 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { * Destroy the corresponding OLM account native object.
* This method must ALWAYS be called when this JAVA instance * is destroyed (ie. garbage collected) to prevent memory leak in native side. - * See {@link #initNewAccountJni()}. + * See {@link #createNewAccountJni()}. */ private native void releaseAccountJni(); /** * Create and initialize a native account instance.
- * Wrapper for {@link #initNewAccountJni()}. * To be called before any other API call. * @exception OlmException the failure reason */ - private void initNewAccount() throws OlmException { + private void createNewAccount() throws OlmException { try { - mNativeId = initNewAccountJni(); + mNativeId = createNewAccountJni(); } catch (Exception e) { throw new OlmException(OlmException.EXCEPTION_CODE_INIT_ACCOUNT_CREATION, e.getMessage()); } } - /** - * Create and initialize an OLM account in native side.
- * Do not forget to call {@link #releaseAccount()} when JAVA side is done. - * @return native account instance identifier (see {@link #mNativeId}) - */ - private native long initNewAccountJni(); - - /** - * Create a native account instance without any initialization.
- * Since the account is left uninitialized, this - * method is intended to be used in the serialization mechanism (see {@link #readObject(ObjectInputStream)}).
- * Public wrapper for {@link #createNewAccountJni()}. - * @return true if init succeed, false otherwise. - */ - private boolean createNewAccount() { - mNativeId = initNewAccountJni(); - return (0 != mNativeId); - } - /** * Create an OLM account in native side.
* Do not forget to call {@link #releaseAccount()} when JAVA side is done. @@ -408,10 +389,9 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { /** * Kick off the deserialization mechanism. * @param aInStream input stream - * @throws IOException exception - * @throws ClassNotFoundException exception + * @throws Exception exception */ - private void readObject(ObjectInputStream aInStream) throws IOException, ClassNotFoundException { + private void readObject(ObjectInputStream aInStream) throws Exception { deserialize(aInStream); } @@ -453,31 +433,28 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { * See {@link #serialize(byte[], StringBuffer)} * @param aSerializedData bytes buffer * @param aKey key used to encrypted + * @exception Exception the exception */ @Override - protected void deserialize(byte[] aSerializedData, byte[] aKey) throws IOException { - if (!createNewAccount()) { - throw new OlmException(OlmException.EXCEPTION_CODE_INIT_ACCOUNT_CREATION,OlmException.EXCEPTION_MSG_INIT_ACCOUNT_CREATION); - } - - StringBuffer errorMsg = new StringBuffer(); + protected void deserialize(byte[] aSerializedData, byte[] aKey) throws Exception { + createNewAccount(); + String errorMsg; try { - String jniError; if ((null == aSerializedData) || (null == aKey)) { Log.e(LOG_TAG, "## deserialize(): invalid input parameters"); - errorMsg.append("invalid input parameters"); - } else if (null != (jniError = deserializeJni(aSerializedData, aKey))) { - errorMsg.append(jniError); + errorMsg = "invalid input parameters"; + } else { + errorMsg = deserializeJni(aSerializedData, aKey); } } catch (Exception e) { Log.e(LOG_TAG, "## deserialize() failed " + e.getMessage()); - errorMsg.append(e.getMessage()); + errorMsg = e.getMessage(); } - if (errorMsg.length() > 0) { + if (!TextUtils.isEmpty(errorMsg)) { releaseAccount(); - throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, String.valueOf(errorMsg)); + throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, errorMsg); } } diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmException.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmException.java index d02b7fc..155724b 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmException.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmException.java @@ -55,6 +55,7 @@ public class OlmException extends IOException { public static final int EXCEPTION_CODE_SESSION_DECRYPT_MESSAGE = 405; public static final int EXCEPTION_CODE_SESSION_SESSION_IDENTIFIER = 406; + public static final int EXCEPTION_CODE_UTILITY_CREATION = 501; public static final int EXCEPTION_CODE_UTILITY_VERIFY_SIGNATURE = 500; // exception human readable messages diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java index dc1b80d..d9075c8 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java @@ -61,11 +61,8 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri * @throws OlmException constructor failure */ public OlmInboundGroupSession(String aSessionKey) throws OlmException { - if(createNewSession()) { - initInboundGroupSession(aSessionKey); - } else { - throw new OlmException(OlmException.EXCEPTION_CODE_CREATE_INBOUND_GROUP_SESSION, OlmException.EXCEPTION_MSG_NEW_INBOUND_GROUP_SESSION); - } + createNewSession(); + initInboundGroupSession(aSessionKey); } /** @@ -88,11 +85,14 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri /** * Create and save the session native instance ID.
* To be called before any other API call. - * @return true if init succeed, false otherwise. + * @exception OlmException the failure reason */ - private boolean createNewSession() { - mNativeId = createNewSessionJni(); - return (0 != mNativeId); + private void createNewSession() throws OlmException { + try { + mNativeId = createNewSessionJni(); + } catch (Exception e) { + throw new OlmException(OlmException.EXCEPTION_CODE_CREATE_INBOUND_GROUP_SESSION, e.getMessage()); + } } /** @@ -190,10 +190,9 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri /** * Kick off the deserialization mechanism. * @param aInStream input stream - * @throws IOException exception - * @throws ClassNotFoundException exception + * @throws Exception exception */ - private void readObject(ObjectInputStream aInStream) throws IOException, ClassNotFoundException { + private void readObject(ObjectInputStream aInStream) throws Exception { deserialize(aInStream); } @@ -242,29 +241,26 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri * @param aKey key used to encrypted */ @Override - protected void deserialize(byte[] aSerializedData, byte[] aKey) throws IOException { - if (!createNewSession()) { - throw new OlmException(OlmException.EXCEPTION_CODE_INIT_ACCOUNT_CREATION,OlmException.EXCEPTION_MSG_INIT_ACCOUNT_CREATION); - } + protected void deserialize(byte[] aSerializedData, byte[] aKey) throws Exception { + createNewSession(); - StringBuffer errorMsg = new StringBuffer(); + String errorMsg; try { - String jniError; if ((null == aSerializedData) || (null == aKey)) { Log.e(LOG_TAG, "## deserialize(): invalid input parameters"); - errorMsg.append("invalid input parameters"); - } else if (null != (jniError = deserializeJni(aSerializedData, aKey))) { - errorMsg.append(jniError); + errorMsg = "invalid input parameters"; + } else { + errorMsg = deserializeJni(aSerializedData, aKey); } } catch (Exception e) { Log.e(LOG_TAG, "## deserialize() failed " + e.getMessage()); - errorMsg.append(e.getMessage()); + errorMsg = e.getMessage(); } - if (errorMsg.length() > 0) { + if (!TextUtils.isEmpty(errorMsg)) { releaseSession(); - throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, String.valueOf(errorMsg)); + throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, errorMsg); } } diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmManager.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmManager.java index 1605392..2efb0c2 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmManager.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmManager.java @@ -42,8 +42,6 @@ public class OlmManager { } public String getSdkOlmVersion() { - //Date currentDate = Calendar.getInstance().getTime(); - //String retVal = new SimpleDateFormat("yyyyMMdd_HH:mm:ss").format(currentDate); return SDK_OLM_VERSION; } diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java index 534636c..a7972d3 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java @@ -50,11 +50,8 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser * @throws OlmException constructor failure */ public OlmOutboundGroupSession() throws OlmException { - if(createNewSession()) { - initOutboundGroupSession(); - } else { - throw new OlmException(OlmException.EXCEPTION_CODE_CREATE_OUTBOUND_GROUP_SESSION, OlmException.EXCEPTION_MSG_NEW_OUTBOUND_GROUP_SESSION); - } + createNewSession(); + initOutboundGroupSession(); } /** @@ -78,11 +75,14 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser * Create and save the session native instance ID. * Wrapper for {@link #createNewSessionJni()}.
* To be called before any other API call. - * @return true if init succeed, false otherwise. + * @exception OlmException the exception. */ - private boolean createNewSession() { - mNativeId = createNewSessionJni(); - return (0 != mNativeId); + private void createNewSession() throws OlmException { + try { + mNativeId = createNewSessionJni(); + } catch (Exception e) { + throw new OlmException(OlmException.EXCEPTION_CODE_CREATE_OUTBOUND_GROUP_SESSION, e.getMessage()); + } } /** @@ -203,10 +203,9 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser /** * Kick off the deserialization mechanism. * @param aInStream input stream - * @throws IOException exception - * @throws ClassNotFoundException exception + * @throws Exception exception */ - private void readObject(ObjectInputStream aInStream) throws IOException, ClassNotFoundException { + private void readObject(ObjectInputStream aInStream) throws Exception { deserialize(aInStream); } @@ -247,31 +246,29 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser * See {@link #serialize(byte[], StringBuffer)} * @param aSerializedData pickled account in a base64 bytes buffer * @param aKey key used to encrypted + * @exception Exception the exception */ @Override - protected void deserialize(byte[] aSerializedData, byte[] aKey) throws IOException { - if (!createNewSession()) { - throw new OlmException(OlmException.EXCEPTION_CODE_INIT_ACCOUNT_CREATION,OlmException.EXCEPTION_MSG_INIT_ACCOUNT_CREATION); - } + protected void deserialize(byte[] aSerializedData, byte[] aKey) throws Exception { + createNewSession(); - StringBuffer errorMsg = new StringBuffer(); + String errorMsg = null; try { - String jniError; if ((null == aSerializedData) || (null == aKey)) { Log.e(LOG_TAG, "## deserialize(): invalid input parameters"); - errorMsg.append("invalid input parameters"); - } else if (null != (jniError = deserializeJni(aSerializedData, aKey))) { - errorMsg.append(jniError); + errorMsg = "invalid input parameters"; + } else { + errorMsg = deserializeJni(aSerializedData, aKey); } } catch (Exception e) { Log.e(LOG_TAG, "## deserialize() failed " + e.getMessage()); - errorMsg.append(e.getMessage()); + errorMsg = e.getMessage(); } - if (errorMsg.length() > 0) { + if (!TextUtils.isEmpty(errorMsg)) { releaseSession(); - throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, String.valueOf(errorMsg)); + throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, errorMsg); } } diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmSession.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmSession.java index bc190ed..2436f0d 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmSession.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmSession.java @@ -43,9 +43,7 @@ public class OlmSession extends CommonSerializeUtils implements Serializable { private transient long mNativeId; public OlmSession() throws OlmException { - if(!initNewSession()) { - throw new OlmException(OlmException.EXCEPTION_CODE_INIT_SESSION_CREATION, OlmException.EXCEPTION_MSG_INIT_SESSION_CREATION); - } + createNewSession(); } /** @@ -60,7 +58,7 @@ public class OlmSession extends CommonSerializeUtils implements Serializable { * Destroy the corresponding OLM session native object.
* This method must ALWAYS be called when this JAVA instance * is destroyed (ie. garbage collected) to prevent memory leak in native side. - * See {@link #initNewSessionJni()}. + * See {@link #createNewSessionJni()}. */ private native void releaseSessionJni(); @@ -73,34 +71,19 @@ public class OlmSession extends CommonSerializeUtils implements Serializable { mNativeId = 0; } - /** - * Create and save the session native instance ID. - * Wrapper for {@link #initNewSessionJni()}.
- * To be called before any other API call. - * @return true if init succeed, false otherwise. - */ - private boolean initNewSession() { - mNativeId = initNewSessionJni(); - return (0 != mNativeId); - } - - /** - * Create the corresponding OLM session in native side.
- * Do not forget to call {@link #releaseSession()} when JAVA side is done. - * @return native session instance identifier (see {@link #mNativeId}) - */ - private native long initNewSessionJni(); - /** * Create a native account instance without any initialization.
* Since the account is left uninitialized, this * method is intended to be used in the serialization mechanism (see {@link #readObject(ObjectInputStream)}).
* Public wrapper for {@link #createNewSessionJni()}. - * @return true if init succeed, false otherwise. + * @exception OlmException the exception */ - private boolean createNewSession() { - mNativeId = initNewSessionJni(); - return (0 != mNativeId); + private void createNewSession() throws OlmException { + try { + mNativeId = createNewSessionJni(); + } catch (Exception e) { + throw new OlmException(OlmException.EXCEPTION_CODE_INIT_SESSION_CREATION, e.getMessage()); + } } /** @@ -331,7 +314,7 @@ public class OlmSession extends CommonSerializeUtils implements Serializable { * @throws IOException exception * @throws ClassNotFoundException exception */ - private void readObject(ObjectInputStream aInStream) throws IOException, ClassNotFoundException { + private void readObject(ObjectInputStream aInStream) throws Exception { deserialize(aInStream); } @@ -374,10 +357,8 @@ public class OlmSession extends CommonSerializeUtils implements Serializable { * @param aKey key used to encrypted */ @Override - protected void deserialize(byte[] aSerializedData, byte[] aKey) throws IOException { - if (!createNewSession()) { - throw new OlmException(OlmException.EXCEPTION_CODE_INIT_ACCOUNT_CREATION,OlmException.EXCEPTION_MSG_INIT_ACCOUNT_CREATION); - } + protected void deserialize(byte[] aSerializedData, byte[] aKey) throws Exception { + createNewSession(); StringBuffer errorMsg = new StringBuffer(); diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtility.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtility.java index 3b1d9e1..d520da6 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtility.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtility.java @@ -29,7 +29,6 @@ public class OlmUtility { private static final String LOG_TAG = "OlmUtility"; public static final int RANDOM_KEY_SIZE = 32; - public static final int RANDOM_RANGE = 256; /** Instance Id returned by JNI. * This value uniquely identifies this utility instance. @@ -46,11 +45,11 @@ public class OlmUtility { * @return true if init succeed, false otherwise. */ private boolean initUtility() { - mNativeId = initUtilityJni(); + mNativeId = createUtilityJni(); return (0 != mNativeId); } - private native long initUtilityJni(); + private native long createUtilityJni(); /** * Release native instance.
@@ -136,6 +135,12 @@ public class OlmUtility { SecureRandom secureRandom = new SecureRandom(); byte[] buffer = new byte[RANDOM_KEY_SIZE]; secureRandom.nextBytes(buffer); + + // the key is saved as string + // so avoid the UTF8 marker bytes + for(int i = 0; i < RANDOM_KEY_SIZE; i++) { + buffer[i] = (byte)(buffer[i] & 0x7F); + } return buffer; } diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp index 7441da4..4da0d67 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp @@ -42,49 +42,13 @@ OlmAccount* initializeAccountMemory() return accountPtr; } -JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(createNewAccountJni)(JNIEnv *env, jobject thiz) -{ - LOGD("## createNewAccountJni(): IN"); - OlmAccount* accountPtr = initializeAccountMemory(); - - LOGD(" ## createNewAccountJni(): success - accountPtr=%p (jlong)(intptr_t)accountPtr=%lld",accountPtr,(jlong)(intptr_t)accountPtr); - return (jlong)(intptr_t)accountPtr; -} - -/** - * Release the account allocation made by initializeAccountMemory().
- * This method MUST be called when java counter part account instance is done. - * - */ -JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(releaseAccountJni)(JNIEnv *env, jobject thiz) -{ - LOGD("## releaseAccountJni(): IN"); - - OlmAccount* accountPtr = getAccountInstanceId(env, thiz); - - if (!accountPtr) - { - LOGE(" ## releaseAccountJni(): failure - invalid Account ptr=NULL"); - } - else - { - LOGD(" ## releaseAccountJni(): accountPtr=%p",accountPtr); - olm_clear_account(accountPtr); - - LOGD(" ## releaseAccountJni(): IN"); - // even if free(NULL) does not crash, logs are performed for debug purpose - free(accountPtr); - LOGD(" ## releaseAccountJni(): OUT"); - } -} - /** -* Initialize a new account and return it to JAVA side.
+* Create a new account and return it to JAVA side.
* Since a C prt is returned as a jlong, special care will be taken * to make the cast (OlmAccount* => jlong) platform independent. * @return the initialized OlmAccount* instance **/ -JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(initNewAccountJni)(JNIEnv *env, jobject thiz) +JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(createNewAccountJni)(JNIEnv *env, jobject thiz) { const char* errorMessage = NULL; OlmAccount *accountPtr = initializeAccountMemory(); @@ -139,6 +103,32 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(initNewAccountJni)(JNIEnv *env, jobject thi return (jlong)(intptr_t)accountPtr; } +/** + * Release the account allocation made by initializeAccountMemory().
+ * This method MUST be called when java counter part account instance is done. + * + */ +JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(releaseAccountJni)(JNIEnv *env, jobject thiz) +{ + LOGD("## releaseAccountJni(): IN"); + + OlmAccount* accountPtr = getAccountInstanceId(env, thiz); + + if (!accountPtr) + { + LOGE(" ## releaseAccountJni(): failure - invalid Account ptr=NULL"); + } + else + { + LOGD(" ## releaseAccountJni(): accountPtr=%p",accountPtr); + olm_clear_account(accountPtr); + + LOGD(" ## releaseAccountJni(): IN"); + // even if free(NULL) does not crash, logs are performed for debug purpose + free(accountPtr); + LOGD(" ## releaseAccountJni(): OUT"); + } +} // ********************************************************************* // ************************* IDENTITY KEYS API ************************* diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.h b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.h index 5b73acd..2bfb39c 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.h +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.h @@ -30,7 +30,6 @@ extern "C" { // account creation/destruction JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(releaseAccountJni)(JNIEnv *env, jobject thiz); -JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(initNewAccountJni)(JNIEnv *env, jobject thiz); JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(createNewAccountJni)(JNIEnv *env, jobject thiz); // identity keys diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.cpp index dd3e0a9..7dd250b 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.cpp @@ -58,6 +58,7 @@ JNIEXPORT void OLM_INBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env **/ JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz) { + const char* errorMessage = NULL; OlmInboundGroupSession* sessionPtr = NULL; size_t sessionSize = 0; @@ -67,6 +68,7 @@ JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv * if (!sessionSize) { LOGE(" ## createNewSessionJni(): failure - inbound group session size = 0"); + errorMessage = "inbound group session size = 0"; } else if ((sessionPtr = (OlmInboundGroupSession*)malloc(sessionSize))) { @@ -76,6 +78,12 @@ JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv * else { LOGE(" ## createNewSessionJni(): failure - inbound group session OOM"); + errorMessage = "inbound group session OOM"; + } + + if (errorMessage) + { + env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); } return (jlong)(intptr_t)sessionPtr; diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp index d1459ae..6e26071 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp @@ -48,7 +48,16 @@ JNIEXPORT jlong OLM_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject t LOGD("## createNewSessionJni(): IN"); OlmSession* accountPtr = initializeSessionMemory(); - LOGD(" ## createNewSessionJni(): success - accountPtr=%p (jlong)(intptr_t)accountPtr=%lld",accountPtr,(jlong)(intptr_t)accountPtr); + if (!accountPtr) + { + LOGE("## initNewAccount(): failure - init session OOM"); + env->ThrowNew(env->FindClass("java/lang/Exception"), "init session OOM"); + } + else + { + LOGD(" ## createNewSessionJni(): success - accountPtr=%p (jlong)(intptr_t)accountPtr=%lld",accountPtr,(jlong)(intptr_t)accountPtr); + } + return (jlong)(intptr_t)accountPtr; } @@ -70,31 +79,6 @@ JNIEXPORT void OLM_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz } } -/** -* Initialize a new session and return it to JAVA side.
-* Since a C prt is returned as a jlong, special care will be taken -* to make the cast (OlmSession* => jlong) platform independent. -* @return the initialized OlmSession* instance if init succeed, NULL otherwise -**/ -JNIEXPORT jlong OLM_SESSION_FUNC_DEF(initNewSessionJni)(JNIEnv *env, jobject thiz) -{ - LOGD("## initNewSessionJni(): OlmSession IN"); - - OlmSession* sessionPtr = initializeSessionMemory(); - - // init account memory allocation - if (!sessionPtr) - { - LOGE(" ## initNewSessionJni(): failure - init session OOM"); - } - else - { - LOGD(" ## initNewSessionJni(): success - OLM session created"); - } - - return (jlong)(intptr_t)sessionPtr; -} - // ********************************************************************* // ********************** OUTBOUND SESSION ***************************** // ********************************************************************* diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.h b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.h index ef92395..76f59c5 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.h +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.h @@ -29,7 +29,6 @@ extern "C" { // session creation/destruction JNIEXPORT void OLM_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz); -JNIEXPORT jlong OLM_SESSION_FUNC_DEF(initNewSessionJni)(JNIEnv *env, jobject thiz); JNIEXPORT jlong OLM_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz); // outbound session diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp index c4e6811..b26cfae 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp @@ -37,20 +37,21 @@ OlmUtility* initializeUtilityMemory() return utilityPtr; } -JNIEXPORT jlong OLM_UTILITY_FUNC_DEF(initUtilityJni)(JNIEnv *env, jobject thiz) +JNIEXPORT jlong OLM_UTILITY_FUNC_DEF(createUtilityJni)(JNIEnv *env, jobject thiz) { OlmUtility* utilityPtr = initializeUtilityMemory(); - LOGD("## initUtilityJni(): IN"); + LOGD("## createUtilityJni(): IN"); // init account memory allocation if (!utilityPtr) { - LOGE(" ## initUtilityJni(): failure - init OOM"); + LOGE(" ## createUtilityJni(): failure - init OOM"); + env->ThrowNew(env->FindClass("java/lang/Exception"), "init OOM"); } else { - LOGD(" ## initUtilityJni(): success"); + LOGD(" ## createUtilityJni(): success"); } return (jlong)(intptr_t)utilityPtr; diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.h b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.h index 16d9e7a..de4b290 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.h +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.h @@ -27,7 +27,7 @@ #ifdef __cplusplus extern "C" { #endif -JNIEXPORT jlong OLM_UTILITY_FUNC_DEF(initUtilityJni)(JNIEnv *env, jobject thiz); +JNIEXPORT jlong OLM_UTILITY_FUNC_DEF(createUtilityJni)(JNIEnv *env, jobject thiz); JNIEXPORT void OLM_UTILITY_FUNC_DEF(releaseUtilityJni)(JNIEnv *env, jobject thiz); JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(verifyEd25519SignatureJni)(JNIEnv *env, jobject thiz, jbyteArray aSignature, jbyteArray aKey, jbyteArray aMessage); JNIEXPORT jbyteArray OLM_UTILITY_FUNC_DEF(sha256Jni)(JNIEnv *env, jobject thiz, jbyteArray aMessageToHash); -- cgit v1.2.3