Chromium Code Reviews| Index: core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp |
| diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp |
| index dd416fbd57a256f5441153b46ea6c52efc9c4c52..ac296d843bbe2932d081a59c3556fbf53cd61cbd 100644 |
| --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp |
| +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp |
| @@ -96,7 +96,6 @@ CPDF_Parser::CPDF_Parser() { |
| m_pDocument = NULL; |
| m_pTrailer = NULL; |
| m_pEncryptDict = NULL; |
| - m_pSecurityHandler = NULL; |
| m_pLinearized = NULL; |
| m_dwFirstPageNo = 0; |
| m_dwXrefStartObjNum = 0; |
| @@ -293,48 +292,40 @@ FX_DWORD CPDF_Parser::SetEncryptHandler() { |
| if (!m_pSecurityHandler->OnInit(this, m_pEncryptDict)) { |
| return err; |
| } |
| - CPDF_CryptoHandler* pCryptoHandler = |
| - m_pSecurityHandler->CreateCryptoHandler(); |
| - if (!pCryptoHandler->Init(m_pEncryptDict, m_pSecurityHandler)) { |
| - delete pCryptoHandler; |
| - pCryptoHandler = NULL; |
| + nonstd::unique_ptr<CPDF_CryptoHandler> pCryptoHandler( |
| + m_pSecurityHandler->CreateCryptoHandler()); |
| + if (!pCryptoHandler->Init(m_pEncryptDict, m_pSecurityHandler.get())) { |
| return PDFPARSE_ERROR_HANDLER; |
| } |
| - m_Syntax.SetEncrypt(pCryptoHandler); |
| + m_Syntax.SetEncrypt(pCryptoHandler.release()); |
| } else if (m_pEncryptDict) { |
| CFX_ByteString filter = m_pEncryptDict->GetString(FX_BSTRC("Filter")); |
| - CPDF_SecurityHandler* pSecurityHandler = NULL; |
| + nonstd::unique_ptr<CPDF_SecurityHandler> pSecurityHandler; |
| FX_DWORD err = PDFPARSE_ERROR_HANDLER; |
| if (filter == FX_BSTRC("Standard")) { |
| - pSecurityHandler = FPDF_CreateStandardSecurityHandler(); |
| + pSecurityHandler.reset(FPDF_CreateStandardSecurityHandler()); |
| err = PDFPARSE_ERROR_PASSWORD; |
| } |
| if (pSecurityHandler == NULL) { |
|
Lei Zhang
2015/10/23 23:41:44
!pSecurityHandler
Oliver Chang
2015/10/24 00:23:35
Done, and replaced all other similar instances of
|
| return PDFPARSE_ERROR_HANDLER; |
| } |
| if (!pSecurityHandler->OnInit(this, m_pEncryptDict)) { |
| - delete pSecurityHandler; |
| - pSecurityHandler = NULL; |
| return err; |
| } |
| - m_pSecurityHandler = pSecurityHandler; |
| - CPDF_CryptoHandler* pCryptoHandler = |
| - pSecurityHandler->CreateCryptoHandler(); |
| - if (!pCryptoHandler->Init(m_pEncryptDict, m_pSecurityHandler)) { |
| - delete pCryptoHandler; |
| - pCryptoHandler = NULL; |
| + m_pSecurityHandler = nonstd::move(pSecurityHandler); |
| + nonstd::unique_ptr<CPDF_CryptoHandler> pCryptoHandler( |
| + pSecurityHandler->CreateCryptoHandler()); |
| + if (!pCryptoHandler->Init(m_pEncryptDict, m_pSecurityHandler.get())) { |
| return PDFPARSE_ERROR_HANDLER; |
| } |
| - m_Syntax.SetEncrypt(pCryptoHandler); |
| + m_Syntax.SetEncrypt(pCryptoHandler.release()); |
| } |
| return PDFPARSE_ERROR_SUCCESS; |
| } |
| void CPDF_Parser::ReleaseEncryptHandler() { |
| - delete m_Syntax.m_pCryptoHandler; |
| - m_Syntax.m_pCryptoHandler = NULL; |
| + m_Syntax.m_pCryptoHandler.reset(nullptr); |
|
Lei Zhang
2015/10/23 23:41:44
just reset(), ditto below
Oliver Chang
2015/10/24 00:23:35
Done.
|
| if (!m_bForceUseSecurityHandler) { |
| - delete m_pSecurityHandler; |
| - m_pSecurityHandler = NULL; |
| + m_pSecurityHandler.reset(nullptr); |
| } |
| } |
| FX_FILESIZE CPDF_Parser::GetObjectOffset(FX_DWORD objnum) { |
| @@ -448,23 +439,21 @@ FX_BOOL CPDF_Parser::LoadLinearizedCrossRefV4(FX_FILESIZE pos, |
| FX_DWORD count = dwObjCount; |
| FX_FILESIZE SavedPos = m_Syntax.SavePos(); |
| int32_t recordsize = 20; |
|
Lei Zhang
2015/10/23 23:41:44
If you make this a constant, I think |buf| can jus
Oliver Chang
2015/10/24 00:23:35
having a 20K stack allocated buffer feels a bit sc
Lei Zhang
2015/10/24 00:26:04
Fair enough. Can we make |recordsize| a const at l
Oliver Chang
2015/10/24 00:28:59
Done.
|
| - char* pBuf = FX_Alloc(char, 1024 * recordsize + 1); |
| - pBuf[1024 * recordsize] = '\0'; |
| + std::vector<char> buf(1024 * recordsize + 1); |
| + buf[1024 * recordsize] = '\0'; |
| int32_t nBlocks = count / 1024 + 1; |
| for (int32_t block = 0; block < nBlocks; block++) { |
| int32_t block_size = block == nBlocks - 1 ? count % 1024 : 1024; |
| FX_DWORD dwReadSize = block_size * recordsize; |
| if ((FX_FILESIZE)(dwStartPos + dwReadSize) > m_Syntax.m_FileLen) { |
| - FX_Free(pBuf); |
| return FALSE; |
| } |
| - if (!m_Syntax.ReadBlock((uint8_t*)pBuf, dwReadSize)) { |
| - FX_Free(pBuf); |
| + if (!m_Syntax.ReadBlock((uint8_t*)buf.data(), dwReadSize)) { |
| return FALSE; |
| } |
| for (int32_t i = 0; i < block_size; i++) { |
| FX_DWORD objnum = start_objnum + block * 1024 + i; |
| - char* pEntry = pBuf + i * recordsize; |
| + char* pEntry = buf.data() + i * recordsize; |
| if (pEntry[17] == 'f') { |
| m_CrossRef.SetAtGrow(objnum, 0); |
| m_V5Type.SetAtGrow(objnum, 0); |
| @@ -473,7 +462,6 @@ FX_BOOL CPDF_Parser::LoadLinearizedCrossRefV4(FX_FILESIZE pos, |
| if (offset == 0) { |
| for (int32_t c = 0; c < 10; c++) { |
| if (pEntry[c] < '0' || pEntry[c] > '9') { |
| - FX_Free(pBuf); |
| return FALSE; |
| } |
| } |
| @@ -496,7 +484,6 @@ FX_BOOL CPDF_Parser::LoadLinearizedCrossRefV4(FX_FILESIZE pos, |
| } |
| } |
| } |
| - FX_Free(pBuf); |
| m_Syntax.RestorePos(SavedPos + count * recordsize); |
| return TRUE; |
| } |
| @@ -544,16 +531,16 @@ bool CPDF_Parser::LoadCrossRefV4(FX_FILESIZE pos, |
| bFirstItem = TRUE; |
| m_dwXrefStartObjNum = start_objnum; |
| if (!bSkip) { |
| - char* pBuf = FX_Alloc(char, 1024 * recordsize + 1); |
| - pBuf[1024 * recordsize] = '\0'; |
| + std::vector<char> buf(1024 * recordsize + 1); |
| + buf[1024 * recordsize] = '\0'; |
| int32_t nBlocks = count / 1024 + 1; |
| FX_BOOL bFirstBlock = TRUE; |
| for (int32_t block = 0; block < nBlocks; block++) { |
| int32_t block_size = block == nBlocks - 1 ? count % 1024 : 1024; |
| - m_Syntax.ReadBlock((uint8_t*)pBuf, block_size * recordsize); |
| + m_Syntax.ReadBlock((uint8_t*)buf.data(), block_size * recordsize); |
| for (int32_t i = 0; i < block_size; i++) { |
| FX_DWORD objnum = start_objnum + block * 1024 + i; |
| - char* pEntry = pBuf + i * recordsize; |
| + char* pEntry = buf.data() + i * recordsize; |
| if (pEntry[17] == 'f') { |
| if (bFirstItem) { |
| objnum = 0; |
| @@ -574,7 +561,6 @@ bool CPDF_Parser::LoadCrossRefV4(FX_FILESIZE pos, |
| if (offset == 0) { |
| for (int32_t c = 0; c < 10; c++) { |
| if (pEntry[c] < '0' || pEntry[c] > '9') { |
| - FX_Free(pBuf); |
| return false; |
| } |
| } |
| @@ -596,7 +582,6 @@ bool CPDF_Parser::LoadCrossRefV4(FX_FILESIZE pos, |
| } |
| } |
| } |
| - FX_Free(pBuf); |
| } |
| m_Syntax.RestorePos(SavedPos + count * recordsize); |
| } |
| @@ -1496,16 +1481,12 @@ FX_BOOL CPDF_Parser::IsOwner() { |
| void CPDF_Parser::SetSecurityHandler(CPDF_SecurityHandler* pSecurityHandler, |
| FX_BOOL bForced) { |
| ASSERT(m_pSecurityHandler == NULL); |
|
Lei Zhang
2015/10/23 23:41:44
This is a weird assert to have considering the nex
Oliver Chang
2015/10/24 00:23:35
removed
|
| - if (!m_bForceUseSecurityHandler) { |
| - delete m_pSecurityHandler; |
| - m_pSecurityHandler = NULL; |
| - } |
| m_bForceUseSecurityHandler = bForced; |
| - m_pSecurityHandler = pSecurityHandler; |
| + m_pSecurityHandler.reset(pSecurityHandler); |
| if (m_bForceUseSecurityHandler) { |
| return; |
| } |
| - m_Syntax.m_pCryptoHandler = pSecurityHandler->CreateCryptoHandler(); |
| + m_Syntax.m_pCryptoHandler.reset(pSecurityHandler->CreateCryptoHandler()); |
| m_Syntax.m_pCryptoHandler->Init(NULL, pSecurityHandler); |
| } |
| FX_BOOL CPDF_Parser::IsLinearizedFile(IFX_FileRead* pFileAccess, |
| @@ -1704,7 +1685,6 @@ int CPDF_SyntaxParser::s_CurrentRecursionDepth = 0; |
| CPDF_SyntaxParser::CPDF_SyntaxParser() { |
| m_pFileAccess = NULL; |
| - m_pCryptoHandler = NULL; |
| m_pFileBuf = NULL; |
| m_BufSize = CPDF_ModuleMgr::kFileBufSize; |
| m_pFileBuf = NULL; |
| @@ -2409,7 +2389,7 @@ CPDF_Stream* CPDF_SyntaxParser::ReadStream(CPDF_Dictionary* pDict, |
| const unsigned int ENDSTREAM_LEN = sizeof("endstream") - 1; |
| const unsigned int ENDOBJ_LEN = sizeof("endobj") - 1; |
| CPDF_CryptoHandler* pCryptoHandler = |
| - objnum == (FX_DWORD)m_MetadataObjnum ? nullptr : m_pCryptoHandler; |
| + objnum == (FX_DWORD)m_MetadataObjnum ? nullptr : m_pCryptoHandler.get(); |
| if (!pCryptoHandler) { |
| FX_BOOL bSearchForKeyword = TRUE; |
| if (len >= 0) { |