refactor textthread

This commit is contained in:
Akash Mozumdar 2018-07-18 16:18:43 -04:00
parent 4368826f2a
commit 904804de28
11 changed files with 106 additions and 263 deletions

View File

@ -23,11 +23,10 @@ TextBuffer::~TextBuffer()
WaitForSingleObject(hThread.get(), 0); WaitForSingleObject(hThread.get(), 0);
} }
void TextBuffer::AddText(LPCWSTR str, int len, bool line) void TextBuffer::AddText(std::wstring text, bool line)
{ {
CSLock lock(cs); CSLock lock(cs);
if (len > 0) this->str.append(text);
this->str.append(str, len);
line_break = line; line_break = line;
} }

View File

@ -9,7 +9,7 @@ public:
TextBuffer(HWND edit); TextBuffer(HWND edit);
~TextBuffer(); ~TextBuffer();
void Flush(); void Flush();
void AddText(LPCWSTR str, int len, bool line); void AddText(std::wstring text, bool line);
void ClearBuffer(); void ClearBuffer();
bool Running() { return running; } bool Running() { return running; }
private: private:

View File

@ -292,3 +292,13 @@ int WC_MB(const wchar_t *wc, char* mb, int mb_length)
{ {
return WideCharToMultiByte(932, 0, wc, -1, mb, mb_length, NULL, NULL); return WideCharToMultiByte(932, 0, wc, -1, mb, mb_length, NULL, NULL);
} }
std::wstring GetEntryString(TextThread* thread)
{
ThreadParameter tp = thread->GetThreadParameter();
std::wstring buffer;
buffer.resize(200);
buffer.resize(swprintf(&buffer[0], L"%.4X:%.4d:0x%08X:0x%08X:0x%08X:", thread->Number(), tp.pid, tp.hook, tp.retn, tp.spl));
buffer += man->GetHook(tp.pid, tp.hook).name;
return buffer;
}

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "ITH.h" #include "ITH.h"
#include "texthook/host/textthread.h"
struct HookParam; struct HookParam;
struct ProcessRecord; struct ProcessRecord;
@ -56,6 +57,7 @@ HANDLE IthCreateFile(LPCWSTR name, DWORD option, DWORD share, DWORD disposition)
int MB_WC(const char* mb, wchar_t* wc, int wc_length); int MB_WC(const char* mb, wchar_t* wc, int wc_length);
int MB_WC_count(const char* mb, int mb_length); int MB_WC_count(const char* mb, int mb_length);
int WC_MB(const wchar_t *wc, char* mb, int mb_length); int WC_MB(const wchar_t *wc, char* mb, int mb_length);
std::wstring GetEntryString(TextThread * thread);
bool Parse(const std::wstring& cmd, HookParam& hp); bool Parse(const std::wstring& cmd, HookParam& hp);
// http://jrdodds.blogs.com/blog/2004/08/raii_in_c.html // http://jrdodds.blogs.com/blog/2004/08/raii_in_c.html

View File

@ -347,16 +347,9 @@ void ClickButton(HWND hWnd, HWND h)
} }
} }
DWORD ThreadOutput(TextThread* thread, const BYTE* out, DWORD len, DWORD new_line) void ThreadOutput(TextThread* thread, std::wstring output)
{ {
if (len == 0) if (thread->Status() & CURRENT_SELECT) texts->AddText(output, false);
return len;
DWORD status = thread->Status();
if (status & CURRENT_SELECT)
{
texts->AddText((LPWSTR)out, len / 2, false);
}
return len;
} }
bool GetHookParam(DWORD pid, DWORD hook_addr, HookParam& hp) bool GetHookParam(DWORD pid, DWORD hook_addr, HookParam& hp)
@ -367,28 +360,21 @@ bool GetHookParam(DWORD pid, DWORD hook_addr, HookParam& hp)
return true; return true;
} }
std::wstring GetEntryString(TextThread& thread) std::wstring CreateEntryWithLink(ThreadParameter tp, std::wstring& entry)
{
CHAR entry[512];
thread.GetEntryString(entry, 512);
return toUnicodeString(entry);
}
std::wstring CreateEntryWithLink(TextThread& thread, std::wstring& entry)
{ {
std::wstring entryWithLink = entry; std::wstring entryWithLink = entry;
if (thread.PID() == 0) if (tp.pid == 0)
entryWithLink += L"ConsoleOutput"; entryWithLink += L"ConsoleOutput";
HookParam hp = {}; HookParam hp = {};
if (GetHookParam(thread.PID(), thread.Addr(), hp)) if (GetHookParam(tp.pid, tp.hook, hp))
entryWithLink += L" (" + GetCode(hp, thread.PID()) + L")"; entryWithLink += L" (" + GetCode(hp, tp.hook) + L")";
return entryWithLink; return entryWithLink;
} }
void AddToCombo(TextThread& thread, bool replace) void AddToCombo(TextThread& thread, bool replace)
{ {
std::wstring entry = GetEntryString(thread); std::wstring entry = GetEntryString(&thread);
std::wstring entryWithLink = CreateEntryWithLink(thread, entry); std::wstring entryWithLink = CreateEntryWithLink(thread.GetThreadParameter(), entry);
int i = ComboBox_FindString(hwndCombo, -1, entry.c_str()); int i = ComboBox_FindString(hwndCombo, -1, entry.c_str());
if (replace) if (replace)
{ {
@ -410,12 +396,10 @@ void AddToCombo(TextThread& thread, bool replace)
void RemoveFromCombo(TextThread* thread) void RemoveFromCombo(TextThread* thread)
{ {
CHAR entry[512]; std::wstring entry = GetEntryString(thread);
thread->GetEntryString(entry, 512); if (thread->GetThreadParameter().pid == 0)
std::wstring unicodeEntry = toUnicodeString(entry); entry += L"ConsoleOutput";
if (thread->PID() == 0) int i = ComboBox_FindString(hwndCombo, 0, entry.c_str());
unicodeEntry += L"ConsoleOutput";
int i = ComboBox_FindString(hwndCombo, 0, unicodeEntry.c_str());
if (i != CB_ERR) if (i != CB_ERR)
{ {
if (ComboBox_DeleteString(hwndCombo, i) == CB_ERR) if (ComboBox_DeleteString(hwndCombo, i) == CB_ERR)
@ -423,7 +407,7 @@ void RemoveFromCombo(TextThread* thread)
} }
} }
DWORD SetEditText(LPWSTR wc) DWORD SetEditText(LPCWSTR wc)
{ {
DWORD line; DWORD line;
Edit_SetText(hwndEdit, wc); Edit_SetText(hwndEdit, wc);
@ -435,21 +419,16 @@ DWORD SetEditText(LPWSTR wc)
DWORD ThreadReset(TextThread* thread) DWORD ThreadReset(TextThread* thread)
{ {
texts->ClearBuffer(); texts->ClearBuffer();
man->SetCurrent(thread); man->SetCurrent(thread);;
thread->LockVector();
DWORD len = 0; std::wstring text = thread->GetStore();
LPWSTR wc = (LPWSTR)thread->GetStore(&len); SetEditText(text.c_str());
len /= 2;
wc[len] = L'\0';
SetEditText(wc);
WCHAR buffer[16]; WCHAR buffer[16];
std::swprintf(buffer, L"%04X", thread->Number()); std::swprintf(buffer, L"%04X", thread->Number());
DWORD tmp = ComboBox_FindString(hwndCombo, 0, buffer); DWORD tmp = ComboBox_FindString(hwndCombo, 0, buffer);
if (tmp != CB_ERR) if (tmp != CB_ERR)
ComboBox_SetCurSel(hwndCombo, tmp); ComboBox_SetCurSel(hwndCombo, tmp);
thread->UnlockVector();
return 0; return 0;
} }
@ -463,20 +442,20 @@ bool IsUnicodeHook(const ProcessRecord& pr, DWORD hook);
DWORD ThreadCreate(TextThread* thread) DWORD ThreadCreate(TextThread* thread)
{ {
thread->RegisterOutputCallBack(ThreadOutput, 0); thread->RegisterOutputCallBack(ThreadOutput);
//thread->RegisterFilterCallBack(ThreadFilter, 0); //thread->RegisterFilterCallBack(ThreadFilter, 0);
AddToCombo(*thread, false); AddToCombo(*thread, false);
const auto& tp = thread->GetThreadParameter(); auto tp = thread->GetThreadParameter();
auto pr = man->GetProcessRecord(tp->pid); auto pr = man->GetProcessRecord(tp.pid);
if (pr == NULL) if (pr == NULL)
return 0; return 0;
if (IsUnicodeHook(*pr, tp->hook)) if (IsUnicodeHook(*pr, tp.hook))
thread->Status() |= USING_UNICODE; thread->Status() |= USING_UNICODE;
auto pf = pfman->GetProfile(tp->pid); auto pf = pfman->GetProfile(tp.pid);
if (!pf) if (!pf)
return 0; return 0;
const std::wstring& hook_name = GetHookNameByAddress(*pr, thread->GetThreadParameter()->hook); const std::wstring& hook_name = GetHookNameByAddress(*pr, thread->GetThreadParameter().hook);
auto thread_profile = pf->FindThread(thread->GetThreadParameter(), hook_name); auto thread_profile = pf->FindThread(&thread->GetThreadParameter(), hook_name);
if (thread_profile != pf->Threads().end()) if (thread_profile != pf->Threads().end())
{ {
(*thread_profile)->HookManagerIndex() = thread->Number(); (*thread_profile)->HookManagerIndex() = thread->Number();
@ -579,7 +558,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
man->RegisterThreadRemoveCallback(ThreadRemove); man->RegisterThreadRemoveCallback(ThreadRemove);
man->RegisterThreadResetCallback(ThreadReset); man->RegisterThreadResetCallback(ThreadReset);
TextThread* console = man->FindSingle(0); TextThread* console = man->FindSingle(0);
console->RegisterOutputCallBack(ThreadOutput, NULL); console->RegisterOutputCallBack(ThreadOutput);
AddToCombo(*console, false); AddToCombo(*console, false);
man->RegisterProcessAttachCallback(RegisterProcess); man->RegisterProcessAttachCallback(RegisterProcess);
man->RegisterProcessDetachCallback(RemoveProcessList); man->RegisterProcessDetachCallback(RemoveProcessList);

View File

@ -18,7 +18,7 @@
#include "profile/pugixml.h" #include "profile/pugixml.h"
#include "profile/misc.h" #include "profile/misc.h"
#define HM_LOCK CriticalSectionLocker d_locker(hmcs) // Synchronized scope for accessing private data #define HM_LOCK CriticalSectionLocker locker(hmcs) // Synchronized scope for accessing private data
HookManager::HookManager() : HookManager::HookManager() :
current(nullptr), current(nullptr),
@ -27,12 +27,11 @@ HookManager::HookManager() :
reset(nullptr), reset(nullptr),
attach(nullptr), attach(nullptr),
detach(nullptr), detach(nullptr),
hook(nullptr),
new_thread_number(0), new_thread_number(0),
textThreadsByParams(), textThreadsByParams(),
processRecordsByIds() 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 }, new_thread_number++, splitDelay);
consoleTextThread->Status() |= USING_UNICODE; consoleTextThread->Status() |= USING_UNICODE;
SetCurrent(consoleTextThread); SetCurrent(consoleTextThread);
@ -79,7 +78,7 @@ void HookManager::RemoveSingleHook(DWORD pid, DWORD addr)
std::vector<ThreadParameter> removedThreads; std::vector<ThreadParameter> removedThreads;
for (auto i : textThreadsByParams) for (auto i : textThreadsByParams)
{ {
if (i.second->PID() == pid && i.second->Addr() == addr) if (i.first.pid == pid && i.first.hook == addr)
{ {
if (remove) if (remove)
{ {
@ -102,7 +101,7 @@ void HookManager::RemoveProcessContext(DWORD pid)
std::vector<ThreadParameter> removedThreads; std::vector<ThreadParameter> removedThreads;
for (auto i : textThreadsByParams) for (auto i : textThreadsByParams)
{ {
if (i.second->PID() == pid) if (i.first.hook == pid)
{ {
if (remove) if (remove)
{ {
@ -274,11 +273,11 @@ void HookManager::AddThreadsToProfile(Profile& pf, const ProcessRecord& pr, DWOR
DWORD AddThreadToProfile(Profile& pf, const ProcessRecord& pr, TextThread* thread) DWORD AddThreadToProfile(Profile& pf, const ProcessRecord& pr, TextThread* thread)
{ {
const ThreadParameter* tp = thread->GetThreadParameter(); ThreadParameter tp = thread->GetThreadParameter();
std::wstring hook_name = GetHookNameByAddress(pr, tp->hook); std::wstring hook_name = GetHookNameByAddress(pr, tp.hook);
if (hook_name.empty()) if (hook_name.empty())
return -1; return -1;
auto thread_profile = new ThreadProfile(hook_name, tp->retn, tp->spl, 0, 0, auto thread_profile = new ThreadProfile(hook_name, tp.retn, tp.spl, 0, 0,
THREAD_MASK_RETN | THREAD_MASK_SPLIT, L""); THREAD_MASK_RETN | THREAD_MASK_SPLIT, L"");
DWORD threads_size = pf.Threads().size(); DWORD threads_size = pf.Threads().size();
int thread_profile_index = pf.AddThread(thread_ptr(thread_profile)); int thread_profile_index = pf.AddThread(thread_ptr(thread_profile));

View File

@ -5,7 +5,7 @@
// Branch: ITH/HookManager.h, rev 133 // Branch: ITH/HookManager.h, rev 133
#include "config.h" #include "config.h"
#include "host/textthread.h" #include "textthread.h"
#include "winmutex/winmutex.h" #include "winmutex/winmutex.h"
#include <unordered_map> #include <unordered_map>
#include <string> #include <string>
@ -16,9 +16,6 @@ namespace pugi {
} }
class Profile; class Profile;
enum { MAX_REGISTER = 0xf };
enum { MAX_PREV_REPEAT_LENGTH = 0x20 };
struct ProcessRecord { struct ProcessRecord {
HANDLE process_handle; HANDLE process_handle;
HANDLE hookman_mutex; HANDLE hookman_mutex;
@ -33,7 +30,8 @@ struct Hook
std::wstring name; std::wstring name;
}; };
typedef DWORD (*ProcessEventCallback)(DWORD pid); typedef DWORD(*ProcessEventCallback)(DWORD pid);
typedef DWORD(*ThreadEventCallback)(TextThread*);
struct ThreadParameterHasher struct ThreadParameterHasher
{ {
@ -67,27 +65,14 @@ public:
HANDLE GetHostPipe(DWORD pid); HANDLE GetHostPipe(DWORD pid);
ThreadEventCallback RegisterThreadCreateCallback(ThreadEventCallback cf) void RegisterThreadCreateCallback(ThreadEventCallback cf) { create = cf; }
{ return (ThreadEventCallback)_InterlockedExchange((long*)&create,(long)cf); } void RegisterThreadRemoveCallback(ThreadEventCallback cf) { remove = cf; }
void RegisterThreadResetCallback(ThreadEventCallback cf) { reset = cf; }
ThreadEventCallback RegisterThreadRemoveCallback(ThreadEventCallback cf) void RegisterProcessAttachCallback(ProcessEventCallback cf) { attach = cf; }
{ return (ThreadEventCallback)_InterlockedExchange((long*)&remove,(long)cf); } void RegisterProcessDetachCallback(ProcessEventCallback cf) { detach = cf; }
ThreadEventCallback RegisterThreadResetCallback(ThreadEventCallback cf)
{ return (ThreadEventCallback)_InterlockedExchange((long*)&reset,(long)cf); }
ThreadEventCallback RegisterAddRemoveLinkCallback(ThreadEventCallback cf)
{ return (ThreadEventCallback)_InterlockedExchange((long*)&addRemoveLink, (long)cf); }
ProcessEventCallback RegisterProcessAttachCallback(ProcessEventCallback cf)
{ return (ProcessEventCallback)_InterlockedExchange((long*)&attach,(long)cf); }
ProcessEventCallback RegisterProcessDetachCallback(ProcessEventCallback cf)
{ return (ProcessEventCallback)_InterlockedExchange((long*)&detach,(long)cf); }
void SetSplitInterval(unsigned int splitDelay) { this->splitDelay = splitDelay; } void SetSplitInterval(unsigned int splitDelay) { this->splitDelay = splitDelay; }
void OnThreadCreate(pugi::xml_node profile_node, TextThread* thread);
void GetProfile(DWORD pid, pugi::xml_node profile_node); void GetProfile(DWORD pid, pugi::xml_node profile_node);
private: private:
@ -99,14 +84,12 @@ private:
TextThread *current; TextThread *current;
ThreadEventCallback create, ThreadEventCallback create,
remove, remove,
reset, reset;
addRemoveLink;
ProcessEventCallback attach, ProcessEventCallback attach,
detach, detach;
hook;
WORD register_count, WORD register_count,
new_thread_number; new_thread_number;
unsigned int splitDelay; unsigned int splitDelay;

View File

@ -66,9 +66,9 @@ DWORD WINAPI TextReceiver(LPVOID lpThreadParameter)
{ {
case HOST_NOTIFICATION_NEWHOOK: case HOST_NOTIFICATION_NEWHOOK:
man->SetHook(processId, man->SetHook(processId,
((HookParam*)(buffer + sizeof(DWORD) * 2))->address, ((HookParam*)(buffer + sizeof(DWORD) * 2))->address, // Hook address
{ {
*(HookParam*)(buffer + sizeof(DWORD) * 2), // Hook address *(HookParam*)(buffer + sizeof(DWORD) * 2), // Hook parameter
std::wstring(A2W( std::wstring(A2W(
(const char*)buffer + sizeof(DWORD) * 2 + sizeof(HookParam) // Hook name (const char*)buffer + sizeof(DWORD) * 2 + sizeof(HookParam) // Hook name
)) ))

View File

@ -14,69 +14,46 @@
#include "vnrhook/include/types.h" #include "vnrhook/include/types.h"
#include <stdio.h> #include <stdio.h>
#include "extensions/Extensions.h" #include "extensions/Extensions.h"
#include "winmutex/winmutex.h"
extern HookManager* man; extern HookManager* man;
DWORD GetHookName(LPSTR str, DWORD pid, DWORD hook_addr, DWORD max)
{
if (!pid)
return 0;
DWORD len = 0;
max--; //for '\0' magic marker.
//if (pid == 0) {
// len = wcslen(HookNameInitTable[0]);
// if (len >= max)
// len = max;
// memcpy(str, HookNameInitTable[0], len << 1);
// str[len] = 0;
// return len;
//}
//::man->LockProcessHookman(pid);
ProcessRecord *pr = ::man->GetProcessRecord(pid);
if (!pr)
return 0;
WaitForSingleObject(pr->hookman_mutex, 0);
const OldHook *hks = (const OldHook *)pr->hookman_map;
for (int i = 0; i < MAX_HOOK; i++)
if (hks[i].Address() == hook_addr) {
len = hks[i].NameLength();
if (len >= max)
len = max;
ReadProcessMemory(pr->process_handle, hks[i].Name(), str, len, &len);
if (str[len - 1] == 0)
len--;
else
str[len] = 0;
break;
}
ReleaseMutex(pr->hookman_mutex);
//::man->UnlockProcessHookman(pid);
return len;
}
extern HWND dummyWindow; extern HWND dummyWindow;
#define TT_LOCK CriticalSectionLocker locker(ttCs) // Synchronized scope for accessing private data
TextThread::TextThread(ThreadParameter tp, unsigned int threadNumber, unsigned int splitDelay) : TextThread::TextThread(ThreadParameter tp, unsigned int threadNumber, unsigned int splitDelay) :
thread_number(threadNumber), storage(),
splitDelay(splitDelay), sentenceBuffer(),
output(nullptr), status(0),
status(0), threadNumber(threadNumber),
tp(tp), splitDelay(splitDelay),
sentenceBuffer() output(nullptr),
tp(tp)
{ {
InitializeCriticalSection(&ttCs);
}
TextThread::~TextThread()
{
TT_LOCK;
DeleteCriticalSection(&ttCs);
} }
void TextThread::Reset() void TextThread::Reset()
{ {
MyVector::Reset(); TT_LOCK;
storage.clear();
}
std::wstring TextThread::GetStore()
{
TT_LOCK;
return storage;
} }
void TextThread::AddSentence() void TextThread::AddSentence()
{ {
TT_LOCK;
std::wstring sentence; std::wstring sentence;
if (status & USING_UNICODE) if (status & USING_UNICODE)
{ {
@ -94,13 +71,15 @@ void TextThread::AddSentence()
void TextThread::AddSentence(std::wstring sentence) void TextThread::AddSentence(std::wstring sentence)
{ {
TT_LOCK;
sentence.append(L"\r\n"); sentence.append(L"\r\n");
if (output) output(this, (const BYTE*)sentence.c_str(), sentence.length() * 2, false); if (output) output(this, sentence);
AddToStore((const BYTE*)sentence.c_str(), sentence.length() * 2); storage.append(sentence);
} }
void TextThread::AddText(const BYTE *con, int len) void TextThread::AddText(const BYTE *con, int len)
{ {
TT_LOCK;
sentenceBuffer.insert(sentenceBuffer.end(), con, con+len); sentenceBuffer.insert(sentenceBuffer.end(), con, con+len);
SetTimer(dummyWindow, (UINT_PTR)this, splitDelay, SetTimer(dummyWindow, (UINT_PTR)this, splitDelay,
[](HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) [](HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
@ -110,11 +89,4 @@ void TextThread::AddText(const BYTE *con, int len)
}); });
} }
void TextThread::GetEntryString(LPSTR buffer, DWORD max)
{
int len = sprintf(buffer, "%.4X:%.4d:0x%08X:0x%08X:0x%08X:",
thread_number, tp. pid, tp.hook, tp.retn, tp.spl);
GetHookName(buffer + len, tp.pid, tp.hook, max - len);
}
// EOF // EOF

View File

@ -4,7 +4,8 @@
// 8/23/2013 jichi // 8/23/2013 jichi
// Branch: ITH/TextThread.h, rev 120 // Branch: ITH/TextThread.h, rev 120
#include "host/textthread_p.h" #include <Windows.h>
#include "config.h"
#include <intrin.h> // require _InterlockedExchange #include <intrin.h> // require _InterlockedExchange
#include <string> #include <string>
#include <vector> #include <vector>
@ -25,41 +26,37 @@ struct ThreadParameter {
#define CURRENT_SELECT 0x1000 #define CURRENT_SELECT 0x1000
class TextThread; class TextThread;
typedef DWORD (* ThreadOutputFilterCallback)(TextThread *,const BYTE *, DWORD, DWORD); typedef void(*ThreadOutputCallback)(TextThread*, std::wstring data);
typedef DWORD (* ThreadEventCallback)(TextThread *);
//extern DWORD split_time,repeat_count,global_filter,cyclic_remove; //extern DWORD split_time,repeat_count,global_filter,cyclic_remove;
class TextThread : public MyVector<BYTE, 0x200> class DLLEXPORT TextThread
{ {
public: public:
TextThread(ThreadParameter tp, unsigned int threadNumber, unsigned int splitDelay); TextThread(ThreadParameter tp, unsigned int threadNumber, unsigned int splitDelay);
~TextThread();
virtual void GetEntryString(LPSTR buffer, DWORD max);
void Reset(); void Reset();
void AddText(const BYTE *con,int len); void AddText(const BYTE *con, int len);
void AddSentence(); void AddSentence();
void AddSentence(std::wstring sentence); void AddSentence(std::wstring sentence);
BYTE *GetStore(DWORD *len) { if (len) *len = used; return storage; } std::wstring GetStore();
DWORD PID() const { return tp.pid; }
DWORD Addr() const {return tp.hook; }
DWORD &Status() { return status; } DWORD &Status() { return status; }
WORD Number() const { return thread_number; } WORD Number() const { return threadNumber; }
ThreadParameter *GetThreadParameter() { return &tp; } ThreadParameter GetThreadParameter() { return tp; }
//LPCWSTR GetComment() { return comment; } //LPCWSTR GetComment() { return comment; }
ThreadOutputFilterCallback RegisterOutputCallBack(ThreadOutputFilterCallback cb, PVOID data) void RegisterOutputCallBack(ThreadOutputCallback cb) { output = cb; }
{
return (ThreadOutputFilterCallback)_InterlockedExchange((long*)&output,(long)cb);
}
private: private:
ThreadParameter tp; CRITICAL_SECTION ttCs;
ThreadOutputFilterCallback output; ThreadOutputCallback output;
std::vector<char> sentenceBuffer; std::vector<char> sentenceBuffer;
unsigned int thread_number; std::wstring storage;
ThreadParameter tp;
unsigned int threadNumber;
unsigned int splitDelay; unsigned int splitDelay;
DWORD status; DWORD status;
}; };

View File

@ -1,98 +0,0 @@
#pragma once
// textthread_p.h
// 8/14/2013 jichi
// Branch: ITH/main_template.h, rev 66
#include <windows.h>
template<class T, int default_size>
class MyVector
{
public:
void LockVector() { EnterCriticalSection(&cs_store); }
void UnlockVector() { LeaveCriticalSection(&cs_store); }
MyVector() : size(default_size), used(0)
{
InitializeCriticalSection(&cs_store);
storage = new T[size];
// jichi 9/21/2013: zero memory
// This would cause trouble if T is not an atomic type
::memset(storage, 0, sizeof(T) * size);
}
virtual ~MyVector()
{
if (storage)
delete[] storage;
DeleteCriticalSection(&cs_store);
storage = 0;
}
protected:
void Reset()
{
EnterCriticalSection(&cs_store);
for (int i = 0; i < used; i++) {
//Release<T>(storage[i]);
storage[i] = T();
}
used = 0;
LeaveCriticalSection(&cs_store);
}
void Remove(int index)
{
if (index>=used)
return;
//Release<T>(storage[index]);
for (int i = index; i < used; i++)
storage[i] = storage[i+1];
used--;
}
void ClearMemory(int offset, int clear_size)
{
if (clear_size < 0)
return;
EnterCriticalSection(&cs_store);
if (offset+clear_size <= size)
::memset(storage+offset, 0, clear_size * sizeof(T)); // jichi 11/30/2013: This is the original code of ITH
LeaveCriticalSection(&cs_store);
//else __asm int 3
}
int AddToStore(const T *con,int amount)
{
if (amount <= 0 || con == 0)
return 0;
int status = 0;
EnterCriticalSection(&cs_store);
if (amount + used + 2 >= size) {
while (amount + used + 2 >= size)
size<<=1;
T *temp;
if (size * sizeof(T) < 0x1000000) {
temp = new T[size];
if (size > used)
::memset(temp, 0, (size - used) * sizeof(T)); // jichi 9/25/2013: zero memory
memcpy(temp, storage, used * sizeof(T));
} else {
size = default_size;
temp = new T[size];
::memset(temp, 0, sizeof(T) * size); // jichi 9/25/2013: zero memory
used = 0;
status = 1;
}
delete[] storage;
storage = temp;
}
memcpy(storage+used, con, amount * sizeof(T));
used += amount;
LeaveCriticalSection(&cs_store);
return status;
}
CRITICAL_SECTION cs_store;
int size,
used;
T *storage;
};
// EOF