Debian Codegraph @

Source/Core/Common/NandPaths.cpp

raw symbols SCIP
1 // Copyright 2008 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "Common/NandPaths.h"
5
6 #include <algorithm>
7 #include <string>
8 #include <vector>
9
10 #include <fmt/format.h>
11 #include <fmt/ranges.h>
12
13 #include "Common/CommonTypes.h"
14 #include "Common/Contains.h"
15 #include "Common/FileUtil.h"
16 #include "Common/StringUtil.h"
17
18 namespace Common
19 {
20 std::string RootUserPath(FromWhichRoot from)
21 {
22 int idx{};
23 switch (from)
24 {
25 case FromWhichRoot::Configured:
26 idx = D_WIIROOT_IDX;
27 break;
28 case FromWhichRoot::Session:
29 idx = D_SESSION_WIIROOT_IDX;
30 break;
31 case FromWhichRoot::Banners:
32 idx = D_BANNERS_WIIROOT_IDX;
33 break;
34 }
35 std::string dir = File::GetUserPath(idx);
36 dir.pop_back(); // remove trailing path separator
37 return dir;
38 }
39
40 static std::string RootUserPath(std::optional<FromWhichRoot> from)
41 {
42 return from ? RootUserPath(*from) : "";
43 }
44
45 std::string GetImportTitlePath(u64 title_id, std::optional<FromWhichRoot> from)
46 {
47 return RootUserPath(from) + fmt::format("/import/{:08x}/{:08x}", static_cast<u32>(title_id >> 32),
48 static_cast<u32>(title_id));
49 }
50
51 std::string GetTicketFileName(u64 title_id, std::optional<FromWhichRoot> from)
52 {
53 return fmt::format("{}/ticket/{:08x}/{:08x}.tik", RootUserPath(from),
54 static_cast<u32>(title_id >> 32), static_cast<u32>(title_id));
55 }
56
57 std::string GetV1TicketFileName(u64 title_id, std::optional<FromWhichRoot> from)
58 {
59 return fmt::format("{}/ticket/{:08x}/{:08x}.tv1", RootUserPath(from),
60 static_cast<u32>(title_id >> 32), static_cast<u32>(title_id));
61 }
62
63 std::string GetTitlePath(u64 title_id, std::optional<FromWhichRoot> from)
64 {
65 return fmt::format("{}/title/{:08x}/{:08x}", RootUserPath(from), static_cast<u32>(title_id >> 32),
66 static_cast<u32>(title_id));
67 }
68
69 std::string GetTitleDataPath(u64 title_id, std::optional<FromWhichRoot> from)
70 {
71 return GetTitlePath(title_id, from) + "/data";
72 }
73
74 std::string GetTitleContentPath(u64 title_id, std::optional<FromWhichRoot> from)
75 {
76 return GetTitlePath(title_id, from) + "/content";
77 }
78
79 std::string GetTMDFileName(u64 title_id, std::optional<FromWhichRoot> from)
80 {
81 return GetTitleContentPath(title_id, from) + "/title.tmd";
82 }
83
84 std::string GetMiiDatabasePath(std::optional<FromWhichRoot> from)
85 {
86 return fmt::format("{}/shared2/menu/FaceLib/RFL_DB.dat", RootUserPath(from));
87 }
88
89 bool IsTitlePath(const std::string& path, std::optional<FromWhichRoot> from, u64* title_id)
90 {
91 std::string expected_prefix = RootUserPath(from) + "/title/";
92 if (!path.starts_with(expected_prefix))
93 {
94 return false;
95 }
96
97 // Try to find a title ID in the remaining path.
98 std::string subdirectory = path.substr(expected_prefix.size());
99 std::vector<std::string> components = SplitString(subdirectory, '/');
100 if (components.size() < 2)
101 {
102 return false;
103 }
104
105 u32 title_id_high, title_id_low;
106 if (Common::FromChars(components[0], title_id_high, 16).ec != std::errc{} ||
107 Common::FromChars(components[1], title_id_low, 16).ec != std::errc{})
108 {
109 return false;
110 }
111
112 if (title_id != nullptr)
113 {
114 *title_id = (static_cast<u64>(title_id_high) << 32) | title_id_low;
115 }
116 return true;
117 }
118
119 static bool IsIllegalCharacter(char c)
120 {
121 static constexpr char illegal_chars[] = {'\"', '*', '/', ':', '<', '>', '?', '\\', '|', '\x7f'};
122 return static_cast<unsigned char>(c) <= 0x1F || Common::Contains(illegal_chars, c);
123 }
124
125 std::string EscapeFileName(const std::string& filename)
126 {
127 // Prevent paths from containing special names like ., .., ..., ...., and so on
128 if (std::ranges::all_of(filename, [](char c) { return c == '.'; }))
129 return ReplaceAll(filename, ".", "__2e__");
130
131 // Escape all double underscores since we will use double underscores for our escape sequences
132 std::string filename_with_escaped_double_underscores = ReplaceAll(filename, "__", "__5f____5f__");
133
134 // Escape all other characters that need to be escaped
135 std::string result;
136 result.reserve(filename_with_escaped_double_underscores.size());
137 for (char c : filename_with_escaped_double_underscores)
138 {
139 if (IsIllegalCharacter(c))
140 result.append(fmt::format("__{:02x}__", c));
141 else
142 result.push_back(c);
143 }
144
145 return result;
146 }
147
148 std::string EscapePath(const std::string& path)
149 {
150 const std::vector<std::string> split_strings = SplitString(path, '/');
151
152 std::vector<std::string> escaped_split_strings;
153 escaped_split_strings.reserve(split_strings.size());
154 for (const std::string& split_string : split_strings)
155 escaped_split_strings.push_back(EscapeFileName(split_string));
156
157 return fmt::to_string(fmt::join(escaped_split_strings, "/"));
158 }
159
160 std::string UnescapeFileName(const std::string& filename)
161 {
162 std::string result = filename;
163 size_t pos = 0;
164
165 // Replace escape sequences of the format "__3f__" with the ASCII
166 // character defined by the escape sequence's two hex digits.
167 while ((pos = result.find("__", pos)) != std::string::npos)
168 {
169 u32 character;
170 if (pos + 6 <= result.size() && result[pos + 4] == '_' && result[pos + 5] == '_')
171 if (Common::FromChars(std::string_view{result}.substr(pos + 2, 2), character, 16).ec ==
172 std::errc{})
173 {
174 result.replace(pos, 6, {static_cast<char>(character)});
175 }
176
177 ++pos;
178 }
179
180 return result;
181 }
182
183 bool IsFileNameSafe(const std::string_view filename)
184 {
185 return !filename.empty() && !std::ranges::all_of(filename, [](char c) { return c == '.'; }) &&
186 std::ranges::none_of(filename, IsIllegalCharacter);
187 }
188 } // namespace Common