- put the entire filesystem code into a namespace and created some subdirectories.
This commit is contained in:
parent
94c9ee4593
commit
ebb71cebf1
108 changed files with 308 additions and 225 deletions
439
src/common/filesystem/source/ancientzip.cpp
Normal file
439
src/common/filesystem/source/ancientzip.cpp
Normal file
|
|
@ -0,0 +1,439 @@
|
|||
/*
|
||||
** ancientzip.cpp
|
||||
**
|
||||
**---------------------------------------------------------------------------
|
||||
** Copyright 2010-2011 Randy Heit
|
||||
** All rights reserved.
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions
|
||||
** are met:
|
||||
**
|
||||
** 1. Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** 2. Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in the
|
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3. The name of the author may not be used to endorse or promote products
|
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**---------------------------------------------------------------------------
|
||||
**
|
||||
** Based in information from
|
||||
**
|
||||
gunzip.c by Pasi Ojala, a1bert@iki.fi
|
||||
http://www.iki.fi/a1bert/
|
||||
|
||||
A hopefully easier to understand guide to GZip
|
||||
(deflate) decompression routine than the GZip
|
||||
source code.
|
||||
|
||||
*/
|
||||
|
||||
/*----------------------------------------------------------------------*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "ancientzip.h"
|
||||
|
||||
namespace FileSys {
|
||||
|
||||
/****************************************************************
|
||||
Bit-I/O variables and routines/macros
|
||||
|
||||
These routines work in the bit level because the target
|
||||
environment does not have a barrel shifter. Trying to
|
||||
handle several bits at once would've only made the code
|
||||
slower.
|
||||
|
||||
If the environment supports multi-bit shifts, you should
|
||||
write these routines again (see e.g. the GZIP sources).
|
||||
|
||||
[RH] Since the target environment is not a C64, I did as
|
||||
suggested and rewrote these using zlib as a reference.
|
||||
|
||||
****************************************************************/
|
||||
|
||||
#define READBYTE(c) \
|
||||
do { \
|
||||
c = 0; \
|
||||
if (InLeft) { \
|
||||
InLeft--; \
|
||||
if (bs < be) \
|
||||
c = ReadBuf[bs++]; \
|
||||
else { \
|
||||
be = (decltype(be))In->Read(&ReadBuf, sizeof(ReadBuf)); \
|
||||
c = ReadBuf[0]; \
|
||||
bs = 1; \
|
||||
} \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/* Get a byte of input into the bit accumulator. */
|
||||
#define PULLBYTE() \
|
||||
do { \
|
||||
unsigned char next; \
|
||||
READBYTE(next); \
|
||||
Hold += (unsigned int)(next) << Bits; \
|
||||
Bits += 8; \
|
||||
} while (0)
|
||||
|
||||
/* Assure that there are at least n bits in the bit accumulator. */
|
||||
#define NEEDBITS(n) \
|
||||
do { \
|
||||
while (Bits < (unsigned)(n)) \
|
||||
PULLBYTE(); \
|
||||
} while (0)
|
||||
|
||||
/* Return the low n bits of the bit accumulator (n < 16) */
|
||||
#define BITS(n) \
|
||||
((unsigned)Hold & ((1U << (n)) - 1))
|
||||
|
||||
/* Remove n bits from the bit accumulator */
|
||||
#define DROPBITS(n) \
|
||||
do { \
|
||||
Hold >>= (n); \
|
||||
Bits -= (unsigned)(n); \
|
||||
} while (0)
|
||||
|
||||
#define READBITS(c, a) \
|
||||
do { \
|
||||
NEEDBITS(a); \
|
||||
c = BITS(a); \
|
||||
DROPBITS(a); \
|
||||
} while (0)
|
||||
|
||||
/****************************************************************
|
||||
Shannon-Fano tree routines
|
||||
****************************************************************/
|
||||
|
||||
static const unsigned char BitReverse4[] = {
|
||||
0x00, 0x08, 0x04, 0x0c, 0x02, 0x0a, 0x06, 0x0e,
|
||||
0x01, 0x09, 0x05, 0x0d, 0x03, 0x0b, 0x07, 0x0f
|
||||
};
|
||||
|
||||
#define FIRST_BIT_LEN 8
|
||||
#define REST_BIT_LEN 4
|
||||
|
||||
void FZipExploder::InsertCode(TArray<HuffNode> &decoder, unsigned int pos, int bits, unsigned short code, int len, unsigned char value)
|
||||
{
|
||||
assert(len > 0);
|
||||
unsigned int node = pos + (code & ((1 << bits) - 1));
|
||||
|
||||
if (len > bits)
|
||||
{ // This code uses more bits than this level has room for. Store the bottom bits
|
||||
// in this table, then proceed to the next one.
|
||||
unsigned int child = decoder[node].ChildTable;
|
||||
if (child == 0)
|
||||
{ // Need to create child table.
|
||||
child = InitTable(decoder, 1 << REST_BIT_LEN);
|
||||
decoder[node].ChildTable = child;
|
||||
decoder[node].Length = bits;
|
||||
decoder[node].Value = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(decoder[node].Length == bits);
|
||||
assert(decoder[node].Value == 0);
|
||||
}
|
||||
InsertCode(decoder, child, REST_BIT_LEN, code >> bits, len - bits, value);
|
||||
}
|
||||
else
|
||||
{ // If this code uses fewer bits than this level of the table, it is
|
||||
// inserted repeatedly for each value that matches it at its lower
|
||||
// bits.
|
||||
for (int i = 1 << (bits - len); --i >= 0; node += 1 << len)
|
||||
{
|
||||
decoder[node].Length = len;
|
||||
decoder[node].Value = value;
|
||||
assert(decoder[node].ChildTable == 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int FZipExploder::InitTable(TArray<HuffNode> &decoder, int numspots)
|
||||
{
|
||||
unsigned int start = decoder.Size();
|
||||
decoder.Reserve(numspots);
|
||||
memset(&decoder[start], 0, sizeof(HuffNode)*numspots);
|
||||
return start;
|
||||
}
|
||||
|
||||
int FZipExploder::buildercmp(const void *a, const void *b)
|
||||
{
|
||||
const TableBuilder *v1 = (const TableBuilder *)a;
|
||||
const TableBuilder *v2 = (const TableBuilder *)b;
|
||||
int d = v1->Length - v2->Length;
|
||||
if (d == 0) {
|
||||
d = v1->Value - v2->Value;
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
int FZipExploder::BuildDecoder(TArray<HuffNode> &decoder, TableBuilder *values, int numvals)
|
||||
{
|
||||
int i;
|
||||
|
||||
qsort(values, numvals, sizeof(*values), buildercmp);
|
||||
|
||||
// Generate the Shannon-Fano tree:
|
||||
unsigned short code = 0;
|
||||
unsigned short code_increment = 0;
|
||||
unsigned short last_bit_length = 0;
|
||||
for (i = numvals - 1; i >= 0; --i)
|
||||
{
|
||||
code += code_increment;
|
||||
if (values[i].Length != last_bit_length)
|
||||
{
|
||||
last_bit_length = values[i].Length;
|
||||
code_increment = 1 << (16 - last_bit_length);
|
||||
}
|
||||
// Reverse the order of the bits in the code before storing it.
|
||||
values[i].Code = BitReverse4[code >> 12] |
|
||||
(BitReverse4[(code >> 8) & 0xf] << 4) |
|
||||
(BitReverse4[(code >> 4) & 0xf] << 8) |
|
||||
(BitReverse4[code & 0xf] << 12);
|
||||
}
|
||||
|
||||
// Insert each code into the hierarchical table. The top level is FIRST_BIT_LEN bits,
|
||||
// and the other levels are REST_BIT_LEN bits. If a code does not completely fill
|
||||
// a level, every permutation for the remaining bits is filled in to
|
||||
// match this one.
|
||||
InitTable(decoder, 1 << FIRST_BIT_LEN); // Set up the top level.
|
||||
for (i = 0; i < numvals; ++i)
|
||||
{
|
||||
InsertCode(decoder, 0, FIRST_BIT_LEN, values[i].Code, values[i].Length, values[i].Value);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int FZipExploder::DecodeSFValue(const TArray<HuffNode> &decoder)
|
||||
{
|
||||
unsigned int bits = FIRST_BIT_LEN, table = 0, code;
|
||||
const HuffNode *pos;
|
||||
do
|
||||
{
|
||||
NEEDBITS(bits);
|
||||
code = BITS(bits);
|
||||
bits = REST_BIT_LEN;
|
||||
pos = &decoder[table + code];
|
||||
DROPBITS(pos->Length);
|
||||
table = pos->ChildTable;
|
||||
}
|
||||
while (table != 0);
|
||||
return pos->Value;
|
||||
}
|
||||
|
||||
|
||||
int FZipExploder::DecodeSF(TArray<HuffNode> &decoder, int numvals)
|
||||
{
|
||||
TableBuilder builder[256];
|
||||
unsigned char a, c;
|
||||
int i, n, v = 0;
|
||||
|
||||
READBYTE(c);
|
||||
n = c + 1;
|
||||
for (i = 0; i < n; i++) {
|
||||
int nv, bl;
|
||||
READBYTE(a);
|
||||
nv = ((a >> 4) & 15) + 1;
|
||||
bl = (a & 15) + 1;
|
||||
while (nv--) {
|
||||
builder[v].Length = bl;
|
||||
builder[v].Value = v;
|
||||
v++;
|
||||
}
|
||||
}
|
||||
if (v != numvals)
|
||||
return 1; /* bad table */
|
||||
return BuildDecoder(decoder, builder, v);
|
||||
}
|
||||
|
||||
int FZipExploder::Explode(unsigned char *out, unsigned int outsize,
|
||||
FileReader &in, unsigned int insize,
|
||||
int flags)
|
||||
{
|
||||
int c, i, minMatchLen = 3, len, dist;
|
||||
int lowDistanceBits;
|
||||
unsigned int bIdx = 0;
|
||||
|
||||
Hold = 0;
|
||||
Bits = 0;
|
||||
In = ∈
|
||||
InLeft = insize;
|
||||
bs = be = 0;
|
||||
|
||||
if ((flags & 4)) {
|
||||
/* 3 trees: literals, lengths, distance top 6 */
|
||||
minMatchLen = 3;
|
||||
if (DecodeSF(LiteralDecoder, 256))
|
||||
return 1;
|
||||
} else {
|
||||
/* 2 trees: lengths, distance top 6 */
|
||||
minMatchLen = 2;
|
||||
}
|
||||
if (DecodeSF(LengthDecoder, 64))
|
||||
return 1;
|
||||
if (DecodeSF(DistanceDecoder, 64))
|
||||
return 1;
|
||||
|
||||
lowDistanceBits = (flags & 2) ? /* 8k dictionary */ 7 : /* 4k dictionary */ 6;
|
||||
while (bIdx < outsize)
|
||||
{
|
||||
READBITS(c, 1);
|
||||
if (c) {
|
||||
/* literal data */
|
||||
if ((flags & 4)) {
|
||||
c = DecodeSFValue(LiteralDecoder);
|
||||
} else {
|
||||
READBITS(c, 8);
|
||||
}
|
||||
out[bIdx++] = c;
|
||||
} else {
|
||||
READBITS(dist, lowDistanceBits);
|
||||
c = DecodeSFValue(DistanceDecoder);
|
||||
dist |= (c << lowDistanceBits);
|
||||
len = DecodeSFValue(LengthDecoder);
|
||||
if (len == 63) {
|
||||
READBITS(c, 8);
|
||||
len += c;
|
||||
}
|
||||
len += minMatchLen;
|
||||
dist++;
|
||||
if (bIdx + len > outsize) {
|
||||
return -1;
|
||||
}
|
||||
if ((unsigned int)dist > bIdx) {
|
||||
/* Anything before the first input byte is zero. */
|
||||
int zeros = dist - bIdx;
|
||||
if (len < zeros)
|
||||
zeros = len;
|
||||
for(i = zeros; i; i--)
|
||||
out[bIdx++] = 0;
|
||||
len -= zeros;
|
||||
}
|
||||
for(i = len; i; i--, bIdx++) {
|
||||
out[bIdx] = out[bIdx - dist];
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* HSIZE is defined as 2^13 (8192) in unzip.h */
|
||||
#define HSIZE 8192
|
||||
#define BOGUSCODE 256
|
||||
#define CODE_MASK (HSIZE - 1) /* 0x1fff (lower bits are parent's index) */
|
||||
#define FREE_CODE HSIZE /* 0x2000 (code is unused or was cleared) */
|
||||
#define HAS_CHILD (HSIZE << 1) /* 0x4000 (code has a child--do not clear) */
|
||||
|
||||
int ShrinkLoop(unsigned char *out, unsigned int outsize, FileReader &_In, unsigned int InLeft)
|
||||
{
|
||||
FileReader *In = &_In;
|
||||
unsigned char ReadBuf[256];
|
||||
unsigned short Parent[HSIZE];
|
||||
unsigned char Value[HSIZE], Stack[HSIZE];
|
||||
unsigned char *newstr;
|
||||
int len;
|
||||
int KwKwK, codesize = 9; /* start at 9 bits/code */
|
||||
int code, oldcode, freecode, curcode;
|
||||
unsigned int Bits = 0, Hold = 0;
|
||||
unsigned int size = 0;
|
||||
unsigned int bs = 0, be = 0;
|
||||
|
||||
freecode = BOGUSCODE;
|
||||
for (code = 0; code < BOGUSCODE; code++)
|
||||
{
|
||||
Value[code] = code;
|
||||
Parent[code] = BOGUSCODE;
|
||||
}
|
||||
for (code = BOGUSCODE+1; code < HSIZE; code++)
|
||||
Parent[code] = FREE_CODE;
|
||||
|
||||
READBITS(oldcode, codesize);
|
||||
if (size < outsize) {
|
||||
out[size++] = oldcode;
|
||||
}
|
||||
|
||||
while (size < outsize)
|
||||
{
|
||||
READBITS(code, codesize);
|
||||
if (code == BOGUSCODE) { /* possible to have consecutive escapes? */
|
||||
READBITS(code, codesize);
|
||||
if (code == 1) {
|
||||
codesize++;
|
||||
} else if (code == 2) {
|
||||
/* clear leafs (nodes with no children) */
|
||||
/* first loop: mark each parent as such */
|
||||
for (code = BOGUSCODE+1; code < HSIZE; ++code) {
|
||||
curcode = (Parent[code] & CODE_MASK);
|
||||
|
||||
if (curcode > BOGUSCODE)
|
||||
Parent[curcode] |= HAS_CHILD; /* set parent's child-bit */
|
||||
}
|
||||
|
||||
/* second loop: clear all nodes *not* marked as parents; reset flag bits */
|
||||
for (code = BOGUSCODE+1; code < HSIZE; ++code) {
|
||||
if (Parent[code] & HAS_CHILD) { /* just clear child-bit */
|
||||
Parent[code] &= ~HAS_CHILD;
|
||||
} else { /* leaf: lose it */
|
||||
Parent[code] = FREE_CODE;
|
||||
}
|
||||
}
|
||||
freecode = BOGUSCODE;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
newstr = &Stack[HSIZE-1];
|
||||
curcode = code;
|
||||
|
||||
if (Parent[curcode] == FREE_CODE) {
|
||||
KwKwK = 1;
|
||||
newstr--; /* last character will be same as first character */
|
||||
curcode = oldcode;
|
||||
len = 1;
|
||||
} else {
|
||||
KwKwK = 0;
|
||||
len = 0;
|
||||
}
|
||||
|
||||
do {
|
||||
*newstr-- = Value[curcode];
|
||||
len++;
|
||||
curcode = (Parent[curcode] & CODE_MASK);
|
||||
} while (curcode != BOGUSCODE);
|
||||
|
||||
newstr++;
|
||||
if (KwKwK) {
|
||||
Stack[HSIZE-1] = *newstr;
|
||||
}
|
||||
|
||||
do {
|
||||
freecode++;
|
||||
} while (Parent[freecode] != FREE_CODE);
|
||||
|
||||
Parent[freecode] = oldcode;
|
||||
Value[freecode] = *newstr;
|
||||
oldcode = code;
|
||||
|
||||
while (len--) {
|
||||
out[size++] = *newstr++;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
47
src/common/filesystem/source/ancientzip.h
Normal file
47
src/common/filesystem/source/ancientzip.h
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
#pragma once
|
||||
#include "fs_files.h"
|
||||
|
||||
namespace FileSys {
|
||||
|
||||
class FZipExploder
|
||||
{
|
||||
unsigned int Hold, Bits;
|
||||
FileReader *In;
|
||||
unsigned int InLeft;
|
||||
|
||||
/****************************************************************
|
||||
Shannon-Fano tree structures, variables and related routines
|
||||
****************************************************************/
|
||||
|
||||
struct HuffNode
|
||||
{
|
||||
unsigned char Value;
|
||||
unsigned char Length;
|
||||
unsigned short ChildTable;
|
||||
};
|
||||
|
||||
struct TableBuilder
|
||||
{
|
||||
unsigned char Value;
|
||||
unsigned char Length;
|
||||
unsigned short Code;
|
||||
};
|
||||
|
||||
TArray<HuffNode> LiteralDecoder;
|
||||
TArray<HuffNode> DistanceDecoder;
|
||||
TArray<HuffNode> LengthDecoder;
|
||||
unsigned char ReadBuf[256];
|
||||
unsigned int bs, be;
|
||||
|
||||
static int buildercmp(const void *a, const void *b);
|
||||
void InsertCode(TArray<HuffNode> &decoder, unsigned int pos, int bits, unsigned short code, int len, unsigned char value);
|
||||
unsigned int InitTable(TArray<HuffNode> &decoder, int numspots);
|
||||
int BuildDecoder(TArray<HuffNode> &decoder, TableBuilder *values, int numvals);
|
||||
int DecodeSFValue(const TArray<HuffNode> ¤tTree);
|
||||
int DecodeSF(TArray<HuffNode> &decoder, int numvals);
|
||||
public:
|
||||
int Explode(unsigned char *out, unsigned int outsize, FileReader &in, unsigned int insize, int flags);
|
||||
};
|
||||
|
||||
int ShrinkLoop(unsigned char *out, unsigned int outsize, FileReader &in, unsigned int insize);
|
||||
}
|
||||
378
src/common/filesystem/source/file_7z.cpp
Normal file
378
src/common/filesystem/source/file_7z.cpp
Normal file
|
|
@ -0,0 +1,378 @@
|
|||
/*
|
||||
** file_7z.cpp
|
||||
**
|
||||
**---------------------------------------------------------------------------
|
||||
** Copyright 2009 Randy Heit
|
||||
** Copyright 2009 Christoph Oelckers
|
||||
** All rights reserved.
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions
|
||||
** are met:
|
||||
**
|
||||
** 1. Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** 2. Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in the
|
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3. The name of the author may not be used to endorse or promote products
|
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**---------------------------------------------------------------------------
|
||||
**
|
||||
**
|
||||
*/
|
||||
|
||||
// Note that 7z made the unwise decision to include windows.h :(
|
||||
#include "7z.h"
|
||||
#include "7zCrc.h"
|
||||
#include "resourcefile.h"
|
||||
#include "fs_findfile.h"
|
||||
|
||||
|
||||
namespace FileSys {
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
//
|
||||
// Interface classes to 7z library
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
extern ISzAlloc g_Alloc;
|
||||
|
||||
struct CZDFileInStream
|
||||
{
|
||||
ISeekInStream s;
|
||||
FileReader &File;
|
||||
|
||||
CZDFileInStream(FileReader &_file)
|
||||
: File(_file)
|
||||
{
|
||||
s.Read = Read;
|
||||
s.Seek = Seek;
|
||||
}
|
||||
|
||||
static SRes Read(const ISeekInStream *pp, void *buf, size_t *size)
|
||||
{
|
||||
CZDFileInStream *p = (CZDFileInStream *)pp;
|
||||
auto numread = p->File.Read(buf, (long)*size);
|
||||
if (numread < 0)
|
||||
{
|
||||
*size = 0;
|
||||
return SZ_ERROR_READ;
|
||||
}
|
||||
*size = numread;
|
||||
return SZ_OK;
|
||||
}
|
||||
|
||||
static SRes Seek(const ISeekInStream *pp, Int64 *pos, ESzSeek origin)
|
||||
{
|
||||
CZDFileInStream *p = (CZDFileInStream *)pp;
|
||||
FileReader::ESeek move_method;
|
||||
int res;
|
||||
if (origin == SZ_SEEK_SET)
|
||||
{
|
||||
move_method = FileReader::SeekSet;
|
||||
}
|
||||
else if (origin == SZ_SEEK_CUR)
|
||||
{
|
||||
move_method = FileReader::SeekCur;
|
||||
}
|
||||
else if (origin == SZ_SEEK_END)
|
||||
{
|
||||
move_method = FileReader::SeekEnd;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
res = (int)p->File.Seek((long)*pos, move_method);
|
||||
*pos = p->File.Tell();
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
struct C7zArchive
|
||||
{
|
||||
CSzArEx DB;
|
||||
CZDFileInStream ArchiveStream;
|
||||
CLookToRead2 LookStream;
|
||||
Byte StreamBuffer[1<<14];
|
||||
UInt32 BlockIndex;
|
||||
Byte *OutBuffer;
|
||||
size_t OutBufferSize;
|
||||
|
||||
C7zArchive(FileReader &file) : ArchiveStream(file)
|
||||
{
|
||||
if (g_CrcTable[1] == 0)
|
||||
{
|
||||
CrcGenerateTable();
|
||||
}
|
||||
file.Seek(0, FileReader::SeekSet);
|
||||
LookToRead2_CreateVTable(&LookStream, false);
|
||||
LookStream.realStream = &ArchiveStream.s;
|
||||
LookToRead2_Init(&LookStream);
|
||||
LookStream.bufSize = sizeof(StreamBuffer);
|
||||
LookStream.buf = StreamBuffer;
|
||||
SzArEx_Init(&DB);
|
||||
BlockIndex = 0xFFFFFFFF;
|
||||
OutBuffer = NULL;
|
||||
OutBufferSize = 0;
|
||||
}
|
||||
|
||||
~C7zArchive()
|
||||
{
|
||||
if (OutBuffer != NULL)
|
||||
{
|
||||
IAlloc_Free(&g_Alloc, OutBuffer);
|
||||
}
|
||||
SzArEx_Free(&DB, &g_Alloc);
|
||||
}
|
||||
|
||||
SRes Open()
|
||||
{
|
||||
return SzArEx_Open(&DB, &LookStream.vt, &g_Alloc, &g_Alloc);
|
||||
}
|
||||
|
||||
SRes Extract(UInt32 file_index, char *buffer)
|
||||
{
|
||||
size_t offset, out_size_processed;
|
||||
SRes res = SzArEx_Extract(&DB, &LookStream.vt, file_index,
|
||||
&BlockIndex, &OutBuffer, &OutBufferSize,
|
||||
&offset, &out_size_processed,
|
||||
&g_Alloc, &g_Alloc);
|
||||
if (res == SZ_OK)
|
||||
{
|
||||
memcpy(buffer, OutBuffer + offset, out_size_processed);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
//==========================================================================
|
||||
//
|
||||
// Zip Lump
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
struct F7ZLump : public FResourceLump
|
||||
{
|
||||
int Position;
|
||||
|
||||
virtual int FillCache() override;
|
||||
|
||||
};
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// 7-zip file
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
class F7ZFile : public FResourceFile
|
||||
{
|
||||
friend struct F7ZLump;
|
||||
|
||||
F7ZLump *Lumps;
|
||||
C7zArchive *Archive;
|
||||
|
||||
public:
|
||||
F7ZFile(const char * filename, FileReader &filer, StringPool* sp);
|
||||
bool Open(LumpFilterInfo* filter, FileSystemMessageFunc Printf);
|
||||
virtual ~F7ZFile();
|
||||
virtual FResourceLump *GetLump(int no) { return ((unsigned)no < NumLumps)? &Lumps[no] : NULL; }
|
||||
};
|
||||
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// 7Z file
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
F7ZFile::F7ZFile(const char * filename, FileReader &filer, StringPool* sp)
|
||||
: FResourceFile(filename, filer, sp)
|
||||
{
|
||||
Lumps = NULL;
|
||||
Archive = NULL;
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Open it
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
bool F7ZFile::Open(LumpFilterInfo *filter, FileSystemMessageFunc Printf)
|
||||
{
|
||||
Archive = new C7zArchive(Reader);
|
||||
int skipped = 0;
|
||||
SRes res;
|
||||
|
||||
res = Archive->Open();
|
||||
if (res != SZ_OK)
|
||||
{
|
||||
delete Archive;
|
||||
Archive = NULL;
|
||||
if (res == SZ_ERROR_UNSUPPORTED)
|
||||
{
|
||||
Printf(FSMessageLevel::Error, "%s: Decoder does not support this archive\n", FileName);
|
||||
}
|
||||
else if (res == SZ_ERROR_MEM)
|
||||
{
|
||||
Printf(FSMessageLevel::Error, "Cannot allocate memory\n");
|
||||
}
|
||||
else if (res == SZ_ERROR_CRC)
|
||||
{
|
||||
Printf(FSMessageLevel::Error, "CRC error\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
Printf(FSMessageLevel::Error, "error #%d\n", res);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
CSzArEx* const archPtr = &Archive->DB;
|
||||
|
||||
NumLumps = archPtr->NumFiles;
|
||||
Lumps = new F7ZLump[NumLumps];
|
||||
|
||||
F7ZLump *lump_p = Lumps;
|
||||
std::u16string nameUTF16;
|
||||
std::string nameASCII;
|
||||
|
||||
for (uint32_t i = 0; i < NumLumps; ++i)
|
||||
{
|
||||
// skip Directories
|
||||
if (SzArEx_IsDir(archPtr, i))
|
||||
{
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
|
||||
const size_t nameLength = SzArEx_GetFileNameUtf16(archPtr, i, NULL);
|
||||
|
||||
if (0 == nameLength)
|
||||
{
|
||||
++skipped;
|
||||
continue;
|
||||
}
|
||||
|
||||
nameUTF16.resize((unsigned)nameLength);
|
||||
nameASCII.resize((unsigned)nameLength);
|
||||
// note that the file system is not equipped to handle non-ASCII, so don't bother with proper Unicode conversion here.
|
||||
SzArEx_GetFileNameUtf16(archPtr, i, (UInt16*)nameUTF16.data());
|
||||
for (size_t c = 0; c < nameLength; ++c)
|
||||
{
|
||||
nameASCII[c] = tolower(static_cast<char>(nameUTF16[c]));
|
||||
}
|
||||
FixPathSeparator(&nameASCII.front());
|
||||
|
||||
lump_p->LumpNameSetup(nameASCII.c_str(), stringpool);
|
||||
lump_p->LumpSize = static_cast<int>(SzArEx_GetFileSize(archPtr, i));
|
||||
lump_p->Owner = this;
|
||||
lump_p->Flags = LUMPF_FULLPATH|LUMPF_COMPRESSED;
|
||||
lump_p->Position = i;
|
||||
lump_p->CheckEmbedded(filter);
|
||||
lump_p++;
|
||||
}
|
||||
// Resize the lump record array to its actual size
|
||||
NumLumps -= skipped;
|
||||
|
||||
if (NumLumps > 0)
|
||||
{
|
||||
// Quick check for unsupported compression method
|
||||
|
||||
TArray<char> temp;
|
||||
temp.Resize(Lumps[0].LumpSize);
|
||||
|
||||
if (SZ_OK != Archive->Extract(Lumps[0].Position, &temp[0]))
|
||||
{
|
||||
Printf(FSMessageLevel::Error, "%s: unsupported 7z/LZMA file!\n", FileName);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
GenerateHash();
|
||||
PostProcessArchive(&Lumps[0], sizeof(F7ZLump), filter);
|
||||
return true;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
//
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
F7ZFile::~F7ZFile()
|
||||
{
|
||||
if (Lumps != NULL)
|
||||
{
|
||||
delete[] Lumps;
|
||||
}
|
||||
if (Archive != NULL)
|
||||
{
|
||||
delete Archive;
|
||||
}
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Fills the lump cache and performs decompression
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
int F7ZLump::FillCache()
|
||||
{
|
||||
Cache = new char[LumpSize];
|
||||
SRes code = static_cast<F7ZFile*>(Owner)->Archive->Extract(Position, Cache);
|
||||
if (code != SZ_OK)
|
||||
{
|
||||
throw FileSystemException("Error %d reading from 7z archive", code);
|
||||
}
|
||||
RefCount = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// File open
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FResourceFile *Check7Z(const char *filename, FileReader &file, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp)
|
||||
{
|
||||
char head[k7zSignatureSize];
|
||||
|
||||
if (file.GetLength() >= k7zSignatureSize)
|
||||
{
|
||||
file.Seek(0, FileReader::SeekSet);
|
||||
file.Read(&head, k7zSignatureSize);
|
||||
file.Seek(0, FileReader::SeekSet);
|
||||
if (!memcmp(head, k7zSignature, k7zSignatureSize))
|
||||
{
|
||||
auto rf = new F7ZFile(filename, file, sp);
|
||||
if (rf->Open(filter, Printf)) return rf;
|
||||
|
||||
file = std::move(rf->Reader); // to avoid destruction of reader
|
||||
delete rf;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
236
src/common/filesystem/source/file_directory.cpp
Normal file
236
src/common/filesystem/source/file_directory.cpp
Normal file
|
|
@ -0,0 +1,236 @@
|
|||
/*
|
||||
** file_directory.cpp
|
||||
**
|
||||
**---------------------------------------------------------------------------
|
||||
** Copyright 2008-2009 Randy Heit
|
||||
** Copyright 2009 Christoph Oelckers
|
||||
** All rights reserved.
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions
|
||||
** are met:
|
||||
**
|
||||
** 1. Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** 2. Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in the
|
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3. The name of the author may not be used to endorse or promote products
|
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**---------------------------------------------------------------------------
|
||||
**
|
||||
**
|
||||
*/
|
||||
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "resourcefile.h"
|
||||
#include "fs_findfile.h"
|
||||
#include "fs_stringpool.h"
|
||||
|
||||
namespace FileSys {
|
||||
|
||||
std::string FS_FullPath(const char* directory);
|
||||
|
||||
#ifdef _WIN32
|
||||
std::wstring toWide(const char* str);
|
||||
#endif
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Zip Lump
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
struct FDirectoryLump : public FResourceLump
|
||||
{
|
||||
FileReader NewReader() override;
|
||||
int FillCache() override;
|
||||
|
||||
std::string mFullPath;
|
||||
};
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Zip file
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
class FDirectory : public FResourceFile
|
||||
{
|
||||
TArray<FDirectoryLump> Lumps;
|
||||
const bool nosubdir;
|
||||
|
||||
int AddDirectory(const char* dirpath, LumpFilterInfo* filter, FileSystemMessageFunc Printf);
|
||||
void AddEntry(const char *fullpath, int size);
|
||||
|
||||
public:
|
||||
FDirectory(const char * dirname, StringPool* sp, bool nosubdirflag = false);
|
||||
bool Open(LumpFilterInfo* filter, FileSystemMessageFunc Printf);
|
||||
virtual FResourceLump *GetLump(int no) { return ((unsigned)no < NumLumps)? &Lumps[no] : NULL; }
|
||||
};
|
||||
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
//
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FDirectory::FDirectory(const char * directory, StringPool* sp, bool nosubdirflag)
|
||||
: FResourceFile("", sp), nosubdir(nosubdirflag)
|
||||
{
|
||||
auto fn = FS_FullPath(directory);
|
||||
if (fn.back() != '/') fn += '/';
|
||||
FileName = sp->Strdup(fn.c_str());
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Windows version
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
int FDirectory::AddDirectory(const char *dirpath, LumpFilterInfo* filter, FileSystemMessageFunc Printf)
|
||||
{
|
||||
int count = 0;
|
||||
|
||||
FileList list;
|
||||
if (!ScanDirectory(list, dirpath, "*"))
|
||||
{
|
||||
Printf(FSMessageLevel::Error, "Could not scan '%s': %s\n", dirpath, strerror(errno));
|
||||
}
|
||||
else
|
||||
{
|
||||
for(auto& entry : list)
|
||||
{
|
||||
if (!entry.isDirectory)
|
||||
{
|
||||
auto fi = entry.FileName;
|
||||
for (auto& c : fi) c = tolower(c);
|
||||
if (strstr(fi.c_str(), ".orig") || strstr(fi.c_str(), ".bak") || strstr(fi.c_str(), ".cache"))
|
||||
{
|
||||
// We shouldn't add backup files to the file system
|
||||
continue;
|
||||
}
|
||||
|
||||
if (filter->filenamecheck == nullptr || filter->filenamecheck(fi.c_str(), entry.FilePath.c_str()))
|
||||
{
|
||||
if (entry.Length > 0x7fffffff)
|
||||
{
|
||||
Printf(FSMessageLevel::Warning, "%s is larger than 2GB and will be ignored\n", entry.FilePath.c_str());
|
||||
continue;
|
||||
}
|
||||
AddEntry(entry.FilePathRel.c_str(), (int)entry.Length);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
//
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
bool FDirectory::Open(LumpFilterInfo* filter, FileSystemMessageFunc Printf)
|
||||
{
|
||||
NumLumps = AddDirectory(FileName, filter, Printf);
|
||||
PostProcessArchive(&Lumps[0], sizeof(FDirectoryLump), filter);
|
||||
return true;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
//
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
void FDirectory::AddEntry(const char *fullpath, int size)
|
||||
{
|
||||
FDirectoryLump *lump_p = &Lumps[Lumps.Reserve(1)];
|
||||
|
||||
// Store the full path here so that we can access the file later, even if it is from a filter directory.
|
||||
lump_p->mFullPath = fullpath;
|
||||
|
||||
// [mxd] Convert name to lowercase
|
||||
std::string name = fullpath + strlen(FileName);
|
||||
for (auto& c : name) c = tolower(c);
|
||||
|
||||
// The lump's name is only the part relative to the main directory
|
||||
lump_p->LumpNameSetup(name.c_str(), stringpool);
|
||||
lump_p->LumpSize = size;
|
||||
lump_p->Owner = this;
|
||||
lump_p->Flags = 0;
|
||||
lump_p->CheckEmbedded(nullptr);
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
//
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FileReader FDirectoryLump::NewReader()
|
||||
{
|
||||
FileReader fr;
|
||||
fr.OpenFile(mFullPath.c_str());
|
||||
return fr;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
//
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
int FDirectoryLump::FillCache()
|
||||
{
|
||||
FileReader fr;
|
||||
Cache = new char[LumpSize];
|
||||
if (!fr.OpenFile(mFullPath.c_str()))
|
||||
{
|
||||
throw FileSystemException("unable to open file");
|
||||
}
|
||||
auto read = fr.Read(Cache, LumpSize);
|
||||
if (read != LumpSize)
|
||||
{
|
||||
throw FileSystemException("only read %d of %d bytes", (int)read, (int)LumpSize);
|
||||
}
|
||||
RefCount = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// File open
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FResourceFile *CheckDir(const char *filename, bool nosubdirflag, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp)
|
||||
{
|
||||
auto rf = new FDirectory(filename, sp, nosubdirflag);
|
||||
if (rf->Open(filter, Printf)) return rf;
|
||||
delete rf;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
156
src/common/filesystem/source/file_grp.cpp
Normal file
156
src/common/filesystem/source/file_grp.cpp
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
** file_grp.cpp
|
||||
**
|
||||
**---------------------------------------------------------------------------
|
||||
** Copyright 1998-2009 Randy Heit
|
||||
** Copyright 2005-2009 Christoph Oelckers
|
||||
** All rights reserved.
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions
|
||||
** are met:
|
||||
**
|
||||
** 1. Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** 2. Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in the
|
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3. The name of the author may not be used to endorse or promote products
|
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**---------------------------------------------------------------------------
|
||||
**
|
||||
**
|
||||
*/
|
||||
|
||||
#include "resourcefile.h"
|
||||
#include "fs_swap.h"
|
||||
|
||||
namespace FileSys {
|
||||
using namespace byteswap;
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
//
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
struct GrpHeader
|
||||
{
|
||||
uint32_t Magic[3];
|
||||
uint32_t NumLumps;
|
||||
};
|
||||
|
||||
struct GrpLump
|
||||
{
|
||||
union
|
||||
{
|
||||
struct
|
||||
{
|
||||
char Name[12];
|
||||
uint32_t Size;
|
||||
};
|
||||
char NameWithZero[13];
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Build GRP file
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
class FGrpFile : public FUncompressedFile
|
||||
{
|
||||
public:
|
||||
FGrpFile(const char * filename, FileReader &file, StringPool* sp);
|
||||
bool Open(LumpFilterInfo* filter);
|
||||
};
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Initializes a Build GRP file
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FGrpFile::FGrpFile(const char *filename, FileReader &file, StringPool* sp)
|
||||
: FUncompressedFile(filename, file, sp)
|
||||
{
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Open it
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
bool FGrpFile::Open(LumpFilterInfo* filter)
|
||||
{
|
||||
GrpHeader header;
|
||||
|
||||
Reader.Read(&header, sizeof(header));
|
||||
NumLumps = LittleLong(header.NumLumps);
|
||||
|
||||
GrpLump *fileinfo = new GrpLump[NumLumps];
|
||||
Reader.Read (fileinfo, NumLumps * sizeof(GrpLump));
|
||||
|
||||
Lumps.Resize(NumLumps);
|
||||
|
||||
int Position = sizeof(GrpHeader) + NumLumps * sizeof(GrpLump);
|
||||
|
||||
for(uint32_t i = 0; i < NumLumps; i++)
|
||||
{
|
||||
Lumps[i].Owner = this;
|
||||
Lumps[i].Position = Position;
|
||||
Lumps[i].LumpSize = LittleLong(fileinfo[i].Size);
|
||||
Position += fileinfo[i].Size;
|
||||
Lumps[i].Flags = 0;
|
||||
fileinfo[i].NameWithZero[12] = '\0'; // Be sure filename is null-terminated
|
||||
Lumps[i].LumpNameSetup(fileinfo[i].NameWithZero, stringpool);
|
||||
}
|
||||
GenerateHash();
|
||||
delete[] fileinfo;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// File open
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FResourceFile *CheckGRP(const char *filename, FileReader &file, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp)
|
||||
{
|
||||
char head[12];
|
||||
|
||||
if (file.GetLength() >= 12)
|
||||
{
|
||||
file.Seek(0, FileReader::SeekSet);
|
||||
file.Read(&head, 12);
|
||||
file.Seek(0, FileReader::SeekSet);
|
||||
if (!memcmp(head, "KenSilverman", 12))
|
||||
{
|
||||
auto rf = new FGrpFile(filename, file, sp);
|
||||
if (rf->Open(filter)) return rf;
|
||||
|
||||
file = std::move(rf->Reader); // to avoid destruction of reader
|
||||
delete rf;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
}
|
||||
97
src/common/filesystem/source/file_lump.cpp
Normal file
97
src/common/filesystem/source/file_lump.cpp
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
/*
|
||||
** file_lump.cpp
|
||||
**
|
||||
**---------------------------------------------------------------------------
|
||||
** Copyright 2009 Christoph Oelckers
|
||||
** All rights reserved.
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions
|
||||
** are met:
|
||||
**
|
||||
** 1. Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** 2. Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in the
|
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3. The name of the author may not be used to endorse or promote products
|
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**---------------------------------------------------------------------------
|
||||
**
|
||||
**
|
||||
*/
|
||||
|
||||
#include "resourcefile.h"
|
||||
|
||||
namespace FileSys {
|
||||
//==========================================================================
|
||||
//
|
||||
// Single lump
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
class FLumpFile : public FUncompressedFile
|
||||
{
|
||||
public:
|
||||
FLumpFile(const char * filename, FileReader &file, StringPool* sp);
|
||||
bool Open(LumpFilterInfo* filter);
|
||||
};
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// FLumpFile::FLumpFile
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FLumpFile::FLumpFile(const char *filename, FileReader &file, StringPool* sp)
|
||||
: FUncompressedFile(filename, file, sp)
|
||||
{
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Open it
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
bool FLumpFile::Open(LumpFilterInfo*)
|
||||
{
|
||||
Lumps.Resize(1);
|
||||
Lumps[0].LumpNameSetup(ExtractBaseName(FileName, true).c_str(), stringpool);
|
||||
Lumps[0].Owner = this;
|
||||
Lumps[0].Position = 0;
|
||||
Lumps[0].LumpSize = (int)Reader.GetLength();
|
||||
Lumps[0].Flags = 0;
|
||||
NumLumps = 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// File open
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FResourceFile *CheckLump(const char *filename, FileReader &file, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp)
|
||||
{
|
||||
// always succeeds
|
||||
auto rf = new FLumpFile(filename, file, sp);
|
||||
if (rf->Open(filter)) return rf;
|
||||
file = std::move(rf->Reader); // to avoid destruction of reader
|
||||
delete rf;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
}
|
||||
149
src/common/filesystem/source/file_pak.cpp
Normal file
149
src/common/filesystem/source/file_pak.cpp
Normal file
|
|
@ -0,0 +1,149 @@
|
|||
/*
|
||||
** file_pak.cpp
|
||||
**
|
||||
**---------------------------------------------------------------------------
|
||||
** Copyright 2009 Christoph Oelckers
|
||||
** All rights reserved.
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions
|
||||
** are met:
|
||||
**
|
||||
** 1. Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** 2. Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in the
|
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3. The name of the author may not be used to endorse or promote products
|
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**---------------------------------------------------------------------------
|
||||
**
|
||||
**
|
||||
*/
|
||||
|
||||
#include "resourcefile.h"
|
||||
|
||||
namespace FileSys {
|
||||
|
||||
using namespace byteswap;
|
||||
//==========================================================================
|
||||
//
|
||||
//
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
struct dpackfile_t
|
||||
{
|
||||
char name[56];
|
||||
uint32_t filepos, filelen;
|
||||
} ;
|
||||
|
||||
struct dpackheader_t
|
||||
{
|
||||
uint32_t ident; // == IDPAKHEADER
|
||||
uint32_t dirofs;
|
||||
uint32_t dirlen;
|
||||
} ;
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Wad file
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
class FPakFile : public FUncompressedFile
|
||||
{
|
||||
public:
|
||||
FPakFile(const char * filename, FileReader &file, StringPool* sp);
|
||||
bool Open(LumpFilterInfo* filter);
|
||||
};
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// FWadFile::FWadFile
|
||||
//
|
||||
// Initializes a WAD file
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FPakFile::FPakFile(const char *filename, FileReader &file, StringPool* sp)
|
||||
: FUncompressedFile(filename, file, sp)
|
||||
{
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Open it
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
bool FPakFile::Open(LumpFilterInfo* filter)
|
||||
{
|
||||
dpackheader_t header;
|
||||
|
||||
Reader.Read(&header, sizeof(header));
|
||||
NumLumps = LittleLong(header.dirlen) / sizeof(dpackfile_t);
|
||||
header.dirofs = LittleLong(header.dirofs);
|
||||
|
||||
TArray<dpackfile_t> fileinfo(NumLumps, true);
|
||||
Reader.Seek (header.dirofs, FileReader::SeekSet);
|
||||
Reader.Read (fileinfo.Data(), NumLumps * sizeof(dpackfile_t));
|
||||
|
||||
Lumps.Resize(NumLumps);
|
||||
|
||||
for(uint32_t i = 0; i < NumLumps; i++)
|
||||
{
|
||||
Lumps[i].LumpNameSetup(fileinfo[i].name, stringpool);
|
||||
Lumps[i].Flags = LUMPF_FULLPATH;
|
||||
Lumps[i].Owner = this;
|
||||
Lumps[i].Position = LittleLong(fileinfo[i].filepos);
|
||||
Lumps[i].LumpSize = LittleLong(fileinfo[i].filelen);
|
||||
Lumps[i].CheckEmbedded(filter);
|
||||
}
|
||||
GenerateHash();
|
||||
PostProcessArchive(&Lumps[0], sizeof(Lumps[0]), filter);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// File open
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FResourceFile *CheckPak(const char *filename, FileReader &file, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp)
|
||||
{
|
||||
char head[4];
|
||||
|
||||
if (file.GetLength() >= 12)
|
||||
{
|
||||
file.Seek(0, FileReader::SeekSet);
|
||||
file.Read(&head, 4);
|
||||
file.Seek(0, FileReader::SeekSet);
|
||||
if (!memcmp(head, "PACK", 4))
|
||||
{
|
||||
auto rf = new FPakFile(filename, file, sp);
|
||||
if (rf->Open(filter)) return rf;
|
||||
|
||||
file = std::move(rf->Reader); // to avoid destruction of reader
|
||||
delete rf;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
}
|
||||
264
src/common/filesystem/source/file_rff.cpp
Normal file
264
src/common/filesystem/source/file_rff.cpp
Normal file
|
|
@ -0,0 +1,264 @@
|
|||
/*
|
||||
** file_rff.cpp
|
||||
**
|
||||
**---------------------------------------------------------------------------
|
||||
** Copyright 1998-2009 Randy Heit
|
||||
** Copyright 2005-2009 Christoph Oelckers
|
||||
** All rights reserved.
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions
|
||||
** are met:
|
||||
**
|
||||
** 1. Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** 2. Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in the
|
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3. The name of the author may not be used to endorse or promote products
|
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**---------------------------------------------------------------------------
|
||||
**
|
||||
**
|
||||
*/
|
||||
|
||||
#include "resourcefile.h"
|
||||
#include "fs_swap.h"
|
||||
|
||||
namespace FileSys {
|
||||
using namespace byteswap;
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
//
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
struct RFFInfo
|
||||
{
|
||||
// Should be "RFF\x18"
|
||||
uint32_t Magic;
|
||||
uint32_t Version;
|
||||
uint32_t DirOfs;
|
||||
uint32_t NumLumps;
|
||||
};
|
||||
|
||||
struct RFFLump
|
||||
{
|
||||
uint32_t DontKnow1[4];
|
||||
uint32_t FilePos;
|
||||
uint32_t Size;
|
||||
uint32_t DontKnow2;
|
||||
uint32_t Time;
|
||||
uint8_t Flags;
|
||||
char Extension[3];
|
||||
char Name[8];
|
||||
uint32_t IndexNum; // Used by .sfx, possibly others
|
||||
};
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Blood RFF lump (uncompressed lump with encryption)
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
struct FRFFLump : public FUncompressedLump
|
||||
{
|
||||
virtual FileReader *GetReader();
|
||||
virtual int FillCache() override;
|
||||
|
||||
uint32_t IndexNum;
|
||||
|
||||
int GetIndexNum() const { return IndexNum; }
|
||||
};
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// BloodCrypt
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
void BloodCrypt (void *data, int key, int len)
|
||||
{
|
||||
int p = (uint8_t)key, i;
|
||||
|
||||
for (i = 0; i < len; ++i)
|
||||
{
|
||||
((uint8_t *)data)[i] ^= (unsigned char)(p+(i>>1));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Blood RFF file
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
class FRFFFile : public FResourceFile
|
||||
{
|
||||
FRFFLump *Lumps;
|
||||
|
||||
public:
|
||||
FRFFFile(const char * filename, FileReader &file, StringPool* sp);
|
||||
virtual ~FRFFFile();
|
||||
virtual bool Open(LumpFilterInfo* filter);
|
||||
virtual FResourceLump *GetLump(int no) { return ((unsigned)no < NumLumps)? &Lumps[no] : NULL; }
|
||||
};
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Initializes a Blood RFF file
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FRFFFile::FRFFFile(const char *filename, FileReader &file, StringPool* sp)
|
||||
: FResourceFile(filename, file, sp)
|
||||
{
|
||||
Lumps = NULL;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Initializes a Blood RFF file
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
bool FRFFFile::Open(LumpFilterInfo*)
|
||||
{
|
||||
RFFLump *lumps;
|
||||
RFFInfo header;
|
||||
|
||||
Reader.Read(&header, sizeof(header));
|
||||
|
||||
NumLumps = LittleLong(header.NumLumps);
|
||||
header.DirOfs = LittleLong(header.DirOfs);
|
||||
lumps = new RFFLump[header.NumLumps];
|
||||
Reader.Seek (header.DirOfs, FileReader::SeekSet);
|
||||
Reader.Read (lumps, header.NumLumps * sizeof(RFFLump));
|
||||
BloodCrypt (lumps, header.DirOfs, header.NumLumps * sizeof(RFFLump));
|
||||
|
||||
Lumps = new FRFFLump[NumLumps];
|
||||
|
||||
for (uint32_t i = 0; i < NumLumps; ++i)
|
||||
{
|
||||
Lumps[i].Position = LittleLong(lumps[i].FilePos);
|
||||
Lumps[i].LumpSize = LittleLong(lumps[i].Size);
|
||||
Lumps[i].Owner = this;
|
||||
if (lumps[i].Flags & 0x10)
|
||||
{
|
||||
Lumps[i].Flags |= LUMPF_COMPRESSED; // flags the lump as not directly usable
|
||||
}
|
||||
Lumps[i].IndexNum = LittleLong(lumps[i].IndexNum);
|
||||
// Rearrange the name and extension to construct the fullname.
|
||||
char name[13];
|
||||
strncpy(name, lumps[i].Name, 8);
|
||||
name[8] = 0;
|
||||
size_t len = strlen(name);
|
||||
assert(len + 4 <= 12);
|
||||
name[len+0] = '.';
|
||||
name[len+1] = lumps[i].Extension[0];
|
||||
name[len+2] = lumps[i].Extension[1];
|
||||
name[len+3] = lumps[i].Extension[2];
|
||||
name[len+4] = 0;
|
||||
Lumps[i].LumpNameSetup(name, stringpool);
|
||||
}
|
||||
delete[] lumps;
|
||||
GenerateHash();
|
||||
return true;
|
||||
}
|
||||
|
||||
FRFFFile::~FRFFFile()
|
||||
{
|
||||
if (Lumps != NULL)
|
||||
{
|
||||
delete[] Lumps;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Get reader (only returns non-NULL if not encrypted)
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FileReader *FRFFLump::GetReader()
|
||||
{
|
||||
// Don't return the reader if this lump is encrypted
|
||||
// In that case always force caching of the lump
|
||||
if (!(Flags & LUMPF_COMPRESSED))
|
||||
{
|
||||
return FUncompressedLump::GetReader();
|
||||
}
|
||||
else
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Fills the lump cache and performs decryption
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
int FRFFLump::FillCache()
|
||||
{
|
||||
int res = FUncompressedLump::FillCache();
|
||||
|
||||
if (Flags & LUMPF_COMPRESSED)
|
||||
{
|
||||
int cryptlen = std::min<int> (LumpSize, 256);
|
||||
uint8_t *data = (uint8_t *)Cache;
|
||||
|
||||
for (int i = 0; i < cryptlen; ++i)
|
||||
{
|
||||
data[i] ^= i >> 1;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// File open
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FResourceFile *CheckRFF(const char *filename, FileReader &file, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp)
|
||||
{
|
||||
char head[4];
|
||||
|
||||
if (file.GetLength() >= 16)
|
||||
{
|
||||
file.Seek(0, FileReader::SeekSet);
|
||||
file.Read(&head, 4);
|
||||
file.Seek(0, FileReader::SeekSet);
|
||||
if (!memcmp(head, "RFF\x1a", 4))
|
||||
{
|
||||
auto rf = new FRFFFile(filename, file, sp);
|
||||
if (rf->Open(filter)) return rf;
|
||||
|
||||
file = std::move(rf->Reader); // to avoid destruction of reader
|
||||
delete rf;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
157
src/common/filesystem/source/file_ssi.cpp
Normal file
157
src/common/filesystem/source/file_ssi.cpp
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
/*
|
||||
** file_grp.cpp
|
||||
**
|
||||
**---------------------------------------------------------------------------
|
||||
** Copyright 1998-2009 Randy Heit
|
||||
** Copyright 2005-2020 Christoph Oelckers
|
||||
** All rights reserved.
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions
|
||||
** are met:
|
||||
**
|
||||
** 1. Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** 2. Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in the
|
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3. The name of the author may not be used to endorse or promote products
|
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**---------------------------------------------------------------------------
|
||||
**
|
||||
**
|
||||
*/
|
||||
|
||||
#include "resourcefile.h"
|
||||
|
||||
namespace FileSys {
|
||||
//==========================================================================
|
||||
//
|
||||
// Build GRP file
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
class FSSIFile : public FUncompressedFile
|
||||
{
|
||||
public:
|
||||
FSSIFile(const char * filename, FileReader &file, StringPool* sp);
|
||||
bool Open(int version, int lumpcount, LumpFilterInfo* filter);
|
||||
};
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Initializes a Build GRP file
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FSSIFile::FSSIFile(const char *filename, FileReader &file, StringPool* sp)
|
||||
: FUncompressedFile(filename, file, sp)
|
||||
{
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Open it
|
||||
// Note that SSIs can contain embedded GRPs which must be flagged accordingly.
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
bool FSSIFile::Open(int version, int lumpcount, LumpFilterInfo*)
|
||||
{
|
||||
NumLumps = lumpcount*2;
|
||||
Lumps.Resize(lumpcount*2);
|
||||
|
||||
|
||||
int32_t j = (version == 2 ? 267 : 254) + (lumpcount * 121);
|
||||
for (uint32_t i = 0; i < NumLumps; i+=2)
|
||||
{
|
||||
char fn[13];
|
||||
int strlength = Reader.ReadUInt8();
|
||||
if (strlength > 12) strlength = 12;
|
||||
|
||||
Reader.Read(fn, 12);
|
||||
fn[strlength] = 0;
|
||||
int flength = Reader.ReadInt32();
|
||||
|
||||
|
||||
Lumps[i].LumpNameSetup(fn, stringpool);
|
||||
Lumps[i].Position = j;
|
||||
Lumps[i].LumpSize = flength;
|
||||
Lumps[i].Owner = this;
|
||||
if (strstr(fn, ".GRP")) Lumps[i].Flags |= LUMPF_EMBEDDED;
|
||||
|
||||
// SSI files can swap the order of the extension's characters - but there's no reliable detection for this and it can be mixed inside the same container,
|
||||
// so we have no choice but to create another file record for the altered name.
|
||||
std::swap(fn[strlength - 1], fn[strlength - 3]);
|
||||
Lumps[i+1].LumpNameSetup(fn, stringpool);
|
||||
Lumps[i+1].Position = j;
|
||||
Lumps[i+1].LumpSize = flength;
|
||||
Lumps[i+1].Owner = this;
|
||||
if (strstr(fn, ".GRP")) Lumps[i+1].Flags |= LUMPF_EMBEDDED;
|
||||
|
||||
j += flength;
|
||||
|
||||
Reader.Seek(104, FileReader::SeekCur);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// File open
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FResourceFile* CheckSSI(const char* filename, FileReader& file, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp)
|
||||
{
|
||||
char zerobuf[72];
|
||||
char buf[72];
|
||||
memset(zerobuf, 0, 72);
|
||||
|
||||
auto skipstring = [&](size_t length)
|
||||
{
|
||||
size_t strlength = file.ReadUInt8();
|
||||
if (strlength > length) return false;
|
||||
size_t count = file.Read(buf, length);
|
||||
buf[length] = 0;
|
||||
if (count != length || strlen(buf) != strlength) return false;
|
||||
if (length != strlength && memcmp(buf + strlength, zerobuf, length - strlength)) return false;
|
||||
return true;
|
||||
};
|
||||
if (file.GetLength() >= 12)
|
||||
{
|
||||
// check if SSI
|
||||
// this performs several checks because there is no "SSI" magic
|
||||
int version = file.ReadInt32();
|
||||
if (version == 1 || version == 2) // if
|
||||
{
|
||||
int numfiles = file.ReadInt32();
|
||||
if (!skipstring(32)) return nullptr;
|
||||
if (version == 2 && !skipstring(12)) return nullptr;
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
if (!skipstring(70)) return nullptr;
|
||||
}
|
||||
auto ssi = new FSSIFile(filename, file, sp);
|
||||
if (ssi->Open(version, numfiles, filter)) return ssi;
|
||||
file = std::move(ssi->Reader); // to avoid destruction of reader
|
||||
delete ssi;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
488
src/common/filesystem/source/file_wad.cpp
Normal file
488
src/common/filesystem/source/file_wad.cpp
Normal file
|
|
@ -0,0 +1,488 @@
|
|||
/*
|
||||
** file_wad.cpp
|
||||
**
|
||||
**---------------------------------------------------------------------------
|
||||
** Copyright 1998-2009 Randy Heit
|
||||
** Copyright 2005-2009 Christoph Oelckers
|
||||
** All rights reserved.
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions
|
||||
** are met:
|
||||
**
|
||||
** 1. Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** 2. Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in the
|
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3. The name of the author may not be used to endorse or promote products
|
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**---------------------------------------------------------------------------
|
||||
**
|
||||
**
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include "resourcefile.h"
|
||||
#include "fs_filesystem.h"
|
||||
#include "fs_swap.h"
|
||||
|
||||
namespace FileSys {
|
||||
using namespace byteswap;
|
||||
|
||||
struct wadinfo_t
|
||||
{
|
||||
// Should be "IWAD" or "PWAD".
|
||||
uint32_t Magic;
|
||||
uint32_t NumLumps;
|
||||
uint32_t InfoTableOfs;
|
||||
};
|
||||
|
||||
struct wadlump_t
|
||||
{
|
||||
uint32_t FilePos;
|
||||
uint32_t Size;
|
||||
char Name[8];
|
||||
};
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Wad Lump (with console doom LZSS support)
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
class FWadFileLump : public FResourceLump
|
||||
{
|
||||
public:
|
||||
bool Compressed;
|
||||
int Position;
|
||||
int Namespace;
|
||||
|
||||
int GetNamespace() const override { return Namespace; }
|
||||
|
||||
int GetFileOffset() override { return Position; }
|
||||
FileReader *GetReader() override
|
||||
{
|
||||
if(!Compressed)
|
||||
{
|
||||
Owner->Reader.Seek(Position, FileReader::SeekSet);
|
||||
return &Owner->Reader;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
int FillCache() override
|
||||
{
|
||||
if(!Compressed)
|
||||
{
|
||||
const char * buffer = Owner->Reader.GetBuffer();
|
||||
|
||||
if (buffer != 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->Reader.Seek(Position, FileReader::SeekSet);
|
||||
Cache = new char[LumpSize];
|
||||
|
||||
if(Compressed)
|
||||
{
|
||||
FileReader lzss;
|
||||
if (lzss.OpenDecompressor(Owner->Reader, LumpSize, METHOD_LZSS, false, true))
|
||||
{
|
||||
lzss.Read(Cache, LumpSize);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
auto read = Owner->Reader.Read(Cache, LumpSize);
|
||||
if (read != LumpSize)
|
||||
{
|
||||
throw FileSystemException("only read %d of %d bytes", (int)read, (int)LumpSize);
|
||||
}
|
||||
}
|
||||
|
||||
RefCount = 1;
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Wad file
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
class FWadFile : public FResourceFile
|
||||
{
|
||||
TArray<FWadFileLump> Lumps;
|
||||
|
||||
bool IsMarker(int lump, const char *marker);
|
||||
void SetNamespace(const char *startmarker, const char *endmarker, namespace_t space, FileSystemMessageFunc Printf, bool flathack=false);
|
||||
void SkinHack (FileSystemMessageFunc Printf);
|
||||
|
||||
public:
|
||||
FWadFile(const char * filename, FileReader &file, StringPool* sp);
|
||||
FResourceLump *GetLump(int lump) { return &Lumps[lump]; }
|
||||
bool Open(LumpFilterInfo* filter, FileSystemMessageFunc Printf);
|
||||
};
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// FWadFile::FWadFile
|
||||
//
|
||||
// Initializes a WAD file
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FWadFile::FWadFile(const char *filename, FileReader &file, StringPool* sp)
|
||||
: FResourceFile(filename, file, sp)
|
||||
{
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Open it
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
bool FWadFile::Open(LumpFilterInfo*, FileSystemMessageFunc Printf)
|
||||
{
|
||||
wadinfo_t header;
|
||||
uint32_t InfoTableOfs;
|
||||
bool isBigEndian = false; // Little endian is assumed until proven otherwise
|
||||
auto wadSize = Reader.GetLength();
|
||||
|
||||
Reader.Read(&header, sizeof(header));
|
||||
NumLumps = LittleLong(header.NumLumps);
|
||||
InfoTableOfs = LittleLong(header.InfoTableOfs);
|
||||
|
||||
// Check to see if the little endian interpretation is valid
|
||||
// This should be sufficient to detect big endian wads.
|
||||
if (InfoTableOfs + NumLumps*sizeof(wadlump_t) > (unsigned)wadSize)
|
||||
{
|
||||
NumLumps = BigLong(header.NumLumps);
|
||||
InfoTableOfs = BigLong(header.InfoTableOfs);
|
||||
isBigEndian = true;
|
||||
|
||||
// Check again to detect broken wads
|
||||
if (InfoTableOfs + NumLumps*sizeof(wadlump_t) > (unsigned)wadSize)
|
||||
{
|
||||
Printf(FSMessageLevel::Error, "%s: Bad directory offset.\n", FileName);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
TArray<wadlump_t> fileinfo(NumLumps, true);
|
||||
Reader.Seek (InfoTableOfs, FileReader::SeekSet);
|
||||
Reader.Read (fileinfo.Data(), NumLumps * sizeof(wadlump_t));
|
||||
|
||||
Lumps.Resize(NumLumps);
|
||||
|
||||
for(uint32_t i = 0; i < NumLumps; i++)
|
||||
{
|
||||
char n[9];
|
||||
for(int j = 0; j < 8; j++) n[j] = toupper(fileinfo[i].Name[j]);
|
||||
n[8] = 0;
|
||||
// This needs to be done differently. We cannot simply assume that all lumps where the first character's high bit is set are compressed without verification.
|
||||
// This requires explicit toggling for precisely the files that need it.
|
||||
#if 0
|
||||
Lumps[i].Compressed = !(gameinfo.flags & GI_SHAREWARE) && (n[0] & 0x80) == 0x80;
|
||||
n[0] &= ~0x80;
|
||||
#else
|
||||
Lumps[i].Compressed = false;
|
||||
#endif
|
||||
Lumps[i].LumpNameSetup(n, stringpool);
|
||||
|
||||
Lumps[i].Owner = this;
|
||||
Lumps[i].Position = isBigEndian ? BigLong(fileinfo[i].FilePos) : LittleLong(fileinfo[i].FilePos);
|
||||
Lumps[i].LumpSize = isBigEndian ? BigLong(fileinfo[i].Size) : LittleLong(fileinfo[i].Size);
|
||||
Lumps[i].Namespace = ns_global;
|
||||
Lumps[i].Flags = Lumps[i].Compressed ? LUMPF_COMPRESSED | LUMPF_SHORTNAME : LUMPF_SHORTNAME;
|
||||
|
||||
// Check if the lump is within the WAD file and print a warning if not.
|
||||
if (Lumps[i].Position + Lumps[i].LumpSize > wadSize || Lumps[i].Position < 0 || Lumps[i].LumpSize < 0)
|
||||
{
|
||||
if (Lumps[i].LumpSize != 0)
|
||||
{
|
||||
Printf(FSMessageLevel::Warning, "%s: Lump %s contains invalid positioning info and will be ignored\n", FileName, Lumps[i].getName());
|
||||
Lumps[i].clearName();
|
||||
}
|
||||
Lumps[i].LumpSize = Lumps[i].Position = 0;
|
||||
}
|
||||
}
|
||||
|
||||
GenerateHash(); // Do this before the lump processing below.
|
||||
|
||||
SetNamespace("S_START", "S_END", ns_sprites, Printf);
|
||||
SetNamespace("F_START", "F_END", ns_flats, Printf, true);
|
||||
SetNamespace("C_START", "C_END", ns_colormaps, Printf);
|
||||
SetNamespace("A_START", "A_END", ns_acslibrary, Printf);
|
||||
SetNamespace("TX_START", "TX_END", ns_newtextures, Printf);
|
||||
SetNamespace("V_START", "V_END", ns_strifevoices, Printf);
|
||||
SetNamespace("HI_START", "HI_END", ns_hires, Printf);
|
||||
SetNamespace("VX_START", "VX_END", ns_voxels, Printf);
|
||||
SkinHack(Printf);
|
||||
return true;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// IsMarker
|
||||
//
|
||||
// (from BOOM)
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
inline bool FWadFile::IsMarker(int lump, const char *marker)
|
||||
{
|
||||
if (Lumps[lump].getName()[0] == marker[0])
|
||||
{
|
||||
return (!strcmp(Lumps[lump].getName(), marker) ||
|
||||
(marker[1] == '_' && !strcmp(Lumps[lump].getName() +1, marker)));
|
||||
}
|
||||
else return false;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// SetNameSpace
|
||||
//
|
||||
// Sets namespace information for the lumps. It always looks for the first
|
||||
// x_START and the last x_END lump, except when loading flats. In this case
|
||||
// F_START may be absent and if that is the case all lumps with a size of
|
||||
// 4096 will be flagged appropriately.
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
// This class was supposed to be local in the function but GCC
|
||||
// does not like that.
|
||||
struct Marker
|
||||
{
|
||||
int markertype;
|
||||
unsigned int index;
|
||||
};
|
||||
|
||||
void FWadFile::SetNamespace(const char *startmarker, const char *endmarker, namespace_t space, FileSystemMessageFunc Printf, bool flathack)
|
||||
{
|
||||
bool warned = false;
|
||||
int numstartmarkers = 0, numendmarkers = 0;
|
||||
unsigned int i;
|
||||
TArray<Marker> markers;
|
||||
|
||||
for(i = 0; i < NumLumps; i++)
|
||||
{
|
||||
if (IsMarker(i, startmarker))
|
||||
{
|
||||
Marker m = { 0, i };
|
||||
markers.Push(m);
|
||||
numstartmarkers++;
|
||||
}
|
||||
else if (IsMarker(i, endmarker))
|
||||
{
|
||||
Marker m = { 1, i };
|
||||
markers.Push(m);
|
||||
numendmarkers++;
|
||||
}
|
||||
}
|
||||
|
||||
if (numstartmarkers == 0)
|
||||
{
|
||||
if (numendmarkers == 0) return; // no markers found
|
||||
|
||||
Printf(FSMessageLevel::Warning, "%s: %s marker without corresponding %s found.\n", FileName, endmarker, startmarker);
|
||||
|
||||
|
||||
if (flathack)
|
||||
{
|
||||
// We have found no F_START but one or more F_END markers.
|
||||
// mark all lumps before the last F_END marker as potential flats.
|
||||
unsigned int end = markers[markers.Size()-1].index;
|
||||
for(unsigned int ii = 0; ii < end; ii++)
|
||||
{
|
||||
if (Lumps[ii].LumpSize == 4096)
|
||||
{
|
||||
// We can't add this to the flats namespace but
|
||||
// it needs to be flagged for the texture manager.
|
||||
Printf(FSMessageLevel::DebugNotify, "%s: Marking %s as potential flat\n", FileName, Lumps[ii].getName());
|
||||
Lumps[ii].Flags |= LUMPF_MAYBEFLAT;
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
i = 0;
|
||||
while (i < markers.Size())
|
||||
{
|
||||
int start, end;
|
||||
if (markers[i].markertype != 0)
|
||||
{
|
||||
Printf(FSMessageLevel::Warning, "%s: %s marker without corresponding %s found.\n", FileName, endmarker, startmarker);
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
start = i++;
|
||||
|
||||
// skip over subsequent x_START markers
|
||||
while (i < markers.Size() && markers[i].markertype == 0)
|
||||
{
|
||||
Printf(FSMessageLevel::Warning, "%s: duplicate %s marker found.\n", FileName, startmarker);
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
// same for x_END markers
|
||||
while (i < markers.Size()-1 && (markers[i].markertype == 1 && markers[i+1].markertype == 1))
|
||||
{
|
||||
Printf(FSMessageLevel::Warning, "%s: duplicate %s marker found.\n", FileName, endmarker);
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
// We found a starting marker but no end marker. Ignore this block.
|
||||
if (i >= markers.Size())
|
||||
{
|
||||
Printf(FSMessageLevel::Warning, "%s: %s marker without corresponding %s found.\n", FileName, startmarker, endmarker);
|
||||
end = NumLumps;
|
||||
}
|
||||
else
|
||||
{
|
||||
end = markers[i++].index;
|
||||
}
|
||||
|
||||
// we found a marked block
|
||||
Printf(FSMessageLevel::DebugNotify, "%s: Found %s block at (%d-%d)\n", FileName, startmarker, markers[start].index, end);
|
||||
for(int j = markers[start].index + 1; j < end; j++)
|
||||
{
|
||||
if (Lumps[j].Namespace != ns_global)
|
||||
{
|
||||
if (!warned)
|
||||
{
|
||||
Printf(FSMessageLevel::Warning, "%s: Overlapping namespaces found (lump %d)\n", FileName, j);
|
||||
}
|
||||
warned = true;
|
||||
}
|
||||
else if (space == ns_sprites && Lumps[j].LumpSize < 8)
|
||||
{
|
||||
// sf 26/10/99:
|
||||
// ignore sprite lumps smaller than 8 bytes (the smallest possible)
|
||||
// in size -- this was used by some dmadds wads
|
||||
// as an 'empty' graphics resource
|
||||
Printf(FSMessageLevel::DebugWarn, "%s: Skipped empty sprite %s (lump %d)\n", FileName, Lumps[j].getName(), j);
|
||||
}
|
||||
else
|
||||
{
|
||||
Lumps[j].Namespace = space;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// W_SkinHack
|
||||
//
|
||||
// Tests a wad file to see if it contains an S_SKIN marker. If it does,
|
||||
// every lump in the wad is moved into a new namespace. Because skins are
|
||||
// only supposed to replace player sprites, sounds, or faces, this should
|
||||
// not be a problem. Yes, there are skins that replace more than that, but
|
||||
// they are such a pain, and breaking them like this was done on purpose.
|
||||
// This also renames any S_SKINxx lumps to just S_SKIN.
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
void FWadFile::SkinHack (FileSystemMessageFunc Printf)
|
||||
{
|
||||
// this being static is not a problem. The only relevant thing is that each skin gets a different number.
|
||||
static int namespc = ns_firstskin;
|
||||
bool skinned = false;
|
||||
bool hasmap = false;
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < NumLumps; i++)
|
||||
{
|
||||
FResourceLump *lump = &Lumps[i];
|
||||
|
||||
if (!strnicmp(lump->getName(), "S_SKIN", 6))
|
||||
{ // Wad has at least one skin.
|
||||
lump->LumpNameSetup("S_SKIN", nullptr);
|
||||
if (!skinned)
|
||||
{
|
||||
skinned = true;
|
||||
uint32_t j;
|
||||
|
||||
for (j = 0; j < NumLumps; j++)
|
||||
{
|
||||
Lumps[j].Namespace = namespc;
|
||||
}
|
||||
namespc++;
|
||||
}
|
||||
}
|
||||
// needless to say, this check is entirely useless these days as map names can be more diverse..
|
||||
if ((lump->getName()[0] == 'M' &&
|
||||
lump->getName()[1] == 'A' &&
|
||||
lump->getName()[2] == 'P' &&
|
||||
lump->getName()[3] >= '0' && lump->getName()[3] <= '9' &&
|
||||
lump->getName()[4] >= '0' && lump->getName()[4] <= '9' &&
|
||||
lump->getName()[5] == '\0')
|
||||
||
|
||||
(lump->getName()[0] == 'E' &&
|
||||
lump->getName()[1] >= '0' && lump->getName()[1] <= '9' &&
|
||||
lump->getName()[2] == 'M' &&
|
||||
lump->getName()[3] >= '0' && lump->getName()[3] <= '9' &&
|
||||
lump->getName()[4] == '\0'))
|
||||
{
|
||||
hasmap = true;
|
||||
}
|
||||
}
|
||||
if (skinned && hasmap)
|
||||
{
|
||||
Printf(FSMessageLevel::Attention, "%s: The maps will not be loaded because it has a skin.\n", FileName);
|
||||
Printf(FSMessageLevel::Attention, "You should remove the skin from the wad to play these maps.\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// File open
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FResourceFile *CheckWad(const char *filename, FileReader &file, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp)
|
||||
{
|
||||
char head[4];
|
||||
|
||||
if (file.GetLength() >= 12)
|
||||
{
|
||||
file.Seek(0, FileReader::SeekSet);
|
||||
file.Read(&head, 4);
|
||||
file.Seek(0, FileReader::SeekSet);
|
||||
if (!memcmp(head, "IWAD", 4) || !memcmp(head, "PWAD", 4))
|
||||
{
|
||||
auto rf = new FWadFile(filename, file, sp);
|
||||
if (rf->Open(filter, Printf)) return rf;
|
||||
|
||||
file = std::move(rf->Reader); // to avoid destruction of reader
|
||||
delete rf;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
}
|
||||
145
src/common/filesystem/source/file_whres.cpp
Normal file
145
src/common/filesystem/source/file_whres.cpp
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
/*
|
||||
** file_whres.cpp
|
||||
**
|
||||
** reads a Witchaven/TekWar sound resource file
|
||||
**
|
||||
**---------------------------------------------------------------------------
|
||||
** Copyright 2009-2019 Christoph Oelckers
|
||||
** All rights reserved.
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions
|
||||
** are met:
|
||||
**
|
||||
** 1. Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** 2. Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in the
|
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3. The name of the author may not be used to endorse or promote products
|
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**---------------------------------------------------------------------------
|
||||
**
|
||||
**
|
||||
*/
|
||||
|
||||
#include "resourcefile.h"
|
||||
#include "fs_stringpool.h"
|
||||
#include "fs_swap.h"
|
||||
|
||||
namespace FileSys {
|
||||
using namespace byteswap;
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// WH resource file
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
class FWHResFile : public FUncompressedFile
|
||||
{
|
||||
const char* BaseName;
|
||||
public:
|
||||
FWHResFile(const char * filename, FileReader &file, StringPool* sp);
|
||||
bool Open(LumpFilterInfo* filter);
|
||||
};
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
//
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FWHResFile::FWHResFile(const char *filename, FileReader &file, StringPool* sp)
|
||||
: FUncompressedFile(filename, file, sp)
|
||||
{
|
||||
BaseName = stringpool->Strdup(ExtractBaseName(filename, false).c_str());
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Open it
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
bool FWHResFile::Open(LumpFilterInfo*)
|
||||
{
|
||||
uint32_t directory[1024];
|
||||
|
||||
Reader.Seek(-4096, FileReader::SeekEnd);
|
||||
Reader.Read(directory, 4096);
|
||||
|
||||
int nl =1024/3;
|
||||
Lumps.Resize(nl);
|
||||
|
||||
|
||||
int i = 0;
|
||||
for(int k = 0; k < nl; k++)
|
||||
{
|
||||
uint32_t offset = LittleLong(directory[k*3]) * 4096;
|
||||
uint32_t length = LittleLong(directory[k*3+1]);
|
||||
if (length == 0) break;
|
||||
char num[5];
|
||||
snprintf(num, 5, "/%04d", k);
|
||||
std::string synthname = BaseName;
|
||||
synthname += num;
|
||||
Lumps[i].LumpNameSetup(synthname.c_str(), stringpool);
|
||||
Lumps[i].Owner = this;
|
||||
Lumps[i].Position = offset;
|
||||
Lumps[i].LumpSize = length;
|
||||
i++;
|
||||
}
|
||||
NumLumps = i;
|
||||
Lumps.Clamp(NumLumps);
|
||||
Lumps.ShrinkToFit();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// File open
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FResourceFile *CheckWHRes(const char *filename, FileReader &file, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp)
|
||||
{
|
||||
if (file.GetLength() >= 8192) // needs to be at least 8192 to contain one file and the directory.
|
||||
{
|
||||
unsigned directory[1024];
|
||||
int nl =1024/3;
|
||||
|
||||
file.Seek(-4096, FileReader::SeekEnd);
|
||||
file.Read(directory, 4096);
|
||||
auto size = file.GetLength();
|
||||
|
||||
uint32_t checkpos = 0;
|
||||
for(int k = 0; k < nl; k++)
|
||||
{
|
||||
unsigned offset = LittleLong(directory[k*3]);
|
||||
unsigned length = LittleLong(directory[k*3+1]);
|
||||
if (length <= 0 && offset == 0) break;
|
||||
if (offset != checkpos || length == 0 || offset + length >= (size_t)size - 4096 ) return nullptr;
|
||||
checkpos += (length+4095) / 4096;
|
||||
}
|
||||
auto rf = new FWHResFile(filename, file, sp);
|
||||
if (rf->Open(filter)) return rf;
|
||||
file = std::move(rf->Reader); // to avoid destruction of reader
|
||||
delete rf;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
}
|
||||
725
src/common/filesystem/source/file_zip.cpp
Normal file
725
src/common/filesystem/source/file_zip.cpp
Normal file
|
|
@ -0,0 +1,725 @@
|
|||
/*
|
||||
** file_zip.cpp
|
||||
**
|
||||
**---------------------------------------------------------------------------
|
||||
** Copyright 1998-2009 Randy Heit
|
||||
** Copyright 2005-2009 Christoph Oelckers
|
||||
** All rights reserved.
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions
|
||||
** are met:
|
||||
**
|
||||
** 1. Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** 2. Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in the
|
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3. The name of the author may not be used to endorse or promote products
|
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**---------------------------------------------------------------------------
|
||||
**
|
||||
**
|
||||
*/
|
||||
|
||||
#include <time.h>
|
||||
#include <stdexcept>
|
||||
#include "file_zip.h"
|
||||
#include "w_zip.h"
|
||||
#include "ancientzip.h"
|
||||
#include "fs_findfile.h"
|
||||
#include "fs_swap.h"
|
||||
|
||||
namespace FileSys {
|
||||
using namespace byteswap;
|
||||
|
||||
#define BUFREADCOMMENT (0x400)
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Decompression subroutine
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
static bool UncompressZipLump(char *Cache, FileReader &Reader, int Method, int LumpSize, int CompressedSize, int GPFlags, bool exceptions)
|
||||
{
|
||||
switch (Method)
|
||||
{
|
||||
case METHOD_STORED:
|
||||
{
|
||||
Reader.Read(Cache, LumpSize);
|
||||
break;
|
||||
}
|
||||
|
||||
case METHOD_DEFLATE:
|
||||
case METHOD_BZIP2:
|
||||
case METHOD_LZMA:
|
||||
{
|
||||
FileReader frz;
|
||||
if (frz.OpenDecompressor(Reader, LumpSize, Method, false, exceptions))
|
||||
{
|
||||
frz.Read(Cache, LumpSize);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// Fixme: These should also use a stream
|
||||
case METHOD_IMPLODE:
|
||||
{
|
||||
FZipExploder exploder;
|
||||
if (exploder.Explode((unsigned char*)Cache, LumpSize, Reader, CompressedSize, GPFlags) == -1)
|
||||
{
|
||||
// decompression failed so zero the cache.
|
||||
memset(Cache, 0, LumpSize);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case METHOD_SHRINK:
|
||||
{
|
||||
ShrinkLoop((unsigned char *)Cache, LumpSize, Reader, CompressedSize);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
assert(0);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FCompressedBuffer::Decompress(char *destbuffer)
|
||||
{
|
||||
FileReader mr;
|
||||
mr.OpenMemory(mBuffer, mCompressedSize);
|
||||
return UncompressZipLump(destbuffer, mr, mMethod, mSize, mCompressedSize, mZipFlags, false);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
//
|
||||
// Finds the central directory end record in the end of the file.
|
||||
// Taken from Quake3 source but the file in question is not GPL'ed. ;)
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
static uint32_t Zip_FindCentralDir(FileReader &fin, bool* zip64)
|
||||
{
|
||||
unsigned char buf[BUFREADCOMMENT + 4];
|
||||
uint32_t FileSize;
|
||||
uint32_t uBackRead;
|
||||
uint32_t uMaxBack; // maximum size of global comment
|
||||
uint32_t uPosFound=0;
|
||||
|
||||
FileSize = (uint32_t)fin.GetLength();
|
||||
uMaxBack = std::min<uint32_t>(0xffff, FileSize);
|
||||
|
||||
uBackRead = 4;
|
||||
while (uBackRead < uMaxBack)
|
||||
{
|
||||
uint32_t uReadSize, uReadPos;
|
||||
int i;
|
||||
if (uBackRead + BUFREADCOMMENT > uMaxBack)
|
||||
uBackRead = uMaxBack;
|
||||
else
|
||||
uBackRead += BUFREADCOMMENT;
|
||||
uReadPos = FileSize - uBackRead;
|
||||
|
||||
uReadSize = std::min<uint32_t>((BUFREADCOMMENT + 4), (FileSize - uReadPos));
|
||||
|
||||
if (fin.Seek(uReadPos, FileReader::SeekSet) != 0) break;
|
||||
|
||||
if (fin.Read(buf, (int32_t)uReadSize) != (int32_t)uReadSize) break;
|
||||
|
||||
for (i = (int)uReadSize - 3; (i--) > 0;)
|
||||
{
|
||||
if (buf[i] == 'P' && buf[i+1] == 'K' && buf[i+2] == 5 && buf[i+3] == 6 && !*zip64 && uPosFound == 0)
|
||||
{
|
||||
*zip64 = false;
|
||||
uPosFound = uReadPos + i;
|
||||
}
|
||||
if (buf[i] == 'P' && buf[i+1] == 'K' && buf[i+2] == 6 && buf[i+3] == 6)
|
||||
{
|
||||
*zip64 = true;
|
||||
uPosFound = uReadPos + i;
|
||||
return uPosFound;
|
||||
}
|
||||
}
|
||||
}
|
||||
return uPosFound;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Zip file
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FZipFile::FZipFile(const char * filename, FileReader &file, StringPool* sp)
|
||||
: FResourceFile(filename, file, sp)
|
||||
{
|
||||
Lumps = NULL;
|
||||
}
|
||||
|
||||
bool FZipFile::Open(LumpFilterInfo* filter, FileSystemMessageFunc Printf)
|
||||
{
|
||||
bool zip64 = false;
|
||||
uint32_t centraldir = Zip_FindCentralDir(Reader, &zip64);
|
||||
int skipped = 0;
|
||||
|
||||
Lumps = NULL;
|
||||
|
||||
if (centraldir == 0)
|
||||
{
|
||||
Printf(FSMessageLevel::Error, "%s: ZIP file corrupt!\n", FileName);
|
||||
return false;
|
||||
}
|
||||
|
||||
uint64_t dirsize, DirectoryOffset;
|
||||
if (!zip64)
|
||||
{
|
||||
FZipEndOfCentralDirectory info;
|
||||
// Read the central directory info.
|
||||
Reader.Seek(centraldir, FileReader::SeekSet);
|
||||
Reader.Read(&info, sizeof(FZipEndOfCentralDirectory));
|
||||
|
||||
// No multi-disk zips!
|
||||
if (info.NumEntries != info.NumEntriesOnAllDisks ||
|
||||
info.FirstDisk != 0 || info.DiskNumber != 0)
|
||||
{
|
||||
Printf(FSMessageLevel::Error, "%s: Multipart Zip files are not supported.\n", FileName);
|
||||
return false;
|
||||
}
|
||||
|
||||
NumLumps = LittleShort(info.NumEntries);
|
||||
dirsize = LittleLong(info.DirectorySize);
|
||||
DirectoryOffset = LittleLong(info.DirectoryOffset);
|
||||
}
|
||||
else
|
||||
{
|
||||
FZipEndOfCentralDirectory64 info;
|
||||
// Read the central directory info.
|
||||
Reader.Seek(centraldir, FileReader::SeekSet);
|
||||
Reader.Read(&info, sizeof(FZipEndOfCentralDirectory64));
|
||||
|
||||
// No multi-disk zips!
|
||||
if (info.NumEntries != info.NumEntriesOnAllDisks ||
|
||||
info.FirstDisk != 0 || info.DiskNumber != 0)
|
||||
{
|
||||
Printf(FSMessageLevel::Error, "%s: Multipart Zip files are not supported.\n", FileName);
|
||||
return false;
|
||||
}
|
||||
|
||||
NumLumps = (uint32_t)info.NumEntries;
|
||||
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;
|
||||
bool foundprefix = false;
|
||||
|
||||
// Check if all files have the same prefix so that this can be stripped out.
|
||||
// This will only be done if there is either a MAPINFO, ZMAPINFO or GAMEINFO lump in the subdirectory, denoting a ZDoom mod.
|
||||
if (NumLumps > 1) for (uint32_t i = 0; i < NumLumps; i++)
|
||||
{
|
||||
FZipCentralDirectoryInfo *zip_fh = (FZipCentralDirectoryInfo *)dirptr;
|
||||
|
||||
int len = LittleShort(zip_fh->NameLength);
|
||||
std::string name(dirptr + sizeof(FZipCentralDirectoryInfo), len);
|
||||
|
||||
dirptr += sizeof(FZipCentralDirectoryInfo) +
|
||||
LittleShort(zip_fh->NameLength) +
|
||||
LittleShort(zip_fh->ExtraLength) +
|
||||
LittleShort(zip_fh->CommentLength);
|
||||
|
||||
if (dirptr > ((char*)directory) + dirsize) // This directory entry goes beyond the end of the file.
|
||||
{
|
||||
free(directory);
|
||||
Printf(FSMessageLevel::Error, "%s: Central directory corrupted.", FileName);
|
||||
return false;
|
||||
}
|
||||
|
||||
for (auto& c : name) c = tolower(c);
|
||||
|
||||
auto vv = name.find("__macosx");
|
||||
if (name.find("filter/") == 0)
|
||||
continue; // 'filter' is a reserved name of the file system.
|
||||
if (name.find("__macosx") == 0)
|
||||
continue; // skip Apple garbage. At this stage only the root folder matters.
|
||||
if (name.find(".bat") != std::string::npos || name.find(".exe") != std::string::npos)
|
||||
continue; // also ignore executables for this.
|
||||
if (!foundprefix)
|
||||
{
|
||||
// check for special names, if one of these gets found this must be treated as a normal zip.
|
||||
bool isspecial = name.find("/") == std::string::npos ||
|
||||
(filter && std::find(filter->reservedFolders.begin(), filter->reservedFolders.end(), name) != filter->reservedFolders.end());
|
||||
if (isspecial) break;
|
||||
name0 = std::string(name, 0, name.rfind("/")+1);
|
||||
name1 = std::string(name, 0, name.find("/") + 1);
|
||||
foundprefix = true;
|
||||
}
|
||||
|
||||
if (name.find(name0) != 0)
|
||||
{
|
||||
if (!name1.empty())
|
||||
{
|
||||
name0 = name1;
|
||||
if (name.find(name0) != 0)
|
||||
{
|
||||
name0 = "";
|
||||
}
|
||||
}
|
||||
if (name0.empty())
|
||||
break;
|
||||
}
|
||||
if (!foundspeciallump && filter)
|
||||
{
|
||||
// at least one of the more common definition lumps must be present.
|
||||
for (auto &p : filter->requiredPrefixes)
|
||||
{
|
||||
if (name.find(name0 + p) == 0 || name.rfind(p) == ptrdiff_t(name.length() - p.length()))
|
||||
{
|
||||
foundspeciallump = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// If it ran through the list without finding anything it should not attempt any path remapping.
|
||||
if (!foundspeciallump) name0 = "";
|
||||
|
||||
dirptr = (char*)directory;
|
||||
lump_p = Lumps;
|
||||
for (uint32_t i = 0; i < NumLumps; i++)
|
||||
{
|
||||
FZipCentralDirectoryInfo *zip_fh = (FZipCentralDirectoryInfo *)dirptr;
|
||||
|
||||
int len = LittleShort(zip_fh->NameLength);
|
||||
std::string name(dirptr + sizeof(FZipCentralDirectoryInfo), len);
|
||||
dirptr += sizeof(FZipCentralDirectoryInfo) +
|
||||
LittleShort(zip_fh->NameLength) +
|
||||
LittleShort(zip_fh->ExtraLength) +
|
||||
LittleShort(zip_fh->CommentLength);
|
||||
|
||||
if (dirptr > ((char*)directory) + dirsize) // This directory entry goes beyond the end of the file.
|
||||
{
|
||||
free(directory);
|
||||
Printf(FSMessageLevel::Error, "%s: Central directory corrupted.", FileName);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (name.find("__macosx") == 0 || name.find("__MACOSX") == 0)
|
||||
{
|
||||
skipped++;
|
||||
continue; // Weed out Apple's resource fork garbage right here because it interferes with safe operation.
|
||||
}
|
||||
if (!name0.empty()) name = std::string(name, name0.length());
|
||||
|
||||
// skip Directories
|
||||
if (name.empty() || (name.back() == '/' && LittleLong(zip_fh->UncompressedSize32) == 0))
|
||||
{
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Ignore unknown compression formats
|
||||
zip_fh->Method = LittleShort(zip_fh->Method);
|
||||
if (zip_fh->Method != METHOD_STORED &&
|
||||
zip_fh->Method != METHOD_DEFLATE &&
|
||||
zip_fh->Method != METHOD_LZMA &&
|
||||
zip_fh->Method != METHOD_BZIP2 &&
|
||||
zip_fh->Method != METHOD_IMPLODE &&
|
||||
zip_fh->Method != METHOD_SHRINK)
|
||||
{
|
||||
Printf(FSMessageLevel::Error, "%s: '%s' uses an unsupported compression algorithm (#%d).\n", FileName, name.c_str(), zip_fh->Method);
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
// Also ignore encrypted entries
|
||||
zip_fh->Flags = LittleShort(zip_fh->Flags);
|
||||
if (zip_fh->Flags & ZF_ENCRYPTED)
|
||||
{
|
||||
Printf(FSMessageLevel::Error, "%s: '%s' is encrypted. Encryption is not supported.\n", FileName, name.c_str());
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
|
||||
FixPathSeparator(&name.front());
|
||||
for (auto& c : name) c = tolower(c);
|
||||
|
||||
uint32_t UncompressedSize =LittleLong(zip_fh->UncompressedSize32);
|
||||
uint32_t CompressedSize = LittleLong(zip_fh->CompressedSize32);
|
||||
uint64_t LocalHeaderOffset = LittleLong(zip_fh->LocalHeaderOffset32);
|
||||
if (zip_fh->ExtraLength > 0)
|
||||
{
|
||||
uint8_t* rawext = (uint8_t*)zip_fh + sizeof(*zip_fh) + zip_fh->NameLength;
|
||||
uint32_t ExtraLength = LittleLong(zip_fh->ExtraLength);
|
||||
|
||||
while (ExtraLength > 0)
|
||||
{
|
||||
auto zip_64 = (FZipCentralDirectoryInfo64BitExt*)rawext;
|
||||
uint32_t BlockLength = LittleLong(zip_64->Length);
|
||||
rawext += BlockLength + 4;
|
||||
ExtraLength -= BlockLength + 4;
|
||||
if (LittleLong(zip_64->Type) == 1 && BlockLength >= 0x18)
|
||||
{
|
||||
if (zip_64->CompressedSize > 0x7fffffff || zip_64->UncompressedSize > 0x7fffffff)
|
||||
{
|
||||
// The file system is limited to 32 bit file sizes;
|
||||
Printf(FSMessageLevel::Warning, "%s: '%s' is too large.\n", FileName, name.c_str());
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
UncompressedSize = (uint32_t)zip_64->UncompressedSize;
|
||||
CompressedSize = (uint32_t)zip_64->CompressedSize;
|
||||
LocalHeaderOffset = zip_64->LocalHeaderOffset;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
//
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FCompressedBuffer FZipLump::GetRawData()
|
||||
{
|
||||
FCompressedBuffer cbuf = { (unsigned)LumpSize, (unsigned)CompressedSize, Method, GPFlags, CRC32, new char[CompressedSize] };
|
||||
if (NeedFileStart) SetLumpAddress();
|
||||
Owner->Reader.Seek(Position, FileReader::SeekSet);
|
||||
Owner->Reader.Read(cbuf.mBuffer, CompressedSize);
|
||||
return cbuf;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// SetLumpAddress
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
void FZipLump::SetLumpAddress()
|
||||
{
|
||||
// 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->Reader.Seek(Position, FileReader::SeekSet);
|
||||
Owner->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->Reader.Seek(Position, FileReader::SeekSet);
|
||||
return &Owner->Reader;
|
||||
}
|
||||
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->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;
|
||||
RefCount = -1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
Owner->Reader.Seek(Position, FileReader::SeekSet);
|
||||
Cache = new char[LumpSize];
|
||||
UncompressZipLump(Cache, Owner->Reader, Method, LumpSize, CompressedSize, GPFlags, true);
|
||||
RefCount = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
//
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
int FZipLump::GetFileOffset()
|
||||
{
|
||||
if (Method != METHOD_STORED) return -1;
|
||||
if (NeedFileStart) SetLumpAddress();
|
||||
return (int)Position;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// File open
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FResourceFile *CheckZip(const char *filename, FileReader &file, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp)
|
||||
{
|
||||
char head[4];
|
||||
|
||||
if (file.GetLength() >= (long)sizeof(FZipLocalFileHeader))
|
||||
{
|
||||
file.Seek(0, FileReader::SeekSet);
|
||||
file.Read(&head, 4);
|
||||
file.Seek(0, FileReader::SeekSet);
|
||||
if (!memcmp(head, "PK\x3\x4", 4))
|
||||
{
|
||||
auto rf = new FZipFile(filename, file, sp);
|
||||
if (rf->Open(filter, Printf)) return rf;
|
||||
|
||||
file = std::move(rf->Reader); // to avoid destruction of reader
|
||||
delete rf;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// time_to_dos
|
||||
//
|
||||
// Converts time from struct tm to the DOS format used by zip files.
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
static std::pair<uint16_t, uint16_t> time_to_dos(struct tm *time)
|
||||
{
|
||||
std::pair<uint16_t, uint16_t> val;
|
||||
if (time == NULL || time->tm_year < 80)
|
||||
{
|
||||
val.first = val.second = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
val.first = (time->tm_year - 80) * 512 + (time->tm_mon + 1) * 32 + time->tm_mday;
|
||||
val.second= time->tm_hour * 2048 + time->tm_min * 32 + time->tm_sec / 2;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// append_to_zip
|
||||
//
|
||||
// Write a given file to the zipFile.
|
||||
//
|
||||
// zipfile: zip object to be written to
|
||||
//
|
||||
// returns: position = success, -1 = error
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
static int AppendToZip(FileWriter *zip_file, const FCompressedBuffer &content, std::pair<uint16_t, uint16_t> &dostime)
|
||||
{
|
||||
FZipLocalFileHeader local;
|
||||
int position;
|
||||
|
||||
local.Magic = ZIP_LOCALFILE;
|
||||
local.VersionToExtract[0] = 20;
|
||||
local.VersionToExtract[1] = 0;
|
||||
local.Flags = content.mMethod == METHOD_DEFLATE ? LittleShort((uint16_t)2) : LittleShort((uint16_t)content.mZipFlags);
|
||||
local.Method = LittleShort((uint16_t)content.mMethod);
|
||||
local.ModDate = LittleShort(dostime.first);
|
||||
local.ModTime = LittleShort(dostime.second);
|
||||
local.CRC32 = content.mCRC32;
|
||||
local.UncompressedSize = LittleLong(content.mSize);
|
||||
local.CompressedSize = LittleLong(content.mCompressedSize);
|
||||
local.NameLength = LittleShort((unsigned short)strlen(content.filename));
|
||||
local.ExtraLength = 0;
|
||||
|
||||
// Fill in local directory header.
|
||||
|
||||
position = (int)zip_file->Tell();
|
||||
|
||||
// Write out the header, file name, and file data.
|
||||
if (zip_file->Write(&local, sizeof(local)) != sizeof(local) ||
|
||||
zip_file->Write(content.filename, strlen(content.filename)) != strlen(content.filename) ||
|
||||
zip_file->Write(content.mBuffer, content.mCompressedSize) != content.mCompressedSize)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return position;
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// write_central_dir
|
||||
//
|
||||
// Writes the central directory entry for a file.
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
int AppendCentralDirectory(FileWriter *zip_file, const FCompressedBuffer &content, std::pair<uint16_t, uint16_t> &dostime, int position)
|
||||
{
|
||||
FZipCentralDirectoryInfo dir;
|
||||
|
||||
dir.Magic = ZIP_CENTRALFILE;
|
||||
dir.VersionMadeBy[0] = 20;
|
||||
dir.VersionMadeBy[1] = 0;
|
||||
dir.VersionToExtract[0] = 20;
|
||||
dir.VersionToExtract[1] = 0;
|
||||
dir.Flags = content.mMethod == METHOD_DEFLATE ? LittleShort((uint16_t)2) : LittleShort((uint16_t)content.mZipFlags);
|
||||
dir.Method = LittleShort((uint16_t)content.mMethod);
|
||||
dir.ModTime = LittleShort(dostime.first);
|
||||
dir.ModDate = LittleShort(dostime.second);
|
||||
dir.CRC32 = content.mCRC32;
|
||||
dir.CompressedSize32 = LittleLong(content.mCompressedSize);
|
||||
dir.UncompressedSize32 = LittleLong(content.mSize);
|
||||
dir.NameLength = LittleShort((unsigned short)strlen(content.filename));
|
||||
dir.ExtraLength = 0;
|
||||
dir.CommentLength = 0;
|
||||
dir.StartingDiskNumber = 0;
|
||||
dir.InternalAttributes = 0;
|
||||
dir.ExternalAttributes = 0;
|
||||
dir.LocalHeaderOffset32 = LittleLong((unsigned)position);
|
||||
|
||||
if (zip_file->Write(&dir, sizeof(dir)) != sizeof(dir) ||
|
||||
zip_file->Write(content.filename, strlen(content.filename)) != strlen(content.filename))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool WriteZip(const char* filename, const FCompressedBuffer* content, size_t contentcount)
|
||||
{
|
||||
// try to determine local time
|
||||
struct tm *ltime;
|
||||
time_t ttime;
|
||||
ttime = time(nullptr);
|
||||
ltime = localtime(&ttime);
|
||||
auto dostime = time_to_dos(ltime);
|
||||
|
||||
TArray<int> positions;
|
||||
|
||||
auto f = FileWriter::Open(filename);
|
||||
if (f != nullptr)
|
||||
{
|
||||
for (size_t i = 0; i < contentcount; i++)
|
||||
{
|
||||
int pos = AppendToZip(f, content[i], dostime);
|
||||
if (pos == -1)
|
||||
{
|
||||
delete f;
|
||||
remove(filename);
|
||||
return false;
|
||||
}
|
||||
positions.Push(pos);
|
||||
}
|
||||
|
||||
int dirofs = (int)f->Tell();
|
||||
for (size_t i = 0; i < contentcount; i++)
|
||||
{
|
||||
if (AppendCentralDirectory(f, content[i], dostime, positions[i]) < 0)
|
||||
{
|
||||
delete f;
|
||||
remove(filename);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Write the directory terminator.
|
||||
FZipEndOfCentralDirectory dirend;
|
||||
dirend.Magic = ZIP_ENDOFDIR;
|
||||
dirend.DiskNumber = 0;
|
||||
dirend.FirstDisk = 0;
|
||||
dirend.NumEntriesOnAllDisks = dirend.NumEntries = LittleShort((uint16_t)contentcount);
|
||||
dirend.DirectoryOffset = LittleLong((unsigned)dirofs);
|
||||
dirend.DirectorySize = LittleLong((uint32_t)(f->Tell() - dirofs));
|
||||
dirend.ZipCommentLength = 0;
|
||||
if (f->Write(&dirend, sizeof(dirend)) != sizeof(dirend))
|
||||
{
|
||||
delete f;
|
||||
remove(filename);
|
||||
return false;
|
||||
}
|
||||
delete f;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
51
src/common/filesystem/source/file_zip.h
Normal file
51
src/common/filesystem/source/file_zip.h
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
#ifndef __FILE_ZIP_H
|
||||
#define __FILE_ZIP_H
|
||||
|
||||
#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;
|
||||
|
||||
private:
|
||||
void SetLumpAddress();
|
||||
virtual int GetFileOffset();
|
||||
FCompressedBuffer GetRawData();
|
||||
};
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// 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; }
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
498
src/common/filesystem/source/files.cpp
Normal file
498
src/common/filesystem/source/files.cpp
Normal file
|
|
@ -0,0 +1,498 @@
|
|||
/*
|
||||
** files.cpp
|
||||
** Implements classes for reading from files or memory blocks
|
||||
**
|
||||
**---------------------------------------------------------------------------
|
||||
** Copyright 1998-2008 Randy Heit
|
||||
** Copyright 2005-2008 Christoph Oelckers
|
||||
** All rights reserved.
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions
|
||||
** are met:
|
||||
**
|
||||
** 1. Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** 2. Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in the
|
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3. The name of the author may not be used to endorse or promote products
|
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**---------------------------------------------------------------------------
|
||||
**
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include "fs_files.h"
|
||||
|
||||
namespace FileSys {
|
||||
|
||||
#ifdef _WIN32
|
||||
std::wstring toWide(const char* str);
|
||||
#endif
|
||||
|
||||
FILE *myfopen(const char *filename, const char *flags)
|
||||
{
|
||||
#ifndef _WIN32
|
||||
return fopen(filename, flags);
|
||||
#else
|
||||
auto widename = toWide(filename);
|
||||
auto wideflags = toWide(flags);
|
||||
return _wfopen(widename.c_str(), wideflags.c_str());
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// StdFileReader
|
||||
//
|
||||
// reads data from an stdio FILE* or part of it.
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
class StdFileReader : public FileReaderInterface
|
||||
{
|
||||
FILE *File = nullptr;
|
||||
long StartPos = 0;
|
||||
long FilePos = 0;
|
||||
|
||||
public:
|
||||
StdFileReader()
|
||||
{}
|
||||
|
||||
~StdFileReader()
|
||||
{
|
||||
if (File != nullptr)
|
||||
{
|
||||
fclose(File);
|
||||
}
|
||||
File = nullptr;
|
||||
}
|
||||
|
||||
bool Open(const char *filename, long startpos = 0, long len = -1)
|
||||
{
|
||||
File = myfopen(filename, "rb");
|
||||
if (File == nullptr) return false;
|
||||
FilePos = startpos;
|
||||
StartPos = startpos;
|
||||
Length = CalcFileLen();
|
||||
if (len >= 0 && len < Length) Length = len;
|
||||
if (startpos > 0) Seek(0, SEEK_SET);
|
||||
return true;
|
||||
}
|
||||
|
||||
long Tell() const override
|
||||
{
|
||||
return FilePos - StartPos;
|
||||
}
|
||||
|
||||
long Seek(long offset, int origin) override
|
||||
{
|
||||
if (origin == SEEK_SET)
|
||||
{
|
||||
offset += StartPos;
|
||||
}
|
||||
else if (origin == SEEK_CUR)
|
||||
{
|
||||
offset += FilePos;
|
||||
}
|
||||
else if (origin == SEEK_END)
|
||||
{
|
||||
offset += StartPos + Length;
|
||||
}
|
||||
if (offset < StartPos || offset > StartPos + Length) return -1; // out of scope
|
||||
|
||||
if (0 == fseek(File, offset, SEEK_SET))
|
||||
{
|
||||
FilePos = offset;
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
long Read(void *buffer, long len) override
|
||||
{
|
||||
assert(len >= 0);
|
||||
if (len <= 0) return 0;
|
||||
if (FilePos + len > StartPos + Length)
|
||||
{
|
||||
len = Length - FilePos + StartPos;
|
||||
}
|
||||
len = (long)fread(buffer, 1, len, File);
|
||||
FilePos += len;
|
||||
return len;
|
||||
}
|
||||
|
||||
char *Gets(char *strbuf, int len) override
|
||||
{
|
||||
if (len <= 0 || FilePos >= StartPos + Length) return NULL;
|
||||
char *p = fgets(strbuf, len, File);
|
||||
if (p != NULL)
|
||||
{
|
||||
int old = FilePos;
|
||||
FilePos = ftell(File);
|
||||
if (FilePos - StartPos > Length)
|
||||
{
|
||||
strbuf[Length - old + StartPos] = 0;
|
||||
}
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
private:
|
||||
long CalcFileLen() const
|
||||
{
|
||||
long endpos;
|
||||
|
||||
fseek(File, 0, SEEK_END);
|
||||
endpos = ftell(File);
|
||||
fseek(File, 0, SEEK_SET);
|
||||
return endpos;
|
||||
}
|
||||
};
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// FileReaderRedirect
|
||||
//
|
||||
// like the above, but uses another File reader as its backing data
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
class FileReaderRedirect : public FileReaderInterface
|
||||
{
|
||||
FileReader *mReader = nullptr;
|
||||
long StartPos = 0;
|
||||
long FilePos = 0;
|
||||
|
||||
public:
|
||||
FileReaderRedirect(FileReader &parent, long start, long length)
|
||||
{
|
||||
mReader = &parent;
|
||||
FilePos = start;
|
||||
StartPos = start;
|
||||
Length = length;
|
||||
Seek(0, SEEK_SET);
|
||||
}
|
||||
|
||||
virtual long Tell() const override
|
||||
{
|
||||
return FilePos - StartPos;
|
||||
}
|
||||
|
||||
virtual long Seek(long offset, int origin) override
|
||||
{
|
||||
switch (origin)
|
||||
{
|
||||
case SEEK_SET:
|
||||
offset += StartPos;
|
||||
break;
|
||||
|
||||
case SEEK_END:
|
||||
offset += StartPos + Length;
|
||||
break;
|
||||
|
||||
case SEEK_CUR:
|
||||
offset += (long)mReader->Tell();
|
||||
break;
|
||||
}
|
||||
if (offset < StartPos || offset > StartPos + Length) return -1; // out of scope
|
||||
if (mReader->Seek(offset, FileReader::SeekSet) == 0)
|
||||
{
|
||||
FilePos = offset;
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
virtual long Read(void *buffer, long len) override
|
||||
{
|
||||
assert(len >= 0);
|
||||
if (len <= 0) return 0;
|
||||
if (FilePos + len > StartPos + Length)
|
||||
{
|
||||
len = Length - FilePos + StartPos;
|
||||
}
|
||||
len = (long)mReader->Read(buffer, len);
|
||||
FilePos += len;
|
||||
return len;
|
||||
}
|
||||
|
||||
virtual char *Gets(char *strbuf, int len) override
|
||||
{
|
||||
if (len <= 0 || FilePos >= StartPos + Length) return NULL;
|
||||
char *p = mReader->Gets(strbuf, len);
|
||||
if (p != NULL)
|
||||
{
|
||||
int old = FilePos;
|
||||
FilePos = (long)mReader->Tell();
|
||||
if (FilePos - StartPos > Length)
|
||||
{
|
||||
strbuf[Length - old + StartPos] = 0;
|
||||
}
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// MemoryReader
|
||||
//
|
||||
// reads data from a block of memory
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
long MemoryReader::Tell() const
|
||||
{
|
||||
return FilePos;
|
||||
}
|
||||
|
||||
long MemoryReader::Seek(long offset, int origin)
|
||||
{
|
||||
switch (origin)
|
||||
{
|
||||
case SEEK_CUR:
|
||||
offset += FilePos;
|
||||
break;
|
||||
|
||||
case SEEK_END:
|
||||
offset += Length;
|
||||
break;
|
||||
|
||||
}
|
||||
if (offset < 0 || offset > Length) return -1;
|
||||
FilePos = std::clamp<long>(offset, 0, Length);
|
||||
return 0;
|
||||
}
|
||||
|
||||
long MemoryReader::Read(void *buffer, long len)
|
||||
{
|
||||
if (len>Length - FilePos) len = Length - FilePos;
|
||||
if (len<0) len = 0;
|
||||
memcpy(buffer, bufptr + FilePos, len);
|
||||
FilePos += len;
|
||||
return len;
|
||||
}
|
||||
|
||||
char *MemoryReader::Gets(char *strbuf, int len)
|
||||
{
|
||||
if (len>Length - FilePos) len = Length - FilePos;
|
||||
if (len <= 0) return NULL;
|
||||
|
||||
char *p = strbuf;
|
||||
while (len > 1)
|
||||
{
|
||||
if (bufptr[FilePos] == 0)
|
||||
{
|
||||
FilePos++;
|
||||
break;
|
||||
}
|
||||
if (bufptr[FilePos] != '\r')
|
||||
{
|
||||
*p++ = bufptr[FilePos];
|
||||
len--;
|
||||
if (bufptr[FilePos] == '\n')
|
||||
{
|
||||
FilePos++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
FilePos++;
|
||||
}
|
||||
if (p == strbuf) return NULL;
|
||||
*p++ = 0;
|
||||
return strbuf;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// MemoryArrayReader
|
||||
//
|
||||
// reads data from an array of memory
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
class MemoryArrayReader : public MemoryReader
|
||||
{
|
||||
std::vector<uint8_t> buf;
|
||||
|
||||
public:
|
||||
MemoryArrayReader(const char *buffer, long length)
|
||||
{
|
||||
if (length > 0)
|
||||
{
|
||||
buf.resize(length);
|
||||
memcpy(&buf[0], buffer, length);
|
||||
}
|
||||
UpdateBuffer();
|
||||
}
|
||||
|
||||
std::vector<uint8_t> &GetArray() { return buf; }
|
||||
|
||||
void UpdateBuffer()
|
||||
{
|
||||
bufptr = (const char*)buf.data();
|
||||
FilePos = 0;
|
||||
Length = (long)buf.size();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// FileReader
|
||||
//
|
||||
// this wraps the different reader types in an object with value semantics.
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
bool FileReader::OpenFile(const char *filename, FileReader::Size start, FileReader::Size length)
|
||||
{
|
||||
auto reader = new StdFileReader;
|
||||
if (!reader->Open(filename, (long)start, (long)length))
|
||||
{
|
||||
delete reader;
|
||||
return false;
|
||||
}
|
||||
Close();
|
||||
mReader = reader;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FileReader::OpenFilePart(FileReader &parent, FileReader::Size start, FileReader::Size length)
|
||||
{
|
||||
auto reader = new FileReaderRedirect(parent, (long)start, (long)length);
|
||||
Close();
|
||||
mReader = reader;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FileReader::OpenMemory(const void *mem, FileReader::Size length)
|
||||
{
|
||||
Close();
|
||||
mReader = new MemoryReader((const char *)mem, (long)length);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FileReader::OpenMemoryArray(const void *mem, FileReader::Size length)
|
||||
{
|
||||
Close();
|
||||
mReader = new MemoryArrayReader((const char *)mem, (long)length);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FileReader::OpenMemoryArray(std::function<bool(std::vector<uint8_t>&)> getter)
|
||||
{
|
||||
auto reader = new MemoryArrayReader(nullptr, 0);
|
||||
if (getter(reader->GetArray()))
|
||||
{
|
||||
Close();
|
||||
reader->UpdateBuffer();
|
||||
mReader = reader;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// This will keep the old buffer, if one existed
|
||||
delete reader;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// FileWriter (the motivation here is to have a buffer writing subclass)
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
bool FileWriter::OpenDirect(const char *filename)
|
||||
{
|
||||
File = myfopen(filename, "wb");
|
||||
return (File != NULL);
|
||||
}
|
||||
|
||||
FileWriter *FileWriter::Open(const char *filename)
|
||||
{
|
||||
FileWriter *fwrit = new FileWriter();
|
||||
if (fwrit->OpenDirect(filename))
|
||||
{
|
||||
return fwrit;
|
||||
}
|
||||
delete fwrit;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t FileWriter::Write(const void *buffer, size_t len)
|
||||
{
|
||||
if (File != NULL)
|
||||
{
|
||||
return fwrite(buffer, 1, len, File);
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
long FileWriter::Tell()
|
||||
{
|
||||
if (File != NULL)
|
||||
{
|
||||
return ftell(File);
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
long FileWriter::Seek(long offset, int mode)
|
||||
{
|
||||
if (File != NULL)
|
||||
{
|
||||
return fseek(File, offset, mode);
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
size_t FileWriter::Printf(const char *fmt, ...)
|
||||
{
|
||||
char c[300];
|
||||
va_list arglist;
|
||||
va_start(arglist, fmt);
|
||||
auto n = vsnprintf(c, 300, fmt, arglist);
|
||||
std::string buf;
|
||||
buf.resize(n);
|
||||
va_start(arglist, fmt);
|
||||
vsnprintf(&buf.front(), n, fmt, arglist);
|
||||
va_end(arglist);
|
||||
return Write(buf.c_str(), n);
|
||||
}
|
||||
|
||||
size_t BufferWriter::Write(const void *buffer, size_t len)
|
||||
{
|
||||
unsigned int ofs = mBuffer.Reserve((unsigned)len);
|
||||
memcpy(&mBuffer[ofs], buffer, len);
|
||||
return len;
|
||||
}
|
||||
|
||||
}
|
||||
758
src/common/filesystem/source/files_decompress.cpp
Normal file
758
src/common/filesystem/source/files_decompress.cpp
Normal file
|
|
@ -0,0 +1,758 @@
|
|||
/*
|
||||
** files.cpp
|
||||
** Implements classes for reading from files or memory blocks
|
||||
**
|
||||
**---------------------------------------------------------------------------
|
||||
** Copyright 1998-2008 Randy Heit
|
||||
** Copyright 2005-2008 Christoph Oelckers
|
||||
** All rights reserved.
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions
|
||||
** are met:
|
||||
**
|
||||
** 1. Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** 2. Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in the
|
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3. The name of the author may not be used to endorse or promote products
|
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**---------------------------------------------------------------------------
|
||||
**
|
||||
*/
|
||||
|
||||
// Caution: LzmaDec also pulls in windows.h!
|
||||
#define NOMINMAX
|
||||
#include "LzmaDec.h"
|
||||
#include <zlib.h>
|
||||
#include <bzlib.h>
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "fs_files.h"
|
||||
|
||||
namespace FileSys {
|
||||
using namespace byteswap;
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// DecompressionError
|
||||
//
|
||||
// Allows catching errors from the decompressor. The default is just to
|
||||
// return failure from the calling function.
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
void DecompressorBase::DecompressionError(const char *error, ...) const
|
||||
{
|
||||
if (exceptions)
|
||||
{
|
||||
va_list argptr;
|
||||
va_start(argptr, error);
|
||||
throw FileSystemException(error, argptr);
|
||||
va_end(argptr);
|
||||
}
|
||||
}
|
||||
|
||||
long DecompressorBase::Tell () const
|
||||
{
|
||||
DecompressionError("Cannot get position of decompressor stream");
|
||||
return 0;
|
||||
}
|
||||
long DecompressorBase::Seek (long offset, int origin)
|
||||
{
|
||||
DecompressionError("Cannot seek in decompressor stream");
|
||||
return 0;
|
||||
}
|
||||
char *DecompressorBase::Gets(char *strbuf, int len)
|
||||
{
|
||||
DecompressionError("Cannot use Gets on decompressor stream");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void DecompressorBase::SetOwnsReader()
|
||||
{
|
||||
OwnedFile = std::move(*File);
|
||||
File = &OwnedFile;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
//
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
static const char* ZLibError(int zerr)
|
||||
{
|
||||
static const char* const errs[6] = { "Errno", "Stream Error", "Data Error", "Memory Error", "Buffer Error", "Version Error" };
|
||||
if (zerr >= 0 || zerr < -6) return "Unknown";
|
||||
else return errs[-zerr - 1];
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// DecompressorZ
|
||||
//
|
||||
// The zlib wrapper
|
||||
// reads data from a ZLib compressed stream
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
class DecompressorZ : public DecompressorBase
|
||||
{
|
||||
enum { BUFF_SIZE = 4096 };
|
||||
|
||||
bool SawEOF = false;
|
||||
z_stream Stream;
|
||||
uint8_t InBuff[BUFF_SIZE];
|
||||
|
||||
public:
|
||||
bool Open (FileReader *file, bool zip)
|
||||
{
|
||||
if (File != nullptr)
|
||||
{
|
||||
DecompressionError("File already open");
|
||||
return false;
|
||||
}
|
||||
|
||||
int err;
|
||||
|
||||
File = file;
|
||||
FillBuffer ();
|
||||
|
||||
Stream.zalloc = Z_NULL;
|
||||
Stream.zfree = Z_NULL;
|
||||
|
||||
if (!zip) err = inflateInit (&Stream);
|
||||
else err = inflateInit2 (&Stream, -MAX_WBITS);
|
||||
|
||||
if (err < Z_OK)
|
||||
{
|
||||
DecompressionError ("DecompressorZ: inflateInit failed: %s\n", ZLibError(err));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
~DecompressorZ ()
|
||||
{
|
||||
inflateEnd (&Stream);
|
||||
}
|
||||
|
||||
long Read (void *buffer, long len) override
|
||||
{
|
||||
int err;
|
||||
|
||||
if (File == nullptr)
|
||||
{
|
||||
DecompressionError("File not open");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Stream.next_out = (Bytef *)buffer;
|
||||
Stream.avail_out = len;
|
||||
|
||||
do
|
||||
{
|
||||
err = inflate (&Stream, Z_SYNC_FLUSH);
|
||||
if (Stream.avail_in == 0 && !SawEOF)
|
||||
{
|
||||
FillBuffer ();
|
||||
}
|
||||
} while (err == Z_OK && Stream.avail_out != 0);
|
||||
|
||||
if (err != Z_OK && err != Z_STREAM_END)
|
||||
{
|
||||
DecompressionError ("Corrupt zlib stream");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (Stream.avail_out != 0)
|
||||
{
|
||||
DecompressionError ("Ran out of data in zlib stream");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return len - Stream.avail_out;
|
||||
}
|
||||
|
||||
void FillBuffer ()
|
||||
{
|
||||
auto numread = File->Read (InBuff, BUFF_SIZE);
|
||||
|
||||
if (numread < BUFF_SIZE)
|
||||
{
|
||||
SawEOF = true;
|
||||
}
|
||||
Stream.next_in = InBuff;
|
||||
Stream.avail_in = (uInt)numread;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// DecompressorZ
|
||||
//
|
||||
// The bzip2 wrapper
|
||||
// reads data from a libbzip2 compressed stream
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
class DecompressorBZ2;
|
||||
static DecompressorBZ2 * stupidGlobal; // Why does that dumb global error callback not pass the decompressor state?
|
||||
// Thanks to that brain-dead interface we have to use a global variable to get the error to the proper handler.
|
||||
|
||||
class DecompressorBZ2 : public DecompressorBase
|
||||
{
|
||||
enum { BUFF_SIZE = 4096 };
|
||||
|
||||
bool SawEOF = false;
|
||||
bz_stream Stream;
|
||||
uint8_t InBuff[BUFF_SIZE];
|
||||
|
||||
public:
|
||||
bool Open(FileReader *file)
|
||||
{
|
||||
if (File != nullptr)
|
||||
{
|
||||
DecompressionError("File already open");
|
||||
return false;
|
||||
}
|
||||
|
||||
int err;
|
||||
|
||||
File = file;
|
||||
stupidGlobal = this;
|
||||
FillBuffer ();
|
||||
|
||||
Stream.bzalloc = NULL;
|
||||
Stream.bzfree = NULL;
|
||||
Stream.opaque = NULL;
|
||||
|
||||
err = BZ2_bzDecompressInit(&Stream, 0, 0);
|
||||
|
||||
if (err != BZ_OK)
|
||||
{
|
||||
DecompressionError ("DecompressorBZ2: bzDecompressInit failed: %d\n", err);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
~DecompressorBZ2 ()
|
||||
{
|
||||
stupidGlobal = this;
|
||||
BZ2_bzDecompressEnd (&Stream);
|
||||
}
|
||||
|
||||
long Read (void *buffer, long len) override
|
||||
{
|
||||
if (File == nullptr)
|
||||
{
|
||||
DecompressionError("File not open");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int err;
|
||||
|
||||
stupidGlobal = this;
|
||||
Stream.next_out = (char *)buffer;
|
||||
Stream.avail_out = len;
|
||||
|
||||
do
|
||||
{
|
||||
err = BZ2_bzDecompress(&Stream);
|
||||
if (Stream.avail_in == 0 && !SawEOF)
|
||||
{
|
||||
FillBuffer ();
|
||||
}
|
||||
} while (err == BZ_OK && Stream.avail_out != 0);
|
||||
|
||||
if (err != BZ_OK && err != BZ_STREAM_END)
|
||||
{
|
||||
DecompressionError ("Corrupt bzip2 stream");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (Stream.avail_out != 0)
|
||||
{
|
||||
DecompressionError ("Ran out of data in bzip2 stream");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return len - Stream.avail_out;
|
||||
}
|
||||
|
||||
void FillBuffer ()
|
||||
{
|
||||
auto numread = File->Read(InBuff, BUFF_SIZE);
|
||||
|
||||
if (numread < BUFF_SIZE)
|
||||
{
|
||||
SawEOF = true;
|
||||
}
|
||||
Stream.next_in = (char *)InBuff;
|
||||
Stream.avail_in = (unsigned)numread;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// bz_internal_error
|
||||
//
|
||||
// libbzip2 wants this, since we build it with BZ_NO_STDIO set.
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
extern "C" void bz_internal_error (int errcode)
|
||||
{
|
||||
if (stupidGlobal) stupidGlobal->DecompressionError("libbzip2: internal error number %d\n", errcode);
|
||||
else std::terminate();
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// DecompressorLZMA
|
||||
//
|
||||
// The lzma wrapper
|
||||
// reads data from a LZMA compressed stream
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
static void *SzAlloc(ISzAllocPtr, size_t size) { return malloc(size); }
|
||||
static void SzFree(ISzAllocPtr, void *address) { free(address); }
|
||||
ISzAlloc g_Alloc = { SzAlloc, SzFree };
|
||||
|
||||
// Wraps around a Decompressor to decompress a lzma stream
|
||||
class DecompressorLZMA : public DecompressorBase
|
||||
{
|
||||
enum { BUFF_SIZE = 4096 };
|
||||
|
||||
bool SawEOF = false;
|
||||
CLzmaDec Stream;
|
||||
size_t Size;
|
||||
size_t InPos, InSize;
|
||||
size_t OutProcessed;
|
||||
uint8_t InBuff[BUFF_SIZE];
|
||||
|
||||
public:
|
||||
|
||||
bool Open(FileReader *file, size_t uncompressed_size)
|
||||
{
|
||||
if (File != nullptr)
|
||||
{
|
||||
DecompressionError("File already open");
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t header[4 + LZMA_PROPS_SIZE];
|
||||
int err;
|
||||
File = file;
|
||||
|
||||
Size = uncompressed_size;
|
||||
OutProcessed = 0;
|
||||
|
||||
// Read zip LZMA properties header
|
||||
if (File->Read(header, sizeof(header)) < (long)sizeof(header))
|
||||
{
|
||||
DecompressionError("DecompressorLZMA: File too short\n");
|
||||
return false;
|
||||
}
|
||||
if (header[2] + header[3] * 256 != LZMA_PROPS_SIZE)
|
||||
{
|
||||
DecompressionError("DecompressorLZMA: LZMA props size is %d (expected %d)\n",
|
||||
header[2] + header[3] * 256, LZMA_PROPS_SIZE);
|
||||
return false;
|
||||
}
|
||||
|
||||
FillBuffer();
|
||||
|
||||
LzmaDec_Construct(&Stream);
|
||||
err = LzmaDec_Allocate(&Stream, header + 4, LZMA_PROPS_SIZE, &g_Alloc);
|
||||
|
||||
if (err != SZ_OK)
|
||||
{
|
||||
DecompressionError("DecompressorLZMA: LzmaDec_Allocate failed: %d\n", err);
|
||||
return false;
|
||||
}
|
||||
|
||||
LzmaDec_Init(&Stream);
|
||||
return true;
|
||||
}
|
||||
|
||||
~DecompressorLZMA ()
|
||||
{
|
||||
LzmaDec_Free(&Stream, &g_Alloc);
|
||||
}
|
||||
|
||||
long Read (void *buffer, long len) override
|
||||
{
|
||||
if (File == nullptr)
|
||||
{
|
||||
DecompressionError("File not open");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int err;
|
||||
Byte *next_out = (Byte *)buffer;
|
||||
|
||||
do
|
||||
{
|
||||
ELzmaFinishMode finish_mode = LZMA_FINISH_ANY;
|
||||
ELzmaStatus status;
|
||||
size_t out_processed = len;
|
||||
size_t in_processed = InSize;
|
||||
|
||||
err = LzmaDec_DecodeToBuf(&Stream, next_out, &out_processed, InBuff + InPos, &in_processed, finish_mode, &status);
|
||||
InPos += in_processed;
|
||||
InSize -= in_processed;
|
||||
next_out += out_processed;
|
||||
len = (long)(len - out_processed);
|
||||
if (err != SZ_OK)
|
||||
{
|
||||
DecompressionError ("Corrupt LZMA stream");
|
||||
return 0;
|
||||
}
|
||||
if (in_processed == 0 && out_processed == 0)
|
||||
{
|
||||
if (status != LZMA_STATUS_FINISHED_WITH_MARK)
|
||||
{
|
||||
DecompressionError ("Corrupt LZMA stream");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (InSize == 0 && !SawEOF)
|
||||
{
|
||||
FillBuffer ();
|
||||
}
|
||||
} while (err == SZ_OK && len != 0);
|
||||
|
||||
if (err != Z_OK && err != Z_STREAM_END)
|
||||
{
|
||||
DecompressionError ("Corrupt LZMA stream");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (len != 0)
|
||||
{
|
||||
DecompressionError ("Ran out of data in LZMA stream");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (long)(next_out - (Byte *)buffer);
|
||||
}
|
||||
|
||||
void FillBuffer ()
|
||||
{
|
||||
auto numread = File->Read(InBuff, BUFF_SIZE);
|
||||
|
||||
if (numread < BUFF_SIZE)
|
||||
{
|
||||
SawEOF = true;
|
||||
}
|
||||
InPos = 0;
|
||||
InSize = numread;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Console Doom LZSS wrapper.
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
class DecompressorLZSS : public DecompressorBase
|
||||
{
|
||||
enum { BUFF_SIZE = 4096, WINDOW_SIZE = 4096, INTERNAL_BUFFER_SIZE = 128 };
|
||||
|
||||
bool SawEOF;
|
||||
uint8_t InBuff[BUFF_SIZE];
|
||||
|
||||
enum StreamState
|
||||
{
|
||||
STREAM_EMPTY,
|
||||
STREAM_BITS,
|
||||
STREAM_FLUSH,
|
||||
STREAM_FINAL
|
||||
};
|
||||
struct
|
||||
{
|
||||
StreamState State;
|
||||
|
||||
uint8_t *In;
|
||||
unsigned int AvailIn;
|
||||
unsigned int InternalOut;
|
||||
|
||||
uint8_t CFlags, Bits;
|
||||
|
||||
uint8_t Window[WINDOW_SIZE+INTERNAL_BUFFER_SIZE];
|
||||
const uint8_t *WindowData;
|
||||
uint8_t *InternalBuffer;
|
||||
} Stream;
|
||||
|
||||
void FillBuffer()
|
||||
{
|
||||
if(Stream.AvailIn)
|
||||
memmove(InBuff, Stream.In, Stream.AvailIn);
|
||||
|
||||
auto numread = File->Read(InBuff+Stream.AvailIn, BUFF_SIZE-Stream.AvailIn);
|
||||
|
||||
if (numread < BUFF_SIZE)
|
||||
{
|
||||
SawEOF = true;
|
||||
}
|
||||
Stream.In = InBuff;
|
||||
Stream.AvailIn = (unsigned)numread+Stream.AvailIn;
|
||||
}
|
||||
|
||||
// Reads a flag byte.
|
||||
void PrepareBlocks()
|
||||
{
|
||||
assert(Stream.InternalBuffer == Stream.WindowData);
|
||||
Stream.CFlags = *Stream.In++;
|
||||
--Stream.AvailIn;
|
||||
Stream.Bits = 0xFF;
|
||||
Stream.State = STREAM_BITS;
|
||||
}
|
||||
|
||||
// Reads the next chunk in the block. Returns true if successful and
|
||||
// returns false if it ran out of input data.
|
||||
bool UncompressBlock()
|
||||
{
|
||||
if(Stream.CFlags & 1)
|
||||
{
|
||||
// Check to see if we have enough input
|
||||
if(Stream.AvailIn < 2)
|
||||
return false;
|
||||
Stream.AvailIn -= 2;
|
||||
|
||||
uint16_t pos = BigShort(*(uint16_t*)Stream.In);
|
||||
uint8_t len = (pos & 0xF)+1;
|
||||
pos >>= 4;
|
||||
Stream.In += 2;
|
||||
if(len == 1)
|
||||
{
|
||||
// We've reached the end of the stream.
|
||||
Stream.State = STREAM_FINAL;
|
||||
return true;
|
||||
}
|
||||
|
||||
const uint8_t* copyStart = Stream.InternalBuffer-pos-1;
|
||||
|
||||
// Complete overlap: Single byte repeated
|
||||
if(pos == 0)
|
||||
memset(Stream.InternalBuffer, *copyStart, len);
|
||||
// No overlap: One copy
|
||||
else if(pos >= len)
|
||||
memcpy(Stream.InternalBuffer, copyStart, len);
|
||||
else
|
||||
{
|
||||
// Partial overlap: Copy in 2 or 3 chunks.
|
||||
do
|
||||
{
|
||||
unsigned int copy = std::min<unsigned int>(len, pos+1);
|
||||
memcpy(Stream.InternalBuffer, copyStart, copy);
|
||||
Stream.InternalBuffer += copy;
|
||||
Stream.InternalOut += copy;
|
||||
len -= copy;
|
||||
pos += copy; // Increase our position since we can copy twice as much the next round.
|
||||
}
|
||||
while(len);
|
||||
}
|
||||
|
||||
Stream.InternalOut += len;
|
||||
Stream.InternalBuffer += len;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Uncompressed byte.
|
||||
*Stream.InternalBuffer++ = *Stream.In++;
|
||||
--Stream.AvailIn;
|
||||
++Stream.InternalOut;
|
||||
}
|
||||
|
||||
Stream.CFlags >>= 1;
|
||||
Stream.Bits >>= 1;
|
||||
|
||||
// If we're done with this block, flush the output
|
||||
if(Stream.Bits == 0)
|
||||
Stream.State = STREAM_FLUSH;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public:
|
||||
bool Open(FileReader *file)
|
||||
{
|
||||
if (File != nullptr)
|
||||
{
|
||||
DecompressionError("File already open");
|
||||
return false;
|
||||
}
|
||||
|
||||
File = file;
|
||||
Stream.State = STREAM_EMPTY;
|
||||
Stream.WindowData = Stream.InternalBuffer = Stream.Window+WINDOW_SIZE;
|
||||
Stream.InternalOut = 0;
|
||||
Stream.AvailIn = 0;
|
||||
|
||||
FillBuffer();
|
||||
return true;
|
||||
}
|
||||
|
||||
~DecompressorLZSS()
|
||||
{
|
||||
}
|
||||
|
||||
long Read(void *buffer, long len) override
|
||||
{
|
||||
|
||||
uint8_t *Out = (uint8_t*)buffer;
|
||||
long AvailOut = len;
|
||||
|
||||
do
|
||||
{
|
||||
while(Stream.AvailIn)
|
||||
{
|
||||
if(Stream.State == STREAM_EMPTY)
|
||||
PrepareBlocks();
|
||||
else if(Stream.State == STREAM_BITS && !UncompressBlock())
|
||||
break;
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
unsigned int copy = std::min<unsigned int>(Stream.InternalOut, AvailOut);
|
||||
if(copy > 0)
|
||||
{
|
||||
memcpy(Out, Stream.WindowData, copy);
|
||||
Out += copy;
|
||||
AvailOut -= copy;
|
||||
|
||||
// Slide our window
|
||||
memmove(Stream.Window, Stream.Window+copy, WINDOW_SIZE+INTERNAL_BUFFER_SIZE-copy);
|
||||
Stream.InternalBuffer -= copy;
|
||||
Stream.InternalOut -= copy;
|
||||
}
|
||||
|
||||
if(Stream.State == STREAM_FINAL)
|
||||
break;
|
||||
|
||||
if(Stream.InternalOut == 0 && Stream.State == STREAM_FLUSH)
|
||||
Stream.State = STREAM_EMPTY;
|
||||
|
||||
if(Stream.AvailIn < 2)
|
||||
FillBuffer();
|
||||
}
|
||||
while(AvailOut && Stream.State != STREAM_FINAL);
|
||||
|
||||
assert(AvailOut == 0);
|
||||
return (long)(Out - (uint8_t*)buffer);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
bool FileReader::OpenDecompressor(FileReader &parent, Size length, int method, bool seekable, bool exceptions)
|
||||
{
|
||||
DecompressorBase* dec = nullptr;
|
||||
try
|
||||
{
|
||||
FileReader* p = &parent;
|
||||
switch (method & ~METHOD_TRANSFEROWNER)
|
||||
{
|
||||
case METHOD_DEFLATE:
|
||||
case METHOD_ZLIB:
|
||||
{
|
||||
auto idec = new DecompressorZ;
|
||||
dec = idec;
|
||||
idec->EnableExceptions(exceptions);
|
||||
if (!idec->Open(p, method == METHOD_DEFLATE))
|
||||
{
|
||||
delete idec;
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case METHOD_BZIP2:
|
||||
{
|
||||
auto idec = new DecompressorBZ2;
|
||||
dec = idec;
|
||||
idec->EnableExceptions(exceptions);
|
||||
if (!idec->Open(p))
|
||||
{
|
||||
delete idec;
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case METHOD_LZMA:
|
||||
{
|
||||
auto idec = new DecompressorLZMA;
|
||||
dec = idec;
|
||||
idec->EnableExceptions(exceptions);
|
||||
if (!idec->Open(p, length))
|
||||
{
|
||||
delete idec;
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case METHOD_LZSS:
|
||||
{
|
||||
auto idec = new DecompressorLZSS;
|
||||
dec = idec;
|
||||
idec->EnableExceptions(exceptions);
|
||||
if (!idec->Open(p))
|
||||
{
|
||||
delete idec;
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// todo: METHOD_IMPLODE, METHOD_SHRINK
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
if (method & METHOD_TRANSFEROWNER)
|
||||
{
|
||||
dec->SetOwnsReader();
|
||||
}
|
||||
|
||||
dec->Length = (long)length;
|
||||
if (!seekable)
|
||||
{
|
||||
Close();
|
||||
mReader = dec;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// todo: create a wrapper. for now this fails
|
||||
delete dec;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
if (dec) delete dec;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
1583
src/common/filesystem/source/filesystem.cpp
Normal file
1583
src/common/filesystem/source/filesystem.cpp
Normal file
File diff suppressed because it is too large
Load diff
408
src/common/filesystem/source/fs_findfile.cpp
Normal file
408
src/common/filesystem/source/fs_findfile.cpp
Normal file
|
|
@ -0,0 +1,408 @@
|
|||
/*
|
||||
** findfile.cpp
|
||||
** Wrapper around the native directory scanning APIs
|
||||
**
|
||||
**---------------------------------------------------------------------------
|
||||
** Copyright 1998-2016 Randy Heit
|
||||
** Copyright 2005-2020 Christoph Oelckers
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions
|
||||
** are met:
|
||||
**
|
||||
** 1. Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** 2. Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in the
|
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3. The name of the author may not be used to endorse or promote products
|
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**---------------------------------------------------------------------------
|
||||
**
|
||||
*/
|
||||
|
||||
#include "fs_findfile.h"
|
||||
#include <vector>
|
||||
|
||||
namespace FileSys {
|
||||
|
||||
enum
|
||||
{
|
||||
ZPATH_MAX = 260
|
||||
};
|
||||
|
||||
#ifndef _WIN32
|
||||
|
||||
|
||||
#else
|
||||
|
||||
|
||||
enum
|
||||
{
|
||||
FA_RDONLY = 1,
|
||||
FA_HIDDEN = 2,
|
||||
FA_SYSTEM = 4,
|
||||
FA_DIREC = 16,
|
||||
FA_ARCH = 32,
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef _WIN32
|
||||
|
||||
#include <unistd.h>
|
||||
#include <fnmatch.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <dirent.h>
|
||||
|
||||
struct findstate_t
|
||||
{
|
||||
std::string path;
|
||||
struct dirent** namelist;
|
||||
int current;
|
||||
int count;
|
||||
};
|
||||
|
||||
static const char* FS_FindName(findstate_t* fileinfo)
|
||||
{
|
||||
return (fileinfo->namelist[fileinfo->current]->d_name);
|
||||
}
|
||||
|
||||
enum
|
||||
{
|
||||
FA_RDONLY = 1,
|
||||
FA_HIDDEN = 2,
|
||||
FA_SYSTEM = 4,
|
||||
FA_DIREC = 8,
|
||||
FA_ARCH = 16,
|
||||
};
|
||||
|
||||
|
||||
static const char *pattern;
|
||||
|
||||
static int matchfile(const struct dirent *ent)
|
||||
{
|
||||
return fnmatch(pattern, ent->d_name, FNM_NOESCAPE) == 0;
|
||||
}
|
||||
|
||||
static void *FS_FindFirst(const char *const filespec, findstate_t *const fileinfo)
|
||||
{
|
||||
const char* dir;
|
||||
|
||||
const char *const slash = strrchr(filespec, '/');
|
||||
|
||||
if (slash)
|
||||
{
|
||||
pattern = slash + 1;
|
||||
fileinfo->path = std::string(filespec, 0, slash - filespec + 1);
|
||||
dir = fileinfo->path.c_str();
|
||||
}
|
||||
else
|
||||
{
|
||||
pattern = filespec;
|
||||
dir = ".";
|
||||
}
|
||||
|
||||
fileinfo->current = 0;
|
||||
fileinfo->count = scandir(dir, &fileinfo->namelist, matchfile, alphasort);
|
||||
|
||||
if (fileinfo->count > 0)
|
||||
{
|
||||
return fileinfo;
|
||||
}
|
||||
|
||||
return (void *)-1;
|
||||
}
|
||||
|
||||
static int FS_FindNext(void *const handle, findstate_t *const fileinfo)
|
||||
{
|
||||
findstate_t *const state = static_cast<findstate_t *>(handle);
|
||||
|
||||
if (state->current < fileinfo->count)
|
||||
{
|
||||
return ++state->current < fileinfo->count ? 0 : -1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int FS_FindClose(void *const handle)
|
||||
{
|
||||
findstate_t *const state = static_cast<findstate_t *>(handle);
|
||||
|
||||
if (handle != (void *)-1 && state->count > 0)
|
||||
{
|
||||
for (int i = 0; i < state->count; ++i)
|
||||
{
|
||||
free(state->namelist[i]);
|
||||
}
|
||||
|
||||
free(state->namelist);
|
||||
state->namelist = nullptr;
|
||||
state->count = 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int FS_FindAttr(findstate_t *const fileinfo)
|
||||
{
|
||||
dirent *const ent = fileinfo->namelist[fileinfo->current];
|
||||
const std::string path = fileinfo->path + ent->d_name;
|
||||
bool isdir;
|
||||
|
||||
if (DirEntryExists(path.c_str(), &isdir))
|
||||
{
|
||||
return isdir ? FA_DIREC : 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string FS_FullPath(const char* directory)
|
||||
{
|
||||
// todo
|
||||
return directory
|
||||
}
|
||||
|
||||
static size_t FS_GetFileSize(findstate_t* handle, const char* pathname)
|
||||
{
|
||||
struct stat info;
|
||||
bool res = stat(pathname, &info) == 0;
|
||||
if (!res || (info.st_mode & S_IFDIR)) return 0;
|
||||
return (size_t)info.st_size;
|
||||
}
|
||||
|
||||
|
||||
#else
|
||||
|
||||
#include <windows.h>
|
||||
#include <direct.h>
|
||||
|
||||
std::wstring toWide(const char* str)
|
||||
{
|
||||
int len = (int)strlen(str);
|
||||
int size_needed = MultiByteToWideChar(CP_UTF8, 0, str, len, nullptr, 0);
|
||||
std::wstring wide;
|
||||
wide.resize(size_needed);
|
||||
MultiByteToWideChar(CP_UTF8, 0, str, len, &wide.front(), size_needed);
|
||||
return wide;
|
||||
}
|
||||
|
||||
static std::string toUtf8(const wchar_t* str)
|
||||
{
|
||||
auto len = wcslen(str);
|
||||
int size_needed = WideCharToMultiByte(CP_UTF8, 0, str, (int)len, nullptr, 0, nullptr, nullptr);
|
||||
std::string utf8;
|
||||
utf8.resize(size_needed);
|
||||
WideCharToMultiByte(CP_UTF8, 0, str, (int)len, &utf8.front(), size_needed, nullptr, nullptr);
|
||||
return utf8;
|
||||
}
|
||||
|
||||
struct findstate_t
|
||||
{
|
||||
struct FileTime
|
||||
{
|
||||
uint32_t lo, hi;
|
||||
};
|
||||
// Mirror WIN32_FIND_DATAW in <winbase.h>. We cannot pull in the Windows header here as it would pollute all consumers' symbol space.
|
||||
struct WinData
|
||||
{
|
||||
uint32_t Attribs;
|
||||
FileTime Times[3];
|
||||
uint32_t Size[2];
|
||||
uint32_t Reserved[2];
|
||||
wchar_t Name[ZPATH_MAX];
|
||||
wchar_t AltName[14];
|
||||
};
|
||||
WinData FindData;
|
||||
std::string UTF8Name;
|
||||
};
|
||||
|
||||
|
||||
static int FS_FindAttr(findstate_t* fileinfo)
|
||||
{
|
||||
return fileinfo->FindData.Attribs;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// FS_FindFirst
|
||||
//
|
||||
// Start a pattern matching sequence.
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
|
||||
static void *FS_FindFirst(const char *filespec, findstate_t *fileinfo)
|
||||
{
|
||||
static_assert(sizeof(WIN32_FIND_DATAW) == sizeof(fileinfo->FindData), "FindData size mismatch");
|
||||
return FindFirstFileW(toWide(filespec).c_str(), (LPWIN32_FIND_DATAW)&fileinfo->FindData);
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// FS_FindNext
|
||||
//
|
||||
// Return the next file in a pattern matching sequence.
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
static int FS_FindNext(void *handle, findstate_t *fileinfo)
|
||||
{
|
||||
return FindNextFileW((HANDLE)handle, (LPWIN32_FIND_DATAW)&fileinfo->FindData) == 0;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// FS_FindClose
|
||||
//
|
||||
// Finish a pattern matching sequence.
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
static int FS_FindClose(void *handle)
|
||||
{
|
||||
return FindClose((HANDLE)handle);
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// FS_FindName
|
||||
//
|
||||
// Returns the name for an entry
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
static const char *FS_FindName(findstate_t *fileinfo)
|
||||
{
|
||||
fileinfo->UTF8Name = toUtf8(fileinfo->FindData.Name);
|
||||
return fileinfo->UTF8Name.c_str();
|
||||
}
|
||||
|
||||
std::string FS_FullPath(const char* directory)
|
||||
{
|
||||
auto wdirectory = _wfullpath(nullptr, toWide(directory).c_str(), _MAX_PATH);
|
||||
std::string sdirectory = toUtf8(wdirectory);
|
||||
free((void*)wdirectory);
|
||||
for (auto& c : sdirectory) if (c == '\\') c = '/';
|
||||
return sdirectory;
|
||||
}
|
||||
|
||||
static size_t FS_GetFileSize(findstate_t* handle, const char* pathname)
|
||||
{
|
||||
return handle->FindData.Size[1] + ((uint64_t)handle->FindData.Size[0] << 32);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// ScanDirectory
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
static bool DoScanDirectory(FileList& list, const char* dirpath, const char* match, const char* relpath, bool nosubdir, bool readhidden)
|
||||
{
|
||||
findstate_t find;
|
||||
|
||||
std::string dirpathn = dirpath;
|
||||
FixPathSeparator(&dirpathn.front());
|
||||
if (dirpathn[dirpathn.length() - 1] != '/') dirpathn += '/';
|
||||
|
||||
std::string dirmatch = dirpathn;
|
||||
dirmatch += match;
|
||||
|
||||
auto handle = FS_FindFirst(dirmatch.c_str(), &find);
|
||||
if (handle == ((void*)(-1))) return false;
|
||||
FileListEntry fl;
|
||||
do
|
||||
{
|
||||
auto attr = FS_FindAttr(&find);
|
||||
if (!readhidden && (attr & FA_HIDDEN))
|
||||
{
|
||||
// Skip hidden files and directories. (Prevents SVN/git bookkeeping info from being included.)
|
||||
continue;
|
||||
}
|
||||
auto fn = FS_FindName(&find);
|
||||
|
||||
if (attr & FA_DIREC)
|
||||
{
|
||||
if (fn[0] == '.' &&
|
||||
(fn[1] == '\0' ||
|
||||
(fn[1] == '.' && fn[2] == '\0')))
|
||||
{
|
||||
// Do not record . and .. directories.
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
fl.FileName = fn;
|
||||
fl.FilePath = dirpathn + fn;
|
||||
fl.FilePathRel = relpath;
|
||||
if (fl.FilePathRel.length() > 0) fl.FilePathRel += '/';
|
||||
fl.FilePathRel += fn;
|
||||
fl.isDirectory = !!(attr & FA_DIREC);
|
||||
fl.isReadonly = !!(attr & FA_RDONLY);
|
||||
fl.isHidden = !!(attr & FA_HIDDEN);
|
||||
fl.isSystem = !!(attr & FA_SYSTEM);
|
||||
fl.Length = FS_GetFileSize(&find, fl.FilePath.c_str());
|
||||
list.push_back(fl);
|
||||
if (!nosubdir && (attr & FA_DIREC))
|
||||
{
|
||||
DoScanDirectory(list, fl.FilePath.c_str(), match, fl.FilePathRel.c_str(), false, readhidden);
|
||||
fl.Length = 0;
|
||||
}
|
||||
} while (FS_FindNext(handle, &find) == 0);
|
||||
FS_FindClose(handle);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool ScanDirectory(std::vector<FileListEntry>& list, const char* dirpath, const char* match, bool nosubdir, bool readhidden)
|
||||
{
|
||||
return DoScanDirectory(list, dirpath, match, "", nosubdir, readhidden);
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// DirEntryExists
|
||||
//
|
||||
// Returns true if the given path exists, be it a directory or a file.
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
bool FS_DirEntryExists(const char* pathname, bool* isdir)
|
||||
{
|
||||
if (isdir) *isdir = false;
|
||||
if (pathname == NULL || *pathname == 0)
|
||||
return false;
|
||||
|
||||
#ifndef _WIN32
|
||||
struct stat info;
|
||||
bool res = stat(pathname, &info) == 0;
|
||||
#else
|
||||
// Windows must use the wide version of stat to preserve non-standard paths.
|
||||
auto wstr = toWide(pathname);
|
||||
struct _stat64 info;
|
||||
bool res = _wstat64(wstr.c_str(), &info) == 0;
|
||||
#endif
|
||||
if (isdir) *isdir = !!(info.st_mode & S_IFDIR);
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
||||
130
src/common/filesystem/source/fs_stringpool.cpp
Normal file
130
src/common/filesystem/source/fs_stringpool.cpp
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
/*
|
||||
** stringpool.cpp
|
||||
** allocate static strings from larger blocks
|
||||
**
|
||||
**---------------------------------------------------------------------------
|
||||
** Copyright 2010 Randy Heit
|
||||
** Copyright 2023 Christoph Oelckers
|
||||
** All rights reserved.
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions
|
||||
** are met:
|
||||
**
|
||||
** 1. Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** 2. Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in the
|
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3. The name of the author may not be used to endorse or promote products
|
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**---------------------------------------------------------------------------
|
||||
**
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include "fs_stringpool.h"
|
||||
|
||||
namespace FileSys {
|
||||
|
||||
struct StringPool::Block
|
||||
{
|
||||
Block *NextBlock;
|
||||
void *Limit; // End of this block
|
||||
void *Avail; // Start of free space in this block
|
||||
void *alignme; // align to 16 bytes.
|
||||
|
||||
void *Alloc(size_t size);
|
||||
};
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// StringPool Destructor
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
StringPool::~StringPool()
|
||||
{
|
||||
for (Block *next, *block = TopBlock; block != nullptr; block = next)
|
||||
{
|
||||
next = block->NextBlock;
|
||||
free(block);
|
||||
}
|
||||
TopBlock = nullptr;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// StringPool :: Alloc
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
void *StringPool::Block::Alloc(size_t size)
|
||||
{
|
||||
if ((char *)Avail + size > Limit)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
void *res = Avail;
|
||||
Avail = ((char *)Avail + size);
|
||||
return res;
|
||||
}
|
||||
|
||||
StringPool::Block *StringPool::AddBlock(size_t size)
|
||||
{
|
||||
size += sizeof(Block); // Account for header size
|
||||
|
||||
// Allocate a new block
|
||||
if (size < BlockSize)
|
||||
{
|
||||
size = BlockSize;
|
||||
}
|
||||
auto mem = (Block *)malloc(size);
|
||||
if (mem == nullptr)
|
||||
{
|
||||
|
||||
}
|
||||
mem->Limit = (uint8_t *)mem + size;
|
||||
mem->Avail = &mem[1];
|
||||
mem->NextBlock = TopBlock;
|
||||
TopBlock = mem;
|
||||
return mem;
|
||||
}
|
||||
|
||||
void *StringPool::iAlloc(size_t size)
|
||||
{
|
||||
Block *block;
|
||||
|
||||
for (block = TopBlock; block != nullptr; block = block->NextBlock)
|
||||
{
|
||||
void *res = block->Alloc(size);
|
||||
if (res != nullptr)
|
||||
{
|
||||
return res;
|
||||
}
|
||||
}
|
||||
block = AddBlock(size);
|
||||
return block->Alloc(size);
|
||||
}
|
||||
|
||||
const char* StringPool::Strdup(const char* str)
|
||||
{
|
||||
char* p = (char*)iAlloc((strlen(str) + 8) & ~7 );
|
||||
strcpy(p, str);
|
||||
return p;
|
||||
}
|
||||
|
||||
}
|
||||
29
src/common/filesystem/source/fs_stringpool.h
Normal file
29
src/common/filesystem/source/fs_stringpool.h
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#pragma once
|
||||
|
||||
namespace FileSys {
|
||||
// Storage for all the static strings the file system must hold.
|
||||
class StringPool
|
||||
{
|
||||
// do not allow externally defining this.
|
||||
friend class FileSystem;
|
||||
friend class FResourceFile;
|
||||
private:
|
||||
StringPool(size_t blocksize = 10*1024) : TopBlock(nullptr), FreeBlocks(nullptr), BlockSize(blocksize) {}
|
||||
public:
|
||||
~StringPool();
|
||||
const char* Strdup(const char*);
|
||||
|
||||
protected:
|
||||
struct Block;
|
||||
|
||||
Block *AddBlock(size_t size);
|
||||
void *iAlloc(size_t size);
|
||||
|
||||
Block *TopBlock;
|
||||
Block *FreeBlocks;
|
||||
size_t BlockSize;
|
||||
public:
|
||||
bool shared;
|
||||
};
|
||||
|
||||
}
|
||||
416
src/common/filesystem/source/md5.hpp
Normal file
416
src/common/filesystem/source/md5.hpp
Normal file
|
|
@ -0,0 +1,416 @@
|
|||
#pragma once
|
||||
/*
|
||||
md5.hpp is a reformulation of the md5.h and md5.c code from
|
||||
http://www.opensource.apple.com/source/cups/cups-59/cups/md5.c to allow it to
|
||||
function as a component of a header only library. This conversion was done by
|
||||
Peter Thorson (webmaster@zaphoyd.com) in 2012 for the WebSocket++ project. The
|
||||
changes are released under the same license as the original (listed below)
|
||||
*/
|
||||
/*
|
||||
Copyright (C) 1999, 2002 Aladdin Enterprises. All rights reserved.
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
|
||||
L. Peter Deutsch
|
||||
ghost@aladdin.com
|
||||
|
||||
*/
|
||||
/* $Id: md5.h,v 1.4 2002/04/13 19:20:28 lpd Exp $ */
|
||||
/*
|
||||
Independent implementation of MD5 (RFC 1321).
|
||||
|
||||
This code implements the MD5 Algorithm defined in RFC 1321, whose
|
||||
text is available at
|
||||
http://www.ietf.org/rfc/rfc1321.txt
|
||||
The code is derived from the text of the RFC, including the test suite
|
||||
(section A.5) but excluding the rest of Appendix A. It does not include
|
||||
any code or documentation that is identified in the RFC as being
|
||||
copyrighted.
|
||||
|
||||
The original and principal author of md5.h is L. Peter Deutsch
|
||||
<ghost@aladdin.com>. Other authors are noted in the change history
|
||||
that follows (in reverse chronological order):
|
||||
|
||||
2002-04-13 lpd Removed support for non-ANSI compilers; removed
|
||||
references to Ghostscript; clarified derivation from RFC 1321;
|
||||
now handles byte order either statically or dynamically.
|
||||
1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
|
||||
1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5);
|
||||
added conditionalization for C++ compilation from Martin
|
||||
Purschke <purschke@bnl.gov>.
|
||||
1999-05-03 lpd Original version.
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* This package supports both compile-time and run-time determination of CPU
|
||||
* byte order. If ARCH_IS_BIG_ENDIAN is defined as 0, the code will be
|
||||
* compiled to run only on little-endian CPUs; if ARCH_IS_BIG_ENDIAN is
|
||||
* defined as non-zero, the code will be compiled to run only on big-endian
|
||||
* CPUs; if ARCH_IS_BIG_ENDIAN is not defined, the code will be compiled to
|
||||
* run on either big- or little-endian CPUs, but will run slightly less
|
||||
* efficiently on either one than if ARCH_IS_BIG_ENDIAN is defined.
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <cstring>
|
||||
|
||||
namespace FileSys {
|
||||
/// Provides MD5 hashing functionality
|
||||
namespace md5 {
|
||||
|
||||
typedef unsigned char md5_byte_t; /* 8-bit byte */
|
||||
typedef unsigned int md5_word_t; /* 32-bit word */
|
||||
|
||||
/* Define the state of the MD5 Algorithm. */
|
||||
typedef struct md5_state_s {
|
||||
md5_word_t count[2]; /* message length in bits, lsw first */
|
||||
md5_word_t abcd[4]; /* digest buffer */
|
||||
md5_byte_t buf[64]; /* accumulate block */
|
||||
} md5_state_t;
|
||||
|
||||
/* Initialize the algorithm. */
|
||||
inline void md5_init(md5_state_t* pms);
|
||||
|
||||
/* Append a string to the message. */
|
||||
inline void md5_append(md5_state_t* pms, md5_byte_t const* data, size_t nbytes);
|
||||
|
||||
/* Finish the message and return the digest. */
|
||||
inline void md5_finish(md5_state_t* pms, md5_byte_t digest[16]);
|
||||
|
||||
#undef ZSW_MD5_BYTE_ORDER /* 1 = big-endian, -1 = little-endian, 0 = unknown */
|
||||
#ifdef ARCH_IS_BIG_ENDIAN
|
||||
# define ZSW_MD5_BYTE_ORDER (ARCH_IS_BIG_ENDIAN ? 1 : -1)
|
||||
#else
|
||||
# define ZSW_MD5_BYTE_ORDER 0
|
||||
#endif
|
||||
|
||||
#define ZSW_MD5_T_MASK ((md5_word_t)~0)
|
||||
#define ZSW_MD5_T1 /* 0xd76aa478 */ (ZSW_MD5_T_MASK ^ 0x28955b87)
|
||||
#define ZSW_MD5_T2 /* 0xe8c7b756 */ (ZSW_MD5_T_MASK ^ 0x173848a9)
|
||||
#define ZSW_MD5_T3 0x242070db
|
||||
#define ZSW_MD5_T4 /* 0xc1bdceee */ (ZSW_MD5_T_MASK ^ 0x3e423111)
|
||||
#define ZSW_MD5_T5 /* 0xf57c0faf */ (ZSW_MD5_T_MASK ^ 0x0a83f050)
|
||||
#define ZSW_MD5_T6 0x4787c62a
|
||||
#define ZSW_MD5_T7 /* 0xa8304613 */ (ZSW_MD5_T_MASK ^ 0x57cfb9ec)
|
||||
#define ZSW_MD5_T8 /* 0xfd469501 */ (ZSW_MD5_T_MASK ^ 0x02b96afe)
|
||||
#define ZSW_MD5_T9 0x698098d8
|
||||
#define ZSW_MD5_T10 /* 0x8b44f7af */ (ZSW_MD5_T_MASK ^ 0x74bb0850)
|
||||
#define ZSW_MD5_T11 /* 0xffff5bb1 */ (ZSW_MD5_T_MASK ^ 0x0000a44e)
|
||||
#define ZSW_MD5_T12 /* 0x895cd7be */ (ZSW_MD5_T_MASK ^ 0x76a32841)
|
||||
#define ZSW_MD5_T13 0x6b901122
|
||||
#define ZSW_MD5_T14 /* 0xfd987193 */ (ZSW_MD5_T_MASK ^ 0x02678e6c)
|
||||
#define ZSW_MD5_T15 /* 0xa679438e */ (ZSW_MD5_T_MASK ^ 0x5986bc71)
|
||||
#define ZSW_MD5_T16 0x49b40821
|
||||
#define ZSW_MD5_T17 /* 0xf61e2562 */ (ZSW_MD5_T_MASK ^ 0x09e1da9d)
|
||||
#define ZSW_MD5_T18 /* 0xc040b340 */ (ZSW_MD5_T_MASK ^ 0x3fbf4cbf)
|
||||
#define ZSW_MD5_T19 0x265e5a51
|
||||
#define ZSW_MD5_T20 /* 0xe9b6c7aa */ (ZSW_MD5_T_MASK ^ 0x16493855)
|
||||
#define ZSW_MD5_T21 /* 0xd62f105d */ (ZSW_MD5_T_MASK ^ 0x29d0efa2)
|
||||
#define ZSW_MD5_T22 0x02441453
|
||||
#define ZSW_MD5_T23 /* 0xd8a1e681 */ (ZSW_MD5_T_MASK ^ 0x275e197e)
|
||||
#define ZSW_MD5_T24 /* 0xe7d3fbc8 */ (ZSW_MD5_T_MASK ^ 0x182c0437)
|
||||
#define ZSW_MD5_T25 0x21e1cde6
|
||||
#define ZSW_MD5_T26 /* 0xc33707d6 */ (ZSW_MD5_T_MASK ^ 0x3cc8f829)
|
||||
#define ZSW_MD5_T27 /* 0xf4d50d87 */ (ZSW_MD5_T_MASK ^ 0x0b2af278)
|
||||
#define ZSW_MD5_T28 0x455a14ed
|
||||
#define ZSW_MD5_T29 /* 0xa9e3e905 */ (ZSW_MD5_T_MASK ^ 0x561c16fa)
|
||||
#define ZSW_MD5_T30 /* 0xfcefa3f8 */ (ZSW_MD5_T_MASK ^ 0x03105c07)
|
||||
#define ZSW_MD5_T31 0x676f02d9
|
||||
#define ZSW_MD5_T32 /* 0x8d2a4c8a */ (ZSW_MD5_T_MASK ^ 0x72d5b375)
|
||||
#define ZSW_MD5_T33 /* 0xfffa3942 */ (ZSW_MD5_T_MASK ^ 0x0005c6bd)
|
||||
#define ZSW_MD5_T34 /* 0x8771f681 */ (ZSW_MD5_T_MASK ^ 0x788e097e)
|
||||
#define ZSW_MD5_T35 0x6d9d6122
|
||||
#define ZSW_MD5_T36 /* 0xfde5380c */ (ZSW_MD5_T_MASK ^ 0x021ac7f3)
|
||||
#define ZSW_MD5_T37 /* 0xa4beea44 */ (ZSW_MD5_T_MASK ^ 0x5b4115bb)
|
||||
#define ZSW_MD5_T38 0x4bdecfa9
|
||||
#define ZSW_MD5_T39 /* 0xf6bb4b60 */ (ZSW_MD5_T_MASK ^ 0x0944b49f)
|
||||
#define ZSW_MD5_T40 /* 0xbebfbc70 */ (ZSW_MD5_T_MASK ^ 0x4140438f)
|
||||
#define ZSW_MD5_T41 0x289b7ec6
|
||||
#define ZSW_MD5_T42 /* 0xeaa127fa */ (ZSW_MD5_T_MASK ^ 0x155ed805)
|
||||
#define ZSW_MD5_T43 /* 0xd4ef3085 */ (ZSW_MD5_T_MASK ^ 0x2b10cf7a)
|
||||
#define ZSW_MD5_T44 0x04881d05
|
||||
#define ZSW_MD5_T45 /* 0xd9d4d039 */ (ZSW_MD5_T_MASK ^ 0x262b2fc6)
|
||||
#define ZSW_MD5_T46 /* 0xe6db99e5 */ (ZSW_MD5_T_MASK ^ 0x1924661a)
|
||||
#define ZSW_MD5_T47 0x1fa27cf8
|
||||
#define ZSW_MD5_T48 /* 0xc4ac5665 */ (ZSW_MD5_T_MASK ^ 0x3b53a99a)
|
||||
#define ZSW_MD5_T49 /* 0xf4292244 */ (ZSW_MD5_T_MASK ^ 0x0bd6ddbb)
|
||||
#define ZSW_MD5_T50 0x432aff97
|
||||
#define ZSW_MD5_T51 /* 0xab9423a7 */ (ZSW_MD5_T_MASK ^ 0x546bdc58)
|
||||
#define ZSW_MD5_T52 /* 0xfc93a039 */ (ZSW_MD5_T_MASK ^ 0x036c5fc6)
|
||||
#define ZSW_MD5_T53 0x655b59c3
|
||||
#define ZSW_MD5_T54 /* 0x8f0ccc92 */ (ZSW_MD5_T_MASK ^ 0x70f3336d)
|
||||
#define ZSW_MD5_T55 /* 0xffeff47d */ (ZSW_MD5_T_MASK ^ 0x00100b82)
|
||||
#define ZSW_MD5_T56 /* 0x85845dd1 */ (ZSW_MD5_T_MASK ^ 0x7a7ba22e)
|
||||
#define ZSW_MD5_T57 0x6fa87e4f
|
||||
#define ZSW_MD5_T58 /* 0xfe2ce6e0 */ (ZSW_MD5_T_MASK ^ 0x01d3191f)
|
||||
#define ZSW_MD5_T59 /* 0xa3014314 */ (ZSW_MD5_T_MASK ^ 0x5cfebceb)
|
||||
#define ZSW_MD5_T60 0x4e0811a1
|
||||
#define ZSW_MD5_T61 /* 0xf7537e82 */ (ZSW_MD5_T_MASK ^ 0x08ac817d)
|
||||
#define ZSW_MD5_T62 /* 0xbd3af235 */ (ZSW_MD5_T_MASK ^ 0x42c50dca)
|
||||
#define ZSW_MD5_T63 0x2ad7d2bb
|
||||
#define ZSW_MD5_T64 /* 0xeb86d391 */ (ZSW_MD5_T_MASK ^ 0x14792c6e)
|
||||
|
||||
static void md5_process(md5_state_t* pms, md5_byte_t const* data /*[64]*/) {
|
||||
md5_word_t
|
||||
a = pms->abcd[0], b = pms->abcd[1],
|
||||
c = pms->abcd[2], d = pms->abcd[3];
|
||||
md5_word_t t;
|
||||
#if ZSW_MD5_BYTE_ORDER > 0
|
||||
/* Define storage only for big-endian CPUs. */
|
||||
md5_word_t X[16];
|
||||
#else
|
||||
/* Define storage for little-endian or both types of CPUs. */
|
||||
md5_word_t xbuf[16];
|
||||
md5_word_t const* X;
|
||||
#endif
|
||||
|
||||
{
|
||||
#if ZSW_MD5_BYTE_ORDER == 0
|
||||
/*
|
||||
* Determine dynamically whether this is a big-endian or
|
||||
* little-endian machine, since we can use a more efficient
|
||||
* algorithm on the latter.
|
||||
*/
|
||||
static int const w = 1;
|
||||
|
||||
if (*((md5_byte_t const*)&w)) /* dynamic little-endian */
|
||||
#endif
|
||||
#if ZSW_MD5_BYTE_ORDER <= 0 /* little-endian */
|
||||
{
|
||||
/*
|
||||
* On little-endian machines, we can process properly aligned
|
||||
* data without copying it.
|
||||
*/
|
||||
if (!((data - (md5_byte_t const*)0) & 3)) {
|
||||
/* data are properly aligned */
|
||||
X = (md5_word_t const*)data;
|
||||
}
|
||||
else {
|
||||
/* not aligned */
|
||||
std::memcpy(xbuf, data, 64);
|
||||
X = xbuf;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#if ZSW_MD5_BYTE_ORDER == 0
|
||||
else /* dynamic big-endian */
|
||||
#endif
|
||||
#if ZSW_MD5_BYTE_ORDER >= 0 /* big-endian */
|
||||
{
|
||||
/*
|
||||
* On big-endian machines, we must arrange the bytes in the
|
||||
* right order.
|
||||
*/
|
||||
const md5_byte_t* xp = data;
|
||||
int i;
|
||||
|
||||
# if ZSW_MD5_BYTE_ORDER == 0
|
||||
X = xbuf; /* (dynamic only) */
|
||||
# else
|
||||
# define xbuf X /* (static only) */
|
||||
# endif
|
||||
for (i = 0; i < 16; ++i, xp += 4)
|
||||
xbuf[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#define ZSW_MD5_ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
|
||||
|
||||
/* Round 1. */
|
||||
/* Let [abcd k s i] denote the operation
|
||||
a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
|
||||
#define ZSW_MD5_F(x, y, z) (((x) & (y)) | (~(x) & (z)))
|
||||
#define SET(a, b, c, d, k, s, Ti)\
|
||||
t = a + ZSW_MD5_F(b,c,d) + X[k] + Ti;\
|
||||
a = ZSW_MD5_ROTATE_LEFT(t, s) + b
|
||||
/* Do the following 16 operations. */
|
||||
SET(a, b, c, d, 0, 7, ZSW_MD5_T1);
|
||||
SET(d, a, b, c, 1, 12, ZSW_MD5_T2);
|
||||
SET(c, d, a, b, 2, 17, ZSW_MD5_T3);
|
||||
SET(b, c, d, a, 3, 22, ZSW_MD5_T4);
|
||||
SET(a, b, c, d, 4, 7, ZSW_MD5_T5);
|
||||
SET(d, a, b, c, 5, 12, ZSW_MD5_T6);
|
||||
SET(c, d, a, b, 6, 17, ZSW_MD5_T7);
|
||||
SET(b, c, d, a, 7, 22, ZSW_MD5_T8);
|
||||
SET(a, b, c, d, 8, 7, ZSW_MD5_T9);
|
||||
SET(d, a, b, c, 9, 12, ZSW_MD5_T10);
|
||||
SET(c, d, a, b, 10, 17, ZSW_MD5_T11);
|
||||
SET(b, c, d, a, 11, 22, ZSW_MD5_T12);
|
||||
SET(a, b, c, d, 12, 7, ZSW_MD5_T13);
|
||||
SET(d, a, b, c, 13, 12, ZSW_MD5_T14);
|
||||
SET(c, d, a, b, 14, 17, ZSW_MD5_T15);
|
||||
SET(b, c, d, a, 15, 22, ZSW_MD5_T16);
|
||||
#undef SET
|
||||
|
||||
/* Round 2. */
|
||||
/* Let [abcd k s i] denote the operation
|
||||
a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */
|
||||
#define ZSW_MD5_G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
|
||||
#define SET(a, b, c, d, k, s, Ti)\
|
||||
t = a + ZSW_MD5_G(b,c,d) + X[k] + Ti;\
|
||||
a = ZSW_MD5_ROTATE_LEFT(t, s) + b
|
||||
/* Do the following 16 operations. */
|
||||
SET(a, b, c, d, 1, 5, ZSW_MD5_T17);
|
||||
SET(d, a, b, c, 6, 9, ZSW_MD5_T18);
|
||||
SET(c, d, a, b, 11, 14, ZSW_MD5_T19);
|
||||
SET(b, c, d, a, 0, 20, ZSW_MD5_T20);
|
||||
SET(a, b, c, d, 5, 5, ZSW_MD5_T21);
|
||||
SET(d, a, b, c, 10, 9, ZSW_MD5_T22);
|
||||
SET(c, d, a, b, 15, 14, ZSW_MD5_T23);
|
||||
SET(b, c, d, a, 4, 20, ZSW_MD5_T24);
|
||||
SET(a, b, c, d, 9, 5, ZSW_MD5_T25);
|
||||
SET(d, a, b, c, 14, 9, ZSW_MD5_T26);
|
||||
SET(c, d, a, b, 3, 14, ZSW_MD5_T27);
|
||||
SET(b, c, d, a, 8, 20, ZSW_MD5_T28);
|
||||
SET(a, b, c, d, 13, 5, ZSW_MD5_T29);
|
||||
SET(d, a, b, c, 2, 9, ZSW_MD5_T30);
|
||||
SET(c, d, a, b, 7, 14, ZSW_MD5_T31);
|
||||
SET(b, c, d, a, 12, 20, ZSW_MD5_T32);
|
||||
#undef SET
|
||||
|
||||
/* Round 3. */
|
||||
/* Let [abcd k s t] denote the operation
|
||||
a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */
|
||||
#define ZSW_MD5_H(x, y, z) ((x) ^ (y) ^ (z))
|
||||
#define SET(a, b, c, d, k, s, Ti)\
|
||||
t = a + ZSW_MD5_H(b,c,d) + X[k] + Ti;\
|
||||
a = ZSW_MD5_ROTATE_LEFT(t, s) + b
|
||||
/* Do the following 16 operations. */
|
||||
SET(a, b, c, d, 5, 4, ZSW_MD5_T33);
|
||||
SET(d, a, b, c, 8, 11, ZSW_MD5_T34);
|
||||
SET(c, d, a, b, 11, 16, ZSW_MD5_T35);
|
||||
SET(b, c, d, a, 14, 23, ZSW_MD5_T36);
|
||||
SET(a, b, c, d, 1, 4, ZSW_MD5_T37);
|
||||
SET(d, a, b, c, 4, 11, ZSW_MD5_T38);
|
||||
SET(c, d, a, b, 7, 16, ZSW_MD5_T39);
|
||||
SET(b, c, d, a, 10, 23, ZSW_MD5_T40);
|
||||
SET(a, b, c, d, 13, 4, ZSW_MD5_T41);
|
||||
SET(d, a, b, c, 0, 11, ZSW_MD5_T42);
|
||||
SET(c, d, a, b, 3, 16, ZSW_MD5_T43);
|
||||
SET(b, c, d, a, 6, 23, ZSW_MD5_T44);
|
||||
SET(a, b, c, d, 9, 4, ZSW_MD5_T45);
|
||||
SET(d, a, b, c, 12, 11, ZSW_MD5_T46);
|
||||
SET(c, d, a, b, 15, 16, ZSW_MD5_T47);
|
||||
SET(b, c, d, a, 2, 23, ZSW_MD5_T48);
|
||||
#undef SET
|
||||
|
||||
/* Round 4. */
|
||||
/* Let [abcd k s t] denote the operation
|
||||
a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */
|
||||
#define ZSW_MD5_I(x, y, z) ((y) ^ ((x) | ~(z)))
|
||||
#define SET(a, b, c, d, k, s, Ti)\
|
||||
t = a + ZSW_MD5_I(b,c,d) + X[k] + Ti;\
|
||||
a = ZSW_MD5_ROTATE_LEFT(t, s) + b
|
||||
/* Do the following 16 operations. */
|
||||
SET(a, b, c, d, 0, 6, ZSW_MD5_T49);
|
||||
SET(d, a, b, c, 7, 10, ZSW_MD5_T50);
|
||||
SET(c, d, a, b, 14, 15, ZSW_MD5_T51);
|
||||
SET(b, c, d, a, 5, 21, ZSW_MD5_T52);
|
||||
SET(a, b, c, d, 12, 6, ZSW_MD5_T53);
|
||||
SET(d, a, b, c, 3, 10, ZSW_MD5_T54);
|
||||
SET(c, d, a, b, 10, 15, ZSW_MD5_T55);
|
||||
SET(b, c, d, a, 1, 21, ZSW_MD5_T56);
|
||||
SET(a, b, c, d, 8, 6, ZSW_MD5_T57);
|
||||
SET(d, a, b, c, 15, 10, ZSW_MD5_T58);
|
||||
SET(c, d, a, b, 6, 15, ZSW_MD5_T59);
|
||||
SET(b, c, d, a, 13, 21, ZSW_MD5_T60);
|
||||
SET(a, b, c, d, 4, 6, ZSW_MD5_T61);
|
||||
SET(d, a, b, c, 11, 10, ZSW_MD5_T62);
|
||||
SET(c, d, a, b, 2, 15, ZSW_MD5_T63);
|
||||
SET(b, c, d, a, 9, 21, ZSW_MD5_T64);
|
||||
#undef SET
|
||||
|
||||
/* Then perform the following additions. (That is increment each
|
||||
of the four registers by the value it had before this block
|
||||
was started.) */
|
||||
pms->abcd[0] += a;
|
||||
pms->abcd[1] += b;
|
||||
pms->abcd[2] += c;
|
||||
pms->abcd[3] += d;
|
||||
}
|
||||
|
||||
void md5_init(md5_state_t* pms) {
|
||||
pms->count[0] = pms->count[1] = 0;
|
||||
pms->abcd[0] = 0x67452301;
|
||||
pms->abcd[1] = /*0xefcdab89*/ ZSW_MD5_T_MASK ^ 0x10325476;
|
||||
pms->abcd[2] = /*0x98badcfe*/ ZSW_MD5_T_MASK ^ 0x67452301;
|
||||
pms->abcd[3] = 0x10325476;
|
||||
}
|
||||
|
||||
void md5_append(md5_state_t* pms, md5_byte_t const* data, size_t nbytes) {
|
||||
md5_byte_t const* p = data;
|
||||
size_t left = nbytes;
|
||||
int offset = (pms->count[0] >> 3) & 63;
|
||||
md5_word_t nbits = (md5_word_t)(nbytes << 3);
|
||||
|
||||
if (nbytes <= 0)
|
||||
return;
|
||||
|
||||
/* Update the message length. */
|
||||
pms->count[1] += (md5_word_t)(nbytes >> 29);
|
||||
pms->count[0] += nbits;
|
||||
if (pms->count[0] < nbits)
|
||||
pms->count[1]++;
|
||||
|
||||
/* Process an initial partial block. */
|
||||
if (offset) {
|
||||
int copy = (offset + nbytes > 64 ? 64 - offset : static_cast<int>(nbytes));
|
||||
|
||||
std::memcpy(pms->buf + offset, p, copy);
|
||||
if (offset + copy < 64)
|
||||
return;
|
||||
p += copy;
|
||||
left -= copy;
|
||||
md5_process(pms, pms->buf);
|
||||
}
|
||||
|
||||
/* Process full blocks. */
|
||||
for (; left >= 64; p += 64, left -= 64)
|
||||
md5_process(pms, p);
|
||||
|
||||
/* Process a final partial block. */
|
||||
if (left)
|
||||
std::memcpy(pms->buf, p, left);
|
||||
}
|
||||
|
||||
void md5_finish(md5_state_t* pms, md5_byte_t digest[16]) {
|
||||
static md5_byte_t const pad[64] = {
|
||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
md5_byte_t data[8];
|
||||
int i;
|
||||
|
||||
/* Save the length before padding. */
|
||||
for (i = 0; i < 8; ++i)
|
||||
data[i] = (md5_byte_t)(pms->count[i >> 2] >> ((i & 3) << 3));
|
||||
/* Pad to 56 bytes mod 64. */
|
||||
md5_append(pms, pad, ((55 - (pms->count[0] >> 3)) & 63) + 1);
|
||||
/* Append the length. */
|
||||
md5_append(pms, data, 8);
|
||||
for (i = 0; i < 16; ++i)
|
||||
digest[i] = (md5_byte_t)(pms->abcd[i >> 2] >> ((i & 3) << 3));
|
||||
}
|
||||
|
||||
|
||||
} // md5
|
||||
} // fs_private
|
||||
|
||||
750
src/common/filesystem/source/resourcefile.cpp
Normal file
750
src/common/filesystem/source/resourcefile.cpp
Normal file
|
|
@ -0,0 +1,750 @@
|
|||
/*
|
||||
** resourcefile.cpp
|
||||
**
|
||||
** Base classes for resource file management
|
||||
**
|
||||
**---------------------------------------------------------------------------
|
||||
** Copyright 2009 Christoph Oelckers
|
||||
** All rights reserved.
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions
|
||||
** are met:
|
||||
**
|
||||
** 1. Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** 2. Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in the
|
||||
** documentation and/or other materials provided with the distribution.
|
||||
** 3. The name of the author may not be used to endorse or promote products
|
||||
** derived from this software without specific prior written permission.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
**---------------------------------------------------------------------------
|
||||
**
|
||||
**
|
||||
*/
|
||||
|
||||
#include <zlib.h>
|
||||
#include "resourcefile.h"
|
||||
#include "md5.hpp"
|
||||
#include "fs_stringpool.h"
|
||||
|
||||
namespace FileSys {
|
||||
|
||||
std::string ExtractBaseName(const char* path, bool include_extension)
|
||||
{
|
||||
const char* src, * dot;
|
||||
|
||||
src = path + strlen(path) - 1;
|
||||
|
||||
if (src >= path)
|
||||
{
|
||||
// back up until a / or the start
|
||||
while (src != path && src[-1] != '/' && src[-1] != '\\') // check both on all systems for consistent behavior with archives.
|
||||
src--;
|
||||
|
||||
if (!include_extension && (dot = strrchr(src, '.')))
|
||||
{
|
||||
return std::string(src, dot - src);
|
||||
}
|
||||
else
|
||||
{
|
||||
return std::string(src);
|
||||
}
|
||||
}
|
||||
return std::string();
|
||||
}
|
||||
|
||||
void strReplace(std::string& str, const char *from, const char* to)
|
||||
{
|
||||
if (*from == 0)
|
||||
return;
|
||||
size_t start_pos = 0;
|
||||
while ((start_pos = str.find(from, start_pos)) != std::string::npos)
|
||||
{
|
||||
str.replace(start_pos, strlen(from), to);
|
||||
start_pos += strlen(to);
|
||||
}
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// File reader that reads from a lump's cache
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
class FLumpReader : public MemoryReader
|
||||
{
|
||||
FResourceLump *source;
|
||||
|
||||
public:
|
||||
FLumpReader(FResourceLump *src)
|
||||
: MemoryReader(NULL, src->LumpSize), source(src)
|
||||
{
|
||||
src->Lock();
|
||||
bufptr = src->Cache;
|
||||
}
|
||||
|
||||
~FLumpReader()
|
||||
{
|
||||
source->Unlock();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Base class for resource lumps
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FResourceLump::~FResourceLump()
|
||||
{
|
||||
if (Cache != NULL && RefCount >= 0)
|
||||
{
|
||||
delete [] Cache;
|
||||
Cache = NULL;
|
||||
}
|
||||
Owner = NULL;
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Sets up the lump name information for anything not coming from a WAD file.
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
void FResourceLump::LumpNameSetup(const char *iname, StringPool* allocator)
|
||||
{
|
||||
// this causes interference with real Dehacked lumps.
|
||||
if (!stricmp(iname, "dehacked.exe"))
|
||||
{
|
||||
iname = "";
|
||||
}
|
||||
else if (allocator)
|
||||
{
|
||||
iname = allocator->Strdup(iname);
|
||||
}
|
||||
|
||||
FullName = iname;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Checks for embedded resource files
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
static bool IsWadInFolder(const FResourceFile* const archive, const char* const resPath)
|
||||
{
|
||||
// Checks a special case when <somefile.wad> was put in
|
||||
// <myproject> directory inside <myproject.zip>
|
||||
|
||||
if (NULL == archive)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto dirName = ExtractBaseName(archive->FileName);
|
||||
const auto fileName = ExtractBaseName(resPath, true);
|
||||
const std::string filePath = dirName + '/' + fileName;
|
||||
|
||||
return 0 == stricmp(filePath.c_str(), resPath);
|
||||
}
|
||||
|
||||
void FResourceLump::CheckEmbedded(LumpFilterInfo* lfi)
|
||||
{
|
||||
// Checks for embedded archives
|
||||
const char *c = strstr(FullName, ".wad");
|
||||
if (c && strlen(c) == 4 && (!strchr(FullName, '/') || IsWadInFolder(Owner, FullName)))
|
||||
{
|
||||
Flags |= LUMPF_EMBEDDED;
|
||||
}
|
||||
else if (lfi) for (auto& fstr : lfi->embeddings)
|
||||
{
|
||||
if (!stricmp(FullName, fstr.c_str()))
|
||||
{
|
||||
Flags |= LUMPF_EMBEDDED;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// this is just for completeness. For non-Zips only an uncompressed lump can
|
||||
// be returned.
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FCompressedBuffer FResourceLump::GetRawData()
|
||||
{
|
||||
FCompressedBuffer cbuf = { (unsigned)LumpSize, (unsigned)LumpSize, METHOD_STORED, 0, 0, new char[LumpSize] };
|
||||
memcpy(cbuf.mBuffer, Lock(), LumpSize);
|
||||
Unlock();
|
||||
cbuf.mCRC32 = crc32(0, (uint8_t*)cbuf.mBuffer, LumpSize);
|
||||
return cbuf;
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Returns the owner's FileReader if it can be used to access this lump
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FileReader *FResourceLump::GetReader()
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Returns a file reader to the lump's cache
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FileReader FResourceLump::NewReader()
|
||||
{
|
||||
return FileReader(new FLumpReader(this));
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Caches a lump's content and increases the reference counter
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
void *FResourceLump::Lock()
|
||||
{
|
||||
if (Cache != NULL)
|
||||
{
|
||||
if (RefCount > 0) RefCount++;
|
||||
}
|
||||
else if (LumpSize > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
FillCache();
|
||||
}
|
||||
catch (const FileSystemException& err)
|
||||
{
|
||||
// enrich the message with info about this lump.
|
||||
throw FileSystemException("%s, file '%s': %s", getName(), Owner->FileName, err.what());
|
||||
}
|
||||
}
|
||||
return Cache;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Decrements reference counter and frees lump if counter reaches 0
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
int FResourceLump::Unlock()
|
||||
{
|
||||
if (LumpSize > 0 && RefCount > 0)
|
||||
{
|
||||
if (--RefCount == 0)
|
||||
{
|
||||
delete [] Cache;
|
||||
Cache = NULL;
|
||||
}
|
||||
}
|
||||
return RefCount;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Opens a resource file
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
typedef FResourceFile * (*CheckFunc)(const char *filename, FileReader &file, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp);
|
||||
|
||||
FResourceFile *CheckWad(const char *filename, FileReader &file, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp);
|
||||
FResourceFile *CheckGRP(const char *filename, FileReader &file, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp);
|
||||
FResourceFile *CheckRFF(const char *filename, FileReader &file, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp);
|
||||
FResourceFile *CheckPak(const char *filename, FileReader &file, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp);
|
||||
FResourceFile *CheckZip(const char *filename, FileReader &file, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp);
|
||||
FResourceFile *Check7Z(const char *filename, FileReader &file, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp);
|
||||
FResourceFile* CheckSSI(const char* filename, FileReader& file, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp);
|
||||
FResourceFile* CheckWHRes(const char* filename, FileReader& file, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp);
|
||||
FResourceFile *CheckLump(const char *filename,FileReader &file, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp);
|
||||
FResourceFile *CheckDir(const char *filename, bool nosub, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp);
|
||||
|
||||
static CheckFunc funcs[] = { CheckWad, CheckZip, Check7Z, CheckPak, CheckGRP, CheckRFF, CheckSSI, CheckWHRes, CheckLump };
|
||||
|
||||
static int nulPrintf(FSMessageLevel msg, const char* fmt, ...)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
FResourceFile *FResourceFile::DoOpenResourceFile(const char *filename, FileReader &file, bool containeronly, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp)
|
||||
{
|
||||
if (Printf == nullptr) Printf = nulPrintf;
|
||||
for(auto func : funcs)
|
||||
{
|
||||
if (containeronly && func == CheckLump) break;
|
||||
FResourceFile *resfile = func(filename, file, filter, Printf, sp);
|
||||
if (resfile != NULL) return resfile;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
FResourceFile *FResourceFile::OpenResourceFile(const char *filename, FileReader &file, bool containeronly, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp)
|
||||
{
|
||||
return DoOpenResourceFile(filename, file, containeronly, filter, Printf, sp);
|
||||
}
|
||||
|
||||
|
||||
FResourceFile *FResourceFile::OpenResourceFile(const char *filename, bool containeronly, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp)
|
||||
{
|
||||
FileReader file;
|
||||
if (!file.OpenFile(filename)) return nullptr;
|
||||
return DoOpenResourceFile(filename, file, containeronly, filter, Printf, sp);
|
||||
}
|
||||
|
||||
FResourceFile *FResourceFile::OpenDirectory(const char *filename, LumpFilterInfo* filter, FileSystemMessageFunc Printf, StringPool* sp)
|
||||
{
|
||||
if (Printf == nullptr) Printf = nulPrintf;
|
||||
return CheckDir(filename, false, filter, Printf, sp);
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Resource file base class
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FResourceFile::FResourceFile(const char *filename, StringPool* sp)
|
||||
{
|
||||
stringpool = sp ? sp : new StringPool;
|
||||
FileName = stringpool->Strdup(filename);
|
||||
}
|
||||
|
||||
FResourceFile::FResourceFile(const char *filename, FileReader &r, StringPool* sp)
|
||||
: FResourceFile(filename,sp)
|
||||
{
|
||||
Reader = std::move(r);
|
||||
}
|
||||
|
||||
FResourceFile::~FResourceFile()
|
||||
{
|
||||
if (!stringpool->shared) delete stringpool;
|
||||
}
|
||||
|
||||
int lumpcmp(const void * a, const void * b)
|
||||
{
|
||||
FResourceLump * rec1 = (FResourceLump *)a;
|
||||
FResourceLump * rec2 = (FResourceLump *)b;
|
||||
return stricmp(rec1->getName(), rec2->getName());
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// FResourceFile :: GenerateHash
|
||||
//
|
||||
// Generates a hash identifier for use in file identification.
|
||||
// Potential uses are mod-wide compatibility settings or localization add-ons.
|
||||
// This only hashes the lump directory but not the actual content
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
void FResourceFile::GenerateHash()
|
||||
{
|
||||
// hash the lump directory after sorting
|
||||
using namespace FileSys::md5;
|
||||
|
||||
auto n = snprintf(Hash, 48, "%08X-%04X-", (unsigned)Reader.GetLength(), NumLumps);
|
||||
|
||||
md5_state_t state;
|
||||
md5_init(&state);
|
||||
|
||||
uint8_t digest[16];
|
||||
for(uint32_t i = 0; i < NumLumps; i++)
|
||||
{
|
||||
auto lump = GetLump(i);
|
||||
md5_append(&state, (const uint8_t*)lump->FullName, (unsigned)strlen(lump->FullName) + 1);
|
||||
md5_append(&state, (const uint8_t*)&lump->LumpSize, 4);
|
||||
}
|
||||
md5_finish(&state, digest);
|
||||
for (auto c : digest)
|
||||
{
|
||||
n += snprintf(Hash + n, 3, "%02X", c);
|
||||
}
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// FResourceFile :: PostProcessArchive
|
||||
//
|
||||
// Sorts files by name.
|
||||
// For files named "filter/<game>/*": Using the same filter rules as config
|
||||
// autoloading, move them to the end and rename them without the "filter/"
|
||||
// prefix. Filtered files that don't match are deleted.
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
void FResourceFile::PostProcessArchive(void *lumps, size_t lumpsize, LumpFilterInfo *filter)
|
||||
{
|
||||
// Entries in archives are sorted alphabetically
|
||||
qsort(lumps, NumLumps, lumpsize, lumpcmp);
|
||||
if (!filter) return;
|
||||
|
||||
// Filter out lumps using the same names as the Autoload.* sections
|
||||
// in the ini file use. We reduce the maximum lump concidered after
|
||||
// each one so that we don't risk refiltering already filtered lumps.
|
||||
uint32_t max = NumLumps;
|
||||
max -= FilterLumpsByGameType(filter, lumps, lumpsize, max);
|
||||
|
||||
ptrdiff_t len;
|
||||
ptrdiff_t lastpos = -1;
|
||||
std::string file;
|
||||
std::string& LumpFilter = filter->dotFilter;
|
||||
while ((len = LumpFilter.find_first_of('.', lastpos+1)) != LumpFilter.npos)
|
||||
{
|
||||
max -= FilterLumps(std::string(LumpFilter, 0, len), lumps, lumpsize, max);
|
||||
lastpos = len;
|
||||
}
|
||||
max -= FilterLumps(LumpFilter, lumps, lumpsize, max);
|
||||
|
||||
JunkLeftoverFilters(lumps, lumpsize, max);
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// FResourceFile :: FilterLumps
|
||||
//
|
||||
// Finds any lumps between [0,<max>) that match the pattern
|
||||
// "filter/<filtername>/*" and moves them to the end of the lump list.
|
||||
// Returns the number of lumps moved.
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
int FResourceFile::FilterLumps(const std::string& filtername, void *lumps, size_t lumpsize, uint32_t max)
|
||||
{
|
||||
uint32_t start, end;
|
||||
|
||||
if (filtername.empty())
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
std::string filter = "filter/" + filtername + '/';
|
||||
|
||||
bool found = FindPrefixRange(filter.c_str(), lumps, lumpsize, max, start, end);
|
||||
|
||||
// Workaround for old Doom filter names (todo: move out of here.)
|
||||
if (!found && filtername.find("doom.id.doom") == 0)
|
||||
{
|
||||
strReplace(filter, "doom.id.doom", "doom.doom");
|
||||
found = FindPrefixRange(filter.c_str(), lumps, lumpsize, max, start, end);
|
||||
}
|
||||
|
||||
if (found)
|
||||
{
|
||||
void *from = (uint8_t *)lumps + start * lumpsize;
|
||||
|
||||
// Remove filter prefix from every name
|
||||
void *lump_p = from;
|
||||
for (uint32_t i = start; i < end; ++i, lump_p = (uint8_t *)lump_p + lumpsize)
|
||||
{
|
||||
FResourceLump *lump = (FResourceLump *)lump_p;
|
||||
assert(strnicmp(lump->FullName, filter.c_str(), filter.length()) == 0);
|
||||
lump->LumpNameSetup(lump->FullName + filter.length(), nullptr);
|
||||
}
|
||||
|
||||
// Move filtered lumps to the end of the lump list.
|
||||
size_t count = (end - start) * lumpsize;
|
||||
void *to = (uint8_t *)lumps + NumLumps * lumpsize - count;
|
||||
assert (to >= from);
|
||||
|
||||
if (from != to)
|
||||
{
|
||||
// Copy filtered lumps to a temporary buffer.
|
||||
uint8_t *filteredlumps = new uint8_t[count];
|
||||
memcpy(filteredlumps, from, count);
|
||||
|
||||
// Shift lumps left to make room for the filtered ones at the end.
|
||||
memmove(from, (uint8_t *)from + count, (NumLumps - end) * lumpsize);
|
||||
|
||||
// Copy temporary buffer to newly freed space.
|
||||
memcpy(to, filteredlumps, count);
|
||||
|
||||
delete[] filteredlumps;
|
||||
}
|
||||
}
|
||||
return end - start;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// FResourceFile :: FilterLumpsByGameType
|
||||
//
|
||||
// Matches any lumps that match "filter/game-<gametype>/*". Includes
|
||||
// inclusive gametypes like Raven.
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
int FResourceFile::FilterLumpsByGameType(LumpFilterInfo *filter, void *lumps, size_t lumpsize, uint32_t max)
|
||||
{
|
||||
if (filter == nullptr)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
int count = 0;
|
||||
for (auto &fstring : filter->gameTypeFilter)
|
||||
{
|
||||
count += FilterLumps(fstring, lumps, lumpsize, max);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// FResourceFile :: JunkLeftoverFilters
|
||||
//
|
||||
// Deletes any lumps beginning with "filter/" that were not matched.
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
void FResourceFile::JunkLeftoverFilters(void *lumps, size_t lumpsize, uint32_t max)
|
||||
{
|
||||
uint32_t start, end;
|
||||
if (FindPrefixRange("filter/", lumps, lumpsize, max, start, end))
|
||||
{
|
||||
// Since the resource lumps may contain non-POD data besides the
|
||||
// full name, we "delete" them by erasing their names so they
|
||||
// can't be found.
|
||||
void *stop = (uint8_t *)lumps + end * lumpsize;
|
||||
for (void *p = (uint8_t *)lumps + start * lumpsize; p < stop; p = (uint8_t *)p + lumpsize)
|
||||
{
|
||||
FResourceLump *lump = (FResourceLump *)p;
|
||||
lump->clearName();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// FResourceFile :: FindPrefixRange
|
||||
//
|
||||
// Finds a range of lumps that start with the prefix string. <start> is left
|
||||
// indicating the first matching one. <end> is left at one plus the last
|
||||
// matching one.
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
bool FResourceFile::FindPrefixRange(const char* filter, void *lumps, size_t lumpsize, uint32_t maxlump, uint32_t &start, uint32_t &end)
|
||||
{
|
||||
uint32_t min, max, mid, inside;
|
||||
FResourceLump *lump;
|
||||
int cmp = 0;
|
||||
|
||||
end = start = 0;
|
||||
|
||||
// Pretend that our range starts at 1 instead of 0 so that we can avoid
|
||||
// unsigned overflow if the range starts at the first lump.
|
||||
lumps = (uint8_t *)lumps - lumpsize;
|
||||
|
||||
// Binary search to find any match at all.
|
||||
mid = min = 1, max = maxlump;
|
||||
while (min <= max)
|
||||
{
|
||||
mid = min + (max - min) / 2;
|
||||
lump = (FResourceLump *)((uint8_t *)lumps + mid * lumpsize);
|
||||
cmp = strnicmp(lump->FullName, filter, (int)strlen(filter));
|
||||
if (cmp == 0)
|
||||
break;
|
||||
else if (cmp < 0)
|
||||
min = mid + 1;
|
||||
else
|
||||
max = mid - 1;
|
||||
}
|
||||
if (max < min)
|
||||
{ // matched nothing
|
||||
return false;
|
||||
}
|
||||
|
||||
// Binary search to find first match.
|
||||
inside = mid;
|
||||
min = 1, max = mid;
|
||||
while (min <= max)
|
||||
{
|
||||
mid = min + (max - min) / 2;
|
||||
lump = (FResourceLump *)((uint8_t *)lumps + mid * lumpsize);
|
||||
cmp = strnicmp(lump->FullName, filter, (int)strlen(filter));
|
||||
// Go left on matches and right on misses.
|
||||
if (cmp == 0)
|
||||
max = mid - 1;
|
||||
else
|
||||
min = mid + 1;
|
||||
}
|
||||
start = mid + (cmp != 0) - 1;
|
||||
|
||||
// Binary search to find last match.
|
||||
min = inside, max = maxlump;
|
||||
while (min <= max)
|
||||
{
|
||||
mid = min + (max - min) / 2;
|
||||
lump = (FResourceLump *)((uint8_t *)lumps + mid * lumpsize);
|
||||
cmp = strnicmp(lump->FullName, filter, (int)strlen(filter));
|
||||
// Go right on matches and left on misses.
|
||||
if (cmp == 0)
|
||||
min = mid + 1;
|
||||
else
|
||||
max = mid - 1;
|
||||
}
|
||||
end = mid - (cmp != 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Finds a lump by a given name. Used for savegames
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FResourceLump *FResourceFile::FindLump(const char *name)
|
||||
{
|
||||
for (unsigned i = 0; i < NumLumps; i++)
|
||||
{
|
||||
FResourceLump *lump = GetLump(i);
|
||||
if (!stricmp(name, lump->FullName))
|
||||
{
|
||||
return lump;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Caches a lump's content and increases the reference counter
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FileReader *FUncompressedLump::GetReader()
|
||||
{
|
||||
Owner->Reader.Seek(Position, FileReader::SeekSet);
|
||||
return &Owner->Reader;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Caches a lump's content and increases the reference counter
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
int FUncompressedLump::FillCache()
|
||||
{
|
||||
const char * buffer = Owner->Reader.GetBuffer();
|
||||
|
||||
if (buffer != 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->Reader.Seek(Position, FileReader::SeekSet);
|
||||
Cache = new char[LumpSize];
|
||||
|
||||
auto read = Owner->Reader.Read(Cache, LumpSize);
|
||||
if (read != LumpSize)
|
||||
{
|
||||
throw FileSystemException("only read %d of %d bytes", (int)read, (int)LumpSize);
|
||||
}
|
||||
|
||||
RefCount = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Base class for uncompressed resource files
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FUncompressedFile::FUncompressedFile(const char *filename, StringPool* sp)
|
||||
: FResourceFile(filename, sp)
|
||||
{}
|
||||
|
||||
FUncompressedFile::FUncompressedFile(const char *filename, FileReader &r, StringPool* sp)
|
||||
: FResourceFile(filename, r, sp)
|
||||
{}
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// external lump
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
FExternalLump::FExternalLump(const char *_filename, int filesize, StringPool* stringpool)
|
||||
{
|
||||
FileName = stringpool->Strdup(_filename);
|
||||
|
||||
if (filesize == -1)
|
||||
{
|
||||
FileReader f;
|
||||
|
||||
if (f.OpenFile(_filename))
|
||||
{
|
||||
LumpSize = (int)f.GetLength();
|
||||
}
|
||||
else
|
||||
{
|
||||
LumpSize = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LumpSize = filesize;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Caches a lump's content and increases the reference counter
|
||||
// For external lumps this reopens the file each time it is accessed
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
int FExternalLump::FillCache()
|
||||
{
|
||||
Cache = new char[LumpSize];
|
||||
FileReader f;
|
||||
|
||||
if (f.OpenFile(FileName))
|
||||
{
|
||||
auto read = f.Read(Cache, LumpSize);
|
||||
if (read != LumpSize)
|
||||
{
|
||||
throw FileSystemException("only read %d of %d bytes", (int)read, (int)LumpSize);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw FileSystemException("unable to open file");
|
||||
}
|
||||
RefCount = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
111
src/common/filesystem/source/w_zip.h
Normal file
111
src/common/filesystem/source/w_zip.h
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
#ifndef __W_ZIP
|
||||
#define __W_ZIP
|
||||
|
||||
#if defined(__GNUC__)
|
||||
#define FORCE_PACKED __attribute__((__packed__))
|
||||
#else
|
||||
#define FORCE_PACKED
|
||||
#endif
|
||||
|
||||
namespace FileSys {
|
||||
|
||||
#pragma pack(1)
|
||||
// FZipCentralInfo
|
||||
struct FZipEndOfCentralDirectory
|
||||
{
|
||||
uint32_t Magic;
|
||||
uint16_t DiskNumber;
|
||||
uint16_t FirstDisk;
|
||||
uint16_t NumEntries;
|
||||
uint16_t NumEntriesOnAllDisks;
|
||||
uint32_t DirectorySize;
|
||||
uint32_t DirectoryOffset;
|
||||
uint16_t ZipCommentLength;
|
||||
} FORCE_PACKED;
|
||||
|
||||
struct FZipEndOfCentralDirectory64
|
||||
{
|
||||
uint32_t Magic;
|
||||
uint64_t StructSize;
|
||||
uint16_t VersionMadeBy;
|
||||
uint16_t VersionNeeded;
|
||||
uint32_t DiskNumber;
|
||||
uint32_t FirstDisk;
|
||||
uint64_t NumEntries;
|
||||
uint64_t NumEntriesOnAllDisks;
|
||||
uint64_t DirectorySize;
|
||||
uint64_t DirectoryOffset;
|
||||
uint16_t ZipCommentLength;
|
||||
} FORCE_PACKED;
|
||||
|
||||
|
||||
// FZipFileInfo
|
||||
struct FZipCentralDirectoryInfo
|
||||
{
|
||||
uint32_t Magic;
|
||||
uint8_t VersionMadeBy[2];
|
||||
uint8_t VersionToExtract[2];
|
||||
uint16_t Flags;
|
||||
uint16_t Method;
|
||||
uint16_t ModTime;
|
||||
uint16_t ModDate;
|
||||
uint32_t CRC32;
|
||||
uint32_t CompressedSize32;
|
||||
uint32_t UncompressedSize32;
|
||||
uint16_t NameLength;
|
||||
uint16_t ExtraLength;
|
||||
uint16_t CommentLength;
|
||||
uint16_t StartingDiskNumber;
|
||||
uint16_t InternalAttributes;
|
||||
uint32_t ExternalAttributes;
|
||||
uint32_t LocalHeaderOffset32;
|
||||
// file name and other variable length info follows
|
||||
} FORCE_PACKED;
|
||||
|
||||
struct FZipCentralDirectoryInfo64BitExt
|
||||
{
|
||||
uint16_t Type;
|
||||
uint16_t Length;
|
||||
uint64_t UncompressedSize;
|
||||
uint64_t CompressedSize;
|
||||
uint64_t LocalHeaderOffset;
|
||||
uint32_t DiskNo;
|
||||
} FORCE_PACKED;
|
||||
|
||||
// FZipLocalHeader
|
||||
struct FZipLocalFileHeader
|
||||
{
|
||||
uint32_t Magic;
|
||||
uint8_t VersionToExtract[2];
|
||||
uint16_t Flags;
|
||||
uint16_t Method;
|
||||
uint16_t ModTime;
|
||||
uint16_t ModDate;
|
||||
uint32_t CRC32;
|
||||
uint32_t CompressedSize;
|
||||
uint32_t UncompressedSize;
|
||||
uint16_t NameLength;
|
||||
uint16_t ExtraLength;
|
||||
// file name and other variable length info follows
|
||||
} FORCE_PACKED;
|
||||
|
||||
#pragma pack()
|
||||
|
||||
#ifndef MAKE_ID
|
||||
#ifndef __BIG_ENDIAN__
|
||||
#define MAKE_ID(a,b,c,d) ((uint32_t)((a)|((b)<<8)|((c)<<16)|((d)<<24)))
|
||||
#else
|
||||
#define MAKE_ID(a,b,c,d) ((uint32_t)((d)|((c)<<8)|((b)<<16)|((a)<<24)))
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
#define ZIP_LOCALFILE MAKE_ID('P','K',3,4)
|
||||
#define ZIP_CENTRALFILE MAKE_ID('P','K',1,2)
|
||||
#define ZIP_ENDOFDIR MAKE_ID('P','K',5,6)
|
||||
|
||||
// File header flags.
|
||||
#define ZF_ENCRYPTED 0x1
|
||||
|
||||
}
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue