| 167 | } |
| 168 | |
| 169 | bool PercentEncoding::Decode(std::string *str) |
| 170 | { |
| 171 | std::string& s = *str; |
| 172 | size_t write_pos = 0; |
| 173 | for (size_t i = 0; i < s.size(); ++i) { |
| 174 | uint8_t ch = 0; |
| 175 | if (s[i] == '%') { |
| 176 | if (i + 2 > s.size()) { |
| 177 | // 后面的数据不完整了,返回吧 |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | if (!Ascii::IsHexDigit(s[i+1]) || !Ascii::IsHexDigit(s[i+2])) |
| 182 | return false; |
| 183 | |
| 184 | ch = (HexValue(s[i+1]) << 4); |
| 185 | ch |= HexValue(s[i+2]); |
| 186 | i += 2; |
| 187 | } else if (s[i] == '+') { |
| 188 | ch = ' '; |
| 189 | } else { |
| 190 | ch = s[i]; |
| 191 | } |
| 192 | s[write_pos++] = static_cast<char>(ch); |
| 193 | } |
| 194 | s.resize(write_pos); |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | } // namespace toft |
| 199 |