FileSystemGCWii.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // Copyright 2008 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "DiscIO/FileSystemGCWii.h"
  4. #include <algorithm>
  5. #include <cstddef>
  6. #include <cstring>
  7. #include <locale>
  8. #include <map>
  9. #include <memory>
  10. #include <optional>
  11. #include <string>
  12. #include <string_view>
  13. #include <vector>
  14. #include "Common/CommonFuncs.h"
  15. #include "Common/CommonTypes.h"
  16. #include "Common/IOFile.h"
  17. #include "Common/Logging/Log.h"
  18. #include "Common/MsgHandler.h"
  19. #include "Common/StringUtil.h"
  20. #include "DiscIO/DiscUtils.h"
  21. #include "DiscIO/Filesystem.h"
  22. #include "DiscIO/VolumeDisc.h"
  23. namespace DiscIO
  24. {
  25. constexpr u32 FST_ENTRY_SIZE = 4 * 3; // An FST entry consists of three 32-bit integers
  26. // Set everything manually.
  27. FileInfoGCWii::FileInfoGCWii(const u8* fst, u8 offset_shift, u32 index, u32 total_file_infos)
  28. : m_fst(fst), m_offset_shift(offset_shift), m_index(index), m_total_file_infos(total_file_infos)
  29. {
  30. }
  31. // For the root object only.
  32. // m_fst and m_index must be correctly set before GetSize() is called!
  33. FileInfoGCWii::FileInfoGCWii(const u8* fst, u8 offset_shift)
  34. : m_fst(fst), m_offset_shift(offset_shift), m_index(0), m_total_file_infos(GetSize())
  35. {
  36. }
  37. // Copy data that is common to the whole file system.
  38. FileInfoGCWii::FileInfoGCWii(const FileInfoGCWii& file_info, u32 index)
  39. : FileInfoGCWii(file_info.m_fst, file_info.m_offset_shift, index, file_info.m_total_file_infos)
  40. {
  41. }
  42. FileInfoGCWii::~FileInfoGCWii() = default;
  43. uintptr_t FileInfoGCWii::GetAddress() const
  44. {
  45. return reinterpret_cast<uintptr_t>(m_fst + FST_ENTRY_SIZE * m_index);
  46. }
  47. u32 FileInfoGCWii::GetNextIndex() const
  48. {
  49. return IsDirectory() ? GetSize() : m_index + 1;
  50. }
  51. FileInfo& FileInfoGCWii::operator++()
  52. {
  53. m_index = GetNextIndex();
  54. return *this;
  55. }
  56. std::unique_ptr<FileInfo> FileInfoGCWii::clone() const
  57. {
  58. return std::make_unique<FileInfoGCWii>(*this);
  59. }
  60. FileInfo::const_iterator FileInfoGCWii::begin() const
  61. {
  62. return const_iterator(std::make_unique<FileInfoGCWii>(*this, m_index + 1));
  63. }
  64. FileInfo::const_iterator FileInfoGCWii::end() const
  65. {
  66. return const_iterator(std::make_unique<FileInfoGCWii>(*this, GetNextIndex()));
  67. }
  68. u32 FileInfoGCWii::Get(EntryProperty entry_property) const
  69. {
  70. return Common::swap32(m_fst + FST_ENTRY_SIZE * m_index +
  71. sizeof(u32) * static_cast<int>(entry_property));
  72. }
  73. u32 FileInfoGCWii::GetSize() const
  74. {
  75. return Get(EntryProperty::FILE_SIZE);
  76. }
  77. u64 FileInfoGCWii::GetOffset() const
  78. {
  79. return static_cast<u64>(Get(EntryProperty::FILE_OFFSET)) << m_offset_shift;
  80. }
  81. bool FileInfoGCWii::IsRoot() const
  82. {
  83. return m_index == 0;
  84. }
  85. bool FileInfoGCWii::IsDirectory() const
  86. {
  87. return (Get(EntryProperty::NAME_OFFSET) & 0xFF000000) != 0;
  88. }
  89. u32 FileInfoGCWii::GetTotalChildren() const
  90. {
  91. return Get(EntryProperty::FILE_SIZE) - (m_index + 1);
  92. }
  93. u64 FileInfoGCWii::GetNameOffset() const
  94. {
  95. return static_cast<u64>(FST_ENTRY_SIZE) * m_total_file_infos +
  96. (Get(EntryProperty::NAME_OFFSET) & 0xFFFFFF);
  97. }
  98. std::string FileInfoGCWii::GetName() const
  99. {
  100. // TODO: Should we really always use SHIFT-JIS?
  101. // Some names in Pikmin (NTSC-U) don't make sense without it, but is it correct?
  102. return SHIFTJISToUTF8(reinterpret_cast<const char*>(m_fst + GetNameOffset()));
  103. }
  104. bool FileInfoGCWii::NameCaseInsensitiveEquals(std::string_view other) const
  105. {
  106. // For speed, this function avoids allocating new strings, except when we are comparing
  107. // non-ASCII characters with non-ASCII characters, which is a rare case.
  108. const char* this_ptr = reinterpret_cast<const char*>(m_fst + GetNameOffset());
  109. const char* other_ptr = other.data();
  110. for (size_t i = 0; i < other.size(); ++i, ++this_ptr, ++other_ptr)
  111. {
  112. if (*this_ptr == '\0')
  113. {
  114. // A null byte in this is always a terminator and a null byte in other is never a terminator,
  115. // so if we reach this case, this is shorter than other
  116. return false;
  117. }
  118. else if (static_cast<unsigned char>(*this_ptr) >= 0x80 &&
  119. static_cast<unsigned char>(*other_ptr) >= 0x80)
  120. {
  121. // other is in UTF-8 and this is in Shift-JIS, so we convert so that we can compare correctly
  122. const std::string this_utf8 = SHIFTJISToUTF8(this_ptr);
  123. return std::equal(this_utf8.cbegin(), this_utf8.cend(), other.cbegin() + i, other.cend(),
  124. [](char a, char b) { return Common::ToLower(a) == Common::ToLower(b); });
  125. }
  126. else if (Common::ToLower(*this_ptr) != Common::ToLower(*other_ptr))
  127. {
  128. return false;
  129. }
  130. }
  131. return *this_ptr == '\0'; // If we're not at a null byte, this is longer than other
  132. }
  133. std::string FileInfoGCWii::GetPath() const
  134. {
  135. // The root entry doesn't have a name
  136. if (m_index == 0)
  137. return "";
  138. if (IsDirectory())
  139. {
  140. u32 parent_directory_index = Get(EntryProperty::FILE_OFFSET);
  141. return FileInfoGCWii(*this, parent_directory_index).GetPath() + GetName() + "/";
  142. }
  143. else
  144. {
  145. // The parent directory can be found by searching backwards
  146. // for a directory that contains this file. The search cannot fail,
  147. // because the root directory at index 0 contains all files.
  148. FileInfoGCWii potential_parent(*this, m_index - 1);
  149. while (!(potential_parent.IsDirectory() &&
  150. potential_parent.Get(EntryProperty::FILE_SIZE) > m_index))
  151. {
  152. potential_parent = FileInfoGCWii(*this, potential_parent.m_index - 1);
  153. }
  154. return potential_parent.GetPath() + GetName();
  155. }
  156. }
  157. bool FileInfoGCWii::IsValid(u64 fst_size, const FileInfoGCWii& parent_directory) const
  158. {
  159. if (GetNameOffset() >= fst_size)
  160. {
  161. ERROR_LOG_FMT(DISCIO, "Impossibly large name offset in file system");
  162. return false;
  163. }
  164. if (IsDirectory())
  165. {
  166. if (Get(EntryProperty::FILE_OFFSET) != parent_directory.m_index)
  167. {
  168. ERROR_LOG_FMT(DISCIO, "Incorrect parent offset in file system");
  169. return false;
  170. }
  171. const u32 size = Get(EntryProperty::FILE_SIZE);
  172. if (size <= m_index)
  173. {
  174. ERROR_LOG_FMT(DISCIO, "Impossibly small directory size in file system");
  175. return false;
  176. }
  177. if (size > parent_directory.Get(EntryProperty::FILE_SIZE))
  178. {
  179. ERROR_LOG_FMT(DISCIO, "Impossibly large directory size in file system");
  180. return false;
  181. }
  182. for (const FileInfo& child : *this)
  183. {
  184. if (!static_cast<const FileInfoGCWii&>(child).IsValid(fst_size, *this))
  185. return false;
  186. }
  187. }
  188. return true;
  189. }
  190. FileSystemGCWii::FileSystemGCWii(const VolumeDisc* volume, const Partition& partition)
  191. : m_valid(false), m_root(nullptr, 0, 0, 0)
  192. {
  193. u8 offset_shift;
  194. // Check if this is a GameCube or Wii disc
  195. if (volume->ReadSwapped<u32>(0x18, partition) == WII_DISC_MAGIC)
  196. offset_shift = 2; // Wii file system
  197. else if (volume->ReadSwapped<u32>(0x1c, partition) == GAMECUBE_DISC_MAGIC)
  198. offset_shift = 0; // GameCube file system
  199. else
  200. return; // Invalid partition (maybe someone removed its data but not its partition table entry)
  201. const std::optional<u64> fst_offset = GetFSTOffset(*volume, partition);
  202. const std::optional<u64> fst_size = GetFSTSize(*volume, partition);
  203. if (!fst_offset || !fst_size)
  204. return;
  205. if (*fst_size < FST_ENTRY_SIZE)
  206. {
  207. ERROR_LOG_FMT(DISCIO, "File system is too small");
  208. return;
  209. }
  210. // 128 MiB is more than the total amount of RAM in a Wii.
  211. // No file system should use anywhere near that much.
  212. static const u32 ARBITRARY_FILE_SYSTEM_SIZE_LIMIT = 128 * 1024 * 1024;
  213. if (*fst_size > ARBITRARY_FILE_SYSTEM_SIZE_LIMIT)
  214. {
  215. // Without this check, Dolphin can crash by trying to allocate too much
  216. // memory when loading a disc image with an incorrect FST size.
  217. ERROR_LOG_FMT(DISCIO, "File system is abnormally large! Aborting loading");
  218. return;
  219. }
  220. // Read the whole FST
  221. m_file_system_table.resize(*fst_size);
  222. if (!volume->Read(*fst_offset, *fst_size, m_file_system_table.data(), partition))
  223. {
  224. ERROR_LOG_FMT(DISCIO, "Couldn't read file system table");
  225. return;
  226. }
  227. // Create the root object
  228. m_root = FileInfoGCWii(m_file_system_table.data(), offset_shift);
  229. if (!m_root.IsDirectory())
  230. {
  231. ERROR_LOG_FMT(DISCIO, "File system root is not a directory");
  232. return;
  233. }
  234. if (FST_ENTRY_SIZE * m_root.GetSize() > *fst_size)
  235. {
  236. ERROR_LOG_FMT(DISCIO, "File system has too many entries for its size");
  237. return;
  238. }
  239. // If the FST's final byte isn't 0, FileInfoGCWii::GetName() can read past the end
  240. if (m_file_system_table[*fst_size - 1] != 0)
  241. {
  242. ERROR_LOG_FMT(DISCIO, "File system does not end with a null byte");
  243. return;
  244. }
  245. m_valid = m_root.IsValid(*fst_size, m_root);
  246. }
  247. FileSystemGCWii::~FileSystemGCWii() = default;
  248. const FileInfo& FileSystemGCWii::GetRoot() const
  249. {
  250. return m_root;
  251. }
  252. std::unique_ptr<FileInfo> FileSystemGCWii::FindFileInfo(std::string_view path) const
  253. {
  254. if (!IsValid())
  255. return nullptr;
  256. return FindFileInfo(path, m_root);
  257. }
  258. std::unique_ptr<FileInfo> FileSystemGCWii::FindFileInfo(std::string_view path,
  259. const FileInfo& file_info) const
  260. {
  261. // Given a path like "directory1/directory2/fileA.bin", this function will
  262. // find directory1 and then call itself to search for "directory2/fileA.bin".
  263. const size_t name_start = path.find_first_not_of('/');
  264. if (name_start == std::string::npos)
  265. return file_info.clone(); // We're done
  266. const size_t name_end = path.find('/', name_start);
  267. const std::string_view name = path.substr(name_start, name_end - name_start);
  268. const std::string_view rest_of_path =
  269. (name_end != std::string::npos) ? path.substr(name_end + 1) : "";
  270. for (const FileInfo& child : file_info)
  271. {
  272. // We need case insensitive comparison since some games have OPENING.BNR instead of opening.bnr
  273. if (child.NameCaseInsensitiveEquals(name))
  274. {
  275. // A match is found. The rest of the path is passed on to finish the search.
  276. std::unique_ptr<FileInfo> result = FindFileInfo(rest_of_path, child);
  277. // If the search wasn't successful, the loop continues, just in case there's a second
  278. // file info that matches searching_for (which probably won't happen in practice)
  279. if (result)
  280. return result;
  281. }
  282. }
  283. return nullptr;
  284. }
  285. std::unique_ptr<FileInfo> FileSystemGCWii::FindFileInfo(u64 disc_offset) const
  286. {
  287. if (!IsValid())
  288. return nullptr;
  289. // Build a cache (unless there already is one)
  290. if (m_offset_file_info_cache.empty())
  291. {
  292. u32 fst_entries = m_root.GetSize();
  293. for (u32 i = 0; i < fst_entries; i++)
  294. {
  295. FileInfoGCWii file_info(m_root, i);
  296. if (!file_info.IsDirectory())
  297. {
  298. const u32 size = file_info.GetSize();
  299. if (size != 0)
  300. m_offset_file_info_cache.emplace(file_info.GetOffset() + size, i);
  301. }
  302. }
  303. }
  304. // Get the first file that ends after disc_offset
  305. const auto it = m_offset_file_info_cache.upper_bound(disc_offset);
  306. if (it == m_offset_file_info_cache.end())
  307. return nullptr;
  308. std::unique_ptr<FileInfo> result(std::make_unique<FileInfoGCWii>(m_root, it->second));
  309. // If the file's start isn't after disc_offset, success
  310. if (result->GetOffset() <= disc_offset)
  311. return result;
  312. return nullptr;
  313. }
  314. } // namespace DiscIO