From f8e94a1179633799579a09dce1841f41e3b73f05 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Mon, 6 Jan 2025 18:34:13 -0600 Subject: Start work on encryption --- src/shared.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'src/shared.c') diff --git a/src/shared.c b/src/shared.c index 5e58de0..0c7c8e2 100644 --- a/src/shared.c +++ b/src/shared.c @@ -1,8 +1,10 @@ #include "shared.h" +#include #include #include #include +#include void* xcalloc(size_t nmemb, size_t size) { void *mem = calloc(nmemb, size); @@ -17,4 +19,44 @@ void* xcalloc(size_t nmemb, size_t size) { return mem; +} + +void* xreallocarray(void *ptr, size_t nmemb, size_t size) { + void *mem = reallocarray(ptr, nmemb, size); + if(mem == NULL) { + #if defined ___VXGG___XCALLOC_EXIT_ON_ERROR___ && ___VXGG___XCALLOC_EXIT_ON_ERROR___ > 0 + error(1, errno, " Could not allocate memory"); + + #endif + + abort(); + } + + return mem; +} + +int vsaprintf(char **str, const char *format, va_list ap) { + va_list ap2; + va_copy(ap2, ap); + + int length = vsnprintf(NULL, 0, format, ap2) + 1; // + 1 because sprintf does not count the null byte + char *temp = reallocarray(*str, length, sizeof(char)); + if(temp == NULL) + return -1; + + int ret = vsnprintf(temp, length, format, ap); + *str = temp; + + va_end(ap2); + return ret; +} + +int saprintf(char **str, const char *format, ...) { + va_list ap; + va_start(ap, format); + + int ret = vsaprintf(str, format, ap); + + va_end(ap); + return ret; } \ No newline at end of file -- cgit v1.2.3