From 44a7aff56f13a0afb16e6b1abcf4b62e49f35863 Mon Sep 17 00:00:00 2001 From: m-holger Date: Fri, 23 Sep 2022 12:46:54 +0100 Subject: Refactor Pl_Buffer Base implementation of the buffer on std::basic_string. --- libqpdf/Pl_Buffer.cc | 43 ++++++++++++------------------------------- 1 file changed, 12 insertions(+), 31 deletions(-) (limited to 'libqpdf') 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 #include -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(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(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(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(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(malloc(size)); + memcpy(*buf, this->m->data.data(), size); } else { *buf = nullptr; } - this->m = std::shared_ptr(new Members()); + this->m->data.clear(); } -- cgit v1.2.3-54-g00ecf