File indexing completed on 2024-06-23 05:48:47

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2009 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "adler32bytearraychecksumalgorithm.hpp"
0010 
0011 // Okteta core
0012 #include <Okteta/AbstractByteArrayModel>
0013 // KF
0014 #include <KLocalizedString>
0015 
0016 static constexpr int MOD_ADLER = 65521;
0017 
0018 Adler32ByteArrayChecksumAlgorithm::Adler32ByteArrayChecksumAlgorithm()
0019     : AbstractByteArrayChecksumAlgorithm(
0020         i18nc("name of the checksum algorithm", "Adler-32"),
0021         QStringLiteral("Adler32")
0022       )
0023 {}
0024 
0025 Adler32ByteArrayChecksumAlgorithm::~Adler32ByteArrayChecksumAlgorithm() = default;
0026 
0027 AbstractByteArrayChecksumParameterSet* Adler32ByteArrayChecksumAlgorithm::parameterSet() { return &mParameterSet; }
0028 
0029 bool Adler32ByteArrayChecksumAlgorithm::calculateChecksum(QString* result,
0030                                                           const Okteta::AbstractByteArrayModel* model, const Okteta::AddressRange& range) const
0031 {
0032     quint32 a = 1;
0033     quint32 b = 0;
0034 
0035     // TODO: this is the "inefficient but straightforward implementation" from the Wikipedia entry, search for improved
0036     Okteta::Address nextBlockEnd = range.start() + CalculatedByteCountSignalLimit;
0037     for (Okteta::Address i = range.start(); i <= range.end(); ++i) {
0038         a = (a + model->byte(i)) % MOD_ADLER;
0039         b = (b + a) % MOD_ADLER;
0040 
0041         if (i >= nextBlockEnd) {
0042             nextBlockEnd += CalculatedByteCountSignalLimit;
0043             Q_EMIT calculatedBytes(range.localIndex(i) + 1);
0044         }
0045     }
0046 
0047     const quint32 sum = (b << 16) | a;
0048     *result = QStringLiteral("%1").arg(sum, 8, 16, QChar::fromLatin1('0'));
0049 
0050     return true;
0051 }
0052 
0053 #include "moc_adler32bytearraychecksumalgorithm.cpp"