mirror of
https://github.com/kasmtech/KasmVNC.git
synced 2025-02-02 11:39:12 +01:00
Simplify stream availability handling
Just have a simply number of bytes argument to avoid a lot of complexity.
This commit is contained in:
parent
92c7695981
commit
57a3c3bba8
@ -44,12 +44,12 @@ size_t BufferedInStream::pos()
|
||||
return offset + ptr - start;
|
||||
}
|
||||
|
||||
size_t BufferedInStream::overrun(size_t itemSize, size_t nItems, bool wait)
|
||||
bool BufferedInStream::overrun(size_t needed, bool wait)
|
||||
{
|
||||
if (itemSize > bufSize)
|
||||
if (needed > bufSize)
|
||||
throw Exception("BufferedInStream overrun: "
|
||||
"requested size of %lu bytes exceeds maximum of %lu bytes",
|
||||
(long unsigned)itemSize, (long unsigned)bufSize);
|
||||
(long unsigned)needed, (long unsigned)bufSize);
|
||||
|
||||
if (end - ptr != 0)
|
||||
memmove(start, ptr, end - ptr);
|
||||
@ -58,15 +58,10 @@ size_t BufferedInStream::overrun(size_t itemSize, size_t nItems, bool wait)
|
||||
end -= ptr - start;
|
||||
ptr = start;
|
||||
|
||||
while (avail() < itemSize) {
|
||||
while (avail() < needed) {
|
||||
if (!fillBuffer(start + bufSize - end, wait))
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t nAvail;
|
||||
nAvail = avail() / itemSize;
|
||||
if (nAvail < nItems)
|
||||
return nAvail;
|
||||
|
||||
return nItems;
|
||||
return true;
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ namespace rdr {
|
||||
private:
|
||||
virtual bool fillBuffer(size_t maxSize, bool wait) = 0;
|
||||
|
||||
virtual size_t overrun(size_t itemSize, size_t nItems, bool wait);
|
||||
virtual bool overrun(size_t needed, bool wait);
|
||||
|
||||
private:
|
||||
size_t bufSize;
|
||||
|
@ -71,22 +71,22 @@ void BufferedOutStream::flush()
|
||||
ptr = sentUpTo = start;
|
||||
}
|
||||
|
||||
size_t BufferedOutStream::overrun(size_t itemSize, size_t nItems)
|
||||
void BufferedOutStream::overrun(size_t needed)
|
||||
{
|
||||
if (itemSize > bufSize)
|
||||
if (needed > bufSize)
|
||||
throw Exception("BufferedOutStream overrun: "
|
||||
"requested size of %lu bytes exceeds maximum of %lu bytes",
|
||||
(long unsigned)itemSize, (long unsigned)bufSize);
|
||||
(long unsigned)needed, (long unsigned)bufSize);
|
||||
|
||||
// First try to get rid of the data we have
|
||||
flush();
|
||||
|
||||
// Still not enough space?
|
||||
while (itemSize > avail()) {
|
||||
while (needed > avail()) {
|
||||
// Can we shuffle things around?
|
||||
// (don't do this if it gains us less than 25%)
|
||||
if (((size_t)(sentUpTo - start) > bufSize / 4) &&
|
||||
(itemSize < bufSize - (ptr - sentUpTo))) {
|
||||
(needed < bufSize - (ptr - sentUpTo))) {
|
||||
memmove(start, sentUpTo, ptr - sentUpTo);
|
||||
ptr = start + (ptr - sentUpTo);
|
||||
sentUpTo = start;
|
||||
@ -105,11 +105,4 @@ size_t BufferedOutStream::overrun(size_t itemSize, size_t nItems)
|
||||
ptr = sentUpTo = start;
|
||||
}
|
||||
}
|
||||
|
||||
size_t nAvail;
|
||||
nAvail = avail() / itemSize;
|
||||
if (nAvail < nItems)
|
||||
return nAvail;
|
||||
|
||||
return nItems;
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ namespace rdr {
|
||||
|
||||
virtual bool flushBuffer(bool wait) = 0;
|
||||
|
||||
virtual size_t overrun(size_t itemSize, size_t nItems);
|
||||
virtual void overrun(size_t needed);
|
||||
|
||||
private:
|
||||
size_t bufSize;
|
||||
|
@ -73,7 +73,7 @@ decodeError:
|
||||
|
||||
|
||||
bool HexInStream::fillBuffer(size_t maxSize, bool wait) {
|
||||
if (!in_stream.check(2, 1, wait))
|
||||
if (!in_stream.check(2, wait))
|
||||
return false;
|
||||
|
||||
const U8* iptr = in_stream.getptr();
|
||||
|
@ -95,18 +95,10 @@ HexOutStream::flush() {
|
||||
out_stream.flush();
|
||||
}
|
||||
|
||||
size_t
|
||||
HexOutStream::overrun(size_t itemSize, size_t nItems) {
|
||||
if (itemSize > bufSize)
|
||||
throw Exception("HexOutStream overrun: max itemSize exceeded");
|
||||
void HexOutStream::overrun(size_t needed) {
|
||||
if (needed > bufSize)
|
||||
throw Exception("HexOutStream overrun: buffer size exceeded");
|
||||
|
||||
writeBuffer();
|
||||
|
||||
size_t nAvail;
|
||||
nAvail = avail() / itemSize;
|
||||
if (nAvail < nItems)
|
||||
return nAvail;
|
||||
|
||||
return nItems;
|
||||
}
|
||||
|
||||
|
@ -37,7 +37,7 @@ namespace rdr {
|
||||
|
||||
private:
|
||||
void writeBuffer();
|
||||
size_t overrun(size_t itemSize, size_t nItems);
|
||||
virtual void overrun(size_t needed);
|
||||
|
||||
OutStream& out_stream;
|
||||
|
||||
|
@ -43,28 +43,17 @@ namespace rdr {
|
||||
return end - ptr;
|
||||
}
|
||||
|
||||
// check() ensures there is buffer data for at least one item of size
|
||||
// itemSize bytes. Returns the number of items in the buffer (up to a
|
||||
// maximum of nItems). If wait is false, then instead of blocking to wait
|
||||
// for the bytes, zero is returned if the bytes are not immediately
|
||||
// available. If itemSize or nItems is zero, check() will return zero.
|
||||
// check() ensures there is buffer data for at least needed bytes. Returns
|
||||
// true once the data is available. If wait is false, then instead of
|
||||
// blocking to wait for the bytes, false is returned if the bytes are not
|
||||
// immediately available.
|
||||
|
||||
inline size_t check(size_t itemSize, size_t nItems=1, bool wait=true)
|
||||
inline size_t check(size_t needed, bool wait=true)
|
||||
{
|
||||
size_t nAvail;
|
||||
if (needed > avail())
|
||||
return overrun(needed, wait);
|
||||
|
||||
if (itemSize == 0 || nItems == 0)
|
||||
return 0;
|
||||
|
||||
if (itemSize > avail())
|
||||
return overrun(itemSize, nItems, wait);
|
||||
|
||||
// itemSize cannot be zero at this point
|
||||
nAvail = avail() / itemSize;
|
||||
if (nAvail < nItems)
|
||||
return nAvail;
|
||||
|
||||
return nItems;
|
||||
return true;
|
||||
}
|
||||
|
||||
// checkNoWait() tries to make sure that the given number of bytes can
|
||||
@ -72,10 +61,7 @@ namespace rdr {
|
||||
// otherwise. The length must be "small" (less than the buffer size).
|
||||
// If length is zero, checkNoWait() will return true.
|
||||
|
||||
inline bool checkNoWait(size_t length)
|
||||
{
|
||||
return length == 0 || check(length, 1, false) > 0;
|
||||
}
|
||||
inline bool checkNoWait(size_t length) { return check(length, false); }
|
||||
|
||||
// readU/SN() methods read unsigned and signed N-bit integers.
|
||||
|
||||
@ -146,13 +132,12 @@ namespace rdr {
|
||||
private:
|
||||
|
||||
// overrun() is implemented by a derived class to cope with buffer overrun.
|
||||
// It ensures there are at least itemSize bytes of buffer data. Returns
|
||||
// the number of items in the buffer (up to a maximum of nItems). itemSize
|
||||
// is supposed to be "small" (a few bytes). If wait is false, then
|
||||
// instead of blocking to wait for the bytes, zero is returned if the bytes
|
||||
// are not immediately available.
|
||||
// It ensures there are at least needed bytes of buffer data. Returns true
|
||||
// once the data is available. If wait is false, then instead of blocking
|
||||
// to wait for the bytes, false is returned if the bytes are not
|
||||
// immediately available.
|
||||
|
||||
virtual size_t overrun(size_t itemSize, size_t nItems, bool wait=true) = 0;
|
||||
virtual bool overrun(size_t needed, bool wait=true) = 0;
|
||||
|
||||
protected:
|
||||
|
||||
|
@ -53,7 +53,7 @@ namespace rdr {
|
||||
|
||||
private:
|
||||
|
||||
size_t overrun(size_t itemSize, size_t nItems, bool wait) { throw EndOfStream(); }
|
||||
bool overrun(size_t needed, bool wait) { throw EndOfStream(); }
|
||||
const U8* start;
|
||||
bool deleteWhenDone;
|
||||
};
|
||||
|
@ -52,11 +52,11 @@ namespace rdr {
|
||||
|
||||
protected:
|
||||
|
||||
// overrun() either doubles the buffer or adds enough space for nItems of
|
||||
// size itemSize bytes.
|
||||
// overrun() either doubles the buffer or adds enough space for
|
||||
// needed bytes.
|
||||
|
||||
size_t overrun(size_t itemSize, size_t nItems) {
|
||||
size_t len = ptr - start + itemSize * nItems;
|
||||
virtual void overrun(size_t needed) {
|
||||
size_t len = ptr - start + needed;
|
||||
if (len < (size_t)(end - start) * 2)
|
||||
len = (end - start) * 2;
|
||||
|
||||
@ -69,8 +69,6 @@ namespace rdr {
|
||||
delete [] start;
|
||||
start = newStart;
|
||||
end = newStart + len;
|
||||
|
||||
return nItems;
|
||||
}
|
||||
|
||||
U8* start;
|
||||
|
@ -48,22 +48,12 @@ namespace rdr {
|
||||
return end - ptr;
|
||||
}
|
||||
|
||||
// check() ensures there is buffer space for at least one item of size
|
||||
// itemSize bytes. Returns the number of items which fit (up to a maximum
|
||||
// of nItems).
|
||||
// check() ensures there is buffer space for at least needed bytes.
|
||||
|
||||
inline size_t check(size_t itemSize, size_t nItems=1)
|
||||
inline void check(size_t needed)
|
||||
{
|
||||
size_t nAvail;
|
||||
|
||||
if (itemSize > avail())
|
||||
return overrun(itemSize, nItems);
|
||||
|
||||
nAvail = avail() / itemSize;
|
||||
if (nAvail < nItems)
|
||||
return nAvail;
|
||||
|
||||
return nItems;
|
||||
if (needed > avail())
|
||||
overrun(needed);
|
||||
}
|
||||
|
||||
// writeU/SN() methods write unsigned and signed N-bit integers.
|
||||
@ -91,19 +81,14 @@ namespace rdr {
|
||||
while (bytes-- > 0) writeU8(0);
|
||||
}
|
||||
|
||||
inline void skip(size_t bytes) {
|
||||
while (bytes > 0) {
|
||||
size_t n = check(1, bytes);
|
||||
ptr += n;
|
||||
bytes -= n;
|
||||
}
|
||||
}
|
||||
|
||||
// writeBytes() writes an exact number of bytes.
|
||||
|
||||
void writeBytes(const void* data, size_t length) {
|
||||
while (length > 0) {
|
||||
size_t n = check(1, length);
|
||||
check(1);
|
||||
size_t n = length;
|
||||
if (length > avail())
|
||||
n = avail();
|
||||
memcpy(ptr, data, n);
|
||||
ptr += n;
|
||||
data = (U8*)data + n;
|
||||
@ -115,7 +100,10 @@ namespace rdr {
|
||||
|
||||
void copyBytes(InStream* is, size_t length) {
|
||||
while (length > 0) {
|
||||
size_t n = check(1, length);
|
||||
check(1);
|
||||
size_t n = length;
|
||||
if (length > avail())
|
||||
n = avail();
|
||||
is->readBytes(ptr, n);
|
||||
ptr += n;
|
||||
length -= n;
|
||||
@ -151,11 +139,9 @@ namespace rdr {
|
||||
private:
|
||||
|
||||
// overrun() is implemented by a derived class to cope with buffer overrun.
|
||||
// It ensures there are at least itemSize bytes of buffer space. Returns
|
||||
// the number of items which fit (up to a maximum of nItems). itemSize is
|
||||
// supposed to be "small" (a few bytes).
|
||||
// It ensures there are at least needed bytes of buffer space.
|
||||
|
||||
virtual size_t overrun(size_t itemSize, size_t nItems) = 0;
|
||||
virtual void overrun(size_t needed) = 0;
|
||||
|
||||
protected:
|
||||
|
||||
|
@ -36,7 +36,7 @@ ssize_t TLSInStream::pull(gnutls_transport_ptr_t str, void* data, size_t size)
|
||||
InStream *in = self->in;
|
||||
|
||||
try {
|
||||
if (!in->check(1, 1, false)) {
|
||||
if (!in->check(1, false)) {
|
||||
gnutls_transport_set_errno(self->session, EAGAIN);
|
||||
return -1;
|
||||
}
|
||||
@ -84,7 +84,7 @@ size_t TLSInStream::readTLS(U8* buf, size_t len, bool wait)
|
||||
int n;
|
||||
|
||||
if (gnutls_record_check_pending(session) == 0) {
|
||||
n = in->check(1, 1, wait);
|
||||
n = in->check(1, wait);
|
||||
if (n == 0)
|
||||
return 0;
|
||||
}
|
||||
|
@ -93,19 +93,12 @@ void TLSOutStream::flush()
|
||||
out->flush();
|
||||
}
|
||||
|
||||
size_t TLSOutStream::overrun(size_t itemSize, size_t nItems)
|
||||
void TLSOutStream::overrun(size_t needed)
|
||||
{
|
||||
if (itemSize > bufSize)
|
||||
throw Exception("TLSOutStream overrun: max itemSize exceeded");
|
||||
if (needed > bufSize)
|
||||
throw Exception("TLSOutStream overrun: buffer size exceeded");
|
||||
|
||||
flush();
|
||||
|
||||
size_t nAvail;
|
||||
nAvail = avail() / itemSize;
|
||||
if (nAvail < nItems)
|
||||
return nAvail;
|
||||
|
||||
return nItems;
|
||||
}
|
||||
|
||||
size_t TLSOutStream::writeTLS(const U8* data, size_t length)
|
||||
|
@ -39,7 +39,7 @@ namespace rdr {
|
||||
size_t length();
|
||||
|
||||
protected:
|
||||
size_t overrun(size_t itemSize, size_t nItems);
|
||||
virtual void overrun(size_t needed);
|
||||
|
||||
private:
|
||||
size_t writeTLS(const U8* data, size_t length);
|
||||
|
@ -94,7 +94,7 @@ bool ZlibInStream::fillBuffer(size_t maxSize, bool wait)
|
||||
zs->next_out = (U8*)end;
|
||||
zs->avail_out = maxSize;
|
||||
|
||||
size_t n = underlying->check(1, 1, wait);
|
||||
size_t n = underlying->check(1, wait);
|
||||
if (n == 0) return false;
|
||||
zs->next_in = (U8*)underlying->getptr();
|
||||
zs->avail_in = underlying->avail();
|
||||
|
@ -95,18 +95,18 @@ void ZlibOutStream::flush()
|
||||
ptr = start;
|
||||
}
|
||||
|
||||
size_t ZlibOutStream::overrun(size_t itemSize, size_t nItems)
|
||||
void ZlibOutStream::overrun(size_t needed)
|
||||
{
|
||||
#ifdef ZLIBOUT_DEBUG
|
||||
fprintf(stderr,"zos overrun\n");
|
||||
#endif
|
||||
|
||||
if (itemSize > bufSize)
|
||||
throw Exception("ZlibOutStream overrun: max itemSize exceeded");
|
||||
if (needed > bufSize)
|
||||
throw Exception("ZlibOutStream overrun: buffer size exceeded");
|
||||
|
||||
checkCompressionLevel();
|
||||
|
||||
while (avail() < itemSize) {
|
||||
while (avail() < needed) {
|
||||
zs->next_in = start;
|
||||
zs->avail_in = ptr - start;
|
||||
|
||||
@ -126,13 +126,6 @@ size_t ZlibOutStream::overrun(size_t itemSize, size_t nItems)
|
||||
ptr -= zs->next_in - start;
|
||||
}
|
||||
}
|
||||
|
||||
size_t nAvail;
|
||||
nAvail = avail() / itemSize;
|
||||
if (nAvail < nItems)
|
||||
return nAvail;
|
||||
|
||||
return nItems;
|
||||
}
|
||||
|
||||
void ZlibOutStream::deflate(int flush)
|
||||
|
@ -45,7 +45,7 @@ namespace rdr {
|
||||
|
||||
private:
|
||||
|
||||
size_t overrun(size_t itemSize, size_t nItems);
|
||||
virtual void overrun(size_t needed);
|
||||
void deflate(int flush);
|
||||
void checkCompressionLevel();
|
||||
|
||||
|
@ -57,7 +57,7 @@ void CMsgWriter::writeSetPixelFormat(const PixelFormat& pf)
|
||||
void CMsgWriter::writeSetEncodings(int nEncodings, rdr::U32* encodings)
|
||||
{
|
||||
startMsg(msgTypeSetEncodings);
|
||||
os->skip(1);
|
||||
os->pad(1);
|
||||
os->writeU16(nEncodings);
|
||||
for (int i = 0; i < nEncodings; i++)
|
||||
os->writeU32(encodings[i]);
|
||||
|
@ -95,7 +95,7 @@ JpegEmptyOutputBuffer(j_compress_ptr cinfo)
|
||||
JpegCompressor *jc = dest->instance;
|
||||
|
||||
jc->setptr(jc->getend());
|
||||
jc->overrun(jc->getend() - jc->getstart(), 1);
|
||||
jc->overrun(jc->getend() - jc->getstart());
|
||||
dest->pub.next_output_byte = jc->getptr();
|
||||
dest->pub.free_in_buffer = jc->avail();
|
||||
|
||||
|
@ -49,8 +49,8 @@ namespace rfb {
|
||||
|
||||
inline rdr::U8* getstart() { return start; }
|
||||
|
||||
virtual inline size_t overrun(size_t itemSize, size_t nItems) {
|
||||
return MemOutStream::overrun(itemSize, nItems);
|
||||
inline virtual void overrun(int needed) {
|
||||
return MemOutStream::overrun(needed);
|
||||
}
|
||||
|
||||
private:
|
||||
|
Loading…
Reference in New Issue
Block a user