Coverage Report

Created: 2025-06-22 06:30

/src/qpdf/libqpdf/Pl_SHA2.cc
Line
Count
Source (jump to first uncovered line)
1
#include <qpdf/Pl_SHA2.hh>
2
3
#include <qpdf/QPDFCryptoProvider.hh>
4
#include <qpdf/QUtil.hh>
5
#include <stdexcept>
6
7
Pl_SHA2::Pl_SHA2(int bits, Pipeline* next) :
8
59.5k
    Pipeline("sha2", next)
9
59.5k
{
10
59.5k
    if (bits) {
11
59.5k
        resetBits(bits);
12
59.5k
    }
13
59.5k
}
14
15
void
16
Pl_SHA2::write(unsigned char const* buf, size_t len)
17
61.2k
{
18
61.2k
    if (!this->in_progress) {
19
59.5k
        this->in_progress = true;
20
59.5k
    }
21
22
    // Write in chunks in case len is too big to fit in an int. Assume int is at least 32 bits.
23
61.2k
    static size_t const max_bytes = 1 << 30;
24
61.2k
    size_t bytes_left = len;
25
61.2k
    unsigned char const* data = buf;
26
121k
    while (bytes_left > 0) {
27
59.9k
        size_t bytes = (bytes_left >= max_bytes ? max_bytes : bytes_left);
28
59.9k
        this->crypto->SHA2_update(data, bytes);
29
59.9k
        bytes_left -= bytes;
30
59.9k
        data += bytes;
31
59.9k
    }
32
33
61.2k
    if (next()) {
34
0
        next()->write(buf, len);
35
0
    }
36
61.2k
}
37
38
void
39
Pl_SHA2::finish()
40
59.5k
{
41
59.5k
    if (next()) {
42
0
        next()->finish();
43
0
    }
44
59.5k
    this->crypto->SHA2_finalize();
45
59.5k
    this->in_progress = false;
46
59.5k
}
47
48
void
49
Pl_SHA2::resetBits(int bits)
50
59.5k
{
51
59.5k
    if (this->in_progress) {
52
0
        throw std::logic_error("bit reset requested for in-progress SHA2 Pipeline");
53
0
    }
54
59.5k
    this->crypto = QPDFCryptoProvider::getImpl();
55
59.5k
    this->crypto->SHA2_init(bits);
56
59.5k
}
57
58
std::string
59
Pl_SHA2::getRawDigest()
60
59.5k
{
61
59.5k
    if (this->in_progress) {
62
0
        throw std::logic_error("digest requested for in-progress SHA2 Pipeline");
63
0
    }
64
59.5k
    return this->crypto->SHA2_digest();
65
59.5k
}
66
67
std::string
68
Pl_SHA2::getHexDigest()
69
0
{
70
0
    if (this->in_progress) {
71
0
        throw std::logic_error("digest requested for in-progress SHA2 Pipeline");
72
0
    }
73
0
    return QUtil::hex_encode(getRawDigest());
74
0
}