diff options
author | axtloss <axtlos@getcryst.al> | 2024-07-16 02:28:06 +0200 |
---|---|---|
committer | axtloss <axtlos@getcryst.al> | 2024-07-16 02:28:06 +0200 |
commit | 8a1c3461bb820bf875a50be56c7652177cf50645 (patch) | |
tree | b79b103693dc01f0f5b0b993e9f9fcf972d67977 | |
parent | 0366952bae967a1b69c510f8e633c0567ccab4a9 (diff) | |
download | extlib-8a1c3461bb820bf875a50be56c7652177cf50645.tar.gz extlib-8a1c3461bb820bf875a50be56c7652177cf50645.tar.bz2 |
fix formatting
-rw-r--r-- | src/constraint_handler.c | 55 | ||||
-rw-r--r-- | src/extlib.c | 90 | ||||
-rw-r--r-- | src/extlib.h | 15 | ||||
-rw-r--r-- | src/extstring.c | 141 | ||||
-rw-r--r-- | src/memset_s.c | 38 | ||||
-rw-r--r-- | tests/test.c | 270 |
6 files changed, 313 insertions, 296 deletions
diff --git a/src/constraint_handler.c b/src/constraint_handler.c index 450632e..5506ab0 100644 --- a/src/constraint_handler.c +++ b/src/constraint_handler.c @@ -11,41 +11,44 @@ static constraint_handler_t *_cnstrnt_handler = NULL; constraint_handler_t set_constraint_handler_s (constraint_handler_t handler) { - constraint_handler_t *new, *old, ret; - - new = malloc (sizeof (constraint_handler_t)); - if (!new) return NULL; - *new = handler; - old = _cnstrnt_handler; - _cnstrnt_handler = new; - if (!old) - ret = NULL; - else { - ret = *old; - free (old); - } - return ret; + constraint_handler_t *new, *old, ret; + + new = malloc (sizeof (constraint_handler_t)); + if (!new) + return NULL; + *new = handler; + old = _cnstrnt_handler; + _cnstrnt_handler = new; + if (!old) + ret = NULL; + else + { + ret = *old; + free (old); + } + return ret; } void -throw_constraint_handler_s (const char * restrict msg, errno_t error) +throw_constraint_handler_s (const char *restrict msg, errno_t error) { - constraint_handler_t ch; - ch = _cnstrnt_handler != NULL ? *_cnstrnt_handler : NULL; - if (ch != NULL) - ch (msg, NULL, error); + constraint_handler_t ch; + ch = _cnstrnt_handler != NULL ? *_cnstrnt_handler : NULL; + if (ch != NULL) + ch (msg, NULL, error); } void -abort_handler_s (const char *restrict msg, void * restrict ptr, errno_t error) +abort_handler_s (const char *restrict msg, void *restrict ptr, errno_t error) { - static const char abrt_msg[] = "abort_handler: "; + static const char abrt_msg[] = "abort_handler: "; - fprintf (stderr, "%s", abrt_msg); - fprintf (stderr, "%s\n", msg); - abort (); + fprintf (stderr, "%s", abrt_msg); + fprintf (stderr, "%s\n", msg); + abort (); } void -ignore_handler_s (const char * restrict msg, void * restrict ptr, errno_t error) -{} +ignore_handler_s (const char *restrict msg, void *restrict ptr, errno_t error) +{ +} diff --git a/src/extlib.c b/src/extlib.c index 2ee4225..e1fdb14 100644 --- a/src/extlib.c +++ b/src/extlib.c @@ -1,15 +1,16 @@ -/* extlib.c +/* + * extlib.c * * Copyright 2024 axtlos <axtlos@disroot.org> * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, version 3. * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. @@ -32,69 +33,66 @@ #undef malloc void -free_secure(void **__ptr, size_t ptrlen) +free_secure (void **__ptr, size_t ptrlen) { - memset_s (*__ptr, ptrlen+1, 0, ptrlen); - free (*__ptr); - *__ptr = NULL; - return; + memset_s (*__ptr, ptrlen + 1, 0, ptrlen); + free (*__ptr); + *__ptr = NULL; + return; } void * malloc_secure (size_t len) { - void *mem = calloc (1, len); - return mem; + void *mem = calloc (1, len); + return mem; } int -memvcmp (void *str, - char val, - size_t n) +memvcmp (void *str, char val, size_t n) { - char str2[n]; - memset (str2, val, n); - return memcmp (str, str2, n); + char str2[n]; + memset (str2, val, n); + return memcmp (str, str2, n); } size_t fcopy (FILE *src, FILE *dst) { - char buffer[BUFSIZ]; - size_t n, copied = 0; + char buffer[BUFSIZ]; + size_t n, copied = 0; - if (src == NULL || dst == NULL) - return -1; + if (src == NULL || dst == NULL) + return -1; - while ((n = fread (buffer, sizeof (char), sizeof (buffer), src)) > 0) - { - if (fwrite (buffer, sizeof (char), n, dst) != n) { - fprintf (stderr, "Failed to copy data"); - return -1; + while ((n = fread (buffer, sizeof (char), sizeof (buffer), src)) > 0) + { + if (fwrite (buffer, sizeof (char), n, dst) != n) + { + fprintf (stderr, "Failed to copy data"); + return -1; + } + fflush (dst); + copied += n; } - fflush (dst); - copied += n; - } - return copied; + return copied; } int -rmfile(const char *fpath, - const struct stat *sb, - int typeflag, - struct FTW *ftwbuf) +rmfile (const char *fpath, + const struct stat *sb, int typeflag, struct FTW *ftwbuf) { - int err = remove (fpath); - if (err < 0) - return err; - return 0; + int err = remove (fpath); + if (err < 0) + return err; + return 0; } int -rrmdir(char *pathname) +rrmdir (char *pathname) { - int err = nftw (pathname, rmfile, 10, FTW_DEPTH|FTW_MOUNT|FTW_PHYS); - if (err < 0) - return err; - return 0; + int err = nftw (pathname, rmfile, 10, FTW_DEPTH | FTW_MOUNT | FTW_PHYS); + if (err < 0) + return err; + return 0; } diff --git a/src/extlib.h b/src/extlib.h index b12ddbf..c62c374 100644 --- a/src/extlib.h +++ b/src/extlib.h @@ -25,7 +25,8 @@ void *malloc_secure (size_t len); typedef int errno_t; typedef size_t rsize_t; -typedef void (*constraint_handler_t) (const char *__restrict, void *__restrict, errno_t); +typedef void (*constraint_handler_t) (const char *__restrict, + void *__restrict, errno_t); /// Copy the value of c (converted to an unsigned char) into each of the first n /// characters of the object pointed to by s. /// Conforms to ISO/IEC 9899:2011 K.3.7.4.1 @@ -33,19 +34,19 @@ errno_t memset_s (void *s, rsize_t max, int c, rsize_t n); constraint_handler_t set_constraint_handler_s (constraint_handler_t handler); void throw_constraint_handler_s (const char *restrict msg, errno_t error); -_Noreturn void abort_handler_s (const char * __restrict, void * __restrict, - errno_t); +_Noreturn void abort_handler_s (const char *__restrict, void *__restrict, + errno_t); void ignore_handler_s (const char *__restrict, void *__restrict, errno_t); #endif /// Copy the data of one filestream to another /// Returns the amount of bytes copied. -size_t fcopy(FILE *src, FILE *dst); +size_t fcopy (FILE * src, FILE * dst); #if (_XOPEN_SOURCE == 500) /// Recursively remove a directory pathname. -int rrmdir(char *pathname); +int rrmdir (char *pathname); #endif /// Compare the first n bytes of a memory area str to the value val. @@ -53,10 +54,10 @@ int rrmdir(char *pathname); int memvcmp (void *str, char val, size_t n); /// Convert a string to lowercase -char *strlwr(char *s); +char *strlwr (char *s); /// Convert a string to uppercase -char *strupr(char *s); +char *strupr (char *s); /// Trim spaces from a string char *trim (char *s, int *rem_front, int *rem_back); diff --git a/src/extstring.c b/src/extstring.c index fc3cb45..044e368 100644 --- a/src/extstring.c +++ b/src/extstring.c @@ -11,102 +11,111 @@ char * strlwr (char *s) { - unsigned char *p = (unsigned char *) s; + unsigned char *p = (unsigned char *) s; - while (*p) { - *p = tolower ((unsigned char)*p); - p++; - } + while (*p) + { + *p = tolower ((unsigned char) *p); + p++; + } - return s; + return s; } char * strupr (char *s) { - unsigned char *p = (unsigned char *) s; + unsigned char *p = (unsigned char *) s; - while (*p) { - *p = toupper ((unsigned char)*p); - p++; - } + while (*p) + { + *p = toupper ((unsigned char) *p); + p++; + } - return s; + return s; } char * trim (char *s, int *rem_front, int *rem_back) { - char *result = strdup (s); - char *end; - - while (isspace ((unsigned char)*result)) { - result++; - if (rem_front) *rem_front += 1; - } + char *result = strdup (s); + char *end; + + while (isspace ((unsigned char) *result)) + { + result++; + if (rem_front) + *rem_front += 1; + } - if (*result == 0) - return result; + if (*result == 0) + return result; - end = result + strlen (result) - 1; - while (end > result && isspace ((unsigned char)*end)) { - end--; - if (rem_back) *rem_back +=1; - } + end = result + strlen (result) - 1; + while (end > result && isspace ((unsigned char) *end)) + { + end--; + if (rem_back) + *rem_back += 1; + } - end[1] = '\0'; + end[1] = '\0'; - return result; + return result; } char * -replace_str (char *s, - char *old, - char *replace) +replace_str (char *s, char *old, char *replace) { - char* result; - int i, cnt = 0; - size_t new_size = strlen (replace); - size_t old_size = strlen (old); - - for (i = 0; s[i] != '\0'; i++) { - if (strstr (&s[i], old) == &s[i]) { - cnt++; - i += old_size - 1; + char *result; + int i, cnt = 0; + size_t new_size = strlen (replace); + size_t old_size = strlen (old); + + for (i = 0; s[i] != '\0'; i++) + { + if (strstr (&s[i], old) == &s[i]) + { + cnt++; + i += old_size - 1; + } } - } - - result = (char*)malloc_secure (i + cnt * (new_size - old_size) + 1); - i = 0; - while (*s) { - if (strstr (s, old) == s) { - strcpy (&result[i], replace); - i += new_size; - s += old_size; + result = (char *) malloc_secure (i + cnt * (new_size - old_size) + 1); + + i = 0; + while (*s) + { + if (strstr (s, old) == s) + { + strcpy (&result[i], replace); + i += new_size; + s += old_size; + } + else + result[i++] = *s++; } - else - result[i++] = *s++; - } - result[i] = '\0'; - return result; + result[i] = '\0'; + return result; }; char * join_str (char **s, size_t len, char delim) { - char *ret = strdup (s[0]); - size_t ret_len = sizeof (char) * len + strlen (ret); - - for (int i = 1; i < len; i++) { - char *ret_tmp = realloc (ret, ret_len + strlen (s[i]) + 1); - ret_len += strlen (s[i]) + 1; - if (ret_tmp != NULL) - ret = ret_tmp; - sprintf (ret, "%s%c%s", ret, delim, s[i]); - } - - return ret; + char *ret = strdup (s[0]); + size_t ret_len = sizeof (char) * len + strlen (ret); + + for (int i = 1; i < len; i++) + { + char *ret_tmp = realloc (ret, ret_len + strlen (s[i]) + 1); + ret_len += strlen (s[i]) + 1; + if (ret_tmp != NULL) + ret = ret_tmp; + sprintf (ret, "%s%c%s", ret, delim, s[i]); + } + + return ret; } diff --git a/src/memset_s.c b/src/memset_s.c index 8b4d342..74c0025 100644 --- a/src/memset_s.c +++ b/src/memset_s.c @@ -7,25 +7,25 @@ #include <errno.h> errno_t -memset_s(void *s, rsize_t smax, int c, rsize_t n) +memset_s (void *s, rsize_t smax, int c, rsize_t n) { - volatile unsigned char *dest = (unsigned char *) s; - errno_t ret = EINVAL; - rsize_t limit = n < smax ? n : smax; + volatile unsigned char *dest = (unsigned char *) s; + errno_t ret = EINVAL; + rsize_t limit = n < smax ? n : smax; - if (!s) - throw_constraint_handler_s("memset_s: s = NULL", ret); - else if (n > RSIZE_MAX) - throw_constraint_handler_s("memset_s: n > RSIZE_MAX", ret); - else if (smax > RSIZE_MAX) - throw_constraint_handler_s("memset_s: smax > RSIZE_MAX", ret); - else if (n > smax) - throw_constraint_handler_s("memset_s: n > smax", ret); - else { - while (limit > 0) - dest[--limit] = (unsigned char)c; - ret = 0; - } - return ret; + if (!s) + throw_constraint_handler_s ("memset_s: s = NULL", ret); + else if (n > RSIZE_MAX) + throw_constraint_handler_s ("memset_s: n > RSIZE_MAX", ret); + else if (smax > RSIZE_MAX) + throw_constraint_handler_s ("memset_s: smax > RSIZE_MAX", ret); + else if (n > smax) + throw_constraint_handler_s ("memset_s: n > smax", ret); + else + { + while (limit > 0) + dest[--limit] = (unsigned char) c; + ret = 0; + } + return ret; } - diff --git a/tests/test.c b/tests/test.c index 0b19f97..332ebb8 100644 --- a/tests/test.c +++ b/tests/test.c @@ -9,168 +9,174 @@ int test_malloc_free_secure (size_t size) { - char *mall_test = malloc_secure (size); - if (!mall_test) - return 1; - if (memvcmp (mall_test, 0, size) != 0) - return 2; - free_secure ((void **)&mall_test, size); - if (mall_test) - return 3; - return 0; + char *mall_test = malloc_secure (size); + if (!mall_test) + return 1; + if (memvcmp (mall_test, 0, size) != 0) + return 2; + free_secure ((void **) &mall_test, size); + if (mall_test) + return 3; + return 0; } int -test_fcopy() +test_fcopy () { - int errno = 0; - char *buf_a; - char *buf_b; - size_t len_a; - size_t len_b; - FILE *file_test_a = open_memstream (&buf_a, &len_a); - FILE *file_test_b = open_memstream (&buf_b, &len_b); - - fprintf (file_test_a, "hello"); - fflush (file_test_a); - fclose (file_test_a); - file_test_a = fmemopen(buf_a, len_a, "r"); - - fcopy (file_test_a, file_test_b); - fclose (file_test_b); - - if (memcmp(buf_a, buf_b, len_a) != 0) - errno = 1; - - free_secure ((void**)&buf_a, len_a); - free_secure ((void **)&buf_b, len_b); - return errno; + int errno = 0; + char *buf_a; + char *buf_b; + size_t len_a; + size_t len_b; + FILE *file_test_a = open_memstream (&buf_a, &len_a); + FILE *file_test_b = open_memstream (&buf_b, &len_b); + + fprintf (file_test_a, "hello"); + fflush (file_test_a); + fclose (file_test_a); + file_test_a = fmemopen (buf_a, len_a, "r"); + + fcopy (file_test_a, file_test_b); + fclose (file_test_b); + + if (memcmp (buf_a, buf_b, len_a) != 0) + errno = 1; + + free_secure ((void **) &buf_a, len_a); + free_secure ((void **) &buf_b, len_b); + return errno; } int test_strlwr_strupr () { - int errno = 0; - char *test_string = strdup ("hello"); - char *uppr_string = strupr (test_string); - char *lower_string = strlwr (uppr_string); + int errno = 0; + char *test_string = strdup ("hello"); + char *uppr_string = strupr (test_string); + char *lower_string = strlwr (uppr_string); - if (strcmp (uppr_string, "HELLO") != 0) - errno = 1; + if (strcmp (uppr_string, "HELLO") != 0) + errno = 1; - if (strcmp (lower_string, "hello") != 0) - errno = 2; + if (strcmp (lower_string, "hello") != 0) + errno = 2; - EXIT: - free_secure ((void **)&test_string, strlen (test_string)); - return errno; + EXIT: + free_secure ((void **) &test_string, strlen (test_string)); + return errno; } int -test_trim() +test_trim () { #undef free - int errno = 0; - char *test_string = strdup ("\t\thi\t\t"); - int rem_front = 0; - int rem_back = 0; - char *trimmed_string = trim (test_string, &rem_front, &rem_back); - if (strstr (trimmed_string, "\t") != NULL) { - errno = 1; - goto EXIT; - } - if (rem_front != 2 && rem_back != 2) { - errno = 2; - goto EXIT; - } - - EXIT: - free (trimmed_string-rem_front); - free_secure ((void**)&test_string, strlen (test_string)); - return errno; + int errno = 0; + char *test_string = strdup ("\t\thi\t\t"); + int rem_front = 0; + int rem_back = 0; + char *trimmed_string = trim (test_string, &rem_front, &rem_back); + if (strstr (trimmed_string, "\t") != NULL) + { + errno = 1; + goto EXIT; + } + if (rem_front != 2 && rem_back != 2) + { + errno = 2; + goto EXIT; + } + + EXIT: + free (trimmed_string - rem_front); + free_secure ((void **) &test_string, strlen (test_string)); + return errno; } int test_replace_str () { - int errno = 0; - char *test_string = strdup ("replace world!"); - char *replaced_string = replace_str (test_string, "replace", "hello"); - if (strcmp (replaced_string, "hello world!") != 0) - errno = 1; - - free_secure ((void**)&test_string, strlen (test_string)); - free_secure ((void**)&replaced_string, strlen (replaced_string)); - return errno; + int errno = 0; + char *test_string = strdup ("replace world!"); + char *replaced_string = replace_str (test_string, "replace", "hello"); + if (strcmp (replaced_string, "hello world!") != 0) + errno = 1; + + free_secure ((void **) &test_string, strlen (test_string)); + free_secure ((void **) &replaced_string, strlen (replaced_string)); + return errno; } int test_join_str () { - int errno = 0; - char **test_string = malloc (sizeof (char*) * 4); - for (int i = 0; i < 3; i++) - test_string [i] = strdup ("hello"); - - char *joined_string = join_str (test_string, 3, ' '); - - if (strcmp (joined_string, "hello hello hello") != 0) - errno = 1; - - free_secure ((void**)&test_string[0], strlen (test_string[0])); - free_secure ((void**)&test_string[1], strlen (test_string[1])); - free_secure ((void**)&test_string[2], strlen (test_string[2])); - free (test_string); - free_secure ((void**)&joined_string, strlen (joined_string)); - return errno; + int errno = 0; + char **test_string = malloc (sizeof (char *) * 4); + for (int i = 0; i < 3; i++) + test_string[i] = strdup ("hello"); + + char *joined_string = join_str (test_string, 3, ' '); + + if (strcmp (joined_string, "hello hello hello") != 0) + errno = 1; + + free_secure ((void **) &test_string[0], strlen (test_string[0])); + free_secure ((void **) &test_string[1], strlen (test_string[1])); + free_secure ((void **) &test_string[2], strlen (test_string[2])); + free (test_string); + free_secure ((void **) &joined_string, strlen (joined_string)); + return errno; } int main (void) { - printf ("=== extlib unit tests ===\n"); - printf ("\n"); - - printf ("Test Case 1: test_malloc_free_secure -- malloc_secure free_secure memvcmp\n"); - int test_1_result = test_malloc_free_secure (12); - if (test_1_result == 0) - printf ("Test Case 1: test_malloc_free_secure -- SUCCESS\n\n"); - else - printf ("Test Case 1: test_malloc_free_secure -- FAILED %d\n\n", test_1_result); - - printf ("Test Case 2: test_fcopy -- fcopy\n"); - int test_2_result = test_fcopy (); - if (test_2_result == 0) - printf ("Test Case 2: test_fcopy -- SUCCESS\n\n"); - else - printf ("Test Case 2: test_fcopy -- FAILED %d\n\n", test_2_result); - - printf ("Test Case 3: test_strlwr_strupr -- strlwr strupr\n"); - int test_3_result = test_strlwr_strupr (); - if (test_2_result == 0) - printf ("Test Case 3: test_strlwr_strupr -- SUCCESS\n\n"); - else - printf ("Test Case 3: test_strlwr_strupr -- FAILED %d\n\n", test_3_result); - - printf ("Test Case 4: test_trim -- trim\n"); - int test_4_result = test_trim (); - if (test_4_result == 0) - printf ("Test Case 4: test_trim -- SUCCESS\n\n"); - else - printf ("Test Case 4: test_trim -- FAILED %d\n\n", test_4_result); - - printf ("Test Case 5: test_replace_str -- replace_str\n"); - int test_5_result = test_replace_str(); - if (test_5_result == 0) - printf ("Test Case 5: test_replace_str -- SUCCESS\n\n"); - else - printf ("Test Case 5: test_replace_str -- FAILED %d\n\n", test_5_result); - - printf ("Test Case 6: test_join_str -- join_str\n"); - int test_6_result = test_join_str(); - if (test_6_result == 0) - printf ("Test Case 6: test_join_str -- SUCCESS\n\n"); - else - printf ("Test Case 6: test_join_str -- FAILED %d\n\n", test_6_result); - return 0; + printf ("=== extlib unit tests ===\n"); + printf ("\n"); + + printf + ("Test Case 1: test_malloc_free_secure -- malloc_secure free_secure memvcmp\n"); + int test_1_result = test_malloc_free_secure (12); + if (test_1_result == 0) + printf ("Test Case 1: test_malloc_free_secure -- SUCCESS\n\n"); + else + printf ("Test Case 1: test_malloc_free_secure -- FAILED %d\n\n", + test_1_result); + + printf ("Test Case 2: test_fcopy -- fcopy\n"); + int test_2_result = test_fcopy (); + if (test_2_result == 0) + printf ("Test Case 2: test_fcopy -- SUCCESS\n\n"); + else + printf ("Test Case 2: test_fcopy -- FAILED %d\n\n", test_2_result); + + printf ("Test Case 3: test_strlwr_strupr -- strlwr strupr\n"); + int test_3_result = test_strlwr_strupr (); + if (test_2_result == 0) + printf ("Test Case 3: test_strlwr_strupr -- SUCCESS\n\n"); + else + printf ("Test Case 3: test_strlwr_strupr -- FAILED %d\n\n", + test_3_result); + + printf ("Test Case 4: test_trim -- trim\n"); + int test_4_result = test_trim (); + if (test_4_result == 0) + printf ("Test Case 4: test_trim -- SUCCESS\n\n"); + else + printf ("Test Case 4: test_trim -- FAILED %d\n\n", test_4_result); + + printf ("Test Case 5: test_replace_str -- replace_str\n"); + int test_5_result = test_replace_str (); + if (test_5_result == 0) + printf ("Test Case 5: test_replace_str -- SUCCESS\n\n"); + else + printf ("Test Case 5: test_replace_str -- FAILED %d\n\n", + test_5_result); + + printf ("Test Case 6: test_join_str -- join_str\n"); + int test_6_result = test_join_str (); + if (test_6_result == 0) + printf ("Test Case 6: test_join_str -- SUCCESS\n\n"); + else + printf ("Test Case 6: test_join_str -- FAILED %d\n\n", test_6_result); + return 0; } |