From e0b592236db902e3e8cbca7ec64f8e2b192e1935 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Wed, 12 Apr 2017 16:50:51 -0700 Subject: Refcount CPDF_CryptoHandler Avoid tracking ownership via m_bLocalCryptoHandler. Also remove m_bEncryptCloned, as it is always false. Replace some methods with direct calls to underlying code. Change-Id: Ifa9d6f721c59d07e3b8e258f76832ca9f2ea0fc9 Reviewed-on: https://pdfium-review.googlesource.com/4111 Reviewed-by: Lei Zhang Commit-Queue: Lei Zhang --- core/fpdfapi/edit/cpdf_creator.h | 6 +----- core/fpdfapi/edit/fpdf_edit_create.cpp | 36 +++++++++------------------------- 2 files changed, 10 insertions(+), 32 deletions(-) (limited to 'core/fpdfapi/edit') diff --git a/core/fpdfapi/edit/cpdf_creator.h b/core/fpdfapi/edit/cpdf_creator.h index 0045a5929f..d3f18a82ab 100644 --- a/core/fpdfapi/edit/cpdf_creator.h +++ b/core/fpdfapi/edit/cpdf_creator.h @@ -44,7 +44,6 @@ class CPDF_Creator { friend class CPDF_XRefStream; bool Create(uint32_t flags); - void ResetStandardSecurity(); void Clear(); void InitOldObjNumOffsets(); @@ -80,10 +79,7 @@ class CPDF_Creator { bool m_bSecurityChanged; CPDF_Dictionary* m_pEncryptDict; uint32_t m_dwEncryptObjNum; - bool m_bEncryptCloned; - CPDF_CryptoHandler* m_pCryptoHandler; - // Whether this owns the crypto handler |m_pCryptoHandler|. - bool m_bLocalCryptoHandler; + CFX_RetainPtr m_pCryptoHandler; CPDF_Object* m_pMetadata; std::unique_ptr m_pXRefStream; int32_t m_ObjectStreamSize; diff --git a/core/fpdfapi/edit/fpdf_edit_create.cpp b/core/fpdfapi/edit/fpdf_edit_create.cpp index d08e4b5302..f643d3f24e 100644 --- a/core/fpdfapi/edit/fpdf_edit_create.cpp +++ b/core/fpdfapi/edit/fpdf_edit_create.cpp @@ -577,7 +577,7 @@ FX_FILESIZE CPDF_ObjectStream::End(CPDF_Creator* pCreator) { tempBuffer << m_Buffer; CPDF_FlateEncoder encoder(tempBuffer.GetBuffer(), tempBuffer.GetLength(), true, false); - CPDF_Encryptor encryptor(pCreator->m_pCryptoHandler, m_dwObjNum, + CPDF_Encryptor encryptor(pCreator->m_pCryptoHandler.Get(), m_dwObjNum, encoder.m_pData.Get(), encoder.m_dwSize); if ((len = pFile->AppendDWord(encryptor.m_dwSize)) < 0) { return -1; @@ -880,9 +880,7 @@ CPDF_Creator::CPDF_Creator(CPDF_Document* pDoc) m_bSecurityChanged(false), m_pEncryptDict(m_pParser ? m_pParser->GetEncryptDict() : nullptr), m_dwEncryptObjNum(0), - m_bEncryptCloned(false), m_pCryptoHandler(m_pParser ? m_pParser->GetCryptoHandler() : nullptr), - m_bLocalCryptoHandler(false), m_pMetadata(nullptr), m_ObjectStreamSize(200), m_dwLastObjNum(m_pDocument->GetLastObjNum()), @@ -896,11 +894,6 @@ CPDF_Creator::CPDF_Creator(CPDF_Document* pDoc) m_FileVersion(0) {} CPDF_Creator::~CPDF_Creator() { - ResetStandardSecurity(); - if (m_bEncryptCloned) { - delete m_pEncryptDict; - m_pEncryptDict = nullptr; - } Clear(); } @@ -1025,7 +1018,7 @@ int32_t CPDF_Creator::WriteIndirectObj(uint32_t objnum, m_Offset += len; if (pObj->IsStream()) { CPDF_CryptoHandler* pHandler = - pObj != m_pMetadata ? m_pCryptoHandler : nullptr; + pObj != m_pMetadata ? m_pCryptoHandler.Get() : nullptr; if (WriteStream(pObj, objnum, pHandler) < 0) return -1; } else { @@ -1086,8 +1079,8 @@ int32_t CPDF_Creator::WriteDirectObj(uint32_t objnum, m_Offset += len; break; } - CPDF_Encryptor encryptor(m_pCryptoHandler, objnum, (uint8_t*)str.c_str(), - str.GetLength()); + CPDF_Encryptor encryptor(m_pCryptoHandler.Get(), objnum, + (uint8_t*)str.c_str(), str.GetLength()); CFX_ByteString content = PDF_EncodeString( CFX_ByteString((const char*)encryptor.m_pData, encryptor.m_dwSize), bHex); @@ -1100,8 +1093,8 @@ int32_t CPDF_Creator::WriteDirectObj(uint32_t objnum, case CPDF_Object::STREAM: { CPDF_FlateEncoder encoder(const_cast(pObj->AsStream()), true); - CPDF_Encryptor encryptor(m_pCryptoHandler, objnum, encoder.m_pData.Get(), - encoder.m_dwSize); + CPDF_Encryptor encryptor(m_pCryptoHandler.Get(), objnum, + encoder.m_pData.Get(), encoder.m_dwSize); if (static_cast(encoder.m_pDict->GetIntegerFor("Length")) != encryptor.m_dwSize) { encoder.CloneDict(); @@ -1860,11 +1853,8 @@ void CPDF_Creator::InitID(bool bDefault) { CPDF_SecurityHandler handler; handler.OnCreate(m_pEncryptDict, m_pIDArray.get(), user_pass.raw_str(), user_pass.GetLength(), flag); - if (m_bLocalCryptoHandler) - delete m_pCryptoHandler; - m_pCryptoHandler = new CPDF_CryptoHandler; + m_pCryptoHandler = pdfium::MakeRetain(); m_pCryptoHandler->Init(m_pEncryptDict, &handler); - m_bLocalCryptoHandler = true; m_bSecurityChanged = true; } } @@ -1902,17 +1892,9 @@ bool CPDF_Creator::SetFileVersion(int32_t fileVersion) { m_FileVersion = fileVersion; return true; } + void CPDF_Creator::RemoveSecurity() { - ResetStandardSecurity(); + m_pCryptoHandler.Reset(); m_bSecurityChanged = true; m_pEncryptDict = nullptr; - m_pCryptoHandler = nullptr; -} -void CPDF_Creator::ResetStandardSecurity() { - if (!m_bLocalCryptoHandler) - return; - - delete m_pCryptoHandler; - m_pCryptoHandler = nullptr; - m_bLocalCryptoHandler = false; } -- cgit v1.2.3