Debian Codegraph @

Source/Core/DiscIO/DirectoryBlob.h

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