161 lines
3.9 KiB
C++
161 lines
3.9 KiB
C++
#include "AuxFun.h"
|
||
#include<Windows.h>
|
||
#include <string>
|
||
#include <assert.h>
|
||
|
||
#include "zlib/zconf.h"
|
||
#include "zlib/zlib.h"
|
||
std::string MBCStoUTF8(const char* mbcsStr)
|
||
{
|
||
wchar_t* wideStr;
|
||
char* strAnsi;
|
||
int charLen;
|
||
//先转换成utf-16
|
||
charLen = MultiByteToWideChar(CP_ACP, 0, mbcsStr, -1, NULL, 0);
|
||
wideStr = (wchar_t*)malloc(sizeof(wchar_t)*charLen);
|
||
MultiByteToWideChar(CP_ACP, 0, mbcsStr, -1, wideStr, charLen);
|
||
//再转换成utf-8
|
||
charLen = WideCharToMultiByte(CP_UTF8, 0, wideStr, -1, NULL, 0, NULL, NULL);
|
||
strAnsi = (char*)malloc(charLen);
|
||
WideCharToMultiByte(CP_UTF8, 0, wideStr, -1, strAnsi, charLen, NULL, NULL);
|
||
std::string strRet = strAnsi;
|
||
|
||
free(wideStr);
|
||
free(strAnsi);
|
||
return strRet;
|
||
}
|
||
std::string UTF8ToMultiByte(const char* mbcsStr)
|
||
{
|
||
wchar_t* wideStr;
|
||
char* strAnsi;
|
||
int charLen;
|
||
|
||
charLen = MultiByteToWideChar(CP_UTF8, 0, mbcsStr, -1, NULL, 0);
|
||
wideStr = (wchar_t*)malloc(sizeof(wchar_t)*charLen);
|
||
MultiByteToWideChar(CP_UTF8, 0, mbcsStr, -1, wideStr, charLen);
|
||
|
||
charLen = WideCharToMultiByte(CP_ACP, 0, wideStr, -1, NULL, 0, NULL, NULL);
|
||
strAnsi = (char*)malloc(charLen);
|
||
WideCharToMultiByte(CP_ACP, 0, wideStr, -1, strAnsi, charLen, NULL, NULL);
|
||
|
||
std::string strRet = strAnsi;
|
||
free(wideStr);
|
||
|
||
free(strAnsi);
|
||
return strRet;
|
||
}
|
||
|
||
//----------------------------------------------
|
||
//Reference:http://blog.csdn.net/gemo/article/details/8468311
|
||
//______________________________________________
|
||
unsigned char ToHex(unsigned char x)
|
||
{
|
||
//十进制 'A'65 '0'48
|
||
return x > 9 ? x + 55 : x + 48;
|
||
}
|
||
|
||
unsigned char FromHex(unsigned char x)
|
||
{
|
||
unsigned char y;
|
||
if (x >= 'A' && x <= 'Z') { y = x - 'A' + 10; }
|
||
else if (x >= 'a' && x <= 'z') { y = x - 'a' + 10; }
|
||
else if (x >= '0' && x <= '9') { y = x - '0'; }
|
||
else { assert(0); }
|
||
return y;
|
||
}
|
||
|
||
std::string UrlEncode(const std::string& str)
|
||
{
|
||
std::string strTemp = "";
|
||
size_t length = str.length();
|
||
for (size_t i = 0; i < length; i++)
|
||
{
|
||
if (isalnum((unsigned char)str[i]) ||
|
||
(str[i] == '-') ||
|
||
(str[i] == '_') ||
|
||
(str[i] == '.') ||
|
||
(str[i] == '~')) { strTemp += str[i]; }
|
||
|
||
else if (str[i] == ' ') { strTemp += "+"; }
|
||
else
|
||
{
|
||
strTemp += '%';
|
||
strTemp += ToHex((unsigned char)str[i] >> 4);
|
||
strTemp += ToHex((unsigned char)str[i] % 16);
|
||
}
|
||
}
|
||
return strTemp;
|
||
}
|
||
|
||
std::string UrlDecode(const std::string& str)
|
||
{
|
||
std::string strTemp = "";
|
||
size_t length = str.length();
|
||
for (size_t i = 0; i < length; i++)
|
||
{
|
||
if (str[i] == '+') strTemp += ' ';
|
||
else if (str[i] == '%')
|
||
{
|
||
assert(i + 2 < length);
|
||
unsigned char high = FromHex((unsigned char)str[++i]);
|
||
unsigned char low = FromHex((unsigned char)str[++i]);
|
||
strTemp += high * 16 + low;
|
||
}
|
||
else strTemp += str[i];
|
||
}
|
||
return strTemp;
|
||
}
|
||
int inflate_read(char *source, int len, char **dest, int gzip, int& iTotalRead)
|
||
{
|
||
int ret;
|
||
unsigned have;
|
||
z_stream strm;
|
||
unsigned char out[10000];
|
||
int totalsize = 0;
|
||
|
||
/* allocate inflate state */
|
||
strm.zalloc = Z_NULL;
|
||
strm.zfree = Z_NULL;
|
||
strm.opaque = Z_NULL;
|
||
strm.avail_in = 0;
|
||
strm.next_in = Z_NULL;
|
||
|
||
if (gzip)
|
||
ret = inflateInit2(&strm, 47);
|
||
else
|
||
ret = inflateInit(&strm);
|
||
|
||
if (ret != Z_OK)
|
||
return ret;
|
||
|
||
strm.avail_in = len;
|
||
strm.next_in = (Bytef*)source;
|
||
|
||
/* run inflate() on input until output buffer not full */
|
||
do {
|
||
strm.avail_out = 10000;
|
||
strm.next_out = out;
|
||
ret = inflate(&strm, Z_NO_FLUSH);
|
||
//assert(ret != Z_STREAM_ERROR); /* state not clobbered */
|
||
|
||
switch (ret) {
|
||
case Z_NEED_DICT:
|
||
ret = Z_DATA_ERROR; /* and fall through */
|
||
case Z_DATA_ERROR:
|
||
case Z_MEM_ERROR:
|
||
inflateEnd(&strm);
|
||
return ret;
|
||
}
|
||
|
||
have = 10000 - strm.avail_out;
|
||
totalsize += have;
|
||
*dest = (char*)realloc(*dest, totalsize);
|
||
iTotalRead = totalsize;
|
||
memcpy(*dest + totalsize - have, out, have);
|
||
} while (strm.avail_out == 0);
|
||
|
||
/* clean up and return */
|
||
(void)inflateEnd(&strm);
|
||
|
||
return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
|
||
} |