2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/Pl_Buffer.hh>
|
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>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
Pl_Buffer::Pl_Buffer(char const* identifier, Pipeline* next) :
|
|
|
|
Pipeline(identifier, next),
|
2018-03-06 16:24:30 +00:00
|
|
|
ready(true),
|
2008-04-29 12:55:25 +00:00
|
|
|
total_size(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2018-08-12 21:26:40 +00:00
|
|
|
PointerHolder<Buffer> cur_buf;
|
|
|
|
size_t cur_size = 0;
|
|
|
|
if (! this->data.empty())
|
|
|
|
{
|
|
|
|
cur_buf = this->data.back();
|
|
|
|
cur_size = cur_buf->getSize();
|
|
|
|
}
|
|
|
|
size_t left = cur_size - this->total_size;
|
|
|
|
if (left < len)
|
|
|
|
{
|
|
|
|
size_t new_size = std::max(this->total_size + len, 2 * cur_size);
|
|
|
|
Buffer* b = new Buffer(new_size);
|
|
|
|
if (cur_buf.getPointer())
|
|
|
|
{
|
|
|
|
memcpy(b->getBuffer(), cur_buf->getBuffer(), this->total_size);
|
|
|
|
}
|
|
|
|
this->data.clear();
|
|
|
|
cur_buf = b;
|
|
|
|
this->data.push_back(cur_buf);
|
|
|
|
}
|
|
|
|
if (len)
|
|
|
|
{
|
|
|
|
memcpy(cur_buf->getBuffer() + this->total_size, buf, len);
|
|
|
|
this->total_size += len;
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
this->ready = false;
|
|
|
|
|
|
|
|
if (getNext(true))
|
|
|
|
{
|
|
|
|
getNext()->write(buf, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_Buffer::finish()
|
|
|
|
{
|
|
|
|
this->ready = true;
|
|
|
|
if (getNext(true))
|
|
|
|
{
|
|
|
|
getNext()->finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Buffer*
|
|
|
|
Pl_Buffer::getBuffer()
|
|
|
|
{
|
|
|
|
if (! this->ready)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
Buffer* b = new Buffer(this->total_size);
|
|
|
|
unsigned char* p = b->getBuffer();
|
2018-08-12 21:26:40 +00:00
|
|
|
if (! this->data.empty())
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2018-08-12 21:26:40 +00:00
|
|
|
PointerHolder<Buffer> bp = this->data.back();
|
|
|
|
this->data.clear();
|
|
|
|
memcpy(p, bp->getBuffer(), this->total_size);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2018-08-12 21:26:40 +00:00
|
|
|
this->total_size = 0;
|
2008-04-29 12:55:25 +00:00
|
|
|
this->ready = false;
|
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|