From eb5128aa6ce4a33aa9021b51231d0934294c7caa Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Mon, 27 Feb 2017 23:42:54 +0300 Subject: Add string function for sanitization strings. --- src/utils/stringutils.cpp | 29 +++++++++ src/utils/stringutils.h | 9 ++- src/utils/stringutils_unittest.cc | 127 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp index bb7f7efb5..28b2c8e1e 100644 --- a/src/utils/stringutils.cpp +++ b/src/utils/stringutils.cpp @@ -385,6 +385,20 @@ std::string& replaceAll(std::string& context, return context; } +void replaceRecursiveAll(std::string& context, + const std::string &restrict from, + const char to) +{ + size_t lookHere = 0; + size_t foundHere; + const size_t fromSize = from.size(); + while ((foundHere = context.find(from, lookHere)) != std::string::npos) + { + context.replace(foundHere, fromSize, 1, to); + lookHere = foundHere; + } +} + bool getBoolFromString(const std::string &text) { std::string txt = text; @@ -1015,6 +1029,21 @@ std::string escapeString(std::string str) return "\"" + str + "\""; } +void sanitizePath(std::string &path) +{ +#ifdef WIN32 + const char sepStr = '\\'; + const std::string sep2Str = "\\\\"; + const std::string sepWrongStr = "/"; +#else + const char sepStr = '/'; + const std::string sep2Str = "//"; + const std::string sepWrongStr = "\\"; +#endif + replaceRecursiveAll(path, sepWrongStr, sepStr); + replaceRecursiveAll(path, sep2Str, sepStr); +} + #ifndef DYECMD void replaceItemLinks(std::string &msg) { diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h index f9d7494aa..4db1041ef 100644 --- a/src/utils/stringutils.h +++ b/src/utils/stringutils.h @@ -163,9 +163,14 @@ std::string getFileName(const std::string &path) A_WARN_UNUSED; std::string getFileDir(const std::string &path) A_WARN_UNUSED; -std::string& replaceAll(std::string& context, const std::string &restrict from, +std::string& replaceAll(std::string& context, + const std::string &restrict from, const std::string &restrict to); +void replaceRecursiveAll(std::string& context, + const std::string &restrict from, + const char to); + /** * Returns a bool value depending on the given string value. * @@ -268,4 +273,6 @@ void replaceItemLinks(std::string &msg); std::string escapeString(std::string str); +void sanitizePath(std::string &path); + #endif // UTILS_STRINGUTILS_H diff --git a/src/utils/stringutils_unittest.cc b/src/utils/stringutils_unittest.cc index 60588571a..82a15fdff 100644 --- a/src/utils/stringutils_unittest.cc +++ b/src/utils/stringutils_unittest.cc @@ -545,6 +545,50 @@ TEST_CASE("stringuntils replaceAll 1") REQUIRE("this is test line" == replaceAll(str1, str2, str3)); } +TEST_CASE("stringuntils replaceRecursiveAll 1") +{ + std::string str; + str = ""; + replaceRecursiveAll(str, "line", '.'); + REQUIRE(str == ""); + str = "test line"; + replaceRecursiveAll(str, "line", '.'); + REQUIRE(str == "test ."); + str = "11112222"; + replaceRecursiveAll(str, "11", '1'); + REQUIRE(str == "12222"); + str = "122221"; + replaceRecursiveAll(str, "11", '1'); + REQUIRE(str == "122221"); + str = "1222211"; + replaceRecursiveAll(str, "11", '1'); + REQUIRE(str == "122221"); + str = "11112222"; + replaceRecursiveAll(str, "112", '1'); + REQUIRE(str == "111222"); + str = "111122224"; + replaceRecursiveAll(str, "112", '1'); + REQUIRE(str == "1112224"); + str = "3111122224"; + replaceRecursiveAll(str, "112", '1'); + REQUIRE(str == "31112224"); + str = "121212"; + replaceRecursiveAll(str, "12", '1'); + REQUIRE(str == "111"); + str = "1121212"; + replaceRecursiveAll(str, "12", '1'); + REQUIRE(str == "1111"); + str = "11212122"; + replaceRecursiveAll(str, "12", '1'); + REQUIRE(str == "1111"); + str = "112121222"; + replaceRecursiveAll(str, "12", '1'); + REQUIRE(str == "1111"); + str = "112211221122"; + replaceRecursiveAll(str, "12", '1'); + REQUIRE(str == "111111"); +} + TEST_CASE("stringuntils getBoolFromString 1") { REQUIRE(getBoolFromString("true")); @@ -1209,6 +1253,89 @@ TEST_CASE("stringuntils escapeString") REQUIRE(escapeString("12\\3") == "\"12\\3\""); } +TEST_CASE("stringuntils sanitizePath") +{ + std::string path; + path = ""; + sanitizePath(path); + REQUIRE(path == ""); + path = "/"; + sanitizePath(path); + REQUIRE(path == "/"); + path = "/\\"; + sanitizePath(path); + REQUIRE(path == "/"); + path = "\\/"; + sanitizePath(path); + REQUIRE(path == "/"); + path = "//"; + sanitizePath(path); + REQUIRE(path == "/"); + path = "///"; + sanitizePath(path); + REQUIRE(path == "/"); + path = "//\\/"; + sanitizePath(path); + REQUIRE(path == "/"); + path = "///\\"; + sanitizePath(path); + REQUIRE(path == "/"); + path = "\\"; + sanitizePath(path); + REQUIRE(path == "/"); + path = "\\\\"; + sanitizePath(path); + REQUIRE(path == "/"); + path = "\\/\\"; + sanitizePath(path); + REQUIRE(path == "/"); + path = "\\\\/"; + sanitizePath(path); + REQUIRE(path == "/"); + path = "test"; + sanitizePath(path); + REQUIRE(path == "test"); + path = "./test"; + sanitizePath(path); + REQUIRE(path == "./test"); + path = "test line"; + sanitizePath(path); + REQUIRE(path == "test line"); + path = "dir/test"; + sanitizePath(path); + REQUIRE(path == "dir/test"); + path = "/dir/test"; + sanitizePath(path); + REQUIRE(path == "/dir/test"); + path = "dir//test"; + sanitizePath(path); + REQUIRE(path == "dir/test"); + path = "dir///test"; + sanitizePath(path); + REQUIRE(path == "dir/test"); + path = "dir///\\test"; + sanitizePath(path); + REQUIRE(path == "dir/test"); + path = "dir/\\//test"; + sanitizePath(path); + REQUIRE(path == "dir/test"); + path = "dir\\test"; + sanitizePath(path); + REQUIRE(path == "dir/test"); + path = "dir/test/"; + sanitizePath(path); + REQUIRE(path == "dir/test/"); + path = "dir/test\\"; + sanitizePath(path); + REQUIRE(path == "dir/test/"); + path = "/very\\long/dir\\with\\sepa/ra/tors"; + sanitizePath(path); + REQUIRE(path == "/very/long/dir/with/sepa/ra/tors"); + path = "/very\\long/dir\\\\with\\sepa//ra/tors"; + sanitizePath(path); + REQUIRE(path == "/very/long/dir/with/sepa/ra/tors"); +} + TEST_CASE("stringuntils secureChatCommand") { std::string str; -- cgit v1.2.3-70-g09d2