Commit 0ed62d9e70f27abc85c9d757427361ade90a9962

Authored by m-holger
1 parent cbd5db35

Refactor `Pl_MD5`: inline member function implementations, improve readability, …

…and simplify pipeline logic.
libqpdf/Pl_MD5.cc
@@ -2,14 +2,6 @@ @@ -2,14 +2,6 @@
2 2
3 #include <stdexcept> 3 #include <stdexcept>
4 4
5 -Pl_MD5::Pl_MD5(char const* identifier, Pipeline* next) :  
6 - Pipeline(identifier, next)  
7 -{  
8 - if (!next) {  
9 - throw std::logic_error("Attempt to create Pl_MD5 with nullptr as next");  
10 - }  
11 -}  
12 -  
13 void 5 void
14 Pl_MD5::write(unsigned char const* buf, size_t len) 6 Pl_MD5::write(unsigned char const* buf, size_t len)
15 { 7 {
@@ -31,36 +23,7 @@ Pl_MD5::write(unsigned char const* buf, size_t len) @@ -31,36 +23,7 @@ Pl_MD5::write(unsigned char const* buf, size_t len)
31 } 23 }
32 } 24 }
33 25
34 - next()->write(buf, len);  
35 -}  
36 -  
37 -void  
38 -Pl_MD5::finish()  
39 -{  
40 - next()->finish();  
41 - if (!persist_across_finish) {  
42 - in_progress = false;  
43 - }  
44 -}  
45 -  
46 -void  
47 -Pl_MD5::enable(bool is_enabled)  
48 -{  
49 - enabled = is_enabled;  
50 -}  
51 -  
52 -void  
53 -Pl_MD5::persistAcrossFinish(bool persist)  
54 -{  
55 - persist_across_finish = persist;  
56 -}  
57 -  
58 -std::string  
59 -Pl_MD5::getHexDigest()  
60 -{  
61 - if (!enabled) {  
62 - throw std::logic_error("digest requested for a disabled MD5 Pipeline"); 26 + if (next()) {
  27 + next()->write(buf, len);
63 } 28 }
64 - in_progress = false;  
65 - return md5.unparse();  
66 } 29 }
libqpdf/qpdf/Pl_MD5.hh
@@ -4,6 +4,8 @@ @@ -4,6 +4,8 @@
4 #include <qpdf/MD5.hh> 4 #include <qpdf/MD5.hh>
5 #include <qpdf/Pipeline.hh> 5 #include <qpdf/Pipeline.hh>
6 6
  7 +#include <stdexcept>
  8 +
7 // This pipeline sends its output to its successor unmodified. After calling finish, the MD5 9 // This pipeline sends its output to its successor unmodified. After calling finish, the MD5
8 // checksum of the data that passed through the pipeline is available. 10 // checksum of the data that passed through the pipeline is available.
9 11
@@ -12,18 +14,47 @@ @@ -12,18 +14,47 @@
12 class Pl_MD5 final: public Pipeline 14 class Pl_MD5 final: public Pipeline
13 { 15 {
14 public: 16 public:
15 - Pl_MD5(char const* identifier, Pipeline* next); 17 + Pl_MD5(char const* identifier, Pipeline* next = nullptr) :
  18 + Pipeline(identifier, next)
  19 + {
  20 + }
  21 +
16 ~Pl_MD5() final = default; 22 ~Pl_MD5() final = default;
17 void write(unsigned char const*, size_t) final; 23 void write(unsigned char const*, size_t) final;
18 - void finish() final;  
19 - std::string getHexDigest(); 24 + void
  25 + finish() final
  26 + {
  27 + if (next()) {
  28 + next()->finish();
  29 + }
  30 + if (!persist_across_finish) {
  31 + in_progress = false;
  32 + }
  33 + }
  34 + std::string
  35 + getHexDigest()
  36 + {
  37 + if (!enabled) {
  38 + throw std::logic_error("digest requested for a disabled MD5 Pipeline");
  39 + }
  40 + in_progress = false;
  41 + return md5.unparse();
  42 + }
20 // Enable/disable. Disabling the pipeline causes it to become a pass-through. This makes it 43 // Enable/disable. Disabling the pipeline causes it to become a pass-through. This makes it
21 // possible to stick an MD5 pipeline in a pipeline when it may or may not be required. Disabling 44 // possible to stick an MD5 pipeline in a pipeline when it may or may not be required. Disabling
22 // it avoids incurring the runtime overhead of doing needless digest computation. 45 // it avoids incurring the runtime overhead of doing needless digest computation.
23 - void enable(bool enabled); 46 + void
  47 + enable(bool val)
  48 + {
  49 + enabled = val;
  50 + }
24 // If persistAcrossFinish is called, calls to finish do not finalize the underlying md5 object. 51 // If persistAcrossFinish is called, calls to finish do not finalize the underlying md5 object.
25 // In this case, the object is not finalized until getHexDigest() is called. 52 // In this case, the object is not finalized until getHexDigest() is called.
26 - void persistAcrossFinish(bool); 53 + void
  54 + persistAcrossFinish(bool val)
  55 + {
  56 + persist_across_finish = val;
  57 + }
27 58
28 private: 59 private:
29 bool in_progress{false}; 60 bool in_progress{false};