From 26b21e3186386df015b02eb23dacd04f86669d8f Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sun, 12 Apr 2020 21:45:55 +0200 Subject: [PATCH 1/3] DiscIO: Fix decompressing writing too little sometimes This issue cannot happen with good dumps due to their size, but it can happen with trimmed dumps. --- Source/Core/DiscIO/CompressedBlob.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Source/Core/DiscIO/CompressedBlob.cpp b/Source/Core/DiscIO/CompressedBlob.cpp index 37af397b38..1fe8c4c921 100644 --- a/Source/Core/DiscIO/CompressedBlob.cpp +++ b/Source/Core/DiscIO/CompressedBlob.cpp @@ -372,6 +372,8 @@ bool DecompressBlobToFile(const std::string& infile_path, const std::string& out static const size_t BUFFER_BLOCKS = 32; size_t buffer_size = header.block_size * BUFFER_BLOCKS; size_t last_buffer_size = header.block_size * (header.num_blocks % BUFFER_BLOCKS); + if (last_buffer_size == 0) + last_buffer_size = buffer_size; std::vector buffer(buffer_size); u32 num_buffers = (header.num_blocks + BUFFER_BLOCKS - 1) / BUFFER_BLOCKS; int progress_monitor = std::max(1, num_buffers / 100); From 3aa463cdaee872aa220fdc474a6e47584e3f3190 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sun, 12 Apr 2020 21:47:10 +0200 Subject: [PATCH 2/3] DiscIO: Fix decompressing writing too much sometimes This issue cannot happen with good dumps due to their size, but it can happen with trimmed dumps. --- Source/Core/DiscIO/CompressedBlob.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Core/DiscIO/CompressedBlob.cpp b/Source/Core/DiscIO/CompressedBlob.cpp index 1fe8c4c921..f1ab226025 100644 --- a/Source/Core/DiscIO/CompressedBlob.cpp +++ b/Source/Core/DiscIO/CompressedBlob.cpp @@ -371,7 +371,7 @@ bool DecompressBlobToFile(const std::string& infile_path, const std::string& out const CompressedBlobHeader& header = reader->GetHeader(); static const size_t BUFFER_BLOCKS = 32; size_t buffer_size = header.block_size * BUFFER_BLOCKS; - size_t last_buffer_size = header.block_size * (header.num_blocks % BUFFER_BLOCKS); + size_t last_buffer_size = header.data_size % buffer_size; if (last_buffer_size == 0) last_buffer_size = buffer_size; std::vector buffer(buffer_size); From 19e9a9c94543701bdff6831c26ee6daff0acb73f Mon Sep 17 00:00:00 2001 From: JosJuice Date: Wed, 15 Apr 2020 22:11:31 +0200 Subject: [PATCH 3/3] DiscIO: Clean up decompression size calculation We can use subtraction and std::min instead of modulo and explicit if statements. This commit does not change the behavior. --- Source/Core/DiscIO/CompressedBlob.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Source/Core/DiscIO/CompressedBlob.cpp b/Source/Core/DiscIO/CompressedBlob.cpp index f1ab226025..5248170683 100644 --- a/Source/Core/DiscIO/CompressedBlob.cpp +++ b/Source/Core/DiscIO/CompressedBlob.cpp @@ -371,9 +371,6 @@ bool DecompressBlobToFile(const std::string& infile_path, const std::string& out const CompressedBlobHeader& header = reader->GetHeader(); static const size_t BUFFER_BLOCKS = 32; size_t buffer_size = header.block_size * BUFFER_BLOCKS; - size_t last_buffer_size = header.data_size % buffer_size; - if (last_buffer_size == 0) - last_buffer_size = buffer_size; std::vector buffer(buffer_size); u32 num_buffers = (header.num_blocks + BUFFER_BLOCKS - 1) / BUFFER_BLOCKS; int progress_monitor = std::max(1, num_buffers / 100); @@ -391,8 +388,9 @@ bool DecompressBlobToFile(const std::string& infile_path, const std::string& out break; } } - const size_t sz = i == num_buffers - 1 ? last_buffer_size : buffer_size; - reader->Read(i * buffer_size, sz, buffer.data()); + const u64 inpos = i * buffer_size; + const u64 sz = std::min(buffer_size, header.data_size - inpos); + reader->Read(inpos, sz, buffer.data()); if (!outfile.WriteBytes(buffer.data(), sz)) { PanicAlertT("Failed to write the output file \"%s\".\n"