| 1 | // Copyright 2020 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DiscIO/ScrubbedBlob.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <memory> |
| 8 | #include <string> |
| 9 | #include <utility> |
| 10 | |
| 11 | #include "Common/Align.h" |
| 12 | #include "DiscIO/Blob.h" |
| 13 | #include "DiscIO/DiscScrubber.h" |
| 14 | #include "DiscIO/VolumeDisc.h" |
| 15 | |
| 16 | namespace DiscIO |
| 17 | { |
| 18 | ScrubbedBlob::ScrubbedBlob(std::unique_ptr<BlobReader> blob_reader, DiscScrubber scrubber) |
| 19 | : m_blob_reader(std::move(blob_reader)), m_scrubber(std::move(scrubber)) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | std::unique_ptr<ScrubbedBlob> ScrubbedBlob::Create(const std::string& path) |
| 24 | { |
| 25 | std::unique_ptr<VolumeDisc> disc = CreateDisc(path); |
| 26 | if (!disc) |
| 27 | return nullptr; |
| 28 | |
| 29 | DiscScrubber scrubber; |
| 30 | if (!scrubber.SetupScrub(*disc)) |
| 31 | return nullptr; |
| 32 | |
| 33 | std::unique_ptr<BlobReader> blob = CreateBlobReader(path); |
| 34 | if (!blob) |
| 35 | return nullptr; |
| 36 | |
| 37 | return std::unique_ptr<ScrubbedBlob>(new ScrubbedBlob(std::move(blob), std::move(scrubber))); |
| 38 | } |
| 39 | |
| 40 | std::unique_ptr<BlobReader> ScrubbedBlob::CopyReader() const |
| 41 | { |
| 42 | return std::unique_ptr<ScrubbedBlob>(new ScrubbedBlob(m_blob_reader->CopyReader(), m_scrubber)); |
| 43 | } |
| 44 | |
| 45 | bool ScrubbedBlob::Read(u64 offset, u64 size, u8* out_ptr) |
| 46 | { |
| 47 | while (size > 0) |
| 48 | { |
| 49 | constexpr size_t CLUSTER_SIZE = DiscScrubber::CLUSTER_SIZE; |
| 50 | const u64 bytes_to_read = |
| 51 | std::min(Common::AlignDown(offset + CLUSTER_SIZE, CLUSTER_SIZE) - offset, size); |
| 52 | |
| 53 | if (m_scrubber.CanBlockBeScrubbed(offset)) |
| 54 | { |
| 55 | std::fill_n(out_ptr, bytes_to_read, 0); |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | if (!m_blob_reader->Read(offset, bytes_to_read, out_ptr)) |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | offset += bytes_to_read; |
| 64 | size -= bytes_to_read; |
| 65 | out_ptr += bytes_to_read; |
| 66 | } |
| 67 | |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | } // namespace DiscIO |