| 5 |
| #include <inttypes.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <openssl/aes.h> |
| 8 | #include <unistd.h> |
| 9 | #include "utils.h" |
| 10 | #include "sqlite.h" |
| 11 | #include "image.h" |
| 12 |
| 13 | int registerNativeTgNetFunctions(JavaVM *vm, JNIEnv *env); |
| 14 | int gifvideoOnJNILoad(JavaVM *vm, JNIEnv *env); |
| 15 |
| 16 | jint JNI_OnLoad(JavaVM *vm, void *reserved) { |
| 17 | JNIEnv *env = 0; |
| 18 | srand(time(NULL)); |
| 19 |
| 20 | if ((*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_6) != JNI_OK) { |
| 21 | return -1; |
| 22 | } |
| 23 |
| 24 | if (sqliteOnJNILoad(vm, reserved, env) == -1) { |
| 25 | return -1; |
| 26 | } |
| 27 |
| 28 | if (imageOnJNILoad(vm, reserved, env) == -1) { |
| 29 | return -1; |
| 30 | } |
| 31 |
| 32 | if (gifvideoOnJNILoad(vm, env) == -1) { |
| 33 | return -1; |
| 34 | } |
| 35 |
| 36 | if (registerNativeTgNetFunctions(vm, env) != JNI_TRUE) { |
| 37 | return -1; |
| 38 | } |
| 39 |
| 40 | return JNI_VERSION_1_6; |
| 41 | } |
| 42 |
| 43 | void JNI_OnUnload(JavaVM *vm, void *reserved) { |
| 44 |
| 45 | } |
| 46 |
| 47 | JNIEXPORT void Java_org_telegram_messenger_Utilities_aesIgeEncryption(JNIEnv *env, jclass class, jobject buffer, jbyteArray key, jbyteArray iv, jboolean encrypt, int offset, int length) { |
| 48 | jbyte *what = (*env)->GetDirectBufferAddress(env, buffer) + offset; |
| 49 | unsigned char *keyBuff = (unsigned char *)(*env)->GetByteArrayElements(env, key, NULL); |
| 50 | unsigned char *ivBuff = (unsigned char *)(*env)->GetByteArrayElements(env, iv, NULL); |
| 51 |
| 52 | AES_KEY akey; |
| 53 | if (!encrypt) { |
| 54 | AES_set_decrypt_key(keyBuff, 32 * 8, &akey); |
| 55 | AES_ige_encrypt(what, what, length, &akey, ivBuff, AES_DECRYPT); |
| 56 | } else { |
| 57 | AES_set_encrypt_key(keyBuff, 32 * 8, &akey); |
| 58 | AES_ige_encrypt(what, what, length, &akey, ivBuff, AES_ENCRYPT); |
| 59 | } |
| 60 | (*env)->ReleaseByteArrayElements(env, key, keyBuff, JNI_ABORT); |
| 61 | (*env)->ReleaseByteArrayElements(env, iv, ivBuff, 0); |
| 62 | } |
| 63 |
| 64 | JNIEXPORT jstring Java_org_telegram_messenger_Utilities_readlink(JNIEnv *env, jclass class, jstring path) { |
| 65 | static char buf[1000]; |
| 66 | char *fileName = (*env)->GetStringUTFChars(env, path, NULL); |
| 67 | int result = readlink(fileName, buf, 999); |
| 68 | jstring value = 0; |
| 69 | if (result != -1) { |
| 70 | buf[result] = '\0'; |
| 71 | value = (*env)->NewStringUTF(env, buf); |
| 72 | } |
| 73 | (*env)->ReleaseStringUTFChars(env, path, fileName); |
| 74 | return value; |
| 75 | } |
| 76 |