#if WS_PLATFORM == WS_PLATFORM_WIN32
std::wstring str2wstr( const std::string &strSrc, UINT CodePage/*=CP_ACP CP_UTF8*/ )
{
if(strSrc.length() == 0)
return L"";
int buflen=MultiByteToWideChar( CodePage, 0, strSrc.c_str(), -1, NULL, 0 ) +1 ;
if(buflen == 0)
return L"";
wchar_t * buf = new wchar_t[buflen];
std::wstring retn = L"";
if (buf)
{
memset(buf, 0, buflen * sizeof(wchar_t));
::MultiByteToWideChar( CodePage, 0, strSrc.c_str(), -1, buf, buflen );
retn = buf;
delete []buf;
}
return retn;
}
#elif WS_PLATFORM == WS_PLATFORM_LINUX
std::wstring str2wstr( const std::string &strSrc, UINT CodePage)
{
UNUSED(CodePage);
if(strSrc.length() == 0)
return L"";
std::string strLocale = setlocale(LC_ALL, "");
int buflen=mbstowcs(NULL, strSrc.c_str(), 0) + 1;
if(buflen == 0)
return L"";
wchar_t * buf = new wchar_t[buflen];
std::wstring retn = L"";
if (buf)
{
memset(buf,0, buflen * sizeof(wchar_t));
mbstowcs(buf, strSrc.c_str(), strSrc.length());
retn = buf;
delete []buf;
}
setlocale(LC_ALL, strLocale.c_str());
return retn;
}
#endif
#if WS_PLATFORM == WS_PLATFORM_WIN32
std::string wstr2str( const std::wstring &wstrSrc, UINT CodePage/*=CP_ACP CP_UTF8*/)
{
if(wstrSrc.length() == 0)
return "";
//得到转化后需要Buf的长度
std::string retn = "";
try
{
int buflen = ::WideCharToMultiByte( CodePage, 0, wstrSrc.c_str(), -1, NULL, 0, NULL, NULL ) + 1;
if(buflen == 0)
return "";
char * buf = new char[buflen];
if(buf != NULL)
{
memset(buf,0, buflen );
::WideCharToMultiByte( CodePage, 0, wstrSrc.c_str(), -1, buf, buflen, NULL, NULL );
retn = buf;
delete []buf;
}
}
catch (...)
{
}
return retn;
}
#elif WS_PLATFORM == WS_PLATFORM_LINUX
std::string wstr2str( const std::wstring &wstrSrc, UINT CodePage)
{
UNUSED(CodePage);
if(wstrSrc.length() == 0)
return "";
//得到转化后需要Buf的长度
std::string retn = "";
try
{
std::string strLocale = setlocale(LC_ALL, "");
int buflen = wcstombs(NULL, wstrSrc.c_str(), 0) + 1;
if(buflen == 0)
return "";
char * buf = new char[buflen];
if(buf != NULL)
{
memset(buf,0, buflen );
wcstombs(buf, wstrSrc.c_str(), buflen);
retn = buf;
delete []buf;
}
setlocale(LC_ALL, strLocale.c_str());
}
catch (...)
{
}
return retn;
}
#endif
std::string Utf8ToAsi(std::string strUtf8)
{
#if WS_PLATFORM == WS_PLATFORM_WIN32
CWSByteArray baData;
baData.Append(strUtf8.c_str());
DWORD UniCodeLen = ::MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), -1, NULL, 0);
std::vector <wchar_t> vWCH(UniCodeLen);
::MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), -1, &vWCH[0], UniCodeLen);
DWORD dwASCIILen = ::WideCharToMultiByte(CP_ACP, 0, &vWCH[0], UniCodeLen, NULL, NULL, NULL, NULL);
if(!dwASCIILen)
return "";
char* pASCIIBuf = new char[dwASCIILen + 1];
if(NULL == pASCIIBuf)
{
return "";
}
memset(pASCIIBuf, 0, dwASCIILen + 1);
::WideCharToMultiByte(CP_ACP, 0, &vWCH[0], UniCodeLen, pASCIIBuf, dwASCIILen, NULL, NULL);
std::string szRetAsiic = pASCIIBuf;
delete[] pASCIIBuf;
return szRetAsiic;
#endif
return strUtf8;
}
string AsiToUtf8(string strAsi)
{
#if WS_PLATFORM == WS_PLATFORM_WIN32
DWORD UniCodeLen = ::MultiByteToWideChar(CP_ACP, 0, strAsi.c_str(), -1, NULL, 0);
std::vector <wchar_t> vWCH(UniCodeLen);
::MultiByteToWideChar(CP_ACP, 0, strAsi.c_str(), -1, &vWCH[0], UniCodeLen);
DWORD dwUTF8Len = ::WideCharToMultiByte(CP_UTF8, 0, &vWCH[0], UniCodeLen, NULL, NULL, NULL, NULL);
if(!dwUTF8Len)
return "";
char* pUTF8Buf = new char[dwUTF8Len + 1];
if(NULL == pUTF8Buf)
{
return "";
}
memset(pUTF8Buf, 0, dwUTF8Len + 1);
::WideCharToMultiByte(CP_UTF8, 0, &vWCH[0], UniCodeLen, pUTF8Buf, dwUTF8Len, NULL, NULL);
std::string szRetUTF8 = pUTF8Buf;
delete[] pUTF8Buf;
return szRetUTF8;
#endif
return strAsi;
}
std::string stringformatA(const char* fmt, ...)
{
std::string s="";
try
{
va_list argptr;
#ifdef _WIN32
#pragma warning( push )
#pragma warning( disable : 4996 )
#endif
va_start(argptr, fmt);
int bufsize = _vsnprintf(NULL, 0, fmt, argptr) + 1;
va_end(argptr);
char* buf=new char[bufsize];
memset(buf, 0, bufsize);
va_start(argptr, fmt);
_vsnprintf(buf, bufsize, fmt, argptr);
va_end(argptr);
#ifdef _WIN32
#pragma warning( pop )
#endif
s=buf;
delete[] buf;
}
catch(...)
{
s="TryError!";
}
return s;
}
std::wstring stringformatW(const wchar_t* fmt, ...)
{
std::wstring s = L"";
try
{
va_list argptr;
#ifdef _WIN32
#pragma warning( push )
#pragma warning( disable : 4996 )
#endif
va_start(argptr, fmt);
int bufsize = _vsnwprintf(NULL, 0, fmt, argptr) + 2;
va_end(argptr);
wchar_t* buf = new wchar_t[bufsize];
memset(buf, 0, bufsize);
va_start(argptr, fmt);
_vsnwprintf(buf, bufsize,fmt, argptr);
va_end(argptr);
#ifdef _WIN32
#pragma warning( pop )
#endif
s = buf;
delete[] buf;
}
catch (...)
{
s = L"TryError!";
}
return s;
}
std::string GetStringBetween(const char* cContent, const char* cStart, const char* cEnd)
{
std::string strRe = "";
char* cStartNow = strstr((char*)cContent,cStart);
if (NULL != cStartNow)
{
cStartNow += strlen(cStart);
strRe = cStartNow;
char* iEnd = strstr(cStartNow,cEnd);
if (NULL != iEnd)
{
strRe.erase(iEnd-cStartNow);
}
}
return strRe;
}
//获取cStart cEnd之间的字符串,限定范围是:cContent+cStartNow iEnd之间
std::string GetStringBetweenEx(const char* cContent, char* cStart, char* cEnd,int cStartNowPos,int iEndPos)
{
std::string strRe = "";
char* cStartNow = strstr((char*)(cContent+cStartNowPos),cStart);
int cStartNowNow = 0;
if ((NULL != cStartNow)&&((cContent+iEndPos)>cStartNow))
{
cStartNowNow = (int)(cStartNow - cContent);
strRe.append(cStartNow,iEndPos-cStartNowNow);///<只写入部分数据
char* iEnd = strstr(cStartNow,cEnd);
if (NULL != iEnd)
{
strRe.erase(iEnd-cStartNow);
}
strRe.erase(0,strlen(cStart));
}
return strRe;
}
char Char2Int(char ch){
if(ch>='0' && ch<='9')return (char)(ch-'0');
if(ch>='a' && ch<='f')return (char)(ch-'a'+10);
if(ch>='A' && ch<='F')return (char)(ch-'A'+10);
return -1;
}
char Str2Bin(char *str)
{
char tempWord[2];
char chn;
tempWord[0] = Char2Int(str[0]);///
tempWord[1] = Char2Int(str[1]); ///
chn = (tempWord[0] << 4) | tempWord[1];///
return chn;
}
size_t StrToBin(const string &strStr, CWSByteArray &arrBin)
{
size_t iLen = 0;
unsigned int nSingle = 0;
BYTE bSingle = 0;
char cSingle[4];
arrBin.RemoveAll();
for(iLen = 0; iLen<(size_t)strStr.length(); iLen+=2)
{
memset(cSingle, 0, sizeof(cSingle));
strncpy_s(cSingle, sizeof(cSingle),strStr.c_str() + iLen, 2);
sscanf_s(cSingle, "%02X", &nSingle);
bSingle = (BYTE)nSingle;
arrBin.Add(bSingle);
}
return arrBin.GetCount();
}
size_t BinToStr(CWSByteArray &arrBin, string &strStr)
{
return BinToStr(arrBin.GetData(), arrBin.GetCount(), strStr);
}
size_t BinToStr(const BYTE* pBuffer, size_t nSize, string &strStr)
{
size_t iLen = 0;
BYTE bSingle = 0;
char cSingle[3];
strStr = "";
for(iLen = 0; iLen<nSize; iLen++)
{
bSingle = *(pBuffer+iLen);
memset(cSingle, 0, sizeof(cSingle));
sprintf_s(cSingle,sizeof(cSingle), "%02X", bSingle);
strStr += cSingle;
}
return strStr.length();
}
std::string trimLeft(const std::string& str)
{
std::string strRet = str;
for (std::string::iterator iter = strRet.begin(); iter != strRet.end(); ++iter)
{
if (!isspace(*iter))
{
strRet.erase(strRet.begin(), iter);
break;
}
}
return strRet;
}
std::string trimRight(const std::string& str)
{
if (str.begin() == str.end())
{
return str;
}
std::string strRet = str;
for (std::string::iterator iter = strRet.end() - 1; iter != strRet.begin(); --iter)
{
if (!isspace(*iter))
{
strRet.erase(iter + 1, strRet.end());
break;
}
}
return strRet;
}
//string 的trim 函数 ,干掉头尾空格
std::string Trim(const std::string& str){
if(str.empty())
return "";
std::string strRet = str;
std::string::iterator iter;
for (iter = strRet.begin(); iter != strRet.end(); ++iter)
{
if (!isspace(*iter))
{
strRet.erase(strRet.begin(), iter);
break;
}
}
if (iter == strRet.end())
{
return strRet;
}
for (iter = strRet.end() - 1; iter != strRet.begin(); ++iter)
{
if (!isspace(*iter)) {
strRet.erase(iter + 1, strRet.end());
break;
}
}
return strRet;
}
char GetBase64Value(char ch)
{
if ((ch >= 'A') && (ch <= 'Z'))
return ch - 'A';
if ((ch >= 'a') && (ch <= 'z'))
return ch - 'a' + 26;
if ((ch >= '0') && (ch <= '9'))
return ch - '0' + 52;
switch (ch)
{
case '+':
return 62;
case '/':
return 63;
case '=': /* base64 padding */
return 0;
default:
return 0;
}
}
int Base64Dec(char *buf,char*text,int size)
{
if(size%4) return 0;
unsigned char chunk[4];
int parsenum=0;
while(size>0)
{
chunk[0] = GetBase64Value(text[0]);
chunk[1] = GetBase64Value(text[1]);
chunk[2] = GetBase64Value(text[2]);
chunk[3] = GetBase64Value(text[3]);
*buf++ = (chunk[0] << 2) | (chunk[1] >> 4);
*buf++ = (chunk[1] << 4) | (chunk[2] >> 2);
*buf++ = (chunk[2] << 6) | (chunk[3]);
text+=4;
size-=4;
parsenum+=3;
}
return parsenum;
}
int Base64Decode(const std::string& strB64, char* pOutput,int iBufferSize)
{
int iB64Len=(int)strB64.size();
//检查是否是4的整数倍
if(iB64Len == 0 || iB64Len%4) return 0;
//检查输出缓冲区是不是正确
int iBINLen=iB64Len*3/4;
if (strB64[strB64.size()-1]=='=')
{
iBINLen--;
}
if (strB64[strB64.size()-2]=='=')
{
iBINLen--;
}
if (iBufferSize<iBINLen) return iBINLen;
if(NULL==pOutput) return 0;
unsigned char chunk[4];
int parsenum=0;
int iNowPos=0;
while(iNowPos<iB64Len)
{
chunk[0] = GetBase64Value(strB64[iNowPos]);
chunk[1] = GetBase64Value(strB64[iNowPos+1]);
chunk[2] = GetBase64Value(strB64[iNowPos+2]);
chunk[3] = GetBase64Value(strB64[iNowPos+3]);
*pOutput++ = (chunk[0] << 2) | (chunk[1] >> 4);
*pOutput++ = (chunk[1] << 4) | (chunk[2] >> 2);
*pOutput++ = (chunk[2] << 6) | (chunk[3]);
iNowPos+=4;
parsenum+=3;
}
if (iNowPos>0 && strB64[iNowPos-4+3]=='=')
{
parsenum--;
}
if (iNowPos>1 && strB64[iNowPos-4+2]=='=')
{
parsenum--;
}
return parsenum;
}
int Base64Enc(char *pOutBuf, char *pInput, int iSize)
{
const char *cBase64_Encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int iBuflen = 0;
int c1 =0,c2 =0,c3 =0;
while(iSize>0)
{
if(iSize>2)
{
c1 = pInput[0] & 0xFF;
c2 = pInput[1] & 0xFF;
c3 = pInput[2] & 0xFF;
*pOutBuf++ = cBase64_Encoding[(c1 & 0xFC) >> 2];
*pOutBuf++ = cBase64_Encoding[((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4)];
*pOutBuf++ = cBase64_Encoding[((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6)];
*pOutBuf++ = cBase64_Encoding[c3 & 0x3F];
}
else
{
switch(iSize)
{
case 1:
c1 = pInput[0] & 0xFF;
*pOutBuf++ = cBase64_Encoding[ (c1 & 0xFC) >> 2];
*pOutBuf++ = cBase64_Encoding[((c1 & 0x03) << 4)];
*pOutBuf++ = '=';
*pOutBuf++ = '=';
break;
case 2:
c1 = pInput[0] & 0xFF;
c2 = pInput[1] & 0xFF;
*pOutBuf++ = cBase64_Encoding[(c1 & 0xFC) >> 2];
*pOutBuf++ = cBase64_Encoding[((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4)];
*pOutBuf++ = cBase64_Encoding[((c2 & 0x0F) << 2)];
*pOutBuf++ = '=';
break;
}
}
pInput +=3;
iSize -=3;
iBuflen +=4;
}
*pOutBuf = 0;
return iBuflen;
}
int Base64Encode(char* pInput,int iSize,std::string& strB64)
{
const char *cBase64_Encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int iBuflen = 0;
int c1 =0,c2 =0,c3 =0;
if(NULL==pInput) return 0;
if(0==iSize) return 0;
while(iSize>0)
{
if(iSize>2)
{
c1 = pInput[0] & 0xFF;
c2 = pInput[1] & 0xFF;
c3 = pInput[2] & 0xFF;
strB64 += cBase64_Encoding[(c1 & 0xFC) >> 2];
strB64 += cBase64_Encoding[((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4)];
strB64 += cBase64_Encoding[((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6)];
strB64 += cBase64_Encoding[c3 & 0x3F];
}
else
{
switch(iSize)
{
case 1:
c1 = pInput[0] & 0xFF;
strB64 += cBase64_Encoding[ (c1 & 0xFC) >> 2];
strB64 += cBase64_Encoding[((c1 & 0x03) << 4)];
strB64 += '=';
strB64 += '=';
break;
case 2:
c1 = pInput[0] & 0xFF;
c2 = pInput[1] & 0xFF;
strB64 += cBase64_Encoding[(c1 & 0xFC) >> 2];
strB64 += cBase64_Encoding[((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4)];
strB64 += cBase64_Encoding[((c2 & 0x0F) << 2)];
strB64 += '=';
break;
}
}
pInput +=3;
iSize -=3;
iBuflen +=4;
}
//*pOutBuf = 0;
return iBuflen;
}
void ConvertJsonToString(Value *pValue, string &strJson)
{
//将doc对象的值写入字符串
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
pValue->Accept(writer);
strJson = buffer.GetString();
}
std::string ConvertJsonToString(Value &pValue)
{
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
pValue.Accept(writer);
return string(buffer.GetString());
}
//异或+7
string EasyEnc(string strKey, string strIn)
{
string strOut = "";
char cSingle[3] = {0};
unsigned char bSingle = '0';
size_t nKeyLen = strKey.length();
for (size_t nIndex=0; nIndex<strIn.length(); nIndex++)
{
//加密,异或+7
bSingle = (strIn.at(nIndex)^strKey.at(nIndex%nKeyLen))+7;
//bin转str
memset(cSingle, 0, sizeof(cSingle));
snprintf(cSingle, 3, "%02X", bSingle);
strOut += cSingle;
}
return strOut;
}
//异或-7
string EasyDec(string strKey, string strIn)
{
string strOut = "";
unsigned int nSingle = 0;
unsigned char bSingle = '0';
char cSingle[3] = {0};
size_t nKeyLen = strKey.length();
for (size_t nIndex=0; nIndex<strIn.length(); nIndex += 2)
{
//str转bin
memset(cSingle,0,sizeof(cSingle));
strncpy_s(cSingle, sizeof(cSingle), strIn.c_str()+nIndex, 2);
sscanf_s(cSingle, "%02X", &nSingle);
//解密,异或-7
bSingle = (unsigned char)(nSingle-7)^strKey.at((nIndex/2)%nKeyLen);
strOut += bSingle;
}
return strOut;
}
std::string TimeToStr(time_t t)
{
std::string strRe = "2000-01-01 00:00:00";
if (0 == t) return strRe;
struct tm newtime;
memset(&newtime, 0, sizeof(newtime));
localtime_s(&newtime, &t);
if (newtime.tm_year > 0)
{
strRe = stringformatA("%04d-%02d-%02d %02d:%02d:%02d",
newtime.tm_year + 1900, newtime.tm_mon + 1, newtime.tm_mday,
newtime.tm_hour, newtime.tm_min, newtime.tm_sec);
}
return strRe;
}
time_t StrToTime(const char *strString)
{
time_t timeRe = 0;
char *pPos = strstr((char*)strString, "-");
if (pPos == NULL)
{
return timeRe;
}
int iYear = std::atoi(strString);
int iMonth = std::atoi(pPos + 1);
pPos = strstr(pPos + 1, "-");
if (pPos == NULL)
{
return timeRe;
}
int iDay = atoi(pPos + 1);
int iHour = 0;
int iMin = 0;
int iSec = 0;
pPos = strstr(pPos + 1, " ");
//为了兼容有些没精确到时分秒的
if (pPos != NULL)
{
iHour = atoi(pPos + 1);
pPos = strstr(pPos + 1, ":");
if (pPos != NULL)
{
iMin = atoi(pPos + 1);
pPos = strstr(pPos + 1, ":");
if (pPos != NULL)
{
iSec = atoi(pPos + 1);
}
}
}
struct tm sourcedate;
memset((void*)&sourcedate, 0, sizeof(sourcedate));
sourcedate.tm_sec = iSec;
sourcedate.tm_min = iMin;
sourcedate.tm_hour = iHour;
sourcedate.tm_mday = iDay;
sourcedate.tm_mon = iMonth - 1;
sourcedate.tm_year = iYear - 1900;
timeRe = _mktime64(&sourcedate);
return timeRe;
}
/// @fn void Split(const std::string& strInput, const std::string& strSep, std::vector &vecSeg);
/// @brief 字符串分割然后赋值给容器
/// @param const std::string& strInput 传入赋值给容器的字符
/// @param const std::string& strSep 传入给容器字符内容的下标数
/// @param std::vector &vecSeg 所要赋值的容器
/// @return 得到数值
/// @retval 返回需要的字符串
/// @note
void Split(const std::string& strInput, const std::string& strSep, std::vector<std::string> &vecSeg)
{
std::string strTemp = "";
if(strInput.empty() || strSep.empty())
return;
vecSeg.clear();
vecSeg.reserve(strInput.size()/2);
size_t nStart = 0;
size_t nPos = std::string::npos;
do{
nPos = strInput.find(strSep,nStart);
strTemp = strInput.substr(nStart,nPos-nStart);
if (strTemp.length() > 0)
{
vecSeg.push_back(strTemp);
}
nStart = nPos + strSep.size();
}
while(nPos!=std::string::npos);
}
/// @fn void Split(const std::wstring &strInput, const std::wstring &strSep, vector &vecSeg);
/// @brief 字符串分割然后赋值给容器
/// @param const std::wstring& strInput 传入赋值给容器的字符
/// @param const std::wstring& strSep 传入给容器字符内容的下标数
/// @param std::vector &vecSeg 所要赋值的容器
/// @retval 返回需要的字符串
void Split(const std::wstring &strInput, const std::wstring &strSep, std::vector<std::wstring> &vecSeg)
{
wstring strTemp = L"";
if(strInput.empty() || strSep.empty())
return;
vecSeg.clear();
vecSeg.reserve(strInput.size()/2);
size_t nStart = 0;
size_t nPos = string::npos;
do{
nPos = strInput.find(strSep, nStart);
strTemp = strInput.substr(nStart, nPos-nStart);
if (strTemp.length() > 0)
{
vecSeg.push_back(strTemp);
}
nStart = nPos + strSep.size();
}
while(nPos != string::npos);
}
std::string GetJsonValue(const std::string& str, const std::string& strKey)
{
string strRet = "";
Document doc;
doc.Parse(str.c_str());
if(doc.HasParseError())
{
return strRet;
}
if(!doc.HasMember(strKey.c_str()))
{
TRACKERROR("str = %s NO Field(%s)!", str.c_str(), strKey.c_str());
return strRet;
}
ConvertJsonToString(&doc[strKey.c_str()],strRet);
if(strRet.find_first_of("\"") == 0)
{
strRet.erase(strRet.begin());
strRet.erase(strRet.end() - 1);
}
return strRet;
}
/// @fn int HexStr2Bin(unsigned char *pBinStr, int nMaxOutLen, const char *pszHexStr, int nHexLen);
/// @brief Base6416进制存储bin文件
/// @param unsigned char *pBinStr 接收文件
/// @param int nMaxOutLen 最大值数
/// @param const char *pszHexStr 被复制的
/// @param int nHexLen 循环次数
/// @return 得到数值
int HexStr2Bin(unsigned char *pBinStr, int nMaxOutLen, const char *pszHexStr, int nHexLen)
{
if (NULL == pBinStr || NULL == pszHexStr)
{
return -1;
}
if (nMaxOutLen <= 0 || nHexLen <= 0 || nHexLen % 2 || nMaxOutLen < nHexLen / 2)
{
return -1;
}
unsigned int dwSingle = 0;
char cSingle[3];
int nOutLen = 0;
for (int i = 0; i < nHexLen; i += 2)
{
memset(cSingle, 0, sizeof(cSingle));
strncpy_s(cSingle, sizeof(cSingle), (char *)pszHexStr + i, 2);
sscanf_s(cSingle, "%02X", &dwSingle);
pBinStr[nOutLen++] = dwSingle;
}
return nOutLen;
}
std::string UrlDecode(std::string strForDec)
{
//1. base64解码
//2.utf8 1/2/3解码
//3. Unicode解码
std::string output="";
DWORD dwCodeType =CODETYPE_UNKNOWN;
//#pragma region 1. base64解码
const static int iCount = 8;
const string strAryTemp[iCount]={ "=?utf-8?B?", "=?UTF-8?B?",
"=?gb2312?B?", "=?GB2312?B?","=?gbk?B?","=?GBK?B?","=?UTF8?B?","=?utf8?B?"};
size_t offset;
int iIndex = 0;
bool bNeedBase64Dec = false;
for(; !bNeedBase64Dec && iIndex < iCount; iIndex++)
{
offset = strForDec.find(strAryTemp[iIndex]);
if ( string::npos != offset)
{
bNeedBase64Dec = true;
break;
}
}
bool bUsedBase64Dec = false;
for ( ; bNeedBase64Dec;)
{
string strTemp = GetStringBetween(strForDec.c_str(),
(char *)strAryTemp[iIndex].c_str(),"?=");
int iLength = (int)strTemp.length();
assert(iLength > 0);
char *pOutBuffer = new char[iLength];
if ( pOutBuffer == NULL )
{
return "";
}
memset(pOutBuffer, 0, iLength);
if (Base64Decode(strTemp,pOutBuffer, iLength )>0)
{
strForDec.erase( offset, strAryTemp[iIndex].length() + iLength + 2);
strForDec.insert( offset, pOutBuffer);
bUsedBase64Dec = true;
delete []pOutBuffer;
}
else
{
delete []pOutBuffer;
return "";
}
offset = strForDec.find(strAryTemp[iIndex]);
bNeedBase64Dec = string::npos != offset;
}
//#pragma endregion 1. base64解码
if ( bUsedBase64Dec )
{
output = strForDec;
if ( lstrcmpiA(strAryTemp[iIndex].c_str(), "=?utf-8?B?")==0 )
{
dwCodeType = CODETYPE_UTF8_OK;
}
}
//2.utf8 1/2/3解码
else
{
dwCodeType =CODETYPE_UTF8_UNKNOWN;
size_t nlen =strForDec.length();
size_t nIndex = 0;
char tmp[2];
char ctmp;
int iUtf8Type = 2;
int iUtf8Count = 0;
while(nIndex<nlen)
{
if(strForDec[nIndex]=='%' && nIndex <(nlen-2))
{
tmp[0]=strForDec[nIndex+1];
tmp[1]=strForDec[nIndex+2];
ctmp = Str2Bin(tmp);
output+=ctmp;
nIndex=nIndex+3;
//#pragma region 只是对字节数判断
if (0 == iUtf8Count)
{
//3字节
if ((ctmp&0xF0)==0xE0)
{
iUtf8Type = 3;
if (dwCodeType == CODETYPE_UTF8_UNKNOWN)
{
dwCodeType = CODETYPE_UTF8_OK;
}
}
//2字节
else if ( ((ctmp&0xF0)==0xC0) || ((ctmp&0xF0)==0xD0))
{
iUtf8Type = 2;
if (dwCodeType == CODETYPE_UTF8_UNKNOWN)
{
dwCodeType = CODETYPE_UTF8_OK;
}
}
else if( !(ctmp & 0x80) ) //1字节 0xxxxxxx
{
iUtf8Type = 1; // add by ybx 2012-11-03
if (dwCodeType == CODETYPE_UTF8_UNKNOWN)
{
dwCodeType = CODETYPE_UTF8_OK;
}
}
else
{
dwCodeType = CODETYPE_UTF8_ERROR;
}
}
iUtf8Count++;
if (iUtf8Count == iUtf8Type) iUtf8Count = 0;
//#pragma endregion 只是对字节数判断
}
else if(strForDec[nIndex]=='+')
{
output+=' ';
iUtf8Count = 0;
nIndex++;
}
else
{
output+=strForDec[nIndex];
iUtf8Count = 0;
nIndex++;
}
}
}
//3. Unicode解码
if (CODETYPE_UTF8_OK == dwCodeType)
{
wstring woutput = L"";
size_t nlen = output.length();
size_t i = 0;
wchar_t tTemp[2];
while(i<nlen)
{
if (((output[i]&0xF0)==0xE0)&&((i+2)<nlen))
{
///UTF8->Unicode
BYTE b1 = ((output[i+0] & 0x0f) <<4) + ((output[i+1] & 0x3c) >>2);
BYTE b2 = ((output[i+1] & 0x03) <<6) + (output[i+2] & 0x3f);
tTemp[0]=b1*0x100+b2;
tTemp[1]=L'\0';
woutput += tTemp;
i+=3;
}
else if ((((output[i]&0xF0)==0xC0)||((output[i]&0xF0)==0xD0))&&((i+1)<nlen))
{
///UTF8->Unicode
BYTE b1 = (UCHAR)((output[i]) & 0x1f) >> 2;
BYTE b2 = ((UCHAR)((output[i]) & 0x1f) << 6) | (output[i+1] & 0x3f);
tTemp[0]=b1*0x100+b2;
tTemp[1]=L'\0';
woutput += tTemp;
i+=2;
}
else
{
tTemp[0]=output[i];
tTemp[1]=L'\0';
woutput += tTemp;
i++;
}
}
output = wstr2str(woutput.c_str(), CP_UTF8);
}
return output;
}
int Base64Decode(const std::string& strB64, char* pOutput,int iBufferSize)
{
int iB64Len=(int)strB64.size();
//检查是否是4的整数倍
if(iB64Len == 0 || iB64Len%4) return 0;
//检查输出缓冲区是不是正确
int iBINLen=iB64Len*3/4;
if (strB64[strB64.size()-1]=='=')
{
iBINLen--;
}
if (strB64[strB64.size()-2]=='=')
{
iBINLen--;
}
if (iBufferSize<iBINLen) return iBINLen;
if(NULL==pOutput) return 0;
unsigned char chunk[4];
int parsenum=0;
int iNowPos=0;
while(iNowPos<iB64Len)
{
chunk[0] = GetBase64Value(strB64[iNowPos]);
chunk[1] = GetBase64Value(strB64[iNowPos+1]);
chunk[2] = GetBase64Value(strB64[iNowPos+2]);
chunk[3] = GetBase64Value(strB64[iNowPos+3]);
*pOutput++ = (chunk[0] << 2) | (chunk[1] >> 4);
*pOutput++ = (chunk[1] << 4) | (chunk[2] >> 2);
*pOutput++ = (chunk[2] << 6) | (chunk[3]);
iNowPos+=4;
parsenum+=3;
}
if (iNowPos>0 && strB64[iNowPos-4+3]=='=')
{
parsenum--;
}
if (iNowPos>1 && strB64[iNowPos-4+2]=='=')
{
parsenum--;
}
return parsenum;
}
/// @fn void Base64Trim(std::string& str);
/// @brief 去除Base64编码字符串开头和结尾两端的空格
/// @param std::string& str
/// @return 得到数值
void Base64Trim(string &str)
{
size_t iposfront = 0;
size_t iposback = 0;
std::string str64Char = base64Char;
str64Char += "=";
for( ;iposback < str.size(); ++iposback)
{
if ( std::string::npos == str64Char.find((char)str.at(iposback)))
{
continue;
}
str.replace(iposfront, 1, 1, str.at(iposback));
++iposfront;
}
str.erase(iposfront, str.size() - iposfront);
}
string DecodeQuoted(const string &strSrc)
{
INT_PTR iSrcSize = strSrc.size();
const char *pByteSrc = (char*)strSrc.data();
char * pDes = new char[iSrcSize];
memset(pDes,0, iSrcSize);
char * pTemp = pDes;
INT_PTR i = 0;
int iSize = 0;
while(i < iSrcSize)
{
if (strncmp(pByteSrc, "=\r\n", 3) == 0) // 软回车,跳过
{
pByteSrc += 3;
i += 3;
}
else
{
if (*pByteSrc == '=') // 是编码字节
{
sscanf_s(pByteSrc, "=%02X", reinterpret_cast<unsigned int *>(pTemp));
pTemp++;
pByteSrc += 3;
i += 3;
}
else // 非编码字节
{
*pTemp++ = (unsigned char)*pByteSrc++;
i++;
}
++iSize;
}
}
std::string result(pDes, iSize);
delete []pDes;
pDes = NULL;
return result;
}
std::string ToLower(const std::string& strIn)
{
std::string strTmp = strIn;
for(size_t i=0; i<strTmp.size(); ++i)
{
char& c = strTmp[i];
if(c >= 'A' && c <= 'Z')
c |= 32;
}
return strTmp;
}
std::wstring ToLower(const std::wstring& strIn)
{
std::wstring strTmp = strIn;
for(size_t i=0; i<strTmp.size(); ++i)
{
wchar_t& c = strTmp[i];
if(c >= L'A' && c <= L'Z')
c|=32;
}
return strTmp;
}
std::string ToUpper(const std::string& strIn)
{
std::string strTmp = strIn;
for(size_t i=0; i<strTmp.size(); ++i)
{
char& c = strTmp[i];
if(c >= 'a' && c <= 'z')
c &= 0xdf;
}
return strTmp;
}
std::wstring ToUpper(const std::wstring& strIn)
{
std::wstring strTmp = strIn;
for(size_t i=0; i<strTmp.size(); ++i)
{
wchar_t& c = strTmp[i];
if(c >= L'A' && c <= L'Z')
c &= 0xdf;
}
return strTmp;
}
int stringreplace(std::string& strString, const char* src, const char* target)
{
size_t nLen = 0;
int iPos = 0;
int iCount = 0;
while (nLen < strString.length())
{
iPos = (int)strString.find(src, nLen);
if (iPos >= 0)
{//找到了
iCount++;
strString.replace(iPos, strlen(src), target);
nLen = iPos + strlen(target);
}
else
{
break;
}
}
return iCount;
}
BOOL StrWildcard(const char *pat, const char *str)
{
while(*str && *pat)
{
if(*pat == '?')
{
if(StrWildcard(pat+1, str)) return TRUE;
str++;
pat++;
}
else if(*pat == '*')
{
while(*pat == '*' || *pat == '?') pat++;
if(!*pat) return TRUE;
while(*str)
{
if(StrWildcard(pat, str)) return TRUE;
str++;
}
return FALSE;
}
else
{
if(*pat!=*str) return FALSE;
str++;
pat++;
}
}
if(*str!=0) return FALSE;
while(*pat == '*' || *pat == '?') pat++;
return !*pat;
}
LONGLONG NetTools_IpString2Ip(const std::string& strRe, BOOL ipv6)
{
LONGLONG ullIp = 0;
int bIP[8];
if (0 == strRe.length())
{
return 0;
}
if (ipv6)
{//ipV6
sscanf_s(strRe.c_str(), "%d.%d.%d.%d.%d.%d", &bIP[5], &bIP[4], &bIP[3], &bIP[2], &bIP[1], &bIP[0]);
ullIp = ((LONGLONG)bIP[5] << 40) | ((LONGLONG)bIP[4] << 32) | ((LONGLONG)bIP[3] << 24) | ((LONGLONG)bIP[2] << 16) | ((LONGLONG)bIP[1] << 8) | (LONGLONG)bIP[0];
}
else
{//ipV4
sscanf_s(strRe.c_str(), "%d.%d.%d.%d", &bIP[3], &bIP[2], &bIP[1], &bIP[0]);
ullIp = ((LONGLONG)bIP[3] << 24) | ((LONGLONG)bIP[2] << 16) | ((LONGLONG)bIP[1] << 8) | (LONGLONG)bIP[0];
}
return ullIp;
}
std::string NetTools_Ip2IpString(LONGLONG llIp, BOOL ipv6)
{
BYTE bIP[6];
std::string strRe;
bIP[0] = (BYTE)((llIp >> (8 * 0)) & (BYTE)0xff);
bIP[1] = (BYTE)((llIp >> (8 * 1)) & (BYTE)0xff);
bIP[2] = (BYTE)((llIp >> (8 * 2)) & (BYTE)0xff);
bIP[3] = (BYTE)((llIp >> (8 * 3)) & (BYTE)0xff);
if (ipv6)
{//ipV6
bIP[4] = (BYTE)((llIp >> (8 * 4)) & (BYTE)0xff);
bIP[5] = (BYTE)((llIp >> (8 * 5)) & (BYTE)0xff);
strRe = stringformatA("%d.%d.%d.%d.%d.%d", bIP[5], bIP[4], bIP[3], bIP[2], bIP[1], bIP[0]);
}
else
{//ipV4
strRe = stringformatA("%d.%d.%d.%d", bIP[3], bIP[2], bIP[1], bIP[0]);
}
return strRe;
}
std::string RandomString(DWORD dwStrLen, DWORD dwType, DWORD dwSrand)
{
std::string strRand = "";
char cRand;
DWORD dwLen = 0;
srand((unsigned)time(NULL) + dwSrand);
while (dwLen < dwStrLen)
{
cRand = rand() % 128;
if (_UPPER&dwType)
{//<大写字母
if (isupper(cRand)) { strRand += cRand; dwLen++; continue; }
}
if (_LOWER&dwType)
{//<小写字母
if (islower(cRand)) { strRand += cRand; dwLen++; continue; }
}
if (_DIGIT&dwType)
{//<数字
if (isdigit(cRand)) { strRand += cRand; dwLen++; continue; }
}
if (_PUNCT&dwType)
{//<特殊字符
if (ispunct(cRand)) { strRand += cRand; dwLen++; continue; }
}
}
return strRand;
}
//返回str中的一个UTF-8编码的子字符串,从位置nStart开始,长度为nLength。
std::string Utf8SubString(const std::string &str, size_t nStart, size_t nLength)
{
if (nLength == 0 || nStart > str.size())
{
return std::string();
}
if (nLength == std::string::npos)
{
return str.substr(nStart, std::string::npos);
}
size_t i = 0, j = 0;
size_t nMin = std::string::npos;
size_t nMax = std::string::npos;
for (i = 0, j = 0; i < str.size(); i++, j++)
{
if (j == nStart)
{
nMin = i;
}
if (i <= nStart + nLength )
{
nMax = i;
}
else
{
break;
}
unsigned char byte = str[i];
if (byte < 128)
{
i += 0;
}
else if ((byte & 0xE0) == 0xC0)
{
i += 1;
}
else if ((byte & 0xF0) == 0xE0)
{
i += 2;
}
else if ((byte & 0xF8) == 0xF0)
{
i += 3;
}
else
{
return std::string();
}
}
if (j <= nStart + nLength)
{
nMax = i;
}
return str.substr(nMin, nMax);
}