strCoding.cpp 5.6 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Copyright (c) 2016 xiongziliang <771730766@qq.com>
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
xzl committed
25 26 27
 */

#include <string.h>
xiongzilaing committed
28
#include "strCoding.h"
xzl committed
29

30 31 32 33
#if defined(_WIN32)
#include <windows.h> 
#endif//defined(_WIN32)

xzl committed
34 35 36
namespace ZL {
namespace Http {

37
//////////////////////////通用///////////////////////
xiongziliang committed
38
void UTF8ToUnicode(wchar_t* pOut, const char *pText)
39 40 41 42 43 44
{
	char* uchar = (char *)pOut;
	uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);
	uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);
	return;
}
xiongziliang committed
45
void UnicodeToUTF8(char* pOut, const wchar_t* pText)
46 47 48 49 50 51 52 53 54 55 56 57 58 59
{
	// 注意 WCHAR高低字的顺序,低字节在前,高字节在后 
	const char* pchar = (const char *)pText;
	pOut[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));
	pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);
	pOut[2] = (0x80 | (pchar[0] & 0x3F));
	return;
}

char CharToInt(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);
xzl committed
60 61
	return -1;
}
62 63
char StrToBin(const char *str) 
{
xzl committed
64 65
	char tempWord[2];
	char chn;
66 67 68
	tempWord[0] = CharToInt(str[0]); //make the B to 11 -- 00001011 
	tempWord[1] = CharToInt(str[1]); //make the 0 to 0 -- 00000000 
	chn = (tempWord[0] << 4) | tempWord[1]; //to change the BO to 10110000 
xzl committed
69 70
	return chn;
}
71 72

string strCoding::UrlUTF8Encode(const string &str) {
xzl committed
73
	string dd;
74
	size_t len = str.size();
xzl committed
75
	for (size_t i = 0; i < len; i++) {
76
		if (isalnum((uint8_t)str[i])) {
xzl committed
77 78 79
			char tempbuff[2];
			sprintf(tempbuff, "%c", str[i]);
			dd.append(tempbuff);
80 81
		}
		else if (isspace((uint8_t)str[i])) {
xzl committed
82
			dd.append("+");
83 84
		}
		else {
xzl committed
85
			char tempbuff[4];
86
			sprintf(tempbuff, "%%%X%X", (uint8_t)str[i] >> 4,(uint8_t)str[i] % 16);
xzl committed
87 88 89 90 91 92 93 94
			dd.append(tempbuff);
		}
	}
	return dd;
}
string strCoding::UrlUTF8Decode(const string &str) {
	string output = "";
	char tmp[2];
95
	int i = 0, len = str.length();
xzl committed
96 97 98 99 100 101
	while (i < len) {
		if (str[i] == '%') {
			tmp[0] = str[i + 1];
			tmp[1] = str[i + 2];
			output += StrToBin(tmp);
			i = i + 3;
102 103
		}
		else if (str[i] == '+') {
xzl committed
104 105
			output += ' ';
			i++;
106 107
		}
		else {
xzl committed
108 109 110 111 112 113 114
			output += str[i];
			i++;
		}
	}
	return output;
}

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
string strCoding::UrlGB2312Encode(const string &str)
{
	string dd;
	size_t len = str.size();
	for (size_t i = 0; i<len; i++)
	{
		if (isalnum((uint8_t)str[i]))
		{
			char tempbuff[2];
			sprintf(tempbuff, "%c", str[i]);
			dd.append(tempbuff);
		}
		else if (isspace((uint8_t)str[i]))
		{
			dd.append("+");
		}
		else
		{
			char tempbuff[4];
			sprintf(tempbuff, "%%%X%X", (uint8_t)str[i] >> 4, (uint8_t)str[i] % 16);
			dd.append(tempbuff);
		}
	}
	return dd;
}

string strCoding::UrlGB2312Decode(const string &str)
{
	string output = "";
	char tmp[2];
	int i = 0, idx = 0, len = str.length();
	while (i<len) {
		if (str[i] == '%') {
			tmp[0] = str[i + 1];
			tmp[1] = str[i + 2];
			output += StrToBin(tmp);
			i = i + 3;
		}
		else if (str[i] == '+') {
			output += ' ';
			i++;
		}
		else {
			output += str[i];
			i++;
		}
	}
	return output;
}

///////////////////////////////windows专用///////////////////////////////////
#if defined(_WIN32)
void UnicodeToGB2312(char* pOut, wchar_t uData)
{
	WideCharToMultiByte(CP_ACP, NULL, &uData, 1, pOut, sizeof(wchar_t), NULL, NULL);
}
void Gb2312ToUnicode(wchar_t* pOut, const char *gbBuffer)
{
	MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, gbBuffer, 2, pOut, 1);
}

string strCoding::UTF8ToGB2312(const string &str) {
	auto len = str.size();
	auto pText = str.data();
	char Ctemp[4] = {0};
	char *pOut = new char[len + 1];
	memset(pOut, 0, len + 1);

	int i = 0, j = 0;
	while (i < len)
	{
		if (pText[i] >= 0)
		{
			pOut[j++] = pText[i++];
		}
		else
		{
			wchar_t Wtemp;
			UTF8ToUnicode(&Wtemp, pText + i);
			UnicodeToGB2312(Ctemp, Wtemp);
			pOut[j] = Ctemp[0];
			pOut[j + 1] = Ctemp[1];
			i += 3;
			j += 2;
		}
	}
	string ret = pOut;
	delete[] pOut;
	return ret;
}

string strCoding::GB2312ToUTF8(const string &str) {
	auto len = str.size();
	auto pText = str.data();
	char buf[4] = { 0 };
	int nLength = len * 3;
	char* pOut = new char[nLength];
	memset(pOut, 0, nLength);
	int i = 0, j = 0;
	while (i < len)
	{
		//如果是英文直接复制就可以   
		if (*(pText + i) >= 0)
		{
			pOut[j++] = pText[i++];
		}
		else
		{
			wchar_t pbuffer;
			Gb2312ToUnicode(&pbuffer, pText + i);
			UnicodeToUTF8(buf, &pbuffer);
			pOut[j] = buf[0];
			pOut[j + 1] = buf[1];
			pOut[j + 2] = buf[2];
			j += 3;
			i += 2;
		}
	}   
	string ret = pOut;
	delete[] pOut;
	return ret;
}
#endif//defined(_WIN32)




xzl committed
242 243
} /* namespace Http */
} /* namespace ZL */