Coverage Report

Created: 2024-09-08 06:05

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