summaryrefslogtreecommitdiff
path: root/Kernel/RandomDevice.cpp
blob: 3544080528da27bbe88372a12496e2ef9b84aaca (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
48
49
50
51
52
53
#include "RandomDevice.h"
#include "Limits.h"
#include <AK/StdLibExtras.h>

RandomDevice::RandomDevice()
    : CharacterDevice(1, 8)
{
}

RandomDevice::~RandomDevice()
{
}

// Simple rand() and srand() borrowed from the POSIX standard:

static unsigned long next = 1;

#define MY_RAND_MAX 32767
static int myrand()
{
    next = next * 1103515245 + 12345;
    return((unsigned)(next/((MY_RAND_MAX + 1) * 2)) % (MY_RAND_MAX + 1));
}

#if 0
static void mysrand(unsigned seed)
{
    next = seed;
}
#endif

bool RandomDevice::can_read(Process&) const
{
    return true;
}

ssize_t RandomDevice::read(Process&, byte* buffer, size_t bufferSize)
{
    const int range = 'z' - 'a';
    ssize_t nread = min(bufferSize, GoodBufferSize);
    for (ssize_t i = 0; i < nread; ++i) {
        dword r = myrand() % range;
        buffer[i] = 'a' + r;
    }
    return nread;
}

ssize_t RandomDevice::write(Process&, const byte*, size_t bufferSize)
{
    // FIXME: Use input for entropy? I guess that could be a neat feature?
    return min(GoodBufferSize, bufferSize);
}