aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoraxtloss <axtlos@getcryst.al>2024-06-24 21:50:48 +0200
committeraxtloss <axtlos@getcryst.al>2024-06-24 21:50:48 +0200
commita084dfa02d28a9372a7e406c9b101a43308a4937 (patch)
tree620d7fe5f55fa76572f5666465997f397b8bbd2a /src
parent1d4b6499993050876a453a3f96e2428fdfd42610 (diff)
downloadextlib-a084dfa02d28a9372a7e406c9b101a43308a4937.tar.gz
extlib-a084dfa02d28a9372a7e406c9b101a43308a4937.tar.bz2
Add new function memvcmp and unit tests
Diffstat (limited to 'src')
-rw-r--r--src/extlib.c18
-rw-r--r--src/extlib.h6
2 files changed, 20 insertions, 4 deletions
diff --git a/src/extlib.c b/src/extlib.c
index ee20d3e..151a74f 100644
--- a/src/extlib.c
+++ b/src/extlib.c
@@ -27,10 +27,11 @@
#undef malloc
void
-free_secure(void *__ptr, size_t ptrlen)
+free_secure(void **__ptr, size_t ptrlen)
{
- memset (__ptr, 0, ptrlen);
- free (__ptr);
+ memset (*__ptr, 0, ptrlen);
+ free (*__ptr);
+ *__ptr = NULL;
return;
}
@@ -42,6 +43,16 @@ malloc_secure (size_t len)
return mem;
}
+int
+memvcmp (void *str,
+ char val,
+ size_t n)
+{
+ char str2[n];
+ memset (str2, val, n);
+ return memcmp (str, str2, n);
+}
+
void
fcopy(FILE *f1, FILE *f2)
{
@@ -54,5 +65,6 @@ fcopy(FILE *f1, FILE *f2)
fprintf (stderr, "Failed to copy data");
return;
}
+ fflush (f2);
}
}
diff --git a/src/extlib.h b/src/extlib.h
index 1948352..15903c3 100644
--- a/src/extlib.h
+++ b/src/extlib.h
@@ -25,7 +25,7 @@
#define malloc(x) error - use malloc_secure
/// Automatically zero out a pointer before freeing it
-void free_secure (void *__ptr, size_t ptrlen);
+void free_secure (void **__ptr, size_t ptrlen);
/// Automatically initialise the allocated memory with zeros
void *malloc_secure (size_t len);
@@ -33,6 +33,10 @@ void *malloc_secure (size_t len);
/// Copy the data of one filestream to another
void fcopy(FILE *f1, FILE *f2);
+/// Compare the first n bytes of a memory area str to the value val.
+/// Returns 0 if the values the same, any other value if they are not the same.
+int memvcmp (void *str, char val, size_t n);
+
/// Convert a string to lowercase
char *strlwr(char *s);