From a99131fa232f9f328c379d2a9670edae62be4f9a Mon Sep 17 00:00:00 2001
From: Akash Mozumdar <akashmozumdar@gmail.com>
Date: Thu, 1 Nov 2018 21:59:13 -0400
Subject: [PATCH] add default codepage option

---
 GUI/host/host.cc           | 4 ++--
 GUI/host/host.h            | 3 ++-
 GUI/host/textthread.cc     | 2 +-
 GUI/mainwindow.cpp         | 2 ++
 GUI/misc.cpp               | 4 ++--
 GUI/misc.h                 | 2 +-
 vnrhook/hijack/texthook.cc | 4 +---
 7 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/GUI/host/host.cc b/GUI/host/host.cc
index 1209402..d21a771 100644
--- a/GUI/host/host.cc
+++ b/GUI/host/host.cc
@@ -126,7 +126,7 @@ namespace
 				case HOST_NOTIFICATION_TEXT:
 				{
 					auto info = *(ConsoleOutputNotif*)buffer;
-					Host::AddConsoleOutput(StringToWideString(info.message, CP_UTF8));
+					Host::AddConsoleOutput(StringToWideString(info.message));
 				}
 				break;
 				default:
@@ -273,7 +273,7 @@ namespace Host
 	std::wstring GetHookName(DWORD processId, uint64_t addr)
 	{
 		LOCK(hostMutex);
-		return StringToWideString(processRecordsByIds.at(processId)->GetHook(addr).hookName, CP_UTF8);
+		return StringToWideString(processRecordsByIds.at(processId)->GetHook(addr).hookName);
 	}
 
 	std::shared_ptr<TextThread> GetThread(ThreadParam tp)
diff --git a/GUI/host/host.h b/GUI/host/host.h
index 00233f7..455042a 100644
--- a/GUI/host/host.h
+++ b/GUI/host/host.h
@@ -30,7 +30,8 @@ namespace Host
 	void AddConsoleOutput(std::wstring text);
 }
 
-inline std::wstring StringToWideString(const std::string& text, UINT encoding)
+inline UINT DEFAULT_CODEPAGE = SHIFT_JIS;
+inline std::wstring StringToWideString(const std::string& text, UINT encoding = DEFAULT_CODEPAGE)
 {
 	std::wstring ret(text.size() + 1, 0);
 	ret.resize(MultiByteToWideChar(encoding, 0, text.c_str(), -1, ret.data(), ret.capacity()) - 1);
diff --git a/GUI/host/textthread.cc b/GUI/host/textthread.cc
index a448d0f..c876fe2 100644
--- a/GUI/host/textthread.cc
+++ b/GUI/host/textthread.cc
@@ -57,7 +57,7 @@ void TextThread::AddText(const BYTE* data, int len)
 	LOCK(threadMutex);
 	buffer += hp.type & USING_UNICODE
 		? std::wstring((wchar_t*)data, len / 2)
-		: StringToWideString(std::string((char*)data, len), hp.codepage);
+		: StringToWideString(std::string((char*)data, len), hp.codepage != 0 ? hp.codepage : DEFAULT_CODEPAGE);
 	if (std::all_of(buffer.begin(), buffer.end(), [&](wchar_t c) { return repeatingChars.count(c) > 0; })) buffer.clear();
 	timestamp = GetTickCount();
 }
diff --git a/GUI/mainwindow.cpp b/GUI/mainwindow.cpp
index 1603257..11605ce 100644
--- a/GUI/mainwindow.cpp
+++ b/GUI/mainwindow.cpp
@@ -18,6 +18,7 @@ MainWindow::MainWindow(QWidget *parent) :
 
 	if (settings.contains("Window")) this->setGeometry(settings.value("Window").toRect());
 	// TODO: add GUI for changing these
+	if (settings.contains("Default_Codepage")) DEFAULT_CODEPAGE = settings.value("Default_Codepage").toInt();
 	if (settings.contains("Flush_Delay")) TextThread::flushDelay = settings.value("Flush_Delay").toInt();
 	if (settings.contains("Max_Buffer_Size")) TextThread::maxBufferSize = settings.value("Max_Buffer_Size").toInt();
 
@@ -42,6 +43,7 @@ MainWindow::MainWindow(QWidget *parent) :
 MainWindow::~MainWindow()
 {
 	settings.setValue("Window", this->geometry());
+	settings.setValue("Default_Codepage", DEFAULT_CODEPAGE);
 	settings.setValue("Flush_Delay", TextThread::flushDelay);
 	settings.setValue("Max_Buffer_Size", TextThread::maxBufferSize);
 	settings.sync();
diff --git a/GUI/misc.cpp b/GUI/misc.cpp
index 059f19d..b074fa1 100644
--- a/GUI/misc.cpp
+++ b/GUI/misc.cpp
@@ -189,7 +189,7 @@ namespace
 		else if (hp.type & USING_UTF8) codeBuilder << "V";
 		else codeBuilder << "S";
 
-		if (hp.codepage != SHIFT_JIS && hp.codepage != CP_UTF8) codeBuilder << hp.codepage << "#";
+		if (hp.codepage != 0 && hp.codepage != CP_UTF8) codeBuilder << hp.codepage << "#";
 
 		codeBuilder.setIntegerBase(16);
 		codeBuilder.setNumberFlags(QTextStream::UppercaseDigits);
@@ -220,7 +220,7 @@ namespace
 		}
 		if (hp.type & NO_CONTEXT) codeBuilder << "N";
 
-		if (hp.codepage != SHIFT_JIS && hp.codepage != CP_UTF8) codeBuilder << hp.codepage << "#";
+		if (hp.codepage != 0 && hp.codepage != CP_UTF8) codeBuilder << hp.codepage << "#";
 
 		codeBuilder.setIntegerBase(16);
 		codeBuilder.setNumberFlags(QTextStream::UppercaseDigits);
diff --git a/GUI/misc.h b/GUI/misc.h
index dd93884..625597c 100644
--- a/GUI/misc.h
+++ b/GUI/misc.h
@@ -15,7 +15,7 @@ static QString CodeInfoDump =
 /H{A|B|W|S|Q|V}[N][codepage#]data_offset[*deref_offset1][:split_offset[*deref_offset2]]@addr[:module[:func]]\r\n\
 OR\r\n\
 Enter read code\r\n\
-/R{S|Q|V}[#codepage][*deref_offset|0]@addr\r\n\
+/R{S|Q|V}[codepage#][*deref_offset|0]@addr\r\n\
 All numbers except codepage in hexadecimal\r\n\
 A/B: Shift-JIS char little/big endian\r\n\
 W: UTF-16 char\r\n\
diff --git a/vnrhook/hijack/texthook.cc b/vnrhook/hijack/texthook.cc
index 5b8acbc..e9e42d4 100644
--- a/vnrhook/hijack/texthook.cc
+++ b/vnrhook/hijack/texthook.cc
@@ -104,6 +104,7 @@ bool TextHook::InsertHook()
 {
 	//ConsoleOutput("vnrcli:InsertHook: enter");
 	LOCK(*sectionMutex);
+	if (hp.type & USING_UTF8) hp.codepage = CP_UTF8;
 	if (hp.type & DIRECT_READ) return InsertReadCode();
 #ifndef _WIN64
 	else return InsertHookCode();
@@ -229,9 +230,6 @@ bool TextHook::InsertHookCode()
 		else if (HMODULE moduleBase = GetModuleHandleW(hp.module)) hp.insertion_address += (uint64_t)moduleBase;
 		else return ConsoleOutput("Textractor: UnsafeInsertHookCode: FAILED: module not present"), false;
 
-	if (hp.type & USING_UTF8) hp.codepage = CP_UTF8;
-	if (hp.codepage == 0) hp.codepage = SHIFT_JIS; // Use Shift-JIS unless custom encoding was specified
-
 	BYTE* original;
 insert:
 	if (MH_STATUS err = MH_CreateHook((void*)hp.insertion_address, (void*)trampoline, (void**)&original))