//base64编码表
const char EnBase64Tab[] = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/”;
//base64解码表
const char DeBase64Tab[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62, // ‘+’
0, 0, 0,
63, // ‘/’
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // ‘0’-‘9’
//0, 1, 2, 3, 4, 5, 6, 7, 8, 9;
0, 0, 0, 0, 0, 0, 0,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
//A, B, C, D, E ,F, G ,H, I, J, K, L, M;
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, //’A’-‘Z’
//N, O, P, Q, R, S, T, U, V, W, X, Y, Z;
0, 0, 0, 0, 0, 0,
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
//a, b, c, d, e, f, g, h, i, j, k, l, m;
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, //’a’-‘z’
//n, o, p, q, r, s, t, u, v, w, x, y, z;
};
const char ASCIITab[] =
{
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, // ‘0’-‘9’
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9;
65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
// A, B, C, D, E , F, G , H, I, J, K, L, M;
78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, //’A’-‘Z’
// N, O, P, Q, R, S, T, U, V, W, X, Y, Z;
97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
//a, b, c, d, e, f, g, h, i, j, k, l, m;
110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122 //’a’-‘z’
//n, o, p, q, r, s, t, u, v, w, x, y, z;
};
//逻辑右移一位除以2去余, 逻辑左移一位乘以2;
//Base64解码
int DecodeBase64(const char* pSrc, int nSrcLen,string& strDst)
{
int nValue; // 解码用到的长整数
int i = 0;
int nDstLen = 0;
char chDst;
// 取4个字符,解码到一个长整数,再经过移位得到3个字节
while (i < nSrcLen)
{
//支持Base64RFC88标准,没76个字符添加换行
if(*pSrc==’\r’ || *pSrc==’\n’)
{
pSrc++;
i++;
continue;
}
nValue = DeBase64Tab[*pSrc++] << 18;
nValue += DeBase64Tab[*pSrc++] << 12;
chDst = (nValue & 0x00ff0000) >> 16;
nDstLen++;
strDst.append(&chDst,1);
if (*pSrc != ‘=’)
{
nValue += DeBase64Tab[*pSrc++] << 6;
chDst = (nValue & 0x0000ff00) >> 8;
nDstLen++;
strDst.append(&chDst,1);
if (*pSrc != ‘=’)
{
nValue += DeBase64Tab[*pSrc++];
chDst =nValue & 0x000000ff;
nDstLen++;
strDst.append(&chDst,1);
}
}
i += 4;
}
return nDstLen;
}
//Base64编码
void EncodeBase64(unsigned char* pSrc,int nSrcLen,std::string& result)
{
unsigned char c1, c2, c3; // 输入缓冲区读出3个字节
char pDst[4]={0}; // 输出缓冲区4个字节
int nDiv = nSrcLen / 3; // 输入数据长度除以3得到的倍数
int nMod = nSrcLen % 3; // 输入数据长度除以3得到的余数
//printf(“EncodeBase64=%d,nDiv=%d,nMod=%d\n”,nSrcLen,nDiv,nMod);
// 每次取3个字节,编码成4个字符
for (int i = 0; i < nDiv; i ++)
{
// 取3个字节
c1 = *pSrc++;
c2 = *pSrc++;
c3 = *pSrc++;
// 编码成4个字符
pDst[0] = EnBase64Tab[c1 >> 2];
pDst[1] = EnBase64Tab[((c1 << 4) | (c2 >> 4)) & 0x3f];
pDst[2] = EnBase64Tab[((c2 << 2) | (c3 >> 6)) & 0x3f];
pDst[3] = EnBase64Tab[c3 & 0x3f];
//printf(“c1=%d,c2=%d,c3=%d,pDst[0]=%d,pDst[1]=%d,pDst[2]=%d,pDst[3]=%d\n”,c1,c2,c3,pDst[0],pDst[1],pDst[2],pDst[3]);
result.append(pDst,4);
}
// 编码余下的字节
if (nMod == 1)
{
c1 = *pSrc++;
pDst[0] = EnBase64Tab[(c1 & 0xfc) >> 2];
pDst[1] = EnBase64Tab[((c1 & 0x03) << 4)];
pDst[2] = ‘=’;
pDst[3] = ‘=’;
result.append(pDst,4);
}
else if (nMod == 2)
{
c1 = *pSrc++;
c2 = *pSrc++;
pDst[0] = EnBase64Tab[(c1 & 0xfc) >> 2];
pDst[1] = EnBase64Tab[((c1 & 0x03) << 4) | ((c2 & 0xf0) >>4)];
pDst[2] = EnBase64Tab[((c2 & 0x0f) << 2)];
pDst[3] = ‘=’;
result.append(pDst,4);
}
}