9
9
10
10
#include < assert.h>
11
11
#include < atlstr.h>
12
+ #include < imagehlp.h>
12
13
#include < sstream> // wstringstream
13
14
#include < iomanip> // setw, setfill
14
15
#include < fstream>
@@ -93,8 +94,8 @@ std::wstring ReadFileToString(const wchar_t* filename) {
93
94
94
95
class ScopedFile {
95
96
public:
96
- ScopedFile (const WCHAR* path)
97
- : file_(CreateFileW(path, GENERIC_READ , FILE_SHARE_READ, NULL , OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL )) {}
97
+ ScopedFile (const WCHAR* path, DWORD access = GENERIC_READ )
98
+ : file_(CreateFileW(path, access , FILE_SHARE_READ, NULL , OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL )) {}
98
99
~ScopedFile () { CloseHandle (file_); }
99
100
100
101
operator HANDLE () { return file_; }
@@ -568,8 +569,8 @@ bool ResourceUpdater::ChangeRcData(UINT id, const WCHAR* pathToResource) {
568
569
569
570
wchar_t abspath[MAX_PATH] = { 0 };
570
571
const auto filePath = _wfullpath (abspath, pathToResource, MAX_PATH) ? abspath : pathToResource;
571
- ScopedFile newRcDataFile (filePath);
572
- if (newRcDataFile == INVALID_HANDLE_VALUE) {
572
+ ScopedFile newRcDataFile (filePath);
573
+ if (newRcDataFile == INVALID_HANDLE_VALUE) {
573
574
fprintf (stderr, " Cannot open new data file '%ws'\n " , filePath);
574
575
return false ;
575
576
}
@@ -581,13 +582,13 @@ bool ResourceUpdater::ChangeRcData(UINT id, const WCHAR* pathToResource) {
581
582
}
582
583
583
584
auto & rcData = rcDataLngPairIt->second [id];
584
- rcData.clear ();
585
+ rcData.clear ();
585
586
rcData.resize (dwFileSize);
586
-
587
- DWORD dwBytesRead{ 0 };
587
+
588
+ DWORD dwBytesRead{ 0 };
588
589
if (!ReadFile (newRcDataFile, rcData.data (), dwFileSize, &dwBytesRead, NULL )) {
589
590
fprintf (stderr, " Cannot read file '%ws'\n " , filePath);
590
- return false ;
591
+ return false ;
591
592
}
592
593
593
594
return true ;
@@ -701,7 +702,7 @@ bool ResourceUpdater::Commit() {
701
702
FreeLibrary (module_);
702
703
module_ = NULL ;
703
704
704
- ScopedResourceUpdater ru (filename_. c_str () , false );
705
+ ScopedResourceUpdater ru (filename_, false );
705
706
if (ru.Get () == NULL ) {
706
707
return false ;
707
708
}
@@ -979,8 +980,8 @@ BOOL CALLBACK ResourceUpdater::OnEnumResourceManifest(HMODULE hModule, LPCTSTR l
979
980
return TRUE ; // Keep going
980
981
}
981
982
982
- ScopedResourceUpdater::ScopedResourceUpdater (const WCHAR* filename, bool deleteOld)
983
- : handle_(BeginUpdateResourceW(filename, deleteOld)) {
983
+ ScopedResourceUpdater::ScopedResourceUpdater (std::wstring filename, bool deleteOld)
984
+ : filename_(filename), handle_(BeginUpdateResourceW(filename.c_str() , deleteOld)) {
984
985
}
985
986
986
987
ScopedResourceUpdater::~ScopedResourceUpdater () {
@@ -995,7 +996,7 @@ HANDLE ScopedResourceUpdater::Get() const {
995
996
996
997
bool ScopedResourceUpdater::Commit () {
997
998
commited_ = true ;
998
- return EndUpdate (true );
999
+ return EndUpdate (true ) && UpdateChecksum () ;
999
1000
}
1000
1001
1001
1002
bool ScopedResourceUpdater::EndUpdate (bool doesCommit) {
@@ -1005,4 +1006,45 @@ bool ScopedResourceUpdater::EndUpdate(bool doesCommit) {
1005
1006
return bResult ? true : false ;
1006
1007
}
1007
1008
1009
+ bool ScopedResourceUpdater::UpdateChecksum () {
1010
+ const WCHAR* path = filename_.c_str ();
1011
+ ScopedFile binFile (path, GENERIC_READ|GENERIC_WRITE);
1012
+ if (binFile == INVALID_HANDLE_VALUE) {
1013
+ fprintf (stderr, " Cannot open '%ws'\n " , path);
1014
+ return false ;
1015
+ }
1016
+ DWORD fileSize = GetFileSize (binFile, NULL );
1017
+ if (fileSize == INVALID_FILE_SIZE) {
1018
+ fprintf (stderr, " Cannot get file size for '%ws'\n " , path);
1019
+ return false ;
1020
+ }
1021
+
1022
+ HANDLE hFilemap = CreateFileMapping (binFile, NULL , PAGE_READWRITE, 0 , 0 , NULL );
1023
+ if (!hFilemap) {
1024
+ fprintf (stderr, " CreateFileMapping(%ws) failed\n " , path);
1025
+ return false ;
1026
+ }
1027
+
1028
+ void * view = MapViewOfFile (hFilemap, FILE_MAP_ALL_ACCESS, 0 , 0 , 0 );
1029
+ if (!view) {
1030
+ fprintf (stderr, " Cannot map '%ws' into memory\n " , path);
1031
+ CloseHandle (hFilemap);
1032
+ return false ;
1033
+ }
1034
+
1035
+ bool ret = false ;
1036
+ DWORD originalSum, checkSum;
1037
+ IMAGE_NT_HEADERS *ntHeader = CheckSumMappedFile (view, fileSize, &originalSum, &checkSum);
1038
+ if (!ntHeader) {
1039
+ fprintf (stderr, " CheckSumMappedFile failed\n " );
1040
+ } else {
1041
+ ntHeader->OptionalHeader .CheckSum = checkSum;
1042
+ ret = true ;
1043
+ }
1044
+
1045
+ UnmapViewOfFile (view);
1046
+ CloseHandle (hFilemap);
1047
+ return ret;
1048
+ }
1049
+
1008
1050
} // namespace rescle
0 commit comments