forked from efidemon69/Voyager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.c
51 lines (44 loc) · 1.47 KB
/
Utils.c
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
#include "Utils.h"
BOOLEAN CheckMask(CHAR8* base, CHAR8* pattern, CHAR8* mask)
{
for (; *mask; ++base, ++pattern, ++mask)
if (*mask == 'x' && *base != *pattern)
return FALSE;
return TRUE;
}
VOID* FindPattern(CHAR8* base, UINTN size, CHAR8* pattern, CHAR8* mask)
{
size -= AsciiStrLen(mask);
for (UINTN i = 0; i <= size; ++i)
{
VOID* addr = &base[i];
if (CheckMask(addr, pattern, mask))
return addr;
}
return NULL;
}
VOID* GetExport(UINT8* ModuleBase, CHAR8* export)
{
EFI_IMAGE_DOS_HEADER* dosHeaders = (EFI_IMAGE_DOS_HEADER*)ModuleBase;
if (dosHeaders->e_magic != EFI_IMAGE_DOS_SIGNATURE)
return NULL;
EFI_IMAGE_NT_HEADERS64* ntHeaders = (EFI_IMAGE_NT_HEADERS64*)(ModuleBase + dosHeaders->e_lfanew);
UINT32 exportsRva = ntHeaders->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
EFI_IMAGE_EXPORT_DIRECTORY* exports = (EFI_IMAGE_EXPORT_DIRECTORY*)(ModuleBase + exportsRva);
UINT32* nameRva = (UINT32*)(ModuleBase + exports->AddressOfNames);
for (UINT32 i = 0; i < exports->NumberOfNames; ++i)
{
CHAR8* func = (CHAR8*)(ModuleBase + nameRva[i]);
if (AsciiStrCmp(func, export) == 0)
{
UINT32* funcRva = (UINT32*)(ModuleBase + exports->AddressOfFunctions);
UINT16* ordinalRva = (UINT16*)(ModuleBase + exports->AddressOfNameOrdinals);
return (VOID*)(((UINT64)ModuleBase) + funcRva[ordinalRva[i]]);
}
}
return NULL;
}
VOID MemCopy(VOID* dest, VOID* src, UINTN size)
{
for (UINT8* d = dest, *s = src; size--; *d++ = *s++);
}