Coverage Report

Created: 2026-01-09 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/libqpdf/SHA2_native.cc
Line
Count
Source
1
#include <qpdf/SHA2_native.hh>
2
3
#include <qpdf/QUtil.hh>
4
#include <cstdio>
5
#include <stdexcept>
6
7
SHA2_native::SHA2_native(int bits) :
8
3.01M
    bits(bits)
9
3.01M
{
10
3.01M
    switch (bits) {
11
1.03M
    case 256:
12
1.03M
        sph_sha256_init(&ctx256);
13
1.03M
        break;
14
991k
    case 384:
15
991k
        sph_sha384_init(&ctx384);
16
991k
        break;
17
990k
    case 512:
18
990k
        sph_sha512_init(&ctx512);
19
990k
        break;
20
0
    default:
21
0
        badBits();
22
0
        break;
23
3.01M
    }
24
3.01M
}
25
26
void
27
SHA2_native::badBits()
28
0
{
29
0
    throw std::logic_error("SHA2_native has bits != 256, 384, or 512");
30
0
}
31
32
void
33
SHA2_native::update(unsigned char const* buf, size_t len)
34
3.07M
{
35
3.07M
    switch (bits) {
36
1.09M
    case 256:
37
1.09M
        sph_sha256(&ctx256, buf, len);
38
1.09M
        break;
39
991k
    case 384:
40
991k
        sph_sha384(&ctx384, buf, len);
41
991k
        break;
42
990k
    case 512:
43
990k
        sph_sha512(&ctx512, buf, len);
44
990k
        break;
45
0
    default:
46
0
        badBits();
47
0
        break;
48
3.07M
    }
49
3.07M
}
50
51
void
52
SHA2_native::finalize()
53
3.01M
{
54
3.01M
    switch (bits) {
55
1.03M
    case 256:
56
1.03M
        sph_sha256_close(&ctx256, sha256sum);
57
1.03M
        break;
58
991k
    case 384:
59
991k
        sph_sha384_close(&ctx384, sha384sum);
60
991k
        break;
61
990k
    case 512:
62
990k
        sph_sha512_close(&ctx512, sha512sum);
63
990k
        break;
64
0
    default:
65
0
        badBits();
66
0
        break;
67
3.01M
    }
68
3.01M
}
69
70
std::string
71
SHA2_native::getRawDigest()
72
3.01M
{
73
3.01M
    std::string result;
74
3.01M
    switch (bits) {
75
1.03M
    case 256:
76
1.03M
        result = std::string(reinterpret_cast<char*>(sha256sum), sizeof(sha256sum));
77
1.03M
        break;
78
991k
    case 384:
79
991k
        result = std::string(reinterpret_cast<char*>(sha384sum), sizeof(sha384sum));
80
991k
        break;
81
990k
    case 512:
82
990k
        result = std::string(reinterpret_cast<char*>(sha512sum), sizeof(sha512sum));
83
990k
        break;
84
0
    default:
85
0
        badBits();
86
0
        break;
87
3.01M
    }
88
3.01M
    return result;
89
3.01M
}