From c8ab26e9f5aa398a208fcac3d8d11335f6c72632 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Mon, 10 Nov 2025 16:09:10 -0600 Subject: Touchup --- src/encryption.c | 67 +++++++++++++++++++++++++++----------------------------- src/shared.c | 11 ++++------ src/threadpool.c | 34 ++++++++++++++-------------- 3 files changed, 53 insertions(+), 59 deletions(-) (limited to 'src') diff --git a/src/encryption.c b/src/encryption.c index 2d48e4e..9b264dc 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -138,6 +138,7 @@ CLEANUP_linkto: * @param dst Destination to write encrypted file * @param key Key for encryption * @retval (int)[-1, 0] Returns 0 on success, sets errno and returns -1 on error + * @todo Rewrite this into being one of my own functions instead of copying from libsodium */ int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { if(!src || !dst || !key) ERRRET(EINVAL, -1); @@ -187,6 +188,7 @@ int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr * @param dst Destination to write decrypted file * @param key Key used to encrypt * @retval (int)[-1, 0] Returns 0 on success, sets errno and returns -1 on error + * @todo Rewrite this into being one of my own functions instead of copying from libsodium */ int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { if(!src || !dst || !key) ERRRET(EINVAL, -1); @@ -250,42 +252,55 @@ int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr * @param output * @param key * @retval (int)[,] + * @todo Fill out warning messages & documentation */ int encryptviatmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { if(!target || !output || !key) ERRRET(EINVAL, -1); - int fd = -1, tfd = -1, res = -1; + int fd = -1, tfd = -1, res = -1, eflag = 0; FILE *src, *dst; char *targetdir; // Open the target file - if((fd = open(target, O_RDONLY)) < 0) return -1; + if((fd = open(target, O_RDONLY)) < 0) {eflag = 1; goto CLEANUP_encryptviatmp;} // Create a temp file for writing targetdir = vxdirname(output); - if(!targetdir) goto CLEANUP_encryptviatmp; + if(!targetdir) {eflag = 2; goto CLEANUP_encryptviatmp;} // Actually get the file descriptor for the temp file tfd = maketmp(targetdir); - if(tfd < 0) goto CLEANUP_encryptviatmp; + if(tfd < 0) {eflag = 3; goto CLEANUP_encryptviatmp;} // Create a FILE* version of the source fd - if(!(src = fdopen(fd, "rb"))) goto CLEANUP_encryptviatmp; + if(!(src = fdopen(fd, "rb"))) {eflag = 4; goto CLEANUP_encryptviatmp;} // Create a FILE* version of the target fd - if(!(dst = fdopen(tfd, "wb"))) goto CLEANUP_encryptviatmp; + if(!(dst = fdopen(tfd, "wb"))) {eflag = 5; goto CLEANUP_encryptviatmp;} // Do the encryption now that everything has been set up - if(encrypttofile(src, dst, key) < 0) // Not going to bother changing this, it probably is catastrophic if an error happens when it shouldn't - ERROR(1, ENOTRECOVERABLE, " I don't even have a way to cause an error here. How did you do it?",); + if(encrypttofile(src, dst, key) < 0) {eflag = 6; goto CLEANUP_encryptviatmp;} // Link the temp file into the system - if(linkto(output, tfd) < 0) - WARN(errno, " Could not link \"%s\" into system after encryption", , output); + if(linkto(output, tfd) < 0) {eflag = 7; goto CLEANUP_encryptviatmp;} res = 0; CLEANUP_encryptviatmp: + // TODO: Add warning messages for verbose errors + if(___VXGG___VERBOSE_ERRORS___) { + switch (eflag) { + case 1: WARN(errno, " Warning: Could not open target fd \"%s\"",, target); + case 2: WARN(errno, " Warning: Could not get real dirname for \"%s\"",, output); + case 3: WARN(errno, " Warning: Could not make temp file in target dir \"%s\"",, targetdir); + case 4: WARN(errno, " Warning: Could not get FILE* handle for source file \"%s\"",, target); + case 5: WARN(errno, " Warning: Could not get FILE* handle for output file",); + case 6: ERROR(1, ENOTRECOVERABLE, " ERROR: I don't even have a way to cause an error here. How did you do it?",); + case 7: WARN(errno, " Warning: Could not link \"%s\" into system after encryption",, output); + } + } + + free(targetdir); fclose(src); fclose(dst); @@ -302,6 +317,7 @@ CLEANUP_encryptviatmp: * @param target * @param key * @retval (int)[,] + * @todo Fill out documentation */ int decryptto(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { if(!target || !output || !key) ERRRET(EINVAL, -1); @@ -333,12 +349,12 @@ CLEANUP_decryptto: if(___VXGG___VERBOSE_ERRORS___) { switch (eflag) { - case 0: WARN(errno, " Could not open \"%s\" for decryption", , target); break; - case 1: WARN(errno, " Could not get temp file for decryption", ); break; - case 2: WARN(errno, " Could not open \"%s\" for writing decrypted data", , output); break; - case 3: ERROR(1, errno, " How did you even cause an error?",); break; - case 4: WARN(errno, " Could not link \"%s\" into system", , output); break; - default: WARN(errno, " Ran into an error", ); break; + case 0: WARN(errno, " Could not open \"%s\" for decryption",, target); break; + case 1: WARN(errno, " Could not get temp file for decryption",); break; + case 2: WARN(errno, " Could not open \"%s\" for writing decrypted data",, output); break; + case 3: ERROR(1, errno, " How did you even cause an error?",); break; + case 4: WARN(errno, " Could not link \"%s\" into system",, output); break; + default: WARN(errno, " Ran into an error",); break; } } @@ -353,7 +369,6 @@ CLEANUP_decryptto: * @retval (int)[-1, words] On success, returns the number of words requested. On error, returns -1 and sets errno */ int genpassword(char **str, unsigned int words) { - // Early returns if(words < 1) return 0; if(!str) ERRRET(EINVAL, -1); @@ -378,24 +393,6 @@ int genpassword(char **str, unsigned int words) { return words; } -/** - * @brief sodium_malloc wrapper. - * - * Calls `error()` or `abort()` depnding on the value of `___VXGG___VXALLOC_EXIT_ON_ERROR___`. Will make sure libsodium is initialized if `___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0` - * - * @param size - * @retval (void*) A pointer to some data allocated via `sodium_malloc()` - */ -void* xsodium_malloc(size_t size) { - void *mem = sodium_malloc(size); - if(mem == NULL) - XALLOC_EXIT(" could not allocate memory... Quitting", ); - - return mem; -} - - - // TODO: Rewrite this to use the threadpool. Each newly scanned file should be pushed onto the threadpool as an encryption task // #include diff --git a/src/shared.c b/src/shared.c index 2397bd6..b6d27e4 100644 --- a/src/shared.c +++ b/src/shared.c @@ -28,13 +28,10 @@ #define ___VXGG___VERBOSE_ERRORS___ 1 //! Macro to exit on an alloc error instead of doing the terrible nested if statement that was being used previously -#define XALLOC_EXIT(msg, ...) do {\ - if(!___VXGG___VXALLOC_EXIT_ON_ERROR___)\ - abort();\ - if(!___VXGG___VERBOSE_ERRORS___)\ - exit(EXIT_FAILURE);\ - error(EXIT_FAILURE, errno, (msg)__VA_ARGS__);\ - exit(EXIT_FAILURE); /* Makes gcc happy */\ +#define XALLOC_EXIT(msg, ...) do { \ + if(___VXGG___VERBOSE_ERRORS___) error(EXIT_FAILURE, errno, (msg)__VA_ARGS__); \ + if(___VXGG___VXALLOC_EXIT_ON_ERROR___) exit(EXIT_FAILURE); \ + abort(); \ } while (0) //! Error macro that gcc will not complain about diff --git a/src/threadpool.c b/src/threadpool.c index 959c060..7a2f93a 100644 --- a/src/threadpool.c +++ b/src/threadpool.c @@ -182,14 +182,15 @@ taskqueue * taskqueue_new(void) { * @param tq A taskqueue to be freed. May be null */ void taskqueue_free(void *tq) { - if(!tq) return; + taskqueue *real = tq; + if(!real) return; - for(tqnode *p = ((taskqueue*)tq)->start, *n; p != NULL;) { + for(tqnode *p = real->start, *n; p != NULL;) { n = p->next; tqnode_free(p); p = n; } - free(tq); + free(real); return; } @@ -221,7 +222,8 @@ int taskqueue_push(taskqueue *tq, task *tsk) { int hf; if((hf = taskqueue_handlefirst(tq, tsk))) return (hf >= 0) ? 0 : -1; - tqnode *curstart = tq->start, *newstart = tqnode_new(tq->start, NULL, tsk); + tqnode *curstart = tq->start; + tqnode *newstart = tqnode_new(curstart, NULL, tsk); if(!newstart) return -1; curstart->prev = newstart; @@ -241,18 +243,18 @@ task * taskqueue_pop(taskqueue *tq) { if(!tq) ERRRET(EINVAL, NULL); if(tq->size <= 0) ERRRET(ENODATA, NULL); - tqnode *end = tq->end; - task *ret = end->task; + tqnode *curend = tq->end; + task *ret = curend->task; if(tq->size == 1) { tq->end = NULL; tq->start = NULL; } else { - tq->end = end->prev; + tq->end = curend->prev; tq->end->next = NULL; } - free(end); + free(curend); tq->size--; return ret; } @@ -270,7 +272,8 @@ int taskqueue_pushfront(taskqueue *tq, task *tsk) { int hf; if((hf = taskqueue_handlefirst(tq, tsk))) return (hf >= 0) ? 0 : -1; - tqnode *end =tq->end, *newend = tqnode_new(NULL, tq->end, tsk); + tqnode *end = tq->end; + tqnode *newend = tqnode_new(NULL, end, tsk); if(!newend) return -1; end->next = newend; @@ -290,18 +293,18 @@ task * taskqueue_popback(taskqueue *tq) { if(!tq) ERRRET(EINVAL, NULL); if(tq->size <= 0) ERRRET(ENODATA, NULL); - tqnode *start = tq->start; - task *ret = start->task; + tqnode *curstart = tq->start; + task *ret = curstart->task; if(tq->size == 1) { tq->start = NULL; tq->end = NULL; } else { - tq->start = start->next; + tq->start = curstart->next; tq->start->prev = NULL; } - free(start); + free(curstart); tq->size--; return ret; } @@ -321,7 +324,6 @@ int taskqueue_size(taskqueue *tq) { mtx_unlock(&(ctq)->mutex); \ return (retval); \ } \ - \ code \ mtx_unlock(&(ctq)->mutex); \ } while (0) @@ -413,8 +415,6 @@ void ctqueue_free(void *ctq) { free(real->thrdarr); free(real); - // TODO: figure out if it's necessary / a good idea to do error handling on these functions - return; } @@ -424,7 +424,7 @@ void ctqueue_free(void *ctq) { * * @param ctq The concurrent taskqueue to modify. Must be non-null * @param tsk The task to push. Must be non-null - * @retval (int) Returns `thrd_success` on success, returns `thrd_error` or `thrd_nomem` on error + * @retval (int)[`thrd_error` | `thrd_nomem`, `thrd_success`] */ int ctqueue_waitpush(ctqueue *ctq, task *tsk) { if(!ctq || !tsk) ERRRET(EINVAL, -1); -- cgit v1.2.3