From 6bf6c4e062bdb4df103a489406e6bca6c26704ef Mon Sep 17 00:00:00 2001 From: Tomaž Vajngerl Date: Tue, 17 Dec 2024 12:08:30 +0900 Subject: pdf: test hybrid mode (PDF attachment) with encryption MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adds a test that checks the attached file is correctly added to the PDF stream when encryption is enabled. Also add support for reading of attachments to PDFium wrapper. The test saves a document as encrypted PDF with hybrid mode enabled, then opens the document in PDFium and saves the attachment content to a temporary file, which is opened again and the content checked. Change-Id: I918f67d269c31fe7826a49473e939d52bac7fe98 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/178629 Reviewed-by: Miklos Vajna Tested-by: Jenkins CollaboraOffice Reviewed-on: https://gerrit.libreoffice.org/c/core/+/178778 Reviewed-by: Tomaž Vajngerl Tested-by: Jenkins --- vcl/source/pdf/PDFiumLibrary.cxx | 56 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'vcl/source') diff --git a/vcl/source/pdf/PDFiumLibrary.cxx b/vcl/source/pdf/PDFiumLibrary.cxx index a1552e9ca1bf..3eea4a2010fc 100644 --- a/vcl/source/pdf/PDFiumLibrary.cxx +++ b/vcl/source/pdf/PDFiumLibrary.cxx @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -489,6 +490,23 @@ public: FPDF_FORMHANDLE getPointer(); }; +class PDFiumAttachmentImpl final : public PDFiumAttachment +{ +private: + FPDF_ATTACHMENT mpAttachment; + PDFiumAttachmentImpl(const PDFiumSignatureImpl&) = delete; + PDFiumAttachmentImpl& operator=(const PDFiumSignatureImpl&) = delete; + +public: + PDFiumAttachmentImpl(FPDF_ATTACHMENT pAttachment) + : mpAttachment(pAttachment) + { + } + + OUString getName() override; + bool getFile(std::vector& rOutBuffer) override; +}; + class PDFiumDocumentImpl : public PDFiumDocument { private: @@ -509,11 +527,13 @@ public: basegfx::B2DSize getPageSize(int nIndex) override; int getPageCount() override; int getSignatureCount() override; + int getAttachmentCount() override; int getFileVersion() override; bool saveWithVersion(SvMemoryStream& rStream, int nFileVersion) override; std::unique_ptr openPage(int nIndex) override; std::unique_ptr getSignature(int nIndex) override; + std::unique_ptr getAttachment(int nIndex) override; std::vector getTrailerEnds() override; OUString getBookmarks() override; }; @@ -742,6 +762,29 @@ util::DateTime PDFiumSignatureImpl::getTime() return aRet; } +OUString PDFiumAttachmentImpl::getName() +{ + return getUnicodeString([this](FPDF_WCHAR* buffer, unsigned long length) { + return FPDFAttachment_GetName(mpAttachment, buffer, length); + }); +} + +bool PDFiumAttachmentImpl::getFile(std::vector& rOutBuffer) +{ + rOutBuffer.clear(); + + unsigned long nLength{}; + if (!FPDFAttachment_GetFile(mpAttachment, nullptr, 0, &nLength)) + return false; + + rOutBuffer.resize(nLength); + unsigned long nActualLength{}; + if (!FPDFAttachment_GetFile(mpAttachment, rOutBuffer.data(), nLength, &nActualLength)) + return false; + rOutBuffer.resize(nActualLength); + return true; +} + PDFiumDocumentImpl::PDFiumDocumentImpl(FPDF_DOCUMENT pPdfDocument) : mpPdfDocument(pPdfDocument) , m_aFormCallbacks() @@ -782,6 +825,17 @@ std::unique_ptr PDFiumDocumentImpl::getSignature(int nIndex) return pPDFiumSignature; } +std::unique_ptr PDFiumDocumentImpl::getAttachment(int nIndex) +{ + std::unique_ptr pPDFiumAttachment; + FPDF_ATTACHMENT pAttachment = FPDFDoc_GetAttachment(mpPdfDocument, nIndex); + if (pAttachment) + { + pPDFiumAttachment = std::make_unique(pAttachment); + } + return pPDFiumAttachment; +} + std::vector PDFiumDocumentImpl::getTrailerEnds() { int nNumTrailers = FPDF_GetTrailerEnds(mpPdfDocument, nullptr, 0); @@ -855,6 +909,8 @@ int PDFiumDocumentImpl::getPageCount() { return FPDF_GetPageCount(mpPdfDocument) int PDFiumDocumentImpl::getSignatureCount() { return FPDF_GetSignatureCount(mpPdfDocument); } +int PDFiumDocumentImpl::getAttachmentCount() { return FPDFDoc_GetAttachmentCount(mpPdfDocument); } + int PDFiumDocumentImpl::getFileVersion() { int nFileVersion = 0; -- cgit