aboutsummaryrefslogtreecommitdiffstats
path: root/random_bytes.c
blob: 723fc6346fbe800d66e272935d4895bbffc580ce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* 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)) {
		rnd = (*rng)(sizeof(uintmax_t) * 8U, user);
		memcpy(buf, &rnd, sizeof(uintmax_t));
		bytes -= sizeof(uintmax_t);
		buf = &buf[sizeof(uintmax_t)];
	}

	if (bytes) {
		rnd = (*rng)(bytes * 8U, user);
		while (bytes) {
			*buf++ = (char)(rnd & 255U);
			bytes--;
			rnd >>= 8;
		}
	}

	return buf;
}


#else
#include "test.h"

int
main(void)
{
	return 0;
}

#endif