mirror of https://github.com/stella-emu/stella.git
Some refactoring in ZipHandler. Debugging code present for now.
This commit is contained in:
parent
62a3a9275c
commit
b17a2e63b2
|
@ -36,11 +36,11 @@ FSNodeZIP::FSNodeZIP(const string& p)
|
||||||
_zipFile = p.substr(0, pos+4);
|
_zipFile = p.substr(0, pos+4);
|
||||||
|
|
||||||
// Expand '~' to the users 'home' directory
|
// Expand '~' to the users 'home' directory
|
||||||
if (_zipFile[0] == '~')
|
if(_zipFile[0] == '~')
|
||||||
{
|
{
|
||||||
#if defined(BSPF_UNIX) || defined(BSPF_MACOS)
|
#if defined(BSPF_UNIX) || defined(BSPF_MACOS)
|
||||||
const char* const home = std::getenv("HOME"); // NOLINT (not thread safe)
|
const char* const home = std::getenv("HOME"); // NOLINT (not thread safe)
|
||||||
if (home != nullptr)
|
if(home != nullptr)
|
||||||
_zipFile.replace(0, 1, home);
|
_zipFile.replace(0, 1, home);
|
||||||
#elif defined(BSPF_WINDOWS)
|
#elif defined(BSPF_WINDOWS)
|
||||||
_zipFile.replace(0, 1, HomeFinder::getHomePath());
|
_zipFile.replace(0, 1, HomeFinder::getHomePath());
|
||||||
|
@ -267,12 +267,24 @@ size_t FSNodeZIP::write(const stringstream& buffer) const
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
AbstractFSNodePtr FSNodeZIP::getParent() const
|
AbstractFSNodePtr FSNodeZIP::getParent() const
|
||||||
{
|
{
|
||||||
|
cerr << "zip : " << _zipFile << endl
|
||||||
|
<< "vp : " << _virtualPath << endl
|
||||||
|
<< "path: " << _path << endl
|
||||||
|
<< "name: " << _name << endl;
|
||||||
if(_virtualPath.empty())
|
if(_virtualPath.empty())
|
||||||
|
{
|
||||||
|
if(_realNode)
|
||||||
|
cerr << "parent: " << _realNode->getParent()->getPath();
|
||||||
|
else
|
||||||
|
cerr << "parent: nullptr";
|
||||||
|
cerr << "\n\n\n";
|
||||||
return _realNode ? _realNode->getParent() : nullptr;
|
return _realNode ? _realNode->getParent() : nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
const char* const start = _path.c_str();
|
const char* const start = _path.c_str();
|
||||||
const char* const end = lastPathComponent(_path);
|
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));
|
return make_unique<FSNodeZIP>(string(start, end - start - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -51,14 +51,7 @@ void ZipHandler::open(const string& filename)
|
||||||
// Open the file and initialize it
|
// Open the file and initialize it
|
||||||
if(!ptr->open())
|
if(!ptr->open())
|
||||||
throw runtime_error(errorMessage(ZipError::FILE_ERROR));
|
throw runtime_error(errorMessage(ZipError::FILE_ERROR));
|
||||||
try
|
ptr->initialize();
|
||||||
{
|
|
||||||
ptr->initialize();
|
|
||||||
}
|
|
||||||
catch(...)
|
|
||||||
{
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
|
|
||||||
myZip = std::move(ptr);
|
myZip = std::move(ptr);
|
||||||
|
|
||||||
|
@ -100,7 +93,7 @@ std::tuple<string, size_t> ZipHandler::next()
|
||||||
{
|
{
|
||||||
if(hasNext())
|
if(hasNext())
|
||||||
{
|
{
|
||||||
const ZipHeader* header = myZip->nextFile();
|
const ZipHeader* const header = myZip->nextFile();
|
||||||
if(!header)
|
if(!header)
|
||||||
throw runtime_error(errorMessage(ZipError::FILE_CORRUPT));
|
throw runtime_error(errorMessage(ZipError::FILE_CORRUPT));
|
||||||
else if(header->uncompressedLength == 0)
|
else if(header->uncompressedLength == 0)
|
||||||
|
@ -207,8 +200,8 @@ void ZipHandler::addToCache()
|
||||||
|
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
ZipHandler::ZipFile::ZipFile(const string& filename)
|
ZipHandler::ZipFile::ZipFile(const string& filename)
|
||||||
: myFilename(filename),
|
: myFilename{filename},
|
||||||
myBuffer(make_unique<uInt8[]>(DECOMPRESS_BUFSIZE))
|
myBuffer{make_unique<uInt8[]>(DECOMPRESS_BUFSIZE)}
|
||||||
{
|
{
|
||||||
std::fill(myBuffer.get(), myBuffer.get() + DECOMPRESS_BUFSIZE, 0);
|
std::fill(myBuffer.get(), myBuffer.get() + DECOMPRESS_BUFSIZE, 0);
|
||||||
}
|
}
|
||||||
|
@ -233,33 +226,26 @@ bool ZipHandler::ZipFile::open()
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
void ZipHandler::ZipFile::initialize()
|
void ZipHandler::ZipFile::initialize()
|
||||||
{
|
{
|
||||||
try
|
// Read ecd data
|
||||||
{
|
readEcd();
|
||||||
// Read ecd data
|
|
||||||
readEcd();
|
|
||||||
|
|
||||||
// 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 runtime_error(errorMessage(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 runtime_error(errorMessage(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;
|
||||||
const bool success = readStream(myCd, myEcd.cdStartDiskOffset, myEcd.cdSize, read_length);
|
const bool success = readStream(myCd, myEcd.cdStartDiskOffset, myEcd.cdSize, read_length);
|
||||||
if(!success)
|
if(!success)
|
||||||
throw runtime_error(errorMessage(ZipError::FILE_ERROR));
|
throw runtime_error(errorMessage(ZipError::FILE_ERROR));
|
||||||
else if(read_length != myEcd.cdSize)
|
else if(read_length != myEcd.cdSize)
|
||||||
throw runtime_error(errorMessage(ZipError::FILE_TRUNCATED));
|
throw runtime_error(errorMessage(ZipError::FILE_TRUNCATED));
|
||||||
}
|
|
||||||
catch(...)
|
|
||||||
{
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
|
@ -306,7 +292,7 @@ void ZipHandler::ZipFile::readEcd()
|
||||||
if(offset >= 0)
|
if(offset >= 0)
|
||||||
{
|
{
|
||||||
// Extract ECD info
|
// Extract ECD info
|
||||||
EcdReader const reader(buffer.get() + offset);
|
const EcdReader reader(buffer.get() + offset);
|
||||||
myEcd.diskNumber = reader.thisDiskNo();
|
myEcd.diskNumber = reader.thisDiskNo();
|
||||||
myEcd.cdStartDiskNumber = reader.dirStartDisk();
|
myEcd.cdStartDiskNumber = reader.dirStartDisk();
|
||||||
myEcd.cdDiskEntries = reader.dirDiskEntries();
|
myEcd.cdDiskEntries = reader.dirDiskEntries();
|
||||||
|
@ -348,7 +334,7 @@ const ZipHandler::ZipHeader* ZipHandler::ZipFile::nextFile()
|
||||||
{
|
{
|
||||||
// Make sure we have enough data
|
// Make sure we have enough data
|
||||||
// If we're at or past the end, we're done
|
// 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))
|
if(!reader.signatureCorrect() || ((myCdPos + reader.totalLength()) > myEcd.cdSize))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
|
@ -380,33 +366,26 @@ void ZipHandler::ZipFile::decompress(const ByteBuffer& out, uInt64 length)
|
||||||
if(myHeader.startDiskNumber != myEcd.diskNumber)
|
if(myHeader.startDiskNumber != myEcd.diskNumber)
|
||||||
throw runtime_error(errorMessage(ZipError::UNSUPPORTED));
|
throw runtime_error(errorMessage(ZipError::UNSUPPORTED));
|
||||||
|
|
||||||
try
|
// Get the compressed data offset
|
||||||
|
const uInt64 offset = getCompressedDataOffset();
|
||||||
|
|
||||||
|
// Handle compression types
|
||||||
|
switch(myHeader.compression)
|
||||||
{
|
{
|
||||||
// Get the compressed data offset
|
case 0:
|
||||||
const uInt64 offset = getCompressedDataOffset();
|
decompressDataType0(offset, out, length);
|
||||||
|
break;
|
||||||
|
|
||||||
// Handle compression types
|
case 8:
|
||||||
switch(myHeader.compression)
|
decompressDataType8(offset, out, length);
|
||||||
{
|
break;
|
||||||
case 0:
|
|
||||||
decompressDataType0(offset, out, length);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 8:
|
case 14:
|
||||||
decompressDataType8(offset, out, length);
|
// FIXME - LZMA format not yet supported
|
||||||
break;
|
throw runtime_error(errorMessage(ZipError::LZMA_UNSUPPORTED));
|
||||||
|
|
||||||
case 14:
|
default:
|
||||||
// FIXME - LZMA format not yet supported
|
throw runtime_error(errorMessage(ZipError::UNSUPPORTED));
|
||||||
throw runtime_error(errorMessage(ZipError::LZMA_UNSUPPORTED));
|
|
||||||
|
|
||||||
default:
|
|
||||||
throw runtime_error(errorMessage(ZipError::UNSUPPORTED));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch(...)
|
|
||||||
{
|
|
||||||
throw;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -414,7 +393,7 @@ void ZipHandler::ZipFile::decompress(const ByteBuffer& out, uInt64 length)
|
||||||
uInt64 ZipHandler::ZipFile::getCompressedDataOffset()
|
uInt64 ZipHandler::ZipFile::getCompressedDataOffset()
|
||||||
{
|
{
|
||||||
// Don't support a number of features
|
// Don't support a number of features
|
||||||
GeneralFlagReader const flags(myHeader.bitFlag);
|
const GeneralFlagReader flags(myHeader.bitFlag);
|
||||||
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())
|
||||||
|
@ -462,7 +441,7 @@ void ZipHandler::ZipFile::decompressDataType8(
|
||||||
stream.opaque = Z_NULL;
|
stream.opaque = Z_NULL;
|
||||||
stream.avail_in = 0;
|
stream.avail_in = 0;
|
||||||
stream.next_out = reinterpret_cast<Bytef *>(out.get());
|
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
|
// Initialize the decompressor
|
||||||
int zerr = inflateInit2(&stream, -MAX_WBITS);
|
int zerr = inflateInit2(&stream, -MAX_WBITS);
|
||||||
|
@ -492,7 +471,7 @@ void ZipHandler::ZipFile::decompressDataType8(
|
||||||
|
|
||||||
// Fill out the input data
|
// Fill out the input data
|
||||||
stream.next_in = myBuffer.get();
|
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;
|
input_remaining -= read_length;
|
||||||
|
|
||||||
// Add a dummy byte at end of compressed data
|
// Add a dummy byte at end of compressed data
|
||||||
|
|
|
@ -300,7 +300,7 @@ class ZipHandler
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static constexpr size_t DECOMPRESS_BUFSIZE = 128_KB;
|
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;
|
ZipFilePtr myZip;
|
||||||
std::array<ZipFilePtr, CACHE_SIZE> myZipCache;
|
std::array<ZipFilePtr, CACHE_SIZE> myZipCache;
|
||||||
|
|
Loading…
Reference in New Issue