- 1
Пиздец-оффтоп #129
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
0
Пиздец-оффтоп #129
#99: https://govnokod.ru/28936 https://govnokod.xyz/_28936
#100: https://govnokod.ru/28940 https://govnokod.xyz/_28940
#101: https://govnokod.ru/28949 https://govnokod.xyz/_28949
#102: https://govnokod.ru/28978 https://govnokod.xyz/_28978
#103: https://govnokod.ru/28982 https://govnokod.xyz/_28982
#104: https://govnokod.ru/28989 https://govnokod.xyz/_28989
#105: https://govnokod.ru/29052 https://govnokod.xyz/_29052
#106: https://govnokod.ru/29069 https://govnokod.xyz/_29069
#107: https://govnokod.ru/29086 https://govnokod.xyz/_29086
#108: https://govnokod.ru/29102 https://govnokod.xyz/_29102
#109: https://govnokod.ru/29126 https://govnokod.xyz/_29126
#110: https://govnokod.ru/29136 https://govnokod.xyz/_29136
#111: https://govnokod.ru/29142 https://govnokod.xyz/_29142
#112: https://govnokod.ru/29155 https://govnokod.xyz/_29155
#113: https://govnokod.ru/29160 https://govnokod.xyz/_29160
#114: https://govnokod.ru/29165 https://govnokod.xyz/_29165
#115: https://govnokod.ru/29173 https://govnokod.xyz/_29173
#116: https://govnokod.ru/29174 https://govnokod.xyz/_29174
#117: https://govnokod.ru/29182 https://govnokod.xyz/_29182
#118: https://govnokod.ru/29191 https://govnokod.xyz/_29191
#119: https://govnokod.ru/29196 https://govnokod.xyz/_29196
#120: https://govnokod.ru/29205 https://govnokod.xyz/_29205
#121: https://govnokod.ru/29216 https://govnokod.xyz/_29216
#122: https://govnokod.ru/29219 https://govnokod.xyz/_29219
#123: https://govnokod.ru/29232 https://govnokod.xyz/_29232
#124: https://govnokod.ru/29237 https://govnokod.xyz/_29237
#125: https://govnokod.ru/29239 https://govnokod.xyz/_29239
#126: https://govnokod.ru/29244 https://govnokod.xyz/_29244
#127: https://govnokod.ru/29248 https://govnokod.xyz/_29248
#128: https://govnokod.ru/29251 https://govnokod.xyz/_29251
0
#define IMAGE_BASE_RELOC_TYPE 0x0C
#define IMAGE_BASE_RELOC_OFFSET 0x0FFF
#define IMAGE_GET_BASE_RELOC_TYPE(entry) entry >> IMAGE_BASE_RELOC_TYPE
#define IMAGE_GET_BASE_RELOC_OFFSET(entry) entry & IMAGE_BASE_RELOC_OFFSET
__declspec(safebuffers) __declspec(noinline) DWORD mapping_code(LPVOID map_struct)
{
auto map = static_cast<MM_DATA*>(map_struct);
auto base = map->base;
auto dos = reinterpret_cast<PIMAGE_DOS_HEADER>(base);
auto nt_headers = reinterpret_cast<PIMAGE_NT_HEADERS>(base + dos->e_lfanew);
auto opt_header = &nt_headers->OptionalHeader;
auto file_header = &nt_headers->FileHeader;
const bool has_entry_point = opt_header->AddressOfEntryPoint != 0;
auto dll_main = reinterpret_cast<DllMainFn>(base + opt_header->AddressOfEntryPoint);
auto load_library = map->load_library;
auto get_proc_address = map->get_proc_address;
auto virtual_protect = map->virtual_protect;
auto rtl_add_function_table = map->rtl_add_function_table;
auto reloc_dir = opt_header->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
auto location_delta = reinterpret_cast<uintptr_t>(base) - static_cast<uintptr_t>(opt_header->ImageBase);
if (location_delta && reloc_dir.Size)
{
auto reloc_begin = reinterpret_cast<PIMAGE_BASE_RELOCATION>(base + reloc_dir.VirtualAddress);
auto reloc_end = reinterpret_cast<PIMAGE_BASE_RELOCATION>(reinterpret_cast<std::byte*>(reloc_begin) + reloc_dir.Size);
while (reloc_begin < reloc_end && reloc_begin->SizeOfBlock)
{
auto amount_of_entries = (reloc_begin->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / sizeof(WORD);
auto entries = reinterpret_cast<PWORD>(reloc_begin + 1);
for (size_t i = 0; i < amount_of_entries; i++)
{
WORD type = IMAGE_GET_BASE_RELOC_TYPE(entries[i]);
WORD offset = IMAGE_GET_BASE_RELOC_OFFSET(entries[i]);
if (type == IMAGE_REL_BASED_DIR64)
{
uintptr_t* path_at = reinterpret_cast<uintptr_t*>(base + reloc_begin->VirtualAddress + offset);
*path_at += location_delta;
}
}
reloc_begin = reinterpret_cast<PIMAGE_BASE_RELOCATION>(reinterpret_cast<std::byte*>(reloc_begin) + reloc_begin->SizeOfBlock);
}
}
auto import_dir = opt_header->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
if (import_dir.Size)
{
auto import_desc = reinterpret_cast<PIMAGE_IMPORT_DESCRIPTOR>(base + import_dir.VirtualAddress);
while (import_desc->Name)
{
auto module_name = reinterpret_cast<char*>(base + import_desc->Name);
HMODULE module = load_library(module_name);
if (!module)
{
map->status = LOAD_LIBRARY_FAILED;
return -2;
}
auto INT_TABLE = reinterpret_cast<PIMAGE_THUNK_DATA>(base + (import_desc->OriginalFirstThunk ? import_desc->OriginalFirstThunk : import_desc->FirstThunk));
auto IAT_TABLE = reinterpret_cast<PIMAGE_THUNK_DATA>(base + (import_desc->FirstThunk));
for (; INT_TABLE->u1.AddressOfData ; INT_TABLE++, IAT_TABLE++)
{
FARPROC address = IMAGE_SNAP_BY_ORDINAL(INT_TABLE->u1.Ordinal) ?
get_proc_address(module, reinterpret_cast<char*>(IMAGE_ORDINAL(INT_TABLE->u1.Ordinal))) :
get_proc_address(module, reinterpret_cast<PIMAGE_IMPORT_BY_NAME>(base + INT_TABLE->u1.AddressOfData)->Name);
if (!address)
{
map->status = GET_PROC_ADDRESS_FAILED;
return -3;
}
IAT_TABLE->u1.Function = reinterpret_cast<uintptr_t>(address);
}
++import_desc;
}
}
auto delay_dir = opt_header->DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT];
if (delay_dir.Size)
{
auto delay_desc = reinterpret_cast<PIMAGE_DELAYLOAD_DESCRIPTOR>(base + delay_dir.VirtualAddress);
while (delay_desc->DllNameRVA)
{