///
CHAR base64Code[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
, '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '='}; // ori : '+' '/' '='
BOOL Base64_EncodeA(LPBYTE pBuffer, int nBufferBytes, LPSTR lpOut, int *pnOutSize)
{
if(pBuffer == NULL || nBufferBytes <= 0 || (lpOut == NULL && pnOutSize == NULL))
{
return FALSE;
}
int nNeededSize = ( (nBufferBytes / 3) + ((nBufferBytes % 3) ? 1 : 0) ) * 4 * sizeof(CHAR);
nNeededSize++;
if(lpOut == NULL)
{
*pnOutSize = nNeededSize;
return TRUE;
}
if(pnOutSize != NULL)
{
if(nNeededSize > *pnOutSize)
{
*pnOutSize = nNeededSize;
return FALSE;
}
*pnOutSize = nNeededSize;
}
int i = 0, j = 0;
BYTE btArray_3[3], btArray_4[4];
while (nBufferBytes--)
{
btArray_3[i++] = (*pBuffer++);
if(i == 3)
{
btArray_4[0] = (btArray_3[0] & 0xFC) >> 2;
btArray_4[1] = ((btArray_3[0] & 0x03) << 4) + ((btArray_3[1] & 0xF0) >> 4);
btArray_4[2] = ((btArray_3[1] & 0x0F) << 2) + ((btArray_3[2] & 0xC0) >> 6);
btArray_4[3] = btArray_3[2] & 0x3F;
for(i=0; i<4; i++)
{
*lpOut++ = base64Code[btArray_4[i]];
}
i = 0;
}
}
if(i != 0)
{
for(j=i; j<3; j++)
{
btArray_3[j] = 0;
}
btArray_4[0] = (btArray_3[0] & 0xFC) >> 2;
btArray_4[1] = ((btArray_3[0] & 0x03) << 4) + ((btArray_3[1] & 0xF0) >> 4);
btArray_4[2] = ((btArray_3[1] & 0x0F) << 2) + ((btArray_3[2] & 0xC0) >> 6);
btArray_4[3] = btArray_3[2] & 0x3F;
for(j=0; j<i+1; j++)
{
*lpOut++ = base64Code[btArray_4[j]];
}
while(i++ < 3)
{
*lpOut++ = '=';
}
*lpOut = '/0';
}
return TRUE;
}
BOOL Is_Base64A(CHAR c)
{
return strchr(base64Code, c) != NULL;
}
int IndexOfCharA(CHAR c)
{
LPCSTR lp = strchr(base64Code, c);
if(lp == NULL)
{
return 0;
}
return lp - base64Code;
}
BOOL Base64_DecodeA(LPCSTR lpEncoded, LPBYTE pBuffer, int *pnBufferBytes)
{
if(lpEncoded == NULL || strlen(lpEncoded) <= 0)
{
return FALSE;
}
int nNeededBytes = (strlen(lpEncoded) / 4) * 3;
LPBYTE pBufferSave = pBuffer;
int i = 0, j = 0, in_ = 0;
BYTE btArray_4[4], btArray_3[3];
while(lpEncoded[in_] != '/0' && lpEncoded[in_] != '=' && Is_Base64A(lpEncoded[in_]))
{
btArray_4[i++] = lpEncoded[in_];
in_++;
if(i == 4)
{
for(i=0; i<4; i++)
{
btArray_4[i] = IndexOfCharA(btArray_4[i]);
}
btArray_3[0] = (btArray_4[0] << 2) + ((btArray_4[1] & 0x30) >> 4);
btArray_3[1] = ((btArray_4[1] & 0x0f) << 4) + ((btArray_4[2] & 0x3C) >> 2);
btArray_3[2] = ((btArray_4[2] & 0x03) << 6) + btArray_4[3];
for(i=0; i<3; i++)
{
*pBuffer++ = btArray_3[i];
}
i = 0;
}
}
if(i != 0)
{
for(j=i; j<4; j++)
btArray_4[j] = 0;
for(j=0; j<4; j++)
{
btArray_4[j] = IndexOfCharA(btArray_4[j]);
}
btArray_3[0] = (btArray_4[0] << 2) + ((btArray_4[1] & 0x30) >> 4);
btArray_3[1] = ((btArray_4[1] & 0x0f) << 4) + ((btArray_4[2] & 0x3C) >> 2);
btArray_3[2] = ((btArray_4[2] & 0x03) << 6) + btArray_4[3];
for(j=0; j<i-1; j++)
{
*pBuffer++ = btArray_3[j];
}
}
if(pnBufferBytes != NULL)
{
*pnBufferBytes = pBuffer - pBufferSave;
}
return TRUE;
}