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