* #35924 (zrtp): switch to libzrtpcpp
diff --git a/jni/libzrtp/sources/CMakeLists.txt b/jni/libzrtp/sources/CMakeLists.txt
index 399e776..752e29f 100755
--- a/jni/libzrtp/sources/CMakeLists.txt
+++ b/jni/libzrtp/sources/CMakeLists.txt
@@ -11,37 +11,54 @@
 cmake_minimum_required(VERSION 2.6)
 
 PROJECT(libzrtpcpp)
-set (VERSION 2.3.4)
-set (PACKAGE libzrtpcpp)
-set (SOVERSION ${VERSION})
-STRING(REGEX REPLACE "[.].*$" "" SOVERSION ${SOVERSION})
 
-SET(CPACK_PACKAGE_VERSION_MAJOR ${SOVERSION})
-SET(CPACK_PACKAGE_VERSION_MINOR ${VERSION})
-SET(CPACK_PACKAGE_VERSION_PATCH ${VERSION})
-STRING(REGEX REPLACE "[.][0-9]*$" "" CPACK_PACKAGE_VERSION_MINOR ${VERSION})
-STRING(REGEX REPLACE ".*[.]" "" CPACK_PACKAGE_VERSION_MINOR ${CPACK_PACKAGE_VERSION_MINOR})
-STRING(REGEX REPLACE ".*[.]" "" CPACK_PACKAGE_VERSION_PATCH ${VERSION})
+SET(CPACK_PACKAGE_VERSION_MAJOR 4)
+SET(CPACK_PACKAGE_VERSION_MINOR 0)
+SET(CPACK_PACKAGE_VERSION_PATCH 0)
 
-if(CMAKE_GENERATOR MATCHES "Unix Makefiles")
-	add_custom_target(cleandist
-		WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
-		COMMAND rm -f "${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}[-_]*.gz"
-		COMMAND rm -f "${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}_*.dsc"
-		COMMAND rm -f "${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}-*.rpm"
-		COMMAND rm -f "${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}[-_]*.deb"
-    COMMAND rm -f "${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}[-_]*.changes"
-		COMMAND rm -f "${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}-*.zip"
-	)
+set (VERSION 4.0.0)
+set (SOVERSION 4)
 
-	add_custom_target(dist
-		DEPENDS cleandist
-		WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
-		COMMAND git archive --format tar --output="${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}-${VERSION}.tar" --prefix="${PACKAGE}-${VERSION}/" HEAD
-		COMMAND gzip "${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}-${VERSION}.tar"
-	)
+# Define supported command line parameters.
+#
+# Example to build the tivi client: cmake -DTIVI=true ..
+# Without any options cmake generates libzrtpcpp for use with GNU ccRTP
+#
+option(CCRTP "Build library to use with GNU ccRTP." OFF)
+option(CORE_LIB "Build core library only, no spcific client support." OFF)
+option(CRYPTO_STANDALONE "Use embedded crypto and big number modules." ON)
+option(TIVI "Build library for the tivi client, implies '-DCRYPTO_STNDALONE=true'." OFF)
+option(SQLITE "Use SQLite DB as backend for ZRTP cache." OFF)
+
+option(ANDROID "Generate Android makefiles (Android.mk)" ON)
+option(JAVA "Generate Java support files (requires JDK and SWIG)" OFF)
+
+
+# **** Check what and how to build ****
+#
+if (CCRTP AND TIVI)
+    MESSAGE(FATAL_ERROR "Cannot build more than one client at once. Use different build directories.")
 endif()
 
+if (CCRTP)
+    set (PACKAGE libzrtpcpp)
+    set(zrtplibName zrtpcpp)
+elseif (TIVI)
+    set (PACKAGE libzrtptivi)
+    set(zrtplibName zrtptivi)
+    set(CRYPTO_STANDALONE true)
+    set(SQLITE true)
+elseif (CORE_LIB)
+    set (PACKAGE libzrtpcore)
+    set(zrtplibName zrtpcppcore)
+else()
+    MESSAGE(WARNING "No client defined, building for GNU ccRTP.")
+    set (PACKAGE libzrtpcpp)
+    set(CCRTP true)
+    set(zrtplibName zrtpcpp)
+endif()
+
+
 if(MSVC60)
     set(BUILD_STATIC ON CACHE BOOL "static linking only" FORCE)
     MARK_AS_ADVANCED(BUILD_STATIC)
@@ -50,26 +67,18 @@
 endif()
 
 # set to true for debug and trace during CMakeLists development
-set(CMAKE_VERBOSE_MAKEFILE FALSE)
+# set(CMAKE_VERBOSE_MAKEFILE TRUE)
 
-MESSAGE( STATUS "Configuring GNU ${PROJECT_NAME} ${VERSION}...")
+execute_process(COMMAND git rev-parse --short HEAD OUTPUT_VARIABLE GIT_COMMIT)
+STRING(REGEX REPLACE "(\r?\n)+$" "" GIT_COMMIT "${GIT_COMMIT}")
+
+MESSAGE( STATUS "Configuring GNU ${PROJECT_NAME} ${VERSION} for ${PACKAGE}, commit: ${GIT_COMMIT} ...")
 
 # include most of the fine stuff we need
-include(cmake/Modules/FindGcryptConfig.cmake)
 include(FindPkgConfig)
 include(CheckLibraryExists)
 include(CheckIncludeFiles)
-include(cmake/Modules/AutoArgs.cmake)
-
-if(${PROJECT_NAME} STREQUAL ${CMAKE_PROJECT_NAME})
-    include(cmake/Modules/GeneratePackage.cmake)
-
-    GENERATE_PACKAGING(${PACKAGE} ${VERSION})
-endif()
-
-# check the -Denable-ccrtp setting, defaults to true
-enable_arg(ccrtp true "Enable GNU ccRTP support for GNU ZRTP")
-args_help()
+include(CheckFunctionExists)
 
 if (NOT LIB_SUFFIX)
     set(LIBDIRNAME "lib")
@@ -83,118 +92,174 @@
     set(LIBDIRNAME "lib${LIB_SUFFIX}")
 endif()
 
-# setup the Thread include and lib
-find_package(Threads)
-if(CMAKE_HAVE_PTHREAD_H)
-  set(HAVE_PTHREAD_H TRUE)
-endif()
-set(LIBS ${LIBS} ${CMAKE_THREAD_LIBS_INIT})
-
-# define the name of the lib. zrtpcppcore does not include the ccRTP stuff.
-set(zrtplib zrtpcppcore)
-if(enable_ccrtp)
-    if (USES_CCRTP_INCLUDE_DIRS)
-        message(STATUS "  Using local commoncpp dependency")
-    else()
-        find_package(PkgConfig)
-        pkg_check_modules(USES_CCRTP libccrtp>=2.0.0)
-    endif()
-    include_directories(${USES_CCRTP_INCLUDE_DIRS})
-    link_directories(${USES_CRTP_LIBRARY_DIRS})
-    add_definitions(${USES_CCRTP_CFLAGS})
-    set (LIBS ${LIBS} ${USES_CCRTP_LDFLAGS} ${USES_CCRTP_LIBRARIES})
-    set(zrtplib zrtpcpp)
-endif()
-
-# now get info about crypto libraries
-pkg_check_modules(OPENSSL libcrypto>=0.9.8)
-if (OPENSSL_FOUND)
-  set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES} ${OPENSSL_INCLUDE_DIRS}) #update include files search directory
-  check_include_files(openssl/bn.h HAVE_OPENSSL_BN_H)
-  check_include_files(openssl/aes.h HAVE_OPENSSL_AES_H)
-  check_include_files(openssl/sha.h HAVE_OPENSSL_SHA_H)
-  check_include_files(openssl/ec.h HAVE_OPENSSL_EC_H)
-  check_library_exists(crypto EVP_CipherInit_ex "${OPENSSL_LIBDIR}" HAVE_SSL_CRYPT) #use search lib directory from pkg-config
-  set(LIBS ${LIBS} -lcrypto)
-  set(CRYPTOBACKEND "libcrypto >= 0.9.8")
-  set(BUILD_REQ "libopenssl-devel >= 0.9.8")
-  set(PACKAGE_REQ "libopenssl >= 0.9.8")
-  include_directories(${OPENSSL_INCLUDE_DIRS}) #update includes directory from pkg-config
-endif()
-
-if(NOT HAVE_OPENSSL_EC_H)
-  gcr_check(GCRYPT gcrypt)
-  if(GCRYPT_FOUND)
-    check_include_files(gcrypt.h HAVE_GCRYPT_H)
-    set(LIBS ${LIBS} ${GCRYPT_LIBRARIES})
-    set(BUILD_REQ "libgcrypt-devel")
-    set(CRYPTOBACKEND="")
-    set(PACKAGE_REQ "libgcrypt")
-  endif()
-endif()
-
-if(NOT OPENSSL_FOUND AND NOT GCRYPT_FOUND)
-  message(FATAL_ERROR "No crypto library found")
-endif()
-
 check_include_files(stdlib.h HAVE_STDLIB_H)
 check_include_files(string.h HAVE_STRING_H)
 
-# necessary and required modules checked, ready to generate config.h
-configure_file(libzrtpcpp-config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/libzrtpcpp-config.h)
-include_directories(${CMAKE_CURRENT_BINARY_DIR})
+if (NOT CRYPTO_STANDALONE)
+    pkg_check_modules(OPENSSL libcrypto>=0.9.8)
+    if (OPENSSL_FOUND)
+      set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES} ${OPENSSL_INCLUDE_DIRS}) #update include files search directory
+      check_include_files(openssl/bn.h HAVE_OPENSSL_BN_H)
+      check_include_files(openssl/aes.h HAVE_OPENSSL_AES_H)
+      check_include_files(openssl/sha.h HAVE_OPENSSL_SHA_H)
+      check_library_exists(crypto EVP_CipherInit_ex "${OPENSSL_LIBDIR}" HAVE_SSL_CRYPT) #use search lib directory from pkg-config
+      set(LIBS ${LIBS} -lcrypto)
+      set(CRYPTOBACKEND "libcrypto >= 0.9.8")
+      set(BUILD_REQ "libopenssl-devel >= 0.9.8")
+      set(PACKAGE_REQ "libopenssl >= 0.9.8")
+      include_directories(${OPENSSL_INCLUDE_DIRS}) #update includes directory from pkg-config
+  else()
+      message(FATAL_ERROR "No crypto library found")
+  endif()
+else()
+    # For crypto standalone mode we need to configure the bnlib. In a first step
+    # without the tests and demos.
+    check_include_files(stdint.h HAVE_STDINT_H)
+    check_include_files(stdint.h HAVE_ASSERT_H)
+    check_include_files(limits.h HAVE_LIMITS_H)
 
-# the following set(...) commands are only to have backward
-# compatibility with autoconf stuff to generate the pc file
-set(prefix ${CMAKE_INSTALL_PREFIX})
-set(exec_prefix ${prefix}/bin)
-set(libdir ${prefix}/${LIBDIRNAME})
-set(includedir ${prefix}/include)
-set(PACKAGE pkgconfig)
-configure_file(libzrtpcpp.pc.cmake ${CMAKE_CURRENT_BINARY_DIR}/lib${zrtplib}.pc @ONLY)
+    check_function_exists(memmove HAVE_MEMMOVE)
+    check_function_exists(memcpy HAVE_MEMCPY)
 
-configure_file(libzrtpcpp.spec.cmake ${CMAKE_CURRENT_BINARY_DIR}/libzrtpcpp.spec @ONLY)
+    # TODO: check if we compile the tests for bnlib
+    #
+    # check_function_exists(clock_gettime HAVE_CLOCK_GETTIME)
+    # check_function_exists(getrusage HAVE_GETRUSAGE)
+    # check_function_exists(clock HAVE_CLOCK)
+    # check_function_exists(time HAVE_TIME)
 
-#to make sure includes are first taken from those directory
-include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/src)
+    # Check if Solaris-style gethrvtime() is available
+    # check_function_exists(gethrvtime HAVE_GETHRVTIME)
+    #
+    # until here
+
+    # necessary and required modules checked, ready to generate config.h
+    configure_file(${CMAKE_SOURCE_DIR}/bnlib/bnconfig.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/bnconfig.h)
+endif()
+
+if (SQLITE)
+    pkg_check_modules(SQLITE3 sqlite3>=3.7)
+    if (SQLITE3_FOUND)
+        check_include_files(sqlite3.h HAVE_SQLITE_H)
+        set(LIBS ${LIBS} -lsqlite3)
+    else()
+      message(FATAL_ERROR "SQLite3 library not found")
+    endif()
+endif()
+
+# necessary and required modules checked, ready to generate config.h in top-level build directory
+configure_file(config.h.cmake ${CMAKE_BINARY_DIR}/config.h)
 
 add_definitions(-g -O2 -fno-strict-aliasing)
 if(CMAKE_COMPILER_IS_GNUCXX)
-  add_definitions(-Wno-long-long -Wno-char-subscripts)
-  add_definitions(-Wall -pedantic)
-  add_definitions(-DNEW_STDCPP)
+#    add_definitions(-Wno-long-long -Wno-char-subscripts)
+#    add_definitions(-Wall -ansi -pedantic)
+#    add_definitions(-Wall -pedantic)
+    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -pedantic -std=c99")
+    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -std=c++11")
+    add_definitions(-DNEW_STDCPP)
 endif()
 
-add_subdirectory(src)
+include_directories(BEFORE ${CMAKE_BINARY_DIR})
+include_directories (${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/zrtp)
 
-if (enable_ccrtp)
+if(CRYPTO_STANDALONE)
+    add_definitions(-DSUPPORT_NON_NIST)
+    include_directories (${CMAKE_SOURCE_DIR}/bnlib)
+endif()
+
+if (NOT CCRTP)
+    set (sdes_src ${CMAKE_SOURCE_DIR}/zrtp/ZrtpSdesStream.cpp)
+endif()
+
+# **** The following source files a common for all clients ****
+#
+set(zrtp_src_no_cache
+    ${CMAKE_SOURCE_DIR}/zrtp/ZrtpCallbackWrapper.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/ZRtp.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/ZrtpCrc32.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/ZrtpPacketCommit.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/ZrtpPacketConf2Ack.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/ZrtpPacketConfirm.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/ZrtpPacketDHPart.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/ZrtpPacketGoClear.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/ZrtpPacketClearAck.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/ZrtpPacketHelloAck.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/ZrtpPacketHello.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/ZrtpPacketError.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/ZrtpPacketErrorAck.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/ZrtpPacketPingAck.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/ZrtpPacketPing.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/ZrtpPacketSASrelay.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/ZrtpPacketRelayAck.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/ZrtpStateClass.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/ZrtpTextData.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/ZrtpConfigure.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/ZrtpCWrapper.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/Base32.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/zrtpB64Encode.c
+    ${CMAKE_SOURCE_DIR}/zrtp/zrtpB64Decode.c
+    ${CMAKE_SOURCE_DIR}/common/osSpecifics.c ${sdes_src})
+
+set(bnlib_src
+    ${CMAKE_SOURCE_DIR}/bnlib/bn00.c
+    ${CMAKE_SOURCE_DIR}/bnlib/lbn00.c
+    ${CMAKE_SOURCE_DIR}/bnlib/bn.c
+    ${CMAKE_SOURCE_DIR}/bnlib/lbnmem.c
+    ${CMAKE_SOURCE_DIR}/bnlib/sieve.c
+    ${CMAKE_SOURCE_DIR}/bnlib/prime.c
+    ${CMAKE_SOURCE_DIR}/bnlib/bnprint.c
+    ${CMAKE_SOURCE_DIR}/bnlib/jacobi.c
+    ${CMAKE_SOURCE_DIR}/bnlib/germain.c
+    ${CMAKE_SOURCE_DIR}/bnlib/ec/ec.c
+    ${CMAKE_SOURCE_DIR}/bnlib/ec/ecdh.c
+    ${CMAKE_SOURCE_DIR}/bnlib/ec/curve25519-donna.c)
+
+set(zrtp_skein_src
+    ${CMAKE_SOURCE_DIR}/zrtp/crypto/skeinMac256.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/crypto/skein256.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/crypto/skeinMac384.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/crypto/skein384.cpp)
+
+set(zrtp_crypto_src
+    ${CMAKE_SOURCE_DIR}/zrtp/crypto/zrtpDH.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/crypto/hmac256.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/crypto/sha256.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/crypto/hmac384.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/crypto/sha384.cpp
+
+    ${CMAKE_SOURCE_DIR}/zrtp/crypto/aesCFB.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/crypto/twoCFB.cpp
+    ${CMAKE_SOURCE_DIR}/zrtp/crypto/sha2.c)
+
+if (NOT SQLITE)
+    set(zrtp_src ${zrtp_src_no_cache}
+        ${CMAKE_SOURCE_DIR}/zrtp/ZIDCacheFile.cpp
+        ${CMAKE_SOURCE_DIR}/zrtp/ZIDRecordFile.cpp)
+else()
+    set(zrtp_src ${zrtp_src_no_cache}
+        ${CMAKE_SOURCE_DIR}/zrtp/ZIDCacheDb.cpp
+        ${CMAKE_SOURCE_DIR}/zrtp/ZIDRecordDb.cpp
+        ${CMAKE_SOURCE_DIR}/zrtp/zrtpCacheSqliteBackend.c)
+
+endif()
+
+if (CCRTP)
+    add_subdirectory(clients/ccrtp)
     add_subdirectory(demo)
 endif()
 
-if (NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/package/)
-    MESSAGE(STATUS "package dir not found")
-    file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/package/)
+if (TIVI)
+    add_subdirectory(clients/tivi)
 endif()
 
-########### install files ###############
-install(FILES ${CMAKE_CURRENT_BINARY_DIR}/lib${zrtplib}.pc DESTINATION ${LIBDIRNAME}/pkgconfig)
-
-if(${PROJECT_NAME} STREQUAL ${CMAKE_PROJECT_NAME})
-
-    ########### Add uninstall target ###############
-    configure_file(
-    "${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in"
-    "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
-    IMMEDIATE @ONLY)
-    add_custom_target(uninstall
-    "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake")
-
+if (CORE_LIB)
+    add_subdirectory(clients/no_client)
 endif()
+
 ##very usefull for macosx, specially when using gtkosx bundler
 if(APPLE)
     if (NOT CMAKE_INSTALL_NAME_DIR)
         set(CMAKE_INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/lib" CACHE STRING "CMAKE_INSTALL_NAME_DIR set for macosx" )
     endif (NOT CMAKE_INSTALL_NAME_DIR)
 endif(APPLE)
-
-