extern "C" DLL_EXPORT uint _dbg_getbranchdestination(uint addr) { DISASM_INSTR instr; memset(&instr, 0, sizeof(instr)); disasmget(addr, &instr); if(instr.type != instr_branch) return 0; if(strstr(instr.instruction, "ret")) { uint atcsp = DbgValFromString("@csp"); if(DbgMemIsValidReadPtr(atcsp)) return atcsp; else return 0; } else if(instr.arg[0].type == arg_memory) return instr.arg[0].memvalue; else return instr.arg[0].value; }
void ExportPatch(const std::wstring & templateContent, DBGPATCHINFO* patchList, size_t numPatches) { printFileName(); size_t idx_template = templateContent.find(L"$TEMPLATE_PREFIX:"); size_t idx_module = templateContent.find(L"$MODULE_PREFIX:"); size_t idx_patch = templateContent.find(L"$PATCH:"); size_t idx_module_suffix = templateContent.find(L"$MODULE_SUFFIX:"); size_t idx_template_suffix = templateContent.find(L"$TEMPLATE_SUFFIX:"); if(idx_template == std::wstring::npos || idx_module == std::wstring::npos || idx_patch == std::wstring::npos || idx_module_suffix == std::wstring::npos || idx_template_suffix == std::wstring::npos) { MessageBox(hwndDlg, LoadWideString(IDS_INVALID_PATCH).c_str(), LoadWideString(IDS_PLUGNAME).c_str(), MB_ICONERROR); return; } HANDLE hProcess; wchar_t ProcessName[1024]; memset(ProcessName, 0, sizeof(ProcessName)); hProcess = (HANDLE)DbgValFromString("$hProcess"); if(GetModuleBaseNameW(hProcess, 0, ProcessName, sizeof(ProcessName) / sizeof(wchar_t)) == 0) { MessageBox(hwndDlg, LoadWideString(IDS_HPROCESSFAIL).c_str(), LoadWideString(IDS_PLUGNAME).c_str(), MB_ICONERROR); return; } std::wstring text = templateContent.substr(idx_template + int(wcslen(L"$TEMPLATE_PREFIX:")), idx_module - idx_template - int(wcslen(L"$TEMPLATE_PREFIX:"))); std::wstring modulePrefix = templateContent.substr(idx_module + int(wcslen(L"$MODULE_PREFIX:")), idx_patch - idx_module - int(wcslen(L"$MODULE_PREFIX:"))); std::wstring patchText = templateContent.substr(idx_patch + int(wcslen(L"$PATCH:")), idx_module_suffix - idx_patch - int(wcslen(L"$PATCH:"))); std::wstring moduleSuffix = templateContent.substr(idx_module_suffix + int(wcslen(L"$MODULE_SUFFIX:")), idx_template_suffix - idx_module_suffix - int(wcslen(L"$MODULE_SUFFIX:"))); std::wstring templateSuffix = templateContent.substr(idx_template_suffix + int(wcslen(L"$TEMPLATE_SUFFIX:"))); std::vector<std::pair<std::wstring, unsigned int>> modules; std::string firstModuleUTF8(patchList[0].mod); std::wstring firstModuleUTF16; unsigned int currentModuleCount = 1; utf8::utf8to16(firstModuleUTF8.begin(), firstModuleUTF8.end(), std::back_inserter(firstModuleUTF16)); modules.push_back(std::make_pair(firstModuleUTF16, 1)); for(duint i = 1; i < numPatches; i++) { firstModuleUTF8 = std::string(patchList[i].mod); firstModuleUTF16.clear(); utf8::utf8to16(firstModuleUTF8.begin(), firstModuleUTF8.end(), std::back_inserter(firstModuleUTF16)); if(firstModuleUTF16.compare(modules.back().first) != 0) { modules.back().second = currentModuleCount; currentModuleCount = 1; modules.push_back(std::make_pair(firstModuleUTF16, 1)); } else currentModuleCount++; } modules.back().second = currentModuleCount; currentModuleCount = 0; duint patches = 0; duint modbase; unsigned int currentModule = 0; std::wstring moduleText; for(duint i = 0; i < numPatches; i++) { if(currentModuleCount == 0) { moduleText += modulePrefix + L"\r\n"; modbase = DbgFunctions()->ModBaseFromName(patchList[i].mod); } std::wstring patchText2(patchText); std::wstring newByteText(printByte(patchList[i].newbyte)); ReplaceWString(patchText2, L"$rva", printHex(patchList[i].addr - modbase)); ReplaceWString(patchText2, L"$newByte", newByteText); ReplaceWString(patchText2, L"$patchIndex", printInto(++patches)); moduleText += patchText2 + L"\r\n"; if(currentModuleCount == modules.at(currentModule).second - 1) { moduleText += moduleSuffix + L"\r\n"; ReplaceWString(moduleText, L"$moduleName", modules.at(currentModule).first); ReplaceWString(moduleText, L"$numPatches", printInto(modules.at(currentModule).second)); text += moduleText; moduleText.clear(); currentModuleCount = 0; patches = 0; currentModule++; } else currentModuleCount++; } text.append(templateSuffix); ReplaceWString(text, L"$numPatches", printInto(numPatches)); ReplaceWString(text, L"$exeName", std::wstring(ProcessName)); ReplaceWString(text, L"$date", printTime()); std::wstring compiledate; std::string compiledateASCII(__DATE__); utf8::utf8to16(compiledateASCII.begin(), compiledateASCII.end(), std::back_inserter(compiledate)); ReplaceWString(text, L"$compiledate", compiledate); // save if(SaveFile(exportedname, text)) _plugin_logputs(LoadUTF8String(IDS_SAVESUCCESS).c_str()); else _plugin_logputs(LoadUTF8String(IDS_SAVEFAIL).c_str()); }