summaryrefslogtreecommitdiff
path: root/Source/Core/DolphinTool/VerifyCommand.cpp
blob: fcaa31b8196f40b9ae7c68d52aa7a0e002f77f8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2021 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "DolphinTool/VerifyCommand.h"

#include <cstdlib>
#include <string>
#include <vector>

#include <OptionParser.h>
#include <fmt/format.h>
#include <fmt/ostream.h>

#include "Common/StringUtil.h"
#include "Core/AchievementManager.h"
#include "DiscIO/Volume.h"
#include "DiscIO/VolumeVerifier.h"
#include "UICommon/UICommon.h"

namespace DolphinTool
{
static std::string HashToHexString(const std::vector<u8>& hash)
{
  std::stringstream ss;
  ss << std::hex;
  for (int i = 0; i < static_cast<int>(hash.size()); ++i)
  {
    ss << std::setw(2) << std::setfill('0') << static_cast<int>(hash[i]);
  }
  return ss.str();
}

static void PrintFullReport(const DiscIO::VolumeVerifier::Result& result)
{
  if (!result.hashes.crc32.empty())
    fmt::print(std::cout, "CRC32: {}\n", HashToHexString(result.hashes.crc32));
  else
    fmt::print(std::cout, "CRC32 not computed\n");

  if (!result.hashes.md5.empty())
    fmt::print(std::cout, "MD5: {}\n", HashToHexString(result.hashes.md5));
  else
    fmt::print(std::cout, "MD5 not computed\n");

  if (!result.hashes.sha1.empty())
    fmt::print(std::cout, "SHA1: {}\n", HashToHexString(result.hashes.sha1));
  else
    fmt::print(std::cout, "SHA1 not computed\n");

  fmt::print(std::cout, "Problems Found: {}\n", result.problems.empty() ? "No" : "Yes");

  for (const auto& problem : result.problems)
  {
    fmt::print(std::cout, "\nSeverity: ");
    switch (problem.severity)
    {
    case DiscIO::VolumeVerifier::Severity::Low:
      fmt::print(std::cout, "Low");
      break;
    case DiscIO::VolumeVerifier::Severity::Medium:
      fmt::print(std::cout, "Medium");
      break;
    case DiscIO::VolumeVerifier::Severity::High:
      fmt::print(std::cout, "High");
      break;
    case DiscIO::VolumeVerifier::Severity::None:
      fmt::print(std::cout, "None");
      break;
    default:
      ASSERT(false);
      break;
    }
    fmt::print(std::cout, "\nSummary: {}\n\n", problem.text);
  }
}

int VerifyCommand(const std::vector<std::string>& args)
{
  optparse::OptionParser parser;

  parser.usage("usage: verify [options]...");

  parser.add_option("-u", "--user")
      .type("string")
      .action("store")
      .help("User folder path, required for temporary processing files. "
            "Will be automatically created if this option is not set.")
      .set_default("");

  parser.add_option("-i", "--input")
      .type("string")
      .action("store")
      .help("Path to input file.")
      .metavar("FILE");

  parser.add_option("-a", "--algorithm")
      .type("string")
      .action("store")
      .help("Optional. Compute and print the digest using the selected algorithm, then exit. "
            "[%choices]")
      .choices({"crc32", "md5", "sha1", "rchash"});

  const optparse::Values& options = parser.parse_args(args);

  // Initialize the dolphin user directory, required for temporary processing files
  // If this is not set, destructive file operations could occur due to path confusion
  UICommon::SetUserDirectory(options["user"]);
  UICommon::Init();

  // Validate options
  if (!options.is_set("input"))
  {
    fmt::print(std::cerr, "Error: No input set\n");
    return EXIT_FAILURE;
  }
  const std::string& input_file_path = options["input"];

  bool rc_hash_calculate = false;
  std::string rc_hash_result = "0";

  DiscIO::Hashes<bool> hashes_to_calculate{};
  const bool algorithm_is_set = options.is_set("algorithm");
  if (!algorithm_is_set)
  {
    hashes_to_calculate = DiscIO::VolumeVerifier::GetDefaultHashesToCalculate();
  }
  else
  {
    const std::string& algorithm = options["algorithm"];
    if (algorithm == "crc32")
      hashes_to_calculate.crc32 = true;
    else if (algorithm == "md5")
      hashes_to_calculate.md5 = true;
    else if (algorithm == "sha1")
      hashes_to_calculate.sha1 = true;
#ifdef USE_RETRO_ACHIEVEMENTS
    else if (algorithm == "rchash")
      rc_hash_calculate = true;
#endif
  }

  if (!hashes_to_calculate.crc32 && !hashes_to_calculate.md5 && !hashes_to_calculate.sha1 &&
      !rc_hash_calculate)
  {
    // optparse should protect from this
    fmt::print(std::cerr, "Error: No algorithms selected for the operation\n");
    return EXIT_FAILURE;
  }

  // Open the volume
  const std::unique_ptr<DiscIO::Volume> volume = DiscIO::CreateVolume(input_file_path);
  if (!volume)
  {
    fmt::print(std::cerr, "Error: Unable to open input file\n");
    return EXIT_FAILURE;
  }

  // Verify the volume
  DiscIO::VolumeVerifier verifier(*volume, false, hashes_to_calculate);
  verifier.Start();
  while (verifier.GetBytesProcessed() != verifier.GetTotalBytes())
  {
    verifier.Process();
  }
  verifier.Finish();
  const DiscIO::VolumeVerifier::Result& result = verifier.GetResult();

#ifdef USE_RETRO_ACHIEVEMENTS
  // Calculate rcheevos hash
  if (rc_hash_calculate)
  {
    rc_hash_result = AchievementManager::CalculateHash(input_file_path);
  }
#endif

  // Print the report
  if (!algorithm_is_set)
  {
    PrintFullReport(result);
  }
  else
  {
    if (hashes_to_calculate.crc32 && !result.hashes.crc32.empty())
      fmt::print(std::cout, "{}\n", HashToHexString(result.hashes.crc32));
    else if (hashes_to_calculate.md5 && !result.hashes.md5.empty())
      fmt::print(std::cout, "{}\n", HashToHexString(result.hashes.md5));
    else if (hashes_to_calculate.sha1 && !result.hashes.sha1.empty())
      fmt::print(std::cout, "{}\n", HashToHexString(result.hashes.sha1));
    else if (rc_hash_calculate)
      fmt::print(std::cout, "{}\n", rc_hash_result);
    else
    {
      fmt::print(std::cerr, "Error: No hash computed\n");
      return EXIT_FAILURE;
    }
  }

  return EXIT_SUCCESS;
}
}  // namespace DolphinTool