diff --git a/Source/Core/DiscIO/CompressedBlob.cpp b/Source/Core/DiscIO/CompressedBlob.cpp index 6170c5ad07..61549e5bda 100644 --- a/Source/Core/DiscIO/CompressedBlob.cpp +++ b/Source/Core/DiscIO/CompressedBlob.cpp @@ -163,11 +163,19 @@ bool CompressFileToBlob(const std::string& infile, const std::string& outfile, u scrubbing = true; } + z_stream z; + memset(&z, 0, sizeof(z)); + if (deflateInit(&z, 9) != Z_OK) + return false; + File::IOFile inf(infile, "rb"); File::IOFile f(outfile, "wb"); if (!f || !inf) + { + deflateEnd(&z); return false; + } callback("Files opened, ready to compress.", 0, arg); @@ -213,23 +221,20 @@ bool CompressFileToBlob(const std::string& infile, const std::string& outfile, u } offsets[i] = position; - // u64 start = i * header.block_size; - // u64 size = header.block_size; - std::fill(in_buf, in_buf + header.block_size, 0); + + size_t read_bytes; if (scrubbing) - DiscScrubber::GetNextBlock(inf, in_buf); + read_bytes = DiscScrubber::GetNextBlock(inf, in_buf); else - inf.ReadBytes(in_buf, header.block_size); - z_stream z; - memset(&z, 0, sizeof(z)); - z.zalloc = Z_NULL; - z.zfree = Z_NULL; - z.opaque = Z_NULL; + inf.ReadArray(in_buf, header.block_size, &read_bytes); + if (read_bytes < header.block_size) + std::fill(in_buf + read_bytes, in_buf + header.block_size, 0); + + int retval = deflateReset(&z); z.next_in = in_buf; z.avail_in = header.block_size; z.next_out = out_buf; z.avail_out = block_size; - int retval = deflateInit(&z, 9); if (retval != Z_OK) { @@ -258,8 +263,6 @@ bool CompressFileToBlob(const std::string& infile, const std::string& outfile, u position += comp_size; num_compressed++; } - - deflateEnd(&z); } header.compressed_data_size = position; @@ -286,6 +289,8 @@ cleanup: delete[] offsets; delete[] hashes; + deflateEnd(&z); + DiscScrubber::Cleanup(); callback("Done compressing disc image.", 1.0f, arg); return true; diff --git a/Source/Core/DiscIO/DiscScrubber.cpp b/Source/Core/DiscIO/DiscScrubber.cpp index 8cdc314ea4..03a45616a4 100644 --- a/Source/Core/DiscIO/DiscScrubber.cpp +++ b/Source/Core/DiscIO/DiscScrubber.cpp @@ -124,24 +124,27 @@ bool SetupScrub(const std::string& filename, int block_size) return success; } -void GetNextBlock(File::IOFile& in, u8* buffer) +size_t GetNextBlock(File::IOFile& in, u8* buffer) { u64 CurrentOffset = m_BlockCount * m_BlockSize; u64 i = CurrentOffset / CLUSTER_SIZE; + size_t ReadBytes = 0; if (m_isScrubbing && m_FreeTable[i]) { DEBUG_LOG(DISCIO, "Freeing 0x%016" PRIx64, CurrentOffset); std::fill(buffer, buffer + m_BlockSize, 0xFF); in.Seek(m_BlockSize, SEEK_CUR); + ReadBytes = m_BlockSize; } else { DEBUG_LOG(DISCIO, "Used 0x%016" PRIx64, CurrentOffset); - in.ReadBytes(buffer, m_BlockSize); + in.ReadArray(buffer, m_BlockSize, &ReadBytes); } m_BlockCount++; + return ReadBytes; } void Cleanup() diff --git a/Source/Core/DiscIO/DiscScrubber.h b/Source/Core/DiscIO/DiscScrubber.h index 0e5943ae2e..2311db4f7b 100644 --- a/Source/Core/DiscIO/DiscScrubber.h +++ b/Source/Core/DiscIO/DiscScrubber.h @@ -25,7 +25,7 @@ namespace DiscScrubber { bool SetupScrub(const std::string& filename, int block_size); -void GetNextBlock(File::IOFile& in, u8* buffer); +size_t GetNextBlock(File::IOFile& in, u8* buffer); void Cleanup(); } // namespace DiscScrubber