vcl/inc/pdf/IPDFEncryptor.hxx     |   19 +++++++++++--------
 vcl/inc/pdf/PDFEncryptor.hxx      |   18 +++---------------
 vcl/source/gdi/pdfwriter_impl.cxx |    8 ++++----
 vcl/source/pdf/PDFEncryptor.cxx   |   12 ++++--------
 4 files changed, 22 insertions(+), 35 deletions(-)

New commits:
commit 98afe0ad746bef755f4af8d782e8a6c501ad9366
Author:     Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk>
AuthorDate: Wed Nov 20 17:04:48 2024 +0900
Commit:     Tomaž Vajngerl <qui...@gmail.com>
CommitDate: Thu Dec 19 23:28:16 2024 +0100

    pdf: move common things to interface, prepare methods
    
    Move common flag, if the stream is encrypted from PDFEncryptor
    impl. to the IPDFEncryptor "interface".
    
    Change setupEncryption and encrypt methods to be closer to what
    will be needed in R6 implementation.
    
    Change-Id: I4dbd787c29e2a13ac900c6df360538b7e06af4ec
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/176880
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/178754
    Reviewed-by: Tomaž Vajngerl <qui...@gmail.com>
    Tested-by: Jenkins

diff --git a/vcl/inc/pdf/IPDFEncryptor.hxx b/vcl/inc/pdf/IPDFEncryptor.hxx
index bcd90dcef4bf..706eb82c71ed 100644
--- a/vcl/inc/pdf/IPDFEncryptor.hxx
+++ b/vcl/inc/pdf/IPDFEncryptor.hxx
@@ -39,6 +39,10 @@ namespace vcl::pdf
  */
 class IPDFEncryptor
 {
+private:
+    /* set to true if the following stream must be encrypted, used inside 
writeBuffer() */
+    bool m_bEncryptThisStream = false;
+
 public:
     virtual ~IPDFEncryptor() {}
 
@@ -73,17 +77,16 @@ public:
     virtual void setupKeysAndCheck(PDFEncryptionProperties& rProperties) = 0;
 
     /** Setup before we start encrypting - remembers the key */
-    virtual void setupEncryption(std::vector<sal_uInt8> const& rEncryptionKey, 
sal_Int32 nObject)
-        = 0;
-
-    virtual void enableStreamEncryption() = 0;
-    virtual void disableStreamEncryption() = 0;
-    virtual bool isStreamEncryptionEnabled() = 0;
+    virtual void setupEncryption(std::vector<sal_uInt8>& rEncryptionKey, 
sal_Int32 nObject) = 0;
 
     /** Encrypts the input and stores into the output */
-    virtual void encrypt(const void* pInput, sal_uInt64 nInputSize, sal_uInt8* 
pOutput,
-                         sal_uInt64 nOutputsSize)
+    virtual void encrypt(const void* pInput, sal_uInt64 nInputSize, 
std::vector<sal_uInt8>& rOutput,
+                         sal_uInt64 nOutputSize)
         = 0;
+
+    void enableStreamEncryption() { m_bEncryptThisStream = true; }
+    void disableStreamEncryption() { m_bEncryptThisStream = false; }
+    bool isStreamEncryptionEnabled() { return m_bEncryptThisStream; }
 };
 }
 
diff --git a/vcl/inc/pdf/PDFEncryptor.hxx b/vcl/inc/pdf/PDFEncryptor.hxx
index e4134605c194..9a65c5a0043b 100644
--- a/vcl/inc/pdf/PDFEncryptor.hxx
+++ b/vcl/inc/pdf/PDFEncryptor.hxx
@@ -15,11 +15,6 @@
 #include <vector>
 #include <pdf/IPDFEncryptor.hxx>
 
-namespace vcl
-{
-struct PDFEncryptionProperties;
-}
-
 namespace com::sun::star::uno
 {
 template <typename> class Reference;
@@ -41,9 +36,6 @@ private:
     sal_Int32 m_nKeyLength = 0; // key length, 16 or 5
     sal_Int32 m_nRC4KeyLength = 0; // key length, 16 or 10, to be input to the 
algorithm 3.1
 
-    /* set to true if the following stream must be encrypted, used inside 
writeBuffer() */
-    bool m_bEncryptThisStream = false;
-
     /* used to cipher the stream data and for password management */
     rtlCipher m_aCipher = nullptr;
 
@@ -67,14 +59,10 @@ public:
 
     void setupKeysAndCheck(PDFEncryptionProperties& rProperties) override;
 
-    void setupEncryption(std::vector<sal_uInt8> const& rEncryptionKey, 
sal_Int32 nObject) override;
-    void enableStreamEncryption() override;
-    void disableStreamEncryption() override;
-
-    bool isStreamEncryptionEnabled() override { return m_bEncryptThisStream; }
+    void setupEncryption(std::vector<sal_uInt8>& rEncryptionKey, sal_Int32 
nObject) override;
 
-    void encrypt(const void* pInput, sal_uInt64 nInputSize, sal_uInt8* pOutput,
-                 sal_uInt64 nOutputsSize) override;
+    void encrypt(const void* pInput, sal_uInt64 nInputSize, 
std::vector<sal_uInt8>& rOutput,
+                 sal_uInt64 nOutputSize) override;
 };
 }
 
diff --git a/vcl/source/gdi/pdfwriter_impl.cxx 
b/vcl/source/gdi/pdfwriter_impl.cxx
index 91eb3a1e078c..aa841973df17 100644
--- a/vcl/source/gdi/pdfwriter_impl.cxx
+++ b/vcl/source/gdi/pdfwriter_impl.cxx
@@ -1618,7 +1618,7 @@ inline void 
PDFWriterImpl::appendUnicodeTextStringEncrypt( const OUString& rInSt
             *pCopy++ = static_cast<sal_uInt8>( aUnChar & 255 );
         }
         //encrypt in place
-        m_pPDFEncryptor->encrypt(m_vEncryptionBuffer.data(), nChars, 
m_vEncryptionBuffer.data(), nChars);
+        m_pPDFEncryptor->encrypt(m_vEncryptionBuffer.data(), nChars, 
m_vEncryptionBuffer, nChars);
         //now append, hexadecimal (appendHex), the encrypted result
         for(int i = 0; i < nChars; i++)
             appendHex( m_vEncryptionBuffer[i], rOutBuffer );
@@ -1638,7 +1638,7 @@ inline void PDFWriterImpl::appendLiteralStringEncrypt( 
std::string_view rInStrin
         m_vEncryptionBuffer.resize(nChars);
         //encrypt the string in a buffer, then append it
         enableStringEncryption(nInObjectNumber);
-        m_pPDFEncryptor->encrypt(rInString.data(), nChars, 
m_vEncryptionBuffer.data(), nChars);
+        m_pPDFEncryptor->encrypt(rInString.data(), nChars, 
m_vEncryptionBuffer, nChars);
         appendLiteralString( 
reinterpret_cast<char*>(m_vEncryptionBuffer.data()), nChars, rOutBuffer );
     }
     else
@@ -1749,7 +1749,7 @@ bool PDFWriterImpl::writeBufferBytes( const void* 
pBuffer, sal_uInt64 nBytes )
         if (bStreamEncryption)
         {
             m_vEncryptionBuffer.resize(nBytes);
-            m_pPDFEncryptor->encrypt(pBuffer, nBytes, 
m_vEncryptionBuffer.data(), nBytes);
+            m_pPDFEncryptor->encrypt(pBuffer, nBytes, m_vEncryptionBuffer, 
nBytes);
         }
 
         const void* pWriteBuffer = bStreamEncryption ? 
m_vEncryptionBuffer.data() : pBuffer;
@@ -9823,7 +9823,7 @@ bool PDFWriterImpl::writeBitmapObject( const BitmapEmit& 
rObject, bool bMask )
                     m_vEncryptionBuffer[nChar++] = rColor.GetBlue();
                 }
                 //encrypt the colorspace lookup table
-                m_pPDFEncryptor->encrypt(m_vEncryptionBuffer.data(), nChar, 
m_vEncryptionBuffer.data(), nChar);
+                m_pPDFEncryptor->encrypt(m_vEncryptionBuffer.data(), nChar, 
m_vEncryptionBuffer, nChar);
                 //now queue the data for output
                 nChar = 0;
                 for( sal_uInt16 i = 0; i < pAccess->GetPaletteEntryCount(); 
i++ )
diff --git a/vcl/source/pdf/PDFEncryptor.cxx b/vcl/source/pdf/PDFEncryptor.cxx
index 63bb49c05425..a5425f014ae0 100644
--- a/vcl/source/pdf/PDFEncryptor.cxx
+++ b/vcl/source/pdf/PDFEncryptor.cxx
@@ -393,11 +393,7 @@ void 
PDFEncryptor::setupKeysAndCheck(vcl::PDFEncryptionProperties& rProperties)
     }
 }
 
-void PDFEncryptor::enableStreamEncryption() { m_bEncryptThisStream = true; }
-
-void PDFEncryptor::disableStreamEncryption() { m_bEncryptThisStream = false; }
-
-void PDFEncryptor::setupEncryption(std::vector<sal_uInt8> const& 
rEncryptionKey, sal_Int32 nObject)
+void PDFEncryptor::setupEncryption(std::vector<sal_uInt8>& rEncryptionKey, 
sal_Int32 nObject)
 {
     std::vector<sal_uInt8> aKey(rEncryptionKey.begin(), rEncryptionKey.begin() 
+ m_nKeyLength);
     std::vector<sal_uInt8> aObjectArray{
@@ -418,10 +414,10 @@ void PDFEncryptor::setupEncryption(std::vector<sal_uInt8> 
const& rEncryptionKey,
 }
 
 /* implement the encryption part of the PDF spec encryption algorithm 3.1 */
-void PDFEncryptor::encrypt(const void* pInput, sal_uInt64 nInputSize, 
sal_uInt8* pOutput,
-                           sal_uInt64 nOutputsSize)
+void PDFEncryptor::encrypt(const void* pInput, sal_uInt64 nInputSize,
+                           std::vector<sal_uInt8>& rOutput, sal_uInt64 
nOutputsSize)
 {
-    rtl_cipher_encodeARCFOUR(m_aCipher, pInput, sal_Size(nInputSize), pOutput,
+    rtl_cipher_encodeARCFOUR(m_aCipher, pInput, sal_Size(nInputSize), 
rOutput.data(),
                              sal_Size(nOutputsSize));
 }
 

Reply via email to