Click here to Skip to main content
15,881,938 members
Articles / Desktop Programming / MFC

A Great Protocol Analyser

Rate me:
Please Sign up or sign in to vote.
3.39/5 (12 votes)
16 Aug 2007CPOL2 min read 39.7K   2.9K   32  
A programmable, easy-to-use protocol decoder for parsing and displaying binary package
#if !defined(_CPLUSBASE_H)
#define _CPLUSBASE_H

#include "PubHeader.h"
#include "Base.h"
#include "Lex.h"
#include "Varient.h"
#include "Utility.h"
#include "ObGrid.h"

////////////////////////////////////////////////////////
#define SHOW_WHEN_COMMENT_NOT_FOUND  "unknow"

#define NW_TRAN_MODE_L_TO_H    1
#define NW_TRAN_MODE_H_TO_L    0
#define DEFAULT_NW_TRAN_MODE   NW_TRAN_MODE_H_TO_L


////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
class CErrHandler
{
public: 
    bool m_bFailStop;
    
public:
    CErrHandler() 
    { 
        Clear(); 
    }
    virtual ~CErrHandler() {}
    
    void SetLastError(Location& locat,const char* pszFormat, ...)
    {
        m_nLastErrorLocat.m_nLocat =locat.m_nLocat;
        m_nLastErrorLocat.m_nLine  =locat.m_nLine;
        
        va_list arglist;
        va_start(arglist, pszFormat);
        m_strLastError.Format_(pszFormat, arglist);
        va_end  (arglist);
    }
    void Clear()
    {
        m_bFailStop=false;
        m_nLastErrorLocat.setLocat(-1,-1);   
        m_strLastError="";
    }
    
    char*     GetLastError()      { return m_strLastError.GetBuffer();  }
    Location& GetLastErrorLocat() { return m_nLastErrorLocat;  }
    
    OTSTR DumpLastError()
    {
        OTSTR str;
        str.Format("%s  %s",m_nLastErrorLocat.Dump().GetBuffer(),m_strLastError.GetBuffer());
        return str;
    }
    
protected:
    Location  m_nLastErrorLocat;
    OTSTR     m_strLastError;                  
};

//////////////////////////////////////////////////////////////////

class CPlusLex: public CLex
{
public:
    enum register_type { REG_KEYWORD =0,    // KEYWORD�����������ʵĸ��ϴ�
                         REG_OPERATOR=1,    // Operator��RelateOperator֧��˫�ַ�
    };
    OTSTR       m_strLastError;
    
public:
    CPlusLex();	
   ~CPlusLex();	

    bool  OpenFile(char* szfilename,bool bAsTextFile=true);
    
    void  Reset(bool bResetBuffer);
    
    /////////////////////////////////////////////////////////////////////
    // Extend Function, implement through GetToken()
    int   GetToken(LexToken& Token, bool bJustPreview=false);     // KeyWord
    int   GetToken(LexToken& Token, WORD nExStyle, bool bJustPreview);

    bool  GetSpecificToken(LexToken& Token,long  TokenId);        // if fail, the locat will not be move forward
    bool  GetSpecificToken(LexToken& Token,char* szTokenName,bool bcase=false);
     
    Location& getLocation();
    bool  setLocation(Location& locat);
    
private:
    char*       m_pbuf;
    long        m_bufLen;
    Location    m_location;
    
    bool        m_bTokenValid;
    LexToken    m_Token;    // used for Preview
    
    ///////////////////////////////
    static StringArray  m_KeyWord_List;
    
    static bool Register(register_type type, char* lpstring);
    
public:
    //////////////////////////////////////////////////
    static void Initial();          
    static void Destroy();          

    static bool isValue(LexToken& Token);
    static bool isValueSet(LexToken& Token);
    static bool isKeyWord(LexToken& Token);
    static bool isIdent(LexToken& Token);   
};

//////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
// Var Definition Object
class VariableItemAttr: public TName
{
public:
    enum type_  
    { 
        // basic type
        T_INT1   = 0,   T_INT2    = 1,   T_INT4  = 2, 
        T_UINT1  = 3,   T_UINT2   = 4,   T_UINT4 = 5,
        T_PVOID  = 10, 
        T_PSTRUCT= 128,   
        T_NULL   = -1,  
        T_BYTE   = 20,  T_WORD    = 21,  T_DWORD = 22,
    };

protected:
    type_ m_type;
    long  m_size;

    type_ m_Extype;
    OTSTR m_strTypeName;
    
public:
    VariableItemAttr();
    virtual ~VariableItemAttr();
    
    bool  setAttr(char* varname,VariableItemAttr::type_ type,long size);
    
    VariableItemAttr::type_ getType();
    int   getSize();
    
    void  clone(VariableItemAttr& Object);
    void  clear();
    
    //////////////////////////////////////////////////////
    // Used for struct_typename and user defined typename
    void  setTypeName(char* typeName,VariableItemAttr::type_ extype);
    char* getTypeName();

    VariableItemAttr::type_ getExType();
    
    ////////////////////////////////////////////////////////////
    static bool isValidType(int type, bool bIncludeAnyType=true);
    static bool isBasicType(int type, bool bIncludeNull, bool bIncludePVOID=true);
    static bool isBasicType(char* szType, bool bIncludeNull, type_* returnType=NULL, type_* returnExType=NULL);

    static VariableItemAttr::type_ getBasicType(VariableItemAttr::type_ ExType);

    static char* getTypeDefaultName(int type); 
};

//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
class StructDefinition;

class VariableDefinition: public VariableItemAttr, public TObject
{
public:
    bool  m_isNetWorkTranMode_L_TO_H;
    
protected:
    StructDefinition*  m_pParent;
    WORD		       m_nIdentifierId;
    DWORD              m_wParam;                    
    
    StructDefinition*  m_pStructDefinitionObject;   

    OTSTR              m_strDescriptionEnd;

public:
    VariableDefinition();
    VariableDefinition(StructDefinition* pParent,WORD nIdentifierId);
   ~VariableDefinition();

    int    ObjectTypeId() { return 4; }
   
    OTSTR  GetVariableName();
    StructDefinition* GetParent() { return m_pParent; }
    
    void   setParam(DWORD param) { m_wParam=param; }
    DWORD  getParam()  { return m_wParam; }

    ///////////////////////////////////////////////////
    void   setDescriptionEnd(char* szDescription) { m_strDescriptionEnd=szDescription; }
    OTSTR& getDescriptionEnd() { return m_strDescriptionEnd; }
    
    void   setStructDefinitionObject(StructDefinition* pStructDefinition);  
    StructDefinition* getStructDefinitionObject();      

protected:
    friend class CVariableItem;
};

class StructDefinition: public TName, public TObject
{
public:
    bool  m_isNetWorkTranMode_L_TO_H;

protected:
    WORD  m_nIdentifierId;
    DWORD m_wParam;
    
    TObArray<VariableDefinition>  m_VariableDefinitionList;
    
public:
    StructDefinition();
    StructDefinition(WORD nIdentifierId);
   ~StructDefinition();

    int   ObjectTypeId() { return 3; }
   
    void  setParam(DWORD param) { m_wParam=param; }
    DWORD getParam()  { return m_wParam; }
    
    int   GetVariableCount();
    
    void  Add(VariableDefinition* pObject,DWORD nParam=0);
    
    VariableDefinition* GetAt(int nIndex,DWORD* lpParam=NULL);
    VariableDefinition* GetVariableDefinition(char* name); 
    
    void  RemoveAll(bool bDeleteObject=true);
};

////////////////////////////////////////////////////
class CEnumItem: public CVarient, public TObject
{
public:
    CEnumItem() {}
   ~CEnumItem() {}

    int ObjectTypeId() { return 1; }
};

class EnumItemList: protected TObArray<CEnumItem>, public TObject
{
public:
    enum { COMMENT_OR=0, COMMENT_EQUAL=1 };

public:
    EnumItemList();
   ~EnumItemList();

    int    ObjectTypeId() { return 2; }
   
    void   setName(char* szName);
    OTSTR& getName();
    
    void   setDescription(char* szDescription);
    OTSTR& getDescription();
    
    /////////////////////////////////////////////////////
    int    GetSize();
    
    CEnumItem* AddItem(CEnumItem& EnumItem);
    
    CEnumItem* GetAt(int nIndex);
    
    bool  Lookup_byValue(long value, OTSTR& strRetrun, char** szDescriptionReturn=NULL);
    
    void  RemoveAll();

    ////////////////////////////////////////////////////////////////
    bool  GetComments(long value,int nMode, OTSTR& strReturn,char* szDefault=NULL);
};


// DumpToList(nDisplayAttr)
enum 
{ 
    SH_SHOW_VALUE_ANYWAY=2, SH_SHOW_COMMENT=4
};


class CStruct;
class NullSyntaxObject;

class CVariableItem: public TVarient
{
public:
    CVariableItem();     
    virtual ~CVariableItem();

    bool   Create(CStruct* pParentStruct,VariableDefinition* pVariableDefinition);  
    bool   Create(StructDefinition* pStructDefinition, char* varname, long size);   
    
    bool   init()    { return false; }  // this function not allowed

    //////////////////////////////////////////////////
    VariableItemAttr&   GetAttr() { return m_Attr; }
    VariableDefinition* GetDefinition() { return m_pVariableDefinition; }
    
    int    getType() { return m_Attr.getType(); }
    int    getSize() { return m_Attr.getSize(); }
    OTSTR& getName() { return m_Attr.getName(); }
    char*  getTypeName()    { return m_Attr.getTypeName(); }
        
    void   setDescription(char* szDescription) { m_Attr.setDescription(szDescription); }
    OTSTR& getDescription() { return m_Attr.getDescription(); }

    OTSTR  GetVariableName();  // name[..]
    OTSTR  GetNameEx();        // structname.itemname
    
    ////////////////////////////////////////////////
    bool   isStruct();

    ////////////////////////////////////////////////
    virtual bool Clone(CVariableItem& VariableItem, bool bCloneNameAlso);   
    
    ////////////////////////////////////////////////
    bool   DumpToList(VirtualList& List,WORD nDisplayAttr);
    
protected:
    CStruct*            m_pParentStruct;
    VariableDefinition* m_pVariableDefinition;     
    StructDefinition*   m_pStructDefinition;       
    VariableItemAttr    m_Attr;
    long                m_Id;                      
    DWORD               m_wParam;
    
    virtual CStruct* NewStruct();

    CStruct* GetStructItem(int nIndex);             
    
    bool Clone(CVariableItem& VariableItem,CStruct* pParentStruct, bool bCloneNameAlso);
    void Destroy();

    ///////////////////////////////////////////////////
    bool DumpToList(VirtualList& List,WORD nDisplayAttr,long nSpaceLen, bool bOneOfMultiStructMember, bool bisFirstItemInStruct); 
    bool DumpStructToList(CVariableItem& VariableItem, int nIndex,VirtualList& List,WORD nDisplayAttr,long nSpaceLen,int nStructIndex,bool bShowChoiceItemMark); 
    
    bool SetListItemData(VirtualList& List,bool& bFirstRow, long& nItem, long nCol, DWORD Itemparam, char* szText);

    virtual void DumpLeadingMask(OTSTR& str) { str=""; }
    virtual bool ValueValid() { return true; }
    bool DumpBitsMask(OTSTR& str);

public:
    ////////////////////////////////////////////////////////////
    virtual bool isValueCommentValid() { return false; }  
    virtual int  GetValueComment(int nArrIndex,OTSTR& strComment,char* szDefault=SHOW_WHEN_COMMENT_NOT_FOUND) { strComment=""; return 0; } // false��ʾδȡ��ƥ���Comment ( or����equal��ƥ�� , strComment�ɷ��ز�ƥ������)

private:
    friend class CStruct;
};


///////////////////////////////////////////
class CStruct: public TName
{
public:
    CStruct();
    virtual ~CStruct();

    int  GetItemCount();
   
    CVariableItem* GetVariable  (char* name, bool bCaseSensitive); 
    CVariableItem* GetVariableAt(int nIndex); 
    
    ///////////////////////////////////////////////////
protected:
    StructDefinition*       m_pStructDefinition;        // Defined StructDefinition
    TObArray<CVariableItem> m_VariableItemList;
    DWORD                   m_wParam;
    
    bool Create(StructDefinition* pStructDefinition,char* szName=NULL);
    bool Clone (CStruct& StructItem);

    void Destroy();
     
    bool AddVariable(CVariableItem* pVariableItem);     // can be used For Choice

    ////////////////////////////////////////////////
    virtual CVariableItem*  NewVariableItem();
    
private:
    friend class CVariableItem;

    static NullSyntaxObject NullObject;
};

/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
static char* cDumpFormat[]=  
{
    0,           
    0,
    0,
    "0x%02X",
    "0x%04X",
    "0x%08X",
    0,
    0,
    0,
    0,
    0,
    0
};

static char* cDumpListFormat[]=  
{
    0,          
    0,
    0,
    "%02X",
    "%04X",
    "%08X",
    0,
    0,
    0,
    0,
    0,
    0
};


#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions