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())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
Pl_Buffer::~Pl_Buffer()
|
|
|
|
{
|
2022-04-15 23:44:07 +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
|
|
|
{
|
2022-09-23 11:46:54 +00:00
|
|
|
this->m->data.append(buf, len);
|
2019-06-22 01:32:47 +00:00
|
|
|
this->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()
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
this->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()
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!this->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
|
|
|
}
|
|
|
|
|
2022-09-23 11:46:54 +00:00
|
|
|
auto size = this->m->data.length();
|
2023-05-20 12:24:10 +00:00
|
|
|
auto* b = new Buffer(size);
|
2022-09-23 11:46:54 +00:00
|
|
|
if (size > 0) {
|
2019-06-22 01:32:47 +00:00
|
|
|
unsigned char* p = b->getBuffer();
|
2022-09-23 11:46:54 +00:00
|
|
|
memcpy(p, this->m->data.data(), size);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-09-23 11:46:54 +00:00
|
|
|
this->m->data.clear();
|
2008-04-29 12:55:25 +00:00
|
|
|
return b;
|
|
|
|
}
|
2021-12-17 12:33:42 +00:00
|
|
|
|
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
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!this->m->ready) {
|
2022-02-08 14:18:08 +00:00
|
|
|
throw std::logic_error(
|
2021-12-17 12:33:42 +00:00
|
|
|
"Pl_Buffer::getMallocBuffer() called when not ready");
|
|
|
|
}
|
2022-09-23 11:46:54 +00:00
|
|
|
auto size = this->m->data.length();
|
|
|
|
*len = size;
|
|
|
|
if (size > 0) {
|
|
|
|
*buf = reinterpret_cast<unsigned char*>(malloc(size));
|
|
|
|
memcpy(*buf, this->m->data.data(), size);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2021-12-17 12:33:42 +00:00
|
|
|
*buf = nullptr;
|
|
|
|
}
|
2022-09-23 11:46:54 +00:00
|
|
|
this->m->data.clear();
|
2021-12-17 12:33:42 +00:00
|
|
|
}
|