diff --git a/include/mango/util.h b/include/mango/util.h index bf8bd4e..89cfb8c 100644 --- a/include/mango/util.h +++ b/include/mango/util.h @@ -53,6 +53,8 @@ extern uint64_t host_to_little_u64(uint64_t v); extern uint64_t big_to_host_u64(uint64_t v); extern uint64_t little_to_host_u64(uint64_t v); +extern bool fill_random(void *buffer, unsigned int size); + #ifdef __cplusplus } #endif diff --git a/libc/include/mango/libc/ctype.h b/libc/include/mango/libc/ctype.h index 27b04b9..4dc2eef 100644 --- a/libc/include/mango/libc/ctype.h +++ b/libc/include/mango/libc/ctype.h @@ -18,7 +18,6 @@ extern int isupper(int c); extern int isxdigit(int c); extern int tolower(int c); extern int toupper(int c); -extern int fill_random(unsigned char *buffer, unsigned int size); #ifdef __cplusplus } diff --git a/util/random.c b/util/random.c new file mode 100644 index 0000000..eb7243c --- /dev/null +++ b/util/random.c @@ -0,0 +1,36 @@ +#include + +static unsigned int random_seed = 53455346; + +bool fill_random(void *p, unsigned int size) +{ + unsigned char *buffer = p; + + if (!buffer || !size) { + return false; + } + + for (uint32_t i = 0; i < size; i++) { + uint32_t next = random_seed; + uint32_t result; + + next *= 1103515245; + next += 12345; + result = (uint32_t)(next / 65536) % 2048; + + next *= 1103515245; + next += 12345; + result <<= 10; + result ^= (uint32_t)(next / 65536) % 1024; + + next *= 1103515245; + next += 12345; + result <<= 10; + result ^= (uint32_t)(next / 65536) % 1024; + random_seed = next; + + buffer[i] = (uint8_t)(result % 256); + } + + return true; +}