Merge pull request #3265 from JosJuice/volumedirectory-simplifications

DiscIO: Small VolumeDirectory simplifications
This commit is contained in:
Markus Wick 2015-12-15 18:36:49 +01:00
commit 2990df3f59
2 changed files with 26 additions and 54 deletions

View File

@ -24,6 +24,9 @@
namespace DiscIO namespace DiscIO
{ {
const size_t CVolumeDirectory::MAX_NAME_LENGTH;
const size_t CVolumeDirectory::MAX_ID_LENGTH;
CVolumeDirectory::CVolumeDirectory(const std::string& _rDirectory, bool _bIsWii, CVolumeDirectory::CVolumeDirectory(const std::string& _rDirectory, bool _bIsWii,
const std::string& _rApploader, const std::string& _rDOL) const std::string& _rApploader, const std::string& _rDOL)
: m_totalNameSize(0) : m_totalNameSize(0)
@ -40,15 +43,11 @@ CVolumeDirectory::CVolumeDirectory(const std::string& _rDirectory, bool _bIsWii,
SetName("Default name"); SetName("Default name");
if (_bIsWii) if (_bIsWii)
{
SetDiskTypeWii(); SetDiskTypeWii();
}
else else
{
SetDiskTypeGC(); SetDiskTypeGC();
}
// Don't load the dol if we've no apploader... // Don't load the DOL if we don't have an apploader
if (SetApploader(_rApploader)) if (SetApploader(_rApploader))
SetDOL(_rDOL); SetDOL(_rDOL);
@ -61,8 +60,7 @@ CVolumeDirectory::~CVolumeDirectory()
bool CVolumeDirectory::IsValidDirectory(const std::string& _rDirectory) bool CVolumeDirectory::IsValidDirectory(const std::string& _rDirectory)
{ {
std::string directoryName = ExtractDirectoryName(_rDirectory); return File::IsDirectory(ExtractDirectoryName(_rDirectory));
return File::IsDirectory(directoryName);
} }
bool CVolumeDirectory::Read(u64 _Offset, u64 _Length, u8* _pBuffer, bool decrypt) const bool CVolumeDirectory::Read(u64 _Offset, u64 _Length, u8* _pBuffer, bool decrypt) const
@ -133,9 +131,7 @@ bool CVolumeDirectory::Read(u64 _Offset, u64 _Length, u8* _pBuffer, bool decrypt
if (fileOffset < fileSize) if (fileOffset < fileSize)
{ {
u64 fileBytes = fileSize - fileOffset; u64 fileBytes = std::min(fileSize - fileOffset, _Length);
if (_Length < fileBytes)
fileBytes = _Length;
if (!file.Seek(fileOffset, SEEK_SET)) if (!file.Seek(fileOffset, SEEK_SET))
return false; return false;
@ -161,24 +157,17 @@ bool CVolumeDirectory::Read(u64 _Offset, u64 _Length, u8* _pBuffer, bool decrypt
std::string CVolumeDirectory::GetUniqueID() const std::string CVolumeDirectory::GetUniqueID() const
{ {
static const size_t ID_LENGTH = 6; return std::string(m_diskHeader.begin(), m_diskHeader.begin() + MAX_ID_LENGTH);
return std::string(m_diskHeader.begin(), m_diskHeader.begin() + ID_LENGTH);
} }
void CVolumeDirectory::SetUniqueID(const std::string& id) void CVolumeDirectory::SetUniqueID(const std::string& id)
{ {
size_t length = id.length(); memcpy(m_diskHeader.data(), id.c_str(), std::min(id.length(), MAX_ID_LENGTH));
if (length > 6)
length = 6;
memcpy(m_diskHeader.data(), id.c_str(), length);
} }
IVolume::ECountry CVolumeDirectory::GetCountry() const IVolume::ECountry CVolumeDirectory::GetCountry() const
{ {
u8 country_code = m_diskHeader[3]; return CountrySwitch(m_diskHeader[3]);
return CountrySwitch(country_code);
} }
std::string CVolumeDirectory::GetMakerID() const std::string CVolumeDirectory::GetMakerID() const
@ -197,19 +186,15 @@ std::string CVolumeDirectory::GetInternalName() const
std::map<IVolume::ELanguage, std::string> CVolumeDirectory::GetNames(bool prefer_long) const std::map<IVolume::ELanguage, std::string> CVolumeDirectory::GetNames(bool prefer_long) const
{ {
std::map<IVolume::ELanguage, std::string> names;
std::string name = GetInternalName(); std::string name = GetInternalName();
if (!name.empty()) if (name.empty())
names[IVolume::ELanguage::LANGUAGE_UNKNOWN] = name; return { { } };
return names; return { { IVolume::LANGUAGE_UNKNOWN, name } };
} }
void CVolumeDirectory::SetName(const std::string& name) void CVolumeDirectory::SetName(const std::string& name)
{ {
size_t length = name.length(); size_t length = std::min(name.length(), MAX_NAME_LENGTH);
if (length > MAX_NAME_LENGTH)
length = MAX_NAME_LENGTH;
memcpy(&m_diskHeader[0x20], name.c_str(), length); memcpy(&m_diskHeader[0x20], name.c_str(), length);
m_diskHeader[length + 0x20] = 0; m_diskHeader[length + 0x20] = 0;
} }
@ -274,10 +259,7 @@ std::string CVolumeDirectory::ExtractDirectoryName(const std::string& _rDirector
void CVolumeDirectory::SetDiskTypeWii() void CVolumeDirectory::SetDiskTypeWii()
{ {
m_diskHeader[0x18] = 0x5d; Write32(0x5d1c9ea3, 0x18, &m_diskHeader);
m_diskHeader[0x19] = 0x1c;
m_diskHeader[0x1a] = 0x9e;
m_diskHeader[0x1b] = 0xa3;
memset(&m_diskHeader[0x1c], 0, 4); memset(&m_diskHeader[0x1c], 0, 4);
m_is_wii = true; m_is_wii = true;
@ -287,10 +269,7 @@ void CVolumeDirectory::SetDiskTypeWii()
void CVolumeDirectory::SetDiskTypeGC() void CVolumeDirectory::SetDiskTypeGC()
{ {
memset(&m_diskHeader[0x18], 0, 4); memset(&m_diskHeader[0x18], 0, 4);
m_diskHeader[0x1c] = 0xc2; Write32(0xc2339f3d, 0x1c, &m_diskHeader);
m_diskHeader[0x1d] = 0x33;
m_diskHeader[0x1e] = 0x9f;
m_diskHeader[0x1f] = 0x3d;
m_is_wii = false; m_is_wii = false;
m_addressShift = 0; m_addressShift = 0;
@ -397,9 +376,7 @@ void CVolumeDirectory::WriteToBuffer(u64 _SrcStartAddress, u64 _SrcLength, const
if (srcOffset < _SrcLength) if (srcOffset < _SrcLength)
{ {
u64 srcBytes = _SrcLength - srcOffset; u64 srcBytes = std::min(_SrcLength - srcOffset, _Length);
if (_Length < srcBytes)
srcBytes = _Length;
memcpy(_pBuffer, _Src + srcOffset, (size_t)srcBytes); memcpy(_pBuffer, _Src + srcOffset, (size_t)srcBytes);
@ -411,15 +388,9 @@ void CVolumeDirectory::WriteToBuffer(u64 _SrcStartAddress, u64 _SrcLength, const
void CVolumeDirectory::PadToAddress(u64 _StartAddress, u64& _Address, u64& _Length, u8*& _pBuffer) const void CVolumeDirectory::PadToAddress(u64 _StartAddress, u64& _Address, u64& _Length, u8*& _pBuffer) const
{ {
if (_StartAddress <= _Address) if (_StartAddress > _Address && _Length > 0)
return;
u64 padBytes = _StartAddress - _Address;
if (padBytes > _Length)
padBytes = _Length;
if (_Length > 0)
{ {
u64 padBytes = std::min(_StartAddress - _Address, _Length);
memset(_pBuffer, 0, (size_t)padBytes); memset(_pBuffer, 0, (size_t)padBytes);
_Length -= padBytes; _Length -= padBytes;
_pBuffer += padBytes; _pBuffer += padBytes;

View File

@ -139,13 +139,14 @@ private:
u64 m_fst_address; u64 m_fst_address;
u64 m_dol_address; u64 m_dol_address;
static const u8 ENTRY_SIZE = 0x0c; static constexpr u8 ENTRY_SIZE = 0x0c;
static const u8 FILE_ENTRY = 0; static constexpr u8 FILE_ENTRY = 0;
static const u8 DIRECTORY_ENTRY = 1; static constexpr u8 DIRECTORY_ENTRY = 1;
static const u64 DISKHEADER_ADDRESS = 0; static constexpr u64 DISKHEADER_ADDRESS = 0;
static const u64 DISKHEADERINFO_ADDRESS = 0x440; static constexpr u64 DISKHEADERINFO_ADDRESS = 0x440;
static const u64 APPLOADER_ADDRESS = 0x2440; static constexpr u64 APPLOADER_ADDRESS = 0x2440;
static const u32 MAX_NAME_LENGTH = 0x3df; static const size_t MAX_NAME_LENGTH = 0x3df;
static const size_t MAX_ID_LENGTH = 6;
}; };
} // namespace } // namespace