-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTSevenZip.cpp
More file actions
519 lines (471 loc) · 11.4 KB
/
TSevenZip.cpp
File metadata and controls
519 lines (471 loc) · 11.4 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#include "stdafx.h"
#include "TSevenZip.h"
DEFINE_GUID(CLSID_CFormat7z,
0x23170F69, 0x40C1, 0x278A, 0x10, 0x00, 0x00, 0x01, 0x10, 0x07, 0x00, 0x00);
HRESULT VarToBool(tagPROPVARIANT& prop, bool* result)
{
if (prop.vt == VT_BOOL)
*result = (prop.boolVal != VARIANT_FALSE);
else if (prop.vt == VT_EMPTY)
*result = false;
else
return E_FAIL;
return S_OK;
}
HRESULT VarToUInt64(tagPROPVARIANT& prop, UInt64* result)
{
switch (prop.vt)
{
case VT_EMPTY:
case VT_NULL:
{
return E_FAIL;
}
case VT_I1:
case VT_I2:
{
*result = prop.iVal;
break;
}
case VT_INT:
case VT_I4:
{
*result = prop.lVal;
break;
}
case VT_I8:
{
*result = prop.hVal.QuadPart;
break;
}
case VT_UI1:
{
*result = prop.bVal;
break;
}
case VT_UI2:
{
*result = prop.uiVal;
break;
}
case VT_UINT:
case VT_UI4:
{
*result = prop.ulVal;
break;
}
case VT_UI8:
{
*result = prop.uhVal.QuadPart;
break;
}
default:
return E_FAIL;
}
return S_OK;
}
HRESULT VarToString(tagPROPVARIANT& prop, LPSTR* result)
{
switch (prop.vt)
{
case VT_EMPTY:
case VT_NULL:
{
return E_FAIL;
}
case VT_LPSTR:
{
size_t len = strlen(prop.pszVal);
*result = new char[len + 1];
strcpy_s(*result, len + 1, prop.pszVal);
break;
}
case VT_LPWSTR:
{
size_t len = lstrlenW(prop.pwszVal);
*result = new char[len + 1];
len = WideCharToMultiByte(CP_ACP, 0, prop.pwszVal, len, *result, len, nullptr, nullptr);
(*result)[len] = '\0';
break;
}
case VT_BSTR:
{
size_t len = lstrlenW(prop.bstrVal);
*result = new char[len + 1];
len = WideCharToMultiByte(CP_ACP, 0, prop.bstrVal, len, *result, len, nullptr, nullptr);
(*result)[len] = '\0';
SysFreeString(prop.bstrVal);
break;
}
default:
return E_FAIL;
}
return S_OK;
}
//////////////////////////////////////////////////////////////
// Input file stream class
class CInFileStream : public IInStream, public IStreamGetSize
{
private:
long m_refCount;
HANDLE m_fileStream;
public:
CInFileStream()
: m_refCount(0)
, m_fileStream(INVALID_HANDLE_VALUE)
{
}
virtual ~CInFileStream()
{
if (m_fileStream != INVALID_HANDLE_VALUE)
{
CloseHandle(m_fileStream);
}
}
STDMETHOD(Open)(LPCSTR lpFileName)
{
m_fileStream = CreateFile(lpFileName, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (m_fileStream != INVALID_HANDLE_VALUE)
{
return S_OK;
}
return HRESULT_FROM_WIN32(GetLastError());
}
STDMETHOD(QueryInterface)(REFIID iid, void** ppvObject)
{
if (iid == __uuidof(IUnknown))
{
*ppvObject = reinterpret_cast<IUnknown*>(this);
AddRef();
return S_OK;
}
if (iid == IID_ISequentialInStream)
{
*ppvObject = static_cast<ISequentialInStream*>(this);
AddRef();
return S_OK;
}
if (iid == IID_IInStream)
{
*ppvObject = static_cast<IInStream*>(this);
AddRef();
return S_OK;
}
if (iid == IID_IStreamGetSize)
{
*ppvObject = static_cast<IStreamGetSize*>(this);
AddRef();
return S_OK;
}
return E_NOINTERFACE;
}
STDMETHOD_(ULONG, AddRef)()
{
return static_cast<ULONG>(InterlockedIncrement(&m_refCount));
}
STDMETHOD_(ULONG, Release)()
{
ULONG res = static_cast<ULONG>(InterlockedDecrement(&m_refCount));
if (res == 0)
{
delete this;
}
return res;
}
// ISequentialInStream
STDMETHOD(Read)(void* data, UInt32 size, UInt32* processedSize)
{
if (m_fileStream != INVALID_HANDLE_VALUE)
{
if (ReadFile(m_fileStream, data, size, (LPDWORD)processedSize, nullptr))
{
return S_OK;
}
return HRESULT_FROM_WIN32(GetLastError());
}
return S_FALSE;
}
// IInStream
STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64* newPosition)
{
if (m_fileStream != INVALID_HANDLE_VALUE)
{
LARGE_INTEGER liDistanceToMove;
liDistanceToMove.QuadPart = offset;
if (SetFilePointerEx(m_fileStream, liDistanceToMove, (PLARGE_INTEGER)newPosition, seekOrigin))
{
return S_OK;
}
return HRESULT_FROM_WIN32(GetLastError());
}
return S_FALSE;
}
// IStreamGetSize
STDMETHOD(GetSize)(UInt64* size)
{
if (m_fileStream != INVALID_HANDLE_VALUE)
{
LARGE_INTEGER lpDistanceToMove;
if (GetFileSizeEx(m_fileStream, (PLARGE_INTEGER)size))
{
return S_OK;
}
return HRESULT_FROM_WIN32(GetLastError());
}
return S_FALSE;
}
};
//////////////////////////////////////////////////////////////
// Archive Open callback class
class CArchiveOpenCallback :
public IArchiveOpenCallback,
public ICryptoGetTextPassword,
public CMyUnknownImp
{
private:
BSTR Password;
bool PasswordIsDefined;
public:
MY_UNKNOWN_IMP1(ICryptoGetTextPassword)
STDMETHOD(SetTotal)(const UInt64* files, const UInt64* bytes)
{
return S_OK;
}
STDMETHOD(SetCompleted)(const UInt64* files, const UInt64* bytes)
{
return S_OK;
}
STDMETHOD(CryptoGetTextPassword)(BSTR* password)
{
if (!PasswordIsDefined)
{
// You can ask real password here from user
// Password = GetPassword(OutStream);
// PasswordIsDefined = true;
return E_ABORT;
}
return StringToBstr(Password, password);
}
CArchiveOpenCallback() : Password(nullptr), PasswordIsDefined(false) {}
};
/////////////////////////////////////////////////////////////////////////////
//FUNCTIONS PRIVATE
/////////////////////////////////////////////////////////////////////////////
HRESULT TSevenZip::ReadPropBool(UInt32 index, PROPID propID, bool* result)
{
tagPROPVARIANT prop;
memset(&prop, 0, sizeof(tagPROPVARIANT));
RINOK(archive->GetProperty(index, propID, &prop));
return VarToBool(prop, result);
}
HRESULT TSevenZip::ReadPropUInt64(UInt32 index, PROPID propID, UInt64* result)
{
tagPROPVARIANT prop;
memset(&prop, 0, sizeof(tagPROPVARIANT));
RINOK(archive->GetProperty(index, propID, &prop));
return VarToUInt64(prop, result);
}
HRESULT TSevenZip::ReadPropString(UInt32 index, PROPID propID, LPSTR* result)
{
tagPROPVARIANT prop;
memset(&prop, 0, sizeof(tagPROPVARIANT));
RINOK(archive->GetProperty(index, propID, &prop));
return VarToString(prop, result);
}
HRESULT TSevenZip::ReadArcPropBool(PROPID propID, bool* result)
{
tagPROPVARIANT prop;
memset(&prop, 0, sizeof(tagPROPVARIANT));
RINOK(archive->GetArchiveProperty(propID, &prop));
return VarToBool(prop, result);
}
HRESULT TSevenZip::ReadArcPropUInt64(PROPID propID, UInt64* result)
{
tagPROPVARIANT prop;
memset(&prop, 0, sizeof(tagPROPVARIANT));
RINOK(archive->GetArchiveProperty(propID, &prop));
return VarToUInt64(prop, result);
}
HRESULT TSevenZip::ReadArcPropString(PROPID propID, LPSTR* result)
{
tagPROPVARIANT prop;
memset(&prop, 0, sizeof(tagPROPVARIANT));
RINOK(archive->GetArchiveProperty(propID, &prop));
return VarToString(prop, result);
}
HRESULT TSevenZip::LoadPlugin()
{
#ifdef _M_AMD64
const LPCSTR lpLibFileName = "TC7Z64.DLL";
#else
const LPCSTR lpLibFileName = "TC7Z.DLL";
#endif
char lpLibFileName64[MAX_PATH] = { 0 };
char lpCommanderPath[MAX_PATH] = { 0 };
char lpLibFilePath64[MAX_PATH] = "%COMMANDER_PATH%";
if (!::ExpandEnvironmentStrings(lpLibFilePath64, lpCommanderPath, MAX_PATH))
{
return HRESULT_FROM_WIN32(GetLastError());
}
snprintf(lpLibFileName64, sizeof(lpLibFileName64), "%s\\%s", lpCommanderPath, lpLibFileName);
if (GetFileAttributes(lpLibFileName64) != INVALID_FILE_ATTRIBUTES)
{
m_hLibrary = LoadLibrary(lpLibFileName64);
}
else
{
snprintf(lpLibFileName64, sizeof(lpLibFileName64), "%s\\7z.dll", lpCommanderPath);
m_hLibrary = LoadLibrary(lpLibFileName64);
}
if (m_hLibrary == 0)
{
return HRESULT_FROM_WIN32(GetLastError());
}
Func_CreateObject createObjectFunc = (Func_CreateObject)GetProcAddress(m_hLibrary, "CreateObject");
if (!createObjectFunc)
{
return HRESULT_FROM_WIN32(GetLastError());
}
RINOK(createObjectFunc(&CLSID_CFormat7z, &IID_IInArchive, (void**)&archive));
}
/////////////////////////////////////////////////////////////////////////////
//FUNCTIONS PUBLIC
/////////////////////////////////////////////////////////////////////////////
TSevenZip::TSevenZip(DataForArchive& dfa, LanguageMessages& langmsg, LanguageResults& langres, char* namearch) :
TArchive(dfa, langmsg, langres, namearch), m_hLibrary(0)
{
LoadPlugin();
}
TSevenZip::~TSevenZip()
{
archive.Release();
if (m_hLibrary) FreeLibrary(m_hLibrary);
}
int TSevenZip::TestFile(char* path)
{
InitialParametrs();
if (!archive)
{
if (m_DetailLF == 2)
m_LogFile->WriteMessage(m_LangMsg.SignatureArchive, TERROR_DLL);
return TERROR_DLL;
}
CInFileStream* fileSpec = new CInFileStream;
file = fileSpec;
if (fileSpec->Open(path) != S_OK)
{
return TERROR_OPEN_FILE;
}
UInt64 newPosition;
UInt32 processedSize;
WORD EXECUTABLE_ID = 0;
if (fileSpec->Read(&EXECUTABLE_ID, sizeof(EXECUTABLE_ID), &processedSize) != S_OK)
{
return TERROR_READ_FILE;
}
if ((fileSpec->Seek(0, STREAM_SEEK_SET, &newPosition) != S_OK) || (newPosition))
{
return TERROR_SEEK_FILE;
}
if (EXECUTABLE_ID == 'ZM')
{
m_SfxModule = 1;
if (!m_CheckSFX) return TERROR_FORMAT;
}
fileSpec->GetSize((UInt64 *)&m_ArchiveSize);
{
CArchiveOpenCallback* openCallbackSpec = new CArchiveOpenCallback;
CMyComPtr<IArchiveOpenCallback> openCallback(openCallbackSpec);
const UInt64 scanSize = 1 << 23;
if (archive->Open(file, &scanSize, openCallback) != S_OK)
{
if (m_DetailLF == 2)
m_LogFile->WriteMessage(m_LangMsg.SignatureArchive, TERROR_FORMAT);
return TERROR_FORMAT;
}
}
return TMESSAGE_OK;
}
int TSevenZip::AnalyzeInfoOfArc(char* path)
{
bool value;
UInt64 size;
LPSTR str = NULL;
UInt32 numItems = 0;
if (ReadArcPropBool(kpidSolid, &value) == S_OK)
{
m_Solid = value;
}
if (ReadArcPropUInt64(kpidOffset, &size) == S_OK)
{
m_Offset = size;
if (m_SfxModule) m_SfxModule = size;
}
if (ReadArcPropUInt64(kpidNumBlocks, &size) == S_OK)
{
m_NumBlocks = size;
}
if (ReadArcPropUInt64(kpidHeadersSize, &size) == S_OK)
{
m_HeadersSize = size;
}
if (ReadArcPropString(kpidMethod, &str) == S_OK)
{
if (m_pMethodPack) { delete[] m_pMethodPack; }
m_pMethodPack = str;
}
if (archive->GetNumberOfItems(&numItems) != S_OK)
{
return TERROR_READ_FILE;
}
for (UInt32 i = 0; i < numItems; i++)
{
if (m_BeginThread)
{
//net lis sobitiya zavershit' rabotu potoka
if (::WaitForSingleObject(m_hEventEnd, 0) == WAIT_OBJECT_0)
{
return TTERMINATE_THREAD;
}
}
// Get uncompressed size of file
if (ReadPropUInt64(i, kpidSize, &size) == S_OK)
{
m_UnpackSizeFiles += size;
}
// Get compressed size of file
if (ReadPropUInt64(i, kpidPackSize, &size) == S_OK)
{
m_PackSizeFiles += size;
}
if (ReadPropBool(i, kpidEncrypted, &value) == S_OK)
{
if (value) m_Password = value;
}
if ((ReadPropBool(i, kpidIsDir, &value) == S_OK) && (value))
{
m_BlockType = 2;
m_NumberFolders++;
}
else
{
m_BlockType = 1;
m_NumberFiles++;
}
if (m_DetailLF == 2)
{
switch (m_BlockType)
{
case 1: m_LogFile->WriteMessage(m_LangMsg.BlockFile, MSG);
break;
case 2: m_LogFile->WriteMessage(m_LangMsg.BlockFolder, MSG);
break;
}
}
}
CountRatioArchiveSize();
CountRatioPackFileSize();
archive->Close();
return TMESSAGE_OK;
}