diff options
author | tsepez <tsepez@chromium.org> | 2016-12-07 09:21:17 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-12-07 09:21:18 -0800 |
commit | 833619b4441915c7c55085d44b3221eaef0d9800 (patch) | |
tree | 68bf76e83078223ba03f490c2c13f484e40154d4 /core/fpdfapi | |
parent | 8f875507a986d10335e40a5f7c1679aff9770d0a (diff) | |
download | pdfium-833619b4441915c7c55085d44b3221eaef0d9800.tar.xz |
Refcount all the IFX_ stream classes all the time.
We can remove a lot of "bOwnsStream" logic in the process.
Always pass these by const reference, in case the called method
wants to hang on to the stream (one exception is where we stick
a raw pointer into a void* slot in a context from another layer).
Review-Url: https://codereview.chromium.org/2451493002
Diffstat (limited to 'core/fpdfapi')
-rw-r--r-- | core/fpdfapi/edit/cpdf_creator.h | 3 | ||||
-rw-r--r-- | core/fpdfapi/edit/fpdf_edit_create.cpp | 11 | ||||
-rw-r--r-- | core/fpdfapi/page/cpdf_image.cpp | 3 | ||||
-rw-r--r-- | core/fpdfapi/page/cpdf_image.h | 2 | ||||
-rw-r--r-- | core/fpdfapi/parser/cfdf_document.cpp | 26 | ||||
-rw-r--r-- | core/fpdfapi/parser/cfdf_document.h | 11 | ||||
-rw-r--r-- | core/fpdfapi/parser/cpdf_data_avail.cpp | 32 | ||||
-rw-r--r-- | core/fpdfapi/parser/cpdf_data_avail.h | 8 | ||||
-rw-r--r-- | core/fpdfapi/parser/cpdf_parser.cpp | 47 | ||||
-rw-r--r-- | core/fpdfapi/parser/cpdf_parser.h | 12 | ||||
-rw-r--r-- | core/fpdfapi/parser/cpdf_parser_unittest.cpp | 20 | ||||
-rw-r--r-- | core/fpdfapi/parser/cpdf_stream.cpp | 5 | ||||
-rw-r--r-- | core/fpdfapi/parser/cpdf_stream.h | 4 | ||||
-rw-r--r-- | core/fpdfapi/parser/cpdf_syntax_parser.cpp | 5 | ||||
-rw-r--r-- | core/fpdfapi/parser/cpdf_syntax_parser.h | 6 | ||||
-rw-r--r-- | core/fpdfapi/parser/cpdf_syntax_parser_unittest.cpp | 52 | ||||
-rw-r--r-- | core/fpdfapi/parser/fpdf_parser_utility.cpp | 2 | ||||
-rw-r--r-- | core/fpdfapi/parser/fpdf_parser_utility.h | 3 |
18 files changed, 115 insertions, 137 deletions
diff --git a/core/fpdfapi/edit/cpdf_creator.h b/core/fpdfapi/edit/cpdf_creator.h index c04db6eb37..aeb7d6675c 100644 --- a/core/fpdfapi/edit/cpdf_creator.h +++ b/core/fpdfapi/edit/cpdf_creator.h @@ -9,6 +9,7 @@ #include <memory> +#include "core/fxcrt/cfx_retain_ptr.h" #include "core/fxcrt/fx_basic.h" class CPDF_Array; @@ -32,7 +33,7 @@ class CPDF_Creator { ~CPDF_Creator(); void RemoveSecurity(); - bool Create(IFX_WriteStream* pFile, uint32_t flags = 0); + bool Create(const CFX_RetainPtr<IFX_WriteStream>& pFile, uint32_t flags = 0); int32_t Continue(IFX_Pause* pPause = nullptr); bool SetFileVersion(int32_t fileVersion = 17); diff --git a/core/fpdfapi/edit/fpdf_edit_create.cpp b/core/fpdfapi/edit/fpdf_edit_create.cpp index b8757ae903..62314f43f5 100644 --- a/core/fpdfapi/edit/fpdf_edit_create.cpp +++ b/core/fpdfapi/edit/fpdf_edit_create.cpp @@ -1479,7 +1479,8 @@ int32_t CPDF_Creator::WriteDoc_Stage1(IFX_Pause* pPause) { InitOldObjNumOffsets(); m_iStage = 20; } else { - IFX_SeekableReadStream* pSrcFile = m_pParser->GetFileAccess(); + CFX_RetainPtr<IFX_SeekableReadStream> pSrcFile = + m_pParser->GetFileAccess(); m_Offset = pSrcFile->GetSize(); m_Pos = (void*)(uintptr_t)m_Offset; m_iStage = 15; @@ -1487,8 +1488,9 @@ int32_t CPDF_Creator::WriteDoc_Stage1(IFX_Pause* pPause) { } if (m_iStage == 15) { if ((m_dwFlags & FPDFCREATE_NO_ORIGINAL) == 0 && m_Pos) { - IFX_SeekableReadStream* pSrcFile = m_pParser->GetFileAccess(); - uint8_t buffer[4096]; + CFX_RetainPtr<IFX_SeekableReadStream> pSrcFile = + m_pParser->GetFileAccess(); + uint8_t buffer[4096]; // TODO(tsepez): don't stack allocate. uint32_t src_size = (uint32_t)(uintptr_t)m_Pos; while (src_size) { uint32_t block_size = src_size > 4096 ? 4096 : src_size; @@ -1904,7 +1906,8 @@ void CPDF_Creator::Clear() { m_pIDArray.reset(); } -bool CPDF_Creator::Create(IFX_WriteStream* pFile, uint32_t flags) { +bool CPDF_Creator::Create(const CFX_RetainPtr<IFX_WriteStream>& pFile, + uint32_t flags) { m_File.AttachFile(pFile); return Create(flags); } diff --git a/core/fpdfapi/page/cpdf_image.cpp b/core/fpdfapi/page/cpdf_image.cpp index 5d1d51de89..a17222e343 100644 --- a/core/fpdfapi/page/cpdf_image.cpp +++ b/core/fpdfapi/page/cpdf_image.cpp @@ -116,7 +116,8 @@ std::unique_ptr<CPDF_Dictionary> CPDF_Image::InitJPEG(uint8_t* pData, return pDict; } -void CPDF_Image::SetJpegImage(IFX_SeekableReadStream* pFile) { +void CPDF_Image::SetJpegImage( + const CFX_RetainPtr<IFX_SeekableReadStream>& pFile) { uint32_t size = pdfium::base::checked_cast<uint32_t>(pFile->GetSize()); if (!size) return; diff --git a/core/fpdfapi/page/cpdf_image.h b/core/fpdfapi/page/cpdf_image.h index 38c7e3e728..1b0d74f123 100644 --- a/core/fpdfapi/page/cpdf_image.h +++ b/core/fpdfapi/page/cpdf_image.h @@ -51,7 +51,7 @@ class CPDF_Image { bool bLoadMask = false) const; void SetImage(const CFX_DIBitmap* pDIBitmap); - void SetJpegImage(IFX_SeekableReadStream* pFile); + void SetJpegImage(const CFX_RetainPtr<IFX_SeekableReadStream>& pFile); void ResetCache(CPDF_Page* pPage, const CFX_DIBitmap* pDIBitmap); diff --git a/core/fpdfapi/parser/cfdf_document.cpp b/core/fpdfapi/parser/cfdf_document.cpp index a6045e7795..65755520c0 100644 --- a/core/fpdfapi/parser/cfdf_document.cpp +++ b/core/fpdfapi/parser/cfdf_document.cpp @@ -15,15 +15,9 @@ #include "third_party/base/ptr_util.h" CFDF_Document::CFDF_Document() - : CPDF_IndirectObjectHolder(), - m_pRootDict(nullptr), - m_pFile(nullptr), - m_bOwnFile(false) {} - -CFDF_Document::~CFDF_Document() { - if (m_bOwnFile && m_pFile) - m_pFile->Release(); -} + : CPDF_IndirectObjectHolder(), m_pRootDict(nullptr) {} + +CFDF_Document::~CFDF_Document() {} std::unique_ptr<CFDF_Document> CFDF_Document::CreateNewDoc() { auto pDoc = pdfium::MakeUnique<CFDF_Document>(); @@ -33,25 +27,23 @@ std::unique_ptr<CFDF_Document> CFDF_Document::CreateNewDoc() { } std::unique_ptr<CFDF_Document> CFDF_Document::ParseFile( - IFX_SeekableReadStream* pFile, - bool bOwnFile) { + const CFX_RetainPtr<IFX_SeekableReadStream>& pFile) { if (!pFile) return nullptr; auto pDoc = pdfium::MakeUnique<CFDF_Document>(); - pDoc->ParseStream(pFile, bOwnFile); + pDoc->ParseStream(pFile); return pDoc->m_pRootDict ? std::move(pDoc) : nullptr; } -std::unique_ptr<CFDF_Document> CFDF_Document::ParseMemory(const uint8_t* pData, +std::unique_ptr<CFDF_Document> CFDF_Document::ParseMemory(uint8_t* pData, uint32_t size) { - return CFDF_Document::ParseFile( - IFX_MemoryStream::Create((uint8_t*)pData, size), true); + return CFDF_Document::ParseFile(IFX_MemoryStream::Create(pData, size)); } -void CFDF_Document::ParseStream(IFX_SeekableReadStream* pFile, bool bOwnFile) { +void CFDF_Document::ParseStream( + const CFX_RetainPtr<IFX_SeekableReadStream>& pFile) { m_pFile = pFile; - m_bOwnFile = bOwnFile; CPDF_SyntaxParser parser; parser.InitParser(m_pFile, 0); while (1) { diff --git a/core/fpdfapi/parser/cfdf_document.h b/core/fpdfapi/parser/cfdf_document.h index 1b47368458..f83adc1a30 100644 --- a/core/fpdfapi/parser/cfdf_document.h +++ b/core/fpdfapi/parser/cfdf_document.h @@ -18,9 +18,9 @@ class CPDF_Dictionary; class CFDF_Document : public CPDF_IndirectObjectHolder { public: static std::unique_ptr<CFDF_Document> CreateNewDoc(); - static std::unique_ptr<CFDF_Document> ParseFile(IFX_SeekableReadStream* pFile, - bool bOwnFile = false); - static std::unique_ptr<CFDF_Document> ParseMemory(const uint8_t* pData, + static std::unique_ptr<CFDF_Document> ParseFile( + const CFX_RetainPtr<IFX_SeekableReadStream>& pFile); + static std::unique_ptr<CFDF_Document> ParseMemory(uint8_t* pData, uint32_t size); CFDF_Document(); @@ -30,11 +30,10 @@ class CFDF_Document : public CPDF_IndirectObjectHolder { CPDF_Dictionary* GetRoot() const { return m_pRootDict; } protected: - void ParseStream(IFX_SeekableReadStream* pFile, bool bOwnFile); + void ParseStream(const CFX_RetainPtr<IFX_SeekableReadStream>& pFile); CPDF_Dictionary* m_pRootDict; - IFX_SeekableReadStream* m_pFile; - bool m_bOwnFile; + CFX_RetainPtr<IFX_SeekableReadStream> m_pFile; }; #endif // CORE_FPDFAPI_PARSER_CFDF_DOCUMENT_H_ diff --git a/core/fpdfapi/parser/cpdf_data_avail.cpp b/core/fpdfapi/parser/cpdf_data_avail.cpp index 248748470c..76ea73d80f 100644 --- a/core/fpdfapi/parser/cpdf_data_avail.cpp +++ b/core/fpdfapi/parser/cpdf_data_avail.cpp @@ -33,9 +33,10 @@ CPDF_DataAvail::DownloadHints::~DownloadHints() {} // static int CPDF_DataAvail::s_CurrentDataAvailRecursionDepth = 0; -CPDF_DataAvail::CPDF_DataAvail(FileAvail* pFileAvail, - IFX_SeekableReadStream* pFileRead, - bool bSupportHintTable) +CPDF_DataAvail::CPDF_DataAvail( + FileAvail* pFileAvail, + const CFX_RetainPtr<IFX_SeekableReadStream>& pFileRead, + bool bSupportHintTable) : m_pFileAvail(pFileAvail), m_pFileRead(pFileRead) { m_Pos = 0; m_dwFileLen = 0; @@ -72,7 +73,6 @@ CPDF_DataAvail::CPDF_DataAvail(FileAvail* pFileAvail, m_pPageDict = nullptr; m_pPageResource = nullptr; m_docStatus = PDF_DATAAVAIL_HEADER; - m_parser.m_bOwnFileRead = false; m_bTotalLoadPageTree = false; m_bCurPageDictLoadOK = false; m_bLinearedDataOK = false; @@ -320,7 +320,6 @@ bool CPDF_DataAvail::LoadAllFile(DownloadHints* pHints) { bool CPDF_DataAvail::LoadAllXref(DownloadHints* pHints) { m_parser.m_pSyntax->InitParser(m_pFileRead, (uint32_t)m_dwHeaderOffset); - m_parser.m_bOwnFileRead = false; if (!m_parser.LoadAllCrossRefV4(m_dwLastXRefOffset) && !m_parser.LoadAllCrossRefV5(m_dwLastXRefOffset)) { m_docStatus = PDF_DATAAVAIL_LOADALLFILE; @@ -718,15 +717,16 @@ bool CPDF_DataAvail::IsLinearizedFile(uint8_t* pData, uint32_t dwLen) { if (m_pLinearized) return true; - ScopedFileStream file(IFX_MemoryStream::Create(pData, (size_t)dwLen, false)); - int32_t offset = GetHeaderOffset(file.get()); + CFX_RetainPtr<IFX_MemoryStream> file = + IFX_MemoryStream::Create(pData, (size_t)dwLen, false); + int32_t offset = GetHeaderOffset(file); if (offset == -1) { m_docStatus = PDF_DATAAVAIL_ERROR; return false; } m_dwHeaderOffset = offset; - m_syntaxParser.InitParser(file.get(), offset); + m_syntaxParser.InitParser(file, offset); m_syntaxParser.RestorePos(m_syntaxParser.m_HeaderOffset + 9); bool bNumber; @@ -753,9 +753,9 @@ bool CPDF_DataAvail::CheckEnd(DownloadHints* pHints) { uint8_t buffer[1024]; m_pFileRead->ReadBlock(buffer, req_pos, dwSize); - ScopedFileStream file( - IFX_MemoryStream::Create(buffer, (size_t)dwSize, false)); - m_syntaxParser.InitParser(file.get(), 0); + CFX_RetainPtr<IFX_MemoryStream> file = + IFX_MemoryStream::Create(buffer, (size_t)dwSize, false); + m_syntaxParser.InitParser(file, 0); m_syntaxParser.RestorePos(dwSize - 1); if (m_syntaxParser.SearchWord("startxref", true, false, dwSize)) { @@ -801,8 +801,9 @@ int32_t CPDF_DataAvail::CheckCrossRefStream(DownloadHints* pHints, m_pFileRead->ReadBlock(pBuf, m_dwCurrentXRefSteam, iSize); - ScopedFileStream file(IFX_MemoryStream::Create(pBuf, (size_t)iSize, false)); - m_parser.m_pSyntax->InitParser(file.get(), 0); + CFX_RetainPtr<IFX_MemoryStream> file = + IFX_MemoryStream::Create(pBuf, (size_t)iSize, false); + m_parser.m_pSyntax->InitParser(file, 0); bool bNumber; CFX_ByteString objnum = m_parser.m_pSyntax->GetNextWord(&bNumber); @@ -1047,8 +1048,9 @@ bool CPDF_DataAvail::CheckTrailer(DownloadHints* pHints) { if (!m_pFileRead->ReadBlock(pBuf, m_dwTrailerOffset, iSize)) return false; - ScopedFileStream file(IFX_MemoryStream::Create(pBuf, (size_t)iSize, false)); - m_syntaxParser.InitParser(file.get(), 0); + CFX_RetainPtr<IFX_MemoryStream> file = + IFX_MemoryStream::Create(pBuf, (size_t)iSize, false); + m_syntaxParser.InitParser(file, 0); std::unique_ptr<CPDF_Object> pTrailer( m_syntaxParser.GetObject(nullptr, 0, 0, true)); diff --git a/core/fpdfapi/parser/cpdf_data_avail.h b/core/fpdfapi/parser/cpdf_data_avail.h index e81d705bcd..1ced2a2133 100644 --- a/core/fpdfapi/parser/cpdf_data_avail.h +++ b/core/fpdfapi/parser/cpdf_data_avail.h @@ -95,7 +95,7 @@ class CPDF_DataAvail final { }; CPDF_DataAvail(FileAvail* pFileAvail, - IFX_SeekableReadStream* pFileRead, + const CFX_RetainPtr<IFX_SeekableReadStream>& pFileRead, bool bSupportHintTable); ~CPDF_DataAvail(); @@ -107,7 +107,9 @@ class CPDF_DataAvail final { DocLinearizationStatus IsLinearizedPDF(); bool IsLinearized(); void GetLinearizedMainXRefInfo(FX_FILESIZE* pPos, uint32_t* pSize); - IFX_SeekableReadStream* GetFileRead() const { return m_pFileRead; } + CFX_RetainPtr<IFX_SeekableReadStream> GetFileRead() const { + return m_pFileRead; + } int GetPageCount() const; CPDF_Dictionary* GetPage(int index); @@ -195,7 +197,7 @@ class CPDF_DataAvail final { bool ValidateForm(); FileAvail* const m_pFileAvail; - IFX_SeekableReadStream* const m_pFileRead; + CFX_RetainPtr<IFX_SeekableReadStream> m_pFileRead; CPDF_Parser m_parser; CPDF_SyntaxParser m_syntaxParser; std::unique_ptr<CPDF_Object> m_pRoot; diff --git a/core/fpdfapi/parser/cpdf_parser.cpp b/core/fpdfapi/parser/cpdf_parser.cpp index 1fae0d641c..47c5c4eab4 100644 --- a/core/fpdfapi/parser/cpdf_parser.cpp +++ b/core/fpdfapi/parser/cpdf_parser.cpp @@ -52,7 +52,6 @@ int32_t GetStreamFirst(CPDF_StreamAcc* pObjStream) { CPDF_Parser::CPDF_Parser() : m_pDocument(nullptr), m_bHasParsed(false), - m_bOwnFileRead(true), m_bXRefStream(false), m_bVersionUpdated(false), m_FileVersion(0), @@ -64,11 +63,6 @@ CPDF_Parser::CPDF_Parser() CPDF_Parser::~CPDF_Parser() { ReleaseEncryptHandler(); SetEncryptDictionary(nullptr); - - if (m_bOwnFileRead && m_pSyntax->m_pFileAccess) { - m_pSyntax->m_pFileAccess->Release(); - m_pSyntax->m_pFileAccess = nullptr; - } } uint32_t CPDF_Parser::GetLastObjNum() const { @@ -109,7 +103,7 @@ CPDF_CryptoHandler* CPDF_Parser::GetCryptoHandler() { return m_pSyntax->m_pCryptoHandler.get(); } -IFX_SeekableReadStream* CPDF_Parser::GetFileAccess() const { +CFX_RetainPtr<IFX_SeekableReadStream> CPDF_Parser::GetFileAccess() const { return m_pSyntax->m_pFileAccess; } @@ -129,31 +123,30 @@ void CPDF_Parser::ShrinkObjectMap(uint32_t objnum) { m_ObjectInfo[objnum - 1].pos = 0; } -CPDF_Parser::Error CPDF_Parser::StartParse(IFX_SeekableReadStream* pFileAccess, - CPDF_Document* pDocument) { +CPDF_Parser::Error CPDF_Parser::StartParse( + const CFX_RetainPtr<IFX_SeekableReadStream>& pFileAccess, + CPDF_Document* pDocument) { ASSERT(!m_bHasParsed); m_bHasParsed = true; - m_bXRefStream = false; m_LastXRefOffset = 0; - m_bOwnFileRead = true; int32_t offset = GetHeaderOffset(pFileAccess); - if (offset == -1) { - if (pFileAccess) - pFileAccess->Release(); + if (offset == -1) return FORMAT_ERROR; - } + m_pSyntax->InitParser(pFileAccess, offset); uint8_t ch; if (!m_pSyntax->GetCharAt(5, ch)) return FORMAT_ERROR; + if (std::isdigit(ch)) m_FileVersion = FXSYS_toDecimalDigit(static_cast<FX_WCHAR>(ch)) * 10; if (!m_pSyntax->GetCharAt(7, ch)) return FORMAT_ERROR; + if (std::isdigit(ch)) m_FileVersion += FXSYS_toDecimalDigit(static_cast<FX_WCHAR>(ch)); @@ -1124,10 +1117,10 @@ std::unique_ptr<CPDF_Object> CPDF_Parser::ParseIndirectObject( if (!pObjStream) return nullptr; - ScopedFileStream file(IFX_MemoryStream::Create( - (uint8_t*)pObjStream->GetData(), (size_t)pObjStream->GetSize(), false)); + CFX_RetainPtr<IFX_MemoryStream> file = IFX_MemoryStream::Create( + (uint8_t*)pObjStream->GetData(), (size_t)pObjStream->GetSize(), false); CPDF_SyntaxParser syntax; - syntax.InitParser(file.get(), 0); + syntax.InitParser(file, 0); const int32_t offset = GetStreamFirst(pObjStream); // Read object numbers from |pObjStream| into a cache. @@ -1203,11 +1196,11 @@ void CPDF_Parser::GetIndirectBinary(uint32_t objnum, int32_t offset = GetStreamFirst(pObjStream); const uint8_t* pData = pObjStream->GetData(); uint32_t totalsize = pObjStream->GetSize(); - ScopedFileStream file( - IFX_MemoryStream::Create((uint8_t*)pData, (size_t)totalsize, false)); - + CFX_RetainPtr<IFX_MemoryStream> file = + IFX_MemoryStream::Create((uint8_t*)pData, (size_t)totalsize, false); CPDF_SyntaxParser syntax; - syntax.InitParser(file.get(), 0); + syntax.InitParser(file, 0); + for (int i = GetStreamNCount(pObjStream); i > 0; --i) { uint32_t thisnum = syntax.GetDirectNum(); uint32_t thisoff = syntax.GetDirectNum(); @@ -1420,8 +1413,9 @@ uint32_t CPDF_Parser::GetPermissions() const { return dwPermission; } -bool CPDF_Parser::IsLinearizedFile(IFX_SeekableReadStream* pFileAccess, - uint32_t offset) { +bool CPDF_Parser::IsLinearizedFile( + const CFX_RetainPtr<IFX_SeekableReadStream>& pFileAccess, + uint32_t offset) { m_pSyntax->InitParser(pFileAccess, offset); m_pSyntax->RestorePos(m_pSyntax->m_HeaderOffset + 9); @@ -1454,13 +1448,11 @@ bool CPDF_Parser::IsLinearizedFile(IFX_SeekableReadStream* pFileAccess, } CPDF_Parser::Error CPDF_Parser::StartLinearizedParse( - IFX_SeekableReadStream* pFileAccess, + const CFX_RetainPtr<IFX_SeekableReadStream>& pFileAccess, CPDF_Document* pDocument) { ASSERT(!m_bHasParsed); - m_bXRefStream = false; m_LastXRefOffset = 0; - m_bOwnFileRead = true; int32_t offset = GetHeaderOffset(pFileAccess); if (offset == -1) @@ -1474,7 +1466,6 @@ CPDF_Parser::Error CPDF_Parser::StartLinearizedParse( m_pDocument = pDocument; FX_FILESIZE dwFirstXRefOffset = m_pSyntax->SavePos(); - bool bXRefRebuilt = false; bool bLoadV4 = LoadCrossRefV4(dwFirstXRefOffset, 0, false); if (!bLoadV4 && !LoadCrossRefV5(&dwFirstXRefOffset, true)) { diff --git a/core/fpdfapi/parser/cpdf_parser.h b/core/fpdfapi/parser/cpdf_parser.h index 5e2cdea08d..f8ff7a17ec 100644 --- a/core/fpdfapi/parser/cpdf_parser.h +++ b/core/fpdfapi/parser/cpdf_parser.h @@ -43,8 +43,9 @@ class CPDF_Parser { CPDF_Parser(); ~CPDF_Parser(); - Error StartParse(IFX_SeekableReadStream* pFile, CPDF_Document* pDocument); - Error StartLinearizedParse(IFX_SeekableReadStream* pFile, + Error StartParse(const CFX_RetainPtr<IFX_SeekableReadStream>& pFile, + CPDF_Document* pDocument); + Error StartLinearizedParse(const CFX_RetainPtr<IFX_SeekableReadStream>& pFile, CPDF_Document* pDocument); void SetPassword(const FX_CHAR* password) { m_Password = password; } @@ -71,7 +72,7 @@ class CPDF_Parser { bool IsVersionUpdated() const { return m_bVersionUpdated; } bool IsObjectFreeOrNull(uint32_t objnum) const; CPDF_CryptoHandler* GetCryptoHandler(); - IFX_SeekableReadStream* GetFileAccess() const; + CFX_RetainPtr<IFX_SeekableReadStream> GetFileAccess() const; FX_FILESIZE GetObjectOffset(uint32_t objnum) const; FX_FILESIZE GetObjectSize(uint32_t objnum) const; @@ -140,7 +141,9 @@ class CPDF_Parser { bool LoadLinearizedAllCrossRefV5(FX_FILESIZE pos); Error LoadLinearizedMainXRefTable(); CPDF_StreamAcc* GetObjectStream(uint32_t number); - bool IsLinearizedFile(IFX_SeekableReadStream* pFileAccess, uint32_t offset); + bool IsLinearizedFile( + const CFX_RetainPtr<IFX_SeekableReadStream>& pFileAccess, + uint32_t offset); void SetEncryptDictionary(CPDF_Dictionary* pDict); void ShrinkObjectMap(uint32_t size); // A simple check whether the cross reference table matches with @@ -149,7 +152,6 @@ class CPDF_Parser { CPDF_Document* m_pDocument; // not owned bool m_bHasParsed; - bool m_bOwnFileRead; bool m_bXRefStream; bool m_bVersionUpdated; int m_FileVersion; diff --git a/core/fpdfapi/parser/cpdf_parser_unittest.cpp b/core/fpdfapi/parser/cpdf_parser_unittest.cpp index 61496a1562..3518f7d16b 100644 --- a/core/fpdfapi/parser/cpdf_parser_unittest.cpp +++ b/core/fpdfapi/parser/cpdf_parser_unittest.cpp @@ -7,6 +7,7 @@ #include "core/fpdfapi/parser/cpdf_parser.h" #include "core/fpdfapi/parser/cpdf_syntax_parser.h" +#include "core/fxcrt/cfx_retain_ptr.h" #include "core/fxcrt/fx_ext.h" #include "core/fxcrt/fx_stream.h" #include "testing/gtest/include/gtest/gtest.h" @@ -15,11 +16,12 @@ // Provide a way to read test data from a buffer instead of a file. class CFX_TestBufferRead : public IFX_SeekableReadStream { public: - CFX_TestBufferRead(const unsigned char* buffer_in, size_t buf_size) - : buffer_(buffer_in), total_size_(buf_size) {} - - // IFX_ReadStream: - void Release() override { delete this; } + static CFX_RetainPtr<CFX_TestBufferRead> Create( + const unsigned char* buffer_in, + size_t buf_size) { + return CFX_RetainPtr<CFX_TestBufferRead>( + new CFX_TestBufferRead(buffer_in, buf_size)); + } // IFX_SeekableReadStream: bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { @@ -33,6 +35,9 @@ class CFX_TestBufferRead : public IFX_SeekableReadStream { FX_FILESIZE GetSize() override { return (FX_FILESIZE)total_size_; }; protected: + CFX_TestBufferRead(const unsigned char* buffer_in, size_t buf_size) + : buffer_(buffer_in), total_size_(buf_size) {} + const unsigned char* buffer_; size_t total_size_; }; @@ -45,7 +50,7 @@ class CPDF_TestParser : public CPDF_Parser { // Setup reading from a file and initial states. bool InitTestFromFile(const FX_CHAR* path) { - IFX_SeekableReadStream* pFileAccess = + CFX_RetainPtr<IFX_SeekableReadStream> pFileAccess = IFX_SeekableReadStream::CreateFromFilename(path); if (!pFileAccess) return false; @@ -57,7 +62,8 @@ class CPDF_TestParser : public CPDF_Parser { // Setup reading from a buffer and initial states. bool InitTestFromBuffer(const unsigned char* buffer, size_t len) { - CFX_TestBufferRead* buffer_reader = new CFX_TestBufferRead(buffer, len); + CFX_RetainPtr<CFX_TestBufferRead> buffer_reader = + CFX_TestBufferRead::Create(buffer, len); // For the test file, the header is set at the beginning. m_pSyntax->InitParser(buffer_reader, 0); diff --git a/core/fpdfapi/parser/cpdf_stream.cpp b/core/fpdfapi/parser/cpdf_stream.cpp index de69bfae7b..4f76ec1c63 100644 --- a/core/fpdfapi/parser/cpdf_stream.cpp +++ b/core/fpdfapi/parser/cpdf_stream.cpp @@ -62,8 +62,9 @@ void CPDF_Stream::InitStream(const uint8_t* pData, m_pDict->SetNewFor<CPDF_Number>("Length", static_cast<int>(m_dwSize)); } -void CPDF_Stream::InitStreamFromFile(IFX_SeekableReadStream* pFile, - std::unique_ptr<CPDF_Dictionary> pDict) { +void CPDF_Stream::InitStreamFromFile( + const CFX_RetainPtr<IFX_SeekableReadStream>& pFile, + std::unique_ptr<CPDF_Dictionary> pDict) { m_pDict = std::move(pDict); m_bMemoryBased = false; m_pDataBuf.reset(); diff --git a/core/fpdfapi/parser/cpdf_stream.h b/core/fpdfapi/parser/cpdf_stream.h index fb2b67cd82..cd4113b22b 100644 --- a/core/fpdfapi/parser/cpdf_stream.h +++ b/core/fpdfapi/parser/cpdf_stream.h @@ -43,7 +43,7 @@ class CPDF_Stream : public CPDF_Object { void InitStream(const uint8_t* pData, uint32_t size, std::unique_ptr<CPDF_Dictionary> pDict); - void InitStreamFromFile(IFX_SeekableReadStream* pFile, + void InitStreamFromFile(const CFX_RetainPtr<IFX_SeekableReadStream>& pFile, std::unique_ptr<CPDF_Dictionary> pDict); bool ReadRawData(FX_FILESIZE start_pos, @@ -62,7 +62,7 @@ class CPDF_Stream : public CPDF_Object { uint32_t m_dwSize = 0; std::unique_ptr<CPDF_Dictionary> m_pDict; std::unique_ptr<uint8_t, FxFreeDeleter> m_pDataBuf; - IFX_SeekableReadStream* m_pFile = nullptr; + CFX_RetainPtr<IFX_SeekableReadStream> m_pFile; }; inline CPDF_Stream* ToStream(CPDF_Object* obj) { diff --git a/core/fpdfapi/parser/cpdf_syntax_parser.cpp b/core/fpdfapi/parser/cpdf_syntax_parser.cpp index 9beae0b7f7..2a20e43591 100644 --- a/core/fpdfapi/parser/cpdf_syntax_parser.cpp +++ b/core/fpdfapi/parser/cpdf_syntax_parser.cpp @@ -756,8 +756,9 @@ std::unique_ptr<CPDF_Stream> CPDF_SyntaxParser::ReadStream( return pStream; } -void CPDF_SyntaxParser::InitParser(IFX_SeekableReadStream* pFileAccess, - uint32_t HeaderOffset) { +void CPDF_SyntaxParser::InitParser( + const CFX_RetainPtr<IFX_SeekableReadStream>& pFileAccess, + uint32_t HeaderOffset) { FX_Free(m_pFileBuf); m_pFileBuf = FX_Alloc(uint8_t, m_BufSize); diff --git a/core/fpdfapi/parser/cpdf_syntax_parser.h b/core/fpdfapi/parser/cpdf_syntax_parser.h index 1e8f736c17..8dd9103f1b 100644 --- a/core/fpdfapi/parser/cpdf_syntax_parser.h +++ b/core/fpdfapi/parser/cpdf_syntax_parser.h @@ -26,7 +26,9 @@ class CPDF_SyntaxParser { explicit CPDF_SyntaxParser(const CFX_WeakPtr<CFX_ByteStringPool>& pPool); ~CPDF_SyntaxParser(); - void InitParser(IFX_SeekableReadStream* pFileAccess, uint32_t HeaderOffset); + void InitParser(const CFX_RetainPtr<IFX_SeekableReadStream>& pFileAccess, + uint32_t HeaderOffset); + FX_FILESIZE SavePos() const { return m_Pos; } void RestorePos(FX_FILESIZE pos) { m_Pos = pos; } @@ -87,7 +89,7 @@ class CPDF_SyntaxParser { FX_FILESIZE m_Pos; uint32_t m_MetadataObjnum; - IFX_SeekableReadStream* m_pFileAccess; + CFX_RetainPtr<IFX_SeekableReadStream> m_pFileAccess; FX_FILESIZE m_HeaderOffset; FX_FILESIZE m_FileLen; uint8_t* m_pFileBuf; diff --git a/core/fpdfapi/parser/cpdf_syntax_parser_unittest.cpp b/core/fpdfapi/parser/cpdf_syntax_parser_unittest.cpp index f94b431c82..faaa83dd19 100644 --- a/core/fpdfapi/parser/cpdf_syntax_parser_unittest.cpp +++ b/core/fpdfapi/parser/cpdf_syntax_parser_unittest.cpp @@ -16,10 +16,8 @@ TEST(cpdf_syntax_parser, ReadHexString) { { // Empty string. uint8_t data[] = ""; - ScopedFileStream stream(IFX_MemoryStream::Create(data, 0, false)); - CPDF_SyntaxParser parser; - parser.InitParser(stream.get(), 0); + parser.InitParser(IFX_MemoryStream::Create(data, 0, false), 0); EXPECT_EQ("", parser.ReadHexString()); EXPECT_EQ(0, parser.SavePos()); } @@ -27,10 +25,8 @@ TEST(cpdf_syntax_parser, ReadHexString) { { // Blank string. uint8_t data[] = " "; - ScopedFileStream stream(IFX_MemoryStream::Create(data, 2, false)); - CPDF_SyntaxParser parser; - parser.InitParser(stream.get(), 0); + parser.InitParser(IFX_MemoryStream::Create(data, 2, false), 0); EXPECT_EQ("", parser.ReadHexString()); EXPECT_EQ(2, parser.SavePos()); } @@ -38,10 +34,8 @@ TEST(cpdf_syntax_parser, ReadHexString) { { // Skips unknown characters. uint8_t data[] = "z12b"; - ScopedFileStream stream(IFX_MemoryStream::Create(data, 4, false)); - CPDF_SyntaxParser parser; - parser.InitParser(stream.get(), 0); + parser.InitParser(IFX_MemoryStream::Create(data, 4, false), 0); EXPECT_EQ("\x12\xb0", parser.ReadHexString()); EXPECT_EQ(4, parser.SavePos()); } @@ -49,10 +43,8 @@ TEST(cpdf_syntax_parser, ReadHexString) { { // Skips unknown characters. uint8_t data[] = "*<&*#$^&@1"; - ScopedFileStream stream(IFX_MemoryStream::Create(data, 10, false)); - CPDF_SyntaxParser parser; - parser.InitParser(stream.get(), 0); + parser.InitParser(IFX_MemoryStream::Create(data, 10, false), 0); EXPECT_EQ("\x10", parser.ReadHexString()); EXPECT_EQ(10, parser.SavePos()); } @@ -60,10 +52,8 @@ TEST(cpdf_syntax_parser, ReadHexString) { { // Skips unknown characters. uint8_t data[] = "\x80zab"; - ScopedFileStream stream(IFX_MemoryStream::Create(data, 4, false)); - CPDF_SyntaxParser parser; - parser.InitParser(stream.get(), 0); + parser.InitParser(IFX_MemoryStream::Create(data, 4, false), 0); EXPECT_EQ("\xab", parser.ReadHexString()); EXPECT_EQ(4, parser.SavePos()); } @@ -71,10 +61,8 @@ TEST(cpdf_syntax_parser, ReadHexString) { { // Skips unknown characters. uint8_t data[] = "\xffzab"; - ScopedFileStream stream(IFX_MemoryStream::Create(data, 4, false)); - CPDF_SyntaxParser parser; - parser.InitParser(stream.get(), 0); + parser.InitParser(IFX_MemoryStream::Create(data, 4, false), 0); EXPECT_EQ("\xab", parser.ReadHexString()); EXPECT_EQ(4, parser.SavePos()); } @@ -82,10 +70,8 @@ TEST(cpdf_syntax_parser, ReadHexString) { { // Regular conversion. uint8_t data[] = "1A2b>abcd"; - ScopedFileStream stream(IFX_MemoryStream::Create(data, 9, false)); - CPDF_SyntaxParser parser; - parser.InitParser(stream.get(), 0); + parser.InitParser(IFX_MemoryStream::Create(data, 9, false), 0); EXPECT_EQ("\x1a\x2b", parser.ReadHexString()); EXPECT_EQ(5, parser.SavePos()); } @@ -93,10 +79,8 @@ TEST(cpdf_syntax_parser, ReadHexString) { { // Position out of bounds. uint8_t data[] = "12ab>"; - ScopedFileStream stream(IFX_MemoryStream::Create(data, 5, false)); - CPDF_SyntaxParser parser; - parser.InitParser(stream.get(), 0); + parser.InitParser(IFX_MemoryStream::Create(data, 5, false), 0); parser.RestorePos(5); EXPECT_EQ("", parser.ReadHexString()); @@ -117,10 +101,8 @@ TEST(cpdf_syntax_parser, ReadHexString) { { // Missing ending >. uint8_t data[] = "1A2b"; - ScopedFileStream stream(IFX_MemoryStream::Create(data, 4, false)); - CPDF_SyntaxParser parser; - parser.InitParser(stream.get(), 0); + parser.InitParser(IFX_MemoryStream::Create(data, 4, false), 0); EXPECT_EQ("\x1a\x2b", parser.ReadHexString()); EXPECT_EQ(4, parser.SavePos()); } @@ -128,10 +110,8 @@ TEST(cpdf_syntax_parser, ReadHexString) { { // Missing ending >. uint8_t data[] = "12abz"; - ScopedFileStream stream(IFX_MemoryStream::Create(data, 5, false)); - CPDF_SyntaxParser parser; - parser.InitParser(stream.get(), 0); + parser.InitParser(IFX_MemoryStream::Create(data, 5, false), 0); EXPECT_EQ("\x12\xab", parser.ReadHexString()); EXPECT_EQ(5, parser.SavePos()); } @@ -139,10 +119,8 @@ TEST(cpdf_syntax_parser, ReadHexString) { { // Uneven number of bytes. uint8_t data[] = "1A2>asdf"; - ScopedFileStream stream(IFX_MemoryStream::Create(data, 8, false)); - CPDF_SyntaxParser parser; - parser.InitParser(stream.get(), 0); + parser.InitParser(IFX_MemoryStream::Create(data, 8, false), 0); EXPECT_EQ("\x1a\x20", parser.ReadHexString()); EXPECT_EQ(4, parser.SavePos()); } @@ -150,10 +128,8 @@ TEST(cpdf_syntax_parser, ReadHexString) { { // Uneven number of bytes. uint8_t data[] = "1A2zasdf"; - ScopedFileStream stream(IFX_MemoryStream::Create(data, 8, false)); - CPDF_SyntaxParser parser; - parser.InitParser(stream.get(), 0); + parser.InitParser(IFX_MemoryStream::Create(data, 8, false), 0); EXPECT_EQ("\x1a\x2a\xdf", parser.ReadHexString()); EXPECT_EQ(8, parser.SavePos()); } @@ -161,10 +137,8 @@ TEST(cpdf_syntax_parser, ReadHexString) { { // Just ending character. uint8_t data[] = ">"; - ScopedFileStream stream(IFX_MemoryStream::Create(data, 1, false)); - CPDF_SyntaxParser parser; - parser.InitParser(stream.get(), 0); + parser.InitParser(IFX_MemoryStream::Create(data, 1, false), 0); EXPECT_EQ("", parser.ReadHexString()); EXPECT_EQ(1, parser.SavePos()); } diff --git a/core/fpdfapi/parser/fpdf_parser_utility.cpp b/core/fpdfapi/parser/fpdf_parser_utility.cpp index ef37d8f39e..9583ea70bc 100644 --- a/core/fpdfapi/parser/fpdf_parser_utility.cpp +++ b/core/fpdfapi/parser/fpdf_parser_utility.cpp @@ -69,7 +69,7 @@ const char PDF_CharType[256] = { 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'W'}; -int32_t GetHeaderOffset(IFX_SeekableReadStream* pFile) { +int32_t GetHeaderOffset(const CFX_RetainPtr<IFX_SeekableReadStream>& pFile) { const size_t kBufSize = 4; uint8_t buf[kBufSize]; for (int32_t offset = 0; offset <= 1024; ++offset) { diff --git a/core/fpdfapi/parser/fpdf_parser_utility.h b/core/fpdfapi/parser/fpdf_parser_utility.h index 589171f1a4..29f54ee853 100644 --- a/core/fpdfapi/parser/fpdf_parser_utility.h +++ b/core/fpdfapi/parser/fpdf_parser_utility.h @@ -7,6 +7,7 @@ #ifndef CORE_FPDFAPI_PARSER_FPDF_PARSER_UTILITY_H_ #define CORE_FPDFAPI_PARSER_FPDF_PARSER_UTILITY_H_ +#include "core/fxcrt/cfx_retain_ptr.h" #include "core/fxcrt/fx_string.h" #include "core/fxcrt/fx_system.h" @@ -33,7 +34,7 @@ inline bool PDFCharIsLineEnding(uint8_t c) { return c == '\r' || c == '\n'; } -int32_t GetHeaderOffset(IFX_SeekableReadStream* pFile); +int32_t GetHeaderOffset(const CFX_RetainPtr<IFX_SeekableReadStream>& pFile); int32_t GetDirectInteger(CPDF_Dictionary* pDict, const CFX_ByteString& key); #endif // CORE_FPDFAPI_PARSER_FPDF_PARSER_UTILITY_H_ |