diff --git a/include/exiv2/futils.hpp b/include/exiv2/futils.hpp index 12874f85..bcf88aa3 100644 --- a/include/exiv2/futils.hpp +++ b/include/exiv2/futils.hpp @@ -65,17 +65,6 @@ namespace Exiv2 */ EXIV2API std::string urlencode(const std::string_view& str); - /*! - @brief Decode the input url. - @param str The url needs decoding. - @return the url-decoded version of str. - - @note Be sure to 'free' the returned string after use with 'delete []'. - Source: http://www.geekhideout.com/urlcode.shtml - @todo This function can probably be hidden into the implementation details - */ - EXIV2API char* urldecode(const char* str); - /*! @brief Like urlencode(char* str) but accept the input url in the std::string and modify it. @todo This function can probably be hidden into the implementation details diff --git a/src/futils.cpp b/src/futils.cpp index 7687ec26..be1f3e91 100644 --- a/src/futils.cpp +++ b/src/futils.cpp @@ -120,31 +120,24 @@ namespace Exiv2 { return encoded; } - char* urldecode(const char* str) { - const char* pstr = str; - auto buf = new char[(strlen(str) + 1)]; - char* pbuf = buf; - while (*pstr) { - if (*pstr == '%') { - if (pstr[1] && pstr[2]) { - *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]); - pstr += 2; + void urldecode(std::string& str) + { + size_t idxIn{0}, idxOut{0}; + size_t sizeStr = str.size(); + while (idxIn < sizeStr) { + if (str[idxIn] == '%') { + if (str[idxIn+1] && str[idxIn+2]) { + str[idxOut++] = from_hex(str[idxIn+1]) << 4 | from_hex(str[idxIn+2]); + idxIn += 2; } - } else if (*pstr == '+') { - *pbuf++ = ' '; + } else if (str[idxIn] == '+') { + str[idxOut++] = ' '; } else { - *pbuf++ = *pstr; + str[idxOut++] = str[idxIn]; } - pstr++; + idxIn++; } - *pbuf = '\0'; - return buf; - } - - void urldecode(std::string& str) { - char* decodeStr = Exiv2::urldecode(str.c_str()); - str = std::string(decodeStr); - delete [] decodeStr; + str.erase(idxOut); } // https://stackoverflow.com/questions/342409/how-do-i-base64-encode-decode-in-c diff --git a/unitTests/test_futils.cpp b/unitTests/test_futils.cpp index 2bb4dc81..8aab12de 100644 --- a/unitTests/test_futils.cpp +++ b/unitTests/test_futils.cpp @@ -97,15 +97,6 @@ TEST(urlencode, encodesGivenUrlWithSpace) ASSERT_STREQ("http%3a%2f%2fwww.geekhideout.com%2furl+code.shtml", url.c_str()); } -TEST(urldecode, decodesGivenUrl) -{ - const std::string expectedDecodedUrl ("http://www.geekhideout.com/urlcode.shtml"); - const std::string url ("http%3a%2f%2fwww.geekhideout.com%2furlcode.shtml"); - char * url3 = urldecode(url.c_str()); - ASSERT_STREQ(expectedDecodedUrl.c_str(), url3); - delete [] url3; -} - TEST(urldecode, decodesGivenUrlInPlace) { const std::string expectedDecodedUrl ("http://www.geekhideout.com/urlcode.shtml");