diff --git a/libqpdf/Pl_MD5.cc b/libqpdf/Pl_MD5.cc index a5985a9..5cf5dba 100644 --- a/libqpdf/Pl_MD5.cc +++ b/libqpdf/Pl_MD5.cc @@ -2,14 +2,6 @@ #include -Pl_MD5::Pl_MD5(char const* identifier, Pipeline* next) : - Pipeline(identifier, next) -{ - if (!next) { - throw std::logic_error("Attempt to create Pl_MD5 with nullptr as next"); - } -} - void Pl_MD5::write(unsigned char const* buf, size_t len) { @@ -31,36 +23,7 @@ Pl_MD5::write(unsigned char const* buf, size_t len) } } - next()->write(buf, len); -} - -void -Pl_MD5::finish() -{ - next()->finish(); - if (!persist_across_finish) { - in_progress = false; - } -} - -void -Pl_MD5::enable(bool is_enabled) -{ - enabled = is_enabled; -} - -void -Pl_MD5::persistAcrossFinish(bool persist) -{ - persist_across_finish = persist; -} - -std::string -Pl_MD5::getHexDigest() -{ - if (!enabled) { - throw std::logic_error("digest requested for a disabled MD5 Pipeline"); + if (next()) { + next()->write(buf, len); } - in_progress = false; - return md5.unparse(); } diff --git a/libqpdf/qpdf/Pl_MD5.hh b/libqpdf/qpdf/Pl_MD5.hh index 31ae263..37c9380 100644 --- a/libqpdf/qpdf/Pl_MD5.hh +++ b/libqpdf/qpdf/Pl_MD5.hh @@ -4,6 +4,8 @@ #include #include +#include + // This pipeline sends its output to its successor unmodified. After calling finish, the MD5 // checksum of the data that passed through the pipeline is available. @@ -12,18 +14,47 @@ class Pl_MD5 final: public Pipeline { public: - Pl_MD5(char const* identifier, Pipeline* next); + Pl_MD5(char const* identifier, Pipeline* next = nullptr) : + Pipeline(identifier, next) + { + } + ~Pl_MD5() final = default; void write(unsigned char const*, size_t) final; - void finish() final; - std::string getHexDigest(); + void + finish() final + { + if (next()) { + next()->finish(); + } + if (!persist_across_finish) { + in_progress = false; + } + } + std::string + getHexDigest() + { + if (!enabled) { + throw std::logic_error("digest requested for a disabled MD5 Pipeline"); + } + in_progress = false; + return md5.unparse(); + } // Enable/disable. Disabling the pipeline causes it to become a pass-through. This makes it // possible to stick an MD5 pipeline in a pipeline when it may or may not be required. Disabling // it avoids incurring the runtime overhead of doing needless digest computation. - void enable(bool enabled); + void + enable(bool val) + { + enabled = val; + } // If persistAcrossFinish is called, calls to finish do not finalize the underlying md5 object. // In this case, the object is not finalized until getHexDigest() is called. - void persistAcrossFinish(bool); + void + persistAcrossFinish(bool val) + { + persist_across_finish = val; + } private: bool in_progress{false};