Debian Codegraph @

Source/Core/DiscIO/Blob.cpp

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