checksumcalculator.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (C) 2023 by Oleksandr Zolotov <alex@nextcloud.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. */
  14. #include "checksumcalculator.h"
  15. #include <zlib.h>
  16. #include <QCryptographicHash>
  17. #include <QFile>
  18. #include <QLoggingCategory>
  19. namespace
  20. {
  21. constexpr qint64 bufSize = 500 * 1024;
  22. }
  23. namespace OCC {
  24. Q_LOGGING_CATEGORY(lcChecksumCalculator, "nextcloud.common.checksumcalculator", QtInfoMsg)
  25. static QCryptographicHash::Algorithm algorithmTypeToQCryptoHashAlgorithm(ChecksumCalculator::AlgorithmType algorithmType)
  26. {
  27. switch (algorithmType) {
  28. case ChecksumCalculator::AlgorithmType::Undefined:
  29. case ChecksumCalculator::AlgorithmType::Adler32:
  30. qCWarning(lcChecksumCalculator) << "Invalid algorithm type" << static_cast<int>(algorithmType);
  31. return static_cast<QCryptographicHash::Algorithm>(-1);
  32. case ChecksumCalculator::AlgorithmType::MD5:
  33. return QCryptographicHash::Algorithm::Md5;
  34. case ChecksumCalculator::AlgorithmType::SHA1:
  35. return QCryptographicHash::Algorithm::Sha1;
  36. case ChecksumCalculator::AlgorithmType::SHA256:
  37. return QCryptographicHash::Algorithm::Sha256;
  38. case ChecksumCalculator::AlgorithmType::SHA3_256:
  39. return QCryptographicHash::Algorithm::Sha3_256;
  40. }
  41. return static_cast<QCryptographicHash::Algorithm>(-1);
  42. }
  43. ChecksumCalculator::ChecksumCalculator(QSharedPointer<QIODevice> sharedDevice, const QByteArray &checksumTypeName)
  44. : _device(sharedDevice)
  45. {
  46. if (checksumTypeName == checkSumMD5C) {
  47. _algorithmType = AlgorithmType::MD5;
  48. } else if (checksumTypeName == checkSumSHA1C) {
  49. _algorithmType = AlgorithmType::SHA1;
  50. } else if (checksumTypeName == checkSumSHA2C) {
  51. _algorithmType = AlgorithmType::SHA256;
  52. } else if (checksumTypeName == checkSumSHA3C) {
  53. _algorithmType = AlgorithmType::SHA3_256;
  54. } else if (checksumTypeName == checkSumAdlerC) {
  55. _algorithmType = AlgorithmType::Adler32;
  56. }
  57. initChecksumAlgorithm();
  58. }
  59. ChecksumCalculator::~ChecksumCalculator()
  60. {
  61. QMutexLocker locker(&_deviceMutex);
  62. if (_device && _device->isOpen()) {
  63. _device->close();
  64. }
  65. }
  66. QByteArray ChecksumCalculator::calculate()
  67. {
  68. QByteArray result;
  69. if (!_isInitialized) {
  70. return result;
  71. }
  72. Q_ASSERT(!_device->isOpen());
  73. if (_device->isOpen()) {
  74. qCWarning(lcChecksumCalculator) << "Device already open. Ignoring.";
  75. }
  76. if (!_device->isOpen() && !_device->open(QIODevice::ReadOnly)) {
  77. if (auto file = qobject_cast<QFile *>(_device.data())) {
  78. qCWarning(lcChecksumCalculator) << "Could not open file" << file->fileName() << "for reading to compute a checksum" << file->errorString();
  79. } else {
  80. qCWarning(lcChecksumCalculator) << "Could not open device" << _device.data() << "for reading to compute a checksum" << _device->errorString();
  81. }
  82. return result;
  83. }
  84. for (;;) {
  85. QMutexLocker locker(&_deviceMutex);
  86. if (!_device->isOpen() || _device->atEnd()) {
  87. break;
  88. }
  89. const auto toRead = qMin(_device->bytesAvailable(), bufSize);
  90. if (toRead <= 0) {
  91. break;
  92. }
  93. QByteArray buf(toRead, Qt::Uninitialized);
  94. const auto sizeRead = _device->read(buf.data(), toRead);
  95. if (sizeRead <= 0) {
  96. break;
  97. }
  98. if (!addChunk(buf, sizeRead)) {
  99. break;
  100. }
  101. }
  102. {
  103. QMutexLocker locker(&_deviceMutex);
  104. if (!_device->isOpen()) {
  105. return result;
  106. }
  107. }
  108. if (_algorithmType == AlgorithmType::Adler32) {
  109. result = QByteArray::number(_adlerHash, 16);
  110. } else {
  111. Q_ASSERT(_cryptographicHash);
  112. if (_cryptographicHash) {
  113. result = _cryptographicHash->result().toHex();
  114. }
  115. }
  116. {
  117. QMutexLocker locker(&_deviceMutex);
  118. if (_device->isOpen()) {
  119. _device->close();
  120. }
  121. }
  122. return result;
  123. }
  124. void ChecksumCalculator::initChecksumAlgorithm()
  125. {
  126. if (_algorithmType == AlgorithmType::Undefined) {
  127. qCWarning(lcChecksumCalculator) << "_algorithmType is Undefined, impossible to init Checksum Algorithm";
  128. return;
  129. }
  130. if (_algorithmType == AlgorithmType::Adler32) {
  131. _adlerHash = adler32(0L, Z_NULL, 0);
  132. } else {
  133. _cryptographicHash.reset(new QCryptographicHash(algorithmTypeToQCryptoHashAlgorithm(_algorithmType)));
  134. }
  135. _isInitialized = true;
  136. }
  137. bool ChecksumCalculator::addChunk(const QByteArray &chunk, const qint64 size)
  138. {
  139. Q_ASSERT(_algorithmType != AlgorithmType::Undefined);
  140. if (_algorithmType == AlgorithmType::Undefined) {
  141. qCWarning(lcChecksumCalculator) << "_algorithmType is Undefined, impossible to add a chunk!";
  142. return false;
  143. }
  144. if (_algorithmType == AlgorithmType::Adler32) {
  145. _adlerHash = adler32(_adlerHash, (const Bytef *)chunk.data(), size);
  146. return true;
  147. } else {
  148. Q_ASSERT(_cryptographicHash);
  149. if (_cryptographicHash) {
  150. _cryptographicHash->addData(chunk.data(), size);
  151. return true;
  152. }
  153. }
  154. return false;
  155. }
  156. }