2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/Pl_Buffer.hh>
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2009-09-26 18:36:04 +00:00
|
|
|
#include <stdexcept>
|
2018-08-16 15:52:59 +00:00
|
|
|
#include <algorithm>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <assert.h>
|
2008-05-04 16:02:53 +00:00
|
|
|
#include <string.h>
|
2021-12-17 12:33:42 +00:00
|
|
|
#include <stdlib.h>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
Pl_Buffer::Members::Members() :
|
2018-03-06 16:24:30 +00:00
|
|
|
ready(true),
|
2008-04-29 12:55:25 +00:00
|
|
|
total_size(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
Pl_Buffer::Members::~Members()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-06-20 15:20:57 +00:00
|
|
|
Pl_Buffer::write(unsigned char* buf, size_t len)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2022-02-04 15:10:19 +00:00
|
|
|
if (this->m->data.get() == 0)
|
2018-08-12 21:26:40 +00:00
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
this->m->data = new Buffer(len);
|
2018-08-12 21:26:40 +00:00
|
|
|
}
|
2019-06-22 01:32:47 +00:00
|
|
|
size_t cur_size = this->m->data->getSize();
|
|
|
|
size_t left = cur_size - this->m->total_size;
|
2018-08-12 21:26:40 +00:00
|
|
|
if (left < len)
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
size_t new_size = std::max(this->m->total_size + len, 2 * cur_size);
|
|
|
|
PointerHolder<Buffer> b = new Buffer(new_size);
|
|
|
|
memcpy(b->getBuffer(), this->m->data->getBuffer(), this->m->total_size);
|
|
|
|
this->m->data = b;
|
2018-08-12 21:26:40 +00:00
|
|
|
}
|
|
|
|
if (len)
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
memcpy(this->m->data->getBuffer() + this->m->total_size, buf, len);
|
|
|
|
this->m->total_size += len;
|
2018-08-12 21:26:40 +00:00
|
|
|
}
|
2019-06-22 01:32:47 +00:00
|
|
|
this->m->ready = false;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
if (getNext(true))
|
|
|
|
{
|
|
|
|
getNext()->write(buf, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_Buffer::finish()
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
this->m->ready = true;
|
2008-04-29 12:55:25 +00:00
|
|
|
if (getNext(true))
|
|
|
|
{
|
|
|
|
getNext()->finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Buffer*
|
|
|
|
Pl_Buffer::getBuffer()
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
if (! this->m->ready)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2009-09-26 18:36:04 +00:00
|
|
|
throw std::logic_error("Pl_Buffer::getBuffer() called when not ready");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
Buffer* b = new Buffer(this->m->total_size);
|
|
|
|
if (this->m->total_size > 0)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
unsigned char* p = b->getBuffer();
|
|
|
|
memcpy(p, this->m->data->getBuffer(), this->m->total_size);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2019-06-22 01:32:47 +00:00
|
|
|
this->m = new Members();
|
2008-04-29 12:55:25 +00:00
|
|
|
return b;
|
|
|
|
}
|
2021-12-17 12:33:42 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Pl_Buffer::getMallocBuffer(unsigned char **buf, size_t* len)
|
|
|
|
{
|
|
|
|
if (! this->m->ready)
|
|
|
|
{
|
|
|
|
throw std::logic_error(
|
|
|
|
"Pl_Buffer::getMallocBuffer() called when not ready");
|
|
|
|
}
|
|
|
|
|
|
|
|
*len = this->m->total_size;
|
|
|
|
if (this->m->total_size > 0)
|
|
|
|
{
|
|
|
|
*buf = reinterpret_cast<unsigned char*>(malloc(this->m->total_size));
|
|
|
|
memcpy(*buf, this->m->data->getBuffer(), this->m->total_size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*buf = nullptr;
|
|
|
|
}
|
|
|
|
this->m = new Members();
|
|
|
|
}
|