2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-06-12 23:22:21 +00:00
qpdf/libqpdf/Pl_Buffer.cc
Zoe Clifford cbae2f916b Remove use of non-standard char_traits<unsigned char> from Pl_Buffer
`basic_string<unsigned char>` implies use of
`char_traits<unsigned char>`.

This char_traits specialization is not standard C++, and will be
removed from LibC++ as of LLVM 18. To ensure continued LibC++
compatibility it needs to be removed.

There are two possible replacements here: `std::string` (e.g.
`std::basic_string<char>`), or `std::vector<unsigned char>`.

I have opted for vector since this code is dealing with a binary
buffer; though probably either way is fine (why does C++ even have
strings anyway??).

https://github.com/qpdf/qpdf/issues/1024
2023-08-22 13:44:58 -07:00

78 lines
1.6 KiB
C++

#include <qpdf/Pl_Buffer.hh>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <stdexcept>
Pl_Buffer::Pl_Buffer(char const* identifier, Pipeline* next) :
Pipeline(identifier, next),
m(new Members())
{
}
Pl_Buffer::~Pl_Buffer() // NOLINT (modernize-use-equals-default)
{
// Must be explicit and not inline -- see QPDF_DLL_CLASS in README-maintainer
}
void
Pl_Buffer::write(unsigned char const* buf, size_t len)
{
m->data.insert(m->data.end(), buf, buf + len);
m->ready = false;
if (getNext(true)) {
getNext()->write(buf, len);
}
}
void
Pl_Buffer::finish()
{
m->ready = true;
if (getNext(true)) {
getNext()->finish();
}
}
Buffer*
Pl_Buffer::getBuffer()
{
if (!m->ready) {
throw std::logic_error("Pl_Buffer::getBuffer() called when not ready");
}
auto size = m->data.size();
auto* b = new Buffer(size);
if (size > 0) {
unsigned char* p = b->getBuffer();
memcpy(p, m->data.data(), size);
}
m->data.clear();
return b;
}
std::shared_ptr<Buffer>
Pl_Buffer::getBufferSharedPointer()
{
return std::shared_ptr<Buffer>(getBuffer());
}
void
Pl_Buffer::getMallocBuffer(unsigned char** buf, size_t* len)
{
if (!m->ready) {
throw std::logic_error("Pl_Buffer::getMallocBuffer() called when not ready");
}
auto size = m->data.size();
*len = size;
if (size > 0) {
*buf = reinterpret_cast<unsigned char*>(malloc(size));
memcpy(*buf, m->data.data(), size);
} else {
*buf = nullptr;
}
m->data.clear();
}