Debian Codegraph @

Source/Core/DiscIO/VolumeVerifier.h

raw symbols SCIP
1 // Copyright 2019 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <future>
7 #include <map>
8 #include <memory>
9 #include <optional>
10 #include <string>
11 #include <vector>
12
13 #include <mbedtls/md5.h>
14
15 #include "Common/CommonTypes.h"
16 #include "Common/Crypto/SHA1.h"
17 #include "Core/IOS/ES/Formats.h"
18 #include "DiscIO/DiscScrubber.h"
19 #include "DiscIO/Volume.h"
20
21 // To be used as follows:
22 //
23 // VolumeVerifier verifier(volume, redump_verification, hashes_to_calculate);
24 // verifier.Start();
25 // while (verifier.GetBytesProcessed() != verifier.GetTotalBytes())
26 // verifier.Process();
27 // verifier.Finish();
28 // auto result = verifier.GetResult();
29 //
30 // Start, Process and Finish may take some time to run.
31 //
32 // GetResult() can be called before the processing is finished, but the result will be incomplete.
33
34 namespace DiscIO
35 {
36 template <typename T>
37 struct Hashes
38 {
39 T crc32;
40 T md5;
41 T sha1;
42 };
43
44 class RedumpVerifier final
45 {
46 public:
47 enum class Status
48 {
49 Unknown,
50 GoodDump,
51 BadDump,
52 Error,
53 };
54
55 struct Result
56 {
57 Status status = Status::Unknown;
58 std::string message;
59 };
60
61 void Start(const Volume& volume);
62 Result Finish(const Hashes<std::vector<u8>>& hashes);
63
64 private:
65 enum class DownloadStatus
66 {
67 NotAttempted,
68 Success,
69 Fail,
70 FailButOldCacheAvailable,
71 };
72
73 struct DownloadState
74 {
75 std::mutex mutex;
76 DownloadStatus status = DownloadStatus::NotAttempted;
77 };
78
79 struct PotentialMatch
80 {
81 u64 size = 0;
82 Hashes<std::vector<u8>> hashes;
83 };
84
85 static DownloadStatus DownloadDatfile(const std::string& system, DownloadStatus old_status);
86 static std::vector<u8> ReadDatfile(const std::string& system);
87 std::vector<PotentialMatch> ScanDatfile(std::span<const u8> data, const std::string& system);
88
89 std::string m_game_id;
90 u16 m_revision = 0;
91 u8 m_disc_number = 0;
92 u64 m_size = 0;
93
94 std::future<std::vector<PotentialMatch>> m_future;
95 Result m_result;
96
97 static DownloadState m_gc_download_state;
98 static DownloadState m_wii_download_state;
99 };
100
101 class VolumeVerifier final
102 {
103 public:
104 enum class Severity
105 {
106 None, // Only used internally
107 Low,
108 Medium,
109 High,
110 };
111
112 struct Problem
113 {
114 Severity severity;
115 std::string text;
116 };
117
118 struct Result
119 {
120 Hashes<std::vector<u8>> hashes;
121 std::string summary_text;
122 std::vector<Problem> problems;
123 RedumpVerifier::Result redump;
124 };
125
126 VolumeVerifier(const Volume& volume, bool redump_verification, Hashes<bool> hashes_to_calculate);
127 ~VolumeVerifier();
128
129 static Hashes<bool> GetDefaultHashesToCalculate();
130 void Start();
131 void Process();
132 u64 GetBytesProcessed() const;
133 u64 GetTotalBytes() const;
134 void Finish();
135 const Result& GetResult() const;
136
137 private:
138 struct GroupToVerify
139 {
140 Partition partition;
141 u64 offset;
142 size_t block_index_start;
143 size_t block_index_end;
144 };
145
146 std::vector<Partition> CheckPartitions();
147 bool CheckPartition(const Partition& partition); // Returns false if partition should be ignored
148 std::string GetPartitionName(std::optional<u32> type) const;
149 bool IsDebugSigned() const;
150 bool ShouldHaveChannelPartition() const;
151 bool ShouldHaveInstallPartition() const;
152 bool ShouldHaveMasterpiecePartitions() const;
153 bool ShouldBeDualLayer() const;
154 void CheckVolumeSize();
155 void CheckMisc();
156 void CheckSuperPaperMario();
157 void SetUpHashing();
158 void WaitForAsyncOperations() const;
159 bool ReadChunkAndWaitForAsyncOperations(u64 bytes_to_read);
160
161 void AddProblem(Severity severity, std::string text);
162
163 const Volume& m_volume;
164 Result m_result;
165 bool m_is_tgc = false;
166 bool m_is_datel = false;
167 bool m_is_triforce = false;
168 bool m_is_not_retail = false;
169
170 bool m_redump_verification;
171 RedumpVerifier m_redump_verifier;
172
173 bool m_read_errors_occurred = false;
174
175 Hashes<bool> m_hashes_to_calculate{};
176 bool m_calculating_any_hash = false;
177 u32 m_crc32_context = 0;
178 mbedtls_md5_context m_md5_context{};
179 std::unique_ptr<Common::SHA1::Context> m_sha1_context;
180
181 u64 m_excess_bytes = 0;
182 std::vector<u8> m_data;
183 std::future<void> m_crc32_future;
184 std::future<void> m_md5_future;
185 std::future<void> m_sha1_future;
186 std::future<void> m_content_future;
187 std::future<void> m_group_future;
188
189 DiscScrubber m_scrubber;
190 IOS::ES::TicketReader m_ticket;
191 std::vector<u64> m_content_offsets;
192 u16 m_content_index = 0;
193 std::vector<GroupToVerify> m_groups;
194 size_t m_group_index = 0; // Index in m_groups, not index in a specific partition
195 std::map<Partition, size_t> m_block_errors;
196 std::map<Partition, size_t> m_unused_block_errors;
197
198 u64 m_biggest_referenced_offset = 0;
199 u64 m_biggest_verified_offset = 0;
200
201 bool m_started = false;
202 bool m_done = false;
203 u64 m_progress = 0;
204 u64 m_max_progress = 0;
205 DataSizeType m_data_size_type;
206 };
207
208 } // namespace DiscIO