/src/qpdf/libqpdf/Pl_MD5.cc
Line | Count | Source (jump to first uncovered line) |
1 | | #include <qpdf/Pl_MD5.hh> |
2 | | |
3 | | #include <stdexcept> |
4 | | |
5 | | Pl_MD5::Pl_MD5(char const* identifier, Pipeline* next) : |
6 | 30.9k | Pipeline(identifier, next) |
7 | 30.9k | { |
8 | 30.9k | if (!next) { |
9 | 0 | throw std::logic_error("Attempt to create Pl_MD5 with nullptr as next"); |
10 | 0 | } |
11 | 30.9k | } |
12 | | |
13 | | void |
14 | | Pl_MD5::write(unsigned char const* buf, size_t len) |
15 | 12.5M | { |
16 | 12.5M | if (enabled) { |
17 | 12.4M | if (!in_progress) { |
18 | 30.9k | md5.reset(); |
19 | 30.9k | in_progress = true; |
20 | 30.9k | } |
21 | | |
22 | | // Write in chunks in case len is too big to fit in an int. Assume int is at least 32 bits. |
23 | 12.4M | static size_t const max_bytes = 1 << 30; |
24 | 12.4M | size_t bytes_left = len; |
25 | 12.4M | unsigned char const* data = buf; |
26 | 24.9M | while (bytes_left > 0) { |
27 | 12.4M | size_t bytes = (bytes_left >= max_bytes ? max_bytes : bytes_left); |
28 | 12.4M | md5.encodeDataIncrementally(reinterpret_cast<char const*>(data), bytes); |
29 | 12.4M | bytes_left -= bytes; |
30 | 12.4M | data += bytes; |
31 | 12.4M | } |
32 | 12.4M | } |
33 | | |
34 | 12.5M | next()->write(buf, len); |
35 | 12.5M | } |
36 | | |
37 | | void |
38 | | Pl_MD5::finish() |
39 | 147k | { |
40 | 147k | next()->finish(); |
41 | 147k | if (!persist_across_finish) { |
42 | 0 | in_progress = false; |
43 | 0 | } |
44 | 147k | } |
45 | | |
46 | | void |
47 | | Pl_MD5::enable(bool is_enabled) |
48 | 29.8k | { |
49 | 29.8k | enabled = is_enabled; |
50 | 29.8k | } |
51 | | |
52 | | void |
53 | | Pl_MD5::persistAcrossFinish(bool persist) |
54 | 30.9k | { |
55 | 30.9k | persist_across_finish = persist; |
56 | 30.9k | } |
57 | | |
58 | | std::string |
59 | | Pl_MD5::getHexDigest() |
60 | 29.8k | { |
61 | 29.8k | if (!enabled) { |
62 | 0 | throw std::logic_error("digest requested for a disabled MD5 Pipeline"); |
63 | 0 | } |
64 | 29.8k | in_progress = false; |
65 | 29.8k | return md5.unparse(); |
66 | 29.8k | } |