2018-10-15 00:44:54 +02:00
|
|
|
#include "RandomDevice.h"
|
2018-12-04 00:27:16 +01:00
|
|
|
#include <AK/StdLibExtras.h>
|
2018-10-15 00:44:54 +02:00
|
|
|
|
|
|
|
RandomDevice::RandomDevice()
|
2018-10-30 13:59:29 +01:00
|
|
|
: CharacterDevice(1, 8)
|
2018-10-15 00:44:54 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
RandomDevice::~RandomDevice()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
static u32 next = 1;
|
2018-10-15 00:44:54 +02:00
|
|
|
|
2019-04-22 23:38:33 +02:00
|
|
|
#define MY_RAND_MAX 4294967295U
|
2019-07-03 21:17:35 +02:00
|
|
|
u32 RandomDevice::random_value()
|
2018-10-15 00:44:54 +02:00
|
|
|
{
|
|
|
|
next = next * 1103515245 + 12345;
|
2019-04-22 23:38:33 +02:00
|
|
|
return next;
|
2019-03-18 04:03:44 +01:00
|
|
|
}
|
|
|
|
|
2018-10-16 14:33:16 +02:00
|
|
|
#if 0
|
2018-10-15 00:44:54 +02:00
|
|
|
static void mysrand(unsigned seed)
|
|
|
|
{
|
|
|
|
next = seed;
|
|
|
|
}
|
2018-10-16 14:33:16 +02:00
|
|
|
#endif
|
2018-10-15 00:44:54 +02:00
|
|
|
|
2019-11-04 14:03:14 +01:00
|
|
|
bool RandomDevice::can_read(const FileDescription&) const
|
2018-10-25 13:07:59 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
ssize_t RandomDevice::read(FileDescription&, u8* buffer, ssize_t size)
|
2018-10-15 00:44:54 +02:00
|
|
|
{
|
|
|
|
const int range = 'z' - 'a';
|
2019-04-03 13:18:42 +02:00
|
|
|
ssize_t nread = min(size, PAGE_SIZE);
|
2018-12-02 23:34:50 +01:00
|
|
|
for (ssize_t i = 0; i < nread; ++i) {
|
2019-07-03 21:17:35 +02:00
|
|
|
u32 r = random_value() % range;
|
|
|
|
buffer[i] = (u8)('a' + r);
|
2018-10-15 00:44:54 +02:00
|
|
|
}
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
ssize_t RandomDevice::write(FileDescription&, const u8*, ssize_t size)
|
2018-10-15 00:44:54 +02:00
|
|
|
{
|
|
|
|
// FIXME: Use input for entropy? I guess that could be a neat feature?
|
2019-04-03 13:18:42 +02:00
|
|
|
return min(PAGE_SIZE, size);
|
2018-10-15 00:44:54 +02:00
|
|
|
}
|