diff options
| -rw-r--r-- | src/Makefile | 6 | ||||
| -rw-r--r-- | src/arena.c | 183 | ||||
| -rw-r--r-- | src/arena.h | 23 | ||||
| -rw-r--r-- | src/encryption.c | 40 | ||||
| -rw-r--r-- | src/encryption.h | 7 | ||||
| -rw-r--r-- | src/ll.c | 2 | ||||
| -rw-r--r-- | src/shared.c | 115 | ||||
| -rw-r--r-- | src/shared.h | 13 |
8 files changed, 264 insertions, 125 deletions
diff --git a/src/Makefile b/src/Makefile index bcb4670..4a0d316 100644 --- a/src/Makefile +++ b/src/Makefile | |||
| @@ -22,10 +22,12 @@ all: main | |||
| 22 | 22 | ||
| 23 | main: main.o shared.o | 23 | main: main.o shared.o |
| 24 | 24 | ||
| 25 | main.o: main.c shared.h | 25 | ll.o: ll.c ll.h |
| 26 | main.o: main.c | ||
| 27 | arena.o: arena.c arena.h | ||
| 26 | shared.o: shared.c shared.h | 28 | shared.o: shared.c shared.h |
| 27 | 29 | ||
| 28 | encryption: encryption.c encryption.h shared.o shared.h | 30 | encryption: encryption.c encryption.h shared.o shared.h |
| 29 | 31 | ||
| 30 | c clean: | 32 | c clean: |
| 31 | rm -rvf $(BINARIES) $(wildcard *.o) $(wildcard *.test*) \ No newline at end of file | 33 | rm -rvf $(BINARIES) $(wildcard *.o) $(wildcard *.test*) $(wildcard *.enc) \ No newline at end of file |
diff --git a/src/arena.c b/src/arena.c new file mode 100644 index 0000000..d340f3a --- /dev/null +++ b/src/arena.c | |||
| @@ -0,0 +1,183 @@ | |||
| 1 | #include "arena.h" | ||
| 2 | #include "shared.h" | ||
| 3 | |||
| 4 | #include <stddef.h> | ||
| 5 | #include <stdint.h> | ||
| 6 | #include <stdlib.h> | ||
| 7 | #include <errno.h> | ||
| 8 | #include <error.h> | ||
| 9 | |||
| 10 | typedef struct an { | ||
| 11 | void *membase; | ||
| 12 | void *memcur; | ||
| 13 | size_t allocated; | ||
| 14 | size_t used; | ||
| 15 | |||
| 16 | struct an *next; | ||
| 17 | } arenanode; | ||
| 18 | |||
| 19 | typedef struct arena { | ||
| 20 | arenanode *start; | ||
| 21 | arenanode *current; | ||
| 22 | |||
| 23 | // For keeping track of the largest possible thing that can be allocated to one node | ||
| 24 | size_t node_memspace; | ||
| 25 | } arena; | ||
| 26 | |||
| 27 | int arenanode_init(arenanode **an, size_t bytes) { | ||
| 28 | (*an) = xcalloc(1, sizeof(**an)); | ||
| 29 | if(!(*an)) | ||
| 30 | return -1; | ||
| 31 | |||
| 32 | (*an)->allocated = bytes; | ||
| 33 | (*an)->used = 0; | ||
| 34 | (*an)->next = NULL; | ||
| 35 | |||
| 36 | void *mem = xcalloc(bytes, 1); | ||
| 37 | if(!mem) { | ||
| 38 | free((*an)); | ||
| 39 | (*an) = NULL; | ||
| 40 | return -1; | ||
| 41 | } | ||
| 42 | |||
| 43 | (*an)->membase = mem; | ||
| 44 | (*an)->memcur = mem; | ||
| 45 | |||
| 46 | return 0; | ||
| 47 | } | ||
| 48 | |||
| 49 | int arena_init(arena **a, size_t bytes) { | ||
| 50 | if(!ISPOWOF2(MEM_ALIGN_BYTES)) | ||
| 51 | XALLOC_EXIT("<arena_init> \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to create a new arena"); | ||
| 52 | |||
| 53 | (*a) = xcalloc(1, sizeof(**a)); | ||
| 54 | if(!(*a)) | ||
| 55 | return -1; | ||
| 56 | (*a)->start = NULL; | ||
| 57 | (*a)->current = NULL; | ||
| 58 | (*a)->node_memspace = bytes; | ||
| 59 | |||
| 60 | arenanode *base; | ||
| 61 | if(arenanode_init(&base, bytes) < 0) { | ||
| 62 | free(*a); | ||
| 63 | (*a) = NULL; | ||
| 64 | return -1; | ||
| 65 | } | ||
| 66 | |||
| 67 | (*a)->start = base; | ||
| 68 | (*a)->current = base; | ||
| 69 | |||
| 70 | return 0; | ||
| 71 | } | ||
| 72 | |||
| 73 | void* arena_alloc(arena * const arena, size_t bytes) { | ||
| 74 | if(!arena) { | ||
| 75 | errno = EINVAL; | ||
| 76 | return NULL; | ||
| 77 | } | ||
| 78 | if(bytes > arena->node_memspace) { | ||
| 79 | errno = ENOMEM; | ||
| 80 | return NULL; | ||
| 81 | } | ||
| 82 | if(!ISPOWOF2(MEM_ALIGN_BYTES)) | ||
| 83 | XALLOC_EXIT("<arena_alloc> \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to allocate any memory"); | ||
| 84 | |||
| 85 | if(bytes > ((arena->current)->allocated - (arena->current)->used)) { | ||
| 86 | arenanode *new; | ||
| 87 | if(arenanode_init(&new, arena->node_memspace) < 0) | ||
| 88 | return NULL; | ||
| 89 | |||
| 90 | arena->current->next = new; | ||
| 91 | arena->current = new; | ||
| 92 | } | ||
| 93 | |||
| 94 | size_t alignment = MEM_ALIGN(bytes); | ||
| 95 | size_t offset = bytes + alignment; | ||
| 96 | |||
| 97 | void *mem = arena->current->memcur; | ||
| 98 | arena->current->memcur = (void*)(((uint8_t*)arena->current->memcur) + offset); | ||
| 99 | |||
| 100 | arena->current->used += offset; | ||
| 101 | |||
| 102 | return mem; | ||
| 103 | |||
| 104 | // Note: This implementation assumes that malloc provides already-aligned memory. If it | ||
| 105 | // doesn't, that sucks and blows everything up | ||
| 106 | } | ||
| 107 | |||
| 108 | int arena_free(arena **arena) { | ||
| 109 | if(!arena) { | ||
| 110 | errno = EINVAL; | ||
| 111 | return -1; | ||
| 112 | } | ||
| 113 | if(!(*arena)) { | ||
| 114 | errno = EINVAL; | ||
| 115 | return -1; | ||
| 116 | } | ||
| 117 | |||
| 118 | (*arena)->current = (*arena)->start; | ||
| 119 | for(arenanode *n; (*arena)->current != NULL;) { | ||
| 120 | n = (*arena)->current->next; | ||
| 121 | |||
| 122 | free((*arena)->current->membase); | ||
| 123 | free((*arena)->current); | ||
| 124 | |||
| 125 | (*arena)->current = n; | ||
| 126 | } | ||
| 127 | free((*arena)); | ||
| 128 | (*arena) = NULL; | ||
| 129 | |||
| 130 | return 0; | ||
| 131 | } | ||
| 132 | |||
| 133 | int arena_clear(arena **arena) { | ||
| 134 | if(!arena) { | ||
| 135 | errno = EINVAL; | ||
| 136 | return -1; | ||
| 137 | } | ||
| 138 | if(!(*arena)) { | ||
| 139 | errno = EINVAL; | ||
| 140 | return -1; | ||
| 141 | } | ||
| 142 | |||
| 143 | size_t bytes = (*arena)->node_memspace; | ||
| 144 | |||
| 145 | int ret = 0; | ||
| 146 | if((ret = arena_free(arena)) < 0) | ||
| 147 | return ret; | ||
| 148 | return arena_init(arena, bytes); | ||
| 149 | } | ||
| 150 | |||
| 151 | |||
| 152 | |||
| 153 | // Simple Arena is an arena that can't expand whenever allocating memory, meaning what you originally allocated is what you get | ||
| 154 | |||
| 155 | typedef arena simplearena; | ||
| 156 | |||
| 157 | int simplearena_init(simplearena **a, size_t bytes) { | ||
| 158 | return arena_init(a, bytes); | ||
| 159 | } | ||
| 160 | |||
| 161 | void* simplearena_alloc(simplearena * const a, size_t bytes) { | ||
| 162 | // The criteria to allocate new memory in arena_alloc is 'bytes > ((a->current)->allocated - (a->current)->used)', so if this | ||
| 163 | // is true, just return NULL & set errno | ||
| 164 | |||
| 165 | if(!a) { | ||
| 166 | errno = EINVAL; | ||
| 167 | return NULL; | ||
| 168 | } | ||
| 169 | if(bytes > ((a->current)->allocated - (a->current)->used)) { | ||
| 170 | errno = ENOMEM; | ||
| 171 | return NULL; | ||
| 172 | } | ||
| 173 | |||
| 174 | return arena_alloc(a, bytes); | ||
| 175 | } | ||
| 176 | |||
| 177 | int simplearena_free(simplearena **a) { | ||
| 178 | return arena_free(a); | ||
| 179 | } | ||
| 180 | |||
| 181 | int simplearena_clear(simplearena **a) { | ||
| 182 | return arena_clear(a); | ||
| 183 | } \ No newline at end of file | ||
diff --git a/src/arena.h b/src/arena.h new file mode 100644 index 0000000..23dc664 --- /dev/null +++ b/src/arena.h | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | #ifndef __VXGG_REWRITE___ARENA_H___25077218513438___ | ||
| 2 | #define __VXGG_REWRITE___ARENA_H___25077218513438___ | ||
| 3 | |||
| 4 | #include <stddef.h> | ||
| 5 | |||
| 6 | typedef struct arena arena; | ||
| 7 | typedef arena simplearena; | ||
| 8 | |||
| 9 | #define ISPOWOF2(x) (((x) & ((x) - 1)) == 0) | ||
| 10 | const size_t MEM_ALIGN_BYTES = (2 * sizeof(void*)); | ||
| 11 | #define MEM_ALIGN(x) ((x) + (((x) & (MEM_ALIGN_BYTES - 1)) != 0) * (MEM_ALIGN_BYTES - ((x) & (MEM_ALIGN_BYTES - 1)))) | ||
| 12 | |||
| 13 | int arena_init(arena **a, size_t bytes); | ||
| 14 | void* arena_alloc(arena * const arena, size_t bytes); | ||
| 15 | int arena_free(arena **arena); | ||
| 16 | int arena_clear(arena **arena); | ||
| 17 | |||
| 18 | int simplearena_init(simplearena **a, size_t bytes); | ||
| 19 | void* simplearena_alloc(simplearena * const a, size_t bytes); | ||
| 20 | int simplearena_free(simplearena **a); | ||
| 21 | int simplearena_free(simplearena **a); | ||
| 22 | |||
| 23 | #endif \ No newline at end of file | ||
diff --git a/src/encryption.c b/src/encryption.c index 8b0ac83..9b8715e 100644 --- a/src/encryption.c +++ b/src/encryption.c | |||
| @@ -19,11 +19,9 @@ | |||
| 19 | #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | 19 | #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 |
| 20 | void naclfaildefault(void *none) { | 20 | void naclfaildefault(void *none) { |
| 21 | none = none; // Makes gcc happy | 21 | none = none; // Makes gcc happy |
| 22 | #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 | 22 | if(___VXGG___VERBOSE_ERRORS___) |
| 23 | error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting..."); | 23 | error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting..."); |
| 24 | #else | 24 | exit(EXIT_FAILURE); |
| 25 | exit(EXIT_FAILURE); | ||
| 26 | #endif | ||
| 27 | } | 25 | } |
| 28 | 26 | ||
| 29 | int checksodiumcb(const vxgg_naclfailcb callback, void *data) { | 27 | int checksodiumcb(const vxgg_naclfailcb callback, void *data) { |
| @@ -49,15 +47,14 @@ void vxgg_setsodiumfailcb(vxgg_naclfailcb cb, void *data) { | |||
| 49 | #endif | 47 | #endif |
| 50 | 48 | ||
| 51 | void checksodium(void) { | 49 | void checksodium(void) { |
| 52 | #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | 50 | #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 |
| 53 | checksodiumcb(NULL, NULL); | 51 | checksodiumcb(NULL, NULL); |
| 54 | #else | 52 | #else |
| 55 | if(sodium_init() < 0) | 53 | if(sodium_init() < 0) { |
| 56 | #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 | 54 | if(___VXGG___VERBOSE_ERRORS___) |
| 57 | error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting..."); | 55 | error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting..."); |
| 58 | #else | 56 | exit(EXIT_FAILURE); |
| 59 | exit(EXIT_FAILURE); | 57 | } |
| 60 | #endif | ||
| 61 | #endif | 58 | #endif |
| 62 | 59 | ||
| 63 | return; | 60 | return; |
| @@ -84,7 +81,7 @@ int maketmp(const char * const dest) { | |||
| 84 | } | 81 | } |
| 85 | 82 | ||
| 86 | int encrypttotmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { | 83 | int encrypttotmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { |
| 87 | #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | 84 | #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 |
| 88 | checksodium(); | 85 | checksodium(); |
| 89 | #endif | 86 | #endif |
| 90 | 87 | ||
| @@ -176,7 +173,7 @@ int genpassword(char **str, unsigned int words) { | |||
| 176 | // Early returns | 173 | // Early returns |
| 177 | if(words < 1) | 174 | if(words < 1) |
| 178 | return 0; | 175 | return 0; |
| 179 | #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | 176 | #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 |
| 180 | checksodium(); | 177 | checksodium(); |
| 181 | #endif | 178 | #endif |
| 182 | 179 | ||
| @@ -203,22 +200,13 @@ int genpassword(char **str, unsigned int words) { | |||
| 203 | 200 | ||
| 204 | // sodium_malloc wrapper. Calls `error()` or `abort()` depnding on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___`. Will make sure libsodium is initialized if `___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0` | 201 | // sodium_malloc wrapper. Calls `error()` or `abort()` depnding on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___`. Will make sure libsodium is initialized if `___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0` |
| 205 | void* xsodium_malloc(size_t size) { | 202 | void* xsodium_malloc(size_t size) { |
| 206 | #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | 203 | #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 |
| 207 | checksodium(); | 204 | checksodium(); |
| 208 | #endif | 205 | #endif |
| 209 | 206 | ||
| 210 | void *mem = sodium_malloc(size); | 207 | void *mem = sodium_malloc(size); |
| 211 | if(mem == NULL) { | 208 | if(mem == NULL) |
| 212 | #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 | 209 | XALLOC_EXIT("<xsodium_malloc> could not allocate memory... Quitting"); |
| 213 | #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 | ||
| 214 | error(1, errno, "<xsodium_malloc> could not allocate memory... Quitting"); | ||
| 215 | #else | ||
| 216 | exit(EXIT_FAILURE); | ||
| 217 | #endif | ||
| 218 | #endif | ||
| 219 | |||
| 220 | abort(); | ||
| 221 | } | ||
| 222 | 210 | ||
| 223 | return mem; | 211 | return mem; |
| 224 | } | 212 | } |
diff --git a/src/encryption.h b/src/encryption.h index 1c38141..418e7f6 100644 --- a/src/encryption.h +++ b/src/encryption.h | |||
| @@ -8,7 +8,8 @@ | |||
| 8 | 8 | ||
| 9 | #define CHUNK_SIZE ((int)(1 << 12)) | 9 | #define CHUNK_SIZE ((int)(1 << 12)) |
| 10 | 10 | ||
| 11 | #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | 11 | // TODO: What the fuck was I thinking when I did any of this callback shit? Make this a different macro and decouple it from ALWAYS_CHECK |
| 12 | #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | ||
| 12 | 13 | ||
| 13 | // Definition for the callback function that fires when a call to checksodium fails | 14 | // Definition for the callback function that fires when a call to checksodium fails |
| 14 | typedef void (*vxgg_naclfailcb)(void*); | 15 | typedef void (*vxgg_naclfailcb)(void*); |
| @@ -18,7 +19,7 @@ void vxgg_setsodiumfailcb(const vxgg_naclfailcb cb, void *data); | |||
| 18 | 19 | ||
| 19 | #endif | 20 | #endif |
| 20 | 21 | ||
| 21 | // I need to store a dictionary of valid words for generating a password, and I don't want to read it in from another file, so I'm experimenting with this | 22 | // Fuck reading from a file. Even if someone ran strings on the binary and got this they wouldn't be able to regenerate the key |
| 22 | #define PASSWORD_WORDS (\ | 23 | #define PASSWORD_WORDS (\ |
| 23 | (const char *[]){\ | 24 | (const char *[]){\ |
| 24 | "the", "of", "to", "and", "for", "our", "their", "has", "in", "he", "a", "them", "that", "these", "by", "have", "we", "us",\ | 25 | "the", "of", "to", "and", "for", "our", "their", "has", "in", "he", "a", "them", "that", "these", "by", "have", "we", "us",\ |
| @@ -73,7 +74,7 @@ void vxgg_setsodiumfailcb(const vxgg_naclfailcb cb, void *data); | |||
| 73 | "reliance", "divine", "providence", "mutually", "pledge", "each", "fortunes", "sacred", "honor"\ | 74 | "reliance", "divine", "providence", "mutually", "pledge", "each", "fortunes", "sacred", "honor"\ |
| 74 | }\ | 75 | }\ |
| 75 | ) | 76 | ) |
| 76 | #define PASSWORD_WORDS_LEN (STATICARR_SIZE(PASSWORD_WORDS)) | 77 | #define PASSWORD_WORDS_LEN (STATIC_ARRAY_LEN(PASSWORD_WORDS)) |
| 77 | 78 | ||
| 78 | // Checks if sodium is initialized. Initializes it if not. If `___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0`, it's possible to set an error callback to avoid exiting the entire program. Otherwise calls `error()` if libsodium can't initialize | 79 | // Checks if sodium is initialized. Initializes it if not. If `___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0`, it's possible to set an error callback to avoid exiting the entire program. Otherwise calls `error()` if libsodium can't initialize |
| 79 | void checksodium(void); | 80 | void checksodium(void); |
| @@ -1,6 +1,8 @@ | |||
| 1 | #include "ll.h" | 1 | #include "ll.h" |
| 2 | #include "shared.h" | ||
| 2 | 3 | ||
| 3 | #include <stddef.h> | 4 | #include <stddef.h> |
| 5 | #include <stdlib.h> | ||
| 4 | 6 | ||
| 5 | void dlinkedlist_init(dlinkedlist **ll) { | 7 | void dlinkedlist_init(dlinkedlist **ll) { |
| 6 | (*ll) = xcalloc(1, sizeof(**ll)); | 8 | (*ll) = xcalloc(1, sizeof(**ll)); |
diff --git a/src/shared.c b/src/shared.c index d67e362..72ede56 100644 --- a/src/shared.c +++ b/src/shared.c | |||
| @@ -1,45 +1,23 @@ | |||
| 1 | #include "shared.h" | 1 | #include "shared.h" |
| 2 | 2 | ||
| 3 | #include <stdarg.h> | ||
| 4 | #include <stdlib.h> | 3 | #include <stdlib.h> |
| 5 | #include <string.h> | 4 | #include <string.h> |
| 6 | #include <unistd.h> | 5 | #include <unistd.h> |
| 7 | #include <errno.h> | 6 | #include <errno.h> |
| 8 | #include <error.h> | 7 | #include <error.h> |
| 9 | #include <stdio.h> | ||
| 10 | 8 | ||
| 11 | void* xcalloc(size_t nmemb, size_t size) { | 9 | void* xcalloc(size_t nmemb, size_t size) { |
| 12 | void *mem = calloc(nmemb, size); | 10 | void *mem = calloc(nmemb, size); |
| 13 | 11 | if(!mem) | |
| 14 | if(!mem) { | 12 | XALLOC_EXIT("<xcalloc> Could not allocate memory"); |
| 15 | #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 | ||
| 16 | #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 | ||
| 17 | error(1, errno, "<xcalloc> Could not allocate memory"); | ||
| 18 | #else | ||
| 19 | exit(EXIT_FAILURE); | ||
| 20 | #endif | ||
| 21 | #endif | ||
| 22 | |||
| 23 | abort(); | ||
| 24 | } | ||
| 25 | 13 | ||
| 26 | |||
| 27 | return mem; | 14 | return mem; |
| 28 | } | 15 | } |
| 29 | 16 | ||
| 30 | void* xreallocarray(void *ptr, size_t nmemb, size_t size) { | 17 | void* xreallocarray(void *ptr, size_t nmemb, size_t size) { |
| 31 | void *mem = reallocarray(ptr, nmemb, size); | 18 | void *mem = reallocarray(ptr, nmemb, size); |
| 32 | if(mem == NULL) { | 19 | if(mem == NULL) |
| 33 | #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 | 20 | XALLOC_EXIT("<xreallocarray> Could not allocate memory"); |
| 34 | #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 | ||
| 35 | error(1, errno, "<xreallocarray> Could not allocate memory"); | ||
| 36 | #else | ||
| 37 | exit(EXIT_FAILURE); | ||
| 38 | #endif | ||
| 39 | #endif | ||
| 40 | |||
| 41 | abort(); | ||
| 42 | } | ||
| 43 | 21 | ||
| 44 | return mem; | 22 | return mem; |
| 45 | } | 23 | } |
| @@ -63,9 +41,9 @@ int readwholebuffer(char **str, unsigned long int initsize, int fd) { | |||
| 63 | 41 | ||
| 64 | tmp = realloc(lstr, csize * sizeof(char)); | 42 | tmp = realloc(lstr, csize * sizeof(char)); |
| 65 | if(!tmp) { | 43 | if(!tmp) { |
| 66 | #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 | 44 | if(___VXGG___VERBOSE_ERRORS___) |
| 67 | error(0, errno, "Could not reallocate enough space for lstr"); | 45 | error(0, errno, "Could not reallocate enough space for lstr"); |
| 68 | #endif | 46 | |
| 69 | free(lstr); | 47 | free(lstr); |
| 70 | lstr = NULL; // Need to set this because of the break | 48 | lstr = NULL; // Need to set this because of the break |
| 71 | bytesread = -100; | 49 | bytesread = -100; |
| @@ -75,9 +53,9 @@ int readwholebuffer(char **str, unsigned long int initsize, int fd) { | |||
| 75 | } | 53 | } |
| 76 | } | 54 | } |
| 77 | if(bytesread < 0 && bytesread != -100) { | 55 | if(bytesread < 0 && bytesread != -100) { |
| 78 | #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 | 56 | if(___VXGG___VERBOSE_ERRORS___) |
| 79 | error(0, errno, "Ran into a read() error"); | 57 | error(0, errno, "Ran into a read() error"); |
| 80 | #endif | 58 | |
| 81 | free(lstr); | 59 | free(lstr); |
| 82 | lstr = NULL; | 60 | lstr = NULL; |
| 83 | } | 61 | } |
| @@ -85,9 +63,9 @@ int readwholebuffer(char **str, unsigned long int initsize, int fd) { | |||
| 85 | if(lstr) { | 63 | if(lstr) { |
| 86 | tmp = realloc(lstr, csize - ccap + 1); | 64 | tmp = realloc(lstr, csize - ccap + 1); |
| 87 | if(!tmp) { | 65 | if(!tmp) { |
| 88 | #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 | 66 | if(___VXGG___VERBOSE_ERRORS___) |
| 89 | error(0, errno, "Could not shrink lstr after reading buffer"); | 67 | error(0, errno, "Could not shrink lstr after reading buffer"); |
| 90 | #endif | 68 | |
| 91 | free(lstr); | 69 | free(lstr); |
| 92 | bytesread = -100; | 70 | bytesread = -100; |
| 93 | } | 71 | } |
| @@ -127,16 +105,8 @@ char *xdirname(const char * const path) { | |||
| 127 | char *tmp = NULL; | 105 | char *tmp = NULL; |
| 128 | if(!path) { // Path being null is a special case which should return super early, before anything else | 106 | if(!path) { // Path being null is a special case which should return super early, before anything else |
| 129 | tmp = strdup("."); | 107 | tmp = strdup("."); |
| 130 | if(!tmp) { | 108 | if(!tmp) |
| 131 | #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 | 109 | XALLOC_EXIT("<xdirname> could not strdup \".\" for set path result \"NULL\""); |
| 132 | #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 | ||
| 133 | error(1, errno, "<xdirname> could not strdup \".\" for set path result \"NULL\""); | ||
| 134 | #else | ||
| 135 | exit(EXIT_FAILURE); | ||
| 136 | #endif | ||
| 137 | #endif | ||
| 138 | abort(); | ||
| 139 | } | ||
| 140 | 110 | ||
| 141 | return tmp; | 111 | return tmp; |
| 142 | } | 112 | } |
| @@ -147,16 +117,9 @@ char *xdirname(const char * const path) { | |||
| 147 | if(strcmp(path, "..") == 0 && !flag) {tmp = strdup("."); flag++;} | 117 | if(strcmp(path, "..") == 0 && !flag) {tmp = strdup("."); flag++;} |
| 148 | 118 | ||
| 149 | if(flag) { | 119 | if(flag) { |
| 150 | if(!tmp) { | 120 | if(!tmp) |
| 151 | #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 | 121 | XALLOC_EXIT("<xdirname> could not strdup a set path result"); |
| 152 | #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 | 122 | |
| 153 | error(1, errno, "<xdirname> could not strdup a set path result"); | ||
| 154 | #else | ||
| 155 | exit(EXIT_FAILURE); | ||
| 156 | #endif | ||
| 157 | #endif | ||
| 158 | abort(); | ||
| 159 | } | ||
| 160 | return tmp; | 123 | return tmp; |
| 161 | } | 124 | } |
| 162 | 125 | ||
| @@ -173,16 +136,8 @@ char *xdirname(const char * const path) { | |||
| 173 | 136 | ||
| 174 | // Get a temp copy of the path for manipulation purposes | 137 | // Get a temp copy of the path for manipulation purposes |
| 175 | tmp = strdup(path); | 138 | tmp = strdup(path); |
| 176 | if(!tmp) { | 139 | if(!tmp) |
| 177 | #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 | 140 | XALLOC_EXIT("<xdirname> could not strdup the given path \"%s\" for internal manipulation", , path); |
| 178 | #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 | ||
| 179 | error(1, errno, "<xdirname> could not strdup the given path \"%s\" for internal manipulation", path); | ||
| 180 | #else | ||
| 181 | exit(EXIT_FAILURE); | ||
| 182 | #endif | ||
| 183 | #endif | ||
| 184 | abort(); | ||
| 185 | } | ||
| 186 | 141 | ||
| 187 | // If there's a trailing '/', delete it | 142 | // If there's a trailing '/', delete it |
| 188 | size_t pathlen = strlen(path); | 143 | size_t pathlen = strlen(path); |
| @@ -204,31 +159,15 @@ char *xdirname(const char * const path) { | |||
| 204 | if(count == 0) { | 159 | if(count == 0) { |
| 205 | free(tmp); | 160 | free(tmp); |
| 206 | tmp = strdup("."); | 161 | tmp = strdup("."); |
| 207 | if(!tmp) { | 162 | if(!tmp) |
| 208 | #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 | 163 | XALLOC_EXIT("<xdirname> could not strdup \".\" for set path result"); |
| 209 | #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 | ||
| 210 | error(1, errno, "<xdirname> could not strdup \".\" for set path result"); | ||
| 211 | #else | ||
| 212 | exit(EXIT_FAILURE); | ||
| 213 | #endif | ||
| 214 | #endif | ||
| 215 | abort(); | ||
| 216 | } | ||
| 217 | return tmp; | 164 | return tmp; |
| 218 | } | 165 | } |
| 219 | if(count == 1) { | 166 | if(count == 1) { |
| 220 | free(tmp); | 167 | free(tmp); |
| 221 | tmp = strdup("/"); | 168 | tmp = strdup("/"); |
| 222 | if(!tmp) { | 169 | if(!tmp) |
| 223 | #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 | 170 | XALLOC_EXIT("<xdirname> could not strdup \"/\" for set path result"); |
| 224 | #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 | ||
| 225 | error(1, errno, "<xdirname> could not strdup \"/\" for set path result"); | ||
| 226 | #else | ||
| 227 | exit(EXIT_FAILURE); | ||
| 228 | #endif | ||
| 229 | #endif | ||
| 230 | abort(); | ||
| 231 | } | ||
| 232 | 171 | ||
| 233 | return tmp; | 172 | return tmp; |
| 234 | } | 173 | } |
| @@ -241,16 +180,8 @@ char *xdirname(const char * const path) { | |||
| 241 | } | 180 | } |
| 242 | 181 | ||
| 243 | char * const actual = strdup(tmp); | 182 | char * const actual = strdup(tmp); |
| 244 | if(!actual) { | 183 | if(!actual) |
| 245 | #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 | 184 | XALLOC_EXIT("<xdirname> could not strdup tmp string to make a shorter end string"); |
| 246 | #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 | ||
| 247 | error(1, errno, "<xdirname> could not strdup tmp string to make a shorter end string"); | ||
| 248 | #else | ||
| 249 | exit(EXIT_FAILURE); | ||
| 250 | #endif | ||
| 251 | #endif | ||
| 252 | abort(); | ||
| 253 | } | ||
| 254 | free(tmp); | 185 | free(tmp); |
| 255 | 186 | ||
| 256 | return actual; | 187 | return actual; |
diff --git a/src/shared.h b/src/shared.h index 3c23676..ee3e06b 100644 --- a/src/shared.h +++ b/src/shared.h | |||
| @@ -2,9 +2,8 @@ | |||
| 2 | #define __VXGG_REWRITE___SHARED_H___3880294315821___ | 2 | #define __VXGG_REWRITE___SHARED_H___3880294315821___ |
| 3 | 3 | ||
| 4 | #include <stddef.h> | 4 | #include <stddef.h> |
| 5 | #include <stdarg.h> | ||
| 6 | 5 | ||
| 7 | #define STATICARR_SIZE(arr) (sizeof((arr))/sizeof((arr)[0])) | 6 | #define STATIC_ARRAY_LEN(arr) (sizeof((arr))/sizeof((arr)[0])) |
| 8 | 7 | ||
| 9 | // Defines how `x___alloc()` functions should exit. `___VXGG___XALLOC_EXIT_ON_ERROR___ > 0` calls `error()`, and thus functions | 8 | // Defines how `x___alloc()` functions should exit. `___VXGG___XALLOC_EXIT_ON_ERROR___ > 0` calls `error()`, and thus functions |
| 10 | // registered with `atexit()` and `on_exit()`. `___VXGG___XALLOC_EXIT_ON_ERROR___ <= 0` calls `abort()` on error. `x___alloc()` | 9 | // registered with `atexit()` and `on_exit()`. `___VXGG___XALLOC_EXIT_ON_ERROR___ <= 0` calls `abort()` on error. `x___alloc()` |
| @@ -15,6 +14,16 @@ | |||
| 15 | // `___VXGG___VERBOSE_ERRORS___ > 0` will print diagnostic error messages, and will do nothing otherwise | 14 | // `___VXGG___VERBOSE_ERRORS___ > 0` will print diagnostic error messages, and will do nothing otherwise |
| 16 | #define ___VXGG___VERBOSE_ERRORS___ 1 | 15 | #define ___VXGG___VERBOSE_ERRORS___ 1 |
| 17 | 16 | ||
| 17 | // Macro to exit on an alloc error instead of doing the terrible nested if statement that was being used previously | ||
| 18 | #define XALLOC_EXIT(msg, ...) do {\ | ||
| 19 | if(!___VXGG___XALLOC_EXIT_ON_ERROR___)\ | ||
| 20 | abort();\ | ||
| 21 | if(!___VXGG___VERBOSE_ERRORS___)\ | ||
| 22 | exit(EXIT_FAILURE);\ | ||
| 23 | error(EXIT_FAILURE, errno, (msg)__VA_ARGS__);\ | ||
| 24 | exit(EXIT_FAILURE); /* Makes gcc happy */\ | ||
| 25 | } while (0) | ||
| 26 | |||
| 18 | // `calloc()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` | 27 | // `calloc()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` |
| 19 | void* xcalloc(size_t nmemb, size_t size); | 28 | void* xcalloc(size_t nmemb, size_t size); |
| 20 | 29 | ||
