DirectoryBlob.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // Copyright 2008 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <cstddef>
  6. #include <functional>
  7. #include <map>
  8. #include <memory>
  9. #include <optional>
  10. #include <set>
  11. #include <string>
  12. #include <variant>
  13. #include <vector>
  14. #include "Common/CommonTypes.h"
  15. #include "Common/FileUtil.h"
  16. #include "DiscIO/Blob.h"
  17. #include "DiscIO/Volume.h"
  18. #include "DiscIO/WiiEncryptionCache.h"
  19. namespace File
  20. {
  21. struct FSTEntry;
  22. class IOFile;
  23. } // namespace File
  24. namespace DiscIO
  25. {
  26. enum class PartitionType : u32;
  27. class DirectoryBlobReader;
  28. class VolumeDisc;
  29. // Returns true if the path is inside a DirectoryBlob and doesn't represent the DirectoryBlob itself
  30. bool ShouldHideFromGameList(const std::string& volume_path);
  31. // Content chunk that is loaded from a file in the host file system.
  32. struct ContentFile
  33. {
  34. // Path where the file can be found.
  35. std::string m_filename;
  36. // Offset from the start of the file where the first byte of this content chunk is.
  37. u64 m_offset = 0;
  38. };
  39. // Content chunk that's just a direct block of memory
  40. typedef std::shared_ptr<std::vector<u8>> ContentMemory;
  41. // Content chunk that loads data from a DirectoryBlobReader.
  42. // Intented for representing a partition within a disc.
  43. struct ContentPartition
  44. {
  45. // Offset from the start of the partition for the first byte represented by this chunk.
  46. u64 m_offset = 0;
  47. // The value passed as partition_data_offset to EncryptPartitionData().
  48. u64 m_partition_data_offset = 0;
  49. };
  50. // Content chunk that loads data from a Volume.
  51. struct ContentVolume
  52. {
  53. // Offset from the start of the volume for the first byte represented by this chunk.
  54. u64 m_offset = 0;
  55. // The partition passed to the Volume's Read() method.
  56. Partition m_partition;
  57. };
  58. // Content chunk representing a run of identical bytes.
  59. // Useful for padding between chunks within a file.
  60. struct ContentFixedByte
  61. {
  62. u8 m_byte = 0;
  63. };
  64. using ContentSource = std::variant<ContentFile, // File
  65. ContentMemory, // Memory/Byte Sequence
  66. ContentPartition, // Partition
  67. ContentVolume, // Volume
  68. ContentFixedByte // Fixed value padding
  69. >;
  70. struct BuilderContentSource
  71. {
  72. u64 m_offset = 0;
  73. u64 m_size = 0;
  74. ContentSource m_source;
  75. };
  76. struct FSTBuilderNode
  77. {
  78. std::string m_filename;
  79. u64 m_size = 0;
  80. std::variant<std::vector<BuilderContentSource>, std::vector<FSTBuilderNode>> m_content;
  81. void* m_user_data = nullptr;
  82. bool IsFile() const
  83. {
  84. return std::holds_alternative<std::vector<BuilderContentSource>>(m_content);
  85. }
  86. std::vector<BuilderContentSource>& GetFileContent()
  87. {
  88. return std::get<std::vector<BuilderContentSource>>(m_content);
  89. }
  90. const std::vector<BuilderContentSource>& GetFileContent() const
  91. {
  92. return std::get<std::vector<BuilderContentSource>>(m_content);
  93. }
  94. bool IsFolder() const { return std::holds_alternative<std::vector<FSTBuilderNode>>(m_content); }
  95. std::vector<FSTBuilderNode>& GetFolderContent()
  96. {
  97. return std::get<std::vector<FSTBuilderNode>>(m_content);
  98. }
  99. const std::vector<FSTBuilderNode>& GetFolderContent() const
  100. {
  101. return std::get<std::vector<FSTBuilderNode>>(m_content);
  102. }
  103. };
  104. class DiscContent
  105. {
  106. public:
  107. DiscContent(u64 offset, u64 size, ContentSource source);
  108. // Provided because it's convenient when searching for DiscContent in an std::set
  109. explicit DiscContent(u64 offset);
  110. u64 GetOffset() const;
  111. u64 GetEndOffset() const;
  112. u64 GetSize() const;
  113. bool Read(u64* offset, u64* length, u8** buffer, DirectoryBlobReader* blob) const;
  114. bool operator==(const DiscContent& other) const { return GetEndOffset() == other.GetEndOffset(); }
  115. bool operator<(const DiscContent& other) const { return GetEndOffset() < other.GetEndOffset(); }
  116. bool operator>(const DiscContent& other) const { return other < *this; }
  117. bool operator<=(const DiscContent& other) const { return !(*this > other); }
  118. bool operator>=(const DiscContent& other) const { return !(*this < other); }
  119. private:
  120. // Position of this content chunk within its parent DiscContentContainer.
  121. u64 m_offset;
  122. // Number of bytes this content chunk takes up.
  123. u64 m_size = 0;
  124. // Where and how to find the data for this content chunk.
  125. ContentSource m_content_source;
  126. };
  127. class DiscContentContainer
  128. {
  129. public:
  130. void Add(u64 offset, std::vector<u8> vector)
  131. {
  132. size_t vector_size = vector.size();
  133. return Add(offset, vector_size, std::make_shared<std::vector<u8>>(std::move(vector)));
  134. }
  135. void Add(u64 offset, u64 size, ContentSource source);
  136. u64 CheckSizeAndAdd(u64 offset, const std::string& path);
  137. u64 CheckSizeAndAdd(u64 offset, u64 max_size, const std::string& path);
  138. bool Read(u64 offset, u64 length, u8* buffer, DirectoryBlobReader* blob) const;
  139. private:
  140. std::set<DiscContent> m_contents;
  141. };
  142. class DirectoryBlobPartition
  143. {
  144. public:
  145. DirectoryBlobPartition() = default;
  146. DirectoryBlobPartition(const std::string& root_directory, std::optional<bool> is_wii);
  147. DirectoryBlobPartition(
  148. DiscIO::VolumeDisc* volume, const DiscIO::Partition& partition, std::optional<bool> is_wii,
  149. const std::function<void(std::vector<FSTBuilderNode>* fst_nodes)>& sys_callback,
  150. const std::function<void(std::vector<FSTBuilderNode>* fst_nodes, FSTBuilderNode* dol_node)>&
  151. fst_callback,
  152. DirectoryBlobReader* blob);
  153. DirectoryBlobPartition(const DirectoryBlobPartition&) = default;
  154. DirectoryBlobPartition& operator=(const DirectoryBlobPartition&) = default;
  155. DirectoryBlobPartition(DirectoryBlobPartition&&) = default;
  156. DirectoryBlobPartition& operator=(DirectoryBlobPartition&&) = default;
  157. bool IsWii() const { return m_is_wii; }
  158. u64 GetDataSize() const { return m_data_size; }
  159. void SetDataSize(u64 size) { m_data_size = size; }
  160. const std::string& GetRootDirectory() const { return m_root_directory; }
  161. const DiscContentContainer& GetContents() const { return m_contents; }
  162. const std::optional<DiscIO::Partition>& GetWrappedPartition() const
  163. {
  164. return m_wrapped_partition;
  165. }
  166. const std::array<u8, VolumeWii::AES_KEY_SIZE>& GetKey() const { return m_key; }
  167. void SetKey(std::array<u8, VolumeWii::AES_KEY_SIZE> key) { m_key = key; }
  168. private:
  169. void SetDiscType(std::optional<bool> is_wii, const std::vector<u8>& disc_header);
  170. void SetBI2FromFile(const std::string& bi2_path);
  171. void SetBI2(std::vector<u8> bi2);
  172. // Returns DOL address
  173. u64 SetApploaderFromFile(const std::string& path);
  174. u64 SetApploader(std::vector<u8> apploader, const std::string& log_path);
  175. // Returns FST address
  176. u64 SetDOLFromFile(const std::string& path, u64 dol_address, std::vector<u8>* disc_header);
  177. u64 SetDOL(FSTBuilderNode dol_node, u64 dol_address, std::vector<u8>* disc_header);
  178. void BuildFSTFromFolder(const std::string& fst_root_path, u64 fst_address,
  179. std::vector<u8>* disc_header);
  180. void BuildFST(std::vector<FSTBuilderNode> root_nodes, u64 fst_address,
  181. std::vector<u8>* disc_header);
  182. // FST creation
  183. void WriteEntryData(std::vector<u8>* fst_data, u32* entry_offset, u8 type, u32 name_offset,
  184. u64 data_offset, u64 length, u32 address_shift);
  185. void WriteEntryName(std::vector<u8>* fst_data, u32* name_offset, const std::string& name,
  186. u64 name_table_offset);
  187. void WriteDirectory(std::vector<u8>* fst_data, std::vector<FSTBuilderNode>* parent_entries,
  188. u32* fst_offset, u32* name_offset, u64* data_offset, u32 parent_entry_index,
  189. u64 name_table_offset);
  190. DiscContentContainer m_contents;
  191. std::array<u8, VolumeWii::AES_KEY_SIZE> m_key{};
  192. std::string m_root_directory;
  193. bool m_is_wii = false;
  194. // GameCube has no shift, Wii has 2 bit shift
  195. u32 m_address_shift = 0;
  196. u64 m_data_size = 0;
  197. std::optional<DiscIO::Partition> m_wrapped_partition = std::nullopt;
  198. };
  199. class DirectoryBlobReader : public BlobReader
  200. {
  201. friend DiscContent;
  202. public:
  203. static std::unique_ptr<DirectoryBlobReader> Create(const std::string& dol_path);
  204. static std::unique_ptr<DirectoryBlobReader> Create(
  205. std::unique_ptr<DiscIO::VolumeDisc> volume,
  206. const std::function<void(std::vector<FSTBuilderNode>* fst_nodes)>& sys_callback,
  207. const std::function<void(std::vector<FSTBuilderNode>* fst_nodes, FSTBuilderNode* dol_node)>&
  208. fst_callback);
  209. DirectoryBlobReader(DirectoryBlobReader&&) = default;
  210. DirectoryBlobReader& operator=(DirectoryBlobReader&&) = default;
  211. bool Read(u64 offset, u64 length, u8* buffer) override;
  212. bool SupportsReadWiiDecrypted(u64 offset, u64 size, u64 partition_data_offset) const override;
  213. bool ReadWiiDecrypted(u64 offset, u64 size, u8* buffer, u64 partition_data_offset) override;
  214. BlobType GetBlobType() const override;
  215. std::unique_ptr<BlobReader> CopyReader() const override;
  216. u64 GetRawSize() const override;
  217. u64 GetDataSize() const override;
  218. DataSizeType GetDataSizeType() const override { return DataSizeType::Accurate; }
  219. u64 GetBlockSize() const override { return 0; }
  220. bool HasFastRandomAccessInBlock() const override { return true; }
  221. std::string GetCompressionMethod() const override { return {}; }
  222. std::optional<int> GetCompressionLevel() const override { return std::nullopt; }
  223. private:
  224. struct PartitionWithType
  225. {
  226. PartitionWithType(DirectoryBlobPartition&& partition_, PartitionType type_)
  227. : partition(std::move(partition_)), type(type_)
  228. {
  229. }
  230. DirectoryBlobPartition partition;
  231. PartitionType type;
  232. };
  233. explicit DirectoryBlobReader(const std::string& game_partition_root,
  234. const std::string& true_root);
  235. explicit DirectoryBlobReader(
  236. std::unique_ptr<DiscIO::VolumeDisc> volume,
  237. const std::function<void(std::vector<FSTBuilderNode>* fst_nodes)>& sys_callback,
  238. const std::function<void(std::vector<FSTBuilderNode>* fst_nodes, FSTBuilderNode* dol_node)>&
  239. fst_callback);
  240. explicit DirectoryBlobReader(const DirectoryBlobReader& rhs);
  241. const DirectoryBlobPartition* GetPartition(u64 offset, u64 size, u64 partition_data_offset) const;
  242. bool EncryptPartitionData(u64 offset, u64 size, u8* buffer, u64 partition_data_offset,
  243. u64 partition_data_decrypted_size);
  244. void SetNonpartitionDiscHeaderFromFile(const std::vector<u8>& partition_header,
  245. const std::string& game_partition_root);
  246. void SetNonpartitionDiscHeader(const std::vector<u8>& partition_header,
  247. std::vector<u8> header_bin);
  248. void SetWiiRegionDataFromFile(const std::string& game_partition_root);
  249. void SetWiiRegionData(const std::vector<u8>& wii_region_data, const std::string& log_path);
  250. void SetPartitions(std::vector<PartitionWithType>&& partitions);
  251. void SetPartitionHeader(DirectoryBlobPartition* partition, u64 partition_address);
  252. DiscIO::VolumeDisc* GetWrappedVolume() { return m_wrapped_volume.get(); }
  253. // For GameCube:
  254. DirectoryBlobPartition m_gamecube_pseudopartition;
  255. // For Wii:
  256. DiscContentContainer m_nonpartition_contents;
  257. std::map<u64, DirectoryBlobPartition> m_partitions;
  258. WiiEncryptionCache m_encryption_cache;
  259. bool m_is_wii;
  260. bool m_encrypted;
  261. u64 m_data_size;
  262. std::unique_ptr<DiscIO::VolumeDisc> m_wrapped_volume;
  263. };
  264. } // namespace DiscIO