sdext/source/pdfimport/filterdet.cxx |   44 ++++++++++++++++++++++++++---------
 1 file changed, 33 insertions(+), 11 deletions(-)

New commits:
commit d629416be0a3742afda0f5c50c2f80a15553143c
Author:     Dr. David Alan Gilbert <d...@treblig.org>
AuthorDate: Wed Apr 9 02:28:38 2025 +0100
Commit:     Tomaž Vajngerl <qui...@gmail.com>
CommitDate: Tue Apr 22 04:39:02 2025 +0200

    tdf#55425, tdf#66580: sdext,pdfimport: embeddedFile password entry
    
    Add password entry for the new embeddedFile based detection.
    
    Note that we need to decrypt to be able to get pdfium to open the
    PDF at all, and fundamentally it's needed to read the filename.
    But that does mean we need the password during the detect phase.
    
    Change-Id: I67473158863ac6eb2ee389659449cf3b5582c579
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/183860
    Reviewed-by: Tomaž Vajngerl <qui...@gmail.com>
    Tested-by: Jenkins

diff --git a/sdext/source/pdfimport/filterdet.cxx 
b/sdext/source/pdfimport/filterdet.cxx
index 371d9cb56cfd..c4dddf1f580c 100644
--- a/sdext/source/pdfimport/filterdet.cxx
+++ b/sdext/source/pdfimport/filterdet.cxx
@@ -303,10 +303,10 @@ constexpr FilenameMime aFilenameMimeMap[] = {
 // This uses PDFium to do the legwork.
 uno::Reference<io::XStream> getEmbeddedFile(const OUString& rInPDFFileURL,
                                             OUString& rOutMimetype,
-                                            OUString& /*io_rPwd*/,
+                                            OUString& io_rPwd,
                                             const 
uno::Reference<uno::XComponentContext>& xContext,
-                                            const 
uno::Sequence<beans::PropertyValue>& /*rFilterData*/,
-                                            bool /*bMayUseUI*/)
+                                            const 
uno::Sequence<beans::PropertyValue>& rFilterData,
+                                            bool bMayUseUI)
 {
     uno::Reference<io::XStream> xEmbed;
     OUString aSysUPath;
@@ -338,22 +338,41 @@ uno::Reference<io::XStream> getEmbeddedFile(const 
OUString& rInPDFFileURL,
             return xEmbed;
         }
 
-        auto pPdfiumDoc = pPdfium->openDocument(pMemRawPdf, nFileSize, 
OString(/*TODO Pass*/));
-
+        bool bAgain = false;
         do {
+            OString aIsoPwd = OUStringToOString(io_rPwd, 
RTL_TEXTENCODING_ISO_8859_1);
+            auto pPdfiumDoc = pPdfium->openDocument(pMemRawPdf, nFileSize, 
aIsoPwd);
+            SAL_INFO("sdext.pdfimport", "getEmbeddedFile pdfium docptr: " << 
pPdfiumDoc);
+
             auto nPdfiumErr = pPdfium->getLastErrorCode();
-            if (nPdfiumErr != vcl::pdf::PDFErrorType::Success
-                && nPdfiumErr != vcl::pdf::PDFErrorType::Password)
+            if (pPdfiumDoc == nullptr
+                && (nPdfiumErr != vcl::pdf::PDFErrorType::Success
+                    && nPdfiumErr != vcl::pdf::PDFErrorType::Password))
             {
                 SAL_WARN("sdext.pdfimport",
                          "getEmbeddedFile pdfium err: " << 
pPdfium->getLastError());
                 break;
             }
-            if (nPdfiumErr == vcl::pdf::PDFErrorType::Password)
+            if (pPdfiumDoc == nullptr && nPdfiumErr == 
vcl::pdf::PDFErrorType::Password)
             {
-                SAL_WARN("sdext.pdfimport", "getEmbeddedFile pdfium Pass 
todo");
+                uno::Reference<task::XInteractionHandler> xIntHdl;
+                for (const beans::PropertyValue& rAttrib : rFilterData)
+                {
+                    if (rAttrib.Name == "InteractionHandler")
+                        rAttrib.Value >>= xIntHdl;
+                }
+                SAL_INFO("sdext.pdfimport",
+                         "getEmbeddedFile pdfium Pass needed: UI: " << 
bMayUseUI);
+                if (bMayUseUI && xIntHdl.is())
+                {
+                    OUString 
aDocName(rInPDFFileURL.copy(rInPDFFileURL.lastIndexOf('/') + 1));
+                    bAgain = getPassword(xIntHdl, io_rPwd, !bAgain, aDocName);
+                    SAL_INFO("sdext.pdfimport", "getEmbeddedFile pdfium Pass 
result: " << bAgain);
+                    continue;
+                }
                 break;
             }
+            bAgain = false;
             // The new style hybrids have exactly one embedded file
             if (pPdfiumDoc->getAttachmentCount() != 1)
             {
@@ -406,7 +425,7 @@ uno::Reference<io::XStream> getEmbeddedFile(const OUString& 
rInPDFFileURL,
             xEmbed = xContextStream;
             rOutMimetype = aMimetype;
             SAL_INFO("sdext.pdfimport", "getEmbeddedFile returning stream");
-        } while(false);
+        } while (bAgain);
 
         osl_unmapMappedFile(fileHandle, pMemRawPdf, nFileSize);
         osl_closeFile(fileHandle);
@@ -469,16 +488,19 @@ OUString SAL_CALL PDFDetector::detect( uno::Sequence< 
beans::PropertyValue >& rF
 
     OUString aEmbedMimetype;
 
+    SAL_INFO( "sdext.pdfimport", "PDFDetector::detect before getEmbeddedFile" 
);
     // Try testing for the newer embedded file format
-    xEmbedStream = getEmbeddedFile(aURL, aEmbedMimetype, aPassword, 
m_xContext, rFilterData, false);
+    xEmbedStream = getEmbeddedFile(aURL, aEmbedMimetype, aPassword, 
m_xContext, rFilterData, true);
 
     if (aEmbedMimetype.isEmpty())
     {
+        SAL_INFO( "sdext.pdfimport", "PDFDetector::detect before 
getAdditionalStream" );
         // No success with embedd file, try the older trailer based 
AdditionalStream
         xEmbedStream =
             getAdditionalStream(aURL, aEmbedMimetype, aPassword, m_xContext, 
rFilterData, false);
     }
 
+    SAL_INFO( "sdext.pdfimport", "PDFDetector::detect after emb/addit: "  << 
aEmbedMimetype);
     if (aFileHandle)
         osl_removeFile(aURL.pData);
 

Reply via email to