aboutsummaryrefslogtreecommitdiff
path: root/src/random.h
blob: 9094d5bea2c1c696faf90a3e6c9538c0fd1f9382 (plain)
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
42
43
44
45
46
47
#ifndef ITS1_RANDOM_H_INCLUDED
#define ITS1_RANDOM_H_INCLUDED

#ifdef __cplusplus
extern "C" {
#endif

struct random_ctx {
	unsigned char (*get_byte)(struct random_ctx *ctx);
};

static unsigned char
random_get_byte(struct random_ctx *ctx)
{
	return ctx->get_byte(ctx);
}

static unsigned short
random_get_word(struct random_ctx *ctx)
{
	return (ctx->get_byte(ctx) << 8)
		 |  ctx->get_byte(ctx)
		 ;
}

static unsigned long
random_get_dword(struct random_ctx *ctx)
{
	return (ctx->get_byte(ctx) << 24)
		 | (ctx->get_byte(ctx) << 16)
		 | (ctx->get_byte(ctx) <<  8)
		 |  ctx->get_byte(ctx)
		 ;
}

static unsigned long
random_get_dword_range(struct random_ctx *ctx, unsigned long l, unsigned long u)
{
	return l + random_get_dword(ctx) % (u - l + 1);
}

#ifdef __cplusplus
}
#endif

#endif	/* ITS1_RANDOM_H_INCLUDED */