2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-11-10 07:11:07 +00:00
qpdf/libqpdf/Pl_Buffer.cc
Jay Berkenbilt 5d4cad9c02 ABI change: fix use of off_t, size_t, and integer types
Significantly improve the code's use of off_t for file offsets, size_t
for memory sizes, and integer types in cases where there has to be
compatibility with external interfaces.  Rework sections of the code
that would have prevented qpdf from working on files larger than 2 (or
maybe 4) GB in size.
2012-06-20 15:20:26 -04:00

67 lines
1.1 KiB
C++

#include <qpdf/Pl_Buffer.hh>
#include <stdexcept>
#include <assert.h>
#include <string.h>
Pl_Buffer::Pl_Buffer(char const* identifier, Pipeline* next) :
Pipeline(identifier, next),
ready(false),
total_size(0)
{
}
Pl_Buffer::~Pl_Buffer()
{
}
void
Pl_Buffer::write(unsigned char* buf, size_t len)
{
Buffer* b = new Buffer(len);
memcpy(b->getBuffer(), buf, len);
this->data.push_back(b);
this->ready = false;
this->total_size += len;
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)
{
throw std::logic_error("Pl_Buffer::getBuffer() called when not ready");
}
Buffer* b = new Buffer(this->total_size);
unsigned char* p = b->getBuffer();
while (! this->data.empty())
{
PointerHolder<Buffer> bp = this->data.front();
this->data.pop_front();
size_t bytes = bp->getSize();
memcpy(p, bp->getBuffer(), bytes);
p += bytes;
this->total_size -= bytes;
}
assert(this->total_size == 0);
this->ready = false;
return b;
}