decode all URIs, fixes #937, 386

This commit is contained in:
Dennis Luxen
2014-03-04 13:56:11 +01:00
parent 0baa8215ef
commit 32bf99ba40
2 changed files with 34 additions and 2 deletions
+31
View File
@@ -36,6 +36,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <boost/spirit/include/qi.hpp>
#include <cstdio>
#include <cctype>
#include <string>
// precision: position after decimal point
@@ -177,6 +178,36 @@ inline std::string HTMLDeEntitize( std::string & result) {
return result;
}
inline std::size_t URIDecode(const std::string & input, std::string & output) {
std::string::const_iterator src_iter = input.begin();
output.resize(input.size()+1);
std::size_t decoded_length = 0;
for( decoded_length = 0; src_iter != input.end(); ++decoded_length ) {
if(
src_iter[0] == '%' &&
src_iter[1] &&
src_iter[2] &&
isxdigit(src_iter[1]) &&
isxdigit(src_iter[2])
) {
std::string::value_type a = src_iter[1];
std::string::value_type b = src_iter[2];
a -= src_iter[1] < 58 ? 48 : src_iter[1] < 71 ? 55 : 87;
b -= src_iter[2] < 58 ? 48 : src_iter[2] < 71 ? 55 : 87;
output[decoded_length] = 16 * a + b;
src_iter += 3;
continue;
}
output[decoded_length] = *src_iter++;
}
output.resize(decoded_length);
return decoded_length;
}
inline std::size_t URIDecodeInPlace(std::string & URI) {
return URIDecode(URI, URI);
}
inline bool StringStartsWith(
const std::string & input,
const std::string & prefix