aboutsummaryrefslogtreecommitdiff
path: root/src/streams.h
diff options
context:
space:
mode:
authorLarry Ruane <larryruane@gmail.com>2019-09-10 07:53:09 -0600
committerLarry Ruane <larryruane@gmail.com>2019-09-10 07:53:09 -0600
commitefd2474d17098c754367b844ec646ebececc7c74 (patch)
tree9329d87a0c8507e8064b4a297a99d7eca07cfc43 /src/streams.h
parent1985c4efda56b48f6f9c04f39d69268ee8f0b40a (diff)
util: CBufferedFile fixes
Diffstat (limited to 'src/streams.h')
-rw-r--r--src/streams.h24
1 files changed, 13 insertions, 11 deletions
diff --git a/src/streams.h b/src/streams.h
index 4e600f1826..517eefc932 100644
--- a/src/streams.h
+++ b/src/streams.h
@@ -735,16 +735,17 @@ protected:
size_t nBytes = fread((void*)&vchBuf[pos], 1, readNow, src);
if (nBytes == 0) {
throw std::ios_base::failure(feof(src) ? "CBufferedFile::Fill: end of file" : "CBufferedFile::Fill: fread failed");
- } else {
- nSrcPos += nBytes;
- return true;
}
+ nSrcPos += nBytes;
+ return true;
}
public:
CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn) :
nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), nReadPos(0), nReadLimit(std::numeric_limits<uint64_t>::max()), nRewind(nRewindIn), vchBuf(nBufSize, 0)
{
+ if (nRewindIn >= nBufSize)
+ throw std::ios_base::failure("Rewind limit must be less than buffer size");
src = fileIn;
}
@@ -777,8 +778,6 @@ public:
void read(char *pch, size_t nSize) {
if (nSize + nReadPos > nReadLimit)
throw std::ios_base::failure("Read attempted past buffer limit");
- if (nSize + nRewind > vchBuf.size())
- throw std::ios_base::failure("Read larger than buffer size");
while (nSize > 0) {
if (nReadPos == nSrcPos)
Fill();
@@ -802,16 +801,19 @@ public:
//! rewind to a given reading position
bool SetPos(uint64_t nPos) {
- nReadPos = nPos;
- if (nReadPos + nRewind < nSrcPos) {
- nReadPos = nSrcPos - nRewind;
+ size_t bufsize = vchBuf.size();
+ if (nPos + bufsize < nSrcPos) {
+ // rewinding too far, rewind as far as possible
+ nReadPos = nSrcPos - bufsize;
return false;
- } else if (nReadPos > nSrcPos) {
+ }
+ if (nPos > nSrcPos) {
+ // can't go this far forward, go as far as possible
nReadPos = nSrcPos;
return false;
- } else {
- return true;
}
+ nReadPos = nPos;
+ return true;
}
bool Seek(uint64_t nPos) {