2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/Pl_Buffer.hh>
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2018-08-16 15:52:59 +00:00
|
|
|
#include <algorithm>
|
2023-05-20 11:22:32 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2023-05-20 18:13:09 +00:00
|
|
|
#include <stdexcept>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
Pl_Buffer::Pl_Buffer(char const* identifier, Pipeline* next) :
|
|
|
|
Pipeline(identifier, next),
|
|
|
|
m(new Members())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-06-01 13:47:36 +00:00
|
|
|
Pl_Buffer::~Pl_Buffer() // NOLINT (modernize-use-equals-default)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-24 15:28:17 +00:00
|
|
|
// Must be explicit and not inline -- see QPDF_DLL_CLASS in README-maintainer
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-05-03 21:43:07 +00:00
|
|
|
Pl_Buffer::write(unsigned char const* buf, size_t len)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-11-16 15:06:19 +00:00
|
|
|
m->data.append(reinterpret_cast<char const*>(buf), len);
|
2023-05-21 13:42:34 +00:00
|
|
|
m->ready = false;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (getNext(true)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
getNext()->write(buf, len);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_Buffer::finish()
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->ready = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (getNext(true)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
getNext()->finish();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Buffer*
|
|
|
|
Pl_Buffer::getBuffer()
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->ready) {
|
2022-02-08 14:18:08 +00:00
|
|
|
throw std::logic_error("Pl_Buffer::getBuffer() called when not ready");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2023-11-17 18:06:57 +00:00
|
|
|
auto* b = new Buffer(std::move(m->data));
|
2023-05-21 13:42:34 +00:00
|
|
|
m->data.clear();
|
2008-04-29 12:55:25 +00:00
|
|
|
return b;
|
|
|
|
}
|
2021-12-17 12:33:42 +00:00
|
|
|
|
2023-11-16 16:26:04 +00:00
|
|
|
std::string
|
|
|
|
Pl_Buffer::getString()
|
|
|
|
{
|
|
|
|
if (!m->ready) {
|
|
|
|
throw std::logic_error("Pl_Buffer::getString() called when not ready");
|
|
|
|
}
|
|
|
|
auto s = std::move(m->data);
|
|
|
|
m->data.clear();
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2022-04-09 18:35:56 +00:00
|
|
|
std::shared_ptr<Buffer>
|
2022-02-06 17:30:18 +00:00
|
|
|
Pl_Buffer::getBufferSharedPointer()
|
|
|
|
{
|
2022-04-09 18:35:56 +00:00
|
|
|
return std::shared_ptr<Buffer>(getBuffer());
|
2022-02-06 17:30:18 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 12:33:42 +00:00
|
|
|
void
|
2022-04-02 21:14:10 +00:00
|
|
|
Pl_Buffer::getMallocBuffer(unsigned char** buf, size_t* len)
|
2021-12-17 12:33:42 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
if (!m->ready) {
|
2023-05-21 17:35:09 +00:00
|
|
|
throw std::logic_error("Pl_Buffer::getMallocBuffer() called when not ready");
|
2021-12-17 12:33:42 +00:00
|
|
|
}
|
2023-08-22 20:44:58 +00:00
|
|
|
auto size = m->data.size();
|
2022-09-23 11:46:54 +00:00
|
|
|
*len = size;
|
|
|
|
if (size > 0) {
|
|
|
|
*buf = reinterpret_cast<unsigned char*>(malloc(size));
|
2023-05-21 13:42:34 +00:00
|
|
|
memcpy(*buf, m->data.data(), size);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2021-12-17 12:33:42 +00:00
|
|
|
*buf = nullptr;
|
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
m->data.clear();
|
2021-12-17 12:33:42 +00:00
|
|
|
}
|