2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/Buffer.hh>
|
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
#include <cstring>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
Buffer::Members::Members(size_t size, unsigned char* buf, bool own_memory) :
|
|
|
|
own_memory(own_memory),
|
|
|
|
size(size),
|
2022-07-26 11:37:50 +00:00
|
|
|
buf(nullptr)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (own_memory) {
|
2022-07-26 11:37:50 +00:00
|
|
|
this->buf = (size ? new unsigned char[size] : nullptr);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
this->buf = buf;
|
2019-06-22 01:32:47 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
Buffer::Members::~Members()
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->own_memory) {
|
|
|
|
delete[] this->buf;
|
2019-06-22 01:32:47 +00:00
|
|
|
}
|
2010-10-01 10:20:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
Buffer::Buffer() :
|
2022-07-26 11:37:50 +00:00
|
|
|
m(new Members(0, nullptr, true))
|
2010-10-01 10:20:38 +00:00
|
|
|
{
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
Buffer::Buffer(size_t size) :
|
2022-07-26 11:37:50 +00:00
|
|
|
m(new Members(size, nullptr, true))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
Buffer::Buffer(unsigned char* buf, size_t size) :
|
|
|
|
m(new Members(size, buf, false))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
Buffer::Buffer(Buffer const& rhs)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
copy(rhs);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
Buffer&
|
|
|
|
Buffer::operator=(Buffer const& rhs)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
copy(rhs);
|
|
|
|
return *this;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-09-04 21:48:25 +00:00
|
|
|
Buffer::Buffer(Buffer&& rhs) noexcept :
|
|
|
|
m(std::move(rhs.m))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Buffer&
|
|
|
|
Buffer::operator=(Buffer&& rhs) noexcept
|
|
|
|
{
|
|
|
|
std::swap(this->m, rhs.m);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
Buffer::copy(Buffer const& rhs)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this != &rhs) {
|
2022-07-26 11:37:50 +00:00
|
|
|
this->m =
|
2022-09-04 21:48:25 +00:00
|
|
|
std::unique_ptr<Members>(new Members(rhs.m->size, nullptr, true));
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->m->size) {
|
2022-02-08 14:18:08 +00:00
|
|
|
memcpy(this->m->buf, rhs.m->buf, this->m->size);
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-20 15:20:57 +00:00
|
|
|
size_t
|
2008-04-29 12:55:25 +00:00
|
|
|
Buffer::getSize() const
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
return this->m->size;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char const*
|
|
|
|
Buffer::getBuffer() const
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
return this->m->buf;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char*
|
|
|
|
Buffer::getBuffer()
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
return this->m->buf;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|