RandomDevice.cpp 919 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "RandomDevice.h"
  2. #include <AK/StdLibExtras.h>
  3. RandomDevice::RandomDevice()
  4. : CharacterDevice(1, 8)
  5. {
  6. }
  7. RandomDevice::~RandomDevice()
  8. {
  9. }
  10. static u32 next = 1;
  11. #define MY_RAND_MAX 4294967295U
  12. u32 RandomDevice::random_value()
  13. {
  14. next = next * 1103515245 + 12345;
  15. return next;
  16. }
  17. #if 0
  18. static void mysrand(unsigned seed)
  19. {
  20. next = seed;
  21. }
  22. #endif
  23. bool RandomDevice::can_read(FileDescription&) const
  24. {
  25. return true;
  26. }
  27. ssize_t RandomDevice::read(FileDescription&, u8* buffer, ssize_t size)
  28. {
  29. const int range = 'z' - 'a';
  30. ssize_t nread = min(size, PAGE_SIZE);
  31. for (ssize_t i = 0; i < nread; ++i) {
  32. u32 r = random_value() % range;
  33. buffer[i] = (u8)('a' + r);
  34. }
  35. return nread;
  36. }
  37. ssize_t RandomDevice::write(FileDescription&, const u8*, ssize_t size)
  38. {
  39. // FIXME: Use input for entropy? I guess that could be a neat feature?
  40. return min(PAGE_SIZE, size);
  41. }