Add zip and LZMA stuff.

Took 1 minute

Took 43 seconds
This commit is contained in:
Lukas Brübach 2025-06-11 02:28:40 +02:00
parent f64d0ec71d
commit 1379464840
11 changed files with 4207 additions and 0 deletions

View file

@ -0,0 +1,250 @@
/*
* Simple routing to extract a single file from a xz archive
* Heavily based from doc/examples/02_decompress.c obtained from
* the official xz git repository: git.tukaani.org/xz.git
* The license from the original file header follows
*
* Author: Lasse Collin
* This file has been put into the public domain.
* You can do whatever you want with this file.
*/
#include <lzma.h>
#include <QDebug>
#include "decompress.h"
XzDecompressor::XzDecompressor(QObject *parent)
: QObject(parent)
{
}
bool XzDecompressor::decompress(QBuffer *in, QBuffer *out)
{
lzma_stream strm = LZMA_STREAM_INIT;
bool success;
if (!init_decoder(&strm)) {
return false;
}
success = internal_decompress(&strm, in, out);
// Free the memory allocated for the decoder. This only needs to be
// done after the last file.
lzma_end(&strm);
return success;
}
bool XzDecompressor::init_decoder(lzma_stream *strm)
{
// Initialize a .xz decoder. The decoder supports a memory usage limit
// and a set of flags.
//
// The memory usage of the decompressor depends on the settings used
// to compress a .xz file. It can vary from less than a megabyte to
// a few gigabytes, but in practice (at least for now) it rarely
// exceeds 65 MiB because that's how much memory is required to
// decompress files created with "xz -9". Settings requiring more
// memory take extra effort to use and don't (at least for now)
// provide significantly better compression in most cases.
//
// Memory usage limit is useful if it is important that the
// decompressor won't consume gigabytes of memory. The need
// for limiting depends on the application. In this example,
// no memory usage limiting is used. This is done by setting
// the limit to UINT64_MAX.
//
// The .xz format allows concatenating compressed files as is:
//
// echo foo | xz > foobar.xz
// echo bar | xz >> foobar.xz
//
// When decompressing normal standalone .xz files, LZMA_CONCATENATED
// should always be used to support decompression of concatenated
// .xz files. If LZMA_CONCATENATED isn't used, the decoder will stop
// after the first .xz stream. This can be useful when .xz data has
// been embedded inside another file format.
//
// Flags other than LZMA_CONCATENATED are supported too, and can
// be combined with bitwise-or. See lzma/container.h
// (src/liblzma/api/lzma/container.h in the source package or e.g.
// /usr/include/lzma/container.h depending on the install prefix)
// for details.
lzma_ret ret = lzma_stream_decoder(
strm, UINT64_MAX, LZMA_CONCATENATED);
// Return successfully if the initialization went fine.
if (ret == LZMA_OK)
return true;
// Something went wrong. The possible errors are documented in
// lzma/container.h (src/liblzma/api/lzma/container.h in the source
// package or e.g. /usr/include/lzma/container.h depending on the
// install prefix).
//
// Note that LZMA_MEMLIMIT_ERROR is never possible here. If you
// specify a very tiny limit, the error will be delayed until
// the first headers have been parsed by a call to lzma_code().
const char *msg;
switch (ret) {
case LZMA_MEM_ERROR:
msg = "Memory allocation failed";
break;
case LZMA_OPTIONS_ERROR:
msg = "Unsupported decompressor flags";
break;
default:
// This is most likely LZMA_PROG_ERROR indicating a bug in
// this program or in liblzma. It is inconvenient to have a
// separate error message for errors that should be impossible
// to occur, but knowing the error code is important for
// debugging. That's why it is good to print the error code
// at least when there is no good error message to show.
msg = "Unknown error, possibly a bug";
break;
}
qDebug() << "Error initializing the decoder:" << msg << "(error code " << ret << ")";
return false;
}
bool XzDecompressor::internal_decompress(lzma_stream *strm, QBuffer *in, QBuffer *out)
{
// When LZMA_CONCATENATED flag was used when initializing the decoder,
// we need to tell lzma_code() when there will be no more input.
// This is done by setting action to LZMA_FINISH instead of LZMA_RUN
// in the same way as it is done when encoding.
//
// When LZMA_CONCATENATED isn't used, there is no need to use
// LZMA_FINISH to tell when all the input has been read, but it
// is still OK to use it if you want. When LZMA_CONCATENATED isn't
// used, the decoder will stop after the first .xz stream. In that
// case some unused data may be left in strm->next_in.
lzma_action action = LZMA_RUN;
uint8_t inbuf[BUFSIZ];
uint8_t outbuf[BUFSIZ];
qint64 bytesAvailable;
strm->next_in = NULL;
strm->avail_in = 0;
strm->next_out = outbuf;
strm->avail_out = sizeof(outbuf);
while (true) {
if (strm->avail_in == 0) {
strm->next_in = inbuf;
bytesAvailable = in->bytesAvailable();
if(bytesAvailable == 0) {
// Once the end of the input file has been reached,
// we need to tell lzma_code() that no more input
// will be coming. As said before, this isn't required
// if the LZMA_CONCATENATED flag isn't used when
// initializing the decoder.
action = LZMA_FINISH;
} else if(bytesAvailable >= BUFSIZ) {
in->read((char*) inbuf, BUFSIZ);
strm->avail_in = BUFSIZ;
} else {
in->read((char*) inbuf, bytesAvailable);
strm->avail_in = bytesAvailable;
}
}
lzma_ret ret = lzma_code(strm, action);
if (strm->avail_out == 0 || ret == LZMA_STREAM_END) {
qint64 write_size = sizeof(outbuf) - strm->avail_out;
if (out->write((char *) outbuf, write_size) != write_size) {
qDebug() << "Write error";
return false;
}
strm->next_out = outbuf;
strm->avail_out = sizeof(outbuf);
}
if (ret != LZMA_OK) {
// Once everything has been decoded successfully, the
// return value of lzma_code() will be LZMA_STREAM_END.
//
// It is important to check for LZMA_STREAM_END. Do not
// assume that getting ret != LZMA_OK would mean that
// everything has gone well or that when you aren't
// getting more output it must have successfully
// decoded everything.
if (ret == LZMA_STREAM_END)
return true;
// It's not LZMA_OK nor LZMA_STREAM_END,
// so it must be an error code. See lzma/base.h
// (src/liblzma/api/lzma/base.h in the source package
// or e.g. /usr/include/lzma/base.h depending on the
// install prefix) for the list and documentation of
// possible values. Many values listen in lzma_ret
// enumeration aren't possible in this example, but
// can be made possible by enabling memory usage limit
// or adding flags to the decoder initialization.
const char *msg;
switch (ret) {
case LZMA_MEM_ERROR:
msg = "Memory allocation failed";
break;
case LZMA_FORMAT_ERROR:
// .xz magic bytes weren't found.
msg = "The input is not in the .xz format";
break;
case LZMA_OPTIONS_ERROR:
// For example, the headers specify a filter
// that isn't supported by this liblzma
// version (or it hasn't been enabled when
// building liblzma, but no-one sane does
// that unless building liblzma for an
// embedded system). Upgrading to a newer
// liblzma might help.
//
// Note that it is unlikely that the file has
// accidentally became corrupt if you get this
// error. The integrity of the .xz headers is
// always verified with a CRC32, so
// unintentionally corrupt files can be
// distinguished from unsupported files.
msg = "Unsupported compression options";
break;
case LZMA_DATA_ERROR:
msg = "Compressed file is corrupt";
break;
case LZMA_BUF_ERROR:
// Typically this error means that a valid
// file has got truncated, but it might also
// be a damaged part in the file that makes
// the decoder think the file is truncated.
// If you prefer, you can use the same error
// message for this as for LZMA_DATA_ERROR.
msg = "Compressed file is truncated or "
"otherwise corrupt";
break;
default:
// This is most likely LZMA_PROG_ERROR.
msg = "Unknown error, possibly a bug";
break;
}
qDebug() << "Decoder error:" << msg << "(error code " << ret << ")";
return false;
}
}
}

View file

@ -0,0 +1,19 @@
#ifndef XZ_DECOMPRESS_H
#define XZ_DECOMPRESS_H
#include <lzma.h>
#include <QBuffer>
class XzDecompressor : public QObject
{
Q_OBJECT
public:
XzDecompressor(QObject *parent = 0);
~XzDecompressor() { };
bool decompress(QBuffer *in, QBuffer *out);
private:
bool init_decoder(lzma_stream *strm);
bool internal_decompress(lzma_stream *strm, QBuffer *in, QBuffer *out);
};
#endif

1425
cockatrice/src/zip/unzip.cpp Executable file

File diff suppressed because it is too large Load diff

155
cockatrice/src/zip/unzip.h Normal file
View file

@ -0,0 +1,155 @@
/****************************************************************************
** Filename: unzip.h
** Last updated [dd/mm/yyyy]: 27/03/2011
**
** pkzip 2.0 decompression.
**
** Some of the code has been inspired by other open source projects,
** (mainly Info-Zip and Gilles Vollant's minizip).
** Compression and decompression actually uses the zlib library.
**
** Copyright (C) 2007-2012 Angius Fabrizio. All rights reserved.
**
** This file is part of the OSDaB project (http://osdab.42cows.org/).
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See the file LICENSE.GPL that came with this software distribution or
** visit http://www.gnu.org/copyleft/gpl.html for GPL licensing information.
**
**********************************************************************/
#ifndef OSDAB_UNZIP__H
#define OSDAB_UNZIP__H
#include "zipglobal.h"
#include <QtCore/QDateTime>
#include <QtCore/QMap>
#include <QtCore/QtGlobal>
#include <zlib.h>
class QDir;
class QFile;
class QIODevice;
class QString;
OSDAB_BEGIN_NAMESPACE(Zip)
class UnzipPrivate;
class OSDAB_ZIP_EXPORT UnZip
{
public:
enum ErrorCode
{
Ok,
ZlibInit,
ZlibError,
OpenFailed,
PartiallyCorrupted,
Corrupted,
WrongPassword,
NoOpenArchive,
FileNotFound,
ReadFailed,
WriteFailed,
SeekFailed,
CreateDirFailed,
InvalidDevice,
InvalidArchive,
HeaderConsistencyError,
Skip,
SkipAll // internal use only
};
enum ExtractionOption
{
ExtractPaths = 0x0001,
SkipPaths = 0x0002,
VerifyOnly = 0x0004,
NoSilentDirectoryCreation = 0x0008
};
Q_DECLARE_FLAGS(ExtractionOptions, ExtractionOption)
enum CompressionMethod
{
NoCompression,
Deflated,
UnknownCompression
};
enum FileType
{
File,
Directory
};
struct ZipEntry
{
ZipEntry();
QString filename;
QString comment;
quint32 compressedSize;
quint32 uncompressedSize;
quint32 crc32;
QDateTime lastModified;
CompressionMethod compression;
FileType type;
bool encrypted;
};
UnZip();
virtual ~UnZip();
bool isOpen() const;
ErrorCode openArchive(const QString &filename);
ErrorCode openArchive(QIODevice *device);
void closeArchive();
QString archiveComment() const;
QString formatError(UnZip::ErrorCode c) const;
bool contains(const QString &file) const;
QStringList fileList() const;
QList<ZipEntry> entryList() const;
ErrorCode verifyArchive();
ErrorCode extractAll(const QString &dirname, ExtractionOptions options = ExtractPaths);
ErrorCode extractAll(const QDir &dir, ExtractionOptions options = ExtractPaths);
ErrorCode extractFile(const QString &filename, const QString &dirname, ExtractionOptions options = ExtractPaths);
ErrorCode extractFile(const QString &filename, const QDir &dir, ExtractionOptions options = ExtractPaths);
ErrorCode extractFile(const QString &filename, QIODevice *device, ExtractionOptions options = ExtractPaths);
ErrorCode
extractFiles(const QStringList &filenames, const QString &dirname, ExtractionOptions options = ExtractPaths);
ErrorCode extractFiles(const QStringList &filenames, const QDir &dir, ExtractionOptions options = ExtractPaths);
void setPassword(const QString &pwd);
private:
UnzipPrivate *d;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(UnZip::ExtractionOptions)
OSDAB_END_NAMESPACE
#endif // OSDAB_UNZIP__H

130
cockatrice/src/zip/unzip_p.h Executable file
View file

@ -0,0 +1,130 @@
/****************************************************************************
** Filename: unzip_p.h
** Last updated [dd/mm/yyyy]: 27/03/2011
**
** pkzip 2.0 decompression.
**
** Some of the code has been inspired by other open source projects,
** (mainly Info-Zip and Gilles Vollant's minizip).
** Compression and decompression actually uses the zlib library.
**
** Copyright (C) 2007-2012 Angius Fabrizio. All rights reserved.
**
** This file is part of the OSDaB project (http://osdab.42cows.org/).
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See the file LICENSE.GPL that came with this software distribution or
** visit http://www.gnu.org/copyleft/gpl.html for GPL licensing information.
**
**********************************************************************/
//
// W A R N I N G
// -------------
//
// This file is not part of the Zip/UnZip API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#ifndef OSDAB_UNZIP_P__H
#define OSDAB_UNZIP_P__H
#include "unzip.h"
#include "zipentry_p.h"
#include <QtCore/QObject>
#include <QtCore/QtGlobal>
// zLib authors suggest using larger buffers (128K or 256K) for (de)compression (especially for inflate())
// we use a 256K buffer here - if you want to use this code on a pre-iceage mainframe please change it ;)
#define UNZIP_READ_BUFFER (256*1024)
OSDAB_BEGIN_NAMESPACE(Zip)
class UnzipPrivate : public QObject
{
Q_OBJECT
public:
UnzipPrivate();
// Replace this with whatever else you use to store/retrieve the password.
QString password;
bool skipAllEncrypted;
QMap<QString,ZipEntryP*>* headers;
QIODevice* device;
QFile* file;
char buffer1[UNZIP_READ_BUFFER];
char buffer2[UNZIP_READ_BUFFER];
unsigned char* uBuffer;
const quint32* crcTable;
// Central Directory (CD) offset
quint32 cdOffset;
// End of Central Directory (EOCD) offset
quint32 eocdOffset;
// Number of entries in the Central Directory (as to the EOCD record)
quint16 cdEntryCount;
// The number of detected entries that have been skipped because of a non compatible format
quint16 unsupportedEntryCount;
QString comment;
UnZip::ErrorCode openArchive(QIODevice* device);
UnZip::ErrorCode seekToCentralDirectory();
UnZip::ErrorCode parseCentralDirectoryRecord();
UnZip::ErrorCode parseLocalHeaderRecord(const QString& path, const ZipEntryP& entry);
void closeArchive();
UnZip::ErrorCode extractFile(const QString& path, const ZipEntryP& entry, const QDir& dir, UnZip::ExtractionOptions options);
UnZip::ErrorCode extractFile(const QString& path, const ZipEntryP& entry, QIODevice* device, UnZip::ExtractionOptions options);
UnZip::ErrorCode testPassword(quint32* keys, const QString&_file, const ZipEntryP& header);
bool testKeys(const ZipEntryP& header, quint32* keys);
bool createDirectory(const QString& path);
inline void decryptBytes(quint32* keys, char* buffer, qint64 read);
inline quint32 getULong(const unsigned char* data, quint32 offset) const;
inline quint64 getULLong(const unsigned char* data, quint32 offset) const;
inline quint16 getUShort(const unsigned char* data, quint32 offset) const;
inline int decryptByte(quint32 key2) const;
inline void updateKeys(quint32* keys, int c) const;
inline void initKeys(const QString& pwd, quint32* keys) const;
inline QDateTime convertDateTime(const unsigned char date[2], const unsigned char time[2]) const;
private slots:
void deviceDestroyed(QObject*);
private:
UnZip::ErrorCode extractStoredFile(const quint32 szComp, quint32** keys,
quint32& myCRC, QIODevice* outDev, UnZip::ExtractionOptions options);
UnZip::ErrorCode inflateFile(const quint32 szComp, quint32** keys,
quint32& myCRC, QIODevice* outDev, UnZip::ExtractionOptions options);
void do_closeArchive();
};
OSDAB_END_NAMESPACE
#endif // OSDAB_UNZIP_P__H

1619
cockatrice/src/zip/zip.cpp Executable file

File diff suppressed because it is too large Load diff

158
cockatrice/src/zip/zip.h Executable file
View file

@ -0,0 +1,158 @@
/****************************************************************************
** Filename: zip.h
** Last updated [dd/mm/yyyy]: 27/03/2011
**
** pkzip 2.0 file compression.
**
** Some of the code has been inspired by other open source projects,
** (mainly Info-Zip and Gilles Vollant's minizip).
** Compression and decompression actually uses the zlib library.
**
** Copyright (C) 2007-2012 Angius Fabrizio. All rights reserved.
**
** This file is part of the OSDaB project (http://osdab.42cows.org/).
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See the file LICENSE.GPL that came with this software distribution or
** visit http://www.gnu.org/copyleft/gpl.html for GPL licensing information.
**
**********************************************************************/
#ifndef OSDAB_ZIP__H
#define OSDAB_ZIP__H
#include "zipglobal.h"
#include <QtCore/QMap>
#include <QtCore/QtGlobal>
#include <zlib/zlib.h>
class QIODevice;
class QFile;
class QDir;
class QStringList;
class QString;
OSDAB_BEGIN_NAMESPACE(Zip)
class ZipPrivate;
class OSDAB_ZIP_EXPORT Zip
{
public:
enum ErrorCode
{
Ok,
ZlibInit,
ZlibError,
FileExists,
OpenFailed,
NoOpenArchive,
FileNotFound,
ReadFailed,
WriteFailed,
SeekFailed,
InternalError
};
enum CompressionLevel
{
Store,
Deflate1 = 1, Deflate2, Deflate3, Deflate4,
Deflate5, Deflate6, Deflate7, Deflate8, Deflate9,
AutoCPU, AutoMIME, AutoFull
};
enum CompressionOption
{
/*! Does not preserve absolute paths in the zip file when adding a
file or directory (default) */
RelativePaths = 0x0001,
/*! Preserve absolute paths */
AbsolutePaths = 0x0002,
/*! Do not store paths. All the files are put in the (evtl. user defined)
root of the zip file */
IgnorePaths = 0x0004,
/*! Works only with addDirectory(). Adds the directory's contents,
including subdirectories, but does not add an entry for the root
directory itself. */
IgnoreRoot = 0x0008,
/*! Used only when compressing a directory or multiple files.
If set invalid or unreadable files are simply skipped.
*/
SkipBadFiles = 0x0020,
/*! Makes sure a file is never added twice to the same zip archive.
This check is only necessary in certain usage scenarios and given
that it slows down processing you need to enable it explicitly with
this flag.
*/
CheckForDuplicates = 0x0040
};
Q_DECLARE_FLAGS(CompressionOptions, CompressionOption)
Zip();
virtual ~Zip();
bool isOpen() const;
void setPassword(const QString& pwd);
void clearPassword();
QString password() const;
ErrorCode createArchive(const QString& file, bool overwrite = true);
ErrorCode createArchive(QIODevice* device);
QString archiveComment() const;
void setArchiveComment(const QString& comment);
ErrorCode addDirectoryContents(const QString& path,
CompressionLevel level = AutoFull);
ErrorCode addDirectoryContents(const QString& path, const QString& root,
CompressionLevel level = AutoFull);
ErrorCode addDirectory(const QString& path,
CompressionLevel level = AutoFull);
ErrorCode addDirectory(const QString& path, const QString& root,
CompressionLevel level = AutoFull);
ErrorCode addDirectory(const QString& path, const QString& root,
CompressionOptions options, CompressionLevel level = AutoFull,
int* addedFiles = 0);
ErrorCode addFile(const QString& path,
CompressionLevel level = AutoFull);
ErrorCode addFile(const QString& path, const QString& root,
CompressionLevel level = AutoFull);
ErrorCode addFile(const QString& path, const QString& root,
CompressionOptions options,
CompressionLevel level = AutoFull);
ErrorCode addFiles(const QStringList& paths,
CompressionLevel level = AutoFull);
ErrorCode addFiles(const QStringList& paths, const QString& root,
CompressionLevel level = AutoFull);
ErrorCode addFiles(const QStringList& paths, const QString& root,
CompressionOptions options,
CompressionLevel level = AutoFull,
int* addedFiles = 0);
ErrorCode closeArchive();
QString formatError(ErrorCode c) const;
private:
ZipPrivate* d;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(Zip::CompressionOptions)
OSDAB_END_NAMESPACE
#endif // OSDAB_ZIP__H

133
cockatrice/src/zip/zip_p.h Executable file
View file

@ -0,0 +1,133 @@
/****************************************************************************
** Filename: zip_p.h
** Last updated [dd/mm/yyyy]: 27/03/2011
**
** pkzip 2.0 file compression.
**
** Some of the code has been inspired by other open source projects,
** (mainly Info-Zip and Gilles Vollant's minizip).
** Compression and decompression actually uses the zlib library.
**
** Copyright (C) 2007-2012 Angius Fabrizio. All rights reserved.
**
** This file is part of the OSDaB project (http://osdab.42cows.org/).
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See the file LICENSE.GPL that came with this software distribution or
** visit http://www.gnu.org/copyleft/gpl.html for GPL licensing information.
**
**********************************************************************/
//
// W A R N I N G
// -------------
//
// This file is not part of the Zip/UnZip API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#ifndef OSDAB_ZIP_P__H
#define OSDAB_ZIP_P__H
#include "zip.h"
#include "zipentry_p.h"
#include <QtCore/QFileInfo>
#include <QtCore/QObject>
#include <QtCore/QtGlobal>
#include <zlib/zconf.h>
/*!
zLib authors suggest using larger buffers (128K or 256K) for (de)compression (especially for inflate())
we use a 256K buffer here - if you want to use this code on a pre-iceage mainframe please change it ;)
*/
#define ZIP_READ_BUFFER (256*1024)
OSDAB_BEGIN_NAMESPACE(Zip)
class ZipPrivate : public QObject
{
Q_OBJECT
public:
// uLongf from zconf.h
typedef uLongf crc_t;
ZipPrivate();
virtual ~ZipPrivate();
QMap<QString,ZipEntryP*>* headers;
QIODevice* device;
QFile* file;
char buffer1[ZIP_READ_BUFFER];
char buffer2[ZIP_READ_BUFFER];
unsigned char* uBuffer;
const crc_t* crcTable;
QString comment;
QString password;
Zip::ErrorCode createArchive(QIODevice* device);
Zip::ErrorCode closeArchive();
void reset();
bool zLibInit();
bool containsEntry(const QFileInfo& info) const;
Zip::ErrorCode addDirectory(const QString& path, const QString& root,
Zip::CompressionOptions options, Zip::CompressionLevel level,
int hierarchyLevel, int* addedFiles = 0);
Zip::ErrorCode addFiles(const QStringList& paths, const QString& root,
Zip::CompressionOptions options, Zip::CompressionLevel level,
int* addedFiles);
Zip::ErrorCode createEntry(const QFileInfo& file, const QString& root,
Zip::CompressionLevel level);
Zip::CompressionLevel detectCompressionByMime(const QString& ext);
inline quint32 updateChecksum(const quint32& crc, const quint32& val) const;
inline void encryptBytes(quint32* keys, char* buffer, qint64 read);
inline void setULong(quint32 v, char* buffer, unsigned int offset);
inline void updateKeys(quint32* keys, int c) const;
inline void initKeys(quint32* keys) const;
inline int decryptByte(quint32 key2) const;
inline QString extractRoot(const QString& p, Zip::CompressionOptions o);
private slots:
void deviceDestroyed(QObject*);
private:
int compressionStrategy(const QString& path, QIODevice& file) const;
Zip::ErrorCode deflateFile(const QFileInfo& fileInfo,
quint32& crc, qint64& written, const Zip::CompressionLevel& level, quint32** keys);
Zip::ErrorCode storeFile(const QString& path, QIODevice& file,
quint32& crc, qint64& written, quint32** keys);
Zip::ErrorCode compressFile(const QString& path, QIODevice& file,
quint32& crc, qint64& written, const Zip::CompressionLevel& level, quint32** keys);
Zip::ErrorCode do_closeArchive();
Zip::ErrorCode writeEntry(const QString& fileName, const ZipEntryP* h, quint32& szCentralDir);
Zip::ErrorCode writeCentralDir(quint32 offCentralDir, quint32 szCentralDir);
};
OSDAB_END_NAMESPACE
#endif // OSDAB_ZIP_P__H

91
cockatrice/src/zip/zipentry_p.h Executable file
View file

@ -0,0 +1,91 @@
/****************************************************************************
** Filename: ZipEntryP.h
** Last updated [dd/mm/yyyy]: 27/03/2011
**
** Wrapper for a ZIP local header.
**
** Some of the code has been inspired by other open source projects,
** (mainly Info-Zip and Gilles Vollant's minizip).
** Compression and decompression actually uses the zlib library.
**
** Copyright (C) 2007-2012 Angius Fabrizio. All rights reserved.
**
** This file is part of the OSDaB project (http://osdab.42cows.org/).
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See the file LICENSE.GPL that came with this software distribution or
** visit http://www.gnu.org/copyleft/gpl.html for GPL licensing information.
**
**********************************************************************/
//
// W A R N I N G
// -------------
//
// This file is not part of the Zip/UnZip API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#ifndef OSDAB_ZIPENTRY_P__H
#define OSDAB_ZIPENTRY_P__H
#include <QtCore/QString>
#include <QtCore/QtGlobal>
OSDAB_BEGIN_NAMESPACE(Zip)
class ZipEntryP
{
public:
ZipEntryP() :
lhOffset(0),
dataOffset(0),
gpFlag(),
compMethod(0),
modTime(),
modDate(),
crc(0),
szComp(0),
szUncomp(0),
absolutePath(),
fileSize(0),
lhEntryChecked(false)
{
gpFlag[0] = gpFlag[1] = 0;
modTime[0] = modTime[1] = 0;
modDate[0] = modDate[1] = 0;
}
quint32 lhOffset; // Offset of the local header record for this entry
mutable quint32 dataOffset; // Offset of the file data for this entry
unsigned char gpFlag[2]; // General purpose flag
quint16 compMethod; // Compression method
unsigned char modTime[2]; // Last modified time
unsigned char modDate[2]; // Last modified date
quint32 crc; // CRC32
quint32 szComp; // Compressed file size
quint32 szUncomp; // Uncompressed file size
QString comment; // File comment
QString absolutePath; // Internal use
qint64 fileSize; // Internal use
mutable bool lhEntryChecked; // Is true if the local header record for this entry has been parsed
inline bool isEncrypted() const { return gpFlag[0] & 0x01; }
inline bool hasDataDescriptor() const { return gpFlag[0] & 0x08; }
};
OSDAB_END_NAMESPACE
#endif // OSDAB_ZIPENTRY_P__H

View file

@ -0,0 +1,150 @@
/****************************************************************************
** Filename: zipglobal.cpp
** Last updated [dd/mm/yyyy]: 06/02/2011
**
** pkzip 2.0 file compression.
**
** Some of the code has been inspired by other open source projects,
** (mainly Info-Zip and Gilles Vollant's minizip).
** Compression and decompression actually uses the zlib library.
**
** Copyright (C) 2007-2012 Angius Fabrizio. All rights reserved.
**
** This file is part of the OSDaB project (http://osdab.42cows.org/).
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See the file LICENSE.GPL that came with this software distribution or
** visit http://www.gnu.org/copyleft/gpl.html for GPL licensing information.
**
**********************************************************************/
#include "zipglobal.h"
#if defined(Q_OS_WIN) || defined(Q_OS_WINCE) || defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
#define OSDAB_ZIP_HAS_UTC
#include <ctime>
#else
#undef OSDAB_ZIP_HAS_UTC
#endif
#if defined(Q_OS_WIN)
#include <QtCore/qt_windows.h>
#elif defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
#include <utime.h>
#endif
OSDAB_BEGIN_NAMESPACE(Zip)
/*! Returns the current UTC offset in seconds unless OSDAB_ZIP_NO_UTC is defined
and method is implemented for the current platform and 0 otherwise.
*/
int OSDAB_ZIP_MANGLE(currentUtcOffset)()
{
#if !(!defined OSDAB_ZIP_NO_UTC && defined OSDAB_ZIP_HAS_UTC)
return 0;
#else
time_t curr_time_t;
time(&curr_time_t);
#if defined Q_OS_WIN
struct tm _tm_struct;
struct tm *tm_struct = &_tm_struct;
#else
struct tm *tm_struct = 0;
#endif
#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
// use the reentrant version of localtime() where available
tzset();
tm res;
tm_struct = gmtime_r(&curr_time_t, &res);
#elif defined Q_OS_WIN && !defined Q_CC_MINGW
if (gmtime_s(tm_struct, &curr_time_t))
return 0;
#else
tm_struct = gmtime(&curr_time_t);
#endif
if (!tm_struct)
return 0;
const time_t global_time_t = mktime(tm_struct);
#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
// use the reentrant version of localtime() where available
tm_struct = localtime_r(&curr_time_t, &res);
#elif defined Q_OS_WIN && !defined Q_CC_MINGW
if (localtime_s(tm_struct, &curr_time_t))
return 0;
#else
tm_struct = localtime(&curr_time_t);
#endif
if (!tm_struct)
return 0;
const time_t local_time_t = mktime(tm_struct);
const int utcOffset = -qRound(difftime(global_time_t, local_time_t));
return tm_struct->tm_isdst > 0 ? utcOffset + 3600 : utcOffset;
#endif // No UTC
}
QDateTime OSDAB_ZIP_MANGLE(fromFileTimestamp)(const QDateTime &dateTime)
{
#if !defined OSDAB_ZIP_NO_UTC && defined OSDAB_ZIP_HAS_UTC
const int utc = OSDAB_ZIP_MANGLE(currentUtcOffset)();
return dateTime.toUTC().addSecs(utc);
#else
return dateTime;
#endif // OSDAB_ZIP_NO_UTC
}
bool OSDAB_ZIP_MANGLE(setFileTimestamp)(const QString &fileName, const QDateTime &dateTime)
{
if (fileName.isEmpty())
return true;
#ifdef Q_OS_WIN
HANDLE hFile =
CreateFileW(fileName.toStdWString().c_str(), GENERIC_WRITE, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
if (hFile == INVALID_HANDLE_VALUE) {
return false;
}
SYSTEMTIME st;
FILETIME ft, ftLastMod;
const QDate date = dateTime.date();
const QTime time = dateTime.time();
st.wYear = date.year();
st.wMonth = date.month();
st.wDay = date.day();
st.wHour = time.hour();
st.wMinute = time.minute();
st.wSecond = time.second();
st.wMilliseconds = time.msec();
SystemTimeToFileTime(&st, &ft);
LocalFileTimeToFileTime(&ft, &ftLastMod);
const bool success = SetFileTime(hFile, NULL, NULL, &ftLastMod);
CloseHandle(hFile);
return success;
#elif defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
struct utimbuf t_buffer;
t_buffer.actime = t_buffer.modtime = dateTime.toSecsSinceEpoch();
return utime(fileName.toLocal8Bit().constData(), &t_buffer) == 0;
#endif
return true;
}
OSDAB_END_NAMESPACE

77
cockatrice/src/zip/zipglobal.h Executable file
View file

@ -0,0 +1,77 @@
/****************************************************************************
** Filename: zipglobal.h
** Last updated [dd/mm/yyyy]: 27/03/2011
**
** pkzip 2.0 file compression.
**
** Some of the code has been inspired by other open source projects,
** (mainly Info-Zip and Gilles Vollant's minizip).
** Compression and decompression actually uses the zlib library.
**
** Copyright (C) 2007-2012 Angius Fabrizio. All rights reserved.
**
** This file is part of the OSDaB project (http://osdab.42cows.org/).
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See the file LICENSE.GPL that came with this software distribution or
** visit http://www.gnu.org/copyleft/gpl.html for GPL licensing information.
**
**********************************************************************/
#ifndef OSDAB_ZIPGLOBAL__H
#define OSDAB_ZIPGLOBAL__H
#include <QtCore/QDateTime>
#include <QtCore/QtGlobal>
/* If you want to build the OSDaB Zip code as
a library, define OSDAB_ZIP_LIB in the library's .pro file and
in the libraries using it OR remove the #ifndef OSDAB_ZIP_LIB
define below and leave the #else body. Also remember to define
OSDAB_ZIP_BUILD_LIB in the library's project).
*/
#ifndef OSDAB_ZIP_LIB
# define OSDAB_ZIP_EXPORT
#else
# if defined(OSDAB_ZIP_BUILD_LIB)
# define OSDAB_ZIP_EXPORT Q_DECL_EXPORT
# else
# define OSDAB_ZIP_EXPORT Q_DECL_IMPORT
# endif
#endif
#ifdef OSDAB_NAMESPACE
#define OSDAB_BEGIN_NAMESPACE(ModuleName) namespace Osdab { namespace ModuleName {
#else
#define OSDAB_BEGIN_NAMESPACE(ModuleName)
#endif
#ifdef OSDAB_NAMESPACE
#define OSDAB_END_NAMESPACE } }
#else
#define OSDAB_END_NAMESPACE
#endif
#ifndef OSDAB_NAMESPACE
#define OSDAB_ZIP_MANGLE(x) zip_##x
#else
#define OSDAB_ZIP_MANGLE(x) x
#endif
OSDAB_BEGIN_NAMESPACE(Zip)
OSDAB_ZIP_EXPORT int OSDAB_ZIP_MANGLE(currentUtcOffset)();
OSDAB_ZIP_EXPORT QDateTime OSDAB_ZIP_MANGLE(fromFileTimestamp)(const QDateTime& dateTime);
OSDAB_ZIP_EXPORT bool OSDAB_ZIP_MANGLE(setFileTimestamp)(const QString& fileName, const QDateTime& dateTime);
OSDAB_END_NAMESPACE
#endif // OSDAB_ZIPGLOBAL__H