Some refactoring in ZipHandler. Debugging code present for now.

This commit is contained in:
Stephen Anthony 2022-12-04 18:38:43 -03:30
parent 62a3a9275c
commit b17a2e63b2
3 changed files with 57 additions and 66 deletions

View File

@ -267,12 +267,24 @@ size_t FSNodeZIP::write(const stringstream& buffer) const
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
AbstractFSNodePtr FSNodeZIP::getParent() const
{
cerr << "zip : " << _zipFile << endl
<< "vp : " << _virtualPath << endl
<< "path: " << _path << endl
<< "name: " << _name << endl;
if(_virtualPath.empty())
{
if(_realNode)
cerr << "parent: " << _realNode->getParent()->getPath();
else
cerr << "parent: nullptr";
cerr << "\n\n\n";
return _realNode ? _realNode->getParent() : nullptr;
}
const char* const start = _path.c_str();
const char* const end = lastPathComponent(_path);
cerr << "new zip: " << string(start, end - start - 1) << "\n\n\n";
return make_unique<FSNodeZIP>(string(start, end - start - 1));
}

View File

@ -51,14 +51,7 @@ void ZipHandler::open(const string& filename)
// Open the file and initialize it
if(!ptr->open())
throw runtime_error(errorMessage(ZipError::FILE_ERROR));
try
{
ptr->initialize();
}
catch(...)
{
throw;
}
myZip = std::move(ptr);
@ -100,7 +93,7 @@ std::tuple<string, size_t> ZipHandler::next()
{
if(hasNext())
{
const ZipHeader* header = myZip->nextFile();
const ZipHeader* const header = myZip->nextFile();
if(!header)
throw runtime_error(errorMessage(ZipError::FILE_CORRUPT));
else if(header->uncompressedLength == 0)
@ -207,8 +200,8 @@ void ZipHandler::addToCache()
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ZipHandler::ZipFile::ZipFile(const string& filename)
: myFilename(filename),
myBuffer(make_unique<uInt8[]>(DECOMPRESS_BUFSIZE))
: myFilename{filename},
myBuffer{make_unique<uInt8[]>(DECOMPRESS_BUFSIZE)}
{
std::fill(myBuffer.get(), myBuffer.get() + DECOMPRESS_BUFSIZE, 0);
}
@ -232,8 +225,6 @@ bool ZipHandler::ZipFile::open()
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ZipHandler::ZipFile::initialize()
{
try
{
// Read ecd data
readEcd();
@ -256,11 +247,6 @@ void ZipHandler::ZipFile::initialize()
else if(read_length != myEcd.cdSize)
throw runtime_error(errorMessage(ZipError::FILE_TRUNCATED));
}
catch(...)
{
throw;
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ZipHandler::ZipFile::close()
@ -306,7 +292,7 @@ void ZipHandler::ZipFile::readEcd()
if(offset >= 0)
{
// Extract ECD info
EcdReader const reader(buffer.get() + offset);
const EcdReader reader(buffer.get() + offset);
myEcd.diskNumber = reader.thisDiskNo();
myEcd.cdStartDiskNumber = reader.dirStartDisk();
myEcd.cdDiskEntries = reader.dirDiskEntries();
@ -348,7 +334,7 @@ const ZipHandler::ZipHeader* ZipHandler::ZipFile::nextFile()
{
// Make sure we have enough data
// If we're at or past the end, we're done
CentralDirEntryReader const reader(myCd.get() + myCdPos);
const CentralDirEntryReader reader(myCd.get() + myCdPos);
if(!reader.signatureCorrect() || ((myCdPos + reader.totalLength()) > myEcd.cdSize))
return nullptr;
@ -380,8 +366,6 @@ void ZipHandler::ZipFile::decompress(const ByteBuffer& out, uInt64 length)
if(myHeader.startDiskNumber != myEcd.diskNumber)
throw runtime_error(errorMessage(ZipError::UNSUPPORTED));
try
{
// Get the compressed data offset
const uInt64 offset = getCompressedDataOffset();
@ -404,17 +388,12 @@ void ZipHandler::ZipFile::decompress(const ByteBuffer& out, uInt64 length)
throw runtime_error(errorMessage(ZipError::UNSUPPORTED));
}
}
catch(...)
{
throw;
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uInt64 ZipHandler::ZipFile::getCompressedDataOffset()
{
// Don't support a number of features
GeneralFlagReader const flags(myHeader.bitFlag);
const GeneralFlagReader flags(myHeader.bitFlag);
if(myHeader.startDiskNumber != myEcd.diskNumber ||
myHeader.versionNeeded > 63 || flags.patchData() ||
flags.encrypted() || flags.strongEncryption())
@ -462,7 +441,7 @@ void ZipHandler::ZipFile::decompressDataType8(
stream.opaque = Z_NULL;
stream.avail_in = 0;
stream.next_out = reinterpret_cast<Bytef *>(out.get());
stream.avail_out = static_cast<uInt32>(length); // TODO - use zip64
stream.avail_out = static_cast<uInt32>(length);
// Initialize the decompressor
int zerr = inflateInit2(&stream, -MAX_WBITS);
@ -492,7 +471,7 @@ void ZipHandler::ZipFile::decompressDataType8(
// Fill out the input data
stream.next_in = myBuffer.get();
stream.avail_in = static_cast<uInt32>(read_length); // TODO - use zip64
stream.avail_in = static_cast<uInt32>(read_length);
input_remaining -= read_length;
// Add a dummy byte at end of compressed data

View File

@ -300,7 +300,7 @@ class ZipHandler
private:
static constexpr size_t DECOMPRESS_BUFSIZE = 128_KB;
static constexpr size_t CACHE_SIZE = 16; // number of open files to cache
static constexpr size_t CACHE_SIZE = 64; // number of open files to cache
ZipFilePtr myZip;
std::array<ZipFilePtr, CACHE_SIZE> myZipCache;