2024-06-23 17:36:53 +08:00

124 lines
3.3 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

CString Base64Decode(LPCTSTR lpszSrc)
{
ASSERT(lpszSrc != NULL && AfxIsValidString(lpszSrc));
const unsigned int BASE64_DECODE_TABLE[256] = {
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 62, 255, 255, 255, 63,
52, 53, 54, 55, 56, 57, 58, 59,
60, 61, 255, 255, 255, 255, 255, 255,
255, 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, 255, 255, 255, 255, 255,
255, 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, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255 };
const int nSrcCount=(int)_tcslen(lpszSrc);
int nSize=nSrcCount/4*3;
if(lpszSrc[nSrcCount-1]=='=')
nSize--;
if(lpszSrc[nSrcCount-2]=='=')
nSize--;
char* pOutBuffer=new char[nSize+3];
ZeroMemory(pOutBuffer,nSize+3);
LPCTSTR pInBuffer=lpszSrc;
UINT iTest,iPack;
for(int i=0;i<nSize/3 ;i++)
{
for(int j=0;j<4;j++)
{
iTest = BASE64_DECODE_TABLE[*pInBuffer++]; // Read from InputBuffer.
//InPtr++;
if (iTest == 0xFF)
{
j--;
continue; //¶Áµ½255·Ç·¨×Ö·û
}
iPack = iPack << 6 ;
iPack = iPack | iTest ;
}
pOutBuffer[2] = iPack;
iPack = iPack >> 8;
pOutBuffer[1] = iPack;
iPack = iPack >> 8;
pOutBuffer[0] = iPack;
//×¼±¸Ð´Èëºó3λ
pOutBuffer+= 3; iPack = 0;
}
switch(nSize%3)
{
case 1:
iTest = BASE64_DECODE_TABLE[*pInBuffer++]; // Read from InputBuffer.
if (iTest != 0xFF)
{
iPack = iPack << 6 ;
iPack = iPack | iTest ;
}
iTest = BASE64_DECODE_TABLE[*pInBuffer++]; // Read from InputBuffer.
if (iTest != 0xFF)
{
iPack = iPack << 6 ;
iPack = iPack | iTest ;
}
iPack = iPack >> 4;
pOutBuffer[0] = iPack;
pOutBuffer++;
break;
case 2:
iTest = BASE64_DECODE_TABLE[*pInBuffer++]; // Read from InputBuffer.
if (iTest != 0xFF)
{
iPack = iPack << 6 ;
iPack = iPack | iTest ;
}
iTest = BASE64_DECODE_TABLE[*pInBuffer++]; // Read from InputBuffer.
if (iTest != 0xFF)
{
iPack = iPack << 6 ;
iPack = iPack | iTest ;
}
iTest = BASE64_DECODE_TABLE[*pInBuffer++]; // Read from InputBuffer.
if (iTest != 0xFF)
{
iPack = iPack << 6 ;
iPack = iPack | iTest ;
}
iPack = iPack >> 2;
pOutBuffer[1] = iPack;
iPack = iPack >> 8;
pOutBuffer[0] = iPack;
pOutBuffer+=2;
break;
default:
break;
}
pOutBuffer-=nSize;
CString strDecode=pOutBuffer;
delete pOutBuffer;
return strDecode;
}