📄 ollymachine.cpp
字号:
///////////////////////////////////////////////////////////////////////////////
//
// FileName : OllyMachine.cpp
// Version : 0.10
// Author : Luo Cong
// Date : 2004-10-16 2:21:49
// Comment :
//
///////////////////////////////////////////////////////////////////////////////
// OllyMachine.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include <Shlwapi.h>
#include <afxdllx.h>
#include "Assembler.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
static AFX_EXTENSION_MODULE OllyMachineDLL = { NULL, NULL };
#include "StdAfx.h"
#include <commdlg.h>
#include <stdio.h>
#include "OllyMachine.h"
#include "Assembler.h"
HINSTANCE g_hInstance = NULL;
HWND g_hWndMain = NULL;
char g_szPluginName[] = "OllyMachine";
char g_szOllyMachineClass[32];
static char g_szIniFilePathName[MAX_PATH];
#ifndef NUM_OF_RECENT_FILE
#define NUM_OF_RECENT_FILE 16
#endif
typedef struct tagRECENTFILE
{
ulong index; // Bookmark index (0..9)
ulong size; // Size of index, always 1 in our case
ulong type; // Type of entry, always 0
FILETIME runtime; // Recent File Index
char szFileName[MAX_PATH];
} RECENTFILE;
static t_table g_windowRecentFile; // Recent File table
typedef struct tagOMB_FILE_HEADER
{
long Signature; // "OMLC"
unsigned char LinkerVersionHi;
unsigned char LinkerVersionLo;
unsigned long SizeOfCode;
unsigned long SizeOfData;
unsigned long BaseOfCode;
unsigned long BaseOfData;
} OMB_FILE_HEADER;
static char *g_OM_RecentFiles = "OllyMacnine Recent Files";
static char *g_OMB_Signature = "OMLC";
static CVM VM;
static void AddNewRecentFile(const char *szFileName)
{
ASSERT(szFileName);
RECENTFILE mark;
RECENTFILE *pMark;
SYSTEMTIME sysTime;
int i;
mark.index = g_windowRecentFile.data.n;
mark.size = 1;
mark.type = 0;
// search the same name which is recent opened
for (i = 0; i < g_windowRecentFile.data.n; ++i)
{
pMark = (RECENTFILE *)Findsorteddata(&(g_windowRecentFile.data), i);
if (NULL != pMark)
{
if (0 == strcmp(pMark->szFileName, szFileName))
{
// if found, replace it
mark.index = pMark->index;
Deletesorteddata(&(g_windowRecentFile.data), i);
}
}
}
GetLocalTime(&sysTime);
SystemTimeToFileTime(&sysTime, &mark.runtime);
strcpy(mark.szFileName, szFileName);
Addsorteddata(&(g_windowRecentFile.data), &mark);
}
static int CompileFile(
/* [size_is][in] */ const unsigned int unFileNameSize,
/* [in] */ const char *szFileName,
/* [out] */ unsigned int *unCodeSize,
/* [out] */ unsigned char **ucCode,
/* [out] */ unsigned int *unDataSize,
/* [out] */ char **cData
)
{
ASSERT(szFileName);
ASSERT(unCodeSize);
ASSERT(ucCode);
ASSERT(unDataSize);
ASSERT(cData);
int nRetResult = 0;
int nRetCode;
CAssembler Assembler;
OM_PROCESS_ERROR((strlen(szFileName) + 1) == unFileNameSize);
nRetCode = Assembler.Assemble(strlen(szFileName) + 1, szFileName);
OM_PROCESS_ERROR(nRetCode);
*unDataSize = Assembler.GetDataSize();
if (*unDataSize)
{
*cData = (char *)malloc(*unDataSize);
OM_PROCESS_ERROR(cData);
nRetCode = Assembler.GetDataContents(*cData);
OM_PROCESS_ERROR(nRetCode);
}
*unCodeSize = Assembler.GetCodeSize();
*ucCode = (unsigned char *)malloc(*unCodeSize);
OM_PROCESS_ERROR(ucCode);
nRetCode = Assembler.GetCodeContents(*ucCode);
OM_PROCESS_ERROR(nRetCode);
nRetResult = 1;
Exit0:
return nRetResult;
}
static int GetFileName(
/* [in] */ const int nMode, // 1 == read, 2 == write
/* [out] */ char *szFileName,
/* [in] */ const char *szInitFileName = NULL
)
{
ASSERT(szFileName);
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = g_hWndMain;
ofn.lpstrFile = szFileName;
// Set lpstrFile[0] to '\0' so that GetOpenFileName does not
// use the contents of szFile to initialize itself.
ofn.nMaxFile = MAX_PATH;
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = '\0';
ofn.nMaxFileTitle = 0;
if (1 == nMode)
{
ofn.lpstrFilter =
"OllyMachine files (*.oms;*.txt;*.omb)\0*.oms;*.txt;*.omb\0"
"All files (*.*)\0*.*\0";
ofn.lpstrFile[0] = '\0';
ofn.lpstrTitle = "Please select source filename";
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
}
else if (2 == nMode)
{
char *pDest = NULL;
int nPos;
strcpy(szFileName, szInitFileName);
pDest = strrchr(szInitFileName, '.');
if (NULL != pDest)
{
nPos = pDest - szInitFileName;
strcpy(&szFileName[nPos], ".omb");
}
ofn.lpstrFilter =
"OllyMachine files (*.omb)\0*.omb\0"
"All files (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.lpstrTitle = "Please enter target filename";
}
return GetOpenFileName(&ofn);
}
static int Loader(
/* [size_is][in] */ const unsigned int unFileNameBinSize,
/* [in] */ const char *szFileNameBin,
/* [out] */ unsigned int *unCodeSize,
/* [out] */ unsigned char **ucCode,
/* [out] */ unsigned int *unDataSize,
/* [out] */ char **cData
)
{
ASSERT(unCodeSize);
ASSERT(ucCode);
ASSERT(unDataSize);
ASSERT(cData);
int nRetResult = 0;
OMB_FILE_HEADER OMBFileHeader;
FILE *fp_in = NULL;
OM_PROCESS_ERROR((strlen(szFileNameBin) + 1) == unFileNameBinSize);
fp_in = fopen(szFileNameBin, "rb");
if (NULL == fp_in)
{
ShowErrMsg("Error on opening bin file!");
goto Exit0;
}
fread(&OMBFileHeader, 1, sizeof(OMB_FILE_HEADER), fp_in);
// Check that if it is an OMB file!
OM_PROCESS_ERROR(OMBFileHeader.Signature == *(long *)g_OMB_Signature);
*unCodeSize = OMBFileHeader.SizeOfCode;
*ucCode = (unsigned char *)malloc(*unCodeSize);
OM_PROCESS_ERROR(ucCode);
fseek(fp_in, OMBFileHeader.BaseOfCode, SEEK_SET);
fread(*ucCode, 1, *unCodeSize, fp_in);
if (OMBFileHeader.SizeOfData)
{
*unDataSize = OMBFileHeader.SizeOfData;
*cData = (char *)malloc(*unDataSize);
OM_PROCESS_ERROR(cData);
fseek(fp_in, OMBFileHeader.BaseOfData, SEEK_SET);
fread(*cData, 1, *unDataSize, fp_in);
}
else
{
*unDataSize = 0;
}
nRetResult = 1;
Exit0:
if (fp_in)
{
fclose(fp_in);
fp_in = NULL;
}
return nRetResult;
}
static int Linker(
/* [len_is][in] */ const unsigned int unCodeSize,
/* [in] */ const unsigned char *ucCode,
/* [len_is][in] */ const unsigned int unDataSize,
/* [in] */ const char *cData,
/* [size_is][in] */ const unsigned int unFileNameBinSize,
/* [in] */ const char *szFileNameBin
)
{
ASSERT(ucCode);
int nRetResult = 0;
OMB_FILE_HEADER OMBFileHeader;
FILE *fp_out = NULL;
OM_PROCESS_ERROR((strlen(szFileNameBin) + 1) == unFileNameBinSize);
fp_out = fopen(szFileNameBin, "wb");
if (NULL == fp_out)
{
ShowErrMsg("Error on creating bin file!");
goto Exit0;
}
OMBFileHeader.Signature = *(unsigned long *)g_OMB_Signature;
OMBFileHeader.LinkerVersionHi = VERSIONHI;
OMBFileHeader.LinkerVersionLo = VERSIONLO;
OMBFileHeader.SizeOfCode = unCodeSize;
OMBFileHeader.SizeOfData = unDataSize;
OMBFileHeader.BaseOfCode = sizeof(OMB_FILE_HEADER);
OMBFileHeader.BaseOfData = sizeof(OMB_FILE_HEADER) + unCodeSize;
fwrite(&OMBFileHeader, 1, sizeof(OMB_FILE_HEADER), fp_out);
fwrite(ucCode, 1, unCodeSize, fp_out);
fwrite(cData, 1, unDataSize, fp_out);
nRetResult = 1;
Exit0:
if (fp_out)
{
fclose(fp_out);
fp_out = NULL;
}
return nRetResult;
}
static int ProcessFile(
/* [in] */ const int nDoWhat, // 1 = run, 2 = compile & link
/* [in] */ const char *szFileNameIn = NULL
)
{
int nRetResult = 0;
int nRetCode;
char szFileName[MAX_PATH];
MCSTATUS OM_status;
unsigned int unCodeSize;
unsigned char *ucCode = NULL;
unsigned int unDataSize;
char *cData = NULL;
if (szFileNameIn && ('\0' != szFileNameIn[0]))
strcpy(szFileName, szFileNameIn);
else
{
nRetCode = GetFileName(1, szFileName);
OM_PROCESS_ERROR(nRetCode);
}
AddNewRecentFile(szFileName);
nRetCode = Loader(
strlen(szFileName) + 1,
szFileName,
&unCodeSize,
&ucCode,
&unDataSize,
&cData
);
if (0 == nRetCode)
{
nRetCode = CompileFile(
strlen(szFileName) + 1,
szFileName,
&unCodeSize,
&ucCode,
&unDataSize,
&cData
);
if (0 == nRetCode)
{
ShowErrMsg("Compile Error!");
goto Exit0;
}
}
if (1 == nDoWhat) // run
{
OM_status = VM.GetStatus();
if (MCS_BACKTOOD != OM_status)
VM.Reset();
if (unDataSize)
{
nRetCode = VM.SetData(unDataSize, cData);
OM_PROCESS_ERROR(nRetCode);
}
nRetCode = VM.SetCode(unCodeSize, ucCode);
OM_PROCESS_ERROR(nRetCode);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -