2018-10-15 01:44:54 +03:00
|
|
|
#include "RandomDevice.h"
|
|
|
|
#include "Limits.h"
|
|
|
|
#include <AK/StdLib.h>
|
|
|
|
|
|
|
|
RandomDevice::RandomDevice()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:33:16 +03:00
|
|
|
#if 0
|
2018-10-15 01:44:54 +03:00
|
|
|
static void mysrand(unsigned seed)
|
|
|
|
{
|
|
|
|
next = seed;
|
|
|
|
}
|
2018-10-16 15:33:16 +03:00
|
|
|
#endif
|
2018-10-15 01:44:54 +03:00
|
|
|
|
2018-10-25 14:07:59 +03:00
|
|
|
bool RandomDevice::hasDataAvailableForRead() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-15 01:44:54 +03:00
|
|
|
Unix::ssize_t RandomDevice::read(byte* buffer, Unix::size_t bufferSize)
|
|
|
|
{
|
|
|
|
const int range = 'z' - 'a';
|
|
|
|
Unix::ssize_t nread = min(bufferSize, GoodBufferSize);
|
|
|
|
for (Unix::ssize_t i = 0; i < nread; ++i) {
|
2018-10-23 01:35:11 +03:00
|
|
|
dword r = myrand() % range;
|
2018-10-15 01:44:54 +03:00
|
|
|
buffer[i] = 'a' + r;
|
|
|
|
}
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
Unix::ssize_t RandomDevice::write(const byte*, Unix::size_t bufferSize)
|
|
|
|
{
|
|
|
|
// FIXME: Use input for entropy? I guess that could be a neat feature?
|
|
|
|
return min(GoodBufferSize, bufferSize);
|
|
|
|
}
|
|
|
|
|