vcl/inc/pdf/PDFEncryptorR6.hxx                 |    6 ++++++
 vcl/qa/cppunit/pdfexport/PDFEncryptionTest.cxx |   13 +++++++++++++
 vcl/source/pdf/PDFEncryptorR6.cxx              |   11 +++++++++++
 3 files changed, 30 insertions(+)

New commits:
commit f44becfdb8bc0dad2d3ec7abd3aaa889d9e0bfea
Author:     Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk>
AuthorDate: Wed Nov 20 18:41:35 2024 +0900
Commit:     Tomaž Vajngerl <qui...@gmail.com>
CommitDate: Sat Dec 21 05:59:37 2024 +0100

    pdf: add function to pad the vector as required by PDF specs
    
    Change-Id: I7196ad523b3084124a3b03fb2e4998d42fd91779
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/176883
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/178757
    Tested-by: Jenkins
    Reviewed-by: Tomaž Vajngerl <qui...@gmail.com>

diff --git a/vcl/inc/pdf/PDFEncryptorR6.hxx b/vcl/inc/pdf/PDFEncryptorR6.hxx
index 70f7a3422f3a..219796ccf0a5 100644
--- a/vcl/inc/pdf/PDFEncryptorR6.hxx
+++ b/vcl/inc/pdf/PDFEncryptorR6.hxx
@@ -101,6 +101,12 @@ VCL_DLLPUBLIC std::vector<sal_uInt8> 
encryptPerms(std::vector<sal_uInt8>& rPerms
 VCL_DLLPUBLIC std::vector<sal_uInt8> createPerms(sal_Int32 nAccessPermissions,
                                                  bool bEncryptMetadata);
 
+/** Padding as described in Internet RFC 8018
+ *
+ * Described in ISO 32000-2:2020(E) - 7.6.3.1
+ */
+VCL_DLLPUBLIC size_t addPaddingToVector(std::vector<sal_uInt8>& rVector, 
size_t nBlockSize);
+
 } // end vcl::pdf
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/vcl/qa/cppunit/pdfexport/PDFEncryptionTest.cxx 
b/vcl/qa/cppunit/pdfexport/PDFEncryptionTest.cxx
index 408735d12ca2..40a4eb94c1b4 100644
--- a/vcl/qa/cppunit/pdfexport/PDFEncryptionTest.cxx
+++ b/vcl/qa/cppunit/pdfexport/PDFEncryptionTest.cxx
@@ -244,6 +244,19 @@ CPPUNIT_TEST_FIXTURE(PDFEncryptionTest, 
testPermsEncryption)
     CPPUNIT_ASSERT_EQUAL(std::string("fcffffffffffffff54616462"),
                          comphelper::hashToString(aPermsWithoutRandomBytes));
 }
+
+CPPUNIT_TEST_FIXTURE(PDFEncryptionTest, testPadding)
+{
+    constexpr size_t constBlockSize = 16;
+    std::vector<sal_uInt8> aVector{ 'T', 'e', 's', 't', '!' };
+    CPPUNIT_ASSERT_EQUAL(size_t(5), aVector.size());
+    size_t nPaddedSize = vcl::pdf::addPaddingToVector(aVector, constBlockSize);
+    CPPUNIT_ASSERT_EQUAL(size_t(constBlockSize), aVector.size());
+    CPPUNIT_ASSERT_EQUAL(size_t(constBlockSize), nPaddedSize);
+    for (size_t i = 6; i < constBlockSize; i++)
+        CPPUNIT_ASSERT_EQUAL(sal_uInt8(0x0B), aVector[i]);
+}
+
 } // end anonymous namespace
 
 CPPUNIT_PLUGIN_IMPLEMENT();
diff --git a/vcl/source/pdf/PDFEncryptorR6.cxx 
b/vcl/source/pdf/PDFEncryptorR6.cxx
index b3850a5929f8..b3e6f9e3059e 100644
--- a/vcl/source/pdf/PDFEncryptorR6.cxx
+++ b/vcl/source/pdf/PDFEncryptorR6.cxx
@@ -238,6 +238,17 @@ std::vector<sal_uInt8> computeHashR6(const sal_uInt8* 
pPassword, size_t nPasswor
     return std::vector<sal_uInt8>(K.begin(), K.begin() + 32);
 }
 
+size_t addPaddingToVector(std::vector<sal_uInt8>& rVector, size_t nBlockSize)
+{
+    size_t nPaddedSize = comphelper::roundUp(rVector.size(), 
size_t(nBlockSize));
+    if (nPaddedSize > rVector.size())
+    {
+        sal_uInt8 nPaddedValue = sal_uInt8(nPaddedSize - rVector.size());
+        rVector.resize(nPaddedSize, nPaddedValue);
+    }
+    return nPaddedSize;
+}
+
 } // end vcl::pdf
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Reply via email to