/src/qpdf/libqpdf/Pl_RC4.cc
Line | Count | Source |
1 | | #include <qpdf/Pl_RC4.hh> |
2 | | |
3 | | #include <qpdf/QUtil.hh> |
4 | | #include <qpdf/Util.hh> |
5 | | |
6 | | using namespace qpdf; |
7 | | |
8 | | Pl_RC4::Pl_RC4(char const* identifier, Pipeline* next, std::string key, size_t out_bufsize) : |
9 | 3.09k | Pipeline(identifier, next), |
10 | 3.09k | out_bufsize(out_bufsize), |
11 | 3.09k | rc4(reinterpret_cast<unsigned char const*>(key.data()), static_cast<int>(key.size())) |
12 | 3.09k | { |
13 | 3.09k | util::assertion(next, "Attempt to create Pl_RC4 with nullptr as next"); |
14 | 3.09k | this->outbuf = QUtil::make_shared_array<unsigned char>(out_bufsize); |
15 | 3.09k | } |
16 | | |
17 | | void |
18 | | Pl_RC4::write(unsigned char const* data, size_t len) |
19 | 3.09k | { |
20 | 3.09k | util::assertion(outbuf.get(), "Pl_RC4: write() called after finish() called"); |
21 | | |
22 | 3.09k | size_t bytes_left = len; |
23 | 3.09k | unsigned char const* p = data; |
24 | | |
25 | 6.24k | while (bytes_left > 0) { |
26 | 3.14k | size_t bytes = (bytes_left < this->out_bufsize ? bytes_left : out_bufsize); |
27 | 3.14k | bytes_left -= bytes; |
28 | 3.14k | rc4.process(p, bytes, outbuf.get()); |
29 | 3.14k | p += bytes; |
30 | 3.14k | next()->write(outbuf.get(), bytes); |
31 | 3.14k | } |
32 | 3.09k | } |
33 | | |
34 | | void |
35 | | Pl_RC4::finish() |
36 | 3.09k | { |
37 | 3.09k | outbuf = nullptr; |
38 | 3.09k | next()->finish(); |
39 | 3.09k | } |