与Web交互可用的图片Base64编码

时间:2025-01-15 10:03:20
#ifndef ___BASE64_H___
#define ___BASE64_H___ #include <string> using namespace std; class CBase64
{
public:
CBase64();
~CBase64(); /*********************************************************
* 函数说明:将输入数据进行base64编码
* 参数说明:[in]pIn 需要进行编码的数据
[in]uInLen 输入参数的字节数
[out]strOut 输出的进行base64编码之后的字符串
* 返回值 :true处理成功,false失败
**********************************************************/
bool static Encode(const unsigned char *pIn, unsigned long uInLen, string& strOut); /*********************************************************
* 函数说明:将输入数据进行base64编码
* 参数说明:[in]pIn 需要进行编码的数据
[in]uInLen 输入参数的字节数
[out]pOut 输出的进行base64编码之后的字符串
[out]uOutLen 输出的进行base64编码之后的字符串长度
* 返回值 :true处理成功,false失败
**********************************************************/
bool static Encode(const unsigned char *pIn, unsigned long uInLen, unsigned char *pOut, unsigned long *uOutLen); /*********************************************************
* 函数说明:将输入数据进行base64解码
* 参数说明:[in]strIn 需要进行解码的数据
[out]pOut 输出解码之后的节数数据
[out]uOutLen 输出的解码之后的字节数长度
* 返回值 :true处理成功,false失败
**********************************************************/
bool static Decode(const string& strIn, unsigned char *pOut, unsigned long *uOutLen) ; /*********************************************************
* 函数说明:将输入数据进行base64解码
* 参数说明:[in]strIn 需要进行解码的数据
[out]pOut 输出解码之后的节数数据
[out]uOutLen 输出的解码之后的字节数长度
* 返回值 :true处理成功,false失败
**********************************************************/
bool static Decode(const unsigned char *pIn, unsigned long uInLen, unsigned char *pOut, unsigned long *uOutLen) ;
}; #endif // ___BASE64_H___
#include "stdafx.h"

/*
*
*Base64?
*
*
*
*/ #include "Base64.h" static const char *g_pCodes =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; static const unsigned char g_pMap[] =
{
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , ,
}; CBase64::CBase64()
{
} CBase64::~CBase64()
{
} bool CBase64::Encode(const unsigned char *pIn, unsigned long uInLen, unsigned char *pOut, unsigned long *uOutLen)
{
unsigned long i, len2, leven;
unsigned char *p; if(pOut == NULL || *uOutLen == )
return false; //ASSERT((pIn != NULL) && (uInLen != 0) && (pOut != NULL) && (uOutLen != NULL)); len2 = ((uInLen + ) / ) << ;
if((*uOutLen) < (len2 + )) return false; p = pOut;
leven = * (uInLen / );
for(i = ; i < leven; i += )
{
*p++ = g_pCodes[pIn[] >> ];
*p++ = g_pCodes[((pIn[] & ) << ) + (pIn[] >> )];
*p++ = g_pCodes[((pIn[] & 0xf) << ) + (pIn[] >> )];
*p++ = g_pCodes[pIn[] & 0x3f];
pIn += ;
} if (i < uInLen)
{
unsigned char a = pIn[];
unsigned char b = ((i + ) < uInLen) ? pIn[] : ;
unsigned char c = ; *p++ = g_pCodes[a >> ];
*p++ = g_pCodes[((a & ) << ) + (b >> )];
*p++ = ((i + ) < uInLen) ? g_pCodes[((b & 0xf) << ) + (c >> )] : '=';
*p++ = '=';
} *p = ; // Append NULL byte
*uOutLen = p - pOut;
return true;
} bool CBase64::Encode(const unsigned char *pIn, unsigned long uInLen, string& strOut)
{
unsigned long i, len2, leven;
strOut = ""; //ASSERT((pIn != NULL) && (uInLen != 0) && (pOut != NULL) && (uOutLen != NULL)); len2 = ((uInLen + ) / ) << ;
//if((*uOutLen) < (len2 + 1)) return false; //p = pOut;
leven = * (uInLen / );
for(i = ; i < leven; i += )
{
strOut += g_pCodes[pIn[] >> ];
strOut += g_pCodes[((pIn[] & ) << ) + (pIn[] >> )];
strOut += g_pCodes[((pIn[] & 0xf) << ) + (pIn[] >> )];
strOut += g_pCodes[pIn[] & 0x3f];
pIn += ;
} if (i < uInLen)
{
unsigned char a = pIn[];
unsigned char b = ((i + ) < uInLen) ? pIn[] : ;
unsigned char c = ; strOut += g_pCodes[a >> ];
strOut += g_pCodes[((a & ) << ) + (b >> )];
strOut += ((i + ) < uInLen) ? g_pCodes[((b & 0xf) << ) + (c >> )] : '=';
strOut += '=';
} //*p = 0; // Append NULL byte
//*uOutLen = p - pOut;
return true;
} bool CBase64::Decode(const string& strIn, unsigned char *pOut, unsigned long *uOutLen)
{
unsigned long t, x, y, z;
unsigned char c;
unsigned long g = ; //ASSERT((pIn != NULL) && (uInLen != 0) && (pOut != NULL) && (uOutLen != NULL)); for(x = y = z = t = ; x < strIn.length(); x++)
{
c = g_pMap[strIn[x]];
if(c == ) continue;
if(c == ) { c = ; g--; } t = (t << ) | c; if(++y == )
{
if((z + g) > *uOutLen) { return false; } // Buffer overflow
pOut[z++] = (unsigned char)((t>>)&);
if(g > ) pOut[z++] = (unsigned char)((t>>)&);
if(g > ) pOut[z++] = (unsigned char)(t&);
y = t = ;
}
} *uOutLen = z;
return true;
}