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