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 9f055eec9fbe800f8f4428017a5f73c33b9bf0cf
Author:     Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk>
AuthorDate: Wed Nov 20 17:04:48 2024 +0900
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Fri Dec 6 09:21:48 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>

diff --git a/vcl/inc/pdf/IPDFEncryptor.hxx b/vcl/inc/pdf/IPDFEncryptor.hxx
index b0f180ad944b..744c8bcac761 100644
--- a/vcl/inc/pdf/IPDFEncryptor.hxx
+++ b/vcl/inc/pdf/IPDFEncryptor.hxx
@@ -40,6 +40,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() {}
 
@@ -74,17 +78,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 a728203e8af6..d8465d80c233 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;
 
@@ -65,14 +57,10 @@ public:
         PDFEncryptionProperties& rProperties) override;
     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 f1e5967ee26c..54f69a87166c 100644
--- a/vcl/source/gdi/pdfwriter_impl.cxx
+++ b/vcl/source/gdi/pdfwriter_impl.cxx
@@ -1627,7 +1627,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 );
@@ -1647,7 +1647,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
@@ -1761,7 +1761,7 @@ bool PDFWriterImpl::writeBufferBytes( const void* 
pBuffer, sal_uInt64 nBytes )
             m_vEncryptionBuffer.resize(nBytes);
             if (buffOK)
             {
-                m_pPDFEncryptor->encrypt(pBuffer, nBytes, 
m_vEncryptionBuffer.data(), nBytes);
+                m_pPDFEncryptor->encrypt(pBuffer, nBytes, m_vEncryptionBuffer, 
nBytes);
             }
         }
 
@@ -9698,7 +9698,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