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