Permalink
Browse files

Volume/VolumeGC/VolumeWii: Amend variable naming

Drops prefixed underscores and normalizes names to follow our coding
style.
  • Loading branch information...
lioncash committed May 30, 2018
1 parent ae4aa0b commit 66122f037e363df456d8ba7f6d4899184294f30b
@@ -43,7 +43,7 @@ class Volume
public:
Volume() {}
virtual ~Volume() {}
virtual bool Read(u64 _Offset, u64 _Length, u8* _pBuffer, const Partition& partition) const = 0;
virtual bool Read(u64 offset, u64 length, u8* buffer, const Partition& partition) const = 0;
template <typename T>
std::optional<T> ReadSwapped(u64 offset, const Partition& partition) const
{
@@ -28,9 +28,9 @@
namespace DiscIO
{
VolumeGC::VolumeGC(std::unique_ptr<BlobReader> reader) : m_pReader(std::move(reader))
VolumeGC::VolumeGC(std::unique_ptr<BlobReader> reader) : m_reader(std::move(reader))
{
ASSERT(m_pReader);
ASSERT(m_reader);
m_file_system = [this]() -> std::unique_ptr<FileSystem> {
auto file_system = std::make_unique<FileSystemGCWii>(this, PARTITION_NONE);
@@ -44,12 +44,12 @@ VolumeGC::~VolumeGC()
{
}
bool VolumeGC::Read(u64 _Offset, u64 _Length, u8* _pBuffer, const Partition& partition) const
bool VolumeGC::Read(u64 offset, u64 length, u8* buffer, const Partition& partition) const
{
if (partition != PARTITION_NONE)
return false;
return m_pReader->Read(_Offset, _Length, _pBuffer);
return m_reader->Read(offset, length, buffer);
}
const FileSystem* VolumeGC::GetFileSystem(const Partition& partition) const
@@ -61,15 +61,15 @@ std::string VolumeGC::GetGameID(const Partition& partition) const
{
static const std::string NO_UID("NO_UID");
char ID[6];
char id[6];
if (!Read(0, sizeof(ID), reinterpret_cast<u8*>(ID), partition))
if (!Read(0, sizeof(id), reinterpret_cast<u8*>(id), partition))
{
PanicAlertT("Failed to read unique ID from disc image");
return NO_UID;
}
return DecodeString(ID);
return DecodeString(id);
}
Region VolumeGC::GetRegion() const
@@ -103,11 +103,11 @@ Country VolumeGC::GetCountry(const Partition& partition) const
std::string VolumeGC::GetMakerID(const Partition& partition) const
{
char makerID[2];
if (!Read(0x4, 0x2, (u8*)&makerID, partition))
char maker_id[2];
if (!Read(0x4, 0x2, reinterpret_cast<u8*>(&maker_id), partition))
return std::string();
return DecodeString(makerID);
return DecodeString(maker_id);
}
std::optional<u16> VolumeGC::GetRevision(const Partition& partition) const
@@ -168,17 +168,17 @@ std::string VolumeGC::GetApploaderDate(const Partition& partition) const
BlobType VolumeGC::GetBlobType() const
{
return m_pReader->GetBlobType();
return m_reader->GetBlobType();
}
u64 VolumeGC::GetSize() const
{
return m_pReader->GetDataSize();
return m_reader->GetDataSize();
}
u64 VolumeGC::GetRawSize() const
{
return m_pReader->GetRawSize();
return m_reader->GetRawSize();
}
std::optional<u8> VolumeGC::GetDiscNumber(const Partition& partition) const
@@ -30,7 +30,7 @@ class VolumeGC : public Volume
public:
VolumeGC(std::unique_ptr<BlobReader> reader);
~VolumeGC();
bool Read(u64 _Offset, u64 _Length, u8* _pBuffer,
bool Read(u64 offset, u64 length, u8* buffer,
const Partition& partition = PARTITION_NONE) const override;
const FileSystem* GetFileSystem(const Partition& partition = PARTITION_NONE) const override;
std::string GetGameID(const Partition& partition = PARTITION_NONE) const override;
@@ -103,7 +103,7 @@ class VolumeGC : public Volume
Common::Lazy<std::unique_ptr<FileSystem>> m_file_system;
std::unique_ptr<BlobReader> m_pReader;
std::unique_ptr<BlobReader> m_reader;
};
} // namespace
@@ -34,17 +34,17 @@
namespace DiscIO
{
VolumeWii::VolumeWii(std::unique_ptr<BlobReader> reader)
: m_pReader(std::move(reader)), m_game_partition(PARTITION_NONE),
: m_reader(std::move(reader)), m_game_partition(PARTITION_NONE),
m_last_decrypted_block(UINT64_MAX)
{
ASSERT(m_pReader);
ASSERT(m_reader);
m_encrypted = m_pReader->ReadSwapped<u32>(0x60) == u32(0);
m_encrypted = m_reader->ReadSwapped<u32>(0x60) == u32(0);
for (u32 partition_group = 0; partition_group < 4; ++partition_group)
{
const std::optional<u32> number_of_partitions =
m_pReader->ReadSwapped<u32>(0x40000 + (partition_group * 8));
m_reader->ReadSwapped<u32>(0x40000 + (partition_group * 8));
if (!number_of_partitions)
continue;
@@ -63,7 +63,7 @@ VolumeWii::VolumeWii(std::unique_ptr<BlobReader> reader)
const Partition partition(*partition_offset);
const std::optional<u32> partition_type =
m_pReader->ReadSwapped<u32>(*partition_table_offset + (i * 8) + 4);
m_reader->ReadSwapped<u32>(*partition_table_offset + (i * 8) + 4);
if (!partition_type)
continue;
@@ -73,13 +73,13 @@ VolumeWii::VolumeWii(std::unique_ptr<BlobReader> reader)
auto get_ticket = [this, partition]() -> IOS::ES::TicketReader {
std::vector<u8> ticket_buffer(sizeof(IOS::ES::Ticket));
if (!m_pReader->Read(partition.offset, ticket_buffer.size(), ticket_buffer.data()))
if (!m_reader->Read(partition.offset, ticket_buffer.size(), ticket_buffer.data()))
return INVALID_TICKET;
return IOS::ES::TicketReader{std::move(ticket_buffer)};
};
auto get_tmd = [this, partition]() -> IOS::ES::TMDReader {
const std::optional<u32> tmd_size = m_pReader->ReadSwapped<u32>(partition.offset + 0x2a4);
const std::optional<u32> tmd_size = m_reader->ReadSwapped<u32>(partition.offset + 0x2a4);
const std::optional<u64> tmd_address =
ReadSwappedAndShifted(partition.offset + 0x2a8, PARTITION_NONE);
if (!tmd_size || !tmd_address)
@@ -92,7 +92,7 @@ VolumeWii::VolumeWii(std::unique_ptr<BlobReader> reader)
return INVALID_TMD;
}
std::vector<u8> tmd_buffer(*tmd_size);
if (!m_pReader->Read(partition.offset + *tmd_address, *tmd_size, tmd_buffer.data()))
if (!m_reader->Read(partition.offset + *tmd_address, *tmd_size, tmd_buffer.data()))
return INVALID_TMD;
return IOS::ES::TMDReader{std::move(tmd_buffer)};
};
@@ -130,10 +130,10 @@ VolumeWii::~VolumeWii()
{
}
bool VolumeWii::Read(u64 _ReadOffset, u64 _Length, u8* _pBuffer, const Partition& partition) const
bool VolumeWii::Read(u64 offset, u64 length, u8* buffer, const Partition& partition) const
{
if (partition == PARTITION_NONE)
return m_pReader->Read(_ReadOffset, _Length, _pBuffer);
return m_reader->Read(offset, length, buffer);
auto it = m_partitions.find(partition);
if (it == m_partitions.end())
@@ -142,29 +142,29 @@ bool VolumeWii::Read(u64 _ReadOffset, u64 _Length, u8* _pBuffer, const Partition
if (!m_encrypted)
{
return m_pReader->Read(partition.offset + *partition_details.data_offset + _ReadOffset, _Length,
_pBuffer);
return m_reader->Read(partition.offset + *partition_details.data_offset + offset, length,
buffer);
}
if (m_pReader->SupportsReadWiiDecrypted())
return m_pReader->ReadWiiDecrypted(_ReadOffset, _Length, _pBuffer, partition.offset);
if (m_reader->SupportsReadWiiDecrypted())
return m_reader->ReadWiiDecrypted(offset, length, buffer, partition.offset);
mbedtls_aes_context* aes_context = partition_details.key->get();
if (!aes_context)
return false;
std::vector<u8> read_buffer(BLOCK_TOTAL_SIZE);
while (_Length > 0)
while (length > 0)
{
// Calculate offsets
u64 block_offset_on_disc = partition.offset + *partition_details.data_offset +
_ReadOffset / BLOCK_DATA_SIZE * BLOCK_TOTAL_SIZE;
u64 data_offset_in_block = _ReadOffset % BLOCK_DATA_SIZE;
offset / BLOCK_DATA_SIZE * BLOCK_TOTAL_SIZE;
u64 data_offset_in_block = offset % BLOCK_DATA_SIZE;
if (m_last_decrypted_block != block_offset_on_disc)
{
// Read the current block
if (!m_pReader->Read(block_offset_on_disc, BLOCK_TOTAL_SIZE, read_buffer.data()))
if (!m_reader->Read(block_offset_on_disc, BLOCK_TOTAL_SIZE, read_buffer.data()))
return false;
// Decrypt the block's data.
@@ -182,14 +182,14 @@ bool VolumeWii::Read(u64 _ReadOffset, u64 _Length, u8* _pBuffer, const Partition
}
// Copy the decrypted data
u64 copy_size = std::min(_Length, BLOCK_DATA_SIZE - data_offset_in_block);
memcpy(_pBuffer, &m_last_decrypted_block_data[data_offset_in_block],
u64 copy_size = std::min(length, BLOCK_DATA_SIZE - data_offset_in_block);
memcpy(buffer, &m_last_decrypted_block_data[data_offset_in_block],
static_cast<size_t>(copy_size));
// Update offsets
_Length -= copy_size;
_pBuffer += copy_size;
_ReadOffset += copy_size;
length -= copy_size;
buffer += copy_size;
offset += copy_size;
}
return true;
@@ -270,17 +270,17 @@ u64 VolumeWii::PartitionOffsetToRawOffset(u64 offset, const Partition& partition
std::string VolumeWii::GetGameID(const Partition& partition) const
{
char ID[6];
char id[6];
if (!Read(0, 6, (u8*)ID, partition))
if (!Read(0, 6, reinterpret_cast<u8*>(id), partition))
return std::string();
return DecodeString(ID);
return DecodeString(id);
}
Region VolumeWii::GetRegion() const
{
const std::optional<u32> region_code = m_pReader->ReadSwapped<u32>(0x4E000);
const std::optional<u32> region_code = m_reader->ReadSwapped<u32>(0x4E000);
if (!region_code)
return Region::Unknown;
const Region region = static_cast<Region>(*region_code);
@@ -301,12 +301,12 @@ Country VolumeWii::GetCountry(const Partition& partition) const
std::string VolumeWii::GetMakerID(const Partition& partition) const
{
char makerID[2];
char maker_id[2];
if (!Read(0x4, 0x2, (u8*)&makerID, partition))
if (!Read(0x4, 0x2, reinterpret_cast<u8*>(&maker_id), partition))
return std::string();
return DecodeString(makerID);
return DecodeString(maker_id);
}
std::optional<u16> VolumeWii::GetRevision(const Partition& partition) const
@@ -366,17 +366,17 @@ std::optional<u8> VolumeWii::GetDiscNumber(const Partition& partition) const
BlobType VolumeWii::GetBlobType() const
{
return m_pReader->GetBlobType();
return m_reader->GetBlobType();
}
u64 VolumeWii::GetSize() const
{
return m_pReader->GetDataSize();
return m_reader->GetDataSize();
}
u64 VolumeWii::GetRawSize() const
{
return m_pReader->GetRawSize();
return m_reader->GetRawSize();
}
bool VolumeWii::CheckIntegrity(const Partition& partition) const
@@ -394,25 +394,27 @@ bool VolumeWii::CheckIntegrity(const Partition& partition) const
return false;
// Get partition data size
u32 partSizeDiv4;
m_pReader->Read(partition.offset + 0x2BC, 4, (u8*)&partSizeDiv4);
u64 partDataSize = (u64)Common::swap32(partSizeDiv4) * 4;
u32 part_size_div4;
m_reader->Read(partition.offset + 0x2BC, 4, reinterpret_cast<u8*>(&part_size_div4));
const u64 part_data_size = static_cast<u64>(Common::swap32(part_size_div4)) * 4;
u32 nClusters = (u32)(partDataSize / 0x8000);
for (u32 clusterID = 0; clusterID < nClusters; ++clusterID)
const u32 num_clusters = static_cast<u32>(part_data_size / 0x8000);
for (u32 cluster_id = 0; cluster_id < num_clusters; ++cluster_id)
{
u64 clusterOff = partition.offset + *partition_details.data_offset + (u64)clusterID * 0x8000;
const u64 cluster_offset =
partition.offset + *partition_details.data_offset + static_cast<u64>(cluster_id) * 0x8000;
// Read and decrypt the cluster metadata
u8 clusterMDCrypted[0x400];
u8 clusterMD[0x400];
u8 IV[16] = {0};
if (!m_pReader->Read(clusterOff, 0x400, clusterMDCrypted))
u8 cluster_metadata_crypted[0x400];
u8 cluster_metadata[0x400];
u8 iv[16] = {0};
if (!m_reader->Read(cluster_offset, 0x400, cluster_metadata_crypted))
{
WARN_LOG(DISCIO, "Integrity Check: fail at cluster %d: could not read metadata", clusterID);
WARN_LOG(DISCIO, "Integrity Check: fail at cluster %d: could not read metadata", cluster_id);
return false;
}
mbedtls_aes_crypt_cbc(aes_context, MBEDTLS_AES_DECRYPT, 0x400, IV, clusterMDCrypted, clusterMD);
mbedtls_aes_crypt_cbc(aes_context, MBEDTLS_AES_DECRYPT, 0x400, iv, cluster_metadata_crypted,
cluster_metadata);
// Some clusters have invalid data and metadata because they aren't
// meant to be read by the game (for example, holes between files). To
@@ -424,30 +426,30 @@ bool VolumeWii::CheckIntegrity(const Partition& partition) const
// valid clusters. To be improved.
bool meaningless = false;
for (u32 idx = 0x26C; idx < 0x280; ++idx)
if (clusterMD[idx] != 0)
if (cluster_metadata[idx] != 0)
meaningless = true;
if (meaningless)
continue;
u8 clusterData[0x7C00];
if (!Read((u64)clusterID * 0x7C00, 0x7C00, clusterData, partition))
u8 cluster_data[0x7C00];
if (!Read((u64)cluster_id * 0x7C00, 0x7C00, cluster_data, partition))
{
WARN_LOG(DISCIO, "Integrity Check: fail at cluster %d: could not read data", clusterID);
WARN_LOG(DISCIO, "Integrity Check: fail at cluster %d: could not read data", cluster_id);
return false;
}
for (u32 hashID = 0; hashID < 31; ++hashID)
for (u32 hash_id = 0; hash_id < 31; ++hash_id)
{
u8 hash[20];
mbedtls_sha1(clusterData + hashID * 0x400, 0x400, hash);
mbedtls_sha1(cluster_data + hash_id * 0x400, 0x400, hash);
// Note that we do not use strncmp here
if (memcmp(hash, clusterMD + hashID * 20, 20))
if (memcmp(hash, cluster_metadata + hash_id * 20, 20))
{
WARN_LOG(DISCIO, "Integrity Check: fail at cluster %d: hash %d is invalid", clusterID,
hashID);
WARN_LOG(DISCIO, "Integrity Check: fail at cluster %d: hash %d is invalid", cluster_id,
hash_id);
return false;
}
}
@@ -32,7 +32,7 @@ class VolumeWii : public Volume
public:
VolumeWii(std::unique_ptr<BlobReader> reader);
~VolumeWii();
bool Read(u64 _Offset, u64 _Length, u8* _pBuffer, const Partition& partition) const override;
bool Read(u64 offset, u64 length, u8* buffer, const Partition& partition) const override;
bool IsEncryptedAndHashed() const override;
std::vector<Partition> GetPartitions() const override;
Partition GetGamePartition() const override;
@@ -81,7 +81,7 @@ class VolumeWii : public Volume
u32 type;
};
std::unique_ptr<BlobReader> m_pReader;
std::unique_ptr<BlobReader> m_reader;
std::map<Partition, PartitionDetails> m_partitions;
Partition m_game_partition;
bool m_encrypted;

0 comments on commit 66122f0

Please sign in to comment.