📄 pjnsmtp.cpp
字号:
return TRUE;
}
void CPJNSMTPBodyPart::SetText(const CString& sText)
{
m_sText = sText;
//Ensure lines are correctly wrapped
m_sText.Replace(_T("\r\n"), _T("\n"));
m_sText.Replace(_T("\r"), _T("\n"));
m_sText.Replace(_T("\n"), _T("\r\n"));
//Also set the content type while we are at it
m_sContentType = _T("text/plain");
}
void CPJNSMTPBodyPart::SetContentID(const CString& sContentID)
{
m_sContentLocation.Empty();
m_sContentID = sContentID;
}
CString CPJNSMTPBodyPart::GetContentID() const
{
return m_sContentID;
}
void CPJNSMTPBodyPart::SetContentLocation(const CString& sContentLocation)
{
m_sContentID.Empty();
m_sContentLocation = sContentLocation;
}
CString CPJNSMTPBodyPart::GetContentLocation() const
{
return m_sContentLocation;
}
char CPJNSMTPBodyPart::HexDigit(int nDigit)
{
if (nDigit < 10)
return static_cast<char>(nDigit + '0');
else
return static_cast<char>(nDigit - 10 + 'A');
}
//Converts text to its Quoted printable equivalent according to RFC 2045
std::string CPJNSMTPBodyPart::QuotedPrintableEncode(const std::string& sText)
{
//get the pointer to the internal buffer, its ASCII buffer
LPCSTR pszAsciiText = sText.c_str();
std::string sTemp;
size_t nSize = strlen(pszAsciiText);
size_t i;
for (i=0; i<nSize; i++)
{
//Pull out the character to operate on
BYTE c = pszAsciiText[i];
if (((c >= 33) && (c <= 60)) || ((c >= 62) && (c <= 126)) || (c == '\r') || (c == '\n') || (c == '\t') || (c == ' '))
sTemp += c;
else
{
//otherwise must quote the text
sTemp += '=';
sTemp += HexDigit((c & 0xF0) >> 4);
sTemp += HexDigit(c & 0x0F);
}
}
//Now insert soft line breaks where appropiate
std::string sOut;
size_t nStartLine = 0;
pszAsciiText = sTemp.c_str();
size_t nLen = strlen(pszAsciiText);
for (i=0; i<nLen; i++)
{
//Pull out the character to operate on
BYTE c = pszAsciiText[i];
if (c == '\n' || c == '\r' || i == (nLen-1))
{
sOut += sTemp.substr(nStartLine, i-nStartLine+1);
nStartLine = i+1;
continue;
}
if ((i - nStartLine) > PJNSMTP_MAXLINE)
{
BOOL bInWord = TRUE;
while (bInWord)
{
if (i>1)
bInWord = (!isspace(c) && sTemp[i-2] != _T('='));
if (bInWord)
{
--i;
c = static_cast<BYTE>(sTemp[i]);
}
if (i == nStartLine)
{
i = nStartLine + PJNSMTP_MAXLINE;
break;
}
}
sOut += sTemp.substr(nStartLine, i-nStartLine+1);
sOut += "=\r\n";
nStartLine = i+1;
}
}
return sOut;
}
int CPJNSMTPBodyPart::ConvertToUTF8(const CString& in, std::string &out)
{
USES_CONVERSION;
LPCWSTR psuBuff = T2CW(in);
int nULength = static_cast<int>(wcslen(psuBuff));
LPSTR pUtf8 = NULL;
//convert the data to utf-8
int iLength = UnicodeToUTF8(psuBuff, nULength, pUtf8, 0);
ASSERT(iLength);
pUtf8 = new char[iLength+1];
iLength = UnicodeToUTF8(psuBuff, nULength, pUtf8, iLength);
ASSERT(iLength);
pUtf8[iLength] = '\0';
out = pUtf8;
delete [] pUtf8;
return iLength;
}
//Copy of the function AtlUnicodeToUTF8 in ATL Server. This allows the
//SMTP class to operate on OS'es which do not support conversion to UTF-8 via
//WideCharToMultiByte
int CPJNSMTPBodyPart::UnicodeToUTF8(LPCWSTR wszSrc, int nSrc, LPSTR szDest, int nDest)
{
LPCWSTR pwszSrc = wszSrc;
int nU8 = 0; // # of UTF8 chars generated
DWORD dwSurrogateChar;
WCHAR wchHighSurrogate = 0;
BOOL bHandled;
while ((nSrc--) && ((nDest == 0) || (nU8 < nDest)))
{
bHandled = FALSE;
//Check if high surrogate is available
if ((*pwszSrc >= 0xd800) && (*pwszSrc <= 0xdbff))
{
if (nDest)
{
//Another high surrogate, then treat the 1st as normal Unicode character.
if (wchHighSurrogate)
{
if ((nU8 + 2) < nDest)
{
szDest[nU8++] = static_cast<char>(0xe0 | (wchHighSurrogate >> 12));
szDest[nU8++] = static_cast<char>(0x80 | (((wchHighSurrogate) & 0x0fc0) >> 6));
szDest[nU8++] = static_cast<char>(0x80 | (wchHighSurrogate & 0x003f));
}
else
{
//not enough buffer
nSrc++;
break;
}
}
}
else
{
nU8 += 3;
}
wchHighSurrogate = *pwszSrc;
bHandled = TRUE;
}
if (!bHandled && wchHighSurrogate)
{
if ((*pwszSrc >= 0xdc00) && (*pwszSrc <= 0xdfff))
{
//valid surrogate pairs
if (nDest)
{
if ((nU8 + 3) < nDest)
{
dwSurrogateChar = (((wchHighSurrogate-0xD800) << 10) + (*pwszSrc - 0xDC00) + 0x10000);
szDest[nU8++] = static_cast<unsigned char>(0xf0 | static_cast<unsigned char>(dwSurrogateChar >> 18)); // 3 bits from 1st byte
szDest[nU8++] = static_cast<unsigned char>(0x80 | static_cast<unsigned char>((dwSurrogateChar >> 12) & 0x3f)); // 6 bits from 2nd byte
szDest[nU8++] = static_cast<unsigned char>(0x80 | static_cast<unsigned char>((dwSurrogateChar >> 6) & 0x3f)); // 6 bits from 3rd byte
szDest[nU8++] = static_cast<unsigned char>(0x80 | static_cast<unsigned char>(0x3f & dwSurrogateChar)); // 6 bits from 4th byte
}
else
{
//not enough buffer
nSrc++;
break;
}
}
else
{
//we already counted 3 previously (in high surrogate)
nU8 += 1;
}
bHandled = TRUE;
}
else
{
//Bad Surrogate pair : ERROR
//Just process wchHighSurrogate , and the code below will
//process the current code point
if (nDest)
{
if ((nU8 + 2) < nDest)
{
szDest[nU8++] = static_cast<char>(0xe0 | (wchHighSurrogate >> 12));
szDest[nU8++] = static_cast<char>(0x80 | (((wchHighSurrogate) & 0x0fc0) >> 6));
szDest[nU8++] = static_cast<char>(0x80 | (wchHighSurrogate & 0x003f));
}
else
{
//not enough buffer
nSrc++;
break;
}
}
}
wchHighSurrogate = 0;
}
if (!bHandled)
{
if (*pwszSrc <= 0x007f)
{
//Found ASCII.
if (nDest)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -