diff options
author | Max Horn | 2003-05-28 19:03:12 +0000 |
---|---|---|
committer | Max Horn | 2003-05-28 19:03:12 +0000 |
commit | 2dd2e99cabd70b4375cfae97771d340591956024 (patch) | |
tree | 61b0524c60b5b70261a6789a84da759db48f3723 | |
parent | 798d23c6a8dcb609de8920e84f3ee8bf76f5ebab (diff) | |
download | scummvm-rg350-2dd2e99cabd70b4375cfae97771d340591956024.tar.gz scummvm-rg350-2dd2e99cabd70b4375cfae97771d340591956024.tar.bz2 scummvm-rg350-2dd2e99cabd70b4375cfae97771d340591956024.zip |
the _encbyte code was evil, because it modified the memory passed to write(); worse, though, it incremented ptr2, which then was later passed to fwrite - hence if used to write something while _encbyte != 0, write() resulted in wrong data being written
svn-id: r8055
-rw-r--r-- | common/file.cpp | 21 | ||||
-rw-r--r-- | common/file.h | 2 |
2 files changed, 14 insertions, 9 deletions
diff --git a/common/file.cpp b/common/file.cpp index 0b9dbbef68..011e167573 100644 --- a/common/file.cpp +++ b/common/file.cpp @@ -266,9 +266,9 @@ uint32 File::readUint32BE() { return (b << 16) | a; } -uint32 File::write(void *ptr, uint32 len) { - byte *ptr2 = (byte *)ptr; - +uint32 File::write(const void *ptr, uint32 len) { + byte *tmp = 0; + if (_handle == NULL) { error("File is not open!"); return 0; @@ -281,17 +281,22 @@ uint32 File::write(void *ptr, uint32 len) { // Maybe FIXME: while it's efficient to do the encoding here, // it not really nice for a write function to modify its input. // Maybe we should work on a copy here... - uint32 t_size = len; - do { - *ptr2++ ^= _encbyte; - } while (--t_size); + tmp = (byte *)malloc(len); + for (uint32 i = 0; i < len; i ++) { + tmp[i] = ((const byte *)ptr)[i] ^ _encbyte; + } + ptr = tmp; } - if ((uint32)fwrite(ptr2, 1, len, _handle) != len) { + if ((uint32)fwrite(ptr, 1, len, _handle) != len) { clearerr(_handle); _ioFailed = true; } + if (_encbyte != 0) { + free(tmp); + } + return len; } diff --git a/common/file.h b/common/file.h index c6b9e72a4f..6d4cc7c545 100644 --- a/common/file.h +++ b/common/file.h @@ -60,7 +60,7 @@ public: uint32 readUint32LE(); uint16 readUint16BE(); uint32 readUint32BE(); - uint32 write(void *ptr, uint32 size); + uint32 write(const void *ptr, uint32 size); void writeByte(byte value); void writeUint16LE(uint16 value); void writeUint32LE(uint32 value); |