Skip to content

Commit

Permalink
Make BufferedInStream allocation more available
Browse files Browse the repository at this point in the history
Allow subclasses to call it, instead of it being strictly internal. This
is useful when a subclass can only provide data in minimum sized chunks.
  • Loading branch information
CendioOssman committed Aug 25, 2022
1 parent 62965aa commit 0ae1557
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 24 deletions.
18 changes: 16 additions & 2 deletions common/rdr/BufferedInStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <config.h>
#endif

#include <assert.h>

#include <rdr/BufferedInStream.h>
#include <rdr/Exception.h>

Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
7 changes: 6 additions & 1 deletion common/rdr/BufferedInStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions common/rdr/FdInStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion common/rdr/FdInStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions common/rdr/FileInStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion common/rdr/FileInStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace rdr {
~FileInStream(void);

private:
virtual bool fillBuffer(size_t maxSize);
virtual bool fillBuffer();

private:
FILE *file;
Expand Down
4 changes: 2 additions & 2 deletions common/rdr/HexInStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ bool HexInStream::hexStrToBin(const char* s, char** data, size_t* length) {
}


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;
Expand Down
2 changes: 1 addition & 1 deletion common/rdr/HexInStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions common/rdr/RandomStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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));
}

Expand Down
2 changes: 1 addition & 1 deletion common/rdr/RandomStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace rdr {
virtual ~RandomStream();

private:
virtual bool fillBuffer(size_t maxSize);
virtual bool fillBuffer();

private:
static unsigned int seed;
Expand Down
4 changes: 2 additions & 2 deletions common/rdr/TLSInStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion common/rdr/TLSInStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions common/rdr/ZlibInStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion common/rdr/ZlibInStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace rdr {
void init();
void deinit();

virtual bool fillBuffer(size_t maxSize);
virtual bool fillBuffer();

private:
InStream* underlying;
Expand Down

0 comments on commit 0ae1557

Please sign in to comment.