diff --git a/src/common/url.cpp b/src/common/url.cpp index d0bf4e0cbe4..f27bb2b73fc 100644 --- a/src/common/url.cpp +++ b/src/common/url.cpp @@ -9,22 +9,22 @@ #include #include -std::string urlDecode(std::string_view urlEncoded) +std::string UrlDecode(std::string_view url_encoded) { std::string res; - res.reserve(urlEncoded.size()); + res.reserve(url_encoded.size()); - for (size_t i = 0; i < urlEncoded.size(); ++i) { - char c = urlEncoded[i]; + for (size_t i = 0; i < url_encoded.size(); ++i) { + char c = url_encoded[i]; // Special handling for percent which should be followed by two hex digits // representing an octet values, see RFC 3986, Section 2.1 Percent-Encoding - if (c == '%' && i + 2 < urlEncoded.size()) { + if (c == '%' && i + 2 < url_encoded.size()) { unsigned int decoded_value{0}; - auto [p, ec] = std::from_chars(urlEncoded.data() + i + 1, urlEncoded.data() + i + 3, decoded_value, 16); + auto [p, ec] = std::from_chars(url_encoded.data() + i + 1, url_encoded.data() + i + 3, decoded_value, 16); // Only if there is no error and the pointer is set to the end of // the string, we can be sure both characters were valid hex - if (ec == std::errc{} && p == urlEncoded.data() + i + 3) { + if (ec == std::errc{} && p == url_encoded.data() + i + 3) { // A null character terminates the string if (decoded_value == 0) { return res; diff --git a/src/common/url.h b/src/common/url.h index 42e982fb941..203f41c70f2 100644 --- a/src/common/url.h +++ b/src/common/url.h @@ -12,6 +12,6 @@ * * Notably this implementation does not decode a '+' to a ' '. */ -std::string urlDecode(std::string_view url_encoded); +std::string UrlDecode(std::string_view url_encoded); #endif // BITCOIN_COMMON_URL_H diff --git a/src/test/common_url_tests.cpp b/src/test/common_url_tests.cpp index 88bc17f22a8..eb92c2ceeea 100644 --- a/src/test/common_url_tests.cpp +++ b/src/test/common_url_tests.cpp @@ -11,60 +11,60 @@ BOOST_AUTO_TEST_SUITE(common_url_tests) // These test vectors were ported from test/regress.c in the libevent library -// which used to be a dependency of the urlDecode function. +// which used to be a dependency of the UrlDecode function. BOOST_AUTO_TEST_CASE(encode_decode_test) { - BOOST_CHECK_EQUAL(urlDecode("Hello"), "Hello"); - BOOST_CHECK_EQUAL(urlDecode("99"), "99"); - BOOST_CHECK_EQUAL(urlDecode(""), ""); - BOOST_CHECK_EQUAL(urlDecode("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789-.~_"), + BOOST_CHECK_EQUAL(UrlDecode("Hello"), "Hello"); + BOOST_CHECK_EQUAL(UrlDecode("99"), "99"); + BOOST_CHECK_EQUAL(UrlDecode(""), ""); + BOOST_CHECK_EQUAL(UrlDecode("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789-.~_"), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789-.~_"); - BOOST_CHECK_EQUAL(urlDecode("%20"), " "); - BOOST_CHECK_EQUAL(urlDecode("%FF%F0%E0"), "\xff\xf0\xe0"); - BOOST_CHECK_EQUAL(urlDecode("%01%19"), "\x01\x19"); - BOOST_CHECK_EQUAL(urlDecode("http%3A%2F%2Fwww.ietf.org%2Frfc%2Frfc3986.txt"), + BOOST_CHECK_EQUAL(UrlDecode("%20"), " "); + BOOST_CHECK_EQUAL(UrlDecode("%FF%F0%E0"), "\xff\xf0\xe0"); + BOOST_CHECK_EQUAL(UrlDecode("%01%19"), "\x01\x19"); + BOOST_CHECK_EQUAL(UrlDecode("http%3A%2F%2Fwww.ietf.org%2Frfc%2Frfc3986.txt"), "http://www.ietf.org/rfc/rfc3986.txt"); - BOOST_CHECK_EQUAL(urlDecode("1%2B2%3D3"), "1+2=3"); + BOOST_CHECK_EQUAL(UrlDecode("1%2B2%3D3"), "1+2=3"); } BOOST_AUTO_TEST_CASE(decode_malformed_test) { - BOOST_CHECK_EQUAL(urlDecode("%%xhello th+ere \xff"), "%%xhello th+ere \xff"); + BOOST_CHECK_EQUAL(UrlDecode("%%xhello th+ere \xff"), "%%xhello th+ere \xff"); - BOOST_CHECK_EQUAL(urlDecode("%"), "%"); - BOOST_CHECK_EQUAL(urlDecode("%%"), "%%"); - BOOST_CHECK_EQUAL(urlDecode("%%%"), "%%%"); - BOOST_CHECK_EQUAL(urlDecode("%%%%"), "%%%%"); + BOOST_CHECK_EQUAL(UrlDecode("%"), "%"); + BOOST_CHECK_EQUAL(UrlDecode("%%"), "%%"); + BOOST_CHECK_EQUAL(UrlDecode("%%%"), "%%%"); + BOOST_CHECK_EQUAL(UrlDecode("%%%%"), "%%%%"); - BOOST_CHECK_EQUAL(urlDecode("+"), "+"); - BOOST_CHECK_EQUAL(urlDecode("++"), "++"); + BOOST_CHECK_EQUAL(UrlDecode("+"), "+"); + BOOST_CHECK_EQUAL(UrlDecode("++"), "++"); - BOOST_CHECK_EQUAL(urlDecode("?"), "?"); - BOOST_CHECK_EQUAL(urlDecode("??"), "??"); + BOOST_CHECK_EQUAL(UrlDecode("?"), "?"); + BOOST_CHECK_EQUAL(UrlDecode("??"), "??"); - BOOST_CHECK_EQUAL(urlDecode("%G1"), "%G1"); - BOOST_CHECK_EQUAL(urlDecode("%2"), "%2"); - BOOST_CHECK_EQUAL(urlDecode("%ZX"), "%ZX"); + BOOST_CHECK_EQUAL(UrlDecode("%G1"), "%G1"); + BOOST_CHECK_EQUAL(UrlDecode("%2"), "%2"); + BOOST_CHECK_EQUAL(UrlDecode("%ZX"), "%ZX"); - BOOST_CHECK_EQUAL(urlDecode("valid%20string%G1"), "valid string%G1"); - BOOST_CHECK_EQUAL(urlDecode("%20invalid%ZX"), " invalid%ZX"); - BOOST_CHECK_EQUAL(urlDecode("%20%G1%ZX"), " %G1%ZX"); + BOOST_CHECK_EQUAL(UrlDecode("valid%20string%G1"), "valid string%G1"); + BOOST_CHECK_EQUAL(UrlDecode("%20invalid%ZX"), " invalid%ZX"); + BOOST_CHECK_EQUAL(UrlDecode("%20%G1%ZX"), " %G1%ZX"); - BOOST_CHECK_EQUAL(urlDecode("%1 "), "%1 "); - BOOST_CHECK_EQUAL(urlDecode("% 9"), "% 9"); - BOOST_CHECK_EQUAL(urlDecode(" %Z "), " %Z "); - BOOST_CHECK_EQUAL(urlDecode(" % X"), " % X"); + BOOST_CHECK_EQUAL(UrlDecode("%1 "), "%1 "); + BOOST_CHECK_EQUAL(UrlDecode("% 9"), "% 9"); + BOOST_CHECK_EQUAL(UrlDecode(" %Z "), " %Z "); + BOOST_CHECK_EQUAL(UrlDecode(" % X"), " % X"); - BOOST_CHECK_EQUAL(urlDecode("%-1"), "%-1"); - BOOST_CHECK_EQUAL(urlDecode("%1-"), "%1-"); + BOOST_CHECK_EQUAL(UrlDecode("%-1"), "%-1"); + BOOST_CHECK_EQUAL(UrlDecode("%1-"), "%1-"); } BOOST_AUTO_TEST_CASE(decode_lowercase_hex_test) { - BOOST_CHECK_EQUAL(urlDecode("%f0%a0%b0"), "\xf0\xa0\xb0"); + BOOST_CHECK_EQUAL(UrlDecode("%f0%a0%b0"), "\xf0\xa0\xb0"); } BOOST_AUTO_TEST_CASE(decode_internal_nulls_test) { - BOOST_CHECK_EQUAL(urlDecode("%00%00x%00%00"), ""); - BOOST_CHECK_EQUAL(urlDecode("abc%00%00"), "abc"); + BOOST_CHECK_EQUAL(UrlDecode("%00%00x%00%00"), ""); + BOOST_CHECK_EQUAL(UrlDecode("abc%00%00"), "abc"); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/fuzz/string.cpp b/src/test/fuzz/string.cpp index e81efac6e0d..631da13803a 100644 --- a/src/test/fuzz/string.cpp +++ b/src/test/fuzz/string.cpp @@ -90,7 +90,7 @@ FUZZ_TARGET(string) (void)ToUpper(random_string_1); (void)TrimString(random_string_1); (void)TrimString(random_string_1, random_string_2); - (void)urlDecode(random_string_1); + (void)UrlDecode(random_string_1); (void)ContainsNoNUL(random_string_1); (void)_(random_string_1.c_str()); try { diff --git a/src/wallet/rpc/util.cpp b/src/wallet/rpc/util.cpp index eb081e765ad..1252843e9d0 100644 --- a/src/wallet/rpc/util.cpp +++ b/src/wallet/rpc/util.cpp @@ -64,7 +64,7 @@ bool GetWalletNameFromJSONRPCRequest(const JSONRPCRequest& request, std::string& { if (request.URI.starts_with(WALLET_ENDPOINT_BASE)) { // wallet endpoint was used - wallet_name = urlDecode(std::string_view{request.URI}.substr(WALLET_ENDPOINT_BASE.size())); + wallet_name = UrlDecode(std::string_view{request.URI}.substr(WALLET_ENDPOINT_BASE.size())); return true; } return false;