Click here to Skip to main content
15,897,273 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.9K   2.9K   32  
A programmable, easy-to-use protocol decoder for parsing and displaying binary package
#include "StdAfx.h"
#include "MsgUtility.h"

///////////////////////////////////////////////////////////////////////
BinStream::BinStream() 
{ 
    m_buf     =NULL;
    m_buflen  =0;
    m_curLocat=0;

    m_Valid_Datalen=0;
}

BinStream::~BinStream()
{
}

void BinStream::Attach(unsigned char* pbuf,long buflen)
{
    m_buf     =pbuf;
    m_buflen  =buflen;
    m_curLocat=0;
    
    setValidDatalen(m_buflen);
    
    setLocat(0);
}

long BinStream::getTotalLength()
{
    return m_buflen;
}

bool BinStream::setValidDatalen(long len)
{
    if ((len>=0)&&(len<=m_buflen))
    {
        m_Valid_Datalen=len;
        return true;
    }
    return false;
}

bool BinStream::isAtEnd()
{
    if (m_curLocat<m_Valid_Datalen)
        return false;
    
    return true;
}

bool BinStream::setLocat(long Locat)
{
    if ((Locat>=0)&&(Locat<m_Valid_Datalen))
    {
        m_curLocat=Locat;
        return true;
    }
    return false;
}

long BinStream::getLocat()
{
    return m_curLocat;
}

bool BinStream::Get_UINT1(UINT1& value, bool bJustPreview)
{
    long Locat=m_curLocat;
    
    if (Locat>=m_Valid_Datalen) return false;	
    
    value=m_buf[Locat];	
    
    if (!bJustPreview)
        m_curLocat=Locat+1;
    
    return true;
}

bool BinStream::Get_UINT2(UINT2& value,bool breverse, bool bJustPreview)
{
    long Locat=m_curLocat;
    
    if (Locat+1>=m_Valid_Datalen) return false;	
    
    if (! breverse)
        value=(m_buf[Locat  ]<<8) + m_buf[Locat+1];	
    else
        value=(m_buf[Locat+1]<<8) + m_buf[Locat];	
    
    if (!bJustPreview)
        m_curLocat=Locat+2;
    
    return true;
}

bool BinStream::Get_UINT4(UINT4& value,bool breverse, bool bJustPreview)
{
    long Locat=m_curLocat;
    
    if (Locat+3>=m_Valid_Datalen) return false;	

    if (! breverse)
        value=(m_buf[Locat]<<24)+(m_buf[Locat+1]<<16)+(m_buf[Locat+2]<<8)+m_buf[Locat+3];
      else
        value=(m_buf[Locat+3]<<24)+(m_buf[Locat+2]<<16)+(m_buf[Locat+1]<<8)+m_buf[Locat];

    if (!bJustPreview)
        m_curLocat=Locat+4;
      
    return true;
}

bool BinStream::Get_INT1(INT1& value, bool bJustPreview)
{
    UINT1 TmpVal;
    if (! Get_UINT1(TmpVal, bJustPreview)) return false;

    value=(INT1)TmpVal;

    return true;
}

bool BinStream::Get_INT2(INT2& value,bool breverse, bool bJustPreview)
{
    UINT2 TmpVal;
    if (! Get_UINT2(TmpVal,breverse,bJustPreview)) return false;
    
    value=(INT2)TmpVal;
    
    return true;
}

bool BinStream::Get_INT4(INT4& value,bool breverse, bool bJustPreview)
{
    UINT4 TmpVal;
    if (! Get_UINT4(TmpVal,breverse,bJustPreview)) return false;
    
    value=(INT4)TmpVal;
    
    return true;
}

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