Coverage Report

Created: 2026-07-30 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/libqpdf/qpdf/Pl_MD5.hh
Line
Count
Source
1
#ifndef PL_MD5_HH
2
#define PL_MD5_HH
3
4
#include <qpdf/MD5.hh>
5
#include <qpdf/Pipeline.hh>
6
#include <qpdf/Util.hh>
7
8
// This pipeline sends its output to its successor unmodified.  After calling finish, the MD5
9
// checksum of the data that passed through the pipeline is available.
10
//
11
// This pipeline is reusable; i.e., it is safe to call write() after calling finish().  The first
12
// call to write() after a call to finish() initializes a new MD5 object.
13
class Pl_MD5 final: public Pipeline
14
{
15
  public:
16
    Pl_MD5(char const* identifier, Pipeline* next = nullptr) :
17
9.55k
        Pipeline(identifier, next)
18
9.55k
    {
19
9.55k
    }
20
21
9.55k
    ~Pl_MD5() final = default;
22
    void write(unsigned char const*, size_t) final;
23
    void
24
    finish() final
25
9.55k
    {
26
9.55k
        if (next()) {
27
9.55k
            next()->finish();
28
9.55k
        }
29
9.55k
        if (!persist_across_finish) {
30
0
            in_progress = false;
31
0
        }
32
9.55k
    }
33
    std::string
34
    getHexDigest()
35
9.35k
    {
36
9.35k
        qpdf::util::assertion(enabled, "digest requested for a disabled MD5 Pipeline");
37
9.35k
        in_progress = false;
38
9.35k
        return md5.unparse();
39
9.35k
    }
40
    // Enable/disable. Disabling the pipeline causes it to become a pass-through. This makes it
41
    // possible to stick an MD5 pipeline in a pipeline when it may or may not be required. Disabling
42
    // it avoids incurring the runtime overhead of doing needless digest computation.
43
    void
44
    enable(bool val)
45
9.35k
    {
46
9.35k
        enabled = val;
47
9.35k
    }
48
    // If persistAcrossFinish is called, calls to finish do not finalize the underlying md5 object.
49
    // In this case, the object is not finalized until getHexDigest() is called.
50
    void
51
    persistAcrossFinish(bool val)
52
9.55k
    {
53
9.55k
        persist_across_finish = val;
54
9.55k
    }
55
56
  private:
57
    bool in_progress{false};
58
    MD5 md5;
59
    bool enabled{true};
60
    bool persist_across_finish{false};
61
};
62
63
#endif // PL_MD5_HH