Skip to content

Commit 6992ae6

Browse files
committed
Clean up hook structure
1 parent 00b0bb2 commit 6992ae6

8 files changed

Lines changed: 156 additions & 13 deletions

File tree

ExtFileSystem/CSVParserData.h

Lines changed: 0 additions & 10 deletions
This file was deleted.

ExtFileSystem/CSVParserHooks.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
3+
#include "ICsvParser.h"
4+
#include "FileMemory.h"
5+
#include "hook.h"
6+
#include "NamedFile.h"
7+
8+
struct CSVParserData
9+
{
10+
ICsvParser *csv_parser;
11+
void *data;
12+
size_t data_length;
13+
};
14+
15+
DEF_HOOK(CSVParserData*, DLL_CSV_CreateCsvParser)
16+
{
17+
return nullptr;
18+
}
19+
20+
DEF_HOOK(bool, DLL_CSV_Open, CSVParserData* parser_data, FileMemory* file)
21+
{
22+
auto named_file = dynamic_cast<NamedFile*>(file);
23+
24+
if (!named_file)
25+
return DLL_CSV_Open_original(parser_data, file);
26+
27+
28+
29+
return true;
30+
}

ExtFileSystem/ExtFileSystem.vcxproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,11 @@
240240
</Link>
241241
</ItemDefinitionGroup>
242242
<ItemGroup>
243+
<ClInclude Include="CSVParserHooks.h" />
244+
<ClInclude Include="FileSystemHooks.h" />
245+
<ClInclude Include="heap.h" />
243246
<ClInclude Include="ICsvParser.h" />
244247
<ClInclude Include="NamedFile.h" />
245-
<ClInclude Include="CSVParserData.h" />
246248
<ClInclude Include="ProxyFileSystem.h" />
247249
<ClInclude Include="FileMemory.h" />
248250
<ClInclude Include="hook.h" />

ExtFileSystem/ExtFileSystem.vcxproj.filters

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
<Filter Include="Header Files\CSV">
2626
<UniqueIdentifier>{533d1c8e-2d7a-4939-82ae-677564ed8a68}</UniqueIdentifier>
2727
</Filter>
28+
<Filter Include="Header Files\Hooks">
29+
<UniqueIdentifier>{9fdf6ea0-642f-4bba-9131-fe8f2625db96}</UniqueIdentifier>
30+
</Filter>
2831
</ItemGroup>
2932
<ItemGroup>
3033
<ClInclude Include="logging.h">
@@ -48,8 +51,14 @@
4851
<ClInclude Include="NamedFile.h">
4952
<Filter>Header Files\FilePointers</Filter>
5053
</ClInclude>
51-
<ClInclude Include="CSVParserData.h">
52-
<Filter>Header Files\CSV</Filter>
54+
<ClInclude Include="FileSystemHooks.h">
55+
<Filter>Header Files\Hooks</Filter>
56+
</ClInclude>
57+
<ClInclude Include="heap.h">
58+
<Filter>Header Files\Util</Filter>
59+
</ClInclude>
60+
<ClInclude Include="CSVParserHooks.h">
61+
<Filter>Header Files\Hooks</Filter>
5362
</ClInclude>
5463
</ItemGroup>
5564
<ItemGroup>

ExtFileSystem/FileSystemHooks.h

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#pragma once
2+
#include "ProxyFileSystem.h"
3+
#include "logging.h"
4+
#include "hook.h"
5+
6+
struct FSArchive
7+
{
8+
void* fs_object;
9+
int fs_type;
10+
};
11+
12+
static bool v_table_initialized = false;
13+
static void** FSArchiveVTable = nullptr;
14+
static void** FSArchiveIOVTable = nullptr;
15+
static void** FSArchiveWideFsTable = nullptr;
16+
static std::vector<ExtArchiveData*> extraArchiveDatas;
17+
18+
inline void init_virutal_tables(void** fs_object)
19+
{
20+
#define CPY_VTABLE(dest, index, size) \
21+
auto dest##_temp = reinterpret_cast<void**>(fs_object[index]);\
22+
dest = new void*[size]; \
23+
memcpy(dest, dest##_temp, size * sizeof(void*));
24+
25+
#define SET(dest, index, func) \
26+
original_functions.func = reinterpret_cast<func##_t>(dest##_temp[index]); \
27+
dest[index] = &func;
28+
29+
CPY_VTABLE(FSArchiveVTable, 0, 15);
30+
31+
//SET(FSArchiveVTable, 5, SetBaseDirectory);
32+
//SET(FSArchiveVTable, 9, AddArchive);
33+
SET(FSArchiveVTable, 10, AddAutoPathForAllFolder);
34+
SET(FSArchiveVTable, 13, AddAutoPath);
35+
36+
CPY_VTABLE(FSArchiveIOVTable, 4, 3);
37+
38+
SET(FSArchiveIOVTable, 0, GetFile);
39+
SET(FSArchiveIOVTable, 1, FileExists);
40+
SET(FSArchiveIOVTable, 2, CreateList);
41+
42+
CPY_VTABLE(FSArchiveWideFsTable, 2, 3);
43+
44+
SET(FSArchiveWideFsTable, 0, GetFileWide);
45+
SET(FSArchiveWideFsTable, 1, FileExistsWide);
46+
SET(FSArchiveWideFsTable, 2, CreateListWide);
47+
48+
#undef CPY_VTABLE
49+
#undef SET
50+
}
51+
52+
DEF_HOOK(void, DLL_FileSystem_CreateFileSystemArchive, FSArchive* dest)
53+
{
54+
LOG("Creating file system. Struct address: " << std::hex << dest);
55+
DLL_FileSystem_CreateFileSystemArchive_original(dest);
56+
57+
if (dest->fs_object == nullptr)
58+
return;
59+
60+
auto fs_obj = reinterpret_cast<void**>(dest->fs_object);
61+
62+
LOG("Filesystem object at: " << std::hex << fs_obj);
63+
64+
if (!v_table_initialized)
65+
{
66+
LOG("Loading virtual tables!");
67+
init_virutal_tables(fs_obj);
68+
v_table_initialized = true;
69+
LOG("Virtual tables loaded and copied over!");
70+
}
71+
72+
// Create a carbon copy of the archive object
73+
auto new_obj = new void* [FS_ARCHIVE_OBJECT_BASE_SIZE + 1];
74+
memcpy(new_obj, fs_obj, FS_ARCHIVE_OBJECT_BASE_SIZE * sizeof(void*));
75+
76+
// Redirect the vtables
77+
new_obj[0] = FSArchiveVTable;
78+
new_obj[2] = FSArchiveWideFsTable;
79+
new_obj[4] = FSArchiveIOVTable;
80+
81+
// Create external data to store pointers to global data
82+
auto ext_data = new ExtArchiveData(proxy_path, original_functions, name_to_full_map);
83+
ext_data->base = new_obj;
84+
ext_data->log_stream = LogStream;
85+
86+
extraArchiveDatas.push_back(ext_data);
87+
88+
// Append extra data to the new object
89+
new_obj[FS_ARCHIVE_OBJECT_BASE_SIZE] = ext_data;
90+
91+
LOG("Freeing up original archive object!");
92+
HeapFree(heap, 0, fs_obj);
93+
LOG("Object freed!");
94+
95+
// Return the hooked object
96+
dest->fs_object = new_obj;
97+
}

ExtFileSystem/Main.cpp

-6.23 KB
Binary file not shown.

ExtFileSystem/heap.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include <windows.h>
4+
5+
static HANDLE heap;
6+
7+
inline void initialize_heap()
8+
{
9+
heap = GetProcessHeap();
10+
}

ExtFileSystem/hook.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
#include <windows.h>
44

5+
#define DEF_HOOK(ret_type, func, ...) \
6+
typedef ret_type (*func##_original_t)(__VA_ARGS__);\
7+
static func##_original_t func##_original = nullptr;\
8+
ret_type func##_hook(__VA_ARGS__)
9+
510
#define RVA2PTR(t, base, rva) ((t)(((char*)base) + rva))
611

712
inline BOOL patch_cur_iat(HMODULE hmodule, char const* targetModule, void* targetFunction, void* hook)

0 commit comments

Comments
 (0)