mirror of
https://github.com/qpdf/qpdf.git
synced 2024-11-10 23:20:58 +00:00
cb769c62e5
This comment expands all tabs using an 8-character tab-width. You should ignore this commit when using git blame or use git blame -w. In the early days, I used to use tabs where possible for indentation, since emacs did this automatically. In recent years, I have switched to only using spaces, which means qpdf source code has been a mixture of spaces and tabs. I have avoided cleaning this up because of not wanting gratuitous whitespaces change to cloud the output of git blame, but I changed my mind after discussing with users who view qpdf source code in editors/IDEs that have other tab widths by default and in light of the fact that I am planning to start applying automatic code formatting soon.
79 lines
1.5 KiB
C++
79 lines
1.5 KiB
C++
#include <qpdf/Pl_MD5.hh>
|
|
|
|
#include <stdexcept>
|
|
|
|
Pl_MD5::Pl_MD5(char const* identifier, Pipeline* next) :
|
|
Pipeline(identifier, next),
|
|
in_progress(false),
|
|
enabled(true),
|
|
persist_across_finish(false)
|
|
{
|
|
}
|
|
|
|
Pl_MD5::~Pl_MD5()
|
|
{
|
|
}
|
|
|
|
void
|
|
Pl_MD5::write(unsigned char* buf, size_t len)
|
|
{
|
|
if (this->enabled)
|
|
{
|
|
if (! this->in_progress)
|
|
{
|
|
this->md5.reset();
|
|
this->in_progress = true;
|
|
}
|
|
|
|
// Write in chunks in case len is too big to fit in an int.
|
|
// Assume int is at least 32 bits.
|
|
static size_t const max_bytes = 1 << 30;
|
|
size_t bytes_left = len;
|
|
unsigned char* data = buf;
|
|
while (bytes_left > 0)
|
|
{
|
|
size_t bytes = (bytes_left >= max_bytes ? max_bytes : bytes_left);
|
|
this->md5.encodeDataIncrementally(
|
|
reinterpret_cast<char*>(data), bytes);
|
|
bytes_left -= bytes;
|
|
data += bytes;
|
|
}
|
|
}
|
|
|
|
this->getNext()->write(buf, len);
|
|
}
|
|
|
|
void
|
|
Pl_MD5::finish()
|
|
{
|
|
this->getNext()->finish();
|
|
if (! this->persist_across_finish)
|
|
{
|
|
this->in_progress = false;
|
|
}
|
|
}
|
|
|
|
void
|
|
Pl_MD5::enable(bool enabled)
|
|
{
|
|
this->enabled = enabled;
|
|
}
|
|
|
|
void
|
|
Pl_MD5::persistAcrossFinish(bool persist)
|
|
{
|
|
this->persist_across_finish = persist;
|
|
}
|
|
|
|
std::string
|
|
Pl_MD5::getHexDigest()
|
|
{
|
|
if (! this->enabled)
|
|
{
|
|
throw std::logic_error(
|
|
"digest requested for a disabled MD5 Pipeline");
|
|
}
|
|
this->in_progress = false;
|
|
return this->md5.unparse();
|
|
}
|