Debian Codegraph @

Source/Core/DiscIO/Blob.h

raw symbols SCIP
1 // Copyright 2008 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 // BLOB
7
8 // Blobs in Dolphin are read only Binary Large OBjects. For example, a typical DVD image.
9 // Often, you may want to store these things in a highly compressed format, but still
10 // allow random access. Or you may store them on an odd device, like raw on a DVD.
11
12 // Always read your BLOBs using an interface returned by CreateBlobReader(). It will
13 // detect whether the file is a compressed blob, or just a big hunk of data, or a drive, and
14 // automatically do the right thing.
15
16 #include <array>
17 #include <functional>
18 #include <memory>
19 #include <optional>
20 #include <string>
21 #include <vector>
22
23 #include "Common/CommonTypes.h"
24 #include "Common/Swap.h"
25
26 namespace DiscIO
27 {
28 enum class WIARVZCompressionType : u32;
29
30 // Increment CACHE_REVISION (GameFileCache.cpp) if the enum below is modified
31 enum class BlobType
32 {
33 PLAIN,
34 DRIVE,
35 DIRECTORY,
36 GCZ,
37 CISO,
38 WBFS,
39 TGC,
40 WIA,
41 RVZ,
42 MOD_DESCRIPTOR,
43 NFS,
44 SPLIT_PLAIN,
45 };
46
47 // If you convert an ISO file to another format and then call GetDataSize on it, what is the result?
48 enum class DataSizeType
49 {
50 // The result is the same as for the ISO.
51 Accurate,
52 // The result is not larger than for the ISO. (It's usually a little smaller than for the ISO.)
53 // Reads to offsets that are larger than the result will return some kind of "blank" data.
54 LowerBound,
55 // The result is not smaller than for the ISO. (It's usually much larger than for the ISO.)
56 UpperBound,
57 };
58
59 std::string GetName(BlobType blob_type, bool translate);
60
61 class BlobReader
62 {
63 public:
64 virtual ~BlobReader() = default;
65
66 virtual BlobType GetBlobType() const = 0;
67 virtual std::unique_ptr<BlobReader> CopyReader() const = 0;
68
69 virtual u64 GetRawSize() const = 0;
70 virtual u64 GetDataSize() const = 0;
71 virtual DataSizeType GetDataSizeType() const = 0;
72
73 // Returns 0 if the format does not use blocks
74 virtual u64 GetBlockSize() const = 0;
75 virtual bool HasFastRandomAccessInBlock() const = 0;
76 virtual std::string GetCompressionMethod() const = 0;
77 virtual std::optional<int> GetCompressionLevel() const = 0;
78
79 // NOT thread-safe - can't call this from multiple threads.
80 virtual bool Read(u64 offset, u64 size, u8* out_ptr) = 0;
81 template <typename T>
82 std::optional<T> ReadSwapped(u64 offset)
83 {
84 T temp;
85 if (!Read(offset, sizeof(T), reinterpret_cast<u8*>(&temp)))
86 return std::nullopt;
87 return Common::FromBigEndian(temp);
88 }
89
90 virtual bool SupportsReadWiiDecrypted(u64 offset, u64 size, u64 partition_data_offset) const
91 {
92 return false;
93 }
94
95 virtual bool ReadWiiDecrypted(u64 offset, u64 size, u8* out_ptr, u64 partition_data_offset)
96 {
97 return false;
98 }
99
100 // Returns true only for CachedBlobReader.
101 virtual bool IsCached() const { return false; }
102
103 protected:
104 BlobReader() {}
105 };
106
107 // Provides caching and byte-operation-to-block-operations facilities.
108 // Used for compressed blob and direct drive reading.
109 // NOTE: GetDataSize() is expected to be evenly divisible by the sector size.
110 class SectorReader : public BlobReader
111 {
112 public:
113 ~SectorReader() override = 0;
114
115 bool Read(u64 offset, u64 size, u8* out_ptr) override;
116
117 protected:
118 void SetSectorSize(int blocksize);
119 int GetSectorSize() const { return m_block_size; }
120 // Set the chunk size -> the number of blocks to read at a time.
121 // Default value is 1 but that is too low for physical devices
122 // like CDROMs. Setting this to a higher value helps reduce seeking
123 // and IO overhead by batching reads. Do not set it too high either
124 // as large reads are slow and will take too long to resolve.
125 void SetChunkSize(int blocks);
126 int GetChunkSize() const { return m_chunk_blocks; }
127 // Read a single block/sector.
128 virtual bool GetBlock(u64 block_num, u8* out) = 0;
129
130 // Read multiple contiguous blocks.
131 // Default implementation just calls GetBlock in a loop, it should be
132 // overridden in derived classes where possible.
133 virtual bool ReadMultipleAlignedBlocks(u64 block_num, u64 num_blocks, u8* out_ptr);
134
135 private:
136 struct Cache
137 {
138 std::vector<u8> data;
139 u64 block_idx = 0;
140 u32 num_blocks = 0;
141
142 // [Pseudo-] Least Recently Used Shift Register
143 // When an empty cache line is needed, the line with the lowest value
144 // is taken and reset; the LRU register is then shifted down 1 place
145 // on all lines (low bit discarded). When a line is used, the high bit
146 // is set marking it as most recently used.
147 u32 lru_sreg = 0;
148
149 void Reset()
150 {
151 block_idx = 0;
152 num_blocks = 0;
153 lru_sreg = 0;
154 }
155 void Fill(u64 block, u32 count)
156 {
157 block_idx = block;
158 num_blocks = count;
159 // NOTE: Setting only the high bit means the newest line will
160 // be selected for eviction if every line in the cache was
161 // touched. This gives MRU behavior which is probably
162 // desirable in that case.
163 MarkUsed();
164 }
165 bool Contains(u64 block) const { return block >= block_idx && block - block_idx < num_blocks; }
166 void MarkUsed() { lru_sreg |= 0x80000000; }
167 void ShiftLRU() { lru_sreg >>= 1; }
168 bool IsLessRecentlyUsedThan(const Cache& other) const { return lru_sreg < other.lru_sreg; }
169 };
170
171 // Gets the cache line that contains the given block, or nullptr.
172 // NOTE: The cache record only lasts until it expires (next GetEmptyCacheLine)
173 const Cache* FindCacheLine(u64 block_num);
174
175 // Finds the least recently used cache line, resets and returns it.
176 Cache* GetEmptyCacheLine();
177
178 // Combines FindCacheLine with GetEmptyCacheLine and ReadChunk.
179 // Always returns a valid cache line (loading the data if needed).
180 // May return nullptr only if the cache missed and the read failed.
181 const Cache* GetCacheLine(u64 block_num);
182
183 // Read all bytes from a chunk of blocks into a buffer.
184 // Returns the number of blocks read (may be less than m_chunk_blocks
185 // if chunk_num is the last chunk on the disk and the disk size is not
186 // evenly divisible into chunks). Returns zero if it fails.
187 u32 ReadChunk(u8* buffer, u64 chunk_num);
188
189 static constexpr int CACHE_LINES = 32;
190 u32 m_block_size = 0; // Bytes in a sector/block
191 u32 m_chunk_blocks = 1; // Number of sectors/blocks in a chunk
192 std::array<Cache, CACHE_LINES> m_cache;
193 };
194
195 // Factory function - examines the path to choose the right type of BlobReader, and returns one.
196 std::unique_ptr<BlobReader> CreateBlobReader(const std::string& filename);
197
198 using CompressCB = std::function<bool(const std::string& text, float percent)>;
199
200 bool ConvertToGCZ(BlobReader* infile, const std::string& infile_path,
201 const std::string& outfile_path, u32 sub_type, int sector_size,
202 const CompressCB& callback);
203 bool ConvertToPlain(BlobReader* infile, const std::string& infile_path,
204 const std::string& outfile_path, const CompressCB& callback);
205 bool ConvertToWIAOrRVZ(BlobReader* infile, const std::string& infile_path,
206 const std::string& outfile_path, bool rvz,
207 WIARVZCompressionType compression_type, int compression_level,
208 int chunk_size, const CompressCB& callback);
209
210 } // namespace DiscIO