优化完善代码

This commit is contained in:
xia-chu 2023-10-27 22:46:02 +08:00
parent 7c81cbb24a
commit e438744d76
1 changed files with 43 additions and 27 deletions

View File

@ -36,23 +36,21 @@ void UnicodeToUTF8(char *pOut, const wchar_t *pText) {
return; return;
} }
char CharToInt(char ch) { char HexStrToBin(char ch) {
if (ch >= '0' && ch <= '9')return (char) (ch - '0'); 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);
if (ch >= 'A' && ch <= 'F')return (char) (ch - 'A' + 10); if (ch >= 'A' && ch <= 'F') return (char)(ch - 'A' + 10);
return -1; return -1;
} }
char StrToBin(const char *str) { char HexStrToBin(const char *str) {
char tempWord[2]; auto high = HexStrToBin(str[0]);
char chn; auto low = HexStrToBin(str[1]);
tempWord[0] = CharToInt(str[0]); //make the B to 11 -- 00001011 if (high < 0 || low < 0) {
tempWord[1] = CharToInt(str[1]); //make the 0 to 0 -- 00000000 // 无法把16进制字符串转换为二进制
if(tempWord[0]<0 || tempWord[1]< 0){
return -1; return -1;
} }
chn = (tempWord[0] << 4) | tempWord[1]; //to change the BO to 10110000 return (high << 4) | low;
return chn;
} }
string strCoding::UrlEncode(const string &str) { string strCoding::UrlEncode(const string &str) {
@ -73,33 +71,51 @@ string strCoding::UrlEncode(const string &str) {
string strCoding::UrlDecode(const string &str) { string strCoding::UrlDecode(const string &str) {
string output; string output;
char tmp[2];
size_t i = 0, len = str.length(); size_t i = 0, len = str.length();
while (i < len) { while (i < len) {
if (str[i] == '%') { if (str[i] == '%') {
if (i > len - 3) { if (i + 3 > len) {
//防止内存溢出 // %后面必须还有两个字节才会反转义
output.append(str, i, len - i);
break; break;
} }
tmp[0] = str[i + 1]; char ch = HexStrToBin(&(str[i + 1]));
tmp[1] = str[i + 2]; if (ch == -1) {
char r = StrToBin(tmp); // %后面两个字节不是16进制字符串转义失败直接拼接3个原始字符
if(r == -1){// % output.append(str, i, 3);
output += str[i]; } else {
output += tmp[0]; output += ch;
output += tmp[1];
}else{
output += r;
} }
i = i + 3; i += 3;
} else { } else {
output += str[i]; output += str[i];
i++; ++i;
} }
} }
return output; return output;
} }
#if 0
#include "Util/onceToken.h"
static toolkit::onceToken token([]() {
auto str0 = strCoding::UrlDecode(
"rtsp%3A%2F%2Fadmin%3AJm13317934%25jm%40111.47.84.69%3A554%2FStreaming%2FChannels%2F101%3Ftransportmode%3Dunicast%26amp%3Bprofile%3DProfile_1");
auto str1 = strCoding::UrlDecode("%j1"); // 测试%后面两个字节不是16进制字符串
auto str2 = strCoding::UrlDecode("%a"); // 测试%后面字节数不够
auto str3 = strCoding::UrlDecode("%"); // 测试只有%
auto str4 = strCoding::UrlDecode("%%%"); // 测试多个%
auto str5 = strCoding::UrlDecode("%%%%40"); // 测试多个非法%后恢复正常解析
auto str6 = strCoding::UrlDecode("Jm13317934%jm"); // 测试多个非法%后恢复正常解析
cout << str0 << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
cout << str4 << endl;
cout << str5 << endl;
cout << str6 << endl;
});
#endif
///////////////////////////////windows专用/////////////////////////////////// ///////////////////////////////windows专用///////////////////////////////////
#if defined(_WIN32) #if defined(_WIN32)
void UnicodeToGB2312(char* pOut, wchar_t uData) void UnicodeToGB2312(char* pOut, wchar_t uData)