2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/Pl_MD5.hh>
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2009-09-26 18:36:04 +00:00
|
|
|
#include <stdexcept>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
Pl_MD5::Pl_MD5(char const* identifier, Pipeline* next) :
|
|
|
|
Pipeline(identifier, next),
|
2015-10-25 15:09:25 +00:00
|
|
|
in_progress(false),
|
|
|
|
enabled(true),
|
|
|
|
persist_across_finish(false)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-05-03 21:43:07 +00:00
|
|
|
Pl_MD5::write(unsigned char const* buf, size_t len)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2015-10-25 15:09:25 +00:00
|
|
|
if (this->enabled) {
|
|
|
|
if (!this->in_progress) {
|
|
|
|
this->md5.reset();
|
|
|
|
this->in_progress = true;
|
|
|
|
}
|
2012-06-20 15:20:57 +00:00
|
|
|
|
2015-10-25 15:09:25 +00:00
|
|
|
// 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;
|
2022-05-03 21:43:07 +00:00
|
|
|
unsigned char const* data = buf;
|
2015-10-25 15:09:25 +00:00
|
|
|
while (bytes_left > 0) {
|
|
|
|
size_t bytes = (bytes_left >= max_bytes ? max_bytes : bytes_left);
|
2022-05-03 21:43:07 +00:00
|
|
|
this->md5.encodeDataIncrementally(reinterpret_cast<char const*>(data), bytes);
|
2015-10-25 15:09:25 +00:00
|
|
|
bytes_left -= bytes;
|
|
|
|
data += bytes;
|
|
|
|
}
|
2012-06-20 15:20:57 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
this->getNext()->write(buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_MD5::finish()
|
|
|
|
{
|
|
|
|
this->getNext()->finish();
|
2015-10-25 15:09:25 +00:00
|
|
|
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;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
Pl_MD5::getHexDigest()
|
|
|
|
{
|
2015-10-25 15:09:25 +00:00
|
|
|
if (!this->enabled) {
|
2022-02-08 14:18:08 +00:00
|
|
|
throw std::logic_error("digest requested for a disabled MD5 Pipeline");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2015-10-25 15:09:25 +00:00
|
|
|
this->in_progress = false;
|
2008-04-29 12:55:25 +00:00
|
|
|
return this->md5.unparse();
|
|
|
|
}
|