forked from pnill/cartographer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGSUtils.cpp
235 lines (209 loc) · 5.69 KB
/
GSUtils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "GSUtils.h"
#include "H2OnscreenDebugLog.h"
#include "Hook.h"
#include <stdio.h>
#include <windows.h>
#include <Wincrypt.h>
void OverwriteAssembly(BYTE* srcAddr, BYTE* writeAssm, int lenAssm) {
WriteBytesASM((DWORD)srcAddr, writeAssm, lenAssm);
/*DWORD dwBack;
VirtualProtect(srcAddr, lenAssm, PAGE_EXECUTE_READWRITE, &dwBack);
for (int i = 0; i < lenAssm; i++)
srcAddr[i] = writeAssm[i];
VirtualProtect(srcAddr, lenAssm, dwBack, &dwBack);*/
}
void PatchCall(DWORD call_addr, DWORD new_function_ptr) {
DWORD callRelative = new_function_ptr - (call_addr + 5);
BYTE* pbyte = (BYTE*)&callRelative;
BYTE assmNewFuncRel[4] = { pbyte[0], pbyte[1], pbyte[2], pbyte[3] };
WriteBytesASM(call_addr + 1, assmNewFuncRel, 4);
}
void HexToByteArray(BYTE* byteArray, char* pointerHex) {
char totext2[32];
memset(totext2, '0', 1);
char* totext3 = totext2 + 1;
sprintf(totext3, "%s", pointerHex);
int len = strlen(totext3);
if (len % 2 == 1) {
totext3 = totext2;
len++;
}
for (int i = 0; i < len / 2; i++) {
char str[3];
snprintf(str, 3, "%s", totext3 + (i * 2));
byteArray[i] = (int)strtol(str, NULL, 16);
}
}
int FindLineStart(FILE* fp, int lineStrLen) {
int fp_offset_orig = ftell(fp);
for (int i = lineStrLen; i < 255; i++) {
if (fp_offset_orig - i < 0) {
fseek(fp, fp_offset_orig, SEEK_SET);
return 0;
}
fseek(fp, fp_offset_orig - i, SEEK_SET);
int c = 0;
if ((c = fgetc(fp)) == '\r' || c == '\n') {
fseek(fp, fp_offset_orig, SEEK_SET);
return fp_offset_orig - i + 1;
}
}
fseek(fp, fp_offset_orig, SEEK_SET);
return fp_offset_orig - lineStrLen;
}
void GetVKeyCodeString(int vkey, char* rtnString, int strLen) {
snprintf(rtnString, 5, "0x%x", vkey);
char key_name[20];
memset(key_name, 0, 1);
if (vkey >= 0x70 && vkey <= 0x87) {
int func_num = vkey - 0x70 + 1;
snprintf(key_name, 20, "VK_F%d", func_num);
}
if (strlen(key_name) > 0) {
snprintf(rtnString + strlen(rtnString), strLen - 5, " - %s", key_name);
}
}
void PadCStringWithChar(char* strToPad, int toFullLength, char c) {
for (int i = strlen(strToPad); i < toFullLength - 1; i++) {
memset(strToPad + i, c, sizeof(char));
}
memset(strToPad + toFullLength - 1, 0, sizeof(char));
}
int ComputeFileMd5Hash(wchar_t* filepath, char* rtnMd5) {
for (int i = 0; i < 33; i++) {
rtnMd5[i] = 0;
}
const int BUFSIZE = 1024;
const int MD5LEN = 16;
DWORD dwStatus = 0;
BOOL bResult = FALSE;
HCRYPTPROV hProv = 0;
HCRYPTHASH hHash = 0;
HANDLE hFile = NULL;
BYTE rgbFile[BUFSIZE];
DWORD cbRead = 0;
BYTE rgbHash[MD5LEN];
DWORD cbHash = 0;
CHAR rgbDigits[] = "0123456789abcdef";
//LPCWSTR filename = L"xinput9_1_0.dll";
// Logic to check usage goes here.
hFile = CreateFile(filepath,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN,
NULL);
if (INVALID_HANDLE_VALUE == hFile)
{
return -1;
}
// Get handle to the crypto provider
if (!CryptAcquireContext(&hProv,
NULL,
NULL,
PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT))
{
dwStatus = GetLastError();
strcpy(rtnMd5, "CryptAcquireContext");
CloseHandle(hFile);
return dwStatus;
}
if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
{
dwStatus = GetLastError();
strcpy(rtnMd5, "CryptAcquireContext");
CloseHandle(hFile);
CryptReleaseContext(hProv, 0);
return dwStatus;
}
while (bResult = ReadFile(hFile, rgbFile, BUFSIZE, &cbRead, NULL))
{
if (0 == cbRead)
{
break;
}
if (!CryptHashData(hHash, rgbFile, cbRead, 0))
{
dwStatus = GetLastError();
strcpy(rtnMd5, "CryptHashData");
CryptReleaseContext(hProv, 0);
CryptDestroyHash(hHash);
CloseHandle(hFile);
return dwStatus;
}
}
if (!bResult)
{
dwStatus = GetLastError();
strcpy(rtnMd5, "ReadFile");
CryptReleaseContext(hProv, 0);
CryptDestroyHash(hHash);
CloseHandle(hFile);
return dwStatus;
}
cbHash = MD5LEN;
if (CryptGetHashParam(hHash, HP_HASHVAL, rgbHash, &cbHash, 0))
{
for (DWORD i = 0; i < cbHash; i++)
{
rtnMd5[i * 2] = rgbDigits[rgbHash[i] >> 4];
rtnMd5[(i * 2) + 1] = rgbDigits[rgbHash[i] & 0xf];
}
}
else
{
dwStatus = GetLastError();
strcpy(rtnMd5, "CryptGetHashParam");
}
CryptDestroyHash(hHash);
CryptReleaseContext(hProv, 0);
CloseHandle(hFile);
return dwStatus;
}
int GetWidePathFromFullWideFilename(wchar_t* filepath, wchar_t* rtnpath) {
wchar_t* offset = wcsrchr(filepath, L'\\');
wchar_t* off2 = wcsrchr(filepath, L'/');
offset = offset == NULL ? off2 : ((off2 != NULL && offset < off2) ? off2 : offset);
if (offset == NULL) {
return -1;
}
swprintf(rtnpath, offset - filepath + 2, filepath);
return 0;
}
LONG GetDWORDRegKey(HKEY hKey, wchar_t* strValueName, DWORD* nValue) {
DWORD dwBufferSize(sizeof(DWORD));
DWORD nResult(0);
LONG nError = ::RegQueryValueExW(hKey,
strValueName,
0,
NULL,
reinterpret_cast<LPBYTE>(&nResult),
&dwBufferSize);
if (ERROR_SUCCESS == nError)
{
*nValue = nResult;
}
return nError;
}
void pushHostLobby() {
char msg[100] = { 0x00, 0x43, 0x05 };
extern UINT g_port;
sprintf(msg + 3, "push clientlobby %d", g_port + 1);
unsigned short int remoteServerPort = 1001;
addDebugText("Pushing open lobby.");
int socketDescriptor;
struct sockaddr_in serverAddress;
if ((socketDescriptor = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
addDebugText("ERROR: Could not create socket.");
}
serverAddress.sin_family = AF_INET;
extern ULONG broadcast_server;
serverAddress.sin_addr.s_addr = broadcast_server;
serverAddress.sin_port = htons(remoteServerPort);
if (sendto(socketDescriptor, msg, strlen(msg + 3) + 3, 0, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) < 0) {
//returns -1 if it wasn't successful. Note that it doesn't return -1 if the connection couldn't be established (UDP)
addDebugText("ERROR: Failed to push open lobby.");
}
}