serenity/Kernel/Devices/RandomDevice.cpp

50 lines
925 B
C++
Raw Normal View History

2018-10-15 00:44:54 +02:00
#include "RandomDevice.h"
#include <AK/StdLibExtras.h>
2018-10-15 00:44:54 +02:00
RandomDevice::RandomDevice()
: CharacterDevice(1, 8)
2018-10-15 00:44:54 +02:00
{
}
RandomDevice::~RandomDevice()
{
}
static u32 next = 1;
2018-10-15 00:44:54 +02:00
#define MY_RAND_MAX 4294967295U
u32 RandomDevice::random_value()
2018-10-15 00:44:54 +02:00
{
next = next * 1103515245 + 12345;
return next;
}
#if 0
2018-10-15 00:44:54 +02:00
static void mysrand(unsigned seed)
{
next = seed;
}
#endif
2018-10-15 00:44:54 +02:00
bool RandomDevice::can_read(const FileDescription&) const
{
return true;
}
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);
for (ssize_t i = 0; i < nread; ++i) {
u32 r = random_value() % range;
buffer[i] = (u8)('a' + r);
2018-10-15 00:44:54 +02:00
}
return nread;
}
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
}