summaryrefslogtreecommitdiff
path: root/vcl/source
diff options
context:
space:
mode:
authorTomaž Vajngerl <tomaz.vajngerl@collabora.co.uk>2024-12-17 12:08:30 +0900
committerMiklos Vajna <vmiklos@collabora.com>2024-12-18 10:16:58 +0100
commit61c48c0020b7de9142c5d251de918a0d88066e14 (patch)
treec2ccc4db47b2a4931b8d77fe5a021e50d942735e /vcl/source
parentddeca485638552a0957ac9dba2542e1601d91153 (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>
Diffstat (limited to 'vcl/source')
-rw-r--r--vcl/source/pdf/PDFiumLibrary.cxx56
1 files changed, 56 insertions, 0 deletions
diff --git a/vcl/source/pdf/PDFiumLibrary.cxx b/vcl/source/pdf/PDFiumLibrary.cxx
index 4ad9af1726d0..d56562f34b74 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>
@@ -488,6 +489,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:
@@ -508,11 +526,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;
};
@@ -728,6 +748,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()
@@ -768,6 +811,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);
@@ -791,6 +845,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;