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

📄 code.cpp

📁 linux 上http email 协议分析程序 主要能够处理大数据量的主干网的应用
💻 CPP
字号:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "Code.h"


char * CQprint::m_sNA ="0123456789ABCDEF";


CQprint ::CQprint()
{
}

CQprint :: ~CQprint()
{
}

void CQprint::Encode( char * szEncoding, char * szOutput )
{
        char * pin;
        char * pout;

        pin=szEncoding;
        pout=szOutput;
        while(*pin!=0)
        {
                if(*pin>=0 && *pin<=127)
                {
                        *pout=*pin;
                        pout++;
                }
                else
                {
                        char c;
                        c=*pin;

                        *pout='=',pout++;
                        *pout=m_sNA[c&0xf0>>4], pout++;
                        *pout=m_sNA[c&0x0f],pout++;
                }
                pin++;
        }
        *pout=0;
        return;
}

int CQprint::Decode( char * szDecoding,int inLen, char * szOutput,int obufLen )
{
	char * pin;
	char * pout;
	int olen=0;
	pin=szDecoding;
	pout=szOutput;
	while(*pin!=0)
	{
		if(*pin!='=')
		{
			*pout=*pin;
			pin++;
		}
		else
		{
			char c,a;
			pin++;
			
			if(*pin==0)
				return 0;
			c=*pin;
			if(c<='9')
				c-='0';
			else
			{
				c-='A';
				c+=10;
			}
			c<<=4;

			pin++;
			if(*pin==0)
				return 0;
			a=*pin;
			if(a<='9')
				a-='0';
			else
			{
				a-='A';
				a+=10;
			}
			c+=a;
			*pout=c;
			
		}
		pout++;
		olen++;
		if(olen>=obufLen)
			break;
	}
	*pout=0;
	return olen;

}


// Static Member Initializers
//

// The 7-bit alphabet used to encode binary information
char * CBase64::m_sBase64Alphabet =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

int CBase64::m_nMask[] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CBase64::CBase64()
{
}

CBase64::~CBase64()
{
}

void CBase64::Encode(char * szEncoding, char * szOutput)
{
	char *pout;
	int nNumBits = 6;
	unsigned int nDigit;
	int lp = 0;
	*szOutput=0;
	if( szEncoding == NULL )
		return ;
	m_szInput = szEncoding;
	m_nInputSize = strlen(szEncoding);

	m_nBitsRemaining = 0;
	nDigit = read_bits( nNumBits, &nNumBits, lp );
	while( nNumBits > 0 )
	{
		*pout = m_sBase64Alphabet[ (int)nDigit ];
		nDigit = read_bits( nNumBits, &nNumBits, lp );
		pout++;
	}
	*pout=0;
	// Pad with '=' as per RFC 1521
	int len=strlen(szOutput);
	len%=4;
	while(len>=0)
	{
		*pout='=';
		pout++;
		len--;
	}
	
	return ;
}

// The size of the output buffer must not be less than
// 3/4 the size of the input buffer. For simplicity,
// make them the same size.
int CBase64::Decode(char * szDecoding,int inLen, char * szOutput,int obufLen)
{
	char * sInput;
    	int c, lp =0;
	int nDigit;
    int nDecode[ 256 ];

	//printf("decodeing is [%s] len=[%d]\n",szDecoding, inLen);
	
	assert( szDecoding != NULL );
	assert( szOutput != NULL );
	m_lBitStorage = 0;
	m_nBitsRemaining =0;


	sInput = szDecoding;

	// Build Decode Table
	//
	int i;
	for( i = 0; i < 256; i++ ) 
		nDecode[i] = -2; // Illegal digit
	for( i=0; i < 64; i++ )
	{
		nDecode[ m_sBase64Alphabet[ i ] ] = i;
		nDecode[ m_sBase64Alphabet[ i ] | 0x80 ] = i; // Ignore 8th bit
		nDecode[ '=' ] = -1; 
		nDecode[ '=' | 0x80 ] = -1; // Ignore MIME padding char
    }

	// Clear the output buffer

	// Decode the Input
	//
	for( lp = 0, i = 0; lp < inLen; lp++ )
	{
		c = sInput[ lp ];
		nDigit = nDecode[ c & 0x7F ];
		if( nDigit < -1 ) 
		{
			//return 0;
			//printf();
		} 
		else if( nDigit >= 0 ) 
			// i (index into output) is incremented by write_bits()
			write_bits( nDigit & 0x3F, 6, szOutput, i );
		if(i>=obufLen)
			break;
    }	
	szOutput[i]=0;
	return i;

}


unsigned int CBase64::read_bits(int nNumBits, int * pBitsRead, int& lp)
{
    unsigned long lScratch;
    while( ( m_nBitsRemaining < nNumBits ) && 
		   ( lp < m_nInputSize ) ) 
	{
		int c = m_szInput[ lp++ ];
        m_lBitStorage <<= 8;
        m_lBitStorage |= (c & 0xff);
		m_nBitsRemaining += 8;
    }
    if( m_nBitsRemaining < nNumBits ) 
	{
		lScratch = m_lBitStorage << ( nNumBits - m_nBitsRemaining );
		*pBitsRead = m_nBitsRemaining;
		m_nBitsRemaining = 0;
    } 
	else 
	{
		lScratch = m_lBitStorage >> ( m_nBitsRemaining - nNumBits );
		*pBitsRead = nNumBits;
		m_nBitsRemaining -= nNumBits;
    }
    return (unsigned int)lScratch & m_nMask[nNumBits];
}


void CBase64::write_bits(unsigned int nBits,
						 int nNumBits,
						 char * szOutput,
						 int& i)
{
	unsigned int nScratch;

	m_lBitStorage = (m_lBitStorage << nNumBits) | nBits;
	m_nBitsRemaining += nNumBits;
	while( m_nBitsRemaining > 7 ) 
	{
		nScratch = m_lBitStorage >> (m_nBitsRemaining - 8);
		szOutput[ i++ ] = nScratch & 0xFF;
		m_nBitsRemaining -= 8;
	}
}

⌨️ 快捷键说明

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