vcl/inc/pdf/COSWriter.hxx | 5 +++ vcl/source/gdi/pdfwriter_impl.cxx | 52 ++++++++++++++++++-------------------- 2 files changed, 30 insertions(+), 27 deletions(-)
New commits: commit 5cd3d158d1bc71f95050507e13386a73c1032f8b Author: Tomaž Vajngerl <[email protected]> AuthorDate: Fri Dec 13 23:26:43 2024 +0900 Commit: Miklos Vajna <[email protected]> CommitDate: Mon Dec 16 14:01:22 2024 +0100 pdf: Fix embedded files when the PDF is encrypted When we encrypt the PDF and want to attach the ODF document to the PDF, the PDF file would not have any attachments when opened. This is because the encryption for the attachments and embedded files was not done correctly. This patch fixes this issue. Change-Id: I5bd8175f6067b8b684defa3b60746176f4314c8c Reviewed-on: https://gerrit.libreoffice.org/c/core/+/178447 Reviewed-by: Miklos Vajna <[email protected]> Tested-by: Jenkins CollaboraOffice <[email protected]> diff --git a/vcl/inc/pdf/COSWriter.hxx b/vcl/inc/pdf/COSWriter.hxx index 2142de1f846e..d63a2198a2e7 100644 --- a/vcl/inc/pdf/COSWriter.hxx +++ b/vcl/inc/pdf/COSWriter.hxx @@ -72,6 +72,11 @@ public: OStringBuffer& getLine() { return mrBuffer; } void startDict() { mrBuffer.append("<<"); } + void startDictWithKey(std::string_view key) + { + mrBuffer.append(key); + mrBuffer.append("<<"); + } void endDict() { mrBuffer.append(">> "); } void startStream() { mrBuffer.append("stream "); } diff --git a/vcl/source/gdi/pdfwriter_impl.cxx b/vcl/source/gdi/pdfwriter_impl.cxx index f94e093a1492..60b25f1c43c9 100644 --- a/vcl/source/gdi/pdfwriter_impl.cxx +++ b/vcl/source/gdi/pdfwriter_impl.cxx @@ -5184,25 +5184,27 @@ bool PDFWriterImpl::emitEmbeddedFiles() aLine.append(" /Params "); appendObjectReference(nParamsObject, aLine); aLine.append(">> stream "); - checkAndEnableStreamEncryption(rEmbeddedFile.m_nObject); CHECK_RETURN(writeBuffer(aLine)); - disableStreamEncryption(); aLine.setLength(0); sal_Int64 nSize{}; if (!rEmbeddedFile.m_aDataContainer.isEmpty()) { nSize = rEmbeddedFile.m_aDataContainer.getSize(); + checkAndEnableStreamEncryption(rEmbeddedFile.m_nObject); CHECK_RETURN(writeBufferBytes(rEmbeddedFile.m_aDataContainer.getData(), rEmbeddedFile.m_aDataContainer.getSize())); + disableStreamEncryption(); } else if (rEmbeddedFile.m_pStream) { + checkAndEnableStreamEncryption(rEmbeddedFile.m_nObject); sal_uInt64 nBegin = getCurrentFilePosition(); css::uno::Reference<css::io::XOutputStream> xStream(new PDFStreamIf(this)); rEmbeddedFile.m_pStream->write(xStream); rEmbeddedFile.m_pStream.reset(); xStream.clear(); nSize = sal_Int64(getCurrentFilePosition() - nBegin); + disableStreamEncryption(); } aLine.append(" endstream endobj "); CHECK_RETURN(writeBuffer(aLine)); @@ -5314,29 +5316,26 @@ bool PDFWriterImpl::emitCatalog() { if (!updateObject(rAttachedFile.mnObjectId)) return false; - aLine.setLength( 0 ); + aLine.setLength(0); - appendObjectID(rAttachedFile.mnObjectId, aLine); - aLine.append("<</Type /Filespec"); - aLine.append("/F<"); - COSWriter::appendUnicodeTextString(rAttachedFile.maFilename, aLine); - aLine.append("> "); + aWriter.startObject(rAttachedFile.mnObjectId); + aWriter.startDict(); + aWriter.write("/Type", "/Filespec"); + aWriter.writeKeyAndUnicodeEncrypt("/F", rAttachedFile.maFilename, rAttachedFile.mnObjectId); if (PDFWriter::PDFVersion::PDF_1_7 <= m_aContext.Version) { - aLine.append("/UF<"); - COSWriter::appendUnicodeTextString(rAttachedFile.maFilename, aLine); - aLine.append("> "); + aWriter.writeKeyAndUnicodeEncrypt("/UF", rAttachedFile.maFilename, rAttachedFile.mnObjectId); } if (!rAttachedFile.maDescription.isEmpty()) { - aLine.append("/Desc <"); - COSWriter::appendUnicodeTextString(rAttachedFile.maDescription, aLine); - aLine.append("> "); + aWriter.writeKeyAndUnicodeEncrypt("/Desc", rAttachedFile.maDescription, rAttachedFile.mnObjectId); } - aLine.append("/EF <</F "); - appendObjectReference(rAttachedFile.mnEmbeddedFileObjectId, aLine); - aLine.append(">>"); - aLine.append(">> endobj "); + aLine.append("/EF"); + aWriter.startDict(); + aWriter.writeKeyAndReference("/F", rAttachedFile.mnEmbeddedFileObjectId); + aWriter.endDict(); + aWriter.endDict(); + aWriter.endObject(); CHECK_RETURN( writeBuffer( aLine ) ); } @@ -5361,18 +5360,17 @@ bool PDFWriterImpl::emitCatalog() if (!m_aDocumentAttachedFiles.empty()) { - aLine.append("/Names "); - aLine.append("<</EmbeddedFiles <</Names ["); + aWriter.startDictWithKey("/Names"); + aWriter.startDictWithKey("/EmbeddedFiles"); + aLine.append("/Names ["); for (auto & rAttachedFile : m_aDocumentAttachedFiles) { - aLine.append('<'); - COSWriter::appendUnicodeTextString(rAttachedFile.maFilename, aLine); - aLine.append('>'); - aLine.append(' '); - appendObjectReference(rAttachedFile.mnObjectId, aLine); + aWriter.writeUnicodeEncrypt(rAttachedFile.maFilename, m_nCatalogObject); + aWriter.writeReference(rAttachedFile.mnObjectId); } - aLine.append("]>>>>"); - aLine.append(" " ); + aLine.append("]"); + aWriter.endDict(); + aWriter.endDict(); } if( m_aContext.PageLayout != PDFWriter::DefaultLayout )
