Remove CBlobBigEndianReader

We can simply put ReadSwapped directly in IBlobReader.
This commit is contained in:
JosJuice 2017-05-19 21:23:00 +02:00
parent f32482b337
commit fcb2c5bd29
4 changed files with 27 additions and 39 deletions

View File

@ -42,8 +42,18 @@ public:
virtual BlobType GetBlobType() const = 0;
virtual u64 GetRawSize() const = 0;
virtual u64 GetDataSize() const = 0;
// NOT thread-safe - can't call this from multiple threads.
virtual bool Read(u64 offset, u64 size, u8* out_ptr) = 0;
template <typename T>
bool ReadSwapped(u64 offset, T* buffer)
{
T temp;
if (!Read(offset, sizeof(T), reinterpret_cast<u8*>(&temp)))
return false;
*buffer = Common::FromBigEndian(temp);
return true;
}
protected:
IBlobReader() {}
@ -137,24 +147,6 @@ private:
std::array<Cache, CACHE_LINES> m_cache;
};
class CBlobBigEndianReader
{
public:
CBlobBigEndianReader(IBlobReader& reader) : m_reader(reader) {}
template <typename T>
bool ReadSwapped(u64 offset, T* buffer) const
{
T temp;
if (!m_reader.Read(offset, sizeof(T), reinterpret_cast<u8*>(&temp)))
return false;
*buffer = Common::FromBigEndian(temp);
return true;
}
private:
IBlobReader& m_reader;
};
// Factory function - examines the path to choose the right type of IBlobReader, and returns one.
std::unique_ptr<IBlobReader> CreateBlobReader(const std::string& filename);

View File

@ -88,13 +88,12 @@ std::unique_ptr<IVolume> CreateVolumeFromFilename(const std::string& filename)
std::unique_ptr<IBlobReader> reader(CreateBlobReader(filename));
if (reader == nullptr)
return nullptr;
CBlobBigEndianReader be_reader(*reader);
// Check for Wii
u32 wii_magic = 0;
be_reader.ReadSwapped(0x18, &wii_magic);
reader->ReadSwapped(0x18, &wii_magic);
u32 wii_container_magic = 0;
be_reader.ReadSwapped(0x60, &wii_container_magic);
reader->ReadSwapped(0x60, &wii_container_magic);
if (wii_magic == 0x5D1C9EA3 && wii_container_magic != 0)
return std::make_unique<CVolumeGC>(std::move(reader));
if (wii_magic == 0x5D1C9EA3 && wii_container_magic == 0)
@ -103,13 +102,13 @@ std::unique_ptr<IVolume> CreateVolumeFromFilename(const std::string& filename)
// Check for WAD
// 0x206962 for boot2 wads
u32 wad_magic = 0;
be_reader.ReadSwapped(0x02, &wad_magic);
reader->ReadSwapped(0x02, &wad_magic);
if (wad_magic == 0x00204973 || wad_magic == 0x00206962)
return std::make_unique<CVolumeWAD>(std::move(reader));
// Check for GC
u32 gc_magic = 0;
be_reader.ReadSwapped(0x1C, &gc_magic);
reader->ReadSwapped(0x1C, &gc_magic);
if (gc_magic == 0xC2339F3D)
return std::make_unique<CVolumeGC>(std::move(reader));

View File

@ -36,28 +36,27 @@ CVolumeWiiCrypted::CVolumeWiiCrypted(std::unique_ptr<IBlobReader> reader)
_assert_(m_pReader);
// Get decryption keys for all partitions
CBlobBigEndianReader big_endian_reader(*m_pReader.get());
for (u32 partition_group = 0; partition_group < 4; ++partition_group)
{
u32 number_of_partitions;
if (!big_endian_reader.ReadSwapped(0x40000 + (partition_group * 8), &number_of_partitions))
if (!m_pReader->ReadSwapped(0x40000 + (partition_group * 8), &number_of_partitions))
continue;
u32 read_buffer;
if (!big_endian_reader.ReadSwapped(0x40000 + (partition_group * 8) + 4, &read_buffer))
if (!m_pReader->ReadSwapped(0x40000 + (partition_group * 8) + 4, &read_buffer))
continue;
const u64 partition_table_offset = (u64)read_buffer << 2;
for (u32 i = 0; i < number_of_partitions; i++)
{
if (!big_endian_reader.ReadSwapped(partition_table_offset + (i * 8), &read_buffer))
if (!m_pReader->ReadSwapped(partition_table_offset + (i * 8), &read_buffer))
continue;
const u64 partition_offset = (u64)read_buffer << 2;
if (m_game_partition == PARTITION_NONE)
{
u32 partition_type;
if (!big_endian_reader.ReadSwapped(partition_table_offset + (i * 8) + 4, &partition_type))
if (!m_pReader->ReadSwapped(partition_table_offset + (i * 8) + 4, &partition_type))
continue;
if (partition_type == 0)
@ -95,7 +94,7 @@ CVolumeWiiCrypted::CVolumeWiiCrypted(std::unique_ptr<IBlobReader> reader)
else
{
u8 key_number = 0;
if (!big_endian_reader.ReadSwapped(partition_offset + 0x1f1, &key_number))
if (!m_pReader->ReadSwapped(partition_offset + 0x1f1, &key_number))
continue;
common_key = (key_number == 1) ? common_key_korean : common_key_standard;
}

View File

@ -34,7 +34,7 @@ std::vector<u8> CreateWADEntry(IBlobReader& reader, u32 size, u64 offset)
return buffer;
}
bool IsWiiWAD(const CBlobBigEndianReader& reader)
bool IsWiiWAD(IBlobReader& reader)
{
u32 header_size = 0;
u32 header_type = 0;
@ -61,9 +61,7 @@ WiiWAD::~WiiWAD()
bool WiiWAD::ParseWAD()
{
CBlobBigEndianReader big_endian_reader(*m_reader);
if (!IsWiiWAD(big_endian_reader))
if (!IsWiiWAD(*m_reader))
return false;
u32 certificate_chain_size;
@ -73,12 +71,12 @@ bool WiiWAD::ParseWAD()
u32 data_app_size;
u32 footer_size;
if (!big_endian_reader.ReadSwapped(0x08, &certificate_chain_size) ||
!big_endian_reader.ReadSwapped(0x0C, &reserved) ||
!big_endian_reader.ReadSwapped(0x10, &ticket_size) ||
!big_endian_reader.ReadSwapped(0x14, &tmd_size) ||
!big_endian_reader.ReadSwapped(0x18, &data_app_size) ||
!big_endian_reader.ReadSwapped(0x1C, &footer_size))
if (!m_reader->ReadSwapped(0x08, &certificate_chain_size) ||
!m_reader->ReadSwapped(0x0C, &reserved) ||
!m_reader->ReadSwapped(0x10, &ticket_size) ||
!m_reader->ReadSwapped(0x14, &tmd_size) ||
!m_reader->ReadSwapped(0x18, &data_app_size) ||
!m_reader->ReadSwapped(0x1C, &footer_size))
return false;
if (MAX_LOGLEVEL >= LogTypes::LOG_LEVELS::LDEBUG)