From c8cc7d5b521c29e0968ce11dd9c63de0ce260c49 Mon Sep 17 00:00:00 2001 From: kh1 Date: Fri, 28 Sep 2012 14:46:17 +0200 Subject: Merge "buffered" calculateHash function and reuse. Change-Id: I294038888bd47a139b8c3df68e298e66e5ec2202 Reviewed-by: Tim Jenssen --- src/libs/installer/downloadarchivesjob.cpp | 13 +++---------- src/libs/installer/getrepositorymetainfojob.cpp | 7 +++---- src/libs/installer/utils.cpp | 11 ++++++++--- src/libs/installer/utils.h | 1 + src/libs/kdtools/kdupdaterfiledownloader.cpp | 24 ------------------------ src/libs/kdtools/kdupdaterfiledownloader.h | 4 ---- src/libs/kdtools/kdupdaterfiledownloader_p.h | 1 - 7 files changed, 15 insertions(+), 46 deletions(-) (limited to 'src') diff --git a/src/libs/installer/downloadarchivesjob.cpp b/src/libs/installer/downloadarchivesjob.cpp index f33108442..f44c5c1f8 100644 --- a/src/libs/installer/downloadarchivesjob.cpp +++ b/src/libs/installer/downloadarchivesjob.cpp @@ -35,6 +35,7 @@ #include "component.h" #include "messageboxhandler.h" #include "packagemanagercore.h" +#include "utils.h" #include "kdupdaterfiledownloader.h" #include "kdupdaterfiledownloaderfactory.h" @@ -227,16 +228,8 @@ void DownloadArchivesJob::registerFile() if (m_core->testChecksum()) { QFile archiveFile(tempFile); if (archiveFile.open(QFile::ReadOnly)) { - static QByteArray buffer(1024 * 1024, '\0'); - QCryptographicHash hash(QCryptographicHash::Sha1); - while (true) { - const qint64 numRead = archiveFile.read(buffer.data(), buffer.size()); - if (numRead <= 0) - break; - hash.addData(buffer.constData(), numRead); - } - - const QByteArray archiveHash = hash.result().toHex(); + const QByteArray archiveHash = QInstaller::calculateHash(&archiveFile, QCryptographicHash::Sha1) + .toHex(); if ((archiveHash != m_currentHash) && (!m_canceled)) { //TODO: Maybe we should try to download the file again automatically const QMessageBox::Button res = diff --git a/src/libs/installer/getrepositorymetainfojob.cpp b/src/libs/installer/getrepositorymetainfojob.cpp index 22fd3c0fd..689f4a4ed 100644 --- a/src/libs/installer/getrepositorymetainfojob.cpp +++ b/src/libs/installer/getrepositorymetainfojob.cpp @@ -36,6 +36,7 @@ #include "messageboxhandler.h" #include "packagemanagercore_p.h" #include "qinstallerglobal.h" +#include "utils.h" #include "kdupdaterfiledownloader.h" #include "kdupdaterfiledownloaderfactory.h" @@ -466,10 +467,8 @@ void GetRepositoryMetaInfoJob::metaDownloadFinished() if (!m_packageHash.isEmpty()) { // verify file hash - QByteArray expectedFileHash = m_packageHash.back().toLatin1(); - QByteArray archContent = arch.readAll(); - QByteArray realFileHash = QString::fromLatin1(QCryptographicHash::hash(archContent, - QCryptographicHash::Sha1).toHex()).toLatin1(); + const QByteArray expectedFileHash = m_packageHash.back().toLatin1(); + const QByteArray realFileHash = QInstaller::calculateHash(&arch, QCryptographicHash::Sha1).toHex(); if (expectedFileHash != realFileHash) { emit infoMessage(this, tr("The hash of one component does not match the expected one.")); metaDownloadError(tr("Bad hash.")); diff --git a/src/libs/installer/utils.cpp b/src/libs/installer/utils.cpp index 4bd93430f..ffe705ba4 100644 --- a/src/libs/installer/utils.cpp +++ b/src/libs/installer/utils.cpp @@ -103,13 +103,11 @@ std::ostream &QInstaller::operator<<(std::ostream &os, const QString &string) return os << qPrintable(string); } -//TODO from kdupdaterfiledownloader.cpp, use that one once merged QByteArray QInstaller::calculateHash(QIODevice *device, QCryptographicHash::Algorithm algo) { Q_ASSERT(device); QCryptographicHash hash(algo); - QByteArray buffer; - buffer.resize(512 * 1024); + static QByteArray buffer(1024 * 1024, '\0'); while (true) { const qint64 numRead = device->read(buffer.data(), buffer.size()); if (numRead <= 0) @@ -119,6 +117,13 @@ QByteArray QInstaller::calculateHash(QIODevice *device, QCryptographicHash::Algo return QByteArray(); // never reached } +QByteArray QInstaller::calculateHash(const QString &path, QCryptographicHash::Algorithm algo) +{ + QFile file(path); + if (!file.open(QIODevice::ReadOnly)) + return QByteArray(); + return calculateHash(&file, algo); +} QString QInstaller::replaceVariables(const QHash &vars, const QString &str) { diff --git a/src/libs/installer/utils.h b/src/libs/installer/utils.h index cbf2e95e7..d4c64f579 100644 --- a/src/libs/installer/utils.h +++ b/src/libs/installer/utils.h @@ -50,6 +50,7 @@ QT_END_NAMESPACE namespace QInstaller { QByteArray INSTALLER_EXPORT calculateHash(QIODevice *device, QCryptographicHash::Algorithm algo); + QByteArray INSTALLER_EXPORT calculateHash(const QString &path, QCryptographicHash::Algorithm algo); QString INSTALLER_EXPORT replaceVariables(const QHash &vars, const QString &str); QString INSTALLER_EXPORT replaceWindowsEnvironmentVariables(const QString &str); diff --git a/src/libs/kdtools/kdupdaterfiledownloader.cpp b/src/libs/kdtools/kdupdaterfiledownloader.cpp index 818c8ac70..ec2293a2c 100644 --- a/src/libs/kdtools/kdupdaterfiledownloader.cpp +++ b/src/libs/kdtools/kdupdaterfiledownloader.cpp @@ -35,7 +35,6 @@ #include #include #include -#include #include #include #include @@ -50,29 +49,6 @@ static double calcProgress(qint32 done, qint32 total) return total ? (double(done) / double(total)) : 0; } -QByteArray KDUpdater::calculateHash(QIODevice* device, QCryptographicHash::Algorithm algo) -{ - Q_ASSERT(device); - QCryptographicHash hash(algo); - QByteArray buffer; - buffer.resize(512 * 1024); - while (true) { - const qint64 numRead = device->read(buffer.data(), buffer.size()); - if (numRead <= 0) - return hash.result(); - hash.addData(buffer.constData(), numRead); - } - return QByteArray(); // never reached -} - -QByteArray KDUpdater::calculateHash(const QString &path, QCryptographicHash::Algorithm algo) -{ - QFile file(path); - if (!file.open(QIODevice::ReadOnly)) - return QByteArray(); - return calculateHash(&file, algo); -} - // -- HashVerificationJob diff --git a/src/libs/kdtools/kdupdaterfiledownloader.h b/src/libs/kdtools/kdupdaterfiledownloader.h index 6408b7216..6f6d03cc5 100644 --- a/src/libs/kdtools/kdupdaterfiledownloader.h +++ b/src/libs/kdtools/kdupdaterfiledownloader.h @@ -28,15 +28,11 @@ #include #include -#include #include namespace KDUpdater { -KDTOOLS_EXPORT QByteArray calculateHash(QIODevice *device, QCryptographicHash::Algorithm algo); -KDTOOLS_EXPORT QByteArray calculateHash(const QString &path, QCryptographicHash::Algorithm algo); - class HashVerificationJob; class FileDownloaderProxyFactory; diff --git a/src/libs/kdtools/kdupdaterfiledownloader_p.h b/src/libs/kdtools/kdupdaterfiledownloader_p.h index 81b3c2c98..c47663bd1 100644 --- a/src/libs/kdtools/kdupdaterfiledownloader_p.h +++ b/src/libs/kdtools/kdupdaterfiledownloader_p.h @@ -25,7 +25,6 @@ #include "kdupdaterfiledownloader.h" -#include #include // these classes are not a part of the public API -- cgit v1.2.3