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 <jenkinscollaboraoffice@gmail.com>
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/177874
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Tested-by: Jenkins
This commit is contained in:
Tomaž Vajngerl 2024-11-11 22:48:34 +01:00 committed by Tomaž Vajngerl
parent 6e049433e4
commit 9b6be690a2
4 changed files with 27 additions and 35 deletions

View file

@ -77,15 +77,16 @@ class VCL_DLLPUBLIC PDFOutputStream
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
@ -98,22 +99,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 Encrypt() const
bool canEncrypt() const
{
return ! OValue.empty() && ! UValue.empty() && ! DocumentIdentifier.empty();
return !OValue.empty() && !UValue.empty() && !DocumentIdentifier.empty();
}
void clear()
{
OValue.clear();
UValue.clear();
EncryptionKey.clear();
}
};

View file

@ -1461,7 +1461,7 @@ PDFWriterImpl::PDFWriterImpl( const PDFWriter::PDFWriterContext& rContext,
m_pPDFEncryptor->prepareEncryption(xEncryptionMaterialHolder, m_aContext.Encryption);
}
if (m_pPDFEncryptor && m_aContext.Encryption.Encrypt())
if (m_pPDFEncryptor && m_aContext.Encryption.canEncrypt())
{
m_pPDFEncryptor->setupKeysAndCheck(m_aContext.Encryption);
}
@ -1597,7 +1597,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();
@ -1631,7 +1631,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
@ -6184,7 +6184,7 @@ bool PDFWriterImpl::emitTrailer()
sal_Int32 nSecObject = 0;
if( m_aContext.Encryption.Encrypt() )
if (m_aContext.Encryption.canEncrypt())
{
nSecObject = emitEncrypt();
}
@ -9806,7 +9806,7 @@ bool PDFWriterImpl::writeBitmapObject( const BitmapEmit& rObject, bool bMask )
aLine.append( "[ /Indexed/DeviceRGB " );
aLine.append( static_cast<sal_Int32>(pAccess->GetPaletteEntryCount()-1) );
aLine.append( "\n<" );
if( m_aContext.Encryption.Encrypt() )
if (m_aContext.Encryption.canEncrypt())
{
enableStringEncryption(rObject.m_nObject);
//check encryption buffer size

View file

@ -1079,7 +1079,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();
@ -1094,7 +1094,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);

View file

@ -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