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

📄 base641.cpp

📁 此程序是Base64算法,其实现是完全用了char* 的数据类型,封装的比较干净,好用
💻 CPP
字号:
// Base641.cpp: implementation of the CBase64 class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Base641.h"
#include <windows.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CBase64::CBase64()
{

}

CBase64::~CBase64()
{

}

int CBase64::Encode( char *Inbuf, char *Outbuf, int nInLen, int nOutLen )
{
	char table[65]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

	unsigned char c80,c81,c82,c6;
	int i,k,len;

	if( ( nInLen * 4 ) > ( nOutLen * 3 + 3 ) )
		return 1;

	k = nInLen / 3;

	len = k * 4;       //real outpur string length

	for( i=0; i<k; i++ )
	{
		c80 = Inbuf[i*3];
		c6 = c80 >> 2;
		Outbuf[i*4] = table[c6 & 0x3f];

		c80 = (c80<<4) & 0x30;
		c81 = Inbuf[i*3+1];
		c6  = (c81>>4) & 0x0f;
		Outbuf[i*4+1] = table[c80 | c6];

		c81 = (c81<<2) & 0x3c;
		c82 = Inbuf[i*3+2];
		c6  = (c82>>6) & 0x03;
		Outbuf[i*4+2] = table[c81 | c6];

		Outbuf[i*4+3] = table[c82 & 0x3f];

	}

	i = nInLen % 3;
	k = nInLen / 3;
	if( i )//some more.
	{
		len += 4;

		c80 = Inbuf[k*3];
		c6 = c80 >> 2;
		Outbuf[k*4] = table[c6 & 0x3f];

		if( k == 1 )
		{
			c80 = (c80<<4) & 0x30;
			Outbuf[k*4+1] = table[c80];
			
			Outbuf[k*4+2] = '=';			
		}
		else
		{
			c80 = (c80<<4) & 0x30;
			c81 = Inbuf[k*3+1];
			c6  = (c81>>4) & 0x0f;
			Outbuf[k*4+1] = table[c80 | c6];

			c81 = (c81<<2) & 0x3c;
			Outbuf[k*4+2] = table[c81];			
		}

		Outbuf[k*4+3] = '=';
	}//end of some more.

	Outbuf[len] = '\0';

	return len;
}

int CBase64::Decode( char *Inbuf, char *Outbuf, int nInLen, int nOutLen )
{
    int nbytesdecoded;
    char *pBufin;
    unsigned char *pszBufout;
    unsigned char szBufout[256];
    char szBufTmp[350];
    int nprbytes;
	DWORD dwCoded = nInLen;
	char *pszCoded = Inbuf;
	char *pszDecoded = Outbuf;

	const int pr2six[256]=
	{
		64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
		64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,62,64,64,64,63,
		52,53,54,55,56,57,58,59,60,61,64,64,64,64,64,64,64,0,1,2,3,4,5,6,7,8,9,
		10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,64,64,64,64,64,64,26,27,
		28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,
		64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
		64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
		64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
		64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
		64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
		64,64,64,64,64,64,64,64,64,64,64,64,64
	};

    //
    // Remove leading whitespace.
    //
    while( ( dwCoded > 0 ) && ( isspace( *pszCoded ) ) )
    {
        pszCoded++; dwCoded--;
    }

    if ( dwCoded > ( 350 - 4 ) )
        return 1;

    strncpy( szBufTmp, pszCoded, dwCoded );
    szBufTmp[dwCoded] = szBufTmp[dwCoded+1] = szBufTmp[dwCoded+2]
                      = szBufTmp[dwCoded+3] = 0;

    //
    // Determine how many characters are in the input buffer.
    // If this would decode into more bytes than would fit into
    // the output buffer, return 2
    //

    pBufin = szBufTmp;
    while( pr2six[ *( pBufin++ ) ] <= 63 );
    nprbytes = pBufin - szBufTmp - 1;
    nbytesdecoded = ( ( nprbytes + 3 ) / 4 ) * 3;

	if( nOutLen < nbytesdecoded )
		return 2;

	if ( nbytesdecoded > ( 256 - 4 ) )
        return 2;

    pszBufout = (unsigned char *) szBufout;

    pBufin = szBufTmp;

    while ( nprbytes > 0 ) 
	{
        *(pszBufout++) =
            (unsigned char) (pr2six[*pBufin] << 2 | pr2six[pBufin[1]] >> 4);
        *(pszBufout++) =
            (unsigned char) (pr2six[pBufin[1]] << 4 | pr2six[pBufin[2]] >> 2);
        *(pszBufout++) =
            (unsigned char) (pr2six[pBufin[2]] << 6 | pr2six[pBufin[3]]);
        pBufin += 4;
        nprbytes -= 4;
    }

    if( nprbytes & 03 ) 
	{
        if ( pr2six[ pBufin[-2] ] > 63 )
            nbytesdecoded -= 2;
        else
            nbytesdecoded -= 1;
    }

    szBufout[ nbytesdecoded ] = '\0';
	
    strcpy( pszDecoded, (char*)szBufout );

    return nbytesdecoded;
}

⌨️ 快捷键说明

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