From 05b088ee2f033120e7f1299a1c7ec123fc3c57c9 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 15 Oct 2018 00:44:54 +0200 Subject: [PATCH] Add a simple /dev/random. --- VirtualFileSystem/Makefile | 1 + VirtualFileSystem/RandomDevice.cpp | 47 +++++++++++++++++++++++++++++ VirtualFileSystem/RandomDevice.h | 13 ++++++++ VirtualFileSystem/small.fs | Bin 2048000 -> 2048000 bytes VirtualFileSystem/test.cpp | 5 ++- 5 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 VirtualFileSystem/RandomDevice.cpp create mode 100644 VirtualFileSystem/RandomDevice.h diff --git a/VirtualFileSystem/Makefile b/VirtualFileSystem/Makefile index 28994083b2..d6114a4ba2 100644 --- a/VirtualFileSystem/Makefile +++ b/VirtualFileSystem/Makefile @@ -22,6 +22,7 @@ VFS_OBJS = \ ZeroDevice.o \ NullDevice.o \ FullDevice.o \ + RandomDevice.o \ test.o OBJS = $(AK_OBJS) $(VFS_OBJS) 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 +#include +#include + +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); +} + diff --git a/VirtualFileSystem/RandomDevice.h b/VirtualFileSystem/RandomDevice.h new file mode 100644 index 0000000000..4a30c259fc --- /dev/null +++ b/VirtualFileSystem/RandomDevice.h @@ -0,0 +1,13 @@ +#pragma once + +#include "CharacterDevice.h" + +class RandomDevice final : public CharacterDevice { +public: + RandomDevice(); + virtual ~RandomDevice(); + + Unix::ssize_t read(byte* buffer, Unix::size_t bufferSize) override; + Unix::ssize_t write(const byte* buffer, Unix::size_t bufferSize) override; +}; + diff --git a/VirtualFileSystem/small.fs b/VirtualFileSystem/small.fs index 0eb8186ea16e243f596bf391fa166eaa34319f0e..f336606ddbb6c623d98fc619528aa35e65435ba3 100644 GIT binary patch delta 246 zcmZo@sBLJd-5|ii_;9iyi#)5>(ZkWqn@w46GBZBh%+K*fnX!H%qs(Rnoi_q(?ne(t zyC2;wWc!YBk`0FvgbNZn9L)d*j9?N*a4=3}v|>?WnVGs-k>kGnW&@53a6(dW(*~0S zF&+jM=CsnB9C-!?h9k^u%teWLDfzh@8MiUFi7>W_Ftv#=w~4T{iLkbbu(gS>w~27H ziEy@waJ7kWw~6qyiSV|G@U@BXw}}X}i3qld2(^g_w~2_fiHNp|h_#7`w~0u!iAc7I WNVSPbw~5HKiO9Bz$So6**8uTgBg}0gjBO%JZ6eHVA}nnptZgD} zZ6fS#A{=cZoNXdpZ6e%lB0Oy(ylo #include #include @@ -29,6 +30,9 @@ int main(int c, char** v) auto full = make(); vfs.registerCharacterDevice(1, 7, *full); + auto random = make(); + vfs.registerCharacterDevice(1, 8, *random); + if (!vfs.mountRoot(makeFileSystem(filename))) { printf("Failed to mount root :(\n"); return 1; @@ -187,7 +191,6 @@ int main(int c, char** v) byte buffer[512]; for (;;) { nread = handle->read(buffer, sizeof(buffer)); - printf("read() returned %d\n", nread); if (nread <= 0) break; fwrite(buffer, 1, nread, stdout);