diff options
-rw-r--r-- | CHANGELOG.rst | 2 | ||||
-rw-r--r-- | libgcompat/string.c | 34 |
2 files changed, 36 insertions, 0 deletions
diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 3ad98fe..ee9851d 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -40,6 +40,8 @@ string ------ * Add __strcspn_c2. +* Add memfrob. +* Add strfry. malloc diff --git a/libgcompat/string.c b/libgcompat/string.c index 5a1e558..9f1704b 100644 --- a/libgcompat/string.c +++ b/libgcompat/string.c @@ -3,6 +3,9 @@ #include <stddef.h> /* NULL, size_t */ #include <stdint.h> /* SIZE_MAX */ #include <string.h> /* memcpy, strcpy, strncat, strndup */ +#include <stdlib.h> /* rand_r */ +#include <unistd.h> /* getpid */ +#include <time.h> /* time */ #include "alias.h" /* weak_alias */ @@ -262,3 +265,34 @@ char *__strtok_r(char *s, const char *delim, char **save_ptr) { return strtok_r(s, delim, save_ptr); } + +void *memfrob(void *s, size_t n) +{ + unsigned char *c = s; + + while (n--) + *c++ ^= 42; + + return s; +} + +char *strfry(char *s) +{ + static unsigned int seed; + size_t len = strlen(s), i, j; + char t; + + if (!len) + return s; + + seed += time(NULL) ^ getpid() ^ (uintptr_t)s; + + for (i = 0; i < len - 1; ++i) { + j = rand_r(&seed) % (len - i) + i; + t = s[i]; + s[i] = s[j]; + s[j] = t; + } + + return s; +} |