/src/qpdf/libqpdf/Pl_Buffer.cc
Line | Count | Source |
1 | | #include <qpdf/Pl_Buffer.hh> |
2 | | |
3 | | #include <qpdf/Util.hh> |
4 | | |
5 | | #include <algorithm> |
6 | | #include <cstdlib> |
7 | | #include <cstring> |
8 | | #include <stdexcept> |
9 | | |
10 | | using namespace qpdf; |
11 | | |
12 | | class Pl_Buffer::Members |
13 | | { |
14 | | public: |
15 | 2.61M | Members() = default; |
16 | | Members(Members const&) = delete; |
17 | | |
18 | | bool ready{true}; |
19 | | std::string data; |
20 | | }; |
21 | | |
22 | | Pl_Buffer::Pl_Buffer(char const* identifier, Pipeline* next) : |
23 | 2.61M | Pipeline(identifier, next), |
24 | 2.61M | m(std::make_unique<Members>()) |
25 | 2.61M | { |
26 | 2.61M | } |
27 | | |
28 | | // Must be explicit and not inline -- see QPDF_DLL_CLASS in README-maintainer |
29 | 2.61M | Pl_Buffer::~Pl_Buffer() = default; |
30 | | |
31 | | void |
32 | | Pl_Buffer::write(unsigned char const* buf, size_t len) |
33 | 744M | { |
34 | 744M | if (!len) { |
35 | 45 | return; |
36 | 45 | } |
37 | 744M | m->data.append(reinterpret_cast<char const*>(buf), len); |
38 | 744M | m->ready = false; |
39 | | |
40 | 744M | if (next()) { |
41 | 0 | next()->write(buf, len); |
42 | 0 | } |
43 | 744M | } |
44 | | |
45 | | void |
46 | | Pl_Buffer::finish() |
47 | 2.61M | { |
48 | 2.61M | m->ready = true; |
49 | 2.61M | if (next()) { |
50 | 0 | next()->finish(); |
51 | 0 | } |
52 | 2.61M | } |
53 | | |
54 | | Buffer* |
55 | | Pl_Buffer::getBuffer() |
56 | 0 | { |
57 | 0 | util::assertion(m->ready, "Pl_Buffer::getBuffer() called when not ready"); |
58 | 0 | auto* b = new Buffer(std::move(m->data)); |
59 | 0 | m->data.clear(); |
60 | 0 | return b; |
61 | 0 | } |
62 | | |
63 | | std::string |
64 | | Pl_Buffer::getString() |
65 | 2.61M | { |
66 | 2.61M | util::assertion(m->ready, "Pl_Buffer::getString() called when not ready"); |
67 | 2.61M | auto s = std::move(m->data); |
68 | 2.61M | m->data.clear(); |
69 | 2.61M | return s; |
70 | 2.61M | } |
71 | | |
72 | | std::shared_ptr<Buffer> |
73 | | Pl_Buffer::getBufferSharedPointer() |
74 | 0 | { |
75 | 0 | return std::shared_ptr<Buffer>(getBuffer()); |
76 | 0 | } |
77 | | |
78 | | void |
79 | | Pl_Buffer::getMallocBuffer(unsigned char** buf, size_t* len) |
80 | 0 | { |
81 | 0 | util::assertion(m->ready, "Pl_Buffer::getMallocBuffer() called when not ready"); |
82 | 0 | auto size = m->data.size(); |
83 | 0 | *len = size; |
84 | 0 | if (size > 0) { |
85 | 0 | *buf = reinterpret_cast<unsigned char*>(malloc(size)); |
86 | 0 | memcpy(*buf, m->data.data(), size); |
87 | 0 | } else { |
88 | 0 | *buf = nullptr; |
89 | 0 | } |
90 | 0 | m->data.clear(); |
91 | 0 | } |