2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-22 19:08:59 +00:00
qpdf/libqpdf/Pl_Buffer.cc
Jay Berkenbilt cb769c62e5 WHITESPACE ONLY -- expand tabs in source code
This comment expands all tabs using an 8-character tab-width. You
should ignore this commit when using git blame or use git blame -w.

In the early days, I used to use tabs where possible for indentation,
since emacs did this automatically. In recent years, I have switched
to only using spaces, which means qpdf source code has been a mixture
of spaces and tabs. I have avoided cleaning this up because of not
wanting gratuitous whitespaces change to cloud the output of git
blame, but I changed my mind after discussing with users who view qpdf
source code in editors/IDEs that have other tab widths by default and
in light of the fact that I am planning to start applying automatic
code formatting soon.
2022-02-08 11:51:15 -05:00

113 lines
2.3 KiB
C++

#include <qpdf/Pl_Buffer.hh>
#include <stdexcept>
#include <algorithm>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
Pl_Buffer::Members::Members() :
ready(true),
total_size(0)
{
}
Pl_Buffer::Members::~Members()
{
}
Pl_Buffer::Pl_Buffer(char const* identifier, Pipeline* next) :
Pipeline(identifier, next),
m(new Members())
{
}
Pl_Buffer::~Pl_Buffer()
{
}
void
Pl_Buffer::write(unsigned char* buf, size_t len)
{
if (this->m->data.get() == 0)
{
this->m->data = make_pointer_holder<Buffer>(len);
}
size_t cur_size = this->m->data->getSize();
size_t left = cur_size - this->m->total_size;
if (left < len)
{
size_t new_size = std::max(this->m->total_size + len, 2 * cur_size);
auto b = make_pointer_holder<Buffer>(new_size);
memcpy(b->getBuffer(), this->m->data->getBuffer(), this->m->total_size);
this->m->data = b;
}
if (len)
{
memcpy(this->m->data->getBuffer() + this->m->total_size, buf, len);
this->m->total_size += len;
}
this->m->ready = false;
if (getNext(true))
{
getNext()->write(buf, len);
}
}
void
Pl_Buffer::finish()
{
this->m->ready = true;
if (getNext(true))
{
getNext()->finish();
}
}
Buffer*
Pl_Buffer::getBuffer()
{
if (! this->m->ready)
{
throw std::logic_error("Pl_Buffer::getBuffer() called when not ready");
}
Buffer* b = new Buffer(this->m->total_size);
if (this->m->total_size > 0)
{
unsigned char* p = b->getBuffer();
memcpy(p, this->m->data->getBuffer(), this->m->total_size);
}
this->m = PointerHolder<Members>(new Members());
return b;
}
PointerHolder<Buffer>
Pl_Buffer::getBufferSharedPointer()
{
return PointerHolder<Buffer>(getBuffer());
}
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 = PointerHolder<Members>(new Members());
}