From cacb1e334110e0eb341db9b724994ab640ffc883 Mon Sep 17 00:00:00 2001 From: Stephen Anthony Date: Wed, 5 Sep 2018 11:14:28 -0230 Subject: [PATCH] Make ZipHandler 64-bit throughout. - although we will probably never deal with 4GB+ files, the underlying stream-reading code is 64-bit anyway - fixes warnings in 32 <-> 64 bit conversions in OSX. --- src/common/FSNodeZIP.cxx | 2 +- src/common/ZipHandler.cxx | 40 +++++++++++++++++++-------------------- src/common/ZipHandler.hxx | 14 +++++++------- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/common/FSNodeZIP.cxx b/src/common/FSNodeZIP.cxx index b61799d9f..7a171e68a 100644 --- a/src/common/FSNodeZIP.cxx +++ b/src/common/FSNodeZIP.cxx @@ -193,7 +193,7 @@ uInt32 FilesystemNodeZIP::read(BytePtr& image) const while(myZipHandler->hasNext() && !found) found = myZipHandler->next() == _virtualPath; - return found ? myZipHandler->decompress(image) : 0; + return found ? uInt32(myZipHandler->decompress(image)) : 0; // TODO: 64bit } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/common/ZipHandler.cxx b/src/common/ZipHandler.cxx index 049eb86d3..72e345d98 100644 --- a/src/common/ZipHandler.cxx +++ b/src/common/ZipHandler.cxx @@ -103,11 +103,11 @@ const string& ZipHandler::next() } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -uInt32 ZipHandler::decompress(BytePtr& image) +uInt64 ZipHandler::decompress(BytePtr& image) { if(myZip && myZip->myHeader.uncompressedLength > 0) { - uInt32 length = myZip->myHeader.uncompressedLength; + uInt64 length = myZip->myHeader.uncompressedLength; image = make_unique(length); if(image == nullptr) throw runtime_error(errorMessage(ZipError::OUT_OF_MEMORY)); @@ -243,7 +243,7 @@ void ZipHandler::ZipFile::initialize() throw ZipError::OUT_OF_MEMORY; // Read the central directory - uInt32 read_length = 0; + uInt64 read_length = 0; bool success = readStream(myCd, myEcd.cdStartDiskOffset, myEcd.cdSize, read_length); if(!success) throw ZipError::FILE_ERROR; @@ -266,13 +266,13 @@ void ZipHandler::ZipFile::close() // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void ZipHandler::ZipFile::readEcd() { - uInt32 buflen = 1024; + uInt64 buflen = 1024; BytePtr buffer; // We may need multiple tries while(buflen < 65536) { - uInt32 read_length; + uInt64 read_length; // Max out the buf length at the size of the file if(buflen > myLength) @@ -321,15 +321,15 @@ void ZipHandler::ZipFile::readEcd() } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -bool ZipHandler::ZipFile::readStream(BytePtr& out, uInt32 offset, - uInt32 length, uInt32& actual) +bool ZipHandler::ZipFile::readStream(BytePtr& out, uInt64 offset, + uInt64 length, uInt64& actual) { try { myStream.seekg(offset); myStream.read(reinterpret_cast(out.get()), length); - actual = uInt32(myStream.gcount()); + actual = myStream.gcount(); return true; } catch(...) @@ -365,7 +365,7 @@ const ZipHandler::ZipHeader* const ZipHandler::ZipFile::nextFile() } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -void ZipHandler::ZipFile::decompress(BytePtr& out, uInt32 length) +void ZipHandler::ZipFile::decompress(BytePtr& out, uInt64 length) { // If we don't have enough buffer, error if(length < myHeader.uncompressedLength) @@ -378,7 +378,7 @@ void ZipHandler::ZipFile::decompress(BytePtr& out, uInt32 length) try { // Get the compressed data offset - uInt32 offset = getCompressedDataOffset(); + uInt64 offset = getCompressedDataOffset(); // Handle compression types switch(myHeader.compression) @@ -402,10 +402,10 @@ void ZipHandler::ZipFile::decompress(BytePtr& out, uInt32 length) } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -uInt32 ZipHandler::ZipFile::getCompressedDataOffset() +uInt64 ZipHandler::ZipFile::getCompressedDataOffset() { // Read the fixed-sized part of the local file header - uInt32 read_length = 0; + uInt64 read_length = 0; bool success = readStream(myBuffer, myHeader.localHeaderOffset, 0x1e, read_length); if(!success) throw ZipError::FILE_ERROR; @@ -420,10 +420,10 @@ uInt32 ZipHandler::ZipFile::getCompressedDataOffset() // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void ZipHandler::ZipFile::decompressDataType0( - uInt32 offset, BytePtr& out, uInt32 length) + uInt64 offset, BytePtr& out, uInt64 length) { // The data is uncompressed; just read it - uInt32 read_length = 0; + uInt64 read_length = 0; bool success = readStream(out, offset, myHeader.compressedLength, read_length); if(!success) throw ZipError::FILE_ERROR; @@ -433,9 +433,9 @@ void ZipHandler::ZipFile::decompressDataType0( // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void ZipHandler::ZipFile::decompressDataType8( - uInt32 offset, BytePtr& out, uInt32 length) + uInt64 offset, BytePtr& out, uInt64 length) { - uInt32 input_remaining = myHeader.compressedLength; + uInt64 input_remaining = myHeader.compressedLength; // Reset the stream z_stream stream; @@ -444,7 +444,7 @@ void ZipHandler::ZipFile::decompressDataType8( stream.opaque = Z_NULL; stream.avail_in = 0; stream.next_out = reinterpret_cast(out.get()); - stream.avail_out = length; + stream.avail_out = uInt32(length); // TODO - use zip64 // Initialize the decompressor int zerr = inflateInit2(&stream, -MAX_WBITS); @@ -455,9 +455,9 @@ void ZipHandler::ZipFile::decompressDataType8( for(;;) { // Read in the next chunk of data - uInt32 read_length = 0; + uInt64 read_length = 0; bool success = readStream(myBuffer, offset, - std::min(input_remaining, uInt32(sizeof(myBuffer.get()))), read_length); + std::min(input_remaining, uInt64(sizeof(myBuffer.get()))), read_length); if(!success) { inflateEnd(&stream); @@ -474,7 +474,7 @@ void ZipHandler::ZipFile::decompressDataType8( // Fill out the input data stream.next_in = myBuffer.get(); - stream.avail_in = read_length; + stream.avail_in = uInt32(read_length); // TODO - use zip64 input_remaining -= read_length; // Add a dummy byte at end of compressed data diff --git a/src/common/ZipHandler.hxx b/src/common/ZipHandler.hxx index 275ff9f7f..7ba453c2d 100644 --- a/src/common/ZipHandler.hxx +++ b/src/common/ZipHandler.hxx @@ -45,7 +45,7 @@ class ZipHandler // Decompress the currently selected file and return its length // An exception will be thrown on any errors - uInt32 decompress(BytePtr& image); + uInt64 decompress(BytePtr& image); // Answer the number of ROM files (with a valid extension) found uInt16 romFiles() const { return myZip ? myZip->myRomfiles : 0; } @@ -105,7 +105,7 @@ class ZipHandler { string myFilename; // copy of ZIP filename (for caching) fstream myStream; // C++ fstream file handle - uInt32 myLength; // length of zip file + uInt64 myLength; // length of zip file uInt16 myRomfiles; // number of ROM files in central directory ZipEcd myEcd; // end of central directory @@ -132,22 +132,22 @@ class ZipHandler void readEcd(); /** Read data from stream */ - bool readStream(BytePtr& out, uInt32 offset, uInt32 length, uInt32& actual); + bool readStream(BytePtr& out, uInt64 offset, uInt64 length, uInt64& actual); /** Return the next entry in the ZIP file */ const ZipHeader* const nextFile(); /** Decompress the most recently found file in the ZIP into target buffer */ - void decompress(BytePtr& out, uInt32 length); + void decompress(BytePtr& out, uInt64 length); /** Return the offset of the compressed data */ - uInt32 getCompressedDataOffset(); + uInt64 getCompressedDataOffset(); /** Decompress type 0 data (which is uncompressed) */ - void decompressDataType0(uInt32 offset, BytePtr& out, uInt32 length); + void decompressDataType0(uInt64 offset, BytePtr& out, uInt64 length); /** Decompress type 8 data (which is deflated) */ - void decompressDataType8(uInt32 offset, BytePtr& out, uInt32 length); + void decompressDataType8(uInt64 offset, BytePtr& out, uInt64 length); }; using ZipFilePtr = unique_ptr;