Kaydet (Commit) 27d03cc3 authored tarafından Noel Grandin's avatar Noel Grandin

loplugin:useuniqueptr in PDFObject

Change-Id: I605dd193f4fd32b07762208766db5f529adf7998
Reviewed-on: https://gerrit.libreoffice.org/59774
Tested-by: Jenkins
Reviewed-by: 's avatarNoel Grandin <noel.grandin@collabora.co.uk>
üst d695fdf9
...@@ -275,7 +275,7 @@ private: ...@@ -275,7 +275,7 @@ private:
// fills *ppStream and *pBytes with start of stream and count of bytes // fills *ppStream and *pBytes with start of stream and count of bytes
// memory returned in *ppStream must be freed with std::free afterwards // memory returned in *ppStream must be freed with std::free afterwards
// fills in NULL and 0 in case of error // fills in NULL and 0 in case of error
bool getDeflatedStream( char** ppStream, unsigned int* pBytes, const PDFContainer* pObjectContainer, EmitContext& rContext ) const; bool getDeflatedStream( std::unique_ptr<char[]>& rpStream, unsigned int* pBytes, const PDFContainer* pObjectContainer, EmitContext& rContext ) const;
}; };
struct PDFPart : public PDFContainer struct PDFPart : public PDFContainer
......
...@@ -650,7 +650,7 @@ PDFObject::~PDFObject() ...@@ -650,7 +650,7 @@ PDFObject::~PDFObject()
{ {
} }
bool PDFObject::getDeflatedStream( char** ppStream, unsigned int* pBytes, const PDFContainer* pObjectContainer, EmitContext& rContext ) const bool PDFObject::getDeflatedStream( std::unique_ptr<char[]>& rpStream, unsigned int* pBytes, const PDFContainer* pObjectContainer, EmitContext& rContext ) const
{ {
bool bIsDeflated = false; bool bIsDeflated = false;
if( m_pStream && m_pStream->m_pDict && if( m_pStream && m_pStream->m_pDict &&
...@@ -658,12 +658,11 @@ bool PDFObject::getDeflatedStream( char** ppStream, unsigned int* pBytes, const ...@@ -658,12 +658,11 @@ bool PDFObject::getDeflatedStream( char** ppStream, unsigned int* pBytes, const
) )
{ {
unsigned int nOuterStreamLen = m_pStream->m_nEndOffset - m_pStream->m_nBeginOffset; unsigned int nOuterStreamLen = m_pStream->m_nEndOffset - m_pStream->m_nBeginOffset;
*ppStream = static_cast<char*>(std::malloc( nOuterStreamLen )); rpStream.reset(new char[ nOuterStreamLen ]);
unsigned int nRead = rContext.readOrigBytes( m_pStream->m_nBeginOffset, nOuterStreamLen, *ppStream ); unsigned int nRead = rContext.readOrigBytes( m_pStream->m_nBeginOffset, nOuterStreamLen, rpStream.get() );
if( nRead != nOuterStreamLen ) if( nRead != nOuterStreamLen )
{ {
std::free( *ppStream ); rpStream.reset();
*ppStream = nullptr;
*pBytes = 0; *pBytes = 0;
return false; return false;
} }
...@@ -689,7 +688,7 @@ bool PDFObject::getDeflatedStream( char** ppStream, unsigned int* pBytes, const ...@@ -689,7 +688,7 @@ bool PDFObject::getDeflatedStream( char** ppStream, unsigned int* pBytes, const
} }
} }
// prepare compressed data section // prepare compressed data section
char* pStream = *ppStream; char* pStream = rpStream.get();
if( pStream[0] == 's' ) if( pStream[0] == 's' )
pStream += 6; // skip "stream" pStream += 6; // skip "stream"
// skip line end after "stream" // skip line end after "stream"
...@@ -697,14 +696,14 @@ bool PDFObject::getDeflatedStream( char** ppStream, unsigned int* pBytes, const ...@@ -697,14 +696,14 @@ bool PDFObject::getDeflatedStream( char** ppStream, unsigned int* pBytes, const
pStream++; pStream++;
// get the compressed length // get the compressed length
*pBytes = m_pStream->getDictLength( pObjectContainer ); *pBytes = m_pStream->getDictLength( pObjectContainer );
if( pStream != *ppStream ) if( pStream != rpStream.get() )
memmove( *ppStream, pStream, *pBytes ); memmove( rpStream.get(), pStream, *pBytes );
if( rContext.m_bDecrypt ) if( rContext.m_bDecrypt )
{ {
EmitImplData* pEData = getEmitData( rContext ); EmitImplData* pEData = getEmitData( rContext );
pEData->decrypt( reinterpret_cast<const sal_uInt8*>(*ppStream), pEData->decrypt( reinterpret_cast<const sal_uInt8*>(rpStream.get()),
*pBytes, *pBytes,
reinterpret_cast<sal_uInt8*>(*ppStream), reinterpret_cast<sal_uInt8*>(rpStream.get()),
m_nNumber, m_nNumber,
m_nGeneration m_nGeneration
); // decrypt inplace ); // decrypt inplace
...@@ -712,7 +711,6 @@ bool PDFObject::getDeflatedStream( char** ppStream, unsigned int* pBytes, const ...@@ -712,7 +711,6 @@ bool PDFObject::getDeflatedStream( char** ppStream, unsigned int* pBytes, const
} }
else else
{ {
*ppStream = nullptr;
*pBytes = 0; *pBytes = 0;
} }
return bIsDeflated; return bIsDeflated;
...@@ -769,19 +767,18 @@ void PDFObject::writeStream( EmitContext& rWriteContext, const PDFFile* pParsedF ...@@ -769,19 +767,18 @@ void PDFObject::writeStream( EmitContext& rWriteContext, const PDFFile* pParsedF
{ {
if( m_pStream ) if( m_pStream )
{ {
char* pStream = nullptr; std::unique_ptr<char[]> pStream;
unsigned int nBytes = 0; unsigned int nBytes = 0;
if( getDeflatedStream( &pStream, &nBytes, pParsedFile, rWriteContext ) && nBytes && rWriteContext.m_bDeflate ) if( getDeflatedStream( pStream, &nBytes, pParsedFile, rWriteContext ) && nBytes && rWriteContext.m_bDeflate )
{ {
sal_uInt8* pOutBytes = nullptr; sal_uInt8* pOutBytes = nullptr;
sal_uInt32 nOutBytes = 0; sal_uInt32 nOutBytes = 0;
unzipToBuffer( pStream, nBytes, &pOutBytes, &nOutBytes ); unzipToBuffer( pStream.get(), nBytes, &pOutBytes, &nOutBytes );
rWriteContext.write( pOutBytes, nOutBytes ); rWriteContext.write( pOutBytes, nOutBytes );
std::free( pOutBytes ); std::free( pOutBytes );
} }
else if( pStream && nBytes ) else if( pStream && nBytes )
rWriteContext.write( pStream, nBytes ); rWriteContext.write( pStream.get(), nBytes );
std::free( pStream );
} }
} }
...@@ -806,20 +803,20 @@ bool PDFObject::emit( EmitContext& rWriteContext ) const ...@@ -806,20 +803,20 @@ bool PDFObject::emit( EmitContext& rWriteContext ) const
pEData->setDecryptObject( m_nNumber, m_nGeneration ); pEData->setDecryptObject( m_nNumber, m_nGeneration );
if( (rWriteContext.m_bDeflate || rWriteContext.m_bDecrypt) && pEData ) if( (rWriteContext.m_bDeflate || rWriteContext.m_bDecrypt) && pEData )
{ {
char* pStream = nullptr; std::unique_ptr<char[]> pStream;
unsigned int nBytes = 0; unsigned int nBytes = 0;
bool bDeflate = getDeflatedStream( &pStream, &nBytes, pEData->m_pObjectContainer, rWriteContext ); bool bDeflate = getDeflatedStream( pStream, &nBytes, pEData->m_pObjectContainer, rWriteContext );
if( pStream && nBytes ) if( pStream && nBytes )
{ {
// unzip the stream // unzip the stream
sal_uInt8* pOutBytes = nullptr; sal_uInt8* pOutBytes = nullptr;
sal_uInt32 nOutBytes = 0; sal_uInt32 nOutBytes = 0;
if( bDeflate && rWriteContext.m_bDeflate ) if( bDeflate && rWriteContext.m_bDeflate )
unzipToBuffer( pStream, nBytes, &pOutBytes, &nOutBytes ); unzipToBuffer( pStream.get(), nBytes, &pOutBytes, &nOutBytes );
else else
{ {
// nothing to deflate, but decryption has happened // nothing to deflate, but decryption has happened
pOutBytes = reinterpret_cast<sal_uInt8*>(pStream); pOutBytes = reinterpret_cast<sal_uInt8*>(pStream.get());
nOutBytes = static_cast<sal_uInt32>(nBytes); nOutBytes = static_cast<sal_uInt32>(nBytes);
} }
...@@ -873,16 +870,14 @@ bool PDFObject::emit( EmitContext& rWriteContext ) const ...@@ -873,16 +870,14 @@ bool PDFObject::emit( EmitContext& rWriteContext ) const
bRet = rWriteContext.write( pOutBytes, nOutBytes ); bRet = rWriteContext.write( pOutBytes, nOutBytes );
if( bRet ) if( bRet )
bRet = rWriteContext.write( "\nendstream\nendobj\n", 18 ); bRet = rWriteContext.write( "\nendstream\nendobj\n", 18 );
if( pOutBytes != reinterpret_cast<sal_uInt8*>(pStream) ) if( pOutBytes != reinterpret_cast<sal_uInt8*>(pStream.get()) )
std::free( pOutBytes ); std::free( pOutBytes );
std::free( pStream );
pEData->setDecryptObject( 0, 0 ); pEData->setDecryptObject( 0, 0 );
return bRet; return bRet;
} }
if( pOutBytes != reinterpret_cast<sal_uInt8*>(pStream) ) if( pOutBytes != reinterpret_cast<sal_uInt8*>(pStream.get()) )
std::free( pOutBytes ); std::free( pOutBytes );
} }
std::free( pStream );
} }
bool bRet = emitSubElements( rWriteContext ) && bool bRet = emitSubElements( rWriteContext ) &&
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment