Blob.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Copyright 2008 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "DiscIO/Blob.h"
  4. #include <algorithm>
  5. #include <cstddef>
  6. #include <limits>
  7. #include <memory>
  8. #include <string>
  9. #include <utility>
  10. #include "Common/CommonTypes.h"
  11. #include "Common/IOFile.h"
  12. #include "Common/MsgHandler.h"
  13. #include "DiscIO/CISOBlob.h"
  14. #include "DiscIO/CompressedBlob.h"
  15. #include "DiscIO/DirectoryBlob.h"
  16. #include "DiscIO/FileBlob.h"
  17. #include "DiscIO/NFSBlob.h"
  18. #include "DiscIO/SplitFileBlob.h"
  19. #include "DiscIO/TGCBlob.h"
  20. #include "DiscIO/WIABlob.h"
  21. #include "DiscIO/WbfsBlob.h"
  22. namespace DiscIO
  23. {
  24. std::string GetName(BlobType blob_type, bool translate)
  25. {
  26. const auto translate_str = [translate](const std::string& str) {
  27. return translate ? Common::GetStringT(str.c_str()) : str;
  28. };
  29. switch (blob_type)
  30. {
  31. case BlobType::PLAIN:
  32. return "ISO";
  33. case BlobType::DIRECTORY:
  34. return translate_str("Directory");
  35. case BlobType::GCZ:
  36. return "GCZ";
  37. case BlobType::CISO:
  38. return "CISO";
  39. case BlobType::WBFS:
  40. return "WBFS";
  41. case BlobType::TGC:
  42. return "TGC";
  43. case BlobType::WIA:
  44. return "WIA";
  45. case BlobType::RVZ:
  46. return "RVZ";
  47. case BlobType::MOD_DESCRIPTOR:
  48. return translate_str("Mod");
  49. case BlobType::NFS:
  50. return "NFS";
  51. case BlobType::SPLIT_PLAIN:
  52. return translate_str("Multi-part ISO");
  53. default:
  54. return "";
  55. }
  56. }
  57. void SectorReader::SetSectorSize(int blocksize)
  58. {
  59. m_block_size = std::max(blocksize, 0);
  60. for (auto& cache_entry : m_cache)
  61. {
  62. cache_entry.Reset();
  63. cache_entry.data.resize(m_chunk_blocks * m_block_size);
  64. }
  65. }
  66. void SectorReader::SetChunkSize(int block_cnt)
  67. {
  68. m_chunk_blocks = std::max(block_cnt, 1);
  69. // Clear cache and resize the data arrays
  70. SetSectorSize(m_block_size);
  71. }
  72. SectorReader::~SectorReader() = default;
  73. const SectorReader::Cache* SectorReader::FindCacheLine(u64 block_num)
  74. {
  75. auto itr =
  76. std::ranges::find_if(m_cache, [&](const Cache& entry) { return entry.Contains(block_num); });
  77. if (itr == m_cache.end())
  78. return nullptr;
  79. itr->MarkUsed();
  80. return &*itr;
  81. }
  82. SectorReader::Cache* SectorReader::GetEmptyCacheLine()
  83. {
  84. Cache* oldest = &m_cache[0];
  85. // Find the Least Recently Used cache line to replace.
  86. std::for_each(m_cache.begin() + 1, m_cache.end(), [&](Cache& line) {
  87. if (line.IsLessRecentlyUsedThan(*oldest))
  88. {
  89. oldest->ShiftLRU();
  90. oldest = &line;
  91. return;
  92. }
  93. line.ShiftLRU();
  94. });
  95. oldest->Reset();
  96. return oldest;
  97. }
  98. const SectorReader::Cache* SectorReader::GetCacheLine(u64 block_num)
  99. {
  100. if (auto entry = FindCacheLine(block_num))
  101. return entry;
  102. // Cache miss. Fault in the missing entry.
  103. Cache* cache = GetEmptyCacheLine();
  104. // We only read aligned chunks, this avoids duplicate overlapping entries.
  105. u64 chunk_idx = block_num / m_chunk_blocks;
  106. u32 blocks_read = ReadChunk(cache->data.data(), chunk_idx);
  107. if (!blocks_read)
  108. return nullptr;
  109. cache->Fill(chunk_idx * m_chunk_blocks, blocks_read);
  110. // Secondary check for out-of-bounds read.
  111. // If we got less than m_chunk_blocks, we may still have missed.
  112. // We do this after the cache fill since the cache line itself is
  113. // fine, the problem is being asked to read past the end of the disk.
  114. return cache->Contains(block_num) ? cache : nullptr;
  115. }
  116. bool SectorReader::Read(u64 offset, u64 size, u8* out_ptr)
  117. {
  118. if (offset + size > GetDataSize())
  119. return false;
  120. u64 remain = size;
  121. u64 block = 0;
  122. u32 position_in_block = static_cast<u32>(offset % m_block_size);
  123. while (remain > 0)
  124. {
  125. block = offset / m_block_size;
  126. const Cache* cache = GetCacheLine(block);
  127. if (!cache)
  128. return false;
  129. // Cache entries are aligned chunks, we may not want to read from the start
  130. u32 read_offset = static_cast<u32>(block - cache->block_idx) * m_block_size + position_in_block;
  131. u32 can_read = m_block_size * cache->num_blocks - read_offset;
  132. u32 was_read = static_cast<u32>(std::min<u64>(can_read, remain));
  133. std::copy_n(cache->data.begin() + read_offset, was_read, out_ptr);
  134. offset += was_read;
  135. out_ptr += was_read;
  136. remain -= was_read;
  137. position_in_block = 0;
  138. }
  139. return true;
  140. }
  141. // Crap default implementation if not overridden.
  142. bool SectorReader::ReadMultipleAlignedBlocks(u64 block_num, u64 cnt_blocks, u8* out_ptr)
  143. {
  144. for (u64 i = 0; i < cnt_blocks; ++i)
  145. {
  146. if (!GetBlock(block_num + i, out_ptr))
  147. return false;
  148. out_ptr += m_block_size;
  149. }
  150. return true;
  151. }
  152. u32 SectorReader::ReadChunk(u8* buffer, u64 chunk_num)
  153. {
  154. u64 block_num = chunk_num * m_chunk_blocks;
  155. u32 cnt_blocks = m_chunk_blocks;
  156. // If we are reading the end of a disk, there may not be enough blocks to
  157. // read a whole chunk. We need to clamp down in that case.
  158. u64 end_block = (GetDataSize() + m_block_size - 1) / m_block_size;
  159. if (end_block)
  160. cnt_blocks = static_cast<u32>(std::min<u64>(m_chunk_blocks, end_block - block_num));
  161. if (ReadMultipleAlignedBlocks(block_num, cnt_blocks, buffer))
  162. {
  163. if (cnt_blocks < m_chunk_blocks)
  164. {
  165. std::fill(buffer + cnt_blocks * m_block_size, buffer + m_chunk_blocks * m_block_size, 0u);
  166. }
  167. return cnt_blocks;
  168. }
  169. // end_block may be zero on real disks if we fail to get the media size.
  170. // We have to fallback to probing the disk instead.
  171. if (!end_block)
  172. {
  173. for (u32 i = 0; i < cnt_blocks; ++i)
  174. {
  175. if (!GetBlock(block_num + i, buffer))
  176. {
  177. std::fill_n(buffer, (cnt_blocks - i) * m_block_size, 0u);
  178. return i;
  179. }
  180. buffer += m_block_size;
  181. }
  182. return cnt_blocks;
  183. }
  184. return 0;
  185. }
  186. std::unique_ptr<BlobReader> CreateBlobReader(const std::string& filename)
  187. {
  188. File::IOFile file(filename, "rb");
  189. u32 magic;
  190. if (!file.ReadArray(&magic, 1))
  191. return nullptr;
  192. // Conveniently, every supported file format (except for plain disc images and
  193. // extracted discs) starts with a 4-byte magic number that identifies the format,
  194. // so we just need a simple switch statement to create the right blob type. If the
  195. // magic number doesn't match any known magic number and the directory structure
  196. // doesn't match the directory blob format, we assume it's a plain disc image. If
  197. // that assumption is wrong, the volume code that runs later will notice the error
  198. // because the blob won't provide the right data when reading the GC/Wii disc header.
  199. switch (magic)
  200. {
  201. case CISO_MAGIC:
  202. return CISOFileReader::Create(std::move(file));
  203. case GCZ_MAGIC:
  204. return CompressedBlobReader::Create(std::move(file), filename);
  205. case TGC_MAGIC:
  206. return TGCFileReader::Create(std::move(file));
  207. case WBFS_MAGIC:
  208. return WbfsFileReader::Create(std::move(file), filename);
  209. case WIA_MAGIC:
  210. return WIAFileReader::Create(std::move(file), filename);
  211. case RVZ_MAGIC:
  212. return RVZFileReader::Create(std::move(file), filename);
  213. case NFS_MAGIC:
  214. return NFSFileReader::Create(std::move(file), filename);
  215. default:
  216. if (auto directory_blob = DirectoryBlobReader::Create(filename))
  217. return std::move(directory_blob);
  218. if (auto split_blob = SplitPlainFileReader::Create(filename))
  219. return std::move(split_blob);
  220. return PlainFileReader::Create(std::move(file));
  221. }
  222. }
  223. } // namespace DiscIO