From 5b39dc76f1bc82f941d5c800ab9f34407a06b53a Mon Sep 17 00:00:00 2001 From: Igor Pavlov <87184205+ip7z@users.noreply.github.com> Date: Wed, 21 Jun 2023 00:00:00 +0000 Subject: 23.01 --- C/Util/7zipInstall/7zipInstall.c | 135 ++++++++++++++++++++++++--------------- 1 file changed, 82 insertions(+), 53 deletions(-) (limited to 'C/Util/7zipInstall/7zipInstall.c') diff --git a/C/Util/7zipInstall/7zipInstall.c b/C/Util/7zipInstall/7zipInstall.c index 2649734..7f5fd19 100644 --- a/C/Util/7zipInstall/7zipInstall.c +++ b/C/Util/7zipInstall/7zipInstall.c @@ -1,16 +1,31 @@ /* 7zipInstall.c - 7-Zip Installer -2022-07-15 : Igor Pavlov : Public domain */ +2023-04-04 : Igor Pavlov : Public domain */ #include "Precomp.h" #define SZ_ERROR_ABORT 100 -#ifdef _MSC_VER +#include "../../7zWindows.h" + +#if defined(_MSC_VER) && _MSC_VER < 1600 #pragma warning(disable : 4201) // nonstandard extension used : nameless struct/union #endif -#include +#ifdef Z7_OLD_WIN_SDK +struct IShellView; +#define SHFOLDERAPI EXTERN_C DECLSPEC_IMPORT HRESULT STDAPICALLTYPE +SHFOLDERAPI SHGetFolderPathW(HWND hwnd, int csidl, HANDLE hToken, DWORD dwFlags, LPWSTR pszPath); +#define BIF_NEWDIALOGSTYLE 0x0040 // Use the new dialog layout with the ability to resize +typedef enum { + SHGFP_TYPE_CURRENT = 0, // current value for user, verify it exists + SHGFP_TYPE_DEFAULT = 1, // default value, may not exist +} SHGFP_TYPE; +#endif +#if defined(__MINGW32__) || defined(__MINGW64__) +#include +#else #include +#endif #include "../../7z.h" #include "../../7zAlloc.h" @@ -22,40 +37,46 @@ #include "resource.h" -#if defined(__GNUC__) && (__GNUC__ >= 8) - #pragma GCC diagnostic ignored "-Wcast-function-type" +#if (defined(__GNUC__) && (__GNUC__ >= 8)) || defined(__clang__) + // #pragma GCC diagnostic ignored "-Wcast-function-type" +#endif + +#if defined(__clang__) || defined(__GNUC__) +typedef void (*Z7_voidFunction)(void); +#define MY_CAST_FUNC (Z7_voidFunction) +#elif defined(_MSC_VER) && _MSC_VER > 1920 +#define MY_CAST_FUNC (void *) +// #pragma warning(disable : 4191) // 'type cast': unsafe conversion from 'FARPROC' to 'void (__cdecl *)()' +#else +#define MY_CAST_FUNC #endif #define LLL_(quote) L##quote #define LLL(quote) LLL_(quote) -#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) - #define wcscat lstrcatW -#define wcslen lstrlenW +#define wcslen (size_t)lstrlenW #define wcscpy lstrcpyW // wcsncpy() and lstrcpynW() work differently. We don't use them. - #define kInputBufSize ((size_t)1 << 18) - -#define _7ZIP_CUR_VER ((MY_VER_MAJOR << 16) | MY_VER_MINOR) -#define _7ZIP_DLL_VER_COMPAT ((16 << 16) | 3) +#define Z7_7ZIP_CUR_VER ((MY_VER_MAJOR << 16) | MY_VER_MINOR) +#define Z7_7ZIP_DLL_VER_COMPAT ((16 << 16) | 3) static LPCSTR const k_7zip = "7-Zip"; static LPCWSTR const k_Reg_Software_7zip = L"Software\\7-Zip"; -// #define _64BIT_INSTALLER 1 +// #define Z7_64BIT_INSTALLER 1 #ifdef _WIN64 - #define _64BIT_INSTALLER 1 + #define Z7_64BIT_INSTALLER 1 #endif #define k_7zip_with_Ver_base L"7-Zip " LLL(MY_VERSION) -#ifdef _64BIT_INSTALLER +#ifdef Z7_64BIT_INSTALLER // #define USE_7ZIP_32_DLL @@ -84,14 +105,14 @@ static LPCWSTR const k_7zip_Setup = k_7zip_with_Ver L" Setup"; static LPCWSTR const k_Reg_Path = L"Path"; static LPCWSTR const k_Reg_Path32 = L"Path" - #ifdef _64BIT_INSTALLER + #ifdef Z7_64BIT_INSTALLER L"64" #else L"32" #endif ; -#if defined(_64BIT_INSTALLER) && !defined(_WIN64) +#if defined(Z7_64BIT_INSTALLER) && !defined(_WIN64) #define k_Reg_WOW_Flag KEY_WOW64_64KEY #else #define k_Reg_WOW_Flag 0 @@ -126,8 +147,6 @@ static WCHAR cmdError[MAX_PATH + 4]; static WCHAR path[MAX_PATH * 2 + 40]; -// #define MAKE_CHAR_UPPER(c) ((((c) >= 'a' && (c) <= 'z') ? (c) -= 0x20 : (c))) - static void CpyAscii(wchar_t *dest, const char *s) { @@ -200,9 +219,12 @@ static DWORD GetFileVersion(LPCWSTR s) return 0; } - my_GetFileVersionInfoSizeW = (Func_GetFileVersionInfoSizeW)GetProcAddress(g_version_dll_hModule, "GetFileVersionInfoSizeW"); - my_GetFileVersionInfoW = (Func_GetFileVersionInfoW)GetProcAddress(g_version_dll_hModule, "GetFileVersionInfoW"); - my_VerQueryValueW = (Func_VerQueryValueW)GetProcAddress(g_version_dll_hModule, "VerQueryValueW"); + my_GetFileVersionInfoSizeW = (Func_GetFileVersionInfoSizeW) MY_CAST_FUNC GetProcAddress(g_version_dll_hModule, + "GetFileVersionInfoSizeW"); + my_GetFileVersionInfoW = (Func_GetFileVersionInfoW) MY_CAST_FUNC GetProcAddress(g_version_dll_hModule, + "GetFileVersionInfoW"); + my_VerQueryValueW = (Func_VerQueryValueW) MY_CAST_FUNC GetProcAddress(g_version_dll_hModule, + "VerQueryValueW"); if (!my_GetFileVersionInfoSizeW || !my_GetFileVersionInfoW @@ -253,7 +275,7 @@ static int ReverseFind_PathSepar(const wchar_t *s) } } -static WRes CreateComplexDir() +static WRes CreateComplexDir(void) { WCHAR s[MAX_PATH + 10]; @@ -287,7 +309,7 @@ static WRes CreateComplexDir() { size_t len = wcslen(s); { - int pos = ReverseFind_PathSepar(s); + const int pos = ReverseFind_PathSepar(s); if (pos < 0) return wres; if ((unsigned)pos < prefixSize) @@ -297,7 +319,7 @@ static WRes CreateComplexDir() if (len == 1) return 0; s[pos] = 0; - len = pos; + len = (unsigned)pos; } } @@ -309,7 +331,7 @@ static WRes CreateComplexDir() break; if (wres == ERROR_ALREADY_EXISTS) { - DWORD attrib = GetFileAttributesW(s); + const DWORD attrib = GetFileAttributesW(s); if (attrib != INVALID_FILE_ATTRIBUTES) if ((attrib & FILE_ATTRIBUTE_DIRECTORY) == 0) return ERROR_ALREADY_EXISTS; @@ -323,7 +345,7 @@ static WRes CreateComplexDir() for (;;) { - size_t pos = wcslen(s); + const size_t pos = wcslen(s); if (pos >= len) return 0; s[pos] = CHAR_PATH_SEPARATOR; @@ -339,7 +361,7 @@ static int MyRegistry_QueryString(HKEY hKey, LPCWSTR name, LPWSTR dest) { DWORD cnt = MAX_PATH * sizeof(name[0]); DWORD type = 0; - LONG res = RegQueryValueExW(hKey, name, NULL, &type, (LPBYTE)dest, (DWORD *)&cnt); + const LONG res = RegQueryValueExW(hKey, name, NULL, &type, (LPBYTE)dest, &cnt); if (type != REG_SZ) return False; return res == ERROR_SUCCESS; @@ -348,11 +370,11 @@ static int MyRegistry_QueryString(HKEY hKey, LPCWSTR name, LPWSTR dest) static int MyRegistry_QueryString2(HKEY hKey, LPCWSTR keyName, LPCWSTR valName, LPWSTR dest) { HKEY key = 0; - LONG res = RegOpenKeyExW(hKey, keyName, 0, KEY_READ | k_Reg_WOW_Flag, &key); + const LONG res = RegOpenKeyExW(hKey, keyName, 0, KEY_READ | k_Reg_WOW_Flag, &key); if (res != ERROR_SUCCESS) return False; { - BoolInt res2 = MyRegistry_QueryString(key, valName, dest); + const BoolInt res2 = MyRegistry_QueryString(key, valName, dest); RegCloseKey(key); return res2; } @@ -550,7 +572,7 @@ static void NormalizePrefix(WCHAR *s) for (;; i++) { - wchar_t c = s[i]; + const wchar_t c = s[i]; if (c == 0) break; if (c == '/') @@ -587,7 +609,7 @@ static LPCWSTR FindSubString(LPCWSTR s1, const char *s2) return NULL; for (i = 0;; i++) { - Byte b = s2[i]; + const char b = s2[i]; if (b == 0) return s1; if (MyWCharLower_Ascii(s1[i]) != (Byte)MyCharLower_Ascii(b)) @@ -610,7 +632,7 @@ static void Set7zipPostfix(WCHAR *s) static int Install(void); -static void OnClose() +static void OnClose(void) { if (g_Install_was_Pressed && !g_Finished) { @@ -624,7 +646,13 @@ static void OnClose() g_HWND = NULL; } -static INT_PTR CALLBACK MyDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) +static +#ifdef Z7_OLD_WIN_SDK + BOOL +#else + INT_PTR +#endif +CALLBACK MyDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { // UNUSED_VAR(hwnd) UNUSED_VAR(lParam) @@ -730,7 +758,7 @@ static LONG SetRegKey_Path2(HKEY parentKey) return res; } -static void SetRegKey_Path() +static void SetRegKey_Path(void) { SetRegKey_Path2(HKEY_CURRENT_USER); SetRegKey_Path2(HKEY_LOCAL_MACHINE); @@ -828,7 +856,7 @@ static void SetShellProgramsGroup(HWND hwndOwner) static LPCWSTR const k_Shell_Approved = L"Software\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved"; static LPCWSTR const k_7zip_ShellExtension = L"7-Zip Shell Extension"; -static void WriteCLSID() +static void WriteCLSID(void) { HKEY destKey; LONG res; @@ -879,12 +907,12 @@ static LPCSTR const k_ShellEx_Items[] = , "Drive\\shellex\\DragDropHandlers" }; -static void WriteShellEx() +static void WriteShellEx(void) { unsigned i; WCHAR destPath[MAX_PATH + 40]; - for (i = 0; i < ARRAY_SIZE(k_ShellEx_Items); i++) + for (i = 0; i < Z7_ARRAY_SIZE(k_ShellEx_Items); i++) { CpyAscii(destPath, k_ShellEx_Items[i]); CatAscii(destPath, "\\7-Zip"); @@ -968,7 +996,7 @@ static const wchar_t *GetCmdParam(const wchar_t *s) quoteMode = !quoteMode; continue; } - if (pos >= ARRAY_SIZE(cmd) - 1) + if (pos >= Z7_ARRAY_SIZE(cmd) - 1) exit(1); cmd[pos++] = c; } @@ -1026,7 +1054,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, for (;;) { { - wchar_t c = *s; + const wchar_t c = *s; if (c == 0) break; if (c == ' ') @@ -1070,11 +1098,12 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, } } - #if defined(_64BIT_INSTALLER) && !defined(_WIN64) + #if defined(Z7_64BIT_INSTALLER) && !defined(_WIN64) { BOOL isWow64 = FALSE; - Func_IsWow64Process func_IsWow64Process = (Func_IsWow64Process) - GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "IsWow64Process"); + const Func_IsWow64Process func_IsWow64Process = (Func_IsWow64Process) + MY_CAST_FUNC GetProcAddress(GetModuleHandleW(L"kernel32.dll"), + "IsWow64Process"); if (func_IsWow64Process) func_IsWow64Process(GetCurrentProcess(), &isWow64); @@ -1093,7 +1122,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, { HKEY key = 0; BoolInt ok = False; - LONG res = RegOpenKeyExW(HKEY_CURRENT_USER, k_Reg_Software_7zip, 0, KEY_READ | k_Reg_WOW_Flag, &key); + const LONG res = RegOpenKeyExW(HKEY_CURRENT_USER, k_Reg_Software_7zip, 0, KEY_READ | k_Reg_WOW_Flag, &key); if (res == ERROR_SUCCESS) { ok = MyRegistry_QueryString(key, k_Reg_Path32, path); @@ -1109,7 +1138,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, CpyAscii(path, "\\Program Files\\"); #else - #ifdef _64BIT_INSTALLER + #ifdef Z7_64BIT_INSTALLER { DWORD ttt = GetEnvironmentVariableW(L"ProgramW6432", path, MAX_PATH); if (ttt == 0 || ttt > MAX_PATH) @@ -1150,7 +1179,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, return 1; { - HICON hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON)); + const HICON hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON)); // SendMessage(g_HWND, WM_SETICON, (WPARAM)ICON_SMALL, (LPARAM)hIcon); SendMessage(g_HWND, WM_SETICON, (WPARAM)ICON_BIG, (LPARAM)hIcon); } @@ -1244,7 +1273,7 @@ static int Install(void) allocTempImp.Free = SzFreeTemp; { - DWORD len = GetModuleFileNameW(NULL, sfxPath, MAX_PATH); + const DWORD len = GetModuleFileNameW(NULL, sfxPath, MAX_PATH); if (len == 0 || len > MAX_PATH) return 1; } @@ -1286,7 +1315,7 @@ if (res == SZ_OK) for (;;) { - wchar_t c = path[i++]; + const wchar_t c = path[i++]; if (c == 0) break; if (c != ' ') @@ -1318,7 +1347,7 @@ if (res == SZ_OK) { lookStream.bufSize = kInputBufSize; lookStream.realStream = &archiveStream.vt; - LookToRead2_Init(&lookStream); + LookToRead2_INIT(&lookStream) } } @@ -1372,7 +1401,7 @@ if (res == SZ_OK) } { - size_t len = SzArEx_GetFileNameUtf16(&db, i, NULL); + const size_t len = SzArEx_GetFileNameUtf16(&db, i, NULL); if (len >= MAX_PATH) { res = SZ_ERROR_FAIL; @@ -1468,8 +1497,8 @@ if (res == SZ_OK) #endif ) { - DWORD ver = GetFileVersion(path); - fileLevel = ((ver < _7ZIP_DLL_VER_COMPAT || ver > _7ZIP_CUR_VER) ? 2 : 1); + const DWORD ver = GetFileVersion(path); + fileLevel = ((ver < Z7_7ZIP_DLL_VER_COMPAT || ver > Z7_7ZIP_CUR_VER) ? 2 : 1); tempIndex++; continue; } @@ -1537,7 +1566,7 @@ if (res == SZ_OK) #endif { - SRes winRes2 = File_Close(&outFile); + const WRes winRes2 = File_Close(&outFile); if (res != SZ_OK) break; if (winRes2 != 0) -- cgit v1.2.3-55-g6feb