include/vcl/pdfwriter.hxx              |    3 -
 vcl/inc/pdf/COSWriter.hxx              |   78 ++++++++++++++++++---------------
 vcl/inc/pdf/pdfwriter_impl.hxx         |    2 
 vcl/source/filter/ipdf/pdfdocument.cxx |    8 ++-
 vcl/source/gdi/pdfwriter_impl.cxx      |   49 +++++++-------------
 5 files changed, 68 insertions(+), 72 deletions(-)

New commits:
commit 8e38f3be07e9ce8ee3a813528c09b3bbc39c9998
Author:     Tomaž Vajngerl <[email protected]>
AuthorDate: Fri Nov 22 21:19:41 2024 +0900
Commit:     Miklos Vajna <[email protected]>
CommitDate: Mon Dec 16 09:41:48 2024 +0100

    pdf: get rid of PDFWriter::AppendUnicodeTextString
    
    It's not needed when we have COSWriter instead that could be used.
    Needed to change IPDFEncryptor from unique_ptr to shared_ptr so we
    can share it with COSWriter, but without the need to be set.
    
    Also rename the string literal and unicode methods that take a key
    to writeKeyAnd... methods to be mroe explicit. We will need methods
    that don't write a key sooner or later.
    
    Change-Id: I15c5d347c6658f7ded2de940acc968dee053d460
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/177037
    Tested-by: Jenkins CollaboraOffice <[email protected]>
    Reviewed-by: Miklos Vajna <[email protected]>

diff --git a/include/vcl/pdfwriter.hxx b/include/vcl/pdfwriter.hxx
index e9d4326eb593..aab2ff9acf5c 100644
--- a/include/vcl/pdfwriter.hxx
+++ b/include/vcl/pdfwriter.hxx
@@ -1275,9 +1275,6 @@ public:
     */
     void AddAttachedFile(OUString const& rFileName, OUString const& rMimeType, 
OUString const& rDescription, std::unique_ptr<PDFOutputStream> pStream);
 
-    /// Write rString as a PDF hex string into rBuffer.
-    static void AppendUnicodeTextString(const OUString& rString, 
OStringBuffer& rBuffer);
-
     /// Get current date/time in PDF D:YYYYMMDDHHMMSS form.
     static OString GetDateTime(svl::crypto::SigningContext* pSigningContext = 
nullptr);
 };
diff --git a/vcl/inc/pdf/COSWriter.hxx b/vcl/inc/pdf/COSWriter.hxx
index 767654689b73..4c90f6f4968e 100644
--- a/vcl/inc/pdf/COSWriter.hxx
+++ b/vcl/inc/pdf/COSWriter.hxx
@@ -21,7 +21,7 @@ namespace vcl::pdf
  */
 class COSWriter
 {
-    std::unique_ptr<IPDFEncryptor>& mpPDFEncryptor;
+    std::shared_ptr<IPDFEncryptor> mpPDFEncryptor;
     OStringBuffer maLine;
 
     void appendLiteralString(const char* pStr, sal_Int32 nLength)
@@ -59,22 +59,23 @@ class COSWriter
             nLength--;
         }
     }
-    template <typename T> void appendHex(T nValue)
+
+    template <typename T> static void appendHex(T nValue, OStringBuffer& 
rBuffer)
     {
         static constexpr const auto constHexDigits = std::to_array<char>(
             { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 
'D', 'E', 'F' });
-        maLine.append(constHexDigits[(nValue >> 4) & 15]);
-        maLine.append(constHexDigits[nValue & 15]);
+        rBuffer.append(constHexDigits[(nValue >> 4) & 15]);
+        rBuffer.append(constHexDigits[nValue & 15]);
     }
 
     void appendHexArray(sal_uInt8* pArray, size_t nSize)
     {
         for (size_t i = 0; i < nSize; i++)
-            appendHex(pArray[i]);
+            appendHex(pArray[i], maLine);
     }
 
 public:
-    COSWriter(std::unique_ptr<IPDFEncryptor>& pPDFEncryptor)
+    COSWriter(std::shared_ptr<IPDFEncryptor> const& pPDFEncryptor)
         : mpPDFEncryptor(pPDFEncryptor)
         , maLine(1024)
     {
@@ -107,22 +108,21 @@ public:
         maLine.append(value);
     }
 
-    void writeString(std::string_view key, char* pString, sal_Int32 nSize)
+    void writeKeyAndUnicode(std::string_view key, OUString const& rString)
     {
         maLine.append(key);
-        maLine.append(" (");
-        appendLiteralString(pString, nSize);
-        maLine.append(")");
+        maLine.append("<");
+        appendUnicodeTextString(rString, maLine);
+        maLine.append(">");
     }
 
-    void writeUnicodeEncrypt(std::string_view key, OUString const& rString, 
sal_Int32 nObject,
-                             bool bEncrypt, std::vector<sal_uInt8>& rKey)
+    void writeKeyAndUnicodeEncrypt(std::string_view key, OUString const& 
rString, sal_Int32 nObject,
+                                   bool bEncrypt, std::vector<sal_uInt8>& rKey)
     {
-        maLine.append(key);
-        maLine.append("<");
-
-        if (bEncrypt)
+        if (bEncrypt && mpPDFEncryptor)
         {
+            maLine.append(key);
+            maLine.append("<");
             const sal_Unicode* pString = rString.getStr();
             size_t nLength = rString.getLength();
             //prepare a unicode string, encrypt it
@@ -143,44 +143,41 @@ public:
             mpPDFEncryptor->encrypt(aEncryptionBuffer.data(), nChars, 
aNewBuffer, nChars);
             //now append, hexadecimal (appendHex), the encrypted result
             appendHexArray(aNewBuffer.data(), aNewBuffer.size());
+            maLine.append(">");
         }
         else
         {
-            //PDFWriter::AppendUnicodeTextString(rInString, maLine);
-            maLine.append("FEFF");
-            const sal_Unicode* pString = rString.getStr();
-            size_t nLength = rString.getLength();
-            for (size_t i = 0; i < nLength; i++)
-            {
-                sal_Unicode aChar = pString[i];
-                appendHex(sal_Int8(aChar >> 8));
-                appendHex(sal_Int8(aChar & 255));
-            }
+            writeKeyAndUnicode(key, rString);
         }
-        maLine.append(">");
     }
 
-    void writeLiteralEncrypt(std::string_view key, std::string_view value, 
sal_Int32 nObject,
-                             bool bEncrypt, std::vector<sal_uInt8>& rKey)
+    void writeKeyAndLiteral(std::string_view key, std::string_view value)
     {
         maLine.append(key);
-
         maLine.append("(");
-        size_t nChars = value.size();
+        appendLiteralString(value.data(), value.size());
+        maLine.append(")");
+    }
 
-        if (bEncrypt)
+    void writeKeyAndLiteralEncrypt(std::string_view key, std::string_view 
value, sal_Int32 nObject,
+                                   bool bEncrypt, std::vector<sal_uInt8>& rKey)
+    {
+        if (bEncrypt && mpPDFEncryptor)
         {
+            maLine.append(key);
+            maLine.append("(");
+            size_t nChars = value.size();
             std::vector<sal_uInt8> aEncryptionBuffer(nChars);
             mpPDFEncryptor->setupEncryption(rKey, nObject);
             mpPDFEncryptor->encrypt(value.data(), nChars, aEncryptionBuffer, 
nChars);
             
appendLiteralString(reinterpret_cast<char*>(aEncryptionBuffer.data()),
                                 aEncryptionBuffer.size());
+            maLine.append(")");
         }
         else
         {
-            appendLiteralString(value.data(), nChars);
+            writeKeyAndLiteral(key, value);
         }
-        maLine.append(")");
     }
 
     void writeHexArray(std::string_view key, sal_uInt8* pData, size_t nSize)
@@ -190,6 +187,19 @@ public:
         appendHexArray(pData, nSize);
         maLine.append(">");
     }
+
+    static void appendUnicodeTextString(const OUString& rString, 
OStringBuffer& rBuffer)
+    {
+        rBuffer.append("FEFF");
+        const sal_Unicode* pString = rString.getStr();
+        size_t nLength = rString.getLength();
+        for (size_t i = 0; i < nLength; i++)
+        {
+            sal_Unicode aChar = pString[i];
+            COSWriter::appendHex(sal_Int8(aChar >> 8), rBuffer);
+            COSWriter::appendHex(sal_Int8(aChar & 255), rBuffer);
+        }
+    }
 };
 }
 
diff --git a/vcl/inc/pdf/pdfwriter_impl.hxx b/vcl/inc/pdf/pdfwriter_impl.hxx
index a35f6f0b5458..4e4bdf89cfff 100644
--- a/vcl/inc/pdf/pdfwriter_impl.hxx
+++ b/vcl/inc/pdf/pdfwriter_impl.hxx
@@ -807,7 +807,7 @@ private:
 
     ExternalPDFStreams m_aExternalPDFStreams;
 
-    std::unique_ptr<IPDFEncryptor> m_pPDFEncryptor;
+    std::shared_ptr<IPDFEncryptor> m_pPDFEncryptor;
 
     /* output redirection; e.g. to accumulate content streams for
        XObjects
diff --git a/vcl/source/filter/ipdf/pdfdocument.cxx 
b/vcl/source/filter/ipdf/pdfdocument.cxx
index 41eaca7aff34..ff7663db8555 100644
--- a/vcl/source/filter/ipdf/pdfdocument.cxx
+++ b/vcl/source/filter/ipdf/pdfdocument.cxx
@@ -31,6 +31,7 @@
 #include <o3tl/safeint.hxx>
 
 #include <pdf/objectcopier.hxx>
+#include <pdf/COSWriter.hxx>
 
 using namespace com::sun::star;
 
@@ -133,6 +134,7 @@ sal_Int32 
PDFDocument::WriteSignatureObject(svl::crypto::SigningContext& rSignin
     aSignatureEntry.SetOffset(m_aEditBuffer.Tell());
     aSignatureEntry.SetDirty(true);
     m_aXRef[nSignatureId] = aSignatureEntry;
+
     OStringBuffer aSigBuffer(OString::number(nSignatureId)
                              + " 0 obj
"
                                "<</Contents <");
@@ -167,9 +169,9 @@ sal_Int32 
PDFDocument::WriteSignatureObject(svl::crypto::SigningContext& rSignin
 
     if (!rDescription.isEmpty())
     {
-        aSigBuffer.append("/Reason<");
-        vcl::PDFWriter::AppendUnicodeTextString(rDescription, aSigBuffer);
-        aSigBuffer.append(">");
+        pdf::COSWriter aWriter(nullptr);
+        aWriter.writeKeyAndUnicode("/Reason", rDescription);
+        aSigBuffer.append(aWriter.getLine());
     }
 
     aSigBuffer.append(" >>
endobj

");
diff --git a/vcl/source/gdi/pdfwriter_impl.cxx 
b/vcl/source/gdi/pdfwriter_impl.cxx
index 36ff34e9770c..a76ca07f8035 100644
--- a/vcl/source/gdi/pdfwriter_impl.cxx
+++ b/vcl/source/gdi/pdfwriter_impl.cxx
@@ -310,19 +310,6 @@ void removePlaceholderSE(std::vector<PDFStructureElement> 
& rStructure, PDFStruc
 
 } // end anonymous namespace
 
-void PDFWriter::AppendUnicodeTextString(const OUString& rString, 
OStringBuffer& rBuffer)
-{
-    rBuffer.append( "FEFF" );
-    const sal_Unicode* pStr = rString.getStr();
-    sal_Int32 nLen = rString.getLength();
-    for( int i = 0; i < nLen; i++ )
-    {
-        sal_Unicode aChar = pStr[i];
-        appendHex( static_cast<sal_Int8>(aChar >> 8), rBuffer );
-        appendHex( static_cast<sal_Int8>(aChar & 255 ), rBuffer );
-    }
-}
-
 void PDFWriterImpl::createWidgetFieldName( sal_Int32 i_nWidgetIndex, const 
PDFWriter::AnyWidget& i_rControl )
 {
     /* #i80258# previously we use appendName here
@@ -662,17 +649,17 @@ void computeDocumentIdentifier(std::vector<sal_uInt8>& 
o_rIdentifier,
     OString aInfoValuesOut;
     OStringBuffer aID(1024);
     if (!i_rDocInfo.Title.isEmpty())
-        PDFWriter::AppendUnicodeTextString(i_rDocInfo.Title, aID);
+        COSWriter::appendUnicodeTextString(i_rDocInfo.Title, aID);
     if (!i_rDocInfo.Author.isEmpty())
-        PDFWriter::AppendUnicodeTextString(i_rDocInfo.Author, aID);
+        COSWriter::appendUnicodeTextString(i_rDocInfo.Author, aID);
     if (!i_rDocInfo.Subject.isEmpty())
-        PDFWriter::AppendUnicodeTextString(i_rDocInfo.Subject, aID);
+        COSWriter::appendUnicodeTextString(i_rDocInfo.Subject, aID);
     if (!i_rDocInfo.Keywords.isEmpty())
-        PDFWriter::AppendUnicodeTextString(i_rDocInfo.Keywords, aID);
+        COSWriter::appendUnicodeTextString(i_rDocInfo.Keywords, aID);
     if (!i_rDocInfo.Creator.isEmpty())
-        PDFWriter::AppendUnicodeTextString(i_rDocInfo.Creator, aID);
+        COSWriter::appendUnicodeTextString(i_rDocInfo.Creator, aID);
     if (!i_rDocInfo.Producer.isEmpty())
-        PDFWriter::AppendUnicodeTextString(i_rDocInfo.Producer, aID);
+        COSWriter::appendUnicodeTextString(i_rDocInfo.Producer, aID);
 
     TimeValue aTVal, aGMT;
     oslDateTime aDT;
@@ -1558,7 +1545,7 @@ inline void 
PDFWriterImpl::appendUnicodeTextStringEncrypt( const OUString& rInSt
         appendHexArray(aNewBuffer.data(), aNewBuffer.size(), rOutBuffer);
     }
     else
-        PDFWriter::AppendUnicodeTextString(rInString, rOutBuffer);
+        COSWriter::appendUnicodeTextString(rInString, rOutBuffer);
     rOutBuffer.append( ">" );
 }
 
@@ -5323,18 +5310,18 @@ bool PDFWriterImpl::emitCatalog()
         appendObjectID(rAttachedFile.mnObjectId, aLine);
         aLine.append("<</Type /Filespec");
         aLine.append("/F<");
-        PDFWriter::AppendUnicodeTextString(rAttachedFile.maFilename, aLine);
+        COSWriter::appendUnicodeTextString(rAttachedFile.maFilename, aLine);
         aLine.append("> ");
         if (PDFWriter::PDFVersion::PDF_1_7 <= m_aContext.Version)
         {
             aLine.append("/UF<");
-            PDFWriter::AppendUnicodeTextString(rAttachedFile.maFilename, 
aLine);
+            COSWriter::appendUnicodeTextString(rAttachedFile.maFilename, 
aLine);
             aLine.append("> ");
         }
         if (!rAttachedFile.maDescription.isEmpty())
         {
             aLine.append("/Desc <");
-            PDFWriter::AppendUnicodeTextString(rAttachedFile.maDescription, 
aLine);
+            COSWriter::appendUnicodeTextString(rAttachedFile.maDescription, 
aLine);
             aLine.append("> ");
         }
         aLine.append("/EF <</F ");
@@ -5370,7 +5357,7 @@ bool PDFWriterImpl::emitCatalog()
         for (auto & rAttachedFile : m_aDocumentAttachedFiles)
         {
             aLine.append('<');
-            PDFWriter::AppendUnicodeTextString(rAttachedFile.maFilename, 
aLine);
+            COSWriter::appendUnicodeTextString(rAttachedFile.maFilename, 
aLine);
             aLine.append('>');
             aLine.append(' ');
             appendObjectReference(rAttachedFile.mnObjectId, aLine);
@@ -5761,31 +5748,31 @@ sal_Int32 PDFWriterImpl::emitInfoDict( )
     {
         if (!m_aContext.DocumentInfo.Title.isEmpty())
         {
-            aWriter.writeUnicodeEncrypt("/Title", 
m_aContext.DocumentInfo.Title, nObject, bEncrypt, rKey);
+            aWriter.writeKeyAndUnicodeEncrypt("/Title", 
m_aContext.DocumentInfo.Title, nObject, bEncrypt, rKey);
         }
         if (!m_aContext.DocumentInfo.Author.isEmpty())
         {
-            aWriter.writeUnicodeEncrypt("/Author", 
m_aContext.DocumentInfo.Author, nObject, bEncrypt, rKey);
+            aWriter.writeKeyAndUnicodeEncrypt("/Author", 
m_aContext.DocumentInfo.Author, nObject, bEncrypt, rKey);
         }
         if (!m_aContext.DocumentInfo.Subject.isEmpty())
         {
-            aWriter.writeUnicodeEncrypt("/Subject", 
m_aContext.DocumentInfo.Subject, nObject, bEncrypt, rKey);
+            aWriter.writeKeyAndUnicodeEncrypt("/Subject", 
m_aContext.DocumentInfo.Subject, nObject, bEncrypt, rKey);
         }
         if (!m_aContext.DocumentInfo.Keywords.isEmpty())
         {
-            aWriter.writeUnicodeEncrypt("/Keywords", 
m_aContext.DocumentInfo.Keywords, nObject, bEncrypt, rKey);
+            aWriter.writeKeyAndUnicodeEncrypt("/Keywords", 
m_aContext.DocumentInfo.Keywords, nObject, bEncrypt, rKey);
         }
         if (!m_aContext.DocumentInfo.Creator.isEmpty())
         {
-            aWriter.writeUnicodeEncrypt("/Creator", 
m_aContext.DocumentInfo.Creator, nObject, bEncrypt, rKey);
+            aWriter.writeKeyAndUnicodeEncrypt("/Creator", 
m_aContext.DocumentInfo.Creator, nObject, bEncrypt, rKey);
         }
         if (!m_aContext.DocumentInfo.Producer.isEmpty())
         {
-            aWriter.writeUnicodeEncrypt("/Producer", 
m_aContext.DocumentInfo.Producer, nObject, bEncrypt, rKey);
+            aWriter.writeKeyAndUnicodeEncrypt("/Producer", 
m_aContext.DocumentInfo.Producer, nObject, bEncrypt, rKey);
         }
     }
     // Allowed in PDF 2.0
-    aWriter.writeLiteralEncrypt("/CreationDate", m_aCreationDateString, 
nObject, bEncrypt, rKey);
+    aWriter.writeKeyAndLiteralEncrypt("/CreationDate", m_aCreationDateString, 
nObject, bEncrypt, rKey);
     aWriter.endDict();
     aWriter.endObject();
 

Reply via email to