ScrubbedBlob.cpp 1.7 KB

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