Coverage Report

Created: 2025-08-26 07:10

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