formatting and code style only Release_2.2.0-RC1
authorheck <heck@pep.foundation>
Mon, 30 Nov 2020 23:57:34 +0100
changeset 973008a60c555e8
parent 972 78534326aafb
child 974 7cfa63037efd
formatting and code style only
src/codegen/gen_cpp_Engine.ysl2
src/codegen/gen_cpp_Message.ysl2
src/cxx/basic_api.cc
src/cxx/foundation_pEp_jniadapter_AbstractEngine.cc
src/cxx/foundation_pEp_jniadapter__Blob.cc
src/cxx/identity_api.cc
src/cxx/jniutils.cc
src/cxx/jniutils.hh
src/cxx/passphrase_callback.hh
src/cxx/passphrase_callback.hxx
     1.1 --- a/src/codegen/gen_cpp_Engine.ysl2	Mon Nov 30 22:05:23 2020 +0100
     1.2 +++ b/src/codegen/gen_cpp_Engine.ysl2	Mon Nov 30 23:57:34 2020 +0100
     1.3 @@ -51,10 +51,8 @@
     1.4          }
     1.5  
     1.6          ||
     1.7 -        JNIEXPORT «$CretType» JNICALL «$mangled»(
     1.8 -                JNIEnv *env,
     1.9 -                jobject obj`apply "parm[in|inout]", mode=sig`
    1.10 -            )
    1.11 +        JNIEXPORT «$CretType» JNICALL «$mangled»(JNIEnv *env,
    1.12 +                jobject obj`apply "parm[in|inout]", mode=sig`)
    1.13          {
    1.14              std::mutex *mutex_local = nullptr;
    1.15              {
    1.16 @@ -83,13 +81,11 @@
    1.17              }
    1.18          }
    1.19          ||
    1.20 -            if ((status > PEP_STATUS_OK && status < PEP_UNENCRYPTED) |`> |`
    1.21 -                    status < PEP_STATUS_OK |`> |`
    1.22 -                    status >= PEP_TRUSTWORD_NOT_FOUND) {
    1.23 +
    1.24 +            if ((status > PEP_STATUS_OK && status < PEP_UNENCRYPTED) |`> |` status < PEP_STATUS_OK |`> |` status >= PEP_TRUSTWORD_NOT_FOUND) {
    1.25                  throw_pEp_Exception(env, status);
    1.26                  return «$CretDefault»;
    1.27              }
    1.28 -
    1.29          ||
    1.30  
    1.31          apply "parm[returns|creates]", mode=return;
    1.32 @@ -188,9 +184,9 @@
    1.33                  }
    1.34                  ||
    1.35                  «$jnitype» «$name»_ = NULL;
    1.36 -                if (_«$name»)
    1.37 +                if (_«$name») {
    1.38                      «$name»_ = «$from_type»(env, _«$name»);
    1.39 -
    1.40 +                }
    1.41                  ||
    1.42              }
    1.43              when "ancestor::namespace/child::enum[@name=$type]" {
    1.44 @@ -200,14 +196,12 @@
    1.45                  {
    1.46                      jclass clazz_«$ljtype» = findClass(env, "foundation/pEp/jniadapter/«$jtype»");
    1.47                      assert(clazz_«$ljtype»);
    1.48 -                    jmethodID method_values = env->GetStaticMethodID(clazz_«$ljtype», "values",
    1.49 -                            "()[Lfoundation/pEp/jniadapter/«$jtype»;");
    1.50 +                    jmethodID method_values = env->GetStaticMethodID(clazz_«$ljtype», "values", "()[Lfoundation/pEp/jniadapter/«$jtype»;");
    1.51                      assert(method_values);
    1.52                      jfieldID field_value = env->GetFieldID(clazz_«$ljtype», "value", "I");
    1.53                      assert(field_value);
    1.54  
    1.55 -                    jobjectArray values = static_cast<jobjectArray>(env->CallStaticObjectMethod(clazz_«$ljtype»,
    1.56 -                            method_values));
    1.57 +                    jobjectArray values = static_cast<jobjectArray>(env->CallStaticObjectMethod(clazz_«$ljtype», method_values));
    1.58                      assert(values);
    1.59                      if (env->ExceptionCheck()) {
    1.60                          return nullptr; // handle exception in Java
     2.1 --- a/src/codegen/gen_cpp_Message.ysl2	Mon Nov 30 22:05:23 2020 +0100
     2.2 +++ b/src/codegen/gen_cpp_Message.ysl2	Mon Nov 30 23:57:34 2020 +0100
     2.3 @@ -25,28 +25,28 @@
     2.4              using namespace std;
     2.5  
     2.6              namespace pEp {
     2.7 -                namespace JNIAdapter {
     2.8 -                    static ::«@name» *«@name»_ptr(JNIEnv *env, jobject me) {
     2.9 -                        jfieldID handle;
    2.10 +            namespace JNIAdapter {
    2.11 +            static ::«@name» *«@name»_ptr(JNIEnv *env, jobject me) {
    2.12 +                jfieldID handle;
    2.13  
    2.14 -                        try {
    2.15 -                            handle = getFieldID(env, "foundation/pEp/jniadapter/«$jname»", "handle", "J");
    2.16 -                        }
    2.17 -                        catch (std::exception& ex) {
    2.18 -                            assert(0);
    2.19 -                            return NULL;
    2.20 -                        }
    2.21 +                try {
    2.22 +                    handle = getFieldID(env, "foundation/pEp/jniadapter/«$jname»", "handle", "J");
    2.23 +                } catch (std::exception& ex) {
    2.24 +                    assert(0);
    2.25 +                    return NULL;
    2.26 +                }
    2.27 +                return reinterpret_cast<::«@name» *>(env->GetLongField(me, handle));
    2.28 +            }
    2.29  
    2.30 -                        return reinterpret_cast<::«@name» *>(env->GetLongField(me, handle));
    2.31 -                    }
    2.32 -                };
    2.33 +            };
    2.34              };
    2.35  
    2.36              extern "C" {
    2.37  
    2.38              using namespace pEp::JNIAdapter;
    2.39  
    2.40 -            JNIEXPORT jlong JNICALL Java_foundation_pEp_jniadapter_«$jname»_init(JNIEnv *env, jobject obj)
    2.41 +            JNIEXPORT jlong JNICALL Java_foundation_pEp_jniadapter_«$jname»_init(JNIEnv *env,
    2.42 +                    jobject obj)
    2.43              {
    2.44                  pEpLog("called");
    2.45                  ::«@name» * _obj = ::new_«@name»(PEP_dir_incoming);
    2.46 @@ -57,7 +57,9 @@
    2.47                  return reinterpret_cast<jlong>(_obj);
    2.48              }
    2.49  
    2.50 -            JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_«$jname»_release(JNIEnv *env, jobject obj, jlong value)
    2.51 +            JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_«$jname»_release(JNIEnv *env,
    2.52 +                    jobject obj,
    2.53 +                    jlong value)
    2.54              {
    2.55                  pEpLog("called");
    2.56                  if (value) {
    2.57 @@ -66,7 +68,9 @@
    2.58                  }
    2.59              }
    2.60  
    2.61 -            JNIEXPORT jlong JNICALL Java_foundation_pEp_jniadapter_«$jname»__1«$jname»(JNIEnv *env, jobject msg, jbyteArray mime_text)
    2.62 +            JNIEXPORT jlong JNICALL Java_foundation_pEp_jniadapter_«$jname»__1«$jname»(JNIEnv *env,
    2.63 +                    jobject msg,
    2.64 +                    jbyteArray mime_text)
    2.65              {
    2.66                  pEpLog("called");
    2.67                  char *_mime_text = to_string(env, mime_text);
    2.68 @@ -74,21 +78,26 @@
    2.69  
    2.70                  ::«@name» *_msg = nullptr;
    2.71                  PEP_STATUS status = mime_decode_«@name»(_mime_text, _size, &_msg, NULL);
    2.72 -                if (status)
    2.73 +                if (status) {
    2.74                      throw_pEp_Exception(env, status);
    2.75 +                }
    2.76 +
    2.77                  return reinterpret_cast<jlong>(_msg);
    2.78              }
    2.79  
    2.80 -            JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_«$jname»__1encodeMIME(JNIEnv *env, jobject msg)
    2.81 +            JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_«$jname»__1encodeMIME(JNIEnv *env,
    2.82 +                    jobject msg)
    2.83              {
    2.84                  pEpLog("called");
    2.85                  «@name» *_obj = «@name»_ptr(env, msg);
    2.86                  char *mime_text = nullptr;
    2.87                  PEP_STATUS status = ::mime_encode_«@name»(_obj, false, &mime_text, false);
    2.88 -                if (status)
    2.89 +                if (status) {
    2.90                      throw_pEp_Exception(env, status);
    2.91 +                }
    2.92                  jbyteArray result = from_string(env, mime_text);
    2.93                  free(mime_text);
    2.94 +
    2.95                  return result;
    2.96              }
    2.97  
    2.98 @@ -134,7 +143,8 @@
    2.99              with "name", "concat('set', $jname)";
   2.100          }
   2.101          ||
   2.102 -        JNIEXPORT `call "jni_type" with "type", "name(.)"` JNICALL «$getname»(JNIEnv *env, jobject obj)
   2.103 +        JNIEXPORT `call "jni_type" with "type", "name(.)"` JNICALL «$getname»(JNIEnv *env,
   2.104 +                jobject obj)
   2.105          {
   2.106              pEpLog("called");
   2.107              «$name» *_obj = «$name»_ptr(env, obj);
   2.108 @@ -148,7 +158,10 @@
   2.109          ||
   2.110          }
   2.111  
   2.112 -        JNIEXPORT void JNICALL «$setname»(JNIEnv *env, jobject obj, `call "jni_type" with "type", "name(.)"` value)
   2.113 +        JNIEXPORT void JNICALL «$setname»(JNIEnv *env,
   2.114 +                jobject obj,
   2.115 +                `call "jni_type" with "type", "name(.)"`
   2.116 +                value)
   2.117          {
   2.118              pEpLog("called");
   2.119              «$name» *_obj = «$name»_ptr(env, obj);
   2.120 @@ -171,7 +184,7 @@
   2.121                          |> free_«$free»(_obj->«$cname»);
   2.122                      }
   2.123                  }
   2.124 -                |> if(value){
   2.125 +                |> if(value) {
   2.126                  choose {
   2.127                      when "$type = 'string'" {
   2.128                          |>> _obj->«$cname» = to_«$type»(env, value);
   2.129 @@ -180,9 +193,9 @@
   2.130                          |>> _obj->«$cname» = to_«$type»(env, value);
   2.131                      }
   2.132                  }
   2.133 -                |> }else{
   2.134 +                |> } else {
   2.135                  |>     _obj->«$cname» = NULL;
   2.136 -                |> }
   2.137 +                |> }
   2.138              }
   2.139          }
   2.140          ||
     3.1 --- a/src/cxx/basic_api.cc	Mon Nov 30 22:05:23 2020 +0100
     3.2 +++ b/src/cxx/basic_api.cc	Mon Nov 30 23:57:34 2020 +0100
     3.3 @@ -12,15 +12,14 @@
     3.4  #include "passphrase_callback.hh"
     3.5  
     3.6  extern "C" {
     3.7 -    using namespace pEp::JNIAdapter;
     3.8 -    using pEp::Adapter::session;
     3.9 -    using pEp::passphrase_cache;
    3.10  
    3.11 -JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_Engine__1trustwords(
    3.12 -        JNIEnv *env,
    3.13 +using namespace pEp::JNIAdapter;
    3.14 +using pEp::Adapter::session;
    3.15 +using pEp::passphrase_cache;
    3.16 +
    3.17 +JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_Engine__1trustwords(JNIEnv *env,
    3.18          jobject obj,
    3.19 -        jobject ident
    3.20 -    )
    3.21 +        jobject ident)
    3.22  {
    3.23      std::mutex *mutex_local = nullptr;
    3.24      {
    3.25 @@ -37,10 +36,11 @@
    3.26      PEP_STATUS status = PEP_STATUS_OK;
    3.27  
    3.28      if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
    3.29 -        if (_ident->me)
    3.30 +        if (_ident->me) {
    3.31              status = passphraseWrap(::myself, session(), _ident);
    3.32 -        else
    3.33 +        } else {
    3.34              status = passphraseWrap(::update_identity, session(), _ident);
    3.35 +        }
    3.36      }
    3.37  
    3.38      if (status != PEP_STATUS_OK) {
    3.39 @@ -54,13 +54,13 @@
    3.40      }
    3.41  
    3.42      const char *lang;
    3.43 -    if (_ident->lang[0])
    3.44 +    if (_ident->lang[0]) {
    3.45          lang = _ident->lang;
    3.46 -    else
    3.47 +    } else {
    3.48          lang = "en";
    3.49 +    }
    3.50  
    3.51 -    status = passphraseWrap(::trustwords,
    3.52 -            session(), static_cast<const char*>(_ident->fpr), lang, &words, &wsize, 10);
    3.53 +    status = passphraseWrap(::trustwords, session(), static_cast<const char *>(_ident->fpr), lang, &words, &wsize, 10);
    3.54  
    3.55      if (status != PEP_STATUS_OK) {
    3.56          throw_pEp_Exception(env, status);
    3.57 @@ -70,11 +70,9 @@
    3.58      return from_string(env, words);
    3.59  }
    3.60  
    3.61 -JNIEXPORT jobject JNICALL Java_foundation_pEp_jniadapter_Engine__1myself(
    3.62 -        JNIEnv *env,
    3.63 +JNIEXPORT jobject JNICALL Java_foundation_pEp_jniadapter_Engine__1myself(JNIEnv *env,
    3.64          jobject obj,
    3.65 -        jobject ident
    3.66 -    )
    3.67 +        jobject ident)
    3.68  {
    3.69      std::mutex *mutex_local = nullptr;
    3.70      {
    3.71 @@ -96,11 +94,9 @@
    3.72      return from_identity(env, _ident);
    3.73  }
    3.74  
    3.75 -JNIEXPORT jobject JNICALL Java_foundation_pEp_jniadapter_Engine__1updateIdentity(
    3.76 -        JNIEnv *env,
    3.77 +JNIEXPORT jobject JNICALL Java_foundation_pEp_jniadapter_Engine__1updateIdentity(JNIEnv *env,
    3.78          jobject obj,
    3.79 -        jobject ident
    3.80 -    )
    3.81 +        jobject ident)
    3.82  {
    3.83      std::mutex *mutex_local = nullptr;
    3.84      {
    3.85 @@ -111,18 +107,15 @@
    3.86      std::lock_guard<std::mutex> l(*mutex_local);
    3.87  
    3.88      pEp_identity *_ident = to_identity(env, ident);
    3.89 -
    3.90      passphraseWrap(::update_identity, session(), _ident);
    3.91  
    3.92      return from_identity(env, _ident);
    3.93  }
    3.94  
    3.95 -JNIEXPORT jobject JNICALL Java_foundation_pEp_jniadapter_Engine__1setOwnKey(
    3.96 -        JNIEnv *env,
    3.97 +JNIEXPORT jobject JNICALL Java_foundation_pEp_jniadapter_Engine__1setOwnKey(JNIEnv *env,
    3.98          jobject obj,
    3.99          jobject ident,
   3.100 -        jbyteArray fpr
   3.101 -    )
   3.102 +        jbyteArray fpr)
   3.103  {
   3.104      std::mutex *mutex_local = nullptr;
   3.105      {
   3.106 @@ -146,11 +139,9 @@
   3.107      return from_identity(env, _ident);
   3.108  }
   3.109  
   3.110 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1keyMistrusted(
   3.111 -        JNIEnv *env,
   3.112 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1keyMistrusted(JNIEnv *env,
   3.113          jobject obj,
   3.114 -        jobject ident
   3.115 -    )
   3.116 +        jobject ident)
   3.117  {
   3.118      std::mutex *mutex_local = nullptr;
   3.119      {
   3.120 @@ -184,11 +175,9 @@
   3.121      passphraseWrap(::key_mistrusted, session(), _ident);
   3.122  }
   3.123  
   3.124 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1keyResetTrust(
   3.125 -        JNIEnv *env,
   3.126 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1keyResetTrust(JNIEnv *env,
   3.127          jobject obj,
   3.128 -        jobject ident
   3.129 -    )
   3.130 +        jobject ident)
   3.131  {
   3.132      std::mutex *mutex_local = nullptr;
   3.133      {
   3.134 @@ -203,10 +192,11 @@
   3.135      PEP_STATUS status = PEP_STATUS_OK;
   3.136  
   3.137      if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
   3.138 -        if (_ident->me)
   3.139 +        if (_ident->me) {
   3.140              status = passphraseWrap(::myself, session(), _ident);
   3.141 -        else
   3.142 +        } else {
   3.143              status = passphraseWrap(::update_identity, session(), _ident);
   3.144 +        }
   3.145      }
   3.146  
   3.147      if (status != PEP_STATUS_OK) {
   3.148 @@ -222,11 +212,9 @@
   3.149      passphraseWrap(::key_reset_trust, session(), _ident);
   3.150  }
   3.151  
   3.152 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1trustPersonalKey(
   3.153 -        JNIEnv *env,
   3.154 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1trustPersonalKey(JNIEnv *env,
   3.155          jobject obj,
   3.156 -        jobject ident
   3.157 -    )
   3.158 +        jobject ident)
   3.159  {
   3.160      std::mutex *mutex_local = nullptr;
   3.161      {
   3.162 @@ -241,10 +229,11 @@
   3.163      PEP_STATUS status = PEP_STATUS_OK;
   3.164  
   3.165      if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
   3.166 -        if (_ident->me)
   3.167 +        if (_ident->me) {
   3.168              status = passphraseWrap(::myself, session(), _ident);
   3.169 -        else
   3.170 +        } else {
   3.171              status = passphraseWrap(::update_identity, session(), _ident);
   3.172 +        }
   3.173      }
   3.174  
   3.175      if (status != PEP_STATUS_OK) {
   3.176 @@ -260,11 +249,9 @@
   3.177      passphraseWrap(::trust_personal_key, session(), _ident);
   3.178  }
   3.179  
   3.180 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1trustOwnKey(
   3.181 -        JNIEnv *env,
   3.182 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1trustOwnKey(JNIEnv *env,
   3.183          jobject obj,
   3.184 -        jobject ident
   3.185 -    )
   3.186 +        jobject ident)
   3.187  {
   3.188      std::mutex *mutex_local = nullptr;
   3.189      {
   3.190 @@ -284,11 +271,9 @@
   3.191      passphraseWrap(::trust_own_key, session(), _ident);
   3.192  }
   3.193  
   3.194 -JNIEXPORT jobject JNICALL Java_foundation_pEp_jniadapter_Engine__1importKey(
   3.195 -        JNIEnv *env,
   3.196 +JNIEXPORT jobject JNICALL Java_foundation_pEp_jniadapter_Engine__1importKey(JNIEnv *env,
   3.197          jobject obj,
   3.198 -        jbyteArray key
   3.199 -    )
   3.200 +        jbyteArray key)
   3.201  {
   3.202      std::mutex *mutex_local = nullptr;
   3.203      {
   3.204 @@ -301,7 +286,7 @@
   3.205      size_t _size = static_cast<size_t>(env->GetArrayLength(key));
   3.206      jbyte* _key = env->GetByteArrayElements(key, NULL);
   3.207  
   3.208 -    if(_key == NULL){
   3.209 +    if(_key == NULL) {
   3.210          throw_pEp_Exception(env, PEP_OUT_OF_MEMORY);
   3.211          return NULL;
   3.212      }
   3.213 @@ -324,11 +309,9 @@
   3.214  }
   3.215  
   3.216  
   3.217 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1config_1passive_1mode(
   3.218 -        JNIEnv *env,
   3.219 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1config_1passive_1mode(JNIEnv *env,
   3.220          jobject obj,
   3.221 -        jboolean enable
   3.222 -    )
   3.223 +        jboolean enable)
   3.224  {
   3.225      std::mutex *mutex_local = nullptr;
   3.226      {
   3.227 @@ -342,11 +325,9 @@
   3.228  }
   3.229  
   3.230  
   3.231 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1config_1unencrypted_1subject(
   3.232 -        JNIEnv *env,
   3.233 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1config_1unencrypted_1subject(JNIEnv *env,
   3.234          jobject obj,
   3.235 -        jboolean enable
   3.236 -    )
   3.237 +        jboolean enable)
   3.238  {
   3.239      std::mutex *mutex_local = nullptr;
   3.240      {
   3.241 @@ -359,8 +340,7 @@
   3.242      ::config_unencrypted_subject(session(), static_cast<bool>(enable));
   3.243  }
   3.244  
   3.245 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1blacklist_1add(
   3.246 -        JNIEnv *env,
   3.247 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1blacklist_1add(JNIEnv *env,
   3.248          jobject obj,
   3.249          jbyteArray fpr
   3.250      )
   3.251 @@ -385,14 +365,11 @@
   3.252          throw_pEp_Exception(env, status);
   3.253          return;
   3.254      }
   3.255 -
   3.256  }
   3.257  
   3.258 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1blacklist_1delete(
   3.259 -        JNIEnv *env,
   3.260 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1blacklist_1delete(JNIEnv *env,
   3.261          jobject obj,
   3.262 -        jbyteArray fpr
   3.263 -    )
   3.264 +        jbyteArray fpr)
   3.265  {
   3.266      std::mutex *mutex_local = nullptr;
   3.267      {
   3.268 @@ -417,11 +394,9 @@
   3.269  
   3.270  }
   3.271  
   3.272 -JNIEXPORT jboolean JNICALL Java_foundation_pEp_jniadapter_Engine__1blacklist_1is_1listed(
   3.273 -        JNIEnv *env,
   3.274 +JNIEXPORT jboolean JNICALL Java_foundation_pEp_jniadapter_Engine__1blacklist_1is_1listed(JNIEnv *env,
   3.275          jobject obj,
   3.276 -        jbyteArray fpr
   3.277 -    )
   3.278 +        jbyteArray fpr)
   3.279  {
   3.280      std::mutex *mutex_local = nullptr;
   3.281      {
   3.282 @@ -448,12 +423,10 @@
   3.283      return (jboolean)_listed;
   3.284  }
   3.285  
   3.286 -JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_Engine__1getCrashdumpLog(
   3.287 -        JNIEnv *env,
   3.288 +JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_Engine__1getCrashdumpLog(JNIEnv *env,
   3.289          jobject obj,
   3.290          jint dummy,
   3.291 -        jint maxlines
   3.292 -    )
   3.293 +        jint maxlines)
   3.294  {
   3.295      std::mutex *mutex_local = nullptr;
   3.296      {
   3.297 @@ -477,40 +450,24 @@
   3.298      return from_string(env, _logdata);
   3.299  }
   3.300  
   3.301 -JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_Engine__1getUserDirectory(
   3.302 -    JNIEnv *env,
   3.303 -    jobject obj
   3.304 -    )
   3.305 +JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_Engine__1getUserDirectory(JNIEnv *env,
   3.306 +        jobject obj)
   3.307  {
   3.308      pEpLog("called");
   3.309      return from_string(env, ::per_user_directory());
   3.310  }
   3.311  
   3.312 -JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_Engine__1getMachineDirectory(
   3.313 -    JNIEnv *env,
   3.314 -    jobject obj
   3.315 -    )
   3.316 +JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_Engine__1getMachineDirectory(JNIEnv *env,
   3.317 +        jobject obj)
   3.318  {
   3.319      pEpLog("called");
   3.320      return from_string(env, ::per_machine_directory());
   3.321  }
   3.322  
   3.323 -//void logPassphraseCache() {
   3.324 -//    try {
   3.325 -////        while(true) {
   3.326 -//            pEpLog("Cache: '" << cache.latest_passphrase() << "'");
   3.327 -////        }
   3.328 -//    } catch(pEp::PassphraseCache::Empty e) {
   3.329 -//        pEpLog(e.what());
   3.330 -//    } catch(pEp::PassphraseCache::Exhausted ex) {
   3.331 -//        pEpLog(ex.what());
   3.332 -//    }
   3.333 -//}
   3.334  
   3.335 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1config_1passphrase
   3.336 -  (JNIEnv * env,
   3.337 -   jobject obj,
   3.338 -   jbyteArray passphrase)
   3.339 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1config_1passphrase(JNIEnv * env,
   3.340 +       jobject obj,
   3.341 +       jbyteArray passphrase)
   3.342  {
   3.343      std::mutex *mutex_local = nullptr;
   3.344      {
   3.345 @@ -530,12 +487,10 @@
   3.346  }
   3.347  
   3.348  
   3.349 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1config_1passphrase_1for_1new_1keys(
   3.350 -        JNIEnv *env,
   3.351 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1config_1passphrase_1for_1new_1keys(JNIEnv *env,
   3.352          jobject obj,
   3.353          jboolean enable,
   3.354 -        jbyteArray passphrase
   3.355 -    )
   3.356 +        jbyteArray passphrase)
   3.357  {
   3.358      std::mutex *mutex_local = nullptr;
   3.359      {
   3.360 @@ -555,8 +510,6 @@
   3.361          throw_pEp_Exception(env, status);
   3.362          return ;
   3.363      }
   3.364 -
   3.365 -
   3.366  }
   3.367  
   3.368  } // extern "C"
     4.1 --- a/src/cxx/foundation_pEp_jniadapter_AbstractEngine.cc	Mon Nov 30 22:05:23 2020 +0100
     4.2 +++ b/src/cxx/foundation_pEp_jniadapter_AbstractEngine.cc	Mon Nov 30 23:57:34 2020 +0100
     4.3 @@ -15,9 +15,7 @@
     4.4  using namespace utility;   // for libpEpAdapter locked queue impl.  TODO:rename
     4.5  
     4.6  bool first = true;
     4.7 -
     4.8  JavaVM *jvm= nullptr;
     4.9 -
    4.10  std::mutex mutex_obj;
    4.11  
    4.12  jfieldID signal_field_value = nullptr;
    4.13 @@ -67,45 +65,60 @@
    4.14  
    4.15  
    4.16  void jni_init() {
    4.17 -    JNIEnv *_env = JNISync::env();
    4.18 +    JNIEnv * _env = JNISync::env();
    4.19  
    4.20 -    messageClass = static_cast<jclass>(
    4.21 -            _env->NewGlobalRef(findClass(_env, "foundation/pEp/jniadapter/Message")));
    4.22 -    identityClass = static_cast<jclass>(
    4.23 -        _env->NewGlobalRef(findClass(_env, "foundation/pEp/jniadapter/_Identity")));
    4.24 -    signalClass = static_cast<jclass>(
    4.25 -            _env->NewGlobalRef(findClass(_env, "foundation/pEp/jniadapter/SyncHandshakeSignal")));
    4.26 -    passphraseTypeClass = static_cast<jclass>(
    4.27 -            _env->NewGlobalRef(findClass(_env, "foundation/pEp/jniadapter/PassphraseType")));
    4.28 +    messageClass = static_cast<jclass>(_env->NewGlobalRef(findClass(_env, "foundation/pEp/jniadapter/Message")));
    4.29 +    identityClass = static_cast<jclass>(_env->NewGlobalRef(findClass(_env, "foundation/pEp/jniadapter/_Identity")));
    4.30 +    signalClass = static_cast<jclass>(_env->NewGlobalRef(findClass(_env, "foundation/pEp/jniadapter/SyncHandshakeSignal")));
    4.31 +    passphraseTypeClass = static_cast<jclass>(_env->NewGlobalRef(findClass(_env, "foundation/pEp/jniadapter/PassphraseType")));
    4.32      engineClass = static_cast<jclass>(_env->NewGlobalRef(findClass(_env, "foundation/pEp/jniadapter/Engine")));
    4.33  
    4.34 -    messageConstructorMethodID = _env->GetMethodID(messageClass, "<init>", "(J)V");
    4.35 +    messageConstructorMethodID = _env->GetMethodID(
    4.36 +            messageClass,
    4.37 +            "<init>",
    4.38 +            "(J)V");
    4.39 +
    4.40      messageToSendMethodID = _env->GetMethodID(
    4.41 -        engineClass,
    4.42 -        "messageToSendCallFromC",
    4.43 -        "(Lfoundation/pEp/jniadapter/Message;)I");
    4.44 +            engineClass,
    4.45 +            "messageToSendCallFromC",
    4.46 +            "(Lfoundation/pEp/jniadapter/Message;)I");
    4.47 +
    4.48      needsFastPollMethodID = _env->GetMethodID(
    4.49 -        engineClass,
    4.50 -        "needsFastPollCallFromC",
    4.51 -        "(Z)I");
    4.52 +            engineClass,
    4.53 +            "needsFastPollCallFromC",
    4.54 +            "(Z)I");
    4.55 +
    4.56      notifyHandShakeMethodID = _env->GetMethodID(
    4.57 -        engineClass,
    4.58 -        "notifyHandshakeCallFromC",
    4.59 -        "(Lfoundation/pEp/jniadapter/_Identity;Lfoundation/pEp/jniadapter/_Identity;Lfoundation/pEp/jniadapter/SyncHandshakeSignal;)I");
    4.60 +            engineClass,
    4.61 +            "notifyHandshakeCallFromC",
    4.62 +            "(Lfoundation/pEp/jniadapter/_Identity;Lfoundation/pEp/jniadapter/_Identity;Lfoundation/pEp/jniadapter/SyncHandshakeSignal;)I");
    4.63 +
    4.64      passphraseRequiredMethodID = _env->GetMethodID(
    4.65 -        engineClass,
    4.66 -        "passphraseRequiredFromC",
    4.67 -        "(Lfoundation/pEp/jniadapter/PassphraseType;)[B");
    4.68 +            engineClass,
    4.69 +            "passphraseRequiredFromC",
    4.70 +            "(Lfoundation/pEp/jniadapter/PassphraseType;)[B");
    4.71 +
    4.72 +    sync_handshake_signal_values = JNISync::env()->GetStaticMethodID(
    4.73 +            signalClass,
    4.74 +            "values",
    4.75 +            "()[Lfoundation/pEp/jniadapter/SyncHandshakeSignal;");
    4.76  
    4.77 -    sync_handshake_signal_values = JNISync::env()->GetStaticMethodID(signalClass, "values",
    4.78 -                "()[Lfoundation/pEp/jniadapter/SyncHandshakeSignal;");
    4.79 -    passphrase_status_values = JNISync::env()->GetStaticMethodID(passphraseTypeClass, "values",
    4.80 -                "()[Lfoundation/pEp/jniadapter/PassphraseType;");
    4.81 -    signal_field_value = JNISync::env()->GetFieldID(signalClass, "value", "I");
    4.82 -    passphrase_type_field_value = JNISync::env()->GetFieldID(passphraseTypeClass, "value", "I");
    4.83 +    passphrase_status_values = JNISync::env()->GetStaticMethodID(
    4.84 +            passphraseTypeClass,
    4.85 +            "values",
    4.86 +            "()[Lfoundation/pEp/jniadapter/PassphraseType;");
    4.87 +
    4.88 +    signal_field_value = JNISync::env()->GetFieldID(
    4.89 +            signalClass,
    4.90 +            "value",
    4.91 +            "I");
    4.92 +
    4.93 +    passphrase_type_field_value = JNISync::env()->GetFieldID(passphraseTypeClass,    "value", "I");
    4.94  }
    4.95  
    4.96 -char* JNIAdapter::passphraseRequiredCallback(const PEP_STATUS status) {
    4.97 +char* JNIAdapter::passphraseRequiredCallback(
    4.98 +    const PEP_STATUS status)
    4.99 +{
   4.100      pEpLog("called");
   4.101      jobject status_ = nullptr;
   4.102      {
   4.103 @@ -113,8 +126,7 @@
   4.104          assert(passphrase_status_values);
   4.105          assert(passphrase_type_field_value);
   4.106  
   4.107 -        jobjectArray values = static_cast<jobjectArray>(JNISync::env()->CallStaticObjectMethod(passphraseTypeClass,
   4.108 -                passphrase_status_values));
   4.109 +        jobjectArray values = static_cast<jobjectArray>(JNISync::env()->CallStaticObjectMethod(passphraseTypeClass, passphrase_status_values));
   4.110  
   4.111          if (JNISync::env()->ExceptionCheck()) {
   4.112              JNISync::env()->ExceptionClear();
   4.113 @@ -149,19 +161,21 @@
   4.114  
   4.115  PEP_STATUS messageToSend(message *msg)
   4.116  {
   4.117 -    std::lock_guard<std::mutex> l(mutex_obj);
   4.118 +    std::lock_guard <std::mutex> l(mutex_obj);
   4.119      pEpLog("called");
   4.120  
   4.121      // Passphrase
   4.122      // When a protocol implementation of the p≡p engine using messageToSend() cannot sign or encrypt with an
   4.123      // empty passphrase and not with the configured passphrase it is calling messageToSend() with a NULL instead
   4.124      // of a struct _message object.
   4.125 -    if (Adapter::on_sync_thread() && !msg)
   4.126 +    if (Adapter::on_sync_thread() && !msg) {
   4.127          return pEp::PassphraseCache::config_next_passphrase();
   4.128 +    }
   4.129  
   4.130      // reset passphrase iterator
   4.131 -    if (Adapter::on_sync_thread())
   4.132 +    if (Adapter::on_sync_thread()) {
   4.133          pEp::PassphraseCache::config_next_passphrase(true);
   4.134 +    }
   4.135  
   4.136      jobject msg_ = nullptr;
   4.137      assert(messageClass && messageConstructorMethodID && objj && messageToSendMethodID);
   4.138 @@ -175,11 +189,12 @@
   4.139          JNISync::env()->ExceptionClear();
   4.140      }
   4.141  
   4.142 -
   4.143      return status;
   4.144  }
   4.145  
   4.146 -PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal)
   4.147 +PEP_STATUS notifyHandshake(pEp_identity *me,
   4.148 +        pEp_identity *partner,
   4.149 +        sync_handshake_signal signal)
   4.150  {
   4.151      std::lock_guard<std::mutex> l(mutex_obj);
   4.152      pEpLog("called");
   4.153 @@ -196,8 +211,7 @@
   4.154          assert(sync_handshake_signal_values);
   4.155          assert(signal_field_value);
   4.156  
   4.157 -        jobjectArray values = static_cast<jobjectArray>(JNISync::env()->CallStaticObjectMethod(signalClass,
   4.158 -                sync_handshake_signal_values));
   4.159 +        jobjectArray values = static_cast<jobjectArray>(JNISync::env()->CallStaticObjectMethod(signalClass, sync_handshake_signal_values));
   4.160          if (JNISync::env()->ExceptionCheck()) {
   4.161              JNISync::env()->ExceptionClear();
   4.162              return PEP_UNKNOWN_ERROR;
   4.163 @@ -231,10 +245,8 @@
   4.164  extern "C" {
   4.165  using namespace pEp;
   4.166  
   4.167 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_init(
   4.168 -        JNIEnv *env,
   4.169 -        jobject obj
   4.170 -    )
   4.171 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_init(JNIEnv *env,
   4.172 +        jobject obj)
   4.173  {
   4.174      std::lock_guard<std::mutex> l(global_mutex); // global mutex for write access to <unordered_map>
   4.175      pEpLog("called");
   4.176 @@ -253,10 +265,8 @@
   4.177      Adapter::session();
   4.178  }
   4.179  
   4.180 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_release(
   4.181 -        JNIEnv *env,
   4.182 -        jobject obj
   4.183 -    )
   4.184 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_release(JNIEnv *env,
   4.185 +        jobject obj)
   4.186  {
   4.187      std::lock_guard<std::mutex> l(global_mutex);  // global mutex for write access to <unordered_map>
   4.188      pEpLog("called");
   4.189 @@ -264,10 +274,8 @@
   4.190      Adapter::session(pEp::Adapter::release);
   4.191  }
   4.192  
   4.193 -JNIEXPORT jstring JNICALL Java_foundation_pEp_jniadapter_AbstractEngine__1getVersion(
   4.194 -        JNIEnv *env,
   4.195 -        jobject obj
   4.196 -    )
   4.197 +JNIEXPORT jstring JNICALL Java_foundation_pEp_jniadapter_AbstractEngine__1getVersion(JNIEnv *env,
   4.198 +        jobject obj)
   4.199  {
   4.200      std::mutex *mutex_local = nullptr;
   4.201      {
   4.202 @@ -280,10 +288,8 @@
   4.203      return env->NewStringUTF(::get_engine_version());
   4.204  }
   4.205  
   4.206 -JNIEXPORT jstring JNICALL Java_foundation_pEp_jniadapter_AbstractEngine__1getProtocolVersion(
   4.207 -        JNIEnv *env,
   4.208 -        jobject obj
   4.209 -    )
   4.210 +JNIEXPORT jstring JNICALL Java_foundation_pEp_jniadapter_AbstractEngine__1getProtocolVersion(JNIEnv *env,
   4.211 +        jobject obj)
   4.212  {
   4.213      std::mutex *mutex_local = nullptr;
   4.214      {
   4.215 @@ -296,9 +302,10 @@
   4.216      return env->NewStringUTF(::get_protocol_version());
   4.217  }
   4.218  
   4.219 -int examine_identity(pEp_identity *ident, void *arg)
   4.220 +int examine_identity(pEp_identity *ident,
   4.221 +        void *arg)
   4.222  {
   4.223 -    locked_queue< pEp_identity * > *queue = static_cast<locked_queue<pEp_identity*>*>(arg);
   4.224 +    locked_queue < pEp_identity * > *queue = static_cast<locked_queue < pEp_identity * > * > (arg);
   4.225      queue->push_back(identity_dup(ident));
   4.226      return 0;
   4.227  }
   4.228 @@ -306,10 +313,11 @@
   4.229  pEp_identity *retrieve_next_identity(void *arg)
   4.230  {
   4.231      pEpLog("called");
   4.232 -    locked_queue< pEp_identity * > *queue = static_cast<locked_queue<pEp_identity*>*>(arg);
   4.233 +    locked_queue < pEp_identity * > *queue = static_cast<locked_queue < pEp_identity * > * > (arg);
   4.234  
   4.235 -    while (!queue->size())
   4.236 +    while (!queue->size()) {
   4.237          usleep(100000);
   4.238 +    }
   4.239  
   4.240      pEp_identity *ident = queue->front();
   4.241      queue->pop_front();
   4.242 @@ -319,7 +327,7 @@
   4.243  static void *keyserver_thread_routine(void *arg)
   4.244  {
   4.245      PEP_STATUS status = do_keymanagement(retrieve_next_identity, arg);
   4.246 -    locked_queue< pEp_identity * > *queue = static_cast<locked_queue<pEp_identity*>*>(arg);
   4.247 +    locked_queue < pEp_identity * > *queue = static_cast<locked_queue < pEp_identity * > * > (arg);
   4.248  
   4.249      while (queue->size()) {
   4.250          pEp_identity *ident = queue->front();
   4.251 @@ -328,13 +336,11 @@
   4.252      }
   4.253  
   4.254      delete queue;
   4.255 -    return reinterpret_cast<void*>(status);
   4.256 +    return reinterpret_cast<void *>(status);
   4.257  }
   4.258  
   4.259 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine__1startKeyserverLookup(
   4.260 -        JNIEnv *env,
   4.261 -        jobject obj
   4.262 -    )
   4.263 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine__1startKeyserverLookup(JNIEnv *env,
   4.264 +        jobject obj)
   4.265  {
   4.266      std::mutex *mutex_local = nullptr;
   4.267      {
   4.268 @@ -375,10 +381,8 @@
   4.269      pthread_create(thread, nullptr, keyserver_thread_routine, static_cast<void*>(queue));
   4.270  }
   4.271  
   4.272 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine__1stopKeyserverLookup(
   4.273 -        JNIEnv *env,
   4.274 -        jobject obj
   4.275 -    )
   4.276 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine__1stopKeyserverLookup(JNIEnv *env,
   4.277 +        jobject obj)
   4.278  {
   4.279      std::mutex *mutex_local = nullptr;
   4.280      {
   4.281 @@ -419,10 +423,8 @@
   4.282      free(thread);
   4.283  }
   4.284  
   4.285 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine__1startSync(
   4.286 -        JNIEnv *env,
   4.287 -        jobject obj
   4.288 -    )
   4.289 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine__1startSync(JNIEnv *env,
   4.290 +        jobject obj)
   4.291  {
   4.292      std::mutex *mutex_local = nullptr;
   4.293      {
   4.294 @@ -434,17 +436,14 @@
   4.295  
   4.296      try {
   4.297          CallbackDispatcher::start_sync();
   4.298 -//        Adapter::startup<JNISync>(messageToSend, notifyHandshake, &o, &JNISync::onSyncStartup, &JNISync::onSyncShutdown);
   4.299      } catch (RuntimeError& ex) {
   4.300          throw_pEp_Exception(env, ex.status);
   4.301          return;
   4.302      }
   4.303  }
   4.304  
   4.305 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine__1stopSync(
   4.306 -        JNIEnv *env,
   4.307 -        jobject obj
   4.308 -    )
   4.309 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine__1stopSync(JNIEnv *env,
   4.310 +        jobject obj)
   4.311  {
   4.312      std::mutex *mutex_local = nullptr;
   4.313      {
   4.314 @@ -455,13 +454,10 @@
   4.315      std::lock_guard<std::mutex> l(*mutex_local);
   4.316  
   4.317      CallbackDispatcher::stop_sync();
   4.318 -//    Adapter::shutdown();
   4.319  }
   4.320  
   4.321 -JNIEXPORT jboolean JNICALL Java_foundation_pEp_jniadapter_AbstractEngine__1isSyncRunning(
   4.322 -        JNIEnv *env,
   4.323 -        jobject obj
   4.324 -    )
   4.325 +JNIEXPORT jboolean JNICALL Java_foundation_pEp_jniadapter_AbstractEngine__1isSyncRunning(JNIEnv *env,
   4.326 +        jobject obj)
   4.327  {
   4.328      std::mutex *mutex_local = nullptr;
   4.329      {
     5.1 --- a/src/cxx/foundation_pEp_jniadapter__Blob.cc	Mon Nov 30 22:05:23 2020 +0100
     5.2 +++ b/src/cxx/foundation_pEp_jniadapter__Blob.cc	Mon Nov 30 23:57:34 2020 +0100
     5.3 @@ -8,39 +8,43 @@
     5.4  #include "foundation_pEp_jniadapter__Blob.h"
     5.5  
     5.6  namespace pEp {
     5.7 -    namespace JNIAdapter {
     5.8 -        static ::bloblist_t *bloblist_ptr(JNIEnv *env, jobject me) {
     5.9 -            jfieldID handle;
    5.10 +namespace JNIAdapter {
    5.11 +
    5.12 +static ::bloblist_t *bloblist_ptr(JNIEnv *env, jobject me) {
    5.13 +    jfieldID handle;
    5.14  
    5.15 -            try {
    5.16 -                handle = getFieldID(env, "foundation/pEp/jniadapter/Blob", "mime_type", "Ljava/lang/String");
    5.17 -            }
    5.18 -            catch (std::exception& ex) {
    5.19 -                assert(0);
    5.20 -                return NULL;
    5.21 -            }
    5.22 +    try {
    5.23 +        handle = getFieldID(env, "foundation/pEp/jniadapter/Blob", "mime_type", "Ljava/lang/String");
    5.24 +    } catch (std::exception &ex) {
    5.25 +        assert(0);
    5.26 +        return NULL;
    5.27 +    }
    5.28  
    5.29 -            return reinterpret_cast<::bloblist_t*>(env->GetLongField(me, handle));
    5.30 -        }
    5.31 -    };
    5.32 -};
    5.33 +    return reinterpret_cast<::bloblist_t *>(env->GetLongField(me, handle));
    5.34 +}
    5.35 +
    5.36 +}; //namespace JNIAdapter
    5.37 +}; //namespace pEp
    5.38  
    5.39  extern "C" {
    5.40  
    5.41  
    5.42  using namespace std;
    5.43  using namespace pEp::JNIAdapter;
    5.44 -JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter__1Blob__1dataToXER(JNIEnv *env, jobject obj)
    5.45 +
    5.46 +JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter__1Blob__1dataToXER(JNIEnv *env,
    5.47 +        jobject obj)
    5.48  {
    5.49      pEpLog("called");
    5.50      bloblist_t *b = to_blob(env, obj);
    5.51      char *out = nullptr;
    5.52  
    5.53      // RFC 1049 / RFC 2045 : The type, subtype, and parameter names are not case sensitive.
    5.54 -    if(strcasecmp(b->mime_type, "application/pEp.sync") == 0) {
    5.55 +    if (strcasecmp(b->mime_type, "application/pEp.sync") == 0) {
    5.56          PEP_STATUS status = ::PER_to_XER_Sync_msg(b->value, static_cast<size_t>(b->size), &out);
    5.57 -        if (status)
    5.58 +        if (status) {
    5.59              throw_pEp_Exception(env, status);
    5.60 +        }
    5.61  
    5.62          jbyteArray result = from_string(env, out);
    5.63          free(out);
    5.64 @@ -48,17 +52,18 @@
    5.65      }
    5.66  
    5.67      // RFC 1049 / RFC 2045 : The type, subtype, and parameter names are not case sensitive.
    5.68 -    if(strcasecmp(b->mime_type, "application/pEp.keyreset") == 0) {
    5.69 +    if (strcasecmp(b->mime_type, "application/pEp.keyreset") == 0) {
    5.70          PEP_STATUS status = ::PER_to_XER_Distribution_msg(b->value, static_cast<size_t>(b->size), &out);
    5.71 -        if (status)
    5.72 +        if (status) {
    5.73              throw_pEp_Exception(env, status);
    5.74 +        }
    5.75  
    5.76          jbyteArray result = from_string(env, out);
    5.77          free(out);
    5.78          return result;
    5.79      }
    5.80  
    5.81 -    return from_string(env,b->value);
    5.82 +    return from_string(env, b->value);
    5.83  }
    5.84  
    5.85  }; // extern "C"
     6.1 --- a/src/cxx/identity_api.cc	Mon Nov 30 22:05:23 2020 +0100
     6.2 +++ b/src/cxx/identity_api.cc	Mon Nov 30 23:57:34 2020 +0100
     6.3 @@ -3,10 +3,12 @@
     6.4  
     6.5  #include "jniutils.hh"
     6.6  
     6.7 -
     6.8  extern "C" {
     6.9  
    6.10 -JNIEXPORT jint JNICALL Java_foundation_pEp_jniadapter_Identity__1getRating(JNIEnv *env, jobject thiz, jint comm_type)
    6.11 +JNIEXPORT jint JNICALL Java_foundation_pEp_jniadapter_Identity__1getRating(
    6.12 +        JNIEnv *env,
    6.13 +        jobject thiz,
    6.14 +        jint comm_type)
    6.15  {
    6.16      pEpLog("called");
    6.17      return ::rating_from_comm_type(static_cast<PEP_comm_type>(comm_type));
     7.1 --- a/src/cxx/jniutils.cc	Mon Nov 30 22:05:23 2020 +0100
     7.2 +++ b/src/cxx/jniutils.cc	Mon Nov 30 23:57:34 2020 +0100
     7.3 @@ -1,8 +1,11 @@
     7.4  #include <cassert>
     7.5  #include "jniutils.hh"
     7.6  #include <pEp/pEpLog.hh>
     7.7 +
     7.8  #ifndef __LP64__
     7.9 +
    7.10  #include <time64.h>
    7.11 +
    7.12  #define time_t time64_t
    7.13  #define timegm timegm64
    7.14  #define gmtime_r gmtime64_r
    7.15 @@ -11,760 +14,782 @@
    7.16  #endif
    7.17  
    7.18  namespace pEp {
    7.19 -    namespace JNIAdapter {
    7.20 -        std::mutex global_mutex;
    7.21 -        std::unordered_map<long, std::mutex*> engine_objid_mutex;
    7.22 +namespace JNIAdapter {
    7.23  
    7.24 -        std::mutex* get_engine_java_object_mutex(
    7.25 -                JNIEnv *env,
    7.26 -                jobject obj
    7.27 -            )
    7.28 -        {
    7.29 -            long engine_obj_id = static_cast<long>(callLongMethod(env, obj, "getId"));
    7.30 -            assert(engine_obj_id);
    7.31 -            pEpLog("for java object id: " << engine_obj_id);
    7.32 -            std::mutex *engine_obj_mutex = engine_objid_mutex.at(engine_obj_id);
    7.33 -            pEpLog("found mutex: " << engine_obj_mutex << " with native_handle: " << engine_obj_mutex->native_handle());
    7.34 -            assert(engine_obj_mutex);
    7.35 -            return engine_obj_mutex;
    7.36 -        }
    7.37 -
    7.38 -        void create_engine_java_object_mutex(
    7.39 -                JNIEnv *env,
    7.40 -                jobject obj
    7.41 -            )
    7.42 -        {
    7.43 -            long engine_obj_id = static_cast<long>(callLongMethod(env, obj, "getId"));
    7.44 -            assert(engine_obj_id);
    7.45 -            std::mutex *engine_obj_mutex = new std::mutex();
    7.46 -            pEpLog(engine_obj_mutex << " with native_handle: " << engine_obj_mutex->native_handle() << " for java object id: " << engine_obj_id);
    7.47 -            assert(engine_obj_mutex);
    7.48 -            if(engine_objid_mutex.count(engine_obj_id) > 0) {
    7.49 -                pEpLog("Fatal: mutex already existing for object id: " << engine_obj_id);
    7.50 -                assert(0);
    7.51 -            }
    7.52 -            engine_objid_mutex.insert(std::make_pair(engine_obj_id, engine_obj_mutex ));
    7.53 -        }
    7.54 +std::mutex global_mutex;
    7.55 +std::unordered_map<long, std::mutex *> engine_objid_mutex;
    7.56  
    7.57 -        void release_engine_java_object_mutex(
    7.58 -                JNIEnv *env,
    7.59 -                jobject obj
    7.60 -            )
    7.61 -        {
    7.62 -            long engine_obj_id = static_cast<long>(callLongMethod(env, obj, "getId"));
    7.63 -            assert(engine_obj_id);
    7.64 -            std::mutex *engine_obj_mutex = engine_objid_mutex.at(engine_obj_id);
    7.65 -            pEpLog(engine_obj_mutex << " with native_handle: " << engine_obj_mutex->native_handle() << " for java object id: " << engine_obj_id);
    7.66 -            assert(engine_obj_mutex);
    7.67 -            engine_objid_mutex.erase(engine_obj_id);
    7.68 -            delete engine_obj_mutex;
    7.69 -        }
    7.70 -
    7.71 -        jclass findClass(JNIEnv *env, const char *classname)
    7.72 -        {
    7.73 -            jclass clazz = env->FindClass(classname);
    7.74 -            if (!clazz)
    7.75 -                fprintf(stderr, "class not found: %s\n", classname);
    7.76 -            assert(clazz);
    7.77 -
    7.78 -            if (clazz == NULL) {
    7.79 -                jclass ex = env->FindClass("java/lang/NoClassDefFoundError");
    7.80 -                assert(ex);
    7.81 -                env->ThrowNew(ex, classname);
    7.82 -                throw std::bad_cast();
    7.83 -            }
    7.84 -            
    7.85 -            return clazz;
    7.86 -        }
    7.87 -
    7.88 -        jfieldID getFieldID(
    7.89 -                JNIEnv *env,
    7.90 -                const char *classname,
    7.91 -                const char *fieldname,
    7.92 -                const char *signature
    7.93 -            )
    7.94 -        {
    7.95 -            jclass clazz = findClass(env, classname);
    7.96 -            assert(clazz);
    7.97 -
    7.98 -            jfieldID field = env->GetFieldID(clazz, fieldname, signature);
    7.99 -            assert(field);
   7.100 -
   7.101 -            if (field == NULL) {
   7.102 -                jclass ex = env->FindClass("java/lang/NoSuchFieldError");
   7.103 -                assert(ex);
   7.104 -                env->ThrowNew(ex, fieldname);
   7.105 -                throw std::invalid_argument(std::string(fieldname));
   7.106 -            }
   7.107 +std::mutex *get_engine_java_object_mutex(JNIEnv *env,
   7.108 +        jobject obj)
   7.109 +{
   7.110 +    long engine_obj_id = static_cast<long>(callLongMethod(env, obj, "getId"));
   7.111 +    assert(engine_obj_id);
   7.112 +    pEpLog("for java object id: " << engine_obj_id);
   7.113 +    std::mutex *engine_obj_mutex = engine_objid_mutex.at(engine_obj_id);
   7.114 +    pEpLog("found mutex: " << engine_obj_mutex << " with native_handle: " << engine_obj_mutex->native_handle());
   7.115 +    assert(engine_obj_mutex);
   7.116 +    return engine_obj_mutex;
   7.117 +}
   7.118  
   7.119 -            env->DeleteLocalRef(clazz);
   7.120 -
   7.121 -            return field;
   7.122 -        }
   7.123 -
   7.124 -        //TODO: fix/generalize/clean patch added to make keysync work using globalref to class
   7.125 -        jfieldID getFieldID(
   7.126 -                JNIEnv *env,
   7.127 -                const char *classname,
   7.128 -                const char *fieldname,
   7.129 -                const char *signature,
   7.130 -                const jclass clazz
   7.131 -            )
   7.132 -        {
   7.133 -
   7.134 -            jfieldID field = env->GetFieldID(clazz, fieldname, signature);
   7.135 -            assert(field);
   7.136 -
   7.137 -            if (field == NULL) {
   7.138 -                jclass ex = env->FindClass("java/lang/NoSuchFieldError");
   7.139 -                assert(ex);
   7.140 -                env->ThrowNew(ex, fieldname);
   7.141 -                throw std::invalid_argument(std::string(fieldname));
   7.142 -            }
   7.143 -
   7.144 -
   7.145 -            return field;
   7.146 -        }
   7.147 -
   7.148 -        jint callIntMethod(
   7.149 -                JNIEnv *env,
   7.150 -                jobject obj,
   7.151 -                const char *methodname
   7.152 -            )
   7.153 -        {
   7.154 -            jclass clazz = env->GetObjectClass(obj);
   7.155 -            assert(clazz);
   7.156 +void create_engine_java_object_mutex(JNIEnv *env,
   7.157 +        jobject obj)
   7.158 +{
   7.159 +    long engine_obj_id = static_cast<long>(callLongMethod(env, obj, "getId"));
   7.160 +    assert(engine_obj_id);
   7.161 +    std::mutex *engine_obj_mutex = new std::mutex();
   7.162 +    pEpLog(engine_obj_mutex << " with native_handle: " << engine_obj_mutex->native_handle() << " for java object id: " << engine_obj_id);
   7.163 +    assert(engine_obj_mutex);
   7.164 +    if (engine_objid_mutex.count(engine_obj_id) > 0) {
   7.165 +        pEpLog("Fatal: mutex already existing for object id: " << engine_obj_id);
   7.166 +        assert(0);
   7.167 +    }
   7.168 +    engine_objid_mutex.insert(std::make_pair(engine_obj_id, engine_obj_mutex));
   7.169 +}
   7.170  
   7.171 -            jmethodID method = env->GetMethodID(clazz, methodname, "()I");
   7.172 -            if (method == NULL) {
   7.173 -                jclass ex = env->FindClass("java/lang/NoSuchMethodError");
   7.174 -                assert(ex);
   7.175 -                env->ThrowNew(ex, methodname);
   7.176 -                throw std::invalid_argument(std::string(methodname));
   7.177 -            }
   7.178 -
   7.179 -            env->DeleteLocalRef(clazz);
   7.180 -
   7.181 -            jint result = env->CallIntMethod(obj, method);
   7.182 -            env->ExceptionCheck(); // handle exception in Java
   7.183 -            return result;
   7.184 -        }
   7.185 -
   7.186 -        jlong callLongMethod(
   7.187 -                JNIEnv *env,
   7.188 -                jobject obj,
   7.189 -                const char *methodname
   7.190 -            )
   7.191 -        {
   7.192 -            jclass clazz = env->GetObjectClass(obj);
   7.193 -            assert(clazz);
   7.194 -
   7.195 -            jmethodID method = env->GetMethodID(clazz, methodname, "()J");
   7.196 -            if (method == NULL) {
   7.197 -                jclass ex = env->FindClass("java/lang/NoSuchMethodError");
   7.198 -                assert(ex);
   7.199 -                env->ThrowNew(ex, methodname);
   7.200 -                throw std::invalid_argument(std::string(methodname));
   7.201 -            }
   7.202 -
   7.203 -            env->DeleteLocalRef(clazz);
   7.204 -
   7.205 -            jlong result = env->CallLongMethod(obj, method);
   7.206 -            env->ExceptionCheck(); // handle exception in Java
   7.207 -            return result;
   7.208 -        }
   7.209 -
   7.210 -        jobject callObjectMethod(
   7.211 -                JNIEnv *env,
   7.212 -                jobject obj,
   7.213 -                const char *methodname,
   7.214 -                jint index
   7.215 -            )
   7.216 -        {
   7.217 -            jclass clazz = env->GetObjectClass(obj);
   7.218 -            assert(clazz);
   7.219 +void release_engine_java_object_mutex(JNIEnv *env,
   7.220 +        jobject obj)
   7.221 +{
   7.222 +    long engine_obj_id = static_cast<long>(callLongMethod(env, obj, "getId"));
   7.223 +    assert(engine_obj_id);
   7.224 +    std::mutex *engine_obj_mutex = engine_objid_mutex.at(engine_obj_id);
   7.225 +    pEpLog(engine_obj_mutex << " with native_handle: " << engine_obj_mutex->native_handle() << " for java object id: " << engine_obj_id);
   7.226 +    assert(engine_obj_mutex);
   7.227 +    engine_objid_mutex.erase(engine_obj_id);
   7.228 +    delete engine_obj_mutex;
   7.229 +}
   7.230  
   7.231 -            jmethodID method = env->GetMethodID(clazz, methodname,
   7.232 -                    "(I)Ljava/lang/Object;");
   7.233 -            if (method == NULL) {
   7.234 -                jclass ex = env->FindClass("java/lang/NoSuchMethodError");
   7.235 -                assert(ex);
   7.236 -                env->ThrowNew(ex, methodname);
   7.237 -                throw std::invalid_argument(std::string(methodname));
   7.238 -            }
   7.239 -
   7.240 -            env->DeleteLocalRef(clazz);
   7.241 -
   7.242 -            jobject result = env->CallObjectMethod(obj, method, index);
   7.243 -            env->ExceptionCheck(); // handle exception in Java
   7.244 -            return result;
   7.245 -        }
   7.246 -
   7.247 -        jboolean callBooleanMethod(
   7.248 -                JNIEnv *env,
   7.249 -                jobject obj,
   7.250 -                const char *methodname,
   7.251 -                jobject o
   7.252 -            )
   7.253 -        {
   7.254 -            jclass clazz = env->GetObjectClass(obj);
   7.255 -            assert(clazz);
   7.256 -
   7.257 -            jmethodID method = env->GetMethodID(clazz, methodname,
   7.258 -                    "(Ljava/lang/Object;)Z");
   7.259 -            if (method == NULL) {
   7.260 -                jclass ex = env->FindClass("java/lang/NoSuchMethodError");
   7.261 -                assert(ex);
   7.262 -                env->ThrowNew(ex, methodname);
   7.263 -                throw std::invalid_argument(std::string(methodname));
   7.264 -            }
   7.265 -
   7.266 -            env->DeleteLocalRef(clazz);
   7.267 -
   7.268 -            jboolean result = env->CallBooleanMethod(obj, method, o);
   7.269 -            env->ExceptionCheck(); // handle exception in Java
   7.270 -            return result;
   7.271 -        }
   7.272 +jclass findClass(JNIEnv *env,
   7.273 +        const char *classname)
   7.274 +{
   7.275 +    jclass clazz = env->FindClass(classname);
   7.276 +    if (!clazz) {
   7.277 +        fprintf(stderr, "class not found: %s\n", classname);
   7.278 +    }
   7.279 +    assert(clazz);
   7.280  
   7.281 -        jint outOfMemory(JNIEnv *env)
   7.282 -        {
   7.283 -            jclass ex;
   7.284 -            const char *ex_name = "java/lang/OutOfMemoryError";
   7.285 -
   7.286 -            ex = env->FindClass(ex_name);
   7.287 -            assert(ex);
   7.288 -            return env->ThrowNew(ex, ex_name);
   7.289 -        }
   7.290 -
   7.291 -        jobject from_Integer(JNIEnv *env, int val)
   7.292 -        {
   7.293 -            assert(env);
   7.294 -            jclass clazz = findClass(env, "java/lang/Integer");
   7.295 -
   7.296 -            jmethodID constructor = env->GetMethodID(clazz, "<init>", "(I)V");
   7.297 -            assert(constructor);
   7.298 -
   7.299 -            jobject obj = env->NewObject(clazz, constructor, val);
   7.300 -            assert(obj);
   7.301 -            return obj;
   7.302 -        }
   7.303 +    if (clazz == NULL) {
   7.304 +        jclass ex = env->FindClass("java/lang/NoClassDefFoundError");
   7.305 +        assert(ex);
   7.306 +        env->ThrowNew(ex, classname);
   7.307 +        throw std::bad_cast();
   7.308 +    }
   7.309  
   7.310 -        int to_Integer(JNIEnv *env, jobject obj)
   7.311 -        {
   7.312 -            assert(env && obj);
   7.313 -            int _val = callIntMethod(env, obj, "intValue");
   7.314 -            return _val;
   7.315 -        }
   7.316 +    return clazz;
   7.317 +}
   7.318  
   7.319 -        jbyteArray from_string(JNIEnv *env, const char *str)
   7.320 -        {
   7.321 -            if (str && str[0]) {
   7.322 -                jboolean isCopy;
   7.323 -                size_t l = strlen(str);
   7.324 -                jbyteArray _str = env->NewByteArray(l);
   7.325 -                env->SetByteArrayRegion(_str, 0, l, (jbyte*)str);
   7.326 -                return _str;
   7.327 -            }
   7.328 -            else if (str) {
   7.329 -                return env->NewByteArray(0);
   7.330 -            } else {
   7.331 -                return (jbyteArray) NULL;
   7.332 -            }
   7.333 -        }
   7.334 +jfieldID getFieldID(JNIEnv *env,
   7.335 +        const char *classname,
   7.336 +        const char *fieldname,
   7.337 +        const char *signature)
   7.338 +{
   7.339 +    jclass clazz = findClass(env, classname);
   7.340 +    assert(clazz);
   7.341  
   7.342 -        char *to_string(JNIEnv *env, jbyteArray str)
   7.343 -        {
   7.344 -            if (str == NULL)
   7.345 -                return NULL;
   7.346 -
   7.347 -            size_t l = env->GetArrayLength(str);
   7.348 -            char *_str = static_cast<char*>(calloc(1,l+1));
   7.349 -            assert(_str);
   7.350 -            env->GetByteArrayRegion(str, 0, l, (jbyte*)_str);
   7.351 -            return _str;
   7.352 -        }
   7.353 -
   7.354 -        jobject from_stringlist(JNIEnv *env, stringlist_t *sl)
   7.355 -        {
   7.356 -            if (!sl)
   7.357 -                return (jobject) NULL;
   7.358 +    jfieldID field = env->GetFieldID(clazz, fieldname, signature);
   7.359 +    assert(field);
   7.360  
   7.361 -            jclass clazz = findClass(env, "java/util/Vector");
   7.362 -            jmethodID constructor = env->GetMethodID(clazz, "<init>", "()V");
   7.363 -            assert(constructor);
   7.364 -            jobject obj = env->NewObject(clazz, constructor);
   7.365 -            assert(obj);
   7.366 -
   7.367 -            stringlist_t *_sl;
   7.368 -            for (_sl = sl; _sl && _sl->value; _sl = _sl->next) {
   7.369 -                jobject o = from_string(env, _sl->value);
   7.370 -                callBooleanMethod(env, obj, "add", o);
   7.371 -            }
   7.372 +    if (field == NULL) {
   7.373 +        jclass ex = env->FindClass("java/lang/NoSuchFieldError");
   7.374 +        assert(ex);
   7.375 +        env->ThrowNew(ex, fieldname);
   7.376 +        throw std::invalid_argument(std::string(fieldname));
   7.377 +    }
   7.378  
   7.379 -            env->DeleteLocalRef(clazz);
   7.380 -
   7.381 -            return obj;
   7.382 -        }
   7.383 +    env->DeleteLocalRef(clazz);
   7.384  
   7.385 -        stringlist_t *to_stringlist(JNIEnv *env, jobject obj)
   7.386 -        {
   7.387 -            if (!obj)
   7.388 -                return NULL;
   7.389 -
   7.390 -            jint size = callIntMethod(env, obj, "size");
   7.391 -            if (size == 0)
   7.392 -                return NULL;
   7.393 +    return field;
   7.394 +}
   7.395  
   7.396 -            stringlist_t *sl = new_stringlist(NULL);
   7.397 -            stringlist_t *_sl;
   7.398 -            jint i;
   7.399 -            for (_sl = sl, i = 0; i < (int) size; i++) {
   7.400 -                jobject o = callObjectMethod(env, obj, "get", i);
   7.401 -                jbyteArray a = static_cast<jbyteArray>(o);
   7.402 -                char * str = to_string(env, a);
   7.403 -                _sl = stringlist_add(_sl, str);
   7.404 -                env->DeleteLocalRef(o);
   7.405 -                free(str);
   7.406 -            }
   7.407 -
   7.408 -            return sl;
   7.409 -        }
   7.410 -
   7.411 -        jobject from_stringpairlist(JNIEnv *env, stringpair_list_t *sl)
   7.412 -        {
   7.413 -            if (!sl)
   7.414 -                return (jobject) NULL;
   7.415 +//TODO: fix/generalize/clean patch added to make keysync work using globalref to class
   7.416 +jfieldID getFieldID(JNIEnv *env,
   7.417 +        const char *classname,
   7.418 +        const char *fieldname,
   7.419 +        const char *signature,
   7.420 +        const jclass clazz)
   7.421 +{
   7.422 +    jfieldID field = env->GetFieldID(clazz, fieldname, signature);
   7.423 +    assert(field);
   7.424  
   7.425 -            jclass clazz = findClass(env, "java/util/ArrayList");
   7.426 -            jclass clazz_pair = findClass(env, "foundation/pEp/jniadapter/Pair");
   7.427 -            jmethodID constructor = env->GetMethodID(clazz, "<init>", "()V");
   7.428 -            assert(constructor);
   7.429 -            jmethodID constructor_pair = env->GetMethodID(clazz_pair, "<init>",
   7.430 -                    "(Ljava/lang/Object;Ljava/lang/Object;)V");
   7.431 -            assert(constructor_pair);
   7.432 -
   7.433 -            jobject obj = env->NewObject(clazz, constructor);
   7.434 -            assert(obj);
   7.435 -
   7.436 -            stringpair_list_t *_sl;
   7.437 -            for (_sl = sl; _sl && _sl->value; _sl = _sl->next) {
   7.438 -                assert(_sl->value->key);
   7.439 -                assert(_sl->value->value);
   7.440 -
   7.441 -                jbyteArray first = from_string(env, _sl->value->key);
   7.442 -                jbyteArray second = from_string(env, _sl->value->value);
   7.443 -                jobject pair = env->NewObject(clazz_pair, constructor_pair,
   7.444 -                        first, second);
   7.445 -                callBooleanMethod(env, obj, "add", pair);
   7.446 -
   7.447 -                env->DeleteLocalRef(first);
   7.448 -                env->DeleteLocalRef(second);
   7.449 -                env->DeleteLocalRef(pair);
   7.450 +    if (field == NULL) {
   7.451 +        jclass ex = env->FindClass("java/lang/NoSuchFieldError");
   7.452 +        assert(ex);
   7.453 +        env->ThrowNew(ex, fieldname);
   7.454 +        throw std::invalid_argument(std::string(fieldname));
   7.455 +    }
   7.456  
   7.457  
   7.458 -            }
   7.459 +    return field;
   7.460 +}
   7.461  
   7.462 -            env->DeleteLocalRef(clazz);
   7.463 -            env->DeleteLocalRef(clazz_pair);
   7.464 -
   7.465 -            return obj;
   7.466 -        }
   7.467 +jint callIntMethod(JNIEnv *env,
   7.468 +        jobject obj,
   7.469 +        const char *methodname)
   7.470 +{
   7.471 +    jclass clazz = env->GetObjectClass(obj);
   7.472 +    assert(clazz);
   7.473  
   7.474 -        stringpair_list_t *to_stringpairlist(JNIEnv *env, jobject obj)
   7.475 -        {
   7.476 -            if (!obj)
   7.477 -                return NULL;
   7.478 +    jmethodID method = env->GetMethodID(clazz, methodname, "()I");
   7.479 +    if (method == NULL) {
   7.480 +        jclass ex = env->FindClass("java/lang/NoSuchMethodError");
   7.481 +        assert(ex);
   7.482 +        env->ThrowNew(ex, methodname);
   7.483 +        throw std::invalid_argument(std::string(methodname));
   7.484 +    }
   7.485 +
   7.486 +    env->DeleteLocalRef(clazz);
   7.487  
   7.488 -            jint size = callIntMethod(env, obj, "size");
   7.489 -            if (size == 0)
   7.490 -                return NULL;
   7.491 +    jint result = env->CallIntMethod(obj, method);
   7.492 +    env->ExceptionCheck(); // handle exception in Java
   7.493 +    return result;
   7.494 +}
   7.495 +
   7.496 +jlong callLongMethod(JNIEnv *env,
   7.497 +        jobject obj,
   7.498 +        const char *methodname)
   7.499 +{
   7.500 +    jclass clazz = env->GetObjectClass(obj);
   7.501 +    assert(clazz);
   7.502  
   7.503 -            jfieldID first_id = getFieldID(env, "foundation/pEp/jniadapter/Pair",
   7.504 -                    "first", "Ljava/lang/Object;");
   7.505 -            jfieldID second_id = getFieldID(env, "foundation/pEp/jniadapter/Pair",
   7.506 -                    "second", "Ljava/lang/Object;");
   7.507 +    jmethodID method = env->GetMethodID(clazz, methodname, "()J");
   7.508 +    if (method == NULL) {
   7.509 +        jclass ex = env->FindClass("java/lang/NoSuchMethodError");
   7.510 +        assert(ex);
   7.511 +        env->ThrowNew(ex, methodname);
   7.512 +        throw std::invalid_argument(std::string(methodname));
   7.513 +    }
   7.514  
   7.515 -            stringpair_list_t *sl = new_stringpair_list(NULL);
   7.516 -            stringpair_list_t *_sl;
   7.517 -            jint i;
   7.518 +    env->DeleteLocalRef(clazz);
   7.519 +
   7.520 +    jlong result = env->CallLongMethod(obj, method);
   7.521 +    env->ExceptionCheck(); // handle exception in Java
   7.522 +    return result;
   7.523 +}
   7.524  
   7.525 -            for (_sl = sl, i = 0; i < (int) size; i++) {
   7.526 -                jobject pair = callObjectMethod(env, obj, "get", i);
   7.527 -                jbyteArray first =
   7.528 -                    static_cast<jbyteArray>(env->GetObjectField(pair,
   7.529 -                                first_id));
   7.530 -                jbyteArray second =
   7.531 -                    static_cast<jbyteArray>(env->GetObjectField(pair,
   7.532 -                                second_id));
   7.533 +jobject callObjectMethod(JNIEnv *env,
   7.534 +        jobject obj,
   7.535 +        const char *methodname,
   7.536 +        jint index)
   7.537 +{
   7.538 +    jclass clazz = env->GetObjectClass(obj);
   7.539 +    assert(clazz);
   7.540  
   7.541 -                char *first_str = to_string(env, first);
   7.542 -                char *second_str = to_string(env, second);
   7.543 -                stringpair_t *sp = new_stringpair(first_str, second_str);
   7.544 -                env->DeleteLocalRef(pair);
   7.545 -                free(first_str);
   7.546 -                free(second_str);
   7.547 +    jmethodID method = env->GetMethodID(clazz, methodname, "(I)Ljava/lang/Object;");
   7.548 +    if (method == NULL) {
   7.549 +        jclass ex = env->FindClass("java/lang/NoSuchMethodError");
   7.550 +        assert(ex);
   7.551 +        env->ThrowNew(ex, methodname);
   7.552 +        throw std::invalid_argument(std::string(methodname));
   7.553 +    }
   7.554 +
   7.555 +    env->DeleteLocalRef(clazz);
   7.556 +
   7.557 +    jobject result = env->CallObjectMethod(obj, method, index);
   7.558 +    env->ExceptionCheck(); // handle exception in Java
   7.559 +    return result;
   7.560 +}
   7.561  
   7.562 -                _sl = stringpair_list_add(_sl, sp);
   7.563 -            }
   7.564 +jboolean callBooleanMethod(JNIEnv *env,
   7.565 +        jobject obj,
   7.566 +        const char *methodname,
   7.567 +        jobject o)
   7.568 +{
   7.569 +    jclass clazz = env->GetObjectClass(obj);
   7.570 +    assert(clazz);
   7.571  
   7.572 -            return sl;
   7.573 -        }
   7.574 +    jmethodID method = env->GetMethodID(clazz, methodname, "(Ljava/lang/Object;)Z");
   7.575 +    if (method == NULL) {
   7.576 +        jclass ex = env->FindClass("java/lang/NoSuchMethodError");
   7.577 +        assert(ex);
   7.578 +        env->ThrowNew(ex, methodname);
   7.579 +        throw std::invalid_argument(std::string(methodname));
   7.580 +    }
   7.581 +
   7.582 +    env->DeleteLocalRef(clazz);
   7.583  
   7.584 -        jobject from_timestamp(JNIEnv *env, timestamp *ts)
   7.585 -        {
   7.586 -            if (!ts)
   7.587 -                return NULL;
   7.588 +    jboolean result = env->CallBooleanMethod(obj, method, o);
   7.589 +    env->ExceptionCheck(); // handle exception in Java
   7.590 +    return result;
   7.591 +}
   7.592 +
   7.593 +jint outOfMemory(JNIEnv *env)
   7.594 +{
   7.595 +    jclass ex;
   7.596 +    const char *ex_name = "java/lang/OutOfMemoryError";
   7.597  
   7.598 -            //LOGD("/* Seconds (0-60) */  FROM   :%d", ts->tm_sec);
   7.599 -            //LOGD("/* Minutes (0-59) */         :%d", ts->tm_min);    
   7.600 -            //LOGD("/* Hours (0-23) */           :%d", ts->tm_hour);   
   7.601 -            //LOGD("/* Day of the month (1-31) */:%d", ts->tm_mday);   
   7.602 -            //LOGD("/* Month (0-11) */           :%d", ts->tm_mon);    
   7.603 -            //LOGD("/* Year - 1900 */            :%d", ts->tm_year);   
   7.604 +    ex = env->FindClass(ex_name);
   7.605 +    assert(ex);
   7.606 +    return env->ThrowNew(ex, ex_name);
   7.607 +}
   7.608 +
   7.609 +jobject from_Integer(JNIEnv *env,
   7.610 +        int val)
   7.611 +{
   7.612 +    assert(env);
   7.613 +    jclass clazz = findClass(env, "java/lang/Integer");
   7.614  
   7.615 -            time_t t = timegm(ts)*1000;
   7.616 -            //LOGD( "TimeGM returns : %lld", t);
   7.617 -            jclass clazz = findClass(env, "java/util/Date");
   7.618 -            jmethodID constructor = env->GetMethodID(clazz, "<init>", "(J)V");
   7.619 -            assert(constructor);
   7.620 +    jmethodID constructor = env->GetMethodID(clazz, "<init>", "(I)V");
   7.621 +    assert(constructor);
   7.622 +
   7.623 +    jobject obj = env->NewObject(clazz, constructor, val);
   7.624 +    assert(obj);
   7.625 +    return obj;
   7.626 +}
   7.627  
   7.628 -            jobject result =  env->NewObject(clazz, constructor, (jlong) t);
   7.629 -
   7.630 -            env->DeleteLocalRef(clazz);
   7.631 -
   7.632 -            return result;
   7.633 +int to_Integer(JNIEnv *env,
   7.634 +        jobject obj)
   7.635 +{
   7.636 +    assert(env && obj);
   7.637 +    int _val = callIntMethod(env, obj, "intValue");
   7.638 +    return _val;
   7.639 +}
   7.640  
   7.641 -        }
   7.642 -
   7.643 -        timestamp *to_timestamp(JNIEnv *env, jobject date)
   7.644 -        {
   7.645 -            if (!date)
   7.646 -                return NULL;
   7.647 +jbyteArray from_string(JNIEnv *env,
   7.648 +        const char *str)
   7.649 +{
   7.650 +    if (str && str[0]) {
   7.651 +        jboolean isCopy;
   7.652 +        size_t l = strlen(str);
   7.653 +        jbyteArray _str = env->NewByteArray(l);
   7.654 +        env->SetByteArrayRegion(_str, 0, l, (jbyte *) str);
   7.655 +        return _str;
   7.656 +    } else if (str) {
   7.657 +        return env->NewByteArray(0);
   7.658 +    } else {
   7.659 +        return (jbyteArray) NULL;
   7.660 +    }
   7.661 +}
   7.662  
   7.663 -            jlong t = callLongMethod(env, date, "getTime");
   7.664 -            //LOGD( "Set Time to : %lld", t);
   7.665 -            timestamp *ts = static_cast<timestamp*>(calloc(1, sizeof(timestamp)));
   7.666 -            assert(ts);
   7.667 -            if (ts == NULL)
   7.668 -                return NULL;
   7.669 +char *to_string(JNIEnv *env,
   7.670 +        jbyteArray str)
   7.671 +{
   7.672 +    if (str == NULL) {
   7.673 +        return NULL;
   7.674 +    }
   7.675  
   7.676 -            if (t){
   7.677 -                time_t clock = t/1000;
   7.678 -                gmtime_r(&clock, ts);
   7.679 +    size_t l = env->GetArrayLength(str);
   7.680 +    char *_str = static_cast<char *>(calloc(1, l + 1));
   7.681 +    assert(_str);
   7.682 +    env->GetByteArrayRegion(str, 0, l, (jbyte *) _str);
   7.683 +    return _str;
   7.684 +}
   7.685 +
   7.686 +jobject from_stringlist(JNIEnv *env,
   7.687 +        stringlist_t *sl)
   7.688 +{
   7.689 +    if (!sl) {
   7.690 +        return (jobject) NULL;
   7.691 +    }
   7.692  
   7.693 -                //LOGD("/* Seconds (0-60) */  TO     :%d", ts->tm_sec);    
   7.694 -                //LOGD("/* Minutes (0-59) */         :%d", ts->tm_min);    
   7.695 -                //LOGD("/* Hours (0-23) */           :%d", ts->tm_hour);   
   7.696 -                //LOGD("/* Day of the month (1-31) */:%d", ts->tm_mday);   
   7.697 -                //LOGD("/* Month (0-11) */           :%d", ts->tm_mon);    
   7.698 -                //LOGD("/* Year - 1900 */            :%d", ts->tm_year);   
   7.699 -            }
   7.700 +    jclass clazz = findClass(env, "java/util/Vector");
   7.701 +    jmethodID constructor = env->GetMethodID(clazz, "<init>", "()V");
   7.702 +    assert(constructor);
   7.703 +    jobject obj = env->NewObject(clazz, constructor);
   7.704 +    assert(obj);
   7.705  
   7.706 -            return ts;
   7.707 -        }
   7.708 +    stringlist_t *_sl;
   7.709 +    for (_sl = sl; _sl && _sl->value; _sl = _sl->next) {
   7.710 +        jobject o = from_string(env, _sl->value);
   7.711 +        callBooleanMethod(env, obj, "add", o);
   7.712 +    }
   7.713 +
   7.714 +    env->DeleteLocalRef(clazz);
   7.715 +
   7.716 +    return obj;
   7.717 +}
   7.718  
   7.719 -        static void _setStringField(JNIEnv *env, const char *classname,
   7.720 -                jobject obj, const char *name, const char *value)
   7.721 -        {
   7.722 -            if (value) {
   7.723 -                jfieldID fieldID = getFieldID(env, classname, name, "[B");
   7.724 -                env->SetObjectField(obj, fieldID, static_cast<jobject>(from_string(env, value)));
   7.725 +stringlist_t *to_stringlist(JNIEnv *env,
   7.726 +        jobject obj)
   7.727 +{
   7.728 +    if (!obj) {
   7.729 +        return NULL;
   7.730 +    }
   7.731  
   7.732 -            }
   7.733 -        }
   7.734 +    jint size = callIntMethod(env, obj, "size");
   7.735 +    if (size == 0) {
   7.736 +        return NULL;
   7.737 +    }
   7.738  
   7.739 -        //TODO: fix/generalize/clean patch added to make keysync work using globalref to class
   7.740 -        static void _setStringField(JNIEnv *env, const char *classname,
   7.741 -                jobject obj, const char *name, const char *value, const jclass clazz)
   7.742 -        {
   7.743 -            if (value) {
   7.744 -                jfieldID fieldID = getFieldID(env, classname, name, "[B", clazz);
   7.745 -                env->SetObjectField(obj, fieldID, static_cast<jobject>(from_string(env, value)));
   7.746 +    stringlist_t *sl = new_stringlist(NULL);
   7.747 +    stringlist_t *_sl;
   7.748 +    jint i;
   7.749 +    for (_sl = sl, i = 0; i < (int) size; i++) {
   7.750 +        jobject o = callObjectMethod(env, obj, "get", i);
   7.751 +        jbyteArray a = static_cast<jbyteArray>(o);
   7.752 +        char *str = to_string(env, a);
   7.753 +        _sl = stringlist_add(_sl, str);
   7.754 +        env->DeleteLocalRef(o);
   7.755 +        free(str);
   7.756 +    }
   7.757 +
   7.758 +    return sl;
   7.759 +}
   7.760  
   7.761 -            }
   7.762 -        }
   7.763 +jobject from_stringpairlist(JNIEnv *env,
   7.764 +        stringpair_list_t *sl)
   7.765 +{
   7.766 +    if (!sl) {
   7.767 +        return (jobject) NULL;
   7.768 +    }
   7.769  
   7.770 -        jobject from_identity(JNIEnv *env, pEp_identity *ident)
   7.771 -        {
   7.772 -            if (!ident)
   7.773 -                return (jobject) NULL;
   7.774 +    jclass clazz = findClass(env, "java/util/ArrayList");
   7.775 +    jclass clazz_pair = findClass(env, "foundation/pEp/jniadapter/Pair");
   7.776 +    jmethodID constructor = env->GetMethodID(clazz, "<init>", "()V");
   7.777 +    assert(constructor);
   7.778 +    jmethodID constructor_pair = env->GetMethodID(clazz_pair, "<init>", "(Ljava/lang/Object;Ljava/lang/Object;)V");
   7.779 +    assert(constructor_pair);
   7.780 +
   7.781 +    jobject obj = env->NewObject(clazz, constructor);
   7.782 +    assert(obj);
   7.783  
   7.784 -            static const char *classname = "foundation/pEp/jniadapter/_Identity";
   7.785 -            jclass clazz = findClass(env, classname);
   7.786 -            jmethodID constructor = env->GetMethodID(clazz, "<init>", "()V");
   7.787 -            assert(constructor);
   7.788 -            jobject obj = env->NewObject(clazz, constructor);
   7.789 +    stringpair_list_t *_sl;
   7.790 +    for (_sl = sl; _sl && _sl->value; _sl = _sl->next) {
   7.791 +        assert(_sl->value->key);
   7.792 +        assert(_sl->value->value);
   7.793 +
   7.794 +        jbyteArray first = from_string(env, _sl->value->key);
   7.795 +        jbyteArray second = from_string(env, _sl->value->value);
   7.796 +        jobject pair = env->NewObject(clazz_pair, constructor_pair, first, second);
   7.797 +        callBooleanMethod(env, obj, "add", pair);
   7.798 +
   7.799 +        env->DeleteLocalRef(first);
   7.800 +        env->DeleteLocalRef(second);
   7.801 +        env->DeleteLocalRef(pair);
   7.802 +    }
   7.803 +
   7.804 +    env->DeleteLocalRef(clazz);
   7.805 +    env->DeleteLocalRef(clazz_pair);
   7.806  
   7.807 -            env->DeleteLocalRef(clazz);
   7.808 -            
   7.809 -            if (ident) {
   7.810 -                _setStringField(env, classname, obj, "address", ident->address);
   7.811 -                _setStringField(env, classname, obj, "fpr", ident->fpr);
   7.812 -                _setStringField(env, classname, obj, "user_id", ident->user_id);
   7.813 -                _setStringField(env, classname, obj, "username", ident->username);
   7.814 +    return obj;
   7.815 +}
   7.816 +
   7.817 +stringpair_list_t *to_stringpairlist(JNIEnv *env,
   7.818 +        jobject obj)
   7.819 +{
   7.820 +    if (!obj) {
   7.821 +        return NULL;
   7.822 +    }
   7.823  
   7.824 -                jfieldID comm_type_id = getFieldID(env, classname, "comm_type", "I");
   7.825 -                env->SetIntField(obj, comm_type_id, static_cast<jint>(ident->comm_type));
   7.826 +    jint size = callIntMethod(env, obj, "size");
   7.827 +    if (size == 0) {
   7.828 +        return NULL;
   7.829 +    }
   7.830  
   7.831 -                _setStringField(env, classname, obj, "lang", ident->lang);
   7.832 +    jfieldID first_id = getFieldID(env, "foundation/pEp/jniadapter/Pair", "first", "Ljava/lang/Object;");
   7.833 +    jfieldID second_id = getFieldID(env, "foundation/pEp/jniadapter/Pair", "second", "Ljava/lang/Object;");
   7.834  
   7.835 -                jfieldID me_id = getFieldID(env, classname, "me", "Z");
   7.836 -                env->SetBooleanField(obj, me_id, static_cast<jboolean>(ident->me));
   7.837 +    stringpair_list_t *sl = new_stringpair_list(NULL);
   7.838 +    stringpair_list_t *_sl;
   7.839 +    jint i;
   7.840  
   7.841 -                jfieldID flags_id = getFieldID(env, classname, "flags", "I");
   7.842 -                env->SetIntField(obj, flags_id, static_cast<jint>(ident->flags));
   7.843 -            }
   7.844 +    for (_sl = sl, i = 0; i < (int) size; i++) {
   7.845 +        jobject pair = callObjectMethod(env, obj, "get", i);
   7.846 +        jbyteArray first = static_cast<jbyteArray>(env->GetObjectField(pair, first_id));
   7.847 +        jbyteArray second = static_cast<jbyteArray>(env->GetObjectField(pair, second_id));
   7.848  
   7.849 -            return obj;
   7.850 -        }
   7.851 +        char *first_str = to_string(env, first);
   7.852 +        char *second_str = to_string(env, second);
   7.853 +        stringpair_t *sp = new_stringpair(first_str, second_str);
   7.854 +        env->DeleteLocalRef(pair);
   7.855 +        free(first_str);
   7.856 +        free(second_str);
   7.857 +
   7.858 +        _sl = stringpair_list_add(_sl, sp);
   7.859 +    }
   7.860 +
   7.861 +    return sl;
   7.862 +}
   7.863  
   7.864 -        //TODO: fix/generalize/clean patch added to make keysync work using globalref to class
   7.865 -        jobject from_identity(JNIEnv *env, pEp_identity *ident, jclass identityClass)
   7.866 -        {
   7.867 -            if (!ident)
   7.868 -                return (jobject) NULL;
   7.869 +jobject from_timestamp(JNIEnv *env,
   7.870 +        timestamp *ts)
   7.871 +{
   7.872 +    if (!ts) {
   7.873 +        return NULL;
   7.874 +    }
   7.875  
   7.876 -            static const char *classname = "foundation/pEp/jniadapter/_Identity";
   7.877 -            jmethodID constructor = env->GetMethodID(identityClass, "<init>", "()V");
   7.878 -            assert(constructor);
   7.879 -            jobject obj = env->NewObject(identityClass, constructor);
   7.880 +    //LOGD("/* Seconds (0-60) */  FROM   :%d", ts->tm_sec);
   7.881 +    //LOGD("/* Minutes (0-59) */         :%d", ts->tm_min);
   7.882 +    //LOGD("/* Hours (0-23) */           :%d", ts->tm_hour);
   7.883 +    //LOGD("/* Day of the month (1-31) */:%d", ts->tm_mday);
   7.884 +    //LOGD("/* Month (0-11) */           :%d", ts->tm_mon);
   7.885 +    //LOGD("/* Year - 1900 */            :%d", ts->tm_year);
   7.886  
   7.887 -            if (ident) {
   7.888 -                _setStringField(env, classname, obj, "address", ident->address, identityClass);
   7.889 -                _setStringField(env, classname, obj, "fpr", ident->fpr, identityClass);
   7.890 -                _setStringField(env, classname, obj, "user_id", ident->user_id, identityClass);
   7.891 -                _setStringField(env, classname, obj, "username", ident->username, identityClass);
   7.892 +    time_t t = timegm(ts) * 1000;
   7.893 +    //LOGD( "TimeGM returns : %lld", t);
   7.894 +    jclass clazz = findClass(env, "java/util/Date");
   7.895 +    jmethodID constructor = env->GetMethodID(clazz, "<init>", "(J)V");
   7.896 +    assert(constructor);
   7.897 +
   7.898 +    jobject result = env->NewObject(clazz, constructor, (jlong) t);
   7.899 +
   7.900 +    env->DeleteLocalRef(clazz);
   7.901  
   7.902 -                jfieldID comm_type_id = getFieldID(env, classname, "comm_type", "I", identityClass);
   7.903 -                env->SetIntField(obj, comm_type_id, static_cast<jint>(ident->comm_type));
   7.904 +    return result;
   7.905 +}
   7.906  
   7.907 -                _setStringField(env, classname, obj, "lang", ident->lang, identityClass);
   7.908 -
   7.909 -                jfieldID me_id = getFieldID(env, classname, "me", "Z", identityClass);
   7.910 -                env->SetBooleanField(obj, me_id, static_cast<jboolean>(ident->me));
   7.911 +timestamp *to_timestamp(JNIEnv *env,
   7.912 +        jobject date)
   7.913 +{
   7.914 +    if (!date) {
   7.915 +        return NULL;
   7.916 +    }
   7.917  
   7.918 -                jfieldID flags_id = getFieldID(env, classname, "flags", "I", identityClass);
   7.919 -                env->SetIntField(obj, flags_id, static_cast<jint>(ident->flags));
   7.920 -            }
   7.921 +    jlong t = callLongMethod(env, date, "getTime");
   7.922 +    //LOGD( "Set Time to : %lld", t);
   7.923 +    timestamp *ts = static_cast<timestamp *>(calloc(1, sizeof(timestamp)));
   7.924 +    assert(ts);
   7.925 +    if (ts == NULL) {
   7.926 +        return NULL;
   7.927 +    }
   7.928  
   7.929 -            return obj;
   7.930 -        }
   7.931 +    if (t) {
   7.932 +        time_t clock = t / 1000;
   7.933 +        gmtime_r(&clock, ts);
   7.934  
   7.935 -        char *_getStringField(JNIEnv *env, const char *classname, jobject obj,
   7.936 -                const char *name)
   7.937 -        {
   7.938 -            jfieldID fieldID = getFieldID(env, classname, name, "[B");
   7.939 -            jobject fobj = env->GetObjectField(obj, fieldID);
   7.940 +        //LOGD("/* Seconds (0-60) */  TO     :%d", ts->tm_sec);
   7.941 +        //LOGD("/* Minutes (0-59) */         :%d", ts->tm_min);
   7.942 +        //LOGD("/* Hours (0-23) */           :%d", ts->tm_hour);
   7.943 +        //LOGD("/* Day of the month (1-31) */:%d", ts->tm_mday);
   7.944 +        //LOGD("/* Month (0-11) */           :%d", ts->tm_mon);
   7.945 +        //LOGD("/* Year - 1900 */            :%d", ts->tm_year);
   7.946 +    }
   7.947 +
   7.948 +    return ts;
   7.949 +}
   7.950  
   7.951 -            char *res =  to_string(env, static_cast<jbyteArray>(fobj));
   7.952 -
   7.953 -            env->DeleteLocalRef(fobj);
   7.954 -            return res;
   7.955 -        }
   7.956 -
   7.957 -        pEp_identity *to_identity(JNIEnv *env, jobject obj)
   7.958 -        {
   7.959 -            if (!obj)
   7.960 -                return NULL;
   7.961 +static void _setStringField(JNIEnv *env,
   7.962 +        const char *classname,
   7.963 +        jobject obj,
   7.964 +        const char *name,
   7.965 +        const char *value)
   7.966 +{
   7.967 +    if (value) {
   7.968 +        jfieldID fieldID = getFieldID(env, classname, name, "[B");
   7.969 +        env->SetObjectField(obj, fieldID, static_cast<jobject>(from_string(env, value)));
   7.970 +    }
   7.971 +}
   7.972  
   7.973 -            static const char *classname = "foundation/pEp/jniadapter/_Identity";
   7.974 -            pEp_identity *ident = new_identity(NULL, NULL, NULL, NULL);
   7.975 +//TODO: fix/generalize/clean patch added to make keysync work using globalref to class
   7.976 +static void _setStringField(JNIEnv *env,
   7.977 +        const char *classname,
   7.978 +        jobject obj,
   7.979 +        const char *name,
   7.980 +        const char *value,
   7.981 +        const jclass clazz)
   7.982 +{
   7.983 +    if (value) {
   7.984 +        jfieldID fieldID = getFieldID(env, classname, name, "[B", clazz);
   7.985 +        env->SetObjectField(obj, fieldID, static_cast<jobject>(from_string(env, value)));
   7.986 +    }
   7.987 +}
   7.988  
   7.989 -            ident->address = _getStringField(env, classname, obj, "address");
   7.990 -            ident->fpr = _getStringField(env, classname, obj, "fpr");
   7.991 -            ident->user_id = _getStringField(env, classname, obj, "user_id");
   7.992 -            ident->username = _getStringField(env, classname, obj, "username");
   7.993 +jobject from_identity(JNIEnv *env,
   7.994 +        pEp_identity *ident)
   7.995 +{
   7.996 +    if (!ident) {
   7.997 +        return (jobject) NULL;
   7.998 +    }
   7.999  
  7.1000 -            jfieldID comm_type_id = getFieldID(env, classname, "comm_type", "I");
  7.1001 -            ident->comm_type = static_cast<PEP_comm_type>(env->GetIntField(obj, comm_type_id));
  7.1002 +    static const char *classname = "foundation/pEp/jniadapter/_Identity";
  7.1003 +    jclass clazz = findClass(env, classname);
  7.1004 +    jmethodID constructor = env->GetMethodID(clazz, "<init>", "()V");
  7.1005 +    assert(constructor);
  7.1006 +    jobject obj = env->NewObject(clazz, constructor);
  7.1007 +
  7.1008 +    env->DeleteLocalRef(clazz);
  7.1009  
  7.1010 -            char *lang = _getStringField(env, classname, obj, "lang");
  7.1011 -            if (lang && lang[0]) {
  7.1012 -                ident->lang[0] = lang[0];
  7.1013 -                ident->lang[1] = lang[1];
  7.1014 -            }
  7.1015 -            free(lang);
  7.1016 +    if (ident) {
  7.1017 +        _setStringField(env, classname, obj, "address", ident->address);
  7.1018 +        _setStringField(env, classname, obj, "fpr", ident->fpr);
  7.1019 +        _setStringField(env, classname, obj, "user_id", ident->user_id);
  7.1020 +        _setStringField(env, classname, obj, "username", ident->username);
  7.1021 +
  7.1022 +        jfieldID comm_type_id = getFieldID(env, classname, "comm_type", "I");
  7.1023 +        env->SetIntField(obj, comm_type_id, static_cast<jint>(ident->comm_type));
  7.1024 +
  7.1025 +        _setStringField(env, classname, obj, "lang", ident->lang);
  7.1026 +
  7.1027 +        jfieldID me_id = getFieldID(env, classname, "me", "Z");
  7.1028 +        env->SetBooleanField(obj, me_id, static_cast<jboolean>(ident->me));
  7.1029  
  7.1030 -            jfieldID me_id = getFieldID(env, classname, "me", "Z");
  7.1031 -            ident->me = static_cast<bool>(env->GetBooleanField(obj, me_id));
  7.1032 -            
  7.1033 -            jfieldID flags_id = getFieldID(env, classname, "flags", "I");
  7.1034 -            ident->flags = static_cast<identity_flags_t>(env->GetIntField(obj, flags_id));
  7.1035 +        jfieldID flags_id = getFieldID(env, classname, "flags", "I");
  7.1036 +        env->SetIntField(obj, flags_id, static_cast<jint>(ident->flags));
  7.1037 +    }
  7.1038 +
  7.1039 +    return obj;
  7.1040 +}
  7.1041  
  7.1042 -            return ident;
  7.1043 -        }
  7.1044 +//TODO: fix/generalize/clean patch added to make keysync work using globalref to class
  7.1045 +jobject from_identity(JNIEnv *env,
  7.1046 +        pEp_identity *ident,
  7.1047 +        jclass identityClass)
  7.1048 +{
  7.1049 +    if (!ident) {
  7.1050 +        return (jobject) NULL;
  7.1051 +    }
  7.1052  
  7.1053 -        jobject from_identitylist(JNIEnv *env, identity_list *il)
  7.1054 -        {
  7.1055 -            if (!il)
  7.1056 -                return (jobject) NULL;
  7.1057 +    static const char *classname = "foundation/pEp/jniadapter/_Identity";
  7.1058 +    jmethodID constructor = env->GetMethodID(identityClass, "<init>", "()V");
  7.1059 +    assert(constructor);
  7.1060 +    jobject obj = env->NewObject(identityClass, constructor);
  7.1061  
  7.1062 -            jclass clazz = findClass(env, "java/util/Vector");
  7.1063 -            jmethodID constructor = env->GetMethodID(clazz, "<init>", "()V");
  7.1064 -            assert(constructor);
  7.1065 -            jobject obj = env->NewObject(clazz, constructor);
  7.1066 -            assert(obj);
  7.1067 +    if (ident) {
  7.1068 +        _setStringField(env, classname, obj, "address", ident->address, identityClass);
  7.1069 +        _setStringField(env, classname, obj, "fpr", ident->fpr, identityClass);
  7.1070 +        _setStringField(env, classname, obj, "user_id", ident->user_id, identityClass);
  7.1071 +        _setStringField(env, classname, obj, "username", ident->username, identityClass);
  7.1072 +
  7.1073 +        jfieldID comm_type_id = getFieldID(env, classname, "comm_type", "I", identityClass);
  7.1074 +        env->SetIntField(obj, comm_type_id, static_cast<jint>(ident->comm_type));
  7.1075 +
  7.1076 +        _setStringField(env, classname, obj, "lang", ident->lang, identityClass);
  7.1077 +
  7.1078 +        jfieldID me_id = getFieldID(env, classname, "me", "Z", identityClass);
  7.1079 +        env->SetBooleanField(obj, me_id, static_cast<jboolean>(ident->me));
  7.1080 +
  7.1081 +        jfieldID flags_id = getFieldID(env, classname, "flags", "I", identityClass);
  7.1082 +        env->SetIntField(obj, flags_id, static_cast<jint>(ident->flags));
  7.1083 +    }
  7.1084 +
  7.1085 +    return obj;
  7.1086 +}
  7.1087  
  7.1088 -            identity_list *_il;
  7.1089 -            for (_il = il; _il && _il->ident; _il = _il->next) {
  7.1090 -                jobject o = from_identity(env, _il->ident);
  7.1091 -                callBooleanMethod(env, obj, "add", o);
  7.1092 -            }
  7.1093 +char *_getStringField(JNIEnv *env,
  7.1094 +        const char *classname,
  7.1095 +        jobject obj,
  7.1096 +        const char *name)
  7.1097 +{
  7.1098 +    jfieldID fieldID = getFieldID(env, classname, name, "[B");
  7.1099 +    jobject fobj = env->GetObjectField(obj, fieldID);
  7.1100  
  7.1101 -            env->DeleteLocalRef(clazz);
  7.1102 +    char *res = to_string(env, static_cast<jbyteArray>(fobj));
  7.1103  
  7.1104 -            return obj;
  7.1105 -        }
  7.1106 +    env->DeleteLocalRef(fobj);
  7.1107 +    return res;
  7.1108 +}
  7.1109  
  7.1110 -        identity_list *to_identitylist(JNIEnv *env, jobject obj)
  7.1111 -        {
  7.1112 -            if (!obj)
  7.1113 -                return NULL;
  7.1114 +pEp_identity *to_identity(JNIEnv *env,
  7.1115 +        jobject obj)
  7.1116 +{
  7.1117 +    if (!obj) {
  7.1118 +        return NULL;
  7.1119 +    }
  7.1120  
  7.1121 -            jint size = callIntMethod(env, obj, "size");
  7.1122 -            if (size == 0)
  7.1123 -                return NULL;
  7.1124 +    static const char *classname = "foundation/pEp/jniadapter/_Identity";
  7.1125 +    pEp_identity *ident = new_identity(NULL, NULL, NULL, NULL);
  7.1126 +
  7.1127 +    ident->address = _getStringField(env, classname, obj, "address");
  7.1128 +    ident->fpr = _getStringField(env, classname, obj, "fpr");
  7.1129 +    ident->user_id = _getStringField(env, classname, obj, "user_id");
  7.1130 +    ident->username = _getStringField(env, classname, obj, "username");
  7.1131 +
  7.1132 +    jfieldID comm_type_id = getFieldID(env, classname, "comm_type", "I");
  7.1133 +    ident->comm_type = static_cast<PEP_comm_type>(env->GetIntField(obj, comm_type_id));
  7.1134  
  7.1135 -            identity_list *il = new_identity_list(NULL);
  7.1136 -            identity_list *_il;
  7.1137 -            jint i;
  7.1138 -            for (_il = il, i = 0; i < (int) size; i++) {
  7.1139 -                jobject o = callObjectMethod(env, obj, "get", i);
  7.1140 -                pEp_identity* ident = to_identity(env, o);
  7.1141 -                _il = identity_list_add(_il, ident);
  7.1142 -                env->DeleteLocalRef(o);
  7.1143 -            }
  7.1144 +    char *lang = _getStringField(env, classname, obj, "lang");
  7.1145 +    if (lang && lang[0]) {
  7.1146 +        ident->lang[0] = lang[0];
  7.1147 +        ident->lang[1] = lang[1];
  7.1148 +    }
  7.1149 +    free(lang);
  7.1150 +
  7.1151 +    jfieldID me_id = getFieldID(env, classname, "me", "Z");
  7.1152 +    ident->me = static_cast<bool>(env->GetBooleanField(obj, me_id));
  7.1153  
  7.1154 -            return il;
  7.1155 -        }
  7.1156 +    jfieldID flags_id = getFieldID(env, classname, "flags", "I");
  7.1157 +    ident->flags = static_cast<identity_flags_t>(env->GetIntField(obj, flags_id));
  7.1158 +
  7.1159 +    return ident;
  7.1160 +}
  7.1161 +
  7.1162 +jobject from_identitylist(JNIEnv *env,
  7.1163 +        identity_list *il)
  7.1164 +{
  7.1165 +    if (!il) {
  7.1166 +        return (jobject) NULL;
  7.1167 +    }
  7.1168  
  7.1169 -        jobject _from_blob(JNIEnv *env, bloblist_t *b)
  7.1170 -        {
  7.1171 -            if (!b)
  7.1172 -                return (jobject) NULL;
  7.1173 +    jclass clazz = findClass(env, "java/util/Vector");
  7.1174 +    jmethodID constructor = env->GetMethodID(clazz, "<init>", "()V");
  7.1175 +    assert(constructor);
  7.1176 +    jobject obj = env->NewObject(clazz, constructor);
  7.1177 +    assert(obj);
  7.1178 +
  7.1179 +    identity_list *_il;
  7.1180 +    for (_il = il; _il && _il->ident; _il = _il->next) {
  7.1181 +        jobject o = from_identity(env, _il->ident);
  7.1182 +        callBooleanMethod(env, obj, "add", o);
  7.1183 +    }
  7.1184 +
  7.1185 +    env->DeleteLocalRef(clazz);
  7.1186 +
  7.1187 +    return obj;
  7.1188 +}
  7.1189  
  7.1190 -            static const char *classname = "foundation/pEp/jniadapter/_Blob";
  7.1191 -            jclass clazz = findClass(env, classname);
  7.1192 -            jmethodID constructor = env->GetMethodID(clazz, "<init>", "()V");
  7.1193 -            assert(constructor);
  7.1194 -            jobject obj = env->NewObject(clazz, constructor);
  7.1195 -            
  7.1196 -            env->DeleteLocalRef(clazz);
  7.1197 +identity_list *to_identitylist(JNIEnv *env,
  7.1198 +        jobject obj)
  7.1199 +{
  7.1200 +    if (!obj) {
  7.1201 +        return NULL;
  7.1202 +    }
  7.1203 +
  7.1204 +    jint size = callIntMethod(env, obj, "size");
  7.1205 +    if (size == 0) {
  7.1206 +        return NULL;
  7.1207 +    }
  7.1208  
  7.1209 -            jfieldID fieldID = getFieldID(env, classname, "data", "[B");
  7.1210 -            jbyteArray _data = env->NewByteArray(static_cast<jsize>(b->size));
  7.1211 -            env->SetByteArrayRegion(_data, 0, b->size, reinterpret_cast<jbyte*>(b->value));
  7.1212 -            env->SetObjectField(obj, fieldID, static_cast<jobject>(_data));
  7.1213 -            _setStringField(env, classname, obj, "mime_type", b->mime_type);
  7.1214 -            _setStringField(env, classname, obj, "filename", b->filename);
  7.1215 +    identity_list *il = new_identity_list(NULL);
  7.1216 +    identity_list *_il;
  7.1217 +    jint i;
  7.1218 +    for (_il = il, i = 0; i < (int) size; i++) {
  7.1219 +        jobject o = callObjectMethod(env, obj, "get", i);
  7.1220 +        pEp_identity *ident = to_identity(env, o);
  7.1221 +        _il = identity_list_add(_il, ident);
  7.1222 +        env->DeleteLocalRef(o);
  7.1223 +    }
  7.1224  
  7.1225 -            return obj;
  7.1226 -        }
  7.1227 +    return il;
  7.1228 +}
  7.1229  
  7.1230 -        jobject from_bloblist(JNIEnv *env, bloblist_t *bl)
  7.1231 -        {
  7.1232 -            if (!bl)
  7.1233 -                return (jobject) NULL;
  7.1234 +jobject _from_blob(JNIEnv *env,
  7.1235 +        bloblist_t *b)
  7.1236 +{
  7.1237 +    if (!b) {
  7.1238 +        return (jobject) NULL;
  7.1239 +    }
  7.1240  
  7.1241 -            jclass clazz = findClass(env, "java/util/Vector");
  7.1242 -            jmethodID constructor = env->GetMethodID(clazz, "<init>", "()V");
  7.1243 -            assert(constructor);
  7.1244 -            jobject obj = env->NewObject(clazz, constructor);
  7.1245 -            assert(obj);
  7.1246 +    static const char *classname = "foundation/pEp/jniadapter/_Blob";
  7.1247 +    jclass clazz = findClass(env, classname);
  7.1248 +    jmethodID constructor = env->GetMethodID(clazz, "<init>", "()V");
  7.1249 +    assert(constructor);
  7.1250 +    jobject obj = env->NewObject(clazz, constructor);
  7.1251 +
  7.1252 +    env->DeleteLocalRef(clazz);
  7.1253  
  7.1254 -            env->DeleteLocalRef(clazz);
  7.1255 +    jfieldID fieldID = getFieldID(env, classname, "data", "[B");
  7.1256 +    jbyteArray _data = env->NewByteArray(static_cast<jsize>(b->size));
  7.1257 +    env->SetByteArrayRegion(_data, 0, b->size, reinterpret_cast<jbyte *>(b->value));
  7.1258 +    env->SetObjectField(obj, fieldID, static_cast<jobject>(_data));
  7.1259 +    _setStringField(env, classname, obj, "mime_type", b->mime_type);
  7.1260 +    _setStringField(env, classname, obj, "filename", b->filename);
  7.1261 +
  7.1262 +    return obj;
  7.1263 +}
  7.1264  
  7.1265 -            bloblist_t *_bl;
  7.1266 -            for (_bl = bl; _bl && _bl->value; _bl = _bl->next) {
  7.1267 -                jobject o = _from_blob(env, _bl);
  7.1268 -                if(o)
  7.1269 -                    callBooleanMethod(env, obj, "add", o);
  7.1270 -            }
  7.1271 +jobject from_bloblist(JNIEnv *env,
  7.1272 +        bloblist_t *bl)
  7.1273 +{
  7.1274 +    if (!bl) {
  7.1275 +        return (jobject) NULL;
  7.1276 +    }
  7.1277  
  7.1278 -            return obj;
  7.1279 -        }
  7.1280 +    jclass clazz = findClass(env, "java/util/Vector");
  7.1281 +    jmethodID constructor = env->GetMethodID(clazz, "<init>", "()V");
  7.1282 +    assert(constructor);
  7.1283 +    jobject obj = env->NewObject(clazz, constructor);
  7.1284 +    assert(obj);
  7.1285  
  7.1286 -        bloblist_t *to_blob(JNIEnv *env, jobject obj)
  7.1287 -        {
  7.1288 -            if (!obj)
  7.1289 -                return NULL;
  7.1290 +    env->DeleteLocalRef(clazz);
  7.1291 +
  7.1292 +    bloblist_t *_bl;
  7.1293 +    for (_bl = bl; _bl && _bl->value; _bl = _bl->next) {
  7.1294 +        jobject o = _from_blob(env, _bl);
  7.1295 +        if (o) {
  7.1296 +            callBooleanMethod(env, obj, "add", o);
  7.1297 +        }
  7.1298 +    }
  7.1299  
  7.1300 -            static const char *classname = "foundation/pEp/jniadapter/_Blob";
  7.1301 -            jclass clazz = findClass(env, classname);
  7.1302 +    return obj;
  7.1303 +}
  7.1304  
  7.1305 -            char *mime_type = _getStringField(env, classname, obj, "mime_type");
  7.1306 -            char *filename = _getStringField(env, classname, obj, "filename");
  7.1307 +bloblist_t *to_blob(JNIEnv *env,
  7.1308 +        jobject obj)
  7.1309 +{
  7.1310 +    if (!obj) {
  7.1311 +        return NULL;
  7.1312 +    }
  7.1313  
  7.1314 -            jfieldID data_id = getFieldID(env, classname, "data", "[B");
  7.1315 -            jbyteArray _data = static_cast<jbyteArray>(env->GetObjectField(obj, data_id));
  7.1316 -            size_t size = static_cast<size_t>(env->GetArrayLength(_data));
  7.1317 -            char *b = static_cast<char *>(malloc(size));
  7.1318 -            assert(b);
  7.1319 +    static const char *classname = "foundation/pEp/jniadapter/_Blob";
  7.1320 +    jclass clazz = findClass(env, classname);
  7.1321 +
  7.1322 +    char *mime_type = _getStringField(env, classname, obj, "mime_type");
  7.1323 +    char *filename = _getStringField(env, classname, obj, "filename");
  7.1324  
  7.1325 -            env->GetByteArrayRegion(_data, 0, size, reinterpret_cast<jbyte*>(b));
  7.1326 -            bloblist_t *bl = new_bloblist( b, size, mime_type, filename);
  7.1327 +    jfieldID data_id = getFieldID(env, classname, "data", "[B");
  7.1328 +    jbyteArray _data = static_cast<jbyteArray>(env->GetObjectField(obj, data_id));
  7.1329 +    size_t size = static_cast<size_t>(env->GetArrayLength(_data));
  7.1330 +    char *b = static_cast<char *>(malloc(size));
  7.1331 +    assert(b);
  7.1332  
  7.1333 -            free(mime_type);
  7.1334 -            free(filename);
  7.1335 -            return bl;
  7.1336 -        }
  7.1337 +    env->GetByteArrayRegion(_data, 0, size, reinterpret_cast<jbyte *>(b));
  7.1338 +    bloblist_t *bl = new_bloblist(b, size, mime_type, filename);
  7.1339 +
  7.1340 +    free(mime_type);
  7.1341 +    free(filename);
  7.1342 +    return bl;
  7.1343 +}
  7.1344  
  7.1345  
  7.1346 -        bloblist_t *to_bloblist(JNIEnv *env, jobject obj)
  7.1347 -        {
  7.1348 -            if (!obj)
  7.1349 -                return NULL;
  7.1350 +bloblist_t *to_bloblist(JNIEnv *env,
  7.1351 +        jobject obj)
  7.1352 +{
  7.1353 +    if (!obj) {
  7.1354 +        return NULL;
  7.1355 +    }
  7.1356  
  7.1357 -            jint size = callIntMethod(env, obj, "size");
  7.1358 -            if (size == 0)
  7.1359 -                return NULL;
  7.1360 +    jint size = callIntMethod(env, obj, "size");
  7.1361 +    if (size == 0) {
  7.1362 +        return NULL;
  7.1363 +    }
  7.1364  
  7.1365 -            bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL);
  7.1366 -            bloblist_t *_bl;
  7.1367 -            _bl = bl;
  7.1368 -            jint i;
  7.1369 -            for (i = 0; i < (int) size; i++) {
  7.1370 -                jobject o = callObjectMethod(env, obj, "get", i);
  7.1371 -                bloblist_t *b = to_blob(env, o);
  7.1372 -                _bl = bloblist_add(_bl, b->value, b->size, b->mime_type, b->filename);
  7.1373 -                env->DeleteLocalRef(o);
  7.1374 -            }
  7.1375 +    bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL);
  7.1376 +    bloblist_t *_bl;
  7.1377 +    _bl = bl;
  7.1378 +    jint i;
  7.1379 +    for (i = 0; i < (int) size; i++) {
  7.1380 +        jobject o = callObjectMethod(env, obj, "get", i);
  7.1381 +        bloblist_t *b = to_blob(env, o);
  7.1382 +        _bl = bloblist_add(_bl, b->value, b->size, b->mime_type, b->filename);
  7.1383 +        env->DeleteLocalRef(o);
  7.1384 +    }
  7.1385  
  7.1386 -            return bl;
  7.1387 -        }
  7.1388 +    return bl;
  7.1389 +}
  7.1390  
  7.1391 -        PEP_enc_format to_EncFormat(JNIEnv *env, jobject obj)
  7.1392 -        {
  7.1393 -            static const char *classname = "foundation/pEp/jniadapter/Message$EncFormat";
  7.1394 -            jclass clazz_enc_format = findClass(env, classname);
  7.1395 -            jfieldID field_value = env->GetFieldID(clazz_enc_format, "value", "I");
  7.1396 -            assert(field_value);
  7.1397 +PEP_enc_format to_EncFormat(JNIEnv *env,
  7.1398 +        jobject obj)
  7.1399 +{
  7.1400 +    static const char *classname = "foundation/pEp/jniadapter/Message$EncFormat";
  7.1401 +    jclass clazz_enc_format = findClass(env, classname);
  7.1402 +    jfieldID field_value = env->GetFieldID(clazz_enc_format, "value", "I");
  7.1403 +    assert(field_value);
  7.1404  
  7.1405 -            env->DeleteLocalRef(clazz_enc_format);
  7.1406 -            return static_cast<PEP_enc_format>(env->GetIntField(obj, field_value));
  7.1407 -        }
  7.1408 +    env->DeleteLocalRef(clazz_enc_format);
  7.1409 +    return static_cast<PEP_enc_format>(env->GetIntField(obj, field_value));
  7.1410 +}
  7.1411  
  7.1412 -        PEP_CIPHER_SUITE to_CipherSuite(JNIEnv *env, jobject obj)
  7.1413 -        {
  7.1414 -            static const char *classname = "foundation/pEp/jniadapter/CipherSuite";
  7.1415 -            jclass clazz_enc_format = findClass(env, classname);
  7.1416 -            jfieldID field_value = env->GetFieldID(clazz_enc_format, "value", "I");
  7.1417 -            assert(field_value);
  7.1418 +PEP_CIPHER_SUITE to_CipherSuite(JNIEnv *env,
  7.1419 +        jobject obj)
  7.1420 +{
  7.1421 +    static const char *classname = "foundation/pEp/jniadapter/CipherSuite";
  7.1422 +    jclass clazz_enc_format = findClass(env, classname);
  7.1423 +    jfieldID field_value = env->GetFieldID(clazz_enc_format, "value", "I");
  7.1424 +    assert(field_value);
  7.1425  
  7.1426 -            env->DeleteLocalRef(clazz_enc_format);
  7.1427 -            return static_cast<PEP_CIPHER_SUITE>(env->GetIntField(obj, field_value));
  7.1428 -        }
  7.1429 +    env->DeleteLocalRef(clazz_enc_format);
  7.1430 +    return static_cast<PEP_CIPHER_SUITE>(env->GetIntField(obj, field_value));
  7.1431 +}
  7.1432  
  7.1433 -        sync_handshake_result to_SyncHandshakeResult(JNIEnv *env, jobject obj)
  7.1434 -        {
  7.1435 -            static const char *classname = "foundation/pEp/jniadapter/SyncHandshakeResult";
  7.1436 -            jclass clazz_enc_format = findClass(env, classname);
  7.1437 -            jfieldID field_value = env->GetFieldID(clazz_enc_format, "value", "I");
  7.1438 -            assert(field_value);
  7.1439 +sync_handshake_result to_SyncHandshakeResult(JNIEnv *env,
  7.1440 +        jobject obj)
  7.1441 +{
  7.1442 +    static const char *classname = "foundation/pEp/jniadapter/SyncHandshakeResult";
  7.1443 +    jclass clazz_enc_format = findClass(env, classname);
  7.1444 +    jfieldID field_value = env->GetFieldID(clazz_enc_format, "value", "I");
  7.1445 +    assert(field_value);
  7.1446  
  7.1447 -            env->DeleteLocalRef(clazz_enc_format);
  7.1448 -            return static_cast<sync_handshake_result>(env->GetIntField(obj, field_value));
  7.1449 -        }
  7.1450 -    };
  7.1451 +    env->DeleteLocalRef(clazz_enc_format);
  7.1452 +    return static_cast<sync_handshake_result>(env->GetIntField(obj, field_value));
  7.1453 +}
  7.1454 +};
  7.1455  };
  7.1456  
     8.1 --- a/src/cxx/jniutils.hh	Mon Nov 30 22:05:23 2020 +0100
     8.2 +++ b/src/cxx/jniutils.hh	Mon Nov 30 23:57:34 2020 +0100
     8.3 @@ -1,4 +1,5 @@
     8.4  #pragma once
     8.5 +
     8.6  #include <unordered_map>
     8.7  #include <thread>
     8.8  #include <mutex>
     8.9 @@ -19,112 +20,128 @@
    8.10  #endif
    8.11  
    8.12  namespace pEp {
    8.13 -    namespace JNIAdapter {
    8.14 -        // Global mutex needs to be locked in all constructors which insert their own mutex object
    8.15 -        // into the unordered_map (which is thread safe for read, but not for write)
    8.16 -        extern std::mutex global_mutex;
    8.17 +namespace JNIAdapter {
    8.18 +// Global mutex needs to be locked in all constructors which insert their own mutex object
    8.19 +// into the unordered_map (which is thread safe for read, but not for write)
    8.20 +extern std::mutex global_mutex;
    8.21  
    8.22 -        // Stores mutex per java object
    8.23 -        extern std::unordered_map<long, std::mutex*> engine_objid_mutex;
    8.24 +// Stores mutex per java object
    8.25 +extern std::unordered_map<long, std::mutex *> engine_objid_mutex;
    8.26  
    8.27 -        // needs to be called after create_engine_java_object_mutex()
    8.28 -        // and before release_engine_java_object_mutex()
    8.29 -        // Thread safe
    8.30 -        std::mutex* get_engine_java_object_mutex(
    8.31 -                JNIEnv *env,
    8.32 -                jobject me
    8.33 -            );
    8.34 +// needs to be called after create_engine_java_object_mutex()
    8.35 +// and before release_engine_java_object_mutex()
    8.36 +// Thread safe
    8.37 +std::mutex *get_engine_java_object_mutex(JNIEnv *env,
    8.38 +        jobject me);
    8.39  
    8.40 -        // Needs to be called exactly once per obj, in the constructor of the obj
    8.41 -        // You need to lock a global mutex before calling this function (write to unordered_map)
    8.42 -        void create_engine_java_object_mutex(
    8.43 -                JNIEnv *env,
    8.44 -                jobject me
    8.45 -            );
    8.46 +// Needs to be called exactly once per obj, in the constructor of the obj
    8.47 +// You need to lock a global mutex before calling this function (write to unordered_map)
    8.48 +void create_engine_java_object_mutex(JNIEnv *env,
    8.49 +        jobject me);
    8.50  
    8.51 -        // Needs to be called exactly once per obj, in the destructor of this obj
    8.52 -        // You need to lock a global mutex before calling this function (write to unordered_map)
    8.53 -        void release_engine_java_object_mutex(
    8.54 -                JNIEnv *env,
    8.55 -                jobject me
    8.56 -            );
    8.57 +// Needs to be called exactly once per obj, in the destructor of this obj
    8.58 +// You need to lock a global mutex before calling this function (write to unordered_map)
    8.59 +void release_engine_java_object_mutex(JNIEnv *env,
    8.60 +        jobject me);
    8.61  
    8.62  
    8.63 -        jclass findClass(JNIEnv *env, const char *classname);
    8.64 +jclass findClass(JNIEnv *env,
    8.65 +        const char *classname);
    8.66 +
    8.67 +jfieldID getFieldID(JNIEnv *env,
    8.68 +        const char *classname,
    8.69 +        const char *fieldname,
    8.70 +        const char *signature);
    8.71  
    8.72 -        jfieldID getFieldID(
    8.73 -                JNIEnv *env,
    8.74 -                const char *classname,
    8.75 -                const char *fieldname,
    8.76 -                const char *signature
    8.77 -            );
    8.78 +jfieldID getFieldID(JNIEnv *env,
    8.79 +        const char *classname,
    8.80 +        const char *fieldname,
    8.81 +        const char *signature,
    8.82 +        const jclass clazz);
    8.83  
    8.84 -            jfieldID getFieldID(
    8.85 -                JNIEnv *env,
    8.86 -                const char *classname,
    8.87 -                const char *fieldname,
    8.88 -                const char *signature,
    8.89 -                const jclass clazz
    8.90 -            );
    8.91 +jint callIntMethod(JNIEnv *env,
    8.92 +        jobject obj,
    8.93 +        const char *methodname);
    8.94 +
    8.95 +jlong callLongMethod(JNIEnv *env,
    8.96 +        jobject obj,
    8.97 +        const char *methodname);
    8.98  
    8.99 -        jint callIntMethod(
   8.100 -                JNIEnv *env,
   8.101 -                jobject obj,
   8.102 -                const char *methodname
   8.103 -            );
   8.104 +jobject callObjectMethod(JNIEnv *env,
   8.105 +        jobject obj,
   8.106 +        const char *methodname,
   8.107 +        jint index);
   8.108 +
   8.109 +jboolean callBooleanMethod(JNIEnv *env,
   8.110 +        jobject obj,
   8.111 +        const char *methodname,
   8.112 +        jobject o);
   8.113 +
   8.114 +jint outOfMemory(JNIEnv *env);
   8.115  
   8.116 -        jlong callLongMethod(
   8.117 -                JNIEnv *env,
   8.118 -                jobject obj,
   8.119 -                const char *methodname
   8.120 -            );
   8.121 +jobject from_Integer(JNIEnv *env,
   8.122 +        int val);
   8.123 +
   8.124 +int to_Integer(JNIEnv *env,
   8.125 +        jobject obj);
   8.126  
   8.127 -        jobject callObjectMethod(
   8.128 -                JNIEnv *env,
   8.129 -                jobject obj,
   8.130 -                const char *methodname,
   8.131 -                jint index
   8.132 -            );
   8.133 +jbyteArray from_string(JNIEnv *env,
   8.134 +        const char *str);
   8.135 +
   8.136 +char *to_string(JNIEnv *env,
   8.137 +        jbyteArray str);
   8.138 +
   8.139 +jobject from_stringlist(JNIEnv *env,
   8.140 +        stringlist_t *sl);
   8.141  
   8.142 -        jboolean callBooleanMethod(
   8.143 -                JNIEnv *env,
   8.144 -                jobject obj,
   8.145 -                const char *methodname,
   8.146 -                jobject o
   8.147 -            );
   8.148 +stringlist_t *to_stringlist(JNIEnv *env,
   8.149 +        jobject obj);
   8.150 +
   8.151 +jobject from_stringpairlist(JNIEnv *env,
   8.152 +        stringpair_list_t *sl);
   8.153  
   8.154 -        jint outOfMemory(JNIEnv *env);
   8.155 +stringpair_list_t *to_stringpairlist(JNIEnv *env,
   8.156 +        jobject obj);
   8.157 +
   8.158 +jobject from_timestamp(JNIEnv *env,
   8.159 +        timestamp *ts);
   8.160  
   8.161 -        jobject from_Integer(JNIEnv *env, int val);
   8.162 -        int to_Integer(JNIEnv *env, jobject obj);
   8.163 +timestamp *to_timestamp(JNIEnv *env,
   8.164 +        jobject date);
   8.165 +
   8.166 +jobject from_identity(JNIEnv *env,
   8.167 +        pEp_identity *ident);
   8.168  
   8.169 -        jbyteArray from_string(JNIEnv *env, const char *str);
   8.170 -        char *to_string(JNIEnv *env, jbyteArray str);
   8.171 +jobject from_identity(JNIEnv *env,
   8.172 +        pEp_identity *ident,
   8.173 +        jclass identityClass);
   8.174  
   8.175 -        jobject from_stringlist(JNIEnv *env, stringlist_t *sl);
   8.176 -        stringlist_t *to_stringlist(JNIEnv *env, jobject obj);
   8.177 +pEp_identity *to_identity(JNIEnv *env,
   8.178 +        jobject obj);
   8.179  
   8.180 -        jobject from_stringpairlist(JNIEnv *env, stringpair_list_t *sl);
   8.181 -        stringpair_list_t *to_stringpairlist(JNIEnv *env, jobject obj);
   8.182 +jobject from_identitylist(JNIEnv *env,
   8.183 +        identity_list *il);
   8.184 +
   8.185 +identity_list *to_identitylist(JNIEnv *env,
   8.186 +        jobject obj);
   8.187  
   8.188 -        jobject from_timestamp(JNIEnv *env, timestamp *ts);
   8.189 -        timestamp *to_timestamp(JNIEnv *env, jobject date);
   8.190 +jobject from_bloblist(JNIEnv *env,
   8.191 +        bloblist_t *bl);
   8.192  
   8.193 -        jobject from_identity(JNIEnv *env, pEp_identity *ident);
   8.194 -        jobject from_identity(JNIEnv *env, pEp_identity *ident, jclass identityClass);
   8.195 -        pEp_identity *to_identity(JNIEnv *env, jobject obj);
   8.196 +bloblist_t *to_blob(JNIEnv *env,
   8.197 +        jobject obj);
   8.198  
   8.199 -        jobject from_identitylist(JNIEnv *env, identity_list *il);
   8.200 -        identity_list *to_identitylist(JNIEnv *env, jobject obj);
   8.201 +bloblist_t *to_bloblist(JNIEnv *env,
   8.202 +        jobject obj);
   8.203 +
   8.204 +PEP_enc_format to_EncFormat(JNIEnv *env,
   8.205 +        jobject obj);
   8.206  
   8.207 -        jobject from_bloblist(JNIEnv *env, bloblist_t *bl);
   8.208 -        bloblist_t *to_blob(JNIEnv *env, jobject obj);
   8.209 -        bloblist_t *to_bloblist(JNIEnv *env, jobject obj);
   8.210 +PEP_CIPHER_SUITE to_CipherSuite(JNIEnv *env,
   8.211 +        jobject obj);
   8.212  
   8.213 -        PEP_enc_format to_EncFormat(JNIEnv *env, jobject obj);
   8.214 -        PEP_CIPHER_SUITE to_CipherSuite(JNIEnv *env, jobject obj);
   8.215 -
   8.216 -        sync_handshake_result to_SyncHandshakeResult(JNIEnv *env, jobject obj);
   8.217 -    };
   8.218 +sync_handshake_result to_SyncHandshakeResult(JNIEnv *env,
   8.219 +        jobject obj);
   8.220 +};
   8.221  };
   8.222  
     9.1 --- a/src/cxx/passphrase_callback.hh	Mon Nov 30 22:05:23 2020 +0100
     9.2 +++ b/src/cxx/passphrase_callback.hh	Mon Nov 30 23:57:34 2020 +0100
     9.3 @@ -1,14 +1,15 @@
     9.4  #pragma once
     9.5 +
     9.6  #include <pEp/passphrase_cache.hh>
     9.7  
     9.8  
     9.9  namespace pEp {
    9.10 -    namespace JNIAdapter {
    9.11 +namespace JNIAdapter {
    9.12  
    9.13 -        char* passphraseRequiredCallback(const PEP_STATUS status);
    9.14 +char *passphraseRequiredCallback(const PEP_STATUS status);
    9.15 +template<typename... A> PEP_STATUS passphraseWrap(PEP_STATUS f(PEP_SESSION, A...), PEP_SESSION session, A... a);
    9.16  
    9.17 -        template<typename... A> PEP_STATUS passphraseWrap(PEP_STATUS f(PEP_SESSION, A...), PEP_SESSION session, A... a);
    9.18 -    }
    9.19 +}
    9.20  }
    9.21  
    9.22  #include "passphrase_callback.hxx"
    9.23 \ No newline at end of file
    10.1 --- a/src/cxx/passphrase_callback.hxx	Mon Nov 30 22:05:23 2020 +0100
    10.2 +++ b/src/cxx/passphrase_callback.hxx	Mon Nov 30 23:57:34 2020 +0100
    10.3 @@ -3,40 +3,40 @@
    10.4  #include "passphrase_callback.hh"
    10.5  
    10.6  namespace pEp {
    10.7 -    namespace JNIAdapter {
    10.8 +namespace JNIAdapter {
    10.9  
   10.10 -        template<typename... A> PEP_STATUS passphraseWrap(PEP_STATUS f(PEP_SESSION, A...), PEP_SESSION session, A... a) {
   10.11 -            pEpLog("cached passphrase mode");
   10.12 -            bool retryAgain = false;
   10.13 -            int maxRetries = 3;
   10.14 -            int retryCount = 0;
   10.15 -            PEP_STATUS status;
   10.16 -            do {
   10.17 -                // the actual target function
   10.18 -                pEpLog("calling passphrase_cache.api from basic_api");
   10.19 -                status = passphrase_cache.api(f, session, a...);
   10.20 -                pEpLog("PEP_STATUS:" << status);
   10.21 -                if (status == PEP_PASSPHRASE_REQUIRED ||
   10.22 -                    status == PEP_WRONG_PASSPHRASE ||
   10.23 -                    status == PEP_PASSPHRASE_FOR_NEW_KEYS_REQUIRED)
   10.24 -                {
   10.25 -                    pEpLog("none of the cached passphrases worked");
   10.26 -                    if (retryCount < maxRetries) {
   10.27 -                        // call the app
   10.28 -                        char *_passphrase = passphraseRequiredCallback(status);
   10.29 -                        pEpLog("callback returned, config_passphrase() with new passphrase");
   10.30 -                        PEP_STATUS status = ::config_passphrase(session, passphrase_cache.add(_passphrase));
   10.31 -                        retryAgain = true;
   10.32 -                        retryCount++;
   10.33 -                    } else {
   10.34 -                        pEpLog("max retries reached:" << maxRetries);
   10.35 -                        retryAgain = false;
   10.36 -                    }
   10.37 -                } else {
   10.38 -                    retryAgain = false;
   10.39 -                }
   10.40 -            } while (retryAgain);
   10.41 -            return status;
   10.42 +template<typename... A> PEP_STATUS passphraseWrap(PEP_STATUS f(PEP_SESSION, A...), PEP_SESSION session, A... a) {
   10.43 +    pEpLog("cached passphrase mode");
   10.44 +    bool retryAgain = false;
   10.45 +    int maxRetries = 3;
   10.46 +    int retryCount = 0;
   10.47 +    PEP_STATUS status;
   10.48 +    do {
   10.49 +        // the actual target function
   10.50 +        pEpLog("calling passphrase_cache.api from basic_api");
   10.51 +        status = passphrase_cache.api(f, session, a...);
   10.52 +        pEpLog("PEP_STATUS:" << status);
   10.53 +        if (status == PEP_PASSPHRASE_REQUIRED ||
   10.54 +            status == PEP_WRONG_PASSPHRASE ||
   10.55 +            status == PEP_PASSPHRASE_FOR_NEW_KEYS_REQUIRED)
   10.56 +        {
   10.57 +            pEpLog("none of the cached passphrases worked");
   10.58 +            if (retryCount < maxRetries) {
   10.59 +                // call the app
   10.60 +                char *_passphrase = passphraseRequiredCallback(status);
   10.61 +                pEpLog("callback returned, config_passphrase() with new passphrase");
   10.62 +                PEP_STATUS status = ::config_passphrase(session, passphrase_cache.add(_passphrase));
   10.63 +                retryAgain = true;
   10.64 +                retryCount++;
   10.65 +            } else {
   10.66 +                pEpLog("max retries reached:" << maxRetries);
   10.67 +                retryAgain = false;
   10.68 +            }
   10.69 +        } else {
   10.70 +            retryAgain = false;
   10.71          }
   10.72 -    }
   10.73 +    } while (retryAgain);
   10.74 +    return status;
   10.75 +}
   10.76 +}
   10.77  }
   10.78 \ No newline at end of file