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
|
// GUI.cpp
#include "StdAfx.h"
#include "../../../Common/MyWindows.h"
#include <shlwapi.h>
#include "../../../../C/Alloc.h"
#include "../../../Common/MyInitGuid.h"
#include "../../../Common/CommandLineParser.h"
#include "../../../Common/IntToString.h"
#include "../../../Common/MyException.h"
#include "../../../Common/StringConvert.h"
#include "../../../Windows/FileDir.h"
#include "../../../Windows/NtCheck.h"
#ifdef _WIN32
#include "../../../Windows/MemoryLock.h"
#endif
#include "../Common/ArchiveCommandLine.h"
#include "../Common/ExitCode.h"
#include "../FileManager/StringUtils.h"
#include "../FileManager/MyWindowsNew.h"
#include "BenchmarkDialog.h"
#include "ExtractGUI.h"
#include "HashGUI.h"
#include "UpdateGUI.h"
#include "ExtractRes.h"
using namespace NWindows;
HINSTANCE g_hInstance;
#ifndef _UNICODE
#endif
#ifndef UNDER_CE
DWORD g_ComCtl32Version;
static DWORD GetDllVersion(LPCTSTR dllName)
{
DWORD dwVersion = 0;
HINSTANCE hinstDll = LoadLibrary(dllName);
if (hinstDll)
{
DLLGETVERSIONPROC pDllGetVersion = (DLLGETVERSIONPROC)GetProcAddress(hinstDll, "DllGetVersion");
if (pDllGetVersion)
{
DLLVERSIONINFO dvi;
ZeroMemory(&dvi, sizeof(dvi));
dvi.cbSize = sizeof(dvi);
HRESULT hr = (*pDllGetVersion)(&dvi);
if (SUCCEEDED(hr))
dwVersion = MAKELONG(dvi.dwMinorVersion, dvi.dwMajorVersion);
}
FreeLibrary(hinstDll);
}
return dwVersion;
}
#endif
bool g_LVN_ITEMACTIVATE_Support = true;
static void ErrorMessage(LPCWSTR message)
{
MessageBoxW(NULL, message, L"7-Zip", MB_ICONERROR | MB_OK);
}
static void ErrorLangMessage(UINT resourceID)
{
ErrorMessage(LangString(resourceID));
}
static const char *kNoFormats = "7-Zip cannot find the code that works with archives.";
static int ShowMemErrorMessage()
{
ErrorLangMessage(IDS_MEM_ERROR);
return NExitCode::kMemoryError;
}
static int ShowSysErrorMessage(DWORD errorCode)
{
if (errorCode == E_OUTOFMEMORY)
return ShowMemErrorMessage();
ErrorMessage(HResultToMessage(errorCode));
return NExitCode::kFatalError;
}
static void ThrowException_if_Error(HRESULT res)
{
if (res != S_OK)
throw CSystemException(res);
}
static int Main2()
{
UStringVector commandStrings;
NCommandLineParser::SplitCommandLine(GetCommandLineW(), commandStrings);
#ifndef UNDER_CE
if (commandStrings.Size() > 0)
commandStrings.Delete(0);
#endif
if (commandStrings.Size() == 0)
{
MessageBoxW(0, L"Specify command", L"7-Zip", 0);
return 0;
}
CArcCmdLineOptions options;
CArcCmdLineParser parser;
parser.Parse1(commandStrings, options);
parser.Parse2(options);
#if defined(_WIN32) && !defined(UNDER_CE)
NSecurity::EnablePrivilege_SymLink();
#ifdef _7ZIP_LARGE_PAGES
if (options.LargePages)
NSecurity::EnablePrivilege_LockMemory();
#endif
#endif
CREATE_CODECS_OBJECT
codecs->CaseSensitiveChange = options.CaseSensitiveChange;
codecs->CaseSensitive = options.CaseSensitive;
ThrowException_if_Error(codecs->Load());
bool isExtractGroupCommand = options.Command.IsFromExtractGroup();
if (codecs->Formats.Size() == 0 &&
(isExtractGroupCommand
|| options.Command.IsFromUpdateGroup()))
{
#ifdef EXTERNAL_CODECS
if (!codecs->MainDll_ErrorPath.IsEmpty())
{
UString s = L"7-Zip cannot load module ";
s += fs2us(codecs->MainDll_ErrorPath);
throw s;
}
#endif
throw kNoFormats;
}
CObjectVector<COpenType> formatIndices;
if (!ParseOpenTypes(*codecs, options.ArcType, formatIndices))
{
ErrorLangMessage(IDS_UNSUPPORTED_ARCHIVE_TYPE);
return NExitCode::kFatalError;
}
CIntVector excludedFormatIndices;
FOR_VECTOR (k, options.ExcludedArcTypes)
{
CIntVector tempIndices;
if (!codecs->FindFormatForArchiveType(options.ExcludedArcTypes[k], tempIndices)
|| tempIndices.Size() != 1)
{
ErrorLangMessage(IDS_UNSUPPORTED_ARCHIVE_TYPE);
return NExitCode::kFatalError;
}
excludedFormatIndices.AddToUniqueSorted(tempIndices[0]);
// excludedFormatIndices.Sort();
}
#ifdef EXTERNAL_CODECS
if (isExtractGroupCommand
|| options.Command.CommandType == NCommandType::kHash
|| options.Command.CommandType == NCommandType::kBenchmark)
ThrowException_if_Error(__externalCodecs.Load());
#endif
if (options.Command.CommandType == NCommandType::kBenchmark)
{
HRESULT res = Benchmark(EXTERNAL_CODECS_VARS_L options.Properties);
/*
if (res == S_FALSE)
{
stdStream << "\nDecoding Error\n";
return NExitCode::kFatalError;
}
*/
ThrowException_if_Error(res);
}
else if (isExtractGroupCommand)
{
UStringVector ArchivePathsSorted;
UStringVector ArchivePathsFullSorted;
CExtractCallbackImp *ecs = new CExtractCallbackImp;
CMyComPtr<IFolderArchiveExtractCallback> extractCallback = ecs;
#ifndef _NO_CRYPTO
ecs->PasswordIsDefined = options.PasswordEnabled;
ecs->Password = options.Password;
#endif
ecs->Init();
CExtractOptions eo;
(CExtractOptionsBase &)eo = options.ExtractOptions;
eo.StdInMode = options.StdInMode;
eo.StdOutMode = options.StdOutMode;
eo.YesToAll = options.YesToAll;
eo.TestMode = options.Command.IsTestCommand();
#ifndef _SFX
eo.Properties = options.Properties;
#endif
bool messageWasDisplayed = false;
#ifndef _SFX
CHashBundle hb;
CHashBundle *hb_ptr = NULL;
if (!options.HashMethods.IsEmpty())
{
hb_ptr = &hb;
ThrowException_if_Error(hb.SetMethods(EXTERNAL_CODECS_VARS_L options.HashMethods));
}
#endif
{
CDirItemsStat st;
HRESULT hresultMain = EnumerateDirItemsAndSort(
options.arcCensor,
NWildcard::k_RelatPath,
UString(), // addPathPrefix
ArchivePathsSorted,
ArchivePathsFullSorted,
st,
NULL // &scan: change it!!!!
);
if (hresultMain != S_OK)
{
/*
if (hresultMain != E_ABORT && messageWasDisplayed)
return NExitCode::kFatalError;
*/
throw CSystemException(hresultMain);
}
}
HRESULT result = ExtractGUI(codecs,
formatIndices, excludedFormatIndices,
ArchivePathsSorted,
ArchivePathsFullSorted,
options.Censor.Pairs.Front().Head,
eo,
#ifndef _SFX
hb_ptr,
#endif
options.ShowDialog, messageWasDisplayed, ecs);
if (result != S_OK)
{
if (result != E_ABORT && messageWasDisplayed)
return NExitCode::kFatalError;
throw CSystemException(result);
}
if (!ecs->IsOK())
return NExitCode::kFatalError;
}
else if (options.Command.IsFromUpdateGroup())
{
#ifndef _NO_CRYPTO
bool passwordIsDefined = options.PasswordEnabled && !options.Password.IsEmpty();
#endif
CUpdateCallbackGUI callback;
// callback.EnablePercents = options.EnablePercents;
#ifndef _NO_CRYPTO
callback.PasswordIsDefined = passwordIsDefined;
callback.AskPassword = options.PasswordEnabled && options.Password.IsEmpty();
callback.Password = options.Password;
#endif
// callback.StdOutMode = options.UpdateOptions.StdOutMode;
callback.Init();
if (!options.UpdateOptions.InitFormatIndex(codecs, formatIndices, options.ArchiveName) ||
!options.UpdateOptions.SetArcPath(codecs, options.ArchiveName))
{
ErrorLangMessage(IDS_UPDATE_NOT_SUPPORTED);
return NExitCode::kFatalError;
}
bool messageWasDisplayed = false;
HRESULT result = UpdateGUI(
codecs, formatIndices,
options.ArchiveName,
options.Censor,
options.UpdateOptions,
options.ShowDialog,
messageWasDisplayed,
&callback);
if (result != S_OK)
{
if (result != E_ABORT && messageWasDisplayed)
return NExitCode::kFatalError;
throw CSystemException(result);
}
if (callback.FailedFiles.Size() > 0)
{
if (!messageWasDisplayed)
throw CSystemException(E_FAIL);
return NExitCode::kWarning;
}
}
else if (options.Command.CommandType == NCommandType::kHash)
{
bool messageWasDisplayed = false;
HRESULT result = HashCalcGUI(EXTERNAL_CODECS_VARS_L
options.Censor, options.HashOptions, messageWasDisplayed);
if (result != S_OK)
{
if (result != E_ABORT && messageWasDisplayed)
return NExitCode::kFatalError;
throw CSystemException(result);
}
/*
if (callback.FailedFiles.Size() > 0)
{
if (!messageWasDisplayed)
throw CSystemException(E_FAIL);
return NExitCode::kWarning;
}
*/
}
else
{
throw "Unsupported command";
}
return 0;
}
#define NT_CHECK_FAIL_ACTION ErrorMessage(L"Unsupported Windows version"); return NExitCode::kFatalError;
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE /* hPrevInstance */,
#ifdef UNDER_CE
LPWSTR
#else
LPSTR
#endif
/* lpCmdLine */, int /* nCmdShow */)
{
g_hInstance = hInstance;
#ifdef _WIN32
NT_CHECK
SetLargePageSize();
#endif
InitCommonControls();
#ifndef UNDER_CE
g_ComCtl32Version = ::GetDllVersion(TEXT("comctl32.dll"));
g_LVN_ITEMACTIVATE_Support = (g_ComCtl32Version >= MAKELONG(71, 4));
#endif
// OleInitialize is required for ProgressBar in TaskBar.
#ifndef UNDER_CE
OleInitialize(NULL);
#endif
LoadLangOneTime();
// setlocale(LC_COLLATE, ".ACP");
try
{
return Main2();
}
catch(const CNewException &)
{
return ShowMemErrorMessage();
}
catch(const CArcCmdLineException &e)
{
ErrorMessage(e);
return NExitCode::kUserError;
}
catch(const CSystemException &systemError)
{
if (systemError.ErrorCode == E_ABORT)
return NExitCode::kUserBreak;
return ShowSysErrorMessage(systemError.ErrorCode);
}
catch(const UString &s)
{
ErrorMessage(s);
return NExitCode::kFatalError;
}
catch(const AString &s)
{
ErrorMessage(GetUnicodeString(s));
return NExitCode::kFatalError;
}
catch(const wchar_t *s)
{
ErrorMessage(s);
return NExitCode::kFatalError;
}
catch(const char *s)
{
ErrorMessage(GetUnicodeString(s));
return NExitCode::kFatalError;
}
catch(int v)
{
wchar_t s[32];
ConvertUInt32ToString(v, s);
ErrorMessage(UString(L"Error: ") + s);
return NExitCode::kFatalError;
}
catch(...)
{
ErrorMessage(L"Unknown error");
return NExitCode::kFatalError;
}
}
|