mirror of
https://github.com/ZDoom/gzdoom.git
synced 2024-11-15 08:52:02 +00:00
moved writezip.cpp out of filesystem and eliminated uses of TArray.
This commit is contained in:
parent
96961a5663
commit
bae3adc89f
12 changed files with 272 additions and 232 deletions
|
@ -1076,6 +1076,7 @@ set (PCH_SOURCES
|
|||
common/utility/s_playlist.cpp
|
||||
common/utility/name.cpp
|
||||
common/utility/r_memory.cpp
|
||||
common/utility/writezip.cpp
|
||||
common/thirdparty/base64.cpp
|
||||
common/thirdparty/md5.cpp
|
||||
common/thirdparty/superfasthash.cpp
|
||||
|
|
|
@ -44,8 +44,6 @@
|
|||
#include <vector>
|
||||
#include "fs_swap.h"
|
||||
|
||||
#include "tarray.h"
|
||||
|
||||
namespace FileSys {
|
||||
|
||||
class FileSystemException : public std::exception
|
||||
|
|
|
@ -23,7 +23,6 @@ using FileList = std::vector<FileListEntry>;
|
|||
|
||||
struct FCompressedBuffer;
|
||||
bool ScanDirectory(std::vector<FileListEntry>& list, const char* dirpath, const char* match, bool nosubdir = false, bool readhidden = false);
|
||||
bool WriteZip(const char* filename, const FCompressedBuffer* content, size_t contentcount);
|
||||
bool FS_DirEntryExists(const char* pathname, bool* isdir);
|
||||
|
||||
inline void FixPathSeparator(char* path)
|
||||
|
|
|
@ -9,7 +9,6 @@
|
|||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace FileSys {
|
||||
|
||||
#pragma pack(1)
|
||||
// FZipCentralInfo
|
||||
|
@ -109,5 +108,4 @@ struct FZipLocalFileHeader
|
|||
// File header flags.
|
||||
#define ZF_ENCRYPTED 0x1
|
||||
|
||||
}
|
||||
#endif
|
|
@ -125,7 +125,7 @@ static const unsigned char BitReverse4[] = {
|
|||
#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)
|
||||
void FZipExploder::InsertCode(std::vector<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));
|
||||
|
@ -161,10 +161,10 @@ void FZipExploder::InsertCode(TArray<HuffNode> &decoder, unsigned int pos, int b
|
|||
}
|
||||
}
|
||||
|
||||
unsigned int FZipExploder::InitTable(TArray<HuffNode> &decoder, int numspots)
|
||||
unsigned int FZipExploder::InitTable(std::vector<HuffNode> &decoder, int numspots)
|
||||
{
|
||||
unsigned int start = decoder.Size();
|
||||
decoder.Reserve(numspots);
|
||||
size_t start = decoder.size();
|
||||
decoder.resize(decoder.size() + numspots);
|
||||
memset(&decoder[start], 0, sizeof(HuffNode)*numspots);
|
||||
return start;
|
||||
}
|
||||
|
@ -180,7 +180,7 @@ int FZipExploder::buildercmp(const void *a, const void *b)
|
|||
return d;
|
||||
}
|
||||
|
||||
int FZipExploder::BuildDecoder(TArray<HuffNode> &decoder, TableBuilder *values, int numvals)
|
||||
int FZipExploder::BuildDecoder(std::vector<HuffNode> &decoder, TableBuilder *values, int numvals)
|
||||
{
|
||||
int i;
|
||||
|
||||
|
@ -218,7 +218,7 @@ int FZipExploder::BuildDecoder(TArray<HuffNode> &decoder, TableBuilder *values,
|
|||
}
|
||||
|
||||
|
||||
int FZipExploder::DecodeSFValue(const TArray<HuffNode> &decoder)
|
||||
int FZipExploder::DecodeSFValue(const std::vector<HuffNode> &decoder)
|
||||
{
|
||||
unsigned int bits = FIRST_BIT_LEN, table = 0, code;
|
||||
const HuffNode *pos;
|
||||
|
@ -236,7 +236,7 @@ int FZipExploder::DecodeSFValue(const TArray<HuffNode> &decoder)
|
|||
}
|
||||
|
||||
|
||||
int FZipExploder::DecodeSF(TArray<HuffNode> &decoder, int numvals)
|
||||
int FZipExploder::DecodeSF(std::vector<HuffNode> &decoder, int numvals)
|
||||
{
|
||||
TableBuilder builder[256];
|
||||
unsigned char a, c;
|
||||
|
|
|
@ -27,18 +27,18 @@ class FZipExploder
|
|||
unsigned short Code;
|
||||
};
|
||||
|
||||
TArray<HuffNode> LiteralDecoder;
|
||||
TArray<HuffNode> DistanceDecoder;
|
||||
TArray<HuffNode> LengthDecoder;
|
||||
std::vector<HuffNode> LiteralDecoder;
|
||||
std::vector<HuffNode> DistanceDecoder;
|
||||
std::vector<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);
|
||||
void InsertCode(std::vector<HuffNode> &decoder, unsigned int pos, int bits, unsigned short code, int len, unsigned char value);
|
||||
unsigned int InitTable(std::vector<HuffNode> &decoder, int numspots);
|
||||
int BuildDecoder(std::vector<HuffNode> &decoder, TableBuilder *values, int numvals);
|
||||
int DecodeSFValue(const std::vector<HuffNode> ¤tTree);
|
||||
int DecodeSF(std::vector<HuffNode> &decoder, int numvals);
|
||||
public:
|
||||
int Explode(unsigned char *out, unsigned int outsize, FileReader &in, unsigned int insize, int flags);
|
||||
};
|
||||
|
|
|
@ -280,10 +280,9 @@ bool F7ZFile::Open(LumpFilterInfo *filter, FileSystemMessageFunc Printf)
|
|||
{
|
||||
// Quick check for unsupported compression method
|
||||
|
||||
TArray<char> temp;
|
||||
temp.Resize(Entries[0].Length);
|
||||
FileData temp(nullptr, Entries[0].Length);
|
||||
|
||||
if (SZ_OK != Archive->Extract(Entries[0].Position, &temp[0]))
|
||||
if (SZ_OK != Archive->Extract(Entries[0].Position, (char*)temp.writable()))
|
||||
{
|
||||
Printf(FSMessageLevel::Error, "%s: unsupported 7z/LZMA file!\n", FileName);
|
||||
return false;
|
||||
|
|
|
@ -73,9 +73,9 @@ static bool OpenPak(FResourceFile* file, LumpFilterInfo* filter)
|
|||
auto Entries = file->AllocateEntries(NumLumps);
|
||||
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));
|
||||
auto fd = Reader->Read (NumLumps * sizeof(dpackfile_t));
|
||||
auto fileinfo = (const dpackfile_t*)fd.data();
|
||||
|
||||
for(uint32_t i = 0; i < NumLumps; i++)
|
||||
{
|
||||
|
|
|
@ -122,9 +122,9 @@ bool FWadFile::Open(LumpFilterInfo*, FileSystemMessageFunc Printf)
|
|||
}
|
||||
}
|
||||
|
||||
TArray<wadlump_t> fileinfo(NumLumps, true);
|
||||
Reader.Seek (InfoTableOfs, FileReader::SeekSet);
|
||||
Reader.Read (fileinfo.Data(), NumLumps * sizeof(wadlump_t));
|
||||
Reader.Seek(InfoTableOfs, FileReader::SeekSet);
|
||||
auto fd = Reader.Read(NumLumps * sizeof(wadlump_t));
|
||||
auto fileinfo = (const wadlump_t*)fd.data();
|
||||
|
||||
AllocateEntries(NumLumps);
|
||||
|
||||
|
@ -214,20 +214,20 @@ void FWadFile::SetNamespace(const char *startmarker, const char *endmarker, name
|
|||
bool warned = false;
|
||||
int numstartmarkers = 0, numendmarkers = 0;
|
||||
unsigned int i;
|
||||
TArray<Marker> markers;
|
||||
std::vector<Marker> markers;
|
||||
|
||||
for(i = 0; i < NumLumps; i++)
|
||||
{
|
||||
if (IsMarker(i, startmarker))
|
||||
{
|
||||
Marker m = { 0, i };
|
||||
markers.Push(m);
|
||||
markers.push_back(m);
|
||||
numstartmarkers++;
|
||||
}
|
||||
else if (IsMarker(i, endmarker))
|
||||
{
|
||||
Marker m = { 1, i };
|
||||
markers.Push(m);
|
||||
markers.push_back(m);
|
||||
numendmarkers++;
|
||||
}
|
||||
}
|
||||
|
@ -243,7 +243,7 @@ void FWadFile::SetNamespace(const char *startmarker, const char *endmarker, name
|
|||
{
|
||||
// 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;
|
||||
unsigned int end = markers[markers.size()-1].index;
|
||||
for(unsigned int ii = 0; ii < end; ii++)
|
||||
{
|
||||
if (Entries[ii].Length == 4096)
|
||||
|
@ -259,7 +259,7 @@ void FWadFile::SetNamespace(const char *startmarker, const char *endmarker, name
|
|||
}
|
||||
|
||||
i = 0;
|
||||
while (i < markers.Size())
|
||||
while (i < markers.size())
|
||||
{
|
||||
int start, end;
|
||||
if (markers[i].markertype != 0)
|
||||
|
@ -271,21 +271,21 @@ void FWadFile::SetNamespace(const char *startmarker, const char *endmarker, name
|
|||
start = i++;
|
||||
|
||||
// skip over subsequent x_START markers
|
||||
while (i < markers.Size() && markers[i].markertype == 0)
|
||||
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))
|
||||
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())
|
||||
if (i >= markers.size())
|
||||
{
|
||||
Printf(FSMessageLevel::Warning, "%s: %s marker without corresponding %s found.\n", FileName, startmarker, endmarker);
|
||||
end = NumLumps;
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
**
|
||||
**---------------------------------------------------------------------------
|
||||
** Copyright 1998-2009 Randy Heit
|
||||
** Copyright 2005-2009 Christoph Oelckers
|
||||
** Copyright 2005-2023 Christoph Oelckers
|
||||
** All rights reserved.
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
|
@ -395,199 +395,4 @@ FResourceFile *CheckZip(const char *filename, FileReader &file, LumpFilterInfo*
|
|||
}
|
||||
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// 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;
|
||||
|
||||
int flags = 0;
|
||||
int method = content.mMethod;
|
||||
if (method >= METHOD_IMPLODE_MIN && method <= METHOD_IMPLODE_MAX)
|
||||
{
|
||||
flags = method - METHOD_IMPLODE_MIN;
|
||||
method = METHOD_IMPLODE;
|
||||
}
|
||||
else if (method == METHOD_DEFLATE)
|
||||
{
|
||||
flags = 2;
|
||||
}
|
||||
else if (method >= 1337)
|
||||
return -1;
|
||||
|
||||
local.Magic = ZIP_LOCALFILE;
|
||||
local.VersionToExtract[0] = 20;
|
||||
local.VersionToExtract[1] = 0;
|
||||
local.Flags = LittleShort((uint16_t)flags);
|
||||
local.Method = LittleShort((uint16_t)method);
|
||||
local.ModDate = LittleShort(dostime.first);
|
||||
local.ModTime = LittleShort(dostime.second);
|
||||
local.CRC32 = content.mCRC32;
|
||||
local.UncompressedSize = LittleLong((unsigned)content.mSize);
|
||||
local.CompressedSize = LittleLong((unsigned)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;
|
||||
|
||||
int flags = 0;
|
||||
int method = content.mMethod;
|
||||
if (method >= METHOD_IMPLODE_MIN && method <= METHOD_IMPLODE_MAX)
|
||||
{
|
||||
flags = method - METHOD_IMPLODE_MIN;
|
||||
method = METHOD_IMPLODE;
|
||||
}
|
||||
else if (method == METHOD_DEFLATE)
|
||||
{
|
||||
flags = 2;
|
||||
}
|
||||
else if (method >= 1337)
|
||||
return -1;
|
||||
|
||||
dir.Magic = ZIP_CENTRALFILE;
|
||||
dir.VersionMadeBy[0] = 20;
|
||||
dir.VersionMadeBy[1] = 0;
|
||||
dir.VersionToExtract[0] = 20;
|
||||
dir.VersionToExtract[1] = 0;
|
||||
dir.Flags = LittleShort((uint16_t)flags);
|
||||
dir.Method = LittleShort((uint16_t)method);
|
||||
dir.ModTime = LittleShort(dostime.first);
|
||||
dir.ModDate = LittleShort(dostime.second);
|
||||
dir.CRC32 = content.mCRC32;
|
||||
dir.CompressedSize32 = LittleLong((unsigned)content.mCompressedSize);
|
||||
dir.UncompressedSize32 = LittleLong((unsigned)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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
239
src/common/utility/writezip.cpp
Normal file
239
src/common/utility/writezip.cpp
Normal file
|
@ -0,0 +1,239 @@
|
|||
/*
|
||||
** writezip.cpp
|
||||
**
|
||||
**---------------------------------------------------------------------------
|
||||
** Copyright 1998-2009 Randy Heit
|
||||
** Copyright 2005-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 <stdint.h>
|
||||
#include <algorithm>
|
||||
#include "tarray.h"
|
||||
#include "files.h"
|
||||
#include "m_swap.h"
|
||||
#include "w_zip.h"
|
||||
|
||||
using FileSys::FCompressedBuffer;
|
||||
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// 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;
|
||||
|
||||
int flags = 0;
|
||||
int method = content.mMethod;
|
||||
if (method >= FileSys::METHOD_IMPLODE_MIN && method <= FileSys::METHOD_IMPLODE_MAX)
|
||||
{
|
||||
flags = method - FileSys::METHOD_IMPLODE_MIN;
|
||||
method = FileSys::METHOD_IMPLODE;
|
||||
}
|
||||
else if (method == FileSys::METHOD_DEFLATE)
|
||||
{
|
||||
flags = 2;
|
||||
}
|
||||
else if (method >= 1337)
|
||||
return -1;
|
||||
|
||||
local.Magic = ZIP_LOCALFILE;
|
||||
local.VersionToExtract[0] = 20;
|
||||
local.VersionToExtract[1] = 0;
|
||||
local.Flags = LittleShort((uint16_t)flags);
|
||||
local.Method = LittleShort((uint16_t)method);
|
||||
local.ModDate = LittleShort(dostime.first);
|
||||
local.ModTime = LittleShort(dostime.second);
|
||||
local.CRC32 = content.mCRC32;
|
||||
local.UncompressedSize = LittleLong((unsigned)content.mSize);
|
||||
local.CompressedSize = LittleLong((unsigned)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;
|
||||
|
||||
int flags = 0;
|
||||
int method = content.mMethod;
|
||||
if (method >= FileSys::METHOD_IMPLODE_MIN && method <= FileSys::METHOD_IMPLODE_MAX)
|
||||
{
|
||||
flags = method - FileSys::METHOD_IMPLODE_MIN;
|
||||
method = FileSys::METHOD_IMPLODE;
|
||||
}
|
||||
else if (method == FileSys::METHOD_DEFLATE)
|
||||
{
|
||||
flags = 2;
|
||||
}
|
||||
else if (method >= 1337)
|
||||
return -1;
|
||||
|
||||
dir.Magic = ZIP_CENTRALFILE;
|
||||
dir.VersionMadeBy[0] = 20;
|
||||
dir.VersionMadeBy[1] = 0;
|
||||
dir.VersionToExtract[0] = 20;
|
||||
dir.VersionToExtract[1] = 0;
|
||||
dir.Flags = LittleShort((uint16_t)flags);
|
||||
dir.Method = LittleShort((uint16_t)method);
|
||||
dir.ModTime = LittleShort(dostime.first);
|
||||
dir.ModDate = LittleShort(dostime.second);
|
||||
dir.CRC32 = content.mCRC32;
|
||||
dir.CompressedSize32 = LittleLong((unsigned)content.mCompressedSize);
|
||||
dir.UncompressedSize32 = LittleLong((unsigned)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;
|
||||
}
|
|
@ -96,6 +96,7 @@
|
|||
static FRandom pr_dmspawn ("DMSpawn");
|
||||
static FRandom pr_pspawn ("PlayerSpawn");
|
||||
|
||||
bool WriteZip(const char* filename, const FileSys::FCompressedBuffer* content, size_t contentcount);
|
||||
bool G_CheckDemoStatus (void);
|
||||
void G_ReadDemoTiccmd (ticcmd_t *cmd, int player);
|
||||
void G_WriteDemoTiccmd (ticcmd_t *cmd, int player, int buf);
|
||||
|
|
Loading…
Reference in a new issue