summaryrefslogtreecommitdiff
path: root/VirtualFileSystem/RandomDevice.cpp
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2018-10-15 00:44:54 +0200
committerAndreas Kling <awesomekling@gmail.com>2018-10-15 00:44:54 +0200
commit05b088ee2f033120e7f1299a1c7ec123fc3c57c9 (patch)
treea5cd13a570251766fe0ec6a55a5166e013a40366 /VirtualFileSystem/RandomDevice.cpp
parent9528edab9225fc506af7fcffe41ce1f33826a50f (diff)
downloadserenity-05b088ee2f033120e7f1299a1c7ec123fc3c57c9.zip
Add a simple /dev/random.
Diffstat (limited to 'VirtualFileSystem/RandomDevice.cpp')
-rw-r--r--VirtualFileSystem/RandomDevice.cpp47
1 files changed, 47 insertions, 0 deletions
diff --git a/VirtualFileSystem/RandomDevice.cpp b/VirtualFileSystem/RandomDevice.cpp
new file mode 100644
index 0000000000..a47dc246dc
--- /dev/null
+++ b/VirtualFileSystem/RandomDevice.cpp
@@ -0,0 +1,47 @@
+#include "RandomDevice.h"
+#include "Limits.h"
+#include <AK/StdLib.h>
+#include <cstring>
+#include <cstdio>
+
+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));
+}
+
+static void mysrand(unsigned seed)
+{
+ next = seed;
+}
+
+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) {
+ double r = ((double)myrand() / (double)MY_RAND_MAX) * (double)range;
+ 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);
+}
+