2023-05-25 13:10:47 +00:00
|
|
|
#include <qpdf/assert_test.h>
|
|
|
|
|
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
|
|
|
|
2023-05-25 13:10:47 +00:00
|
|
|
bool test_mode = false;
|
|
|
|
|
|
|
|
// During CI the Buffer copy constructor and copy assignment operator throw an assertion error to
|
|
|
|
// detect their accidental use. Call setTestMode to surpress the assertion errors for testing of
|
|
|
|
// copy construction and assignment.
|
|
|
|
void
|
|
|
|
Buffer::setTestMode() noexcept
|
|
|
|
{
|
|
|
|
test_mode = true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
if (own_memory) {
|
2022-07-26 11:37:50 +00:00
|
|
|
this->buf = (size ? new unsigned char[size] : nullptr);
|
2019-06-22 01:32:47 +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
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
if (this->own_memory) {
|
2022-02-08 14:18:08 +00:00
|
|
|
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
|
|
|
{
|
2023-05-25 13:10:47 +00:00
|
|
|
assert(test_mode);
|
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
|
|
|
{
|
2023-05-25 13:10:47 +00:00
|
|
|
assert(test_mode);
|
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(m, rhs.m);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
Buffer::copy(Buffer const& rhs)
|
|
|
|
{
|
|
|
|
if (this != &rhs) {
|
2022-09-04 21:48:25 +00:00
|
|
|
m = std::unique_ptr<Members>(new Members(rhs.m->size, nullptr, true));
|
2022-02-08 14:18:08 +00:00
|
|
|
if (m->size) {
|
|
|
|
memcpy(m->buf, rhs.m->buf, 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 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 m->buf;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char*
|
|
|
|
Buffer::getBuffer()
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
return m->buf;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2023-05-25 13:10:47 +00:00
|
|
|
|
|
|
|
Buffer
|
|
|
|
Buffer::copy() const
|
|
|
|
{
|
|
|
|
auto result = Buffer(m->size);
|
|
|
|
if (m->size) {
|
|
|
|
memcpy(result.m->buf, m->buf, m->size);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|