| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <limits> |
| 7 |
#include <map> |
| 8 |
#include <memory> |
| 9 |
#include <optional> |
| 10 |
#include <span> |
| 11 |
#include <string> |
| 12 |
#include <vector> |
| 13 |
|
| 14 |
#include "Common/CommonTypes.h" |
| 15 |
#include "Common/Crypto/SHA1.h" |
| 16 |
#include "Common/Swap.h" |
| 17 |
#include "Core/IOS/ES/Formats.h" |
| 18 |
#include "DiscIO/Enums.h" |
| 19 |
|
| 20 |
namespace DiscIO |
| 21 |
{ |
| 22 |
class BlobReader; |
| 23 |
enum class BlobType; |
| 24 |
enum class DataSizeType; |
| 25 |
class FileSystem; |
| 26 |
class VolumeDisc; |
| 27 |
class VolumeWAD; |
| 28 |
|
| 29 |
struct Partition final |
| 30 |
{ |
| 31 |
constexpr Partition() = default; |
| 32 |
constexpr explicit Partition(u64 offset_) : offset(offset_) {} |
| 33 |
constexpr bool operator==(const Partition& other) const { return offset == other.offset; } |
| 34 |
constexpr auto operator<=>(const Partition other) const { return offset <=> other.offset; } |
| 35 |
u64 offset{std::numeric_limits<u64>::max()}; |
| 36 |
}; |
| 37 |
|
| 38 |
constexpr Partition PARTITION_NONE(std::numeric_limits<u64>::max() - 1); |
| 39 |
|
| 40 |
class Volume |
| 41 |
{ |
| 42 |
public: |
| 43 |
Volume() {} |
| 44 |
virtual ~Volume() {} |
| 45 |
virtual bool Read(u64 offset, u64 length, u8* buffer, const Partition& partition) const = 0; |
| 46 |
template <typename T> |
| 47 |
std::optional<T> ReadSwapped(u64 offset, const Partition& partition) const |
| 48 |
{ |
| 49 |
T temp; |
| 50 |
if (!Read(offset, sizeof(T), reinterpret_cast<u8*>(&temp), partition)) |
| 51 |
return std::nullopt; |
| 52 |
return Common::FromBigEndian(temp); |
| 53 |
} |
| 54 |
std::optional<u64> ReadSwappedAndShifted(u64 offset, const Partition& partition) const |
| 55 |
{ |
| 56 |
const std::optional<u32> temp = ReadSwapped<u32>(offset, partition); |
| 57 |
if (!temp) |
| 58 |
return std::nullopt; |
| 59 |
return static_cast<u64>(*temp) << GetOffsetShift(); |
| 60 |
} |
| 61 |
|
| 62 |
virtual bool HasWiiHashes() const { return false; } |
| 63 |
virtual bool HasWiiEncryption() const { return false; } |
| 64 |
virtual std::vector<Partition> GetPartitions() const { return {}; } |
| 65 |
virtual Partition GetGamePartition() const { return PARTITION_NONE; } |
| 66 |
virtual std::optional<u32> GetPartitionType(const Partition& partition) const |
| 67 |
{ |
| 68 |
return std::nullopt; |
| 69 |
} |
| 70 |
std::optional<u64> GetTitleID() const { return GetTitleID(GetGamePartition()); } |
| 71 |
virtual std::optional<u64> GetTitleID(const Partition& partition) const { return std::nullopt; } |
| 72 |
virtual const IOS::ES::TicketReader& GetTicket(const Partition& partition) const |
| 73 |
{ |
| 74 |
return INVALID_TICKET; |
| 75 |
} |
| 76 |
virtual const IOS::ES::TMDReader& GetTMD(const Partition& partition) const { return INVALID_TMD; } |
| 77 |
virtual const std::vector<u8>& GetCertificateChain(const Partition& partition) const |
| 78 |
{ |
| 79 |
return INVALID_CERT_CHAIN; |
| 80 |
} |
| 81 |
virtual std::vector<u8> GetContent(u16 index) const { return {}; } |
| 82 |
virtual std::vector<u64> GetContentOffsets() const { return {}; } |
| 83 |
virtual bool CheckContentIntegrity(const IOS::ES::Content& content, |
| 84 |
std::span<const u8> encrypted_data, |
| 85 |
const IOS::ES::TicketReader& ticket) const |
| 86 |
{ |
| 87 |
return false; |
| 88 |
} |
| 89 |
virtual IOS::ES::TicketReader GetTicketWithFixedCommonKey() const { return {}; } |
| 90 |
|
| 91 |
virtual const FileSystem* GetFileSystem(const Partition& partition) const = 0; |
| 92 |
virtual u64 PartitionOffsetToRawOffset(u64 offset, const Partition& partition) const |
| 93 |
{ |
| 94 |
return offset; |
| 95 |
} |
| 96 |
virtual std::string GetGameID(const Partition& partition = PARTITION_NONE) const = 0; |
| 97 |
virtual std::string GetGameTDBID(const Partition& partition = PARTITION_NONE) const = 0; |
| 98 |
virtual std::string GetMakerID(const Partition& partition = PARTITION_NONE) const = 0; |
| 99 |
virtual std::optional<u16> GetRevision(const Partition& partition = PARTITION_NONE) const = 0; |
| 100 |
virtual std::string GetInternalName(const Partition& partition = PARTITION_NONE) const = 0; |
| 101 |
virtual std::map<Language, std::string> GetShortNames() const { return {}; } |
| 102 |
virtual std::map<Language, std::string> GetLongNames() const { return {}; } |
| 103 |
virtual std::map<Language, std::string> GetShortMakers() const { return {}; } |
| 104 |
virtual std::map<Language, std::string> GetLongMakers() const { return {}; } |
| 105 |
virtual std::map<Language, std::string> GetDescriptions() const { return {}; } |
| 106 |
virtual std::vector<u32> GetBanner(u32* width, u32* height) const = 0; |
| 107 |
std::string GetApploaderDate() const { return GetApploaderDate(GetGamePartition()); } |
| 108 |
virtual std::string GetApploaderDate(const Partition& partition) const = 0; |
| 109 |
|
| 110 |
virtual std::optional<u8> GetDiscNumber(const Partition& partition = PARTITION_NONE) const |
| 111 |
{ |
| 112 |
return 0; |
| 113 |
} |
| 114 |
virtual Platform GetVolumeType() const = 0; |
| 115 |
virtual bool IsDatelDisc() const = 0; |
| 116 |
virtual bool IsNKit() const = 0; |
| 117 |
virtual bool CheckH3TableIntegrity(const Partition& partition) const { return false; } |
| 118 |
virtual bool CheckBlockIntegrity(u64 block_index, const u8* encrypted_data, |
| 119 |
const Partition& partition) const |
| 120 |
{ |
| 121 |
return false; |
| 122 |
} |
| 123 |
virtual bool CheckBlockIntegrity(u64 block_index, const Partition& partition) const |
| 124 |
{ |
| 125 |
return false; |
| 126 |
} |
| 127 |
virtual Region GetRegion() const = 0; |
| 128 |
virtual Country GetCountry(const Partition& partition = PARTITION_NONE) const = 0; |
| 129 |
virtual BlobType GetBlobType() const = 0; |
| 130 |
|
| 131 |
virtual u64 GetDataSize() const = 0; |
| 132 |
virtual DataSizeType GetDataSizeType() const = 0; |
| 133 |
|
| 134 |
virtual u64 GetRawSize() const = 0; |
| 135 |
virtual const BlobReader& GetBlobReader() const = 0; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
virtual std::array<u8, 20> GetSyncHash() const = 0; |
| 143 |
|
| 144 |
protected: |
| 145 |
std::string DecodeString(std::span<const char> data) const; |
| 146 |
static std::string FilterGameID(std::span<const char> data); |
| 147 |
|
| 148 |
void ReadAndAddToSyncHash(Common::SHA1::Context* context, u64 offset, u64 length, |
| 149 |
const Partition& partition) const; |
| 150 |
void AddTMDToSyncHash(Common::SHA1::Context* context, const Partition& partition) const; |
| 151 |
|
| 152 |
virtual u32 GetOffsetShift() const { return 0; } |
| 153 |
static std::map<Language, std::string> ReadWiiNames(std::span<const char16_t> data); |
| 154 |
|
| 155 |
static constexpr size_t NUMBER_OF_LANGUAGES = 10; |
| 156 |
static constexpr size_t NAME_CHARS_LENGTH = 42; |
| 157 |
static constexpr size_t NAME_BYTES_LENGTH = NAME_CHARS_LENGTH * sizeof(char16_t); |
| 158 |
static constexpr size_t NAMES_TOTAL_CHARS = NAME_CHARS_LENGTH * NUMBER_OF_LANGUAGES; |
| 159 |
static constexpr size_t NAMES_TOTAL_BYTES = NAME_BYTES_LENGTH * NUMBER_OF_LANGUAGES; |
| 160 |
|
| 161 |
static const IOS::ES::TicketReader INVALID_TICKET; |
| 162 |
static const IOS::ES::TMDReader INVALID_TMD; |
| 163 |
static const std::vector<u8> INVALID_CERT_CHAIN; |
| 164 |
}; |
| 165 |
|
| 166 |
std::unique_ptr<VolumeDisc> CreateDisc(std::unique_ptr<BlobReader> reader); |
| 167 |
std::unique_ptr<VolumeDisc> CreateDisc(const std::string& path); |
| 168 |
|
| 169 |
std::unique_ptr<VolumeDisc> CreateDiscForCore(const std::string& path); |
| 170 |
|
| 171 |
std::unique_ptr<VolumeWAD> CreateWAD(std::unique_ptr<BlobReader> reader); |
| 172 |
std::unique_ptr<VolumeWAD> CreateWAD(const std::string& path); |
| 173 |
std::unique_ptr<Volume> CreateVolume(std::unique_ptr<BlobReader> reader); |
| 174 |
std::unique_ptr<Volume> CreateVolume(const std::string& path); |
| 175 |
|
| 176 |
} |