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);
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;
}
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;
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;
ptr = sentUpTo = start;
}
}
-
- size_t nAvail;
- nAvail = avail() / itemSize;
- if (nAvail < nItems)
- return nAvail;
-
- return nItems;
}
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;
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();
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;
}
private:
void writeBuffer();
- size_t overrun(size_t itemSize, size_t nItems);
+ virtual void overrun(size_t needed);
OutStream& out_stream;
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.
+ // 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 > avail())
- return overrun(itemSize, nItems, wait);
-
- nAvail = avail() / itemSize;
- if (nAvail < nItems)
- return nAvail;
-
- return nItems;
+ return true;
}
// checkNoWait() tries to make sure that the given number of bytes can
// be read without blocking. It returns true if this is the case, false
// otherwise. The length must be "small" (less than the buffer size).
- inline bool checkNoWait(size_t length) { return 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.
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:
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;
};
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;
delete [] start;
start = newStart;
end = newStart + len;
-
- return nItems;
}
U8* start;
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.
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;
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;
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:
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;
}
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;
}
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)
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);
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();
ptr = start;
}
-size_t ZlibOutStream::overrun(size_t itemSize, size_t nItems)
+void ZlibOutStream::overrun(size_t needed)
{
#ifdef ZLIBOUT_DEBUG
vlog.debug("overrun");
#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;
ptr -= zs->next_in - start;
}
}
-
- size_t nAvail;
- nAvail = avail() / itemSize;
- if (nAvail < nItems)
- return nAvail;
-
- return nItems;
}
void ZlibOutStream::deflate(int flush)
private:
- size_t overrun(size_t itemSize, size_t nItems);
+ virtual void overrun(size_t needed);
void deflate(int flush);
void checkCompressionLevel();
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();
inline rdr::U8* getstart() { return start; }
- inline int overrun(int itemSize, int nItems) {
- return MemOutStream::overrun(itemSize, nItems);
+ inline virtual void overrun(int needed) {
+ return MemOutStream::overrun(needed);
}
private:
virtual void flush();
private:
- virtual size_t overrun(size_t itemSize, size_t nItems);
+ virtual void overrun(size_t needed);
int offset;
rdr::U8 buf[131072];
ptr = buf;
}
-size_t DummyOutStream::overrun(size_t itemSize, size_t nItems)
+void DummyOutStream::overrun(size_t needed)
{
flush();
- if (itemSize * nItems > avail())
- nItems = avail() / itemSize;
- return nItems;
}
CConn::CConn(const char *filename)
virtual void flush();
private:
- virtual size_t overrun(size_t itemSize, size_t nItems);
+ virtual void overrun(size_t needed);
int offset;
rdr::U8 buf[131072];
ptr = buf;
}
-size_t DummyOutStream::overrun(size_t itemSize, size_t nItems)
+void DummyOutStream::overrun(size_t needed)
{
flush();
- if (itemSize * nItems > avail())
- nItems = avail() / itemSize;
- return nItems;
}
CConn::CConn(const char *filename)