Use runtime_error exceptions everywhere in ZipHandler.

This commit is contained in:
Stephen Anthony 2019-12-28 19:01:39 -03:30
parent ca1207344c
commit 04fe64568a
2 changed files with 32 additions and 31 deletions

View File

@ -55,9 +55,9 @@ void ZipHandler::open(const string& filename)
{ {
ptr->initialize(); ptr->initialize();
} }
catch(const ZipError& err) catch(...)
{ {
throw runtime_error(errorMessage(err)); throw;
} }
myZip = std::move(ptr); myZip = std::move(ptr);
@ -124,7 +124,7 @@ uInt64 ZipHandler::decompress(ByteBuffer& image)
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string ZipHandler::errorMessage(ZipError err) const string ZipHandler::errorMessage(ZipError err)
{ {
static constexpr std::array<const char*, 10> zip_error_s = { static constexpr std::array<const char*, 10> zip_error_s = {
"ZIP NONE", "ZIP NONE",
@ -232,22 +232,22 @@ void ZipHandler::ZipFile::initialize()
// Verify that we can work with this zipfile (no disk spanning allowed) // Verify that we can work with this zipfile (no disk spanning allowed)
if(myEcd.diskNumber != myEcd.cdStartDiskNumber || if(myEcd.diskNumber != myEcd.cdStartDiskNumber ||
myEcd.cdDiskEntries != myEcd.cdTotalEntries) myEcd.cdDiskEntries != myEcd.cdTotalEntries)
throw ZipError::UNSUPPORTED; throw runtime_error(errorMessage(ZipError::UNSUPPORTED));
// Allocate memory for the central directory // Allocate memory for the central directory
myCd = make_unique<uInt8[]>(myEcd.cdSize + 1); myCd = make_unique<uInt8[]>(myEcd.cdSize + 1);
if(myCd == nullptr) if(myCd == nullptr)
throw ZipError::OUT_OF_MEMORY; throw runtime_error(errorMessage(ZipError::OUT_OF_MEMORY));
// Read the central directory // Read the central directory
uInt64 read_length = 0; uInt64 read_length = 0;
bool success = readStream(myCd, myEcd.cdStartDiskOffset, myEcd.cdSize, read_length); bool success = readStream(myCd, myEcd.cdStartDiskOffset, myEcd.cdSize, read_length);
if(!success) if(!success)
throw ZipError::FILE_ERROR; throw runtime_error(errorMessage(ZipError::FILE_ERROR));
else if(read_length != myEcd.cdSize) else if(read_length != myEcd.cdSize)
throw ZipError::FILE_TRUNCATED; throw runtime_error(errorMessage(ZipError::FILE_TRUNCATED));
} }
catch(const ZipError&) catch(...)
{ {
throw; throw;
} }
@ -278,12 +278,12 @@ void ZipHandler::ZipFile::readEcd()
// Allocate buffer // Allocate buffer
buffer = make_unique<uInt8[]>(buflen + 1); buffer = make_unique<uInt8[]>(buflen + 1);
if(buffer == nullptr) if(buffer == nullptr)
throw ZipError::OUT_OF_MEMORY; throw runtime_error(errorMessage(ZipError::OUT_OF_MEMORY));
// Read in one buffers' worth of data // Read in one buffers' worth of data
bool success = readStream(buffer, myLength - buflen, buflen, read_length); bool success = readStream(buffer, myLength - buflen, buflen, read_length);
if(!success || read_length != buflen) if(!success || read_length != buflen)
throw ZipError::FILE_ERROR; throw runtime_error(errorMessage(ZipError::FILE_ERROR));
// Find the ECD signature // Find the ECD signature
Int32 offset; Int32 offset;
@ -312,9 +312,9 @@ void ZipHandler::ZipFile::readEcd()
if(buflen < myLength) if(buflen < myLength)
buflen *= 2; buflen *= 2;
else else
throw ZipError::BAD_SIGNATURE; throw runtime_error(errorMessage(ZipError::BAD_SIGNATURE));
} }
throw ZipError::OUT_OF_MEMORY; throw runtime_error(errorMessage(ZipError::OUT_OF_MEMORY));
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -366,11 +366,11 @@ void ZipHandler::ZipFile::decompress(ByteBuffer& out, uInt64 length)
{ {
// If we don't have enough buffer, error // If we don't have enough buffer, error
if(length < myHeader.uncompressedLength) if(length < myHeader.uncompressedLength)
throw ZipError::BUFFER_TOO_SMALL; throw runtime_error(errorMessage(ZipError::BUFFER_TOO_SMALL));
// Make sure the info in the header aligns with what we know // Make sure the info in the header aligns with what we know
if(myHeader.startDiskNumber != myEcd.diskNumber) if(myHeader.startDiskNumber != myEcd.diskNumber)
throw ZipError::UNSUPPORTED; throw runtime_error(errorMessage(ZipError::UNSUPPORTED));
try try
{ {
@ -389,13 +389,14 @@ void ZipHandler::ZipFile::decompress(ByteBuffer& out, uInt64 length)
break; break;
case 14: case 14:
throw ZipError::LZMA_UNSUPPORTED; // FIXME - LZMA format not yet supported // FIXME - LZMA format not yet supported
throw runtime_error(errorMessage(ZipError::LZMA_UNSUPPORTED));
default: default:
throw ZipError::UNSUPPORTED; throw runtime_error(errorMessage(ZipError::UNSUPPORTED));
} }
} }
catch(const ZipError&) catch(...)
{ {
throw; throw;
} }
@ -409,20 +410,20 @@ uInt64 ZipHandler::ZipFile::getCompressedDataOffset()
if(myHeader.startDiskNumber != myEcd.diskNumber || if(myHeader.startDiskNumber != myEcd.diskNumber ||
myHeader.versionNeeded > 63 || flags.patchData() || myHeader.versionNeeded > 63 || flags.patchData() ||
flags.encrypted() || flags.strongEncryption()) flags.encrypted() || flags.strongEncryption())
throw ZipError::UNSUPPORTED; throw runtime_error(errorMessage(ZipError::UNSUPPORTED));
// Read the fixed-sized part of the local file header // Read the fixed-sized part of the local file header
uInt64 read_length = 0; uInt64 read_length = 0;
bool success = readStream(myBuffer, myHeader.localHeaderOffset, 0x1e, read_length); bool success = readStream(myBuffer, myHeader.localHeaderOffset, 0x1e, read_length);
if(!success) if(!success)
throw ZipError::FILE_ERROR; throw runtime_error(errorMessage(ZipError::FILE_ERROR));
else if(read_length != LocalFileHeaderReader::minimumLength()) else if(read_length != LocalFileHeaderReader::minimumLength())
throw ZipError::FILE_TRUNCATED; throw runtime_error(errorMessage(ZipError::FILE_TRUNCATED));
// Compute the final offset // Compute the final offset
LocalFileHeaderReader reader(&myBuffer[0]); LocalFileHeaderReader reader(&myBuffer[0]);
if(!reader.signatureCorrect()) if(!reader.signatureCorrect())
throw ZipError::BAD_SIGNATURE; throw runtime_error(errorMessage(ZipError::BAD_SIGNATURE));
return myHeader.localHeaderOffset + reader.totalLength(); return myHeader.localHeaderOffset + reader.totalLength();
} }
@ -435,9 +436,9 @@ void ZipHandler::ZipFile::decompressDataType0(
uInt64 read_length = 0; uInt64 read_length = 0;
bool success = readStream(out, offset, myHeader.compressedLength, read_length); bool success = readStream(out, offset, myHeader.compressedLength, read_length);
if(!success) if(!success)
throw ZipError::FILE_ERROR; throw runtime_error(errorMessage(ZipError::FILE_ERROR));
else if(read_length != myHeader.compressedLength) else if(read_length != myHeader.compressedLength)
throw ZipError::FILE_TRUNCATED; throw runtime_error(errorMessage(ZipError::FILE_TRUNCATED));
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -458,7 +459,7 @@ void ZipHandler::ZipFile::decompressDataType8(
// Initialize the decompressor // Initialize the decompressor
int zerr = inflateInit2(&stream, -MAX_WBITS); int zerr = inflateInit2(&stream, -MAX_WBITS);
if(zerr != Z_OK) if(zerr != Z_OK)
throw ZipError::DECOMPRESS_ERROR; throw runtime_error(errorMessage(ZipError::DECOMPRESS_ERROR));
// Loop until we're done // Loop until we're done
for(;;) for(;;)
@ -470,7 +471,7 @@ void ZipHandler::ZipFile::decompressDataType8(
if(!success) if(!success)
{ {
inflateEnd(&stream); inflateEnd(&stream);
throw ZipError::FILE_ERROR; throw runtime_error(errorMessage(ZipError::FILE_ERROR));
} }
offset += read_length; offset += read_length;
@ -478,7 +479,7 @@ void ZipHandler::ZipFile::decompressDataType8(
if(read_length == 0 && input_remaining > 0) if(read_length == 0 && input_remaining > 0)
{ {
inflateEnd(&stream); inflateEnd(&stream);
throw ZipError::FILE_TRUNCATED; throw runtime_error(errorMessage(ZipError::FILE_TRUNCATED));
} }
// Fill out the input data // Fill out the input data
@ -497,18 +498,18 @@ void ZipHandler::ZipFile::decompressDataType8(
else if(zerr != Z_OK) else if(zerr != Z_OK)
{ {
inflateEnd(&stream); inflateEnd(&stream);
throw ZipError::DECOMPRESS_ERROR; throw runtime_error(errorMessage(ZipError::DECOMPRESS_ERROR));
} }
} }
// Finish decompression // Finish decompression
zerr = inflateEnd(&stream); zerr = inflateEnd(&stream);
if(zerr != Z_OK) if(zerr != Z_OK)
throw ZipError::DECOMPRESS_ERROR; throw runtime_error(errorMessage(ZipError::DECOMPRESS_ERROR));
// If anything looks funny, report an error // If anything looks funny, report an error
if(stream.avail_out > 0 || input_remaining > 0) if(stream.avail_out > 0 || input_remaining > 0)
throw ZipError::DECOMPRESS_ERROR; throw runtime_error(errorMessage(ZipError::DECOMPRESS_ERROR));
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -110,7 +110,7 @@ class ZipHandler
ZipEcd myEcd; // end of central directory ZipEcd myEcd; // end of central directory
ByteBuffer myCd; // central directory raw data ByteBuffer myCd; // central directory raw data
uInt64 myCdPos; // position in central directory uInt64 myCdPos; // position in central directory
ZipHeader myHeader; // current file header ZipHeader myHeader; // current file header
@ -294,7 +294,7 @@ class ZipHandler
private: private:
/** Get message for given ZipError enumeration */ /** Get message for given ZipError enumeration */
string errorMessage(ZipError err) const; static string errorMessage(ZipError err);
/** Search cache for given ZIP file */ /** Search cache for given ZIP file */
ZipFilePtr findCached(const string& filename); ZipFilePtr findCached(const string& filename);