Coverage Report

Created: 2026-03-12 06:57

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