@@ -25,7 +25,7 @@ using namespace Jpp;
2525constexpr static jsize MB = 1 ;
2626
2727// / release buffer size.
28- constexpr static jsize BUFFER_SIZE = MB * 1024 * 1024 ;
28+ constexpr static jsize BUFFER_SIZE = ( MB * 1024 * 1024 ) + ((MB * 1024 * 1024 ) / 8 ) ;
2929
3030// / debug buffer size.
3131// constexpr static jsize BUFFER_SIZE = 32;
@@ -77,25 +77,26 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_e
7777) {
7878 ArrayList<Uri> file_queue (env, target_files);
7979
80- jbyte *aeskey = env->GetByteArrayElements (key_file, nullptr );
80+ auto *aeskey = (jbyte *) env->GetPrimitiveArrayCritical (key_file, nullptr );
8181 jint aeskey_size = env->GetArrayLength (key_file);
8282
8383 Mode::CBC<BlockCipher::AES, Padding::PKCS_5_7> aes_scheme (
8484 reinterpret_cast <Bytes *>(aeskey),
8585 aeskey_size
8686 );
8787
88- env->ReleaseByteArrayElements (key_file, aeskey, 0 );
88+ env->ReleasePrimitiveArrayCritical (key_file, aeskey, JNI_ABORT );
8989
9090 std::atomic<jint> cnt (0 );
9191 std::mutex vector_mtx;
9292
93- if (BUFFER_SIZE % AES_BLOCK_SIZE != 0 || BUFFER_SIZE <= AES_BLOCK_SIZE) {
93+ if constexpr (BUFFER_SIZE % AES_BLOCK_SIZE != 0 || BUFFER_SIZE <= AES_BLOCK_SIZE) {
9494 return RESULT_CODE::INVALID_INTERNAL_BUFFER_SIZE;
9595 }
9696
9797 JavaVM* javaVM;
9898 env->GetJavaVM ((_JavaVM**) &javaVM);
99+
99100 jobject DocumentFileClass = env->NewGlobalRef ((jobject) env->FindClass (" androidx/documentfile/provider/DocumentFile" ));
100101 jobject globalThis = env->NewGlobalRef (thiz);
101102 jobject globalOutputPath = env->NewGlobalRef (output_path);
@@ -106,7 +107,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_e
106107 jint isDifferentThread = javaVM->GetEnv ((void **) &threadEnv, JNI_VERSION_1_6);
107108
108109 if (isDifferentThread == JNI_EDETACHED) {
109- javaVM->AttachCurrentThread ((_JNIEnv**) &threadEnv, NULL );
110+ javaVM->AttachCurrentThread ((_JNIEnv**) &threadEnv, nullptr );
110111 } else if (isDifferentThread == JNI_OK) {
111112 threadEnv = env;
112113 } else {
@@ -116,7 +117,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_e
116117
117118 bool run_thread = true ;
118119
119- Bytes *encryptedBuffer = new Bytes[BUFFER_SIZE];
120+ auto *encryptedBuffer = new Bytes[BUFFER_SIZE];
120121
121122 std::string target_file;
122123
@@ -131,7 +132,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_e
131132 target_uri._thiz = file_queue.remove (threadEnv, file_queue.size (threadEnv) - 1 )._thiz ;
132133 target_uri._Jclass = threadEnv->GetObjectClass (target_uri._thiz );
133134 jstring filename = getFileName (threadEnv, globalThis, target_uri._thiz );
134- const char *c_filename_buffer = threadEnv->GetStringUTFChars (filename, NULL );
135+ const char *c_filename_buffer = threadEnv->GetStringUTFChars (filename, nullptr );
135136 target_file = std::string (c_filename_buffer);
136137 threadEnv->ReleaseStringUTFChars (filename, c_filename_buffer);
137138 } else {
@@ -211,7 +212,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_e
211212 break ;
212213 }
213214
214- prevBuffer = (jbyte *) threadEnv->GetPrimitiveArrayCritical (prevJniBuffer, NULL );
215+ prevBuffer = (jbyte *) threadEnv->GetPrimitiveArrayCritical (prevJniBuffer, nullptr );
215216 for (size_t index = 0 ; index < prevBufferSize; index += AES_BLOCK_SIZE) {
216217 aes_scheme.blockEncrypt (
217218 reinterpret_cast <unsigned char *>(prevBuffer + index),
@@ -239,7 +240,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_e
239240 bool excludeLastBlock = (remainingBlocks && remainingBytes == 0 );
240241
241242 if (remainingBlocks) {
242- prevBuffer = (jbyte *) threadEnv->GetPrimitiveArrayCritical (prevJniBuffer, NULL );
243+ prevBuffer = (jbyte *) threadEnv->GetPrimitiveArrayCritical (prevJniBuffer, nullptr );
243244
244245 for (; index < remainingBlocks - excludeLastBlock; ++index) {
245246 aes_scheme.blockEncrypt (
@@ -258,7 +259,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_e
258259
259260 Krypt::ByteArray cipher;
260261
261- prevBuffer = (jbyte *) threadEnv->GetPrimitiveArrayCritical (prevJniBuffer, NULL );
262+ prevBuffer = (jbyte *) threadEnv->GetPrimitiveArrayCritical (prevJniBuffer, nullptr );
262263 if (excludeLastBlock) {
263264 cipher = aes_scheme.encrypt (
264265 reinterpret_cast <unsigned char *>(prevBuffer + (index * AES_BLOCK_SIZE)), AES_BLOCK_SIZE, iv
@@ -272,7 +273,9 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_e
272273
273274 threadEnv->SetByteArrayRegion (
274275 prevJniBuffer, 0 , cipher.length ,
275- reinterpret_cast <jbyte *>(cipher.array ));
276+ reinterpret_cast <jbyte *>(cipher.array )
277+ );
278+
276279 outgoing_bytes.write (prevJniBuffer, 0 , cipher.length );
277280
278281 if (threadEnv->ExceptionCheck ()) {
@@ -300,7 +303,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_e
300303 }
301304 };
302305
303- int physical_threads = std::thread::hardware_concurrency ();
306+ int physical_threads = ( int ) std::thread::hardware_concurrency ();
304307
305308 std::vector<std::thread> threads;
306309
@@ -310,7 +313,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_e
310313 vector_mtx.unlock ();
311314
312315 if (notEmpty) {
313- threads.push_back ( std::thread ( encrypt_lambda) );
316+ threads.emplace_back ( encrypt_lambda);
314317 }
315318 }
316319
@@ -322,8 +325,8 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_e
322325 encrypt_lambda ();
323326 }
324327
325- for (size_t i = 0 ; i < threads. size (); ++i ) {
326- threads[i] .join ();
328+ for (auto & thrd : threads) {
329+ thrd .join ();
327330 }
328331
329332 env->DeleteGlobalRef (DocumentFileClass);
@@ -341,20 +344,20 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_d
341344) {
342345 ArrayList<Uri> file_queue (env, target_files);
343346
344- jbyte *aeskey = env->GetByteArrayElements (key_file, nullptr );
347+ auto *aeskey = (jbyte *) env->GetPrimitiveArrayCritical (key_file, nullptr );
345348 jint aeskey_size = env->GetArrayLength (key_file);
346349
347350 Mode::CBC<BlockCipher::AES, Padding::PKCS_5_7> aes_scheme (
348351 reinterpret_cast <Bytes *>(aeskey),
349352 aeskey_size
350353 );
351354
352- env->ReleaseByteArrayElements (key_file, aeskey, 0 );
355+ env->ReleasePrimitiveArrayCritical (key_file, aeskey, JNI_ABORT );
353356
354357 std::atomic<jint> cnt (0 );
355358 std::mutex vector_mtx;
356359
357- if (BUFFER_SIZE % AES_BLOCK_SIZE != 0 || BUFFER_SIZE <= AES_BLOCK_SIZE) {
360+ if constexpr (BUFFER_SIZE % AES_BLOCK_SIZE != 0 || BUFFER_SIZE <= AES_BLOCK_SIZE) {
358361 return RESULT_CODE::INVALID_INTERNAL_BUFFER_SIZE;
359362 }
360363
@@ -370,7 +373,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_d
370373 jint isDifferentThread = javaVM->GetEnv ((void **) &threadEnv, JNI_VERSION_1_6);
371374
372375 if (isDifferentThread == JNI_EDETACHED) {
373- javaVM->AttachCurrentThread ((_JNIEnv**) &threadEnv, NULL );
376+ javaVM->AttachCurrentThread ((_JNIEnv**) &threadEnv, nullptr );
374377 } else if (isDifferentThread == JNI_OK) {
375378 threadEnv = env;
376379 } else {
@@ -380,7 +383,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_d
380383
381384 bool run_thread = true ;
382385
383- Bytes *decryptedBuffer = new Bytes[BUFFER_SIZE];
386+ auto *decryptedBuffer = new Bytes[BUFFER_SIZE];
384387
385388 std::string target_file;
386389
@@ -395,7 +398,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_d
395398 target_uri._thiz = file_queue.remove (threadEnv, 0 )._thiz ;
396399 target_uri._Jclass = threadEnv->GetObjectClass (target_uri._thiz );
397400 jstring filename = getFileName (threadEnv, globalThis, target_uri._thiz );
398- const char *c_filename_buffer = threadEnv->GetStringUTFChars (filename, NULL );
401+ const char *c_filename_buffer = threadEnv->GetStringUTFChars (filename, nullptr );
399402 target_file = std::string (c_filename_buffer);
400403 threadEnv->ReleaseStringUTFChars (filename, c_filename_buffer);
401404 } else {
@@ -416,7 +419,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_d
416419 );
417420
418421 std::string outfname (target_file);
419- std::string fileExtension = " " ;
422+ std::string fileExtension;
420423
421424 if (outfname.size () > FILE_EXTENSION_SIZE) {
422425 fileExtension = outfname.substr (outfname.size () - FILE_EXTENSION_SIZE, FILE_EXTENSION_SIZE);
@@ -452,7 +455,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_d
452455 }
453456
454457 const jbyte fileSig[FILE_SIGNATURE_SIZE] = {0x42 , 0x45 , 0x54 , 0x48 , 0x45 , 0x4c , 0x41 };
455- jbyte *fileSigRead = threadEnv->GetByteArrayElements (fileSignature, NULL );
458+ auto *fileSigRead = (jbyte *) threadEnv->GetPrimitiveArrayCritical (fileSignature, nullptr );
456459
457460 bool fileSignatureIncorrect = false ;
458461 for (int i = 0 ; i < FILE_SIGNATURE_SIZE; ++i) {
@@ -462,6 +465,9 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_d
462465 }
463466 }
464467
468+ jboolean signFailed = std::memcmp (fileSigRead, fileSig, FILE_SIGNATURE_SIZE);
469+ threadEnv->ReleasePrimitiveArrayCritical (fileSignature, fileSigRead, JNI_ABORT);
470+
465471 if (fileSignatureIncorrect) {
466472 incoming_bytes.close ();
467473 outgoing_bytes.close ();
@@ -473,16 +479,13 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_d
473479 jbyteArray jniBuffer = threadEnv->NewByteArray (BUFFER_SIZE);
474480 jbyteArray jniIV = threadEnv->NewByteArray (AES_BLOCK_SIZE);
475481 incoming_bytes.read (jniIV);
476- jbyte *iv = threadEnv->GetByteArrayElements (jniIV, NULL );
482+ jbyte *iv = threadEnv->GetByteArrayElements (jniIV, nullptr );
477483
478484 std::string properFileExtension = " .bthl" ;
479485
480- jboolean signFailed = std::memcmp (fileSigRead, fileSig, FILE_SIGNATURE_SIZE);
481486 jboolean wrongFileExtension = fileExtension != properFileExtension;
482487 jboolean JNIException = threadEnv->ExceptionCheck ();
483488
484- threadEnv->ReleaseByteArrayElements (fileSignature, fileSigRead, 0 );
485-
486489 if (signFailed || wrongFileExtension || JNIException) {
487490 if (JNIException) {
488491 threadEnv->ExceptionDescribe ();
@@ -506,7 +509,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_d
506509 break ;
507510 }
508511
509- prevBuffer = (jbyte *) threadEnv->GetPrimitiveArrayCritical (prevJniBuffer, NULL );
512+ prevBuffer = (jbyte *) threadEnv->GetPrimitiveArrayCritical (prevJniBuffer, nullptr );
510513 for (size_t index = 0 ; index < prevBufferSize; index += AES_BLOCK_SIZE) {
511514 aes_scheme.blockDecrypt (
512515 reinterpret_cast <unsigned char *>(prevBuffer + index),
@@ -518,7 +521,9 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_d
518521
519522 threadEnv->SetByteArrayRegion (
520523 prevJniBuffer, 0 , prevBufferSize,
521- reinterpret_cast <jbyte *>(decryptedBuffer));
524+ reinterpret_cast <jbyte *>(decryptedBuffer)
525+ );
526+
522527 outgoing_bytes.write (prevJniBuffer, 0 , prevBufferSize);
523528
524529 std::swap (prevJniBuffer, nextJniBuffer);
@@ -534,17 +539,17 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_d
534539 bool excludeLastBlock = (remainingBlocks && remainingBytes == 0 );
535540
536541 if (remainingBlocks) {
537- prevBuffer = (jbyte *) threadEnv->GetPrimitiveArrayCritical (prevJniBuffer, NULL );
538542
543+ prevBuffer = (jbyte *) threadEnv->GetPrimitiveArrayCritical (prevJniBuffer, nullptr );
539544 for (; index < remainingBlocks - excludeLastBlock; ++index) {
540545 aes_scheme.blockDecrypt (
541546 reinterpret_cast <unsigned char *>(prevBuffer + (index * AES_BLOCK_SIZE)),
542547 reinterpret_cast <unsigned char *>(decryptedBuffer + (index * AES_BLOCK_SIZE)),
543548 reinterpret_cast <unsigned char *>(iv)
544549 );
545550 }
546-
547551 threadEnv->ReleasePrimitiveArrayCritical (prevJniBuffer, prevBuffer, JNI_ABORT);
552+
548553 threadEnv->SetByteArrayRegion (
549554 prevJniBuffer, 0 , (remainingBlocks - excludeLastBlock) * AES_BLOCK_SIZE,
550555 reinterpret_cast <jbyte *>(decryptedBuffer)
@@ -555,8 +560,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_d
555560
556561 Krypt::ByteArray recover;
557562
558- prevBuffer = (jbyte *) threadEnv->GetPrimitiveArrayCritical (prevJniBuffer, NULL );
559-
563+ prevBuffer = (jbyte *) threadEnv->GetPrimitiveArrayCritical (prevJniBuffer, nullptr );
560564 if (excludeLastBlock) {
561565 recover = aes_scheme.decrypt (
562566 reinterpret_cast <unsigned char *>(prevBuffer + (index * AES_BLOCK_SIZE)),
@@ -570,9 +574,9 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_d
570574 reinterpret_cast <unsigned char *>(iv)
571575 );
572576 }
573-
574577 threadEnv->ReleasePrimitiveArrayCritical (prevJniBuffer, prevBuffer, JNI_ABORT);
575- threadEnv->ReleaseByteArrayElements (jniIV, iv, 0 );
578+
579+ threadEnv->ReleaseByteArrayElements (jniIV, iv, JNI_ABORT);
576580
577581 threadEnv->SetByteArrayRegion (
578582 prevJniBuffer, 0 , recover.length ,
@@ -604,7 +608,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_d
604608 }
605609 };
606610
607- int physical_threads = std::thread::hardware_concurrency ();
611+ int physical_threads = ( int ) std::thread::hardware_concurrency ();
608612
609613 std::vector<std::thread> threads;
610614
@@ -614,7 +618,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_d
614618 vector_mtx.unlock ();
615619
616620 if (notEmpty) {
617- threads.push_back ( std::thread ( decrypt_lambda) );
621+ threads.emplace_back ( decrypt_lambda);
618622 }
619623 }
620624
@@ -626,8 +630,8 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_d
626630 decrypt_lambda ();
627631 }
628632
629- for (size_t i = 0 ; i < threads. size (); ++i ) {
630- threads[i] .join ();
633+ for (auto & thrd : threads) {
634+ thrd .join ();
631635 }
632636
633637 env->DeleteGlobalRef (DocumentFileClass);
0 commit comments