From 08fb644c7d101551edfe8fc2608e0ac501b3df9f Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Sun, 8 Jun 2025 17:25:13 -0500 Subject: Trim the fat --- src/arena.c | 143 --------------------------------- src/arena.h | 41 ---------- src/encryption.c | 56 ++++++------- src/ll.c | 235 ------------------------------------------------------- src/ll.h | 31 -------- src/main.c | 5 +- src/scanner.c | 49 ++++++------ src/scanner.h | 4 - src/shared.c | 105 +++++++++---------------- src/shared.h | 26 ++---- 10 files changed, 89 insertions(+), 606 deletions(-) delete mode 100644 src/arena.c delete mode 100644 src/arena.h delete mode 100644 src/ll.c delete mode 100644 src/ll.h diff --git a/src/arena.c b/src/arena.c deleted file mode 100644 index 8f32064..0000000 --- a/src/arena.c +++ /dev/null @@ -1,143 +0,0 @@ -#include "arena.h" -#include "shared.h" - -#include -#include -#include -#include -#include -#include - -typedef struct an { - void *membase; - void *memcur; - size_t allocated; - size_t used; - - struct an *next; -} arenanode; - -typedef struct arena { - arenanode *start; - arenanode *current; - - // For keeping track of the largest possible thing that can be allocated to one node - // I don't know if I care to keep this or remove it. I'll see if it becomes a problem / is useful - size_t node_memspace; -} arena; - -arenanode * arenanode_init(size_t bytes) { - if(bytes < 1) ERRRET(EINVAL, NULL); - - arenanode *an = VALLOC(1, sizeof(*an)); - if(!an) - return NULL; - - an->allocated = bytes; - an->used = 0; - an->next = NULL; - - void *mem = VALLOC(bytes, 1); - if(!mem) { - free(an); - return NULL; - } - - an->membase = mem; - an->memcur = mem; - - return an; -} - -arena * arena_init(size_t bytes) { - if(!ISPOWOF2(MEM_ALIGN_BYTES)) XALLOC_EXIT(" \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to create a new arena", ); - if(bytes < 1) ERRRET(EINVAL, NULL); - - arena *a = VALLOC(1, sizeof(arena)); - if(!a) - return NULL; - - a->start = NULL; - a->current = NULL; - a->node_memspace = bytes; - - arenanode *base = arenanode_init(bytes); - if(!base) { - free(a); - return NULL; - } - - a->start = base; - a->current = base; - - return a; -} - -void * arena_alloc(arena * const arena, size_t bytes) { - if(!ISPOWOF2(MEM_ALIGN_BYTES)) XALLOC_EXIT(" \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to allocate any memory", ); - if(!arena) ERRRET(EINVAL, NULL); - if(bytes > arena->node_memspace) ERRRET(ENOMEM, NULL); - - if(bytes > ((arena->current)->allocated - (arena->current)->used)) { - arenanode *new = arenanode_init(arena->node_memspace); - if(!new) - return NULL; - - arena->current->next = new; - arena->current = new; - } - - size_t alignment = MEM_ALIGN(bytes); - size_t offset = bytes + alignment; - - void *mem = arena->current->memcur; - arena->current->memcur = (void*)(((uint8_t*)arena->current->memcur) + offset); - - arena->current->used += offset; - - return mem; - - // Note: This implementation assumes that malloc provides already-aligned memory. If it - // doesn't, that sucks and blows everything up -} - -void arena_free(void *a) { - if(!a) - return; - - arena *real = (arena *)a; - real->current = real->start; - for(arenanode *n; real->current != NULL;) { - n = real->current->next; - - free(real->current->membase); - free(real->current); - - real->current = n; - } - free(real); - - return; -} - - - - -simplearena * simplearena_init(size_t bytes) { - return arena_init(bytes); -} - -void * simplearena_alloc(simplearena * const a, size_t bytes) { - // The criteria to allocate new memory in arena_alloc is 'bytes > ((a->current)->allocated - (a->current)->used)', so if this - // is true, just return NULL & set errno - - if(!a) ERRRET(EINVAL, NULL); - if(bytes > ((a->current)->allocated - (a->current)->used)) ERRRET(ENOMEM, NULL); - - return arena_alloc(a, bytes); -} - -void simplearena_free(simplearena *a) { - arena_free(a); - return; -} \ No newline at end of file diff --git a/src/arena.h b/src/arena.h deleted file mode 100644 index 907a2fa..0000000 --- a/src/arena.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef __VXGG_REWRITE___ARENA_H___25077218513438___ -#define __VXGG_REWRITE___ARENA_H___25077218513438___ - -#include - -// Normal arena. Can expand in size beyond original allocation -typedef struct arena arena; - -// A wrapper for the normal arena struct. Can not expand in size -typedef arena simplearena; - -// Bitwise operation to check if a number is a power of 2 -#define ISPOWOF2(x) (((x) & ((x) - 1)) == 0) - -// Variable for memory alignment -static const size_t MEM_ALIGN_BYTES = (2 * sizeof(void*)); - -// Macro to get the remaining bytes necessary to be on a power of 2 -#define MEM_ALIGN(x) ((x) + (((x) & (MEM_ALIGN_BYTES - 1)) != 0) * (MEM_ALIGN_BYTES - ((x) & (MEM_ALIGN_BYTES - 1)))) - - -// Initialize an arena with `bytes` bytes of pre-allocated memory. Exits / returns NULL on error depending on the value of `___VXGG___USE_XALLOC_FOR_ARENAS___` -arena * arena_init(size_t bytes); - -// Take `bytes` bytes of memory from `arena`'s memory pool. Exits / returns NULL on error depending on the value of `___VXGG___USE_XALLOC_FOR_ARENAS___` -void * arena_alloc(arena * const arena, size_t bytes); - -// Free an arena its memory pool(s). Returns -1 and sets `errno` if the given arena is NULL -void arena_free(void *a); - - -// Initializes a simple arena. -simplearena * simplearena_init(size_t bytes); - -// Grabs some memory from the simple arena's pool. Exits / returns NULL on error depending on the value of `___VXGG___USE_XALLOC_FOR_ARENAS___` -void * simplearena_alloc(simplearena * const a, size_t bytes); - -// Free a simple arena. Exits / returns -1 on error depending on the value of `___VXGG___USE_XALLOC_FOR_ARENAS___` -void simplearena_free(simplearena *a); - -#endif \ No newline at end of file diff --git a/src/encryption.c b/src/encryption.c index 3bf9dd4..b6832ed 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -1,4 +1,7 @@ // TODO: Go back and make sure every function has proper error handling +// Oh fucking christ what have I done +// I need to make sure every single function in this file returns with an indicated error instead of nuking the whole program with +// error() #define _GNU_SOURCE @@ -27,7 +30,7 @@ void naclfaildefault(void *none) { exit(EXIT_FAILURE); } -int checksodiumcb(const vxgg_naclfailcb callback, void *data, unsigned char set) { +int checksodiumcb(vxgg_naclfailcb const callback, void *data, unsigned char set) { static vxgg_naclfailcb cb = naclfaildefault; static void *usr = NULL; int ret; @@ -84,7 +87,9 @@ int linkto(const char * const target, int tgfd) { if(!path) ERROR(1, errno, " Couldn't get path to move file into system",); remove(target); // Make sure an old version isn't sticking around (it's not catastrophic if this fails, but it should be noted or logged somewhere) - return linkat(AT_FDCWD, path, AT_FDCWD, target, AT_SYMLINK_FOLLOW); + int res = linkat(AT_FDCWD, path, AT_FDCWD, target, AT_SYMLINK_FOLLOW); + free(path); + return res; } int encryptviatmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { @@ -127,17 +132,11 @@ int encryptviatmp(const char * const target, const char * const output, const un } int decryptto(const char * const encrypted, const char * const target, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { + if(!encrypted || !target || !key) ERRRET(EINVAL, -1); #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 checksodium(); #endif - - if(!encrypted) - ERRRET(EINVAL, -1); - if(!target) - ERRRET(EINVAL, -1); - if(!key) - ERRRET(EINVAL, -1); - + FILE *src, *dst; if(!(src = fopen(encrypted, "rb"))) ERROR(1, errno, " Could not open \"%s\" for decryption", , encrypted); @@ -164,6 +163,11 @@ int decryptto(const char * const encrypted, const char * const target, const uns } int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { + if(!src || !dst || !key) ERRRET(EINVAL, -1); + #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 + checksodium(); + #endif + unsigned char buf[CHUNKSIZE], cbuf[CHUNKSIZE + crypto_secretstream_xchacha20poly1305_ABYTES]; unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; crypto_secretstream_xchacha20poly1305_state state; @@ -172,17 +176,6 @@ int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr size_t bytesread; int eof; - #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 - checksodium(); - #endif - - if(!src) - ERRRET(EINVAL, -1); - if(!dst) - ERRRET(EINVAL, -1); - if(!key) - ERRRET(EINVAL, -1); - // Write the header crypto_secretstream_xchacha20poly1305_init_push(&state, header, key); if(fwrite(header, 1, sizeof(header), dst) < sizeof(header)) @@ -206,7 +199,12 @@ int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr return 0; } -int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { +int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { + if(!src || !dst || !key) ERRRET(EINVAL, -1); + #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 + checksodium(); + #endif + unsigned char cbuf[CHUNKSIZE + crypto_secretstream_xchacha20poly1305_ABYTES], buf[CHUNKSIZE]; unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; crypto_secretstream_xchacha20poly1305_state state; @@ -215,17 +213,6 @@ int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr size_t bytesread; int eof; - #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 - checksodium(); - #endif - - if(!src) - ERRRET(EINVAL, -1); - if(!dst) - ERRRET(EINVAL, -1); - if(!key) - ERRRET(EINVAL, -1); - // Read the header if(fread(header, 1, sizeof(header), src) < sizeof(header)) if(ferror(src)) @@ -258,7 +245,8 @@ int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr int genpassword(char **str, unsigned int words) { // Early returns - if(words < 1) {return 0;} + if(words < 1) return 0; + if(!str) ERRRET(EINVAL, -1); #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 checksodium(); #endif diff --git a/src/ll.c b/src/ll.c deleted file mode 100644 index 908f25f..0000000 --- a/src/ll.c +++ /dev/null @@ -1,235 +0,0 @@ -#include "ll.h" -#include "shared.h" - -#include -#include -#include - -typedef struct dln { - void *data; - dll_freecb freecb; - - struct dln *next; - struct dln *prev; - -} dllnode; -typedef struct dlinked { - int size; - dllnode *start; - dllnode *end; - -} dlinkedlist; - -dllnode * dllnode_init(void *data, dll_freecb fcb) { - dllnode *n = VALLOC(1, sizeof(*n)); - if(!n) - return NULL; - - n->data = data; - n->freecb = fcb; - n->prev = NULL; - n->next = NULL; - - return n; -} - -dlinkedlist * dlinkedlist_init(void) { - dlinkedlist *ll = VALLOC(1, sizeof(*ll)); - if(!ll) - return NULL; - - ll->end = NULL; - ll->start = NULL; - ll->size = 0; - - return ll; -} - -void dlinkedlist_free(void *dll) { - if(!dll) - return; - - dlinkedlist *ll = (dlinkedlist *)dll; - for(dllnode *current = ll->start, *next; current != NULL; ) { - next = current->next; - - // This used to be a function but was causing issues for some inscrutable reason - if(current->freecb != NULL) - current->freecb(current->data); - free(current); - - current = next; - } - free(ll); - - return; -} - -int dlinkedlist_size(const dlinkedlist * const ll) { - if(!ll) ERRRET(EINVAL, -1); - return ll->size; -} - -int dlinkedlist_handlefirstnode(dlinkedlist * const ll, void *data, dll_freecb fcb) { - if(!ll) ERRRET(EINVAL, 1); - if(ll->size > 0) - return 1; - - // Need to handle adding the first element differently than other opers, so do it here - dllnode *n = dllnode_init(data, fcb); - if(!n) - return -1; - // dllnode_init already sets n.prev and n.next to null, so no need to do so again - - ll->end = n; - ll->start = n; - ll->size = 1; - - return 0; -} - -int dlinkedlist_xxxend(dlinkedlist * const ll, void *data, dll_freecb fcb, char op) { - if(!ll || (op != 'a' && op != 'p')) ERRRET(EINVAL, 1); - - int handleret; - if((handleret = dlinkedlist_handlefirstnode(ll, data, fcb)) == 0) - return 0; - - dllnode *n = dllnode_init(data, fcb); - if(!n || handleret < 0) - return -1; - - switch (op) { - case 'a': - n->prev = (ll->end); - (ll->end)->next = n; - ll->end = n; - break; - - case 'p': - n->next = (ll->start); - (ll->start)->prev = n; - ll->start = n; - break; - - default: - XALLOC_EXIT(" got an invalid op token when it shouldn't be possible to recieve one", ); - // Technically not an alloc, but also there's no reason I can't reuse a perfectly good macro - } - - ll->size++; - return 0; -} -// TODO: Figure out where the memory leak gcc keeps complaining about is & fix it - - -int dlinkedlist_append(dlinkedlist * const ll, void *data, dll_freecb fcb) { - return dlinkedlist_xxxend(ll, data, fcb, 'a'); -} - -int dlinkedlist_prepend(dlinkedlist * const ll, void *data, dll_freecb fcb) { - return dlinkedlist_xxxend(ll, data, fcb, 'p'); -} - -dllnode * dlinkedlist_getnode(const dlinkedlist * const ll, int index) { - if(!ll) ERRRET(EINVAL, NULL); - if(index < 0 || index >= ll->size) ERRRET(EINVAL, NULL); - - if(index == 0) - return ll->start; - if(index == (ll->size - 1)) - return ll->end; - - int spoint = (index <= (ll->size / 2)); - dllnode *p = (spoint) ? ll->start : ll->end; - int i = (spoint) ? 0 : ll->size - 1; - - // This should (theoretically) be faster on average - for(; p != NULL && ((spoint) ? (i < index) : (i > index)); (spoint) ? i++ : i--, p = (spoint) ? p->next : p->prev); - - return p; -} - -int dlinkedlist_insert(dlinkedlist * const ll, void *data, dll_freecb fcb, int index) { - if(!ll) ERRRET(EINVAL, 1); - if(index < 0 || index >= ll->size) ERRRET(EINVAL, 1); - - // Handle the special cases of appending or prepending - if(index == 0) - return dlinkedlist_prepend(ll, data, fcb); - if(index == (ll->size - 1)) - return dlinkedlist_append(ll, data, fcb); - - // Insert between 2 nodes - // If you're inserting at index 2, the new node becomes index 2, and the previous node at index 2 moves to index 3 - - dllnode *new = dllnode_init(data, fcb); - if(!new) - return -1; - dllnode *current = dlinkedlist_getnode(ll, index); - if(!current) - XALLOC_EXIT(" somehow managed to pull a null node from dlinkedlist_getnode(... , ... , ... , %d)", , (index)); - - (current->prev)->next = new; - new->prev = (current->prev); - new->next = current; - current->prev = new; - - ll->size++; - return 0; -} - -int dlinkedlist_remove(dlinkedlist * const ll, int index) { - if(!ll) ERRRET(EINVAL, 1); - if(index < 0 || index >= ll->size) ERRRET(EINVAL, 2); - - dllnode *current = dlinkedlist_getnode(ll, index); - if(!current) - return -1; - - if(index == 0) { - ll->start = current->next; - ll->start->prev = NULL; - } else if(index == (ll->size - 1)) { - ll->end = current->prev; - ll->end->next = NULL; - } else { - current->prev->next = current->next; - current->next->prev = current->prev; - } - - if(current->freecb != NULL) - current->freecb(current->data); - free(current); - - ll->size--; - - return 0; -} - -void* dlinkedlist_get(const dlinkedlist * const ll, int index) { - dllnode *n = dlinkedlist_getnode(ll, index); - if(!n) - return NULL; - - return n->data; -} - -int dlinkedlist_foreach(dlinkedlist *ll, int (*callback)(void*)) { - if(!ll || callback == NULL) ERRRET(EINVAL, -1); - - for(dllnode *p = ll->start; p != NULL; p = p->next) - callback(p->data); - - return 0; -} - -void * dlinkedlist_poplast(dlinkedlist *ll) { - if(!ll) ERRRET(EINVAL, NULL); - if(dlinkedlist_isempty(ll)) ERRRET(ENODATA, NULL); - - void *data = dlinkedlist_get(ll, ll->size - 1); - dlinkedlist_remove(ll, ll->size - 1); - - return data; -} \ No newline at end of file diff --git a/src/ll.h b/src/ll.h deleted file mode 100644 index f5801e0..0000000 --- a/src/ll.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef __VXGG_REWRITE___LL_H___305861098005___ -#define __VXGG_REWRITE___LL_H___305861098005___ - -// Notice: dlinked functions are no longer necessarily xalloc, but MAY be IF the `___VXGG___USE_XALLOC_FOR_VALLOC___` is greater than 0 - -/* TODO: Implement a way to register a set of alloc functions to a linked list so I can give it arenas for memory allocation -// instead of just xcalloc */ - // I don't know if I care to do this right now. I might be able to hack it in but it would be a fugly hack, something as bad if not worse than that weird callback thing I was doing with the ALWAYS_CHECK_LIBSODIUM macro - -typedef void (*dll_freecb)(void*); -typedef struct dlinked dlinkedlist; - -#ifndef __VXGG_REWRITE___LL_INTERNAL___ - -dlinkedlist * dlinkedlist_init(void); -void dlinkedlist_free(void *dll); -int dlinkedlist_append(dlinkedlist * const ll, void *data, dll_freecb fcb); -int dlinkedlist_prepend(dlinkedlist * const ll, void *data, dll_freecb fcb); -int dlinkedlist_insert(dlinkedlist * const ll, void *data, dll_freecb fcb, int index); -void* dlinkedlist_get(const dlinkedlist * const ll, int index); -int dlinkedlist_remove(dlinkedlist * const ll, int index); -void * dlinkedlist_poplast(dlinkedlist *ll); - -int dlinkedlist_size(const dlinkedlist * const ll); -#define dlinkedlist_isempty(ll) (dlinkedlist_size((ll)) == 0) - -int dlinkedlist_foreach(dlinkedlist *ll, int (*callback)(void*)); - -#endif - -#endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index b0727e2..4b9968c 100755 --- a/src/main.c +++ b/src/main.c @@ -1,8 +1,7 @@ -#include "arena.h" +#include "shared.h" + #include "encryption.h" -#include "ll.h" #include "scanner.h" -#include "shared.h" #include "threadpool.h" #include diff --git a/src/scanner.c b/src/scanner.c index ea35e3c..2ffbea4 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -1,7 +1,6 @@ #define _GNU_SOURCE #include "shared.h" -#include "ll.h" #include "scanner.h" #include @@ -10,27 +9,29 @@ #include #include -dlinkedlist * scandirlist(const char * const dir, int (*selector)(const struct dirent *), int (*cmp)(const struct dirent **, const struct dirent **)) { - if(!dir || selector == NULL || cmp == NULL) ERRRET(EINVAL, NULL); +// dlinkedlist * scandirlist(const char * const dir, int (*selector)(const struct dirent *), int (*cmp)(const struct dirent **, const struct dirent **)) { +// if(!dir || selector == NULL || cmp == NULL) ERRRET(EINVAL, NULL); - struct dirent **namelist = NULL; - dlinkedlist *list = NULL; - int numentries = -1; - - if((numentries = scandir(dir, &namelist, selector, cmp)) < 0) - ERRRET(errno, NULL); - - list = dlinkedlist_init(); - for(int i = 0; i < numentries; i++) - if(dlinkedlist_append(list, (void *)(namelist[i]), free) < 0) { - dlinkedlist_free(list); - for(int j = i; j < numentries; j++) - free(namelist[j]); - - free(namelist); - ERRRET(errno, NULL); - } - free(namelist); - - return list; -} +// struct dirent **namelist = NULL; +// dlinkedlist *list = NULL; +// int numentries = -1; + +// if((numentries = scandir(dir, &namelist, selector, cmp)) < 0) +// ERRRET(errno, NULL); + +// list = dlinkedlist_init(); +// for(int i = 0; i < numentries; i++) +// if(dlinkedlist_append(list, (void *)(namelist[i]), free) < 0) { +// dlinkedlist_free(list); +// for(int j = i; j < numentries; j++) +// free(namelist[j]); + +// free(namelist); +// ERRRET(errno, NULL); +// } +// free(namelist); + +// return list; +// } + +// TODO: Rewrite this to use the threadpool. Each newly scanned file should be pushed onto the threadpool as an encryption task \ No newline at end of file diff --git a/src/scanner.h b/src/scanner.h index f42d9b9..b9f2f9a 100644 --- a/src/scanner.h +++ b/src/scanner.h @@ -1,9 +1,5 @@ #ifndef __VXGG_REWRITE___SCANNER_H___7164133769617___ #define __VXGG_REWRITE___SCANNER_H___7164133769617___ -#include "ll.h" -#include - -dlinkedlist * scandirlist(const char * const dir, int (*selector)(const struct dirent *), int (*cmp)(const struct dirent **, const struct dirent **)); #endif \ No newline at end of file diff --git a/src/shared.c b/src/shared.c index e83261c..c02414b 100644 --- a/src/shared.c +++ b/src/shared.c @@ -6,55 +6,6 @@ #include #include -enum XALLOC_TYPE { - XALLOC_INVAL, // Default when unset - - XALLOC_MALLOC, - XALLOC_CALLOC, - XALLOC_REALLOC, - - XALLOC_2BIG // Out of range -}; - -void * xalloc(size_t nmemb, size_t size, enum XALLOC_TYPE actype, void *ptr) { - if(actype <= XALLOC_INVAL || actype >= XALLOC_2BIG) ERRRET(EINVAL, NULL); - - void *mem = NULL; - switch(actype) { - case XALLOC_MALLOC: - mem = malloc(nmemb * size); - break; - - case XALLOC_CALLOC: - mem = calloc(nmemb, size); - break; - - case XALLOC_REALLOC: - mem = realloc(ptr, nmemb * size); - break; - - default: - XALLOC_EXIT(" An unknown alloc type was passed, which shouldn't be possible", ); - } - - if(!mem) - XALLOC_EXIT(" Could not allocate memory", ); - - return mem; -} - -void * xmalloc(size_t size) { - return xalloc(size, 1, XALLOC_MALLOC, NULL); -} - -void * xcalloc(size_t nmemb, size_t size) { - return xalloc(nmemb, size, XALLOC_CALLOC, NULL); -} - -void * xreallocarray(void *ptr, size_t nmemb, size_t size) { - return xalloc(nmemb, size, XALLOC_REALLOC, ptr); -} - int rwbuf(char **str, unsigned long int initsize, int fd) { // Try to read bytes from fd into str // Bytes read == 0, return 0 @@ -67,7 +18,10 @@ int rwbuf(char **str, unsigned long int initsize, int fd) { ssize_t bytesread = -1; int csize = initsize, ccap = initsize; - lstr = xcalloc(initsize, sizeof(char)); + lstr = calloc(initsize, sizeof(char)); + if(!lstr) + return -1; + while((bytesread = read(fd, lstr + (csize - ccap), ccap)) > 0) { ccap -= bytesread; if(ccap <= 0) { @@ -138,12 +92,14 @@ int wwbuf(int fd, const unsigned char *buf, int len) { // Thanks Beej! // dirname but less retarded hopefully -char * xdirname(const char * const path) { +char * vxdirname(const char * const path) { char *tmp = NULL; - if(!path) { // Path being null is a special case which should return super early, before anything else + if(!path) { // Path being null is a special case which should return early, before anything else (as to avoid null dereference) tmp = strdup("."); - if(!tmp) - XALLOC_EXIT(" could not strdup \".\" for set path result \"NULL\"", ); + if(!tmp) { + WARN(errno, " could not strdup \".\" for set path result \"NULL\"", ); + return NULL; + } return tmp; } @@ -154,9 +110,11 @@ char * xdirname(const char * const path) { if(strcmp(path, "..") == 0 && !flag) {tmp = strdup("."); flag++;} if(flag) { - if(!tmp) - XALLOC_EXIT(" could not strdup a set path result", ); - + if(!tmp) { + WARN(errno, " could not strdup a set path result", ); + return NULL; + } + return tmp; } @@ -173,8 +131,10 @@ char * xdirname(const char * const path) { // Get a temp copy of the path for manipulation purposes tmp = strdup(path); - if(!tmp) - XALLOC_EXIT(" could not strdup the given path \"%s\" for internal manipulation", , path); + if(!tmp) { + WARN(errno, " could not strdup the given path \"%s\" for internal manipulation", , path); + return NULL; + } // If there's a trailing '/', delete it size_t pathlen = strlen(path); @@ -193,21 +153,24 @@ char * xdirname(const char * const path) { count++; } - if(count == 0) { + if(count == 0 || count == 1) free(tmp); + if(count == 0) { tmp = strdup("."); - if(!tmp) - XALLOC_EXIT(" could not strdup \".\" for set path result", ); + if(!tmp) { + WARN(errno, " could not strdup \".\" for set path result", ); + return NULL; + } return tmp; - } - if(count == 1) { - free(tmp); + } else if(count == 1) { tmp = strdup("/"); - if(!tmp) - XALLOC_EXIT(" could not strdup \"/\" for set path result", ); - + if(!tmp) { + WARN(errno, " could not strdup \"/\" for set path result", ); + return NULL; + } return tmp; } + // This is retarded, fix it for(size_t i = 0, c2 = 0; i < pathlen; i++) { if(tmp[i] == '/') @@ -217,9 +180,11 @@ char * xdirname(const char * const path) { } char * const actual = strdup(tmp); - if(!actual) - XALLOC_EXIT(" could not strdup tmp string to make a shorter end string", ); free(tmp); + if(!actual) { + WARN(errno, " could not strdup tmp string to make a shorter end string", ); + return NULL; + } return actual; } diff --git a/src/shared.h b/src/shared.h index 0b401fe..efb95ad 100644 --- a/src/shared.h +++ b/src/shared.h @@ -32,26 +32,19 @@ typedef void (*fcallback)(void*); // free()-like callback signature exit(EXIT_FAILURE); /* Makes gcc happy */\ } while (0) -// Whether to use `XALLOC` or normal `malloc` functions for acquiring new memory via the `VALLOC()` macro. `> 0` -// enables XALLOC functionality -#define ___VXGG___USE_XALLOC_FOR_VALLOC___ 0 -#define VALLOC(nmemb, size) ((___VXGG___USE_XALLOC_FOR_VALLOC___ > 0) ? xmalloc((nmemb) * (size)) : malloc((nmemb) * (size))) + +#define VALLOC(nmemb, size) malloc((nmemb) * (size)) // Error macro that gcc will not complain about #define ERROR(status, errnum, format, ...) do {error((status), (errnum), (format)__VA_ARGS__); exit((status));} while (0) +#define WARN(errnum, format, ...) do {error(0, (errnum), (format)__VA_ARGS__);} while (0) -// `malloc()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` -void * xmalloc(size_t size); -// `calloc()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` -void * xcalloc(size_t nmemb, size_t size); -// `reallocarray()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` -void * xreallocarray(void *ptr, size_t nmemb, size_t size); // Read the entire contents of a file descriptor into a malloc()'ed buffer int rwbuf(char **str, unsigned long int initsize, int fd); // Write the entire contents of a buffer into a file descriptor int wwbuf(int fd, const unsigned char *buf, int len); -// `dirname()` reimplementation that returns a malloc()'ed string. According to the `x___` naming scheme, exits/aborts on alloc error. -char * xdirname(const char * const path); +// `dirname()` reimplementation that returns a malloc()'ed string +char * vxdirname(const char * const path); @@ -87,13 +80,4 @@ cleanup_init(&__CLEANUP, __CLEANUP_FUNCS, __CLEANUP_ARGS, (size)) #define cleanup_ERRORFLAGGED (__FLAG != 0) #define cleanup_CNDEXEC(code) while(!cleanup_ERRORFLAGGED) {code; break;} - - -// Generic task to be executed by a consumer -typedef struct task task; -// A queue of tasks -typedef struct taskqueue taskqueue; -// A concurrent queue of tasks, basically a threadpool tasks can be dispatched to -typedef struct ctqueue ctqueue; - #endif \ No newline at end of file -- cgit v1.2.3