Debian Codegraph @

Source/Core/DolphinTool/VerifyCommand.cpp

raw symbols SCIP
1 // Copyright 2021 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "DolphinTool/VerifyCommand.h"
5
6 #include <cstdlib>
7 #include <string>
8 #include <vector>
9
10 #include <OptionParser.h>
11 #include <fmt/ostream.h>
12
13 #include "Core/AchievementManager.h"
14 #include "DiscIO/Volume.h"
15 #include "DiscIO/VolumeVerifier.h"
16 #include "UICommon/UICommon.h"
17
18 namespace DolphinTool
19 {
20 static std::string HashToHexString(const std::vector<u8>& hash)
21 {
22 std::stringstream ss;
23 ss << std::hex;
24 for (int i = 0; i < static_cast<int>(hash.size()); ++i)
25 {
26 ss << std::setw(2) << std::setfill('0') << static_cast<int>(hash[i]);
27 }
28 return ss.str();
29 }
30
31 static void PrintFullReport(const DiscIO::VolumeVerifier::Result& result)
32 {
33 if (!result.hashes.crc32.empty())
34 fmt::print(std::cout, "CRC32: {}\n", HashToHexString(result.hashes.crc32));
35 else
36 fmt::print(std::cout, "CRC32 not computed\n");
37
38 if (!result.hashes.md5.empty())
39 fmt::print(std::cout, "MD5: {}\n", HashToHexString(result.hashes.md5));
40 else
41 fmt::print(std::cout, "MD5 not computed\n");
42
43 if (!result.hashes.sha1.empty())
44 fmt::print(std::cout, "SHA1: {}\n", HashToHexString(result.hashes.sha1));
45 else
46 fmt::print(std::cout, "SHA1 not computed\n");
47
48 fmt::print(std::cout, "Problems Found: {}\n", result.problems.empty() ? "No" : "Yes");
49
50 for (const auto& problem : result.problems)
51 {
52 fmt::print(std::cout, "\nSeverity: ");
53 switch (problem.severity)
54 {
55 case DiscIO::VolumeVerifier::Severity::Low:
56 fmt::print(std::cout, "Low");
57 break;
58 case DiscIO::VolumeVerifier::Severity::Medium:
59 fmt::print(std::cout, "Medium");
60 break;
61 case DiscIO::VolumeVerifier::Severity::High:
62 fmt::print(std::cout, "High");
63 break;
64 case DiscIO::VolumeVerifier::Severity::None:
65 fmt::print(std::cout, "None");
66 break;
67 default:
68 ASSERT(false);
69 break;
70 }
71 fmt::print(std::cout, "\nSummary: {}\n\n", problem.text);
72 }
73 }
74
75 int VerifyCommand(const std::vector<std::string>& args)
76 {
77 optparse::OptionParser parser;
78
79 parser.usage("usage: verify [options]...");
80
81 parser.add_option("-u", "--user")
82 .type("string")
83 .action("store")
84 .help("User folder path, required for temporary processing files. "
85 "Will be automatically created if this option is not set.")
86 .set_default("");
87
88 parser.add_option("-i", "--input")
89 .type("string")
90 .action("store")
91 .help("Path to input file.")
92 .metavar("FILE");
93
94 parser.add_option("-a", "--algorithm")
95 .type("string")
96 .action("store")
97 .help("Optional. Compute and print the digest using the selected algorithm, then exit. "
98 "[%choices]")
99 .choices({"crc32", "md5", "sha1", "rchash"});
100
101 const optparse::Values& options = parser.parse_args(args);
102
103 // Initialize the dolphin user directory, required for temporary processing files
104 // If this is not set, destructive file operations could occur due to path confusion
105 UICommon::SetUserDirectory(options["user"]);
106 UICommon::Init();
107
108 // Validate options
109 if (!options.is_set("input"))
110 {
111 fmt::print(std::cerr, "Error: No input set\n");
112 return EXIT_FAILURE;
113 }
114 const std::string& input_file_path = options["input"];
115
116 bool rc_hash_calculate = false;
117 std::string rc_hash_result = "0";
118
119 DiscIO::Hashes<bool> hashes_to_calculate{};
120 const bool algorithm_is_set = options.is_set("algorithm");
121 if (!algorithm_is_set)
122 {
123 hashes_to_calculate = DiscIO::VolumeVerifier::GetDefaultHashesToCalculate();
124 }
125 else
126 {
127 const std::string& algorithm = options["algorithm"];
128 if (algorithm == "crc32")
129 hashes_to_calculate.crc32 = true;
130 else if (algorithm == "md5")
131 hashes_to_calculate.md5 = true;
132 else if (algorithm == "sha1")
133 hashes_to_calculate.sha1 = true;
134 #ifdef USE_RETRO_ACHIEVEMENTS
135 else if (algorithm == "rchash")
136 rc_hash_calculate = true;
137 #endif
138 }
139
140 if (!hashes_to_calculate.crc32 && !hashes_to_calculate.md5 && !hashes_to_calculate.sha1 &&
141 !rc_hash_calculate)
142 {
143 // optparse should protect from this
144 fmt::print(std::cerr, "Error: No algorithms selected for the operation\n");
145 return EXIT_FAILURE;
146 }
147
148 // Open the volume
149 const std::unique_ptr<DiscIO::Volume> volume = DiscIO::CreateVolume(input_file_path);
150 if (!volume)
151 {
152 fmt::print(std::cerr, "Error: Unable to open input file\n");
153 return EXIT_FAILURE;
154 }
155
156 // Verify the volume
157 DiscIO::VolumeVerifier verifier(*volume, false, hashes_to_calculate);
158 verifier.Start();
159 while (verifier.GetBytesProcessed() != verifier.GetTotalBytes())
160 {
161 verifier.Process();
162 }
163 verifier.Finish();
164 const DiscIO::VolumeVerifier::Result& result = verifier.GetResult();
165
166 #ifdef USE_RETRO_ACHIEVEMENTS
167 // Calculate rcheevos hash
168 if (rc_hash_calculate)
169 {
170 rc_hash_result = AchievementManager::CalculateHash(input_file_path);
171 }
172 #endif
173
174 // Print the report
175 if (!algorithm_is_set)
176 {
177 PrintFullReport(result);
178 }
179 else
180 {
181 if (hashes_to_calculate.crc32 && !result.hashes.crc32.empty())
182 fmt::print(std::cout, "{}\n", HashToHexString(result.hashes.crc32));
183 else if (hashes_to_calculate.md5 && !result.hashes.md5.empty())
184 fmt::print(std::cout, "{}\n", HashToHexString(result.hashes.md5));
185 else if (hashes_to_calculate.sha1 && !result.hashes.sha1.empty())
186 fmt::print(std::cout, "{}\n", HashToHexString(result.hashes.sha1));
187 else if (rc_hash_calculate)
188 fmt::print(std::cout, "{}\n", rc_hash_result);
189 else
190 {
191 fmt::print(std::cerr, "Error: No hash computed\n");
192 return EXIT_FAILURE;
193 }
194 }
195
196 return EXIT_SUCCESS;
197 }
198 } // namespace DolphinTool