merge in JNI-106 - Java Interfaces And Project Structure Release_2.1.0-RC13
authorheck <heck@pep.foundation>
Fri, 28 Aug 2020 18:18:26 +0200
changeset 932c9c4a76bd662
parent 926 38f14a4ba8b8
parent 931 fafa74cb1056
child 933 0bb230306a90
merge in JNI-106 - Java Interfaces And Project Structure
     1.1 --- a/.hgignore	Tue Aug 18 21:57:06 2020 +0200
     1.2 +++ b/.hgignore	Fri Aug 28 18:18:26 2020 +0200
     1.3 @@ -3,44 +3,52 @@
     1.4  # ignore all of them
     1.5  
     1.6  *.class
     1.7 -*.jar
     1.8 -*.swp
     1.9 -*.o
    1.10 -*.a
    1.11 -*.dylib
    1.12 -*.so
    1.13 -*.orig
    1.14 -*~
    1.15 +*.log
    1.16  .DS_Store
    1.17 -*.db
    1.18 -*.log
    1.19 +
    1.20 +# ignore config files
    1.21 +local.conf
    1.22 +
    1.23 +# ignore build and dist dirs
    1.24 +build/*
    1.25 +dist/*
    1.26  
    1.27  # ignore generated files
    1.28 +status_list.yml2
    1.29 +passphrase_status_list.yml2
    1.30  
    1.31 -org_pEp_*.h
    1.32 -org_pEp_*.cc
    1.33 -pEp_*.h
    1.34 -pEp_*.cc
    1.35 -org/pEp/jniadapter/pEp*.java
    1.36 -foundation_pEp_*.h
    1.37 -foundation_pEp_*.cc
    1.38 -foundation/pEp/jniadapter/pEp*.java
    1.39 +foundation_pEp_jniadapter__Blob.h
    1.40 +foundation_pEp_jniadapter_AbstractEngine.h
    1.41 +foundation_pEp_jniadapter_Engine.*
    1.42 +foundation_pEp_jniadapter_Message.*
    1.43 +foundation_pEp_jniadapter_Identity.h
    1.44 +throw_pEp_exception.*
    1.45 +src/java/foundation/pEp/jniadapter/exceptions/*.java
    1.46  
    1.47 -pEp/jniadapter/pEp*.java
    1.48 -throw_pEp_exception.*
    1.49 +CipherSuite.java
    1.50  Color.java
    1.51 +DecryptFlags.java
    1.52  Engine.java
    1.53 +EngineInterface.java
    1.54 +IdentityFlags.java
    1.55  Message.java
    1.56 -DecryptFlags.java
    1.57 +MessageInterface.java
    1.58  Rating.java
    1.59  Status.java
    1.60  SyncHandshakeResult.java
    1.61 -IdentityFlags.java
    1.62  SyncHandshakeSignal.java
    1.63 -CipherSuite.java
    1.64 +PassphraseType.java
    1.65  
    1.66 -passphrase_status_list.yml2
    1.67 -status_list.yml2
    1.68 +# ignore pep-homes
    1.69 +test/resources/per-user-dirs/alice/.pEp
    1.70 +test/resources/per-user-dirs/bob/.pEp
    1.71 +test/resources/per-user-dirs/carol/.pEp
    1.72 +
    1.73 +# FsMsgQueue data
    1.74 +test/resources/fsmsgqueue-test/Alice
    1.75 +test/resources/fsmsgqueue-test/Bob
    1.76 +test/resources/fsmsgqueue-test/Carol
    1.77 +
    1.78  
    1.79  ndk*/
    1.80  # ignore vim workspace
    1.81 @@ -78,15 +86,12 @@
    1.82  androidTests/gradle/wrapper/gradle-wrapper.properties
    1.83  androidTests/gradlew
    1.84  androidTests/gradlew.bat
    1.85 -src/org.tar.gz
    1.86 -src/local.conf
    1.87 +org.tar.gz
    1.88  
    1.89  *.iml
    1.90  
    1.91  syntax: regexp
    1.92  android/external/[^.]*
    1.93  
    1.94 -local.conf
    1.95 -src/local.conf
    1.96  # Default ignored files
    1.97  .idea/
     2.1 --- a/Makefile	Tue Aug 18 21:57:06 2020 +0200
     2.2 +++ b/Makefile	Fri Aug 28 18:18:26 2020 +0200
     2.3 @@ -3,7 +3,7 @@
     2.4  # This file may be used under the terms of the GNU General Public License version 3
     2.5  # see LICENSE.txt
     2.6  
     2.7 -.PHONY: all src test clean doxy-all doxy-cxx doxy-java doxy-clean
     2.8 +.PHONY: all src test clean doxy-all doxy-cxx doxy-java doxy-clean install uninstall
     2.9  
    2.10  all: src
    2.11  
    2.12 @@ -29,3 +29,10 @@
    2.13  doxy-clean:
    2.14  	rm -rf doc/doxygen/cxx
    2.15  	rm -rf doc/doxygen/java
    2.16 +
    2.17 +install:
    2.18 +	$(MAKE) -C src install
    2.19 +
    2.20 +uninstall:
    2.21 +	$(MAKE) -C src uninstall
    2.22 +
     3.1 --- a/Makefile.conf	Tue Aug 18 21:57:06 2020 +0200
     3.2 +++ b/Makefile.conf	Fri Aug 28 18:18:26 2020 +0200
     3.3 @@ -9,14 +9,15 @@
     3.4  # DEV ENV PATHS & CFG
     3.5  # The dev environment paths and configs are set to a default value which can be overridden by ./local.conf and overridden again by <subdir>/local.conf
     3.6  ######### Build Config Defaults #########
     3.7 -DEBUG=1
     3.8 -YML2_PATH=$(HOME)/yml2
     3.9 +DEBUG=0
    3.10 +PREFIX=$(HOME)
    3.11 +YML2_PATH=$(PREFIX)/src/yml2
    3.12  YML2_PROC=$(YML2_PATH)/yml2proc $(YML2_OPTS)
    3.13  YML2_OPTS=--encoding=utf8
    3.14 -ENGINE_LIB_PATH=$(HOME)/lib
    3.15 -ENGINE_INC_PATH=$(HOME)/include
    3.16 -AD_LIB_PATH=$(HOME)/lib
    3.17 -AD_INC_PATH=$(HOME)/include
    3.18 +ENGINE_LIB_PATH=$(PREFIX)/lib
    3.19 +ENGINE_INC_PATH=$(PREFIX)/include
    3.20 +AD_LIB_PATH=$(PREFIX)/lib
    3.21 +AD_INC_PATH=$(PREFIX)/include
    3.22  
    3.23  ### Guessing JAVA_HOME
    3.24  ifeq ($(PLATFORM),linux)
    3.25 @@ -24,6 +25,12 @@
    3.26  endif
    3.27  
    3.28  
    3.29 +# Old versions of a Java distribution have a `javah` binary, new versions do not. This checks whether or not `javah` can be found in the Java distribution found in the directory `$JAVA_HOME`.
    3.30 +DUMMY:=$(shell which $(JAVA_HOME)/bin/javah)
    3.31 +ifeq ($(.SHELLSTATUS),0)
    3.32 +    OLD_JAVA=true
    3.33 +endif
    3.34 +
    3.35  ######### Overrides from the config file(s) #########
    3.36  ifneq ("$(wildcard $(HERE)local.conf)","")
    3.37      $(info including: $(HERE)local.conf)
    3.38 @@ -32,19 +39,17 @@
    3.39      $(info Optional build config not found: $(HERE)local.conf)
    3.40  endif
    3.41  
    3.42 -ifneq ("$(wildcard $(HERE)src/local.conf)","")
    3.43 -    $(info including: $(HERE)src/local.conf)
    3.44 -    -include $(HERE)src/local.conf
    3.45 -else
    3.46 -    $(info Optional build config not found: $(HERE)src/local.conf)
    3.47 -endif
    3.48 -
    3.49  ### Apply config
    3.50  ENGINE_LIB=-L$(ENGINE_LIB_PATH)
    3.51  ENGINE_INC=-I$(ENGINE_INC_PATH)
    3.52  AD_LIB=-L$(AD_LIB_PATH)
    3.53  AD_INC=-I$(AD_INC_PATH)
    3.54  
    3.55 +ifndef JAVA_HOME
    3.56 +    $(error JAVA_HOME is not set!)
    3.57 +endif
    3.58 +
    3.59 +JAVA_BIN_DIR=$(JAVA_HOME)/bin
    3.60  
    3.61  ######### C and C++ #########
    3.62  CXXFLAGS+=-O0 -std=c++11 -fpermissive -fPIC -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/$(PLATFORM) $(AD_INC) $(ENGINE_INC)
     4.1 --- a/android/build.gradle	Tue Aug 18 21:57:06 2020 +0200
     4.2 +++ b/android/build.gradle	Fri Aug 28 18:18:26 2020 +0200
     4.3 @@ -94,7 +94,7 @@
     4.4      // call source generation makefile target
     4.5      task genSources(type:Exec, dependsOn: 'genpEpEngineAsn1Sources') {
     4.6          workingDir '../src'
     4.7 -        commandLine 'make', "-j${threadsToUse}", 'pEp.jar'
     4.8 +        commandLine 'make', "-j${threadsToUse}", 'lib-java'
     4.9      }
    4.10  
    4.11      task genpEpEngineSyncSources(type:Exec) {
     5.1 --- a/android/jni/Android.mk	Tue Aug 18 21:57:06 2020 +0200
     5.2 +++ b/android/jni/Android.mk	Fri Aug 28 18:18:26 2020 +0200
     5.3 @@ -47,14 +47,14 @@
     5.4  LOCAL_CPP_FEATURES += exceptions
     5.5  LOCAL_CPPFLAGS += -std=c++11 -DANDROID_STL=c++_shared -DHAVE_PTHREADS -DDISABLE_SYNC -fuse-ld=lld
     5.6  LOCAL_SRC_FILES  := \
     5.7 -		  ../../src/foundation_pEp_jniadapter_AbstractEngine.cc \
     5.8 -		  ../../src/foundation_pEp_jniadapter_Engine.cc \
     5.9 -		  ../../src/foundation_pEp_jniadapter_Message.cc \
    5.10 -		  ../../src/foundation_pEp_jniadapter__Blob.cc \
    5.11 -		  ../../src/throw_pEp_exception.cc \
    5.12 -		  ../../src/basic_api.cc \
    5.13 -		  ../../src/identity_api.cc \
    5.14 -		  ../../src/jniutils.cc
    5.15 +		  ../../src/cxx/foundation_pEp_jniadapter_AbstractEngine.cc \
    5.16 +		  ../../src/cxx/foundation_pEp_jniadapter_Engine.cc \
    5.17 +		  ../../src/cxx/foundation_pEp_jniadapter_Message.cc \
    5.18 +		  ../../src/cxx/foundation_pEp_jniadapter__Blob.cc \
    5.19 +		  ../../src/cxx/throw_pEp_exception.cc \
    5.20 +		  ../../src/cxx/basic_api.cc \
    5.21 +		  ../../src/cxx/identity_api.cc \
    5.22 +		  ../../src/cxx/jniutils.cc
    5.23  
    5.24  LOCAL_C_INCLUDES += $(GPGBUILD)/$(TARGET_ARCH_ABI)/include
    5.25  LOCAL_C_INCLUDES += $(LIB_PEP_ADAPTER_PATH)/build-android/include $(SRC_PATH)/libpEpAdapter
     6.1 --- a/android/src/foundation/pEp/jniadapter/AndroidHelper.java	Tue Aug 18 21:57:06 2020 +0200
     6.2 +++ b/android/src/foundation/pEp/jniadapter/AndroidHelper.java	Fri Aug 28 18:18:26 2020 +0200
     6.3 @@ -10,6 +10,8 @@
     6.4  import java.lang.reflect.Method;
     6.5  import java.util.Scanner;
     6.6  
     6.7 +import foundation.pEp.jniadapter.exceptions.pEpException;
     6.8 +
     6.9  public class AndroidHelper {
    6.10      static {
    6.11          System.loadLibrary("pEpJNIAndroidHelper");
     7.1 --- a/local.conf.example	Tue Aug 18 21:57:06 2020 +0200
     7.2 +++ b/local.conf.example	Fri Aug 28 18:18:26 2020 +0200
     7.3 @@ -2,23 +2,28 @@
     7.4  # you might not need this file, but if the defaults dont work for you
     7.5  # You can override them here.
     7.6  # Tweak the values to your needs and rename it to local.conf
     7.7 +# These example values here reflect the defaults
     7.8 +
     7.9 +############ Install ###########
    7.10 +# PREFIX=$(HOME)
    7.11  
    7.12  ######### C++ Compiler #########
    7.13  # Should work with clang and g++
    7.14  # CXX=g++
    7.15 -# DEBUG=1       # DEBUG Build (Default)
    7.16 -# DEBUG=0       # RELEASE Build
    7.17 +# DEBUG=0       # RELEASE Build / set to 1 for DEBUG build
    7.18  
    7.19 -######### JAVA #########
    7.20 +############ JAVA ##############
    7.21  # JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home
    7.22  
    7.23 -######### YML2 #########
    7.24 -# YML2_PATH=$(HOME)/code/yml2
    7.25 +############ YML2 ##############
    7.26 +# YML2_PATH=$(HOME)/src/yml2
    7.27  
    7.28 -######### Engine #########
    7.29 -# ENGINE_LIB_PATH=$(HOME)/local/lib
    7.30 -# ENGINE_INC_PATH=$(HOME)/local/include
    7.31 +########### Engine #############
    7.32 +# ENGINE_LIB_PATH=$(PREFIX)/lib
    7.33 +# ENGINE_INC_PATH=$(PREFIX)/include
    7.34  
    7.35 -######### libAdapter #########
    7.36 -# AD_LIB_PATH=$(HOME)/local/lib
    7.37 -# AD_INC_PATH=$(HOME)/local/include
    7.38 +########## libAdapter ##########
    7.39 +# AD_LIB_PATH=$(PREFIX)/lib
    7.40 +# AD_INC_PATH=$(PREFIX)/include
    7.41 +
    7.42 +
     8.1 --- a/src/Makefile	Tue Aug 18 21:57:06 2020 +0200
     8.2 +++ b/src/Makefile	Fri Aug 28 18:18:26 2020 +0200
     8.3 @@ -5,6 +5,30 @@
     8.4  
     8.5  include ../Makefile.conf
     8.6  
     8.7 +# Names of the java and c++ libs to be built
     8.8 +LIB_JAVA_NAME=pEp.jar
     8.9 +LIB_CXX_NAME=libpEpJNI
    8.10 +DIST_DIR=../dist/
    8.11 +
    8.12 +
    8.13 +LIB_CXX_STATIC_NAME=$(addsuffix .a,$(LIB_CXX_NAME))
    8.14 +ifeq ($(PLATFORM),linux)
    8.15 +    LIB_CXX_DYN_NAME=$(addsuffix .so,$(LIB_CXX_NAME))
    8.16 +else ifeq ($(PLATFORM),darwin)
    8.17 +    LIB_CXX_DYN_NAME=$(addsuffix .dylib,$(LIB_CXX_NAME))
    8.18 +else
    8.19 +    $(error I dont know how to build for $(PLATFORM).)
    8.20 +endif
    8.21 +
    8.22 +LIB_JAVA=$(DIST_DIR)/$(LIB_JAVA_NAME)
    8.23 +LIB_CXX_STATIC=$(DIST_DIR)/$(LIB_CXX_STATIC_NAME)
    8.24 +LIB_CXX_DYN=$(DIST_DIR)/$(LIB_CXX_DYN_NAME)
    8.25 +
    8.26 +$(info ------------------ DIST FILES ---------------------)
    8.27 +$(info LIB_JAVA:        $(LIB_JAVA))
    8.28 +$(info LIB_CXX_STATIC:  $(LIB_CXX_STATIC))
    8.29 +$(info LIB_CXX_DYN:     $(LIB_CXX_DYN))
    8.30 +$(info ------------------ BUILD INFO ---------------------)
    8.31  $(info PLATFORM:        $(PLATFORM))
    8.32  $(info DEBUG:           $(DEBUG))
    8.33  $(info CXX:             $(CXX))
    8.34 @@ -17,133 +41,151 @@
    8.35  $(info CXXFLAGS:        $(CXXFLAGS))
    8.36  $(info LDFLAGS:         $(LDFLAGS))
    8.37  $(info LDLIBS:          $(LDLIBS))
    8.38 +$(info ---------------------------------------------------)
    8.39  
    8.40 -ifndef JAVA_HOME
    8.41 -    $(error JAVA_HOME is not set!)
    8.42 +# Dirs
    8.43 +# relative to "src/"
    8.44 +BUILD_ROOT=../build/
    8.45 +CXX_DIR=cxx/
    8.46 +JAVA_DIR=java/
    8.47 +JAVA_PKG_BASENAME=foundation/pEp/jniadapter/
    8.48 +
    8.49 +JAVA_PKG_ROOT=$(JAVA_DIR)/$(JAVA_PKG_BASENAME)
    8.50 +JAVA_BUILD_ROOT=$(BUILD_ROOT)/$(JAVA_DIR)
    8.51 +OBJ_DIR=$(BUILD_ROOT)/$(CXX_DIR)
    8.52 +
    8.53 +
    8.54 +# for "make clean" only
    8.55 +GENERATED_JAVA=\
    8.56 +	$(JAVA_PKG_ROOT)/Engine.java \
    8.57 +	$(JAVA_PKG_ROOT)/Message.java \
    8.58 +	$(JAVA_PKG_ROOT)/CipherSuite.java \
    8.59 +	$(JAVA_PKG_ROOT)/Color.java \
    8.60 +	$(JAVA_PKG_ROOT)/DecryptFlags.java \
    8.61 +	$(JAVA_PKG_ROOT)/IdentityFlags.java \
    8.62 +	$(JAVA_PKG_ROOT)/Rating.java \
    8.63 +	$(JAVA_PKG_ROOT)/SyncHandshakeResult.java \
    8.64 +	$(JAVA_PKG_ROOT)/SyncHandshakeSignal.java \
    8.65 +	$(JAVA_PKG_ROOT)/PassphraseType.java \
    8.66 +	$(JAVA_PKG_ROOT)/interfaces/EngineInterface.java \
    8.67 + 	$(JAVA_PKG_ROOT)/interfaces/MessageInterface.java \
    8.68 + 	$(JAVA_PKG_ROOT)/exceptions/*.java
    8.69 +
    8.70 +# files to compile which will be be generated
    8.71 +GENERATED_CC=\
    8.72 +	$(CXX_DIR)/foundation_pEp_jniadapter_Engine.cc \
    8.73 +	$(CXX_DIR)/foundation_pEp_jniadapter_Message.cc \
    8.74 +	$(CXX_DIR)/throw_pEp_exception.cc
    8.75 +
    8.76 +# for "make clean" only
    8.77 +GENERATED_HH=\
    8.78 +	$(CXX_DIR)/throw_pEp_exception.hh
    8.79 +
    8.80 +# Generated JNI headers (javac -h)
    8.81 +JNI_GENERATED_HH=\
    8.82 +	$(CXX_DIR)/foundation_pEp_jniadapter_AbstractEngine.h \
    8.83 +    $(CXX_DIR)/foundation_pEp_jniadapter__Blob.h \
    8.84 +    $(CXX_DIR)/foundation_pEp_jniadapter_Engine.h \
    8.85 +    $(CXX_DIR)/foundation_pEp_jniadapter_Identity.h \
    8.86 +    $(CXX_DIR)/foundation_pEp_jniadapter_Message.h
    8.87 +
    8.88 +# Auto dependencies using gcc/clang
    8.89 +CXXFLAGS+= -MMD -MP
    8.90 +
    8.91 +SOURCES=$(wildcard $(CXX_DIR)/*.cc)
    8.92 +SOURCES+=$(GENERATED_CC)
    8.93 +tmp=$(SOURCES:.cc=.o)
    8.94 +OBJECTS=$(addprefix $(BUILD_ROOT), $(tmp))
    8.95 +DEPENDS=$(OBJECTS:.o=.d)
    8.96 +
    8.97 +-include $(DEPENDS)
    8.98 +VPATH = $(CXX_DIR)/
    8.99 +
   8.100 +.PHONY: all lib-java lib-cxx compile-java compile-cxx gen-jni-headers codegen clean create-dirs remove-dirs install uninstall
   8.101 +
   8.102 +$(info SOURCES: $(SOURCES))
   8.103 +$(info OBJECTS: $(OBJECTS))
   8.104 +$(info DEPENDS: $(DEPENDS))
   8.105 +
   8.106 +all: lib-java lib-cxx
   8.107 +
   8.108 +# Build only java lib
   8.109 +lib-java: $(LIB_JAVA)
   8.110 +
   8.111 +# Build only c++ static/shared lib
   8.112 +lib-cxx: $(LIB_CXX_DYN) $(LIB_CXX_STATIC)
   8.113 +
   8.114 +# ---------------- Link -----------------
   8.115 +$(LIB_JAVA): compile-java
   8.116 +	$(JAVA_BIN_DIR)/jar cf $@ -C $(JAVA_BUILD_ROOT) foundation
   8.117 +
   8.118 +$(LIB_CXX_DYN): compile-cxx
   8.119 +	$(CXX) $(OBJ_DIR)*.o $(LDFLAGS) $(LDLIBS) -o $@
   8.120 +
   8.121 +$(LIB_CXX_STATIC): compile-cxx
   8.122 +	$(AR) -r $@ $(OBJ_DIR)*.o
   8.123 +
   8.124 +
   8.125 +# -------------- Compile -----------------
   8.126 +compile-java: create-dirs codegen
   8.127 +	cd $(JAVA_DIR);$(JAVA_BIN_DIR)/javac -d ../$(JAVA_BUILD_ROOT) $(JAVA_PKG_BASENAME)/*.java
   8.128 +	cd $(JAVA_DIR);$(JAVA_BIN_DIR)/javac -d ../$(JAVA_BUILD_ROOT) $(JAVA_PKG_BASENAME)/exceptions/*.java
   8.129 +	cd $(JAVA_DIR);$(JAVA_BIN_DIR)/javac -d ../$(JAVA_BUILD_ROOT) $(JAVA_PKG_BASENAME)/interfaces/*.java
   8.130 +
   8.131 +
   8.132 +compile-cxx: create-dirs gen-jni-headers $(OBJECTS)
   8.133 +
   8.134 +$(OBJECTS): $(BUILD_ROOT)%.o: %.cc
   8.135 +	$(CXX) $(CXXFLAGS) -c $< -o $@
   8.136 +
   8.137 +
   8.138 +# --------- Generate JNI headers ----------
   8.139 +gen-jni-headers: codegen $(JNI_GENERATED_HH)
   8.140 +
   8.141 +$(JNI_GENERATED_HH):
   8.142 +ifdef OLD_JAVA
   8.143 +	cd $(JAVA_DIR);$(JAVA_BIN_DIR)/javah -d ../$(JAVA_BUILD_ROOT) $(subst /,.,$(subst .java,,$<))
   8.144 +else
   8.145 +	cd $(JAVA_DIR);$(JAVA_BIN_DIR)/javac -d ../$(JAVA_BUILD_ROOT) -h ../$(CXX_DIR)/ $(JAVA_PKG_BASENAME)/*.java
   8.146  endif
   8.147  
   8.148 -JP=$(JAVA_HOME)/bin
   8.149  
   8.150 -# Old versions of a Java distribution have a `javah` binary, new versions do not. This checks whether or not `javah` can be found in the Java distribution found in the directory `$JAVA_HOME`.
   8.151 -NOT_USED:=$(shell which $(JAVA_HOME)/bin/javah)
   8.152 -ifeq ($(.SHELLSTATUS),0)
   8.153 -    OLD_JAVA=placeholder
   8.154 -endif
   8.155 +# ------------- YML2 CodeGen --------------
   8.156 +codegen:
   8.157 +	$(MAKE) -C codegen
   8.158  
   8.159 -LIBRARY=libpEpJNI.a
   8.160 -JAR=pEp.jar
   8.161 +# ------------- Housekeeping ---------------
   8.162 +create-dirs:
   8.163 +	$(MAKE) -C codegen create-dirs
   8.164 +	mkdir -p $(JAVA_BUILD_ROOT)/$(JAVA_PKG_BASENAME)
   8.165 +	mkdir -p $(JAVA_BUILD_ROOT)/$(JAVA_PKG_BASENAME)/exceptions
   8.166 +	mkdir -p $(OBJ_DIR)
   8.167 +	mkdir -p $(DIST_DIR)
   8.168 +	mkdir -p $(JAVA_PKG_ROOT)/exceptions
   8.169  
   8.170 -ifeq ($(PLATFORM),linux)
   8.171 -    SHARED=libpEpJNI.so
   8.172 -else ifeq ($(PLATFORM),darwin)
   8.173 -    SHARED=libpEpJNI.dylib
   8.174 -else
   8.175 -    $(error I dont know how to build for $(PLATFORM).)
   8.176 -endif
   8.177 +#rm -rf is too dangerous for vars
   8.178 +remove-dirs:
   8.179 +	$(MAKE) -C codegen remove-dirs
   8.180 +	rm -rf ../build
   8.181 +	rm -rf ../dist
   8.182  
   8.183 -JAVA_SOURCES=foundation/pEp/jniadapter/AbstractEngine.java \
   8.184 -    foundation/pEp/jniadapter/Blob.java \
   8.185 -    foundation/pEp/jniadapter/CommType.java \
   8.186 -    foundation/pEp/jniadapter/Identity.java \
   8.187 -    foundation/pEp/jniadapter/Pair.java \
   8.188 -    foundation/pEp/jniadapter/Sync.java \
   8.189 -    foundation/pEp/jniadapter/_Blob.java \
   8.190 -    foundation/pEp/jniadapter/_Identity.java \
   8.191 -    foundation/pEp/jniadapter/pEpException.java \
   8.192 -    foundation/pEp/jniadapter/Message.java \
   8.193 -    foundation/pEp/jniadapter/Engine.java \
   8.194 +clean: remove-dirs
   8.195 +	$(MAKE) -C codegen clean
   8.196 +	rm -f $(GENERATED_JAVA)
   8.197 +	rm -f $(GENERATED_CC)
   8.198 +	rm -f $(GENERATED_HH)
   8.199 +	rm -f $(JNI_GENERATED_HH)
   8.200 +	rm -f $(DEPENDS)
   8.201 +	rm -f $(OBJECTS)
   8.202  
   8.203 -C_SOURCES=foundation_pEp_jniadapter_Engine.cc \
   8.204 -    foundation_pEp_jniadapter_Engine.h \
   8.205 -    foundation_pEp_jniadapter_Message.cc \
   8.206 -    foundation_pEp_jniadapter_Message.h \
   8.207 -    throw_pEp_exception.cc \
   8.208 -    throw_pEp_exception.hh \
   8.209 -    foundation_pEp_jniadapter_AbstractEngine.h \
   8.210 -    foundation_pEp_jniadapter__Blob.cc \
   8.211 -    foundation_pEp_jniadapter__Blob.h
   8.212 +install: $(LIB_JAVA) $(LIB_CXX_STATIC) $(LIB_CXX_DYN)
   8.213 +	mkdir -p $(PREFIX)/lib
   8.214 +	cp -v $(LIB_JAVA) 		$(PREFIX)/lib/
   8.215 +	cp -v $(LIB_CXX_STATIC) $(PREFIX)/lib/
   8.216 +	cp -v $(LIB_CXX_DYN) 	$(PREFIX)/lib/
   8.217  
   8.218 -PEP_HEADER:=$(shell $(CXX) $(CXXFLAGS) -E -M get_header.cc | grep -oe '[^[:space:]]*pEpEngine\.h' | head -1)
   8.219 +uninstall:
   8.220 +	cd $(PREFIX)/lib && rm -vf $(LIB_JAVA_NAME)
   8.221 +	cd $(PREFIX)/lib && rm -vf $(LIB_CXX_STATIC_NAME)
   8.222 +	cd $(PREFIX)/lib && rm -vf $(LIB_CXX_DYN_NAME)
   8.223  
   8.224 -
   8.225 -.PHONY: all
   8.226 -all: $(JAR) $(SHARED)
   8.227 -
   8.228 -$(JAR): status_list.yml2 passphrase_status_list.yml2 $(JAVA_SOURCES) $(C_SOURCES)
   8.229 -	$(JP)/javac foundation/pEp/jniadapter/*.java
   8.230 -	$(JP)/jar cf $@ foundation/pEp/jniadapter/*.class
   8.231 -
   8.232 -BLUBB=foundation_pEp_jniadapter_AbstractEngine.h foundation_pEp_jniadapter_Engine.h foundation_pEp_jniadapter_Message.h foundation_pEp_jniadapter__Blob.h
   8.233 -$(BLUBB): foundation_pEp_jniadapter_%.h: foundation/pEp/jniadapter/%.java
   8.234 -ifdef OLD_JAVA
   8.235 -	$(JP)/javah $(subst /,.,$(subst .java,,$<))
   8.236 -else
   8.237 -	$(JP)/javac -h . $<
   8.238 -endif
   8.239 -
   8.240 -foundation_pEp_jniadapter_AbstractEngine.o: %.o: %.cc %.h throw_pEp_exception.hh jniutils.hh
   8.241 -	$(CXX) $(CXXFLAGS) -c $< -o $@
   8.242 -
   8.243 -foundation_pEp_jniadapter_Engine.o foundation_pEp_jniadapter_Message.o foundation_pEp_jniadapter__Blob.o : %.o: %.cc %.h
   8.244 -	$(CXX) $(CXXFLAGS) -c $< -o $@
   8.245 -
   8.246 -$(LIBRARY): foundation_pEp_jniadapter_AbstractEngine.o foundation_pEp_jniadapter_Engine.o foundation_pEp_jniadapter_Message.o throw_pEp_exception.o jniutils.o basic_api.o foundation_pEp_jniadapter__Blob.o
   8.247 -	ar -r $@ *.o
   8.248 -
   8.249 -$(SHARED): $(LIBRARY)
   8.250 -	$(CXX) *.o $(LDFLAGS) $(LDLIBS) -o $@
   8.251 -
   8.252 -status_list.yml2: pEp.yml2
   8.253 -	bash ../utils/extract_pEp_status_codes_from_engine.sh "$(PEP_HEADER)" $@
   8.254 -
   8.255 -passphrase_status_list.yml2: status_list.yml2
   8.256 -	grep passphrase $< > $@
   8.257 -
   8.258 -foundation/pEp/jniadapter/pEpException.java: pEp.yml2 gen_java_exceptions.ysl2 pEp.yml2
   8.259 -	$(YML2_PROC) -y gen_java_exceptions.ysl2 $< -o $@
   8.260 -
   8.261 -foundation/pEp/jniadapter/Message.java: pEp.yml2 gen_java_Message.ysl2 types_java.ysl2
   8.262 -	$(YML2_PROC) -y gen_java_Message.ysl2 $<
   8.263 -
   8.264 -foundation/pEp/jniadapter/Engine.java: pEp.yml2 gen_java_Engine.ysl2 types_java.ysl2
   8.265 -	$(YML2_PROC) -y gen_java_Engine.ysl2 $<
   8.266 -
   8.267 -foundation_pEp_jniadapter_Message.cc: pEp.yml2 gen_cpp_Message.ysl2 types_c.ysl2
   8.268 -	$(YML2_PROC) -y gen_cpp_Message.ysl2 $<
   8.269 -
   8.270 -foundation_pEp_jniadapter_Engine.cc: pEp.yml2 gen_cpp_Engine.ysl2 types_c.ysl2
   8.271 -	$(YML2_PROC) -y gen_cpp_Engine.ysl2 $<
   8.272 -
   8.273 -throw_pEp_exception.cc throw_pEp_exception.hh: pEp.yml2 gen_throw_pEp_exception.ysl2 textutils.ysl2
   8.274 -	$(YML2_PROC) -y gen_throw_pEp_exception.ysl2 $< -o throw_pEp_exception.cc
   8.275 -
   8.276 -throw_pEp_exception.o: throw_pEp_exception.cc throw_pEp_exception.hh
   8.277 -
   8.278 -basic_api.o: basic_api.cc jniutils.hh throw_pEp_exception.hh
   8.279 -
   8.280 -.PHONY: clean
   8.281 -clean:
   8.282 -	rm -f $(JAR) $(LIBRARY) $(SHARED)
   8.283 -	rm -f *.so
   8.284 -	rm -f *.dylib
   8.285 -	rm -f *.o
   8.286 -	rm -f *.class
   8.287 -	rm -f *.xml *.xsl
   8.288 -	rm -f foundation_pEp_jniadapter_*.h
   8.289 -	rm -f foundation/pEp/jniadapter/*.class
   8.290 -	rm -f foundation/pEp/jniadapter/pEp*.java
   8.291 -	rm -f foundation/pEp/jniadapter/Engine.java
   8.292 -	rm -f foundation/pEp/jniadapter/Message.java
   8.293 -	rm -f foundation/pEp/jniadapter/Color.java
   8.294 -	rm -f foundation/pEp/jniadapter/DecryptFlags.java
   8.295 -	rm -f foundation/pEp/jniadapter/IdentityFlags.java
   8.296 -	rm -f foundation/pEp/jniadapter/Rating.java
   8.297 -	rm -f foundation/pEp/jniadapter/Status.java
   8.298 -	rm -f foundation/pEp/jniadapter/SyncHandshakeResult.java
   8.299 -	rm -f foundation/pEp/jniadapter/SyncHandshakeSignal.java
   8.300 -	rm -f foundation/pEp/jniadapter/CipherSuite.java
   8.301 -	rm -f foundation/pEp/jniadapter/PassphraseType.java
   8.302 -	rm -f throw_pEp_exception.*
   8.303 -	rm -f foundation_pEp_jniadapter_Message.cc foundation_pEp_jniadapter_Engine.cc
   8.304 -	rm -f status_list.yml2
   8.305 -	rm -f passphrase_status_list.yml2
     9.1 --- a/src/basic_api.cc	Tue Aug 18 21:57:06 2020 +0200
     9.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.3 @@ -1,563 +0,0 @@
     9.4 -#include <pEp/keymanagement.h>
     9.5 -#include <pEp/blacklist.h>
     9.6 -#include <pEp/Adapter.hh>
     9.7 -#include <pEp/pEpLog.hh>
     9.8 -
     9.9 -#ifndef ANDROID
    9.10 -#include <string.h>
    9.11 -#endif
    9.12 -
    9.13 -#include "throw_pEp_exception.hh"
    9.14 -#include "jniutils.hh"
    9.15 -#include "passphrase_callback.hh"
    9.16 -
    9.17 -extern "C" {
    9.18 -    using namespace pEp::JNIAdapter;
    9.19 -    using pEp::Adapter::session;
    9.20 -    using pEp::passphrase_cache;
    9.21 -
    9.22 -JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_Engine__1trustwords(
    9.23 -        JNIEnv *env,
    9.24 -        jobject obj,
    9.25 -        jobject ident
    9.26 -    )
    9.27 -{
    9.28 -    std::mutex *mutex_local = nullptr;
    9.29 -    {
    9.30 -        std::lock_guard<std::mutex> l(global_mutex);
    9.31 -        pEpLog("called with lock_guard");
    9.32 -        mutex_local = get_engine_java_object_mutex(env, obj);
    9.33 -    }
    9.34 -    std::lock_guard<std::mutex> l(*mutex_local);
    9.35 -
    9.36 -    pEp_identity *_ident = to_identity(env, ident);
    9.37 -    char *words;
    9.38 -    size_t wsize;
    9.39 -
    9.40 -    PEP_STATUS status = PEP_STATUS_OK;
    9.41 -
    9.42 -    if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
    9.43 -        if (_ident->me)
    9.44 -            status = passphraseWrap(::myself, session(), _ident);
    9.45 -        else
    9.46 -            status = passphraseWrap(::update_identity, session(), _ident);
    9.47 -    }
    9.48 -
    9.49 -    if (status != PEP_STATUS_OK) {
    9.50 -        throw_pEp_Exception(env, status);
    9.51 -        return NULL;
    9.52 -    }
    9.53 -
    9.54 -    if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
    9.55 -        throw_pEp_Exception(env, PEP_CANNOT_FIND_IDENTITY);
    9.56 -        return NULL;
    9.57 -    }
    9.58 -
    9.59 -    const char *lang;
    9.60 -    if (_ident->lang[0])
    9.61 -        lang = _ident->lang;
    9.62 -    else
    9.63 -        lang = "en";
    9.64 -
    9.65 -    status = passphraseWrap(::trustwords,
    9.66 -            session(), (const char *) _ident->fpr, lang, &words, &wsize, 10);
    9.67 -
    9.68 -    if (status != PEP_STATUS_OK) {
    9.69 -        throw_pEp_Exception(env, status);
    9.70 -        return NULL;
    9.71 -    }
    9.72 -
    9.73 -    return from_string(env, words);
    9.74 -}
    9.75 -
    9.76 -JNIEXPORT jobject JNICALL Java_foundation_pEp_jniadapter_Engine__1myself(
    9.77 -        JNIEnv *env,
    9.78 -        jobject obj,
    9.79 -        jobject ident
    9.80 -    )
    9.81 -{
    9.82 -    std::mutex *mutex_local = nullptr;
    9.83 -    {
    9.84 -        std::lock_guard<std::mutex> l(global_mutex);
    9.85 -        pEpLog("called with lock_guard");
    9.86 -        mutex_local = get_engine_java_object_mutex(env, obj);
    9.87 -    }
    9.88 -    std::lock_guard<std::mutex> l(*mutex_local);
    9.89 -
    9.90 -    pEp_identity *_ident = to_identity(env, ident);
    9.91 -
    9.92 -    PEP_STATUS status = passphraseWrap(::myself, session(), _ident);
    9.93 -
    9.94 -    if (status != PEP_STATUS_OK) {
    9.95 -        LOGD("Failed Myself: 0x%04x\\n", status);
    9.96 -        throw_pEp_Exception(env, status);
    9.97 -        return NULL;
    9.98 -    }
    9.99 -    return from_identity(env, _ident);
   9.100 -}
   9.101 -
   9.102 -JNIEXPORT jobject JNICALL Java_foundation_pEp_jniadapter_Engine__1updateIdentity(
   9.103 -        JNIEnv *env,
   9.104 -        jobject obj,
   9.105 -        jobject ident
   9.106 -    )
   9.107 -{
   9.108 -    std::mutex *mutex_local = nullptr;
   9.109 -    {
   9.110 -        std::lock_guard<std::mutex> l(global_mutex);
   9.111 -        pEpLog("called with lock_guard");
   9.112 -        mutex_local = get_engine_java_object_mutex(env, obj);
   9.113 -    }
   9.114 -    std::lock_guard<std::mutex> l(*mutex_local);
   9.115 -
   9.116 -    pEp_identity *_ident = to_identity(env, ident);
   9.117 -
   9.118 -    passphraseWrap(::update_identity, session(), _ident);
   9.119 -
   9.120 -    return from_identity(env, _ident);
   9.121 -}
   9.122 -
   9.123 -JNIEXPORT jobject JNICALL Java_foundation_pEp_jniadapter_Engine__1setOwnKey(
   9.124 -        JNIEnv *env,
   9.125 -        jobject obj,
   9.126 -        jobject ident,
   9.127 -        jbyteArray fpr
   9.128 -    )
   9.129 -{
   9.130 -    std::mutex *mutex_local = nullptr;
   9.131 -    {
   9.132 -        std::lock_guard<std::mutex> l(global_mutex);
   9.133 -        pEpLog("called with lock_guard");
   9.134 -        mutex_local = get_engine_java_object_mutex(env, obj);
   9.135 -    }
   9.136 -    std::lock_guard<std::mutex> l(*mutex_local);
   9.137 -
   9.138 -    pEp_identity *_ident = to_identity(env, ident);
   9.139 -    const char *_fpr = to_string(env, fpr);
   9.140 -
   9.141 -    PEP_STATUS status = passphraseWrap(::set_own_key, session(), _ident, _fpr);
   9.142 -
   9.143 -    if (status != PEP_STATUS_OK) {
   9.144 -        LOGD("Failed setOwnKey: 0x%04x\\n", status);
   9.145 -        throw_pEp_Exception(env, status);
   9.146 -        return NULL;
   9.147 -    }
   9.148 -
   9.149 -    return from_identity(env, _ident);
   9.150 -}
   9.151 -
   9.152 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1keyMistrusted(
   9.153 -        JNIEnv *env,
   9.154 -        jobject obj,
   9.155 -        jobject ident
   9.156 -    )
   9.157 -{
   9.158 -    std::mutex *mutex_local = nullptr;
   9.159 -    {
   9.160 -        std::lock_guard<std::mutex> l(global_mutex);
   9.161 -        pEpLog("called with lock_guard");
   9.162 -        mutex_local = get_engine_java_object_mutex(env, obj);
   9.163 -    }
   9.164 -    std::lock_guard<std::mutex> l(*mutex_local);
   9.165 -
   9.166 -    pEp_identity *_ident = to_identity(env, ident);
   9.167 -
   9.168 -    PEP_STATUS status = PEP_STATUS_OK;
   9.169 -
   9.170 -    if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
   9.171 -        if (_ident->me)
   9.172 -            status = passphraseWrap(::myself, session(), _ident);
   9.173 -        else
   9.174 -            status = passphraseWrap(::update_identity, session(), _ident);
   9.175 -    }
   9.176 -
   9.177 -    if (status != PEP_STATUS_OK) {
   9.178 -        throw_pEp_Exception(env, status);
   9.179 -        return;
   9.180 -    }
   9.181 -
   9.182 -    if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
   9.183 -        throw_pEp_Exception(env, PEP_CANNOT_FIND_IDENTITY);
   9.184 -        return;
   9.185 -    }
   9.186 -
   9.187 -    passphraseWrap(::key_mistrusted, session(), _ident);
   9.188 -}
   9.189 -
   9.190 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1keyResetTrust(
   9.191 -        JNIEnv *env,
   9.192 -        jobject obj,
   9.193 -        jobject ident
   9.194 -    )
   9.195 -{
   9.196 -    std::mutex *mutex_local = nullptr;
   9.197 -    {
   9.198 -        std::lock_guard<std::mutex> l(global_mutex);
   9.199 -        pEpLog("called with lock_guard");
   9.200 -        mutex_local = get_engine_java_object_mutex(env, obj);
   9.201 -    }
   9.202 -    std::lock_guard<std::mutex> l(*mutex_local);
   9.203 -
   9.204 -    pEp_identity *_ident = to_identity(env, ident);
   9.205 -
   9.206 -    PEP_STATUS status = PEP_STATUS_OK;
   9.207 -
   9.208 -    if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
   9.209 -        if (_ident->me)
   9.210 -            status = passphraseWrap(::myself, session(), _ident);
   9.211 -        else
   9.212 -            status = passphraseWrap(::update_identity, session(), _ident);
   9.213 -    }
   9.214 -
   9.215 -    if (status != PEP_STATUS_OK) {
   9.216 -        throw_pEp_Exception(env, status);
   9.217 -        return;
   9.218 -    }
   9.219 -
   9.220 -    if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
   9.221 -        throw_pEp_Exception(env, PEP_CANNOT_FIND_IDENTITY);
   9.222 -        return;
   9.223 -    }
   9.224 -
   9.225 -    passphraseWrap(::key_reset_trust, session(), _ident);
   9.226 -}
   9.227 -
   9.228 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1trustPersonalKey(
   9.229 -        JNIEnv *env,
   9.230 -        jobject obj,
   9.231 -        jobject ident
   9.232 -    )
   9.233 -{
   9.234 -    std::mutex *mutex_local = nullptr;
   9.235 -    {
   9.236 -        std::lock_guard<std::mutex> l(global_mutex);
   9.237 -        pEpLog("called with lock_guard");
   9.238 -        mutex_local = get_engine_java_object_mutex(env, obj);
   9.239 -    }
   9.240 -    std::lock_guard<std::mutex> l(*mutex_local);
   9.241 -
   9.242 -    pEp_identity *_ident = to_identity(env, ident);
   9.243 -
   9.244 -    PEP_STATUS status = PEP_STATUS_OK;
   9.245 -
   9.246 -    if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
   9.247 -        if (_ident->me)
   9.248 -            status = passphraseWrap(::myself, session(), _ident);
   9.249 -        else
   9.250 -            status = passphraseWrap(::update_identity, session(), _ident);
   9.251 -    }
   9.252 -
   9.253 -    if (status != PEP_STATUS_OK) {
   9.254 -        throw_pEp_Exception(env, status);
   9.255 -        return;
   9.256 -    }
   9.257 -
   9.258 -    if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
   9.259 -        throw_pEp_Exception(env, PEP_CANNOT_FIND_IDENTITY);
   9.260 -        return;
   9.261 -    }
   9.262 -
   9.263 -    passphraseWrap(::trust_personal_key, session(), _ident);
   9.264 -}
   9.265 -
   9.266 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1trustOwnKey(
   9.267 -        JNIEnv *env,
   9.268 -        jobject obj,
   9.269 -        jobject ident
   9.270 -    )
   9.271 -{
   9.272 -    std::mutex *mutex_local = nullptr;
   9.273 -    {
   9.274 -        std::lock_guard<std::mutex> l(global_mutex);
   9.275 -        pEpLog("called with lock_guard");
   9.276 -        mutex_local = get_engine_java_object_mutex(env, obj);
   9.277 -    }
   9.278 -    std::lock_guard<std::mutex> l(*mutex_local);
   9.279 -
   9.280 -    pEp_identity *_ident = to_identity(env, ident);
   9.281 -
   9.282 -    if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
   9.283 -        throw_pEp_Exception(env, PEP_CANNOT_FIND_IDENTITY);
   9.284 -        return;
   9.285 -    }
   9.286 -
   9.287 -    passphraseWrap(::trust_own_key, session(), _ident);
   9.288 -}
   9.289 -
   9.290 -JNIEXPORT jobject JNICALL Java_foundation_pEp_jniadapter_Engine__1importKey(
   9.291 -        JNIEnv *env,
   9.292 -        jobject obj,
   9.293 -        jbyteArray key
   9.294 -    )
   9.295 -{
   9.296 -    std::mutex *mutex_local = nullptr;
   9.297 -    {
   9.298 -        std::lock_guard<std::mutex> l(global_mutex);
   9.299 -        pEpLog("called with lock_guard");
   9.300 -        mutex_local = get_engine_java_object_mutex(env, obj);
   9.301 -    }
   9.302 -    std::lock_guard<std::mutex> l(*mutex_local);
   9.303 -
   9.304 -    size_t _size = (size_t) env->GetArrayLength(key);
   9.305 -    const char *_key = (char *) env->GetByteArrayElements(key, NULL);
   9.306 -
   9.307 -    if(_key == NULL){
   9.308 -        throw_pEp_Exception(env, PEP_OUT_OF_MEMORY);
   9.309 -        return NULL;
   9.310 -    }
   9.311 -
   9.312 -    identity_list *_identities;
   9.313 -
   9.314 -    PEP_STATUS status =  passphraseWrap(::import_key, session(), _key, _size, &_identities);
   9.315 -    if (status != PEP_STATUS_OK && status != PEP_KEY_IMPORTED) {
   9.316 -        throw_pEp_Exception(env, status);
   9.317 -        return NULL;
   9.318 -    }
   9.319 -
   9.320 -    jobject identities_ = NULL;
   9.321 -    if (_identities) {
   9.322 -        identities_ = from_identitylist(env, _identities);
   9.323 -    }
   9.324 -
   9.325 -    env->ReleaseByteArrayElements(key, (jbyte *) _key, JNI_ABORT);
   9.326 -    return identities_;
   9.327 -}
   9.328 -
   9.329 -
   9.330 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1config_1passive_1mode(
   9.331 -        JNIEnv *env,
   9.332 -        jobject obj,
   9.333 -        jboolean enable
   9.334 -    )
   9.335 -{
   9.336 -    std::mutex *mutex_local = nullptr;
   9.337 -    {
   9.338 -        std::lock_guard<std::mutex> l(global_mutex);
   9.339 -        pEpLog("called with lock_guard");
   9.340 -        mutex_local = get_engine_java_object_mutex(env, obj);
   9.341 -    }
   9.342 -    std::lock_guard<std::mutex> l(*mutex_local);
   9.343 -
   9.344 -    ::config_passive_mode(session(), (bool)enable);
   9.345 -}
   9.346 -
   9.347 -
   9.348 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1config_1unencrypted_1subject(
   9.349 -        JNIEnv *env,
   9.350 -        jobject obj,
   9.351 -        jboolean enable
   9.352 -    )
   9.353 -{
   9.354 -    std::mutex *mutex_local = nullptr;
   9.355 -    {
   9.356 -        std::lock_guard<std::mutex> l(global_mutex);
   9.357 -        pEpLog("called with lock_guard");
   9.358 -        mutex_local = get_engine_java_object_mutex(env, obj);
   9.359 -    }
   9.360 -    std::lock_guard<std::mutex> l(*mutex_local);
   9.361 -
   9.362 -    ::config_unencrypted_subject(session(), (bool)enable);
   9.363 -}
   9.364 -
   9.365 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1blacklist_1add(
   9.366 -        JNIEnv *env,
   9.367 -        jobject obj,
   9.368 -        jbyteArray fpr
   9.369 -    )
   9.370 -{
   9.371 -    std::mutex *mutex_local = nullptr;
   9.372 -    {
   9.373 -        std::lock_guard<std::mutex> l(global_mutex);
   9.374 -        pEpLog("called with lock_guard");
   9.375 -        mutex_local = get_engine_java_object_mutex(env, obj);
   9.376 -    }
   9.377 -    std::lock_guard<std::mutex> l(*mutex_local);
   9.378 -
   9.379 -    const char *_fpr = to_string(env, fpr);
   9.380 -
   9.381 -    if(_fpr == NULL){
   9.382 -        throw_pEp_Exception(env, PEP_OUT_OF_MEMORY);
   9.383 -        return;
   9.384 -    }
   9.385 -
   9.386 -    PEP_STATUS status = passphraseWrap(::blacklist_add, session(), _fpr);
   9.387 -    if (status != PEP_STATUS_OK) {
   9.388 -        throw_pEp_Exception(env, status);
   9.389 -        return;
   9.390 -    }
   9.391 -
   9.392 -}
   9.393 -
   9.394 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1blacklist_1delete(
   9.395 -        JNIEnv *env,
   9.396 -        jobject obj,
   9.397 -        jbyteArray fpr
   9.398 -    )
   9.399 -{
   9.400 -    std::mutex *mutex_local = nullptr;
   9.401 -    {
   9.402 -        std::lock_guard<std::mutex> l(global_mutex);
   9.403 -        pEpLog("called with lock_guard");
   9.404 -        mutex_local = get_engine_java_object_mutex(env, obj);
   9.405 -    }
   9.406 -    std::lock_guard<std::mutex> l(*mutex_local);
   9.407 -
   9.408 -    const char *_fpr = to_string(env, fpr);
   9.409 -
   9.410 -    if(_fpr == NULL){
   9.411 -        throw_pEp_Exception(env, PEP_OUT_OF_MEMORY);
   9.412 -        return;
   9.413 -    }
   9.414 -
   9.415 -    PEP_STATUS status = passphraseWrap(::blacklist_delete, session(), _fpr);
   9.416 -    if (status != PEP_STATUS_OK) {
   9.417 -        throw_pEp_Exception(env, status);
   9.418 -        return;
   9.419 -    }
   9.420 -
   9.421 -}
   9.422 -
   9.423 -JNIEXPORT jboolean JNICALL Java_foundation_pEp_jniadapter_Engine__1blacklist_1is_1listed(
   9.424 -        JNIEnv *env,
   9.425 -        jobject obj,
   9.426 -        jbyteArray fpr
   9.427 -    )
   9.428 -{
   9.429 -    std::mutex *mutex_local = nullptr;
   9.430 -    {
   9.431 -        std::lock_guard<std::mutex> l(global_mutex);
   9.432 -        pEpLog("called with lock_guard");
   9.433 -        mutex_local = get_engine_java_object_mutex(env, obj);
   9.434 -    }
   9.435 -    std::lock_guard<std::mutex> l(*mutex_local);
   9.436 -
   9.437 -    const char *_fpr = to_string(env, fpr);
   9.438 -    bool _listed = 0;
   9.439 -
   9.440 -    if(_fpr == NULL){
   9.441 -        throw_pEp_Exception(env, PEP_OUT_OF_MEMORY);
   9.442 -        return 0;
   9.443 -    }
   9.444 -
   9.445 -    PEP_STATUS status = passphraseWrap(::blacklist_is_listed, session(), _fpr, &_listed);
   9.446 -    if (status != PEP_STATUS_OK) {
   9.447 -        throw_pEp_Exception(env, status);
   9.448 -        return 0;
   9.449 -    }
   9.450 -
   9.451 -    return (jboolean)_listed;
   9.452 -}
   9.453 -
   9.454 -JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_Engine__1getCrashdumpLog(
   9.455 -        JNIEnv *env,
   9.456 -        jobject obj,
   9.457 -        jint dummy,
   9.458 -        jint maxlines
   9.459 -    )
   9.460 -{
   9.461 -    std::mutex *mutex_local = nullptr;
   9.462 -    {
   9.463 -        std::lock_guard<std::mutex> l(global_mutex);
   9.464 -        pEpLog("called with lock_guard");
   9.465 -        mutex_local = get_engine_java_object_mutex(env, obj);
   9.466 -    }
   9.467 -    std::lock_guard<std::mutex> l(*mutex_local);
   9.468 -
   9.469 -    int _maxlines = (int) maxlines;
   9.470 -    char *_logdata;
   9.471 -
   9.472 -    PEP_STATUS status = passphraseWrap(::get_crashdump_log, session(), _maxlines, &_logdata);
   9.473 -    if ((status > PEP_STATUS_OK && status < PEP_UNENCRYPTED) ||
   9.474 -            status < PEP_STATUS_OK ||
   9.475 -            status >= PEP_TRUSTWORD_NOT_FOUND) {
   9.476 -        throw_pEp_Exception(env, status);
   9.477 -        return NULL;
   9.478 -    }
   9.479 -
   9.480 -    return from_string(env, _logdata);
   9.481 -}
   9.482 -
   9.483 -JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_Engine__1getUserDirectory(
   9.484 -    JNIEnv *env,
   9.485 -    jobject obj
   9.486 -    )
   9.487 -{
   9.488 -    pEpLog("called");
   9.489 -    return from_string(env, ::per_user_directory());
   9.490 -}
   9.491 -
   9.492 -JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_Engine__1getMachineDirectory(
   9.493 -    JNIEnv *env,
   9.494 -    jobject obj
   9.495 -    )
   9.496 -{
   9.497 -    pEpLog("called");
   9.498 -    return from_string(env, ::per_machine_directory());
   9.499 -}
   9.500 -
   9.501 -//void logPassphraseCache() {
   9.502 -//    try {
   9.503 -////        while(true) {
   9.504 -//            pEpLog("Cache: '" << cache.latest_passphrase() << "'");
   9.505 -////        }
   9.506 -//    } catch(pEp::PassphraseCache::Empty e) {
   9.507 -//        pEpLog(e.what());
   9.508 -//    } catch(pEp::PassphraseCache::Exhausted ex) {
   9.509 -//        pEpLog(ex.what());
   9.510 -//    }
   9.511 -//}
   9.512 -
   9.513 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1config_1passphrase
   9.514 -  (JNIEnv * env,
   9.515 -   jobject obj,
   9.516 -   jbyteArray passphrase)
   9.517 -{
   9.518 -    std::mutex *mutex_local = nullptr;
   9.519 -    {
   9.520 -        std::lock_guard<std::mutex> l(global_mutex);
   9.521 -        pEpLog("called with lock_guard");
   9.522 -        mutex_local = get_engine_java_object_mutex(env, obj);
   9.523 -    }
   9.524 -    std::lock_guard<std::mutex> l(*mutex_local);
   9.525 -
   9.526 -    char* _passphrase = to_string(env, passphrase);
   9.527 -
   9.528 -    PEP_STATUS status = ::config_passphrase(session(),passphrase_cache.add(_passphrase));
   9.529 -    if (status != 0) {
   9.530 -        throw_pEp_Exception(env, status);
   9.531 -        return;
   9.532 -    }
   9.533 -}
   9.534 -
   9.535 -
   9.536 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1config_1passphrase_1for_1new_1keys(
   9.537 -        JNIEnv *env,
   9.538 -        jobject obj,
   9.539 -        jboolean enable,
   9.540 -        jbyteArray passphrase
   9.541 -    )
   9.542 -{
   9.543 -    std::mutex *mutex_local = nullptr;
   9.544 -    {
   9.545 -        std::lock_guard<std::mutex> l(global_mutex);
   9.546 -        pEpLog("called with lock_guard");
   9.547 -        mutex_local = get_engine_java_object_mutex(env, obj);
   9.548 -    }
   9.549 -    std::lock_guard<std::mutex> l(*mutex_local);
   9.550 -
   9.551 -    bool _enable = (bool) enable;
   9.552 -    const char *_passphrase = to_string(env, passphrase);
   9.553 -
   9.554 -    PEP_STATUS status = ::config_passphrase_for_new_keys(session(),_enable,passphrase_cache.add_stored(_passphrase));
   9.555 -    if ((status > PEP_STATUS_OK && status < PEP_UNENCRYPTED) ||
   9.556 -            status < PEP_STATUS_OK ||
   9.557 -            status >= PEP_TRUSTWORD_NOT_FOUND) {
   9.558 -        throw_pEp_Exception(env, status);
   9.559 -        return ;
   9.560 -    }
   9.561 -
   9.562 -
   9.563 -}
   9.564 -
   9.565 -} // extern "C"
   9.566 -
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/src/codegen/Makefile	Fri Aug 28 18:18:26 2020 +0200
    10.3 @@ -0,0 +1,60 @@
    10.4 +# Copyright 2018, pEp Foundation
    10.5 +# This file is part of pEp JNI Adapter
    10.6 +# This file may be used under the terms of the GNU General Public License version 3
    10.7 +# see LICENSE.txt
    10.8 +
    10.9 +include ../../Makefile.conf
   10.10 +
   10.11 +MARKER_DIR=../../build/marker/
   10.12 +
   10.13 +PEP_HEADER:=$(shell $(CXX) $(CXXFLAGS) -E -M ../cxx/get_header.cc | grep -oe '[^[:space:]]*pEpEngine\.h' | head -1)
   10.14 +
   10.15 +# Every ysl2 file that needs to be "compiled" separately, needs to generate a "marker" file
   10.16 +# The marker serves as the make target.
   10.17 +# If the marker file is older than its corresponding ysl2 file, or not exsiting, the ysl2 file will be "compiled"
   10.18 +# Naming:
   10.19 +# For a ysl2 file called "gen_example_stuff.ysl2", a marker file called "gen_example_stuff.marker" is expected.
   10.20 +YML2_MARKERS= \
   10.21 +	$(MARKER_DIR)/gen_java_Engine.marker \
   10.22 +	$(MARKER_DIR)/gen_java_Message.marker \
   10.23 +	$(MARKER_DIR)/gen_cpp_Engine.marker \
   10.24 +	$(MARKER_DIR)/gen_cpp_Message.marker \
   10.25 +	$(MARKER_DIR)/gen_throw_pEp_exception.marker
   10.26 +
   10.27 +# All code genration will be done upon change of these files
   10.28 +YML2_INCLUDES= \
   10.29 +	textutils.ysl2 \
   10.30 +    types_c.ysl2 \
   10.31 +    types_java.ysl2
   10.32 +
   10.33 +
   10.34 +.PHONY: all codegen gen-status-codes create-dirs remove-dirs clean
   10.35 +
   10.36 +all: codegen
   10.37 +
   10.38 +# ------------- YML2 CodeGen --------------
   10.39 +codegen: create-dirs gen-status-codes $(YML2_MARKERS)
   10.40 +
   10.41 +$(YML2_MARKERS): $(MARKER_DIR)/%.marker : %.ysl2 pEp.yml2 $(YML2_INCLUDES)
   10.42 +	$(YML2_PROC) -y $< pEp.yml2
   10.43 +
   10.44 +gen-status-codes: status_list.yml2 passphrase_status_list.yml2
   10.45 +
   10.46 +status_list.yml2: pEp.yml2
   10.47 +	bash ../../utils/extract_pEp_status_codes_from_engine.sh "$(PEP_HEADER)" $@
   10.48 +
   10.49 +passphrase_status_list.yml2: status_list.yml2
   10.50 +	grep passphrase $< > $@
   10.51 +
   10.52 +# ------------- Housekeeping ---------------
   10.53 +create-dirs:
   10.54 +	mkdir -p $(MARKER_DIR)
   10.55 +
   10.56 +#rm -rf is too dangerous for vars
   10.57 +remove-dirs:
   10.58 +	rm -rf ../../build/marker
   10.59 +
   10.60 +clean: remove-dirs
   10.61 +	rm -f status_list.yml2
   10.62 +	rm -f passphrase_status_list.yml2
   10.63 +
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/src/codegen/gen_cpp_Engine.ysl2	Fri Aug 28 18:18:26 2020 +0200
    11.3 @@ -0,0 +1,324 @@
    11.4 +include yslt.yml2
    11.5 +
    11.6 +tstylesheet {
    11.7 +    include ./textutils.ysl2
    11.8 +    include ./types_c.ysl2
    11.9 +    include ./types_java.ysl2
   11.10 +
   11.11 +    template "/namespace[@name='pEp']" {
   11.12 +        apply "interface", 0;
   11.13 +        document("../../build/marker/gen_cpp_Engine.marker", "text") > ""
   11.14 +    }
   11.15 +
   11.16 +    template "interface" document("../cxx/foundation_pEp_jniadapter_{@name}.cc", "text")
   11.17 +        ||
   11.18 +        #include <cassert>
   11.19 +        #include <pEp/blacklist.h>
   11.20 +        #include <pEp/openpgp_compat.h>
   11.21 +        #include <pEp/key_reset.h>
   11.22 +        #include <pEp/Adapter.hh>
   11.23 +        #include <pEp/pEpLog.hh>
   11.24 +        #include <pEp/passphrase_cache.hh>
   11.25 +
   11.26 +        #include "foundation_pEp_jniadapter_«@name».h"
   11.27 +        #include "throw_pEp_exception.hh"
   11.28 +        #include "jniutils.hh"
   11.29 +        #include "passphrase_callback.hh"
   11.30 +
   11.31 +        using pEp::Adapter::session;
   11.32 +        using pEp::passphrase_cache;
   11.33 +        using namespace pEp::JNIAdapter;
   11.34 +
   11.35 +        extern "C" {
   11.36 +
   11.37 +        `` apply "method", 0
   11.38 +        } // extern "C"
   11.39 +
   11.40 +        ||
   11.41 +
   11.42 +    template "method" {
   11.43 +        const "mangled" call "mangle";
   11.44 +        const "returning", "count(parm/creates|parm/returns)";
   11.45 +        const "CretType" choose {
   11.46 +            when "$returning = 0" > void
   11.47 +            when "$returning = 1"
   11.48 +                call "jni_type" with "type", "name(parm[returns|creates]/*[2])";
   11.49 +            when "$returning > 0" > jobject
   11.50 +        }
   11.51 +        const "CretDefault" choose {
   11.52 +            when "$returning = 0" >
   11.53 +            when "$returning > 0" > NULL
   11.54 +        }
   11.55 +
   11.56 +        ||
   11.57 +        JNIEXPORT «$CretType» JNICALL «$mangled»(
   11.58 +                JNIEnv *env,
   11.59 +                jobject obj`apply "parm[in|inout]", mode=sig`
   11.60 +            )
   11.61 +        {
   11.62 +            std::mutex *mutex_local = nullptr;
   11.63 +            {
   11.64 +                std::lock_guard<std::mutex> l(global_mutex);
   11.65 +                pEpLog("called with lock_guard");
   11.66 +                mutex_local = get_engine_java_object_mutex(env, obj);
   11.67 +            }
   11.68 +            std::lock_guard<std::mutex> l(*mutex_local);
   11.69 +
   11.70 +        ||
   11.71 +
   11.72 +        apply "parm[in|inout]", mode=in;
   11.73 +        apply "parm[creates|returns]", mode=out;
   11.74 +        ||
   11.75 +
   11.76 +        ||
   11.77 +        choose {
   11.78 +            when "@cached = 'true'" {
   11.79 +            ||
   11.80 +                PEP_STATUS status = passphraseWrap(::«@name», session()`apply "parm", mode=call`);
   11.81 +            ||
   11.82 +            } otherwise {
   11.83 +            ||
   11.84 +                PEP_STATUS status = ::«@name»(session()`apply "parm", mode=call`);
   11.85 +            ||
   11.86 +            }
   11.87 +        }
   11.88 +        ||
   11.89 +            if ((status > PEP_STATUS_OK && status < PEP_UNENCRYPTED) |`> |`
   11.90 +                    status < PEP_STATUS_OK |`> |`
   11.91 +                    status >= PEP_TRUSTWORD_NOT_FOUND) {
   11.92 +                throw_pEp_Exception(env, status);
   11.93 +                return «$CretDefault»;
   11.94 +            }
   11.95 +
   11.96 +        ||
   11.97 +
   11.98 +        apply "parm[returns|creates]", mode=return;
   11.99 +
  11.100 +        choose {
  11.101 +            when "$returning > 1" {
  11.102 +
  11.103 +                ||
  11.104 +                    jobject result_ = NULL;
  11.105 +                    jclass clazz_result_ = findClass(env, "foundation/pEp/jniadapter/Engine$_«@name»_Return");
  11.106 +                    assert(clazz_result_);
  11.107 +                    jmethodID constructor_result_ = env->GetMethodID(clazz_result_, "<init>", "(Lfoundation/pEp/jniadapter/Engine;)V");
  11.108 +                    assert(constructor_result_);
  11.109 +                    result_ = env->NewObject(clazz_result_, constructor_result_, obj);
  11.110 +                ||
  11.111 +
  11.112 +                apply "parm[returns|creates|inout[../int]]", mode=setresult
  11.113 +                    with "jtype" > foundation/pEp/jniadapter/Engine$_«@name»_Return
  11.114 +
  11.115 +                ||
  11.116 +                    return result_;
  11.117 +                ||
  11.118 +            }
  11.119 +
  11.120 +            when "$returning = 1" {
  11.121 +
  11.122 +                ||
  11.123 +                    return «name(parm[returns|creates|inout[../int]]/*[3])»_;
  11.124 +                ||
  11.125 +            }
  11.126 +        }
  11.127 +
  11.128 +        ||
  11.129 +        }
  11.130 +
  11.131 +        ||
  11.132 +    }
  11.133 +
  11.134 +    template "parm", mode=setresult {
  11.135 +        param "jtype";
  11.136 +        const "dir", "name(*[1])";
  11.137 +        const "type", "name(*[2])";
  11.138 +        const "name", "name(*[3])";
  11.139 +        const "sig" call "toSig" with "type", "$type";
  11.140 +
  11.141 +
  11.142 +        choose {
  11.143 +            when "$type='int'"
  11.144 +                ||
  11.145 +                jfieldID «$name»_field = env->GetFieldID(clazz_result_, "«$name»", "I");
  11.146 +                ||
  11.147 +            otherwise {
  11.148 +                ||
  11.149 +                jfieldID «$name»_field = getFieldID(env, "«$jtype»", "«$name»", "«$sig»");
  11.150 +                ||
  11.151 +
  11.152 +            }
  11.153 +        }
  11.154 +
  11.155 +        choose {
  11.156 +            when "$type='int'"
  11.157 +                | env->SetIntField(result_, «$name»_field, _«$name»);
  11.158 +            otherwise {
  11.159 +                ||
  11.160 +                if («$name»_)
  11.161 +                    env->SetObjectField(result_, «$name»_field, «$name»_);
  11.162 +                ||
  11.163 +                if "$name = 'dst'"
  11.164 +                ||
  11.165 +                else
  11.166 +                    env->SetObjectField(result_, «$name»_field, src);
  11.167 +                ||
  11.168 +            }
  11.169 +        }
  11.170 +
  11.171 +        ||
  11.172 +
  11.173 +        ||
  11.174 +    }
  11.175 +
  11.176 +    template "parm", mode=return {
  11.177 +        param "dir", "name(*[1])";
  11.178 +        param "type", "name(*[2])";
  11.179 +        param "jtype" call "toJava" with "type", "$type";
  11.180 +        param "name", "name(*[3])";
  11.181 +
  11.182 +        choose {
  11.183 +            when "$type = 'stringlist' or $type = 'string' or $type = 'sstring' or $type = 'stringpairlist' or $type = 'identity' or $type = 'identitylist'"
  11.184 +            {
  11.185 +                const "jnitype" call "jni_type" with "type", "$type";
  11.186 +                const "from_type" choose {
  11.187 +                    when "$type = 'sstring'"
  11.188 +                        > from_string
  11.189 +                    otherwise
  11.190 +                        > from_«$type»
  11.191 +                }
  11.192 +                ||
  11.193 +                «$jnitype» «$name»_ = NULL;
  11.194 +                if (_«$name»)
  11.195 +                    «$name»_ = «$from_type»(env, _«$name»);
  11.196 +
  11.197 +                ||
  11.198 +            }
  11.199 +            when "ancestor::namespace/child::enum[@name=$type]" {
  11.200 +                const "ljtype" call "lcase" with "text","$jtype";
  11.201 +                ||
  11.202 +                jobject «$name»_ = NULL;
  11.203 +                {
  11.204 +                    jclass clazz_«$ljtype» = findClass(env, "foundation/pEp/jniadapter/«$jtype»");
  11.205 +                    assert(clazz_«$ljtype»);
  11.206 +                    jmethodID method_values = env->GetStaticMethodID(clazz_«$ljtype», "values",
  11.207 +                            "()[Lfoundation/pEp/jniadapter/«$jtype»;");
  11.208 +                    assert(method_values);
  11.209 +                    jfieldID field_value = env->GetFieldID(clazz_«$ljtype», "value", "I");
  11.210 +                    assert(field_value);
  11.211 +
  11.212 +                    jobjectArray values = (jobjectArray) env->CallStaticObjectMethod(clazz_«$ljtype»,
  11.213 +                            method_values);
  11.214 +                    assert(values);
  11.215 +                    if (env->ExceptionCheck()) {
  11.216 +                        return nullptr; // handle exception in Java
  11.217 +                    }
  11.218 +
  11.219 +                    jsize values_size = env->GetArrayLength(values);
  11.220 +                    for (jsize i = 0; i < values_size; i++) {
  11.221 +                        jobject element = env->GetObjectArrayElement(values, i);
  11.222 +                        assert(element);
  11.223 +                        jint value = env->GetIntField(element, field_value);
  11.224 +                        if (value == (jint) _«$name») {
  11.225 +                            «$name»_ = element;
  11.226 +                            break;
  11.227 +                        }
  11.228 +                    }
  11.229 +                }
  11.230 +
  11.231 +                ||
  11.232 +            }
  11.233 +            otherwise {
  11.234 +                ||
  11.235 +                jobject «$name»_ = NULL;
  11.236 +                if (_«$name») {
  11.237 +                    jclass clazz_«$name»_ = findClass(env, "foundation/pEp/jniadapter/«$jtype»");
  11.238 +                    assert(clazz_«$name»_);
  11.239 +                    jmethodID constructor_«$name»_ = env->GetMethodID(clazz_«$name»_, "<init>", "(J)V");
  11.240 +                    assert(constructor_«$name»_);
  11.241 +                    «$name»_ = env->NewObject(clazz_«$name»_, constructor_«$name»_, (jlong) _«$name»);
  11.242 +                }
  11.243 +
  11.244 +                ||
  11.245 +            }
  11.246 +        }
  11.247 +    }
  11.248 +
  11.249 +    template "parm", mode=in {
  11.250 +        const "dir", "name(*[1])";
  11.251 +        const "type", "name(*[2])";
  11.252 +        const "name", "name(*[3])";
  11.253 +        const "const", "name(*[4])";
  11.254 +
  11.255 +        choose {
  11.256 +            when "$type = 'message'"
  11.257 +                | message *_«$name» = (message *) callLongMethod(env, «$name», "getHandle");
  11.258 +            when "$type='EncFormat' or $type='SyncHandshakeResult' or $type='CipherSuite'"
  11.259 +                | √$type _«$name» = to_«$type»(env, «$name»);
  11.260 +            when "$type='bool'"
  11.261 +                | bool _«$name» = (bool) «$name»;
  11.262 +            when "$type='int'"
  11.263 +                | int _«$name» = (int) «$name»;
  11.264 +            when "$type='uint'"
  11.265 +                | auto _«$name» = (unsigned int) «$name»;
  11.266 +            when "$type='string'"
  11.267 +                | const char *_«$name» = to_string(env, «$name»);
  11.268 +            otherwise {
  11.269 +                choose {
  11.270 +                    when "$const"
  11.271 +                        | const √$type *_«$name» = to_«$type»(env, «$name»);
  11.272 +                    otherwise
  11.273 +                        | √$type *_«$name» = to_«$type»(env, «$name»);
  11.274 +                }
  11.275 +            }
  11.276 +        }
  11.277 +    }
  11.278 +
  11.279 +    template "parm", mode=out {
  11.280 +        param "name", "name(*[3])";
  11.281 +        const "type", "name(*[2])";
  11.282 +
  11.283 +        choose {
  11.284 +            when "ancestor::namespace/child::enum[@name=$type]"
  11.285 +                | √$type _«$name»;
  11.286 +            when "$type='sstring'"{
  11.287 +                | char * _«$name»;
  11.288 +                | size_t _«$name»_size;
  11.289 +            }
  11.290 +            otherwise
  11.291 +                | √$type *_«$name»;
  11.292 +        }
  11.293 +    }
  11.294 +
  11.295 +    template "parm", mode=call {
  11.296 +        const "dir", "name(*[1])";
  11.297 +        const "type", "name(*[2])";
  11.298 +        const "name", "name(*[3])";
  11.299 +
  11.300 +        > ,
  11.301 +
  11.302 +        choose {
  11.303 +            when "$dir = 'Cconst'"
  11.304 +                value "*[3]";
  11.305 +            when "($dir = 'creates' or $dir = 'returns') and $type='sstring'"
  11.306 +                > &_«$name», &_«$name»_size
  11.307 +            when "$dir = 'creates' or $dir = 'returns'"
  11.308 +                > &_«$name»
  11.309 +            when "$dir = 'inout' and $type='int'"
  11.310 +                > (unsigned int *) &_«$name»
  11.311 +            when "$dir = 'inout' and $type='stringlist'"
  11.312 +                > &_«$name»
  11.313 +            otherwise
  11.314 +                > _«$name»
  11.315 +        }
  11.316 +    }
  11.317 +
  11.318 +    function "mangle" {
  11.319 +        > «concat('Java_foundation_pEp_jniadapter_', ../@name, '__1', str:replace(@name, '_', '_1'))»
  11.320 +    }
  11.321 +
  11.322 +    template "parm", mode=sig {
  11.323 +        const "name", "name(*[3])";
  11.324 +        > ,\n        `call "jni_type" with "type", "name(*[2])", with "dir", "name(*[1])"` «$name»
  11.325 +    }
  11.326 +}
  11.327 +
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/src/codegen/gen_cpp_Message.ysl2	Fri Aug 28 18:18:26 2020 +0200
    12.3 @@ -0,0 +1,194 @@
    12.4 +include yslt.yml2
    12.5 +include yslt.yml2
    12.6 +
    12.7 +tstylesheet {
    12.8 +    include ./textutils.ysl2
    12.9 +    include ./types_c.ysl2
   12.10 +
   12.11 +    template "/namespace[@name='pEp']" {
   12.12 +        apply "struct", 0;
   12.13 +        document("../../build/marker/gen_cpp_Message.marker", "text") > ""
   12.14 +    }
   12.15 +
   12.16 +    template "struct" {
   12.17 +        const "jname" call "CamelCase" with "text", "@name";
   12.18 +
   12.19 +        document("../cxx/foundation_pEp_jniadapter_{$jname}.cc", "text") {
   12.20 +            ||
   12.21 +            #include <cassert>
   12.22 +            #include <pEp/mime.h>
   12.23 +            #include <pEp/pEpLog.hh>
   12.24 +            #include "jniutils.hh"
   12.25 +            #include "throw_pEp_exception.hh"
   12.26 +            #include "foundation_pEp_jniadapter_«$jname».h"
   12.27 +
   12.28 +            using namespace std;
   12.29 +
   12.30 +            namespace pEp {
   12.31 +                namespace JNIAdapter {
   12.32 +                    static ::«@name» *«@name»_ptr(JNIEnv *env, jobject me) {
   12.33 +                        jfieldID handle;
   12.34 +
   12.35 +                        try {
   12.36 +                            handle = getFieldID(env, "foundation/pEp/jniadapter/«$jname»", "handle", "J");
   12.37 +                        }
   12.38 +                        catch (std::exception& ex) {
   12.39 +                            assert(0);
   12.40 +                            return NULL;
   12.41 +                        }
   12.42 +
   12.43 +                        return (::«@name» *) (intptr_t) (int64_t) env->GetLongField(me, handle);
   12.44 +                    }
   12.45 +                };
   12.46 +            };
   12.47 +
   12.48 +            extern "C" {
   12.49 +
   12.50 +            using namespace pEp::JNIAdapter;
   12.51 +
   12.52 +            JNIEXPORT jlong JNICALL Java_foundation_pEp_jniadapter_«$jname»_init(JNIEnv *env, jobject obj)
   12.53 +            {
   12.54 +                pEpLog("called");
   12.55 +                ::«@name» * _obj = ::new_«@name»(PEP_dir_incoming);
   12.56 +                if (!_obj) {
   12.57 +                    outOfMemory(env);
   12.58 +                    return (jlong) 0;
   12.59 +                }
   12.60 +                return (jlong) (int64_t) (intptr_t) _obj;
   12.61 +            }
   12.62 +
   12.63 +            JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_«$jname»_release(JNIEnv *env, jobject obj, jlong value)
   12.64 +            {
   12.65 +                pEpLog("called");
   12.66 +                if (value) {
   12.67 +                    ::«@name» *_obj = (::«@name» *) (intptr_t) (int64_t) value;
   12.68 +                    ::free_«@name»(_obj);
   12.69 +                }
   12.70 +            }
   12.71 +
   12.72 +            JNIEXPORT jlong JNICALL Java_foundation_pEp_jniadapter_«$jname»__1«$jname»(JNIEnv *env, jobject msg, jbyteArray mime_text)
   12.73 +            {
   12.74 +                pEpLog("called");
   12.75 +                char *_mime_text = to_string(env, mime_text);
   12.76 +                size_t _size = (size_t) env->GetArrayLength(mime_text);
   12.77 +
   12.78 +                ::«@name» *_msg = nullptr;
   12.79 +                PEP_STATUS status = mime_decode_«@name»(_mime_text, _size, &_msg, NULL);
   12.80 +                if (status)
   12.81 +                    throw_pEp_Exception(env, status);
   12.82 +                return (jlong) (int64_t) (intptr_t) _msg;
   12.83 +            }
   12.84 +
   12.85 +            JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_«$jname»__1encodeMIME(JNIEnv *env, jobject msg)
   12.86 +            {
   12.87 +                pEpLog("called");
   12.88 +                «@name» *_obj = «@name»_ptr(env, msg);
   12.89 +                char *mime_text = nullptr;
   12.90 +                PEP_STATUS status = ::mime_encode_«@name»(_obj, false, &mime_text, false);
   12.91 +                if (status)
   12.92 +                    throw_pEp_Exception(env, status);
   12.93 +                jbyteArray result = from_string(env, mime_text);
   12.94 +                free(mime_text);
   12.95 +                return result;
   12.96 +            }
   12.97 +
   12.98 +            ||
   12.99 +            apply "*[name(.)!='enum']", 0, mode=entry {
  12.100 +                with "name", "@name";
  12.101 +                with "class" call "CamelCase" with "text", "@name";
  12.102 +            }
  12.103 +            ||
  12.104 +            } // extern "C"
  12.105 +
  12.106 +            ||
  12.107 +        }
  12.108 +    }
  12.109 +
  12.110 +    function "mangle" {
  12.111 +        param "type";
  12.112 +        param "name";
  12.113 +        param "classname" call "CamelCase" with "text", "../@name";
  12.114 +        const "convert", "$type != 'timestamp'";
  12.115 +
  12.116 +        choose {
  12.117 +            when "$convert"
  12.118 +                > «concat('Java_foundation_pEp_jniadapter_', $classname, '__1', str:replace($name, '_', '_1'))»
  12.119 +            otherwise
  12.120 +                > «concat('Java_foundation_pEp_jniadapter_', $classname, '_', str:replace($name, '_', '_1'))»
  12.121 +        }
  12.122 +    }
  12.123 +
  12.124 +    template "*", mode=entry {
  12.125 +        param "name";
  12.126 +        param "class";
  12.127 +        const "ctype" call "toC" with "type", "name(.)";
  12.128 +        const "jname" call "CamelCase" with "text", "name(*[1])";
  12.129 +        const "cname" call "lcase" with "text", "name(*[1])";
  12.130 +        const "type", "name(.)";
  12.131 +        const "getname" call "mangle" {
  12.132 +            with "type", "$type";
  12.133 +            with "name", "concat('get', $jname)";
  12.134 +        }
  12.135 +        const "setname" call "mangle" {
  12.136 +            with "type", "$type";
  12.137 +            with "name", "concat('set', $jname)";
  12.138 +        }
  12.139 +        ||
  12.140 +        JNIEXPORT `call "jni_type" with "type", "name(.)"` JNICALL «$getname»(JNIEnv *env, jobject obj)
  12.141 +        {
  12.142 +            pEpLog("called");
  12.143 +            «$name» *_obj = «$name»_ptr(env, obj);
  12.144 +        ||
  12.145 +        choose {
  12.146 +            when "../enum[@name=$type]"
  12.147 +                |> return (jint) _obj->«$cname»;
  12.148 +            otherwise
  12.149 +                |> return from_«$type»(env, _obj->«$cname»);
  12.150 +        }
  12.151 +        ||
  12.152 +        }
  12.153 +
  12.154 +        JNIEXPORT void JNICALL «$setname»(JNIEnv *env, jobject obj, `call "jni_type" with "type", "name(.)"` value)
  12.155 +        {
  12.156 +            pEpLog("called");
  12.157 +            «$name» *_obj = «$name»_ptr(env, obj);
  12.158 +        ||
  12.159 +        choose {
  12.160 +            when "../enum[@name=$type]"
  12.161 +                |> _obj->«$cname» = («$ctype») (int) value;
  12.162 +            otherwise {
  12.163 +                choose {
  12.164 +                    when "$type = 'string'" {
  12.165 +                        |> free(_obj->«$cname»);
  12.166 +                    }
  12.167 +                    otherwise {
  12.168 +                        const "free" choose {
  12.169 +                            when "$ctype = 'pEp_identity'" > identity
  12.170 +                            when "contains($ctype, '_t')" value "substring-before($ctype, '_t')";
  12.171 +                            otherwise value "$ctype";
  12.172 +                        }
  12.173 +
  12.174 +                        |> free_«$free»(_obj->«$cname»);
  12.175 +                    }
  12.176 +                }
  12.177 +                |> if(value){
  12.178 +                choose {
  12.179 +                    when "$type = 'string'" {
  12.180 +                        |>> _obj->«$cname» = to_«$type»(env, value);
  12.181 +                    }
  12.182 +                    otherwise {
  12.183 +                        |>> _obj->«$cname» = to_«$type»(env, value);
  12.184 +                    }
  12.185 +                }
  12.186 +                |> }else{
  12.187 +                |>     _obj->«$cname» = NULL;
  12.188 +                |> }
  12.189 +            }
  12.190 +        }
  12.191 +        ||
  12.192 +        }
  12.193 +
  12.194 +        ||
  12.195 +    }
  12.196 +}
  12.197 +
    13.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2 +++ b/src/codegen/gen_java_Engine.ysl2	Fri Aug 28 18:18:26 2020 +0200
    13.3 @@ -0,0 +1,364 @@
    13.4 +include yslt.yml2
    13.5 +
    13.6 +tstylesheet {
    13.7 +    include ./textutils.ysl2
    13.8 +    include ./types_java.ysl2
    13.9 +
   13.10 +    template "/namespace[@name='pEp']" {
   13.11 +        apply "interface", 0;
   13.12 +        document("../../build/marker/gen_java_Engine.marker", "text") > ""
   13.13 +    }
   13.14 +
   13.15 +    template "interface" {
   13.16 +        const "cname" call "toJava" with "type", "@name";
   13.17 +        document("../java/foundation/pEp/jniadapter/{$cname}.java", "text")
   13.18 +            ||
   13.19 +            package foundation.pEp.jniadapter;
   13.20 +
   13.21 +            import foundation.pEp.jniadapter.interfaces.*;
   13.22 +            import foundation.pEp.jniadapter.exceptions.*;
   13.23 +            import java.util.ArrayList;
   13.24 +            import java.util.Vector;
   13.25 +
   13.26 +            final public class «$cname» extends AbstractEngine implements EngineInterface {
   13.27 +                public «$cname»() throws pEpException { }
   13.28 +                `` apply "method"
   13.29 +                `` apply "basic"
   13.30 +            }
   13.31 +
   13.32 +            ||
   13.33 +        document("../java/foundation/pEp/jniadapter/interfaces/{$cname}Interface.java", "text")
   13.34 +            ||
   13.35 +            package foundation.pEp.jniadapter.interfaces;
   13.36 +
   13.37 +            import foundation.pEp.jniadapter.*;
   13.38 +            import java.util.ArrayList;
   13.39 +            import java.util.Vector;
   13.40 +
   13.41 +            public interface «$cname»Interface extends AbstractEngineInterface {
   13.42 +                `` apply "method", mode=java_interface
   13.43 +                `` apply "basic", mode=java_interface
   13.44 +            }
   13.45 +            ||
   13.46 +    }
   13.47 +
   13.48 +    template "basic" {
   13.49 +        const "itype" call "toIntermediate" with "type", "@type";
   13.50 +        const "jtype" call "toJava" with "type", "@type";
   13.51 +
   13.52 +        ||
   13.53 +
   13.54 +        // CodeGen "basic"
   13.55 +        public «$jtype» «@name»(`apply "parm/*", mode=basic_parm`) {
   13.56 +        ||
   13.57 +        apply "parm/*", mode=basic_parm_set;
   13.58 +        choose {
   13.59 +            when "@type = 'void'"
   13.60 +                |> _«@name»(`apply "parm/*", mode=basic_parm_name`);
   13.61 +            when "@type = 'identity'"
   13.62 +                |> return new Identity(_«@name»(`apply "parm/*", mode=basic_parm_name`));
   13.63 +            when "@type = 'bool'"
   13.64 +                |> return new Boolean(_«@name»(`apply "parm/*", mode=basic_parm_name`));
   13.65 +            when "@type = 'identitylist'" {
   13.66 +            ||
   13.67 +                Vector<_Identity> glist = _«@name»(`apply "parm/*", mode=basic_parm_name`);
   13.68 +                Vector<Identity> ret = new Vector<Identity>();
   13.69 +                if(glist != null) {
   13.70 +                    for (_Identity i : glist) {
   13.71 +                        ret.add(new Identity(i));
   13.72 +                    }
   13.73 +                }
   13.74 +                return ret;
   13.75 +            ||
   13.76 +            } otherwise
   13.77 +                |> return Utils.toUTF16(_«@name»(`apply "parm/*", mode=basic_parm_name`));
   13.78 +        }
   13.79 +        ||
   13.80 +        }
   13.81 +
   13.82 +        private native «$itype» _«@name»(`apply "parm/*", mode=basic_iparm`);
   13.83 +        ||
   13.84 +    }
   13.85 +
   13.86 +    template "basic" mode=java_interface {
   13.87 +        const "jtype" call "toJava" with "type", "@type";
   13.88 +        ||
   13.89 +        public «$jtype» «@name»(`apply "parm/*", mode=basic_parm`);
   13.90 +        ||
   13.91 +    }
   13.92 +
   13.93 +    template "*", mode=basic_parm_name choose {
   13.94 +        when "position() mod 2" {
   13.95 +            if "position() = 1 and name(.)='int'"
   13.96 +                > 23, 
   13.97 +            if "position() > 1" > , 
   13.98 +        }
   13.99 +        otherwise {
  13.100 +            > _«name(.)»
  13.101 +        }
  13.102 +    }
  13.103 +
  13.104 +    template "*", mode=basic_parm choose {
  13.105 +        when "position() mod 2" {
  13.106 +            if "position() > 1" > , 
  13.107 +            choose {
  13.108 +                when "name(.) = 'string'"
  13.109 +                    > String 
  13.110 +                when "name(.) = 'identity'"
  13.111 +                    > Identity 
  13.112 +                when "name(.) = 'identitylist'"
  13.113 +                    > IdentityList 
  13.114 +                when "name(.) = 'bool'"
  13.115 +                    > Boolean 
  13.116 +                when "name(.) = 'bytearray'"
  13.117 +                    > byte[] 
  13.118 +                otherwise
  13.119 +                    > «name(.)» 
  13.120 +            }
  13.121 +        }
  13.122 +        otherwise {
  13.123 +            > «name(.)»
  13.124 +        }
  13.125 +    }
  13.126 +
  13.127 +    template "*", mode=basic_iparm choose {
  13.128 +        when "position() mod 2" {
  13.129 +            const "pitype" call "toIntermediate" with "type", "name(.)";
  13.130 +            if "position() = 1 and name(.)='int'"
  13.131 +                > int dummy, 
  13.132 +            if "position() > 1" > , 
  13.133 +            > «$pitype» 
  13.134 +        }
  13.135 +        otherwise {
  13.136 +            > «name(.)»
  13.137 +        }
  13.138 +    }
  13.139 +
  13.140 +    template "*", mode=basic_parm_set {
  13.141 +        const "ptype", "name(preceding-sibling::*[1])";
  13.142 +        const "pname", "name(.)";
  13.143 +        const "pitype" call "toIntermediate" with "type", "$ptype";
  13.144 +
  13.145 +        if "not(position() mod 2)" choose {
  13.146 +            when "$ptype = 'string'"
  13.147 +            | «$pitype» _«$pname» = Utils.toUTF8(«$pname»);
  13.148 +            when "$ptype = 'bool'"
  13.149 +            | «$pitype» _«$pname» = «$pname».booleanValue();
  13.150 +            when "$ptype = 'bytearray'"
  13.151 +            | «$pitype» _«$pname» = «$pname».clone();
  13.152 +            when "$ptype = 'int'"
  13.153 +            | «$pitype» _«$pname» = «$pname»; 
  13.154 +            when "ancestor::namespace/child::enum[@name=$ptype]"
  13.155 +            | «$pitype» _«$pname» = «$pname».value;
  13.156 +            otherwise
  13.157 +            | «$pitype» _«$pname» = new «$pitype»(«$pname»);
  13.158 +        }
  13.159 +    }
  13.160 +
  13.161 +    function "returnJava" {
  13.162 +        const "returning", "count(parm/creates|parm/returns)";
  13.163 +        const "returntype" call "toJava" with "type", "name(parm[creates|returns]/*[2])";
  13.164 +
  13.165 +        choose {
  13.166 +            when "$returning = 0" > void
  13.167 +            when "$returning = 1" > «$returntype»
  13.168 +            otherwise > «@name»_Return
  13.169 +        }
  13.170 +    }
  13.171 +
  13.172 +    function "returnInter" {
  13.173 +        const "returning", "count(parm/creates|parm/returns)";
  13.174 +        const "returntype" call "toIntermediate" with "type", "name(parm[creates|returns]/*[2])";
  13.175 +
  13.176 +        choose {
  13.177 +            when "$returning = 0" > void
  13.178 +            when "$returning = 1" > «$returntype»
  13.179 +            otherwise > _«@name»_Return
  13.180 +        }
  13.181 +    }
  13.182 +
  13.183 +    template "method" {
  13.184 +        const "singlereturn", "count(parm/creates|parm/returns|parm/inout[../int]) = 1";
  13.185 +        const "multireturn", "count(parm/creates|parm/returns|parm/inout[../int]) > 1";
  13.186 +
  13.187 +        ||
  13.188 +
  13.189 +        //CodeGen "method"
  13.190 +        ||
  13.191 +
  13.192 +        if "$multireturn" {
  13.193 +            ||
  13.194 +            //CodeGen "mutlireturn" - class for return type
  13.195 +            private class _«@name»_Return {
  13.196 +            `` apply "parm[creates|returns|inout[../int]]", 1, mode=intermediate with "public", 1;
  13.197 +            }
  13.198 +
  13.199 +            ||
  13.200 +        }
  13.201 +
  13.202 +        ||
  13.203 +        public `call "returnJava"` «@name»(
  13.204 +        `` apply "parm[in|inout]", 2, mode=java
  13.205 +        ) throws pEpException
  13.206 +        {
  13.207 +        ||
  13.208 +
  13.209 +        apply "parm", mode=convertIn;
  13.210 +
  13.211 +        if "$singlereturn" {
  13.212 +            choose {
  13.213 +                when "count(parm[returns][stringlist|string|sstring]) > 0"
  13.214 +                    |> return Utils.toUTF16(_«@name»(`apply "parm[in]", mode=call;`));
  13.215 +
  13.216 +                when "count(parm[returns]/stringpairlist) > 0"
  13.217 +                    ||
  13.218 +                        ArrayList<Pair<byte[], byte[]>> glist = _«@name»(`apply "parm[in]", mode=call;`);
  13.219 +                        if(glist != null){
  13.220 +                            ArrayList<Pair<String, String>> list = new ArrayList<Pair<String, String>>();
  13.221 +                            for (Pair<byte[],byte[]> i : glist) {
  13.222 +                                list.add(new Pair<String, String>(Utils.toUTF16(i.first), Utils.toUTF16(i.second)));
  13.223 +                            }
  13.224 +                            return list;
  13.225 +                        }
  13.226 +                        return null;
  13.227 +                    ||
  13.228 +
  13.229 +                when "count(parm[returns]/identity) > 0"
  13.230 +                    |> return new Identity(_«@name»(`apply "parm[in]", mode=call;`));
  13.231 +
  13.232 +                when "count(parm[returns]/identitylist) > 0"
  13.233 +                    ||
  13.234 +                        Vector<_Identity> glist = _«@name»(`apply "parm[in]", mode=call;`);
  13.235 +                        if(glist != null){
  13.236 +                            Vector<Identity> list = new Vector<Identity>();
  13.237 +                            for (_Identity i : glist) {
  13.238 +                                list.add(new Identity(i));
  13.239 +                            }
  13.240 +                            return list;
  13.241 +                        }
  13.242 +                        return null;
  13.243 +                    ||
  13.244 +
  13.245 +                otherwise
  13.246 +                    |> return _«@name»(`apply "parm[in]", mode=call;`);
  13.247 +            }
  13.248 +        }
  13.249 +
  13.250 +        if "$multireturn" {
  13.251 +            ||
  13.252 +                _«@name»_Return _result = _«@name»(`apply "parm[in|inout]", mode=call;`);
  13.253 +                «@name»_Return result = new «@name»_Return();
  13.254 +                `` apply "parm[creates|returns|inout[../int]]", mode=convertOut;
  13.255 +                return result;
  13.256 +            ||
  13.257 +        }
  13.258 +
  13.259 +        if "not($singlereturn) and not($multireturn)" {
  13.260 +            ||
  13.261 +                _«@name»(`apply "parm[in]", mode=call;`);
  13.262 +            ||
  13.263 +        }
  13.264 +        ||
  13.265 +        }
  13.266 +
  13.267 +        private native `call "returnInter"` _«@name»(
  13.268 +        `` apply "parm[in|inout]", 2, mode=intermediate
  13.269 +        ) throws pEpException;
  13.270 +        ||
  13.271 +    }
  13.272 +
  13.273 +    template "method", mode=java_interface {
  13.274 +        ||
  13.275 +        public `call "returnJava"` «@name»(
  13.276 +        `` apply "parm[in|inout]", 2, mode=java
  13.277 +        );
  13.278 +
  13.279 +        ||
  13.280 +    }
  13.281 +
  13.282 +    template "parm", mode=java {
  13.283 +        param "public", 0;
  13.284 +        const "dir", "name(*[1])";
  13.285 +        const "type", "name(*[2])";
  13.286 +        const "name", "name(*[3])";
  13.287 +
  13.288 +        indent(0);
  13.289 +
  13.290 +        if "$public" > public 
  13.291 +        call "toJava" with "type", "$type";
  13.292 +        >  «translate($name,'-','_')»
  13.293 +        choose {
  13.294 +            when "$public" > ;
  13.295 +            otherwise if "position()!=last()" > ,
  13.296 +        }
  13.297 +        > \n
  13.298 +    }
  13.299 +
  13.300 +    template "parm", mode=intermediate {
  13.301 +        param "public", 0;
  13.302 +        const "dir", "name(*[1])";
  13.303 +        const "type", "name(*[2])";
  13.304 +        const "name", "name(*[3])";
  13.305 +
  13.306 +        indent(0);
  13.307 +
  13.308 +        if "$public" > public 
  13.309 +        call "toIntermediate" with "type", "$type", with "dir", "$dir";
  13.310 +        >  «translate($name,'-','_')»
  13.311 +        choose {
  13.312 +            when "$public" > ;
  13.313 +            otherwise if "position()!=last()" > ,
  13.314 +        }
  13.315 +        > \n
  13.316 +    }
  13.317 +
  13.318 +    template "parm", mode=convertIn {
  13.319 +        const "dir", "name(*[1])";
  13.320 +        const "type", "name(*[2])";
  13.321 +        const "name" call "lcase" with "text", "name(*[3])";
  13.322 +
  13.323 +        if "$dir = 'in' or $dir = 'inout'" choose {
  13.324 +            when "$type = 'string' or $type = 'stringlist'"
  13.325 +                | ¡$type _«$name» = Utils.toUTF8(«$name»);
  13.326 +            when "$type = 'identity'"
  13.327 +                ||
  13.328 +                _Identity _«$name» = null; 
  13.329 +                if («$name» != null) {
  13.330 +                    _«$name» = new _Identity(«$name»);
  13.331 +                }
  13.332 +                ||
  13.333 +            when "$type = 'identitylist'"
  13.334 +                ||
  13.335 +                Vector<_Identity> _«$name» = new Vector<_Identity>();
  13.336 +                for (Identity i : «$name») {
  13.337 +                    _«$name».add(new _Identity(i));
  13.338 +                }
  13.339 +                ||
  13.340 +        }
  13.341 +    }
  13.342 +
  13.343 +    template "parm", mode=convertOut {
  13.344 +        const "dir", "name(*[1])";
  13.345 +        const "type", "name(*[2])";
  13.346 +        const "name" call "lcase" with "text", "name(*[3])";
  13.347 +
  13.348 +        if "$dir != 'in'" choose {
  13.349 +            when "$type = 'string' or $type = 'stringlist'"
  13.350 +                | result.«$name» = Utils.toUTF16(_result.«$name»);
  13.351 +            when "$type = 'identity'"
  13.352 +                | result.«$name» = Identity(«$name»);
  13.353 +            otherwise
  13.354 +                | result.«$name» = _result.«$name»;
  13.355 +        }
  13.356 +    }
  13.357 +
  13.358 +    template "parm", mode=call {
  13.359 +        const "type", "name(*[2])";
  13.360 +        const "name" call "lcase" with "text", "name(*[3])";
  13.361 +
  13.362 +        if "$type = 'string' or $type = 'stringlist' or $type = 'identity' or $type = 'identitylist'"
  13.363 +            > _
  13.364 +        > «$name»`if "position()!=last()" > , `
  13.365 +    }
  13.366 +}
  13.367 +
    14.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2 +++ b/src/codegen/gen_java_Message.ysl2	Fri Aug 28 18:18:26 2020 +0200
    14.3 @@ -0,0 +1,327 @@
    14.4 +include yslt.yml2
    14.5 +
    14.6 +tstylesheet {
    14.7 +    include ./textutils.ysl2
    14.8 +    include ./types_java.ysl2
    14.9 +
   14.10 +    template "/namespace[@name='pEp']" {
   14.11 +        apply "struct|enum|exception", 0;
   14.12 +        document "../java/foundation/pEp/jniadapter/exceptions/pEpException.java", "text" {
   14.13 +            ||
   14.14 +            package foundation.pEp.jniadapter.exceptions;
   14.15 +
   14.16 +            public class pEpException extends RuntimeException {
   14.17 +              public pEpException(String message) {
   14.18 +                super(message);
   14.19 +              }
   14.20 +            }
   14.21 +            ||
   14.22 +        }
   14.23 +        document("../../build/marker/gen_java_Message.marker", "text") > ""
   14.24 +    }
   14.25 +
   14.26 +    template "struct" {
   14.27 +        const "cname" call "toJava" with "type", "@name";
   14.28 +        document("../java/foundation/pEp/jniadapter/{$cname}.java", "text")
   14.29 +            ||
   14.30 +            package foundation.pEp.jniadapter;
   14.31 +
   14.32 +            import foundation.pEp.jniadapter.interfaces.*;
   14.33 +            import foundation.pEp.jniadapter.exceptions.*;
   14.34 +            import java.util.ArrayList;
   14.35 +            import java.util.Vector;
   14.36 +            import java.util.Date;
   14.37 +            import java.util.HashMap;
   14.38 +            import java.io.Serializable;
   14.39 +
   14.40 +            public class «$cname» implements MessageInterface, AutoCloseable, Serializable {
   14.41 +                private static final long serialVersionUID = 2119420428331150924L;
   14.42 +                private long handle;
   14.43 +
   14.44 +                native long init();
   14.45 +                native void release(long handle);
   14.46 +
   14.47 +                public «$cname»() {
   14.48 +                    handle = init();
   14.49 +                }
   14.50 +
   14.51 +                private native long _«$cname»(
   14.52 +                        byte[] mime_text
   14.53 +                    ) throws pEpException;
   14.54 +                
   14.55 +                public «$cname»(String mime_text) {
   14.56 +                    byte[] _mime_text = Utils.toUTF8(mime_text);
   14.57 +                    handle = _«$cname»(_mime_text);
   14.58 +                }
   14.59 +
   14.60 +                private native byte[] _encodeMIME() throws pEpException;
   14.61 +
   14.62 +                public String encodeMIME() {
   14.63 +                    return Utils.toUTF16(_encodeMIME());
   14.64 +                }
   14.65 +
   14.66 +                private «$cname»(long h) {
   14.67 +                    handle = h;
   14.68 +                }
   14.69 +
   14.70 +                public final void close() {
   14.71 +                    release(handle);
   14.72 +                }
   14.73 +
   14.74 +                final protected long getHandle() {
   14.75 +                    return handle;
   14.76 +                }
   14.77 +
   14.78 +                `` apply "enum", mode=inner
   14.79 +                `` apply "*[name(.)!='enum']", mode=entry
   14.80 +            }
   14.81 +            ||
   14.82 +        document("../java/foundation/pEp/jniadapter/interfaces/{$cname}Interface.java", "text")
   14.83 +            ||
   14.84 +            package foundation.pEp.jniadapter.interfaces;
   14.85 +
   14.86 +            import foundation.pEp.jniadapter.*;
   14.87 +            import foundation.pEp.jniadapter.Message.*;
   14.88 +            import java.util.Date;
   14.89 +            import java.util.Vector;
   14.90 +            import java.util.ArrayList;
   14.91 +
   14.92 +            public interface «$cname»Interface {
   14.93 +                public String encodeMIME();
   14.94 +
   14.95 +                `` apply "*[name(.)!='enum']", mode=interface
   14.96 +            }
   14.97 +            ||
   14.98 +    }
   14.99 +
  14.100 +    template "enum" {
  14.101 +        const "jname" call "toJava" with "type", "@name";
  14.102 +        document("../java/foundation/pEp/jniadapter/{$jname}.java", "text")
  14.103 +            ||
  14.104 +            // CodeGen template enum
  14.105 +            package foundation.pEp.jniadapter;
  14.106 +
  14.107 +            import java.util.HashMap;
  14.108 +
  14.109 +            `` apply ".", 0, mode=inner
  14.110 +            ||
  14.111 +
  14.112 +    }
  14.113 +
  14.114 +    template "enum", mode=inner {
  14.115 +        const "jname" call "CamelCase" with "text", "@name";
  14.116 +        ||
  14.117 +        // CodeGen template enum, mode=inner
  14.118 +        public enum «$jname» {
  14.119 +            `` apply "*", mode=value
  14.120 +            ;
  14.121 +
  14.122 +            public final int value;
  14.123 +
  14.124 +            private static HashMap<Integer, «$jname»> intMap;
  14.125 +
  14.126 +            private «$jname»(int value) {
  14.127 +                this.value = value;
  14.128 +            }
  14.129 +
  14.130 +            public static «$jname» getByInt(int value){
  14.131 +                if (intMap == null) {
  14.132 +                    intMap = new HashMap<Integer, «$jname»>();
  14.133 +                    for («$jname» s : «$jname».values()) {
  14.134 +                        intMap.put(s.value, s);
  14.135 +                    }
  14.136 +                }
  14.137 +                if (intMap.containsKey(value)) {
  14.138 +                    return intMap.get(value);
  14.139 +                }
  14.140 +                return null;
  14.141 +            }
  14.142 +        }
  14.143 +
  14.144 +        ||
  14.145 +    }
  14.146 +
  14.147 +    function "exception" {
  14.148 +        param "name";
  14.149 +
  14.150 +        document "../java/foundation/pEp/jniadapter/exceptions/{$name}.java", "text" {
  14.151 +            | package foundation.pEp.jniadapter.exceptions;
  14.152 +            |
  14.153 +            | public class «$name» extends pEpException {
  14.154 +            |   public «$name»(String message) {
  14.155 +            |     super(message);
  14.156 +            |   }
  14.157 +            | }
  14.158 +        }
  14.159 +    }
  14.160 +
  14.161 +    template "exception" for "*[text()!=0]" call "exception"
  14.162 +        with "name" call "CamelCase" with "text", "name(.)";
  14.163 +
  14.164 +
  14.165 +    template "*", mode=entry {
  14.166 +        const "ctype", "name(.)";
  14.167 +        const "type" call "toJava" with "type", "name(.)";
  14.168 +        const "itype" call "toIntermediate" with "type", "name(.)";
  14.169 +        const "name" call "toJava" with "type", "name(*[position()=1])";
  14.170 +
  14.171 +        ||
  14.172 +        // CodeGen template * mode=entry
  14.173 +        ||
  14.174 +        choose {
  14.175 +            when "$ctype = 'identity'" {
  14.176 +                ||
  14.177 +                // Property type: Identity. [java: «$type», intermediate: «$itype», ctype: «$ctype»]
  14.178 +                public «$type» get«$name»() {
  14.179 +                    «$itype» res = _get«$name»();
  14.180 +                    if (res != null) {
  14.181 +                        return new «$type»(_get«$name»());
  14.182 +                    } else {
  14.183 +                        return null;
  14.184 +                    }
  14.185 +                }
  14.186 +                private native «$itype» _get«$name»();
  14.187 +
  14.188 +                public void set«$name»(«$type» value) {
  14.189 +                    if (value != null) {
  14.190 +                        _set«$name»(new «$itype»(value));
  14.191 +                    } else {
  14.192 +                        _set«$name»(null);
  14.193 +                    }
  14.194 +                }
  14.195 +                private native void _set«$name»(«$itype» value);
  14.196 +
  14.197 +
  14.198 +                ||
  14.199 +            }
  14.200 +
  14.201 +            when "$ctype = 'identitylist' or $ctype = 'bloblist' or $ctype = 'stringlist' or $ctype = 'stringpairlist'" {
  14.202 +                const "ename", "substring-after(substring($type,1,string-length($type)-1), '<')";
  14.203 +                const "iename" choose {
  14.204 +                    when "$ctype = 'stringlist'" > byte[]
  14.205 +                    when "$ctype = 'stringpairlist'" > Pair<byte[],byte[]>
  14.206 +                    otherwise > _«$ename»
  14.207 +                }
  14.208 +                const "convget" choose {
  14.209 +                    when "$ctype = 'stringlist'" > Utils.toUTF16(i)
  14.210 +                    when "$ctype = 'stringpairlist'" > new Pair<String, String>(Utils.toUTF16(i.first), Utils.toUTF16(i.second))
  14.211 +                    otherwise > new «$ename»(i)
  14.212 +                }
  14.213 +                const "convset" choose {
  14.214 +                    when "$ctype = 'stringlist'" > Utils.toUTF8(i)
  14.215 +                    when "$ctype = 'stringpairlist'" > new Pair<byte[],byte[]>(Utils.toUTF8(i.first), Utils.toUTF8(i.second))
  14.216 +                    otherwise > new _«$ename»(i)
  14.217 +                }
  14.218 +                ||
  14.219 +                // Property type: list type. [java: «$type», intermediate: «$itype», ctype: «$ctype»]
  14.220 +                public «$type» get«$name»() {
  14.221 +                    «$itype» glist = _get«$name»();
  14.222 +                    if (glist != null) {
  14.223 +                        «$type» list = new «$type»();
  14.224 +                        for («$iename» i : glist) {
  14.225 +                            list.add(«$convget»);
  14.226 +                        }
  14.227 +                        return list;
  14.228 +                    }
  14.229 +                    return null;
  14.230 +                }
  14.231 +                private native «$itype» _get«$name»();
  14.232 +
  14.233 +                public void set«$name»(«$type» value) {
  14.234 +                    if (value != null) {
  14.235 +                        «$itype» list = new «$itype»();
  14.236 +                        for («$ename» i : value) {
  14.237 +                            list.add(«$convset»);
  14.238 +                        }
  14.239 +                        _set«$name»(list);
  14.240 +                    } else {
  14.241 +                        _set«$name»(null);
  14.242 +                    }
  14.243 +                }
  14.244 +                private native void _set«$name»(«$itype» value);
  14.245 +
  14.246 +
  14.247 +                ||
  14.248 +            }
  14.249 +            
  14.250 +            when "$itype != $type" {
  14.251 +                ||
  14.252 +                // Property type: differs from intermediate. [java: «$type», intermediate: «$itype», ctype: «$ctype»]
  14.253 +                public «$type» get«$name»() {
  14.254 +                    «$itype» res = _get«$name»();
  14.255 +                    if (res != null) {
  14.256 +                        return Utils.toUTF16(res);
  14.257 +                    } else {
  14.258 +                        return null;
  14.259 +                    }
  14.260 +                }
  14.261 +                private native «$itype» _get«$name»();
  14.262 +
  14.263 +                public void set«$name»(«$type» value) {
  14.264 +                    if (value != null) {
  14.265 +                        _set«$name»(Utils.toUTF8(value));
  14.266 +                    } else {
  14.267 +                        _set«$name»(new byte[0]);
  14.268 +                    }
  14.269 +                }
  14.270 +                private native void _set«$name»(«$itype» value);
  14.271 +
  14.272 +
  14.273 +                ||
  14.274 +            }
  14.275 +
  14.276 +            when "../enum[@name=$ctype]" {
  14.277 +                ||
  14.278 +                // Property type: enum type. [java: «$type», intermediate: «$itype», ctype: «$ctype»]
  14.279 +                public «$type» get«$name»() {
  14.280 +                    return «$type».getByInt(_get«$name»());
  14.281 +                }
  14.282 +                private native int _get«$name»();
  14.283 +                public void set«$name»(«$type» value) {
  14.284 +                    if(value != null)
  14.285 +                        _set«$name»(value.value);
  14.286 +                    else
  14.287 +                        _set«$name»(0);
  14.288 +                }
  14.289 +                private native void _set«$name»(int value);
  14.290 +
  14.291 +                ||
  14.292 +            }
  14.293 +
  14.294 +            otherwise {
  14.295 +                ||
  14.296 +                // Property type: no intermediate type [java: «$type», intermediate: «$itype», ctype: «$ctype»]
  14.297 +                public «$type» get«$name»() {
  14.298 +                    return get«$name»();
  14.299 +                }
  14.300 +                private native «$type» _get«$name»();
  14.301 +
  14.302 +                public void set«$name»(«$type» value) {
  14.303 +                    _set«$name»(value);
  14.304 +                }
  14.305 +                private native void _set«$name»(«$type» value);
  14.306 +
  14.307 +
  14.308 +                ||
  14.309 +            }
  14.310 +        }
  14.311 +    }
  14.312 +
  14.313 +    template "*", mode=interface {
  14.314 +        const "type" call "toJava" with "type", "name(.)";
  14.315 +        const "name" call "toJava" with "type", "name(*[position()=1])";
  14.316 +
  14.317 +        ||
  14.318 +        public «$type» get«$name»();
  14.319 +
  14.320 +        public void set«$name»(«$type» value);
  14.321 +
  14.322 +        ||
  14.323 +    }
  14.324 +
  14.325 +    template "*", mode=value {
  14.326 +        const "name" call "toJava" with "type", "name(.)";
  14.327 +        | «$name» («.»)`if "position()!=last()" > , `
  14.328 +    }
  14.329 +}
  14.330 +
    15.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.2 +++ b/src/codegen/gen_throw_pEp_exception.ysl2	Fri Aug 28 18:18:26 2020 +0200
    15.3 @@ -0,0 +1,65 @@
    15.4 +include yslt.yml2
    15.5 +
    15.6 +tstylesheet {
    15.7 +    include ./textutils.ysl2
    15.8 +
    15.9 +    template "/" {
   15.10 +        apply "namespace", 0;
   15.11 +        document "../cxx/throw_pEp_exception.hh", "text"
   15.12 +            ||
   15.13 +            #pragma once
   15.14 +
   15.15 +            #include <jni.h>
   15.16 +
   15.17 +            namespace pEp {
   15.18 +                namespace JNIAdapter {
   15.19 +                    jint throw_pEp_Exception(JNIEnv *env, PEP_STATUS status);
   15.20 +                };
   15.21 +            };
   15.22 +
   15.23 +            ||
   15.24 +        document("../../build/marker/gen_throw_pEp_exception.marker", "text") > ""
   15.25 +    }
   15.26 +
   15.27 +    template "namespace"
   15.28 +        document("../cxx/throw_pEp_exception.cc", "text")
   15.29 +            ||
   15.30 +            #include <assert.h>
   15.31 +            #include <pEp/pEpEngine.h>
   15.32 +            #include "throw_pEp_exception.hh"
   15.33 +
   15.34 +            namespace pEp {
   15.35 +                namespace JNIAdapter {
   15.36 +                    jint throw_pEp_Exception(JNIEnv *env, PEP_STATUS status)
   15.37 +                    {
   15.38 +                        jclass ex;
   15.39 +                        const char *ex_name;
   15.40 +
   15.41 +                        switch (status) {
   15.42 +                            `` apply "exception/*[text()!=0]", 4, mode=case
   15.43 +                            default:
   15.44 +                                assert(0);
   15.45 +                                ex_name = "Exception";
   15.46 +                        }
   15.47 +
   15.48 +                        ex = env->FindClass(ex_name);
   15.49 +                        assert(ex);
   15.50 +
   15.51 +                        if (ex == NULL) {
   15.52 +                            ex = env->FindClass("java/lang/NoClassDefFoundError");
   15.53 +                            assert(ex);
   15.54 +                        }
   15.55 +
   15.56 +                        return env->ThrowNew(ex, ex_name);
   15.57 +                    }
   15.58 +                };
   15.59 +            };
   15.60 +            ||
   15.61 +
   15.62 +    template "*", mode=case {
   15.63 +        | case `call "UCASE" with "text", "name(.)"`:
   15.64 +        |     ex_name = "foundation/pEp/jniadapter/exceptions/`call "CamelCase" with "text", "name(.)"`";
   15.65 +        |     break;
   15.66 +    }
   15.67 +}
   15.68 +
    16.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    16.2 +++ b/src/codegen/pEp.yml2	Fri Aug 28 18:18:26 2020 +0200
    16.3 @@ -0,0 +1,319 @@
    16.4 +decl namespace @name;
    16.5 +decl enum @name;
    16.6 +decl interface @name;
    16.7 +decl exception @name;
    16.8 +decl method @name;
    16.9 +decl struct @name;
   16.10 +decl basic @type @name;
   16.11 +
   16.12 +namespace pEp {
   16.13 +    exception Status {
   16.14 +       include ./status_list.yml2
   16.15 +    };
   16.16 +
   16.17 +    enum PassphraseType {
   16.18 +        include ./passphrase_status_list.yml2
   16.19 +    };
   16.20 +
   16.21 +    enum Color {
   16.22 +        PEP_color_no_color > 0
   16.23 +        PEP_color_yellow > 1
   16.24 +        PEP_color_green > 2
   16.25 +        PEP_color_red > -1
   16.26 +    };
   16.27 +
   16.28 +    enum Rating {
   16.29 +        pEp_rating_undefined > 0
   16.30 +        pEp_rating_cannot_decrypt > 1
   16.31 +        pEp_rating_have_no_key > 2
   16.32 +        pEp_rating_unencrypted > 3
   16.33 +        pEp_rating_unencrypted_for_some > 4
   16.34 +        pEp_rating_unreliable > 5
   16.35 +        pEp_rating_reliable > 6
   16.36 +        pEp_rating_trusted > 7
   16.37 +        pEp_rating_trusted_and_anonymized > 8
   16.38 +        pEp_rating_fully_anonymous > 9
   16.39 +
   16.40 +        pEp_rating_mistrust > -1
   16.41 +        pEp_rating_b0rken > -2
   16.42 +        pEp_rating_under_attack > -3
   16.43 +    };
   16.44 +
   16.45 +    enum DecryptFlags {
   16.46 +        pEp_decrypt_flag_own_private_key > 1
   16.47 +        pEp_decrypt_flag_consumed > 2
   16.48 +        pEp_decrypt_flag_ignored > 4
   16.49 +        pEp_decrypt_flag_src_modified > 8
   16.50 +        pEp_decrypt_flag_untrusted_server > 0x100
   16.51 +        pEp_decrypt_flag_dont_trigger_sync > 0x200
   16.52 +    };
   16.53 +
   16.54 +    enum IdentityFlags {
   16.55 +        pEp_idf_not_for_sync > 1
   16.56 +        pEp_idf_list > 2
   16.57 +        pEp_idf_devicegroup > 256
   16.58 +    };
   16.59 +
   16.60 +    enum SyncHandshakeResult {
   16.61 +        sync_handshake_cancel   > -1
   16.62 +        sync_handshake_accepted > 0
   16.63 +        sync_handshake_rejected > 1
   16.64 +    };
   16.65 +
   16.66 +    enum SyncHandshakeSignal {
   16.67 +        sync_notify_undefined > 0
   16.68 +        sync_notify_init_add_our_device > 1
   16.69 +        sync_notify_init_add_other_device > 2
   16.70 +        sync_notify_init_form_group > 3
   16.71 +        // sync_notify_init_move_our_device > 4
   16.72 +        sync_notify_timeout > 5
   16.73 +        sync_notify_accepted_device_added > 6
   16.74 +        sync_notify_accepted_group_created > 7
   16.75 +        sync_notify_accepted_device_accepted > 8
   16.76 +        // sync_notify_overtaken > 9
   16.77 +        // sync_notify_forming_group > 10
   16.78 +        sync_passphrase_required > 128
   16.79 +        sync_notify_sole > 254
   16.80 +        sync_notify_in_group > 255
   16.81 +    };
   16.82 +
   16.83 +    enum CipherSuite {
   16.84 +        pEp_cipher_suite_default > 0
   16.85 +        pEp_cipher_suite_cv25519 > 1
   16.86 +        pEp_cipher_suite_p256 > 2
   16.87 +        pEp_cipher_suite_p384 > 3
   16.88 +        pEp_cipher_suite_p521 > 4
   16.89 +        pEp_cipher_suite_rsa2k > 5
   16.90 +        pEp_cipher_suite_rsa3k > 6
   16.91 +        pEp_cipher_suite_rsa4k > 7
   16.92 +        pEp_cipher_suite_rsa8k > 8
   16.93 +    };
   16.94 +
   16.95 +    interface Engine {
   16.96 +        method cached=true encrypt_message(
   16.97 +                in message src,
   16.98 +                in stringlist extra,
   16.99 +                creates message dst,
  16.100 +                in EncFormat format,
  16.101 +                Cconst PEP_encrypt_flags flags "(PEP_encrypt_flags_t)0"
  16.102 +            );
  16.103 +
  16.104 +        method cached=true encrypt_message_and_add_priv_key(
  16.105 +                in message src,
  16.106 +                creates message dst,
  16.107 +                in string fpr,
  16.108 +                Cconst PEP_enc_format encformat "PEP_enc_PEP",
  16.109 +                Cconst PEP_encrypt_flags flags "(PEP_encrypt_flags_t)0"
  16.110 +            );
  16.111 +
  16.112 +        method cached=true encrypt_message_for_self(
  16.113 +                in identity target,
  16.114 +                in message src,
  16.115 +                in stringlist extra,
  16.116 +                creates message dst,
  16.117 +                Cconst PEP_enc_format encformat "PEP_enc_PEP",
  16.118 +                Cconst PEP_encrypt_flags flags "(PEP_encrypt_flags_t)0"
  16.119 +            );
  16.120 +
  16.121 +        method cached=true decrypt_message(
  16.122 +                inout message src,
  16.123 +                creates message dst,
  16.124 +                inout stringlist keylist,
  16.125 +                returns Rating rating,
  16.126 +                inout int flags
  16.127 +            );
  16.128 +
  16.129 +        method cached=true re_evaluate_message_rating(
  16.130 +                in message src,
  16.131 +                Cconst stringlist x_keylist "static_cast<stringlist_t*>(nullptr)",
  16.132 +                Cconst Rating x_enc_status "PEP_rating_undefined",
  16.133 +                returns Rating rating
  16.134 +            );
  16.135 +
  16.136 +        method cached=true outgoing_message_rating(
  16.137 +                in message msg,
  16.138 +                returns Rating rating
  16.139 +            );
  16.140 +
  16.141 +        method outgoing_message_rating_preview(
  16.142 +                in message msg,
  16.143 +                returns Rating rating
  16.144 +            );
  16.145 +
  16.146 +        method cached=true get_identity(
  16.147 +                in string address,
  16.148 +                in string userid,
  16.149 +                returns identity ident
  16.150 +            );
  16.151 +
  16.152 +        method cached=true identity_rating(
  16.153 +                in identity ident,
  16.154 +                returns Rating rating
  16.155 +            );
  16.156 +
  16.157 +        method cached=true blacklist_retrieve(
  16.158 +                returns stringlist blacklist
  16.159 +            );
  16.160 +
  16.161 +        // TODO: WTF is...
  16.162 +        method cached=true own_message_private_key_details(
  16.163 +                in message msg,
  16.164 +                returns identity ident 
  16.165 +            );
  16.166 +
  16.167 +        method cached=true OpenPGP_list_keyinfo(
  16.168 +                in string pattern,
  16.169 +                returns stringpairlist keyinfoList
  16.170 +            );
  16.171 +
  16.172 +        //TODO Move to use IdentityFlag instead of ints
  16.173 +        method cached=true set_identity_flags(
  16.174 +                in identity ident,
  16.175 +                in uint flags
  16.176 +            );
  16.177 +
  16.178 +        method cached=true unset_identity_flags(
  16.179 +                in identity ident,
  16.180 +                in uint flags
  16.181 +            );
  16.182 +
  16.183 +        method cached=true own_identities_retrieve(
  16.184 +                returns identitylist identities
  16.185 +            );
  16.186 +
  16.187 +
  16.188 +        method cached=true get_trustwords(
  16.189 +                in identity id1 const,
  16.190 +                in identity id2 const,
  16.191 +                in string lang,
  16.192 +                returns sstring words,
  16.193 +                in bool full
  16.194 +            );
  16.195 +
  16.196 +        method cached=true get_trustwords_for_fprs(
  16.197 +                in string fpr1,
  16.198 +                in string fpr2,
  16.199 +                in string lang,
  16.200 +                returns sstring words,
  16.201 +		in bool full
  16.202 +            );
  16.203 +
  16.204 +        method cached=true get_message_trustwords(
  16.205 +                in message msg,
  16.206 +                in stringlist keylist,
  16.207 +                in identity receivedby,
  16.208 +                in string lang,
  16.209 +                returns string words,
  16.210 +                in bool full
  16.211 +            );
  16.212 +
  16.213 +        method cached=true get_languagelist(
  16.214 +                returns string languagelist
  16.215 +            );
  16.216 +
  16.217 +        // this function is not related to key reset
  16.218 +
  16.219 +        method cached=true key_reset_trust(
  16.220 +                in identity ident
  16.221 +            );
  16.222 +
  16.223 +        method cached=true key_reset_identity(
  16.224 +                in identity ident,
  16.225 +                in string fpr
  16.226 +            );
  16.227 +
  16.228 +        method cached=true key_reset_user(
  16.229 +                in string userid,
  16.230 +                in string fpr
  16.231 +            );
  16.232 +
  16.233 +        method cached=true key_reset_all_own_keys();
  16.234 +                
  16.235 +        method cached=true deliverHandshakeResult(
  16.236 +                in SyncHandshakeResult shr,
  16.237 +                in identitylist identities const
  16.238 +            );
  16.239 +
  16.240 +        method cached=true leave_device_group();
  16.241 +
  16.242 +        method cached=true enable_identity_for_sync(
  16.243 +                in identity ident
  16.244 +            );
  16.245 +
  16.246 +	    method cached=true disable_identity_for_sync(
  16.247 +                in identity ident
  16.248 +            );
  16.249 +
  16.250 +        method config_cipher_suite(
  16.251 +                in CipherSuite suite
  16.252 +            );
  16.253 +
  16.254 +
  16.255 +        // "basic" methods are generated on the java side, but
  16.256 +        // on the C++ side, manually implemented in basic_api.cc
  16.257 +
  16.258 +        basic string trustwords(identity ident);
  16.259 +        basic identity myself(identity ident);
  16.260 +        basic identity updateIdentity(identity ident);
  16.261 +        basic identity setOwnKey(identity ident, string fpr);
  16.262 +        basic void keyMistrusted(identity ident);
  16.263 +        basic void keyResetTrust(identity ident);
  16.264 +        basic void trustPersonalKey(identity ident);
  16.265 +        basic void trustOwnKey(identity ident);
  16.266 +        basic identitylist importKey(bytearray key);
  16.267 +        basic void blacklist_add(string fpr);
  16.268 +        basic void blacklist_delete(string fpr);
  16.269 +        basic bool blacklist_is_listed(string fpr);
  16.270 +        basic void config_passive_mode(bool enable);
  16.271 +        basic void config_unencrypted_subject(bool enable);
  16.272 +        basic string getCrashdumpLog(int maxlines);
  16.273 +        basic string getUserDirectory();
  16.274 +        basic string getMachineDirectory();
  16.275 +        basic void config_passphrase(string passphrase);
  16.276 +        basic void config_passphrase_for_new_keys(bool enable, string passphrase);
  16.277 +    };
  16.278 +
  16.279 +    struct message {
  16.280 +        enum TextFormat {
  16.281 +            plain       > 0
  16.282 +            html        > 1
  16.283 +            other       > 255
  16.284 +        }
  16.285 +
  16.286 +        enum direction {
  16.287 +            incoming    > 0
  16.288 +            outgoing    > 1
  16.289 +        }
  16.290 +
  16.291 +        enum EncFormat {
  16.292 +            none               > 0
  16.293 +            inline             > 1
  16.294 +            SMIME              > 2
  16.295 +            PGPMIME            > 3
  16.296 +            PEP                > 4
  16.297 +            PEP_enc_inline_EA  > 6
  16.298 +        }
  16.299 +
  16.300 +         direction dir;
  16.301 +         string id;
  16.302 +         string shortmsg;
  16.303 +         string longmsg;
  16.304 +         string longmsg_formatted;
  16.305 +         bloblist attachments;
  16.306 +         timestamp sent;
  16.307 +         timestamp recv;
  16.308 +         identity from;
  16.309 +         identitylist to;
  16.310 +         identity recv_by;
  16.311 +         identitylist cc;
  16.312 +         identitylist bcc;
  16.313 +         identitylist reply_to;
  16.314 +         stringlist in_reply_to;
  16.315 +         stringlist references;
  16.316 +         stringlist keywords;
  16.317 +         string comments;
  16.318 +         stringpairlist opt_fields;
  16.319 +         EncFormat enc_format;
  16.320 +    };
  16.321 +};
  16.322 +
    17.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.2 +++ b/src/codegen/textutils.ysl2	Fri Aug 28 18:18:26 2020 +0200
    17.3 @@ -0,0 +1,29 @@
    17.4 +function "UCASE" {
    17.5 +    param "text";
    17.6 +
    17.7 +    value "translate($text, 'abcdefghijklmnopqrstuvwxyz-', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_')";
    17.8 +}
    17.9 +
   17.10 +function "lcase" {
   17.11 +    param "text";
   17.12 +
   17.13 +    value "translate($text, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ-', 'abcdefghijklmnopqrstuvwxyz_')";
   17.14 +}
   17.15 +
   17.16 +function "CamelCase" {
   17.17 +    param "text";
   17.18 +    const "tokens", "str:tokenize($text, '-')";
   17.19 +
   17.20 +    for "$tokens" {
   17.21 +        choose {
   17.22 +            when ".='pEp'" > pEp
   17.23 +            otherwise {
   17.24 +                call "UCASE" with "text", "substring(., 1, 1)";
   17.25 +                value "substring(., 2)";
   17.26 +            }
   17.27 +        }
   17.28 +    }
   17.29 +}
   17.30 +
   17.31 +define operator "©([$@]?[a-zA-Z0-9_]+)" as call "CamelCase" with "text", "%1";
   17.32 +
    18.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    18.2 +++ b/src/codegen/types_c.ysl2	Fri Aug 28 18:18:26 2020 +0200
    18.3 @@ -0,0 +1,41 @@
    18.4 +function "toC" {
    18.5 +    param "type";
    18.6 +
    18.7 +    choose {
    18.8 +        when "$type='stringlist'" > stringlist_t
    18.9 +        when "$type='string' or $type='sstring'" > char
   18.10 +        when "$type='identity'" > pEp_identity
   18.11 +        when "$type='identitylist'" > identity_list
   18.12 +        when "$type='timestamp'" > timestamp
   18.13 +        when "$type='EncFormat'" > PEP_enc_format
   18.14 +        when "$type='bloblist'" > bloblist_t
   18.15 +        when "$type='stringpairlist'" > stringpair_list_t
   18.16 +        when "$type='direction'" > PEP_msg_direction
   18.17 +        when "$type='bool'" > bool
   18.18 +        when "$type='DecryptFlags'" > PEP_decrypt_flags_t
   18.19 +        when "$type='Color'" > PEP_color
   18.20 +        when "$type='Rating'" > PEP_rating
   18.21 +        when "$type='SyncHandshakeResult'" > sync_handshake_result
   18.22 +        when "$type='CipherSuite'" > PEP_CIPHER_SUITE
   18.23 +        when "$type='uint'" > uint
   18.24 +
   18.25 +        otherwise value "$type";
   18.26 +    }
   18.27 +}
   18.28 +
   18.29 +function "jni_type" {
   18.30 +    param "type";
   18.31 +    param "dir";
   18.32 +
   18.33 +    choose {
   18.34 +        when "../enum[@name=$type]" > jint
   18.35 +        when "$type = 'string' or $type = 'sstring'" > jbyteArray
   18.36 +        when "$type='bool'" > jboolean
   18.37 +        when "$type='int'" > jint
   18.38 +        when "$type='uint'" > jint
   18.39 +        otherwise > jobject
   18.40 +    }
   18.41 +}
   18.42 +
   18.43 +define operator "√([$@]?[a-zA-Z0-9_]+)" as call "toC" with "type", "%1";
   18.44 +
    19.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    19.2 +++ b/src/codegen/types_java.ysl2	Fri Aug 28 18:18:26 2020 +0200
    19.3 @@ -0,0 +1,85 @@
    19.4 +function "toJava" {
    19.5 +    param "type";
    19.6 +
    19.7 +    choose {
    19.8 +        when "$type='bool'" > Boolean
    19.9 +        when "$type='int'" > int
   19.10 +        when "$type='uint'" > int
   19.11 +        when "$type='string' or $type='sstring'" > String
   19.12 +        when "$type='timestamp'" > Date
   19.13 +        when "$type='void'" > void
   19.14 +
   19.15 +        when "$type='identity'" > Identity
   19.16 +        when "$type='message'" > Message
   19.17 +
   19.18 +        when "$type='bloblist'" > Vector<Blob>
   19.19 +        when "$type='identitylist'" > Vector<Identity>
   19.20 +        when "$type='stringlist'" > Vector<String>
   19.21 +        when "$type='stringpairlist'" > ArrayList<Pair<String, String>>
   19.22 +        when "$type='bytearray'" > byte[]
   19.23 +
   19.24 +        when "$type='Color'" > Color
   19.25 +        when "$type='DecryptFlags'" > DecryptFlags
   19.26 +        when "$type='EncFormat'" > Message.EncFormat
   19.27 +        when "$type='Rating'" > Rating
   19.28 +
   19.29 +        otherwise call "CamelCase" with "text", "$type";
   19.30 +    }
   19.31 +}
   19.32 +
   19.33 +function "toSig" {
   19.34 +    param "type";
   19.35 +
   19.36 +    choose {
   19.37 +        when "$type='string'" error | # cannot declare "string"
   19.38 +        when "$type='bool'" > Z
   19.39 +        when "$type='bytearray'" > [b
   19.40 +        otherwise {
   19.41 +            > L
   19.42 +            choose {
   19.43 +                when "$type='timestamp'" > java/util/Date
   19.44 +                when "$type='int'" > java/lang/Integer
   19.45 +
   19.46 +                when "$type='identity'" > foundation/pEp/jniadapter/_Identity
   19.47 +                when "$type='message'" > foundation/pEp/jniadapter/Message
   19.48 +
   19.49 +                when "$type='bloblist'" > java/util/Vector
   19.50 +                when "$type='identitylist'" > java/util/Vector
   19.51 +                when "$type='stringlist'" > java/util/Vector
   19.52 +                when "$type='stringpairlist'" > java/util/ArrayList
   19.53 +
   19.54 +                when "$type='Color'" > foundation/pEp/jniadapter/Color
   19.55 +                when "$type='DecryptFlags'" > foundation/pEp/jniadapter/DecryptFlags
   19.56 +                when "$type='EncFormat'" > foundation/pEp/jniadapter/Message/EncFormat
   19.57 +                when "$type='Rating'" > foundation/pEp/jniadapter/Rating
   19.58 +
   19.59 +                otherwise error | # cannot declare "«$type»"
   19.60 +            }
   19.61 +            > ;
   19.62 +        }
   19.63 +    }
   19.64 +}
   19.65 +
   19.66 +function "toIntermediate" {
   19.67 +    param "type";
   19.68 +    param "dir";
   19.69 +
   19.70 +    choose {
   19.71 +        when "$type='bool'" > boolean
   19.72 +        when "$type='int'" > int
   19.73 +        when "$type='string' or $type='sstring'" > byte[]
   19.74 +        when "$type='bytearray' or $type='sstring'" > byte[]
   19.75 +
   19.76 +        when "$type='bloblist'" > Vector<_Blob>
   19.77 +        when "$type='identity'" > _Identity
   19.78 +        when "$type='identitylist'" > Vector<_Identity>
   19.79 +        when "$type='stringlist'" > Vector<byte[]>
   19.80 +        when "$type='stringpairlist'" > ArrayList<Pair<byte[], byte[]>>
   19.81 +
   19.82 +        otherwise call "toJava" with "type", "$type";
   19.83 +    }
   19.84 +}
   19.85 +
   19.86 +define operator "†([$@]?[a-zA-Z0-9_]+)" as call "toJava" with "type", "%1";
   19.87 +define operator "¡([$@]?[a-zA-Z0-9_]+)" as call "toIntermediate" with "type", "%1";
   19.88 +
    20.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    20.2 +++ b/src/cxx/basic_api.cc	Fri Aug 28 18:18:26 2020 +0200
    20.3 @@ -0,0 +1,563 @@
    20.4 +#include <pEp/keymanagement.h>
    20.5 +#include <pEp/blacklist.h>
    20.6 +#include <pEp/Adapter.hh>
    20.7 +#include <pEp/pEpLog.hh>
    20.8 +
    20.9 +#ifndef ANDROID
   20.10 +#include <string.h>
   20.11 +#endif
   20.12 +
   20.13 +#include "throw_pEp_exception.hh"
   20.14 +#include "jniutils.hh"
   20.15 +#include "passphrase_callback.hh"
   20.16 +
   20.17 +extern "C" {
   20.18 +    using namespace pEp::JNIAdapter;
   20.19 +    using pEp::Adapter::session;
   20.20 +    using pEp::passphrase_cache;
   20.21 +
   20.22 +JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_Engine__1trustwords(
   20.23 +        JNIEnv *env,
   20.24 +        jobject obj,
   20.25 +        jobject ident
   20.26 +    )
   20.27 +{
   20.28 +    std::mutex *mutex_local = nullptr;
   20.29 +    {
   20.30 +        std::lock_guard<std::mutex> l(global_mutex);
   20.31 +        pEpLog("called with lock_guard");
   20.32 +        mutex_local = get_engine_java_object_mutex(env, obj);
   20.33 +    }
   20.34 +    std::lock_guard<std::mutex> l(*mutex_local);
   20.35 +
   20.36 +    pEp_identity *_ident = to_identity(env, ident);
   20.37 +    char *words;
   20.38 +    size_t wsize;
   20.39 +
   20.40 +    PEP_STATUS status = PEP_STATUS_OK;
   20.41 +
   20.42 +    if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
   20.43 +        if (_ident->me)
   20.44 +            status = passphraseWrap(::myself, session(), _ident);
   20.45 +        else
   20.46 +            status = passphraseWrap(::update_identity, session(), _ident);
   20.47 +    }
   20.48 +
   20.49 +    if (status != PEP_STATUS_OK) {
   20.50 +        throw_pEp_Exception(env, status);
   20.51 +        return NULL;
   20.52 +    }
   20.53 +
   20.54 +    if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
   20.55 +        throw_pEp_Exception(env, PEP_CANNOT_FIND_IDENTITY);
   20.56 +        return NULL;
   20.57 +    }
   20.58 +
   20.59 +    const char *lang;
   20.60 +    if (_ident->lang[0])
   20.61 +        lang = _ident->lang;
   20.62 +    else
   20.63 +        lang = "en";
   20.64 +
   20.65 +    status = passphraseWrap(::trustwords,
   20.66 +            session(), (const char *) _ident->fpr, lang, &words, &wsize, 10);
   20.67 +
   20.68 +    if (status != PEP_STATUS_OK) {
   20.69 +        throw_pEp_Exception(env, status);
   20.70 +        return NULL;
   20.71 +    }
   20.72 +
   20.73 +    return from_string(env, words);
   20.74 +}
   20.75 +
   20.76 +JNIEXPORT jobject JNICALL Java_foundation_pEp_jniadapter_Engine__1myself(
   20.77 +        JNIEnv *env,
   20.78 +        jobject obj,
   20.79 +        jobject ident
   20.80 +    )
   20.81 +{
   20.82 +    std::mutex *mutex_local = nullptr;
   20.83 +    {
   20.84 +        std::lock_guard<std::mutex> l(global_mutex);
   20.85 +        pEpLog("called with lock_guard");
   20.86 +        mutex_local = get_engine_java_object_mutex(env, obj);
   20.87 +    }
   20.88 +    std::lock_guard<std::mutex> l(*mutex_local);
   20.89 +
   20.90 +    pEp_identity *_ident = to_identity(env, ident);
   20.91 +
   20.92 +    PEP_STATUS status = passphraseWrap(::myself, session(), _ident);
   20.93 +
   20.94 +    if (status != PEP_STATUS_OK) {
   20.95 +        LOGD("Failed Myself: 0x%04x\\n", status);
   20.96 +        throw_pEp_Exception(env, status);
   20.97 +        return NULL;
   20.98 +    }
   20.99 +    return from_identity(env, _ident);
  20.100 +}
  20.101 +
  20.102 +JNIEXPORT jobject JNICALL Java_foundation_pEp_jniadapter_Engine__1updateIdentity(
  20.103 +        JNIEnv *env,
  20.104 +        jobject obj,
  20.105 +        jobject ident
  20.106 +    )
  20.107 +{
  20.108 +    std::mutex *mutex_local = nullptr;
  20.109 +    {
  20.110 +        std::lock_guard<std::mutex> l(global_mutex);
  20.111 +        pEpLog("called with lock_guard");
  20.112 +        mutex_local = get_engine_java_object_mutex(env, obj);
  20.113 +    }
  20.114 +    std::lock_guard<std::mutex> l(*mutex_local);
  20.115 +
  20.116 +    pEp_identity *_ident = to_identity(env, ident);
  20.117 +
  20.118 +    passphraseWrap(::update_identity, session(), _ident);
  20.119 +
  20.120 +    return from_identity(env, _ident);
  20.121 +}
  20.122 +
  20.123 +JNIEXPORT jobject JNICALL Java_foundation_pEp_jniadapter_Engine__1setOwnKey(
  20.124 +        JNIEnv *env,
  20.125 +        jobject obj,
  20.126 +        jobject ident,
  20.127 +        jbyteArray fpr
  20.128 +    )
  20.129 +{
  20.130 +    std::mutex *mutex_local = nullptr;
  20.131 +    {
  20.132 +        std::lock_guard<std::mutex> l(global_mutex);
  20.133 +        pEpLog("called with lock_guard");
  20.134 +        mutex_local = get_engine_java_object_mutex(env, obj);
  20.135 +    }
  20.136 +    std::lock_guard<std::mutex> l(*mutex_local);
  20.137 +
  20.138 +    pEp_identity *_ident = to_identity(env, ident);
  20.139 +    const char *_fpr = to_string(env, fpr);
  20.140 +
  20.141 +    PEP_STATUS status = passphraseWrap(::set_own_key, session(), _ident, _fpr);
  20.142 +
  20.143 +    if (status != PEP_STATUS_OK) {
  20.144 +        LOGD("Failed setOwnKey: 0x%04x\\n", status);
  20.145 +        throw_pEp_Exception(env, status);
  20.146 +        return NULL;
  20.147 +    }
  20.148 +
  20.149 +    return from_identity(env, _ident);
  20.150 +}
  20.151 +
  20.152 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1keyMistrusted(
  20.153 +        JNIEnv *env,
  20.154 +        jobject obj,
  20.155 +        jobject ident
  20.156 +    )
  20.157 +{
  20.158 +    std::mutex *mutex_local = nullptr;
  20.159 +    {
  20.160 +        std::lock_guard<std::mutex> l(global_mutex);
  20.161 +        pEpLog("called with lock_guard");
  20.162 +        mutex_local = get_engine_java_object_mutex(env, obj);
  20.163 +    }
  20.164 +    std::lock_guard<std::mutex> l(*mutex_local);
  20.165 +
  20.166 +    pEp_identity *_ident = to_identity(env, ident);
  20.167 +
  20.168 +    PEP_STATUS status = PEP_STATUS_OK;
  20.169 +
  20.170 +    if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
  20.171 +        if (_ident->me)
  20.172 +            status = passphraseWrap(::myself, session(), _ident);
  20.173 +        else
  20.174 +            status = passphraseWrap(::update_identity, session(), _ident);
  20.175 +    }
  20.176 +
  20.177 +    if (status != PEP_STATUS_OK) {
  20.178 +        throw_pEp_Exception(env, status);
  20.179 +        return;
  20.180 +    }
  20.181 +
  20.182 +    if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
  20.183 +        throw_pEp_Exception(env, PEP_CANNOT_FIND_IDENTITY);
  20.184 +        return;
  20.185 +    }
  20.186 +
  20.187 +    passphraseWrap(::key_mistrusted, session(), _ident);
  20.188 +}
  20.189 +
  20.190 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1keyResetTrust(
  20.191 +        JNIEnv *env,
  20.192 +        jobject obj,
  20.193 +        jobject ident
  20.194 +    )
  20.195 +{
  20.196 +    std::mutex *mutex_local = nullptr;
  20.197 +    {
  20.198 +        std::lock_guard<std::mutex> l(global_mutex);
  20.199 +        pEpLog("called with lock_guard");
  20.200 +        mutex_local = get_engine_java_object_mutex(env, obj);
  20.201 +    }
  20.202 +    std::lock_guard<std::mutex> l(*mutex_local);
  20.203 +
  20.204 +    pEp_identity *_ident = to_identity(env, ident);
  20.205 +
  20.206 +    PEP_STATUS status = PEP_STATUS_OK;
  20.207 +
  20.208 +    if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
  20.209 +        if (_ident->me)
  20.210 +            status = passphraseWrap(::myself, session(), _ident);
  20.211 +        else
  20.212 +            status = passphraseWrap(::update_identity, session(), _ident);
  20.213 +    }
  20.214 +
  20.215 +    if (status != PEP_STATUS_OK) {
  20.216 +        throw_pEp_Exception(env, status);
  20.217 +        return;
  20.218 +    }
  20.219 +
  20.220 +    if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
  20.221 +        throw_pEp_Exception(env, PEP_CANNOT_FIND_IDENTITY);
  20.222 +        return;
  20.223 +    }
  20.224 +
  20.225 +    passphraseWrap(::key_reset_trust, session(), _ident);
  20.226 +}
  20.227 +
  20.228 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1trustPersonalKey(
  20.229 +        JNIEnv *env,
  20.230 +        jobject obj,
  20.231 +        jobject ident
  20.232 +    )
  20.233 +{
  20.234 +    std::mutex *mutex_local = nullptr;
  20.235 +    {
  20.236 +        std::lock_guard<std::mutex> l(global_mutex);
  20.237 +        pEpLog("called with lock_guard");
  20.238 +        mutex_local = get_engine_java_object_mutex(env, obj);
  20.239 +    }
  20.240 +    std::lock_guard<std::mutex> l(*mutex_local);
  20.241 +
  20.242 +    pEp_identity *_ident = to_identity(env, ident);
  20.243 +
  20.244 +    PEP_STATUS status = PEP_STATUS_OK;
  20.245 +
  20.246 +    if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
  20.247 +        if (_ident->me)
  20.248 +            status = passphraseWrap(::myself, session(), _ident);
  20.249 +        else
  20.250 +            status = passphraseWrap(::update_identity, session(), _ident);
  20.251 +    }
  20.252 +
  20.253 +    if (status != PEP_STATUS_OK) {
  20.254 +        throw_pEp_Exception(env, status);
  20.255 +        return;
  20.256 +    }
  20.257 +
  20.258 +    if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
  20.259 +        throw_pEp_Exception(env, PEP_CANNOT_FIND_IDENTITY);
  20.260 +        return;
  20.261 +    }
  20.262 +
  20.263 +    passphraseWrap(::trust_personal_key, session(), _ident);
  20.264 +}
  20.265 +
  20.266 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1trustOwnKey(
  20.267 +        JNIEnv *env,
  20.268 +        jobject obj,
  20.269 +        jobject ident
  20.270 +    )
  20.271 +{
  20.272 +    std::mutex *mutex_local = nullptr;
  20.273 +    {
  20.274 +        std::lock_guard<std::mutex> l(global_mutex);
  20.275 +        pEpLog("called with lock_guard");
  20.276 +        mutex_local = get_engine_java_object_mutex(env, obj);
  20.277 +    }
  20.278 +    std::lock_guard<std::mutex> l(*mutex_local);
  20.279 +
  20.280 +    pEp_identity *_ident = to_identity(env, ident);
  20.281 +
  20.282 +    if (_ident->fpr == NULL || _ident->fpr[0] == 0) {
  20.283 +        throw_pEp_Exception(env, PEP_CANNOT_FIND_IDENTITY);
  20.284 +        return;
  20.285 +    }
  20.286 +
  20.287 +    passphraseWrap(::trust_own_key, session(), _ident);
  20.288 +}
  20.289 +
  20.290 +JNIEXPORT jobject JNICALL Java_foundation_pEp_jniadapter_Engine__1importKey(
  20.291 +        JNIEnv *env,
  20.292 +        jobject obj,
  20.293 +        jbyteArray key
  20.294 +    )
  20.295 +{
  20.296 +    std::mutex *mutex_local = nullptr;
  20.297 +    {
  20.298 +        std::lock_guard<std::mutex> l(global_mutex);
  20.299 +        pEpLog("called with lock_guard");
  20.300 +        mutex_local = get_engine_java_object_mutex(env, obj);
  20.301 +    }
  20.302 +    std::lock_guard<std::mutex> l(*mutex_local);
  20.303 +
  20.304 +    size_t _size = (size_t) env->GetArrayLength(key);
  20.305 +    const char *_key = (char *) env->GetByteArrayElements(key, NULL);
  20.306 +
  20.307 +    if(_key == NULL){
  20.308 +        throw_pEp_Exception(env, PEP_OUT_OF_MEMORY);
  20.309 +        return NULL;
  20.310 +    }
  20.311 +
  20.312 +    identity_list *_identities;
  20.313 +
  20.314 +    PEP_STATUS status =  passphraseWrap(::import_key, session(), _key, _size, &_identities);
  20.315 +    if (status != PEP_STATUS_OK && status != PEP_KEY_IMPORTED) {
  20.316 +        throw_pEp_Exception(env, status);
  20.317 +        return NULL;
  20.318 +    }
  20.319 +
  20.320 +    jobject identities_ = NULL;
  20.321 +    if (_identities) {
  20.322 +        identities_ = from_identitylist(env, _identities);
  20.323 +    }
  20.324 +
  20.325 +    env->ReleaseByteArrayElements(key, (jbyte *) _key, JNI_ABORT);
  20.326 +    return identities_;
  20.327 +}
  20.328 +
  20.329 +
  20.330 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1config_1passive_1mode(
  20.331 +        JNIEnv *env,
  20.332 +        jobject obj,
  20.333 +        jboolean enable
  20.334 +    )
  20.335 +{
  20.336 +    std::mutex *mutex_local = nullptr;
  20.337 +    {
  20.338 +        std::lock_guard<std::mutex> l(global_mutex);
  20.339 +        pEpLog("called with lock_guard");
  20.340 +        mutex_local = get_engine_java_object_mutex(env, obj);
  20.341 +    }
  20.342 +    std::lock_guard<std::mutex> l(*mutex_local);
  20.343 +
  20.344 +    ::config_passive_mode(session(), (bool)enable);
  20.345 +}
  20.346 +
  20.347 +
  20.348 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1config_1unencrypted_1subject(
  20.349 +        JNIEnv *env,
  20.350 +        jobject obj,
  20.351 +        jboolean enable
  20.352 +    )
  20.353 +{
  20.354 +    std::mutex *mutex_local = nullptr;
  20.355 +    {
  20.356 +        std::lock_guard<std::mutex> l(global_mutex);
  20.357 +        pEpLog("called with lock_guard");
  20.358 +        mutex_local = get_engine_java_object_mutex(env, obj);
  20.359 +    }
  20.360 +    std::lock_guard<std::mutex> l(*mutex_local);
  20.361 +
  20.362 +    ::config_unencrypted_subject(session(), (bool)enable);
  20.363 +}
  20.364 +
  20.365 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1blacklist_1add(
  20.366 +        JNIEnv *env,
  20.367 +        jobject obj,
  20.368 +        jbyteArray fpr
  20.369 +    )
  20.370 +{
  20.371 +    std::mutex *mutex_local = nullptr;
  20.372 +    {
  20.373 +        std::lock_guard<std::mutex> l(global_mutex);
  20.374 +        pEpLog("called with lock_guard");
  20.375 +        mutex_local = get_engine_java_object_mutex(env, obj);
  20.376 +    }
  20.377 +    std::lock_guard<std::mutex> l(*mutex_local);
  20.378 +
  20.379 +    const char *_fpr = to_string(env, fpr);
  20.380 +
  20.381 +    if(_fpr == NULL){
  20.382 +        throw_pEp_Exception(env, PEP_OUT_OF_MEMORY);
  20.383 +        return;
  20.384 +    }
  20.385 +
  20.386 +    PEP_STATUS status = passphraseWrap(::blacklist_add, session(), _fpr);
  20.387 +    if (status != PEP_STATUS_OK) {
  20.388 +        throw_pEp_Exception(env, status);
  20.389 +        return;
  20.390 +    }
  20.391 +
  20.392 +}
  20.393 +
  20.394 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1blacklist_1delete(
  20.395 +        JNIEnv *env,
  20.396 +        jobject obj,
  20.397 +        jbyteArray fpr
  20.398 +    )
  20.399 +{
  20.400 +    std::mutex *mutex_local = nullptr;
  20.401 +    {
  20.402 +        std::lock_guard<std::mutex> l(global_mutex);
  20.403 +        pEpLog("called with lock_guard");
  20.404 +        mutex_local = get_engine_java_object_mutex(env, obj);
  20.405 +    }
  20.406 +    std::lock_guard<std::mutex> l(*mutex_local);
  20.407 +
  20.408 +    const char *_fpr = to_string(env, fpr);
  20.409 +
  20.410 +    if(_fpr == NULL){
  20.411 +        throw_pEp_Exception(env, PEP_OUT_OF_MEMORY);
  20.412 +        return;
  20.413 +    }
  20.414 +
  20.415 +    PEP_STATUS status = passphraseWrap(::blacklist_delete, session(), _fpr);
  20.416 +    if (status != PEP_STATUS_OK) {
  20.417 +        throw_pEp_Exception(env, status);
  20.418 +        return;
  20.419 +    }
  20.420 +
  20.421 +}
  20.422 +
  20.423 +JNIEXPORT jboolean JNICALL Java_foundation_pEp_jniadapter_Engine__1blacklist_1is_1listed(
  20.424 +        JNIEnv *env,
  20.425 +        jobject obj,
  20.426 +        jbyteArray fpr
  20.427 +    )
  20.428 +{
  20.429 +    std::mutex *mutex_local = nullptr;
  20.430 +    {
  20.431 +        std::lock_guard<std::mutex> l(global_mutex);
  20.432 +        pEpLog("called with lock_guard");
  20.433 +        mutex_local = get_engine_java_object_mutex(env, obj);
  20.434 +    }
  20.435 +    std::lock_guard<std::mutex> l(*mutex_local);
  20.436 +
  20.437 +    const char *_fpr = to_string(env, fpr);
  20.438 +    bool _listed = 0;
  20.439 +
  20.440 +    if(_fpr == NULL){
  20.441 +        throw_pEp_Exception(env, PEP_OUT_OF_MEMORY);
  20.442 +        return 0;
  20.443 +    }
  20.444 +
  20.445 +    PEP_STATUS status = passphraseWrap(::blacklist_is_listed, session(), _fpr, &_listed);
  20.446 +    if (status != PEP_STATUS_OK) {
  20.447 +        throw_pEp_Exception(env, status);
  20.448 +        return 0;
  20.449 +    }
  20.450 +
  20.451 +    return (jboolean)_listed;
  20.452 +}
  20.453 +
  20.454 +JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_Engine__1getCrashdumpLog(
  20.455 +        JNIEnv *env,
  20.456 +        jobject obj,
  20.457 +        jint dummy,
  20.458 +        jint maxlines
  20.459 +    )
  20.460 +{
  20.461 +    std::mutex *mutex_local = nullptr;
  20.462 +    {
  20.463 +        std::lock_guard<std::mutex> l(global_mutex);
  20.464 +        pEpLog("called with lock_guard");
  20.465 +        mutex_local = get_engine_java_object_mutex(env, obj);
  20.466 +    }
  20.467 +    std::lock_guard<std::mutex> l(*mutex_local);
  20.468 +
  20.469 +    int _maxlines = (int) maxlines;
  20.470 +    char *_logdata;
  20.471 +
  20.472 +    PEP_STATUS status = passphraseWrap(::get_crashdump_log, session(), _maxlines, &_logdata);
  20.473 +    if ((status > PEP_STATUS_OK && status < PEP_UNENCRYPTED) ||
  20.474 +            status < PEP_STATUS_OK ||
  20.475 +            status >= PEP_TRUSTWORD_NOT_FOUND) {
  20.476 +        throw_pEp_Exception(env, status);
  20.477 +        return NULL;
  20.478 +    }
  20.479 +
  20.480 +    return from_string(env, _logdata);
  20.481 +}
  20.482 +
  20.483 +JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_Engine__1getUserDirectory(
  20.484 +    JNIEnv *env,
  20.485 +    jobject obj
  20.486 +    )
  20.487 +{
  20.488 +    pEpLog("called");
  20.489 +    return from_string(env, ::per_user_directory());
  20.490 +}
  20.491 +
  20.492 +JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_Engine__1getMachineDirectory(
  20.493 +    JNIEnv *env,
  20.494 +    jobject obj
  20.495 +    )
  20.496 +{
  20.497 +    pEpLog("called");
  20.498 +    return from_string(env, ::per_machine_directory());
  20.499 +}
  20.500 +
  20.501 +//void logPassphraseCache() {
  20.502 +//    try {
  20.503 +////        while(true) {
  20.504 +//            pEpLog("Cache: '" << cache.latest_passphrase() << "'");
  20.505 +////        }
  20.506 +//    } catch(pEp::PassphraseCache::Empty e) {
  20.507 +//        pEpLog(e.what());
  20.508 +//    } catch(pEp::PassphraseCache::Exhausted ex) {
  20.509 +//        pEpLog(ex.what());
  20.510 +//    }
  20.511 +//}
  20.512 +
  20.513 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1config_1passphrase
  20.514 +  (JNIEnv * env,
  20.515 +   jobject obj,
  20.516 +   jbyteArray passphrase)
  20.517 +{
  20.518 +    std::mutex *mutex_local = nullptr;
  20.519 +    {
  20.520 +        std::lock_guard<std::mutex> l(global_mutex);
  20.521 +        pEpLog("called with lock_guard");
  20.522 +        mutex_local = get_engine_java_object_mutex(env, obj);
  20.523 +    }
  20.524 +    std::lock_guard<std::mutex> l(*mutex_local);
  20.525 +
  20.526 +    char* _passphrase = to_string(env, passphrase);
  20.527 +
  20.528 +    PEP_STATUS status = ::config_passphrase(session(),passphrase_cache.add(_passphrase));
  20.529 +    if (status != 0) {
  20.530 +        throw_pEp_Exception(env, status);
  20.531 +        return;
  20.532 +    }
  20.533 +}
  20.534 +
  20.535 +
  20.536 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_Engine__1config_1passphrase_1for_1new_1keys(
  20.537 +        JNIEnv *env,
  20.538 +        jobject obj,
  20.539 +        jboolean enable,
  20.540 +        jbyteArray passphrase
  20.541 +    )
  20.542 +{
  20.543 +    std::mutex *mutex_local = nullptr;
  20.544 +    {
  20.545 +        std::lock_guard<std::mutex> l(global_mutex);
  20.546 +        pEpLog("called with lock_guard");
  20.547 +        mutex_local = get_engine_java_object_mutex(env, obj);
  20.548 +    }
  20.549 +    std::lock_guard<std::mutex> l(*mutex_local);
  20.550 +
  20.551 +    bool _enable = (bool) enable;
  20.552 +    const char *_passphrase = to_string(env, passphrase);
  20.553 +
  20.554 +    PEP_STATUS status = ::config_passphrase_for_new_keys(session(),_enable,passphrase_cache.add_stored(_passphrase));
  20.555 +    if ((status > PEP_STATUS_OK && status < PEP_UNENCRYPTED) ||
  20.556 +            status < PEP_STATUS_OK ||
  20.557 +            status >= PEP_TRUSTWORD_NOT_FOUND) {
  20.558 +        throw_pEp_Exception(env, status);
  20.559 +        return ;
  20.560 +    }
  20.561 +
  20.562 +
  20.563 +}
  20.564 +
  20.565 +} // extern "C"
  20.566 +
    21.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    21.2 +++ b/src/cxx/foundation_pEp_jniadapter_AbstractEngine.cc	Fri Aug 28 18:18:26 2020 +0200
    21.3 @@ -0,0 +1,478 @@
    21.4 +#include "foundation_pEp_jniadapter_AbstractEngine.h"
    21.5 +#include <unistd.h>
    21.6 +#include <pEp/keymanagement.h>
    21.7 +#include <pEp/message_api.h>
    21.8 +#include <pEp/sync_api.h>
    21.9 +#include <pEp/pEpLog.hh>
   21.10 +#include <pEp/passphrase_cache.hh>
   21.11 +#include <pEp/callback_dispatcher.hh>
   21.12 +#include "throw_pEp_exception.hh"
   21.13 +#include "jniutils.hh"
   21.14 +#include "passphrase_callback.hh"
   21.15 +
   21.16 +namespace pEp {
   21.17 +using namespace pEp::JNIAdapter;
   21.18 +using namespace utility;   // for libpEpAdapter locked queue impl.  TODO:rename
   21.19 +
   21.20 +bool first = true;
   21.21 +
   21.22 +JavaVM *jvm= nullptr;
   21.23 +
   21.24 +std::mutex mutex_obj;
   21.25 +
   21.26 +jfieldID signal_field_value = nullptr;
   21.27 +jfieldID passphrase_type_field_value = nullptr;
   21.28 +jmethodID messageConstructorMethodID = nullptr;
   21.29 +jmethodID messageToSendMethodID = nullptr;
   21.30 +jmethodID notifyHandShakeMethodID = nullptr;
   21.31 +jmethodID needsFastPollMethodID = nullptr;
   21.32 +jmethodID passphraseRequiredMethodID = nullptr;
   21.33 +jmethodID sync_handshake_signal_values = nullptr;
   21.34 +jmethodID passphrase_status_values = nullptr;
   21.35 +jmethodID passphrase_callback_values = nullptr;
   21.36 +
   21.37 +jobject objj = nullptr;
   21.38 +
   21.39 +jclass messageClass = nullptr;
   21.40 +jclass identityClass = nullptr;
   21.41 +jclass signalClass = nullptr;
   21.42 +jclass engineClass = nullptr;
   21.43 +jclass passphraseTypeClass = nullptr;
   21.44 +
   21.45 +namespace JNISync {
   21.46 +    JNIEnv* env() {
   21.47 +        JNIEnv *thread_env = nullptr;
   21.48 +        int status = jvm->GetEnv((void**)&thread_env, JNI_VERSION_1_6);
   21.49 +        if (status < 0) {
   21.50 +#ifdef ANDROID
   21.51 +            status = jvm->AttachCurrentThread(&thread_env, nullptr);
   21.52 +#else
   21.53 +            status = jvm->AttachCurrentThread((void **) &thread_env, nullptr);
   21.54 +#endif
   21.55 +        }
   21.56 +        assert(status >= 0);
   21.57 +        return thread_env;
   21.58 +    }
   21.59 +
   21.60 +    void onSyncStartup() {
   21.61 +        pEpLog("called");
   21.62 +        env();
   21.63 +    }
   21.64 +
   21.65 +    void onSyncShutdown() {
   21.66 +        pEpLog("called");
   21.67 +        jvm->DetachCurrentThread();
   21.68 +    }
   21.69 +};
   21.70 +
   21.71 +
   21.72 +void jni_init() {
   21.73 +    JNIEnv *_env = JNISync::env();
   21.74 +
   21.75 +    messageClass = reinterpret_cast<jclass>(
   21.76 +            _env->NewGlobalRef(findClass(_env, "foundation/pEp/jniadapter/Message")));
   21.77 +    identityClass = reinterpret_cast<jclass>(
   21.78 +        _env->NewGlobalRef(findClass(_env, "foundation/pEp/jniadapter/_Identity")));
   21.79 +    signalClass = reinterpret_cast<jclass>(
   21.80 +            _env->NewGlobalRef(findClass(_env, "foundation/pEp/jniadapter/SyncHandshakeSignal")));
   21.81 +    passphraseTypeClass = reinterpret_cast<jclass>(
   21.82 +            _env->NewGlobalRef(findClass(_env, "foundation/pEp/jniadapter/PassphraseType")));
   21.83 +    engineClass = reinterpret_cast<jclass>(_env->NewGlobalRef(findClass(_env, "foundation/pEp/jniadapter/Engine")));
   21.84 +
   21.85 +    messageConstructorMethodID = _env->GetMethodID(messageClass, "<init>", "(J)V");
   21.86 +    messageToSendMethodID = _env->GetMethodID(
   21.87 +        engineClass,
   21.88 +        "messageToSendCallFromC",
   21.89 +        "(Lfoundation/pEp/jniadapter/Message;)I");
   21.90 +    needsFastPollMethodID = _env->GetMethodID(
   21.91 +        engineClass,
   21.92 +        "needsFastPollCallFromC",
   21.93 +        "(Z)I");
   21.94 +    notifyHandShakeMethodID = _env->GetMethodID(
   21.95 +        engineClass,
   21.96 +        "notifyHandshakeCallFromC",
   21.97 +        "(Lfoundation/pEp/jniadapter/_Identity;Lfoundation/pEp/jniadapter/_Identity;Lfoundation/pEp/jniadapter/SyncHandshakeSignal;)I");
   21.98 +    passphraseRequiredMethodID = _env->GetMethodID(
   21.99 +        engineClass,
  21.100 +        "passphraseRequiredFromC",
  21.101 +        "(Lfoundation/pEp/jniadapter/PassphraseType;)[B");
  21.102 +
  21.103 +    sync_handshake_signal_values = JNISync::env()->GetStaticMethodID(signalClass, "values",
  21.104 +                "()[Lfoundation/pEp/jniadapter/SyncHandshakeSignal;");
  21.105 +    passphrase_status_values = JNISync::env()->GetStaticMethodID(passphraseTypeClass, "values",
  21.106 +                "()[Lfoundation/pEp/jniadapter/PassphraseType;");
  21.107 +    signal_field_value = JNISync::env()->GetFieldID(signalClass, "value", "I");
  21.108 +    passphrase_type_field_value = JNISync::env()->GetFieldID(passphraseTypeClass, "value", "I");
  21.109 +}
  21.110 +
  21.111 +char* JNIAdapter::passphraseRequiredCallback(const PEP_STATUS status) {
  21.112 +    pEpLog("called");
  21.113 +    jobject status_ = nullptr;
  21.114 +    {
  21.115 +        assert(passphraseTypeClass);
  21.116 +        assert(passphrase_status_values);
  21.117 +        assert(passphrase_type_field_value);
  21.118 +
  21.119 +        jobjectArray values = (jobjectArray) JNISync::env()->CallStaticObjectMethod(passphraseTypeClass,
  21.120 +                passphrase_status_values);
  21.121 +
  21.122 +        if (JNISync::env()->ExceptionCheck()) {
  21.123 +            JNISync::env()->ExceptionClear();
  21.124 +            throw_pEp_Exception(JNISync::env(), PEP_UNKNOWN_ERROR);
  21.125 +        }
  21.126 +
  21.127 +        jsize values_size = JNISync::env()->GetArrayLength(values);
  21.128 +        for (jsize i = 0; i < values_size; i++) {
  21.129 +            jobject element = JNISync::env()->GetObjectArrayElement(values, i);
  21.130 +            assert(element);
  21.131 +            jint value = JNISync::env()->GetIntField(element, passphrase_type_field_value);
  21.132 +            if (value == (jint) status) {
  21.133 +                status_ = element;
  21.134 +                break;
  21.135 +            }
  21.136 +            JNISync::env()->DeleteLocalRef(element);
  21.137 +        }
  21.138 +    }
  21.139 +    assert(objj && passphraseRequiredMethodID);
  21.140 +
  21.141 +    jobject ppJO = JNISync::env()->CallObjectMethod(objj, passphraseRequiredMethodID, status_);
  21.142 +    if (JNISync::env()->ExceptionCheck()) {
  21.143 +        JNISync::env()->ExceptionDescribe();
  21.144 +        JNISync::env()->ExceptionClear();
  21.145 +    }
  21.146 +
  21.147 +    jbyteArray ppJBA = reinterpret_cast<jbyteArray>(ppJO);
  21.148 +    char* passphrase_ = to_string( JNISync::env(), ppJBA);
  21.149 +
  21.150 +    return passphrase_;
  21.151 +}
  21.152 +
  21.153 +PEP_STATUS messageToSend(message *msg)
  21.154 +{
  21.155 +    std::lock_guard<std::mutex> l(mutex_obj);
  21.156 +    pEpLog("called");
  21.157 +
  21.158 +    // Passphrase
  21.159 +    // When a protocol implementation of the p≡p engine using messageToSend() cannot sign or encrypt with an
  21.160 +    // empty passphrase and not with the configured passphrase it is calling messageToSend() with a NULL instead
  21.161 +    // of a struct _message object.
  21.162 +    if (Adapter::on_sync_thread() && !msg)
  21.163 +        return pEp::PassphraseCache::config_next_passphrase();
  21.164 +
  21.165 +    // reset passphrase iterator
  21.166 +    if (Adapter::on_sync_thread())
  21.167 +        pEp::PassphraseCache::config_next_passphrase(true);
  21.168 +
  21.169 +    jobject msg_ = nullptr;
  21.170 +    assert(messageClass && messageConstructorMethodID && objj && messageToSendMethodID);
  21.171 +
  21.172 +    msg_ = JNISync::env()->NewObject(messageClass, messageConstructorMethodID, (jlong) msg);
  21.173 +
  21.174 +    PEP_STATUS status = (PEP_STATUS) JNISync::env()->CallIntMethod(objj, messageToSendMethodID, msg_);
  21.175 +    if (JNISync::env()->ExceptionCheck()) {
  21.176 +        JNISync::env()->ExceptionDescribe();
  21.177 +        status = PEP_UNKNOWN_ERROR;
  21.178 +        JNISync::env()->ExceptionClear();
  21.179 +    }
  21.180 +
  21.181 +
  21.182 +    return status;
  21.183 +}
  21.184 +
  21.185 +PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal)
  21.186 +{
  21.187 +    std::lock_guard<std::mutex> l(mutex_obj);
  21.188 +    pEpLog("called");
  21.189 +
  21.190 +    jobject me_ = nullptr;
  21.191 +    jobject partner_ = nullptr;
  21.192 +
  21.193 +    me_ = from_identity(JNISync::env(), me, identityClass);
  21.194 +    partner_ = from_identity(JNISync::env(), partner, identityClass);
  21.195 +
  21.196 +    jobject signal_ = nullptr;
  21.197 +    {
  21.198 +        assert(signalClass);
  21.199 +        assert(sync_handshake_signal_values);
  21.200 +        assert(signal_field_value);
  21.201 +
  21.202 +        jobjectArray values = (jobjectArray) JNISync::env()->CallStaticObjectMethod(signalClass,
  21.203 +                sync_handshake_signal_values);
  21.204 +        if (JNISync::env()->ExceptionCheck()) {
  21.205 +            JNISync::env()->ExceptionClear();
  21.206 +            return PEP_UNKNOWN_ERROR;
  21.207 +        }
  21.208 +
  21.209 +        jsize values_size = JNISync::env()->GetArrayLength(values);
  21.210 +        for (jsize i = 0; i < values_size; i++) {
  21.211 +            jobject element = JNISync::env()->GetObjectArrayElement(values, i);
  21.212 +            assert(element);
  21.213 +            jint value = JNISync::env()->GetIntField(element, signal_field_value);
  21.214 +            if (value == (jint) signal) {
  21.215 +                signal_ = element;
  21.216 +                break;
  21.217 +            }
  21.218 +            JNISync::env() -> DeleteLocalRef(element);
  21.219 +        }
  21.220 +    }
  21.221 +
  21.222 +    assert(objj && notifyHandShakeMethodID);
  21.223 +
  21.224 +    PEP_STATUS status = (PEP_STATUS) JNISync::env()->CallIntMethod(objj, notifyHandShakeMethodID, me_, partner_, signal_);
  21.225 +    if (JNISync::env()->ExceptionCheck()) {
  21.226 +        JNISync::env()->ExceptionClear();
  21.227 +        return PEP_UNKNOWN_ERROR;
  21.228 +    }
  21.229 +
  21.230 +    return status;
  21.231 +}
  21.232 +}
  21.233 +
  21.234 +extern "C" {
  21.235 +using namespace pEp;
  21.236 +
  21.237 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_init(
  21.238 +        JNIEnv *env,
  21.239 +        jobject obj
  21.240 +    )
  21.241 +{
  21.242 +    std::lock_guard<std::mutex> l(global_mutex); // global mutex for write access to <unordered_map>
  21.243 +    pEpLog("called");
  21.244 +
  21.245 +    if (first) {
  21.246 +        pEpLog("first Engine instance");
  21.247 +        first = false;
  21.248 +        env->GetJavaVM(&jvm);
  21.249 +        jni_init();
  21.250 +        objj = env->NewGlobalRef(obj);
  21.251 +        callback_dispatcher.add(messageToSend, notifyHandshake, JNISync::onSyncStartup, JNISync::onSyncShutdown);
  21.252 +        Adapter::_messageToSend = CallbackDispatcher::messageToSend;
  21.253 +    }
  21.254 +
  21.255 +    create_engine_java_object_mutex(env, obj);  // Create a mutex per java object
  21.256 +    Adapter::session();
  21.257 +}
  21.258 +
  21.259 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_release(
  21.260 +        JNIEnv *env,
  21.261 +        jobject obj
  21.262 +    )
  21.263 +{
  21.264 +    std::lock_guard<std::mutex> l(global_mutex);  // global mutex for write access to <unordered_map>
  21.265 +    pEpLog("called");
  21.266 +    release_engine_java_object_mutex(env, obj);
  21.267 +    Adapter::session(pEp::Adapter::release);
  21.268 +}
  21.269 +
  21.270 +JNIEXPORT jstring JNICALL Java_foundation_pEp_jniadapter_AbstractEngine__1getVersion(
  21.271 +        JNIEnv *env,
  21.272 +        jobject obj
  21.273 +    )
  21.274 +{
  21.275 +    std::mutex *mutex_local = nullptr;
  21.276 +    {
  21.277 +        std::lock_guard<std::mutex> l(global_mutex);
  21.278 +        pEpLog("called with lock_guard");
  21.279 +        mutex_local = get_engine_java_object_mutex(env, obj);
  21.280 +    }
  21.281 +    std::lock_guard<std::mutex> l(*mutex_local);
  21.282 +
  21.283 +    return env->NewStringUTF(::get_engine_version());
  21.284 +}
  21.285 +
  21.286 +JNIEXPORT jstring JNICALL Java_foundation_pEp_jniadapter_AbstractEngine__1getProtocolVersion(
  21.287 +        JNIEnv *env,
  21.288 +        jobject obj
  21.289 +    )
  21.290 +{
  21.291 +    std::mutex *mutex_local = nullptr;
  21.292 +    {
  21.293 +        std::lock_guard<std::mutex> l(global_mutex);
  21.294 +        pEpLog("called with lock_guard");
  21.295 +        mutex_local = get_engine_java_object_mutex(env, obj);
  21.296 +    }
  21.297 +    std::lock_guard<std::mutex> l(*mutex_local);
  21.298 +
  21.299 +    return env->NewStringUTF(::get_protocol_version());
  21.300 +}
  21.301 +
  21.302 +int examine_identity(pEp_identity *ident, void *arg)
  21.303 +{
  21.304 +    locked_queue< pEp_identity * > *queue = (locked_queue< pEp_identity * > *) arg;
  21.305 +    queue->push_back(identity_dup(ident));
  21.306 +    return 0;
  21.307 +}
  21.308 +
  21.309 +pEp_identity *retrieve_next_identity(void *arg)
  21.310 +{
  21.311 +    pEpLog("called");
  21.312 +    locked_queue< pEp_identity * > *queue = (locked_queue< pEp_identity * > *) arg;
  21.313 +
  21.314 +    while (!queue->size())
  21.315 +        usleep(100000);
  21.316 +
  21.317 +    pEp_identity *ident = queue->front();
  21.318 +    queue->pop_front();
  21.319 +    return ident;
  21.320 +}
  21.321 +
  21.322 +static void *keyserver_thread_routine(void *arg)
  21.323 +{
  21.324 +    PEP_STATUS status = do_keymanagement(retrieve_next_identity, arg);
  21.325 +    locked_queue< pEp_identity * > *queue = (locked_queue< pEp_identity * > *) arg;
  21.326 +
  21.327 +    while (queue->size()) {
  21.328 +        pEp_identity *ident = queue->front();
  21.329 +        queue->pop_front();
  21.330 +        free_identity(ident);
  21.331 +    }
  21.332 +
  21.333 +    delete queue;
  21.334 +    return (void *) status;
  21.335 +}
  21.336 +
  21.337 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine__1startKeyserverLookup(
  21.338 +        JNIEnv *env,
  21.339 +        jobject obj
  21.340 +    )
  21.341 +{
  21.342 +    std::mutex *mutex_local = nullptr;
  21.343 +    {
  21.344 +        std::lock_guard<std::mutex> l(global_mutex);
  21.345 +        pEpLog("called with lock_guard");
  21.346 +        mutex_local = get_engine_java_object_mutex(env, obj);
  21.347 +    }
  21.348 +    std::lock_guard<std::mutex> l(*mutex_local);
  21.349 +
  21.350 +    pthread_t *thread = nullptr;
  21.351 +    locked_queue< pEp_identity * > *queue = nullptr;
  21.352 +
  21.353 +    jfieldID thread_handle;
  21.354 +    jfieldID queue_handle;
  21.355 +
  21.356 +    try {
  21.357 +        thread_handle = getFieldID(env, "foundation/pEp/jniadapter/Engine", "keyserverThread", "J");
  21.358 +        queue_handle = getFieldID(env, "foundation/pEp/jniadapter/Engine", "keyserverQueue", "J");
  21.359 +    }
  21.360 +    catch (std::exception& ex) {
  21.361 +        assert(0);
  21.362 +        return;
  21.363 +    }
  21.364 +
  21.365 +    thread = (pthread_t *) env->GetLongField(obj, thread_handle);
  21.366 +    if (thread)
  21.367 +        return;
  21.368 +
  21.369 +    thread = (pthread_t *) calloc(1, sizeof(pthread_t));
  21.370 +    assert(thread);
  21.371 +    env->SetLongField(obj, thread_handle, (jlong) thread);
  21.372 +
  21.373 +    queue = new locked_queue< pEp_identity * >();
  21.374 +    env->SetLongField(obj, queue_handle, (jlong) queue);
  21.375 +
  21.376 +    register_examine_function(Adapter::session(), examine_identity, (void *) queue);
  21.377 +
  21.378 +    pthread_create(thread, nullptr, keyserver_thread_routine, (void *) queue);
  21.379 +}
  21.380 +
  21.381 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine__1stopKeyserverLookup(
  21.382 +        JNIEnv *env,
  21.383 +        jobject obj
  21.384 +    )
  21.385 +{
  21.386 +    std::mutex *mutex_local = nullptr;
  21.387 +    {
  21.388 +        std::lock_guard<std::mutex> l(global_mutex);
  21.389 +        pEpLog("called with lock_guard");
  21.390 +        mutex_local = get_engine_java_object_mutex(env, obj);
  21.391 +    }
  21.392 +    std::lock_guard<std::mutex> l(*mutex_local);
  21.393 +
  21.394 +    pthread_t *thread = nullptr;
  21.395 +    locked_queue< pEp_identity * > *queue = nullptr;
  21.396 +
  21.397 +    jfieldID thread_handle;
  21.398 +    jfieldID queue_handle;
  21.399 +
  21.400 +    try {
  21.401 +        thread_handle = getFieldID(env, "foundation/pEp/jniadapter/Engine", "keyserverThread", "J");
  21.402 +        queue_handle = getFieldID(env, "foundation/pEp/jniadapter/Engine", "keyserverQueue", "J");
  21.403 +    }
  21.404 +    catch (std::exception& ex) {
  21.405 +        assert(0);
  21.406 +        return;
  21.407 +    }
  21.408 +
  21.409 +    thread = (pthread_t *) env->GetLongField(obj, thread_handle);
  21.410 +    if (!thread)
  21.411 +        return;
  21.412 +
  21.413 +    queue = (locked_queue< pEp_identity * > *) env->GetLongField(obj, queue_handle);
  21.414 +
  21.415 +    env->SetLongField(obj, queue_handle, (jlong) 0);
  21.416 +    env->SetLongField(obj, thread_handle, (jlong) 0);
  21.417 +
  21.418 +    register_examine_function(Adapter::session(), nullptr, nullptr);
  21.419 +
  21.420 +    queue->push_front(nullptr);
  21.421 +    pthread_join(*thread, nullptr);
  21.422 +    free(thread);
  21.423 +}
  21.424 +
  21.425 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine__1startSync(
  21.426 +        JNIEnv *env,
  21.427 +        jobject obj
  21.428 +    )
  21.429 +{
  21.430 +    std::mutex *mutex_local = nullptr;
  21.431 +    {
  21.432 +        std::lock_guard<std::mutex> l(global_mutex);
  21.433 +        pEpLog("called with lock_guard");
  21.434 +        mutex_local = get_engine_java_object_mutex(env, obj);
  21.435 +    }
  21.436 +    std::lock_guard<std::mutex> l(*mutex_local);
  21.437 +
  21.438 +    try {
  21.439 +        CallbackDispatcher::start_sync();
  21.440 +//        Adapter::startup<JNISync>(messageToSend, notifyHandshake, &o, &JNISync::onSyncStartup, &JNISync::onSyncShutdown);
  21.441 +    } catch (RuntimeError& ex) {
  21.442 +        throw_pEp_Exception(env, ex.status);
  21.443 +        return;
  21.444 +    }
  21.445 +}
  21.446 +
  21.447 +JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine__1stopSync(
  21.448 +        JNIEnv *env,
  21.449 +        jobject obj
  21.450 +    )
  21.451 +{
  21.452 +    std::mutex *mutex_local = nullptr;
  21.453 +    {
  21.454 +        std::lock_guard<std::mutex> l(global_mutex);
  21.455 +        pEpLog("called with lock_guard");
  21.456 +        mutex_local = get_engine_java_object_mutex(env, obj);
  21.457 +    }
  21.458 +    std::lock_guard<std::mutex> l(*mutex_local);
  21.459 +
  21.460 +    CallbackDispatcher::stop_sync();
  21.461 +//    Adapter::shutdown();
  21.462 +}
  21.463 +
  21.464 +JNIEXPORT jboolean JNICALL Java_foundation_pEp_jniadapter_AbstractEngine__1isSyncRunning(
  21.465 +        JNIEnv *env,
  21.466 +        jobject obj
  21.467 +    )
  21.468 +{
  21.469 +    std::mutex *mutex_local = nullptr;
  21.470 +    {
  21.471 +        std::lock_guard<std::mutex> l(global_mutex);
  21.472 +        pEpLog("called with lock_guard");
  21.473 +        mutex_local = get_engine_java_object_mutex(env, obj);
  21.474 +    }
  21.475 +    std::lock_guard<std::mutex> l(*mutex_local);
  21.476 +
  21.477 +    return (jboolean) Adapter::is_sync_running();
  21.478 +}
  21.479 +
  21.480 +} // extern "C"
  21.481 +
    22.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    22.2 +++ b/src/cxx/foundation_pEp_jniadapter__Blob.cc	Fri Aug 28 18:18:26 2020 +0200
    22.3 @@ -0,0 +1,64 @@
    22.4 +#include <cassert>
    22.5 +#include <pEp/platform.h>
    22.6 +#include <pEp/sync_codec.h>
    22.7 +#include <pEp/distribution_codec.h>
    22.8 +#include <pEp/pEpLog.hh>
    22.9 +#include "jniutils.hh"
   22.10 +#include "throw_pEp_exception.hh"
   22.11 +#include "foundation_pEp_jniadapter__Blob.h"
   22.12 +
   22.13 +namespace pEp {
   22.14 +    namespace JNIAdapter {
   22.15 +        static ::bloblist_t *bloblist_ptr(JNIEnv *env, jobject me) {
   22.16 +            jfieldID handle;
   22.17 +
   22.18 +            try {
   22.19 +                handle = getFieldID(env, "foundation/pEp/jniadapter/Blob", "mime_type", "Ljava/lang/String");
   22.20 +            }
   22.21 +            catch (std::exception& ex) {
   22.22 +                assert(0);
   22.23 +                return NULL;
   22.24 +            }
   22.25 +
   22.26 +            return (::bloblist_t *) (intptr_t) (int64_t) env->GetLongField(me, handle);
   22.27 +        }
   22.28 +    };
   22.29 +};
   22.30 +
   22.31 +extern "C" {
   22.32 +
   22.33 +
   22.34 +using namespace std;
   22.35 +using namespace pEp::JNIAdapter;
   22.36 +JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter__1Blob__1dataToXER(JNIEnv *env, jobject obj)
   22.37 +{
   22.38 +    pEpLog("called");
   22.39 +    bloblist_t *b = to_blob(env, obj);
   22.40 +    char *out = nullptr;
   22.41 +
   22.42 +    // RFC 1049 / RFC 2045 : The type, subtype, and parameter names are not case sensitive.
   22.43 +    if(strcasecmp(b->mime_type, "application/pEp.sync") == 0) {
   22.44 +        PEP_STATUS status = ::PER_to_XER_Sync_msg(b->value, (size_t) b->size, &out);
   22.45 +        if (status)
   22.46 +            throw_pEp_Exception(env, status);
   22.47 +
   22.48 +        jbyteArray result = from_string(env, out);
   22.49 +        free(out);
   22.50 +        return result;
   22.51 +    }
   22.52 +
   22.53 +    // RFC 1049 / RFC 2045 : The type, subtype, and parameter names are not case sensitive.
   22.54 +    if(strcasecmp(b->mime_type, "application/pEp.keyreset") == 0) {
   22.55 +        PEP_STATUS status = ::PER_to_XER_Distribution_msg(b->value, (size_t) b->size, &out);
   22.56 +        if (status)
   22.57 +            throw_pEp_Exception(env, status);
   22.58 +
   22.59 +        jbyteArray result = from_string(env, out);
   22.60 +        free(out);
   22.61 +        return result;
   22.62 +    }
   22.63 +
   22.64 +    return from_string(env,b->value);
   22.65 +}
   22.66 +
   22.67 +}; // extern "C"
    23.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    23.2 +++ b/src/cxx/get_header.cc	Fri Aug 28 18:18:26 2020 +0200
    23.3 @@ -0,0 +1,1 @@
    23.4 +#include <pEp/pEpEngine.h>
    24.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    24.2 +++ b/src/cxx/identity_api.cc	Fri Aug 28 18:18:26 2020 +0200
    24.3 @@ -0,0 +1,16 @@
    24.4 +#include <pEp/message_api.h>
    24.5 +#include <pEp/pEpLog.hh>
    24.6 +
    24.7 +#include "jniutils.hh"
    24.8 +
    24.9 +
   24.10 +extern "C" {
   24.11 +
   24.12 +JNIEXPORT jint JNICALL Java_foundation_pEp_jniadapter_Identity__1getRating(JNIEnv *env, jobject thiz, jint comm_type)
   24.13 +{
   24.14 +    pEpLog("called");
   24.15 +    return ::rating_from_comm_type((PEP_comm_type) comm_type);
   24.16 +}
   24.17 +
   24.18 +} // extern "C"
   24.19 +
    25.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    25.2 +++ b/src/cxx/jniutils.cc	Fri Aug 28 18:18:26 2020 +0200
    25.3 @@ -0,0 +1,772 @@
    25.4 +#include <cassert>
    25.5 +#include "jniutils.hh"
    25.6 +#include <pEp/pEpLog.hh>
    25.7 +#ifndef __LP64__
    25.8 +#include <time64.h>
    25.9 +#define time_t time64_t
   25.10 +#define timegm timegm64
   25.11 +#define gmtime_r gmtime64_r
   25.12 +#else
   25.13 +#include <string.h>
   25.14 +#endif
   25.15 +
   25.16 +namespace pEp {
   25.17 +    namespace JNIAdapter {
   25.18 +        std::mutex global_mutex;
   25.19 +        std::unordered_map<long, std::mutex*> engine_objid_mutex;
   25.20 +
   25.21 +        std::mutex* get_engine_java_object_mutex(
   25.22 +                JNIEnv *env,
   25.23 +                jobject obj
   25.24 +            )
   25.25 +        {
   25.26 +            long engine_obj_id = (long)callLongMethod(env, obj, "getId");
   25.27 +            assert(engine_obj_id);
   25.28 +            pEpLog("for java object id: " << engine_obj_id);
   25.29 +            std::mutex *engine_obj_mutex = engine_objid_mutex.at(engine_obj_id);
   25.30 +            pEpLog("found mutex: " << engine_obj_mutex << " with native_handle: " << engine_obj_mutex->native_handle());
   25.31 +            assert(engine_obj_mutex);
   25.32 +            return engine_obj_mutex;
   25.33 +        }
   25.34 +
   25.35 +        void create_engine_java_object_mutex(
   25.36 +                JNIEnv *env,
   25.37 +                jobject obj
   25.38 +            )
   25.39 +        {
   25.40 +            long engine_obj_id = (long)callLongMethod(env, obj, "getId");
   25.41 +            assert(engine_obj_id);
   25.42 +            std::mutex *engine_obj_mutex = new std::mutex();
   25.43 +            pEpLog(engine_obj_mutex << " with native_handle: " << engine_obj_mutex->native_handle() << " for java object id: " << engine_obj_id);
   25.44 +            assert(engine_obj_mutex);
   25.45 +            if(engine_objid_mutex.count(engine_obj_id) > 0) {
   25.46 +                pEpLog("Fatal: mutex already existing for object id: " << engine_obj_id);
   25.47 +                assert(0);
   25.48 +            }
   25.49 +            engine_objid_mutex.insert(std::make_pair(engine_obj_id, engine_obj_mutex ));
   25.50 +        }
   25.51 +
   25.52 +        void release_engine_java_object_mutex(
   25.53 +                JNIEnv *env,
   25.54 +                jobject obj
   25.55 +            )
   25.56 +        {
   25.57 +            long engine_obj_id = (long)callLongMethod(env, obj, "getId");
   25.58 +            assert(engine_obj_id);
   25.59 +            std::mutex *engine_obj_mutex = engine_objid_mutex.at(engine_obj_id);
   25.60 +            pEpLog(engine_obj_mutex << " with native_handle: " << engine_obj_mutex->native_handle() << " for java object id: " << engine_obj_id);
   25.61 +            assert(engine_obj_mutex);
   25.62 +            engine_objid_mutex.erase(engine_obj_id);
   25.63 +            delete engine_obj_mutex;
   25.64 +        }
   25.65 +
   25.66 +        jclass findClass(JNIEnv *env, const char *classname)
   25.67 +        {
   25.68 +            jclass clazz = env->FindClass(classname);
   25.69 +            if (!clazz)
   25.70 +                fprintf(stderr, "class not found: %s\n", classname);
   25.71 +            assert(clazz);
   25.72 +
   25.73 +            if (clazz == NULL) {
   25.74 +                jclass ex = env->FindClass("java/lang/NoClassDefFoundError");
   25.75 +                assert(ex);
   25.76 +                env->ThrowNew(ex, classname);
   25.77 +                throw std::bad_cast();
   25.78 +            }
   25.79 +            
   25.80 +            return clazz;
   25.81 +        }
   25.82 +
   25.83 +        jfieldID getFieldID(
   25.84 +                JNIEnv *env,
   25.85 +                const char *classname,
   25.86 +                const char *fieldname,
   25.87 +                const char *signature
   25.88 +            )
   25.89 +        {
   25.90 +            jclass clazz = findClass(env, classname);
   25.91 +            assert(clazz);
   25.92 +
   25.93 +            jfieldID field = env->GetFieldID(clazz, fieldname, signature);
   25.94 +            assert(field);
   25.95 +
   25.96 +            if (field == NULL) {
   25.97 +                jclass ex = env->FindClass("java/lang/NoSuchFieldError");
   25.98 +                assert(ex);
   25.99 +                env->ThrowNew(ex, fieldname);
  25.100 +                throw std::invalid_argument(std::string(fieldname));
  25.101 +            }
  25.102 +
  25.103 +            env->DeleteLocalRef(clazz);
  25.104 +
  25.105 +            return field;
  25.106 +        }
  25.107 +
  25.108 +        //TODO: fix/generalize/clean patch added to make keysync work using globalref to class
  25.109 +        jfieldID getFieldID(
  25.110 +                JNIEnv *env,
  25.111 +                const char *classname,
  25.112 +                const char *fieldname,
  25.113 +                const char *signature,
  25.114 +                const jclass clazz
  25.115 +            )
  25.116 +        {
  25.117 +
  25.118 +            jfieldID field = env->GetFieldID(clazz, fieldname, signature);
  25.119 +            assert(field);
  25.120 +
  25.121 +            if (field == NULL) {
  25.122 +                jclass ex = env->FindClass("java/lang/NoSuchFieldError");
  25.123 +                assert(ex);
  25.124 +                env->ThrowNew(ex, fieldname);
  25.125 +                throw std::invalid_argument(std::string(fieldname));
  25.126 +            }
  25.127 +
  25.128 +
  25.129 +            return field;
  25.130 +        }
  25.131 +
  25.132 +        jint callIntMethod(
  25.133 +                JNIEnv *env,
  25.134 +                jobject obj,
  25.135 +                const char *methodname
  25.136 +            )
  25.137 +        {
  25.138 +            jclass clazz = env->GetObjectClass(obj);
  25.139 +            assert(clazz);
  25.140 +
  25.141 +            jmethodID method = env->GetMethodID(clazz, methodname, "()I");
  25.142 +            if (method == NULL) {
  25.143 +                jclass ex = env->FindClass("java/lang/NoSuchMethodError");
  25.144 +                assert(ex);
  25.145 +                env->ThrowNew(ex, methodname);
  25.146 +                throw std::invalid_argument(std::string(methodname));
  25.147 +            }
  25.148 +
  25.149 +            env->DeleteLocalRef(clazz);
  25.150 +
  25.151 +            jint result = env->CallIntMethod(obj, method);
  25.152 +            env->ExceptionCheck(); // handle exception in Java
  25.153 +            return result;
  25.154 +        }
  25.155 +
  25.156 +        jlong callLongMethod(
  25.157 +                JNIEnv *env,
  25.158 +                jobject obj,
  25.159 +                const char *methodname
  25.160 +            )
  25.161 +        {
  25.162 +            jclass clazz = env->GetObjectClass(obj);
  25.163 +            assert(clazz);
  25.164 +
  25.165 +            jmethodID method = env->GetMethodID(clazz, methodname, "()J");
  25.166 +            if (method == NULL) {
  25.167 +                jclass ex = env->FindClass("java/lang/NoSuchMethodError");
  25.168 +                assert(ex);
  25.169 +                env->ThrowNew(ex, methodname);
  25.170 +                throw std::invalid_argument(std::string(methodname));
  25.171 +            }
  25.172 +
  25.173 +            env->DeleteLocalRef(clazz);
  25.174 +
  25.175 +            jlong result = env->CallLongMethod(obj, method);
  25.176 +            env->ExceptionCheck(); // handle exception in Java
  25.177 +            return result;
  25.178 +        }
  25.179 +
  25.180 +        jobject callObjectMethod(
  25.181 +                JNIEnv *env,
  25.182 +                jobject obj,
  25.183 +                const char *methodname,
  25.184 +                jint index
  25.185 +            )
  25.186 +        {
  25.187 +            jclass clazz = env->GetObjectClass(obj);
  25.188 +            assert(clazz);
  25.189 +
  25.190 +            jmethodID method = env->GetMethodID(clazz, methodname,
  25.191 +                    "(I)Ljava/lang/Object;");
  25.192 +            if (method == NULL) {
  25.193 +                jclass ex = env->FindClass("java/lang/NoSuchMethodError");
  25.194 +                assert(ex);
  25.195 +                env->ThrowNew(ex, methodname);
  25.196 +                throw std::invalid_argument(std::string(methodname));
  25.197 +            }
  25.198 +
  25.199 +            env->DeleteLocalRef(clazz);
  25.200 +
  25.201 +            jobject result = env->CallObjectMethod(obj, method, index);
  25.202 +            env->ExceptionCheck(); // handle exception in Java
  25.203 +            return result;
  25.204 +        }
  25.205 +
  25.206 +        jboolean callBooleanMethod(
  25.207 +                JNIEnv *env,
  25.208 +                jobject obj,
  25.209 +                const char *methodname,
  25.210 +                jobject o
  25.211 +            )
  25.212 +        {
  25.213 +            jclass clazz = env->GetObjectClass(obj);
  25.214 +            assert(clazz);
  25.215 +
  25.216 +            jmethodID method = env->GetMethodID(clazz, methodname,
  25.217 +                    "(Ljava/lang/Object;)Z");
  25.218 +            if (method == NULL) {
  25.219 +                jclass ex = env->FindClass("java/lang/NoSuchMethodError");
  25.220 +                assert(ex);
  25.221 +                env->ThrowNew(ex, methodname);
  25.222 +                throw std::invalid_argument(std::string(methodname));
  25.223 +            }
  25.224 +
  25.225 +            env->DeleteLocalRef(clazz);
  25.226 +
  25.227 +            jboolean result = env->CallBooleanMethod(obj, method, o);
  25.228 +            env->ExceptionCheck(); // handle exception in Java
  25.229 +            return result;
  25.230 +        }
  25.231 +
  25.232 +        jint outOfMemory(JNIEnv *env)
  25.233 +        {
  25.234 +            jclass ex;
  25.235 +            const char *ex_name = "java/lang/OutOfMemoryError";
  25.236 +
  25.237 +            ex = env->FindClass(ex_name);
  25.238 +            assert(ex);
  25.239 +            return env->ThrowNew(ex, ex_name);
  25.240 +        }
  25.241 +
  25.242 +        jobject from_Integer(JNIEnv *env, int val)
  25.243 +        {
  25.244 +            assert(env);
  25.245 +            jclass clazz = findClass(env, "java/lang/Integer");
  25.246 +
  25.247 +            jmethodID constructor = env->GetMethodID(clazz, "<init>", "(I)V");
  25.248 +            assert(constructor);
  25.249 +
  25.250 +            jobject obj = env->NewObject(clazz, constructor, val);
  25.251 +            assert(obj);
  25.252 +            return obj;
  25.253 +        }
  25.254 +
  25.255 +        int to_Integer(JNIEnv *env, jobject obj)
  25.256 +        {
  25.257 +            assert(env && obj);
  25.258 +            int _val = callIntMethod(env, obj, "intValue");
  25.259 +            return _val;
  25.260 +        }
  25.261 +
  25.262 +        jbyteArray from_string(JNIEnv *env, const char *str)
  25.263 +        {
  25.264 +            if (str && str[0]) {
  25.265 +                jboolean isCopy;
  25.266 +                size_t l = strlen(str);
  25.267 +                jbyteArray _str = env->NewByteArray(l);
  25.268 +                env->SetByteArrayRegion(_str, 0, l, (jbyte*)str);
  25.269 +                return _str;
  25.270 +            }
  25.271 +            else if (str) {
  25.272 +                return env->NewByteArray(0);
  25.273 +            } else {
  25.274 +                return (jbyteArray) NULL;
  25.275 +            }
  25.276 +        }
  25.277 +
  25.278 +        char *to_string(JNIEnv *env, jbyteArray str)
  25.279 +        {
  25.280 +            if (str == NULL)
  25.281 +                return NULL;
  25.282 +
  25.283 +            size_t l = env->GetArrayLength(str);
  25.284 +            char *_str = (char *) calloc(1,l+1);
  25.285 +            assert(_str);
  25.286 +            env->GetByteArrayRegion(str, 0, l, (jbyte*)_str);
  25.287 +            return _str;
  25.288 +        }
  25.289 +
  25.290 +        jobject from_stringlist(JNIEnv *env, stringlist_t *sl)
  25.291 +        {
  25.292 +            if (!sl)
  25.293 +                return (jobject) NULL;
  25.294 +
  25.295 +            jclass clazz = findClass(env, "java/util/Vector");
  25.296 +            jmethodID constructor = env->GetMethodID(clazz, "<init>", "()V");
  25.297 +            assert(constructor);
  25.298 +            jobject obj = env->NewObject(clazz, constructor);
  25.299 +            assert(obj);
  25.300 +
  25.301 +            stringlist_t *_sl;
  25.302 +            for (_sl = sl; _sl && _sl->value; _sl = _sl->next) {
  25.303 +                jobject o = from_string(env, _sl->value);
  25.304 +                callBooleanMethod(env, obj, "add", o);
  25.305 +            }
  25.306 +
  25.307 +            env->DeleteLocalRef(clazz);
  25.308 +
  25.309 +            return obj;
  25.310 +        }
  25.311 +
  25.312 +        stringlist_t *to_stringlist(JNIEnv *env, jobject obj)
  25.313 +        {
  25.314 +            if (!obj)
  25.315 +                return NULL;
  25.316 +
  25.317 +            jint size = callIntMethod(env, obj, "size");
  25.318 +            if (size == 0)
  25.319 +                return NULL;
  25.320 +
  25.321 +            stringlist_t *sl = new_stringlist(NULL);
  25.322 +            stringlist_t *_sl;
  25.323 +            jint i;
  25.324 +            for (_sl = sl, i = 0; i < (int) size; i++) {
  25.325 +                jobject o = callObjectMethod(env, obj, "get", i);
  25.326 +                jbyteArray a = reinterpret_cast<jbyteArray>(o);
  25.327 +                char * str = to_string(env, a);
  25.328 +                _sl = stringlist_add(_sl, str);
  25.329 +                env->DeleteLocalRef(o);
  25.330 +                free(str);
  25.331 +            }
  25.332 +
  25.333 +            return sl;
  25.334 +        }
  25.335 +
  25.336 +        jobject from_stringpairlist(JNIEnv *env, stringpair_list_t *sl)
  25.337 +        {
  25.338 +            if (!sl)
  25.339 +                return (jobject) NULL;
  25.340 +
  25.341 +            jclass clazz = findClass(env, "java/util/ArrayList");
  25.342 +            jclass clazz_pair = findClass(env, "foundation/pEp/jniadapter/Pair");
  25.343 +            jmethodID constructor = env->GetMethodID(clazz, "<init>", "()V");
  25.344 +            assert(constructor);
  25.345 +            jmethodID constructor_pair = env->GetMethodID(clazz_pair, "<init>",
  25.346 +                    "(Ljava/lang/Object;Ljava/lang/Object;)V");
  25.347 +            assert(constructor_pair);
  25.348 +
  25.349 +            jobject obj = env->NewObject(clazz, constructor);
  25.350 +            assert(obj);
  25.351 +
  25.352 +            stringpair_list_t *_sl;
  25.353 +            for (_sl = sl; _sl && _sl->value; _sl = _sl->next) {
  25.354 +                assert(_sl->value->key);
  25.355 +                assert(_sl->value->value);
  25.356 +
  25.357 +                jbyteArray first = from_string(env, _sl->value->key);
  25.358 +                jbyteArray second = from_string(env, _sl->value->value);
  25.359 +                jobject pair = env->NewObject(clazz_pair, constructor_pair,
  25.360 +                        first, second);
  25.361 +                callBooleanMethod(env, obj, "add", pair);
  25.362 +
  25.363 +                env->DeleteLocalRef(first);
  25.364 +                env->DeleteLocalRef(second);
  25.365 +                env->DeleteLocalRef(pair);
  25.366 +
  25.367 +
  25.368 +            }
  25.369 +
  25.370 +            env->DeleteLocalRef(clazz);
  25.371 +            env->DeleteLocalRef(clazz_pair);
  25.372 +
  25.373 +            return obj;
  25.374 +        }
  25.375 +
  25.376 +        stringpair_list_t *to_stringpairlist(JNIEnv *env, jobject obj)
  25.377 +        {
  25.378 +            if (!obj)
  25.379 +                return NULL;
  25.380 +
  25.381 +            jint size = callIntMethod(env, obj, "size");
  25.382 +            if (size == 0)
  25.383 +                return NULL;
  25.384 +
  25.385 +            jfieldID first_id = getFieldID(env, "foundation/pEp/jniadapter/Pair",
  25.386 +                    "first", "Ljava/lang/Object;");
  25.387 +            jfieldID second_id = getFieldID(env, "foundation/pEp/jniadapter/Pair",
  25.388 +                    "second", "Ljava/lang/Object;");
  25.389 +
  25.390 +            stringpair_list_t *sl = new_stringpair_list(NULL);
  25.391 +            stringpair_list_t *_sl;
  25.392 +            jint i;
  25.393 +
  25.394 +            for (_sl = sl, i = 0; i < (int) size; i++) {
  25.395 +                jobject pair = callObjectMethod(env, obj, "get", i);
  25.396 +                jbyteArray first =
  25.397 +                    reinterpret_cast<jbyteArray>(env->GetObjectField(pair,
  25.398 +                                first_id));
  25.399 +                jbyteArray second =
  25.400 +                    reinterpret_cast<jbyteArray>(env->GetObjectField(pair,
  25.401 +                                second_id));
  25.402 +
  25.403 +                char *first_str = to_string(env, first);
  25.404 +                char *second_str = to_string(env, second);
  25.405 +                stringpair_t *sp = new_stringpair(first_str, second_str);
  25.406 +                env->DeleteLocalRef(pair);
  25.407 +                free(first_str);
  25.408 +                free(second_str);
  25.409 +
  25.410 +                _sl = stringpair_list_add(_sl, sp);
  25.411 +            }
  25.412 +
  25.413 +            return sl;
  25.414 +        }
  25.415 +
  25.416 +        jobject from_timestamp(JNIEnv *env, timestamp *ts)
  25.417 +        {
  25.418 +            if (!ts)
  25.419 +                return (jobject) NULL;
  25.420 +
  25.421 +            //LOGD("/* Seconds (0-60) */  FROM   :%d", ts->tm_sec);
  25.422 +            //LOGD("/* Minutes (0-59) */         :%d", ts->tm_min);    
  25.423 +            //LOGD("/* Hours (0-23) */           :%d", ts->tm_hour);   
  25.424 +            //LOGD("/* Day of the month (1-31) */:%d", ts->tm_mday);   
  25.425 +            //LOGD("/* Month (0-11) */           :%d", ts->tm_mon);    
  25.426 +            //LOGD("/* Year - 1900 */            :%d", ts->tm_year);   
  25.427 +
  25.428 +            time_t t = timegm(ts)*1000;
  25.429 +            //LOGD( "TimeGM returns : %lld", t);
  25.430 +            jclass clazz = findClass(env, "java/util/Date");
  25.431 +            jmethodID constructor = env->GetMethodID(clazz, "<init>", "(J)V");
  25.432 +            assert(constructor);
  25.433 +
  25.434 +            jobject result =  env->NewObject(clazz, constructor, (jlong) t);
  25.435 +
  25.436 +            env->DeleteLocalRef(clazz);
  25.437 +
  25.438 +            return result;
  25.439 +
  25.440 +        }
  25.441 +
  25.442 +        timestamp *to_timestamp(JNIEnv *env, jobject date)
  25.443 +        {
  25.444 +            if (!date)
  25.445 +                return NULL;
  25.446 +
  25.447 +            jlong t = callLongMethod(env, date, "getTime");
  25.448 +            //LOGD( "Set Time to : %lld", t);
  25.449 +            timestamp *ts = (timestamp*)calloc(1, sizeof(timestamp));
  25.450 +            assert(ts);
  25.451 +            if (ts == NULL)
  25.452 +                return NULL;
  25.453 +
  25.454 +            if (t){
  25.455 +                time_t clock = t/1000;
  25.456 +                gmtime_r(&clock, ts);
  25.457 +
  25.458 +                //LOGD("/* Seconds (0-60) */  TO     :%d", ts->tm_sec);    
  25.459 +                //LOGD("/* Minutes (0-59) */         :%d", ts->tm_min);    
  25.460 +                //LOGD("/* Hours (0-23) */           :%d", ts->tm_hour);   
  25.461 +                //LOGD("/* Day of the month (1-31) */:%d", ts->tm_mday);   
  25.462 +                //LOGD("/* Month (0-11) */           :%d", ts->tm_mon);    
  25.463 +                //LOGD("/* Year - 1900 */            :%d", ts->tm_year);   
  25.464 +            }
  25.465 +
  25.466 +            return ts;
  25.467 +        }
  25.468 +
  25.469 +        static void _setStringField(JNIEnv *env, const char *classname,
  25.470 +                jobject obj, const char *name, const char *value)
  25.471 +        {
  25.472 +            if (value) {
  25.473 +                jfieldID fieldID = getFieldID(env, classname, name, "[B");
  25.474 +                env->SetObjectField(obj, fieldID,
  25.475 +                        reinterpret_cast<jobject>(from_string(env, value)));
  25.476 +
  25.477 +            }
  25.478 +        }
  25.479 +
  25.480 +        //TODO: fix/generalize/clean patch added to make keysync work using globalref to class
  25.481 +        static void _setStringField(JNIEnv *env, const char *classname,
  25.482 +                jobject obj, const char *name, const char *value, const jclass clazz)
  25.483 +        {
  25.484 +            if (value) {
  25.485 +                jfieldID fieldID = getFieldID(env, classname, name, "[B", clazz);
  25.486 +                env->SetObjectField(obj, fieldID,
  25.487 +                        reinterpret_cast<jobject>(from_string(env, value)));
  25.488 +
  25.489 +            }
  25.490 +        }
  25.491 +
  25.492 +        jobject from_identity(JNIEnv *env, pEp_identity *ident)
  25.493 +        {
  25.494 +            if (!ident)
  25.495 +                return (jobject) NULL;
  25.496 +
  25.497 +            static const char *classname = "foundation/pEp/jniadapter/_Identity";
  25.498 +            jclass clazz = findClass(env, classname);
  25.499 +            jmethodID constructor = env->GetMethodID(clazz, "<init>", "()V");
  25.500 +            assert(constructor);
  25.501 +            jobject obj = env->NewObject(clazz, constructor);
  25.502 +
  25.503 +            env->DeleteLocalRef(clazz);
  25.504 +            
  25.505 +            if (ident) {
  25.506 +                _setStringField(env, classname, obj, "address", ident->address);
  25.507 +                _setStringField(env, classname, obj, "fpr", ident->fpr);
  25.508 +                _setStringField(env, classname, obj, "user_id", ident->user_id);
  25.509 +                _setStringField(env, classname, obj, "username", ident->username);
  25.510 +
  25.511 +                jfieldID comm_type_id = getFieldID(env, classname, "comm_type", "I");
  25.512 +                env->SetIntField(obj, comm_type_id, (jint) (int) ident->comm_type);
  25.513 +
  25.514 +                _setStringField(env, classname, obj, "lang", ident->lang);
  25.515 +
  25.516 +                jfieldID me_id = getFieldID(env, classname, "me", "Z");
  25.517 +                env->SetBooleanField(obj, me_id, (jboolean) ident->me);
  25.518 +
  25.519 +                jfieldID flags_id = getFieldID(env, classname, "flags", "I");
  25.520 +                env->SetIntField(obj, flags_id, (jint) (int) ident->flags);
  25.521 +            }
  25.522 +
  25.523 +            return obj;
  25.524 +        }
  25.525 +
  25.526 +        //TODO: fix/generalize/clean patch added to make keysync work using globalref to class
  25.527 +        jobject from_identity(JNIEnv *env, pEp_identity *ident, jclass identityClass)
  25.528 +        {
  25.529 +            if (!ident)
  25.530 +                return (jobject) NULL;
  25.531 +
  25.532 +            static const char *classname = "foundation/pEp/jniadapter/_Identity";
  25.533 +            jmethodID constructor = env->GetMethodID(identityClass, "<init>", "()V");
  25.534 +            assert(constructor);
  25.535 +            jobject obj = env->NewObject(identityClass, constructor);
  25.536 +
  25.537 +            if (ident) {
  25.538 +                _setStringField(env, classname, obj, "address", ident->address, identityClass);
  25.539 +                _setStringField(env, classname, obj, "fpr", ident->fpr, identityClass);
  25.540 +                _setStringField(env, classname, obj, "user_id", ident->user_id, identityClass);
  25.541 +                _setStringField(env, classname, obj, "username", ident->username, identityClass);
  25.542 +
  25.543 +                jfieldID comm_type_id = getFieldID(env, classname, "comm_type", "I", identityClass);
  25.544 +                env->SetIntField(obj, comm_type_id, (jint) (int) ident->comm_type);
  25.545 +
  25.546 +                _setStringField(env, classname, obj, "lang", ident->lang, identityClass);
  25.547 +
  25.548 +                jfieldID me_id = getFieldID(env, classname, "me", "Z", identityClass);
  25.549 +                env->SetBooleanField(obj, me_id, (jboolean) ident->me);
  25.550 +
  25.551 +                jfieldID flags_id = getFieldID(env, classname, "flags", "I", identityClass);
  25.552 +                env->SetIntField(obj, flags_id, (jint) (int) ident->flags);
  25.553 +            }
  25.554 +
  25.555 +            return obj;
  25.556 +        }
  25.557 +
  25.558 +        char *_getStringField(JNIEnv *env, const char *classname, jobject obj,
  25.559 +                const char *name)
  25.560 +        {
  25.561 +            jfieldID fieldID = getFieldID(env, classname, name, "[B");
  25.562 +            jobject fobj = env->GetObjectField(obj, fieldID);
  25.563 +
  25.564 +            char *res =  to_string(env, reinterpret_cast<jbyteArray>(fobj));
  25.565 +
  25.566 +            env->DeleteLocalRef(fobj);
  25.567 +            return res;
  25.568 +        }
  25.569 +
  25.570 +        pEp_identity *to_identity(JNIEnv *env, jobject obj)
  25.571 +        {
  25.572 +            if (!obj)
  25.573 +                return NULL;
  25.574 +
  25.575 +            static const char *classname = "foundation/pEp/jniadapter/_Identity";
  25.576 +            pEp_identity *ident = new_identity(NULL, NULL, NULL, NULL);
  25.577 +
  25.578 +            ident->address = _getStringField(env, classname, obj, "address");
  25.579 +            ident->fpr = _getStringField(env, classname, obj, "fpr");
  25.580 +            ident->user_id = _getStringField(env, classname, obj, "user_id");
  25.581 +            ident->username = _getStringField(env, classname, obj, "username");
  25.582 +
  25.583 +            jfieldID comm_type_id = getFieldID(env, classname, "comm_type", "I");
  25.584 +            ident->comm_type = (PEP_comm_type) (int) env->GetIntField(obj, comm_type_id);
  25.585 +
  25.586 +            char *lang = _getStringField(env, classname, obj, "lang");
  25.587 +            if (lang && lang[0]) {
  25.588 +                ident->lang[0] = lang[0];
  25.589 +                ident->lang[1] = lang[1];
  25.590 +            }
  25.591 +            free(lang);
  25.592 +
  25.593 +            jfieldID me_id = getFieldID(env, classname, "me", "Z");
  25.594 +            ident->me = (bool) env->GetBooleanField(obj, me_id);
  25.595 +            
  25.596 +            jfieldID flags_id = getFieldID(env, classname, "flags", "I");
  25.597 +            ident->flags = (identity_flags_t) (int) env->GetIntField(obj, flags_id);
  25.598 +
  25.599 +            return ident;
  25.600 +        }
  25.601 +
  25.602 +        jobject from_identitylist(JNIEnv *env, identity_list *il)
  25.603 +        {
  25.604 +            if (!il)
  25.605 +                return (jobject) NULL;
  25.606 +
  25.607 +            jclass clazz = findClass(env, "java/util/Vector");
  25.608 +            jmethodID constructor = env->GetMethodID(clazz, "<init>", "()V");
  25.609 +            assert(constructor);
  25.610 +            jobject obj = env->NewObject(clazz, constructor);
  25.611 +            assert(obj);
  25.612 +
  25.613 +            identity_list *_il;
  25.614 +            for (_il = il; _il && _il->ident; _il = _il->next) {
  25.615 +                jobject o = from_identity(env, _il->ident);
  25.616 +                callBooleanMethod(env, obj, "add", o);
  25.617 +            }
  25.618 +
  25.619 +            env->DeleteLocalRef(clazz);
  25.620 +
  25.621 +            return obj;
  25.622 +        }
  25.623 +
  25.624 +        identity_list *to_identitylist(JNIEnv *env, jobject obj)
  25.625 +        {
  25.626 +            if (!obj)
  25.627 +                return NULL;
  25.628 +
  25.629 +            jint size = callIntMethod(env, obj, "size");
  25.630 +            if (size == 0)
  25.631 +                return NULL;
  25.632 +
  25.633 +            identity_list *il = new_identity_list(NULL);
  25.634 +            identity_list *_il;
  25.635 +            jint i;
  25.636 +            for (_il = il, i = 0; i < (int) size; i++) {
  25.637 +                jobject o = callObjectMethod(env, obj, "get", i);
  25.638 +                pEp_identity* ident = to_identity(env, o);
  25.639 +                _il = identity_list_add(_il, ident);
  25.640 +                env->DeleteLocalRef(o);
  25.641 +            }
  25.642 +
  25.643 +            return il;
  25.644 +        }
  25.645 +
  25.646 +        jobject _from_blob(JNIEnv *env, bloblist_t *b)
  25.647 +        {
  25.648 +            if (!b)
  25.649 +                return (jobject) NULL;
  25.650 +
  25.651 +            static const char *classname = "foundation/pEp/jniadapter/_Blob";
  25.652 +            jclass clazz = findClass(env, classname);
  25.653 +            jmethodID constructor = env->GetMethodID(clazz, "<init>", "()V");
  25.654 +            assert(constructor);
  25.655 +            jobject obj = env->NewObject(clazz, constructor);
  25.656 +            
  25.657 +            env->DeleteLocalRef(clazz);
  25.658 +
  25.659 +            jfieldID fieldID = getFieldID(env, classname, "data", "[B");
  25.660 +            jbyteArray _data = env->NewByteArray((jsize) b->size);
  25.661 +            env->SetByteArrayRegion(_data, 0, b->size, (jbyte*)b->value);
  25.662 +            env->SetObjectField(obj, fieldID, reinterpret_cast<jobject>(_data));
  25.663 +            _setStringField(env, classname, obj, "mime_type", b->mime_type);
  25.664 +            _setStringField(env, classname, obj, "filename", b->filename);
  25.665 +
  25.666 +            return obj;
  25.667 +        }
  25.668 +
  25.669 +        jobject from_bloblist(JNIEnv *env, bloblist_t *bl)
  25.670 +        {
  25.671 +            if (!bl)
  25.672 +                return (jobject) NULL;
  25.673 +
  25.674 +            jclass clazz = findClass(env, "java/util/Vector");
  25.675 +            jmethodID constructor = env->GetMethodID(clazz, "<init>", "()V");
  25.676 +            assert(constructor);
  25.677 +            jobject obj = env->NewObject(clazz, constructor);
  25.678 +            assert(obj);
  25.679 +
  25.680 +            env->DeleteLocalRef(clazz);
  25.681 +
  25.682 +            bloblist_t *_bl;
  25.683 +            for (_bl = bl; _bl && _bl->value; _bl = _bl->next) {
  25.684 +                jobject o = _from_blob(env, _bl);
  25.685 +                if(o)
  25.686 +                    callBooleanMethod(env, obj, "add", o);
  25.687 +            }
  25.688 +
  25.689 +            return obj;
  25.690 +        }
  25.691 +
  25.692 +        bloblist_t *to_blob(JNIEnv *env, jobject obj)
  25.693 +        {
  25.694 +            if (!obj)
  25.695 +                return NULL;
  25.696 +
  25.697 +            static const char *classname = "foundation/pEp/jniadapter/_Blob";
  25.698 +            jclass clazz = findClass(env, classname);
  25.699 +
  25.700 +            char *mime_type = _getStringField(env, classname, obj, "mime_type");
  25.701 +            char *filename = _getStringField(env, classname, obj, "filename");
  25.702 +
  25.703 +            jfieldID data_id = getFieldID(env, classname, "data", "[B");
  25.704 +            jbyteArray _data = reinterpret_cast<jbyteArray>(env->GetObjectField(obj, data_id));
  25.705 +            size_t size = (size_t) env->GetArrayLength(_data);
  25.706 +            char *b = (char *) malloc(size);
  25.707 +            assert(b);
  25.708 +
  25.709 +            env->GetByteArrayRegion(_data, 0, size, (jbyte*)b);
  25.710 +            bloblist_t *bl = new_bloblist( b, size, mime_type, filename);
  25.711 +
  25.712 +            free(mime_type);
  25.713 +            free(filename);
  25.714 +            return bl;
  25.715 +        }
  25.716 +
  25.717 +
  25.718 +        bloblist_t *to_bloblist(JNIEnv *env, jobject obj)
  25.719 +        {
  25.720 +            if (!obj)
  25.721 +                return NULL;
  25.722 +
  25.723 +            jint size = callIntMethod(env, obj, "size");
  25.724 +            if (size == 0)
  25.725 +                return NULL;
  25.726 +
  25.727 +            bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL);
  25.728 +            bloblist_t *_bl;
  25.729 +            _bl = bl;
  25.730 +            jint i;
  25.731 +            for (i = 0; i < (int) size; i++) {
  25.732 +                jobject o = callObjectMethod(env, obj, "get", i);
  25.733 +                bloblist_t *b = to_blob(env, o);
  25.734 +                _bl = bloblist_add(_bl, b->value, b->size, b->mime_type, b->filename);
  25.735 +                env->DeleteLocalRef(o);
  25.736 +            }
  25.737 +
  25.738 +            return bl;
  25.739 +        }
  25.740 +
  25.741 +        PEP_enc_format to_EncFormat(JNIEnv *env, jobject obj)
  25.742 +        {
  25.743 +            static const char *classname = "foundation/pEp/jniadapter/Message$EncFormat";
  25.744 +            jclass clazz_enc_format = findClass(env, classname);
  25.745 +            jfieldID field_value = env->GetFieldID(clazz_enc_format, "value", "I");
  25.746 +            assert(field_value);
  25.747 +
  25.748 +            env->DeleteLocalRef(clazz_enc_format);
  25.749 +            return (PEP_enc_format) env->GetIntField(obj, field_value);
  25.750 +        }
  25.751 +
  25.752 +        PEP_CIPHER_SUITE to_CipherSuite(JNIEnv *env, jobject obj)
  25.753 +        {
  25.754 +            static const char *classname = "foundation/pEp/jniadapter/CipherSuite";
  25.755 +            jclass clazz_enc_format = findClass(env, classname);
  25.756 +            jfieldID field_value = env->GetFieldID(clazz_enc_format, "value", "I");
  25.757 +            assert(field_value);
  25.758 +
  25.759 +            env->DeleteLocalRef(clazz_enc_format);
  25.760 +            return (PEP_CIPHER_SUITE) env->GetIntField(obj, field_value);
  25.761 +        }
  25.762 +
  25.763 +        sync_handshake_result to_SyncHandshakeResult(JNIEnv *env, jobject obj)
  25.764 +        {
  25.765 +            static const char *classname = "foundation/pEp/jniadapter/SyncHandshakeResult";
  25.766 +            jclass clazz_enc_format = findClass(env, classname);
  25.767 +            jfieldID field_value = env->GetFieldID(clazz_enc_format, "value", "I");
  25.768 +            assert(field_value);
  25.769 +
  25.770 +            env->DeleteLocalRef(clazz_enc_format);
  25.771 +            return (sync_handshake_result) env->GetIntField(obj, field_value);
  25.772 +        }
  25.773 +    };
  25.774 +};
  25.775 +
    26.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    26.2 +++ b/src/cxx/jniutils.hh	Fri Aug 28 18:18:26 2020 +0200
    26.3 @@ -0,0 +1,130 @@
    26.4 +#pragma once
    26.5 +#include <unordered_map>
    26.6 +#include <thread>
    26.7 +#include <mutex>
    26.8 +#include <jni.h>
    26.9 +#include <pEp/stringpair.h>
   26.10 +#include <pEp/identity_list.h>
   26.11 +#include <pEp/bloblist.h>
   26.12 +#include <pEp/message.h>
   26.13 +#include <pEp/sync_api.h>
   26.14 +#include <pEp/passphrase_cache.hh>
   26.15 +
   26.16 +#if 0 // Enable if log needed
   26.17 +#include <android/log.h>
   26.18 +#define  LOG_TAG    "pEpJNIAdapter"
   26.19 +#define  LOGD(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
   26.20 +#else
   26.21 +#define  LOGD(...) do{}while(0)
   26.22 +#endif
   26.23 +
   26.24 +namespace pEp {
   26.25 +    namespace JNIAdapter {
   26.26 +        // Global mutex needs to be locked in all constructors which insert their own mutex object
   26.27 +        // into the unordered_map (which is thread safe for read, but not for write)
   26.28 +        extern std::mutex global_mutex;
   26.29 +
   26.30 +        // Stores mutex per java object
   26.31 +        extern std::unordered_map<long, std::mutex*> engine_objid_mutex;
   26.32 +
   26.33 +        // needs to be called after create_engine_java_object_mutex()
   26.34 +        // and before release_engine_java_object_mutex()
   26.35 +        // Thread safe
   26.36 +        std::mutex* get_engine_java_object_mutex(
   26.37 +                JNIEnv *env,
   26.38 +                jobject me
   26.39 +            );
   26.40 +
   26.41 +        // Needs to be called exactly once per obj, in the constructor of the obj
   26.42 +        // You need to lock a global mutex before calling this function (write to unordered_map)
   26.43 +        void create_engine_java_object_mutex(
   26.44 +                JNIEnv *env,
   26.45 +                jobject me
   26.46 +            );
   26.47 +
   26.48 +        // Needs to be called exactly once per obj, in the destructor of this obj
   26.49 +        // You need to lock a global mutex before calling this function (write to unordered_map)
   26.50 +        void release_engine_java_object_mutex(
   26.51 +                JNIEnv *env,
   26.52 +                jobject me
   26.53 +            );
   26.54 +
   26.55 +
   26.56 +        jclass findClass(JNIEnv *env, const char *classname);
   26.57 +
   26.58 +        jfieldID getFieldID(
   26.59 +                JNIEnv *env,
   26.60 +                const char *classname,
   26.61 +                const char *fieldname,
   26.62 +                const char *signature
   26.63 +            );
   26.64 +
   26.65 +            jfieldID getFieldID(
   26.66 +                JNIEnv *env,
   26.67 +                const char *classname,
   26.68 +                const char *fieldname,
   26.69 +                const char *signature,
   26.70 +                const jclass clazz
   26.71 +            );
   26.72 +
   26.73 +        jint callIntMethod(
   26.74 +                JNIEnv *env,
   26.75 +                jobject obj,
   26.76 +                const char *methodname
   26.77 +            );
   26.78 +
   26.79 +        jlong callLongMethod(
   26.80 +                JNIEnv *env,
   26.81 +                jobject obj,
   26.82 +                const char *methodname
   26.83 +            );
   26.84 +
   26.85 +        jobject callObjectMethod(
   26.86 +                JNIEnv *env,
   26.87 +                jobject obj,
   26.88 +                const char *methodname,
   26.89 +                jint index
   26.90 +            );
   26.91 +
   26.92 +        jboolean callBooleanMethod(
   26.93 +                JNIEnv *env,
   26.94 +                jobject obj,
   26.95 +                const char *methodname,
   26.96 +                jobject o
   26.97 +            );
   26.98 +
   26.99 +        jint outOfMemory(JNIEnv *env);
  26.100 +
  26.101 +        jobject from_Integer(JNIEnv *env, int val);
  26.102 +        int to_Integer(JNIEnv *env, jobject obj);
  26.103 +
  26.104 +        jbyteArray from_string(JNIEnv *env, const char *str);
  26.105 +        char *to_string(JNIEnv *env, jbyteArray str);
  26.106 +
  26.107 +        jobject from_stringlist(JNIEnv *env, stringlist_t *sl);
  26.108 +        stringlist_t *to_stringlist(JNIEnv *env, jobject obj);
  26.109 +
  26.110 +        jobject from_stringpairlist(JNIEnv *env, stringpair_list_t *sl);
  26.111 +        stringpair_list_t *to_stringpairlist(JNIEnv *env, jobject obj);
  26.112 +
  26.113 +        jobject from_timestamp(JNIEnv *env, timestamp *ts);
  26.114 +        timestamp *to_timestamp(JNIEnv *env, jobject date);
  26.115 +
  26.116 +        jobject from_identity(JNIEnv *env, pEp_identity *ident);
  26.117 +        jobject from_identity(JNIEnv *env, pEp_identity *ident, jclass identityClass);
  26.118 +        pEp_identity *to_identity(JNIEnv *env, jobject obj);
  26.119 +
  26.120 +        jobject from_identitylist(JNIEnv *env, identity_list *il);
  26.121 +        identity_list *to_identitylist(JNIEnv *env, jobject obj);
  26.122 +
  26.123 +        jobject from_bloblist(JNIEnv *env, bloblist_t *bl);
  26.124 +        bloblist_t *to_blob(JNIEnv *env, jobject obj);
  26.125 +        bloblist_t *to_bloblist(JNIEnv *env, jobject obj);
  26.126 +
  26.127 +        PEP_enc_format to_EncFormat(JNIEnv *env, jobject obj);
  26.128 +        PEP_CIPHER_SUITE to_CipherSuite(JNIEnv *env, jobject obj);
  26.129 +
  26.130 +        sync_handshake_result to_SyncHandshakeResult(JNIEnv *env, jobject obj);
  26.131 +    };
  26.132 +};
  26.133 +
    27.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    27.2 +++ b/src/cxx/passphrase_callback.hh	Fri Aug 28 18:18:26 2020 +0200
    27.3 @@ -0,0 +1,14 @@
    27.4 +#pragma once
    27.5 +#include <pEp/passphrase_cache.hh>
    27.6 +
    27.7 +
    27.8 +namespace pEp {
    27.9 +    namespace JNIAdapter {
   27.10 +
   27.11 +        char* passphraseRequiredCallback(const PEP_STATUS status);
   27.12 +
   27.13 +        template<typename... A> PEP_STATUS passphraseWrap(PEP_STATUS f(PEP_SESSION, A...), PEP_SESSION session, A... a);
   27.14 +    }
   27.15 +}
   27.16 +
   27.17 +#include "passphrase_callback.hxx"
   27.18 \ No newline at end of file
    28.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    28.2 +++ b/src/cxx/passphrase_callback.hxx	Fri Aug 28 18:18:26 2020 +0200
    28.3 @@ -0,0 +1,42 @@
    28.4 +#pragma once
    28.5 +
    28.6 +#include "passphrase_callback.hh"
    28.7 +
    28.8 +namespace pEp {
    28.9 +    namespace JNIAdapter {
   28.10 +
   28.11 +        template<typename... A> PEP_STATUS passphraseWrap(PEP_STATUS f(PEP_SESSION, A...), PEP_SESSION session, A... a) {
   28.12 +            pEpLog("cached passphrase mode");
   28.13 +            bool retryAgain = false;
   28.14 +            int maxRetries = 3;
   28.15 +            int retryCount = 0;
   28.16 +            PEP_STATUS status;
   28.17 +            do {
   28.18 +                // the actual target function
   28.19 +                pEpLog("calling passphrase_cache.api from basic_api");
   28.20 +                status = passphrase_cache.api(f, session, a...);
   28.21 +                pEpLog("PEP_STATUS:" << status);
   28.22 +                if (status == PEP_PASSPHRASE_REQUIRED ||
   28.23 +                    status == PEP_WRONG_PASSPHRASE ||
   28.24 +                    status == PEP_PASSPHRASE_FOR_NEW_KEYS_REQUIRED)
   28.25 +                {
   28.26 +                    pEpLog("none of the cached passphrases worked");
   28.27 +                    if (retryCount < maxRetries) {
   28.28 +                        // call the app
   28.29 +                        char *_passphrase = passphraseRequiredCallback(status);
   28.30 +                        pEpLog("callback returned, config_passphrase() with new passphrase");
   28.31 +                        PEP_STATUS status = ::config_passphrase(session, passphrase_cache.add(_passphrase));
   28.32 +                        retryAgain = true;
   28.33 +                        retryCount++;
   28.34 +                    } else {
   28.35 +                        pEpLog("max retries reached:" << maxRetries);
   28.36 +                        retryAgain = false;
   28.37 +                    }
   28.38 +                } else {
   28.39 +                    retryAgain = false;
   28.40 +                }
   28.41 +            } while (retryAgain);
   28.42 +            return status;
   28.43 +        }
   28.44 +    }
   28.45 +}
   28.46 \ No newline at end of file
    29.1 --- a/src/foundation/pEp/jniadapter/AbstractEngine.java	Tue Aug 18 21:57:06 2020 +0200
    29.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    29.3 @@ -1,269 +0,0 @@
    29.4 -package foundation.pEp.jniadapter;
    29.5 -
    29.6 -import java.util.ArrayList;
    29.7 -import java.util.Vector;
    29.8 -
    29.9 -import foundation.pEp.jniadapter.Sync.DefaultCallback;
   29.10 -
   29.11 -import java.io.UnsupportedEncodingException;
   29.12 -import java.text.Normalizer;
   29.13 -
   29.14 -abstract class AbstractEngine extends UniquelyIdentifiable implements AutoCloseable {
   29.15 -    static {
   29.16 -        System.loadLibrary("pEpJNI");
   29.17 -    }
   29.18 -
   29.19 -    private Sync.MessageToSendCallback messageToSendCallback;
   29.20 -    private Sync.NotifyHandshakeCallback notifyHandshakeCallback;
   29.21 -    private Sync.NeedsFastPollCallback needsFastPollCallback;
   29.22 -    private Sync.PassphraseRequiredCallback passphraseRequiredCallback;
   29.23 -
   29.24 -    private final static DefaultCallback defaultCallback = new DefaultCallback();
   29.25 -
   29.26 -    private native void init();
   29.27 -    private native void release();
   29.28 -
   29.29 -    public AbstractEngine() throws pEpException {
   29.30 -        synchronized (AbstractEngine.class) {
   29.31 -            init();
   29.32 -        }
   29.33 -    }
   29.34 -
   29.35 -    final public void close() {
   29.36 -        synchronized (AbstractEngine.class){
   29.37 -            release();
   29.38 -        }
   29.39 -    }
   29.40 -
   29.41 -    public native String getVersion();
   29.42 -    public native String getProtocolVersion();
   29.43 -
   29.44 -    private long keyserverThread;
   29.45 -    private long keyserverQueue;
   29.46 -
   29.47 -    public native void startKeyserverLookup();
   29.48 -    public native void stopKeyserverLookup();
   29.49 -
   29.50 -    public native void startSync();
   29.51 -    public native void stopSync();
   29.52 -    public native boolean isSyncRunning();
   29.53 -
   29.54 -    public static byte[] toUTF8(String str) {
   29.55 -        if (str == null)
   29.56 -            return null;
   29.57 -
   29.58 -        try {
   29.59 -            String _str = Normalizer.normalize(str, Normalizer.Form.NFC);
   29.60 -            byte _buf[] = _str.getBytes("UTF-8");
   29.61 -            byte _cpy[] = new byte[_buf.length];
   29.62 -            System.arraycopy(_buf,0,_cpy,0,_buf.length);
   29.63 -            return _cpy;
   29.64 -        }
   29.65 -        catch (UnsupportedEncodingException e) {
   29.66 -            assert false;
   29.67 -            return new byte[0];
   29.68 -        }
   29.69 -    }
   29.70 -
   29.71 -    public static Vector<byte[]> toUTF8(Vector<String> list) {
   29.72 -        if (list == null)
   29.73 -            return null;
   29.74 -
   29.75 -        Vector<byte[]> result = new Vector<byte[]>(list.size());
   29.76 -
   29.77 -        for (int i=0; i<list.size(); i++)
   29.78 -            result.add(toUTF8(list.get(i)));
   29.79 -
   29.80 -        return result;
   29.81 -    }
   29.82 -
   29.83 -    public static Pair<byte[], byte[]> toUTF8(Pair<String, String> pair) {
   29.84 -        if (pair == null)
   29.85 -            return null;
   29.86 -
   29.87 -        Pair<byte[], byte[]> result = new Pair<byte[], byte[]>();
   29.88 -
   29.89 -        result.first = toUTF8(pair.first);
   29.90 -        result.second = toUTF8(pair.second);
   29.91 -
   29.92 -        return result;
   29.93 -    }
   29.94 -
   29.95 -    public static ArrayList<Pair<byte[], byte[]>> toUTF8(ArrayList<Pair<String, String>> list) {
   29.96 -        if (list == null)
   29.97 -            return null;
   29.98 -
   29.99 -        ArrayList<Pair<byte[], byte[]>> result = new ArrayList<Pair<byte[], byte[]>>(list.size());
  29.100 -
  29.101 -        for (int i=0; i<list.size(); i++)
  29.102 -            result.set(i, toUTF8(list.get(i)));
  29.103 -
  29.104 -        return result;
  29.105 -    }
  29.106 -
  29.107 -    public static String toUTF16(byte[] utf8) {
  29.108 -        if (utf8 == null)
  29.109 -            return null;
  29.110 -
  29.111 -        try {
  29.112 -            byte newUtf8[] = new byte[utf8.length];
  29.113 -            System.arraycopy(utf8,0,newUtf8,0,utf8.length);
  29.114 -
  29.115 -            return new String(newUtf8, "UTF-8");
  29.116 -        }
  29.117 -        catch (UnsupportedEncodingException e) {
  29.118 -            assert false;
  29.119 -            return new String();
  29.120 -        }
  29.121 -    }
  29.122 -
  29.123 -    public static Vector<String> toUTF16(Vector<byte[]> list) {
  29.124 -        if (list == null)
  29.125 -            return null;
  29.126 -
  29.127 -        Vector<String> result = new Vector<String>(list.size());
  29.128 -
  29.129 -        for (int i=0; i<list.size(); i++)
  29.130 -            result.add(toUTF16(list.get(i)));
  29.131 -
  29.132 -        return result;
  29.133 -    }
  29.134 -
  29.135 -    public static Pair<String, String> toUTF16(Pair<byte[], byte[]> pair) {
  29.136 -        if (pair == null)
  29.137 -            return null;
  29.138 -
  29.139 -        Pair<String, String> result = new Pair<String,String>();
  29.140 -
  29.141 -        result.first = toUTF16(pair.first);
  29.142 -        result.second = toUTF16(pair.second);
  29.143 -
  29.144 -        return result;
  29.145 -    }
  29.146 -
  29.147 -    public static ArrayList<Pair<String, String>> toUTF16(ArrayList<Pair<byte[], byte[]>> list) {
  29.148 -        if (list == null)
  29.149 -            return null;
  29.150 -
  29.151 -        ArrayList<Pair<String, String>> result = new ArrayList<Pair<String, String>>(list.size());
  29.152 -
  29.153 -        for (int i=0; i<list.size(); i++)
  29.154 -            result.set(i, toUTF16(list.get(i)));
  29.155 -
  29.156 -        return result;
  29.157 -    }
  29.158 -
  29.159 -    public void setMessageToSendCallback(Sync.MessageToSendCallback messageToSendCallback) {
  29.160 -        this.messageToSendCallback = messageToSendCallback;
  29.161 -    }
  29.162 -
  29.163 -    public void setNotifyHandshakeCallback(Sync.NotifyHandshakeCallback notifyHandshakeCallback) {
  29.164 -        this.notifyHandshakeCallback = notifyHandshakeCallback;
  29.165 -    }
  29.166 -
  29.167 -    public void setNeedsFastPollCallback(Sync.NeedsFastPollCallback needsFastPollCallback) {
  29.168 -        this.needsFastPollCallback = needsFastPollCallback;
  29.169 -    }
  29.170 -
  29.171 -    public void setPassphraseRequiredCallback(Sync.PassphraseRequiredCallback passphraseRequiredCallback) {
  29.172 -        System.out.println("passphraseRequiredCallback has been registered to:" + passphraseRequiredCallback.toString() + " on engine ObjID: " + getId());
  29.173 -
  29.174 -        this.passphraseRequiredCallback = passphraseRequiredCallback;
  29.175 -    }
  29.176 -
  29.177 -    public int needsFastPollCallFromC(boolean fast_poll_needed) {
  29.178 -        if (needsFastPollCallback != null) {
  29.179 -            needsFastPollCallback.needsFastPollCallFromC(fast_poll_needed);
  29.180 -        } else {
  29.181 -            defaultCallback.needsFastPollCallFromC(fast_poll_needed);
  29.182 -        }
  29.183 -        return 0;
  29.184 -    }
  29.185 -
  29.186 -    public int notifyHandshakeCallFromC(_Identity _myself, _Identity _partner, SyncHandshakeSignal _signal) {
  29.187 -        Identity myself = new Identity(_myself);
  29.188 -        Identity partner = (_partner != null) ? new Identity(_partner) : null;
  29.189 -
  29.190 -        System.out.println("pEpSync" +"notifyHandshakeCallFromC: " + notifyHandshakeCallback);
  29.191 -        if (notifyHandshakeCallback != null) {
  29.192 -            notifyHandshakeCallback.notifyHandshake(myself, partner, _signal);
  29.193 -        } else {
  29.194 -            defaultCallback.notifyHandshake(myself, partner, _signal);
  29.195 -        }
  29.196 -        return 0;
  29.197 -    }
  29.198 -
  29.199 -    public byte[] passphraseRequiredFromC(final PassphraseType passphraseType) {
  29.200 -        String ret = "";
  29.201 -        if (passphraseRequiredCallback != null) {
  29.202 -            System.out.println("calling passphraseRequiredCallback on engine ObjID:" + getId());
  29.203 -            ret = passphraseRequiredCallback.passphraseRequired(passphraseType);
  29.204 -        } else {
  29.205 -            System.out.println("no callback registered on engine ObjID:" + getId());
  29.206 -            // if this happens (no callback registered
  29.207 -            // we simply return ""
  29.208 -            // it will fail
  29.209 -            // this repeats MaxRetries times (currentluy hardcoded to 3)
  29.210 -            // Then the orig call will return with the PEP_STATUS (most likely PEP_PASSPHRASE_REQUIRED)
  29.211 -        }
  29.212 -        return toUTF8(ret);
  29.213 -    }
  29.214 -
  29.215 -    public int messageToSendCallFromC (Message message) {
  29.216 -        System.out.println("pEpSync" + "messageToSendCallFromC: " + messageToSendCallback );
  29.217 -        if (messageToSendCallback != null) {
  29.218 -            messageToSendCallback.messageToSend(message);
  29.219 -        } else {
  29.220 -            defaultCallback.messageToSend(message);
  29.221 -        }
  29.222 -        return 0;
  29.223 -    }
  29.224 -
  29.225 -    public Message incomingMessageFromPGPText(String pgpText, Message.EncFormat encFormat) {
  29.226 -        Message msg = new Message();
  29.227 -        msg.setDir(Message.Direction.Incoming);
  29.228 -        msg.setEncFormat(encFormat);
  29.229 -
  29.230 -        // Opts
  29.231 -        ArrayList<Pair<String, String>> opts = new ArrayList<>();
  29.232 -        Pair<String, String> xpEp = new Pair<>();
  29.233 -        xpEp.first = "X-pEp-Version";
  29.234 -        xpEp.second = this.getProtocolVersion();;
  29.235 -        opts.add(xpEp);
  29.236 -        msg.setOptFields(opts);
  29.237 -
  29.238 -        if(encFormat == Message.EncFormat.PEP) {
  29.239 -            // For EncFormat.PEP
  29.240 -            // The pgpText goes into the attachment index 1
  29.241 -            msg.setShortmsg("p≡p");
  29.242 -            msg.setLongmsg("this message was encrypted with p≡p https://pEp-project.org");
  29.243 -
  29.244 -            // Attachments
  29.245 -            Blob att0 = new Blob();
  29.246 -            att0.mime_type = "application/pgp-encrypted";
  29.247 -            att0.filename = null;
  29.248 -            att0.data = "Version: 1".getBytes();
  29.249 -
  29.250 -            Blob att1 = new Blob();
  29.251 -            att1.mime_type = "application/octet-stream";
  29.252 -            att1.filename = "file://msg.asc";
  29.253 -            att1.data = pgpText.getBytes();
  29.254 -
  29.255 -            Vector<Blob> attachments = new Vector<>();
  29.256 -            attachments.add(att0);
  29.257 -            attachments.add(att1);
  29.258 -            msg.setAttachments(attachments);
  29.259 -        }
  29.260 -        else if (encFormat == Message.EncFormat.PEPEncInlineEA) {
  29.261 -            // For EncFormat.PEPEncInlineEA
  29.262 -            // The pgpText goes into the longMessage
  29.263 -            msg.setShortmsg("");
  29.264 -            msg.setLongmsg(pgpText);
  29.265 -        }
  29.266 -        else {
  29.267 -            throw new pEpCannotEncode("Message.Encformat not supported: " + encFormat.toString());
  29.268 -        }
  29.269 -
  29.270 -        return msg;
  29.271 -    }
  29.272 -}
  29.273 \ No newline at end of file
    30.1 --- a/src/foundation/pEp/jniadapter/Blob.java	Tue Aug 18 21:57:06 2020 +0200
    30.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    30.3 @@ -1,36 +0,0 @@
    30.4 -package foundation.pEp.jniadapter;
    30.5 -
    30.6 -import java.io.Serializable;
    30.7 -
    30.8 -public class Blob implements Serializable {
    30.9 -    public byte[] data;
   30.10 -    public String mime_type;
   30.11 -    public String filename;
   30.12 -
   30.13 -    public Blob() {
   30.14 -        mime_type = "application/octet-stream";
   30.15 -    }
   30.16 -
   30.17 -    /** Human readable string representation of Blob.
   30.18 -     * The data field is ASN.1 XER decoded for mime_types:
   30.19 -     * "application/pEp.sync"
   30.20 -     * "application/pEp.keyreset"
   30.21 -     * @return String Blob as String
   30.22 -     */
   30.23 -    public String toString() {
   30.24 -        _Blob _b = new _Blob(this);
   30.25 -        String ret = "";
   30.26 -        ret += "mime_type: \"" + mime_type + "\"\n";
   30.27 -        ret += "filename: \"" + filename + "\"\n";
   30.28 -        ret += "data plain: \"" + AbstractEngine.toUTF16(data) + "\"\n";
   30.29 -        ret += "data decoded: \"" + _b.dataToXER() + "\"\n";
   30.30 -        return ret;
   30.31 -    }
   30.32 -
   30.33 -    Blob(_Blob b) {
   30.34 -        data = b.data;
   30.35 -        mime_type = AbstractEngine.toUTF16(b.mime_type);
   30.36 -        filename = AbstractEngine.toUTF16(b.filename);
   30.37 -    }
   30.38 -}
   30.39 -
    31.1 --- a/src/foundation/pEp/jniadapter/CommType.java	Tue Aug 18 21:57:06 2020 +0200
    31.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    31.3 @@ -1,80 +0,0 @@
    31.4 -package foundation.pEp.jniadapter;
    31.5 -
    31.6 -import java.util.HashMap;
    31.7 -
    31.8 -public enum CommType {
    31.9 -    PEP_ct_unknown (0),
   31.10 -
   31.11 -    // range 0x01 to 0x09: no encryption, 0x0a to 0x0e: nothing reasonable
   31.12 -
   31.13 -    PEP_ct_no_encryption (0x01),                // generic
   31.14 -    PEP_ct_no_encrypted_channel (0x02),
   31.15 -    PEP_ct_key_not_found (0x03),
   31.16 -    PEP_ct_key_expired (0x04),
   31.17 -    PEP_ct_key_revoked (0x05),
   31.18 -    PEP_ct_key_b0rken (0x06),
   31.19 -    PEP_ct_my_key_not_included (0x09),
   31.20 -
   31.21 -    PEP_ct_security_by_obscurity (0x0a),
   31.22 -    PEP_ct_b0rken_crypto (0x0b),
   31.23 -    PEP_ct_key_too_short (0x0c),
   31.24 -
   31.25 -    PEP_ct_compromized (0x0e),                  // known compromized connection
   31.26 -    PEP_ct_mistrusted (0x0f),                   // known mistrusted key
   31.27 -
   31.28 -    // range 0x10 to 0x3f: unconfirmed encryption
   31.29 -
   31.30 -    PEP_ct_unconfirmed_encryption (0x10),       // generic
   31.31 -    PEP_ct_OpenPGP_weak_unconfirmed (0x11),	    // RSA 1024 is weak
   31.32 -
   31.33 -    PEP_ct_to_be_checked (0x20),                // generic
   31.34 -    PEP_ct_SMIME_unconfirmed (0x21),
   31.35 -    PEP_ct_CMS_unconfirmed (0x22),
   31.36 -
   31.37 -    PEP_ct_strong_but_unconfirmed (0x30),       // generic
   31.38 -    PEP_ct_OpenPGP_unconfirmed (0x38),          // key at least 2048 bit RSA or EC
   31.39 -    PEP_ct_OTR_unconfirmed (0x3a),
   31.40 -
   31.41 -    // range 0x40 to 0x7f: unconfirmed encryption and anonymization
   31.42 -
   31.43 -    PEP_ct_unconfirmed_enc_anon (0x40),         // generic
   31.44 -    PEP_ct_PEP_unconfirmed (0x7f),
   31.45 -
   31.46 -    PEP_ct_confirmed (0x80),                    // this bit decides if trust is confirmed
   31.47 -
   31.48 -    // range 0x81 to 0x8f: reserved
   31.49 -    // range 0x90 to 0xbf: confirmed encryption
   31.50 -
   31.51 -    PEP_ct_confirmed_encryption (0x90),         // generic
   31.52 -	PEP_ct_OpenPGP_weak (0x91),                 // RSA 1024 is weak
   31.53 -
   31.54 -    PEP_ct_to_be_checked_confirmed (0xa0),      //generic
   31.55 -    PEP_ct_SMIME (0xa1),
   31.56 -    PEP_ct_CMS (0xa2),
   31.57 -
   31.58 -    PEP_ct_strong_encryption (0xb0),            // generic
   31.59 -	PEP_ct_OpenPGP (0xb8),                      // key at least 2048 bit RSA or EC
   31.60 -	PEP_ct_OTR (0xba),
   31.61 -
   31.62 -    // range 0xc0 to 0xff: confirmed encryption and anonymization
   31.63 -
   31.64 -    PEP_ct_confirmed_enc_anon (0xc0),           // generic
   31.65 -	PEP_ct_pEp (0xff);
   31.66 -
   31.67 -    static class Management {
   31.68 -        public static final HashMap<Integer, CommType> tag =
   31.69 -                new HashMap<Integer, CommType>();
   31.70 -    }
   31.71 -
   31.72 -    public final int value;
   31.73 -
   31.74 -    CommType(int value) {
   31.75 -        this.value = value;
   31.76 -        fill(value);
   31.77 -    }
   31.78 -
   31.79 -    private void fill(int value) {
   31.80 -        Management.tag.put(value, this);
   31.81 -    }
   31.82 -}
   31.83 -
    32.1 --- a/src/foundation/pEp/jniadapter/Engine.java.target	Tue Aug 18 21:57:06 2020 +0200
    32.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    32.3 @@ -1,28 +0,0 @@
    32.4 -package foundation.pEp.jniadapter;
    32.5 -
    32.6 -import java.util.ArrayList;
    32.7 -import java.util.Vector;
    32.8 -
    32.9 -final public class Engine extends AbstractEngine {
   32.10 -    public Engine() throws pEpException { }
   32.11 -
   32.12 -    public native Message encrypt_message(
   32.13 -            Message src,
   32.14 -            ArrayList<String> extra,
   32.15 -            Message.EncFormat enc_format
   32.16 -        ) throws pEpException;
   32.17 -
   32.18 -    public class DecryptResult {
   32.19 -        public Message dst;
   32.20 -        public Color color;
   32.21 -    }
   32.22 -
   32.23 -    public native DecryptResult decrypt_message(
   32.24 -            Message src,
   32.25 -            ArrayList<String> keylist
   32.26 -        ) throws pEpException;
   32.27 -
   32.28 -    public native Color outgoing_message_color(
   32.29 -            Message msg
   32.30 -        ) throws pEpException;
   32.31 -}
    33.1 --- a/src/foundation/pEp/jniadapter/Identity.java	Tue Aug 18 21:57:06 2020 +0200
    33.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    33.3 @@ -1,50 +0,0 @@
    33.4 -package foundation.pEp.jniadapter;
    33.5 -
    33.6 -import java.io.Serializable;
    33.7 -
    33.8 -public class Identity implements Serializable{
    33.9 -    public String address;
   33.10 -    public String fpr;
   33.11 -    public String user_id;
   33.12 -    public String username;
   33.13 -    public CommType comm_type;
   33.14 -    public String lang;
   33.15 -    public boolean me;
   33.16 -    public int flags;
   33.17 -
   33.18 -    public Identity() {
   33.19 -        this.me = false;
   33.20 -        comm_type = CommType.PEP_ct_unknown;
   33.21 -    }
   33.22 -
   33.23 -    public Identity(boolean me) {
   33.24 -        this.me = me;
   33.25 -        comm_type = CommType.PEP_ct_unknown;
   33.26 -    }
   33.27 -
   33.28 -    public Identity(_Identity i) {
   33.29 -        address = AbstractEngine.toUTF16(i.address);
   33.30 -        fpr = AbstractEngine.toUTF16(i.fpr);
   33.31 -        user_id = AbstractEngine.toUTF16(i.user_id);
   33.32 -        username = AbstractEngine.toUTF16(i.username);
   33.33 -        comm_type = CommType.Management.tag.get(i.comm_type);
   33.34 -        lang = AbstractEngine.toUTF16(i.lang);
   33.35 -        me = i.me;
   33.36 -        flags = i.flags;
   33.37 -    }
   33.38 -
   33.39 -    @Override
   33.40 -    public String toString() {
   33.41 -        return address + "::" + username + "\n" +
   33.42 -                user_id + "::" + fpr;
   33.43 -    }
   33.44 -
   33.45 -    // Native
   33.46 -    private native int _getRating(int commType);
   33.47 -
   33.48 -    public Rating getRating() {
   33.49 -        return Rating.getByInt(_getRating(comm_type.value));
   33.50 -    }
   33.51 -
   33.52 -}
   33.53 -
    34.1 --- a/src/foundation/pEp/jniadapter/Message.java.target	Tue Aug 18 21:57:06 2020 +0200
    34.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    34.3 @@ -1,218 +0,0 @@
    34.4 -package foundation.pEp.jniadapter;
    34.5 -
    34.6 -import java.util.ArrayList;
    34.7 -import java.util.Vector;
    34.8 -import java.util.Date;
    34.9 -import java.util.HashMap;
   34.10 -
   34.11 -public class Message implements AutoCloseable {
   34.12 -    private final long handle;
   34.13 -
   34.14 -    native long init();
   34.15 -    native void release(long handle);
   34.16 -
   34.17 -    public Message() {
   34.18 -        handle = init();
   34.19 -    }
   34.20 -
   34.21 -    public final void close() {
   34.22 -        release(handle);
   34.23 -    }
   34.24 -
   34.25 -    public enum TextFormat {
   34.26 -        Plain (0), 
   34.27 -        Html (1), 
   34.28 -        Other (255)
   34.29 -        ;
   34.30 -    
   34.31 -        static class Management {
   34.32 -            public static final HashMap<Integer, TextFormat> tag =
   34.33 -                    new HashMap<Integer, TextFormat>();
   34.34 -        }
   34.35 -    
   34.36 -        public final int value;
   34.37 -    
   34.38 -        TextFormat(int value) {
   34.39 -            this.value = value;
   34.40 -            fill(value);
   34.41 -        }
   34.42 -    
   34.43 -        private void fill(int value) {
   34.44 -            Management.tag.put(value, this);
   34.45 -        }
   34.46 -    }
   34.47 -    
   34.48 -    public enum Direction {
   34.49 -        Incoming (0), 
   34.50 -        Outgoing (1)
   34.51 -        ;
   34.52 -    
   34.53 -        static class Management {
   34.54 -            public static final HashMap<Integer, Direction> tag =
   34.55 -                    new HashMap<Integer, Direction>();
   34.56 -        }
   34.57 -    
   34.58 -        public final int value;
   34.59 -    
   34.60 -        Direction(int value) {
   34.61 -            this.value = value;
   34.62 -            fill(value);
   34.63 -        }
   34.64 -    
   34.65 -        private void fill(int value) {
   34.66 -            Management.tag.put(value, this);
   34.67 -        }
   34.68 -    }
   34.69 -    
   34.70 -    public enum EncFormat {
   34.71 -        None (0), 
   34.72 -        Pieces (1), 
   34.73 -        SMIME (2), 
   34.74 -        PGPMIME (3), 
   34.75 -        PEP (4)
   34.76 -        ;
   34.77 -    
   34.78 -        static class Management {
   34.79 -            public static final HashMap<Integer, EncFormat> tag =
   34.80 -                    new HashMap<Integer, EncFormat>();
   34.81 -        }
   34.82 -    
   34.83 -        public final int value;
   34.84 -    
   34.85 -        EncFormat(int value) {
   34.86 -            this.value = value;
   34.87 -            fill(value);
   34.88 -        }
   34.89 -    
   34.90 -        private void fill(int value) {
   34.91 -            Management.tag.put(value, this);
   34.92 -        }
   34.93 -    }
   34.94 -    
   34.95 -    private native int _getDir();
   34.96 -    private native void _setDir(int value);
   34.97 -    public Direction getDir() {
   34.98 -        return Direction.tag.get(_getDir());
   34.99 -    }
  34.100 -    public void setDir(Direction value) {
  34.101 -        _setDir(value.value);
  34.102 -    }
  34.103 -    
  34.104 -    private native byte[] _getId();
  34.105 -    private native void _setId(byte[] value);
  34.106 -    public String getId() {
  34.107 -        return AbstractEngine.toUTF16(_getId());
  34.108 -    }
  34.109 -    public void setId(String value) {
  34.110 -        _setId(AbstractEngine.toUTF8(value));
  34.111 -    }
  34.112 -    
  34.113 -    private native byte[] _getShortmsg();
  34.114 -    private native void _setShortmsg(byte[] value);
  34.115 -    public String getShortmsg() {
  34.116 -        return AbstractEngine.toUTF16(_getShortmsg());
  34.117 -    }
  34.118 -    public void setShortmsg(String value) {
  34.119 -        _setShortmsg(AbstractEngine.toUTF8(value));
  34.120 -    }
  34.121 -    
  34.122 -    private native byte[] _getLongmsg();
  34.123 -    private native void _setLongmsg(byte[] value);
  34.124 -    public String getLongmsg() {
  34.125 -        return AbstractEngine.toUTF16(_getLongmsg());
  34.126 -    }
  34.127 -    public void setLongmsg(String value) {
  34.128 -        _setLongmsg(AbstractEngine.toUTF8(value));
  34.129 -    }
  34.130 -    
  34.131 -    private native byte[] _getLongmsgFormatted();
  34.132 -    private native void _setLongmsgFormatted(byte[] value);
  34.133 -    public String getLongmsgFormatted() {
  34.134 -        return AbstractEngine.toUTF16(_getLongmsgFormatted());
  34.135 -    }
  34.136 -    public void setLongmsgFormatted(String value) {
  34.137 -        _setLongmsgFormatted(AbstractEngine.toUTF8(value));
  34.138 -    }
  34.139 -    
  34.140 -    public native ArrayList<Blob> getAttachments();
  34.141 -    public native void setAttachments(ArrayList<Blob> value);
  34.142 -    
  34.143 -    public native Date getSent();
  34.144 -    public native void setSent(Date value);
  34.145 -    
  34.146 -    public native Date getRecv();
  34.147 -    public native void setRecv(Date value);
  34.148 -    
  34.149 -    private native AbstractEngine._Identity _getFrom();
  34.150 -    private native void _setFrom(AbstractEngine._Identity value);
  34.151 -    public Identity getFrom() {
  34.152 -        return new _getFrom().getIdentity();
  34.153 -    }
  34.154 -    public void setFrom(Identity value) {
  34.155 -        _setFrom(new AbstractEngine._Identity(value));
  34.156 -    }
  34.157 -    
  34.158 -    public native ArrayList<Identity> getTo();
  34.159 -    public native void setTo(ArrayList<Identity> value);
  34.160 -    
  34.161 -    public native Identity getRecvBy();
  34.162 -    public native void setRecvBy(Identity value);
  34.163 -    
  34.164 -    public native ArrayList<Identity> getCc();
  34.165 -    public native void setCc(ArrayList<Identity> value);
  34.166 -    
  34.167 -    public native ArrayList<Identity> getBcc();
  34.168 -    public native void setBcc(ArrayList<Identity> value);
  34.169 -    
  34.170 -    public native ArrayList<Identity> getReplyTo();
  34.171 -    public native void setReplyTo(ArrayList<Identity> value);
  34.172 -    
  34.173 -    private native ArrayList<byte[]> _getInReplyTo();
  34.174 -    private native void _setInReplyTo(ArrayList<byte[]> value);
  34.175 -    public ArrayList<String> getInReplyTo() {
  34.176 -        return AbstractEngine.toUTF16(_getInReplyTo());
  34.177 -    }
  34.178 -    public void setInReplyTo(ArrayList<String> value) {
  34.179 -        _setInReplyTo(AbstractEngine.toUTF8(value));
  34.180 -    }
  34.181 -    
  34.182 -    private native ArrayList<byte[]> _getReferences();
  34.183 -    private native void _setReferences(ArrayList<byte[]> value);
  34.184 -    public ArrayList<String> getReferences() {
  34.185 -        return AbstractEngine.toUTF16(_getReferences());
  34.186 -    }
  34.187 -    public void setReferences(ArrayList<String> value) {
  34.188 -        _setReferences(AbstractEngine.toUTF8(value));
  34.189 -    }
  34.190 -    
  34.191 -    private native ArrayList<byte[]> _getKeywords();
  34.192 -    private native void _setKeywords(ArrayList<byte[]> value);
  34.193 -    public ArrayList<String> getKeywords() {
  34.194 -        return AbstractEngine.toUTF16(_getKeywords());
  34.195 -    }
  34.196 -    public void setKeywords(ArrayList<String> value) {
  34.197 -        _setKeywords(AbstractEngine.toUTF8(value));
  34.198 -    }
  34.199 -    
  34.200 -    private native byte[] _getComments();
  34.201 -    private native void _setComments(byte[] value);
  34.202 -    public String getComments() {
  34.203 -        return AbstractEngine.toUTF16(_getComments());
  34.204 -    }
  34.205 -    public void setComments(String value) {
  34.206 -        _setComments(AbstractEngine.toUTF8(value));
  34.207 -    }
  34.208 -    
  34.209 -    private native Vector<Pair<byte[], byte[]>> _getOptFields();
  34.210 -    private native void _setOptFields(Vector<Pair<byte[], byte[]>> value);
  34.211 -    public Vector<Pair<String, String>> getOptFields() {
  34.212 -        return AbstractEngine.toUTF16(_getOptFields());
  34.213 -    }
  34.214 -    public void setOptFields(Vector<Pair<String, String>> value) {
  34.215 -        _setOptFields(AbstractEngine.toUTF8(value));
  34.216 -    }
  34.217 -    
  34.218 -    public native Message.EncFormat getEncFormat();
  34.219 -    public native void setEncFormat(Message.EncFormat value);
  34.220 -    
  34.221 -}
    35.1 --- a/src/foundation/pEp/jniadapter/Pair.java	Tue Aug 18 21:57:06 2020 +0200
    35.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    35.3 @@ -1,14 +0,0 @@
    35.4 -package foundation.pEp.jniadapter;
    35.5 -
    35.6 -public class Pair<F, S> {
    35.7 -    public F first;
    35.8 -    public S second;
    35.9 -
   35.10 -    public Pair() { }
   35.11 -
   35.12 -    public Pair(F f, S s) {
   35.13 -        first = f;
   35.14 -        second = s;
   35.15 -    }
   35.16 -}
   35.17 -
    36.1 --- a/src/foundation/pEp/jniadapter/Sync.java	Tue Aug 18 21:57:06 2020 +0200
    36.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    36.3 @@ -1,44 +0,0 @@
    36.4 -package foundation.pEp.jniadapter;
    36.5 -
    36.6 -import foundation.pEp.jniadapter.Identity;
    36.7 -import foundation.pEp.jniadapter.Message;
    36.8 -import foundation.pEp.jniadapter.pEpException;
    36.9 -
   36.10 -public interface Sync {
   36.11 -
   36.12 -     
   36.13 -    interface NeedsFastPollCallback {
   36.14 -        void needsFastPollCallFromC(Boolean fast_poll_needed);
   36.15 -    }
   36.16 -
   36.17 -    interface MessageToSendCallback {
   36.18 -        void messageToSend(Message message);
   36.19 -    }
   36.20 -
   36.21 -    interface NotifyHandshakeCallback {
   36.22 -        void notifyHandshake(Identity myself, Identity partner, SyncHandshakeSignal signal);
   36.23 -    }
   36.24 -
   36.25 -    interface PassphraseRequiredCallback {
   36.26 -        String passphraseRequired(final PassphraseType pEpStatus);
   36.27 -    }
   36.28 -
   36.29 -    public class DefaultCallback 
   36.30 -            implements Sync.MessageToSendCallback, Sync.NotifyHandshakeCallback,  Sync.NeedsFastPollCallback {
   36.31 -        @Override
   36.32 -        public void needsFastPollCallFromC(Boolean fast_poll_needed) {
   36.33 -            System.out.println("Need fast Poll");
   36.34 -        }
   36.35 -
   36.36 -        @Override
   36.37 -        public void messageToSend(Message message) {
   36.38 -            System.out.println("messageToSend Defualt Callback");
   36.39 -        }
   36.40 -        
   36.41 -        @Override
   36.42 -        public void notifyHandshake(Identity myself, Identity partner, SyncHandshakeSignal signal) {
   36.43 -            System.out.println("notifyHandshake Default Callback");
   36.44 -        }
   36.45 -    }
   36.46 -
   36.47 -}
    37.1 --- a/src/foundation/pEp/jniadapter/UniquelyIdentifiable.java	Tue Aug 18 21:57:06 2020 +0200
    37.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    37.3 @@ -1,11 +0,0 @@
    37.4 -package foundation.pEp.jniadapter;
    37.5 -import java.util.concurrent.atomic.AtomicLong;
    37.6 -
    37.7 -abstract class UniquelyIdentifiable {
    37.8 -    static final AtomicLong NEXT_ID = new AtomicLong(1);
    37.9 -    final long id = NEXT_ID.getAndIncrement();
   37.10 -
   37.11 -    public long getId() {
   37.12 -        return id;
   37.13 -    }
   37.14 -}
    38.1 --- a/src/foundation/pEp/jniadapter/_Blob.java	Tue Aug 18 21:57:06 2020 +0200
    38.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    38.3 @@ -1,21 +0,0 @@
    38.4 -package foundation.pEp.jniadapter;
    38.5 -
    38.6 -public class _Blob {
    38.7 -    public byte[] data;
    38.8 -    public byte[] mime_type;
    38.9 -    public byte[] filename;
   38.10 -
   38.11 -    _Blob() { }
   38.12 -
   38.13 -    private native byte[] _dataToXER() throws pEpException;;
   38.14 -    public String dataToXER() {
   38.15 -        return AbstractEngine.toUTF16(_dataToXER());
   38.16 -    }
   38.17 -
   38.18 -    _Blob(Blob b) {
   38.19 -        data = b.data;
   38.20 -        mime_type = AbstractEngine.toUTF8(b.mime_type);
   38.21 -        filename = AbstractEngine.toUTF8(b.filename);
   38.22 -    }
   38.23 -}
   38.24 -
    39.1 --- a/src/foundation/pEp/jniadapter/_Identity.java	Tue Aug 18 21:57:06 2020 +0200
    39.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    39.3 @@ -1,32 +0,0 @@
    39.4 -package foundation.pEp.jniadapter;
    39.5 -
    39.6 -public class _Identity {
    39.7 -    public byte[] address;
    39.8 -    public byte[] fpr;
    39.9 -    public byte[] user_id;
   39.10 -    public byte[] username;
   39.11 -    public int comm_type;
   39.12 -    public byte[] lang;
   39.13 -    public boolean me;
   39.14 -    public int flags;
   39.15 -
   39.16 -    public _Identity() {
   39.17 -        this.me = false;
   39.18 -    }
   39.19 -
   39.20 -    public _Identity(boolean me) {
   39.21 -        this.me = me;
   39.22 -    }
   39.23 -
   39.24 -    public _Identity(Identity i) {
   39.25 -        address = AbstractEngine.toUTF8(i.address);
   39.26 -        fpr = AbstractEngine.toUTF8(i.fpr);
   39.27 -        user_id = AbstractEngine.toUTF8(i.user_id);
   39.28 -        username = AbstractEngine.toUTF8(i.username);
   39.29 -        comm_type = i.comm_type.value;
   39.30 -        lang = AbstractEngine.toUTF8(i.lang);
   39.31 -        me = i.me;
   39.32 -        flags = i.flags;
   39.33 -    }
   39.34 -}
   39.35 -
    40.1 --- a/src/foundation_pEp_jniadapter_AbstractEngine.cc	Tue Aug 18 21:57:06 2020 +0200
    40.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    40.3 @@ -1,478 +0,0 @@
    40.4 -#include "foundation_pEp_jniadapter_AbstractEngine.h"
    40.5 -#include <unistd.h>
    40.6 -#include <pEp/keymanagement.h>
    40.7 -#include <pEp/message_api.h>
    40.8 -#include <pEp/sync_api.h>
    40.9 -#include <pEp/pEpLog.hh>
   40.10 -#include <pEp/passphrase_cache.hh>
   40.11 -#include <pEp/callback_dispatcher.hh>
   40.12 -#include "throw_pEp_exception.hh"
   40.13 -#include "jniutils.hh"
   40.14 -#include "passphrase_callback.hh"
   40.15 -
   40.16 -namespace pEp {
   40.17 -using namespace pEp::JNIAdapter;
   40.18 -using namespace utility;   // for libpEpAdapter locked queue impl.  TODO:rename
   40.19 -
   40.20 -bool first = true;
   40.21 -
   40.22 -JavaVM *jvm= nullptr;
   40.23 -
   40.24 -std::mutex mutex_obj;
   40.25 -
   40.26 -jfieldID signal_field_value = nullptr;
   40.27 -jfieldID passphrase_type_field_value = nullptr;
   40.28 -jmethodID messageConstructorMethodID = nullptr;
   40.29 -jmethodID messageToSendMethodID = nullptr;
   40.30 -jmethodID notifyHandShakeMethodID = nullptr;
   40.31 -jmethodID needsFastPollMethodID = nullptr;
   40.32 -jmethodID passphraseRequiredMethodID = nullptr;
   40.33 -jmethodID sync_handshake_signal_values = nullptr;
   40.34 -jmethodID passphrase_status_values = nullptr;
   40.35 -jmethodID passphrase_callback_values = nullptr;
   40.36 -
   40.37 -jobject objj = nullptr;
   40.38 -
   40.39 -jclass messageClass = nullptr;
   40.40 -jclass identityClass = nullptr;
   40.41 -jclass signalClass = nullptr;
   40.42 -jclass engineClass = nullptr;
   40.43 -jclass passphraseTypeClass = nullptr;
   40.44 -
   40.45 -namespace JNISync {
   40.46 -    JNIEnv* env() {
   40.47 -        JNIEnv *thread_env = nullptr;
   40.48 -        int status = jvm->GetEnv((void**)&thread_env, JNI_VERSION_1_6);
   40.49 -        if (status < 0) {
   40.50 -#ifdef ANDROID
   40.51 -            status = jvm->AttachCurrentThread(&thread_env, nullptr);
   40.52 -#else
   40.53 -            status = jvm->AttachCurrentThread((void **) &thread_env, nullptr);
   40.54 -#endif
   40.55 -        }
   40.56 -        assert(status >= 0);
   40.57 -        return thread_env;
   40.58 -    }
   40.59 -
   40.60 -    void onSyncStartup() {
   40.61 -        pEpLog("called");
   40.62 -        env();
   40.63 -    }
   40.64 -
   40.65 -    void onSyncShutdown() {
   40.66 -        pEpLog("called");
   40.67 -        jvm->DetachCurrentThread();
   40.68 -    }
   40.69 -};
   40.70 -
   40.71 -
   40.72 -void jni_init() {
   40.73 -    JNIEnv *_env = JNISync::env();
   40.74 -
   40.75 -    messageClass = reinterpret_cast<jclass>(
   40.76 -            _env->NewGlobalRef(findClass(_env, "foundation/pEp/jniadapter/Message")));
   40.77 -    identityClass = reinterpret_cast<jclass>(
   40.78 -        _env->NewGlobalRef(findClass(_env, "foundation/pEp/jniadapter/_Identity")));
   40.79 -    signalClass = reinterpret_cast<jclass>(
   40.80 -            _env->NewGlobalRef(findClass(_env, "foundation/pEp/jniadapter/SyncHandshakeSignal")));
   40.81 -    passphraseTypeClass = reinterpret_cast<jclass>(
   40.82 -            _env->NewGlobalRef(findClass(_env, "foundation/pEp/jniadapter/PassphraseType")));
   40.83 -    engineClass = reinterpret_cast<jclass>(_env->NewGlobalRef(findClass(_env, "foundation/pEp/jniadapter/Engine")));
   40.84 -
   40.85 -    messageConstructorMethodID = _env->GetMethodID(messageClass, "<init>", "(J)V");
   40.86 -    messageToSendMethodID = _env->GetMethodID(
   40.87 -        engineClass,
   40.88 -        "messageToSendCallFromC",
   40.89 -        "(Lfoundation/pEp/jniadapter/Message;)I");
   40.90 -    needsFastPollMethodID = _env->GetMethodID(
   40.91 -        engineClass,
   40.92 -        "needsFastPollCallFromC",
   40.93 -        "(Z)I");
   40.94 -    notifyHandShakeMethodID = _env->GetMethodID(
   40.95 -        engineClass,
   40.96 -        "notifyHandshakeCallFromC",
   40.97 -        "(Lfoundation/pEp/jniadapter/_Identity;Lfoundation/pEp/jniadapter/_Identity;Lfoundation/pEp/jniadapter/SyncHandshakeSignal;)I");
   40.98 -    passphraseRequiredMethodID = _env->GetMethodID(
   40.99 -        engineClass,
  40.100 -        "passphraseRequiredFromC",
  40.101 -        "(Lfoundation/pEp/jniadapter/PassphraseType;)[B");
  40.102 -
  40.103 -    sync_handshake_signal_values = JNISync::env()->GetStaticMethodID(signalClass, "values",
  40.104 -                "()[Lfoundation/pEp/jniadapter/SyncHandshakeSignal;");
  40.105 -    passphrase_status_values = JNISync::env()->GetStaticMethodID(passphraseTypeClass, "values",
  40.106 -                "()[Lfoundation/pEp/jniadapter/PassphraseType;");
  40.107 -    signal_field_value = JNISync::env()->GetFieldID(signalClass, "value", "I");
  40.108 -    passphrase_type_field_value = JNISync::env()->GetFieldID(passphraseTypeClass, "value", "I");
  40.109 -}
  40.110 -
  40.111 -char* JNIAdapter::passphraseRequiredCallback(const PEP_STATUS status) {
  40.112 -    pEpLog("called");
  40.113 -    jobject status_ = nullptr;
  40.114 -    {
  40.115 -        assert(passphraseTypeClass);
  40.116 -        assert(passphrase_status_values);
  40.117 -        assert(passphrase_type_field_value);
  40.118 -
  40.119 -        jobjectArray values = (jobjectArray) JNISync::env()->CallStaticObjectMethod(passphraseTypeClass,
  40.120 -                passphrase_status_values);
  40.121 -
  40.122 -        if (JNISync::env()->ExceptionCheck()) {
  40.123 -            JNISync::env()->ExceptionClear();
  40.124 -            throw_pEp_Exception(JNISync::env(), PEP_UNKNOWN_ERROR);
  40.125 -        }
  40.126 -
  40.127 -        jsize values_size = JNISync::env()->GetArrayLength(values);
  40.128 -        for (jsize i = 0; i < values_size; i++) {
  40.129 -            jobject element = JNISync::env()->GetObjectArrayElement(values, i);
  40.130 -            assert(element);
  40.131 -            jint value = JNISync::env()->GetIntField(element, passphrase_type_field_value);
  40.132 -            if (value == (jint) status) {
  40.133 -                status_ = element;
  40.134 -                break;
  40.135 -            }
  40.136 -            JNISync::env()->DeleteLocalRef(element);
  40.137 -        }
  40.138 -    }
  40.139 -    assert(objj && passphraseRequiredMethodID);
  40.140 -
  40.141 -    jobject ppJO = JNISync::env()->CallObjectMethod(objj, passphraseRequiredMethodID, status_);
  40.142 -    if (JNISync::env()->ExceptionCheck()) {
  40.143 -        JNISync::env()->ExceptionDescribe();
  40.144 -        JNISync::env()->ExceptionClear();
  40.145 -    }
  40.146 -
  40.147 -    jbyteArray ppJBA = reinterpret_cast<jbyteArray>(ppJO);
  40.148 -    char* passphrase_ = to_string( JNISync::env(), ppJBA);
  40.149 -
  40.150 -    return passphrase_;
  40.151 -}
  40.152 -
  40.153 -PEP_STATUS messageToSend(message *msg)
  40.154 -{
  40.155 -    std::lock_guard<std::mutex> l(mutex_obj);
  40.156 -    pEpLog("called");
  40.157 -
  40.158 -    // Passphrase
  40.159 -    // When a protocol implementation of the p≡p engine using messageToSend() cannot sign or encrypt with an
  40.160 -    // empty passphrase and not with the configured passphrase it is calling messageToSend() with a NULL instead
  40.161 -    // of a struct _message object.
  40.162 -    if (Adapter::on_sync_thread() && !msg)
  40.163 -        return pEp::PassphraseCache::config_next_passphrase();
  40.164 -
  40.165 -    // reset passphrase iterator
  40.166 -    if (Adapter::on_sync_thread())
  40.167 -        pEp::PassphraseCache::config_next_passphrase(true);
  40.168 -
  40.169 -    jobject msg_ = nullptr;
  40.170 -    assert(messageClass && messageConstructorMethodID && objj && messageToSendMethodID);
  40.171 -
  40.172 -    msg_ = JNISync::env()->NewObject(messageClass, messageConstructorMethodID, (jlong) msg);
  40.173 -
  40.174 -    PEP_STATUS status = (PEP_STATUS) JNISync::env()->CallIntMethod(objj, messageToSendMethodID, msg_);
  40.175 -    if (JNISync::env()->ExceptionCheck()) {
  40.176 -        JNISync::env()->ExceptionDescribe();
  40.177 -        status = PEP_UNKNOWN_ERROR;
  40.178 -        JNISync::env()->ExceptionClear();
  40.179 -    }
  40.180 -
  40.181 -
  40.182 -    return status;
  40.183 -}
  40.184 -
  40.185 -PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal)
  40.186 -{
  40.187 -    std::lock_guard<std::mutex> l(mutex_obj);
  40.188 -    pEpLog("called");
  40.189 -
  40.190 -    jobject me_ = nullptr;
  40.191 -    jobject partner_ = nullptr;
  40.192 -
  40.193 -    me_ = from_identity(JNISync::env(), me, identityClass);
  40.194 -    partner_ = from_identity(JNISync::env(), partner, identityClass);
  40.195 -
  40.196 -    jobject signal_ = nullptr;
  40.197 -    {
  40.198 -        assert(signalClass);
  40.199 -        assert(sync_handshake_signal_values);
  40.200 -        assert(signal_field_value);
  40.201 -
  40.202 -        jobjectArray values = (jobjectArray) JNISync::env()->CallStaticObjectMethod(signalClass,
  40.203 -                sync_handshake_signal_values);
  40.204 -        if (JNISync::env()->ExceptionCheck()) {
  40.205 -            JNISync::env()->ExceptionClear();
  40.206 -            return PEP_UNKNOWN_ERROR;
  40.207 -        }
  40.208 -
  40.209 -        jsize values_size = JNISync::env()->GetArrayLength(values);
  40.210 -        for (jsize i = 0; i < values_size; i++) {
  40.211 -            jobject element = JNISync::env()->GetObjectArrayElement(values, i);
  40.212 -            assert(element);
  40.213 -            jint value = JNISync::env()->GetIntField(element, signal_field_value);
  40.214 -            if (value == (jint) signal) {
  40.215 -                signal_ = element;
  40.216 -                break;
  40.217 -            }
  40.218 -            JNISync::env() -> DeleteLocalRef(element);
  40.219 -        }
  40.220 -    }
  40.221 -
  40.222 -    assert(objj && notifyHandShakeMethodID);
  40.223 -
  40.224 -    PEP_STATUS status = (PEP_STATUS) JNISync::env()->CallIntMethod(objj, notifyHandShakeMethodID, me_, partner_, signal_);
  40.225 -    if (JNISync::env()->ExceptionCheck()) {
  40.226 -        JNISync::env()->ExceptionClear();
  40.227 -        return PEP_UNKNOWN_ERROR;
  40.228 -    }
  40.229 -
  40.230 -    return status;
  40.231 -}
  40.232 -}
  40.233 -
  40.234 -extern "C" {
  40.235 -using namespace pEp;
  40.236 -
  40.237 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_init(
  40.238 -        JNIEnv *env,
  40.239 -        jobject obj
  40.240 -    )
  40.241 -{
  40.242 -    std::lock_guard<std::mutex> l(global_mutex); // global mutex for write access to <unordered_map>
  40.243 -    pEpLog("called");
  40.244 -
  40.245 -    if (first) {
  40.246 -        pEpLog("first Engine instance");
  40.247 -        first = false;
  40.248 -        env->GetJavaVM(&jvm);
  40.249 -        jni_init();
  40.250 -        objj = env->NewGlobalRef(obj);
  40.251 -        callback_dispatcher.add(messageToSend, notifyHandshake, JNISync::onSyncStartup, JNISync::onSyncShutdown);
  40.252 -        Adapter::_messageToSend = CallbackDispatcher::messageToSend;
  40.253 -    }
  40.254 -
  40.255 -    create_engine_java_object_mutex(env, obj);  // Create a mutex per java object
  40.256 -    Adapter::session();
  40.257 -}
  40.258 -
  40.259 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_release(
  40.260 -        JNIEnv *env,
  40.261 -        jobject obj
  40.262 -    )
  40.263 -{
  40.264 -    std::lock_guard<std::mutex> l(global_mutex);  // global mutex for write access to <unordered_map>
  40.265 -    pEpLog("called");
  40.266 -    release_engine_java_object_mutex(env, obj);
  40.267 -    Adapter::session(pEp::Adapter::release);
  40.268 -}
  40.269 -
  40.270 -JNIEXPORT jstring JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_getVersion(
  40.271 -        JNIEnv *env,
  40.272 -        jobject obj
  40.273 -    )
  40.274 -{
  40.275 -    std::mutex *mutex_local = nullptr;
  40.276 -    {
  40.277 -        std::lock_guard<std::mutex> l(global_mutex);
  40.278 -        pEpLog("called with lock_guard");
  40.279 -        mutex_local = get_engine_java_object_mutex(env, obj);
  40.280 -    }
  40.281 -    std::lock_guard<std::mutex> l(*mutex_local);
  40.282 -
  40.283 -    return env->NewStringUTF(::get_engine_version());
  40.284 -}
  40.285 -
  40.286 -JNIEXPORT jstring JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_getProtocolVersion(
  40.287 -        JNIEnv *env,
  40.288 -        jobject obj
  40.289 -    )
  40.290 -{
  40.291 -    std::mutex *mutex_local = nullptr;
  40.292 -    {
  40.293 -        std::lock_guard<std::mutex> l(global_mutex);
  40.294 -        pEpLog("called with lock_guard");
  40.295 -        mutex_local = get_engine_java_object_mutex(env, obj);
  40.296 -    }
  40.297 -    std::lock_guard<std::mutex> l(*mutex_local);
  40.298 -
  40.299 -    return env->NewStringUTF(::get_protocol_version());
  40.300 -}
  40.301 -
  40.302 -int examine_identity(pEp_identity *ident, void *arg)
  40.303 -{
  40.304 -    locked_queue< pEp_identity * > *queue = (locked_queue< pEp_identity * > *) arg;
  40.305 -    queue->push_back(identity_dup(ident));
  40.306 -    return 0;
  40.307 -}
  40.308 -
  40.309 -pEp_identity *retrieve_next_identity(void *arg)
  40.310 -{
  40.311 -    pEpLog("called");
  40.312 -    locked_queue< pEp_identity * > *queue = (locked_queue< pEp_identity * > *) arg;
  40.313 -
  40.314 -    while (!queue->size())
  40.315 -        usleep(100000);
  40.316 -
  40.317 -    pEp_identity *ident = queue->front();
  40.318 -    queue->pop_front();
  40.319 -    return ident;
  40.320 -}
  40.321 -
  40.322 -static void *keyserver_thread_routine(void *arg)
  40.323 -{
  40.324 -    PEP_STATUS status = do_keymanagement(retrieve_next_identity, arg);
  40.325 -    locked_queue< pEp_identity * > *queue = (locked_queue< pEp_identity * > *) arg;
  40.326 -
  40.327 -    while (queue->size()) {
  40.328 -        pEp_identity *ident = queue->front();
  40.329 -        queue->pop_front();
  40.330 -        free_identity(ident);
  40.331 -    }
  40.332 -
  40.333 -    delete queue;
  40.334 -    return (void *) status;
  40.335 -}
  40.336 -
  40.337 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_startKeyserverLookup(
  40.338 -        JNIEnv *env,
  40.339 -        jobject obj
  40.340 -    )
  40.341 -{
  40.342 -    std::mutex *mutex_local = nullptr;
  40.343 -    {
  40.344 -        std::lock_guard<std::mutex> l(global_mutex);
  40.345 -        pEpLog("called with lock_guard");
  40.346 -        mutex_local = get_engine_java_object_mutex(env, obj);
  40.347 -    }
  40.348 -    std::lock_guard<std::mutex> l(*mutex_local);
  40.349 -
  40.350 -    pthread_t *thread = nullptr;
  40.351 -    locked_queue< pEp_identity * > *queue = nullptr;
  40.352 -
  40.353 -    jfieldID thread_handle;
  40.354 -    jfieldID queue_handle;
  40.355 -
  40.356 -    try {
  40.357 -        thread_handle = getFieldID(env, "foundation/pEp/jniadapter/Engine", "keyserverThread", "J");
  40.358 -        queue_handle = getFieldID(env, "foundation/pEp/jniadapter/Engine", "keyserverQueue", "J");
  40.359 -    }
  40.360 -    catch (std::exception& ex) {
  40.361 -        assert(0);
  40.362 -        return;
  40.363 -    }
  40.364 -
  40.365 -    thread = (pthread_t *) env->GetLongField(obj, thread_handle);
  40.366 -    if (thread)
  40.367 -        return;
  40.368 -
  40.369 -    thread = (pthread_t *) calloc(1, sizeof(pthread_t));
  40.370 -    assert(thread);
  40.371 -    env->SetLongField(obj, thread_handle, (jlong) thread);
  40.372 -
  40.373 -    queue = new locked_queue< pEp_identity * >();
  40.374 -    env->SetLongField(obj, queue_handle, (jlong) queue);
  40.375 -
  40.376 -    register_examine_function(Adapter::session(), examine_identity, (void *) queue);
  40.377 -
  40.378 -    pthread_create(thread, nullptr, keyserver_thread_routine, (void *) queue);
  40.379 -}
  40.380 -
  40.381 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_stopKeyserverLookup(
  40.382 -        JNIEnv *env,
  40.383 -        jobject obj
  40.384 -    )
  40.385 -{
  40.386 -    std::mutex *mutex_local = nullptr;
  40.387 -    {
  40.388 -        std::lock_guard<std::mutex> l(global_mutex);
  40.389 -        pEpLog("called with lock_guard");
  40.390 -        mutex_local = get_engine_java_object_mutex(env, obj);
  40.391 -    }
  40.392 -    std::lock_guard<std::mutex> l(*mutex_local);
  40.393 -
  40.394 -    pthread_t *thread = nullptr;
  40.395 -    locked_queue< pEp_identity * > *queue = nullptr;
  40.396 -
  40.397 -    jfieldID thread_handle;
  40.398 -    jfieldID queue_handle;
  40.399 -
  40.400 -    try {
  40.401 -        thread_handle = getFieldID(env, "foundation/pEp/jniadapter/Engine", "keyserverThread", "J");
  40.402 -        queue_handle = getFieldID(env, "foundation/pEp/jniadapter/Engine", "keyserverQueue", "J");
  40.403 -    }
  40.404 -    catch (std::exception& ex) {
  40.405 -        assert(0);
  40.406 -        return;
  40.407 -    }
  40.408 -
  40.409 -    thread = (pthread_t *) env->GetLongField(obj, thread_handle);
  40.410 -    if (!thread)
  40.411 -        return;
  40.412 -
  40.413 -    queue = (locked_queue< pEp_identity * > *) env->GetLongField(obj, queue_handle);
  40.414 -
  40.415 -    env->SetLongField(obj, queue_handle, (jlong) 0);
  40.416 -    env->SetLongField(obj, thread_handle, (jlong) 0);
  40.417 -
  40.418 -    register_examine_function(Adapter::session(), nullptr, nullptr);
  40.419 -
  40.420 -    queue->push_front(nullptr);
  40.421 -    pthread_join(*thread, nullptr);
  40.422 -    free(thread);
  40.423 -}
  40.424 -
  40.425 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_startSync(
  40.426 -        JNIEnv *env,
  40.427 -        jobject obj
  40.428 -    )
  40.429 -{
  40.430 -    std::mutex *mutex_local = nullptr;
  40.431 -    {
  40.432 -        std::lock_guard<std::mutex> l(global_mutex);
  40.433 -        pEpLog("called with lock_guard");
  40.434 -        mutex_local = get_engine_java_object_mutex(env, obj);
  40.435 -    }
  40.436 -    std::lock_guard<std::mutex> l(*mutex_local);
  40.437 -
  40.438 -    try {
  40.439 -        CallbackDispatcher::start_sync();
  40.440 -//        Adapter::startup<JNISync>(messageToSend, notifyHandshake, &o, &JNISync::onSyncStartup, &JNISync::onSyncShutdown);
  40.441 -    } catch (RuntimeError& ex) {
  40.442 -        throw_pEp_Exception(env, ex.status);
  40.443 -        return;
  40.444 -    }
  40.445 -}
  40.446 -
  40.447 -JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_stopSync(
  40.448 -        JNIEnv *env,
  40.449 -        jobject obj
  40.450 -    )
  40.451 -{
  40.452 -    std::mutex *mutex_local = nullptr;
  40.453 -    {
  40.454 -        std::lock_guard<std::mutex> l(global_mutex);
  40.455 -        pEpLog("called with lock_guard");
  40.456 -        mutex_local = get_engine_java_object_mutex(env, obj);
  40.457 -    }
  40.458 -    std::lock_guard<std::mutex> l(*mutex_local);
  40.459 -
  40.460 -    CallbackDispatcher::stop_sync();
  40.461 -//    Adapter::shutdown();
  40.462 -}
  40.463 -
  40.464 -JNIEXPORT jboolean JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_isSyncRunning(
  40.465 -        JNIEnv *env,
  40.466 -        jobject obj
  40.467 -    )
  40.468 -{
  40.469 -    std::mutex *mutex_local = nullptr;
  40.470 -    {
  40.471 -        std::lock_guard<std::mutex> l(global_mutex);
  40.472 -        pEpLog("called with lock_guard");
  40.473 -        mutex_local = get_engine_java_object_mutex(env, obj);
  40.474 -    }
  40.475 -    std::lock_guard<std::mutex> l(*mutex_local);
  40.476 -
  40.477 -    return (jboolean) Adapter::is_sync_running();
  40.478 -}
  40.479 -
  40.480 -} // extern "C"
  40.481 -
    41.1 --- a/src/foundation_pEp_jniadapter__Blob.cc	Tue Aug 18 21:57:06 2020 +0200
    41.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    41.3 @@ -1,64 +0,0 @@
    41.4 -#include <cassert>
    41.5 -#include <pEp/platform.h>
    41.6 -#include <pEp/sync_codec.h>
    41.7 -#include <pEp/distribution_codec.h>
    41.8 -#include <pEp/pEpLog.hh>
    41.9 -#include "jniutils.hh"
   41.10 -#include "throw_pEp_exception.hh"
   41.11 -#include "foundation_pEp_jniadapter__Blob.h"
   41.12 -
   41.13 -namespace pEp {
   41.14 -    namespace JNIAdapter {
   41.15 -        static ::bloblist_t *bloblist_ptr(JNIEnv *env, jobject me) {
   41.16 -            jfieldID handle;
   41.17 -
   41.18 -            try {
   41.19 -                handle = getFieldID(env, "foundation/pEp/jniadapter/Blob", "mime_type", "Ljava/lang/String");
   41.20 -            }
   41.21 -            catch (std::exception& ex) {
   41.22 -                assert(0);
   41.23 -                return NULL;
   41.24 -            }
   41.25 -
   41.26 -            return (::bloblist_t *) (intptr_t) (int64_t) env->GetLongField(me, handle);
   41.27 -        }
   41.28 -    };
   41.29 -};
   41.30 -
   41.31 -extern "C" {
   41.32 -
   41.33 -
   41.34 -using namespace std;
   41.35 -using namespace pEp::JNIAdapter;
   41.36 -JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter__1Blob__1dataToXER(JNIEnv *env, jobject obj)
   41.37 -{
   41.38 -    pEpLog("called");
   41.39 -    bloblist_t *b = to_blob(env, obj);
   41.40 -    char *out = nullptr;
   41.41 -
   41.42 -    // RFC 1049 / RFC 2045 : The type, subtype, and parameter names are not case sensitive.
   41.43 -    if(strcasecmp(b->mime_type, "application/pEp.sync") == 0) {
   41.44 -        PEP_STATUS status = ::PER_to_XER_Sync_msg(b->value, (size_t) b->size, &out);
   41.45 -        if (status)
   41.46 -            throw_pEp_Exception(env, status);
   41.47 -
   41.48 -        jbyteArray result = from_string(env, out);
   41.49 -        free(out);
   41.50 -        return result;
   41.51 -    }
   41.52 -
   41.53 -    // RFC 1049 / RFC 2045 : The type, subtype, and parameter names are not case sensitive.
   41.54 -    if(strcasecmp(b->mime_type, "application/pEp.keyreset") == 0) {
   41.55 -        PEP_STATUS status = ::PER_to_XER_Distribution_msg(b->value, (size_t) b->size, &out);
   41.56 -        if (status)
   41.57 -            throw_pEp_Exception(env, status);
   41.58 -
   41.59 -        jbyteArray result = from_string(env, out);
   41.60 -        free(out);
   41.61 -        return result;
   41.62 -    }
   41.63 -
   41.64 -    return from_string(env,b->value);
   41.65 -}
   41.66 -
   41.67 -}; // extern "C"
    42.1 --- a/src/gen_cpp_Engine.ysl2	Tue Aug 18 21:57:06 2020 +0200
    42.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    42.3 @@ -1,328 +0,0 @@
    42.4 -include yslt.yml2
    42.5 -
    42.6 -tstylesheet {
    42.7 -    include ./textutils.ysl2
    42.8 -    include ./types_c.ysl2
    42.9 -    include ./types_java.ysl2
   42.10 -
   42.11 -    template "/namespace[@name='pEp']" apply "interface", 0;
   42.12 -
   42.13 -    template "interface" document("foundation_pEp_jniadapter_{@name}.cc", "text")
   42.14 -        ||
   42.15 -        #include <cassert>
   42.16 -        #include <pEp/blacklist.h>
   42.17 -        #include <pEp/openpgp_compat.h>
   42.18 -        #include <pEp/key_reset.h>
   42.19 -        #include <pEp/Adapter.hh>
   42.20 -        #include <pEp/pEpLog.hh>
   42.21 -        #include <pEp/passphrase_cache.hh>
   42.22 -
   42.23 -        #include "foundation_pEp_jniadapter_«@name».h"
   42.24 -        #include "throw_pEp_exception.hh"
   42.25 -        #include "jniutils.hh"
   42.26 -        #include "passphrase_callback.hh"
   42.27 -
   42.28 -        using pEp::Adapter::session;
   42.29 -        using pEp::passphrase_cache;
   42.30 -        using namespace pEp::JNIAdapter;
   42.31 -
   42.32 -        extern "C" {
   42.33 -
   42.34 -        `` apply "method", 0
   42.35 -        } // extern "C"
   42.36 -
   42.37 -        ||
   42.38 -
   42.39 -    template "method" {
   42.40 -        const "mangled" call "mangle";
   42.41 -        const "returning", "count(parm/creates|parm/returns)";
   42.42 -        const "CretType" choose {
   42.43 -            when "$returning = 0" > void
   42.44 -            when "$returning = 1"
   42.45 -                call "jni_type" with "type", "name(parm[returns|creates]/*[2])";
   42.46 -            when "$returning > 0" > jobject
   42.47 -        }
   42.48 -        const "CretDefault" choose {
   42.49 -            when "$returning = 0" >
   42.50 -            when "$returning > 0" > NULL
   42.51 -        }
   42.52 -
   42.53 -        ||
   42.54 -        JNIEXPORT «$CretType» JNICALL «$mangled»(
   42.55 -                JNIEnv *env,
   42.56 -                jobject obj`apply "parm[in|inout]", mode=sig`
   42.57 -            )
   42.58 -        {
   42.59 -            std::mutex *mutex_local = nullptr;
   42.60 -            {
   42.61 -                std::lock_guard<std::mutex> l(global_mutex);
   42.62 -                pEpLog("called with lock_guard");
   42.63 -                mutex_local = get_engine_java_object_mutex(env, obj);
   42.64 -            }
   42.65 -            std::lock_guard<std::mutex> l(*mutex_local);
   42.66 -
   42.67 -        ||
   42.68 -
   42.69 -        apply "parm[in|inout]", mode=in;
   42.70 -        apply "parm[creates|returns]", mode=out;
   42.71 -        ||
   42.72 -
   42.73 -        ||
   42.74 -        choose {
   42.75 -            when "@cached = 'true'" {
   42.76 -            ||
   42.77 -                PEP_STATUS status = passphraseWrap(::«@name», session()`apply "parm", mode=call`);
   42.78 -            ||
   42.79 -            } otherwise {
   42.80 -            ||
   42.81 -                PEP_STATUS status = ::«@name»(session()`apply "parm", mode=call`);
   42.82 -            ||
   42.83 -            }
   42.84 -        }
   42.85 -        ||
   42.86 -            if ((status > PEP_STATUS_OK && status < PEP_UNENCRYPTED) |`> |`
   42.87 -                    status < PEP_STATUS_OK |`> |`
   42.88 -                    status >= PEP_TRUSTWORD_NOT_FOUND) {
   42.89 -                throw_pEp_Exception(env, status);
   42.90 -                return «$CretDefault»;
   42.91 -            }
   42.92 -
   42.93 -        ||
   42.94 -
   42.95 -        apply "parm[returns|creates]", mode=return;
   42.96 -
   42.97 -        choose {
   42.98 -            when "$returning > 1" {
   42.99 -
  42.100 -                ||
  42.101 -                    jobject result_ = NULL;
  42.102 -                    jclass clazz_result_ = findClass(env, "foundation/pEp/jniadapter/Engine$_«@name»_Return");
  42.103 -                    assert(clazz_result_);
  42.104 -                    jmethodID constructor_result_ = env->GetMethodID(clazz_result_, "<init>", "(Lfoundation/pEp/jniadapter/Engine;)V");
  42.105 -                    assert(constructor_result_);
  42.106 -                    result_ = env->NewObject(clazz_result_, constructor_result_, obj);
  42.107 -                ||
  42.108 -
  42.109 -                apply "parm[returns|creates|inout[../int]]", mode=setresult
  42.110 -                    with "jtype" > foundation/pEp/jniadapter/Engine$_«@name»_Return
  42.111 -
  42.112 -                ||
  42.113 -                    return result_;
  42.114 -                ||
  42.115 -            }
  42.116 -
  42.117 -            when "$returning = 1" {
  42.118 -
  42.119 -                ||
  42.120 -                    return «name(parm[returns|creates|inout[../int]]/*[3])»_;
  42.121 -                ||
  42.122 -            }
  42.123 -        }
  42.124 -
  42.125 -        ||
  42.126 -        }
  42.127 -
  42.128 -        ||
  42.129 -    }
  42.130 -
  42.131 -    template "parm", mode=setresult {
  42.132 -        param "jtype";
  42.133 -        const "dir", "name(*[1])";
  42.134 -        const "type", "name(*[2])";
  42.135 -        const "name", "name(*[3])";
  42.136 -        const "sig" call "toSig" with "type", "$type";
  42.137 -
  42.138 -
  42.139 -        choose {
  42.140 -            when "$type='int'"
  42.141 -                ||
  42.142 -                jfieldID «$name»_field = env->GetFieldID(clazz_result_, "«$name»", "I");
  42.143 -                ||
  42.144 -            otherwise {
  42.145 -                ||
  42.146 -                jfieldID «$name»_field = getFieldID(env, "«$jtype»", "«$name»", "«$sig»");
  42.147 -                ||
  42.148 -
  42.149 -            }
  42.150 -        }
  42.151 -
  42.152 -        choose {
  42.153 -            when "$type='int'"
  42.154 -                | env->SetIntField(result_, «$name»_field, _«$name»);
  42.155 -            otherwise {
  42.156 -                ||
  42.157 -                if («$name»_)
  42.158 -                    env->SetObjectField(result_, «$name»_field, «$name»_);
  42.159 -                ||
  42.160 -                if "$name = 'dst'"
  42.161 -                ||
  42.162 -                else
  42.163 -                    env->SetObjectField(result_, «$name»_field, src);
  42.164 -                ||
  42.165 -            }
  42.166 -        }
  42.167 -
  42.168 -        ||
  42.169 -
  42.170 -        ||
  42.171 -    }
  42.172 -
  42.173 -    template "parm", mode=return {
  42.174 -        param "dir", "name(*[1])";
  42.175 -        param "type", "name(*[2])";
  42.176 -        param "jtype" call "toJava" with "type", "$type";
  42.177 -        param "name", "name(*[3])";
  42.178 -
  42.179 -        choose {
  42.180 -            when "$type = 'stringlist' or $type = 'string' or $type = 'sstring' or $type = 'stringpairlist' or $type = 'identity' or $type = 'identitylist'"
  42.181 -            {
  42.182 -                const "jnitype" call "jni_type" with "type", "$type";
  42.183 -                const "from_type" choose {
  42.184 -                    when "$type = 'sstring'"
  42.185 -                        > from_string
  42.186 -                    otherwise
  42.187 -                        > from_«$type»
  42.188 -                }
  42.189 -                ||
  42.190 -                «$jnitype» «$name»_ = NULL;
  42.191 -                if (_«$name»)
  42.192 -                    «$name»_ = «$from_type»(env, _«$name»);
  42.193 -
  42.194 -                ||
  42.195 -            }
  42.196 -            when "ancestor::namespace/child::enum[@name=$type]" {
  42.197 -                const "ljtype" call "lcase" with "text","$jtype";
  42.198 -                ||
  42.199 -                jobject «$name»_ = NULL;
  42.200 -                {
  42.201 -                    jclass clazz_«$ljtype» = findClass(env, "foundation/pEp/jniadapter/«$jtype»");
  42.202 -                    assert(clazz_«$ljtype»);
  42.203 -                    jmethodID method_values = env->GetStaticMethodID(clazz_«$ljtype», "values",
  42.204 -                            "()[Lfoundation/pEp/jniadapter/«$jtype»;");
  42.205 -                    assert(method_values);
  42.206 -                    jfieldID field_value = env->GetFieldID(clazz_«$ljtype», "value", "I");
  42.207 -                    assert(field_value);
  42.208 -
  42.209 -                    jobjectArray values = (jobjectArray) env->CallStaticObjectMethod(clazz_«$ljtype»,
  42.210 -                            method_values);
  42.211 -                    assert(values);
  42.212 -                    if (env->ExceptionCheck()) {
  42.213 -                        return nullptr; // handle exception in Java
  42.214 -                    }
  42.215 -
  42.216 -                    jsize values_size = env->GetArrayLength(values);
  42.217 -                    for (jsize i = 0; i < values_size; i++) {
  42.218 -                        jobject element = env->GetObjectArrayElement(values, i);
  42.219 -                        assert(element);
  42.220 -                        jint value = env->GetIntField(element, field_value);
  42.221 -                        if (value == (jint) _«$name») {
  42.222 -                            «$name»_ = element;
  42.223 -                            break;
  42.224 -                        }
  42.225 -                    }
  42.226 -                }
  42.227 -
  42.228 -                ||
  42.229 -            }
  42.230 -            otherwise {
  42.231 -                ||
  42.232 -                jobject «$name»_ = NULL;
  42.233 -                if (_«$name») {
  42.234 -                    jclass clazz_«$name»_ = findClass(env, "foundation/pEp/jniadapter/«$jtype»");
  42.235 -                    assert(clazz_«$name»_);
  42.236 -                    jmethodID constructor_«$name»_ = env->GetMethodID(clazz_«$name»_, "<init>", "(J)V");
  42.237 -                    assert(constructor_«$name»_);
  42.238 -                    «$name»_ = env->NewObject(clazz_«$name»_, constructor_«$name»_, (jlong) _«$name»);
  42.239 -                }
  42.240 -
  42.241 -                ||
  42.242 -            }
  42.243 -        }
  42.244 -    }
  42.245 -
  42.246 -    template "parm", mode=in {
  42.247 -        const "dir", "name(*[1])";
  42.248 -        const "type", "name(*[2])";
  42.249 -        const "name", "name(*[3])";
  42.250 -        const "const", "name(*[4])";
  42.251 -
  42.252 -        choose {
  42.253 -            when "$type = 'message'"
  42.254 -                | message *_«$name» = (message *) callLongMethod(env, «$name», "getHandle");
  42.255 -            when "$type='EncFormat' or $type='SyncHandshakeResult' or $type='CipherSuite'"
  42.256 -                | √$type _«$name» = to_«$type»(env, «$name»);
  42.257 -            when "$type='bool'"
  42.258 -                | bool _«$name» = (bool) «$name»;
  42.259 -            when "$type='int'"
  42.260 -                | int _«$name» = (int) «$name»;
  42.261 -            when "$type='uint'"
  42.262 -                | auto _«$name» = (unsigned int) «$name»;
  42.263 -            when "$type='string'"
  42.264 -                | const char *_«$name» = to_string(env, «$name»);
  42.265 -            otherwise {
  42.266 -                choose {
  42.267 -                    when "$const"
  42.268 -                        | const √$type *_«$name» = to_«$type»(env, «$name»);
  42.269 -                    otherwise
  42.270 -                        | √$type *_«$name» = to_«$type»(env, «$name»);
  42.271 -                }
  42.272 -            }
  42.273 -        }
  42.274 -    }
  42.275 -
  42.276 -    template "parm", mode=out {
  42.277 -        param "name", "name(*[3])";
  42.278 -        const "type", "name(*[2])";
  42.279 -
  42.280 -        choose {
  42.281 -            when "ancestor::namespace/child::enum[@name=$type]"
  42.282 -                | √$type _«$name»;
  42.283 -            when "$type='sstring'"{
  42.284 -                | char * _«$name»;
  42.285 -                | size_t _«$name»_size;
  42.286 -            }
  42.287 -            otherwise
  42.288 -                | √$type *_«$name»;
  42.289 -        }
  42.290 -    }
  42.291 -
  42.292 -    template "parm", mode=call {
  42.293 -        const "dir", "name(*[1])";
  42.294 -        const "type", "name(*[2])";
  42.295 -        const "name", "name(*[3])";
  42.296 -
  42.297 -        > ,
  42.298 -
  42.299 -        choose {
  42.300 -            when "$dir = 'Cconst'"
  42.301 -                value "*[3]";
  42.302 -            when "($dir = 'creates' or $dir = 'returns') and $type='sstring'"
  42.303 -                > &_«$name», &_«$name»_size
  42.304 -            when "$dir = 'creates' or $dir = 'returns'"
  42.305 -                > &_«$name»
  42.306 -            when "$dir = 'inout' and $type='int'"
  42.307 -                > (unsigned int *) &_«$name»
  42.308 -            when "$dir = 'inout' and $type='stringlist'"
  42.309 -                > &_«$name»
  42.310 -            otherwise
  42.311 -                > _«$name»
  42.312 -        }
  42.313 -    }
  42.314 -
  42.315 -    function "mangle" {
  42.316 -        const "convert", "count(parm/stringlist|parm/string|parm/identity|parm/identitylist) > 0";
  42.317 -
  42.318 -        choose {
  42.319 -            when "$convert"
  42.320 -                > «concat('Java_foundation_pEp_jniadapter_', ../@name, '__1', str:replace(@name, '_', '_1'))»
  42.321 -            otherwise
  42.322 -                > «concat('Java_foundation_pEp_jniadapter_', ../@name, '_', str:replace(@name, '_', '_1'))»
  42.323 -        }
  42.324 -    }
  42.325 -
  42.326 -    template "parm", mode=sig {
  42.327 -        const "name", "name(*[3])";
  42.328 -        > ,\n        `call "jni_type" with "type", "name(*[2])", with "dir", "name(*[1])"` «$name»
  42.329 -    }
  42.330 -}
  42.331 -
    43.1 --- a/src/gen_cpp_Message.ysl2	Tue Aug 18 21:57:06 2020 +0200
    43.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    43.3 @@ -1,191 +0,0 @@
    43.4 -include yslt.yml2
    43.5 -include yslt.yml2
    43.6 -
    43.7 -tstylesheet {
    43.8 -    include ./textutils.ysl2
    43.9 -    include ./types_c.ysl2
   43.10 -
   43.11 -    template "/namespace[@name='pEp']" apply "struct", 0;
   43.12 -
   43.13 -    template "struct" {
   43.14 -        const "jname" call "CamelCase" with "text", "@name";
   43.15 -
   43.16 -        document("foundation_pEp_jniadapter_{$jname}.cc", "text") {
   43.17 -            ||
   43.18 -            #include <cassert>
   43.19 -            #include <pEp/mime.h>
   43.20 -            #include <pEp/pEpLog.hh>
   43.21 -            #include "jniutils.hh"
   43.22 -            #include "throw_pEp_exception.hh"
   43.23 -            #include "foundation_pEp_jniadapter_«$jname».h"
   43.24 -
   43.25 -            using namespace std;
   43.26 -
   43.27 -            namespace pEp {
   43.28 -                namespace JNIAdapter {
   43.29 -                    static ::«@name» *«@name»_ptr(JNIEnv *env, jobject me) {
   43.30 -                        jfieldID handle;
   43.31 -
   43.32 -                        try {
   43.33 -                            handle = getFieldID(env, "foundation/pEp/jniadapter/«$jname»", "handle", "J");
   43.34 -                        }
   43.35 -                        catch (std::exception& ex) {
   43.36 -                            assert(0);
   43.37 -                            return NULL;
   43.38 -                        }
   43.39 -
   43.40 -                        return (::«@name» *) (intptr_t) (int64_t) env->GetLongField(me, handle);
   43.41 -                    }
   43.42 -                };
   43.43 -            };
   43.44 -
   43.45 -            extern "C" {
   43.46 -
   43.47 -            using namespace pEp::JNIAdapter;
   43.48 -
   43.49 -            JNIEXPORT jlong JNICALL Java_foundation_pEp_jniadapter_«$jname»_init(JNIEnv *env, jobject obj)
   43.50 -            {
   43.51 -                pEpLog("called");
   43.52 -                ::«@name» * _obj = ::new_«@name»(PEP_dir_incoming);
   43.53 -                if (!_obj) {
   43.54 -                    outOfMemory(env);
   43.55 -                    return (jlong) 0;
   43.56 -                }
   43.57 -                return (jlong) (int64_t) (intptr_t) _obj;
   43.58 -            }
   43.59 -
   43.60 -            JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_«$jname»_release(JNIEnv *env, jobject obj, jlong value)
   43.61 -            {
   43.62 -                pEpLog("called");
   43.63 -                if (value) {
   43.64 -                    ::«@name» *_obj = (::«@name» *) (intptr_t) (int64_t) value;
   43.65 -                    ::free_«@name»(_obj);
   43.66 -                }
   43.67 -            }
   43.68 -
   43.69 -            JNIEXPORT jlong JNICALL Java_foundation_pEp_jniadapter_«$jname»__1«$jname»(JNIEnv *env, jobject msg, jbyteArray mime_text)
   43.70 -            {
   43.71 -                pEpLog("called");
   43.72 -                char *_mime_text = to_string(env, mime_text);
   43.73 -                size_t _size = (size_t) env->GetArrayLength(mime_text);
   43.74 -
   43.75 -                ::«@name» *_msg = nullptr;
   43.76 -                PEP_STATUS status = mime_decode_«@name»(_mime_text, _size, &_msg, NULL);
   43.77 -                if (status)
   43.78 -                    throw_pEp_Exception(env, status);
   43.79 -                return (jlong) (int64_t) (intptr_t) _msg;
   43.80 -            }
   43.81 -
   43.82 -            JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_«$jname»__1encodeMIME(JNIEnv *env, jobject msg)
   43.83 -            {
   43.84 -                pEpLog("called");
   43.85 -                «@name» *_obj = «@name»_ptr(env, msg);
   43.86 -                char *mime_text = nullptr;
   43.87 -                PEP_STATUS status = ::mime_encode_«@name»(_obj, false, &mime_text, false);
   43.88 -                if (status)
   43.89 -                    throw_pEp_Exception(env, status);
   43.90 -                jbyteArray result = from_string(env, mime_text);
   43.91 -                free(mime_text);
   43.92 -                return result;
   43.93 -            }
   43.94 -
   43.95 -            ||
   43.96 -            apply "*[name(.)!='enum']", 0, mode=entry {
   43.97 -                with "name", "@name";
   43.98 -                with "class" call "CamelCase" with "text", "@name";
   43.99 -            }
  43.100 -            ||
  43.101 -            } // extern "C"
  43.102 -
  43.103 -            ||
  43.104 -        }
  43.105 -    }
  43.106 -
  43.107 -    function "mangle" {
  43.108 -        param "type";
  43.109 -        param "name";
  43.110 -        param "classname" call "CamelCase" with "text", "../@name";
  43.111 -        const "convert", "$type != 'timestamp'";
  43.112 -
  43.113 -        choose {
  43.114 -            when "$convert"
  43.115 -                > «concat('Java_foundation_pEp_jniadapter_', $classname, '__1', str:replace($name, '_', '_1'))»
  43.116 -            otherwise
  43.117 -                > «concat('Java_foundation_pEp_jniadapter_', $classname, '_', str:replace($name, '_', '_1'))»
  43.118 -        }
  43.119 -    }
  43.120 -
  43.121 -    template "*", mode=entry {
  43.122 -        param "name";
  43.123 -        param "class";
  43.124 -        const "ctype" call "toC" with "type", "name(.)";
  43.125 -        const "jname" call "CamelCase" with "text", "name(*[1])";
  43.126 -        const "cname" call "lcase" with "text", "name(*[1])";
  43.127 -        const "type", "name(.)";
  43.128 -        const "getname" call "mangle" {
  43.129 -            with "type", "$type";
  43.130 -            with "name", "concat('get', $jname)";
  43.131 -        }
  43.132 -        const "setname" call "mangle" {
  43.133 -            with "type", "$type";
  43.134 -            with "name", "concat('set', $jname)";
  43.135 -        }
  43.136 -        ||
  43.137 -        JNIEXPORT `call "jni_type" with "type", "name(.)"` JNICALL «$getname»(JNIEnv *env, jobject obj)
  43.138 -        {
  43.139 -            pEpLog("called");
  43.140 -            «$name» *_obj = «$name»_ptr(env, obj);
  43.141 -        ||
  43.142 -        choose {
  43.143 -            when "../enum[@name=$type]"
  43.144 -                |> return (jint) _obj->«$cname»;
  43.145 -            otherwise
  43.146 -                |> return from_«$type»(env, _obj->«$cname»);
  43.147 -        }
  43.148 -        ||
  43.149 -        }
  43.150 -
  43.151 -        JNIEXPORT void JNICALL «$setname»(JNIEnv *env, jobject obj, `call "jni_type" with "type", "name(.)"` value)
  43.152 -        {
  43.153 -            pEpLog("called");
  43.154 -            «$name» *_obj = «$name»_ptr(env, obj);
  43.155 -        ||
  43.156 -        choose {
  43.157 -            when "../enum[@name=$type]"
  43.158 -                |> _obj->«$cname» = («$ctype») (int) value;
  43.159 -            otherwise {
  43.160 -                choose {
  43.161 -                    when "$type = 'string'" {
  43.162 -                        |> free(_obj->«$cname»);
  43.163 -                    }
  43.164 -                    otherwise {
  43.165 -                        const "free" choose {
  43.166 -                            when "$ctype = 'pEp_identity'" > identity
  43.167 -                            when "contains($ctype, '_t')" value "substring-before($ctype, '_t')";
  43.168 -                            otherwise value "$ctype";
  43.169 -                        }
  43.170 -
  43.171 -                        |> free_«$free»(_obj->«$cname»);
  43.172 -                    }
  43.173 -                }
  43.174 -                |> if(value){
  43.175 -                choose {
  43.176 -                    when "$type = 'string'" {
  43.177 -                        |>> _obj->«$cname» = to_«$type»(env, value);
  43.178 -                    }
  43.179 -                    otherwise {
  43.180 -                        |>> _obj->«$cname» = to_«$type»(env, value);
  43.181 -                    }
  43.182 -                }
  43.183 -                |> }else{
  43.184 -                |>     _obj->«$cname» = NULL;
  43.185 -                |> }
  43.186 -            }
  43.187 -        }
  43.188 -        ||
  43.189 -        }
  43.190 -
  43.191 -        ||
  43.192 -    }
  43.193 -}
  43.194 -
    44.1 --- a/src/gen_java_Engine.ysl2	Tue Aug 18 21:57:06 2020 +0200
    44.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    44.3 @@ -1,346 +0,0 @@
    44.4 -include yslt.yml2
    44.5 -
    44.6 -tstylesheet {
    44.7 -    include ./textutils.ysl2
    44.8 -    include ./types_java.ysl2
    44.9 -
   44.10 -    template "/namespace[@name='pEp']" apply "interface", 0;
   44.11 -
   44.12 -    template "interface" {
   44.13 -        const "cname" call "toJava" with "type", "@name";
   44.14 -        document("foundation/pEp/jniadapter/{$cname}.java", "text")
   44.15 -            ||
   44.16 -            package foundation.pEp.jniadapter;
   44.17 -
   44.18 -            import java.util.ArrayList;
   44.19 -            import java.util.Vector;
   44.20 -
   44.21 -            final public class «$cname» extends AbstractEngine {
   44.22 -                public «$cname»() throws pEpException { }
   44.23 -
   44.24 -                `` apply "method", mode=plain
   44.25 -                `` apply "basic"
   44.26 -            }
   44.27 -
   44.28 -            ||
   44.29 -    }
   44.30 -
   44.31 -    template "basic" {
   44.32 -        const "itype" call "toIntermediate" with "type", "@type";
   44.33 -        const "jtype" call "toJava" with "type", "@type";
   44.34 -
   44.35 -        choose {
   44.36 -            when "name(parm/*[1])='bytearray|identitylist'"
   44.37 -            ||
   44.38 -            public native «$itype» «@name»(`apply "parm/*", mode=basic_iparm`);
   44.39 -
   44.40 -            ||
   44.41 -            otherwise {
   44.42 -            ||
   44.43 -            private native «$itype» _«@name»(`apply "parm/*", mode=basic_iparm`);
   44.44 -
   44.45 -            public «$jtype» «@name»(`apply "parm/*", mode=basic_parm`) {
   44.46 -            ||
   44.47 -            apply "parm/*", mode=basic_parm_set;
   44.48 -            choose {
   44.49 -                when "@type = 'void'"
   44.50 -                    |> _«@name»(`apply "parm/*", mode=basic_parm_name`);
   44.51 -                when "@type = 'identity'"
   44.52 -                    |> return new Identity(_«@name»(`apply "parm/*", mode=basic_parm_name`));
   44.53 -                when "@type = 'bool'"
   44.54 -                    |> return new Boolean(_«@name»(`apply "parm/*", mode=basic_parm_name`));
   44.55 -                when "@type = 'identitylist'" {
   44.56 -                ||
   44.57 -                    Vector<_Identity> glist = _«@name»(`apply "parm/*", mode=basic_parm_name`);
   44.58 -                    Vector<Identity> ret = new Vector<Identity>();
   44.59 -                    if(glist != null) {
   44.60 -                        for (_Identity i : glist) {
   44.61 -                            ret.add(new Identity(i));
   44.62 -                        }
   44.63 -                    }
   44.64 -                    return ret;
   44.65 -                ||
   44.66 -                } otherwise
   44.67 -                    |> return AbstractEngine.toUTF16(_«@name»(`apply "parm/*", mode=basic_parm_name`));
   44.68 -            }
   44.69 -            ||
   44.70 -            }
   44.71 -
   44.72 -            ||
   44.73 -            }
   44.74 -        }
   44.75 -    }
   44.76 -    
   44.77 -    template "*", mode=basic_parm_name choose {
   44.78 -        when "position() mod 2" {
   44.79 -            if "position() = 1 and name(.)='int'"
   44.80 -                > 23, 
   44.81 -            if "position() > 1" > , 
   44.82 -        }
   44.83 -        otherwise {
   44.84 -            > _«name(.)»
   44.85 -        }
   44.86 -    }
   44.87 -
   44.88 -    template "*", mode=basic_parm choose {
   44.89 -        when "position() mod 2" {
   44.90 -            if "position() > 1" > , 
   44.91 -            choose {
   44.92 -                when "name(.) = 'string'"
   44.93 -                    > String 
   44.94 -                when "name(.) = 'identity'"
   44.95 -                    > Identity 
   44.96 -                when "name(.) = 'identitylist'"
   44.97 -                    > IdentityList 
   44.98 -                when "name(.) = 'bool'"
   44.99 -                    > Boolean 
  44.100 -                when "name(.) = 'bytearray'"
  44.101 -                    > byte[] 
  44.102 -                otherwise
  44.103 -                    > «name(.)» 
  44.104 -            }
  44.105 -        }
  44.106 -        otherwise {
  44.107 -            > «name(.)»
  44.108 -        }
  44.109 -    }
  44.110 -
  44.111 -    template "*", mode=basic_iparm choose {
  44.112 -        when "position() mod 2" {
  44.113 -            const "pitype" call "toIntermediate" with "type", "name(.)";
  44.114 -            if "position() = 1 and name(.)='int'"
  44.115 -                > int dummy, 
  44.116 -            if "position() > 1" > , 
  44.117 -            > «$pitype» 
  44.118 -        }
  44.119 -        otherwise {
  44.120 -            > «name(.)»
  44.121 -        }
  44.122 -    }
  44.123 -
  44.124 -    template "*", mode=basic_parm_set {
  44.125 -        const "ptype", "name(preceding-sibling::*[1])";
  44.126 -        const "pname", "name(.)";
  44.127 -        const "pitype" call "toIntermediate" with "type", "$ptype";
  44.128 -
  44.129 -        if "not(position() mod 2)" choose {
  44.130 -            when "$ptype = 'string'"
  44.131 -            | «$pitype» _«$pname» = AbstractEngine.toUTF8(«$pname»);
  44.132 -            when "$ptype = 'bool'"
  44.133 -            | «$pitype» _«$pname» = «$pname».booleanValue();
  44.134 -            when "$ptype = 'bytearray'"
  44.135 -            | «$pitype» _«$pname» = «$pname».clone();
  44.136 -            when "$ptype = 'int'"
  44.137 -            | «$pitype» _«$pname» = «$pname»; 
  44.138 -            when "ancestor::namespace/child::enum[@name=$ptype]"
  44.139 -            | «$pitype» _«$pname» = «$pname».value;
  44.140 -            otherwise
  44.141 -            | «$pitype» _«$pname» = new «$pitype»(«$pname»);
  44.142 -        }
  44.143 -    }
  44.144 -
  44.145 -    function "returnJava" {
  44.146 -        const "returning", "count(parm/creates|parm/returns)";
  44.147 -        const "returntype" call "toJava" with "type", "name(parm[creates|returns]/*[2])";
  44.148 -
  44.149 -        choose {
  44.150 -            when "$returning = 0" > void
  44.151 -            when "$returning = 1" > «$returntype»
  44.152 -            otherwise > «@name»_Return
  44.153 -        }
  44.154 -    }
  44.155 -
  44.156 -    function "returnInter" {
  44.157 -        const "returning", "count(parm/creates|parm/returns)";
  44.158 -        const "returntype" call "toIntermediate" with "type", "name(parm[creates|returns]/*[2])";
  44.159 -
  44.160 -        choose {
  44.161 -            when "$returning = 0" > void
  44.162 -            when "$returning = 1" > «$returntype»
  44.163 -            otherwise > _«@name»_Return
  44.164 -        }
  44.165 -    }
  44.166 -
  44.167 -    template "method", mode=plain {
  44.168 -        const "convert", "count(parm/stringlist|parm/stringpairlist|parm/string|parm/identity|parm/identitylist) > 0";
  44.169 -        const "singlereturn", "count(parm/creates|parm/returns|parm/inout[../int]) = 1";
  44.170 -        const "multireturn", "count(parm/creates|parm/returns|parm/inout[../int]) > 1";
  44.171 -
  44.172 -        if "$multireturn" {
  44.173 -            if "$convert"
  44.174 -            ||
  44.175 -            private class _«@name»_Return {
  44.176 -            `` apply "parm[creates|returns|inout[../int]]", 1, mode=intermediate with "public", 1;
  44.177 -            }
  44.178 -
  44.179 -            ||
  44.180 -
  44.181 -            ||
  44.182 -            public class «@name»_Return {
  44.183 -            `` apply "parm[creates|returns|inout[../int]]", 1, mode=java with "public", 1;
  44.184 -            }
  44.185 -
  44.186 -            ||
  44.187 -        }
  44.188 -
  44.189 -        if "$convert"
  44.190 -        ||
  44.191 -        private native `call "returnInter"` _«@name»(
  44.192 -        `` apply "parm[in|inout]", 2, mode=intermediate
  44.193 -            ) throws pEpException;
  44.194 -
  44.195 -        ||
  44.196 -
  44.197 -        ||
  44.198 -        public `if "not($convert)" > native ``call "returnJava"` «@name»(
  44.199 -        `` apply "parm[in|inout]", 2, mode=java
  44.200 -            ) throws pEpException`if "not($convert)" > ;`
  44.201 -        ||
  44.202 -
  44.203 -        if "$convert" {
  44.204 -            | {
  44.205 -            apply "parm", mode=convertIn;
  44.206 -
  44.207 -            if "$singlereturn" choose {
  44.208 -
  44.209 -                // TODO factorize type conversion with multireturn and gen_java_message.java
  44.210 -
  44.211 -                when "count(parm[returns][stringlist|string|sstring]) > 0"
  44.212 -                    |> return AbstractEngine.toUTF16(_«@name»(`apply "parm[in]", mode=call;`));
  44.213 -                when "count(parm[returns]/stringpairlist) > 0"
  44.214 -                    ||
  44.215 -                        ArrayList<Pair<byte[], byte[]>> glist = _«@name»(`apply "parm[in]", mode=call;`);
  44.216 -                        if(glist != null){
  44.217 -                            ArrayList<Pair<String, String>> list = new ArrayList<Pair<String, String>>();
  44.218 -                            for (Pair<byte[],byte[]> i : glist)
  44.219 -                                list.add(new Pair<String, String>(AbstractEngine.toUTF16(i.first), AbstractEngine.toUTF16(i.second)));
  44.220 -                            return list;
  44.221 -                        }
  44.222 -                        return null;
  44.223 -                    ||
  44.224 -
  44.225 -                when "count(parm[returns]/identity) > 0"
  44.226 -                    |> return new Identity(_«@name»(`apply "parm[in]", mode=call;`));
  44.227 -                when "count(parm[returns]/identitylist) > 0"
  44.228 -                    ||
  44.229 -                        Vector<_Identity> glist = _«@name»(`apply "parm[in]", mode=call;`);
  44.230 -                        if(glist != null){
  44.231 -                            Vector<Identity> list = new Vector<Identity>();
  44.232 -                            for (_Identity i : glist)
  44.233 -                                list.add(new Identity(i));
  44.234 -                            return list;
  44.235 -                        }
  44.236 -                        return null;
  44.237 -                    ||
  44.238 -                otherwise
  44.239 -                    |> return _«@name»(`apply "parm[in]", mode=call;`);
  44.240 -            }
  44.241 -
  44.242 -            if "$multireturn"
  44.243 -            ||
  44.244 -                _«@name»_Return _result = _«@name»(`apply "parm[in|inout]", mode=call;`);
  44.245 -                «@name»_Return result = new «@name»_Return();
  44.246 -                `` apply "parm[creates|returns|inout[../int]]", mode=convertOut;
  44.247 -                return result;
  44.248 -            ||
  44.249 -
  44.250 -            if "not($singlereturn) and not($multireturn)"
  44.251 -            ||
  44.252 -                _«@name»(`apply "parm[in]", mode=call;`);
  44.253 -                return;
  44.254 -            ||
  44.255 -                
  44.256 -            ||
  44.257 -            }
  44.258 -            ||
  44.259 -        }
  44.260 -        ||
  44.261 -
  44.262 -        ||
  44.263 -    }
  44.264 -
  44.265 -    template "parm", mode=java {
  44.266 -        param "public", 0;
  44.267 -        const "dir", "name(*[1])";
  44.268 -        const "type", "name(*[2])";
  44.269 -        const "name", "name(*[3])";
  44.270 -
  44.271 -        indent(0);
  44.272 -
  44.273 -        if "$public" > public 
  44.274 -        call "toJava" with "type", "$type";
  44.275 -        >  «translate($name,'-','_')»
  44.276 -        choose {
  44.277 -            when "$public" > ;
  44.278 -            otherwise if "position()!=last()" > ,
  44.279 -        }
  44.280 -        > \n
  44.281 -    }
  44.282 -
  44.283 -    template "parm", mode=intermediate {
  44.284 -        param "public", 0;
  44.285 -        const "dir", "name(*[1])";
  44.286 -        const "type", "name(*[2])";
  44.287 -        const "name", "name(*[3])";
  44.288 -
  44.289 -        indent(0);
  44.290 -
  44.291 -        if "$public" > public 
  44.292 -        call "toIntermediate" with "type", "$type", with "dir", "$dir";
  44.293 -        >  «translate($name,'-','_')»
  44.294 -        choose {
  44.295 -            when "$public" > ;
  44.296 -            otherwise if "position()!=last()" > ,
  44.297 -        }
  44.298 -        > \n
  44.299 -    }
  44.300 -
  44.301 -    template "parm", mode=convertIn {
  44.302 -        const "dir", "name(*[1])";
  44.303 -        const "type", "name(*[2])";
  44.304 -        const "name" call "lcase" with "text", "name(*[3])";
  44.305 -
  44.306 -        if "$dir = 'in' or $dir = 'inout'" choose {
  44.307 -            when "$type = 'string' or $type = 'stringlist'"
  44.308 -                | ¡$type _«$name» = AbstractEngine.toUTF8(«$name»);
  44.309 -            when "$type = 'identity'"
  44.310 -                ||
  44.311 -                _Identity _«$name» = null; 
  44.312 -                if («$name» != null) {
  44.313 -                    _«$name» = new _Identity(«$name»);
  44.314 -                }
  44.315 -                ||
  44.316 -            when "$type = 'identitylist'"
  44.317 -                ||
  44.318 -                Vector<_Identity> _«$name» = new Vector<_Identity>();
  44.319 -                for (Identity i : «$name»)
  44.320 -                    _«$name».add(new _Identity(i));
  44.321 -                ||
  44.322 -        }
  44.323 -    }
  44.324 -
  44.325 -    template "parm", mode=convertOut {
  44.326 -        const "dir", "name(*[1])";
  44.327 -        const "type", "name(*[2])";
  44.328 -        const "name" call "lcase" with "text", "name(*[3])";
  44.329 -
  44.330 -        if "$dir != 'in'" choose {
  44.331 -            when "$type = 'string' or $type = 'stringlist'"
  44.332 -                | result.«$name» = AbstractEngine.toUTF16(_result.«$name»);
  44.333 -            when "$type = 'identity'"
  44.334 -                | result.«$name» = Identity(«$name»);
  44.335 -            otherwise
  44.336 -                | result.«$name» = _result.«$name»;
  44.337 -        }
  44.338 -    }
  44.339 -
  44.340 -    template "parm", mode=call {
  44.341 -        const "type", "name(*[2])";
  44.342 -        const "name" call "lcase" with "text", "name(*[3])";
  44.343 -
  44.344 -        if "$type = 'string' or $type = 'stringlist' or $type = 'identity' or $type = 'identitylist'"
  44.345 -            > _
  44.346 -        > «$name»`if "position()!=last()" > , `
  44.347 -    }
  44.348 -}
  44.349 -
    45.1 --- a/src/gen_java_Message.ysl2	Tue Aug 18 21:57:06 2020 +0200
    45.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    45.3 @@ -1,252 +0,0 @@
    45.4 -include yslt.yml2
    45.5 -
    45.6 -tstylesheet {
    45.7 -    include ./textutils.ysl2
    45.8 -    include ./types_java.ysl2
    45.9 -
   45.10 -    template "/namespace[@name='pEp']" apply "struct|enum|exception", 0;
   45.11 -
   45.12 -    template "struct" {
   45.13 -        const "cname" call "toJava" with "type", "@name";
   45.14 -        document("foundation/pEp/jniadapter/{$cname}.java", "text")
   45.15 -            ||
   45.16 -            package foundation.pEp.jniadapter;
   45.17 -
   45.18 -            import java.util.ArrayList;
   45.19 -            import java.util.Vector;
   45.20 -            import java.util.Date;
   45.21 -            import java.util.HashMap;
   45.22 -            import java.io.Serializable;
   45.23 -
   45.24 -            public class «$cname» implements AutoCloseable, Serializable {
   45.25 -                private static final long serialVersionUID = 2119420428331150924L;
   45.26 -                private long handle;
   45.27 -
   45.28 -                native long init();
   45.29 -                native void release(long handle);
   45.30 -
   45.31 -                public «$cname»() {
   45.32 -                    handle = init();
   45.33 -                }
   45.34 -
   45.35 -                private native long _«$cname»(
   45.36 -                        byte[] mime_text
   45.37 -                    ) throws pEpException;
   45.38 -                
   45.39 -                public «$cname»(String mime_text) {
   45.40 -                    byte[] _mime_text = AbstractEngine.toUTF8(mime_text);
   45.41 -                    handle = _«$cname»(_mime_text);
   45.42 -                }
   45.43 -
   45.44 -                public native byte[] _encodeMIME() throws pEpException;
   45.45 -
   45.46 -                public String encodeMIME() {
   45.47 -                    return AbstractEngine.toUTF16(_encodeMIME());
   45.48 -                }
   45.49 -
   45.50 -                private «$cname»(long h) {
   45.51 -                    handle = h;
   45.52 -                }
   45.53 -
   45.54 -                public final void close() {
   45.55 -                    release(handle);
   45.56 -                }
   45.57 -
   45.58 -                final protected long getHandle() {
   45.59 -                    return handle;
   45.60 -                }
   45.61 -
   45.62 -                `` apply "enum", mode=inner
   45.63 -                `` apply "*[name(.)!='enum']", mode=entry
   45.64 -            }
   45.65 -            ||
   45.66 -    }
   45.67 -
   45.68 -    template "enum|exception" {
   45.69 -        const "jname" call "toJava" with "type", "@name";
   45.70 -        document("foundation/pEp/jniadapter/{$jname}.java", "text")
   45.71 -            ||
   45.72 -            package foundation.pEp.jniadapter;
   45.73 -
   45.74 -            import java.util.HashMap;
   45.75 -            
   45.76 -            `` apply ".", 0, mode=inner
   45.77 -            ||
   45.78 -    }
   45.79 -
   45.80 -    template "enum|exception", mode=inner {
   45.81 -        const "jname" call "CamelCase" with "text", "@name";
   45.82 -        ||
   45.83 -        public enum «$jname» {
   45.84 -            `` apply "*", mode=value
   45.85 -            ;
   45.86 -
   45.87 -            public final int value;
   45.88 -
   45.89 -            private static HashMap<Integer, «$jname»> intMap;
   45.90 -
   45.91 -            private «$jname»(int value) {
   45.92 -                this.value = value;
   45.93 -            }
   45.94 -
   45.95 -            public static «$jname» getByInt(int value){
   45.96 -                if (intMap == null) {
   45.97 -                    intMap = new HashMap<Integer, «$jname»>();
   45.98 -                    for («$jname» s : «$jname».values()) {
   45.99 -                        intMap.put(s.value, s);
  45.100 -                    }
  45.101 -                }
  45.102 -                if (intMap.containsKey(value)) {
  45.103 -                    return intMap.get(value);
  45.104 -                }
  45.105 -                return null;
  45.106 -            }
  45.107 -        }
  45.108 -        ||
  45.109 -    }
  45.110 -
  45.111 -    template "*", mode=entry {
  45.112 -        const "ctype", "name(.)";
  45.113 -        const "type" call "toJava" with "type", "name(.)";
  45.114 -        const "itype" call "toIntermediate" with "type", "name(.)";
  45.115 -        const "name" call "toJava" with "type", "name(*[position()=1])";
  45.116 -
  45.117 -        choose {
  45.118 -            when "$ctype = 'identity'"
  45.119 -            ||
  45.120 -            private native «$itype» _get«$name»();
  45.121 -            private native void _set«$name»(«$itype» value);
  45.122 -            public «$type» get«$name»() {
  45.123 -                «$itype» res = _get«$name»();
  45.124 -                if(res != null){
  45.125 -                    return new «$type»(_get«$name»());
  45.126 -                }else{
  45.127 -                    return null;
  45.128 -                }
  45.129 -            }
  45.130 -            public void set«$name»(«$type» value) {
  45.131 -                if(value != null)
  45.132 -                    _set«$name»(new «$itype»(value));
  45.133 -                else
  45.134 -                    _set«$name»(null);
  45.135 -            }
  45.136 -            
  45.137 -            ||
  45.138 -
  45.139 -            when "$ctype = 'identitylist' or $ctype = 'bloblist' or $ctype = 'stringlist' or $ctype = 'stringpairlist'"
  45.140 -            {
  45.141 -
  45.142 -                const "ename", "substring-after(substring($type,1,string-length($type)-1), '<')";
  45.143 -                const "iename" choose {
  45.144 -                    when "$ctype = 'stringlist'" > byte[]
  45.145 -                    when "$ctype = 'stringpairlist'" > Pair<byte[],byte[]>
  45.146 -                    otherwise > _«$ename»
  45.147 -                }
  45.148 -                const "convget" choose {
  45.149 -                    when "$ctype = 'stringlist'" > AbstractEngine.toUTF16(i)
  45.150 -                    when "$ctype = 'stringpairlist'" > new Pair<String, String>(AbstractEngine.toUTF16(i.first), AbstractEngine.toUTF16(i.second))
  45.151 -                    otherwise > new «$ename»(i)
  45.152 -                }
  45.153 -                const "convset" choose {
  45.154 -                    when "$ctype = 'stringlist'" > AbstractEngine.toUTF8(i)
  45.155 -                    when "$ctype = 'stringpairlist'" > new Pair<byte[],byte[]>(AbstractEngine.toUTF8(i.first), AbstractEngine.toUTF8(i.second))
  45.156 -                    otherwise > new _«$ename»(i)
  45.157 -                }
  45.158 -                ||
  45.159 -                private native «$itype» _get«$name»();
  45.160 -                private native void _set«$name»(«$itype» value);
  45.161 -                public «$type» get«$name»() {
  45.162 -                    «$itype» glist = _get«$name»();
  45.163 -                    if(glist != null){
  45.164 -                        «$type» list = new «$type»();
  45.165 -                        for («$iename» i : glist)
  45.166 -                            list.add(«$convget»);
  45.167 -                        return list;
  45.168 -                    }
  45.169 -                    return null;
  45.170 -                }
  45.171 -                public void set«$name»(«$type» value) {
  45.172 -                    if(value != null){
  45.173 -                        «$itype» list = new «$itype»();
  45.174 -                        for («$ename» i : value)
  45.175 -                            list.add(«$convset»);
  45.176 -                        _set«$name»(list);
  45.177 -                    }else{
  45.178 -                        _set«$name»(null);
  45.179 -                    }
  45.180 -                }
  45.181 -                
  45.182 -                ||
  45.183 -            }
  45.184 -            
  45.185 -            when "$itype != $type"
  45.186 -            ||
  45.187 -            private native «$itype» _get«$name»();
  45.188 -            private native void _set«$name»(«$itype» value);
  45.189 -            public «$type» get«$name»() {
  45.190 -                «$itype» res = _get«$name»();
  45.191 -                if(res != null)
  45.192 -                    return AbstractEngine.toUTF16(res);
  45.193 -                else
  45.194 -                    return null;
  45.195 -            }
  45.196 -            public void set«$name»(«$type» value) {
  45.197 -                if(value != null)
  45.198 -                    _set«$name»(AbstractEngine.toUTF8(value));
  45.199 -                else
  45.200 -                    _set«$name»(new byte[0]);
  45.201 -            }
  45.202 -
  45.203 -            ||
  45.204 -
  45.205 -            when "$itype != $type"
  45.206 -            ||
  45.207 -            private native «$itype» _get«$name»();
  45.208 -            private native void _set«$name»(«$itype» value);
  45.209 -            public «$type» get«$name»() {
  45.210 -                «$itype» res = _get«$name»();
  45.211 -                if(res != null)
  45.212 -                    return AbstractEngine.toUTF16(res);
  45.213 -                else
  45.214 -                    return null;
  45.215 -            }
  45.216 -            public void set«$name»(«$type» value) {
  45.217 -                if(value != null)
  45.218 -                    _set«$name»(AbstractEngine.toUTF8(value));
  45.219 -                else
  45.220 -                    _set«$name»(null);
  45.221 -            }
  45.222 -
  45.223 -            ||
  45.224 -
  45.225 -            when "../enum[@name=$ctype]"
  45.226 -            ||
  45.227 -            private native int _get«$name»();
  45.228 -            public «$itype» get«$name»() {
  45.229 -                return «$itype».getByInt(_get«$name»());
  45.230 -            }
  45.231 -            private native void _set«$name»(int value);
  45.232 -            public void set«$name»(«$itype» value) {
  45.233 -                if(value != null)
  45.234 -                    _set«$name»(value.value);
  45.235 -                else
  45.236 -                    _set«$name»(0);
  45.237 -            }
  45.238 -
  45.239 -            ||
  45.240 -
  45.241 -            otherwise
  45.242 -            ||
  45.243 -            public native «$itype» get«$name»();
  45.244 -            public native void set«$name»(«$itype» value);
  45.245 -
  45.246 -            ||
  45.247 -        }
  45.248 -    }
  45.249 -
  45.250 -    template "*", mode=value {
  45.251 -        const "name" call "toJava" with "type", "name(.)";
  45.252 -        | «$name» («.»)`if "position()!=last()" > , `
  45.253 -    }
  45.254 -}
  45.255 -
    46.1 --- a/src/gen_java_exceptions.ysl2	Tue Aug 18 21:57:06 2020 +0200
    46.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    46.3 @@ -1,35 +0,0 @@
    46.4 -include yslt.yml2
    46.5 -
    46.6 -tstylesheet {
    46.7 -    include ./textutils.ysl2
    46.8 -
    46.9 -    template "/" {
   46.10 -        | package foundation.pEp.jniadapter;
   46.11 -        | 
   46.12 -        | public class pEpException extends RuntimeException {
   46.13 -        |   public pEpException(String message) {
   46.14 -        |     super(message);
   46.15 -        |   }
   46.16 -        | }
   46.17 -
   46.18 -        apply "namespace/exception[@name='Status']", 0;
   46.19 -    }
   46.20 -
   46.21 -    function "exception" {
   46.22 -        param "name";
   46.23 -
   46.24 -        document "foundation/pEp/jniadapter/{$name}.java", "text" {
   46.25 -            | package foundation.pEp.jniadapter;
   46.26 -            |
   46.27 -            | public class «$name» extends pEpException {
   46.28 -            |   public «$name»(String message) {
   46.29 -            |     super(message);
   46.30 -            |   }
   46.31 -            | }
   46.32 -        }
   46.33 -    }
   46.34 -
   46.35 -    template "exception" for "*[text()!=0]" call "exception"
   46.36 -        with "name" call "CamelCase" with "text", "name(.)";
   46.37 -}
   46.38 -
    47.1 --- a/src/gen_throw_pEp_exception.ysl2	Tue Aug 18 21:57:06 2020 +0200
    47.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    47.3 @@ -1,64 +0,0 @@
    47.4 -include yslt.yml2
    47.5 -
    47.6 -tstylesheet {
    47.7 -    include ./textutils.ysl2
    47.8 -
    47.9 -    template "/" {
   47.10 -        apply "namespace", 0;
   47.11 -        document "throw_pEp_exception.hh", "text"
   47.12 -            ||
   47.13 -            #pragma once
   47.14 -
   47.15 -            #include <jni.h>
   47.16 -
   47.17 -            namespace pEp {
   47.18 -                namespace JNIAdapter {
   47.19 -                    jint throw_pEp_Exception(JNIEnv *env, PEP_STATUS status);
   47.20 -                };
   47.21 -            };
   47.22 -
   47.23 -            ||
   47.24 -
   47.25 -    }
   47.26 -
   47.27 -    template "namespace"
   47.28 -        ||
   47.29 -        #include <assert.h>
   47.30 -        #include <pEp/pEpEngine.h>
   47.31 -        #include "throw_pEp_exception.hh"
   47.32 -
   47.33 -        namespace pEp {
   47.34 -            namespace JNIAdapter {
   47.35 -                jint throw_pEp_Exception(JNIEnv *env, PEP_STATUS status)
   47.36 -                {
   47.37 -                    jclass ex;
   47.38 -                    const char *ex_name;
   47.39 -        
   47.40 -                    switch (status) {
   47.41 -                        `` apply "exception/*[text()!=0]", 4, mode=case
   47.42 -                        default:
   47.43 -                            assert(0);
   47.44 -                            ex_name = "Exception";
   47.45 -                    }
   47.46 -        
   47.47 -                    ex = env->FindClass(ex_name);
   47.48 -                    assert(ex);
   47.49 -        
   47.50 -                    if (ex == NULL) {
   47.51 -                        ex = env->FindClass("java/lang/NoClassDefFoundError");
   47.52 -                        assert(ex);
   47.53 -                    }
   47.54 -        
   47.55 -                    return env->ThrowNew(ex, ex_name);
   47.56 -                }
   47.57 -            };
   47.58 -        };
   47.59 -        ||
   47.60 -
   47.61 -    template "*", mode=case {
   47.62 -        | case `call "UCASE" with "text", "name(.)"`:
   47.63 -        |     ex_name = "foundation/pEp/jniadapter/`call "CamelCase" with "text", "name(.)"`";
   47.64 -        |     break;
   47.65 -    }
   47.66 -}
   47.67 -
    48.1 --- a/src/get_header.cc	Tue Aug 18 21:57:06 2020 +0200
    48.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    48.3 @@ -1,1 +0,0 @@
    48.4 -#include <pEp/pEpEngine.h>
    49.1 --- a/src/identity_api.cc	Tue Aug 18 21:57:06 2020 +0200
    49.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    49.3 @@ -1,16 +0,0 @@
    49.4 -#include <pEp/message_api.h>
    49.5 -#include <pEp/pEpLog.hh>
    49.6 -
    49.7 -#include "jniutils.hh"
    49.8 -
    49.9 -
   49.10 -extern "C" {
   49.11 -
   49.12 -JNIEXPORT jint JNICALL Java_foundation_pEp_jniadapter_Identity__1getRating(JNIEnv *env, jobject thiz, jint comm_type)
   49.13 -{
   49.14 -    pEpLog("called");
   49.15 -    return ::rating_from_comm_type((PEP_comm_type) comm_type);
   49.16 -}
   49.17 -
   49.18 -} // extern "C"
   49.19 -
    50.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    50.2 +++ b/src/java/foundation/pEp/jniadapter/AbstractEngine.java	Fri Aug 28 18:18:26 2020 +0200
    50.3 @@ -0,0 +1,198 @@
    50.4 +package foundation.pEp.jniadapter;
    50.5 +
    50.6 +import java.util.ArrayList;
    50.7 +import java.util.Vector;
    50.8 +
    50.9 +import foundation.pEp.jniadapter.Sync.DefaultCallback;
   50.10 +import foundation.pEp.jniadapter.interfaces.*;
   50.11 +import foundation.pEp.jniadapter.exceptions.*;
   50.12 +
   50.13 +
   50.14 +abstract class AbstractEngine extends UniquelyIdentifiable implements AbstractEngineInterface {
   50.15 +    static {
   50.16 +        System.loadLibrary("pEpJNI");
   50.17 +    }
   50.18 +
   50.19 +    private Sync.MessageToSendCallback messageToSendCallback;
   50.20 +    private Sync.NotifyHandshakeCallback notifyHandshakeCallback;
   50.21 +    private Sync.NeedsFastPollCallback needsFastPollCallback;
   50.22 +    private Sync.PassphraseRequiredCallback passphraseRequiredCallback;
   50.23 +
   50.24 +    private final static DefaultCallback defaultCallback = new DefaultCallback();
   50.25 +
   50.26 +    private native void init();
   50.27 +    private native void release();
   50.28 +
   50.29 +    private long keyserverThread;
   50.30 +    private long keyserverQueue;
   50.31 +
   50.32 +    public AbstractEngine() throws pEpException {
   50.33 +        synchronized (AbstractEngine.class) {
   50.34 +            init();
   50.35 +        }
   50.36 +    }
   50.37 +
   50.38 +    final public void close() {
   50.39 +        synchronized (AbstractEngine.class){
   50.40 +            release();
   50.41 +        }
   50.42 +    }
   50.43 +
   50.44 +    public String getVersion() {
   50.45 +        return _getVersion();
   50.46 +    }
   50.47 +
   50.48 +    private native String _getVersion();
   50.49 +
   50.50 +
   50.51 +    public String getProtocolVersion() {
   50.52 +        return _getProtocolVersion();
   50.53 +    }
   50.54 +
   50.55 +    private native String _getProtocolVersion();
   50.56 +
   50.57 +
   50.58 +    public void startKeyserverLookup() {
   50.59 +        _startKeyserverLookup();
   50.60 +    }
   50.61 +
   50.62 +    private native void _startKeyserverLookup();
   50.63 +
   50.64 +    public void stopKeyserverLookup() {
   50.65 +        _startKeyserverLookup();
   50.66 +    }
   50.67 +
   50.68 +    private native void _stopKeyserverLookup();
   50.69 +
   50.70 +
   50.71 +    public void startSync() {
   50.72 +        _startSync();
   50.73 +    }
   50.74 +
   50.75 +    private native void _startSync();
   50.76 +
   50.77 +    public void stopSync() {
   50.78 +        _stopSync();
   50.79 +    }
   50.80 +
   50.81 +    private native void _stopSync();
   50.82 +
   50.83 +    public boolean isSyncRunning() {
   50.84 +        return _isSyncRunning();
   50.85 +    }
   50.86 +
   50.87 +    private native boolean _isSyncRunning();
   50.88 +
   50.89 +    // Callbacks
   50.90 +    public void setMessageToSendCallback(Sync.MessageToSendCallback messageToSendCallback) {
   50.91 +        this.messageToSendCallback = messageToSendCallback;
   50.92 +    }
   50.93 +
   50.94 +    public void setNotifyHandshakeCallback(Sync.NotifyHandshakeCallback notifyHandshakeCallback) {
   50.95 +        this.notifyHandshakeCallback = notifyHandshakeCallback;
   50.96 +    }
   50.97 +
   50.98 +    public void setNeedsFastPollCallback(Sync.NeedsFastPollCallback needsFastPollCallback) {
   50.99 +        this.needsFastPollCallback = needsFastPollCallback;
  50.100 +    }
  50.101 +
  50.102 +    public void setPassphraseRequiredCallback(Sync.PassphraseRequiredCallback passphraseRequiredCallback) {
  50.103 +        System.out.println("passphraseRequiredCallback has been registered to:" + passphraseRequiredCallback.toString() + " on engine ObjID: " + getId());
  50.104 +
  50.105 +        this.passphraseRequiredCallback = passphraseRequiredCallback;
  50.106 +    }
  50.107 +
  50.108 +    private int needsFastPollCallFromC(boolean fast_poll_needed) {
  50.109 +        if (needsFastPollCallback != null) {
  50.110 +            needsFastPollCallback.needsFastPollCallFromC(fast_poll_needed);
  50.111 +        } else {
  50.112 +            defaultCallback.needsFastPollCallFromC(fast_poll_needed);
  50.113 +        }
  50.114 +        return 0;
  50.115 +    }
  50.116 +
  50.117 +    private int notifyHandshakeCallFromC(_Identity _myself, _Identity _partner, SyncHandshakeSignal _signal) {
  50.118 +        Identity myself = new Identity(_myself);
  50.119 +        Identity partner = (_partner != null) ? new Identity(_partner) : null;
  50.120 +
  50.121 +        System.out.println("pEpSync" +"notifyHandshakeCallFromC: " + notifyHandshakeCallback);
  50.122 +        if (notifyHandshakeCallback != null) {
  50.123 +            notifyHandshakeCallback.notifyHandshake(myself, partner, _signal);
  50.124 +        } else {
  50.125 +            defaultCallback.notifyHandshake(myself, partner, _signal);
  50.126 +        }
  50.127 +        return 0;
  50.128 +    }
  50.129 +
  50.130 +    private byte[] passphraseRequiredFromC(final PassphraseType passphraseType) {
  50.131 +        String ret = "";
  50.132 +        if (passphraseRequiredCallback != null) {
  50.133 +            System.out.println("calling passphraseRequiredCallback on engine ObjID:" + getId());
  50.134 +            ret = passphraseRequiredCallback.passphraseRequired(passphraseType);
  50.135 +        } else {
  50.136 +            System.out.println("no callback registered on engine ObjID:" + getId());
  50.137 +            // if this happens (no callback registered
  50.138 +            // we simply return ""
  50.139 +            // it will fail
  50.140 +            // this repeats MaxRetries times (currentluy hardcoded to 3)
  50.141 +            // Then the orig call will return with the PEP_STATUS (most likely PEP_PASSPHRASE_REQUIRED)
  50.142 +        }
  50.143 +        return Utils.toUTF8(ret);
  50.144 +    }
  50.145 +
  50.146 +    private int messageToSendCallFromC (Message message) {
  50.147 +        System.out.println("pEpSync" + "messageToSendCallFromC: " + messageToSendCallback );
  50.148 +        if (messageToSendCallback != null) {
  50.149 +            messageToSendCallback.messageToSend(message);
  50.150 +        } else {
  50.151 +            defaultCallback.messageToSend(message);
  50.152 +        }
  50.153 +        return 0;
  50.154 +    }
  50.155 +
  50.156 +    public Message incomingMessageFromPGPText(String pgpText, Message.EncFormat encFormat) {
  50.157 +        Message msg = new Message();
  50.158 +        msg.setDir(Message.Direction.Incoming);
  50.159 +        msg.setEncFormat(encFormat);
  50.160 +
  50.161 +        // Opts
  50.162 +        ArrayList<Pair<String, String>> opts = new ArrayList<>();
  50.163 +        Pair<String, String> xpEp = new Pair<>();
  50.164 +        xpEp.first = "X-pEp-Version";
  50.165 +        xpEp.second = this.getProtocolVersion();
  50.166 +        opts.add(xpEp);
  50.167 +        msg.setOptFields(opts);
  50.168 +
  50.169 +        if (encFormat == Message.EncFormat.PEP) {
  50.170 +            // For EncFormat.PEP
  50.171 +            // The pgpText goes into the attachment index 1
  50.172 +            msg.setShortmsg("p≡p");
  50.173 +            msg.setLongmsg("this message was encrypted with p≡p https://pEp-project.org");
  50.174 +
  50.175 +            // Attachments
  50.176 +            Blob att0 = new Blob();
  50.177 +            att0.mime_type = "application/pgp-encrypted";
  50.178 +            att0.filename = null;
  50.179 +            att0.data = "Version: 1".getBytes();
  50.180 +
  50.181 +            Blob att1 = new Blob();
  50.182 +            att1.mime_type = "application/octet-stream";
  50.183 +            att1.filename = "file://msg.asc";
  50.184 +            att1.data = pgpText.getBytes();
  50.185 +
  50.186 +            Vector<Blob> attachments = new Vector<>();
  50.187 +            attachments.add(att0);
  50.188 +            attachments.add(att1);
  50.189 +            msg.setAttachments(attachments);
  50.190 +        } else if (encFormat == Message.EncFormat.PEPEncInlineEA) {
  50.191 +            // For EncFormat.PEPEncInlineEA
  50.192 +            // The pgpText goes into the longMessage
  50.193 +            msg.setShortmsg("");
  50.194 +            msg.setLongmsg(pgpText);
  50.195 +        } else {
  50.196 +            throw new pEpCannotEncode("Message.Encformat not supported: " + encFormat.toString());
  50.197 +        }
  50.198 +
  50.199 +        return msg;
  50.200 +    }
  50.201 +}
  50.202 \ No newline at end of file
    51.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    51.2 +++ b/src/java/foundation/pEp/jniadapter/Blob.java	Fri Aug 28 18:18:26 2020 +0200
    51.3 @@ -0,0 +1,40 @@
    51.4 +package foundation.pEp.jniadapter;
    51.5 +
    51.6 +import foundation.pEp.jniadapter.interfaces.*;
    51.7 +import java.io.Serializable;
    51.8 +
    51.9 +public class Blob implements BlobInterface, Serializable {
   51.10 +    public byte[] data;
   51.11 +    public String mime_type;
   51.12 +    public String filename;
   51.13 +
   51.14 +    public Blob() {
   51.15 +        mime_type = "application/octet-stream";
   51.16 +    }
   51.17 +
   51.18 +    Blob(_Blob b) {
   51.19 +        data = b.data;
   51.20 +        mime_type = Utils.toUTF16(b.mime_type);
   51.21 +        filename = Utils.toUTF16(b.filename);
   51.22 +    }
   51.23 +
   51.24 +    /**
   51.25 +     * Human readable string representation of Blob.
   51.26 +     * The data field is ASN.1 XER decoded for mime_types:
   51.27 +     * "application/pEp.sync"
   51.28 +     * "application/pEp.keyreset"
   51.29 +     *
   51.30 +     * @return String Blob as String
   51.31 +     */
   51.32 +    public String toString() {
   51.33 +        _Blob _b = new _Blob(this);
   51.34 +        String ret = "";
   51.35 +        ret += "mime_type: \"" + mime_type + "\"\n";
   51.36 +        ret += "filename: \"" + filename + "\"\n";
   51.37 +        ret += "data plain: \"" + Utils.toUTF16(data) + "\"\n";
   51.38 +        ret += "data decoded: \"" + _b.dataToXER() + "\"\n";
   51.39 +        return ret;
   51.40 +    }
   51.41 +
   51.42 +}
   51.43 +
    52.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    52.2 +++ b/src/java/foundation/pEp/jniadapter/CommType.java	Fri Aug 28 18:18:26 2020 +0200
    52.3 @@ -0,0 +1,80 @@
    52.4 +package foundation.pEp.jniadapter;
    52.5 +
    52.6 +import java.util.HashMap;
    52.7 +
    52.8 +public enum CommType {
    52.9 +    PEP_ct_unknown (0),
   52.10 +
   52.11 +    // range 0x01 to 0x09: no encryption, 0x0a to 0x0e: nothing reasonable
   52.12 +
   52.13 +    PEP_ct_no_encryption (0x01),                // generic
   52.14 +    PEP_ct_no_encrypted_channel (0x02),
   52.15 +    PEP_ct_key_not_found (0x03),
   52.16 +    PEP_ct_key_expired (0x04),
   52.17 +    PEP_ct_key_revoked (0x05),
   52.18 +    PEP_ct_key_b0rken (0x06),
   52.19 +    PEP_ct_my_key_not_included (0x09),
   52.20 +
   52.21 +    PEP_ct_security_by_obscurity (0x0a),
   52.22 +    PEP_ct_b0rken_crypto (0x0b),
   52.23 +    PEP_ct_key_too_short (0x0c),
   52.24 +
   52.25 +    PEP_ct_compromized (0x0e),                  // known compromized connection
   52.26 +    PEP_ct_mistrusted (0x0f),                   // known mistrusted key
   52.27 +
   52.28 +    // range 0x10 to 0x3f: unconfirmed encryption
   52.29 +
   52.30 +    PEP_ct_unconfirmed_encryption (0x10),       // generic
   52.31 +    PEP_ct_OpenPGP_weak_unconfirmed (0x11),	    // RSA 1024 is weak
   52.32 +
   52.33 +    PEP_ct_to_be_checked (0x20),                // generic
   52.34 +    PEP_ct_SMIME_unconfirmed (0x21),
   52.35 +    PEP_ct_CMS_unconfirmed (0x22),
   52.36 +
   52.37 +    PEP_ct_strong_but_unconfirmed (0x30),       // generic
   52.38 +    PEP_ct_OpenPGP_unconfirmed (0x38),          // key at least 2048 bit RSA or EC
   52.39 +    PEP_ct_OTR_unconfirmed (0x3a),
   52.40 +
   52.41 +    // range 0x40 to 0x7f: unconfirmed encryption and anonymization
   52.42 +
   52.43 +    PEP_ct_unconfirmed_enc_anon (0x40),         // generic
   52.44 +    PEP_ct_PEP_unconfirmed (0x7f),
   52.45 +
   52.46 +    PEP_ct_confirmed (0x80),                    // this bit decides if trust is confirmed
   52.47 +
   52.48 +    // range 0x81 to 0x8f: reserved
   52.49 +    // range 0x90 to 0xbf: confirmed encryption
   52.50 +
   52.51 +    PEP_ct_confirmed_encryption (0x90),         // generic
   52.52 +	PEP_ct_OpenPGP_weak (0x91),                 // RSA 1024 is weak
   52.53 +
   52.54 +    PEP_ct_to_be_checked_confirmed (0xa0),      //generic
   52.55 +    PEP_ct_SMIME (0xa1),
   52.56 +    PEP_ct_CMS (0xa2),
   52.57 +
   52.58 +    PEP_ct_strong_encryption (0xb0),            // generic
   52.59 +	PEP_ct_OpenPGP (0xb8),                      // key at least 2048 bit RSA or EC
   52.60 +	PEP_ct_OTR (0xba),
   52.61 +
   52.62 +    // range 0xc0 to 0xff: confirmed encryption and anonymization
   52.63 +
   52.64 +    PEP_ct_confirmed_enc_anon (0xc0),           // generic
   52.65 +	PEP_ct_pEp (0xff);
   52.66 +
   52.67 +    static class Management {
   52.68 +        public static final HashMap<Integer, CommType> tag =
   52.69 +                new HashMap<Integer, CommType>();
   52.70 +    }
   52.71 +
   52.72 +    public final int value;
   52.73 +
   52.74 +    CommType(int value) {
   52.75 +        this.value = value;
   52.76 +        fill(value);
   52.77 +    }
   52.78 +
   52.79 +    private void fill(int value) {
   52.80 +        Management.tag.put(value, this);
   52.81 +    }