2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-11-16 17:45:09 +00:00
qpdf/libqpdf/Pl_Buffer.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

78 lines
1.5 KiB
C++
Raw Normal View History

#include <qpdf/Pl_Buffer.hh>
2018-08-16 15:52:59 +00:00
#include <algorithm>
2023-05-20 11:22:32 +00:00
#include <cstdlib>
#include <cstring>
#include <stdexcept>
Pl_Buffer::Pl_Buffer(char const* identifier, Pipeline* next) :
Pipeline(identifier, next),
m(new Members())
{
}
Pl_Buffer::~Pl_Buffer()
{
// Must be explicit and not inline -- see QPDF_DLL_CLASS in README-maintainer
}
void
Pl_Buffer::write(unsigned char const* buf, size_t len)
{
m->data.append(buf, len);
m->ready = false;
if (getNext(true)) {
getNext()->write(buf, len);
}
}
void
Pl_Buffer::finish()
{
m->ready = true;
if (getNext(true)) {
getNext()->finish();
}
}
Buffer*
Pl_Buffer::getBuffer()
{
if (!m->ready) {
throw std::logic_error("Pl_Buffer::getBuffer() called when not ready");
}
auto size = m->data.length();
2023-05-20 12:24:10 +00:00
auto* b = new Buffer(size);
if (size > 0) {
unsigned char* p = b->getBuffer();
memcpy(p, m->data.data(), size);
}
m->data.clear();
return b;
}
2021-12-17 12:33:42 +00:00
std::shared_ptr<Buffer>
Pl_Buffer::getBufferSharedPointer()
{
return std::shared_ptr<Buffer>(getBuffer());
}
2021-12-17 12:33:42 +00:00
void
Pl_Buffer::getMallocBuffer(unsigned char** buf, size_t* len)
{
if (!m->ready) {
throw std::logic_error("Pl_Buffer::getMallocBuffer() called when not ready");
}
auto size = m->data.length();
*len = size;
if (size > 0) {
*buf = reinterpret_cast<unsigned char*>(malloc(size));
memcpy(*buf, m->data.data(), size);
2021-12-17 12:33:42 +00:00
} else {
*buf = nullptr;
}
m->data.clear();
2021-12-17 12:33:42 +00:00
}