Commit 8f3d5bed authored by ylecollen's avatar ylecollen
Browse files

Fix tabulations mix and split block of variables when it is possible.

parent f4ae0d86
...@@ -26,17 +26,18 @@ using namespace AndroidOlmSdk; ...@@ -26,17 +26,18 @@ using namespace AndroidOlmSdk;
**/ **/
OlmAccount* initializeAccountMemory() OlmAccount* initializeAccountMemory()
{ {
OlmAccount* accountPtr = NULL;
size_t accountSize = olm_account_size(); size_t accountSize = olm_account_size();
OlmAccount* accountPtr = (OlmAccount*)malloc(accountSize);
if(NULL != (accountPtr=(OlmAccount*)malloc(accountSize))) if (accountPtr)
{ // init account object {
accountPtr = olm_account(accountPtr); // init account object
LOGD("## initializeAccountMemory(): success - OLM account size=%lu",static_cast<long unsigned int>(accountSize)); accountPtr = olm_account(accountPtr);
LOGD("## initializeAccountMemory(): success - OLM account size=%lu",static_cast<long unsigned int>(accountSize));
} }
else else
{ {
LOGE("## initializeAccountMemory(): failure - OOM"); LOGE("## initializeAccountMemory(): failure - OOM");
} }
return accountPtr; return accountPtr;
...@@ -60,24 +61,24 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(createNewAccountJni)(JNIEnv *env, jobject t ...@@ -60,24 +61,24 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(createNewAccountJni)(JNIEnv *env, jobject t
*/ */
JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(releaseAccountJni)(JNIEnv *env, jobject thiz) JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(releaseAccountJni)(JNIEnv *env, jobject thiz)
{ {
OlmAccount* accountPtr = NULL; LOGD("## releaseAccountJni(): IN");
LOGD("## releaseAccountJni(): IN"); OlmAccount* accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz);
if(NULL == (accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz))) if(!accountPtr)
{ {
LOGE(" ## releaseAccountJni(): failure - invalid Account ptr=NULL"); LOGE(" ## releaseAccountJni(): failure - invalid Account ptr=NULL");
} }
else else
{ {
LOGD(" ## releaseAccountJni(): accountPtr=%p",accountPtr); LOGD(" ## releaseAccountJni(): accountPtr=%p",accountPtr);
olm_clear_account(accountPtr); olm_clear_account(accountPtr);
LOGD(" ## releaseAccountJni(): IN"); LOGD(" ## releaseAccountJni(): IN");
// even if free(NULL) does not crash, logs are performed for debug purpose // even if free(NULL) does not crash, logs are performed for debug purpose
free(accountPtr); free(accountPtr);
LOGD(" ## releaseAccountJni(): OUT"); LOGD(" ## releaseAccountJni(): OUT");
} }
} }
/** /**
...@@ -88,24 +89,25 @@ JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(releaseAccountJni)(JNIEnv *env, jobject thiz ...@@ -88,24 +89,25 @@ 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(initNewAccountJni)(JNIEnv *env, jobject thiz)
{ {
OlmAccount *accountPtr = NULL; OlmAccount *accountPtr = initializeAccountMemory();
uint8_t *randomBuffPtr = NULL;
size_t accountRetCode;
size_t randomSize;
// init account memory allocation // init account memory allocation
if(NULL == (accountPtr = initializeAccountMemory())) if (!accountPtr)
{ {
LOGE("## initNewAccount(): failure - init account OOM"); LOGE("## initNewAccount(): failure - init account OOM");
} }
else else
{ {
// get random buffer size // get random buffer size
randomSize = olm_create_account_random_length(accountPtr); size_t randomSize = olm_create_account_random_length(accountPtr);
LOGD("## initNewAccount(): randomSize=%lu", static_cast<long unsigned int>(randomSize)); LOGD("## initNewAccount(): randomSize=%lu", static_cast<long unsigned int>(randomSize));
uint8_t *randomBuffPtr = NULL;
size_t accountRetCode;
// allocate random buffer // allocate random buffer
if((0!=randomSize) && !setRandomInBuffer(env, &randomBuffPtr, randomSize)) if ((0 != randomSize) && !setRandomInBuffer(env, &randomBuffPtr, randomSize))
{ {
LOGE("## initNewAccount(): failure - random buffer init"); LOGE("## initNewAccount(): failure - random buffer init");
} }
...@@ -113,25 +115,25 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(initNewAccountJni)(JNIEnv *env, jobject thi ...@@ -113,25 +115,25 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(initNewAccountJni)(JNIEnv *env, jobject thi
{ {
// create account // create account
accountRetCode = olm_create_account(accountPtr, (void*)randomBuffPtr, randomSize); accountRetCode = olm_create_account(accountPtr, (void*)randomBuffPtr, randomSize);
if(accountRetCode == olm_error()) {
if (accountRetCode == olm_error())
{
LOGE("## initNewAccount(): failure - account creation failed Msg=%s", olm_account_last_error(accountPtr)); LOGE("## initNewAccount(): failure - account creation failed Msg=%s", olm_account_last_error(accountPtr));
} }
LOGD("## initNewAccount(): success - OLM account created"); LOGD("## initNewAccount(): success - OLM account created");
LOGD("## initNewAccount(): success - accountPtr=%p (jlong)(intptr_t)accountPtr=%lld",accountPtr,(jlong)(intptr_t)accountPtr); LOGD("## initNewAccount(): success - accountPtr=%p (jlong)(intptr_t)accountPtr=%lld",accountPtr,(jlong)(intptr_t)accountPtr);
} }
}
if(NULL != randomBuffPtr) if (randomBuffPtr)
{ {
free(randomBuffPtr); free(randomBuffPtr);
}
} }
return (jlong)(intptr_t)accountPtr; return (jlong)(intptr_t)accountPtr;
} }
// ********************************************************************* // *********************************************************************
// ************************* IDENTITY KEYS API ************************* // ************************* IDENTITY KEYS API *************************
// ********************************************************************* // *********************************************************************
...@@ -142,34 +144,40 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(initNewAccountJni)(JNIEnv *env, jobject thi ...@@ -142,34 +144,40 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(initNewAccountJni)(JNIEnv *env, jobject thi
**/ **/
JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(identityKeysJni)(JNIEnv *env, jobject thiz) JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(identityKeysJni)(JNIEnv *env, jobject thiz)
{ {
OlmAccount* accountPtr = NULL;
size_t identityKeysLength;
uint8_t *identityKeysBytesPtr;
size_t keysResult;
jbyteArray byteArrayRetValue = NULL; jbyteArray byteArrayRetValue = NULL;
OlmAccount* accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz);
if(NULL == (accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz))) if (NULL == accountPtr)
{ {
LOGE("## identityKeys(): failure - invalid Account ptr=NULL"); LOGE("## identityKeys(): failure - invalid Account ptr=NULL");
} }
else else
{ {
LOGD("## identityKeys(): accountPtr =%p", accountPtr); LOGD("## identityKeys(): accountPtr =%p", accountPtr);
// identity keys allocation // identity keys allocation
identityKeysLength = olm_account_identity_keys_length(accountPtr); size_t identityKeysLength = olm_account_identity_keys_length(accountPtr);
if(NULL == (identityKeysBytesPtr=(uint8_t*)malloc(identityKeysLength))) uint8_t *identityKeysBytesPtr = (uint8_t*)malloc(identityKeysLength);
if (!identityKeysBytesPtr)
{ {
LOGE("## identityKeys(): failure - identity keys array OOM"); LOGE("## identityKeys(): failure - identity keys array OOM");
} }
else else
{ // retrieve key pairs in identityKeysBytesPtr {
keysResult = olm_account_identity_keys(accountPtr, identityKeysBytesPtr, identityKeysLength); // retrieve key pairs in identityKeysBytesPtr
if(keysResult == olm_error()) { size_t keysResult = olm_account_identity_keys(accountPtr, identityKeysBytesPtr, identityKeysLength);
if(keysResult == olm_error())
{
LOGE("## identityKeys(): failure - error getting identity keys Msg=%s",(const char *)olm_account_last_error(accountPtr)); LOGE("## identityKeys(): failure - error getting identity keys Msg=%s",(const char *)olm_account_last_error(accountPtr));
} }
else else
{ // allocate the byte array to be returned to java {
if(NULL == (byteArrayRetValue=env->NewByteArray(identityKeysLength))) // allocate the byte array to be returned to java
byteArrayRetValue = env->NewByteArray(identityKeysLength);
if(NULL == byteArrayRetValue)
{ {
LOGE("## identityKeys(): failure - return byte array OOM"); LOGE("## identityKeys(): failure - return byte array OOM");
} }
...@@ -196,10 +204,10 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(identityKeysJni)(JNIEnv *env, jobject ...@@ -196,10 +204,10 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(identityKeysJni)(JNIEnv *env, jobject
**/ **/
JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(maxOneTimeKeysJni)(JNIEnv *env, jobject thiz) JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(maxOneTimeKeysJni)(JNIEnv *env, jobject thiz)
{ {
OlmAccount* accountPtr = NULL; OlmAccount* accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz);
size_t maxKeys = -1; size_t maxKeys = -1;
if(NULL == (accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz))) if (!accountPtr)
{ {
LOGE("## maxOneTimeKey(): failure - invalid Account ptr=NULL"); LOGE("## maxOneTimeKey(): failure - invalid Account ptr=NULL");
} }
...@@ -207,6 +215,7 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(maxOneTimeKeysJni)(JNIEnv *env, jobject thi ...@@ -207,6 +215,7 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(maxOneTimeKeysJni)(JNIEnv *env, jobject thi
{ {
maxKeys = olm_account_max_number_of_one_time_keys(accountPtr); maxKeys = olm_account_max_number_of_one_time_keys(accountPtr);
} }
LOGD("## maxOneTimeKey(): Max keys=%lu", static_cast<long unsigned int>(maxKeys)); LOGD("## maxOneTimeKey(): Max keys=%lu", static_cast<long unsigned int>(maxKeys));
return (jlong)maxKeys; return (jlong)maxKeys;
...@@ -219,23 +228,22 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(maxOneTimeKeysJni)(JNIEnv *env, jobject thi ...@@ -219,23 +228,22 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(maxOneTimeKeysJni)(JNIEnv *env, jobject thi
**/ **/
JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeysJni)(JNIEnv *env, jobject thiz, jint aNumberOfKeys) JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeysJni)(JNIEnv *env, jobject thiz, jint aNumberOfKeys)
{ {
OlmAccount *accountPtr = NULL; OlmAccount *accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz);
uint8_t *randomBufferPtr = NULL;
jint retCode = ERROR_CODE_KO; jint retCode = ERROR_CODE_KO;
size_t randomLength;
size_t result;
if(NULL == (accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz))) if (!accountPtr)
{ {
LOGE("## generateOneTimeKeysJni(): failure - invalid Account ptr"); LOGE("## generateOneTimeKeysJni(): failure - invalid Account ptr");
} }
else else
{ // keys memory allocation {
randomLength = olm_account_generate_one_time_keys_random_length(accountPtr, (size_t)aNumberOfKeys); // keys memory allocation
size_t randomLength = olm_account_generate_one_time_keys_random_length(accountPtr, (size_t)aNumberOfKeys);
LOGD("## generateOneTimeKeysJni(): randomLength=%lu", static_cast<long unsigned int>(randomLength)); LOGD("## generateOneTimeKeysJni(): randomLength=%lu", static_cast<long unsigned int>(randomLength));
if((0!=randomLength) && !setRandomInBuffer(env, &randomBufferPtr, randomLength)) uint8_t *randomBufferPtr = NULL;
if ( (0!=randomLength) && !setRandomInBuffer(env, &randomBufferPtr, randomLength))
{ {
LOGE("## generateOneTimeKeysJni(): failure - random buffer init"); LOGE("## generateOneTimeKeysJni(): failure - random buffer init");
} }
...@@ -244,8 +252,9 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeysJni)(JNIEnv *env, jobject ...@@ -244,8 +252,9 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeysJni)(JNIEnv *env, jobject
LOGD("## generateOneTimeKeysJni(): accountPtr =%p aNumberOfKeys=%d",accountPtr, aNumberOfKeys); LOGD("## generateOneTimeKeysJni(): accountPtr =%p aNumberOfKeys=%d",accountPtr, aNumberOfKeys);
// retrieve key pairs in keysBytesPtr // retrieve key pairs in keysBytesPtr
result = olm_account_generate_one_time_keys(accountPtr, (size_t)aNumberOfKeys, (void*)randomBufferPtr, randomLength); size_t result = olm_account_generate_one_time_keys(accountPtr, (size_t)aNumberOfKeys, (void*)randomBufferPtr, randomLength);
if(result == olm_error()) {
if (result == olm_error()) {
LOGE("## generateOneTimeKeysJni(): failure - error generating one time keys Msg=%s",(const char *)olm_account_last_error(accountPtr)); LOGE("## generateOneTimeKeysJni(): failure - error generating one time keys Msg=%s",(const char *)olm_account_last_error(accountPtr));
} }
else else
...@@ -254,11 +263,12 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeysJni)(JNIEnv *env, jobject ...@@ -254,11 +263,12 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeysJni)(JNIEnv *env, jobject
LOGD("## generateOneTimeKeysJni(): success - result=%lu", static_cast<long unsigned int>(result)); LOGD("## generateOneTimeKeysJni(): success - result=%lu", static_cast<long unsigned int>(result));
} }
} }
}
if(NULL != randomBufferPtr)
{ if (randomBufferPtr)
free(randomBufferPtr); {
free(randomBufferPtr);
}
} }
return retCode; return retCode;
...@@ -271,34 +281,38 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeysJni)(JNIEnv *env, jobject ...@@ -271,34 +281,38 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeysJni)(JNIEnv *env, jobject
**/ **/
JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(oneTimeKeysJni)(JNIEnv *env, jobject thiz) JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(oneTimeKeysJni)(JNIEnv *env, jobject thiz)
{ {
OlmAccount* accountPtr = NULL;
size_t keysLength;
uint8_t *keysBytesPtr;
size_t keysResult;
jbyteArray byteArrayRetValue = NULL; jbyteArray byteArrayRetValue = NULL;
OlmAccount* accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz);
LOGD("## oneTimeKeysJni(): IN"); LOGD("## oneTimeKeysJni(): IN");
if(NULL == (accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz))) if (!accountPtr)
{ {
LOGE("## oneTimeKeysJni(): failure - invalid Account ptr"); LOGE("## oneTimeKeysJni(): failure - invalid Account ptr");
} }
else else
{ // keys memory allocation {
keysLength = olm_account_one_time_keys_length(accountPtr); // keys memory allocation
if(NULL == (keysBytesPtr=(uint8_t *)malloc(keysLength*sizeof(uint8_t)))) size_t keysLength = olm_account_one_time_keys_length(accountPtr);
uint8_t *keysBytesPtr = (uint8_t *)malloc(keysLength*sizeof(uint8_t));
if (!keysBytesPtr)
{ {
LOGE("## oneTimeKeysJni(): failure - one time keys array OOM"); LOGE("## oneTimeKeysJni(): failure - one time keys array OOM");
} }
else else
{ // retrieve key pairs in keysBytesPtr {
keysResult = olm_account_one_time_keys(accountPtr, keysBytesPtr, keysLength); // retrieve key pairs in keysBytesPtr
size_t keysResult = olm_account_one_time_keys(accountPtr, keysBytesPtr, keysLength);
if(keysResult == olm_error()) { if(keysResult == olm_error()) {
LOGE("## oneTimeKeysJni(): failure - error getting one time keys Msg=%s",(const char *)olm_account_last_error(accountPtr)); LOGE("## oneTimeKeysJni(): failure - error getting one time keys Msg=%s",(const char *)olm_account_last_error(accountPtr));
} }
else else
{ // allocate the byte array to be returned to java {
if(NULL == (byteArrayRetValue=env->NewByteArray(keysLength))) // allocate the byte array to be returned to java
byteArrayRetValue = env->NewByteArray(keysLength);
if (!byteArrayRetValue)
{ {
LOGE("## oneTimeKeysJni(): failure - return byte array OOM"); LOGE("## oneTimeKeysJni(): failure - return byte array OOM");
} }
...@@ -327,20 +341,20 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(removeOneTimeKeysForSessionJni)(JNIEnv *env, ...@@ -327,20 +341,20 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(removeOneTimeKeysForSessionJni)(JNIEnv *env,
jint retCode = ERROR_CODE_KO; jint retCode = ERROR_CODE_KO;
OlmAccount* accountPtr = NULL; OlmAccount* accountPtr = NULL;
OlmSession* sessionPtr = (OlmSession*)aNativeOlmSessionId; OlmSession* sessionPtr = (OlmSession*)aNativeOlmSessionId;
size_t result;
if(NULL == sessionPtr) if (!sessionPtr)
{ {
LOGE("## removeOneTimeKeysForSessionJni(): failure - invalid session ptr"); LOGE("## removeOneTimeKeysForSessionJni(): failure - invalid session ptr");
} }
else if(NULL == (accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz))) else if(!(accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz)))
{ {
LOGE("## removeOneTimeKeysForSessionJni(): failure - invalid account ptr"); LOGE("## removeOneTimeKeysForSessionJni(): failure - invalid account ptr");
} }
else else
{ {
result = olm_remove_one_time_keys(accountPtr, sessionPtr); size_t result = olm_remove_one_time_keys(accountPtr, sessionPtr);
if(result == olm_error())
if (result == olm_error())
{ // the account doesn't have any matching "one time keys".. { // the account doesn't have any matching "one time keys"..
LOGW("## removeOneTimeKeysForSessionJni(): failure - removing one time keys Msg=%s",(const char *)olm_account_last_error(accountPtr)); LOGW("## removeOneTimeKeysForSessionJni(): failure - removing one time keys Msg=%s",(const char *)olm_account_last_error(accountPtr));
...@@ -363,17 +377,17 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(removeOneTimeKeysForSessionJni)(JNIEnv *env, ...@@ -363,17 +377,17 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(removeOneTimeKeysForSessionJni)(JNIEnv *env,
JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env, jobject thiz) JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env, jobject thiz)
{ {
jint retCode = ERROR_CODE_OK; jint retCode = ERROR_CODE_OK;
OlmAccount* accountPtr = NULL; OlmAccount* accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz);
size_t result;
if(NULL == (accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz))) if (!accountPtr)
{ {
LOGE("## markOneTimeKeysAsPublishedJni(): failure - invalid account ptr"); LOGE("## markOneTimeKeysAsPublishedJni(): failure - invalid account ptr");
retCode = ERROR_CODE_KO; retCode = ERROR_CODE_KO;
} }
else else
{ {
result = olm_account_mark_keys_as_published(accountPtr); size_t result = olm_account_mark_keys_as_published(accountPtr);
if(result == olm_error()) if(result == olm_error())
{ {
LOGW("## markOneTimeKeysAsPublishedJni(): failure - Msg=%s",(const char *)olm_account_last_error(accountPtr)); LOGW("## markOneTimeKeysAsPublishedJni(): failure - Msg=%s",(const char *)olm_account_last_error(accountPtr));
...@@ -396,64 +410,63 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env, ...@@ -396,64 +410,63 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env,
**/ **/
JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aMessage) JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aMessage)
{ {
OlmAccount* accountPtr = NULL; OlmAccount* accountPtr = NULL;
size_t signatureLength; jstring signedMsgRetValue = NULL;
void* signedMsgPtr;
size_t resultSign; if (!aMessage)
jstring signedMsgRetValue = NULL; {
LOGE("## signMessageJni(): failure - invalid aMessage param");
if(NULL == aMessage) }
{ else if(NULL == (accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz)))
LOGE("## signMessageJni(): failure - invalid aMessage param"); {
} LOGE("## signMessageJni(): failure - invalid account ptr");
else if(NULL == (accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz)))
{
LOGE("## signMessageJni(): failure - invalid account ptr");
}
else
{
int messageLength = env->GetArrayLength(aMessage);
jbyte* messageToSign = env->GetByteArrayElements(aMessage, NULL);
// signature memory allocation
signatureLength = olm_account_signature_length(accountPtr);
if(NULL == (signedMsgPtr = (void*)malloc((signatureLength+1)*sizeof(uint8_t))))
{
LOGE("## signMessageJni(): failure - signature allocation OOM");
} }
else else
{ {
// sign message int messageLength = env->GetArrayLength(aMessage);
resultSign = olm_account_sign(accountPtr, jbyte* messageToSign = env->GetByteArrayElements(aMessage, NULL);
// signature memory allocation
size_t signatureLength = olm_account_signature_length(accountPtr);
void* signedMsgPtr = malloc((signatureLength+1)*sizeof(uint8_t));
if (!signedMsgPtr)
{
LOGE("## signMessageJni(): failure - signature allocation OOM");
}
else
{
// sign message
size_t resultSign = olm_account_sign(accountPtr,
(void*)messageToSign, (void*)messageToSign,
(size_t)messageLength, (size_t)messageLength,
signedMsgPtr, signedMsgPtr,
signatureLength); signatureLength);
if(resultSign == olm_error())
{
LOGE("## signMessageJni(): failure - error signing message Msg=%s",(const char *)olm_account_last_error(accountPtr));
}
else
{
// info: signatureLength is always equal to resultSign
(static_cast<char*>(signedMsgPtr))[signatureLength] = static_cast<char>('\0');
// convert to jstring
signedMsgRetValue = env->NewStringUTF((const char*)signedMsgPtr); // UTF8
LOGD("## signMessageJni(): success - retCode=%lu signatureLength=%lu", static_cast<long unsigned int>(resultSign), static_cast<long unsigned int>(signatureLength));
}
free(signedMsgPtr); if (resultSign == olm_error())
} {
LOGE("## signMessageJni(): failure - error signing message Msg=%s",(const char *)olm_account_last_error(accountPtr));
}
else
{
// info: signatureLength is always equal to resultSign
(static_cast<char*>(signedMsgPtr))[signatureLength] = static_cast<char>('\0');
// convert to jstring
signedMsgRetValue = env->NewStringUTF((const char*)signedMsgPtr); // UTF8
LOGD("## signMessageJni(): success - retCode=%lu signatureLength=%lu", static_cast<long unsigned int>(resultSign), static_cast<long unsigned int>(signatureLength));
}
// release messageToSign free(signedMsgPtr);
if (messageToSign) }
{
env->ReleaseByteArrayElements(aMessage, messageToSign, JNI_ABORT); // release messageToSign