From c3fa0d6edf0ed65acdf789debed57cd4aa7955ba Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sun, 4 Jun 2017 10:33:14 +0200 Subject: DiscIO: Use std::optional in Volume and Blob --- Source/Core/DiscIO/FileSystemGCWii.cpp | 108 ++++++++++++++++----------------- 1 file changed, 53 insertions(+), 55 deletions(-) (limited to 'Source/Core/DiscIO/FileSystemGCWii.cpp') diff --git a/Source/Core/DiscIO/FileSystemGCWii.cpp b/Source/Core/DiscIO/FileSystemGCWii.cpp index fc598f851b..e7068ba15d 100644 --- a/Source/Core/DiscIO/FileSystemGCWii.cpp +++ b/Source/Core/DiscIO/FileSystemGCWii.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -128,24 +129,23 @@ bool CFileSystemGCWii::ExportFile(const std::string& _rFullPath, bool CFileSystemGCWii::ExportApploader(const std::string& _rExportFolder) const { - u32 apploader_size; - u32 trailer_size; - const u32 header_size = 0x20; - if (!m_rVolume->ReadSwapped(0x2440 + 0x14, &apploader_size, m_partition) || - !m_rVolume->ReadSwapped(0x2440 + 0x18, &trailer_size, m_partition)) + std::optional apploader_size = m_rVolume->ReadSwapped(0x2440 + 0x14, m_partition); + const std::optional trailer_size = m_rVolume->ReadSwapped(0x2440 + 0x18, m_partition); + constexpr u32 header_size = 0x20; + if (!apploader_size || !trailer_size) return false; - apploader_size += trailer_size + header_size; - DEBUG_LOG(DISCIO, "Apploader size -> %x", apploader_size); + *apploader_size += *trailer_size + header_size; + DEBUG_LOG(DISCIO, "Apploader size -> %x", *apploader_size); - std::vector buffer(apploader_size); - if (m_rVolume->Read(0x2440, apploader_size, buffer.data(), m_partition)) + std::vector buffer(*apploader_size); + if (m_rVolume->Read(0x2440, *apploader_size, buffer.data(), m_partition)) { std::string exportName(_rExportFolder + "/apploader.img"); File::IOFile AppFile(exportName, "wb"); if (AppFile) { - AppFile.WriteBytes(buffer.data(), apploader_size); + AppFile.WriteBytes(buffer.data(), *apploader_size); return true; } } @@ -153,40 +153,38 @@ bool CFileSystemGCWii::ExportApploader(const std::string& _rExportFolder) const return false; } -u64 CFileSystemGCWii::GetBootDOLOffset() const +std::optional CFileSystemGCWii::GetBootDOLOffset() const { - u32 offset = 0; - m_rVolume->ReadSwapped(0x420, &offset, m_partition); - return static_cast(offset) << m_offset_shift; + std::optional offset = m_rVolume->ReadSwapped(0x420, m_partition); + return offset ? static_cast(*offset) << 2 : std::optional(); } -u32 CFileSystemGCWii::GetBootDOLSize(u64 dol_offset) const +std::optional CFileSystemGCWii::GetBootDOLSize(u64 dol_offset) const { - // The dol_offset value is usually obtained by calling GetBootDOLOffset. - // If GetBootDOLOffset fails by returning 0, GetBootDOLSize should also fail. - if (dol_offset == 0) - return 0; - u32 dol_size = 0; - u32 offset = 0; - u32 size = 0; // Iterate through the 7 code segments for (u8 i = 0; i < 7; i++) { - if (!m_rVolume->ReadSwapped(dol_offset + 0x00 + i * 4, &offset, m_partition) || - !m_rVolume->ReadSwapped(dol_offset + 0x90 + i * 4, &size, m_partition)) - return 0; - dol_size = std::max(offset + size, dol_size); + const std::optional offset = + m_rVolume->ReadSwapped(dol_offset + 0x00 + i * 4, m_partition); + const std::optional size = + m_rVolume->ReadSwapped(dol_offset + 0x90 + i * 4, m_partition); + if (!offset || !size) + return {}; + dol_size = std::max(*offset + *size, dol_size); } // Iterate through the 11 data segments for (u8 i = 0; i < 11; i++) { - if (!m_rVolume->ReadSwapped(dol_offset + 0x1c + i * 4, &offset, m_partition) || - !m_rVolume->ReadSwapped(dol_offset + 0xac + i * 4, &size, m_partition)) - return 0; - dol_size = std::max(offset + size, dol_size); + const std::optional offset = + m_rVolume->ReadSwapped(dol_offset + 0x1c + i * 4, m_partition); + const std::optional size = + m_rVolume->ReadSwapped(dol_offset + 0xac + i * 4, m_partition); + if (!offset || !size) + return {}; + dol_size = std::max(*offset + *size, dol_size); } return dol_size; @@ -194,21 +192,22 @@ u32 CFileSystemGCWii::GetBootDOLSize(u64 dol_offset) const bool CFileSystemGCWii::ExportDOL(const std::string& _rExportFolder) const { - u64 DolOffset = GetBootDOLOffset(); - u32 DolSize = GetBootDOLSize(DolOffset); - - if (DolOffset == 0 || DolSize == 0) + std::optional dol_offset = GetBootDOLOffset(); + if (!dol_offset) + return false; + std::optional dol_size = GetBootDOLSize(*dol_offset); + if (!dol_size) return false; - std::vector buffer(DolSize); - if (m_rVolume->Read(DolOffset, DolSize, &buffer[0], m_partition)) + std::vector buffer(*dol_size); + if (m_rVolume->Read(*dol_offset, *dol_size, &buffer[0], m_partition)) { std::string exportName(_rExportFolder + "/boot.dol"); File::IOFile DolFile(exportName, "wb"); if (DolFile) { - DolFile.WriteBytes(&buffer[0], DolSize); + DolFile.WriteBytes(&buffer[0], *dol_size); return true; } } @@ -251,13 +250,12 @@ const SFileInfo* CFileSystemGCWii::FindFileInfo(const std::string& _rFullPath) bool CFileSystemGCWii::DetectFileSystem() { - u32 magic_bytes; - if (m_rVolume->ReadSwapped(0x18, &magic_bytes, m_partition) && magic_bytes == 0x5D1C9EA3) + if (m_rVolume->ReadSwapped(0x18, m_partition) == u32(0x5D1C9EA3)) { m_offset_shift = 2; // Wii file system return true; } - else if (m_rVolume->ReadSwapped(0x1c, &magic_bytes, m_partition) && magic_bytes == 0xC2339F3D) + else if (m_rVolume->ReadSwapped(0x1c, m_partition) == u32(0xC2339F3D)) { m_offset_shift = 0; // GameCube file system return true; @@ -271,18 +269,19 @@ void CFileSystemGCWii::InitFileSystem() m_Initialized = true; // read the whole FST - u32 fst_offset_unshifted; - if (!m_rVolume->ReadSwapped(0x424, &fst_offset_unshifted, m_partition)) + const std::optional fst_offset_unshifted = m_rVolume->ReadSwapped(0x424, m_partition); + if (!fst_offset_unshifted) return; - u64 FSTOffset = static_cast(fst_offset_unshifted) << m_offset_shift; + const u64 FSTOffset = static_cast(*fst_offset_unshifted) << m_offset_shift; // read all fileinfos - u32 name_offset, offset, size; - if (!m_rVolume->ReadSwapped(FSTOffset + 0x0, &name_offset, m_partition) || - !m_rVolume->ReadSwapped(FSTOffset + 0x4, &offset, m_partition) || - !m_rVolume->ReadSwapped(FSTOffset + 0x8, &size, m_partition)) + const std::optional root_name_offset = m_rVolume->ReadSwapped(FSTOffset, m_partition); + const std::optional root_offset = m_rVolume->ReadSwapped(FSTOffset + 0x4, m_partition); + const std::optional root_size = m_rVolume->ReadSwapped(FSTOffset + 0x8, m_partition); + if (!root_name_offset || !root_offset || !root_size) return; - SFileInfo root = {name_offset, static_cast(offset) << m_offset_shift, size}; + SFileInfo root = {*root_name_offset, static_cast(*root_offset) << m_offset_shift, + *root_size}; if (!root.IsDirectory()) return; @@ -307,13 +306,12 @@ void CFileSystemGCWii::InitFileSystem() for (u32 i = 0; i < root.m_FileSize; i++) { const u64 read_offset = FSTOffset + (i * 0xC); - name_offset = 0; - m_rVolume->ReadSwapped(read_offset + 0x0, &name_offset, m_partition); - offset = 0; - m_rVolume->ReadSwapped(read_offset + 0x4, &offset, m_partition); - size = 0; - m_rVolume->ReadSwapped(read_offset + 0x8, &size, m_partition); - m_FileInfoVector.emplace_back(name_offset, static_cast(offset) << m_offset_shift, size); + const std::optional name_offset = m_rVolume->ReadSwapped(read_offset, m_partition); + const std::optional offset = m_rVolume->ReadSwapped(read_offset + 0x4, m_partition); + const std::optional size = m_rVolume->ReadSwapped(read_offset + 0x8, m_partition); + m_FileInfoVector.emplace_back(name_offset.value_or(0), + static_cast(offset.value_or(0)) << m_offset_shift, + size.value_or(0)); NameTableOffset += 0xC; } -- cgit v1.2.3