DXGL r949 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r948‎ | r949 | r950 >
Date:03:57, 16 September 2019
Author:admin
Status:new
Tags:
Comment:
Implement fixed profile path hash and installer migration.
Modified paths:
  • /Installer/dxgl.nsi (modified) (history)
  • /cfgmgr/cfgmgr.c (modified) (history)

Diff [purge]

Index: Installer/dxgl.nsi
@@ -251,8 +251,11 @@
252252 !endif
253253
254254 Section -PostInstall
255 - ExecWait '"$INSTDIR\dxglcfg.exe" upgrade'
 255+ CreateDirectory $INSTDIR\Temp
 256+ CopyFiles $INSTDIR\dxglcfg.exe $INSTDIR\Temp
 257+ ExecWait '"$INSTDIR\Temp\dxglcfg.exe" upgrade'
256258 ExecWait '"$INSTDIR\dxglcfg.exe" profile_install'
 259+ RMDir /r /REBOOTOK $INSTDIR\Temp
257260 SectionEnd
258261
259262 Section "Set Wine DLL Overrides" SEC_WINEDLLOVERRIDE
Index: cfgmgr/cfgmgr.c
@@ -41,10 +41,11 @@
4242 typedef LONG LSTATUS;
4343 #endif
4444
45 -TCHAR regkeyglobal[] = _T("Software\\DXGL\\Global");
46 -TCHAR regkeyprofiles[] = _T("Software\\DXGL\\Profiles\\");
47 -TCHAR regkeybase[] = _T("Software\\DXGL\\");
48 -TCHAR regkeydxgl[] = _T("Software\\DXGL");
 45+static const TCHAR regkeyglobal[] = _T("Software\\DXGL\\Global");
 46+static const TCHAR regkeyprofiles[] = _T("Software\\DXGL\\Profiles\\");
 47+static const TCHAR regkeyprofilesmigrated[] = _T("Software\\DXGL\\ProfilesMigrated\\");
 48+static const TCHAR regkeybase[] = _T("Software\\DXGL\\");
 49+static const TCHAR regkeydxgl[] = _T("Software\\DXGL");
4950
5051 DXGLCFG defaultmask;
5152
@@ -73,7 +74,7 @@
7475 #endif
7576 }
7677
77 -void _wchartotchar(TCHAR *dest, WCHAR *src, int length)
 78+void _wchartotchar(TCHAR *dest, const WCHAR *src, int length)
7879 {
7980 #ifdef _UNICODE
8081 if (length == -1) wcscpy(dest, src);
@@ -85,7 +86,7 @@
8687 #endif
8788 }
8889
89 -void utf8to16(WCHAR* dest, CHAR* src)
 90+void utf8to16(WCHAR* dest, const CHAR* src)
9091 {
9192 int sizein;
9293 int sizeout;
@@ -933,7 +934,7 @@
934935 _tcslwr(filename);
935936 _tchartowchar(filename2,filename,-1);
936937 Sha256Initialise(&sha_context);
937 - Sha256Update(&sha_context, filename2, (uint32_t)wcslen(filename2));
 938+ Sha256Update(&sha_context, filename2, (uint32_t)wcslen(filename2) * sizeof(WCHAR));
938939 Sha256Finalise(&sha_context, &sha256);
939940 for (i = 0; i < (256 / 8); i++)
940941 {
@@ -970,7 +971,7 @@
971972 _tcslwr(pathlwr);
972973 _tchartowchar(pathlwr2,pathlwr,-1);
973974 Sha256Initialise(&sha_context);
974 - Sha256Update(&sha_context, pathlwr2, (uint32_t)wcslen(pathlwr2));
 975+ Sha256Update(&sha_context, pathlwr2, (uint32_t)wcslen(pathlwr2) * sizeof(WCHAR));
975976 Sha256Finalise(&sha_context, &sha256);
976977 for (i = 0; i < (256 / 8); i++)
977978 {
@@ -1700,7 +1701,7 @@
17011702 _tcslwr(filename);
17021703 _tchartowchar(filename2, filename, -1);
17031704 Sha256Initialise(&sha_context);
1704 - Sha256Update(&sha_context, filename2, (uint32_t)wcslen(filename2));
 1705+ Sha256Update(&sha_context, filename2, (uint32_t)wcslen(filename2) * sizeof(WCHAR));
17051706 Sha256Finalise(&sha_context, &sha256);
17061707 for (i = 0; i < (256 / 8); i++)
17071708 {
@@ -1954,7 +1955,7 @@
19551956 _tcslwr(installpath);
19561957 _tchartowchar(installpath2, installpath, -1);
19571958 Sha256Initialise(&sha_context);
1958 - Sha256Update(&sha_context, installpath2, (uint32_t)wcslen(installpath2));
 1959+ Sha256Update(&sha_context, installpath2, (uint32_t)wcslen(installpath2) * sizeof(WCHAR));
19591960 Sha256Finalise(&sha_context, &sha256);
19601961 for (i = 0; i < (256 / 8); i++)
19611962 {
@@ -2046,6 +2047,7 @@
20472048 HKEY hKey;
20482049 HKEY hKeyProfile;
20492050 HKEY hKeyDest;
 2051+ HKEY hKeyProfileDest;
20502052 TCHAR regkey[MAX_PATH + 24];
20512053 TCHAR subkey[MAX_PATH];
20522054 TCHAR exepath[(MAX_PATH * 2) + 1];
@@ -2058,6 +2060,7 @@
20592061 DWORD olddirsize = 1024;
20602062 TCHAR *olddir = NULL;
20612063 DWORD oldvaluesize = 1024;
 2064+ WCHAR dir_unicode[MAX_PATH];
20622065 TCHAR *oldvalue = NULL;
20632066 TCHAR *ptr;
20642067 size_t length;
@@ -2065,6 +2068,8 @@
20662069 DWORD regtype;
20672070 DWORD sizeout, sizeout2;
20682071 Sha256Context sha_context;
 2072+ SHA256_HASH PathHash;
 2073+ TCHAR PathHashString[65];
20692074 LONG error;
20702075 LONG error2;
20712076 DWORD i;
@@ -2387,8 +2392,216 @@
23882393 RegCloseKey(hKey);
23892394 }
23902395 ver2to3:
 2396+ // Version 2 to 3: Fix profile path hashes
 2397+ if (version >= 3) return;
23912398 // Transfer profiles to pre-migrate path
2392 -
 2399+ error = RegCreateKeyEx(HKEY_CURRENT_USER, regkeyprofiles, 0, NULL, 0, KEY_READ, NULL, &hKey, NULL);
 2400+ if (error == ERROR_SUCCESS)
 2401+ {
 2402+ error = RegCreateKeyEx(HKEY_CURRENT_USER, regkeyprofilesmigrated, 0, NULL, 0, KEY_ALL_ACCESS,
 2403+ NULL, &hKeyDest, NULL);
 2404+ if (error == ERROR_SUCCESS)
 2405+ {
 2406+ olddirsize = 1024;
 2407+ oldvaluesize = 1024;
 2408+ olddir = malloc(olddirsize * 2);
 2409+ oldvalue = malloc(oldvaluesize);
 2410+ keyindex = 0;
 2411+ do
 2412+ {
 2413+ sizeout = MAX_PATH;
 2414+ error = RegEnumKeyEx(hKey, keyindex, subkey, &sizeout,
 2415+ NULL, NULL, NULL, NULL);
 2416+ keyindex++;
 2417+ if (error == ERROR_SUCCESS)
 2418+ {
 2419+ error2 = RegOpenKeyEx(hKey, subkey, 0, KEY_READ, &hKeyProfile);
 2420+ if (error2 == ERROR_SUCCESS)
 2421+ {
 2422+ // Rename subkey
 2423+ sizeout = MAX_PATH;
 2424+ regtype = REG_SZ;
 2425+ error2 = RegQueryValueEx(hKeyProfile, _T("InstallPath"), NULL, &regtype, (LPBYTE)olddir, &sizeout);
 2426+ if (error2 == ERROR_SUCCESS)
 2427+ {
 2428+ _tcslwr(olddir);
 2429+ _tchartowchar(dir_unicode, olddir, MAX_PATH);
 2430+ Sha256Initialise(&sha_context);
 2431+ Sha256Update(&sha_context, dir_unicode, (uint32_t)wcslen(dir_unicode) * sizeof(WCHAR));
 2432+ Sha256Finalise(&sha_context, &PathHash);
 2433+ for (i = 0; i < (256 / 8); i++)
 2434+ {
 2435+ PathHashString[i * 2] = (TCHAR)hexdigit(PathHash.bytes[i] >> 4);
 2436+ PathHashString[(i * 2) + 1] = (TCHAR)hexdigit(PathHash.bytes[i] & 0xF);
 2437+ }
 2438+ PathHashString[256 / 4] = 0;
 2439+ ptr = _tcsrchr(subkey, '-');
 2440+ _tcscpy(ptr + 1, PathHashString);
 2441+ }
 2442+ error2 = RegCreateKeyEx(hKeyDest, subkey, 0, NULL, 0, KEY_ALL_ACCESS,
 2443+ NULL, &hKeyProfileDest, NULL);
 2444+ if (error2 == ERROR_SUCCESS)
 2445+ {
 2446+ numvalue = 0;
 2447+ do
 2448+ {
 2449+ sizeout = olddirsize;
 2450+ sizeout2 = oldvaluesize;
 2451+ error2 = RegEnumValue(hKeyProfile, numvalue, olddir, &sizeout, NULL, &regtype, (LPBYTE)oldvalue, &sizeout2);
 2452+ if (error2 == ERROR_MORE_DATA)
 2453+ {
 2454+ if (sizeout > olddirsize)
 2455+ {
 2456+ olddirsize = sizeout;
 2457+ olddir = realloc(olddir, olddirsize * 2);
 2458+ if (!olddir)
 2459+ {
 2460+ MessageBox(NULL, _T("Out of memory updating registry"), _T("Fatal error"), MB_ICONSTOP | MB_OK);
 2461+ ExitProcess(error2);
 2462+ }
 2463+ }
 2464+ if (sizeout2 > oldvaluesize)
 2465+ {
 2466+ oldvaluesize = sizeout2;
 2467+ oldvalue = realloc(oldvalue, oldvaluesize);
 2468+ if (!oldvalue)
 2469+ {
 2470+ MessageBox(NULL, _T("Out of memory updating registry"), _T("Fatal error"), MB_ICONSTOP | MB_OK);
 2471+ ExitProcess(error2);
 2472+ }
 2473+ }
 2474+ sizeout = olddirsize;
 2475+ sizeout2 = oldvaluesize;
 2476+ error2 = RegEnumValue(hKeyProfile, numvalue, olddir, &sizeout, NULL, &regtype, (LPBYTE)oldvalue, &sizeout2);
 2477+ }
 2478+ if (error2 == ERROR_SUCCESS)
 2479+ {
 2480+ RegSetValueEx(hKeyProfileDest, olddir, 0, regtype, (BYTE*)oldvalue, sizeout2);
 2481+ }
 2482+ numvalue++;
 2483+ } while (error2 == ERROR_SUCCESS);
 2484+ RegCloseKey(hKeyProfileDest);
 2485+ }
 2486+ RegCloseKey(hKeyProfile);
 2487+ }
 2488+ }
 2489+ } while (error == ERROR_SUCCESS);
 2490+ RegCloseKey(hKeyDest);
 2491+ free(olddir);
 2492+ free(oldvalue);
 2493+ }
 2494+ // Delete source keys
 2495+ do
 2496+ {
 2497+ sizeout = MAX_PATH;
 2498+ error = RegEnumKeyEx(hKey, 0, subkey, &sizeout,
 2499+ NULL, NULL, NULL, NULL);
 2500+ if (error == ERROR_SUCCESS)
 2501+ {
 2502+ error2 = RegDeleteKey(hKey, subkey);
 2503+ if (error2 != ERROR_SUCCESS) break;
 2504+ }
 2505+ } while (error == ERROR_SUCCESS);
 2506+ RegCloseKey(hKey);
 2507+ }
 2508+ // Migrate keys to new names
 2509+ error = RegCreateKeyEx(HKEY_CURRENT_USER, regkeyprofilesmigrated, 0, NULL, 0, KEY_READ, NULL, &hKey, NULL);
 2510+ if (error == ERROR_SUCCESS)
 2511+ {
 2512+ error = RegCreateKeyEx(HKEY_CURRENT_USER, regkeyprofiles, 0, NULL, 0, KEY_ALL_ACCESS,
 2513+ NULL, &hKeyDest, NULL);
 2514+ if (error == ERROR_SUCCESS)
 2515+ {
 2516+ olddirsize = 1024;
 2517+ oldvaluesize = 1024;
 2518+ olddir = malloc(olddirsize * 2);
 2519+ oldvalue = malloc(oldvaluesize);
 2520+ keyindex = 0;
 2521+ do
 2522+ {
 2523+ sizeout = MAX_PATH;
 2524+ error = RegEnumKeyEx(hKey, keyindex, subkey, &sizeout,
 2525+ NULL, NULL, NULL, NULL);
 2526+ keyindex++;
 2527+ if (error == ERROR_SUCCESS)
 2528+ {
 2529+ error2 = RegOpenKeyEx(hKey, subkey, 0, KEY_READ, &hKeyProfile);
 2530+ if (error2 == ERROR_SUCCESS)
 2531+ {
 2532+ error2 = RegCreateKeyEx(hKeyDest, subkey, 0, NULL, 0, KEY_ALL_ACCESS,
 2533+ NULL, &hKeyProfileDest, NULL);
 2534+ if (error2 == ERROR_SUCCESS)
 2535+ {
 2536+ numvalue = 0;
 2537+ do
 2538+ {
 2539+ sizeout = olddirsize;
 2540+ sizeout2 = oldvaluesize;
 2541+ error2 = RegEnumValue(hKeyProfile, numvalue, olddir, &sizeout, NULL, &regtype, (LPBYTE)oldvalue, &sizeout2);
 2542+ if (error2 == ERROR_MORE_DATA)
 2543+ {
 2544+ if (sizeout > olddirsize)
 2545+ {
 2546+ olddirsize = sizeout;
 2547+ olddir = realloc(olddir, olddirsize * 2);
 2548+ if (!olddir)
 2549+ {
 2550+ MessageBox(NULL, _T("Out of memory updating registry"), _T("Fatal error"), MB_ICONSTOP | MB_OK);
 2551+ ExitProcess(error2);
 2552+ }
 2553+ }
 2554+ if (sizeout2 > oldvaluesize)
 2555+ {
 2556+ oldvaluesize = sizeout2;
 2557+ oldvalue = realloc(oldvalue, oldvaluesize);
 2558+ if (!oldvalue)
 2559+ {
 2560+ MessageBox(NULL, _T("Out of memory updating registry"), _T("Fatal error"), MB_ICONSTOP | MB_OK);
 2561+ ExitProcess(error2);
 2562+ }
 2563+ }
 2564+ sizeout = olddirsize;
 2565+ sizeout2 = oldvaluesize;
 2566+ error2 = RegEnumValue(hKeyProfile, numvalue, olddir, &sizeout, NULL, &regtype, (LPBYTE)oldvalue, &sizeout2);
 2567+ }
 2568+ if (error2 == ERROR_SUCCESS)
 2569+ {
 2570+ RegSetValueEx(hKeyProfileDest, olddir, 0, regtype, (BYTE*)oldvalue, sizeout2);
 2571+ }
 2572+ numvalue++;
 2573+ } while (error2 == ERROR_SUCCESS);
 2574+ RegCloseKey(hKeyProfileDest);
 2575+ }
 2576+ RegCloseKey(hKeyProfile);
 2577+ }
 2578+ }
 2579+ } while (error == ERROR_SUCCESS);
 2580+ RegCloseKey(hKeyDest);
 2581+ free(olddir);
 2582+ free(oldvalue);
 2583+ }
 2584+ // Delete temporary keys
 2585+ do
 2586+ {
 2587+ sizeout = MAX_PATH;
 2588+ error = RegEnumKeyEx(hKey, 0, subkey, &sizeout,
 2589+ NULL, NULL, NULL, NULL);
 2590+ if (error == ERROR_SUCCESS)
 2591+ {
 2592+ error2 = RegDeleteKey(hKey, subkey);
 2593+ if (error2 != ERROR_SUCCESS) break;
 2594+ }
 2595+ } while (error == ERROR_SUCCESS);
 2596+ RegCloseKey(hKey);
 2597+ RegDeleteKey(HKEY_CURRENT_USER, regkeyprofilesmigrated);
 2598+ }
 2599+ error = RegCreateKeyEx(HKEY_CURRENT_USER, regkey, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, NULL);
 2600+ if (error == ERROR_SUCCESS)
 2601+ {
 2602+ sizeout = 3;
 2603+ RegSetValueEx(hKey, _T("Configuration Version"), 0, REG_DWORD, (BYTE*)& sizeout, 4);
 2604+ RegCloseKey(hKey);
 2605+ }
23932606 return;
23942607 }
23952608