27 #include <shared_mutex>
78 resetChecksumToProcess();
84 unique_lock lock(mutex);
96 unique_lock lock(mutex);
97 if (curCk < checksums.size())
98 return checksums[curCk++];
128 DefaultChecksumCalculatorProgress ChecksumCalculator::defaultChecksumCalculatorProgress;
135 const unsigned int newNbThreads,
136 const size_t newBufferSize) :
137 progress(progressHandler)
189 unsigned int autoNbThreads = thread::hardware_concurrency();
190 if (autoNbThreads == 0u)
195 const unsigned int knownAlgos = (ckfKnownAlgos <= 0) ? 1u : ckfKnownAlgos;
196 nbThreads = min(knownAlgos, autoNbThreads);
202 const unsigned int knownAlgos = (ckfKnownAlgos <= 0) ? 1u : ckfKnownAlgos;
204 nbThreads = min(newNbThreads, maxNbThreads);
234 sumValue = values.front();
253 checksums.reserve(ids.size());
256 bool notFound =
true;
257 ArrayChecksum::const_iterator it = checksums.cbegin();
258 while (notFound && it != checksums.cend())
260 if ((*it)->getID() ==
id)
269 checksums.push_back(c);
274 if (checksums.empty())
281 uint8_t* buff =
new uint8_t[bufSize];
286 bool cancelled =
false;
288 ios::iostate errorState = ios::goodbit;
290 const size_t s = checksums.size();
294 if (nThreads >
static_cast<unsigned int>(s))
295 nThreads =
static_cast<unsigned int>(s);
301 vector<thread> threads(nThreads);
304 while (!cancelled && !is.eof() && (errorState & ios::goodbit) == ios::goodbit)
306 read = is.read(
reinterpret_cast<char*
>(buff), bufSize).gcount();
307 if (read > 0u && read <= bufSize)
309 for (i = 0u; i < nThreads; i++)
311 for (i = 0u; i < nThreads; i++)
316 errorState = is.rdstate();
321 while (!cancelled && !is.eof() && (errorState & ios::goodbit) == ios::goodbit)
323 read = is.read(
reinterpret_cast<char*
>(buff), bufSize).gcount();
324 if (read > 0u && read <= bufSize)
326 for (i = 0u; i < s; i++)
327 checksums[i]->update(buff, read);
330 errorState = is.rdstate();
339 for_each(checksums.begin(), checksums.end(), [](
Checksum* c){ delete c; });
343 if (((errorState & ios::goodbit) != ios::goodbit && (errorState & ios::eofbit) != ios::eofbit))
345 for_each(checksums.begin(), checksums.end(), [](
Checksum* c){ delete c; });
376 const unsigned int newNbThreads,
377 const size_t newBufferSize) :
391 const std::filesystem::path& filepath)
394 if (!filesystem::exists(filepath, ec))
397 ifstream is(filepath, ios_base::in | ios_base::binary);
411 const std::filesystem::path& filepath)
414 if (!filesystem::exists(filepath, ec))
417 ifstream is(filepath, ios_base::in | ios_base::binary);
Interface for classes that compute checksums.
vector< Checksum * > ArrayChecksum
Array of pointers on checksums.
Utility classes to compute and verify the checksums.
Classes for enumerate and create all the checksums' algorithms that the application knows.
std::vector< ChecksumValue > ArrayChecksumValue
Array of values of checksum.
Handles the progression of the process of computing of a checksum.
virtual void update(size_t read, bool &cancelled)=0
Updates the progress of the computing of a checksum.
size_t setBufferSize(const size_t newBufSize)
Sets the size of the buffer to use for reading in the input stream.
ChecksumCalculatorProgress & progress
The progress handler used to show the progression.
State
States that can be returned by the calculate or the check method.
@ FileNotFound
The file has been not found.
@ Ok
Checksums have been calculated.
@ OutOfMemory
No enough memory left.
@ CantOpenFile
Can't open the stream.
@ NoValidAlgoId
No valid id of algorithm of checksum has been provided.
@ CancelledByUser
User has cancelled the calculation.
@ ReadError
Error while reading the stream.
unsigned int setNbThreads(const unsigned int newNbThreads)
Sets the number of threads to use to compute checksums.
static constexpr unsigned int automatic_thread_number
System dependent value for number of threads for computing checksums.
static constexpr size_t max_buffer_size
Maximal buffer size for reading streams.
size_t bufferSize
The size of the buffer to use for reading the input stream.
static constexpr size_t default_buffer_size
Default buffer size for reading streams.
ChecksumCalculatorProgress & getChecksumProgress() const
Gets the progress handler used to show the progression.
static constexpr unsigned int max_thread_number
Maximal value for number of threads for computing checksums.
static constexpr size_t min_buffer_size
Minimal buffer size for reading streams.
static constexpr unsigned int default_thread_number
Default number of threads for computing checksums if the number of cores of the system cannot be auto...
unsigned int nbThreads
Number of threads to compute checksums.
size_t getBufferSize() const
Gets the size of the buffer to use for reading in the input stream.
State calculate(ChecksumValue &sumValue, const ChecksumAlgoId id, std::istream &is)
Calculates the checksum from the given stream.
unsigned int getNbThreads() const
Returns the number of threads to use to compute checksums.
static Checksum * getNewInstance(const ChecksumAlgoId id)
Gives a pointer on a new instance of the specified checksum or hash identifier.
static int getAlgorithmsCount()
Returns the number of available checksum or hash algorithms.
State calculate(ChecksumValue &sumValue, const ChecksumAlgoId id, const std::filesystem::path &filepath)
Calculates the checksum from the given file.
ChecksumFileCalculator(ChecksumCalculatorProgress &progressHandler=defaultChecksumCalculatorProgress, const unsigned int newNbThreads=automatic_thread_number, const size_t newBufferSize=default_file_buffer_size)
Constructor.
Stores the value of a checksum.
Computes a checksum from a byte stream.
virtual void update(const uint8_t *buf, size_t len)=0
Updates the checksum with specified array of bytes.
A thread safe checksums calculator.
ArrayChecksum checksums
Array of checksums.
ThreadSafeChecksumCalculator & operator=(const ThreadSafeChecksumCalculator &)=delete
Delete assignment operator.
size_t curCk
Current checksum to process.
Checksum * nextChecksumToProcess()
Gets the next checksum to process.
ThreadSafeChecksumCalculator()=delete
Deleted default constructor.
ThreadSafeChecksumCalculator(const ThreadSafeChecksumCalculator &)=delete
Deleted copy constructor.
void resetChecksumToProcess()
Resets the current checksum to process.
static void updateChecksum(ThreadSafeChecksumCalculator *c, const uint8_t *buf, size_t len)
Updates the checksums with specified array of bytes.
ThreadSafeChecksumCalculator(const ArrayChecksum &ckArray)
Constructor.
std::vector< ChecksumAlgoId > ArrayChecksumAlgoId
Array of ids of algorithms of checksums.
ChecksumAlgoId
Ids of algorithms of checksums.