修复url反转义失败时字符乱码相关bug (#2932 #2935)

This commit is contained in:
xiongguangjie 2023-10-27 22:49:42 +08:00 committed by GitHub
parent 6d06649a5b
commit 0f94b48823
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -36,20 +36,21 @@ void UnicodeToUTF8(char *pOut, const wchar_t *pText) {
return; return;
} }
char CharToInt(char ch) { char HexCharToBin(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 = HexCharToBin(str[0]);
char chn; auto low = HexCharToBin(str[1]);
tempWord[0] = CharToInt(str[0]); //make the B to 11 -- 00001011 if (high == -1 || low == -1) {
tempWord[1] = CharToInt(str[1]); //make the 0 to 0 -- 00000000 // 无法把16进制字符串转换为二进制
chn = (tempWord[0] << 4) | tempWord[1]; //to change the BO to 10110000 return -1;
return chn; }
return (high << 4) | low;
} }
string strCoding::UrlEncode(const string &str) { string strCoding::UrlEncode(const string &str) {
@ -70,26 +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) {
output += StrToBin(tmp); // %后面两个字节不是16进制字符串转义失败直接拼接3个原始字符
i = i + 3; output.append(str, i, 3);
} else {
output += ch;
}
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)