diff options
author | axtloss <axtlos@getcryst.al> | 2024-06-23 03:39:42 +0200 |
---|---|---|
committer | axtloss <axtlos@getcryst.al> | 2024-06-23 03:39:42 +0200 |
commit | fa4666ae07fb7525aa8e84aacf906bc8cb398a6a (patch) | |
tree | e5d1f8b8f3da397c1be313119bb1061bdcadd2d7 | |
download | extlib-fa4666ae07fb7525aa8e84aacf906bc8cb398a6a.tar.gz extlib-fa4666ae07fb7525aa8e84aacf906bc8cb398a6a.tar.bz2 |
Add base extlib
-rw-r--r-- | .gitignore | 6 | ||||
-rw-r--r-- | Makefile | 30 | ||||
-rw-r--r-- | extlib.pc.in | 9 | ||||
-rw-r--r-- | src/extlib.c | 58 | ||||
-rw-r--r-- | src/extlib.h | 46 | ||||
-rw-r--r-- | src/extstring.c | 105 | ||||
-rw-r--r-- | test.c | 27 |
7 files changed, 281 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3f8c65b --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +*.so +*.o +*.pc +*# +*.~undo-tree~ +*~
\ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fb74653 --- /dev/null +++ b/Makefile @@ -0,0 +1,30 @@ +CC = gcc +CFLAGS = -g +PREFIX = /usr + +extlib: extlib.o extstring.o + $(CC) src/extlib.o src/extstring.o $(CFLAGS) -shared -fPIC -o libextlib.so + +extlib.pc: extlib.pc.in + sed 's|@prefix@|$(PREFIX)|g' extlib.pc.in > extlib.pc + sed 's|@libdir@|$(PREFIX)/lib|g' extlib.pc > extlib.pc.tmp + sed 's|@includedir@|$(PREFIX)/include|g' extlib.pc.tmp > extlib.pc + rm extlib.pc.tmp + +extlib.o: + cc src/extlib.c $(CFLAGS) -c -fPIC -o src/extlib.o + +extstring.o: + cc src/extstring.c $(CFLAGS) -c -fPIC -o src/extstring.o + +clean: + rm -r src/extlib.o src/exststring.o libextlib.so extlib.pc test + +install: extlib extlib.pc + install -Dm655 -t $(PREFIX)/lib/ libextlib.so + install -Dm655 -t $(PREFIX)/include/ src/extlib.h + install -Dm655 -t $(PREFIX)/share/pkgconfig/ extlib.pc + +test: + $(CC) -g -lextlib -I/usr/include/extlib -fsanitize=undefined,address test.c -o test + ./test diff --git a/extlib.pc.in b/extlib.pc.in new file mode 100644 index 0000000..07a763c --- /dev/null +++ b/extlib.pc.in @@ -0,0 +1,9 @@ +prefix=@prefix@ +libdir=@libdir@ +includedir=@includedir@ + +Name: extlib +Description: extension for stdlib +Version: 1.0.0 +Libs: -L${libdir} -lextlib +Cflags: -I${includedir}/extlib diff --git a/src/extlib.c b/src/extlib.c new file mode 100644 index 0000000..ee20d3e --- /dev/null +++ b/src/extlib.c @@ -0,0 +1,58 @@ +/* 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 + * 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. + * + * 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/>. + * + * SPDX-License-Identifier: LGPL-3.0-only + */ + +#include "extlib.h" + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#undef free +#undef malloc + +void +free_secure(void *__ptr, size_t ptrlen) +{ + memset (__ptr, 0, ptrlen); + free (__ptr); + return; +} + +void * +malloc_secure (size_t len) +{ + void *mem = malloc (len); + memset (mem, 0, len); + return mem; +} + +void +fcopy(FILE *f1, FILE *f2) +{ + char buffer[BUFSIZ]; + size_t n; + + while ((n = fread (buffer, sizeof (char), sizeof (buffer), f1)) > 0) + { + if (fwrite (buffer, sizeof (char), n, f2) != n) { + fprintf (stderr, "Failed to copy data"); + return; + } + } +} diff --git a/src/extlib.h b/src/extlib.h new file mode 100644 index 0000000..1948352 --- /dev/null +++ b/src/extlib.h @@ -0,0 +1,46 @@ +/* extlib.h + * + * 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 + * 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. + * + * 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/>. + * + * SPDX-License-Identifier: LGPL-3.0-only + */ + +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> + +#define free(x) error - use free_secure +#define malloc(x) error - use malloc_secure + +/// Automatically zero out a pointer before freeing it +void free_secure (void *__ptr, size_t ptrlen); + +/// Automatically initialise the allocated memory with zeros +void *malloc_secure (size_t len); + +/// Copy the data of one filestream to another +void fcopy(FILE *f1, FILE *f2); + +/// Convert a string to lowercase +char *strlwr(char *s); + +/// Convert a string to uppercase +char *strupr(char *s); + +/// Trim spaces from a string +char *trim (char *s); + +/// Match string old and replace it with string replace +char *replace_str (char *s, char *old, char *replace); diff --git a/src/extstring.c b/src/extstring.c new file mode 100644 index 0000000..5867fc5 --- /dev/null +++ b/src/extstring.c @@ -0,0 +1,105 @@ +/* extstring.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 + * 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. + * + * 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/>. + * + * SPDX-License-Identifier: LGPL-3.0-only + */ + +#include <stdlib.h> +#include <string.h> +#include <ctype.h> +#include "extlib.h" + +char * +strlwr (char *s) +{ + unsigned char *p = (unsigned char *) s; + + while (*p) { + *p = tolower ((unsigned char)*p); + p++; + } + + return s; +} + +char * +strupr (char *s) +{ + unsigned char *p = (unsigned char *) s; + + while (*p) { + *p = toupper ((unsigned char)*p); + p++; + } + + return s; + +} + +char * +trim (char *s) +{ + char *result = strdup (s); + char *end; + + while (isspace ((unsigned char)*result)) + result++; + + if (*result == 0) + return result; + + end = result + strlen (result) - 1; + while (end > result && isspace ((unsigned char)*end)) + end--; + + end[1] = '\0'; + + return result; +} + +char * +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; + } + } + + 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++; + } + + result[i] = '\0'; + return result; +}; @@ -0,0 +1,27 @@ +#include <stdlib.h> +#include <string.h> +#include <stdio.h> +#include <extlib.h> +#include <assert.h> + +void print_hex(const char *s) +{ + while(*s) + printf("%02x", (unsigned int) *s++); + printf("\n"); +} + +int +main(void) { + char *test = malloc_secure (9); + sprintf (test, "meowmeow"); + assert (strcmp (test, "meowmeow") == 0); + puts ("Test 1 passed"); + char *upper = strupr (test); + assert (strcmp (upper, "MEOWMEOW") == 0); + puts ("Test 2 passed"); + char *lower = strlwr (test); + assert (strcmp (lower, "meowmeow") == 0); + puts ("Test 3 passed"); + free_secure (lower, strlen (lower)); +} |