From 89c901780edb286df7cfbdce8cb1b38a6cb529e2 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sat, 17 Jun 2017 13:37:33 +0200 Subject: DiscScrubber: Deduplicate code for writing to file --- Source/Core/DiscIO/DiscExtractor.cpp | 52 +++++++++++++----------------------- 1 file changed, 18 insertions(+), 34 deletions(-) (limited to 'Source/Core/DiscIO/DiscExtractor.cpp') diff --git a/Source/Core/DiscIO/DiscExtractor.cpp b/Source/Core/DiscIO/DiscExtractor.cpp index 430ab7f55f..8adbcf02fe 100644 --- a/Source/Core/DiscIO/DiscExtractor.cpp +++ b/Source/Core/DiscIO/DiscExtractor.cpp @@ -34,39 +34,43 @@ u64 ReadFile(const Volume& volume, const Partition& partition, const FileInfo* f return read_length; } -bool ExportFile(const Volume& volume, const Partition& partition, const FileInfo* file_info, +bool ExportData(const Volume& volume, const Partition& partition, u64 offset, u64 size, const std::string& export_filename) { - if (!file_info || file_info->IsDirectory()) - return false; - - u64 remaining_size = file_info->GetSize(); - u64 file_offset = file_info->GetOffset(); - File::IOFile f(export_filename, "wb"); if (!f) return false; - while (remaining_size) + while (size) { // Limit read size to 128 MB - const size_t read_size = static_cast(std::min(remaining_size, 0x08000000)); + const size_t read_size = static_cast(std::min(size, 0x08000000)); std::vector buffer(read_size); - if (!volume.Read(file_offset, read_size, buffer.data(), partition)) + if (!volume.Read(offset, read_size, buffer.data(), partition)) return false; if (!f.WriteBytes(buffer.data(), read_size)) return false; - remaining_size -= read_size; - file_offset += read_size; + size -= read_size; + offset += read_size; } return true; } +bool ExportFile(const Volume& volume, const Partition& partition, const FileInfo* file_info, + const std::string& export_filename) +{ + if (!file_info || file_info->IsDirectory()) + return false; + + return ExportData(volume, partition, file_info->GetOffset(), file_info->GetSize(), + export_filename); +} + bool ExportApploader(const Volume& volume, const Partition& partition, const std::string& export_folder) { @@ -81,17 +85,7 @@ bool ExportApploader(const Volume& volume, const Partition& partition, *apploader_size += *trailer_size + header_size; DEBUG_LOG(DISCIO, "Apploader size -> %x", *apploader_size); - std::vector buffer(*apploader_size); - if (volume.Read(0x2440, *apploader_size, buffer.data(), partition)) - { - const std::string export_name(export_folder + "/apploader.img"); - - File::IOFile apploader_file(export_name, "wb"); - if (apploader_file.WriteBytes(buffer.data(), *apploader_size)) - return true; - } - - return false; + return ExportData(volume, partition, 0x2440, *apploader_size, export_folder + "/apploader.img"); } std::optional GetBootDOLOffset(const Volume& volume, const Partition& partition) @@ -147,17 +141,7 @@ bool ExportDOL(const Volume& volume, const Partition& partition, const std::stri if (!dol_size) return false; - std::vector buffer(*dol_size); - if (volume.Read(*dol_offset, *dol_size, buffer.data(), partition)) - { - const std::string export_name(export_folder + "/boot.dol"); - - File::IOFile dol_file(export_name, "wb"); - if (dol_file.WriteBytes(buffer.data(), *dol_size)) - return true; - } - - return false; + return ExportData(volume, partition, *dol_offset, *dol_size, export_folder + "/boot.dol"); } } // namespace DiscIO -- cgit v1.2.3