Coverage Report

Created: 2025-08-28 06:33

/src/qpdf/libqpdf/Pl_MD5.cc
Line
Count
Source (jump to first uncovered line)
1
#include <qpdf/Pl_MD5.hh>
2
3
#include <stdexcept>
4
5
Pl_MD5::Pl_MD5(char const* identifier, Pipeline* next) :
6
9.22k
    Pipeline(identifier, next)
7
9.22k
{
8
9.22k
    if (!next) {
9
0
        throw std::logic_error("Attempt to create Pl_MD5 with nullptr as next");
10
0
    }
11
9.22k
}
12
13
void
14
Pl_MD5::write(unsigned char const* buf, size_t len)
15
3.37M
{
16
3.37M
    if (enabled) {
17
3.30M
        if (!in_progress) {
18
9.22k
            md5.reset();
19
9.22k
            in_progress = true;
20
9.22k
        }
21
22
        // Write in chunks in case len is too big to fit in an int. Assume int is at least 32 bits.
23
3.30M
        static size_t const max_bytes = 1 << 30;
24
3.30M
        size_t bytes_left = len;
25
3.30M
        unsigned char const* data = buf;
26
6.60M
        while (bytes_left > 0) {
27
3.30M
            size_t bytes = (bytes_left >= max_bytes ? max_bytes : bytes_left);
28
3.30M
            md5.encodeDataIncrementally(reinterpret_cast<char const*>(data), bytes);
29
3.30M
            bytes_left -= bytes;
30
3.30M
            data += bytes;
31
3.30M
        }
32
3.30M
    }
33
34
3.37M
    next()->write(buf, len);
35
3.37M
}
36
37
void
38
Pl_MD5::finish()
39
9.22k
{
40
9.22k
    next()->finish();
41
9.22k
    if (!persist_across_finish) {
42
0
        in_progress = false;
43
0
    }
44
9.22k
}
45
46
void
47
Pl_MD5::enable(bool is_enabled)
48
9.08k
{
49
9.08k
    enabled = is_enabled;
50
9.08k
}
51
52
void
53
Pl_MD5::persistAcrossFinish(bool persist)
54
9.22k
{
55
9.22k
    persist_across_finish = persist;
56
9.22k
}
57
58
std::string
59
Pl_MD5::getHexDigest()
60
9.08k
{
61
9.08k
    if (!enabled) {
62
0
        throw std::logic_error("digest requested for a disabled MD5 Pipeline");
63
0
    }
64
9.08k
    in_progress = false;
65
9.08k
    return md5.unparse();
66
9.08k
}