From 2f66e8678971ba0340a96d811ced405d75dbb114 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Sun, 19 Jan 2025 17:58:45 -0600 Subject: Write example code for generating, storing, and verifying a password --- src/shared.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) (limited to 'src/shared.c') diff --git a/src/shared.c b/src/shared.c index b154391..e5f0f13 100644 --- a/src/shared.c +++ b/src/shared.c @@ -2,6 +2,8 @@ #include #include +#include +#include #include #include #include @@ -9,7 +11,7 @@ void* xcalloc(size_t nmemb, size_t size) { void *mem = calloc(nmemb, size); - if(mem == NULL) { + if(!mem) { #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 error(1, errno, " Could not allocate memory"); #endif @@ -33,4 +35,55 @@ void* xreallocarray(void *ptr, size_t nmemb, size_t size) { } return mem; -} \ No newline at end of file +} + +int readwholebuffer(char **str, unsigned long int initsize, int fd) { + // Try to read bytes from fd into str + // Bytes read == 0, return 0 + // Bytes read < 0, free string, return -1; + // When string hits capacity, double the capacity, and reallocate the string + + char *lstr = NULL, *tmp = NULL; + ssize_t bytesread = -1; + int csize = initsize, ccap = initsize; + + lstr = xcalloc(initsize, sizeof(char)); + while((bytesread = read(fd, lstr + (csize - ccap), ccap)) > 0) { + ccap -= bytesread; + if(ccap <= 0) { + csize *= 2; + ccap = csize / 2; + + tmp = realloc(lstr, csize * sizeof(char)); + if(!tmp) { + error(0, errno, "Could not reallocate enough space for lstr"); + free(lstr); + bytesread = -100; + break; + } + lstr = tmp; + } + } + if(bytesread < 0 && bytesread != -100) { + error(0, errno, "Ran into a read() error"); + free(lstr); + lstr = NULL; + } + + if(lstr) { + tmp = realloc(lstr, csize - ccap + 1); + if(!tmp) { + error(0, errno, "Could not shrink lstr after reading buffer"); + free(lstr); + bytesread = -100; + } + lstr = tmp; + } + + if(lstr) { + lstr[csize - ccap - 1] = '\0'; // Don't include the newline + } + + *str = lstr; + return ((bytesread == 0) ? (csize - ccap) : -1); +} -- cgit v1.2.3