1 /****************************************************************************
2 ** Filename: unzip_p.h
3 ** Last updated [dd/mm/yyyy]: 27/03/2011
4 **
5 ** pkzip 2.0 decompression.
6 **
7 ** Some of the code has been inspired by other open source projects,
8 ** (mainly Info-Zip and Gilles Vollant's minizip).
9 ** Compression and decompression actually uses the zlib library.
10 **
11 ** Copyright (C) 2007-2012 Angius Fabrizio. All rights reserved.
12 **
13 ** This file is part of the OSDaB project (http://osdab.42cows.org/).
14 **
15 ** This file may be distributed and/or modified under the terms of the
16 ** GNU General Public License version 2 as published by the Free Software
17 ** Foundation and appearing in the file LICENSE.GPL included in the
18 ** packaging of this file.
19 **
20 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
21 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 **
23 ** See the file LICENSE.GPL that came with this software distribution or
24 ** visit http://www.gnu.org/copyleft/gpl.html for GPL licensing information.
25 **
26 **********************************************************************/
27
28 //
29 // W A R N I N G
30 // -------------
31 //
32 // This file is not part of the Zip/UnZip API. It exists purely as an
33 // implementation detail. This header file may change from version to
34 // version without notice, or even be removed.
35 //
36 // We mean it.
37 //
38
39 #ifndef OSDAB_UNZIP_P__H
40 #define OSDAB_UNZIP_P__H
41
42 #include "unzip.h"
43 #include "zipentry_p.h"
44
45 #include <QtCore/QObject>
46 #include <QtCore/QtGlobal>
47
48 // zLib authors suggest using larger buffers (128K or 256K) for (de)compression (especially for inflate())
49 // we use a 256K buffer here - if you want to use this code on a pre-iceage mainframe please change it ;)
50 #define UNZIP_READ_BUFFER (256*1024)
51
OSDAB_BEGIN_NAMESPACE(Zip)52 OSDAB_BEGIN_NAMESPACE(Zip)
53
54 class UnzipPrivate : public QObject
55 {
56 Q_OBJECT
57
58 public:
59 UnzipPrivate();
60
61 // Replace this with whatever else you use to store/retrieve the password.
62 QString password;
63
64 bool skipAllEncrypted;
65
66 QMap<QString,ZipEntryP*>* headers;
67
68 QIODevice* device;
69 QFile* file;
70
71 char buffer1[UNZIP_READ_BUFFER];
72 char buffer2[UNZIP_READ_BUFFER];
73
74 unsigned char* uBuffer;
75 const quint32* crcTable;
76
77 // Central Directory (CD) offset
78 quint32 cdOffset;
79 // End of Central Directory (EOCD) offset
80 quint32 eocdOffset;
81
82 // Number of entries in the Central Directory (as to the EOCD record)
83 quint16 cdEntryCount;
84
85 // The number of detected entries that have been skipped because of a non compatible format
86 quint16 unsupportedEntryCount;
87
88 QString comment;
89
90 UnZip::ErrorCode openArchive(QIODevice* device);
91
92 UnZip::ErrorCode seekToCentralDirectory();
93 UnZip::ErrorCode parseCentralDirectoryRecord();
94 UnZip::ErrorCode parseLocalHeaderRecord(const QString& path, const ZipEntryP& entry);
95
96 void closeArchive();
97
98 UnZip::ErrorCode extractFile(const QString& path, const ZipEntryP& entry, const QDir& dir, UnZip::ExtractionOptions options);
99 UnZip::ErrorCode extractFile(const QString& path, const ZipEntryP& entry, QIODevice* device, UnZip::ExtractionOptions options);
100
101 UnZip::ErrorCode testPassword(quint32* keys, const QString& file, const ZipEntryP& header);
102 bool testKeys(const ZipEntryP& header, quint32* keys);
103
104 bool createDirectory(const QString& path);
105
106 inline void decryptBytes(quint32* keys, char* buffer, qint64 read);
107
108 inline quint32 getULong(const unsigned char* data, quint32 offset) const;
109 inline quint64 getULLong(const unsigned char* data, quint32 offset) const;
110 inline quint16 getUShort(const unsigned char* data, quint32 offset) const;
111 inline int decryptByte(quint32 key2) const;
112 inline void updateKeys(quint32* keys, int c) const;
113 inline void initKeys(const QString& pwd, quint32* keys) const;
114
115 inline QDateTime convertDateTime(const unsigned char date[2], const unsigned char time[2]) const;
116
117 private slots:
118 void deviceDestroyed(QObject*);
119
120 private:
121 UnZip::ErrorCode extractStoredFile(const quint32 szComp, quint32** keys,
122 quint32& myCRC, QIODevice* outDev, UnZip::ExtractionOptions options);
123 UnZip::ErrorCode inflateFile(const quint32 szComp, quint32** keys,
124 quint32& myCRC, QIODevice* outDev, UnZip::ExtractionOptions options);
125 void do_closeArchive();
126 };
127
128 OSDAB_END_NAMESPACE
129
130 #endif // OSDAB_UNZIP_P__H
131