Coverage Report

Created: 2025-11-11 07:02

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