From 1a590a5859207d5adb4f7a72d6b9ed1ab301e8c4 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Sun, 26 Jan 2025 23:24:58 -0600 Subject: Reimplement dirname() as xdirname() --- src/encryption.c | 81 +++++++++------------------------------ src/shared.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++---- src/shared.h | 4 ++ 3 files changed, 130 insertions(+), 70 deletions(-) diff --git a/src/encryption.c b/src/encryption.c index 32a5ce0..e4fed1f 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -19,7 +19,11 @@ #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 void naclfaildefault(void *none) { none = none; // Makes gcc happy - error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting..."); + #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 + error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting..."); + #else + exit(EXIT_FAILURE); + #endif } int checksodiumcb(const vxgg_naclfailcb callback, void *data) { @@ -49,7 +53,11 @@ void checksodium(void) { checksodiumcb(NULL, NULL); #else if(sodium_init() < 0) - error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting..."); + #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 + error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting..."); + #else + exit(EXIT_FAILURE); + #endif #endif return; @@ -75,25 +83,12 @@ int maketmp(const char * const dest) { return open(dest, (O_TMPFILE | O_WRONLY | O_CLOEXEC | O_SYNC), (S_IRUSR | S_IWUSR)); } -int encrypttotmp(const char * const toencrypt) { +int encrypttotmp(const char * const target, const char * const output, const char * const password, int chunksize) { #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 checksodium(); #endif - struct stat esb; - int efd = -1; - - // Make sure the file is real and an actual file that can be encrypted - if(stat(toencrypt, &esb) < 0) - return -1; - if(!S_ISREG(esb.st_mode)) - return -2; - - // Open the file as read-only - if((efd = open(toencrypt, O_RDONLY)) < 0) - return -3; - - // Need to get a secret key from a password and then set up cryptostream from libsodium + return 0; } @@ -136,7 +131,11 @@ void* xsodium_malloc(size_t size) { void *mem = sodium_malloc(size); if(mem == NULL) { #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 - error(1, errno, "xsodium_malloc: could not allocate memory... Quitting"); + #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 + error(1, errno, " could not allocate memory... Quitting"); + #else + exit(EXIT_FAILURE); + #endif #endif abort(); @@ -218,7 +217,7 @@ int main(void) { //*/// - //*// Example code for generating a key from a password and encrypting a test file + /*// Example code for generating a key from a password and encrypting a test file const char *dir = ".", *fname = "toBeEncrypted.test.txt", *pass = "this is a password"; char *path = NULL, *message = NULL, *efname = NULL; @@ -302,50 +301,6 @@ int main(void) { free(buf); free(cbuf); - - // Ok now decryption to make sure I didn't fuck it - unsigned char dheader[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; - crypto_secretstream_xchacha20poly1305_state dstate; - - if((fd = open(efname, O_RDONLY)) < 0) - error(1, errno, "Could not open file for decryption"); - if((tfd = open("lmao.test.dec", O_WRONLY | O_CREAT | O_TRUNC, (S_IRUSR | S_IWUSR))) < 0) - error(1, errno, "Could not open file for result of decryption"); - - if(read(fd, header, sizeof(header)) < 0) - error(1, errno, "Could not read header of encrypted file"); - - if(crypto_secretstream_xchacha20poly1305_init_pull(&dstate, dheader, key) != 0) - error(1, EINVAL, "Incomplete header"); - - bytesread = -1; - unsigned char tag = 255; - buf = xcalloc(CHUNK_SIZE + 1, sizeof(*buf)); - cbuf = xcalloc((CHUNK_SIZE + 1) + crypto_secretstream_xchacha20poly1305_ABYTES, sizeof(*cbuf)); - while((bytesread = read(fd, cbuf, (CHUNK_SIZE + 1) + crypto_secretstream_xchacha20poly1305_ABYTES)) >= 0) { - if(crypto_secretstream_xchacha20poly1305_pull(&dstate, buf, NULL, &tag, cbuf, bytesread, NULL, 0) < 0) - error(1, errno, "Found a corrupted chunk"); - - if(tag == crypto_secretstream_xchacha20poly1305_TAG_FINAL && bytesread != 0) - error(1, errno, "End tag before end of file"); - - if(bytesread == 0 && tag != crypto_secretstream_xchacha20poly1305_TAG_FINAL) - error(1, errno, "End of file before end tag"); - - if(writewholebuffer(tfd, buf, bytesread) < 0) - error(1, errno, "write() error"); - - if(bytesread == 0) - break; - } - if(bytesread < 0) - error(1, errno, "read() error"); - - close(fd); - close(tfd); - free(buf); - free(cbuf); - //*/// return 0; diff --git a/src/shared.c b/src/shared.c index 40240d5..c46b001 100644 --- a/src/shared.c +++ b/src/shared.c @@ -13,7 +13,11 @@ void* xcalloc(size_t nmemb, size_t size) { if(!mem) { #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 - error(1, errno, " Could not allocate memory"); + #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 + error(1, errno, " Could not allocate memory"); + #else + exit(EXIT_FAILURE); + #endif #endif abort(); @@ -27,8 +31,11 @@ void* xreallocarray(void *ptr, size_t nmemb, size_t size) { void *mem = reallocarray(ptr, nmemb, size); if(mem == NULL) { #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 - error(1, errno, " Could not allocate memory"); - + #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 + error(1, errno, " Could not allocate memory"); + #else + exit(EXIT_FAILURE); + #endif #endif abort(); @@ -56,7 +63,9 @@ int readwholebuffer(char **str, unsigned long int initsize, int fd) { tmp = realloc(lstr, csize * sizeof(char)); if(!tmp) { - error(0, errno, "Could not reallocate enough space for lstr"); + #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 + error(0, errno, "Could not reallocate enough space for lstr"); + #endif free(lstr); lstr = NULL; // Need to set this because of the break bytesread = -100; @@ -66,7 +75,9 @@ int readwholebuffer(char **str, unsigned long int initsize, int fd) { } } if(bytesread < 0 && bytesread != -100) { - error(0, errno, "Ran into a read() error"); + #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 + error(0, errno, "Ran into a read() error"); + #endif free(lstr); lstr = NULL; } @@ -74,7 +85,9 @@ int readwholebuffer(char **str, unsigned long int initsize, int fd) { if(lstr) { tmp = realloc(lstr, csize - ccap + 1); if(!tmp) { - error(0, errno, "Could not shrink lstr after reading buffer"); + #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 + error(0, errno, "Could not shrink lstr after reading buffer"); + #endif free(lstr); bytesread = -100; } @@ -107,4 +120,92 @@ int writewholebuffer(int fd, const unsigned char *buf, int len) { } // Adapted from Beej's `sendall()` function // https://beej.us/guide/bgnet/html/split/slightly-advanced-techniques.html#sendall - // Thanks Beej! \ No newline at end of file + // Thanks Beej! + +// dirname but less retarded hopefully +char *xdirname(const char * const path) { + char *tmp = NULL; + if(!path) { // Path being null is a special case which should return super early, before anything else + tmp = strdup("."); + if(!tmp) + abort(); + + return tmp; + } + + unsigned char flag = 0; + if(strcmp(path, ".") == 0) {tmp = strdup("."); flag++;} + if(strcmp(path, "/") == 0 && !flag) {tmp = strdup("/"); flag++;} + if(strcmp(path, "..") == 0 && !flag) {tmp = strdup("."); flag++;} + + if(flag) { + if(!tmp) + abort(); + + return tmp; + } + + + /* From the manpages: (man 3 dirname) + // +=======================================+ + // | path dirname basename | + // +=======================================+ + // | /usr/lib /usr lib | + // | /usr/ / usr | + // | usr . usr | + // +=======================================+ + */ + + // Get a temp copy of the path for manipulation purposes + tmp = strdup(path); + if(!tmp) + abort(); + + // If there's a trailing '/', delete it + size_t pathlen = strlen(path); + if(tmp[pathlen - 1] == '/') { + tmp[pathlen - 1] = '\0'; + pathlen--; + } + + // Ok, I think the easiest way to do this (if maybe a bit slow) is to count the number of '/'s in the string + // If there's only one, return '/' + // If there are 2 or more, find the last one in the list and set it to '\0' + + size_t count = 0; + for(size_t i = 0; i < pathlen; i++) { + if(tmp[i] == '/') + count++; + } + + if(count == 0) { + free(tmp); + tmp = strdup("."); + if(!tmp) + abort(); + + return tmp; + } + if(count == 1) { + free(tmp); + tmp = strdup("/"); + if(!tmp) + abort(); + + return tmp; + } + + for(size_t i = 0, c2 = 0; i < pathlen; i++) { + if(tmp[i] == '/') + c2++; + if(c2 == count) + tmp[i] = '\0'; + } + + char * const actual = strdup(tmp); + if(!actual) + abort(); + free(tmp); + + return actual; +} \ No newline at end of file diff --git a/src/shared.h b/src/shared.h index 3602a4e..9cdbd33 100644 --- a/src/shared.h +++ b/src/shared.h @@ -11,6 +11,10 @@ // type functions will ALWAYS 'abort', doing otherwise defeats the purpose of the function type #define ___VXGG___XALLOC_EXIT_ON_ERROR___ 1 +// Defines whether vxgg functions that can error print out a short warning of the error when one is encountered. +// `___VXGG___VERBOSE_ERRORS___ > 0` will print diagnostic error messages, and will do nothing otherwise +#define ___VXGG___VERBOSE_ERRORS___ 1 + // `calloc()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` void* xcalloc(size_t nmemb, size_t size); -- cgit v1.2.3