Skip to content

Commit

Permalink
Merge pull request #921 from lioncash/unique
Browse files Browse the repository at this point in the history
DiscIO: Move some raw pointers over to unique_ptr
  • Loading branch information
delroth committed Sep 1, 2014
2 parents 4bbf96f + 4cb4687 commit d9950d8
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 48 deletions.
9 changes: 4 additions & 5 deletions Source/Core/DiscIO/DiscScrubber.cpp
Expand Up @@ -6,6 +6,7 @@
#include <cinttypes>
#include <cstddef>
#include <cstdio>
#include <memory>
#include <string>
#include <vector>

Expand Down Expand Up @@ -275,17 +276,17 @@ bool ParsePartitionData(SPartition& _rPartition)

// Ready some stuff
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);
ParsedOK = false;
}
else
{
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
// Header, Header Information, Apploader
Expand Down Expand Up @@ -330,8 +331,6 @@ bool ParsePartitionData(SPartition& _rPartition)
}
}

delete FileSystem;

// Swap back
delete m_Disc;
m_Disc = OldVolume;
Expand Down
19 changes: 9 additions & 10 deletions Source/Core/DiscIO/FileHandlerARC.cpp
Expand Up @@ -5,6 +5,7 @@
#include <cstddef>
#include <cstdio>
#include <cstring>
#include <memory>
#include <string>

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

m_Initialized = ParseBuffer();
}
Expand All @@ -38,13 +38,12 @@ CARCFile::CARCFile(const std::string& _rFilename, u32 offset)
: m_pBuffer(nullptr)
, m_Initialized(false)
{
DiscIO::IBlobReader* pReader = DiscIO::CreateBlobReader(_rFilename.c_str());
if (pReader != nullptr)
std::unique_ptr<IBlobReader> reader(DiscIO::CreateBlobReader(_rFilename));
if (reader != nullptr)
{
u64 FileSize = pReader->GetDataSize() - offset;
u64 FileSize = reader->GetDataSize() - offset;
m_pBuffer = new u8[(u32)FileSize];
pReader->Read(offset, FileSize, m_pBuffer);
delete pReader;
reader->Read(offset, FileSize, m_pBuffer);

m_Initialized = ParseBuffer();
}
Expand Down
3 changes: 1 addition & 2 deletions Source/Core/DiscIO/VolumeGC.cpp
Expand Up @@ -3,6 +3,7 @@
// Refer to the license.txt file included.

#include <cstddef>
#include <memory>
#include <string>
#include <vector>

Expand All @@ -21,8 +22,6 @@ CVolumeGC::CVolumeGC(IBlobReader* _pReader)

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
Expand Down
3 changes: 2 additions & 1 deletion Source/Core/DiscIO/VolumeGC.h
Expand Up @@ -4,6 +4,7 @@

#pragma once

#include <memory>
#include <string>
#include <vector>

Expand Down Expand Up @@ -41,7 +42,7 @@ class CVolumeGC : public IVolume
static StringDecoder GetStringDecoder(ECountry country);

private:
IBlobReader* m_pReader;
std::unique_ptr<IBlobReader> m_pReader;
};

} // namespace
1 change: 0 additions & 1 deletion Source/Core/DiscIO/VolumeWad.cpp
Expand Up @@ -41,7 +41,6 @@ CVolumeWAD::CVolumeWAD(IBlobReader* _pReader)

CVolumeWAD::~CVolumeWAD()
{
delete m_pReader;
}

bool CVolumeWAD::Read(u64 _Offset, u64 _Length, u8* _pBuffer) const
Expand Down
3 changes: 2 additions & 1 deletion Source/Core/DiscIO/VolumeWad.h
Expand Up @@ -4,6 +4,7 @@

#pragma once

#include <memory>
#include <string>
#include <vector>

Expand Down Expand Up @@ -37,7 +38,7 @@ class CVolumeWAD : public IVolume
u64 GetRawSize() const override;

private:
IBlobReader* m_pReader;
std::unique_ptr<IBlobReader> m_pReader;
u32 OpeningBnrOffset, hdr_size, cert_size, tick_size, tmd_size, data_size;
u8 m_Country;
};
Expand Down
12 changes: 4 additions & 8 deletions Source/Core/DiscIO/VolumeWiiCrypted.cpp
Expand Up @@ -21,25 +21,21 @@ namespace DiscIO
CVolumeWiiCrypted::CVolumeWiiCrypted(IBlobReader* _pReader, u64 _VolumeOffset,
const unsigned char* _pVolumeKey)
: m_pReader(_pReader),
m_AES_ctx(new aes_context),
m_pBuffer(nullptr),
m_VolumeOffset(_VolumeOffset),
dataOffset(0x20000),
m_LastDecryptedBlockOffset(-1)
{
m_AES_ctx = new aes_context;
aes_setkey_dec(m_AES_ctx, _pVolumeKey, 128);
aes_setkey_dec(m_AES_ctx.get(), _pVolumeKey, 128);
m_pBuffer = new u8[0x8000];
}


CVolumeWiiCrypted::~CVolumeWiiCrypted()
{
delete m_pReader; // is this really our responsibility?
m_pReader = nullptr;
delete[] m_pBuffer;
m_pBuffer = nullptr;
delete m_AES_ctx;
m_AES_ctx = nullptr;
}

bool CVolumeWiiCrypted::RAWRead( u64 _Offset, u64 _Length, u8* _pBuffer ) const
Expand Down Expand Up @@ -78,7 +74,7 @@ bool CVolumeWiiCrypted::Read(u64 _ReadOffset, u64 _Length, u8* _pBuffer) const
if (m_LastDecryptedBlockOffset != Block)
{
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;
}
Expand Down Expand Up @@ -261,7 +257,7 @@ bool CVolumeWiiCrypted::CheckIntegrity() const
NOTICE_LOG(DISCIO, "Integrity Check: fail at cluster %d: could not read metadata", clusterID);
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
Expand Down
5 changes: 3 additions & 2 deletions Source/Core/DiscIO/VolumeWiiCrypted.h
Expand Up @@ -4,6 +4,7 @@

#pragma once

#include <memory>
#include <string>
#include <vector>
#include <polarssl/aes.h>
Expand Down Expand Up @@ -40,10 +41,10 @@ class CVolumeWiiCrypted : public IVolume
bool CheckIntegrity() const override;

private:
IBlobReader* m_pReader;
std::unique_ptr<IBlobReader> m_pReader;
std::unique_ptr<aes_context> m_AES_ctx;

u8* m_pBuffer;
aes_context* m_AES_ctx;

u64 m_VolumeOffset;
u64 dataOffset;
Expand Down
33 changes: 15 additions & 18 deletions Source/Core/DiscIO/WiiWad.cpp
Expand Up @@ -4,6 +4,7 @@


#include <cstddef>
#include <memory>
#include <string>

#include "Common/Common.h"
Expand Down Expand Up @@ -32,18 +33,16 @@ class CBlobBigEndianReader
DiscIO::IBlobReader& m_rReader;
};

WiiWAD::WiiWAD(const std::string& _rName)
WiiWAD::WiiWAD(const std::string& name)
{
DiscIO::IBlobReader* pReader = DiscIO::CreateBlobReader(_rName);
if (pReader == nullptr || File::IsDirectory(_rName))
std::unique_ptr<IBlobReader> reader(DiscIO::CreateBlobReader(name));
if (reader == nullptr || File::IsDirectory(name))
{
m_Valid = false;
if (pReader) delete pReader;
return;
}

m_Valid = ParseWAD(*pReader);
delete pReader;
m_Valid = ParseWAD(*reader);
}

WiiWAD::~WiiWAD()
Expand Down Expand Up @@ -118,30 +117,28 @@ bool WiiWAD::ParseWAD(DiscIO::IBlobReader& _rReader)
return true;
}

bool WiiWAD::IsWiiWAD(const std::string& _rName)
bool WiiWAD::IsWiiWAD(const std::string& name)
{
DiscIO::IBlobReader* pReader = DiscIO::CreateBlobReader(_rName);
if (pReader == nullptr)
std::unique_ptr<IBlobReader> blob_reader(DiscIO::CreateBlobReader(name));
if (blob_reader == nullptr)
return false;

CBlobBigEndianReader Reader(*pReader);
bool Result = false;
CBlobBigEndianReader big_endian_reader(*blob_reader);
bool result = false;

// check for wii wad
if (Reader.Read32(0x00) == 0x20)
if (big_endian_reader.Read32(0x00) == 0x20)
{
u32 WADTYpe = Reader.Read32(0x04);
switch (WADTYpe)
u32 wad_type = big_endian_reader.Read32(0x04);
switch (wad_type)
{
case 0x49730000:
case 0x69620000:
Result = true;
result = true;
}
}

delete pReader;

return Result;
return result;
}


Expand Down

0 comments on commit d9950d8

Please sign in to comment.