Coverage Report

Created: 2025-07-01 06:10

/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
0
    Pipeline("sha2", next)
9
0
{
10
0
    if (bits) {
11
0
        resetBits(bits);
12
0
    }
13
0
}
14
15
void
16
Pl_SHA2::write(unsigned char const* buf, size_t len)
17
0
{
18
0
    if (!this->in_progress) {
19
0
        this->in_progress = true;
20
0
    }
21
22
    // Write in chunks in case len is too big to fit in an int. Assume int is at least 32 bits.
23
0
    static size_t const max_bytes = 1 << 30;
24
0
    size_t bytes_left = len;
25
0
    unsigned char const* data = buf;
26
0
    while (bytes_left > 0) {
27
0
        size_t bytes = (bytes_left >= max_bytes ? max_bytes : bytes_left);
28
0
        this->crypto->SHA2_update(data, bytes);
29
0
        bytes_left -= bytes;
30
0
        data += bytes;
31
0
    }
32
33
0
    if (next()) {
34
0
        next()->write(buf, len);
35
0
    }
36
0
}
37
38
void
39
Pl_SHA2::finish()
40
0
{
41
0
    if (next()) {
42
0
        next()->finish();
43
0
    }
44
0
    this->crypto->SHA2_finalize();
45
0
    this->in_progress = false;
46
0
}
47
48
void
49
Pl_SHA2::resetBits(int bits)
50
0
{
51
0
    if (this->in_progress) {
52
0
        throw std::logic_error("bit reset requested for in-progress SHA2 Pipeline");
53
0
    }
54
0
    this->crypto = QPDFCryptoProvider::getImpl();
55
0
    this->crypto->SHA2_init(bits);
56
0
}
57
58
std::string
59
Pl_SHA2::getRawDigest()
60
0
{
61
0
    if (this->in_progress) {
62
0
        throw std::logic_error("digest requested for in-progress SHA2 Pipeline");
63
0
    }
64
0
    return this->crypto->SHA2_digest();
65
0
}
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
}