From 6f2ccb15b1599700dc6c9c2388609eccca10a608 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 2 Sep 2024 18:02:13 +0200 Subject: m + add libsimple_random_bytes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- random_bytes.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 random_bytes.c (limited to 'random_bytes.c') diff --git a/random_bytes.c b/random_bytes.c new file mode 100644 index 0000000..cd7416f --- /dev/null +++ b/random_bytes.c @@ -0,0 +1,39 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" +#ifndef TEST + + +void * +libsimple_random_bytes(uintmax_t (*rng)(size_t bits, void *user), void *user, void *buffer, size_t bytes) +{ + char *buf = buffer; + uintmax_t rnd; + + while (bytes >= sizeof(uintmax_t)) { + *(uintmax_t *)buf = (*rng)(sizeof(uintmax_t) * 8U, user); + bytes -= sizeof(uintmax_t); + buf = &buf[sizeof(uintmax_t)]; + } + + if (bytes) { + rnd = (*rng)(bytes * 8U, user); + while (bytes) { + *buf++ = (char)(rnd & 255U); + rnd >>= 8; + } + } + + return buf; +} + + +#else +#include "test.h" + +int +main(void) +{ + return 0; +} + +#endif -- cgit v1.2.3-70-g09d2