From 3237ef70fb77ce323394de1e2793abdb5ae85384 Mon Sep 17 00:00:00 2001 From: m-holger Date: Thu, 16 Nov 2023 16:26:04 +0000 Subject: [PATCH] Add new method Pl_Buffer::getString --- include/qpdf/Pl_Buffer.hh | 4 ++++ libqpdf/Pl_Buffer.cc | 11 +++++++++++ libqpdf/QPDFAcroFormDocumentHelper.cc | 3 +-- libqpdf/QPDFObjectHandle.cc | 3 +-- libqpdf/QPDFWriter.cc | 5 +---- libqpdf/QPDF_encryption.cc | 9 +++------ 6 files changed, 21 insertions(+), 14 deletions(-) diff --git a/include/qpdf/Pl_Buffer.hh b/include/qpdf/Pl_Buffer.hh index 6cf4560a..5def8b4f 100644 --- a/include/qpdf/Pl_Buffer.hh +++ b/include/qpdf/Pl_Buffer.hh @@ -64,6 +64,10 @@ class QPDF_DLL_CLASS Pl_Buffer: public Pipeline QPDF_DLL void getMallocBuffer(unsigned char** buf, size_t* len); + // Same as getBuffer but returns the result as a string. + QPDF_DLL + std::string getString(); + private: class QPDF_DLL_PRIVATE Members { diff --git a/libqpdf/Pl_Buffer.cc b/libqpdf/Pl_Buffer.cc index 072b04d4..05d2ebdb 100644 --- a/libqpdf/Pl_Buffer.cc +++ b/libqpdf/Pl_Buffer.cc @@ -53,6 +53,17 @@ Pl_Buffer::getBuffer() return b; } +std::string +Pl_Buffer::getString() +{ + if (!m->ready) { + throw std::logic_error("Pl_Buffer::getString() called when not ready"); + } + auto s = std::move(m->data); + m->data.clear(); + return s; +} + std::shared_ptr Pl_Buffer::getBufferSharedPointer() { diff --git a/libqpdf/QPDFAcroFormDocumentHelper.cc b/libqpdf/QPDFAcroFormDocumentHelper.cc index 5a54bf75..f2daa0c7 100644 --- a/libqpdf/QPDFAcroFormDocumentHelper.cc +++ b/libqpdf/QPDFAcroFormDocumentHelper.cc @@ -584,8 +584,7 @@ QPDFAcroFormDocumentHelper::adjustDefaultAppearances( ResourceReplacer rr(dr_map, rf.getNamesByResourceType()); Pl_Buffer buf_pl("filtered DA"); da_stream.filterAsContents(&rr, &buf_pl); - auto buf = buf_pl.getBufferSharedPointer(); - std::string new_da(reinterpret_cast(buf->getBuffer()), buf->getSize()); + std::string new_da = buf_pl.getString(); obj.replaceKey("/DA", QPDFObjectHandle::newString(new_da)); } diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index d5ac137a..d543f98e 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -1708,8 +1708,7 @@ QPDFObjectHandle::pipeContentStreams( need_newline = (lc.getLastChar() != static_cast('\n')); QTC::TC("qpdf", "QPDFObjectHandle need_newline", need_newline ? 0 : 1); } - std::unique_ptr b(buf.getBuffer()); - p->write(b->getBuffer(), b->getSize()); + p->writeString(buf.getString()); p->finish(); } diff --git a/libqpdf/QPDFWriter.cc b/libqpdf/QPDFWriter.cc index db3b42e0..664ea5ff 100644 --- a/libqpdf/QPDFWriter.cc +++ b/libqpdf/QPDFWriter.cc @@ -1579,10 +1579,7 @@ QPDFWriter::unparseObject( m->cur_data_key.length()); pl.writeString(val); pl.finish(); - auto buf = bufpl.getBufferSharedPointer(); - val = QPDF_String( - std::string(reinterpret_cast(buf->getBuffer()), buf->getSize())) - .unparse(true); + val = QPDF_String(bufpl.getString()).unparse(true); } else { auto tmp_ph = QUtil::make_unique_cstr(val); char* tmp = tmp_ph.get(); diff --git a/libqpdf/QPDF_encryption.cc b/libqpdf/QPDF_encryption.cc index b5cc44ee..d405b4dd 100644 --- a/libqpdf/QPDF_encryption.cc +++ b/libqpdf/QPDF_encryption.cc @@ -229,13 +229,11 @@ process_with_aes( aes.writeString(data); } aes.finish(); - auto bufp = buffer.getBufferSharedPointer(); if (outlength == 0) { - outlength = bufp->getSize(); + return buffer.getString(); } else { - outlength = std::min(outlength, bufp->getSize()); + return buffer.getString().substr(0, outlength); } - return {reinterpret_cast(bufp->getBuffer()), outlength}; } static std::string @@ -1021,8 +1019,7 @@ QPDF::decryptString(std::string& str, QPDFObjGen const& og) key.length()); pl.writeString(str); pl.finish(); - auto buf = bufpl.getBufferSharedPointer(); - str = std::string(reinterpret_cast(buf->getBuffer()), buf->getSize()); + str = bufpl.getString(); } else { QTC::TC("qpdf", "QPDF_encryption rc4 decode string"); size_t vlen = str.length();