From 3f35c5c2308249b36f5d4c9b439b29a83a17578b Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Sat, 25 Aug 2018 16:02:16 -0400 Subject: [PATCH] refactor --- GUI/host/textthread.cc | 5 +- GUI/host/textthread.h | 2 +- GUI/misc.cpp | 371 +++++++++++++++++++++-------------------- 3 files changed, 190 insertions(+), 188 deletions(-) diff --git a/GUI/host/textthread.cc b/GUI/host/textthread.cc index d53d41e..32fb3a7 100644 --- a/GUI/host/textthread.cc +++ b/GUI/host/textthread.cc @@ -27,10 +27,10 @@ std::wstring TextThread::GetStore() return storage; } -bool TextThread::Flush() +void TextThread::Flush() { LOCK ttLock(ttMutex); - if (timestamp - GetTickCount() < 250 || buffer.size() == 0) return true; // TODO: let user change delay before sentence is flushed + if (timestamp - GetTickCount() < 250 || buffer.size() == 0) return; // TODO: let user change delay before sentence is flushed std::wstring sentence; if (status & USING_UNICODE) { @@ -44,7 +44,6 @@ bool TextThread::Flush() } AddSentence(sentence); buffer.clear(); - return true; } void TextThread::AddSentence(std::wstring sentence) diff --git a/GUI/host/textthread.h b/GUI/host/textthread.h index 8b8a811..cba1090 100644 --- a/GUI/host/textthread.h +++ b/GUI/host/textthread.h @@ -24,7 +24,7 @@ public: void AddSentence(std::wstring sentence); private: - bool Flush(); + void Flush(); std::vector buffer; std::wstring storage; diff --git a/GUI/misc.cpp b/GUI/misc.cpp index 8930ecb..4b42290 100644 --- a/GUI/misc.cpp +++ b/GUI/misc.cpp @@ -30,119 +30,201 @@ std::unordered_map GetAllProcesses() return ret; } -DWORD Hash(QString module) +namespace { - module = module.toLower(); - DWORD hash = 0; - for (auto i : module) hash = _rotr(hash, 7) + i.unicode(); - return hash; -} + DWORD Hash(QString module) + { + module = module.toLower(); + DWORD hash = 0; + for (auto i : module) hash = _rotr(hash, 7) + i.unicode(); + return hash; + } -std::optional ParseRCode(QString RCode) -{ - HookParam hp = {}; - hp.type |= DIRECT_READ; - switch (RCode.at(0).unicode()) + std::optional ParseRCode(QString RCode) { - case L'S': - break; - case L'Q': - hp.type |= USING_STRING | USING_UNICODE; - break; - case L'V': - hp.type |= USING_STRING | USING_UTF8; - break; - default: - return {}; - } - RCode.remove(0, 1); - QRegExp stringGap("^\\-?[\\dA-F]+"); - if (stringGap.indexIn(RCode) == -1) return {}; - hp.offset = stringGap.cap(0).toInt(nullptr, 16); - RCode.remove(0, stringGap.cap(0).length()); - if (RCode.at(0).unicode() != L'@') return {}; - RCode.remove(0, 1); - QRegExp address("[\\dA-F]+$"); - if (address.indexIn(RCode) == -1) return {}; - hp.address = address.cap(0).toULongLong(nullptr, 16); - return hp; -} - -std::optional ParseHCode(QString HCode) -{ - HookParam hp = {}; - switch (HCode.at(0).unicode()) - { - case L'S': - hp.type |= USING_STRING; - break; - case L'A': - hp.type |= BIG_ENDIAN; - hp.length_offset = 1; - break; - case L'B': - hp.length_offset = 1; - break; - case L'Q': - hp.type |= USING_STRING | USING_UNICODE; - break; - case L'W': - hp.type |= USING_UNICODE; - hp.length_offset = 1; - break; - case L'V': - hp.type |= USING_STRING | USING_UTF8; - break; - default: - return {}; - } - HCode.remove(0, 1); - if (HCode.at(0).unicode() == L'N') - { - hp.type |= NO_CONTEXT; - HCode.remove(0, 1); - } - QRegExp dataOffset("^\\-?[\\dA-F]+"); - if (dataOffset.indexIn(HCode) == -1) return {}; - hp.offset = dataOffset.cap(0).toInt(nullptr, 16); - HCode.remove(0, dataOffset.cap(0).length()); - QRegExp dataIndirect("^\\*(\\-?[\\dA-F]+)"); - if (dataIndirect.indexIn(HCode) != -1) - { - hp.type |= DATA_INDIRECT; - hp.index = dataIndirect.cap(1).toInt(nullptr, 16); - HCode.remove(0, dataIndirect.cap(0).length()); - } - QRegExp split("^\\:(\\-?[\\dA-F]+)"); - if (split.indexIn(HCode) != -1) - { - hp.type |= USING_SPLIT; - hp.split = split.cap(1).toInt(nullptr, 16); - HCode.remove(0, split.cap(0).length()); - QRegExp splitIndirect("^\\*(\\-?[\\dA-F]+)"); - if (splitIndirect.indexIn(HCode) != -1) + HookParam hp = {}; + hp.type |= DIRECT_READ; + switch (RCode.at(0).unicode()) { - hp.type |= SPLIT_INDIRECT; - hp.split_index = splitIndirect.cap(1).toInt(nullptr, 16); - HCode.remove(0, splitIndirect.cap(0).length()); + case L'S': + break; + case L'Q': + hp.type |= USING_STRING | USING_UNICODE; + break; + case L'V': + hp.type |= USING_STRING | USING_UTF8; + break; + default: + return {}; } + RCode.remove(0, 1); + QRegExp stringGap("^\\-?[\\dA-F]+"); + if (stringGap.indexIn(RCode) == -1) return {}; + hp.offset = stringGap.cap(0).toInt(nullptr, 16); + RCode.remove(0, stringGap.cap(0).length()); + if (RCode.at(0).unicode() != L'@') return {}; + RCode.remove(0, 1); + QRegExp address("[\\dA-F]+$"); + if (address.indexIn(RCode) == -1) return {}; + hp.address = address.cap(0).toULongLong(nullptr, 16); + return hp; } - if (HCode.at(0).unicode() != L'@') return {}; - HCode.remove(0, 1); - QRegExp address("^([\\dA-F]+):?"); - if (address.indexIn(HCode) == -1) return {}; - hp.address = address.cap(1).toInt(nullptr, 16); - HCode.remove(address.cap(0)); - if (HCode.length()) + + std::optional ParseHCode(QString HCode) { - hp.type |= MODULE_OFFSET; - hp.module = Hash(HCode); + HookParam hp = {}; + switch (HCode.at(0).unicode()) + { + case L'S': + hp.type |= USING_STRING; + break; + case L'A': + hp.type |= BIG_ENDIAN; + hp.length_offset = 1; + break; + case L'B': + hp.length_offset = 1; + break; + case L'Q': + hp.type |= USING_STRING | USING_UNICODE; + break; + case L'W': + hp.type |= USING_UNICODE; + hp.length_offset = 1; + break; + case L'V': + hp.type |= USING_STRING | USING_UTF8; + break; + default: + return {}; + } + HCode.remove(0, 1); + if (HCode.at(0).unicode() == L'N') + { + hp.type |= NO_CONTEXT; + HCode.remove(0, 1); + } + QRegExp dataOffset("^\\-?[\\dA-F]+"); + if (dataOffset.indexIn(HCode) == -1) return {}; + hp.offset = dataOffset.cap(0).toInt(nullptr, 16); + HCode.remove(0, dataOffset.cap(0).length()); + QRegExp dataIndirect("^\\*(\\-?[\\dA-F]+)"); + if (dataIndirect.indexIn(HCode) != -1) + { + hp.type |= DATA_INDIRECT; + hp.index = dataIndirect.cap(1).toInt(nullptr, 16); + HCode.remove(0, dataIndirect.cap(0).length()); + } + QRegExp split("^\\:(\\-?[\\dA-F]+)"); + if (split.indexIn(HCode) != -1) + { + hp.type |= USING_SPLIT; + hp.split = split.cap(1).toInt(nullptr, 16); + HCode.remove(0, split.cap(0).length()); + QRegExp splitIndirect("^\\*(\\-?[\\dA-F]+)"); + if (splitIndirect.indexIn(HCode) != -1) + { + hp.type |= SPLIT_INDIRECT; + hp.split_index = splitIndirect.cap(1).toInt(nullptr, 16); + HCode.remove(0, splitIndirect.cap(0).length()); + } + } + if (HCode.at(0).unicode() != L'@') return {}; + HCode.remove(0, 1); + QRegExp address("^([\\dA-F]+):?"); + if (address.indexIn(HCode) == -1) return {}; + hp.address = address.cap(1).toInt(nullptr, 16); + HCode.remove(address.cap(0)); + if (HCode.length()) + { + hp.type |= MODULE_OFFSET; + hp.module = Hash(HCode); + } + if (hp.offset < 0) + hp.offset -= 4; + if (hp.split < 0) + hp.split -= 4; + return hp; + } + + QString GenerateHCode(HookParam hp, DWORD processId) + { + QString code = "/H"; + if (hp.type & USING_UNICODE) + { + if (hp.type & USING_STRING) + code += "Q"; + else + code += "W"; + } + else + { + if (hp.type & USING_UTF8) + code += "V"; + else if (hp.type & USING_STRING) + code += "S"; + else if (hp.type & BIG_ENDIAN) + code += "A"; + else + code += "B"; + } + if (hp.type & NO_CONTEXT) + code += "N"; + if (hp.offset < 0) hp.offset += 4; + if (hp.split < 0) hp.split += 4; + if (hp.offset < 0) + code += "-" + QString::number(-hp.offset, 16); + else + code += QString::number(hp.offset, 16); + if (hp.type & DATA_INDIRECT) + { + if (hp.index < 0) + code += "*-" + QString::number(-hp.index, 16); + else + code += "*" + QString::number(hp.index, 16); + } + if (hp.type & USING_SPLIT) + { + if (hp.split < 0) + code += ":-" + QString::number(-hp.split, 16); + else + code += ":" + QString::number(hp.split, 16); + } + if (hp.type & SPLIT_INDIRECT) + { + if (hp.split_index < 0) + code += "*-" + QString::number(-hp.split_index, 16); + else + code += "*" + QString::number(hp.split_index, 16); + } + code += "@"; + QString badCode = (code + QString::number(hp.address, 16)).toUpper(); + HANDLE processHandle; + if (!(processHandle = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, processId))) return badCode; + MEMORY_BASIC_INFORMATION info; + if (!VirtualQueryEx(processHandle, (LPCVOID)hp.address, &info, sizeof(info))) return badCode; + QString moduleName = GetModuleName(processId, (HMODULE)info.AllocationBase); + if (moduleName.size() == 0) return badCode; + code += QString::number(hp.address - (DWORD)info.AllocationBase, 16) + ":"; + code = code.toUpper(); + code += moduleName; + return code; + } + + QString GenerateRCode(HookParam hp) + { + QString code = "/R"; + if (hp.type & USING_UNICODE) + code += "Q"; + else if (hp.type & USING_UTF8) + code += "V"; + else + code += "S"; + code += QString::number(hp.offset, 16); + code += "@"; + code += QString::number(hp.address, 16); + return code.toUpper(); } - if (hp.offset < 0) - hp.offset -= 4; - if (hp.split < 0) - hp.split -= 4; - return hp; } std::optional ParseCode(QString code) @@ -153,85 +235,6 @@ std::optional ParseCode(QString code) else return {}; } -QString GenerateHCode(HookParam hp, DWORD processId) -{ - QString code = "/H"; - if (hp.type & USING_UNICODE) - { - if (hp.type & USING_STRING) - code += "Q"; - else - code += "W"; - } - else - { - if (hp.type & USING_UTF8) - code += "V"; - else if (hp.type & USING_STRING) - code += "S"; - else if (hp.type & BIG_ENDIAN) - code += "A"; - else - code += "B"; - } - if (hp.type & NO_CONTEXT) - code += "N"; - if (hp.offset < 0) hp.offset += 4; - if (hp.split < 0) hp.split += 4; - if (hp.offset < 0) - code += "-" + QString::number(-hp.offset, 16); - else - code += QString::number(hp.offset, 16); - if (hp.type & DATA_INDIRECT) - { - if (hp.index < 0) - code += "*-" + QString::number(-hp.index, 16); - else - code += "*" + QString::number(hp.index, 16); - } - if (hp.type & USING_SPLIT) - { - if (hp.split < 0) - code += ":-" + QString::number(-hp.split, 16); - else - code += ":" + QString::number(hp.split, 16); - } - if (hp.type & SPLIT_INDIRECT) - { - if (hp.split_index < 0) - code += "*-" + QString::number(-hp.split_index, 16); - else - code += "*" + QString::number(hp.split_index, 16); - } - code += "@"; - QString badCode = (code + QString::number(hp.address, 16)).toUpper(); - HANDLE processHandle; - if (!(processHandle = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, processId))) return badCode; - MEMORY_BASIC_INFORMATION info; - if (!VirtualQueryEx(processHandle, (LPCVOID)hp.address, &info, sizeof(info))) return badCode; - QString moduleName = GetModuleName(processId, (HMODULE)info.AllocationBase); - if (moduleName.size() == 0) return badCode; - code += QString::number(hp.address - (DWORD)info.AllocationBase, 16) + ":"; - code = code.toUpper(); - code += moduleName; - return code; -} - -QString GenerateRCode(HookParam hp) -{ - QString code = "/R"; - if (hp.type & USING_UNICODE) - code += "Q"; - else if (hp.type & USING_UTF8) - code += "V"; - else - code += "S"; - code += QString::number(hp.offset, 16); - code += "@"; - code += QString::number(hp.address, 16); - return code.toUpper(); -} - QString GenerateCode(HookParam hp, DWORD processId) { if (hp.type & DIRECT_READ) return GenerateRCode(hp);