Commit 3237ef70fb77ce323394de1e2793abdb5ae85384

Authored by m-holger
1 parent d11622b6

Add new method Pl_Buffer::getString

include/qpdf/Pl_Buffer.hh
@@ -64,6 +64,10 @@ class QPDF_DLL_CLASS Pl_Buffer: public Pipeline @@ -64,6 +64,10 @@ class QPDF_DLL_CLASS Pl_Buffer: public Pipeline
64 QPDF_DLL 64 QPDF_DLL
65 void getMallocBuffer(unsigned char** buf, size_t* len); 65 void getMallocBuffer(unsigned char** buf, size_t* len);
66 66
  67 + // Same as getBuffer but returns the result as a string.
  68 + QPDF_DLL
  69 + std::string getString();
  70 +
67 private: 71 private:
68 class QPDF_DLL_PRIVATE Members 72 class QPDF_DLL_PRIVATE Members
69 { 73 {
libqpdf/Pl_Buffer.cc
@@ -53,6 +53,17 @@ Pl_Buffer::getBuffer() @@ -53,6 +53,17 @@ Pl_Buffer::getBuffer()
53 return b; 53 return b;
54 } 54 }
55 55
  56 +std::string
  57 +Pl_Buffer::getString()
  58 +{
  59 + if (!m->ready) {
  60 + throw std::logic_error("Pl_Buffer::getString() called when not ready");
  61 + }
  62 + auto s = std::move(m->data);
  63 + m->data.clear();
  64 + return s;
  65 +}
  66 +
56 std::shared_ptr<Buffer> 67 std::shared_ptr<Buffer>
57 Pl_Buffer::getBufferSharedPointer() 68 Pl_Buffer::getBufferSharedPointer()
58 { 69 {
libqpdf/QPDFAcroFormDocumentHelper.cc
@@ -584,8 +584,7 @@ QPDFAcroFormDocumentHelper::adjustDefaultAppearances( @@ -584,8 +584,7 @@ QPDFAcroFormDocumentHelper::adjustDefaultAppearances(
584 ResourceReplacer rr(dr_map, rf.getNamesByResourceType()); 584 ResourceReplacer rr(dr_map, rf.getNamesByResourceType());
585 Pl_Buffer buf_pl("filtered DA"); 585 Pl_Buffer buf_pl("filtered DA");
586 da_stream.filterAsContents(&rr, &buf_pl); 586 da_stream.filterAsContents(&rr, &buf_pl);
587 - auto buf = buf_pl.getBufferSharedPointer();  
588 - std::string new_da(reinterpret_cast<char*>(buf->getBuffer()), buf->getSize()); 587 + std::string new_da = buf_pl.getString();
589 obj.replaceKey("/DA", QPDFObjectHandle::newString(new_da)); 588 obj.replaceKey("/DA", QPDFObjectHandle::newString(new_da));
590 } 589 }
591 590
libqpdf/QPDFObjectHandle.cc
@@ -1708,8 +1708,7 @@ QPDFObjectHandle::pipeContentStreams( @@ -1708,8 +1708,7 @@ QPDFObjectHandle::pipeContentStreams(
1708 need_newline = (lc.getLastChar() != static_cast<unsigned char>('\n')); 1708 need_newline = (lc.getLastChar() != static_cast<unsigned char>('\n'));
1709 QTC::TC("qpdf", "QPDFObjectHandle need_newline", need_newline ? 0 : 1); 1709 QTC::TC("qpdf", "QPDFObjectHandle need_newline", need_newline ? 0 : 1);
1710 } 1710 }
1711 - std::unique_ptr<Buffer> b(buf.getBuffer());  
1712 - p->write(b->getBuffer(), b->getSize()); 1711 + p->writeString(buf.getString());
1713 p->finish(); 1712 p->finish();
1714 } 1713 }
1715 1714
libqpdf/QPDFWriter.cc
@@ -1579,10 +1579,7 @@ QPDFWriter::unparseObject( @@ -1579,10 +1579,7 @@ QPDFWriter::unparseObject(
1579 m->cur_data_key.length()); 1579 m->cur_data_key.length());
1580 pl.writeString(val); 1580 pl.writeString(val);
1581 pl.finish(); 1581 pl.finish();
1582 - auto buf = bufpl.getBufferSharedPointer();  
1583 - val = QPDF_String(  
1584 - std::string(reinterpret_cast<char*>(buf->getBuffer()), buf->getSize()))  
1585 - .unparse(true); 1582 + val = QPDF_String(bufpl.getString()).unparse(true);
1586 } else { 1583 } else {
1587 auto tmp_ph = QUtil::make_unique_cstr(val); 1584 auto tmp_ph = QUtil::make_unique_cstr(val);
1588 char* tmp = tmp_ph.get(); 1585 char* tmp = tmp_ph.get();
libqpdf/QPDF_encryption.cc
@@ -229,13 +229,11 @@ process_with_aes( @@ -229,13 +229,11 @@ process_with_aes(
229 aes.writeString(data); 229 aes.writeString(data);
230 } 230 }
231 aes.finish(); 231 aes.finish();
232 - auto bufp = buffer.getBufferSharedPointer();  
233 if (outlength == 0) { 232 if (outlength == 0) {
234 - outlength = bufp->getSize(); 233 + return buffer.getString();
235 } else { 234 } else {
236 - outlength = std::min(outlength, bufp->getSize()); 235 + return buffer.getString().substr(0, outlength);
237 } 236 }
238 - return {reinterpret_cast<char*>(bufp->getBuffer()), outlength};  
239 } 237 }
240 238
241 static std::string 239 static std::string
@@ -1021,8 +1019,7 @@ QPDF::decryptString(std::string&amp; str, QPDFObjGen const&amp; og) @@ -1021,8 +1019,7 @@ QPDF::decryptString(std::string&amp; str, QPDFObjGen const&amp; og)
1021 key.length()); 1019 key.length());
1022 pl.writeString(str); 1020 pl.writeString(str);
1023 pl.finish(); 1021 pl.finish();
1024 - auto buf = bufpl.getBufferSharedPointer();  
1025 - str = std::string(reinterpret_cast<char*>(buf->getBuffer()), buf->getSize()); 1022 + str = bufpl.getString();
1026 } else { 1023 } else {
1027 QTC::TC("qpdf", "QPDF_encryption rc4 decode string"); 1024 QTC::TC("qpdf", "QPDF_encryption rc4 decode string");
1028 size_t vlen = str.length(); 1025 size_t vlen = str.length();