优化url编解码
This commit is contained in:
parent
e4e5400641
commit
119d90bc58
|
|
@ -69,24 +69,19 @@ char StrToBin(const char *str)
|
||||||
}
|
}
|
||||||
|
|
||||||
string strCoding::UrlEncode(const string &str) {
|
string strCoding::UrlEncode(const string &str) {
|
||||||
string dd;
|
string out;
|
||||||
size_t len = str.size();
|
size_t len = str.size();
|
||||||
for (size_t i = 0; i < len; i++) {
|
for (size_t i = 0; i < len; ++i) {
|
||||||
if (isalnum((uint8_t)str[i])) {
|
char ch = str[i];
|
||||||
char tempbuff[2];
|
if (isalnum((uint8_t)ch)) {
|
||||||
sprintf(tempbuff, "%c", str[i]);
|
out.push_back(ch);
|
||||||
dd.append(tempbuff);
|
}else {
|
||||||
}
|
|
||||||
else if (isspace((uint8_t)str[i])) {
|
|
||||||
dd.append("+");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
char tempbuff[4];
|
char tempbuff[4];
|
||||||
sprintf(tempbuff, "%%%X%X", (uint8_t)str[i] >> 4,(uint8_t)str[i] % 16);
|
sprintf(tempbuff, "%%%X%X", (uint8_t)str[i] >> 4,(uint8_t)str[i] % 16);
|
||||||
dd.append(tempbuff);
|
out.append(tempbuff);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return dd;
|
return out;
|
||||||
}
|
}
|
||||||
string strCoding::UrlDecode(const string &str) {
|
string strCoding::UrlDecode(const string &str) {
|
||||||
string output = "";
|
string output = "";
|
||||||
|
|
@ -94,16 +89,18 @@ string strCoding::UrlDecode(const string &str) {
|
||||||
int i = 0, len = str.length();
|
int i = 0, len = str.length();
|
||||||
while (i < len) {
|
while (i < len) {
|
||||||
if (str[i] == '%') {
|
if (str[i] == '%') {
|
||||||
|
if(i > len - 3){
|
||||||
|
//防止内存溢出
|
||||||
|
break;
|
||||||
|
}
|
||||||
tmp[0] = str[i + 1];
|
tmp[0] = str[i + 1];
|
||||||
tmp[1] = str[i + 2];
|
tmp[1] = str[i + 2];
|
||||||
output += StrToBin(tmp);
|
output += StrToBin(tmp);
|
||||||
i = i + 3;
|
i = i + 3;
|
||||||
}
|
} else if (str[i] == '+') {
|
||||||
else if (str[i] == '+') {
|
|
||||||
output += ' ';
|
output += ' ';
|
||||||
i++;
|
i++;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
output += str[i];
|
output += str[i];
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue