-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathcompressed_archive.cpp
322 lines (273 loc) · 13.5 KB
/
compressed_archive.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <QtCore>
#include "compressed_archive.h"
#include "extract_delegate.h"
#include "yacreader_global.h"
#include <QLibrary>
#include <QFileInfo>
#include <QDebug>
#include <QCoreApplication>
#include "open_callbacks.h"
#include "extract_callbacks.h"
#include "7z_includes.h"
#include "lib7zip/CPP/Common/MyInitGuid.h"
#include "QsLog.h"
#define _MY_WINAPI WINAPI
typedef quint32(_MY_WINAPI *CreateObjectFunc)(const GUID *clsID, const GUID *interfaceID, void **outObject);
typedef quint32(_MY_WINAPI *GetMethodPropertyFunc)(quint32 index, PROPID propID, PROPVARIANT *value);
typedef quint32(_MY_WINAPI *GetNumberOfMethodsFunc)(quint32 *numMethods);
typedef quint32(_MY_WINAPI *GetNumberOfFormatsFunc)(quint32 *numFormats);
typedef quint32(_MY_WINAPI *GetHandlerPropertyFunc)(PROPID propID, PROPVARIANT *value);
typedef quint32(_MY_WINAPI *GetHandlerPropertyFunc2)(quint32 index, PROPID propID, PROPVARIANT *value);
typedef quint32(_MY_WINAPI *SetLargePageModeFunc)();
// DEFINE_GUID(CLSID_CFormat7z,0x23170F69, 0x40C1, 0x278A, 0x10, 0x00, 0x00, 0x01, 0x10, 0x07, 0x00, 0x00);
// DEFINE_GUID(IArchiveKK,0x23170F69, 0x40C1, 0x278A, 0x00, 0x00, 0x00, 0x06, 0x00, 0x60, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormat7z, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0x07, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatRar, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0x03, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatRar5, 0X23170F69, 0x40C1, 0x278A, 0x10, 0x00, 0x00, 0x01, 0x10, 0xCC, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatZip, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0x01, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatTar, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0xee, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatArj, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0x04, 0x00, 0x00);
// unused Formats
/*DEFINE_GUID(CLSID_CFormatBZip2, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0x02, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatCab, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0x08, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatChm, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0xe9, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatCompound,0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0xe5, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatCpio, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0xed, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatDeb, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0xec, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatGZip, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0xef, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatIso, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0xe7, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatLzh, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0x06, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatLzma, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0x0a, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatNsis, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0x09, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatRpm, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0xeb, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatSplit, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0xea, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatWim, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0xe6, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatZ, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0x05, 0x00, 0x00);*/
GUID _supportedFileFormats[] = { CLSID_CFormatRar, CLSID_CFormatZip, CLSID_CFormatTar, CLSID_CFormat7z, CLSID_CFormatArj, CLSID_CFormatRar5 };
std::vector<GUID> supportedFileFormats(_supportedFileFormats, _supportedFileFormats + sizeof(_supportedFileFormats) / sizeof(_supportedFileFormats[0]));
DEFINE_GUID(IID_InArchive, 0x23170F69, 0x40C1, 0x278A, 0x00, 0x00, 0x00, 0x06, 0x00, 0x60, 0x00, 0x00);
DEFINE_GUID(IID_ISetCompressCodecsInfoX, 0x23170F69, 0x40C1, 0x278A, 0x00, 0x00, 0x00, 0x04, 0x00, 0x61, 0x00, 0x00);
struct SevenZipInterface {
CreateObjectFunc createObjectFunc;
GetMethodPropertyFunc getMethodPropertyFunc;
GetNumberOfMethodsFunc getNumberOfMethodsFunc;
GetNumberOfFormatsFunc getNumberOfFormatsFunc;
GetHandlerPropertyFunc getHandlerPropertyFunc;
GetHandlerPropertyFunc2 getHandlerPropertyFunc2;
SetLargePageModeFunc setLargePageModeFunc;
CMyComPtr<IInArchive> archive;
};
// SevenZipInterface * szInterface;
const unsigned char rar[7] = { static_cast<unsigned char>(0x52), static_cast<unsigned char>(0x61), static_cast<unsigned char>(0x72), static_cast<unsigned char>(0x21), static_cast<unsigned char>(0x1A), static_cast<unsigned char>(0x07), static_cast<unsigned char>(0x00) };
const unsigned char rar5[8] = { static_cast<unsigned char>(0x52), static_cast<unsigned char>(0x61), static_cast<unsigned char>(0x72), static_cast<unsigned char>(0x21), static_cast<unsigned char>(0x1A), static_cast<unsigned char>(0x07), static_cast<unsigned char>(0x01), static_cast<unsigned char>(0x00) };
const unsigned char zip[2] = { static_cast<unsigned char>(0x50), static_cast<unsigned char>(0x4B) };
const unsigned char sevenz[6] = { static_cast<unsigned char>(0x37), static_cast<unsigned char>(0x7A), static_cast<unsigned char>(0xBC), static_cast<unsigned char>(0xAF), static_cast<unsigned char>(0x27), static_cast<unsigned char>(0x1C) };
const unsigned char tar[6] = "ustar";
const unsigned char arj[2] = { static_cast<unsigned char>(0x60), static_cast<unsigned char>(0xEA) };
CompressedArchive::CompressedArchive(const QString &filePath, QObject *parent)
: QObject(parent), sevenzLib(nullptr), tools(false), valid(false)
{
szInterface = new SevenZipInterface;
// load functions
if (!loadFunctions())
return;
tools = true;
// load file
if (szInterface->createObjectFunc != 0) {
// QUuid CLSID_CFormat7z("23170f69-40c1-278a-1000-000110070000");
// se crea el objeto Archivo: formato,tipo,objeto
bool formatFound = false;
CInFileStream *fileSpec = new CInFileStream;
CMyComPtr<IInStream> file = fileSpec;
YCArchiveOpenCallback *openCallbackSpec = new YCArchiveOpenCallback;
CMyComPtr<IArchiveOpenCallback> openCallback(openCallbackSpec);
openCallbackSpec->PasswordIsDefined = false;
// get file type from suffix
int i = -1;
QFile filex(filePath);
if (!filex.open(QIODevice::ReadOnly))
return;
QByteArray magicNumber = filex.read(8); // read first 8 bytes
if (memcmp(magicNumber, rar, 6) == 0)
if (memcmp(magicNumber, rar5, 7) == 0)
i = 5;
else
i = 0;
else if (memcmp(magicNumber, zip, 2) == 0)
i = 1;
else if (memcmp(magicNumber, sevenz, 6) == 0)
i = 3;
else if (memcmp(magicNumber, arj, 2) == 0)
i = 4;
else {
filex.seek(257);
magicNumber = filex.read(8);
if (memcmp(magicNumber, tar, 5) == 0)
i = 2;
}
if (i == -1) // fallback code
{
QFileInfo fileinfo(filePath);
if (fileinfo.suffix() == "zip" || fileinfo.suffix() == "cbz") {
i = 1;
} else {
return;
}
}
#ifdef USE_UNICODE_FSTRING
if (!fileSpec->Open((LPCTSTR)filePath.toStdWString().c_str()))
#else
if (!fileSpec->Open((LPCTSTR)filePath.toStdString().c_str()))
#endif
{
qDebug() << "unable to load" + filePath;
return;
}
// GUID uuid = supportedFileFormats[i];
// qDebug() << "trying : " << uuid << endl;
if (szInterface->createObjectFunc(&supportedFileFormats[i], &IID_InArchive, (void **)&szInterface->archive) == S_OK) {
// qDebug() << "Can not open archive file : " + filePath << Qt::endl;
if (szInterface->archive->Open(file, 0, openCallback) == S_OK) {
valid = formatFound = true;
qDebug() << "Opened archive file : " + filePath << Qt::endl;
setupFilesNames();
return;
}
}
if (!formatFound) {
qDebug() << "Can not open archive" << Qt::endl;
}
}
}
CompressedArchive::~CompressedArchive()
{
// always close the archive!
if (szInterface->archive) {
szInterface->archive->Close();
}
delete szInterface;
delete sevenzLib;
}
bool CompressedArchive::loadFunctions()
{
// LOAD library
if (sevenzLib == nullptr) {
sevenzLib = YACReader::load7zLibrary();
}
if (!sevenzLib->load()) {
QLOG_ERROR() << "Error Loading 7z.dll : " + sevenzLib->errorString() << Qt::endl;
QCoreApplication::exit(700); // TODO yacreader_global can't be used here, it is GUI dependant, YACReader::SevenZNotFound
return false;
} else {
qDebug() << "Loading functions" << Qt::endl;
if ((szInterface->createObjectFunc = (CreateObjectFunc)sevenzLib->resolve("CreateObject")) == 0)
qDebug() << "fail loading function : CreateObject" << Qt::endl;
if ((szInterface->getMethodPropertyFunc = (GetMethodPropertyFunc)sevenzLib->resolve("GetMethodProperty")) == 0)
qDebug() << "fail loading function : GetMethodProperty" << Qt::endl;
if ((szInterface->getNumberOfMethodsFunc = (GetNumberOfMethodsFunc)sevenzLib->resolve("GetNumberOfMethods")) == 0)
qDebug() << "fail loading function : GetNumberOfMethods" << Qt::endl;
if ((szInterface->getNumberOfFormatsFunc = (GetNumberOfFormatsFunc)sevenzLib->resolve("GetNumberOfFormats")) == 0)
qDebug() << "fail loading function : GetNumberOfFormats" << Qt::endl;
if ((szInterface->getHandlerPropertyFunc = (GetHandlerPropertyFunc)sevenzLib->resolve("GetHandlerProperty")) == 0)
qDebug() << "fail loading function : GetHandlerProperty" << Qt::endl;
if ((szInterface->getHandlerPropertyFunc2 = (GetHandlerPropertyFunc2)sevenzLib->resolve("GetHandlerProperty2")) == 0)
qDebug() << "fail loading function : GetHandlerProperty2" << Qt::endl;
if ((szInterface->setLargePageModeFunc = (SetLargePageModeFunc)sevenzLib->resolve("SetLargePageMode")) == 0)
qDebug() << "fail loading function : SetLargePageMode" << Qt::endl;
}
return true;
}
void CompressedArchive::setupFilesNames()
{
quint32 numItems = getNumEntries();
quint32 p = 0;
for (quint32 i = 0; i < numItems; i++) {
// Get name of file
NWindows::NCOM::CPropVariant prop;
szInterface->archive->GetProperty(i, kpidIsDir, &prop);
bool isDir;
if (prop.vt == VT_BOOL)
isDir = VARIANT_BOOLToBool(prop.boolVal);
else if (prop.vt == VT_EMPTY)
isDir = false;
else
continue;
if (!isDir) {
szInterface->archive->GetProperty(i, kpidPath, &prop);
UString s = prop.bstrVal;
const wchar_t *chars = s.operator const wchar_t *();
files.append(QString::fromWCharArray(chars));
offsets.append(i);
indexesToPages.insert(i, p);
p++;
}
}
}
QVector<quint32> CompressedArchive::translateIndexes(const QVector<quint32> &indexes)
{
QVector<quint32> translatedIndexes;
foreach (quint32 i, indexes) {
if (i < (quint32)offsets.length())
translatedIndexes.append(offsets.at(i));
}
return translatedIndexes;
}
QList<QString> CompressedArchive::getFileNames()
{
return files;
}
bool CompressedArchive::isValid()
{
return valid;
}
bool CompressedArchive::toolsLoaded()
{
return tools;
}
int CompressedArchive::getNumFiles()
{
return files.length();
}
int CompressedArchive::getNumEntries()
{
quint32 numItems = 0;
szInterface->archive->GetNumberOfItems(&numItems);
return numItems;
}
QList<QByteArray> CompressedArchive::getAllData(const QVector<quint32> &indexes, ExtractDelegate *delegate)
{
YCArchiveExtractCallback *extractCallbackSpec = new YCArchiveExtractCallback(indexesToPages, true, delegate);
CMyComPtr<IArchiveExtractCallback> extractCallback(extractCallbackSpec);
extractCallbackSpec->Init(szInterface->archive, L""); // second parameter is output folder path
extractCallbackSpec->PasswordIsDefined = false;
QVector<quint32> currentIndexes = translateIndexes(indexes);
HRESULT result;
if (indexes.isEmpty())
result = szInterface->archive->Extract(NULL, -1, false, extractCallback);
else
result = szInterface->archive->Extract(currentIndexes.data(), currentIndexes.count(), false, extractCallback);
if (result != S_OK) {
qDebug() << "Extract Error" << Qt::endl;
}
return extractCallbackSpec->allFiles;
}
QByteArray CompressedArchive::getRawDataAtIndex(int index)
{
if (index >= 0 && index < getNumFiles()) {
YCArchiveExtractCallback *extractCallbackSpec = new YCArchiveExtractCallback(indexesToPages);
CMyComPtr<IArchiveExtractCallback> extractCallback(extractCallbackSpec);
extractCallbackSpec->Init(szInterface->archive, L""); // second parameter is output folder path
extractCallbackSpec->PasswordIsDefined = false;
UInt32 indices[1];
if (index < offsets.length())
indices[0] = offsets.at(index);
else
indices[0] = index;
HRESULT result = szInterface->archive->Extract(indices, 1, false, extractCallback);
if (result != S_OK) {
qDebug() << "Extract Error" << Qt::endl;
}
return QByteArray((char *)extractCallbackSpec->data, extractCallbackSpec->newFileSize);
}
return QByteArray();
}