2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2010-09-05 22:36:58 -06:00
|
|
|
|
2018-04-22 21:13:28 -06:00
|
|
|
#include "Common/NandPaths.h"
|
|
|
|
|
2016-11-26 07:39:00 -07:00
|
|
|
#include <algorithm>
|
2014-02-19 20:11:52 -07:00
|
|
|
#include <string>
|
2016-11-26 07:39:00 -07:00
|
|
|
#include <unordered_set>
|
2017-03-03 12:43:52 -07:00
|
|
|
#include <vector>
|
2010-09-06 06:14:18 -06:00
|
|
|
|
2019-06-14 08:53:46 -06:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2014-09-07 19:06:58 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/FileUtil.h"
|
2014-02-19 20:11:52 -07:00
|
|
|
#include "Common/StringUtil.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
|
2010-09-05 22:36:58 -06:00
|
|
|
namespace Common
|
|
|
|
{
|
2017-01-06 13:59:02 -07:00
|
|
|
std::string RootUserPath(FromWhichRoot from)
|
2015-06-21 11:19:52 -06:00
|
|
|
{
|
|
|
|
int idx = from == FROM_CONFIGURED_ROOT ? D_WIIROOT_IDX : D_SESSION_WIIROOT_IDX;
|
2021-11-21 19:20:17 -07:00
|
|
|
std::string dir = File::GetUserPath(idx);
|
|
|
|
dir.pop_back(); // remove trailing path separator
|
|
|
|
return dir;
|
2015-06-21 11:19:52 -06:00
|
|
|
}
|
|
|
|
|
2018-04-08 03:57:36 -06:00
|
|
|
static std::string RootUserPath(std::optional<FromWhichRoot> from)
|
|
|
|
{
|
|
|
|
return from ? RootUserPath(*from) : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetImportTitlePath(u64 title_id, std::optional<FromWhichRoot> from)
|
2017-03-05 06:04:35 -07:00
|
|
|
{
|
2019-06-14 08:53:46 -06:00
|
|
|
return RootUserPath(from) + fmt::format("/import/{:08x}/{:08x}", static_cast<u32>(title_id >> 32),
|
|
|
|
static_cast<u32>(title_id));
|
2017-03-05 06:04:35 -07:00
|
|
|
}
|
|
|
|
|
2018-04-08 03:57:36 -06:00
|
|
|
std::string GetTicketFileName(u64 title_id, std::optional<FromWhichRoot> from)
|
2010-09-05 22:36:58 -06:00
|
|
|
{
|
2019-06-14 08:53:46 -06:00
|
|
|
return fmt::format("{}/ticket/{:08x}/{:08x}.tik", RootUserPath(from),
|
|
|
|
static_cast<u32>(title_id >> 32), static_cast<u32>(title_id));
|
2010-09-05 22:36:58 -06:00
|
|
|
}
|
|
|
|
|
2018-04-08 03:57:36 -06:00
|
|
|
std::string GetTitlePath(u64 title_id, std::optional<FromWhichRoot> from)
|
2017-05-06 09:45:08 -06:00
|
|
|
{
|
2019-06-14 08:53:46 -06:00
|
|
|
return fmt::format("{}/title/{:08x}/{:08x}", RootUserPath(from), static_cast<u32>(title_id >> 32),
|
|
|
|
static_cast<u32>(title_id));
|
2017-05-06 09:45:08 -06:00
|
|
|
}
|
|
|
|
|
2018-04-08 03:57:36 -06:00
|
|
|
std::string GetTitleDataPath(u64 title_id, std::optional<FromWhichRoot> from)
|
2010-09-07 00:06:08 -06:00
|
|
|
{
|
2018-04-08 03:57:36 -06:00
|
|
|
return GetTitlePath(title_id, from) + "/data";
|
2010-09-07 00:06:08 -06:00
|
|
|
}
|
|
|
|
|
2018-04-08 03:57:36 -06:00
|
|
|
std::string GetTitleContentPath(u64 title_id, std::optional<FromWhichRoot> from)
|
2011-05-08 23:47:29 -06:00
|
|
|
{
|
2018-04-08 03:57:36 -06:00
|
|
|
return GetTitlePath(title_id, from) + "/content";
|
2011-05-08 23:47:29 -06:00
|
|
|
}
|
2017-05-06 09:45:08 -06:00
|
|
|
|
2018-04-08 03:57:36 -06:00
|
|
|
std::string GetTMDFileName(u64 title_id, std::optional<FromWhichRoot> from)
|
2010-09-05 22:36:58 -06:00
|
|
|
{
|
2018-04-08 03:57:36 -06:00
|
|
|
return GetTitleContentPath(title_id, from) + "/title.tmd";
|
2017-08-06 09:11:25 -06:00
|
|
|
}
|
|
|
|
|
2019-04-08 05:06:21 -06:00
|
|
|
std::string GetMiiDatabasePath(std::optional<FromWhichRoot> from)
|
|
|
|
{
|
2019-06-14 08:53:46 -06:00
|
|
|
return fmt::format("{}/shared2/menu/FaceLib/RFL_DB.dat", RootUserPath(from));
|
2019-04-08 05:06:21 -06:00
|
|
|
}
|
|
|
|
|
2018-04-08 03:57:36 -06:00
|
|
|
bool IsTitlePath(const std::string& path, std::optional<FromWhichRoot> from, u64* title_id)
|
2017-08-06 09:11:25 -06:00
|
|
|
{
|
|
|
|
std::string expected_prefix = RootUserPath(from) + "/title/";
|
|
|
|
if (!StringBeginsWith(path, expected_prefix))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find a title ID in the remaining path.
|
|
|
|
std::string subdirectory = path.substr(expected_prefix.size());
|
|
|
|
std::vector<std::string> components = SplitString(subdirectory, '/');
|
|
|
|
if (components.size() < 2)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 title_id_high, title_id_low;
|
|
|
|
if (!AsciiToHex(components[0], title_id_high) || !AsciiToHex(components[1], title_id_low))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (title_id != nullptr)
|
|
|
|
{
|
|
|
|
*title_id = (static_cast<u64>(title_id_high) << 32) | title_id_low;
|
|
|
|
}
|
|
|
|
return true;
|
2010-09-05 22:36:58 -06:00
|
|
|
}
|
|
|
|
|
2021-05-28 05:11:52 -06:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2016-11-26 07:39:00 -07:00
|
|
|
std::string EscapeFileName(const std::string& filename)
|
2010-12-21 17:48:59 -07:00
|
|
|
{
|
2016-11-26 07:39:00 -07:00
|
|
|
// Prevent paths from containing special names like ., .., ..., ...., and so on
|
|
|
|
if (std::all_of(filename.begin(), filename.end(), [](char c) { return c == '.'; }))
|
|
|
|
return ReplaceAll(filename, ".", "__2e__");
|
|
|
|
|
|
|
|
// Escape all double underscores since we will use double underscores for our escape sequences
|
|
|
|
std::string filename_with_escaped_double_underscores = ReplaceAll(filename, "__", "__5f____5f__");
|
|
|
|
|
|
|
|
// Escape all other characters that need to be escaped
|
|
|
|
std::string result;
|
|
|
|
result.reserve(filename_with_escaped_double_underscores.size());
|
|
|
|
for (char c : filename_with_escaped_double_underscores)
|
|
|
|
{
|
2021-05-28 05:11:52 -06:00
|
|
|
if (IsIllegalCharacter(c))
|
2019-06-14 08:53:46 -06:00
|
|
|
result.append(fmt::format("__{:02x}__", c));
|
2016-11-26 07:39:00 -07:00
|
|
|
else
|
|
|
|
result.push_back(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string EscapePath(const std::string& path)
|
|
|
|
{
|
2017-06-11 08:33:10 -06:00
|
|
|
const std::vector<std::string> split_strings = SplitString(path, '/');
|
2016-11-26 07:39:00 -07:00
|
|
|
|
|
|
|
std::vector<std::string> escaped_split_strings;
|
|
|
|
escaped_split_strings.reserve(split_strings.size());
|
|
|
|
for (const std::string& split_string : split_strings)
|
|
|
|
escaped_split_strings.push_back(EscapeFileName(split_string));
|
|
|
|
|
|
|
|
return JoinStrings(escaped_split_strings, "/");
|
2010-12-21 17:48:59 -07:00
|
|
|
}
|
|
|
|
|
2016-11-26 07:39:00 -07:00
|
|
|
std::string UnescapeFileName(const std::string& filename)
|
2010-12-21 17:48:59 -07:00
|
|
|
{
|
2016-11-26 07:39:00 -07:00
|
|
|
std::string result = filename;
|
|
|
|
size_t pos = 0;
|
2010-12-21 17:48:59 -07:00
|
|
|
|
2016-11-26 07:39:00 -07:00
|
|
|
// Replace escape sequences of the format "__3f__" with the ASCII
|
|
|
|
// character defined by the escape sequence's two hex digits.
|
|
|
|
while ((pos = result.find("__", pos)) != std::string::npos)
|
|
|
|
{
|
|
|
|
u32 character;
|
|
|
|
if (pos + 6 <= result.size() && result[pos + 4] == '_' && result[pos + 5] == '_')
|
|
|
|
if (AsciiToHex(result.substr(pos + 2, 2), character))
|
|
|
|
result.replace(pos, 6, {static_cast<char>(character)});
|
2010-12-21 17:48:59 -07:00
|
|
|
|
2016-11-26 07:39:00 -07:00
|
|
|
++pos;
|
|
|
|
}
|
2010-12-21 17:48:59 -07:00
|
|
|
|
2016-11-26 07:39:00 -07:00
|
|
|
return result;
|
2010-12-21 17:48:59 -07:00
|
|
|
}
|
2021-05-28 05:11:52 -06:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2019-04-08 05:06:21 -06:00
|
|
|
} // namespace Common
|