高分求教编码问题!
文件编码:
int CSMTPAttachment::Base64BufferSize(int nInputSize)
{
int nOutSize = (nInputSize+2)/3*4; // 3:4 conversion ratio
nOutSize += strlen(EOL)*nOutSize/BASE64_MAXLINE + 3; // Space for newlines and NUL
return nOutSize;
}
BOOL CSMTPAttachment::EncodeBase64(const char* pszIn, int nInLen, char* pszOut, int nOutSize, int* nOutLen)
{
//Input Parameter validation
ASSERT(pszIn);
ASSERT(pszOut);
ASSERT(nOutSize);
ASSERT(nOutSize >= Base64BufferSize(nInLen));
#ifndef _DEBUG
//justs get rid of "unreferenced formal parameter"
//compiler warning when doing a release build
nOutSize;
#endif
//Set up the parameters prior to the main encoding loop
int nInPos = 0;
int nOutPos = 0;
int nLineLen = 0;
// Get three characters at a time from the input buffer and encode them
for (int i=0; i<nInLen/3; ++i)
{
//Get the next 2 characters
int c1 = pszIn[nInPos++] & 0xFF;
int c2 = pszIn[nInPos++] & 0xFF;
int c3 = pszIn[nInPos++] & 0xFF;
//Encode into the 4 6 bit characters
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;
//Handle the case where we have gone over the max line boundary
if (nLineLen >= BASE64_MAXLINE-3)
{
char* cp = EOL;
pszOut[nOutPos++] = *cp++;
if (*cp)
pszOut[nOutPos++] = *cp;
nLineLen = 0;
}
}
// Encode the remaining one or two characters in the input buffer
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;
}
这个是Module : SMTP.CPP
Purpose: Implementation for a MFC class encapsulation of the SMTP protocol
Created: PJN / 22-05-1998
不大理解哦
哪儿有算法啊!!!