Merge pull request #921 from lioncash/unique

DiscIO: Move some raw pointers over to unique_ptr
This commit is contained in:
Pierre Bourdon 2014-09-01 20:11:18 +02:00
commit d9950d8cab
9 changed files with 40 additions and 48 deletions

View File

@ -6,6 +6,7 @@
#include <cinttypes> #include <cinttypes>
#include <cstddef> #include <cstddef>
#include <cstdio> #include <cstdio>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@ -275,9 +276,9 @@ bool ParsePartitionData(SPartition& _rPartition)
// Ready some stuff // Ready some stuff
m_Disc = CreateVolumeFromFilename(m_Filename, _rPartition.GroupNumber, _rPartition.Number); m_Disc = CreateVolumeFromFilename(m_Filename, _rPartition.GroupNumber, _rPartition.Number);
IFileSystem *FileSystem = CreateFileSystem(m_Disc); std::unique_ptr<IFileSystem> filesystem(CreateFileSystem(m_Disc));
if (!FileSystem) if (!filesystem)
{ {
ERROR_LOG(DISCIO, "Failed to create filesystem for group %d partition %u", _rPartition.GroupNumber, _rPartition.Number); ERROR_LOG(DISCIO, "Failed to create filesystem for group %d partition %u", _rPartition.GroupNumber, _rPartition.Number);
ParsedOK = false; ParsedOK = false;
@ -285,7 +286,7 @@ bool ParsePartitionData(SPartition& _rPartition)
else else
{ {
std::vector<const SFileInfo *> Files; std::vector<const SFileInfo *> Files;
size_t numFiles = FileSystem->GetFileList(Files); size_t numFiles = filesystem->GetFileList(Files);
// Mark things as used which are not in the filesystem // Mark things as used which are not in the filesystem
// Header, Header Information, Apploader // Header, Header Information, Apploader
@ -330,8 +331,6 @@ bool ParsePartitionData(SPartition& _rPartition)
} }
} }
delete FileSystem;
// Swap back // Swap back
delete m_Disc; delete m_Disc;
m_Disc = OldVolume; m_Disc = OldVolume;

View File

@ -5,6 +5,7 @@
#include <cstddef> #include <cstddef>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <memory>
#include <string> #include <string>
#include "Common/Common.h" #include "Common/Common.h"
@ -22,13 +23,12 @@ CARCFile::CARCFile(const std::string& _rFilename)
: m_pBuffer(nullptr) : m_pBuffer(nullptr)
, m_Initialized(false) , m_Initialized(false)
{ {
DiscIO::IBlobReader* pReader = DiscIO::CreateBlobReader(_rFilename.c_str()); std::unique_ptr<IBlobReader> reader(DiscIO::CreateBlobReader(_rFilename));
if (pReader != nullptr) if (reader != nullptr)
{ {
u64 FileSize = pReader->GetDataSize(); u64 FileSize = reader->GetDataSize();
m_pBuffer = new u8[(u32)FileSize]; m_pBuffer = new u8[(u32)FileSize];
pReader->Read(0, FileSize, m_pBuffer); reader->Read(0, FileSize, m_pBuffer);
delete pReader;
m_Initialized = ParseBuffer(); m_Initialized = ParseBuffer();
} }
@ -38,13 +38,12 @@ CARCFile::CARCFile(const std::string& _rFilename, u32 offset)
: m_pBuffer(nullptr) : m_pBuffer(nullptr)
, m_Initialized(false) , m_Initialized(false)
{ {
DiscIO::IBlobReader* pReader = DiscIO::CreateBlobReader(_rFilename.c_str()); std::unique_ptr<IBlobReader> reader(DiscIO::CreateBlobReader(_rFilename));
if (pReader != nullptr) if (reader != nullptr)
{ {
u64 FileSize = pReader->GetDataSize() - offset; u64 FileSize = reader->GetDataSize() - offset;
m_pBuffer = new u8[(u32)FileSize]; m_pBuffer = new u8[(u32)FileSize];
pReader->Read(offset, FileSize, m_pBuffer); reader->Read(offset, FileSize, m_pBuffer);
delete pReader;
m_Initialized = ParseBuffer(); m_Initialized = ParseBuffer();
} }

View File

@ -3,6 +3,7 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <cstddef> #include <cstddef>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@ -21,8 +22,6 @@ CVolumeGC::CVolumeGC(IBlobReader* _pReader)
CVolumeGC::~CVolumeGC() CVolumeGC::~CVolumeGC()
{ {
delete m_pReader;
m_pReader = nullptr; // I don't think this makes any difference, but anyway
} }
bool CVolumeGC::Read(u64 _Offset, u64 _Length, u8* _pBuffer) const bool CVolumeGC::Read(u64 _Offset, u64 _Length, u8* _pBuffer) const

View File

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@ -41,7 +42,7 @@ public:
static StringDecoder GetStringDecoder(ECountry country); static StringDecoder GetStringDecoder(ECountry country);
private: private:
IBlobReader* m_pReader; std::unique_ptr<IBlobReader> m_pReader;
}; };
} // namespace } // namespace

View File

@ -41,7 +41,6 @@ CVolumeWAD::CVolumeWAD(IBlobReader* _pReader)
CVolumeWAD::~CVolumeWAD() CVolumeWAD::~CVolumeWAD()
{ {
delete m_pReader;
} }
bool CVolumeWAD::Read(u64 _Offset, u64 _Length, u8* _pBuffer) const bool CVolumeWAD::Read(u64 _Offset, u64 _Length, u8* _pBuffer) const

View File

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@ -37,7 +38,7 @@ public:
u64 GetRawSize() const override; u64 GetRawSize() const override;
private: private:
IBlobReader* m_pReader; std::unique_ptr<IBlobReader> m_pReader;
u32 OpeningBnrOffset, hdr_size, cert_size, tick_size, tmd_size, data_size; u32 OpeningBnrOffset, hdr_size, cert_size, tick_size, tmd_size, data_size;
u8 m_Country; u8 m_Country;
}; };

View File

@ -21,25 +21,21 @@ namespace DiscIO
CVolumeWiiCrypted::CVolumeWiiCrypted(IBlobReader* _pReader, u64 _VolumeOffset, CVolumeWiiCrypted::CVolumeWiiCrypted(IBlobReader* _pReader, u64 _VolumeOffset,
const unsigned char* _pVolumeKey) const unsigned char* _pVolumeKey)
: m_pReader(_pReader), : m_pReader(_pReader),
m_AES_ctx(new aes_context),
m_pBuffer(nullptr), m_pBuffer(nullptr),
m_VolumeOffset(_VolumeOffset), m_VolumeOffset(_VolumeOffset),
dataOffset(0x20000), dataOffset(0x20000),
m_LastDecryptedBlockOffset(-1) m_LastDecryptedBlockOffset(-1)
{ {
m_AES_ctx = new aes_context; aes_setkey_dec(m_AES_ctx.get(), _pVolumeKey, 128);
aes_setkey_dec(m_AES_ctx, _pVolumeKey, 128);
m_pBuffer = new u8[0x8000]; m_pBuffer = new u8[0x8000];
} }
CVolumeWiiCrypted::~CVolumeWiiCrypted() CVolumeWiiCrypted::~CVolumeWiiCrypted()
{ {
delete m_pReader; // is this really our responsibility?
m_pReader = nullptr;
delete[] m_pBuffer; delete[] m_pBuffer;
m_pBuffer = nullptr; m_pBuffer = nullptr;
delete m_AES_ctx;
m_AES_ctx = nullptr;
} }
bool CVolumeWiiCrypted::RAWRead( u64 _Offset, u64 _Length, u8* _pBuffer ) const bool CVolumeWiiCrypted::RAWRead( u64 _Offset, u64 _Length, u8* _pBuffer ) const
@ -78,7 +74,7 @@ bool CVolumeWiiCrypted::Read(u64 _ReadOffset, u64 _Length, u8* _pBuffer) const
if (m_LastDecryptedBlockOffset != Block) if (m_LastDecryptedBlockOffset != Block)
{ {
memcpy(IV, m_pBuffer + 0x3d0, 16); memcpy(IV, m_pBuffer + 0x3d0, 16);
aes_crypt_cbc(m_AES_ctx, AES_DECRYPT, 0x7C00, IV, m_pBuffer + 0x400, m_LastDecryptedBlock); aes_crypt_cbc(m_AES_ctx.get(), AES_DECRYPT, 0x7C00, IV, m_pBuffer + 0x400, m_LastDecryptedBlock);
m_LastDecryptedBlockOffset = Block; m_LastDecryptedBlockOffset = Block;
} }
@ -261,7 +257,7 @@ bool CVolumeWiiCrypted::CheckIntegrity() const
NOTICE_LOG(DISCIO, "Integrity Check: fail at cluster %d: could not read metadata", clusterID); NOTICE_LOG(DISCIO, "Integrity Check: fail at cluster %d: could not read metadata", clusterID);
return false; return false;
} }
aes_crypt_cbc(m_AES_ctx, AES_DECRYPT, 0x400, IV, clusterMDCrypted, clusterMD); aes_crypt_cbc(m_AES_ctx.get(), AES_DECRYPT, 0x400, IV, clusterMDCrypted, clusterMD);
// Some clusters have invalid data and metadata because they aren't // Some clusters have invalid data and metadata because they aren't

View File

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include <polarssl/aes.h> #include <polarssl/aes.h>
@ -40,10 +41,10 @@ public:
bool CheckIntegrity() const override; bool CheckIntegrity() const override;
private: private:
IBlobReader* m_pReader; std::unique_ptr<IBlobReader> m_pReader;
std::unique_ptr<aes_context> m_AES_ctx;
u8* m_pBuffer; u8* m_pBuffer;
aes_context* m_AES_ctx;
u64 m_VolumeOffset; u64 m_VolumeOffset;
u64 dataOffset; u64 dataOffset;

View File

@ -4,6 +4,7 @@
#include <cstddef> #include <cstddef>
#include <memory>
#include <string> #include <string>
#include "Common/Common.h" #include "Common/Common.h"
@ -32,18 +33,16 @@ private:
DiscIO::IBlobReader& m_rReader; DiscIO::IBlobReader& m_rReader;
}; };
WiiWAD::WiiWAD(const std::string& _rName) WiiWAD::WiiWAD(const std::string& name)
{ {
DiscIO::IBlobReader* pReader = DiscIO::CreateBlobReader(_rName); std::unique_ptr<IBlobReader> reader(DiscIO::CreateBlobReader(name));
if (pReader == nullptr || File::IsDirectory(_rName)) if (reader == nullptr || File::IsDirectory(name))
{ {
m_Valid = false; m_Valid = false;
if (pReader) delete pReader;
return; return;
} }
m_Valid = ParseWAD(*pReader); m_Valid = ParseWAD(*reader);
delete pReader;
} }
WiiWAD::~WiiWAD() WiiWAD::~WiiWAD()
@ -118,30 +117,28 @@ bool WiiWAD::ParseWAD(DiscIO::IBlobReader& _rReader)
return true; return true;
} }
bool WiiWAD::IsWiiWAD(const std::string& _rName) bool WiiWAD::IsWiiWAD(const std::string& name)
{ {
DiscIO::IBlobReader* pReader = DiscIO::CreateBlobReader(_rName); std::unique_ptr<IBlobReader> blob_reader(DiscIO::CreateBlobReader(name));
if (pReader == nullptr) if (blob_reader == nullptr)
return false; return false;
CBlobBigEndianReader Reader(*pReader); CBlobBigEndianReader big_endian_reader(*blob_reader);
bool Result = false; bool result = false;
// check for wii wad // check for wii wad
if (Reader.Read32(0x00) == 0x20) if (big_endian_reader.Read32(0x00) == 0x20)
{ {
u32 WADTYpe = Reader.Read32(0x04); u32 wad_type = big_endian_reader.Read32(0x04);
switch (WADTYpe) switch (wad_type)
{ {
case 0x49730000: case 0x49730000:
case 0x69620000: case 0x69620000:
Result = true; result = true;
} }
} }
delete pReader; return result;
return Result;
} }