From 6598b979b3c8f70eccd53799ed12b1dc811cb6c5 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Thu, 19 Jul 2018 00:46:52 -0400 Subject: [PATCH] more refactoring --- vnr/texthook/CMakeLists.txt | 2 +- vnr/texthook/config.h | 10 ---------- vnr/texthook/hookman.cc | 10 +++++----- vnr/texthook/hookman.h | 28 +++++++++++----------------- vnr/texthook/host.cc | 6 ++---- vnr/texthook/host.h | 6 ++---- vnr/texthook/pipe.cc | 14 ++++---------- vnr/texthook/pipe.h | 11 +++++++++++ 8 files changed, 36 insertions(+), 51 deletions(-) delete mode 100644 vnr/texthook/config.h create mode 100644 vnr/texthook/pipe.h diff --git a/vnr/texthook/CMakeLists.txt b/vnr/texthook/CMakeLists.txt index d9dbcb4..3c7ec3b 100644 --- a/vnr/texthook/CMakeLists.txt +++ b/vnr/texthook/CMakeLists.txt @@ -7,9 +7,9 @@ # DEFINES += _CRT_NON_CONFORMING_SWPRINTFS set(vnrhost_src - config.h hookman.h host.h + pipe.h textthread.h hookman.cc host.cc diff --git a/vnr/texthook/config.h b/vnr/texthook/config.h deleted file mode 100644 index fa322ea..0000000 --- a/vnr/texthook/config.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -// config.h -// 8/23/2013 jichi -// The first header file that are included by all source files. - -#define IHF // for dll import -#define DLLEXPORT __declspec(dllexport) - -// EOF diff --git a/vnr/texthook/hookman.cc b/vnr/texthook/hookman.cc index 8bb3be4..5c5676c 100644 --- a/vnr/texthook/hookman.cc +++ b/vnr/texthook/hookman.cc @@ -12,7 +12,7 @@ #include "vnrhook/include/const.h" #include "vnrhook/include/defs.h" #include "vnrhook/include/types.h" -#include +#include "winmutex/winmutex.h" #include #define HM_LOCK CriticalSectionLocker locker(hmcs) // Synchronized scope for accessing private data @@ -24,11 +24,11 @@ HookManager::HookManager() : reset(nullptr), attach(nullptr), detach(nullptr), - new_thread_number(0), + nextThreadNumber(0), textThreadsByParams(), processRecordsByIds() { - TextThread* consoleTextThread = textThreadsByParams[{ 0, -1UL, -1UL, -1UL }] = new TextThread({ 0, -1UL, -1UL, -1UL }, new_thread_number++, splitDelay); + TextThread* consoleTextThread = textThreadsByParams[{ 0, -1UL, -1UL, -1UL }] = new TextThread({ 0, -1UL, -1UL, -1UL }, nextThreadNumber++, splitDelay); consoleTextThread->Status() |= USING_UNICODE; SetCurrent(consoleTextThread); @@ -147,7 +147,7 @@ void HookManager::UnRegisterProcess(DWORD pid) detach(pid); } -void HookManager::DispatchText(DWORD pid, const BYTE *text, DWORD hook, DWORD retn, DWORD spl, int len) +void HookManager::DispatchText(DWORD pid, DWORD hook, DWORD retn, DWORD spl, const BYTE *text, int len) { // jichi 20/27/2013: When PID is zero, the text comes from console, which I don't need if (!text || !pid || len <= 0) @@ -157,7 +157,7 @@ void HookManager::DispatchText(DWORD pid, const BYTE *text, DWORD hook, DWORD re TextThread *it; if (!(it = textThreadsByParams[tp])) { - it = textThreadsByParams[tp] = new TextThread(tp, new_thread_number++, splitDelay); + it = textThreadsByParams[tp] = new TextThread(tp, nextThreadNumber++, splitDelay); if (create) { create(it); diff --git a/vnr/texthook/hookman.h b/vnr/texthook/hookman.h index 3204f23..76b7a8d 100644 --- a/vnr/texthook/hookman.h +++ b/vnr/texthook/hookman.h @@ -4,14 +4,14 @@ // 8/23/2013 jichi // Branch: ITH/HookManager.h, rev 133 -#include "config.h" +#include #include "textthread.h" -#include "winmutex/winmutex.h" #include #include #include "vnrhook/include/types.h" -struct ProcessRecord { +struct ProcessRecord +{ HANDLE process_handle; HANDLE hookman_mutex; HANDLE hookman_section; @@ -30,29 +30,26 @@ struct ThreadParameterHasher } }; -class DLLEXPORT HookManager +class __declspec(dllexport) HookManager { public: HookManager(); ~HookManager(); + TextThread *FindSingle(DWORD number); ProcessRecord *GetProcessRecord(DWORD pid); + HANDLE GetHostPipe(DWORD pid); void ClearCurrent(); void SelectCurrent(DWORD num); void SetCurrent(TextThread *it); void AddConsoleOutput(LPCWSTR text); - - // jichi 10/27/2013: Add const; add space. - void DispatchText(DWORD pid, const BYTE *text, DWORD hook, DWORD retn, DWORD split, int len); + void DispatchText(DWORD pid, DWORD hook, DWORD retn, DWORD split, const BYTE *text, int len); void RemoveProcessContext(DWORD pid); // private void RemoveSingleHook(DWORD pid, DWORD addr); void RegisterProcess(DWORD pid, HANDLE hostPipe); void UnRegisterProcess(DWORD pid); HookParam GetHookParam(DWORD pid, DWORD addr); std::wstring GetHookName(DWORD pid, DWORD addr); - //void SetName(DWORD); - - HANDLE GetHostPipe(DWORD pid); void RegisterThreadCreateCallback(ThreadEventCallback cf) { create = cf; } void RegisterThreadRemoveCallback(ThreadEventCallback cf) { remove = cf; } @@ -69,14 +66,11 @@ private: CRITICAL_SECTION hmcs; TextThread *current; - ThreadEventCallback create, - remove, - reset; - ProcessEventCallback attach, - detach; - WORD register_count, - new_thread_number; + ThreadEventCallback create, remove, reset; + ProcessEventCallback attach, detach; + + WORD nextThreadNumber; unsigned int splitDelay; }; diff --git a/vnr/texthook/host.cc b/vnr/texthook/host.cc index 2f3ecf3..9056cff 100644 --- a/vnr/texthook/host.cc +++ b/vnr/texthook/host.cc @@ -3,6 +3,7 @@ // Branch IHF/main.cpp, rev 111 #include "host.h" +#include "pipe.h" #include "vnrhook/include/const.h" #include "vnrhook/include/defs.h" #include "vnrhook/include/types.h" @@ -27,8 +28,6 @@ namespace } } // unnamed namespace -void CreateNewPipe(); - BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID unused) { switch (fdwReason) @@ -135,8 +134,7 @@ DLLEXPORT bool InsertHook(DWORD pid, const HookParam *hp, std::string name) *(HookParam*)(buffer + sizeof(DWORD)) = *hp; if (name.size()) strcpy((char*)buffer + sizeof(DWORD) + sizeof(HookParam), name.c_str()); DWORD unused; - WriteFile(commandPipe, buffer, sizeof(DWORD) + sizeof(HookParam) + name.size(), &unused, nullptr); - return true; + return WriteFile(commandPipe, buffer, sizeof(DWORD) + sizeof(HookParam) + name.size(), &unused, nullptr); } DLLEXPORT bool RemoveHook(DWORD pid, DWORD addr) diff --git a/vnr/texthook/host.h b/vnr/texthook/host.h index 0aa3c58..36ed001 100644 --- a/vnr/texthook/host.h +++ b/vnr/texthook/host.h @@ -4,13 +4,11 @@ // 8/23/2013 jichi // Branch: ITH/IHF.h, rev 105 -//#include "host/settings.h" -#include "config.h" +#define DLLEXPORT __declspec(dllexport) #include "hookman.h" +#include "vnrhook/include/types.h" #include -struct HookParam; - DLLEXPORT void OpenHost(); DLLEXPORT bool StartHost(); DLLEXPORT void CloseHost(); diff --git a/vnr/texthook/pipe.cc b/vnr/texthook/pipe.cc index 7556558..fa69470 100644 --- a/vnr/texthook/pipe.cc +++ b/vnr/texthook/pipe.cc @@ -1,10 +1,9 @@ // pipe.cc // 8/24/2013 jichi // Branch IHF/pipe.cpp, rev 93 -// 8/24/2013 TODO: Clean up this file +#include "pipe.h" #include "host.h" -#include "hookman.h" #include "vnrhook/include/defs.h" #include "vnrhook/include/const.h" #include @@ -17,8 +16,6 @@ struct Pipes HANDLE hostPipe; }; -DWORD WINAPI TextReceiver(LPVOID lpThreadParameter); - void CreateNewPipe() { CloseHandle(CreateThread(nullptr, 0, TextReceiver, new Pipes @@ -64,15 +61,12 @@ DWORD WINAPI TextReceiver(LPVOID lpThreadParameter) } else { - // jichi 9/28/2013: Debug raw data - //ITH_DEBUG_DWORD9(RecvLen - 0xc, - // buffer[0xc], buffer[0xd], buffer[0xe], buffer[0xf], - // buffer[0x10], buffer[0x11], buffer[0x12], buffer[0x13]); - man->DispatchText(processId, buffer + HEADER_SIZE, + man->DispatchText(processId, *(DWORD*)buffer, // Hook address *(DWORD*)(buffer + sizeof(DWORD)), // Return address *(DWORD*)(buffer + sizeof(DWORD) * 2), // Split - bytesRead - HEADER_SIZE + buffer + HEADER_SIZE, // Data + bytesRead - HEADER_SIZE // Data size ); } } diff --git a/vnr/texthook/pipe.h b/vnr/texthook/pipe.h new file mode 100644 index 0000000..78f7546 --- /dev/null +++ b/vnr/texthook/pipe.h @@ -0,0 +1,11 @@ +#pragma once + +// pipe.h +// 7/19/2018 Artikash + +#include + +void CreateNewPipe(); +DWORD WINAPI TextReceiver(LPVOID lpThreadParam); + +// EOF \ No newline at end of file