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