diff options
| author | @syxhe <https://t.me/syxhe> | 2025-06-14 18:16:03 -0500 |
|---|---|---|
| committer | @syxhe <https://t.me/syxhe> | 2025-06-14 18:16:03 -0500 |
| commit | d5de3e243a8481ad879dd8effc5759d406dc90b7 (patch) | |
| tree | 8dfcb2e8b0fc22e4162748c46065c252e178c14c | |
| parent | d26eeecfc7e4c997bc441a0879ddb8c2aba3898f (diff) | |
Work on implementing getfilelist function
| -rw-r--r-- | src/encryption.c | 109 |
1 files changed, 92 insertions, 17 deletions
diff --git a/src/encryption.c b/src/encryption.c index 02abccd..65b8843 100644 --- a/src/encryption.c +++ b/src/encryption.c | |||
| @@ -222,8 +222,8 @@ int encryptviatmp(const char * const target, const char * const output, const un | |||
| 222 | return 0; | 222 | return 0; |
| 223 | } | 223 | } |
| 224 | 224 | ||
| 225 | int decryptto(const char * const encrypted, const char * const target, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { | 225 | int decryptto(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { |
| 226 | if(!encrypted || !target || !key) ERRRET(EINVAL, -1); | 226 | if(!target || !output || !key) ERRRET(EINVAL, -1); |
| 227 | #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | 227 | #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 |
| 228 | checksodium(); | 228 | checksodium(); |
| 229 | #endif | 229 | #endif |
| @@ -233,15 +233,15 @@ int decryptto(const char * const encrypted, const char * const target, const uns | |||
| 233 | int fdst; | 233 | int fdst; |
| 234 | 234 | ||
| 235 | // Open the source file | 235 | // Open the source file |
| 236 | if(!(src = fopen(encrypted, "rb"))) { | 236 | if(!(src = fopen(target, "rb"))) { |
| 237 | WARN(errno, "<decryptto> Could not open \"%s\" for decryption", , encrypted); | 237 | WARN(errno, "<decryptto> Could not open \"%s\" for decryption", , target); |
| 238 | return -1; | 238 | return -1; |
| 239 | } | 239 | } |
| 240 | cleanup_REGISTER(__ucl_fclose, src); | 240 | cleanup_REGISTER(__ucl_fclose, src); |
| 241 | 241 | ||
| 242 | // Get a temp descriptor for the temp file | 242 | // Get a temp descriptor for the temp file |
| 243 | cleanup_CNDEXEC( | 243 | cleanup_CNDEXEC( |
| 244 | fdst = maketmp(target); | 244 | fdst = maketmp(output); |
| 245 | if(!fdst) { | 245 | if(!fdst) { |
| 246 | WARN(errno, "<decryptto> Could not get temp file for decryption", ); | 246 | WARN(errno, "<decryptto> Could not get temp file for decryption", ); |
| 247 | cleanup_MARK(); | 247 | cleanup_MARK(); |
| @@ -252,7 +252,7 @@ int decryptto(const char * const encrypted, const char * const target, const uns | |||
| 252 | // Open a FILE* version of the temp file | 252 | // Open a FILE* version of the temp file |
| 253 | cleanup_CNDEXEC( | 253 | cleanup_CNDEXEC( |
| 254 | if(!(dst = fdopen(fdst, "wb"))) { | 254 | if(!(dst = fdopen(fdst, "wb"))) { |
| 255 | WARN(errno, "<decryptto> Could not open \"%s\" for writing decrypted data", , target); | 255 | WARN(errno, "<decryptto> Could not open \"%s\" for writing decrypted data", , output); |
| 256 | cleanup_MARK(); | 256 | cleanup_MARK(); |
| 257 | } | 257 | } |
| 258 | cleanup_CNDREGISTER(__ucl_fclose, dst); | 258 | cleanup_CNDREGISTER(__ucl_fclose, dst); |
| @@ -264,8 +264,8 @@ int decryptto(const char * const encrypted, const char * const target, const uns | |||
| 264 | ERROR(1, errno, "<decryptto> How did you even cause an error?",); | 264 | ERROR(1, errno, "<decryptto> How did you even cause an error?",); |
| 265 | 265 | ||
| 266 | // Link temp into system | 266 | // Link temp into system |
| 267 | if(linkto(target, fdst) < 0) | 267 | if(linkto(output, fdst) < 0) |
| 268 | WARN(errno, "<decryptto> Could not link \"%s\" into system", , target); | 268 | WARN(errno, "<decryptto> Could not link \"%s\" into system", , output); |
| 269 | 269 | ||
| 270 | fclose(dst); | 270 | fclose(dst); |
| 271 | fclose(src); | 271 | fclose(src); |
| @@ -461,6 +461,56 @@ void* xsodium_malloc(size_t size) { | |||
| 461 | 461 | ||
| 462 | 462 | ||
| 463 | 463 | ||
| 464 | struct __FLC_FKP { | ||
| 465 | char *target; | ||
| 466 | char *output; | ||
| 467 | const unsigned char *key; | ||
| 468 | }; | ||
| 469 | |||
| 470 | struct __FLC_FKP * fkp_init(char * const target, char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { | ||
| 471 | if(!target || !output || !key) ERRRET(EINVAL, NULL); | ||
| 472 | struct __FLC_FKP *fkp = calloc(1, sizeof(*fkp)); | ||
| 473 | if(!fkp) | ||
| 474 | return NULL; | ||
| 475 | |||
| 476 | fkp->key = key; | ||
| 477 | fkp->output = output; | ||
| 478 | fkp->target = target; | ||
| 479 | |||
| 480 | return fkp; | ||
| 481 | } | ||
| 482 | |||
| 483 | void fkp_free(void *fkp) { | ||
| 484 | if(!fkp) return; | ||
| 485 | struct __FLC_FKP *real = (struct __FLC_FKP *)fkp; | ||
| 486 | |||
| 487 | free(real->output); | ||
| 488 | free(real->target); | ||
| 489 | free(real); | ||
| 490 | |||
| 491 | return; | ||
| 492 | } | ||
| 493 | |||
| 494 | static int __FLC_TASK_ENCRYPT(void *fkp) { | ||
| 495 | if(!fkp) | ||
| 496 | return -1; | ||
| 497 | struct __FLC_FKP *real = (struct __FLC_FKP *)fkp; | ||
| 498 | return encryptviatmp(real->target, real->output, real->key); | ||
| 499 | } | ||
| 500 | static int __FLC_TASK_DECRYPT(void *fkp) { | ||
| 501 | if(!fkp) | ||
| 502 | return -1; | ||
| 503 | struct __FLC_FKP *real = (struct __FLC_FKP *)fkp; | ||
| 504 | return decryptto(real->target, real->output, real->key); | ||
| 505 | } | ||
| 506 | |||
| 507 | enum VXGG_FLC { | ||
| 508 | VXGG_FLC__INVALID, | ||
| 509 | VXGG_FLC__ENCRYPT, | ||
| 510 | VXGG_FLC__DECRYPT, | ||
| 511 | VXGG_FLC__TOOBIG | ||
| 512 | }; | ||
| 513 | |||
| 464 | struct __ucl_namelist_vals { | 514 | struct __ucl_namelist_vals { |
| 465 | struct dirent **namelist; | 515 | struct dirent **namelist; |
| 466 | int entries; | 516 | int entries; |
| @@ -475,15 +525,16 @@ static void __ucl_namelist(void *namelist) { | |||
| 475 | return; | 525 | return; |
| 476 | } | 526 | } |
| 477 | 527 | ||
| 478 | enum VXGG_FLC { | 528 | // TODO: Write these |
| 479 | VXGG_FLC__INVALID, | 529 | static int encryption_filter(const struct dirent *de) { |
| 480 | VXGG_FLC__ENCRYPT, | ||
| 481 | VXGG_FLC__DECRYPT, | ||
| 482 | VXGG_FLC__TOOBIG | ||
| 483 | }; | ||
| 484 | 530 | ||
| 485 | ctqueue * getfilelist(enum VXGG_FLC mode, const char * const dir, int threads, int (*filter)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **)) { | 531 | } |
| 486 | if(mode <= VXGG_FLC__INVALID || mode >= VXGG_FLC__TOOBIG || !dir || threads <= 0 || !filter || !compar) ERRRET(EINVAL, NULL); | 532 | static int decryption_filter(const struct dirent *de) { |
| 533 | |||
| 534 | } | ||
| 535 | |||
| 536 | ctqueue * getfilelist(enum VXGG_FLC mode, const char * const dir, int threads, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { | ||
| 537 | if(mode <= VXGG_FLC__INVALID || mode >= VXGG_FLC__TOOBIG || !dir || threads <= 0 || !key) ERRRET(EINVAL, NULL); | ||
| 487 | 538 | ||
| 488 | cleanup_CREATE(10); | 539 | cleanup_CREATE(10); |
| 489 | ctqueue *ctq = NULL; | 540 | ctqueue *ctq = NULL; |
| @@ -497,12 +548,36 @@ ctqueue * getfilelist(enum VXGG_FLC mode, const char * const dir, int threads, | |||
| 497 | 548 | ||
| 498 | // Get the scandir list | 549 | // Get the scandir list |
| 499 | struct dirent **namelist; | 550 | struct dirent **namelist; |
| 500 | if((files = scandir(dir, &namelist, filter, compar)) < 0) | 551 | if((files = scandir(dir, &namelist, ((mode == VXGG_FLC__ENCRYPT) ? encryption_filter : decryption_filter), alphasort)) < 0) |
| 501 | cleanup_MARK(); | 552 | cleanup_MARK(); |
| 502 | cleanup_CNDREGISTER(__ucl_namelist, (void*)(&(struct __ucl_namelist_vals){.namelist = namelist, .entries = files})); | 553 | cleanup_CNDREGISTER(__ucl_namelist, (void*)(&(struct __ucl_namelist_vals){.namelist = namelist, .entries = files})); |
| 503 | 554 | ||
| 504 | // Push everything onto the ctqueue | 555 | // Push everything onto the ctqueue |
| 505 | // TODO: Write task* compatible callbacks for encryption and decryption, then populate the ctqueue based on the specified mode | 556 | // TODO: Write task* compatible callbacks for encryption and decryption, then populate the ctqueue based on the specified mode |
| 557 | cleanup_CNDEXEC( | ||
| 558 | for(int i = 0; i < files; i++) { | ||
| 559 | |||
| 560 | // Target is either "filename" or "filename.vxggr" | ||
| 561 | // Output is either "filename.vxggr" or "filename" | ||
| 562 | |||
| 563 | struct __FLC_FKP *fkp = fkp_init(target, output, key); | ||
| 564 | if(!fkp) { | ||
| 565 | WARN(errno, "<getfilelist> Could not create file-key pair for \"%s\"'s %scryption task, skipping...",, namelist[i]->d_name, ((mode == VXGG_FLC__ENCRYPT) ? "en" : "de")); | ||
| 566 | free(target); | ||
| 567 | free(output); | ||
| 568 | } | ||
| 569 | task *ctask = task_init((mode == VXGG_FLC__ENCRYPT) ? __FLC_TASK_ENCRYPT : __FLC_TASK_DECRYPT, fkp_free, fkp); | ||
| 570 | if(!ctask) { | ||
| 571 | WARN(errno, "<getfilelist> Could not push \"%s\"'s %scryption task, skipping...",, namelist[i]->d_name, ((mode == VXGG_FLC__ENCRYPT) ? "en" : "de")); | ||
| 572 | free(namelist[i]); | ||
| 573 | continue; | ||
| 574 | } | ||
| 575 | ctqueue_waitpush(ctq, ctask); | ||
| 576 | } | ||
| 577 | |||
| 578 | free(namelist); | ||
| 579 | // namelist is the array that holds each pointer to each dirent, so it needs to be free'd separately from the other elements | ||
| 580 | ); | ||
| 506 | 581 | ||
| 507 | cleanup_CNDFIRE(); | 582 | cleanup_CNDFIRE(); |
| 508 | if(cleanup_ERRORFLAGGED) | 583 | if(cleanup_ERRORFLAGGED) |
