include/vcl/pdfwriter.hxx | 40 +++++++++++++++---------------------- vcl/source/gdi/pdfwriter_impl.cxx | 10 ++++----- vcl/source/gdi/pdfwriter_impl2.cxx | 4 +-- vcl/source/pdf/PDFEncryptor.cxx | 7 +----- 4 files changed, 26 insertions(+), 35 deletions(-)
New commits: commit bb7cbd0f79e6ef18a55b5f983001229f2517c26b Author: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk> AuthorDate: Mon Nov 11 22:48:34 2024 +0100 Commit: Miklos Vajna <vmik...@collabora.com> CommitDate: Thu Dec 5 10:20:15 2024 +0100 pdf: cleanup and improve PDFEncryptionProperties Add clear method to clear the variables that we usually want to be cleared. Also rename Encrypt to canEncrypt - which is more clear what it means. Cleanup initializers. Change-Id: I96735eb6f73a699fb0759496fc8781bcff3854de Reviewed-on: https://gerrit.libreoffice.org/c/core/+/176455 Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com> Reviewed-by: Miklos Vajna <vmik...@collabora.com> diff --git a/include/vcl/pdfwriter.hxx b/include/vcl/pdfwriter.hxx index b901505c28cd..e982761fdc04 100644 --- a/include/vcl/pdfwriter.hxx +++ b/include/vcl/pdfwriter.hxx @@ -74,20 +74,20 @@ class VCL_DLLPUBLIC PDFOutputStream virtual void write( const css::uno::Reference< css::io::XOutputStream >& xStream ) = 0; }; - /* The following structure describes the permissions used in PDF security */ struct PDFEncryptionProperties { //for both 40 and 128 bit security, see 3.5.2 PDF v 1.4 table 3.15, v 1.5 and v 1.6 table 3.20. - bool CanPrintTheDocument; - bool CanModifyTheContent; - bool CanCopyOrExtract; - bool CanAddOrModify; + bool CanPrintTheDocument = false; + bool CanModifyTheContent = false; + bool CanCopyOrExtract = false; + bool CanAddOrModify = false; + //for revision 3 (bit 128 security) only - bool CanFillInteractive; - bool CanExtractForAccessibility; - bool CanAssemble; - bool CanPrintFull; + bool CanFillInteractive = false; + bool CanExtractForAccessibility = true; + bool CanAssemble = false; + bool CanPrintFull = false; // encryption will only happen if EncryptionKey is not empty // EncryptionKey is actually a construct out of OValue, UValue and DocumentIdentifier @@ -100,22 +100,16 @@ struct PDFEncryptionProperties std::vector<sal_uInt8> EncryptionKey; std::vector<sal_uInt8> DocumentIdentifier; - //permission default set for 128 bit, accessibility only - PDFEncryptionProperties() : - CanPrintTheDocument ( false ), - CanModifyTheContent ( false ), - CanCopyOrExtract ( false ), - CanAddOrModify ( false ), - CanFillInteractive ( false ), - CanExtractForAccessibility ( true ), - CanAssemble ( false ), - CanPrintFull ( false ) - {} - + bool canEncrypt() const + { + return !OValue.empty() && !UValue.empty() && !DocumentIdentifier.empty(); + } - bool Encrypt() const + void clear() { - return ! OValue.empty() && ! UValue.empty() && ! DocumentIdentifier.empty(); + OValue.clear(); + UValue.clear(); + EncryptionKey.clear(); } }; diff --git a/vcl/source/gdi/pdfwriter_impl.cxx b/vcl/source/gdi/pdfwriter_impl.cxx index a28a919ff7f6..fd7aeba24787 100644 --- a/vcl/source/gdi/pdfwriter_impl.cxx +++ b/vcl/source/gdi/pdfwriter_impl.cxx @@ -1470,7 +1470,7 @@ PDFWriterImpl::PDFWriterImpl( const PDFWriter::PDFWriterContext& rContext, m_pPDFEncryptor->prepareEncryption(xEncryptionMaterialHolder, m_aContext.Encryption); } - if (m_aContext.Encryption.Encrypt()) + if (m_aContext.Encryption.canEncrypt()) { m_pPDFEncryptor->setupKeysAndCheck(m_aContext.Encryption); } @@ -1606,7 +1606,7 @@ append the string as unicode hex, encrypted if needed inline void PDFWriterImpl::appendUnicodeTextStringEncrypt( const OUString& rInString, const sal_Int32 nInObjectNumber, OStringBuffer& rOutBuffer ) { rOutBuffer.append( "<" ); - if (m_aContext.Encryption.Encrypt()) + if (m_aContext.Encryption.canEncrypt()) { const sal_Unicode* pStr = rInString.getStr(); sal_Int32 nLen = rInString.getLength(); @@ -1640,7 +1640,7 @@ inline void PDFWriterImpl::appendLiteralStringEncrypt( std::string_view rInStrin rOutBuffer.append( "(" ); sal_Int32 nChars = rInString.size(); //check for encryption, if ok, encrypt the string, then convert with appndLiteralString - if (m_aContext.Encryption.Encrypt()) + if (m_aContext.Encryption.canEncrypt()) { m_vEncryptionBuffer.resize(nChars); //encrypt the string in a buffer, then append it @@ -6135,7 +6135,7 @@ bool PDFWriterImpl::emitTrailer() sal_Int32 nSecObject = 0; - if( m_aContext.Encryption.Encrypt() ) + if (m_aContext.Encryption.canEncrypt()) { nSecObject = emitEncrypt(); } @@ -9681,7 +9681,7 @@ bool PDFWriterImpl::writeBitmapObject( const BitmapEmit& rObject, bool bMask ) aLine.append( "[ /Indexed/DeviceRGB " ); aLine.append( static_cast<sal_Int32>(pAccess->GetPaletteEntryCount()-1) ); aLine.append( " <" ); - if( m_aContext.Encryption.Encrypt() ) + if (m_aContext.Encryption.canEncrypt()) { enableStringEncryption(rObject.m_nObject); //check encryption buffer size diff --git a/vcl/source/gdi/pdfwriter_impl2.cxx b/vcl/source/gdi/pdfwriter_impl2.cxx index 2f67295b9a7f..81cc21b50152 100644 --- a/vcl/source/gdi/pdfwriter_impl2.cxx +++ b/vcl/source/gdi/pdfwriter_impl2.cxx @@ -1080,7 +1080,7 @@ void PDFWriterImpl::playMetafile( const GDIMetaFile& i_rMtf, vcl::PDFExtOutDevDa void PDFWriterImpl::checkAndEnableStreamEncryption(sal_Int32 nObject) { - if (!m_aContext.Encryption.Encrypt() || !m_pPDFEncryptor) + if (!m_aContext.Encryption.canEncrypt() || !m_pPDFEncryptor) return; m_pPDFEncryptor->enableStreamEncryption(); @@ -1095,7 +1095,7 @@ void PDFWriterImpl::disableStreamEncryption() void PDFWriterImpl::enableStringEncryption(sal_Int32 nObject) { - if (!m_aContext.Encryption.Encrypt() || !m_pPDFEncryptor) + if (!m_aContext.Encryption.canEncrypt() || !m_pPDFEncryptor) return; m_pPDFEncryptor->setupEncryption(m_aContext.Encryption.EncryptionKey, nObject); diff --git a/vcl/source/pdf/PDFEncryptor.cxx b/vcl/source/pdf/PDFEncryptor.cxx index 183e66e8e713..63bb49c05425 100644 --- a/vcl/source/pdf/PDFEncryptor.cxx +++ b/vcl/source/pdf/PDFEncryptor.cxx @@ -369,9 +369,7 @@ bool PDFEncryptor::prepareEncryption( if (!bSuccess) { - rProperties.OValue.clear(); - rProperties.UValue.clear(); - rProperties.EncryptionKey.clear(); + rProperties.clear(); } return bSuccess; } @@ -386,8 +384,7 @@ void PDFEncryptor::setupKeysAndCheck(vcl::PDFEncryptionProperties& rProperties) { // the field lengths are invalid ? This was not setup by initEncryption. // do not encrypt after all - rProperties.OValue.clear(); - rProperties.UValue.clear(); + rProperties.clear(); OSL_ENSURE(false, "encryption data failed sanity check, encryption disabled"); } else // setup key lengths