⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 assembler.h

📁 Ollydbg环境下的一款插件源代码
💻 H
字号:
///////////////////////////////////////////////////////////////////////////////
//
//  FileName    :   Assembler.h
//  Version     :   0.10
//  Author      :   Luo Cong
//  Date        :   2004-10-28 22:11:42
//  Comment     :   first version started on 2004-10-16
//
///////////////////////////////////////////////////////////////////////////////

#ifndef __ASSEMBLER_H__
#define __ASSEMBLER_H__

#include "VM.h"

#ifndef CODE_INIT_SIZE
#define CODE_INIT_SIZE  1024
#endif

#ifndef CODE_INC_SIZE
#define CODE_INC_SIZE   1024
#endif

#ifndef DATA_INIT_SIZE
#define DATA_INIT_SIZE  128
#endif

#ifndef DATA_INC_SIZE
#define DATA_INC_SIZE   128
#endif

// if add a new type or delete an exist type, must modify Match() function!
typedef enum tagTOKENTYPE
{
    TOK_UNKNOWN = 0,
    TOK_TOKEN,
    TOK_REGISTER,
    TOK_LABEL,
    TOK_INDEXOF,
    TOK_LBRACE,     // {
    TOK_RBRACE,     // }
    TOK_LBRACKET,   // [
    TOK_RBRACKET,   // ]
    TOK_DOT,        // .
    TOK_OPERATOR,   // +, -, *
    TOK_DIGIT_DEC,
    TOK_DIGIT_HEX,
    TOK_BAD_DIGIT_HEX,
    TOK_LINECOMMENT,
    TOK_COMMENT0,
    TOK_BLOCKCOMMENT,
    TOK_BAD_COMMENT,
    TOK_COMMA,
    TOK_CRLF,
    TOK_DQUOTE,
    TOK_EOF,
    TOK_ERR
} TOKENTYPE;

typedef enum tagERRTYPE
{
    ERR_UNKNOWN = 0,
    ERR_NOID,                       // undeclared identifier
    ERR_BADBLOCKCMT,                // bad block comment
    ERR_REDEFID,                    // re-defined identifier
    ERR_REDEFLABEL,                 // re-defined label
    ERR_BADHEX,                     // bad hex
    ERR_NOLINEEND,                  // no line end
    ERR_NEWLINEINCONST,             // new line in const
    ERR_NOINCLUDEFILE,              // no include file
    ERR_ERRORONID,                  // error on identifier
    ERR_EXPECTED,                   // expected something
    ERR_IDISAKEYWORDCANTBEALABEL,   // id is a keyword, can't be a label
    ERR_MUSTFOLLOWAREGISTER,        // must follow a register
    ERR_EMBEDDEDCODESIZETOOBIG,     // embedded code size too big
    ERR_EMBEDDEDASMCANTCOMPILE,     // can not compile embedded asm
} ERRTYPE;

typedef struct tagERRINFO
{
    ERRTYPE     ErrType;
    CString     TokenName;
    char        FileName[MAX_PATH];
    int         LineNum;
} ERRINFO;

typedef enum tagWARNTYPE
{
    WARN_UNREFLABEL = 0
} WARNTYPE;

typedef struct tagWARNINFO
{
    WARNTYPE    WarnType;
    CString     TokenName;
    char        FileName[MAX_PATH];
    int         LineNum;
} WARNINFO;

typedef struct tagFILEINFO
{
    char        FileName[MAX_PATH];
    FILE       *fp;
    int         LineNum;
} FILEINFO;

typedef struct tagLABELINFO
{
    CString     Name;
    long        Addr;
    int         Refed;  // referenced?
    char        FileName[MAX_PATH];
    int         LineNum;
} LABELINFO;

typedef enum tagPASSTYPE
{
    PT_PASS1 = 0,
    PT_PASS2
} PASSTYPE;

typedef struct tagAPIPARAMETER
{
    CString Name;
    long    lFileOffset;
} APIPARAMETER;


class CAssembler
{
protected:
    int m_nStopFlag;
    int m_nFileListCount;
    CList<ERRINFO, ERRINFO&> *m_pErrList;
    CList<WARNINFO, WARNINFO&> *m_pWarnList;
    CList<LABELINFO, LABELINFO&> *m_pLabelList;
    CVM *m_VM;

    unsigned char *m_Code;
    unsigned int m_unCodeLen;
    unsigned int m_unCodeSize;

    char *m_Data;
    unsigned int m_unDataLen;
    unsigned int m_unDataSize;

private:
    // Pass 1: make symbol table
    int MakeSymbolTable(
        /* [size_is][in] */ const UINT unFileNameSize,
        /* [in] */          const char *szFileName
    );

    // Pass 2: generate codes
    int GenerateCodes(
        /* [size_is][in] */ const UINT unFileNameSize,
        /* [in] */          const char *szFileName
    );

    TOKENTYPE NextToken(
        /* [in][out] */     FILEINFO *fii,
        /* [out] */         CString *strTokenName,
        /* [in] */          const int nIsInEmbeddedAsm = 0
    );

    int IsRegister(
        /* [size_is][in] */ const UINT unTokenNameSize,
        /* [in] */          const char *szTokenName,
        /* [out] */         REGISTERTYPE *RegType = NULL
    );

    int IsMnemonic(
        /* [size_is][in] */ const UINT unTokenNameSize,
        /* [in] */          const char *szTokenName,
        /* [out] */         MNEMONICTYPE *MnemonicType = NULL
    ) const;

    int GetString(
        /* [in][out] */     FILEINFO *fii,
        /* [out] */         CString *strString
    );

    int GetEnd(
        /* [in][out] */     FILEINFO *fii,
        /* [in] */          const MNEMONICTYPE MneType
    );

    int ProcessInclude(
        /* [in][out] */     FILEINFO *fii,
        /* [in] */          const PASSTYPE pt
    );

    int ProcessInvoke(
        /* [in][out] */     FILEINFO *fii,
        /* [in] */          const PASSTYPE pt
    );

    int ProcessEmbeddedAsm(
        /* [in][out] */     FILEINFO *fii,
        /* [in] */          const PASSTYPE pt
    );

    int DeclareLabel(
        /* [in] */          const FILEINFO *fii,
        /* [in] */          const UINT unLabelNameSize,
        /* [in] */          const char *szLabelName
    );

    int SetLabelRef(
        /* [size_is][in] */ const UINT unLabelNameSize,
        /* [in] */          const char *szLabelName
    );

    int FindLabelName(
        /* [size_is][in] */ const UINT unLabelNameSize,
        /* [in] */          const char *szLabelName,
        /* [out] */         LABELINFO *LabelFound = NULL,
        /* [out] */         int *nLblIdx = NULL
    ) const;

    int AddLabel(
        /* [in] */          const FILEINFO *fii,
        /* [in] */          const UINT unLabelNameSize,
        /* [in] */          const char *szLabelName
    );

    void Error(
        /* [in] */          const ERRTYPE ErrType,
        /* [in] */          const char *szTokenName,
        /* [in] */          const char *szFileName,
        /* [in] */          const int nLineNum
    );

    void Warning(
        /* [in] */          const WARNTYPE WarnType,
        /* [in] */          const char *szTokenName,
        /* [in] */          const char *szFileName,
        /* [in] */          const int nLineNum
    );

    void CheckLabelRef();

    int ReportFails() const;

    int CheckCodeCapacity(
        /* [in] */          const int nExpectedLen
    );

    int CheckDataCapacity(
        /* [in] */          const int nExpectedLen
    );

    int Match(
        /* [in][out] */     FILEINFO *fii,
        /* [in] */          const TOKENTYPE TokType,
        /* [out] */         CString *strTokenName = NULL
    );

// Generate methods:
private:
    int GenArithmetic(
        /* [in][out] */     FILEINFO *fii,
        /* [in] */          const MNEMONICTYPE MneType
    );

    int GenPush(
        /* [in][out] */     FILEINFO *fii,
        /* [in] */          const int nNeedGetEnd = 1
    );

    /**
     *  Constant
    **/
    int C(
        /* [in] */          const long lConstant
    );

    /**
     *  Register
    **/
    int R(
        /* [in] */          const FILEINFO *fii,
        /* [size_is][in] */ const unsigned int unRegNameSize,
        /* [in] */          const char *szRegName
    );

    /**
     *  String
    **/
    int S(
        /* [size_is][in] */ const unsigned int unStringSize,
        /* [in] */          const char *szString
    );

    /**
     *  Instruction
    **/
    int I(
        /* [in][out] */     FILEINFO *fii,
        /* [in] */          const MNEMONICTYPE MneType
    );

    /**
     *  Instruction Label
    **/
    int IL(
        /* [in][out] */     FILEINFO *fii,
        /* [in] */          const MNEMONICTYPE MneType
    );

    /**
     *  Instruction Constant
    **/
    int IC(
        /* [in][out] */     FILEINFO *fii,
        /* [in] */          const MNEMONICTYPE MneType
    );

    /**
     *  Instruction Register
    **/
    int IR(
        /* [in][out] */     FILEINFO *fii,
        /* [in] */          const MNEMONICTYPE MneType
    );

    /**
     *  Instruction Register, Register
    **/
    int IRR(
        /* [in][out] */     FILEINFO *fii,
        /* [in] */          const MNEMONICTYPE MneType
    );

    /**
     *  Instruction Register, Constant
    **/
    int IRC(
        /* [in][out] */     FILEINFO *fii,
        /* [in] */          const MNEMONICTYPE MneType
    );

    /**
     *  Instruction Register, String
    **/
    int IRS(
        /* [in][out] */     FILEINFO *fii,
        /* [in] */          const MNEMONICTYPE MneType
    );

public:
    CAssembler();
    ~CAssembler();

    int Assemble(
        /* [size_is][in] */ const UINT unFileNameSize,
        /* [in] */          const char *szFileName
    );

    UINT GetCodeSize() const;

    int GetCodeContents(
        /* [out] */         unsigned char *ucCode
    ) const;

    UINT GetDataSize() const;

    int GetDataContents(
        /* [out] */         char *ucData
    ) const;
};

inline CAssembler::CAssembler()
{
    m_nStopFlag = 0;
    m_nFileListCount = 0;
    m_pErrList = new CList<ERRINFO, ERRINFO&>;
    m_pWarnList = new CList<WARNINFO, WARNINFO&>;
    m_pLabelList = new CList<LABELINFO, LABELINFO&>;
    m_VM = new CVM;

    m_unCodeLen = 0;
    m_unCodeSize = CODE_INIT_SIZE;
    m_Code = (unsigned char *)malloc(CODE_INIT_SIZE);

    m_unDataLen = 0;
    m_unDataSize = DATA_INIT_SIZE;
    m_Data = (char *)malloc(DATA_INIT_SIZE);
}

inline CAssembler::~CAssembler()
{
    if (m_pLabelList)
    {
        delete m_pLabelList;
        m_pLabelList = NULL;
    }
    if (m_VM)
    {
        delete m_VM;
        m_VM = NULL;
    }
    if (m_Data)
    {
        free(m_Data);
        m_Data = NULL;
    }
    if (m_Code)
    {
        free(m_Code);
        m_Code = NULL;
    }
    if (m_pWarnList)
    {
        delete m_pWarnList;
        m_pWarnList = NULL;
    }
    if (m_pErrList)
    {
        delete m_pErrList;
        m_pErrList = NULL;
    }
}

#endif  // __ASSEMBLER_H__

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -