-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvorbisFile.cpp
More file actions
456 lines (381 loc) · 13.4 KB
/
vorbisFile.cpp
File metadata and controls
456 lines (381 loc) · 13.4 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// GTA: SA ASI Loader 1.2
// Written by Silent
// Based on ASI Loader by Stanislav "listener" Golovin
// Initialization part made by NTAuthority
// Modified by fastman92 to add sorting of ASI files by their name
#include <windows.h>
#include <iostream>
#include <algorithm>
#include <deque>
#include <string>
BYTE originalCode[5];
BYTE* originalEP = 0;
HINSTANCE hExecutableInstance;
//=============================================================================
// vorbisfile connector
extern "C" {
struct ov_callbacks {
size_t (*read_func) (void* ptr, size_t size, size_t nmemb, void* datasource);
int (*seek_func) (void* datasource, long long offset, int whence);
int (*close_func) (void* datasource);
long (*tell_func) (void* datasource);
};
void* __ov_open_callbacks;
__declspec(dllexport, naked) int ov_open_callbacks(void* datasource, void* vf, char* initial, long ibytes, ov_callbacks callbacks)
{ _asm jmp __ov_open_callbacks }
void* __ov_clear;
__declspec(dllexport, naked) int ov_clear(void* vf)
{ _asm jmp __ov_clear }
void* __ov_time_total;
__declspec(dllexport, naked) double ov_time_total (void* vf, int i)
{ _asm jmp __ov_time_total }
void* __ov_time_tell;
__declspec(dllexport, naked) double ov_time_tell(void* vf)
{ _asm jmp __ov_time_tell }
void* __ov_read;
__declspec(dllexport, naked) long ov_read(void* vf, char* buffer, int length, int bigendianp, int word, int sgned, int* bitstream)
{ _asm jmp __ov_read }
void* __ov_info;
__declspec(dllexport, naked) void* ov_info(void* vf, int link)
{ _asm jmp __ov_info }
void* __ov_time_seek;
__declspec(dllexport, naked) int ov_time_seek(void* vf, double pos)
{ _asm jmp __ov_time_seek }
void* __ov_time_seek_page;
__declspec(dllexport, naked) int ov_time_seek_page(void* vf, double pos)
{ _asm jmp __ov_time_seek_page }
}
void ShowErrorAndExit(const char * format, ...)
{
char buffer[4096];
va_list vl;
va_start(vl, format);
_vsnprintf_s(buffer, _countof(buffer), _TRUNCATE, format, vl);
MessageBoxA(NULL, buffer, "Silent ASI loader error", MB_OK);
ExitProcess(NULL);
va_end(vl);
}
struct ExcludedEntry {
char* entry;
ExcludedEntry* prev;
ExcludedEntry* next;
};
struct ExcludedEntriesList {
ExcludedEntry* first;
ExcludedEntry* last;
};
void ExcludedEntriesListInit(ExcludedEntriesList* list)
{
list->first = NULL;
list->last = NULL;
}
void ExcludedEntriesListPush(ExcludedEntriesList* list, const char* entryName)
{
ExcludedEntry* newEntry = (ExcludedEntry*)malloc(sizeof(ExcludedEntry));
int length = strlen(entryName) + 1;
if ( !list->first )
list->first = newEntry;
else
list->last->next = newEntry;
newEntry->prev = list->last;
newEntry->next = NULL;
list->last = newEntry;
newEntry->entry = (char*)malloc(length);
strncpy(newEntry->entry, entryName, length);
}
bool ExcludedEntriesListHasEntry(ExcludedEntriesList* list, const char* entryName)
{
ExcludedEntry* it = list->first;
while ( it )
{
if ( !_stricmp(it->entry, entryName) )
{
// It has an entry, we can pop it now
if ( it->next )
it->next->prev = it->prev;
if ( it->prev )
it->prev->next = it->next;
if ( list->first == it )
list->first = it->next;
free(it->entry);
free(it);
return true;
}
it = it->next;
}
return false;
}
void ExcludedEntriesListFree(ExcludedEntriesList* list)
{
ExcludedEntry* it = list->first;
while ( it )
{
ExcludedEntry* nextEntry = it->next;
free(it->entry);
free(it);
it = nextEntry;
}
}
struct tPluginToBeLoaded
{
std::string dirPath;
std::string pluginFilename;
};
void FindFiles(std::deque<tPluginToBeLoaded>& pluginPaths, WIN32_FIND_DATAA* fd, ExcludedEntriesList* list)
{
char curDir[MAX_PATH];
GetCurrentDirectoryA(_countof(curDir), curDir);
HANDLE asiFile = FindFirstFileA ("*.asi", fd);
if (asiFile != INVALID_HANDLE_VALUE)
{
do {
if (!(fd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
unsigned int pos = 5;
while (fd->cFileName[pos])
++pos;
if (fd->cFileName[pos-4] == '.' &&
(fd->cFileName[pos-3] == 'a' || fd->cFileName[pos-3] == 'A') &&
(fd->cFileName[pos-2] == 's' || fd->cFileName[pos-2] == 'S') &&
(fd->cFileName[pos-1] == 'i' || fd->cFileName[pos-1] == 'I'))
{
if (!list || !ExcludedEntriesListHasEntry(list, fd->cFileName))
{
tPluginToBeLoaded plugin;
plugin.dirPath = curDir;
plugin.pluginFilename = fd->cFileName;
pluginPaths.push_back(plugin);
}
}
}
} while (FindNextFileA (asiFile, fd));
FindClose (asiFile);
}
}
void LoadPlugins()
{
HMODULE vorbisHooked = LoadLibraryA ("vorbishooked");
if ( vorbisHooked )
{
__ov_open_callbacks = GetProcAddress (vorbisHooked, "ov_open_callbacks");
__ov_clear = GetProcAddress (vorbisHooked, "ov_clear");
__ov_time_total = GetProcAddress (vorbisHooked, "ov_time_total");
__ov_time_tell = GetProcAddress (vorbisHooked, "ov_time_tell");
__ov_read = GetProcAddress (vorbisHooked, "ov_read");
__ov_info = GetProcAddress (vorbisHooked, "ov_info");
__ov_time_seek = GetProcAddress (vorbisHooked, "ov_time_seek");
__ov_time_seek_page = GetProcAddress (vorbisHooked, "ov_time_seek_page");
// Regular ASI Loader
WIN32_FIND_DATAA fd;
std::deque<tPluginToBeLoaded> pluginsToBeLoaded;
char moduleName[MAX_PATH];
char preparedPath[128]; // stores scripts\*exename*\settings.ini
char* tempPointer;
int nWantsToLoadPlugins;
int nThatExeWantsPlugins;
GetModuleFileNameA(NULL, moduleName, MAX_PATH);
tempPointer = strrchr(moduleName, '.');
*tempPointer = '\0';
tempPointer = strrchr(moduleName, '\\');
strncpy(preparedPath, "scripts", 8);
strcat(preparedPath, tempPointer);
strcat(preparedPath, "\\settings.ini");
// Before we load any ASI files, let's see if user wants to do it at all
nWantsToLoadPlugins = GetPrivateProfileIntA("globalsets", "loadplugins", TRUE, "scripts\\global.ini");
// Or perhaps this EXE wants to override global settings?
nThatExeWantsPlugins = GetPrivateProfileIntA("exclusivesets", "loadplugins", -1, preparedPath);
if ( nThatExeWantsPlugins ) // Will not process only if this EXE wishes not to load anything but its exclusive plugins
{
if ( nWantsToLoadPlugins || nThatExeWantsPlugins == TRUE )
{
// Load excludes
ExcludedEntriesList excludes;
ExcludedEntriesListInit(&excludes);
if ( FILE* iniFile = fopen(preparedPath, "rt") )
{
char line[256];
bool bItsExcludesList = false;
while ( fgets(line, 256, iniFile) )
{
char* newline = strchr(line, '\n');
if ( newline )
*newline = '\0';
if ( bItsExcludesList )
{
if ( line[0] && line[0] != ';' )
ExcludedEntriesListPush(&excludes, line);
}
else
{
if ( !_stricmp(line, "[excludes]") )
bItsExcludesList = true;
}
}
fclose(iniFile);
}
FindFiles(pluginsToBeLoaded, &fd, &excludes);
if ( SetCurrentDirectoryA("scripts\\") )
{
FindFiles(pluginsToBeLoaded, &fd, &excludes);
if ( SetCurrentDirectoryA(tempPointer + 1) )
{
FindFiles(pluginsToBeLoaded, &fd, NULL); // Exclusive plugins are not being excluded
SetCurrentDirectoryA("..\\..\\");
}
else
SetCurrentDirectoryA("..\\");
}
// Free the remaining excludes
ExcludedEntriesListFree(&excludes);
}
}
else
{
// Load only exclusive plugins, if exists
// We need to cut settings.ini from the path again
tempPointer = strrchr(preparedPath, '\\');
tempPointer[1] = '\0';
if ( SetCurrentDirectoryA(preparedPath) )
{
FindFiles(pluginsToBeLoaded, &fd, NULL);
SetCurrentDirectoryA("..\\..\\");
}
}
// Prevent loading of ASI plugins with the same file name
for (auto& plugin : pluginsToBeLoaded)
{
for (auto& plugin2 : pluginsToBeLoaded)
{
if (&plugin != &plugin2 && !_stricmp(plugin.pluginFilename.c_str(), plugin2.pluginFilename.c_str()))
ShowErrorAndExit("An ASI with name %s exists in more than one directory.\n\nDirectories:\n%s\n%s",
plugin.pluginFilename.c_str(),
plugin.dirPath.c_str(), plugin2.dirPath.c_str()
);
}
}
// Load ASI plugins
std::sort(pluginsToBeLoaded.begin(), pluginsToBeLoaded.end(), [](tPluginToBeLoaded& a, tPluginToBeLoaded& b) {
return _stricmp(a.pluginFilename.c_str(), b.pluginFilename.c_str()) < 0;
});
for (auto& plugin : pluginsToBeLoaded)
{
char pluginPath[MAX_PATH];
sprintf(pluginPath, "%s\\%s", plugin.dirPath.c_str(), plugin.pluginFilename.c_str());
// MessageBoxA(NULL, pluginPath, "Test", MB_OK);
LoadLibraryA(pluginPath);
}
// Use if debugging
// ExitProcess(NULL);
}
// Unprotect the module NOW (CLEO 4.1.1.30f crash fix)
IMAGE_NT_HEADERS* ntHeader = (IMAGE_NT_HEADERS*)((DWORD)hExecutableInstance + ((IMAGE_DOS_HEADER*)hExecutableInstance)->e_lfanew);
SIZE_T size = ntHeader->OptionalHeader.SizeOfImage;
DWORD oldProtect;
VirtualProtect((VOID*)hExecutableInstance, size, PAGE_EXECUTE_READWRITE, &oldProtect);
}
static bool bLoadedPluginsYet = false;
void WINAPI CustomGetStartupInfoA(LPSTARTUPINFOA lpStartupInfo)
{
if ( !bLoadedPluginsYet )
{
// At the time this is called, the EXE is fully decrypted - we don't need any tricks from the ASI side
LoadPlugins();
bLoadedPluginsYet = true;
}
GetStartupInfoA(lpStartupInfo);
}
void WINAPI CustomGetStartupInfoW(LPSTARTUPINFOW lpStartupInfo)
{
if ( !bLoadedPluginsYet )
{
// At the time this is called, the EXE is fully decrypted - we don't need any tricks from the ASI side
LoadPlugins();
bLoadedPluginsYet = true;
}
GetStartupInfoW(lpStartupInfo);
}
void PatchIAT()
{
// Find IAT
IMAGE_NT_HEADERS* ntHeader = (IMAGE_NT_HEADERS*)((DWORD)hExecutableInstance + ((IMAGE_DOS_HEADER*)hExecutableInstance)->e_lfanew);
IMAGE_IMPORT_DESCRIPTOR* pImports = (IMAGE_IMPORT_DESCRIPTOR*)((DWORD)hExecutableInstance + ntHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
DWORD nNumImports = ntHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size / sizeof(IMAGE_IMPORT_DESCRIPTOR) - 1;
// Find kernel32.dll
for ( DWORD i = 0; i < nNumImports; i++ )
{
if ( !_stricmp((const char*)((DWORD)hExecutableInstance + pImports->Name), "KERNEL32.DLL") )
{
IMAGE_IMPORT_BY_NAME** pFunctions = (IMAGE_IMPORT_BY_NAME**)((DWORD)hExecutableInstance + pImports->OriginalFirstThunk);
// kernel32.dll found, find GetStartupInfoA
for ( DWORD j = 0; pFunctions[j]; j++ )
{
if ( !_stricmp((const char*)((DWORD)hExecutableInstance + pFunctions[j]->Name), "GetStartupInfoA") )
{
// Overwrite the address with the address to a custom GetStartupInfoA
DWORD dwProtect[2];
DWORD* pAddress = &((DWORD*)((DWORD)hExecutableInstance + pImports->FirstThunk))[j];
VirtualProtect(pAddress, sizeof(DWORD), PAGE_EXECUTE_READWRITE, &dwProtect[0]);
*pAddress = (DWORD)CustomGetStartupInfoA;
VirtualProtect(pAddress, sizeof(DWORD), dwProtect[0], &dwProtect[1]);
// return to the original EP
*(DWORD*)originalEP = *(DWORD*)&originalCode;
*(BYTE*)(originalEP+4) = originalCode[4];
return;
}
// For new SA Steam EXE
if ( !_stricmp((const char*)((DWORD)hExecutableInstance + pFunctions[j]->Name), "GetStartupInfoW") )
{
// Overwrite the address with the address to a custom GetStartupInfoA
DWORD dwProtect[2];
DWORD* pAddress = &((DWORD*)((DWORD)hExecutableInstance + pImports->FirstThunk))[j];
VirtualProtect(pAddress, sizeof(DWORD), PAGE_EXECUTE_READWRITE, &dwProtect[0]);
*pAddress = (DWORD)CustomGetStartupInfoW;
VirtualProtect(pAddress, sizeof(DWORD), dwProtect[0], &dwProtect[1]);
// return to the original EP
*(DWORD*)originalEP = *(DWORD*)&originalCode;
*(BYTE*)(originalEP+4) = originalCode[4];
return;
}
}
}
pImports++;
}
// No luck. Oh well.
// return to the original EP anyway
*(DWORD*)originalEP = *(DWORD*)&originalCode;
*(BYTE*)(originalEP+4) = originalCode[4];
}
void __declspec(naked) Main_DoInit()
{
_asm
{
call PatchIAT
jmp originalEP
}
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
if ( ul_reason_for_call == DLL_PROCESS_ATTACH )
{
hExecutableInstance = GetModuleHandle(NULL); // passing NULL should be safe even with the loader lock being held (according to ReactOS ldr.c)
if (hExecutableInstance)
{
IMAGE_NT_HEADERS* ntHeader = (IMAGE_NT_HEADERS*)((DWORD)hExecutableInstance + ((IMAGE_DOS_HEADER*)hExecutableInstance)->e_lfanew);
BYTE* ep = (BYTE*)((DWORD)hExecutableInstance + ntHeader->OptionalHeader.AddressOfEntryPoint);
// Unprotect the entry point
DWORD oldProtect;
VirtualProtect(ep, 5, PAGE_EXECUTE_READWRITE, &oldProtect);
// back up original code
*(DWORD*)&originalCode = *(DWORD*)ep;
originalCode[4] = *(ep+4);
// patch to call our EP
int newEP = (int)Main_DoInit - ((int)ep + 5);
ep[0] = 0xE9;
*(int*)&ep[1] = newEP;
originalEP = ep;
}
}
return TRUE;
}