From a30330702d4dcafab3f8b0deae2485997e28a01d Mon Sep 17 00:00:00 2001 From: Tomaž Vajngerl Date: Fri, 13 Dec 2024 23:26:43 +0900 Subject: pdf: Fix embedded files when the PDF is encrypted MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Tested-by: Jenkins CollaboraOffice Reviewed-on: https://gerrit.libreoffice.org/c/core/+/178771 Tested-by: Tomaž Vajngerl Reviewed-by: Tomaž Vajngerl --- vcl/source/gdi/pdfwriter_impl.cxx | 52 +++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 27 deletions(-) (limited to 'vcl/source') diff --git a/vcl/source/gdi/pdfwriter_impl.cxx b/vcl/source/gdi/pdfwriter_impl.cxx index faf795fa60c7..cee972efb2fe 100644 --- a/vcl/source/gdi/pdfwriter_impl.cxx +++ b/vcl/source/gdi/pdfwriter_impl.cxx @@ -5233,26 +5233,28 @@ bool PDFWriterImpl::emitEmbeddedFiles() aLine.append(" /Params "); appendObjectReference(nParamsObject, aLine); aLine.append(">>\nstream\n"); - checkAndEnableStreamEncryption(rEmbeddedFile.m_nObject); if (!writeBuffer(aLine)) return false; - disableStreamEncryption(); aLine.setLength(0); sal_Int64 nSize{}; if (!rEmbeddedFile.m_aDataContainer.isEmpty()) { nSize = rEmbeddedFile.m_aDataContainer.getSize(); + checkAndEnableStreamEncryption(rEmbeddedFile.m_nObject); if (!writeBufferBytes(rEmbeddedFile.m_aDataContainer.getData(), rEmbeddedFile.m_aDataContainer.getSize())) return false; + disableStreamEncryption(); } else if (rEmbeddedFile.m_pStream) { + checkAndEnableStreamEncryption(rEmbeddedFile.m_nObject); sal_uInt64 nBegin = getCurrentFilePosition(); css::uno::Reference xStream(new PDFStreamIf(this)); rEmbeddedFile.m_pStream->write(xStream); rEmbeddedFile.m_pStream.reset(); xStream.clear(); nSize = sal_Int64(getCurrentFilePosition() - nBegin); + disableStreamEncryption(); } aLine.append("\nendstream\nendobj\n\n"); if (!writeBuffer(aLine)) return false; @@ -5361,29 +5363,26 @@ bool PDFWriterImpl::emitCatalog() { if (!updateObject(rAttachedFile.mnObjectId)) return false; - aLine.setLength( 0 ); + aLine.setLength(0); - appendObjectID(rAttachedFile.mnObjectId, 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 <>"); - aLine.append(">>\nendobj\n\n"); + aLine.append("/EF"); + aWriter.startDict(); + aWriter.writeKeyAndReference("/F", rAttachedFile.mnEmbeddedFileObjectId); + aWriter.endDict(); + aWriter.endDict(); + aWriter.endObject(); if (!writeBuffer(aLine)) return false; } @@ -5408,18 +5407,17 @@ bool PDFWriterImpl::emitCatalog() if (!m_aDocumentAttachedFiles.empty()) { - aLine.append("/Names "); - aLine.append("<'); - aLine.append(' '); - appendObjectReference(rAttachedFile.mnObjectId, aLine); + aWriter.writeUnicodeEncrypt(rAttachedFile.maFilename, m_nCatalogObject); + aWriter.writeReference(rAttachedFile.mnObjectId); } - aLine.append("]>>>>"); - aLine.append("\n" ); + aLine.append("]"); + aWriter.endDict(); + aWriter.endDict(); } if( m_aContext.PageLayout != PDFWriter::DefaultLayout ) -- cgit