From 1db1a8aacf7c5d0d8a7e36f2c6f2e452b0971ed0 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 31 Aug 2015 19:27:18 -0400 Subject: [PATCH] VolumeCreator: Use a unique_ptr in CreateVolumeFromFilename --- Source/Core/DiscIO/VolumeCreator.cpp | 42 ++++++++++--------------- Source/Core/DiscIO/VolumeGC.cpp | 5 +-- Source/Core/DiscIO/VolumeGC.h | 2 +- Source/Core/DiscIO/VolumeWad.cpp | 6 ++-- Source/Core/DiscIO/VolumeWad.h | 2 +- Source/Core/DiscIO/VolumeWiiCrypted.cpp | 6 ++-- Source/Core/DiscIO/VolumeWiiCrypted.h | 2 +- 7 files changed, 31 insertions(+), 34 deletions(-) diff --git a/Source/Core/DiscIO/VolumeCreator.cpp b/Source/Core/DiscIO/VolumeCreator.cpp index fdcf4f955e..58f5bee187 100644 --- a/Source/Core/DiscIO/VolumeCreator.cpp +++ b/Source/Core/DiscIO/VolumeCreator.cpp @@ -4,7 +4,9 @@ #include #include +#include #include +#include #include #include @@ -69,35 +71,26 @@ static const unsigned char s_master_key_korean[16] = { 0x13,0xf2,0xfe,0xfb,0xba,0x4c,0x9b,0x7e }; -static IVolume* CreateVolumeFromCryptedWiiImage(IBlobReader& _rReader, u32 _PartitionGroup, u32 _VolumeType, u32 _VolumeNum); +static IVolume* CreateVolumeFromCryptedWiiImage(std::unique_ptr reader, u32 _PartitionGroup, u32 _VolumeType, u32 _VolumeNum); EDiscType GetDiscType(IBlobReader& _rReader); IVolume* CreateVolumeFromFilename(const std::string& _rFilename, u32 _PartitionGroup, u32 _VolumeNum) { - IBlobReader* pReader = CreateBlobReader(_rFilename); - if (pReader == nullptr) + std::unique_ptr reader(CreateBlobReader(_rFilename)); + if (reader == nullptr) return nullptr; - switch (GetDiscType(*pReader)) + switch (GetDiscType(*reader)) { case DISC_TYPE_WII: case DISC_TYPE_GC: - return new CVolumeGC(pReader); + return new CVolumeGC(std::move(reader)); case DISC_TYPE_WAD: - return new CVolumeWAD(pReader); + return new CVolumeWAD(std::move(reader)); case DISC_TYPE_WII_CONTAINER: - { - IVolume* pVolume = CreateVolumeFromCryptedWiiImage(*pReader, _PartitionGroup, 0, _VolumeNum); - - if (pVolume == nullptr) - { - delete pReader; - } - - return pVolume; - } + return CreateVolumeFromCryptedWiiImage(std::move(reader), _PartitionGroup, 0, _VolumeNum); case DISC_TYPE_UNK: default: @@ -106,7 +99,6 @@ IVolume* CreateVolumeFromFilename(const std::string& _rFilename, u32 _PartitionG Filename += ext; NOTICE_LOG(DISCIO, "%s does not have the Magic word for a gcm, wiidisc or wad file\n" "Set Log Verbosity to Warning and attempt to load the game again to view the values", Filename.c_str()); - delete pReader; } return nullptr; @@ -153,12 +145,12 @@ void VolumeKeyForParition(IBlobReader& _rReader, u64 offset, u8* VolumeKey) aes_crypt_cbc(&AES_ctx, AES_DECRYPT, 16, IV, SubKey, VolumeKey); } -static IVolume* CreateVolumeFromCryptedWiiImage(IBlobReader& _rReader, u32 _PartitionGroup, u32 _VolumeType, u32 _VolumeNum) +static IVolume* CreateVolumeFromCryptedWiiImage(std::unique_ptr reader, u32 _PartitionGroup, u32 _VolumeType, u32 _VolumeNum) { - CBlobBigEndianReader Reader(_rReader); + CBlobBigEndianReader big_endian_reader(*reader); - u32 numPartitions = Reader.Read32(0x40000 + (_PartitionGroup * 8)); - u64 PartitionsOffset = (u64)Reader.Read32(0x40000 + (_PartitionGroup * 8) + 4) << 2; + u32 numPartitions = big_endian_reader.Read32(0x40000 + (_PartitionGroup * 8)); + u64 PartitionsOffset = (u64)big_endian_reader.Read32(0x40000 + (_PartitionGroup * 8) + 4) << 2; // Check if we're looking for a valid partition if ((int)_VolumeNum != -1 && _VolumeNum > numPartitions) @@ -184,8 +176,8 @@ static IVolume* CreateVolumeFromCryptedWiiImage(IBlobReader& _rReader, u32 _Part for (u32 i = 0; i < numPartitions; i++) { SPartition Partition; - Partition.Offset = ((u64)Reader.Read32(PartitionsOffset + (i * 8) + 0)) << 2; - Partition.Type = Reader.Read32(PartitionsOffset + (i * 8) + 4); + Partition.Offset = ((u64)big_endian_reader.Read32(PartitionsOffset + (i * 8) + 0)) << 2; + Partition.Type = big_endian_reader.Read32(PartitionsOffset + (i * 8) + 4); group.PartitionsVec.push_back(Partition); } } @@ -200,8 +192,8 @@ static IVolume* CreateVolumeFromCryptedWiiImage(IBlobReader& _rReader, u32 _Part if ((rPartition.Type == _VolumeType && (int)_VolumeNum == -1) || i == _VolumeNum) { u8 VolumeKey[16]; - VolumeKeyForParition(_rReader, rPartition.Offset, VolumeKey); - return new CVolumeWiiCrypted(&_rReader, rPartition.Offset, VolumeKey); + VolumeKeyForParition(*reader, rPartition.Offset, VolumeKey); + return new CVolumeWiiCrypted(std::move(reader), rPartition.Offset, VolumeKey); } } diff --git a/Source/Core/DiscIO/VolumeGC.cpp b/Source/Core/DiscIO/VolumeGC.cpp index f2e54bdde8..f6b4c6ae64 100644 --- a/Source/Core/DiscIO/VolumeGC.cpp +++ b/Source/Core/DiscIO/VolumeGC.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include "Common/ColorUtil.h" @@ -20,8 +21,8 @@ namespace DiscIO { -CVolumeGC::CVolumeGC(IBlobReader* _pReader) - : m_pReader(_pReader) +CVolumeGC::CVolumeGC(std::unique_ptr reader) + : m_pReader(std::move(reader)) {} CVolumeGC::~CVolumeGC() diff --git a/Source/Core/DiscIO/VolumeGC.h b/Source/Core/DiscIO/VolumeGC.h index c1abcb28d2..7a409657a2 100644 --- a/Source/Core/DiscIO/VolumeGC.h +++ b/Source/Core/DiscIO/VolumeGC.h @@ -22,7 +22,7 @@ class IBlobReader; class CVolumeGC : public IVolume { public: - CVolumeGC(IBlobReader* _pReader); + CVolumeGC(std::unique_ptr reader); ~CVolumeGC(); bool Read(u64 _Offset, u64 _Length, u8* _pBuffer, bool decrypt = false) const override; std::string GetUniqueID() const override; diff --git a/Source/Core/DiscIO/VolumeWad.cpp b/Source/Core/DiscIO/VolumeWad.cpp index a86c944844..f37e165b56 100644 --- a/Source/Core/DiscIO/VolumeWad.cpp +++ b/Source/Core/DiscIO/VolumeWad.cpp @@ -4,7 +4,9 @@ #include #include +#include #include +#include #include #include "Common/CommonTypes.h" @@ -18,8 +20,8 @@ namespace DiscIO { -CVolumeWAD::CVolumeWAD(IBlobReader* _pReader) - : m_pReader(_pReader), m_offset(0), m_tmd_offset(0), m_opening_bnr_offset(0), +CVolumeWAD::CVolumeWAD(std::unique_ptr reader) + : m_pReader(std::move(reader)), m_offset(0), m_tmd_offset(0), m_opening_bnr_offset(0), m_hdr_size(0), m_cert_size(0), m_tick_size(0), m_tmd_size(0), m_data_size(0) { // Source: http://wiibrew.org/wiki/WAD_files diff --git a/Source/Core/DiscIO/VolumeWad.h b/Source/Core/DiscIO/VolumeWad.h index 16168aefad..eb38542fa6 100644 --- a/Source/Core/DiscIO/VolumeWad.h +++ b/Source/Core/DiscIO/VolumeWad.h @@ -24,7 +24,7 @@ class IBlobReader; class CVolumeWAD : public IVolume { public: - CVolumeWAD(IBlobReader* _pReader); + CVolumeWAD(std::unique_ptr reader); ~CVolumeWAD(); bool Read(u64 _Offset, u64 _Length, u8* _pBuffer, bool decrypt = false) const override; bool GetTitleID(u8* _pBuffer) const override; diff --git a/Source/Core/DiscIO/VolumeWiiCrypted.cpp b/Source/Core/DiscIO/VolumeWiiCrypted.cpp index 5701dab1cc..adfe408e3e 100644 --- a/Source/Core/DiscIO/VolumeWiiCrypted.cpp +++ b/Source/Core/DiscIO/VolumeWiiCrypted.cpp @@ -5,7 +5,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -25,9 +27,9 @@ namespace DiscIO { -CVolumeWiiCrypted::CVolumeWiiCrypted(IBlobReader* _pReader, u64 _VolumeOffset, +CVolumeWiiCrypted::CVolumeWiiCrypted(std::unique_ptr reader, u64 _VolumeOffset, const unsigned char* _pVolumeKey) - : m_pReader(_pReader), + : m_pReader(std::move(reader)), m_AES_ctx(new aes_context), m_pBuffer(nullptr), m_VolumeOffset(_VolumeOffset), diff --git a/Source/Core/DiscIO/VolumeWiiCrypted.h b/Source/Core/DiscIO/VolumeWiiCrypted.h index 88773af68b..32813a7c62 100644 --- a/Source/Core/DiscIO/VolumeWiiCrypted.h +++ b/Source/Core/DiscIO/VolumeWiiCrypted.h @@ -23,7 +23,7 @@ class IBlobReader; class CVolumeWiiCrypted : public IVolume { public: - CVolumeWiiCrypted(IBlobReader* _pReader, u64 _VolumeOffset, const unsigned char* _pVolumeKey); + CVolumeWiiCrypted(std::unique_ptr reader, u64 _VolumeOffset, const unsigned char* _pVolumeKey); ~CVolumeWiiCrypted(); bool Read(u64 _Offset, u64 _Length, u8* _pBuffer, bool decrypt) const override; bool GetTitleID(u8* _pBuffer) const override;