1
0
mirror of https://github.com/MGislv/NekoX.git synced 2024-07-04 11:13:36 +00:00
NekoX/TMessagesProj/jni/jni.c

87 lines
2.9 KiB
C
Raw Normal View History

2013-10-25 15:19:00 +00:00
#include <stdio.h>
#include <string.h>
#include <jni.h>
#include <sys/types.h>
#include <inttypes.h>
2014-03-27 14:25:53 +00:00
#include <stdlib.h>
2015-09-24 20:52:02 +00:00
#include <openssl/aes.h>
2016-03-16 12:26:32 +00:00
#include <unistd.h>
2014-03-27 14:25:53 +00:00
#include "utils.h"
2015-01-02 22:15:07 +00:00
#include "image.h"
2014-03-27 14:25:53 +00:00
2015-09-24 20:52:02 +00:00
int registerNativeTgNetFunctions(JavaVM *vm, JNIEnv *env);
int gifvideoOnJNILoad(JavaVM *vm, JNIEnv *env);
2015-09-24 20:52:02 +00:00
2014-03-27 14:25:53 +00:00
jint JNI_OnLoad(JavaVM *vm, void *reserved) {
JNIEnv *env = 0;
srand(time(NULL));
2015-01-02 22:15:07 +00:00
if ((*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_6) != JNI_OK) {
2014-03-27 14:25:53 +00:00
return -1;
}
2015-01-02 22:15:07 +00:00
if (imageOnJNILoad(vm, reserved, env) == -1) {
return -1;
}
if (gifvideoOnJNILoad(vm, env) == -1) {
return -1;
}
2015-09-24 20:52:02 +00:00
if (registerNativeTgNetFunctions(vm, env) != JNI_TRUE) {
return -1;
}
2015-01-02 22:15:07 +00:00
return JNI_VERSION_1_6;
2014-03-27 14:25:53 +00:00
}
void JNI_OnUnload(JavaVM *vm, void *reserved) {
2014-03-27 14:25:53 +00:00
}
2013-10-25 15:19:00 +00:00
JNIEXPORT void Java_org_telegram_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);
2013-10-25 15:19:00 +00:00
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);
}
2016-03-06 01:49:31 +00:00
2017-07-08 16:32:04 +00:00
JNIEXPORT jint Java_org_telegram_messenger_Utilities_aesCtrDecryption(JNIEnv *env, jclass class, jobject buffer, jbyteArray key, jbyteArray iv, 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;
unsigned int num = 0;
uint8_t count[16];
memset(count, 0, 16);
AES_set_encrypt_key(keyBuff, 32 * 8, &akey);
AES_ctr128_encrypt(what, what, length, &akey, ivBuff, count, &num);
(*env)->ReleaseByteArrayElements(env, key, keyBuff, JNI_ABORT);
(*env)->ReleaseByteArrayElements(env, iv, ivBuff, JNI_ABORT);
return num;
}
2016-03-06 01:49:31 +00:00
JNIEXPORT jstring Java_org_telegram_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;
}