Skip to content

Commit

Permalink
manually correct ambigous encoding ones
Browse files Browse the repository at this point in the history
  • Loading branch information
namazso committed Dec 6, 2020
1 parent 412be72 commit cc82d3a
Show file tree
Hide file tree
Showing 23 changed files with 632 additions and 632 deletions.
20 changes: 10 additions & 10 deletions cache.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "override.h"

//CreateDIB计数,将在绘制下列次数后更新DIB区
#define BITMAP_REDUCE_COUNTER 256//トャネマ1024
#define BITMAP_REDUCE_COUNTER 256//默认1024


HDC CBitmapCache::CreateDC(HDC dc)
Expand All @@ -23,8 +23,8 @@ HBITMAP CBitmapCache::CreateDIB(int width, int height, BYTE** lplpPixels)
*lplpPixels = m_lpPixels;
return m_hbmp;
}
//カウンタ超過
//ただしサイズが全く同じなら再生成しない
//カウンタ超過
//ただしサイズが全く同じなら再生成しない
if (dibSize.cx == width && dibSize.cy == height) {
m_counter = 0;
*lplpPixels = m_lpPixels;
Expand All @@ -46,16 +46,16 @@ HBITMAP CBitmapCache::CreateDIB(int width, int height, BYTE** lplpPixels)
}
TRACE(_T("width=%d, height=%d\n"), width, height);

//メモリ不足等でhbmpNew==NULLの場合を想定し、
//成功したときのみキャッシュを更新
//メモリ不足等でhbmpNew==NULLの場合を想定し、
//成功したときのみキャッシュを更新
if (m_hbmp) {
DeleteBitmap(m_hbmp);
}

m_hbmp = hbmpNew;
dibSize.cx = width;
dibSize.cy = height;
//CreateDIBSectionは多分ページ境界かセグメント境界
//CreateDIBSectionは多分ページ境界かセグメント境界
m_lpPixels = *lplpPixels;
m_counter = 0;
return m_hbmp;
Expand All @@ -79,7 +79,7 @@ void CBitmapCache::FillSolidRect(COLORREF rgb, const RECT* lprc)
const DWORD dwBmpBytes = m_dibSize.cx * m_dibSize.cy;
rgb = RGB2DIB(rgb);
//TODO: MMX or SSE化
//TODO: MMX or SSE化
__asm {
mov edi, dword ptr [lpPixels]
mov ecx, dword ptr [dwBmpBytes]
Expand All @@ -94,7 +94,7 @@ void CBitmapCache::FillSolidRect(COLORREF rgb, const RECT* lprc)
// }
}

//水平線を引く
//水平線を引く
//(X1,Y1) (X2,Y1)
// +-----------------+ ^
// | rgb | | width
Expand All @@ -111,7 +111,7 @@ void CBitmapCache::DrawHorizontalLine(int X1, int Y1, int X2, COLORREF rgb, int
X2 = xx;
}

//クリッピング
//クリッピング
const int xSize = m_dibSize.cx;
const int ySize = m_dibSize.cy;
X1 = Bound(X1, 0, xSize);
Expand Down Expand Up @@ -142,7 +142,7 @@ void CBitmapCache::DrawHorizontalLine(int X1, int Y1, int X2, COLORREF rgb, int
}*/

/*#ifdef _M_IX86
//無意味にアセンブリ化
//無意味にアセンブリ化
__asm {
mov ebx, dword ptr [Yd]
mov edx, dword ptr [lpPixels]
Expand Down
6 changes: 3 additions & 3 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ typedef StringHashT<LF_FACESIZE,true> StringHashFont;
typedef StringHashT<MAX_PATH,true> StringHashModule;


//COLORREF(RR GG BB 00) を DIB32(BB GG RR 00) に変換
//COLORREF(RR GG BB 00) を DIB32(BB GG RR 00) に変換
#define RGB2DIB(rgb) RGB(GetBValue(rgb), GetGValue(rgb), GetRValue(rgb))
#define DIB2RGB(dib) RGB2DIB(dib)

// ExtTextOutWのビットマップキャッシュ
// ExtTextOutWのビットマップキャッシュ
class CBitmapCache
{
private:
Expand Down Expand Up @@ -142,7 +142,7 @@ class CBitmapCache
*m_CurrentPixel = RGB2DIB(rgb);
}

//本体はcache.cpp
//本体はcache.cpp
HDC CreateDC(HDC dc);
HBITMAP CreateDIB(int width, int height, BYTE** lplpPixels);
void FillSolidRect(COLORREF rgb, const RECT* lprc);
Expand Down
32 changes: 16 additions & 16 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ FORCEINLINE HINSTANCE GetDLLInstance()
return g_hinstDLL;
}

//排他制御
//排他制御
class CCriticalSectionLock
{
#define MAX_CRITICAL_COUNT 20
Expand Down Expand Up @@ -148,11 +148,11 @@ static void _Trace(LPCTSTR pszFormat, ...)
CCriticalSectionLock __lock;
va_list argptr;
va_start(argptr, pszFormat);
//w(v)sprintfは1024文字以上返してこない
//w(v)sprintfは1024文字以上返してこない
TCHAR szBuffer[10240];
wvsprintf(szBuffer, pszFormat, argptr);

//デバッガをアタッチしてる時はデバッガにメッセージを出す
//デバッガをアタッチしてる時はデバッガにメッセージを出す
//if (IsDebuggerPresent()) {
OutputDebugString(szBuffer);
return;
Expand Down Expand Up @@ -188,7 +188,7 @@ static void _Trace(LPCTSTR pszFormat, ...)
}
#else //!_DEBUG
#define TRACE NOP_FUNCTION
//↓PSDK 2003R2のwinnt.h
//↓PSDK 2003R2のwinnt.h
//#ifndef NOP_FUNCTION
//#if (_MSC_VER >= 1210)
//#define NOP_FUNCTION __noop
Expand All @@ -198,8 +198,8 @@ static void _Trace(LPCTSTR pszFormat, ...)
//#endif
#endif //_DEBUG

//TRACEマクロ
//使用例: TRACE(_T("cx: %d\n"), cx);
//TRACEマクロ
//使用例: TRACE(_T("cx: %d\n"), cx);
#ifdef USE_TRACE
#define TRACE2 _Trace2
#define TRACE2_STR _Trace2_Str
Expand All @@ -212,7 +212,7 @@ static void _Trace2(LPCTSTR pszFormat, ...)
CCriticalSectionLock __lock;
va_list argptr;
va_start(argptr, pszFormat);
//w(v)sprintfは1024文字以上返してこない
//w(v)sprintfは1024文字以上返してこない
TCHAR szBuffer[1024];
wvsprintf(szBuffer, pszFormat, argptr);
OutputDebugString(szBuffer);
Expand Down Expand Up @@ -332,7 +332,7 @@ class CCriticalSectionLockTry
}
};

// 使用後はfreeで開放する事
// 使用後はfreeで開放する事
LPWSTR _StrDupExAtoW(LPCSTR pszMB, int cchMB, LPWSTR pszStack, int cchStack, int* pcchWC, int nACP = CP_ACP);
static inline LPWSTR _StrDupAtoW(LPCSTR pszMB, int cchMB = -1, int* pcchWC = NULL)
{
Expand All @@ -358,8 +358,8 @@ template<typename T> FORCEINLINE T Bound(T x, T m, T M) { return (x < m) ? m : (
template<typename T> FORCEINLINE int Sgn(T x, T y) { return (x > y) ? 1 : ((x < y) ? -1 : 0); }


//型チェック機能つきDeleteXXX/SelectXXX
//SelectObject/DeleteObjectは使用できなくなる
//型チェック機能つきDeleteXXX/SelectXXX
//SelectObject/DeleteObjectは使用できなくなる

#ifdef _DEBUG
#undef DeletePen
Expand Down Expand Up @@ -448,8 +448,8 @@ DEFINE_SELECT_FUNCTION(HBITMAP, Bitmap)
#endif //_DEBUG


//TRACEマクロ
//使用例: TRACE(_T("cx: %d\n"), cx);
//TRACEマクロ
//使用例: TRACE(_T("cx: %d\n"), cx);
#ifndef _WIN64
#ifdef _DEBUG
FORCEINLINE static __int64 GetClockCount()
Expand All @@ -463,12 +463,12 @@ FORCEINLINE static __int64 GetClockCount()
return cycles.QuadPart;
}

//使用例
//使用例
//{
// CDebugElapsedCounter _cntr("hogehoge");
// : (適当な処理)
// : (適当な処理)
//}
//出力例: "hogehoge: 10000 clocks"
//出力例: "hogehoge: 10000 clocks"
class CDebugElapsedCounter
{
private:
Expand Down Expand Up @@ -577,7 +577,7 @@ int _httoi(const TCHAR *value)
return result;
}
//atofにデフォルト値を返せるようにしたような物
//atofにデフォルト値を返せるようにしたような物
float _StrToFloat(LPCTSTR pStr, float fDefault)
{
#define isspace(ch) (ch == _T('\t') || ch == _T(' '))
Expand Down
48 changes: 24 additions & 24 deletions dll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ CMemLoadDll::~CMemLoadDll()
{
//ASSERT(pImageBase != NULL);
//ASSERT(pDllMain != NULL);
//脱钩,准备卸载dll
//脱钩,准备卸载dll
if (m_bInitDllMain)
pDllMain((HINSTANCE)pImageBase,DLL_PROCESS_DETACH,0);
VirtualFree((LPVOID)pImageBase, 0, MEM_RELEASE);
}
}

//MemLoadLibrary函数从内存缓冲区数据中加载一个dll到当前进程的地址空间,缺省位置0x10000000
//返回值: 成功返回TRUE , 失败返回FALSE
//返回值: 成功返回TRUE , 失败返回FALSE
//lpFileData: 存放dll文件数据的缓冲区
//DataLength: 缓冲区中数据的总长度
BOOL CMemLoadDll::MemLoadLibrary(void* lpFileData, int DataLength, bool bInitDllMain, bool bFreeOnRavFail)
{
this->m_bInitDllMain = bInitDllMain;
if(pImageBase != NULL)
{
return FALSE; //已经加载一个dll,还没有释放,不能加载新的dll
return FALSE; //已经加载一个dll,还没有释放,不能加载新的dll
}
//检查数据有效性,并初始化
if(!CheckDataValide(lpFileData, DataLength))return FALSE;
Expand All @@ -43,7 +43,7 @@ BOOL CMemLoadDll::MemLoadLibrary(void* lpFileData, int DataLength, bool bInitDll
else
{
CopyDllDatas(pMemoryAddress, lpFileData); //复制dll数据,并对齐每个段
//重定位信息
//重定位信息
/*if(pNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress >0
&& pNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size>0)
{
Expand All @@ -56,7 +56,7 @@ BOOL CMemLoadDll::MemLoadLibrary(void* lpFileData, int DataLength, bool bInitDll
return FALSE;
}*/
//修改页属性。应该根据每个页的属性单独设置其对应内存页的属性。这里简化一下。
//统一设置成一个属性PAGE_EXECUTE_READWRITE
//统一设置成一个属性PAGE_EXECUTE_READWRITE
unsigned long old;
VirtualProtect(pMemoryAddress, ImageSize, PAGE_EXECUTE_READWRITE,&old);
}
Expand All @@ -66,7 +66,7 @@ BOOL CMemLoadDll::MemLoadLibrary(void* lpFileData, int DataLength, bool bInitDll
//接下来要调用一下dll的入口函数,做初始化工作。
pDllMain = (ProcDllMain)(pNTHeader->OptionalHeader.AddressOfEntryPoint +(DWORD_PTR) pMemoryAddress);
BOOL InitResult = !bInitDllMain || pDllMain((HINSTANCE)pMemoryAddress,DLL_PROCESS_ATTACH,0);
if(!InitResult) //初始化失败
if(!InitResult) //初始化失败
{
pDllMain((HINSTANCE)pMemoryAddress,DLL_PROCESS_DETACH,0);
VirtualFree(pMemoryAddress,0,MEM_RELEASE);
Expand Down Expand Up @@ -136,10 +136,10 @@ FARPROC CMemLoadDll::MemGetProcAddress(LPCSTR lpProcName)
}


// 重定向PE用到的地址
// 重定向PE用到的地址
void CMemLoadDll::DoRelocation( void *NewBase)
{
/* 重定位表的结构:
/* 重定位表的结构:
// DWORD sectionAddress, DWORD size (包括本节需要重定位的数据)
// 例如 1000节需要修正5个重定位数据的话,重定位表的数据是
// 00 10 00 00 14 00 00 00 xxxx xxxx xxxx xxxx xxxx 0000
Expand All @@ -166,7 +166,7 @@ void CMemLoadDll::DoRelocation( void *NewBase)
// 举例:
// pLoc->VirtualAddress = 0x1000;
// pLocData[i] = 0x313E; 表示本节偏移地址0x13E处需要修正
// 因此 pAddress = 基地址 + 0x113E
// 因此 pAddress = 基地址 + 0x113E
// 里面的内容是 A1 ( 0c d4 02 10) 汇编代码是: mov eax , [1002d40c]
// 需要修正1002d40c这个地址
DWORD * pAddress = (DWORD *)((DWORD_PTR)NewBase + pLoc->VirtualAddress + (pLocData[i] & 0x0FFF));
Expand All @@ -187,7 +187,7 @@ BOOL CMemLoadDll::FillRavAddress(void *pImageBase)
// DWORD OriginalFirstThunk; // 0表示结束,否则指向未绑定的IAT结构数组
// DWORD TimeDateStamp;
// DWORD ForwarderChain; // -1 if no forwarders
// DWORD Name; // 给出dll的名字
// DWORD Name; // 给出dll的名字
// DWORD FirstThunk; // 指向IAT结构数组的地址(绑定后,这些IAT里面就是实际的函数地址)
unsigned long Offset = pNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress ;
if(Offset == 0) return TRUE; //No Import Table
Expand All @@ -196,7 +196,7 @@ BOOL CMemLoadDll::FillRavAddress(void *pImageBase)
{
PIMAGE_THUNK_DATA32 pRealIAT = (PIMAGE_THUNK_DATA32)((DWORD_PTR)pImageBase + pID->FirstThunk);
PIMAGE_THUNK_DATA32 pOriginalIAT = (PIMAGE_THUNK_DATA32)((DWORD_PTR)pImageBase + pID->OriginalFirstThunk);
//获取dll的名字
//获取dll的名字
WCHAR buf[256]; //dll name;
BYTE* pName = (BYTE*)((DWORD_PTR)pImageBase + pID->Name);
int i;
Expand All @@ -210,7 +210,7 @@ BOOL CMemLoadDll::FillRavAddress(void *pImageBase)
HMODULE hDll = GetModuleHandle(buf);
if(hDll == NULL)return FALSE; //NOT FOUND DLL
//获取DLL中每个导出函数的地址,填入IAT
//每个IAT结构是 :
//每个IAT结构是 :
// union { PBYTE ForwarderString;
// PDWORD Function;
// DWORD Ordinal;
Expand All @@ -235,7 +235,7 @@ BOOL CMemLoadDll::FillRavAddress(void *pImageBase)
// else
lpFunction = GetProcAddress(hDll, (char *)pByName->Name);
}
if(lpFunction != NULL) //找到了!
if(lpFunction != NULL) //找到了!
{
pRealIAT[i].u1.Function = (DWORD) lpFunction;
}
Expand All @@ -249,7 +249,7 @@ BOOL CMemLoadDll::FillRavAddress(void *pImageBase)
}

//CheckDataValide函数用于检查缓冲区中的数据是否有效的dll文件
//返回值: 是一个可执行的dll则返回TRUE,否则返回FALSE。
//返回值: 是一个可执行的dll则返回TRUE,否则返回FALSE。
//lpFileData: 存放dll数据的内存缓冲区
//DataLength: dll文件的长度
BOOL CMemLoadDll::CheckDataValide(void* lpFileData, int DataLength)
Expand All @@ -262,7 +262,7 @@ BOOL CMemLoadDll::CheckDataValide(void* lpFileData, int DataLength)

//检查长度
if((DWORD)DataLength < (pDosHeader->e_lfanew + sizeof(IMAGE_NT_HEADERS32)) ) return FALSE;
//取得pe头
//取得pe头
pNTHeader = (PIMAGE_NT_HEADERS32)( (DWORD_PTR)lpFileData + (DWORD_PTR)pDosHeader->e_lfanew); // PEͷ
//检查pe头的合法性
if(pNTHeader->Signature != IMAGE_NT_SIGNATURE) return FALSE; //0x00004550 : PE00
Expand All @@ -273,7 +273,7 @@ BOOL CMemLoadDll::CheckDataValide(void* lpFileData, int DataLength)
if(pNTHeader->FileHeader.SizeOfOptionalHeader != sizeof(IMAGE_OPTIONAL_HEADER32)) return FALSE;


//取得节表(段表)
//取得节表(段表)
pSectionHeader = (PIMAGE_SECTION_HEADER)((DWORD_PTR)pNTHeader + sizeof(IMAGE_NT_HEADERS32));
//验证每个节表的空间
for(int i=0; i< pNTHeader->FileHeader.NumberOfSections; i++)
Expand All @@ -297,10 +297,10 @@ int CMemLoadDll::CalcTotalImageSize()

// 计算所有头的尺寸。包括dos, coff, pe头 和 段表的大小
Size = GetAlignedSize(pNTHeader->OptionalHeader.SizeOfHeaders, nAlign);
// 计算所有节的大小
// 计算所有节的大小
for(int i=0; i < pNTHeader->FileHeader.NumberOfSections; ++i)
{
//得到该节的大小
//得到该节的大小
int CodeSize = pSectionHeader[i].Misc.VirtualSize ;
int LoadSize = pSectionHeader[i].SizeOfRawData;
int MaxSize = (LoadSize > CodeSize)?(LoadSize):(CodeSize);
Expand All @@ -320,14 +320,14 @@ void CMemLoadDll::CopyDllDatas(void* pDest, void* pSrc)
int HeaderSize = pNTHeader->OptionalHeader.SizeOfHeaders;
int SectionSize = pNTHeader->FileHeader.NumberOfSections * sizeof(IMAGE_SECTION_HEADER);
int MoveSize = HeaderSize + SectionSize;
//复制头和段信息
//复制头和段信息
memmove(pDest, pSrc, MoveSize);

//复制每个节
//复制每个节
for(int i=0; i < pNTHeader->FileHeader.NumberOfSections; ++i)
{
if(pSectionHeader[i].VirtualAddress == 0 || pSectionHeader[i].SizeOfRawData == 0)continue;
// 定位该节在内存中的位置
// 定位该节在内存中的位置
void *pSectionAddress = (void *)((DWORD_PTR)pDest + pSectionHeader[i].VirtualAddress);
// 复制段数据到虚拟内存
memmove((void *)pSectionAddress,
Expand All @@ -336,11 +336,11 @@ void CMemLoadDll::CopyDllDatas(void* pDest, void* pSrc)
}

//修正指针,指向新分配的内存
//新的dos头
//新的dos头
pDosHeader = (PIMAGE_DOS_HEADER)pDest;
//新的pe头地址
//新的pe头地址
pNTHeader = (PIMAGE_NT_HEADERS32)((DWORD_PTR)pDest + (DWORD_PTR)(pDosHeader->e_lfanew));
//新的节表地址
//新的节表地址
pSectionHeader = (PIMAGE_SECTION_HEADER)((DWORD_PTR)pNTHeader + sizeof(IMAGE_NT_HEADERS32));
return ;
}
Loading

0 comments on commit cc82d3a

Please sign in to comment.