From 8459f5552d4c0bb025d022ba193ac724812ff4e7 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Wed, 5 Apr 2017 14:35:28 +0200 Subject: [PATCH] Avoid global JNI-namespace pollution. --- MessengerProj/jni/Android.mk | 3 +- MessengerProj/jni/audio.c | 104 +++++++++++------- MessengerProj/jni/gifvideo.cpp | 41 +++++-- MessengerProj/jni/gifvideo.h | 33 ++++++ MessengerProj/jni/image.c | 67 ++++++++--- MessengerProj/jni/image.h | 32 +++++- MessengerProj/jni/jni.c | 67 ----------- MessengerProj/jni/mrjnimain.c | 77 +++++++++++++ MessengerProj/jni/mrjnimain.h | 54 +++++++++ MessengerProj/jni/mrwrapper.c | 16 +-- MessengerProj/jni/utils.c | 14 --- MessengerProj/jni/utils.h | 29 ----- MessengerProj/jni/video.c | 28 ++++- .../java/com/b44t/messenger/Utilities.java | 86 --------------- 14 files changed, 375 insertions(+), 276 deletions(-) create mode 100644 MessengerProj/jni/gifvideo.h delete mode 100644 MessengerProj/jni/jni.c create mode 100644 MessengerProj/jni/mrjnimain.c create mode 100644 MessengerProj/jni/mrjnimain.h delete mode 100644 MessengerProj/jni/utils.c delete mode 100644 MessengerProj/jni/utils.h diff --git a/MessengerProj/jni/Android.mk b/MessengerProj/jni/Android.mk index ccf4e9cef..2e1e0547a 100644 --- a/MessengerProj/jni/Android.mk +++ b/MessengerProj/jni/Android.mk @@ -1598,9 +1598,8 @@ ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) endif LOCAL_SRC_FILES += \ -./jni.c \ +./mrjnimain.c \ ./audio.c \ -./utils.c \ ./image.c \ ./video.c \ ./gifvideo.cpp \ diff --git a/MessengerProj/jni/audio.c b/MessengerProj/jni/audio.c index 520abf205..5c84b4e0e 100644 --- a/MessengerProj/jni/audio.c +++ b/MessengerProj/jni/audio.c @@ -1,3 +1,26 @@ +/******************************************************************************* + * + * Messenger Android Frontend + * (C) 2013-2016 Nikolai Kudashov + * (C) 2017 Björn Petersen + * Contact: r10s@b44t.com, http://b44t.com + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see http://www.gnu.org/licenses/ . + * + ******************************************************************************/ + + #include #include #include @@ -6,7 +29,7 @@ #include #include #include -#include "utils.h" +#include "mrjnimain.h" typedef struct { int version; @@ -115,7 +138,7 @@ static int read_chars(ROPacket *p, unsigned char *str, int nb_chars) return 1; } -int opus_header_to_packet(const OpusHeader *h, unsigned char *packet, int len) { +static int opus_header_to_packet(const OpusHeader *h, unsigned char *packet, int len) { int i; Packet p; unsigned char ch; @@ -219,34 +242,34 @@ static int writeOggPage(ogg_page *page, FILE *os) { return written; } -const opus_int32 bitrate = 16000; -const opus_int32 rate = 16000; -const opus_int32 frame_size = 960; -const int with_cvbr = 1; -const int max_ogg_delay = 0; -const int comment_padding = 512; +static const opus_int32 bitrate = 16000; +static const opus_int32 rate = 16000; +static const opus_int32 frame_size = 960; +static const int with_cvbr = 1; +static const int max_ogg_delay = 0; +static const int comment_padding = 512; -opus_int32 coding_rate = 16000; -ogg_int32_t _packetId; -OpusEncoder *_encoder = 0; -uint8_t *_packet = 0; -ogg_stream_state os; -FILE *_fileOs = 0; -oe_enc_opt inopt; -OpusHeader header; -opus_int32 min_bytes; -int max_frame_bytes; -ogg_packet op; -ogg_page og; -opus_int64 bytes_written; -opus_int64 pages_out; -opus_int64 total_samples; -ogg_int64_t enc_granulepos; -ogg_int64_t last_granulepos; -int size_segments; -int last_segments; +static opus_int32 coding_rate = 16000; +static ogg_int32_t _packetId; +static OpusEncoder *_encoder = 0; +static uint8_t *_packet = 0; +static ogg_stream_state os; +static FILE *_fileOs = 0; +static oe_enc_opt inopt; +static OpusHeader header; +static opus_int32 min_bytes; +static int max_frame_bytes; +static ogg_packet op; +static ogg_page og; +static opus_int64 bytes_written; +static opus_int64 pages_out; +static opus_int64 total_samples; +static ogg_int64_t enc_granulepos; +static ogg_int64_t last_granulepos; +static int size_segments; +static int last_segments; -void cleanupRecorder() { +static void cleanupRecorder() { ogg_stream_flush(&os, &og); @@ -282,7 +305,7 @@ void cleanupRecorder() { memset(&og, 0, sizeof(ogg_page)); } -int initRecorder(const char *path) { +static int initRecorder(const char *path) { cleanupRecorder(); if (!path) { @@ -421,7 +444,7 @@ int initRecorder(const char *path) { return 1; } -int writeFrame(uint8_t *framePcmBytes, unsigned int frameByteCount) { +static int writeFrame(uint8_t *framePcmBytes, unsigned int frameByteCount) { int cur_frame_size = frame_size; _packetId++; @@ -527,15 +550,15 @@ JNIEXPORT void Java_com_b44t_messenger_MediaController_stopRecord(JNIEnv *env, j } //player -OggOpusFile *_opusFile; -int _isSeekable = 0; -int64_t _totalPcmDuration = 0; -int64_t _currentPcmOffset = 0; -int _finished = 0; +static OggOpusFile *_opusFile; +static int _isSeekable = 0; +static int64_t _totalPcmDuration = 0; +static int64_t _currentPcmOffset = 0; +static int _finished = 0; static const int playerBuffersCount = 3; static const int playerSampleRate = 48000; -void cleanupPlayer() { +static void cleanupPlayer() { if (_opusFile) { op_free(_opusFile); _opusFile = 0; @@ -546,7 +569,7 @@ void cleanupPlayer() { _finished = 0; } -int seekPlayer(float position) { +static int seekPlayer(float position) { if (!_opusFile || !_isSeekable || position < 0) { return 0; } @@ -559,7 +582,7 @@ int seekPlayer(float position) { return result == OPUS_OK; } -int initPlayer(const char *path) { +static int initPlayer(const char *path) { cleanupPlayer(); int openError = OPUS_OK; @@ -576,7 +599,7 @@ int initPlayer(const char *path) { return 1; } -void fillBuffer(uint8_t *buffer, int capacity, int *args) { +static void fillBuffer(uint8_t *buffer, int capacity, int *args) { if (_opusFile) { args[1] = max(0, op_pcm_tell(_opusFile)); @@ -735,7 +758,7 @@ JNIEXPORT jbyteArray Java_com_b44t_messenger_MediaController_getWaveform2(JNIEnv return result; } -int16_t *sampleBuffer = NULL; +static int16_t *sampleBuffer = NULL; JNIEXPORT jbyteArray Java_com_b44t_messenger_MediaController_getWaveform(JNIEnv *env, jclass class, jstring path) { @@ -819,3 +842,4 @@ JNIEXPORT jbyteArray Java_com_b44t_messenger_MediaController_getWaveform(JNIEnv return result; } + diff --git a/MessengerProj/jni/gifvideo.cpp b/MessengerProj/jni/gifvideo.cpp index bb839bf70..f2666433d 100644 --- a/MessengerProj/jni/gifvideo.cpp +++ b/MessengerProj/jni/gifvideo.cpp @@ -1,5 +1,28 @@ +/******************************************************************************* + * + * Messenger Android Frontend + * (C) 2013-2016 Nikolai Kudashov + * (C) 2017 Björn Petersen + * Contact: r10s@b44t.com, http://b44t.com + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see http://www.gnu.org/licenses/ . + * + ******************************************************************************/ + + #include -#include +#include "mrjnimain.h" #include #include #include @@ -53,19 +76,14 @@ typedef struct VideoInfo { AVPacket orig_pkt; }; -jobject makeGlobarRef(JNIEnv *env, jobject object) { - if (object) { - return env->NewGlobalRef(object); - } - return 0; -} -int gifvideoOnJNILoad(JavaVM *vm, JNIEnv *env) { +int gifvideoOnJNILoad(JavaVM *vm, JNIEnv *env) { /* called from JNI_OnLoad() */ av_register_all(); return 0; } -int open_codec_context(int *stream_idx, AVFormatContext *fmt_ctx, enum AVMediaType type) { + +static int open_codec_context(int *stream_idx, AVFormatContext *fmt_ctx, enum AVMediaType type) { int ret; AVStream *st; AVCodecContext *dec_ctx = NULL; @@ -97,7 +115,7 @@ int open_codec_context(int *stream_idx, AVFormatContext *fmt_ctx, enum AVMediaTy return 0; } -int decode_packet(VideoInfo *info, int *got_frame) { +static int decode_packet(VideoInfo *info, int *got_frame) { int ret = 0; int decoded = info->pkt.size; @@ -263,4 +281,5 @@ jint Java_com_b44t_ui_Components_AnimatedFileDrawable_getVideoFrame(JNIEnv *env, } return 0; } -} + +} /* extern "C" */ diff --git a/MessengerProj/jni/gifvideo.h b/MessengerProj/jni/gifvideo.h new file mode 100644 index 000000000..4b1cb571a --- /dev/null +++ b/MessengerProj/jni/gifvideo.h @@ -0,0 +1,33 @@ +/******************************************************************************* + * + * Messenger Android Frontend + * (C) 2013-2016 Nikolai Kudashov + * (C) 2017 Björn Petersen + * Contact: r10s@b44t.com, http://b44t.com + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see http://www.gnu.org/licenses/ . + * + ******************************************************************************/ + + +#ifndef __GIFVIDEO_H__ +#define __GIFVIDEO_H__ + + +#include + +int gifvideoOnJNILoad(JavaVM *vm, JNIEnv *env); + + +#endif /* __GIFVIDEO_H__ */ diff --git a/MessengerProj/jni/image.c b/MessengerProj/jni/image.c index fbb421a35..ab5779002 100644 --- a/MessengerProj/jni/image.c +++ b/MessengerProj/jni/image.c @@ -1,3 +1,26 @@ +/******************************************************************************* + * + * Messenger Android Frontend + * (C) 2013-2016 Nikolai Kudashov + * (C) 2017 Björn Petersen + * Contact: r10s@b44t.com, http://b44t.com + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see http://www.gnu.org/licenses/ . + * + ******************************************************************************/ + + #include #include #include @@ -5,21 +28,35 @@ #include #include #include -#include "utils.h" +#include "mrjnimain.h" #include "image.h" -jclass jclass_NullPointerException; -jclass jclass_RuntimeException; -jclass jclass_Options; -jfieldID jclass_Options_inJustDecodeBounds; -jfieldID jclass_Options_outHeight; -jfieldID jclass_Options_outWidth; +static jclass jclass_NullPointerException; +static jclass jclass_RuntimeException; -const uint32_t PGPhotoEnhanceHistogramBins = 256; -const uint32_t PGPhotoEnhanceSegments = 4; +static jclass jclass_Options; +static jfieldID jclass_Options_inJustDecodeBounds; +static jfieldID jclass_Options_outHeight; +static jfieldID jclass_Options_outWidth; -jclass createGlobarRef(JNIEnv *env, jclass class) { +static const uint32_t PGPhotoEnhanceHistogramBins = 256; +static const uint32_t PGPhotoEnhanceSegments = 4; + +static void throwException(JNIEnv *env, char *format, ...) { + jclass exClass = (*env)->FindClass(env, "java/lang/UnsupportedOperationException"); + if (!exClass) { + return; + } + char dest[256]; + va_list argptr; + va_start(argptr, format); + vsprintf(dest, format, argptr); + va_end(argptr); + (*env)->ThrowNew(env, exClass, dest); +} + +static jclass createGlobalRef(JNIEnv *env, jclass class) { if (class) { return (*env)->NewGlobalRef(env, class); } @@ -27,16 +64,16 @@ jclass createGlobarRef(JNIEnv *env, jclass class) { } jint imageOnJNILoad(JavaVM *vm, void *reserved, JNIEnv *env) { - jclass_NullPointerException = createGlobarRef(env, (*env)->FindClass(env, "java/lang/NullPointerException")); + jclass_NullPointerException = createGlobalRef(env, (*env)->FindClass(env, "java/lang/NullPointerException")); if (jclass_NullPointerException == 0) { return -1; } - jclass_RuntimeException = createGlobarRef(env, (*env)->FindClass(env, "java/lang/RuntimeException")); + jclass_RuntimeException = createGlobalRef(env, (*env)->FindClass(env, "java/lang/RuntimeException")); if (jclass_RuntimeException == 0) { return -1; } - jclass_Options = createGlobarRef(env, (*env)->FindClass(env, "android/graphics/BitmapFactory$Options")); + jclass_Options = createGlobalRef(env, (*env)->FindClass(env, "android/graphics/BitmapFactory$Options")); if (jclass_Options == 0) { return -1; } @@ -262,7 +299,7 @@ typedef struct my_error_mgr { } *my_error_ptr; -METHODDEF(void) my_error_exit(j_common_ptr cinfo) { +METHODDEF(void) my_jpeglib_error_exit(j_common_ptr cinfo) { my_error_ptr myerr = (my_error_ptr) cinfo->err; (*cinfo->err->output_message) (cinfo); longjmp(myerr->setjmp_buffer, 1); @@ -424,7 +461,7 @@ JNIEXPORT void Java_com_b44t_messenger_Utilities_loadBitmap(JNIEnv *env, jclass struct jpeg_decompress_struct cinfo; cinfo.err = jpeg_std_error(&jerr.pub); - jerr.pub.error_exit = my_error_exit; + jerr.pub.error_exit = my_jpeglib_error_exit; if (!setjmp(jerr.setjmp_buffer)) { jpeg_create_decompress(&cinfo); diff --git a/MessengerProj/jni/image.h b/MessengerProj/jni/image.h index 58dd5385b..ff3f5a5c0 100644 --- a/MessengerProj/jni/image.h +++ b/MessengerProj/jni/image.h @@ -1,8 +1,34 @@ -#ifndef image_h -#define image_h +/******************************************************************************* + * + * Messenger Android Frontend + * (C) 2013-2016 Nikolai Kudashov + * (C) 2017 Björn Petersen + * Contact: r10s@b44t.com, http://b44t.com + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see http://www.gnu.org/licenses/ . + * + ******************************************************************************/ + + +#ifndef __IMAGE_H__ +#define __IMAGE_H__ + #include jint imageOnJNILoad(JavaVM *vm, void *reserved, JNIEnv *env); -#endif + +#endif /* __IMAGE_H__ */ + diff --git a/MessengerProj/jni/jni.c b/MessengerProj/jni/jni.c deleted file mode 100644 index 9d3c992a4..000000000 --- a/MessengerProj/jni/jni.c +++ /dev/null @@ -1,67 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include "utils.h" -#include "image.h" - -int gifvideoOnJNILoad(JavaVM *vm, JNIEnv *env); - -jint JNI_OnLoad(JavaVM *vm, void *reserved) { /* this function is called automatically by the JNI when the library gets loaded */ - JNIEnv *env = 0; - srand(time(NULL)); - - if ((*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_6) != JNI_OK) { - return -1; - } - - if (imageOnJNILoad(vm, reserved, env) == -1) { - return -1; - } - - if (gifvideoOnJNILoad(vm, env) == -1) { - return -1; - } - - return JNI_VERSION_1_6; -} - -void JNI_OnUnload(JavaVM *vm, void *reserved) { - -} - -/* -JNIEXPORT void Java_com_b44t_messenger_Utilities_aesIgeEncryption(JNIEnv *env, jclass class, jobject buffer, jbyteArray key, jbyteArray iv, jboolean encrypt, int offset, int length) { - jbyte *what = (*env)->GetDirectBufferAddress(env, buffer) + offset; - unsigned char *keyBuff = (unsigned char *)(*env)->GetByteArrayElements(env, key, NULL); - unsigned char *ivBuff = (unsigned char *)(*env)->GetByteArrayElements(env, iv, NULL); - - AES_KEY akey; - if (!encrypt) { - AES_set_decrypt_key(keyBuff, 32 * 8, &akey); - AES_ige_encrypt(what, what, length, &akey, ivBuff, AES_DECRYPT); - } else { - AES_set_encrypt_key(keyBuff, 32 * 8, &akey); - AES_ige_encrypt(what, what, length, &akey, ivBuff, AES_ENCRYPT); - } - (*env)->ReleaseByteArrayElements(env, key, keyBuff, JNI_ABORT); - (*env)->ReleaseByteArrayElements(env, iv, ivBuff, 0); -} -*/ - -JNIEXPORT jstring Java_com_b44t_messenger_Utilities_readlink(JNIEnv *env, jclass class, jstring path) { - static char buf[1000]; - char *fileName = (*env)->GetStringUTFChars(env, path, NULL); - int result = readlink(fileName, buf, 999); - jstring value = 0; - if (result != -1) { - buf[result] = '\0'; - value = (*env)->NewStringUTF(env, buf); - } - (*env)->ReleaseStringUTFChars(env, path, fileName); - return value; -} diff --git a/MessengerProj/jni/mrjnimain.c b/MessengerProj/jni/mrjnimain.c new file mode 100644 index 000000000..0b5a2b20a --- /dev/null +++ b/MessengerProj/jni/mrjnimain.c @@ -0,0 +1,77 @@ +/******************************************************************************* + * + * Messenger Android Frontend + * (C) 2013-2016 Nikolai Kudashov + * (C) 2017 Björn Petersen + * Contact: r10s@b44t.com, http://b44t.com + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see http://www.gnu.org/licenses/ . + * + ******************************************************************************/ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include "mrjnimain.h" +#include "image.h" +#include "gifvideo.h" + + +jint JNI_OnLoad(JavaVM *vm, void *reserved) { /* this function is called automatically by the JNI when the library gets loaded */ + JNIEnv *env = 0; + srand(time(NULL)); + + __android_log_write(ANDROID_LOG_INFO, "DeltaChat", "JNI_OnLoad() ..."); + + if ((*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_6) != JNI_OK) { + return -1; + } + + if (imageOnJNILoad(vm, reserved, env) == -1) { + return -1; + } + + if (gifvideoOnJNILoad(vm, env) == -1) { + return -1; + } + + __android_log_write(ANDROID_LOG_INFO, "DeltaChat", "JNI_OnLoad() succeeded."); + + return JNI_VERSION_1_6; +} + + +void JNI_OnUnload(JavaVM *vm, void *reserved) { +} + + +JNIEXPORT jstring Java_com_b44t_messenger_Utilities_readlink(JNIEnv *env, jclass class, jstring path) { + static char buf[1000]; + char *fileName = (*env)->GetStringUTFChars(env, path, NULL); + int result = readlink(fileName, buf, 999); + jstring value = 0; + if (result != -1) { + buf[result] = '\0'; + value = (*env)->NewStringUTF(env, buf); + } + (*env)->ReleaseStringUTFChars(env, path, fileName); + return value; +} + diff --git a/MessengerProj/jni/mrjnimain.h b/MessengerProj/jni/mrjnimain.h new file mode 100644 index 000000000..5b1357b35 --- /dev/null +++ b/MessengerProj/jni/mrjnimain.h @@ -0,0 +1,54 @@ +/******************************************************************************* + * + * Messenger Android Frontend + * (C) 2013-2016 Nikolai Kudashov + * (C) 2017 Björn Petersen + * Contact: r10s@b44t.com, http://b44t.com + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see http://www.gnu.org/licenses/ . + * + ******************************************************************************/ + + +#ifndef __MRJNIMAIN_H__ +#define __MRJNIMAIN_H__ + + +#include +#include + + +#define LOG_TAG "DeltaChat" +#ifndef LOG_DISABLED +#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) +#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__) +#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) +#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__) +#else +#define LOGI(...) +#define LOGD(...) +#define LOGE(...) +#define LOGV(...) +#endif + +#ifndef max +#define max(x, y) ((x) > (y)) ? (x) : (y) +#endif +#ifndef min +#define min(x, y) ((x) < (y)) ? (x) : (y) +#endif + + +#endif /* __MRJNIMAIN_H__*/ + diff --git a/MessengerProj/jni/mrwrapper.c b/MessengerProj/jni/mrwrapper.c index f6dccede3..5963f89bb 100644 --- a/MessengerProj/jni/mrwrapper.c +++ b/MessengerProj/jni/mrwrapper.c @@ -23,8 +23,8 @@ * Purpose: The C part of the Java<->C Wrapper, see also MrMailbox.java * ******************************************************************************/ - - + + #include #include #include "messenger-backend/src/mrmailbox.h" @@ -81,6 +81,8 @@ static void s_init_globals(JNIEnv *env, jclass MrMailbox_class) s_global_init_done = 1; /* prepare calling back a Java function */ + __android_log_write(ANDROID_LOG_INFO, "DeltaChat", "JNI: s_init_globals()..."); /* low-level logging, mrmailbox_log_*() may not be yet available */ + (*env)->GetJavaVM(env, &s_jvm); /* JNIEnv cannot be shared between threads, so we share the JavaVM object */ s_MrMailbox_class = (*env)->NewGlobalRef(env, MrMailbox_class); s_MrCallback_methodID = (*env)->GetStaticMethodID(env, MrMailbox_class, "MrCallback","(IJJ)J" /*signature as "(param)ret" with I=int, J=long*/ ); @@ -92,23 +94,23 @@ static void s_init_globals(JNIEnv *env, jclass MrMailbox_class) int mrosnative_setup_thread(mrmailbox_t* mailbox) { if( s_jvm == NULL ) { - mrmailbox_log_error(mailbox, 0, "Not ready, cannot setup thread."); + __android_log_write(ANDROID_LOG_ERROR, "DeltaChat", "Not ready, cannot setup thread."); /* low-level logging, mrmailbox_log_*() may not be yet available */ return 0; } - mrmailbox_log_info(mailbox, 0, "Attaching C-thread to Java VM..."); + __android_log_write(ANDROID_LOG_INFO, "DeltaChat", "Attaching C-thread to Java VM..."); JNIEnv* env = NULL; (*s_jvm)->AttachCurrentThread(s_jvm, &env, NULL); - mrmailbox_log_info(mailbox, 0, "Attaching ok."); + __android_log_write(ANDROID_LOG_INFO, "DeltaChat", "Attaching ok."); return 1; } void mrosnative_unsetup_thread(mrmailbox_t* mailbox) { - mrmailbox_log_info(mailbox, 0, "Detaching C-thread from Java VM..."); + __android_log_write(ANDROID_LOG_INFO, "DeltaChat", "Detaching C-thread from Java VM..."); /* low-level logging, mrmailbox_log_*() may not be yet available */ (*s_jvm)->DetachCurrentThread(s_jvm); - mrmailbox_log_info(mailbox, 0, "Detaching done."); + __android_log_write(ANDROID_LOG_INFO, "DeltaChat", "Detaching done."); } diff --git a/MessengerProj/jni/utils.c b/MessengerProj/jni/utils.c deleted file mode 100644 index 1321a8213..000000000 --- a/MessengerProj/jni/utils.c +++ /dev/null @@ -1,14 +0,0 @@ -#include "utils.h" - -void throwException(JNIEnv *env, char *format, ...) { - jclass exClass = (*env)->FindClass(env, "java/lang/UnsupportedOperationException"); - if (!exClass) { - return; - } - char dest[256]; - va_list argptr; - va_start(argptr, format); - vsprintf(dest, format, argptr); - va_end(argptr); - (*env)->ThrowNew(env, exClass, dest); -} diff --git a/MessengerProj/jni/utils.h b/MessengerProj/jni/utils.h deleted file mode 100644 index a26c03cca..000000000 --- a/MessengerProj/jni/utils.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef log_h -#define log_h - -#include -#include - -#define LOG_TAG "DeltaChat" -#ifndef LOG_DISABLED -#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) -#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__) -#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) -#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__) -#else -#define LOGI(...) -#define LOGD(...) -#define LOGE(...) -#define LOGV(...) -#endif - -#ifndef max -#define max(x, y) ((x) > (y)) ? (x) : (y) -#endif -#ifndef min -#define min(x, y) ((x) < (y)) ? (x) : (y) -#endif - -void throwException(JNIEnv *env, char *format, ...); - -#endif diff --git a/MessengerProj/jni/video.c b/MessengerProj/jni/video.c index 883e319ad..1c81c6b71 100644 --- a/MessengerProj/jni/video.c +++ b/MessengerProj/jni/video.c @@ -1,6 +1,30 @@ +/******************************************************************************* + * + * Messenger Android Frontend + * (C) 2013-2016 Nikolai Kudashov + * (C) 2017 Björn Petersen + * Contact: r10s@b44t.com, http://b44t.com + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see http://www.gnu.org/licenses/ . + * + ******************************************************************************/ + + #include #include -#include +#include "mrjnimain.h" + enum COLOR_FORMATTYPE { COLOR_FormatMonochrome = 1, @@ -52,7 +76,7 @@ enum COLOR_FORMATTYPE { COLOR_QCOM_FormatYUV420SemiPlanar = 0x7fa30c00 }; -int isSemiPlanarYUV(int colorFormat) { +static int isSemiPlanarYUV(int colorFormat) { switch (colorFormat) { case COLOR_FormatYUV420Planar: case COLOR_FormatYUV420PackedPlanar: diff --git a/MessengerProj/src/main/java/com/b44t/messenger/Utilities.java b/MessengerProj/src/main/java/com/b44t/messenger/Utilities.java index 63456a5cb..7271f40cb 100644 --- a/MessengerProj/src/main/java/com/b44t/messenger/Utilities.java +++ b/MessengerProj/src/main/java/com/b44t/messenger/Utilities.java @@ -67,15 +67,8 @@ public class Utilities { public native static void calcCDT(ByteBuffer hsvBuffer, int width, int height, ByteBuffer buffer); public native static boolean loadWebpImage(Bitmap bitmap, ByteBuffer buffer, int len, BitmapFactory.Options options, boolean unpin); public native static int convertVideoFrame(ByteBuffer src, ByteBuffer dest, int destFormat, int width, int height, int padding, int swap); - //private native static void aesIgeEncryption(ByteBuffer buffer, byte[] key, byte[] iv, boolean encrypt, int offset, int length); public native static String readlink(String path); - /* - public static void aesIgeEncryption(ByteBuffer buffer, byte[] key, byte[] iv, boolean encrypt, boolean changeIv, int offset, int length) { - aesIgeEncryption(buffer, key, changeIv ? iv : iv.clone(), encrypt, offset, length); - } - */ - public static Integer parseInt(String value) { if (value == null) { return 0; @@ -132,85 +125,6 @@ public class Utilities { return new String(hexChars); } - public static byte[] hexToBytes(String hex) { - if (hex == null) { - return null; - } - int len = hex.length(); - byte[] data = new byte[len / 2]; - for (int i = 0; i < len; i += 2) { - data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16)); - } - return data; - } - - public static boolean isGoodPrime(byte[] prime, int g) { - if (!(g >= 2 && g <= 7)) { - return false; - } - - if (prime.length != 256 || prime[0] >= 0) { - return false; - } - - BigInteger dhBI = new BigInteger(1, prime); - - if (g == 2) { // p mod 8 = 7 for g = 2; - BigInteger res = dhBI.mod(BigInteger.valueOf(8)); - if (res.intValue() != 7) { - return false; - } - } else if (g == 3) { // p mod 3 = 2 for g = 3; - BigInteger res = dhBI.mod(BigInteger.valueOf(3)); - if (res.intValue() != 2) { - return false; - } - } else if (g == 5) { // p mod 5 = 1 or 4 for g = 5; - BigInteger res = dhBI.mod(BigInteger.valueOf(5)); - int val = res.intValue(); - if (val != 1 && val != 4) { - return false; - } - } else if (g == 6) { // p mod 24 = 19 or 23 for g = 6; - BigInteger res = dhBI.mod(BigInteger.valueOf(24)); - int val = res.intValue(); - if (val != 19 && val != 23) { - return false; - } - } else if (g == 7) { // p mod 7 = 3, 5 or 6 for g = 7. - BigInteger res = dhBI.mod(BigInteger.valueOf(7)); - int val = res.intValue(); - if (val != 3 && val != 5 && val != 6) { - return false; - } - } - - String hex = bytesToHex(prime); - if (hex.equals("C71CAEB9C6B1C9048E6C522F70F13F73980D40238E3E21C14934D037563D930F48198A0AA7C14058229493D22530F4DBFA336F6E0AC925139543AED44CCE7C3720FD51F69458705AC68CD4FE6B6B13ABDC9746512969328454F18FAF8C595F642477FE96BB2A941D5BCD1D4AC8CC49880708FA9B378E3C4F3A9060BEE67CF9A4A4A695811051907E162753B56B0F6B410DBA74D8A84B2A14B3144E0EF1284754FD17ED950D5965B4B9DD46582DB1178D169C6BC465B0D6FF9CA3928FEF5B9AE4E418FC15E83EBEA0F87FA9FF5EED70050DED2849F47BF959D956850CE929851F0D8115F635B105EE2E4E15D04B2454BF6F4FADF034B10403119CD8E3B92FCC5B")) { - return true; - } - - BigInteger dhBI2 = dhBI.subtract(BigInteger.valueOf(1)).divide(BigInteger.valueOf(2)); - return !(!dhBI.isProbablePrime(30) || !dhBI2.isProbablePrime(30)); - } - - public static boolean isGoodGaAndGb(BigInteger g_a, BigInteger p) { - return !(g_a.compareTo(BigInteger.valueOf(1)) != 1 || g_a.compareTo(p.subtract(BigInteger.valueOf(1))) != -1); - } - - public static boolean arraysEquals(byte[] arr1, int offset1, byte[] arr2, int offset2) { - if (arr1 == null || arr2 == null || offset1 < 0 || offset2 < 0 || arr1.length - offset1 != arr2.length - offset2 || arr1.length - offset1 < 0 || arr2.length - offset2 < 0) { - return false; - } - boolean result = true; - for (int a = offset1; a < arr1.length; a++) { - if (arr1[a + offset1] != arr2[a + offset2]) { - result = false; - } - } - return result; - } - public static byte[] computeSHA1(byte[] convertme, int offset, int len) { try { MessageDigest md = MessageDigest.getInstance("SHA-1");