diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2024-11-20 18:41:35 +0900 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2024-12-21 05:59:37 +0100 |
commit | f44becfdb8bc0dad2d3ec7abd3aaa889d9e0bfea (patch) | |
tree | 0b7a7c37ab7fc475f9a672cb321d957bba66343f | |
parent | 0c09e5fb6eca9d3128e69779a654c4c6f01d9c02 (diff) |
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 <vmiklos@collabora.com>
Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com>
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/178757
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
-rw-r--r-- | vcl/inc/pdf/PDFEncryptorR6.hxx | 6 | ||||
-rw-r--r-- | vcl/qa/cppunit/pdfexport/PDFEncryptionTest.cxx | 13 | ||||
-rw-r--r-- | vcl/source/pdf/PDFEncryptorR6.cxx | 11 |
3 files changed, 30 insertions, 0 deletions
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: */ |