summaryrefslogtreecommitdiff
path: root/Source/Core/Common/NandPaths.cpp
diff options
context:
space:
mode:
authorJosJuice <josjuice@gmail.com>2021-05-31 19:46:26 +0200
committerGitHub <noreply@github.com>2021-05-31 19:46:26 +0200
commitc404452d3edcd3d0cf2d870a51166c6f6132a5e1 (patch)
tree619f4f1d7c37c484377440d05984d47e6d666457 /Source/Core/Common/NandPaths.cpp
parentc648058efd4383b2ac69c4ed52e98973b735321b (diff)
parent1377f31cf8cd841ceeb1a7fce2f070a9f4049cea (diff)
Merge pull request #9759 from Techjar/netplay-sanitize-gci
NetPlay: Check file names when receiving GCI folder
Diffstat (limited to 'Source/Core/Common/NandPaths.cpp')
-rw-r--r--Source/Core/Common/NandPaths.cpp18
1 files changed, 15 insertions, 3 deletions
diff --git a/Source/Core/Common/NandPaths.cpp b/Source/Core/Common/NandPaths.cpp
index ac57ccdcdc..502146f2d6 100644
--- a/Source/Core/Common/NandPaths.cpp
+++ b/Source/Core/Common/NandPaths.cpp
@@ -95,6 +95,13 @@ bool IsTitlePath(const std::string& path, std::optional<FromWhichRoot> from, u64
return true;
}
+static bool IsIllegalCharacter(char c)
+{
+ static const std::unordered_set<char> illegal_chars = {'\"', '*', '/', ':', '<',
+ '>', '?', '\\', '|', '\x7f'};
+ return (c >= 0 && c <= 0x1F) || illegal_chars.find(c) != illegal_chars.end();
+}
+
std::string EscapeFileName(const std::string& filename)
{
// Prevent paths from containing special names like ., .., ..., ...., and so on
@@ -105,13 +112,11 @@ std::string EscapeFileName(const std::string& filename)
std::string filename_with_escaped_double_underscores = ReplaceAll(filename, "__", "__5f____5f__");
// Escape all other characters that need to be escaped
- static const std::unordered_set<char> chars_to_replace = {'\"', '*', '/', ':', '<',
- '>', '?', '\\', '|', '\x7f'};
std::string result;
result.reserve(filename_with_escaped_double_underscores.size());
for (char c : filename_with_escaped_double_underscores)
{
- if ((c >= 0 && c <= 0x1F) || chars_to_replace.find(c) != chars_to_replace.end())
+ if (IsIllegalCharacter(c))
result.append(fmt::format("__{:02x}__", c));
else
result.push_back(c);
@@ -151,4 +156,11 @@ std::string UnescapeFileName(const std::string& filename)
return result;
}
+
+bool IsFileNameSafe(const std::string_view filename)
+{
+ return !filename.empty() &&
+ !std::all_of(filename.begin(), filename.end(), [](char c) { return c == '.'; }) &&
+ std::none_of(filename.begin(), filename.end(), IsIllegalCharacter);
+}
} // namespace Common