Merge pull request #2932 from lioncash/disc

VolumeCreator: Use a unique_ptr in CreateVolumeFromFilename
This commit is contained in:
flacs 2015-09-01 04:09:05 +02:00
commit 0ba7a65f08
8 changed files with 34 additions and 37 deletions

View File

@ -4,7 +4,9 @@
#include <cstddef>
#include <cstring>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <polarssl/aes.h>
@ -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<IBlobReader> 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<IBlobReader> 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;
@ -128,7 +120,7 @@ bool IsVolumeWadFile(const IVolume *_rVolume)
return (Common::swap32(MagicWord) == 0x00204973 || Common::swap32(MagicWord) == 0x00206962);
}
void VolumeKeyForParition(IBlobReader& _rReader, u64 offset, u8* VolumeKey)
void VolumeKeyForPartition(IBlobReader& _rReader, u64 offset, u8* VolumeKey)
{
CBlobBigEndianReader Reader(_rReader);
@ -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<IBlobReader> 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);
VolumeKeyForPartition(*reader, rPartition.Offset, VolumeKey);
return new CVolumeWiiCrypted(std::move(reader), rPartition.Offset, VolumeKey);
}
}

View File

@ -18,6 +18,6 @@ IVolume* CreateVolumeFromFilename(const std::string& _rFilename, u32 _PartitionG
IVolume* CreateVolumeFromDirectory(const std::string& _rDirectory, bool _bIsWii, const std::string& _rApploader = "", const std::string& _rDOL = "");
bool IsVolumeWiiDisc(const IVolume *_rVolume);
bool IsVolumeWadFile(const IVolume *_rVolume);
void VolumeKeyForParition(IBlobReader& _rReader, u64 offset, u8* VolumeKey);
void VolumeKeyForPartition(IBlobReader& _rReader, u64 offset, u8* VolumeKey);
} // namespace

View File

@ -6,6 +6,7 @@
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "Common/ColorUtil.h"
@ -20,8 +21,8 @@
namespace DiscIO
{
CVolumeGC::CVolumeGC(IBlobReader* _pReader)
: m_pReader(_pReader)
CVolumeGC::CVolumeGC(std::unique_ptr<IBlobReader> reader)
: m_pReader(std::move(reader))
{}
CVolumeGC::~CVolumeGC()

View File

@ -22,7 +22,7 @@ class IBlobReader;
class CVolumeGC : public IVolume
{
public:
CVolumeGC(IBlobReader* _pReader);
CVolumeGC(std::unique_ptr<IBlobReader> reader);
~CVolumeGC();
bool Read(u64 _Offset, u64 _Length, u8* _pBuffer, bool decrypt = false) const override;
std::string GetUniqueID() const override;

View File

@ -4,7 +4,9 @@
#include <cstddef>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#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<IBlobReader> 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

View File

@ -24,7 +24,7 @@ class IBlobReader;
class CVolumeWAD : public IVolume
{
public:
CVolumeWAD(IBlobReader* _pReader);
CVolumeWAD(std::unique_ptr<IBlobReader> reader);
~CVolumeWAD();
bool Read(u64 _Offset, u64 _Length, u8* _pBuffer, bool decrypt = false) const override;
bool GetTitleID(u8* _pBuffer) const override;

View File

@ -5,7 +5,9 @@
#include <cstddef>
#include <cstring>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <polarssl/aes.h>
#include <polarssl/sha1.h>
@ -25,9 +27,9 @@
namespace DiscIO
{
CVolumeWiiCrypted::CVolumeWiiCrypted(IBlobReader* _pReader, u64 _VolumeOffset,
CVolumeWiiCrypted::CVolumeWiiCrypted(std::unique_ptr<IBlobReader> 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),
@ -44,7 +46,7 @@ bool CVolumeWiiCrypted::ChangePartition(u64 offset)
m_LastDecryptedBlockOffset = -1;
u8 volume_key[16];
DiscIO::VolumeKeyForParition(*m_pReader, offset, volume_key);
DiscIO::VolumeKeyForPartition(*m_pReader, offset, volume_key);
aes_setkey_dec(m_AES_ctx.get(), volume_key, 128);
return true;
}

View File

@ -23,7 +23,7 @@ class IBlobReader;
class CVolumeWiiCrypted : public IVolume
{
public:
CVolumeWiiCrypted(IBlobReader* _pReader, u64 _VolumeOffset, const unsigned char* _pVolumeKey);
CVolumeWiiCrypted(std::unique_ptr<IBlobReader> 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;