@@ -28,7 +28,14 @@ __declspec(dllexport) int WINAPI HookedRand()
2828 return dist (gen);
2929}
3030
31- bool MatchPattern (const unsigned char * data, const unsigned char * pattern, int length)
31+ /* *
32+ * Match a pattern against data.
33+ * @param data The data to match against.
34+ * @param pattern The pattern to match.
35+ * @param length The length of the pattern.
36+ * @return True if the pattern matches the data, false otherwise.
37+ */
38+ bool MatchPattern (const unsigned char *data, const unsigned char *pattern, int length)
3239{
3340 for (int i = 0 ; i < length; ++i)
3441 {
@@ -44,7 +51,13 @@ bool MatchPattern(const unsigned char* data, const unsigned char* pattern, int l
4451 return true ;
4552}
4653
47- // Find the offset of a function within a module using a pattern
54+ /* *
55+ * Find the offset of a function within a module using a pattern.
56+ * @param hModule Handle to the module to search within.
57+ * @param pattern The pattern to search for.
58+ * @param length The length of the pattern.
59+ * @return The offset of the function if found, or 0x0 if not found.
60+ */
4861LPBYTE FindFunctionOffset (HMODULE hModule, const unsigned char *pattern, int length)
4962{
5063 // Get the base address of the module
@@ -53,12 +66,12 @@ LPBYTE FindFunctionOffset(HMODULE hModule, const unsigned char *pattern, int len
5366 // Get the module's PE header
5467 auto ntHeader = ImageNtHeader (hModule);
5568 if (!ntHeader)
56- return 0 ; // Invalid PE header
69+ return 0x0 ; // Invalid PE header
5770
5871 // Get the image section header
5972 auto sectionHeader = IMAGE_FIRST_SECTION (ntHeader);
6073 if (!sectionHeader)
61- return 0 ; // Invalid section header
74+ return 0x0 ; // Invalid section header
6275
6376 // Search within the module's code section
6477 IMAGE_SECTION_HEADER *codeSectionHeader = nullptr ;
@@ -82,35 +95,36 @@ LPBYTE FindFunctionOffset(HMODULE hModule, const unsigned char *pattern, int len
8295 for (; codeSectionMemory < endAddress; codeSectionMemory++)
8396 {
8497 if (MatchPattern (codeSectionMemory, pattern, length))
85- // if (memcmp(codeSectionMemory, pattern, length) == 0)
8698 return (LPBYTE )codeSectionMemory;
8799 }
88100
89101 // Pattern not found
90102 return 0x0 ;
91103}
92104
93- // Function to initialize the hook
105+ /* *
106+ * Initialize the hook by finding the target function and attaching the custom implementation.
107+ */
94108void InitHook ()
95109{
96110 // Get the address of the target function
97111 auto hModule = GetModuleHandle (nullptr );
98112
99113 LPBYTE pFunctionAddress = 0x0 ;
100114
101- // Find the offset of the target function using a specific pattern
102-
103- // Baldur's Gate EE, Baldur's Gate 2 EE, Icewind Dale EE,
115+ // Find the offset of the target function using specific patterns
116+
117+ // Pattern for Baldur's Gate EE, Baldur's Gate 2 EE, Icewind Dale EE
104118 unsigned char pattern1[] = " \x48\x83 ..\xE8 .......\xFD\x43\x03\x00 " ;
105119 if (pFunctionAddress == 0x0 )
106120 pFunctionAddress = FindFunctionOffset (hModule, pattern1, sizeof (pattern1) - 1 );
107121
108- // Planescape EE
122+ // Pattern for Planescape EE
109123 unsigned char pattern2[] = " \xE8 .......\xFD\x43\x03\x00 " ;
110124 if (pFunctionAddress == 0x0 )
111125 pFunctionAddress = FindFunctionOffset (hModule, pattern2, sizeof (pattern2) - 1 );
112126
113- // Baldur's Gate, Baldur's Gate 2, Icewind Dale, Planescape Torment
127+ // Pattern for Baldur's Gate, Baldur's Gate 2, Icewind Dale, Planescape Torment
114128 unsigned char pattern3[] = " \xE8 ....\x8B ....\xFD\x43\x03\x00 " ;
115129 if (pFunctionAddress == 0x0 )
116130 pFunctionAddress = FindFunctionOffset (hModule, pattern3, sizeof (pattern3) - 1 );
@@ -121,10 +135,14 @@ void InitHook()
121135 throw (" Error: original rand() method not found!" );
122136 }
123137
138+ // Obtain system information
124139 SYSTEM_INFO systemInfo;
125140 GetSystemInfo (&systemInfo);
126141
142+ // Retrieve the base address of the application
127143 auto baseAddress = systemInfo.lpMinimumApplicationAddress ;
144+
145+ // Calculate the scan size by subtracting the minimum application address from the maximum application address
128146 auto scanSize = reinterpret_cast <SIZE_T >(systemInfo.lpMaximumApplicationAddress ) - reinterpret_cast <SIZE_T >(systemInfo.lpMinimumApplicationAddress );
129147
130148 // Get module info
@@ -141,7 +159,9 @@ void InitHook()
141159 DetourTransactionCommit ();
142160}
143161
144- // Function to remove the hook
162+ /* *
163+ * Remove the hook by detaching the custom implementation from the target function.
164+ */
145165void RemoveHook ()
146166{
147167 // Detach the hook
@@ -151,7 +171,13 @@ void RemoveHook()
151171 DetourTransactionCommit ();
152172}
153173
154- // Entry point of the DLL
174+ /* *
175+ * Entry point of the DLL.
176+ * @param hinst The handle to the DLL module.
177+ * @param dwReason The reason for calling the DLL entry point.
178+ * @param reserved Reserved parameter.
179+ * @return True if the DLL was initialized successfully, false otherwise.
180+ */
155181BOOL WINAPI DllMain (HINSTANCE hinst, DWORD dwReason, LPVOID reserved)
156182{
157183 if (DetourIsHelperProcess ())
0 commit comments