got rid of FZipLump.

This commit is contained in:
Christoph Oelckers 2023-12-12 20:46:57 +01:00
parent c27c8c232a
commit e35b0f1453
6 changed files with 30 additions and 207 deletions

View file

@ -110,7 +110,6 @@ public:
static uint32_t LumpNameHash (const char *name); // [RH] Create hash key from an 8-char name
int FileLength (int lump) const;
int GetFileOffset (int lump); // [RH] Returns offset of lump in the wadfile
int GetFileFlags (int lump); // Return the flags for this lump
const char* GetFileShortName(int lump) const;
const char *GetFileFullName (int lump, bool returnshort = true) const; // [RH] Returns the lump's full name

View file

@ -137,7 +137,6 @@ protected:
virtual FileReader *GetReader();
virtual FileReader NewReader();
virtual int GetFileOffset() { return -1; }
virtual int GetIndexNum() const { return -1; }
virtual int GetNamespace() const { return 0; }
void LumpNameSetup(const char* iname, StringPool* allocator);
@ -189,6 +188,10 @@ protected:
// for archives that can contain directories
void GenerateHash();
void PostProcessArchive(void *lumps, size_t lumpsize, LumpFilterInfo *filter);
virtual void SetEntryAddress(uint32_t entry)
{
Entries[entry].Flags &= ~RESFF_NEEDFILESTART;
}
private:
uint32_t FirstLump;

View file

@ -47,47 +47,6 @@ namespace FileSys {
#define BUFREADCOMMENT (0x400)
//==========================================================================
//
// Decompression subroutine
//
//==========================================================================
static bool UncompressZipLump(char *Cache, FileReader &Reader, int Method, ptrdiff_t LumpSize, ptrdiff_t CompressedSize, int GPFlags, bool exceptions)
{
switch (Method)
{
case METHOD_STORED:
{
Reader.Read(Cache, LumpSize);
break;
}
case METHOD_DEFLATE:
case METHOD_BZIP2:
case METHOD_LZMA:
case METHOD_XZ:
case METHOD_IMPLODE_0:
case METHOD_IMPLODE_2:
case METHOD_IMPLODE_4:
case METHOD_IMPLODE_6:
case METHOD_SHRINK:
{
FileReader frz;
if (frz.OpenDecompressor(Reader, LumpSize, Method, false, exceptions))
{
frz.Read(Cache, LumpSize);
}
break;
}
default:
assert(0);
return false;
}
return true;
}
//-----------------------------------------------------------------------
//
// Finds the central directory end record in the end of the file.
@ -147,10 +106,25 @@ static uint32_t Zip_FindCentralDir(FileReader &fin, bool* zip64)
//
//==========================================================================
class FZipFile : public FResourceFile
{
void SetEntryAddress(uint32_t entry) override;
public:
FZipFile(const char* filename, FileReader& file, StringPool* sp);
bool Open(LumpFilterInfo* filter, FileSystemMessageFunc Printf);
FCompressedBuffer GetRawData(uint32_t entry) override;
};
//==========================================================================
//
// Zip file
//
//==========================================================================
FZipFile::FZipFile(const char * filename, FileReader &file, StringPool* sp)
: FResourceFile(filename, file, sp)
{
Lumps = NULL;
}
bool FZipFile::Open(LumpFilterInfo* filter, FileSystemMessageFunc Printf)
@ -159,8 +133,6 @@ bool FZipFile::Open(LumpFilterInfo* filter, FileSystemMessageFunc Printf)
uint32_t centraldir = Zip_FindCentralDir(Reader, &zip64);
int skipped = 0;
Lumps = NULL;
if (centraldir == 0)
{
Printf(FSMessageLevel::Error, "%s: ZIP file corrupt!\n", FileName);
@ -206,15 +178,12 @@ bool FZipFile::Open(LumpFilterInfo* filter, FileSystemMessageFunc Printf)
dirsize = info.DirectorySize;
DirectoryOffset = info.DirectoryOffset;
}
Lumps = new FZipLump[NumLumps];
// Load the entire central directory. Too bad that this contains variable length entries...
void *directory = malloc(dirsize);
Reader.Seek(DirectoryOffset, FileReader::SeekSet);
Reader.Read(directory, dirsize);
char *dirptr = (char*)directory;
FZipLump *lump_p = Lumps;
std::string name0, name1;
bool foundspeciallump = false;
@ -291,7 +260,6 @@ bool FZipFile::Open(LumpFilterInfo* filter, FileSystemMessageFunc Printf)
if (!foundspeciallump) name0 = "";
dirptr = (char*)directory;
lump_p = Lumps;
AllocateEntries(NumLumps);
auto Entry = Entries;
for (uint32_t i = 0; i < NumLumps; i++)
@ -402,49 +370,15 @@ bool FZipFile::Open(LumpFilterInfo* filter, FileSystemMessageFunc Printf)
Entry++;
lump_p->LumpNameSetup(name.c_str(), stringpool);
lump_p->LumpSize = UncompressedSize;
lump_p->Owner = this;
// The start of the Reader will be determined the first time it is accessed.
lump_p->Flags = LUMPF_FULLPATH;
lump_p->NeedFileStart = true;
lump_p->Method = uint8_t(zip_fh->Method);
if (lump_p->Method != METHOD_STORED) lump_p->Flags |= LUMPF_COMPRESSED;
/*if (lump_p->Method == METHOD_IMPLODE)
{
// merge the flags into the compression method to tag less data around.
if ((zip_fh->Flags & 6) == 2) lump_p->Method = METHOD_IMPLODE_2;
if ((zip_fh->Flags & 6) == 4) lump_p->Method = METHOD_IMPLODE_4;
if ((zip_fh->Flags & 6) == 6) lump_p->Method = METHOD_IMPLODE_6;
}*/
lump_p->GPFlags = zip_fh->Flags;
lump_p->CRC32 = zip_fh->CRC32;
lump_p->CompressedSize = CompressedSize;
lump_p->Position = LocalHeaderOffset;
lump_p->CheckEmbedded(filter);
lump_p++;
}
// Resize the lump record array to its actual size
NumLumps -= skipped;
free(directory);
GenerateHash();
PostProcessArchive(&Lumps[0], sizeof(FZipLump), filter);
return true;
}
//==========================================================================
//
// Zip file
//
//==========================================================================
FZipFile::~FZipFile()
{
if (Lumps != NULL) delete [] Lumps;
}
//==========================================================================
//
//
@ -463,8 +397,7 @@ FCompressedBuffer FZipFile::GetRawData(uint32_t entry)
{
auto& e = Entries[entry];
cbuf = { e.Length, e.CompressedSize, e.Method, e.CRC32, new char[e.CompressedSize] };
if (e.Flags & RESFF_NEEDFILESTART) Lumps[entry].SetLumpAddress();
e.Position = Lumps[entry].Position;
if (e.Flags & RESFF_NEEDFILESTART) SetEntryAddress(entry);
Reader.Seek(e.Position, FileReader::SeekSet);
Reader.Read(cbuf.mBuffer, e.CompressedSize);
}
@ -478,78 +411,19 @@ FCompressedBuffer FZipFile::GetRawData(uint32_t entry)
//
//==========================================================================
void FZipLump::SetLumpAddress()
void FZipFile::SetEntryAddress(uint32_t entry)
{
if (!NeedFileStart) return;
// This file is inside a zip and has not been opened before.
// Position points to the start of the local file header, which we must
// read and skip so that we can get to the actual file data.
FZipLocalFileHeader localHeader;
int skiplen;
Owner->GetContainerReader()->Seek(Position, FileReader::SeekSet);
Owner->GetContainerReader()->Read(&localHeader, sizeof(localHeader));
Reader.Seek(Entries[entry].Position, FileReader::SeekSet);
Reader.Read(&localHeader, sizeof(localHeader));
skiplen = LittleShort(localHeader.NameLength) + LittleShort(localHeader.ExtraLength);
Position += sizeof(localHeader) + skiplen;
NeedFileStart = false;
}
//==========================================================================
//
// Get reader (only returns non-NULL if not encrypted)
//
//==========================================================================
FileReader *FZipLump::GetReader()
{
// Don't return the reader if this lump is encrypted
// In that case always force caching of the lump
if (Method == METHOD_STORED)
{
if (NeedFileStart) SetLumpAddress();
Owner->GetContainerReader()->Seek(Position, FileReader::SeekSet);
return Owner->GetContainerReader();
}
else return NULL;
}
//==========================================================================
//
// Fills the lump cache and performs decompression
//
//==========================================================================
int FZipLump::FillCache()
{
if (NeedFileStart) SetLumpAddress();
const char *buffer;
if (Method == METHOD_STORED && (buffer = Owner->GetContainerReader()->GetBuffer()) != NULL)
{
// This is an in-memory file so the cache can point directly to the file's data.
Cache = const_cast<char*>(buffer) + Position;
RefCount = -1;
return -1;
}
Owner->GetContainerReader()->Seek(Position, FileReader::SeekSet);
Cache = new char[LumpSize];
UncompressZipLump(Cache, *Owner->GetContainerReader(), Method, LumpSize, CompressedSize, GPFlags, true);
RefCount = 1;
return 1;
}
//==========================================================================
//
//
//
//==========================================================================
int FZipLump::GetFileOffset()
{
if (Method != METHOD_STORED) return -1;
if (NeedFileStart) SetLumpAddress();
return (int)Position;
Entries[entry].Position += sizeof(localHeader) + skiplen;
Entries[entry].Flags &= ~RESFF_NEEDFILESTART;
}
//==========================================================================

View file

@ -4,45 +4,7 @@
#include "resourcefile.h"
namespace FileSys {
//==========================================================================
//
// Zip Lump
//
//==========================================================================
struct FZipLump : public FResourceLump
{
uint16_t GPFlags;
uint8_t Method;
bool NeedFileStart;
int CompressedSize;
int64_t Position;
unsigned CRC32;
virtual FileReader *GetReader();
virtual int FillCache() override;
void SetLumpAddress();
virtual int GetFileOffset();
};
//==========================================================================
//
// Zip file
//
//==========================================================================
class FZipFile : public FResourceFile
{
FZipLump *Lumps;
public:
FZipFile(const char * filename, FileReader &file, StringPool* sp);
virtual ~FZipFile();
bool Open(LumpFilterInfo* filter, FileSystemMessageFunc Printf);
virtual FResourceLump *GetLump(int no) { return ((unsigned)no < NumLumps)? &Lumps[no] : NULL; }
FCompressedBuffer GetRawData(uint32_t entry) override;
};
}

View file

@ -781,25 +781,6 @@ int FileSystem::FileLength (int lump) const
return lump_p.resfile->Length(lump_p.resindex);
}
//==========================================================================
//
// GetFileOffset
//
// Returns the offset from the beginning of the file to the lump.
// Returns -1 if the lump is compressed or can't be read directly
//
//==========================================================================
int FileSystem::GetFileOffset (int lump)
{
if ((size_t)lump >= NumEntries)
{
return -1;
}
const auto &lump_p = FileInfo[lump];
return lump_p.resfile->Offset(lump_p.resindex);
}
//==========================================================================
//
//

View file

@ -709,6 +709,10 @@ FileReader FUncompressedFile::GetEntryReader(uint32_t entry, bool newreader)
FileReader fr;
if (entry < NumLumps)
{
if (Entries[entry].Flags & RESFF_NEEDFILESTART)
{
SetEntryAddress(entry);
}
if (!(Entries[entry].Flags & RESFF_COMPRESSED))
{
if (!newreader)