Blob.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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()
  73. {
  74. }
  75. const SectorReader::Cache* SectorReader::FindCacheLine(u64 block_num)
  76. {
  77. auto itr =
  78. std::ranges::find_if(m_cache, [&](const Cache& entry) { return entry.Contains(block_num); });
  79. if (itr == m_cache.end())
  80. return nullptr;
  81. itr->MarkUsed();
  82. return &*itr;
  83. }
  84. SectorReader::Cache* SectorReader::GetEmptyCacheLine()
  85. {
  86. Cache* oldest = &m_cache[0];
  87. // Find the Least Recently Used cache line to replace.
  88. std::for_each(m_cache.begin() + 1, m_cache.end(), [&](Cache& line) {
  89. if (line.IsLessRecentlyUsedThan(*oldest))
  90. {
  91. oldest->ShiftLRU();
  92. oldest = &line;
  93. return;
  94. }
  95. line.ShiftLRU();
  96. });
  97. oldest->Reset();
  98. return oldest;
  99. }
  100. const SectorReader::Cache* SectorReader::GetCacheLine(u64 block_num)
  101. {
  102. if (auto entry = FindCacheLine(block_num))
  103. return entry;
  104. // Cache miss. Fault in the missing entry.
  105. Cache* cache = GetEmptyCacheLine();
  106. // We only read aligned chunks, this avoids duplicate overlapping entries.
  107. u64 chunk_idx = block_num / m_chunk_blocks;
  108. u32 blocks_read = ReadChunk(cache->data.data(), chunk_idx);
  109. if (!blocks_read)
  110. return nullptr;
  111. cache->Fill(chunk_idx * m_chunk_blocks, blocks_read);
  112. // Secondary check for out-of-bounds read.
  113. // If we got less than m_chunk_blocks, we may still have missed.
  114. // We do this after the cache fill since the cache line itself is
  115. // fine, the problem is being asked to read past the end of the disk.
  116. return cache->Contains(block_num) ? cache : nullptr;
  117. }
  118. bool SectorReader::Read(u64 offset, u64 size, u8* out_ptr)
  119. {
  120. if (offset + size > GetDataSize())
  121. return false;
  122. u64 remain = size;
  123. u64 block = 0;
  124. u32 position_in_block = static_cast<u32>(offset % m_block_size);
  125. while (remain > 0)
  126. {
  127. block = offset / m_block_size;
  128. const Cache* cache = GetCacheLine(block);
  129. if (!cache)
  130. return false;
  131. // Cache entries are aligned chunks, we may not want to read from the start
  132. u32 read_offset = static_cast<u32>(block - cache->block_idx) * m_block_size + position_in_block;
  133. u32 can_read = m_block_size * cache->num_blocks - read_offset;
  134. u32 was_read = static_cast<u32>(std::min<u64>(can_read, remain));
  135. std::copy_n(cache->data.begin() + read_offset, was_read, out_ptr);
  136. offset += was_read;
  137. out_ptr += was_read;
  138. remain -= was_read;
  139. position_in_block = 0;
  140. }
  141. return true;
  142. }
  143. // Crap default implementation if not overridden.
  144. bool SectorReader::ReadMultipleAlignedBlocks(u64 block_num, u64 cnt_blocks, u8* out_ptr)
  145. {
  146. for (u64 i = 0; i < cnt_blocks; ++i)
  147. {
  148. if (!GetBlock(block_num + i, out_ptr))
  149. return false;
  150. out_ptr += m_block_size;
  151. }
  152. return true;
  153. }
  154. u32 SectorReader::ReadChunk(u8* buffer, u64 chunk_num)
  155. {
  156. u64 block_num = chunk_num * m_chunk_blocks;
  157. u32 cnt_blocks = m_chunk_blocks;
  158. // If we are reading the end of a disk, there may not be enough blocks to
  159. // read a whole chunk. We need to clamp down in that case.
  160. u64 end_block = (GetDataSize() + m_block_size - 1) / m_block_size;
  161. if (end_block)
  162. cnt_blocks = static_cast<u32>(std::min<u64>(m_chunk_blocks, end_block - block_num));
  163. if (ReadMultipleAlignedBlocks(block_num, cnt_blocks, buffer))
  164. {
  165. if (cnt_blocks < m_chunk_blocks)
  166. {
  167. std::fill(buffer + cnt_blocks * m_block_size, buffer + m_chunk_blocks * m_block_size, 0u);
  168. }
  169. return cnt_blocks;
  170. }
  171. // end_block may be zero on real disks if we fail to get the media size.
  172. // We have to fallback to probing the disk instead.
  173. if (!end_block)
  174. {
  175. for (u32 i = 0; i < cnt_blocks; ++i)
  176. {
  177. if (!GetBlock(block_num + i, buffer))
  178. {
  179. std::fill_n(buffer, (cnt_blocks - i) * m_block_size, 0u);
  180. return i;
  181. }
  182. buffer += m_block_size;
  183. }
  184. return cnt_blocks;
  185. }
  186. return 0;
  187. }
  188. std::unique_ptr<BlobReader> CreateBlobReader(const std::string& filename)
  189. {
  190. File::IOFile file(filename, "rb");
  191. u32 magic;
  192. if (!file.ReadArray(&magic, 1))
  193. return nullptr;
  194. // Conveniently, every supported file format (except for plain disc images and
  195. // extracted discs) starts with a 4-byte magic number that identifies the format,
  196. // so we just need a simple switch statement to create the right blob type. If the
  197. // magic number doesn't match any known magic number and the directory structure
  198. // doesn't match the directory blob format, we assume it's a plain disc image. If
  199. // that assumption is wrong, the volume code that runs later will notice the error
  200. // because the blob won't provide the right data when reading the GC/Wii disc header.
  201. switch (magic)
  202. {
  203. case CISO_MAGIC:
  204. return CISOFileReader::Create(std::move(file));
  205. case GCZ_MAGIC:
  206. return CompressedBlobReader::Create(std::move(file), filename);
  207. case TGC_MAGIC:
  208. return TGCFileReader::Create(std::move(file));
  209. case WBFS_MAGIC:
  210. return WbfsFileReader::Create(std::move(file), filename);
  211. case WIA_MAGIC:
  212. return WIAFileReader::Create(std::move(file), filename);
  213. case RVZ_MAGIC:
  214. return RVZFileReader::Create(std::move(file), filename);
  215. case NFS_MAGIC:
  216. return NFSFileReader::Create(std::move(file), filename);
  217. default:
  218. if (auto directory_blob = DirectoryBlobReader::Create(filename))
  219. return std::move(directory_blob);
  220. if (auto split_blob = SplitPlainFileReader::Create(filename))
  221. return std::move(split_blob);
  222. return PlainFileReader::Create(std::move(file));
  223. }
  224. }
  225. } // namespace DiscIO