other-licenses/7zstub/src/7zip/Bundles/SFXSetup-moz/Main.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 // Main.cpp
michael@0 2
michael@0 3 #include "StdAfx.h"
michael@0 4
michael@0 5 #include <initguid.h>
michael@0 6
michael@0 7 #include "Common/StringConvert.h"
michael@0 8 #include "Common/Random.h"
michael@0 9 #include "Common/TextConfig.h"
michael@0 10 #include "Common/CommandLineParser.h"
michael@0 11
michael@0 12 #include "Windows/FileDir.h"
michael@0 13 #include "Windows/FileIO.h"
michael@0 14 #include "Windows/FileFind.h"
michael@0 15 #include "Windows/FileName.h"
michael@0 16 #include "Windows/DLL.h"
michael@0 17 #include "Windows/ResourceString.h"
michael@0 18
michael@0 19 #include "../../IPassword.h"
michael@0 20 #include "../../ICoder.h"
michael@0 21 #include "../../Archive/IArchive.h"
michael@0 22 #include "../../UI/Explorer/MyMessages.h"
michael@0 23
michael@0 24 // #include "../../UI/GUI/ExtractGUI.h"
michael@0 25
michael@0 26 #include "ExtractEngine.h"
michael@0 27
michael@0 28 #include "resource.h"
michael@0 29
michael@0 30 using namespace NWindows;
michael@0 31
michael@0 32 HINSTANCE g_hInstance;
michael@0 33
michael@0 34 static LPCTSTR kTempDirPrefix = TEXT("7zS");
michael@0 35
michael@0 36 #define _SHELL_EXECUTE
michael@0 37
michael@0 38 static bool ReadDataString(LPCWSTR fileName, LPCSTR startID,
michael@0 39 LPCSTR endID, AString &stringResult)
michael@0 40 {
michael@0 41 stringResult.Empty();
michael@0 42 NFile::NIO::CInFile inFile;
michael@0 43 if (!inFile.Open(fileName))
michael@0 44 return false;
michael@0 45 const int kBufferSize = (1 << 12);
michael@0 46
michael@0 47 Byte buffer[kBufferSize];
michael@0 48 int signatureStartSize = lstrlenA(startID);
michael@0 49 int signatureEndSize = lstrlenA(endID);
michael@0 50
michael@0 51 UInt32 numBytesPrev = 0;
michael@0 52 bool writeMode = false;
michael@0 53 UInt64 posTotal = 0;
michael@0 54 while(true)
michael@0 55 {
michael@0 56 if (posTotal > (1 << 20))
michael@0 57 return (stringResult.IsEmpty());
michael@0 58 UInt32 numReadBytes = kBufferSize - numBytesPrev;
michael@0 59 UInt32 processedSize;
michael@0 60 if (!inFile.Read(buffer + numBytesPrev, numReadBytes, processedSize))
michael@0 61 return false;
michael@0 62 if (processedSize == 0)
michael@0 63 return true;
michael@0 64 UInt32 numBytesInBuffer = numBytesPrev + processedSize;
michael@0 65 UInt32 pos = 0;
michael@0 66 while (true)
michael@0 67 {
michael@0 68 if (writeMode)
michael@0 69 {
michael@0 70 if (pos > numBytesInBuffer - signatureEndSize)
michael@0 71 break;
michael@0 72 if (memcmp(buffer + pos, endID, signatureEndSize) == 0)
michael@0 73 return true;
michael@0 74 char b = buffer[pos];
michael@0 75 if (b == 0)
michael@0 76 return false;
michael@0 77 stringResult += b;
michael@0 78 pos++;
michael@0 79 }
michael@0 80 else
michael@0 81 {
michael@0 82 if (pos > numBytesInBuffer - signatureStartSize)
michael@0 83 break;
michael@0 84 if (memcmp(buffer + pos, startID, signatureStartSize) == 0)
michael@0 85 {
michael@0 86 writeMode = true;
michael@0 87 pos += signatureStartSize;
michael@0 88 }
michael@0 89 else
michael@0 90 pos++;
michael@0 91 }
michael@0 92 }
michael@0 93 numBytesPrev = numBytesInBuffer - pos;
michael@0 94 posTotal += pos;
michael@0 95 memmove(buffer, buffer + pos, numBytesPrev);
michael@0 96 }
michael@0 97 }
michael@0 98
michael@0 99 static char kStartID[] = ",!@Install@!UTF-8!";
michael@0 100 static char kEndID[] = ",!@InstallEnd@!";
michael@0 101
michael@0 102 class CInstallIDInit
michael@0 103 {
michael@0 104 public:
michael@0 105 CInstallIDInit()
michael@0 106 {
michael@0 107 kStartID[0] = ';';
michael@0 108 kEndID[0] = ';';
michael@0 109 };
michael@0 110 } g_CInstallIDInit;
michael@0 111
michael@0 112
michael@0 113 class CCurrentDirRestorer
michael@0 114 {
michael@0 115 CSysString m_CurrentDirectory;
michael@0 116 public:
michael@0 117 CCurrentDirRestorer()
michael@0 118 { NFile::NDirectory::MyGetCurrentDirectory(m_CurrentDirectory); }
michael@0 119 ~CCurrentDirRestorer()
michael@0 120 { RestoreDirectory();}
michael@0 121 bool RestoreDirectory()
michael@0 122 { return BOOLToBool(::SetCurrentDirectory(m_CurrentDirectory)); }
michael@0 123 };
michael@0 124
michael@0 125 #ifndef _UNICODE
michael@0 126 bool g_IsNT = false;
michael@0 127 static inline bool IsItWindowsNT()
michael@0 128 {
michael@0 129 OSVERSIONINFO versionInfo;
michael@0 130 versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
michael@0 131 if (!::GetVersionEx(&versionInfo))
michael@0 132 return false;
michael@0 133 return (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT);
michael@0 134 }
michael@0 135 #endif
michael@0 136
michael@0 137 // Delayed load libraries are loaded when the first symbol is used.
michael@0 138 // The following ensures that we load the delayed loaded libraries from the
michael@0 139 // system directory.
michael@0 140 struct AutoLoadSystemDependencies
michael@0 141 {
michael@0 142 AutoLoadSystemDependencies()
michael@0 143 {
michael@0 144 static LPCWSTR delayDLLs[] = { L"dwmapi.dll", L"cryptbase.dll",
michael@0 145 L"SHCore.dll", L"uxtheme.dll",
michael@0 146 L"oleacc.dll", L"apphelp.dll" };
michael@0 147 WCHAR systemDirectory[MAX_PATH + 1] = { L'\0' };
michael@0 148 // If GetSystemDirectory fails we accept that we'll load the DLLs from the
michael@0 149 // normal search path.
michael@0 150 GetSystemDirectoryW(systemDirectory, MAX_PATH + 1);
michael@0 151 size_t systemDirLen = wcslen(systemDirectory);
michael@0 152
michael@0 153 // Make the system directory path terminate with a slash
michael@0 154 if (systemDirectory[systemDirLen - 1] != L'\\' && systemDirLen) {
michael@0 155 systemDirectory[systemDirLen] = L'\\';
michael@0 156 ++systemDirLen;
michael@0 157 // No need to re-NULL terminate
michael@0 158 }
michael@0 159
michael@0 160 // For each known DLL ensure it is loaded from the system32 directory
michael@0 161 for (size_t i = 0; i < sizeof(delayDLLs) / sizeof(delayDLLs[0]); ++i) {
michael@0 162 size_t fileLen = wcslen(delayDLLs[i]);
michael@0 163 wcsncpy(systemDirectory + systemDirLen, delayDLLs[i],
michael@0 164 MAX_PATH - systemDirLen);
michael@0 165 if (systemDirLen + fileLen <= MAX_PATH) {
michael@0 166 systemDirectory[systemDirLen + fileLen] = L'\0';
michael@0 167 } else {
michael@0 168 systemDirectory[MAX_PATH] = L'\0';
michael@0 169 }
michael@0 170 LPCWSTR fullModulePath = systemDirectory; // just for code readability
michael@0 171 LoadLibraryW(fullModulePath);
michael@0 172 }
michael@0 173 }
michael@0 174 } loadDLLs;
michael@0 175
michael@0 176 BOOL
michael@0 177 RemoveCurrentDirFromSearchPath()
michael@0 178 {
michael@0 179 // kernel32.dll is in the knownDLL list so it is safe to load without a full path
michael@0 180 HMODULE kernel32 = LoadLibraryW(L"kernel32.dll");
michael@0 181 if (!kernel32) {
michael@0 182 return FALSE;
michael@0 183 }
michael@0 184
michael@0 185 typedef BOOL (WINAPI *SetDllDirectoryType)(LPCWSTR);
michael@0 186 SetDllDirectoryType SetDllDirectoryFn =
michael@0 187 (SetDllDirectoryType)GetProcAddress(kernel32, "SetDllDirectoryW");
michael@0 188 if (!SetDllDirectoryFn) {
michael@0 189 FreeLibrary(kernel32);
michael@0 190 return FALSE;
michael@0 191 }
michael@0 192
michael@0 193 // If this call fails we can't do much about it, so ignore it.
michael@0 194 // It is unlikely to fail and this is just a precaution anyway.
michael@0 195 SetDllDirectoryFn(L"");
michael@0 196 FreeLibrary(kernel32);
michael@0 197 return TRUE;
michael@0 198 }
michael@0 199
michael@0 200 int APIENTRY WinMain(
michael@0 201 HINSTANCE hInstance,
michael@0 202 HINSTANCE hPrevInstance,
michael@0 203 LPSTR lpCmdLine,
michael@0 204 int nCmdShow)
michael@0 205 {
michael@0 206 // Disable current directory from being in the search path.
michael@0 207 // This call does not help with implicitly loaded DLLs.
michael@0 208 if (!RemoveCurrentDirFromSearchPath()) {
michael@0 209 WCHAR minOSTitle[512] = { '\0' };
michael@0 210 WCHAR minOSText[512] = { '\0' };
michael@0 211 LoadStringW(NULL, IDS_MIN_OS_TITLE, minOSTitle,
michael@0 212 sizeof(minOSTitle) / sizeof(minOSTitle[0]));
michael@0 213 LoadStringW(NULL, IDS_MIN_OS_TEXT, minOSText,
michael@0 214 sizeof(minOSText) / sizeof(minOSText[0]));
michael@0 215 MessageBoxW(NULL, minOSText, minOSTitle, MB_OK | MB_ICONERROR);
michael@0 216 return 1;
michael@0 217 }
michael@0 218
michael@0 219 g_hInstance = (HINSTANCE)hInstance;
michael@0 220 #ifndef _UNICODE
michael@0 221 g_IsNT = IsItWindowsNT();
michael@0 222 #endif
michael@0 223 InitCommonControls();
michael@0 224
michael@0 225 UString archiveName, switches;
michael@0 226 #ifdef _SHELL_EXECUTE
michael@0 227 UString executeFile, executeParameters;
michael@0 228 #endif
michael@0 229 NCommandLineParser::SplitCommandLine(GetCommandLineW(), archiveName, switches);
michael@0 230
michael@0 231 UString fullPath;
michael@0 232 NDLL::MyGetModuleFileName(g_hInstance, fullPath);
michael@0 233
michael@0 234 switches.Trim();
michael@0 235 bool assumeYes = false;
michael@0 236 if (switches.Left(2).CompareNoCase(UString(L"-y")) == 0)
michael@0 237 {
michael@0 238 assumeYes = true;
michael@0 239 switches = switches.Mid(2);
michael@0 240 switches.Trim();
michael@0 241 }
michael@0 242
michael@0 243 /* BEGIN Mozilla customizations */
michael@0 244 bool showProgress = true;
michael@0 245 bool extractOnly = false;
michael@0 246 if (switches.Left(3).CompareNoCase(UString(L"-ms")) == 0 ||
michael@0 247 switches.Left(4).CompareNoCase(UString(L"/ini")) == 0 ||
michael@0 248 switches.Left(2).CompareNoCase(UString(L"/s")) == 0) {
michael@0 249 showProgress = false;
michael@0 250 } else if (switches.Left(12).CompareNoCase(UString(L"/extractdir=")) == 0) {
michael@0 251 assumeYes = true;
michael@0 252 showProgress = false;
michael@0 253 extractOnly = true;
michael@0 254 }
michael@0 255 /* END Mozilla customizations */
michael@0 256
michael@0 257 AString config;
michael@0 258 if (!ReadDataString(fullPath, kStartID, kEndID, config))
michael@0 259 {
michael@0 260 if (!assumeYes)
michael@0 261 MyMessageBox(L"Can't load config info");
michael@0 262 return 1;
michael@0 263 }
michael@0 264
michael@0 265 UString dirPrefix = L".\\";
michael@0 266 UString appLaunched;
michael@0 267 if (!config.IsEmpty())
michael@0 268 {
michael@0 269 CObjectVector<CTextConfigPair> pairs;
michael@0 270 if (!GetTextConfig(config, pairs))
michael@0 271 {
michael@0 272 if (!assumeYes)
michael@0 273 MyMessageBox(L"Config failed");
michael@0 274 return 1;
michael@0 275 }
michael@0 276 UString friendlyName = GetTextConfigValue(pairs, L"Title");
michael@0 277 UString installPrompt = GetTextConfigValue(pairs, L"BeginPrompt");
michael@0 278 UString progress = GetTextConfigValue(pairs, L"Progress");
michael@0 279 if (progress.CompareNoCase(L"no") == 0)
michael@0 280 showProgress = false;
michael@0 281 int index = FindTextConfigItem(pairs, L"Directory");
michael@0 282 if (index >= 0)
michael@0 283 dirPrefix = pairs[index].String;
michael@0 284 if (!installPrompt.IsEmpty() && !assumeYes)
michael@0 285 {
michael@0 286 if (MessageBoxW(0, installPrompt, friendlyName, MB_YESNO |
michael@0 287 MB_ICONQUESTION) != IDYES)
michael@0 288 return 0;
michael@0 289 }
michael@0 290 appLaunched = GetTextConfigValue(pairs, L"RunProgram");
michael@0 291
michael@0 292 #ifdef _SHELL_EXECUTE
michael@0 293 executeFile = GetTextConfigValue(pairs, L"ExecuteFile");
michael@0 294 executeParameters = GetTextConfigValue(pairs, L"ExecuteParameters") + switches;
michael@0 295 #endif
michael@0 296 }
michael@0 297
michael@0 298 NFile::NDirectory::CTempDirectory tempDir;
michael@0 299 /* Mozilla customizations - Added !extractOnly */
michael@0 300 if (!extractOnly && !tempDir.Create(kTempDirPrefix))
michael@0 301 {
michael@0 302 if (!assumeYes)
michael@0 303 MyMessageBox(L"Can not create temp folder archive");
michael@0 304 return 1;
michael@0 305 }
michael@0 306
michael@0 307 /* BEGIN Mozilla customizations */
michael@0 308 UString tempDirPath = (extractOnly ? switches.Mid(12) : GetUnicodeString(tempDir.GetPath()));
michael@0 309 /* END Mozilla customizations */
michael@0 310
michael@0 311 COpenCallbackGUI openCallback;
michael@0 312
michael@0 313 bool isCorrupt = false;
michael@0 314 UString errorMessage;
michael@0 315 HRESULT result = ExtractArchive(fullPath, tempDirPath, &openCallback, showProgress,
michael@0 316 isCorrupt, errorMessage);
michael@0 317
michael@0 318 if (result != S_OK)
michael@0 319 {
michael@0 320 if (!assumeYes)
michael@0 321 {
michael@0 322 if (result == S_FALSE || isCorrupt)
michael@0 323 {
michael@0 324 errorMessage = NWindows::MyLoadStringW(IDS_EXTRACTION_ERROR_MESSAGE);
michael@0 325 result = E_FAIL;
michael@0 326 }
michael@0 327 if (result != E_ABORT && !errorMessage.IsEmpty())
michael@0 328 ::MessageBoxW(0, errorMessage, NWindows::MyLoadStringW(IDS_EXTRACTION_ERROR_TITLE), MB_ICONERROR);
michael@0 329 }
michael@0 330 return 1;
michael@0 331 }
michael@0 332
michael@0 333 /* BEGIN Mozilla customizations */
michael@0 334 // The code immediately above handles the error case for extraction.
michael@0 335 if (extractOnly) {
michael@0 336 return 0;
michael@0 337 }
michael@0 338 /* END Mozilla customizations */
michael@0 339
michael@0 340 CCurrentDirRestorer currentDirRestorer;
michael@0 341
michael@0 342 if (!SetCurrentDirectory(tempDir.GetPath()))
michael@0 343 return 1;
michael@0 344
michael@0 345 HANDLE hProcess = 0;
michael@0 346 #ifdef _SHELL_EXECUTE
michael@0 347 if (!executeFile.IsEmpty())
michael@0 348 {
michael@0 349 CSysString filePath = GetSystemString(executeFile);
michael@0 350 SHELLEXECUTEINFO execInfo;
michael@0 351 execInfo.cbSize = sizeof(execInfo);
michael@0 352 execInfo.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_DDEWAIT;
michael@0 353 execInfo.hwnd = NULL;
michael@0 354 execInfo.lpVerb = NULL;
michael@0 355 execInfo.lpFile = filePath;
michael@0 356
michael@0 357 if (!switches.IsEmpty())
michael@0 358 executeParameters += switches;
michael@0 359
michael@0 360 CSysString parametersSys = GetSystemString(executeParameters);
michael@0 361 if (parametersSys.IsEmpty())
michael@0 362 execInfo.lpParameters = NULL;
michael@0 363 else
michael@0 364 execInfo.lpParameters = parametersSys;
michael@0 365
michael@0 366 execInfo.lpDirectory = NULL;
michael@0 367 execInfo.nShow = SW_SHOWNORMAL;
michael@0 368 execInfo.hProcess = 0;
michael@0 369 bool success = BOOLToBool(::ShellExecuteEx(&execInfo));
michael@0 370 result = (UINT32)execInfo.hInstApp;
michael@0 371 if(result <= 32)
michael@0 372 {
michael@0 373 if (!assumeYes)
michael@0 374 MyMessageBox(L"Can not open file");
michael@0 375 return 1;
michael@0 376 }
michael@0 377 hProcess = execInfo.hProcess;
michael@0 378 }
michael@0 379 else
michael@0 380 #endif
michael@0 381 {
michael@0 382 if (appLaunched.IsEmpty())
michael@0 383 {
michael@0 384 appLaunched = L"setup.exe";
michael@0 385 if (!NFile::NFind::DoesFileExist(GetSystemString(appLaunched)))
michael@0 386 {
michael@0 387 if (!assumeYes)
michael@0 388 MyMessageBox(L"Can not find setup.exe");
michael@0 389 return 1;
michael@0 390 }
michael@0 391 }
michael@0 392
michael@0 393 {
michael@0 394 UString s2 = tempDirPath;
michael@0 395 NFile::NName::NormalizeDirPathPrefix(s2);
michael@0 396 appLaunched.Replace(L"%%T\\", s2);
michael@0 397 }
michael@0 398
michael@0 399 appLaunched.Replace(L"%%T", tempDirPath);
michael@0 400
michael@0 401 if (!switches.IsEmpty())
michael@0 402 {
michael@0 403 appLaunched += L' ';
michael@0 404 appLaunched += switches;
michael@0 405 }
michael@0 406 STARTUPINFO startupInfo;
michael@0 407 startupInfo.cb = sizeof(startupInfo);
michael@0 408 startupInfo.lpReserved = 0;
michael@0 409 startupInfo.lpDesktop = 0;
michael@0 410 startupInfo.lpTitle = 0;
michael@0 411 startupInfo.dwFlags = 0;
michael@0 412 startupInfo.cbReserved2 = 0;
michael@0 413 startupInfo.lpReserved2 = 0;
michael@0 414
michael@0 415 PROCESS_INFORMATION processInformation;
michael@0 416
michael@0 417 CSysString appLaunchedSys = GetSystemString(dirPrefix + appLaunched);
michael@0 418
michael@0 419 BOOL createResult = CreateProcess(NULL, (LPTSTR)(LPCTSTR)appLaunchedSys,
michael@0 420 NULL, NULL, FALSE, 0, NULL, NULL /*tempDir.GetPath() */,
michael@0 421 &startupInfo, &processInformation);
michael@0 422 if (createResult == 0)
michael@0 423 {
michael@0 424 if (!assumeYes)
michael@0 425 ShowLastErrorMessage();
michael@0 426 return 1;
michael@0 427 }
michael@0 428 ::CloseHandle(processInformation.hThread);
michael@0 429 hProcess = processInformation.hProcess;
michael@0 430 }
michael@0 431 if (hProcess != 0)
michael@0 432 {
michael@0 433 WaitForSingleObject(hProcess, INFINITE);
michael@0 434 ::CloseHandle(hProcess);
michael@0 435 }
michael@0 436 return 0;
michael@0 437 }

mercurial