diff options
Diffstat (limited to 'engines/cge/fileio.cpp')
-rw-r--r-- | engines/cge/fileio.cpp | 46 |
1 files changed, 28 insertions, 18 deletions
diff --git a/engines/cge/fileio.cpp b/engines/cge/fileio.cpp index 609d5e86aa..df5c31d367 100644 --- a/engines/cge/fileio.cpp +++ b/engines/cge/fileio.cpp @@ -8,12 +8,12 @@ * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. - + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - + * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. @@ -70,14 +70,14 @@ ResourceManager::ResourceManager() { _catFile = new Common::File(); _catFile->open(kCatName); - if ((!_datFile) || (!_catFile)) + if (!_datFile->isOpen() || !_catFile->isOpen()) error("Unable to open data files"); for (int i = 0; i < kBtLevel; i++) { _buff[i]._page = new BtPage; _buff[i]._pageNo = kBtValNone; _buff[i]._index = -1; - assert(_buff[i]._page != NULL); + assert(_buff[i]._page != nullptr); } } @@ -93,20 +93,18 @@ ResourceManager::~ResourceManager() { delete _buff[i]._page; } -uint16 ResourceManager::XCrypt(void *buf, uint16 length) { - byte *b = static_cast<byte *>(buf); +void ResourceManager::XCrypt(byte *buf, uint16 length) { + byte *b = buf; for (uint16 i = 0; i < length; i++) *b++ ^= kCryptSeed; - - return kCryptSeed; } bool ResourceManager::seek(int32 offs, int whence) { return _datFile->seek(offs, whence); } -uint16 ResourceManager::read(void *buf, uint16 length) { +uint16 ResourceManager::read(byte *buf, uint16 length) { if (!_datFile->isOpen()) return 0; @@ -118,12 +116,18 @@ uint16 ResourceManager::read(void *buf, uint16 length) { } BtPage *ResourceManager::getPage(int level, uint16 pageId) { - debugC(1, kCGEDebugFile, "IoHand::getPage(%d, %d)", level, pageId); + debugC(1, kCGEDebugFile, "ResourceManager::getPage(%d, %d)", level, pageId); + + if (level >= kBtLevel) + return nullptr; if (_buff[level]._pageNo != pageId) { int32 pos = pageId * kBtSize; _buff[level]._pageNo = pageId; - assert(_catFile->size() > pos); + + if (_catFile->size() <= pos) + return nullptr; + // In the original, there was a check verifying if the // purpose was to write a new file. This should only be // to create a new file, thus it was removed. @@ -142,17 +146,19 @@ BtPage *ResourceManager::getPage(int level, uint16 pageId) { } BtKeypack *ResourceManager::find(const char *key) { - debugC(1, kCGEDebugFile, "IoHand::find(%s)", key); + debugC(1, kCGEDebugFile, "ResourceManager::find(%s)", key); int lev = 0; uint16 nxt = kBtValRoot; while (!_catFile->eos()) { BtPage *pg = getPage(lev, nxt); + if (!pg) + return nullptr; + // search if (pg->_header._down != kBtValNone) { int i; for (i = 0; i < pg->_header._count; i++) { - // Does this work, or does it have to compare the entire buffer? if (scumm_strnicmp((const char *)key, (const char*)pg->_inner[i]._key, kBtKeySize) < 0) break; } @@ -169,16 +175,20 @@ BtKeypack *ResourceManager::find(const char *key) { return &pg->_leaf[i]; } } - return NULL; + return nullptr; } bool ResourceManager::exist(const char *name) { debugC(1, kCGEDebugFile, "ResourceManager::exist(%s)", name); - return scumm_stricmp(find(name)->_key, name) == 0; + BtKeypack* result = find(name); + if (!result) + return false; + + return scumm_stricmp(result->_key, name) == 0; } -uint16 ResourceManager::catRead(void *buf, uint16 length) { +uint16 ResourceManager::catRead(byte *buf, uint16 length) { if (!_catFile->isOpen()) return 0; @@ -225,12 +235,12 @@ EncryptedStream::EncryptedStream(CGEEngine *vm, const char *name) : _vm(vm) { _readStream = new Common::MemoryReadStream(dataBuffer, bufSize, DisposeAfterUse::YES); } -uint32 EncryptedStream::read(void *dataPtr, uint32 dataSize) { +uint32 EncryptedStream::read(byte *dataPtr, uint32 dataSize) { return _readStream->read(dataPtr, dataSize); } bool EncryptedStream::err() { - return (_error & _readStream->err()); + return (_error || _readStream->err()); } bool EncryptedStream::eos() { |