DiscScrubber: Make struct and variable names conformant to the coding style

This commit is contained in:
Lioncash 2017-01-04 15:39:27 -05:00
parent 4a9bc2340b
commit 5b1aae0cbf
2 changed files with 125 additions and 119 deletions

View File

@ -27,14 +27,13 @@ DiscScrubber::~DiscScrubber() = default;
bool DiscScrubber::SetupScrub(const std::string& filename, int block_size) bool DiscScrubber::SetupScrub(const std::string& filename, int block_size)
{ {
bool success = true; m_filename = filename;
m_Filename = filename; m_block_size = block_size;
m_BlockSize = block_size;
if (CLUSTER_SIZE % m_BlockSize != 0) if (CLUSTER_SIZE % m_block_size != 0)
{ {
ERROR_LOG(DISCIO, "Block size %i is not a factor of 0x8000, scrubbing not possible", ERROR_LOG(DISCIO, "Block size %i is not a factor of 0x8000, scrubbing not possible",
m_BlockSize); m_block_size);
return false; return false;
} }
@ -42,9 +41,9 @@ bool DiscScrubber::SetupScrub(const std::string& filename, int block_size)
if (!m_disc) if (!m_disc)
return false; return false;
m_FileSize = m_disc->GetSize(); m_file_size = m_disc->GetSize();
u32 numClusters = (u32)(m_FileSize / CLUSTER_SIZE); u32 numClusters = (u32)(m_file_size / CLUSTER_SIZE);
// Warn if not DVD5 or DVD9 size // Warn if not DVD5 or DVD9 size
if (numClusters != 0x23048 && numClusters != 0x46090) if (numClusters != 0x23048 && numClusters != 0x46090)
@ -54,53 +53,53 @@ bool DiscScrubber::SetupScrub(const std::string& filename, int block_size)
} }
// Table of free blocks // Table of free blocks
m_FreeTable.resize(numClusters, 1); m_free_table.resize(numClusters, 1);
// Fill out table of free blocks // Fill out table of free blocks
success = ParseDisc(); const bool success = ParseDisc();
// Done with it; need it closed for the next part // Done with it; need it closed for the next part
m_disc.reset(); m_disc.reset();
m_BlockCount = 0; m_block_count = 0;
m_isScrubbing = success; m_is_scrubbing = success;
return success; return success;
} }
size_t DiscScrubber::GetNextBlock(File::IOFile& in, u8* buffer) size_t DiscScrubber::GetNextBlock(File::IOFile& in, u8* buffer)
{ {
u64 CurrentOffset = m_BlockCount * m_BlockSize; const u64 current_offset = m_block_count * m_block_size;
u64 i = CurrentOffset / CLUSTER_SIZE; const u64 i = current_offset / CLUSTER_SIZE;
size_t ReadBytes = 0; size_t read_bytes = 0;
if (m_isScrubbing && m_FreeTable[i]) if (m_is_scrubbing && m_free_table[i])
{ {
DEBUG_LOG(DISCIO, "Freeing 0x%016" PRIx64, CurrentOffset); DEBUG_LOG(DISCIO, "Freeing 0x%016" PRIx64, current_offset);
std::fill(buffer, buffer + m_BlockSize, 0x00); std::fill(buffer, buffer + m_block_size, 0x00);
in.Seek(m_BlockSize, SEEK_CUR); in.Seek(m_block_size, SEEK_CUR);
ReadBytes = m_BlockSize; read_bytes = m_block_size;
} }
else else
{ {
DEBUG_LOG(DISCIO, "Used 0x%016" PRIx64, CurrentOffset); DEBUG_LOG(DISCIO, "Used 0x%016" PRIx64, current_offset);
in.ReadArray(buffer, m_BlockSize, &ReadBytes); in.ReadArray(buffer, m_block_size, &read_bytes);
} }
m_BlockCount++; m_block_count++;
return ReadBytes; return read_bytes;
} }
void DiscScrubber::MarkAsUsed(u64 _Offset, u64 _Size) void DiscScrubber::MarkAsUsed(u64 offset, u64 size)
{ {
u64 CurrentOffset = _Offset; u64 current_offset = offset;
u64 EndOffset = CurrentOffset + _Size; const u64 end_offset = current_offset + size;
DEBUG_LOG(DISCIO, "Marking 0x%016" PRIx64 " - 0x%016" PRIx64 " as used", _Offset, EndOffset); DEBUG_LOG(DISCIO, "Marking 0x%016" PRIx64 " - 0x%016" PRIx64 " as used", offset, end_offset);
while ((CurrentOffset < EndOffset) && (CurrentOffset < m_FileSize)) while (current_offset < end_offset && current_offset < m_file_size)
{ {
m_FreeTable[CurrentOffset / CLUSTER_SIZE] = 0; m_free_table[current_offset / CLUSTER_SIZE] = 0;
CurrentOffset += CLUSTER_SIZE; current_offset += CLUSTER_SIZE;
} }
} }
@ -124,17 +123,17 @@ void DiscScrubber::MarkAsUsedE(u64 partition_data_offset, u64 offset, u64 size)
} }
// Helper functions for reading the BE volume // Helper functions for reading the BE volume
bool DiscScrubber::ReadFromVolume(u64 _Offset, u32& _Buffer, bool _Decrypt) bool DiscScrubber::ReadFromVolume(u64 offset, u32& buffer, bool decrypt)
{ {
return m_disc->ReadSwapped(_Offset, &_Buffer, _Decrypt); return m_disc->ReadSwapped(offset, &buffer, decrypt);
} }
bool DiscScrubber::ReadFromVolume(u64 _Offset, u64& _Buffer, bool _Decrypt) bool DiscScrubber::ReadFromVolume(u64 offset, u64& buffer, bool decrypt)
{ {
u32 temp_buffer; u32 temp_buffer;
if (!m_disc->ReadSwapped(_Offset, &temp_buffer, _Decrypt)) if (!m_disc->ReadSwapped(offset, &temp_buffer, decrypt))
return false; return false;
_Buffer = static_cast<u64>(temp_buffer) << 2; buffer = static_cast<u64>(temp_buffer) << 2;
return true; return true;
} }
@ -145,49 +144,53 @@ bool DiscScrubber::ParseDisc()
for (int x = 0; x < 4; x++) for (int x = 0; x < 4; x++)
{ {
if (!ReadFromVolume(0x40000 + (x * 8) + 0, PartitionGroup[x].numPartitions, false) || if (!ReadFromVolume(0x40000 + (x * 8) + 0, m_partition_group[x].num_partitions, false) ||
!ReadFromVolume(0x40000 + (x * 8) + 4, PartitionGroup[x].PartitionsOffset, false)) !ReadFromVolume(0x40000 + (x * 8) + 4, m_partition_group[x].partitions_offset, false))
return false;
// Read all partitions
for (u32 i = 0; i < PartitionGroup[x].numPartitions; i++)
{ {
SPartition Partition;
Partition.GroupNumber = x;
Partition.Number = i;
if (!ReadFromVolume(PartitionGroup[x].PartitionsOffset + (i * 8) + 0, Partition.Offset,
false) ||
!ReadFromVolume(PartitionGroup[x].PartitionsOffset + (i * 8) + 4, Partition.Type,
false) ||
!ReadFromVolume(Partition.Offset + 0x2a4, Partition.Header.TMDSize, false) ||
!ReadFromVolume(Partition.Offset + 0x2a8, Partition.Header.TMDOffset, false) ||
!ReadFromVolume(Partition.Offset + 0x2ac, Partition.Header.CertChainSize, false) ||
!ReadFromVolume(Partition.Offset + 0x2b0, Partition.Header.CertChainOffset, false) ||
!ReadFromVolume(Partition.Offset + 0x2b4, Partition.Header.H3Offset, false) ||
!ReadFromVolume(Partition.Offset + 0x2b8, Partition.Header.DataOffset, false) ||
!ReadFromVolume(Partition.Offset + 0x2bc, Partition.Header.DataSize, false))
return false; return false;
PartitionGroup[x].PartitionsVec.push_back(Partition);
} }
for (auto& rPartition : PartitionGroup[x].PartitionsVec) // Read all partitions
for (u32 i = 0; i < m_partition_group[x].num_partitions; i++)
{ {
const SPartitionHeader& rHeader = rPartition.Header; Partition partition;
MarkAsUsed(rPartition.Offset, 0x2c0); partition.group_number = x;
partition.number = i;
MarkAsUsed(rPartition.Offset + rHeader.TMDOffset, rHeader.TMDSize); if (!ReadFromVolume(m_partition_group[x].partitions_offset + (i * 8) + 0, partition.offset,
MarkAsUsed(rPartition.Offset + rHeader.CertChainOffset, rHeader.CertChainSize); false) ||
MarkAsUsed(rPartition.Offset + rHeader.H3Offset, 0x18000); !ReadFromVolume(m_partition_group[x].partitions_offset + (i * 8) + 4, partition.type,
false) ||
!ReadFromVolume(partition.offset + 0x2a4, partition.header.tmd_size, false) ||
!ReadFromVolume(partition.offset + 0x2a8, partition.header.tmd_offset, false) ||
!ReadFromVolume(partition.offset + 0x2ac, partition.header.cert_chain_size, false) ||
!ReadFromVolume(partition.offset + 0x2b0, partition.header.cert_chain_offset, false) ||
!ReadFromVolume(partition.offset + 0x2b4, partition.header.h3_offset, false) ||
!ReadFromVolume(partition.offset + 0x2b8, partition.header.data_offset, false) ||
!ReadFromVolume(partition.offset + 0x2bc, partition.header.data_size, false))
{
return false;
}
m_partition_group[x].partitions.push_back(partition);
}
for (auto& partition : m_partition_group[x].partitions)
{
const PartitionHeader& header = partition.header;
MarkAsUsed(partition.offset, 0x2c0);
MarkAsUsed(partition.offset + header.tmd_offset, header.tmd_size);
MarkAsUsed(partition.offset + header.cert_chain_offset, header.cert_chain_size);
MarkAsUsed(partition.offset + header.h3_offset, 0x18000);
// This would mark the whole (encrypted) data area // This would mark the whole (encrypted) data area
// we need to parse FST and other crap to find what's free within it! // we need to parse FST and other crap to find what's free within it!
// MarkAsUsed(rPartition.Offset + rHeader.DataOffset, rHeader.DataSize); // MarkAsUsed(partition.offset + header.data_offset, header.data_size);
// Parse Data! This is where the big gain is // Parse Data! This is where the big gain is
if (!ParsePartitionData(rPartition)) if (!ParsePartitionData(partition))
return false; return false;
} }
} }
@ -196,7 +199,7 @@ bool DiscScrubber::ParseDisc()
} }
// Operations dealing with encrypted space are done here - the volume is swapped to allow this // Operations dealing with encrypted space are done here - the volume is swapped to allow this
bool DiscScrubber::ParsePartitionData(SPartition& partition) bool DiscScrubber::ParsePartitionData(Partition& partition)
{ {
bool parsed_ok = true; bool parsed_ok = true;
@ -205,10 +208,10 @@ bool DiscScrubber::ParsePartitionData(SPartition& partition)
m_disc.swap(old_volume); m_disc.swap(old_volume);
// Ready some stuff // Ready some stuff
m_disc = CreateVolumeFromFilename(m_Filename, partition.GroupNumber, partition.Number); m_disc = CreateVolumeFromFilename(m_filename, partition.group_number, partition.number);
if (m_disc == nullptr) if (m_disc == nullptr)
{ {
ERROR_LOG(DISCIO, "Failed to create volume from file %s", m_Filename.c_str()); ERROR_LOG(DISCIO, "Failed to create volume from file %s", m_filename.c_str());
m_disc.swap(old_volume); m_disc.swap(old_volume);
return false; return false;
} }
@ -217,38 +220,41 @@ bool DiscScrubber::ParsePartitionData(SPartition& partition)
if (!filesystem) if (!filesystem)
{ {
ERROR_LOG(DISCIO, "Failed to create filesystem for group %d partition %u", ERROR_LOG(DISCIO, "Failed to create filesystem for group %d partition %u",
partition.GroupNumber, partition.Number); partition.group_number, partition.number);
parsed_ok = false; parsed_ok = false;
} }
else else
{ {
// 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
parsed_ok = parsed_ok && ReadFromVolume(0x2440 + 0x14, partition.Header.ApploaderSize, true); parsed_ok = parsed_ok && ReadFromVolume(0x2440 + 0x14, partition.header.apploader_size, true);
parsed_ok = parsed_ok =
parsed_ok && ReadFromVolume(0x2440 + 0x18, partition.Header.ApploaderTrailerSize, true); parsed_ok && ReadFromVolume(0x2440 + 0x18, partition.header.apploader_trailer_size, true);
MarkAsUsedE(partition.Offset + partition.Header.DataOffset, 0, MarkAsUsedE(partition.offset + partition.header.data_offset, 0,
0x2440 + partition.Header.ApploaderSize + partition.Header.ApploaderTrailerSize); 0x2440 + partition.header.apploader_size + partition.header.apploader_trailer_size);
// DOL // DOL
partition.Header.DOLOffset = filesystem->GetBootDOLOffset(); partition.header.dol_offset = filesystem->GetBootDOLOffset();
partition.Header.DOLSize = filesystem->GetBootDOLSize(partition.Header.DOLOffset); partition.header.dol_size = filesystem->GetBootDOLSize(partition.header.dol_offset);
parsed_ok = parsed_ok && partition.Header.DOLOffset && partition.Header.DOLSize; parsed_ok = parsed_ok && partition.header.dol_offset && partition.header.dol_size;
MarkAsUsedE(partition.Offset + partition.Header.DataOffset, partition.Header.DOLOffset, MarkAsUsedE(partition.offset + partition.header.data_offset, partition.header.dol_offset,
partition.Header.DOLSize); partition.header.dol_size);
// FST // FST
parsed_ok = parsed_ok && ReadFromVolume(0x424, partition.Header.FSTOffset, true); parsed_ok = parsed_ok && ReadFromVolume(0x424, partition.header.fst_offset, true);
parsed_ok = parsed_ok && ReadFromVolume(0x428, partition.Header.FSTSize, true); parsed_ok = parsed_ok && ReadFromVolume(0x428, partition.header.fst_size, true);
MarkAsUsedE(partition.Offset + partition.Header.DataOffset, partition.Header.FSTOffset, MarkAsUsedE(partition.offset + partition.header.data_offset, partition.header.fst_offset,
partition.Header.FSTSize); partition.header.fst_size);
// Go through the filesystem and mark entries as used // Go through the filesystem and mark entries as used
for (SFileInfo file : filesystem->GetFileList()) for (SFileInfo file : filesystem->GetFileList())
{ {
DEBUG_LOG(DISCIO, "%s", file.m_FullPath.empty() ? "/" : file.m_FullPath.c_str()); DEBUG_LOG(DISCIO, "%s", file.m_FullPath.empty() ? "/" : file.m_FullPath.c_str());
if ((file.m_NameOffset & 0x1000000) == 0) if ((file.m_NameOffset & 0x1000000) == 0)
MarkAsUsedE(partition.Offset + partition.Header.DataOffset, file.m_Offset, file.m_FileSize); {
MarkAsUsedE(partition.offset + partition.header.data_offset, file.m_Offset,
file.m_FileSize);
}
} }
} }

View File

@ -37,40 +37,40 @@ public:
size_t GetNextBlock(File::IOFile& in, u8* buffer); size_t GetNextBlock(File::IOFile& in, u8* buffer);
private: private:
struct SPartitionHeader final struct PartitionHeader final
{ {
u8* Ticket[0x2a4]; u8* ticket[0x2a4];
u32 TMDSize; u32 tmd_size;
u64 TMDOffset; u64 tmd_offset;
u32 CertChainSize; u32 cert_chain_size;
u64 CertChainOffset; u64 cert_chain_offset;
// H3Size is always 0x18000 // H3Size is always 0x18000
u64 H3Offset; u64 h3_offset;
u64 DataOffset; u64 data_offset;
u64 DataSize; u64 data_size;
// TMD would be here // TMD would be here
u64 DOLOffset; u64 dol_offset;
u64 DOLSize; u64 dol_size;
u64 FSTOffset; u64 fst_offset;
u64 FSTSize; u64 fst_size;
u32 ApploaderSize; u32 apploader_size;
u32 ApploaderTrailerSize; u32 apploader_trailer_size;
}; };
struct SPartition final struct Partition final
{ {
u32 GroupNumber; u32 group_number;
u32 Number; u32 number;
u64 Offset; u64 offset;
u32 Type; u32 type;
SPartitionHeader Header; PartitionHeader header;
}; };
struct SPartitionGroup final struct PartitionGroup final
{ {
u32 numPartitions; u32 num_partitions;
u64 PartitionsOffset; u64 partitions_offset;
std::vector<SPartition> PartitionsVec; std::vector<Partition> partitions;
}; };
void MarkAsUsed(u64 offset, u64 size); void MarkAsUsed(u64 offset, u64 size);
@ -78,18 +78,18 @@ private:
bool ReadFromVolume(u64 offset, u32& buffer, bool decrypt); bool ReadFromVolume(u64 offset, u32& buffer, bool decrypt);
bool ReadFromVolume(u64 offset, u64& buffer, bool decrypt); bool ReadFromVolume(u64 offset, u64& buffer, bool decrypt);
bool ParseDisc(); bool ParseDisc();
bool ParsePartitionData(SPartition& partition); bool ParsePartitionData(Partition& partition);
std::string m_Filename; std::string m_filename;
std::unique_ptr<IVolume> m_disc; std::unique_ptr<IVolume> m_disc;
std::array<SPartitionGroup, 4> PartitionGroup{}; std::array<PartitionGroup, 4> m_partition_group{};
std::vector<u8> m_FreeTable; std::vector<u8> m_free_table;
u64 m_FileSize = 0; u64 m_file_size = 0;
u64 m_BlockCount = 0; u64 m_block_count = 0;
u32 m_BlockSize = 0; u32 m_block_size = 0;
bool m_isScrubbing = false; bool m_is_scrubbing = false;
}; };
} // namespace DiscIO } // namespace DiscIO