--- Daodan/src/Daodan_Patch.c 2014/04/07 10:33:27 994 +++ Daodan/src/Daodan_Patch.c 2016/08/28 16:01:38 1045 @@ -41,28 +41,11 @@ bool DDrPatch_MakeCall(void* from, void* void* DDrPatch_MakeDetour(void* from, void* to) { int len = 0; + /* - DISASM MyDisasm; - int i = 0; - STARTUPMESSAGE("", 0); - STARTUPMESSAGE("", 0); - - memset (&MyDisasm, 0, sizeof(DISASM)); - MyDisasm.EIP = (UIntPtr) from; - i = 0; - STARTUPMESSAGE("Orig before @ 0x%06x", from); - while (i<10){ - len = Disasm(&MyDisasm); - if (len != UNKNOWN_OPCODE) { - STARTUPMESSAGE("%s, Opcode: 0x%x, len: %d, branch: %d, to: 0x%06x", MyDisasm.CompleteInstr, MyDisasm.Instruction.Opcode, len, MyDisasm.Instruction.BranchType, MyDisasm.Instruction.AddrValue); - STARTUPMESSAGE(" Cat: 0x%04x, prefix count: %d", MyDisasm.Instruction.Category & 0xffff, MyDisasm.Prefix.Number ); - MyDisasm.EIP += (UIntPtr)len; - i++; - } - }; - STARTUPMESSAGE("", 0); + STARTUPMESSAGE("Orig before", 0); + DDrPatch_PrintDisasm(from, 10, 0); */ - DISASM disasm; memset(&disasm, 0, sizeof(DISASM)); disasm.EIP = (UIntPtr) from; @@ -82,7 +65,7 @@ void* DDrPatch_MakeDetour(void* from, vo } branches++; int target = disasm.Instruction.AddrValue; - bool targetInTrampoline = ((void*)disasm.Instruction.AddrValue - from) < 5; + bool targetInTrampoline = ((void*)((int)disasm.Instruction.AddrValue) - from) < 5; switch (disasm.Instruction.BranchType) { case JmpType: case CallType: @@ -174,52 +157,17 @@ void* DDrPatch_MakeDetour(void* from, vo return (void*)-1; } DDrPatch_MakeJump(from, to); + /* - memset (&MyDisasm, 0, sizeof(DISASM)); - MyDisasm.EIP = (UIntPtr) trampoline; - i = 0; - STARTUPMESSAGE("Trampoline @ 0x%06x", trampoline); - while (i<10){ - len = Disasm(&MyDisasm); - if (len != UNKNOWN_OPCODE) { - STARTUPMESSAGE("%s", MyDisasm.CompleteInstr); - MyDisasm.EIP += (UIntPtr)len; - i++; - } - }; - STARTUPMESSAGE("", 0); - - memset (&MyDisasm, 0, sizeof(DISASM)); - MyDisasm.EIP = disasm.EIP; - i = 0; - STARTUPMESSAGE("Orig after @ 0x%06x", disasm.EIP); - while (i<7){ - len = Disasm(&MyDisasm); - if (len != UNKNOWN_OPCODE) { - STARTUPMESSAGE("%s", MyDisasm.CompleteInstr); - MyDisasm.EIP += (UIntPtr)len; - i++; - } - }; - STARTUPMESSAGE("", 0); - - memset (&MyDisasm, 0, sizeof(DISASM)); - MyDisasm.EIP = (UIntPtr) from; - i = 0; - STARTUPMESSAGE("Orig start after @ 0x%06x", from); - while (i<3){ - len = Disasm(&MyDisasm); - if (len != UNKNOWN_OPCODE) { - STARTUPMESSAGE("%s", MyDisasm.CompleteInstr); - MyDisasm.EIP += (UIntPtr)len; - i++; - } - }; - STARTUPMESSAGE("", 0); - STARTUPMESSAGE("", 0); - STARTUPMESSAGE("", 0); -*/ + STARTUPMESSAGE("Trampoline", 0); + DDrPatch_PrintDisasm(trampoline, 10, 6); + + STARTUPMESSAGE("Orig after", 0); + DDrPatch_PrintDisasm(disasm.EIP, 7, 0); + STARTUPMESSAGE("Orig start after", 0); + DDrPatch_PrintDisasm(from, 3, 6); +*/ return trampoline; } @@ -293,6 +241,27 @@ bool DDrPatch_NOOP(char* dest, unsigned return false; } +void* DDrPatch_ExecutableASM(char* from, char* nextInst, const unsigned char* code, int length) +{ + char* newCode = malloc(length+5); + if (!DDrPatch_NOOP(newCode, length+5)) + return (void*)-1; + + memcpy(newCode, code, length); + if (!DDrPatch_MakeJump(&newCode[length], nextInst)) + return (void*)-1; + + DWORD oldp; + if (!VirtualProtect(newCode, length+5, PAGE_EXECUTE_READWRITE, &oldp)) { + STARTUPMESSAGE("ExecASM: Could not mark page for new code as executable: from address 0x%08x", from); + return (void*)-1; + } + + if (!DDrPatch_MakeJump(from, newCode)) + return (void*)-1; + + return newCode; +} void DDrPatch_PrintDisasm(void* addr, int instLimit, int sizeLimit) {