RandomDevice.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "RandomDevice.h"
  2. #include "Limits.h"
  3. #include <AK/StdLib.h>
  4. RandomDevice::RandomDevice()
  5. {
  6. }
  7. RandomDevice::~RandomDevice()
  8. {
  9. }
  10. // Simple rand() and srand() borrowed from the POSIX standard:
  11. static unsigned long next = 1;
  12. #define MY_RAND_MAX 32767
  13. static int myrand()
  14. {
  15. next = next * 1103515245 + 12345;
  16. return((unsigned)(next/((MY_RAND_MAX + 1) * 2)) % (MY_RAND_MAX + 1));
  17. }
  18. #if 0
  19. static void mysrand(unsigned seed)
  20. {
  21. next = seed;
  22. }
  23. #endif
  24. bool RandomDevice::hasDataAvailableForRead() const
  25. {
  26. return true;
  27. }
  28. Unix::ssize_t RandomDevice::read(byte* buffer, Unix::size_t bufferSize)
  29. {
  30. const int range = 'z' - 'a';
  31. Unix::ssize_t nread = min(bufferSize, GoodBufferSize);
  32. for (Unix::ssize_t i = 0; i < nread; ++i) {
  33. dword r = myrand() % range;
  34. buffer[i] = 'a' + r;
  35. }
  36. return nread;
  37. }
  38. Unix::ssize_t RandomDevice::write(const byte*, Unix::size_t bufferSize)
  39. {
  40. // FIXME: Use input for entropy? I guess that could be a neat feature?
  41. return min(GoodBufferSize, bufferSize);
  42. }