| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <array> |
| 7 |
#include <limits> |
| 8 |
#include <map> |
| 9 |
#include <memory> |
| 10 |
#include <mutex> |
| 11 |
#include <type_traits> |
| 12 |
#include <utility> |
| 13 |
|
| 14 |
#include "Common/CommonTypes.h" |
| 15 |
#include "Common/Crypto/SHA1.h" |
| 16 |
#include "Common/DirectIOFile.h" |
| 17 |
#include "Common/Swap.h" |
| 18 |
#include "DiscIO/Blob.h" |
| 19 |
#include "DiscIO/MultithreadedCompressor.h" |
| 20 |
#include "DiscIO/WIACompression.h" |
| 21 |
#include "DiscIO/WiiEncryptionCache.h" |
| 22 |
|
| 23 |
namespace DiscIO |
| 24 |
{ |
| 25 |
class FileSystem; |
| 26 |
class VolumeDisc; |
| 27 |
|
| 28 |
enum class WIARVZCompressionType : u32 |
| 29 |
{ |
| 30 |
None = 0, |
| 31 |
Purge = 1, |
| 32 |
Bzip2 = 2, |
| 33 |
LZMA = 3, |
| 34 |
LZMA2 = 4, |
| 35 |
Zstd = 5, |
| 36 |
}; |
| 37 |
|
| 38 |
std::pair<int, int> GetAllowedCompressionLevels(WIARVZCompressionType compression_type, bool gui); |
| 39 |
|
| 40 |
constexpr u32 WIA_MAGIC = 0x01414957; |
| 41 |
constexpr u32 RVZ_MAGIC = 0x015A5652; |
| 42 |
|
| 43 |
template <bool RVZ> |
| 44 |
class WIARVZFileReader final : public BlobReader |
| 45 |
{ |
| 46 |
public: |
| 47 |
~WIARVZFileReader() override; |
| 48 |
|
| 49 |
static std::unique_ptr<WIARVZFileReader> Create(File::DirectIOFile file, const std::string& path); |
| 50 |
|
| 51 |
BlobType GetBlobType() const override; |
| 52 |
std::unique_ptr<BlobReader> CopyReader() const override; |
| 53 |
|
| 54 |
u64 GetRawSize() const override { return Common::swap64(m_header_1.wia_file_size); } |
| 55 |
u64 GetDataSize() const override { return Common::swap64(m_header_1.iso_file_size); } |
| 56 |
DataSizeType GetDataSizeType() const override { return DataSizeType::Accurate; } |
| 57 |
|
| 58 |
u64 GetBlockSize() const override { return Common::swap32(m_header_2.chunk_size); } |
| 59 |
bool HasFastRandomAccessInBlock() const override { return false; } |
| 60 |
std::string GetCompressionMethod() const override; |
| 61 |
std::optional<int> GetCompressionLevel() const override |
| 62 |
{ |
| 63 |
return static_cast<int>(Common::swap32(m_header_2.compression_level)); |
| 64 |
} |
| 65 |
|
| 66 |
bool Read(u64 offset, u64 size, u8* out_ptr) override; |
| 67 |
bool SupportsReadWiiDecrypted(u64 offset, u64 size, u64 partition_data_offset) const override; |
| 68 |
bool ReadWiiDecrypted(u64 offset, u64 size, u8* out_ptr, u64 partition_data_offset) override; |
| 69 |
|
| 70 |
static ConversionResultCode Convert(BlobReader* infile, const VolumeDisc* infile_volume, |
| 71 |
File::DirectIOFile* outfile, |
| 72 |
WIARVZCompressionType compression_type, int compression_level, |
| 73 |
int chunk_size, CompressCB callback); |
| 74 |
|
| 75 |
private: |
| 76 |
using WiiKey = std::array<u8, 16>; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
#pragma pack(push, 1) |
| 81 |
struct WIAHeader1 |
| 82 |
{ |
| 83 |
u32 magic; |
| 84 |
u32 version; |
| 85 |
u32 version_compatible; |
| 86 |
u32 header_2_size; |
| 87 |
Common::SHA1::Digest header_2_hash; |
| 88 |
u64 iso_file_size; |
| 89 |
u64 wia_file_size; |
| 90 |
Common::SHA1::Digest header_1_hash; |
| 91 |
}; |
| 92 |
static_assert(sizeof(WIAHeader1) == 0x48, "Wrong size for WIA header 1"); |
| 93 |
|
| 94 |
struct WIAHeader2 |
| 95 |
{ |
| 96 |
u32 disc_type; |
| 97 |
u32 compression_type; |
| 98 |
s32 compression_level; |
| 99 |
u32 chunk_size; |
| 100 |
|
| 101 |
std::array<u8, 0x80> disc_header; |
| 102 |
|
| 103 |
u32 number_of_partition_entries; |
| 104 |
u32 partition_entry_size; |
| 105 |
u64 partition_entries_offset; |
| 106 |
Common::SHA1::Digest partition_entries_hash; |
| 107 |
|
| 108 |
u32 number_of_raw_data_entries; |
| 109 |
u64 raw_data_entries_offset; |
| 110 |
u32 raw_data_entries_size; |
| 111 |
|
| 112 |
u32 number_of_group_entries; |
| 113 |
u64 group_entries_offset; |
| 114 |
u32 group_entries_size; |
| 115 |
|
| 116 |
u8 compressor_data_size; |
| 117 |
u8 compressor_data[7]; |
| 118 |
}; |
| 119 |
static_assert(sizeof(WIAHeader2) == 0xdc, "Wrong size for WIA header 2"); |
| 120 |
|
| 121 |
struct PartitionDataEntry |
| 122 |
{ |
| 123 |
u32 first_sector; |
| 124 |
u32 number_of_sectors; |
| 125 |
u32 group_index; |
| 126 |
u32 number_of_groups; |
| 127 |
}; |
| 128 |
static_assert(sizeof(PartitionDataEntry) == 0x10, "Wrong size for WIA partition data entry"); |
| 129 |
|
| 130 |
struct PartitionEntry |
| 131 |
{ |
| 132 |
WiiKey partition_key; |
| 133 |
std::array<PartitionDataEntry, 2> data_entries; |
| 134 |
}; |
| 135 |
static_assert(sizeof(PartitionEntry) == 0x30, "Wrong size for WIA partition entry"); |
| 136 |
|
| 137 |
struct RawDataEntry |
| 138 |
{ |
| 139 |
u64 data_offset; |
| 140 |
u64 data_size; |
| 141 |
u32 group_index; |
| 142 |
u32 number_of_groups; |
| 143 |
}; |
| 144 |
static_assert(sizeof(RawDataEntry) == 0x18, "Wrong size for WIA raw data entry"); |
| 145 |
|
| 146 |
struct WIAGroupEntry |
| 147 |
{ |
| 148 |
u32 data_offset; |
| 149 |
u32 data_size; |
| 150 |
}; |
| 151 |
static_assert(sizeof(WIAGroupEntry) == 0x08, "Wrong size for WIA group entry"); |
| 152 |
|
| 153 |
struct RVZGroupEntry |
| 154 |
{ |
| 155 |
u32 data_offset; |
| 156 |
u32 data_size; |
| 157 |
u32 rvz_packed_size; |
| 158 |
}; |
| 159 |
static_assert(sizeof(RVZGroupEntry) == 0x0c, "Wrong size for RVZ group entry"); |
| 160 |
|
| 161 |
using GroupEntry = std::conditional_t<RVZ, RVZGroupEntry, WIAGroupEntry>; |
| 162 |
|
| 163 |
struct HashExceptionEntry |
| 164 |
{ |
| 165 |
u16 offset; |
| 166 |
Common::SHA1::Digest hash; |
| 167 |
}; |
| 168 |
static_assert(sizeof(HashExceptionEntry) == 0x16, "Wrong size for WIA hash exception entry"); |
| 169 |
#pragma pack(pop) |
| 170 |
|
| 171 |
struct DataEntry |
| 172 |
{ |
| 173 |
u32 index; |
| 174 |
bool is_partition; |
| 175 |
u8 partition_data_index; |
| 176 |
|
| 177 |
DataEntry(size_t index_) |
| 178 |
: index(static_cast<u32>(index_)), is_partition(false), partition_data_index(0) |
| 179 |
{ |
| 180 |
} |
| 181 |
DataEntry(size_t index_, size_t partition_data_index_) |
| 182 |
: index(static_cast<u32>(index_)), is_partition(true), |
| 183 |
partition_data_index(static_cast<u8>(partition_data_index_)) |
| 184 |
{ |
| 185 |
} |
| 186 |
}; |
| 187 |
|
| 188 |
class Chunk |
| 189 |
{ |
| 190 |
public: |
| 191 |
Chunk(); |
| 192 |
Chunk(File::DirectIOFile* file, u64 offset_in_file, u64 compressed_size, u64 decompressed_size, |
| 193 |
u32 exception_lists, bool compressed_exception_lists, u32 rvz_packed_size, |
| 194 |
u64 data_offset, std::unique_ptr<Decompressor> decompressor); |
| 195 |
|
| 196 |
bool Read(u64 offset, u64 size, u8* out_ptr); |
| 197 |
|
| 198 |
|
| 199 |
void GetHashExceptions(std::vector<HashExceptionEntry>* exception_list, |
| 200 |
u64 exception_list_index, u16 additional_offset) const; |
| 201 |
|
| 202 |
template <typename T> |
| 203 |
bool ReadAll(std::vector<T>* vector) |
| 204 |
{ |
| 205 |
return Read(0, vector->size() * sizeof(T), reinterpret_cast<u8*>(vector->data())); |
| 206 |
} |
| 207 |
|
| 208 |
private: |
| 209 |
bool Decompress(); |
| 210 |
bool HandleExceptions(const u8* data, size_t bytes_allocated, size_t bytes_written, |
| 211 |
size_t* bytes_used, bool align); |
| 212 |
|
| 213 |
size_t GetOutBytesWrittenExcludingExceptions() const; |
| 214 |
|
| 215 |
DecompressionBuffer m_in; |
| 216 |
DecompressionBuffer m_out; |
| 217 |
size_t m_in_bytes_read = 0; |
| 218 |
|
| 219 |
std::unique_ptr<Decompressor> m_decompressor = nullptr; |
| 220 |
File::DirectIOFile* m_file = nullptr; |
| 221 |
u64 m_offset_in_file = 0; |
| 222 |
|
| 223 |
size_t m_out_bytes_allocated_for_exceptions = 0; |
| 224 |
size_t m_out_bytes_used_for_exceptions = 0; |
| 225 |
size_t m_in_bytes_used_for_exceptions = 0; |
| 226 |
u32 m_exception_lists = 0; |
| 227 |
bool m_compressed_exception_lists = false; |
| 228 |
u32 m_rvz_packed_size = 0; |
| 229 |
u64 m_data_offset = 0; |
| 230 |
}; |
| 231 |
|
| 232 |
explicit WIARVZFileReader(File::DirectIOFile file, const std::string& path); |
| 233 |
bool Initialize(const std::string& path); |
| 234 |
bool HasDataOverlap() const; |
| 235 |
|
| 236 |
const PartitionEntry* GetPartition(u64 partition_data_offset, u32* partition_first_sector) const; |
| 237 |
|
| 238 |
bool ReadFromGroups(u64* offset, u64* size, u8** out_ptr, u64 chunk_size, u32 sector_size, |
| 239 |
u64 data_offset, u64 data_size, u32 group_index, u32 number_of_groups, |
| 240 |
u32 exception_lists); |
| 241 |
Chunk& ReadCompressedData(u64 offset_in_file, u64 compressed_size, u64 decompressed_size, |
| 242 |
WIARVZCompressionType compression_type, u32 exception_lists = 0, |
| 243 |
u32 rvz_packed_size = 0, u64 data_offset = 0); |
| 244 |
|
| 245 |
static bool ApplyHashExceptions(std::span<const HashExceptionEntry> exception_list, |
| 246 |
VolumeWii::HashBlock hash_blocks[VolumeWii::BLOCKS_PER_GROUP]); |
| 247 |
|
| 248 |
static std::string VersionToString(u32 version); |
| 249 |
|
| 250 |
struct ReuseID |
| 251 |
{ |
| 252 |
bool operator==(const ReuseID& other) const |
| 253 |
{ |
| 254 |
return std::tie(partition_key, data_size, encrypted, value) == |
| 255 |
std::tie(other.partition_key, other.data_size, other.encrypted, other.value); |
| 256 |
} |
| 257 |
bool operator<(const ReuseID& other) const |
| 258 |
{ |
| 259 |
return std::tie(partition_key, data_size, encrypted, value) < |
| 260 |
std::tie(other.partition_key, other.data_size, other.encrypted, other.value); |
| 261 |
} |
| 262 |
bool operator>(const ReuseID& other) const |
| 263 |
{ |
| 264 |
return std::tie(partition_key, data_size, encrypted, value) > |
| 265 |
std::tie(other.partition_key, other.data_size, other.encrypted, other.value); |
| 266 |
} |
| 267 |
|
| 268 |
bool operator>=(const ReuseID& other) const { return !operator<(other); } |
| 269 |
bool operator<=(const ReuseID& other) const { return !operator>(other); } |
| 270 |
|
| 271 |
WiiKey partition_key; |
| 272 |
u64 data_size; |
| 273 |
bool encrypted; |
| 274 |
u8 value; |
| 275 |
}; |
| 276 |
|
| 277 |
struct CompressThreadState |
| 278 |
{ |
| 279 |
using WiiBlockData = std::array<u8, VolumeWii::BLOCK_DATA_SIZE>; |
| 280 |
|
| 281 |
std::unique_ptr<Compressor> compressor; |
| 282 |
|
| 283 |
std::vector<WiiBlockData> decryption_buffer = |
| 284 |
std::vector<WiiBlockData>(VolumeWii::BLOCKS_PER_GROUP); |
| 285 |
|
| 286 |
std::vector<VolumeWii::HashBlock> hash_buffer = |
| 287 |
std::vector<VolumeWii::HashBlock>(VolumeWii::BLOCKS_PER_GROUP); |
| 288 |
}; |
| 289 |
|
| 290 |
struct CompressParameters |
| 291 |
{ |
| 292 |
std::vector<u8> data{}; |
| 293 |
const DataEntry* data_entry = nullptr; |
| 294 |
u64 data_offset = 0; |
| 295 |
u64 bytes_read = 0; |
| 296 |
size_t group_index = 0; |
| 297 |
}; |
| 298 |
|
| 299 |
struct WIAOutputParametersEntry |
| 300 |
{ |
| 301 |
std::vector<u8> exception_lists; |
| 302 |
std::vector<u8> main_data; |
| 303 |
std::optional<ReuseID> reuse_id; |
| 304 |
std::optional<GroupEntry> reused_group; |
| 305 |
}; |
| 306 |
|
| 307 |
struct RVZOutputParametersEntry |
| 308 |
{ |
| 309 |
std::vector<u8> exception_lists; |
| 310 |
std::vector<u8> main_data; |
| 311 |
std::optional<ReuseID> reuse_id; |
| 312 |
std::optional<GroupEntry> reused_group; |
| 313 |
size_t rvz_packed_size = 0; |
| 314 |
bool compressed = false; |
| 315 |
}; |
| 316 |
|
| 317 |
using OutputParametersEntry = |
| 318 |
std::conditional_t<RVZ, RVZOutputParametersEntry, WIAOutputParametersEntry>; |
| 319 |
|
| 320 |
struct OutputParameters |
| 321 |
{ |
| 322 |
std::vector<OutputParametersEntry> entries; |
| 323 |
u64 bytes_read = 0; |
| 324 |
size_t group_index = 0; |
| 325 |
}; |
| 326 |
|
| 327 |
static bool PadTo4(File::DirectIOFile* file, u64* bytes_written); |
| 328 |
static void AddRawDataEntry(u64 offset, u64 size, int chunk_size, u32* total_groups, |
| 329 |
std::vector<RawDataEntry>* raw_data_entries, |
| 330 |
std::vector<DataEntry>* data_entries); |
| 331 |
static PartitionDataEntry |
| 332 |
CreatePartitionDataEntry(u64 offset, u64 size, u32 index, int chunk_size, u32* total_groups, |
| 333 |
const std::vector<PartitionEntry>& partition_entries, |
| 334 |
std::vector<DataEntry>* data_entries); |
| 335 |
static ConversionResultCode SetUpDataEntriesForWriting( |
| 336 |
const VolumeDisc* volume, int chunk_size, u64 iso_size, u32* total_groups, |
| 337 |
std::vector<PartitionEntry>* partition_entries, std::vector<RawDataEntry>* raw_data_entries, |
| 338 |
std::vector<DataEntry>* data_entries, std::vector<const FileSystem*>* partition_file_systems); |
| 339 |
static std::optional<std::vector<u8>> Compress(Compressor* compressor, const u8* data, |
| 340 |
size_t size); |
| 341 |
static bool WriteHeader(File::DirectIOFile* file, const u8* data, size_t size, u64 upper_bound, |
| 342 |
u64* bytes_written, u64* offset_out); |
| 343 |
|
| 344 |
static void SetUpCompressor(std::unique_ptr<Compressor>* compressor, |
| 345 |
WIARVZCompressionType compression_type, int compression_level, |
| 346 |
WIAHeader2* header_2); |
| 347 |
static bool TryReuse(std::map<ReuseID, GroupEntry>* reusable_groups, |
| 348 |
std::mutex* reusable_groups_mutex, OutputParametersEntry* entry); |
| 349 |
static ConversionResult<OutputParameters> |
| 350 |
ProcessAndCompress(CompressThreadState* state, CompressParameters parameters, |
| 351 |
const std::vector<PartitionEntry>& partition_entries, |
| 352 |
const std::vector<DataEntry>& data_entries, const FileSystem* file_system, |
| 353 |
std::map<ReuseID, GroupEntry>* reusable_groups, |
| 354 |
std::mutex* reusable_groups_mutex, u64 chunks_per_wii_group, |
| 355 |
u64 exception_lists_per_chunk, bool compressed_exception_lists, |
| 356 |
bool compression); |
| 357 |
static ConversionResultCode Output(std::vector<OutputParametersEntry>* entries, |
| 358 |
File::DirectIOFile* outfile, |
| 359 |
std::map<ReuseID, GroupEntry>* reusable_groups, |
| 360 |
std::mutex* reusable_groups_mutex, GroupEntry* group_entry, |
| 361 |
u64* bytes_written); |
| 362 |
static ConversionResultCode RunCallback(size_t groups_written, u64 bytes_read, u64 bytes_written, |
| 363 |
u32 total_groups, u64 iso_size, |
| 364 |
const CompressCB& callback); |
| 365 |
|
| 366 |
bool m_valid; |
| 367 |
WIARVZCompressionType m_compression_type; |
| 368 |
|
| 369 |
File::DirectIOFile m_file; |
| 370 |
std::string m_path; |
| 371 |
Chunk m_cached_chunk; |
| 372 |
u64 m_cached_chunk_offset = std::numeric_limits<u64>::max(); |
| 373 |
WiiEncryptionCache m_encryption_cache; |
| 374 |
|
| 375 |
std::vector<HashExceptionEntry> m_exception_list; |
| 376 |
bool m_write_to_exception_list = false; |
| 377 |
u64 m_exception_list_last_group_index; |
| 378 |
|
| 379 |
WIAHeader1 m_header_1; |
| 380 |
WIAHeader2 m_header_2; |
| 381 |
std::vector<PartitionEntry> m_partition_entries; |
| 382 |
std::vector<RawDataEntry> m_raw_data_entries; |
| 383 |
std::vector<GroupEntry> m_group_entries; |
| 384 |
|
| 385 |
std::map<u64, DataEntry> m_data_entries; |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
static constexpr u32 WIA_VERSION = 0x01000000; |
| 392 |
static constexpr u32 WIA_VERSION_WRITE_COMPATIBLE = 0x01000000; |
| 393 |
static constexpr u32 WIA_VERSION_READ_COMPATIBLE = 0x00080000; |
| 394 |
|
| 395 |
static constexpr u32 RVZ_VERSION = 0x01000000; |
| 396 |
static constexpr u32 RVZ_VERSION_WRITE_COMPATIBLE = 0x00030000; |
| 397 |
static constexpr u32 RVZ_VERSION_READ_COMPATIBLE = 0x00030000; |
| 398 |
}; |
| 399 |
|
| 400 |
using WIAFileReader = WIARVZFileReader<false>; |
| 401 |
using RVZFileReader = WIARVZFileReader<true>; |
| 402 |
|
| 403 |
} |