BOOL CEDCoder::EncodeBase64(const char *pszIn, int nInLen, char *pszOut, int nOutSize, int *nOutLen)
{
ASSERT(pszIn);
ASSERT(pszOut);
ASSERT(nOutSize);
ASSERT(nOutSize >= Base64BufferSize(nInLen));
#ifndef _DEBUG
nOutSize;
#endif
//初始化编码过程中的循环变量
int nInPos = 0;
int nOutPos = 0;
int nLineLen = 0;
//一次从源串中取得三个字符并编码
for( int i=0 ; i<nInLen/3 ; i++)
{
//取得三个字符
int c1 = pszIn[nInPos++]&0xFF;
int c2 = pszIn[nInPos++]&0xFF;
int c3 = pszIn[nInPos++]&0xFF;
//将三个字符进行Base64编码
pszOut[nOutPos++] = m_base64tab[(c1&0xFC)>>2];
pszOut[nOutPos++] = m_base64tab[((c1&0x03)<<4)|((c2&0xF0)>>4)];
pszOut[nOutPos++] = m_base64tab[((c2&0x0F)<<2)|((c3&0xC0)>>6)];
pszOut[nOutPos++] = m_base64tab[c3&0x3F];
nLineLen += 4;
//处理行数越界的情况
if(nLineLen >= BASE64_MAXLINE-3)
{
char* cp = EOL;
pszOut[nOutPos++] = *cp++;
if(*cp)
pszOut[nOutPos++] = *cp;
nLineLen = 0;
}
}
//处理剩余的1或2个字符
char* cp;
switch(nInLen%3)
{
case 0:
{
cp = EOL;
pszOut[nOutPos++] = *cp++;
if(*cp)
pszOut[nOutPos] = *cp;
break;
}
case 1:
{
int c1 = pszIn[nInPos]&0xFF;
pszOut[nOutPos++] = m_base64tab[(c1&0xFC)>>2];
pszOut[nOutPos++] = m_base64tab[(c1&0x03)<<4];
pszOut[nOutPos++] = '=';
pszOut[nOutPos++] = '=';
cp = EOL;
pszOut[nOutPos++] = *cp++;
if(*cp)
pszOut[nOutPos++] = *cp;
break;
}
case 2:
{
int c1 = pszIn[nInPos++]&0xFF;
int c2 = pszIn[nInPos]&0xFF;
pszOut[nOutPos++] = m_base64tab[(c1&0xFC)>>2];
pszOut[nOutPos++] = m_base64tab[((c1&0x03)<<4)|((c2&0xF0)>>4)];
pszOut[nOutPos++] = m_base64tab[(c2&0x0F)<<2];
pszOut[nOutPos++] = '=';
cp = EOL;
pszOut[nOutPos++] = *cp++;
if(*cp)
pszOut[nOutPos++] = *cp;
break;
}
default:
{
//出错了!!
ASSERT(FALSE);
break;
}
}
pszOut[nOutPos] = 0;
*nOutLen = nOutPos;
return TRUE;
}
{
ASSERT(pszIn);
ASSERT(pszOut);
ASSERT(nOutSize);
ASSERT(nOutSize >= Base64BufferSize(nInLen));
#ifndef _DEBUG
nOutSize;
#endif
//初始化编码过程中的循环变量
int nInPos = 0;
int nOutPos = 0;
int nLineLen = 0;
//一次从源串中取得三个字符并编码
for( int i=0 ; i<nInLen/3 ; i++)
{
//取得三个字符
int c1 = pszIn[nInPos++]&0xFF;
int c2 = pszIn[nInPos++]&0xFF;
int c3 = pszIn[nInPos++]&0xFF;
//将三个字符进行Base64编码
pszOut[nOutPos++] = m_base64tab[(c1&0xFC)>>2];
pszOut[nOutPos++] = m_base64tab[((c1&0x03)<<4)|((c2&0xF0)>>4)];
pszOut[nOutPos++] = m_base64tab[((c2&0x0F)<<2)|((c3&0xC0)>>6)];
pszOut[nOutPos++] = m_base64tab[c3&0x3F];
nLineLen += 4;
//处理行数越界的情况
if(nLineLen >= BASE64_MAXLINE-3)
{
char* cp = EOL;
pszOut[nOutPos++] = *cp++;
if(*cp)
pszOut[nOutPos++] = *cp;
nLineLen = 0;
}
}
//处理剩余的1或2个字符
char* cp;
switch(nInLen%3)
{
case 0:
{
cp = EOL;
pszOut[nOutPos++] = *cp++;
if(*cp)
pszOut[nOutPos] = *cp;
break;
}
case 1:
{
int c1 = pszIn[nInPos]&0xFF;
pszOut[nOutPos++] = m_base64tab[(c1&0xFC)>>2];
pszOut[nOutPos++] = m_base64tab[(c1&0x03)<<4];
pszOut[nOutPos++] = '=';
pszOut[nOutPos++] = '=';
cp = EOL;
pszOut[nOutPos++] = *cp++;
if(*cp)
pszOut[nOutPos++] = *cp;
break;
}
case 2:
{
int c1 = pszIn[nInPos++]&0xFF;
int c2 = pszIn[nInPos]&0xFF;
pszOut[nOutPos++] = m_base64tab[(c1&0xFC)>>2];
pszOut[nOutPos++] = m_base64tab[((c1&0x03)<<4)|((c2&0xF0)>>4)];
pszOut[nOutPos++] = m_base64tab[(c2&0x0F)<<2];
pszOut[nOutPos++] = '=';
cp = EOL;
pszOut[nOutPos++] = *cp++;
if(*cp)
pszOut[nOutPos++] = *cp;
break;
}
default:
{
//出错了!!
ASSERT(FALSE);
break;
}
}
pszOut[nOutPos] = 0;
*nOutLen = nOutPos;
return TRUE;
}