aboutsummaryrefslogtreecommitdiffstats
path: root/common/rdr/RandomStream.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'common/rdr/RandomStream.cxx')
-rw-r--r--common/rdr/RandomStream.cxx44
1 files changed, 8 insertions, 36 deletions
diff --git a/common/rdr/RandomStream.cxx b/common/rdr/RandomStream.cxx
index 0290c24d..6333be3f 100644
--- a/common/rdr/RandomStream.cxx
+++ b/common/rdr/RandomStream.cxx
@@ -35,15 +35,10 @@ static rfb::LogWriter vlog("RandomStream");
using namespace rdr;
-const size_t DEFAULT_BUF_LEN = 256;
-
unsigned int RandomStream::seed;
RandomStream::RandomStream()
- : offset(0)
{
- ptr = end = start = new U8[DEFAULT_BUF_LEN];
-
#ifdef RFB_HAVE_WINCRYPT
provider = 0;
if (!CryptAcquireContext(&provider, 0, 0, PROV_RSA_FULL, 0)) {
@@ -75,8 +70,6 @@ RandomStream::RandomStream()
}
RandomStream::~RandomStream() {
- delete [] start;
-
#ifdef RFB_HAVE_WINCRYPT
if (provider)
CryptReleaseContext(provider, 0);
@@ -86,50 +79,29 @@ RandomStream::~RandomStream() {
#endif
}
-size_t RandomStream::pos() {
- return offset + ptr - start;
-}
-
-size_t RandomStream::overrun(size_t itemSize, size_t nItems, bool wait) {
- if (itemSize > DEFAULT_BUF_LEN)
- throw Exception("RandomStream overrun: max itemSize exceeded");
-
- if (end - ptr != 0)
- memmove(start, ptr, end - ptr);
-
- end -= ptr - start;
- offset += ptr - start;
- ptr = start;
-
- size_t length = start + DEFAULT_BUF_LEN - end;
-
+bool RandomStream::fillBuffer(size_t maxSize, bool wait) {
#ifdef RFB_HAVE_WINCRYPT
if (provider) {
- if (!CryptGenRandom(provider, length, (U8*)end))
+ if (!CryptGenRandom(provider, maxSize, (U8*)end))
throw rdr::SystemException("unable to CryptGenRandom", GetLastError());
- end += length;
+ end += maxSize;
} else {
#else
#ifndef WIN32
if (fp) {
- size_t n = fread((U8*)end, length, 1, fp);
- if (n != 1)
+ size_t n = fread((U8*)end, 1, maxSize, fp);
+ if (n <= 0)
throw rdr::SystemException("reading /dev/urandom or /dev/random failed",
errno);
- end += length;
+ end += n;
} else {
#else
{
#endif
#endif
- for (size_t i=0; i<length; i++)
+ for (size_t i=0; i<maxSize; i++)
*(U8*)end++ = (int) (256.0*rand()/(RAND_MAX+1.0));
}
- size_t nAvail;
- nAvail = avail() / itemSize;
- if (nAvail < nItems)
- return nAvail;
-
- return nItems;
+ return true;
}