diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2024-12-17 12:08:30 +0900 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2024-12-27 01:39:09 +0100 |
commit | 6bf6c4e062bdb4df103a489406e6bca6c26704ef (patch) | |
tree | 043c43b6b992b4058ee95c78c8fb1ed0280f6bc2 /vcl/source | |
parent | 5d0629877b9f3e818fabed30d4add2dd948f3911 (diff) |
pdf: test hybrid mode (PDF attachment) with encryption
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 <vmiklos@collabora.com>
Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com>
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/178778
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Tested-by: Jenkins
Diffstat (limited to 'vcl/source')
-rw-r--r-- | vcl/source/pdf/PDFiumLibrary.cxx | 56 |
1 files changed, 56 insertions, 0 deletions
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 <fpdf_save.h> #include <fpdf_signature.h> #include <fpdf_formfill.h> +#include <fpdf_attachment.h> #include <osl/endian.h> #include <vcl/bitmap.hxx> @@ -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<unsigned char>& 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<PDFiumPage> openPage(int nIndex) override; std::unique_ptr<PDFiumSignature> getSignature(int nIndex) override; + std::unique_ptr<PDFiumAttachment> getAttachment(int nIndex) override; std::vector<unsigned int> 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<unsigned char>& 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<PDFiumSignature> PDFiumDocumentImpl::getSignature(int nIndex) return pPDFiumSignature; } +std::unique_ptr<PDFiumAttachment> PDFiumDocumentImpl::getAttachment(int nIndex) +{ + std::unique_ptr<PDFiumAttachment> pPDFiumAttachment; + FPDF_ATTACHMENT pAttachment = FPDFDoc_GetAttachment(mpPdfDocument, nIndex); + if (pAttachment) + { + pPDFiumAttachment = std::make_unique<PDFiumAttachmentImpl>(pAttachment); + } + return pPDFiumAttachment; +} + std::vector<unsigned int> 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; |