| 
									
										
										
										
											2015-05-24 06:55:12 +02:00
										 |  |  | // Copyright 2008 Dolphin Emulator Project
 | 
					
						
							| 
									
										
										
										
											2015-05-18 01:08:10 +02:00
										 |  |  | // Licensed under GPLv2+
 | 
					
						
							| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2010-09-06 04:36:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-26 15:39:00 +01:00
										 |  |  | #include <algorithm>
 | 
					
						
							| 
									
										
										
										
											2014-02-20 04:11:52 +01:00
										 |  |  | #include <string>
 | 
					
						
							| 
									
										
										
										
											2016-11-26 15:39:00 +01:00
										 |  |  | #include <unordered_set>
 | 
					
						
							| 
									
										
										
										
											2014-02-20 04:11:52 +01:00
										 |  |  | #include <utility>
 | 
					
						
							| 
									
										
										
										
											2017-03-03 14:43:52 -05:00
										 |  |  | #include <vector>
 | 
					
						
							| 
									
										
										
										
											2010-09-06 12:14:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-07 20:06:58 -05:00
										 |  |  | #include "Common/CommonTypes.h"
 | 
					
						
							| 
									
										
										
										
											2017-01-15 21:46:32 +01:00
										 |  |  | #include "Common/File.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "Common/FileUtil.h"
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | #include "Common/Logging/Log.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "Common/NandPaths.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-20 04:11:52 +01:00
										 |  |  | #include "Common/StringUtil.h"
 | 
					
						
							| 
									
										
										
										
											2017-03-03 14:43:52 -05:00
										 |  |  | #include "Common/Swap.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-06 04:36:58 +00:00
										 |  |  | namespace Common | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-01-06 21:59:02 +01:00
										 |  |  | std::string RootUserPath(FromWhichRoot from) | 
					
						
							| 
									
										
										
										
											2015-06-21 13:19:52 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   int idx = from == FROM_CONFIGURED_ROOT ? D_WIIROOT_IDX : D_SESSION_WIIROOT_IDX; | 
					
						
							|  |  |  |   return File::GetUserPath(idx); | 
					
						
							| 
									
										
										
										
											2015-06-21 13:19:52 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 14:04:35 +01:00
										 |  |  | std::string GetImportTitlePath(u64 title_id, FromWhichRoot from) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return RootUserPath(from) + StringFromFormat("/import/%08x/%08x", | 
					
						
							|  |  |  |                                                static_cast<u32>(title_id >> 32), | 
					
						
							|  |  |  |                                                static_cast<u32>(title_id)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-21 13:19:52 -04:00
										 |  |  | std::string GetTicketFileName(u64 _titleID, FromWhichRoot from) | 
					
						
							| 
									
										
										
										
											2010-09-06 04:36:58 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return StringFromFormat("%s/ticket/%08x/%08x.tik", RootUserPath(from).c_str(), | 
					
						
							|  |  |  |                           (u32)(_titleID >> 32), (u32)_titleID); | 
					
						
							| 
									
										
										
										
											2010-09-06 04:36:58 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-06 17:45:08 +02:00
										 |  |  | std::string GetTitlePath(u64 title_id, FromWhichRoot from) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return StringFromFormat("%s/title/%08x/%08x/", RootUserPath(from).c_str(), | 
					
						
							|  |  |  |                           static_cast<u32>(title_id >> 32), static_cast<u32>(title_id)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-21 13:19:52 -04:00
										 |  |  | std::string GetTitleDataPath(u64 _titleID, FromWhichRoot from) | 
					
						
							| 
									
										
										
										
											2010-09-07 06:06:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-05-06 17:45:08 +02:00
										 |  |  |   return GetTitlePath(_titleID, from) + "data/"; | 
					
						
							| 
									
										
										
										
											2010-09-07 06:06:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-06 17:51:50 +02:00
										 |  |  | std::string GetTitleContentPath(u64 _titleID, FromWhichRoot from) | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-05-06 17:51:50 +02:00
										 |  |  |   return GetTitlePath(_titleID, from) + "content/"; | 
					
						
							| 
									
										
										
										
											2011-05-09 05:47:29 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-05-06 17:45:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-06 17:51:50 +02:00
										 |  |  | std::string GetTMDFileName(u64 _titleID, FromWhichRoot from) | 
					
						
							| 
									
										
										
										
											2010-09-06 04:36:58 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-05-06 17:51:50 +02:00
										 |  |  |   return GetTitleContentPath(_titleID, from) + "title.tmd"; | 
					
						
							| 
									
										
										
										
											2017-08-06 17:11:25 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool IsTitlePath(const std::string& path, FromWhichRoot from, u64* title_id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   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-06 04:36:58 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-26 15:39:00 +01:00
										 |  |  | std::string EscapeFileName(const std::string& filename) | 
					
						
							| 
									
										
										
										
											2010-12-22 00:48:59 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-11-26 15:39:00 +01: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
 | 
					
						
							|  |  |  |   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()) | 
					
						
							|  |  |  |       result.append(StringFromFormat("__%02x__", c)); | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       result.push_back(c); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | std::string EscapePath(const std::string& path) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-06-11 16:33:10 +02:00
										 |  |  |   const std::vector<std::string> split_strings = SplitString(path, '/'); | 
					
						
							| 
									
										
										
										
											2016-11-26 15:39:00 +01: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-22 00:48:59 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-26 15:39:00 +01:00
										 |  |  | std::string UnescapeFileName(const std::string& filename) | 
					
						
							| 
									
										
										
										
											2010-12-22 00:48:59 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-11-26 15:39:00 +01:00
										 |  |  |   std::string result = filename; | 
					
						
							|  |  |  |   size_t pos = 0; | 
					
						
							| 
									
										
										
										
											2010-12-22 00:48:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-26 15:39:00 +01: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-22 00:48:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-26 15:39:00 +01:00
										 |  |  |     ++pos; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-12-22 00:48:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-26 15:39:00 +01:00
										 |  |  |   return result; | 
					
						
							| 
									
										
										
										
											2010-12-22 00:48:59 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-09-11 13:00:40 -04:00
										 |  |  | } |