Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
tab to space
Browse files Browse the repository at this point in the history
  • Loading branch information
liberize committed Oct 16, 2015
1 parent 5eb6fe2 commit a16c78d
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 106 deletions.
44 changes: 22 additions & 22 deletions DetourHook/DetourHook/DetourHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,40 @@ LPSTR GetLastErrorAsString()

int _tmain(int argc, _TCHAR* argv[])
{
if (argc < 3) {
printf("usage: DetourHook <cmd> <dll>\n");
return 0;
}
if (argc < 3) {
printf("usage: DetourHook <cmd> <dll>\n");
return 0;
}

LPTSTR cmdLine = argv[1];
LPSTR dllPath = NULL;
LPTSTR cmdLine = argv[1];
LPSTR dllPath = NULL;

#ifdef UNICODE
DWORD num = WideCharToMultiByte(CP_OEMCP, NULL, argv[2], -1, NULL, 0, NULL, FALSE);
dllPath = new CHAR[num];
WideCharToMultiByte(CP_OEMCP, NULL, argv[2], -1, dllPath, num, NULL, FALSE);
DWORD num = WideCharToMultiByte(CP_OEMCP, NULL, argv[2], -1, NULL, 0, NULL, FALSE);
dllPath = new CHAR[num];
WideCharToMultiByte(CP_OEMCP, NULL, argv[2], -1, dllPath, num, NULL, FALSE);
#else
dllPath = argv[2];
dllPath = argv[2];
#endif

STARTUPINFO si;
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(STARTUPINFO));
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
si.cb = sizeof(STARTUPINFO);

DWORD flags = CREATE_DEFAULT_ERROR_MODE | CREATE_SUSPENDED;
DWORD flags = CREATE_DEFAULT_ERROR_MODE | CREATE_SUSPENDED;
if (!DetourCreateProcessWithDll(NULL, cmdLine,
NULL, NULL, FALSE, flags, NULL, NULL, &si, &pi,
dllPath, NULL)) {
LPSTR errMsg = GetLastErrorAsString();
printf("failed to create process, error: %s", errMsg);
LocalFree(errMsg);
}

ResumeThread(pi.hThread);
WaitForSingleObject(pi.hProcess, INFINITE);
NULL, NULL, FALSE, flags, NULL, NULL, &si, &pi,
dllPath, NULL)) {
LPSTR errMsg = GetLastErrorAsString();
printf("failed to create process, error: %s", errMsg);
LocalFree(errMsg);
}

ResumeThread(pi.hThread);
WaitForSingleObject(pi.hProcess, INFINITE);
#ifdef UNICODE
delete [] dllPath;
#endif
Expand Down
168 changes: 84 additions & 84 deletions DetourHook/DetourHookDll/DetourHookDll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,76 +19,76 @@ static int (WINAPI *oldMultiByteToWideChar)(UINT CodePage, DWORD dwFlags, LPCCH
int WINAPI newMultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCCH lpMultiByteStr, int cbMultiByte,
LPWSTR lpWideCharStr, int cchWideChar)
{
int ret = oldMultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, lpWideCharStr, cchWideChar);
if (cbMultiByte > 0) {
std::string str(lpMultiByteStr, cbMultiByte);
char *pattern = "^\\s*(("
"_END|_ENDFILE|_SUB|POS|\\{|\\}|\\[|\\]|LAMBDA|ADSL|BASE|BROW|CALC|CALL|CHEK|CODE|COME|NOTE|CMPS|DATE|"
"DEVI|DFMT|DIR|DISK|DISP|DTIM|EDIT|EJEC|ENVI|SET|EXEC|EXIT|FBWF|FDIR|FDRV|FEXT|FILE|FIND|FLNK|FONT|"
"FORM|FORX|GETF|GROU|HASH|HELP|HIDE|HIVE|HKEY|HOME|HOTK|IFEX|IMAG|IMPORT|INIT|IPAD|ITEM|KILL|LABE|"
"LINK|LIST|LOAD|LOCK|LOGO|LOGS|LOOP|LPOS|LSTR|MAIN|MDIR|MEMO|MENU|MESS|MOUN|MSTR|FNAM|NAME|NTPC|NUMK|"
"PAGE|PART|PATH|PBAR|PCIP|PINT|PUTF|RADI|RAMD|RAND|READ|RECY|REGI|RPOS|RSTR|RUNS|SCRN|SED|SEND|SERV|"
"SHEL|SHOW|SHUT|SITE|SIZE|SLID|SOCK|SPIN|SSTR|STRL|SUBJ|SWIN|TABL|TABS|TEAM|TEMP|TEXT|THREAD|THRD|"
"TIME|TIPS|UPNP|USER|WALL|WAIT|WRIT|DLL|\\\\|//|;|`|-|@|%|#(!|code)"
").*(\r|\n|\r\n)?|(\r|\n|\r\n))";
std::smatch group;
bool match = std::regex_match(str, group, std::regex(pattern, ECMAScript | icase));
pattern = "^\\s*(Disk error).*(\r|\n|\r\n)?";
match = match && !std::regex_match(str, std::regex(pattern, ECMAScript | icase));
strings.push_back(std::make_pair(str, match));
int ret = oldMultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, lpWideCharStr, cchWideChar);
if (cbMultiByte > 0) {
std::string str(lpMultiByteStr, cbMultiByte);
char *pattern = "^\\s*(("
"_END|_ENDFILE|_SUB|POS|\\{|\\}|\\[|\\]|LAMBDA|ADSL|BASE|BROW|CALC|CALL|CHEK|CODE|COME|NOTE|CMPS|DATE|"
"DEVI|DFMT|DIR|DISK|DISP|DTIM|EDIT|EJEC|ENVI|SET|EXEC|EXIT|FBWF|FDIR|FDRV|FEXT|FILE|FIND|FLNK|FONT|"
"FORM|FORX|GETF|GROU|HASH|HELP|HIDE|HIVE|HKEY|HOME|HOTK|IFEX|IMAG|IMPORT|INIT|IPAD|ITEM|KILL|LABE|"
"LINK|LIST|LOAD|LOCK|LOGO|LOGS|LOOP|LPOS|LSTR|MAIN|MDIR|MEMO|MENU|MESS|MOUN|MSTR|FNAM|NAME|NTPC|NUMK|"
"PAGE|PART|PATH|PBAR|PCIP|PINT|PUTF|RADI|RAMD|RAND|READ|RECY|REGI|RPOS|RSTR|RUNS|SCRN|SED|SEND|SERV|"
"SHEL|SHOW|SHUT|SITE|SIZE|SLID|SOCK|SPIN|SSTR|STRL|SUBJ|SWIN|TABL|TABS|TEAM|TEMP|TEXT|THREAD|THRD|"
"TIME|TIPS|UPNP|USER|WALL|WAIT|WRIT|DLL|\\\\|//|;|`|-|@|%|#(!|code)"
").*(\r|\n|\r\n)?|(\r|\n|\r\n))";
std::smatch group;
bool match = std::regex_match(str, group, std::regex(pattern, ECMAScript | icase));
pattern = "^\\s*(Disk error).*(\r|\n|\r\n)?";
match = match && !std::regex_match(str, std::regex(pattern, ECMAScript | icase));
strings.push_back(std::make_pair(str, match));

if (match) {
for (int i = 0; i < cchWideChar; i++) {
lpWideCharStr[i] = L'\0';
}
if (group[2] == "_SUB") {
pattern = "^\\s*_SUB\\s+([0-9a-z_]+).*(\r|\n|\r\n)?";
match = std::regex_match(str, group, std::regex(pattern, ECMAScript | icase));
if (match) {
subprocs.push_back(group[1]);
}
}
}
}
if (match) {
for (int i = 0; i < cchWideChar; i++) {
lpWideCharStr[i] = L'\0';
}
if (group[2] == "_SUB") {
pattern = "^\\s*_SUB\\s+([0-9a-z_]+).*(\r|\n|\r\n)?";
match = std::regex_match(str, group, std::regex(pattern, ECMAScript | icase));
if (match) {
subprocs.push_back(group[1]);
}
}
}
}
return ret;
}

void compose(std::string &script)
{
std::string pattern;
if (!subprocs.empty()) {
std::ostringstream oss;
auto it1 = subprocs.begin();
oss << "^\\s*(" << *it1++;
for (; it1 != subprocs.end(); ++it1) {
oss << '|' << *it1;
}
oss << ").*(\r|\n|\r\n)?";
pattern = oss.str();
}
std::string pattern;
if (!subprocs.empty()) {
std::ostringstream oss;
auto it1 = subprocs.begin();
oss << "^\\s*(" << *it1++;
for (; it1 != subprocs.end(); ++it1) {
oss << '|' << *it1;
}
oss << ").*(\r|\n|\r\n)?";
pattern = oss.str();
}

for (auto it2 = strings.begin(); it2 != strings.end(); ++it2) {
if (it2->second || (!pattern.empty() &&
std::regex_match(it2->first, std::regex(pattern, ECMAScript | icase)))) {
script += it2->first;
}
}
for (auto it2 = strings.begin(); it2 != strings.end(); ++it2) {
if (it2->second || (!pattern.empty() &&
std::regex_match(it2->first, std::regex(pattern, ECMAScript | icase)))) {
script += it2->first;
}
}
}

bool save(const std::string &script)
{
FILE *logFile = NULL;
TCHAR logPath[MAX_PATH] = {0};
GetCurrentDirectory(MAX_PATH, logPath);
_tcscat_s(logPath, MAX_PATH, _T("\\original.ini"));
_tfopen_s(&logFile, logPath, _T("w"));
if (!logFile) {
MessageBoxA(NULL, "failed to save ini file", "Error", MB_OK);
return false;
}
fwrite(script.c_str(), 1, script.size(), logFile);
fclose(logFile);
return true;
FILE *logFile = NULL;
TCHAR logPath[MAX_PATH] = {0};
GetCurrentDirectory(MAX_PATH, logPath);
_tcscat_s(logPath, MAX_PATH, _T("\\original.ini"));
_tfopen_s(&logFile, logPath, _T("w"));
if (!logFile) {
MessageBoxA(NULL, "failed to save ini file", "Error", MB_OK);
return false;
}
fwrite(script.c_str(), 1, script.size(), logFile);
fclose(logFile);
return true;
}

void hook()
Expand Down Expand Up @@ -116,30 +116,30 @@ extern "C" __declspec(dllexport) void dummy()
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
hook();
break;
}
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
{
unhook();
try {
std::string script;
compose(script);
save(script);
} catch (const std::exception &exc) {
MessageBoxA(NULL, exc.what(), "Error", MB_OK);
}
break;
}
}
return TRUE;
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
hook();
break;
}
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
{
unhook();
try {
std::string script;
compose(script);
save(script);
} catch (const std::exception &exc) {
MessageBoxA(NULL, exc.what(), "Error", MB_OK);
}
break;
}
}
return TRUE;
}

0 comments on commit a16c78d

Please sign in to comment.