aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorm-holger <m-holger@kubitscheck.org>2022-09-23 13:46:54 +0200
committerJay Berkenbilt <jberkenbilt@users.noreply.github.com>2022-09-23 21:09:13 +0200
commit44a7aff56f13a0afb16e6b1abcf4b62e49f35863 (patch)
treec2c211cc6852e8a02f898828aca689478ef00ce3
parentae800361fefb6d30d054f5d90af310c85765b044 (diff)
downloadqpdf-44a7aff56f13a0afb16e6b1abcf4b62e49f35863.tar.zst
Refactor Pl_Buffer
Base implementation of the buffer on std::basic_string<unsigned char>.
-rw-r--r--include/qpdf/Pl_Buffer.hh8
-rw-r--r--libqpdf/Pl_Buffer.cc43
2 files changed, 15 insertions, 36 deletions
diff --git a/include/qpdf/Pl_Buffer.hh b/include/qpdf/Pl_Buffer.hh
index 7784286d..ed9056ac 100644
--- a/include/qpdf/Pl_Buffer.hh
+++ b/include/qpdf/Pl_Buffer.hh
@@ -35,7 +35,6 @@
#include <qpdf/Buffer.hh>
#include <qpdf/Pipeline.hh>
-#include <qpdf/PointerHolder.hh>
#include <memory>
@@ -80,12 +79,11 @@ class QPDF_DLL_CLASS Pl_Buffer: public Pipeline
~Members() = default;
private:
- Members();
+ Members() = default;
Members(Members const&) = delete;
- bool ready;
- std::shared_ptr<Buffer> data;
- size_t total_size;
+ bool ready{true};
+ std::basic_string<unsigned char> data;
};
std::shared_ptr<Members> m;
diff --git a/libqpdf/Pl_Buffer.cc b/libqpdf/Pl_Buffer.cc
index 791656d8..ca29ef60 100644
--- a/libqpdf/Pl_Buffer.cc
+++ b/libqpdf/Pl_Buffer.cc
@@ -5,12 +5,6 @@
#include <stdlib.h>
#include <string.h>
-Pl_Buffer::Members::Members() :
- ready(true),
- total_size(0)
-{
-}
-
Pl_Buffer::Pl_Buffer(char const* identifier, Pipeline* next) :
Pipeline(identifier, next),
m(new Members())
@@ -26,21 +20,7 @@ Pl_Buffer::~Pl_Buffer()
void
Pl_Buffer::write(unsigned char const* buf, size_t len)
{
- if (this->m->data == nullptr) {
- this->m->data = std::make_shared<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 = std::make_shared<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->data.append(buf, len);
this->m->ready = false;
if (getNext(true)) {
@@ -64,12 +44,13 @@ Pl_Buffer::getBuffer()
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) {
+ auto size = this->m->data.length();
+ Buffer* b = new Buffer(size);
+ if (size > 0) {
unsigned char* p = b->getBuffer();
- memcpy(p, this->m->data->getBuffer(), this->m->total_size);
+ memcpy(p, this->m->data.data(), size);
}
- this->m = std::shared_ptr<Members>(new Members());
+ this->m->data.clear();
return b;
}
@@ -86,13 +67,13 @@ Pl_Buffer::getMallocBuffer(unsigned char** buf, size_t* len)
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);
+ auto size = this->m->data.length();
+ *len = size;
+ if (size > 0) {
+ *buf = reinterpret_cast<unsigned char*>(malloc(size));
+ memcpy(*buf, this->m->data.data(), size);
} else {
*buf = nullptr;
}
- this->m = std::shared_ptr<Members>(new Members());
+ this->m->data.clear();
}