aboutsummaryrefslogtreecommitdiffstats
path: root/common/rdr
diff options
context:
space:
mode:
Diffstat (limited to 'common/rdr')
-rw-r--r--common/rdr/BufferedInStream.cxx18
-rw-r--r--common/rdr/BufferedInStream.h7
-rw-r--r--common/rdr/FdInStream.cxx4
-rw-r--r--common/rdr/FdInStream.h2
-rw-r--r--common/rdr/FileInStream.cxx4
-rw-r--r--common/rdr/FileInStream.h2
-rw-r--r--common/rdr/HexInStream.cxx4
-rw-r--r--common/rdr/HexInStream.h2
-rw-r--r--common/rdr/RandomStream.cxx10
-rw-r--r--common/rdr/RandomStream.h2
-rw-r--r--common/rdr/TLSInStream.cxx4
-rw-r--r--common/rdr/TLSInStream.h2
-rw-r--r--common/rdr/ZlibInStream.cxx4
-rw-r--r--common/rdr/ZlibInStream.h2
14 files changed, 43 insertions, 24 deletions
diff --git a/common/rdr/BufferedInStream.cxx b/common/rdr/BufferedInStream.cxx
index d322793d..9972e13e 100644
--- a/common/rdr/BufferedInStream.cxx
+++ b/common/rdr/BufferedInStream.cxx
@@ -21,6 +21,8 @@
#include <config.h>
#endif
+#include <assert.h>
+
#include <rdr/BufferedInStream.h>
#include <rdr/Exception.h>
@@ -47,10 +49,15 @@ size_t BufferedInStream::pos()
return offset + ptr - start;
}
-bool BufferedInStream::overrun(size_t needed)
+void BufferedInStream::ensureSpace(size_t needed)
{
struct timeval now;
+ // Given argument is how much free space is needed, but for allocation
+ // purposes we need to now how much space everything needs, including
+ // any existing data already in the buffer
+ needed += avail();
+
if (needed > bufSize) {
size_t newSize;
U8* newBuffer;
@@ -110,9 +117,16 @@ bool BufferedInStream::overrun(size_t needed)
end -= ptr - start;
ptr = start;
}
+}
+
+bool BufferedInStream::overrun(size_t needed)
+{
+ // Make sure fillBuffer() has room for all the requested data
+ assert(needed > avail());
+ ensureSpace(needed - avail());
while (avail() < needed) {
- if (!fillBuffer(start + bufSize - end))
+ if (!fillBuffer())
return false;
}
diff --git a/common/rdr/BufferedInStream.h b/common/rdr/BufferedInStream.h
index 84405255..05f7231e 100644
--- a/common/rdr/BufferedInStream.h
+++ b/common/rdr/BufferedInStream.h
@@ -37,8 +37,13 @@ namespace rdr {
virtual size_t pos();
+ protected:
+ size_t availSpace() { return start + bufSize - end; }
+
+ void ensureSpace(size_t needed);
+
private:
- virtual bool fillBuffer(size_t maxSize) = 0;
+ virtual bool fillBuffer() = 0;
virtual bool overrun(size_t needed);
diff --git a/common/rdr/FdInStream.cxx b/common/rdr/FdInStream.cxx
index ecc34ecd..e0174a8f 100644
--- a/common/rdr/FdInStream.cxx
+++ b/common/rdr/FdInStream.cxx
@@ -57,9 +57,9 @@ FdInStream::~FdInStream()
}
-bool FdInStream::fillBuffer(size_t maxSize)
+bool FdInStream::fillBuffer()
{
- size_t n = readFd((U8*)end, maxSize);
+ size_t n = readFd((U8*)end, availSpace());
if (n == 0)
return false;
end += n;
diff --git a/common/rdr/FdInStream.h b/common/rdr/FdInStream.h
index 0ae419a9..71370a59 100644
--- a/common/rdr/FdInStream.h
+++ b/common/rdr/FdInStream.h
@@ -37,7 +37,7 @@ namespace rdr {
int getFd() { return fd; }
private:
- virtual bool fillBuffer(size_t maxSize);
+ virtual bool fillBuffer();
size_t readFd(void* buf, size_t len);
diff --git a/common/rdr/FileInStream.cxx b/common/rdr/FileInStream.cxx
index 198b9ab9..43f9ea4c 100644
--- a/common/rdr/FileInStream.cxx
+++ b/common/rdr/FileInStream.cxx
@@ -43,9 +43,9 @@ FileInStream::~FileInStream(void) {
}
}
-bool FileInStream::fillBuffer(size_t maxSize)
+bool FileInStream::fillBuffer()
{
- size_t n = fread((U8 *)end, 1, maxSize, file);
+ size_t n = fread((U8 *)end, 1, availSpace(), file);
if (n == 0) {
if (ferror(file))
throw SystemException("fread", errno);
diff --git a/common/rdr/FileInStream.h b/common/rdr/FileInStream.h
index 619397f0..e13596ce 100644
--- a/common/rdr/FileInStream.h
+++ b/common/rdr/FileInStream.h
@@ -34,7 +34,7 @@ namespace rdr {
~FileInStream(void);
private:
- virtual bool fillBuffer(size_t maxSize);
+ virtual bool fillBuffer();
private:
FILE *file;
diff --git a/common/rdr/HexInStream.cxx b/common/rdr/HexInStream.cxx
index 9f871f1a..3eac9d2e 100644
--- a/common/rdr/HexInStream.cxx
+++ b/common/rdr/HexInStream.cxx
@@ -76,11 +76,11 @@ decodeError:
}
-bool HexInStream::fillBuffer(size_t maxSize) {
+bool HexInStream::fillBuffer() {
if (!in_stream.hasData(2))
return false;
- size_t length = min(in_stream.avail()/2, maxSize);
+ size_t length = min(in_stream.avail()/2, availSpace());
const U8* iptr = in_stream.getptr(length*2);
U8* optr = (U8*) end;
diff --git a/common/rdr/HexInStream.h b/common/rdr/HexInStream.h
index bd7ca305..4ce6f7fd 100644
--- a/common/rdr/HexInStream.h
+++ b/common/rdr/HexInStream.h
@@ -33,7 +33,7 @@ namespace rdr {
static bool hexStrToBin(const char* s, char** data, size_t* length);
private:
- virtual bool fillBuffer(size_t maxSize);
+ virtual bool fillBuffer();
private:
InStream& in_stream;
diff --git a/common/rdr/RandomStream.cxx b/common/rdr/RandomStream.cxx
index ec367212..c9be704c 100644
--- a/common/rdr/RandomStream.cxx
+++ b/common/rdr/RandomStream.cxx
@@ -83,17 +83,17 @@ RandomStream::~RandomStream() {
#endif
}
-bool RandomStream::fillBuffer(size_t maxSize) {
+bool RandomStream::fillBuffer() {
#ifdef RFB_HAVE_WINCRYPT
if (provider) {
- if (!CryptGenRandom(provider, maxSize, (U8*)end))
+ if (!CryptGenRandom(provider, availSpace(), (U8*)end))
throw rdr::SystemException("unable to CryptGenRandom", GetLastError());
- end += maxSize;
+ end += availSpace();
} else {
#else
#ifndef WIN32
if (fp) {
- size_t n = fread((U8*)end, 1, maxSize, fp);
+ size_t n = fread((U8*)end, 1, availSpace(), fp);
if (n <= 0)
throw rdr::SystemException("reading /dev/urandom or /dev/random failed",
errno);
@@ -103,7 +103,7 @@ bool RandomStream::fillBuffer(size_t maxSize) {
{
#endif
#endif
- for (size_t i=0; i<maxSize; i++)
+ for (size_t i=availSpace(); i>0; i--)
*(U8*)end++ = (int) (256.0*rand()/(RAND_MAX+1.0));
}
diff --git a/common/rdr/RandomStream.h b/common/rdr/RandomStream.h
index 58986433..521012e0 100644
--- a/common/rdr/RandomStream.h
+++ b/common/rdr/RandomStream.h
@@ -40,7 +40,7 @@ namespace rdr {
virtual ~RandomStream();
private:
- virtual bool fillBuffer(size_t maxSize);
+ virtual bool fillBuffer();
private:
static unsigned int seed;
diff --git a/common/rdr/TLSInStream.cxx b/common/rdr/TLSInStream.cxx
index 014789e3..aa552e86 100644
--- a/common/rdr/TLSInStream.cxx
+++ b/common/rdr/TLSInStream.cxx
@@ -88,9 +88,9 @@ TLSInStream::~TLSInStream()
delete saved_exception;
}
-bool TLSInStream::fillBuffer(size_t maxSize)
+bool TLSInStream::fillBuffer()
{
- size_t n = readTLS((U8*) end, maxSize);
+ size_t n = readTLS((U8*) end, availSpace());
if (n == 0)
return false;
end += n;
diff --git a/common/rdr/TLSInStream.h b/common/rdr/TLSInStream.h
index 46779d4c..397a7b3d 100644
--- a/common/rdr/TLSInStream.h
+++ b/common/rdr/TLSInStream.h
@@ -33,7 +33,7 @@ namespace rdr {
virtual ~TLSInStream();
private:
- virtual bool fillBuffer(size_t maxSize);
+ virtual bool fillBuffer();
size_t readTLS(U8* buf, size_t len);
static ssize_t pull(gnutls_transport_ptr_t str, void* data, size_t size);
diff --git a/common/rdr/ZlibInStream.cxx b/common/rdr/ZlibInStream.cxx
index 377147ac..03df10cc 100644
--- a/common/rdr/ZlibInStream.cxx
+++ b/common/rdr/ZlibInStream.cxx
@@ -89,13 +89,13 @@ void ZlibInStream::deinit()
zs = NULL;
}
-bool ZlibInStream::fillBuffer(size_t maxSize)
+bool ZlibInStream::fillBuffer()
{
if (!underlying)
throw Exception("ZlibInStream overrun: no underlying stream");
zs->next_out = (U8*)end;
- zs->avail_out = maxSize;
+ zs->avail_out = availSpace();
if (!underlying->hasData(1))
return false;
diff --git a/common/rdr/ZlibInStream.h b/common/rdr/ZlibInStream.h
index 302c99d8..cce6a6e0 100644
--- a/common/rdr/ZlibInStream.h
+++ b/common/rdr/ZlibInStream.h
@@ -44,7 +44,7 @@ namespace rdr {
void init();
void deinit();
- virtual bool fillBuffer(size_t maxSize);
+ virtual bool fillBuffer();
private:
InStream* underlying;