- transition to new FileReader interface complete, but untested.

This commit is contained in:
Christoph Oelckers 2018-03-11 12:33:46 +01:00
commit b14ee50d0d
31 changed files with 1253 additions and 1708 deletions

View file

@ -51,7 +51,7 @@
//
//==========================================================================
static bool UncompressZipLump(char *Cache, FileReader *Reader, int Method, int LumpSize, int CompressedSize, int GPFlags)
static bool UncompressZipLump(char *Cache, FileRdr &Reader, int Method, int LumpSize, int CompressedSize, int GPFlags)
{
try
{
@ -59,31 +59,23 @@ static bool UncompressZipLump(char *Cache, FileReader *Reader, int Method, int L
{
case METHOD_STORED:
{
Reader->Read(Cache, LumpSize);
Reader.Read(Cache, LumpSize);
break;
}
case METHOD_DEFLATE:
{
FileReaderZ frz(*Reader, true);
frz.Read(Cache, LumpSize);
break;
}
case METHOD_BZIP2:
{
FileReaderBZ2 frz(*Reader);
frz.Read(Cache, LumpSize);
break;
}
case METHOD_LZMA:
{
FileReaderLZMA frz(*Reader, LumpSize, true);
frz.Read(Cache, LumpSize);
FileRdr frz;
if (frz.OpenDecompressor(Reader, LumpSize, Method, false))
{
frz.Read(Cache, LumpSize);
}
break;
}
// Fixme: These should also use a stream
case METHOD_IMPLODE:
{
FZipExploder exploder;
@ -112,8 +104,9 @@ static bool UncompressZipLump(char *Cache, FileReader *Reader, int Method, int L
bool FCompressedBuffer::Decompress(char *destbuffer)
{
MemoryReader mr(mBuffer, mCompressedSize);
return UncompressZipLump(destbuffer, &mr, mMethod, mSize, mCompressedSize, mZipFlags);
FileRdr mr;
mr.OpenMemory(mBuffer, mCompressedSize);
return UncompressZipLump(destbuffer, mr, mMethod, mSize, mCompressedSize, mZipFlags);
}
//-----------------------------------------------------------------------
@ -123,7 +116,7 @@ bool FCompressedBuffer::Decompress(char *destbuffer)
//
//-----------------------------------------------------------------------
static uint32_t Zip_FindCentralDir(FileReader * fin)
static uint32_t Zip_FindCentralDir(FileRdr &fin)
{
unsigned char buf[BUFREADCOMMENT + 4];
uint32_t FileSize;
@ -131,9 +124,9 @@ static uint32_t Zip_FindCentralDir(FileReader * fin)
uint32_t uMaxBack; // maximum size of global comment
uint32_t uPosFound=0;
fin->Seek(0, SEEK_END);
fin.Seek(0, FileRdr::SeekEnd);
FileSize = fin->Tell();
FileSize = (uint32_t)fin.Tell();
uMaxBack = MIN<uint32_t>(0xffff, FileSize);
uBackRead = 4;
@ -149,9 +142,9 @@ static uint32_t Zip_FindCentralDir(FileReader * fin)
uReadSize = MIN<uint32_t>((BUFREADCOMMENT + 4), (FileSize - uReadPos));
if (fin->Seek(uReadPos, SEEK_SET) != 0) break;
if (fin.Seek(uReadPos, FileRdr::SeekSet) != 0) break;
if (fin->Read(buf, (int32_t)uReadSize) != (int32_t)uReadSize) break;
if (fin.Read(buf, (int32_t)uReadSize) != (int32_t)uReadSize) break;
for (i = (int)uReadSize - 3; (i--) > 0;)
{
@ -174,7 +167,7 @@ static uint32_t Zip_FindCentralDir(FileReader * fin)
//
//==========================================================================
FZipFile::FZipFile(const char * filename, FileReader *file)
FZipFile::FZipFile(const char * filename, FileRdr &file)
: FResourceFile(filename, file)
{
Lumps = NULL;
@ -195,8 +188,8 @@ bool FZipFile::Open(bool quiet)
}
// Read the central directory info.
Reader->Seek(centraldir, SEEK_SET);
Reader->Read(&info, sizeof(FZipEndOfCentralDirectory));
Reader.Seek(centraldir, FileRdr::SeekSet);
Reader.Read(&info, sizeof(FZipEndOfCentralDirectory));
// No multi-disk zips!
if (info.NumEntries != info.NumEntriesOnAllDisks ||
@ -212,8 +205,8 @@ bool FZipFile::Open(bool quiet)
// Load the entire central directory. Too bad that this contains variable length entries...
int dirsize = LittleLong(info.DirectorySize);
void *directory = malloc(dirsize);
Reader->Seek(LittleLong(info.DirectoryOffset), SEEK_SET);
Reader->Read(directory, dirsize);
Reader.Seek(LittleLong(info.DirectoryOffset), FileRdr::SeekSet);
Reader.Read(directory, dirsize);
char *dirptr = (char*)directory;
FZipLump *lump_p = Lumps;
@ -349,6 +342,7 @@ bool FZipFile::Open(bool quiet)
// The start of the Reader will be determined the first time it is accessed.
lump_p->Flags = LUMPF_ZIPFILE | LUMPFZIP_NEEDFILESTART;
lump_p->Method = uint8_t(zip_fh->Method);
if (lump_p->Method != METHOD_STORED) lump_p->Flags |= LUMPF_COMPRESSED;
lump_p->GPFlags = zip_fh->Flags;
lump_p->CRC32 = zip_fh->CRC32;
lump_p->CompressedSize = LittleLong(zip_fh->CompressedSize);
@ -394,8 +388,8 @@ FCompressedBuffer FZipLump::GetRawData()
{
FCompressedBuffer cbuf = { (unsigned)LumpSize, (unsigned)CompressedSize, Method, GPFlags, CRC32, new char[CompressedSize] };
if (Flags & LUMPFZIP_NEEDFILESTART) SetLumpAddress();
Owner->Reader->Seek(Position, SEEK_SET);
Owner->Reader->Read(cbuf.mBuffer, CompressedSize);
Owner->Reader.Seek(Position, FileRdr::SeekSet);
Owner->Reader.Read(cbuf.mBuffer, CompressedSize);
return cbuf;
}
@ -413,10 +407,8 @@ void FZipLump::SetLumpAddress()
FZipLocalFileHeader localHeader;
int skiplen;
FileReader *file = Owner->Reader;
file->Seek(Position, SEEK_SET);
file->Read(&localHeader, sizeof(localHeader));
Owner->Reader.Seek(Position, FileRdr::SeekSet);
Owner->Reader.Read(&localHeader, sizeof(localHeader));
skiplen = LittleShort(localHeader.NameLength) + LittleShort(localHeader.ExtraLength);
Position += sizeof(localHeader) + skiplen;
Flags &= ~LUMPFZIP_NEEDFILESTART;
@ -428,15 +420,15 @@ void FZipLump::SetLumpAddress()
//
//==========================================================================
FileReader *FZipLump::GetReader()
FileRdr *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 (Flags & LUMPFZIP_NEEDFILESTART) SetLumpAddress();
Owner->Reader->Seek(Position, SEEK_SET);
return Owner->Reader;
Owner->Reader.Seek(Position, FileRdr::SeekSet);
return &Owner->Reader;
}
else return NULL;
}
@ -452,7 +444,7 @@ int FZipLump::FillCache()
if (Flags & LUMPFZIP_NEEDFILESTART) SetLumpAddress();
const char *buffer;
if (Method == METHOD_STORED && (buffer = Owner->Reader->GetBuffer()) != NULL)
if (Method == METHOD_STORED && (buffer = Owner->Reader.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;
@ -460,7 +452,7 @@ int FZipLump::FillCache()
return -1;
}
Owner->Reader->Seek(Position, SEEK_SET);
Owner->Reader.Seek(Position, FileRdr::SeekSet);
Cache = new char[LumpSize];
UncompressZipLump(Cache, Owner->Reader, Method, LumpSize, CompressedSize, GPFlags);
RefCount = 1;
@ -486,21 +478,21 @@ int FZipLump::GetFileOffset()
//
//==========================================================================
FResourceFile *CheckZip(const char *filename, FileReader *file, bool quiet)
FResourceFile *CheckZip(const char *filename, FileRdr &file, bool quiet)
{
char head[4];
if (file->GetLength() >= (long)sizeof(FZipLocalFileHeader))
if (file.GetLength() >= (long)sizeof(FZipLocalFileHeader))
{
file->Seek(0, SEEK_SET);
file->Read(&head, 4);
file->Seek(0, SEEK_SET);
file.Seek(0, FileRdr::SeekSet);
file.Read(&head, 4);
file.Seek(0, FileRdr::SeekSet);
if (!memcmp(head, "PK\x3\x4", 4))
{
FResourceFile *rf = new FZipFile(filename, file);
if (rf->Open(quiet)) return rf;
rf->Reader = NULL; // to avoid destruction of reader
file = std::move(rf->Reader); // to avoid destruction of reader
delete rf;
}
}