VolumeVerifier: Calculate CRC32/MD5/SHA-1

This commit is contained in:
JosJuice
2019-03-30 12:45:00 +01:00
parent 4fd2d8e8c4
commit eced9d7c7e
6 changed files with 151 additions and 63 deletions

View File

@ -78,7 +78,6 @@ QGroupBox* InfoWidget::CreateISODetails()
QLineEdit* maker =
CreateValueDisplay((game_maker.empty() ? UNKNOWN_NAME.toStdString() : game_maker) + " (" +
m_game.GetMakerID() + ")");
QWidget* checksum = CreateChecksumComputer();
layout->addRow(tr("Name:"), internal_name);
layout->addRow(tr("File:"), file_path);
@ -89,8 +88,6 @@ QGroupBox* InfoWidget::CreateISODetails()
if (!m_game.GetApploaderDate().empty())
layout->addRow(tr("Apploader Date:"), CreateValueDisplay(m_game.GetApploaderDate()));
layout->addRow(tr("MD5 Checksum:"), checksum);
group->setLayout(layout);
return group;
}
@ -194,53 +191,3 @@ void InfoWidget::ChangeLanguage()
m_maker->setText(QString::fromStdString(m_game.GetLongMaker(language)));
m_description->setText(QString::fromStdString(m_game.GetDescription(language)));
}
QWidget* InfoWidget::CreateChecksumComputer()
{
QWidget* widget = new QWidget();
QHBoxLayout* layout = new QHBoxLayout();
layout->setContentsMargins(0, 0, 0, 0);
m_checksum_result = new QLineEdit();
m_checksum_result->setReadOnly(true);
QPushButton* calculate = new QPushButton(tr("Compute"));
connect(calculate, &QPushButton::clicked, this, &InfoWidget::ComputeChecksum);
layout->addWidget(m_checksum_result);
layout->addWidget(calculate);
widget->setLayout(layout);
return widget;
}
void InfoWidget::ComputeChecksum()
{
QCryptographicHash hash(QCryptographicHash::Md5);
hash.reset();
std::unique_ptr<DiscIO::BlobReader> file(DiscIO::CreateBlobReader(m_game.GetFilePath()));
std::vector<u8> file_data(8 * 1080 * 1080); // read 1MB at a time
u64 game_size = file->GetDataSize();
u64 read_offset = 0;
// a maximum of 1000 is used instead of game_size because otherwise 8GB games overflow the int
// typed maximum parameter
QProgressDialog* progress =
new QProgressDialog(tr("Computing MD5 Checksum"), tr("Cancel"), 0, 1000, this);
progress->setWindowTitle(tr("Computing MD5 Checksum"));
progress->setWindowFlags(progress->windowFlags() & ~Qt::WindowContextHelpButtonHint);
progress->setMinimumDuration(500);
progress->setWindowModality(Qt::WindowModal);
while (read_offset < game_size)
{
progress->setValue(static_cast<double>(read_offset) / static_cast<double>(game_size) * 1000);
if (progress->wasCanceled())
return;
u64 read_size = std::min<u64>(file_data.size(), game_size - read_offset);
file->Read(read_offset, read_size, file_data.data());
hash.addData(reinterpret_cast<char*>(file_data.data()), read_size);
read_offset += read_size;
}
m_checksum_result->setText(QString::fromUtf8(hash.result().toHex()));
Q_ASSERT(read_offset == game_size);
progress->setValue(1000);
}

View File

@ -23,7 +23,6 @@ public:
explicit InfoWidget(const UICommon::GameFile& game);
private:
void ComputeChecksum();
void ChangeLanguage();
void SaveBanner();
@ -31,12 +30,10 @@ private:
QGroupBox* CreateISODetails();
QLineEdit* CreateValueDisplay(const QString& value);
QLineEdit* CreateValueDisplay(const std::string& value = "");
QWidget* CreateChecksumComputer();
void CreateLanguageSelector();
QWidget* CreateBannerGraphic(const QPixmap& image);
UICommon::GameFile m_game;
QLineEdit* m_checksum_result;
QComboBox* m_language_selector;
QLineEdit* m_name;
QLineEdit* m_maker;

View File

@ -5,12 +5,17 @@
#include "DolphinQt/Config/VerifyWidget.h"
#include <memory>
#include <tuple>
#include <vector>
#include <QByteArray>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QLabel>
#include <QProgressDialog>
#include <QVBoxLayout>
#include "Common/CommonTypes.h"
#include "DiscIO/Volume.h"
#include "DiscIO/VolumeVerifier.h"
@ -23,6 +28,7 @@ VerifyWidget::VerifyWidget(std::shared_ptr<DiscIO::Volume> volume) : m_volume(st
layout->addWidget(m_problems);
layout->addWidget(m_summary_text);
layout->addLayout(m_hash_layout);
layout->addWidget(m_verify_button);
layout->setStretchFactor(m_problems, 5);
@ -44,17 +50,47 @@ void VerifyWidget::CreateWidgets()
m_summary_text = new QTextEdit(this);
m_summary_text->setReadOnly(true);
m_hash_layout = new QFormLayout(this);
std::tie(m_crc32_checkbox, m_crc32_line_edit) = AddHashLine(m_hash_layout, tr("CRC32:"));
std::tie(m_md5_checkbox, m_md5_line_edit) = AddHashLine(m_hash_layout, tr("MD5:"));
std::tie(m_sha1_checkbox, m_sha1_line_edit) = AddHashLine(m_hash_layout, tr("SHA-1:"));
m_verify_button = new QPushButton(tr("Verify Integrity"), this);
}
std::pair<QCheckBox*, QLineEdit*> VerifyWidget::AddHashLine(QFormLayout* layout, QString text)
{
QLineEdit* line_edit = new QLineEdit(this);
line_edit->setReadOnly(true);
QCheckBox* checkbox = new QCheckBox(tr("Calculate"), this);
checkbox->setChecked(true);
QHBoxLayout* hbox_layout = new QHBoxLayout(this);
hbox_layout->addWidget(line_edit);
hbox_layout->addWidget(checkbox);
layout->addRow(text, hbox_layout);
return std::pair(checkbox, line_edit);
}
void VerifyWidget::ConnectWidgets()
{
connect(m_verify_button, &QPushButton::clicked, this, &VerifyWidget::Verify);
}
static void SetHash(QLineEdit* line_edit, const std::vector<u8>& hash)
{
const QByteArray byte_array = QByteArray::fromRawData(reinterpret_cast<const char*>(hash.data()),
static_cast<int>(hash.size()));
line_edit->setText(QString::fromLatin1(byte_array.toHex()));
}
void VerifyWidget::Verify()
{
DiscIO::VolumeVerifier verifier(*m_volume);
DiscIO::VolumeVerifier verifier(
*m_volume,
{m_crc32_checkbox->isChecked(), m_md5_checkbox->isChecked(), m_sha1_checkbox->isChecked()});
// We have to divide the number of processed bytes with something so it won't make ints overflow
constexpr int DIVISOR = 0x100;
@ -104,6 +140,10 @@ void VerifyWidget::Verify()
SetProblemCellText(i, 0, QString::fromStdString(problem.text));
SetProblemCellText(i, 1, severity);
}
SetHash(m_crc32_line_edit, result.hashes.crc32);
SetHash(m_md5_line_edit, result.hashes.md5);
SetHash(m_sha1_line_edit, result.hashes.sha1);
}
void VerifyWidget::SetProblemCellText(int row, int column, QString text)

View File

@ -6,7 +6,11 @@
#include <memory>
#include <string>
#include <utility>
#include <QCheckBox>
#include <QFormLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QTableWidget>
#include <QTextEdit>
@ -25,6 +29,7 @@ public:
private:
void CreateWidgets();
std::pair<QCheckBox*, QLineEdit*> AddHashLine(QFormLayout* layout, QString text);
void ConnectWidgets();
void Verify();
@ -33,5 +38,12 @@ private:
std::shared_ptr<DiscIO::Volume> m_volume;
QTableWidget* m_problems;
QTextEdit* m_summary_text;
QFormLayout* m_hash_layout;
QCheckBox* m_crc32_checkbox;
QCheckBox* m_md5_checkbox;
QCheckBox* m_sha1_checkbox;
QLineEdit* m_crc32_line_edit;
QLineEdit* m_md5_line_edit;
QLineEdit* m_sha1_line_edit;
QPushButton* m_verify_button;
};