From afd0d1f488ea55da545b3310fd8f22e45522a695 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Tue, 4 Apr 2017 14:37:18 -0700 Subject: RefCount CPDF_StreamAcc all the time. Pass stream argument to constructor; it feels like a stream accessor should always be made from a stream rather than passing one in after the fact. Change-Id: Iaa46cb37677b81f0170f5d39bab76ad38ea4af44 Reviewed-on: https://pdfium-review.googlesource.com/3620 Commit-Queue: Tom Sepez Reviewed-by: Lei Zhang --- core/fpdfapi/parser/cpdf_document.cpp | 3 ++- core/fpdfapi/parser/cpdf_document.h | 2 +- core/fpdfapi/parser/cpdf_hint_tables.cpp | 8 ++++---- core/fpdfapi/parser/cpdf_parser.cpp | 28 ++++++++++++++------------- core/fpdfapi/parser/cpdf_parser.h | 8 ++++---- core/fpdfapi/parser/cpdf_stream.cpp | 14 +++++++------- core/fpdfapi/parser/cpdf_stream_acc.cpp | 30 ++++++++++++++--------------- core/fpdfapi/parser/cpdf_stream_acc.h | 15 +++++++++------ core/fpdfapi/parser/fpdf_parser_utility.cpp | 6 +++--- 9 files changed, 59 insertions(+), 55 deletions(-) (limited to 'core/fpdfapi/parser') diff --git a/core/fpdfapi/parser/cpdf_document.cpp b/core/fpdfapi/parser/cpdf_document.cpp index a2816d48a9..4d99ac910f 100644 --- a/core/fpdfapi/parser/cpdf_document.cpp +++ b/core/fpdfapi/parser/cpdf_document.cpp @@ -630,7 +630,8 @@ CPDF_Font* CPDF_Document::LoadFont(CPDF_Dictionary* pFontDict) { return m_pDocPage->GetFont(pFontDict); } -CPDF_StreamAcc* CPDF_Document::LoadFontFile(CPDF_Stream* pStream) { +CFX_RetainPtr CPDF_Document::LoadFontFile( + CPDF_Stream* pStream) { return m_pDocPage->GetFontFileStreamAcc(pStream); } diff --git a/core/fpdfapi/parser/cpdf_document.h b/core/fpdfapi/parser/cpdf_document.h index 91cc2d7419..8d3525419f 100644 --- a/core/fpdfapi/parser/cpdf_document.h +++ b/core/fpdfapi/parser/cpdf_document.h @@ -79,7 +79,7 @@ class CPDF_Document : public CPDF_IndirectObjectHolder { const CFX_Matrix& matrix); CFX_RetainPtr LoadImageFromPageData(uint32_t dwStreamObjNum); - CPDF_StreamAcc* LoadFontFile(CPDF_Stream* pStream); + CFX_RetainPtr LoadFontFile(CPDF_Stream* pStream); CFX_RetainPtr LoadIccProfile(CPDF_Stream* pStream); void LoadDoc(); diff --git a/core/fpdfapi/parser/cpdf_hint_tables.cpp b/core/fpdfapi/parser/cpdf_hint_tables.cpp index e000188e7d..24abf5dd13 100644 --- a/core/fpdfapi/parser/cpdf_hint_tables.cpp +++ b/core/fpdfapi/parser/cpdf_hint_tables.cpp @@ -464,10 +464,10 @@ bool CPDF_HintTables::LoadHintStream(CPDF_Stream* pHintStream) { if (shared_hint_table_offset <= 0) return false; - CPDF_StreamAcc acc; - acc.LoadAllData(pHintStream); + auto pAcc = pdfium::MakeRetain(pHintStream); + pAcc->LoadAllData(); - uint32_t size = acc.GetSize(); + uint32_t size = pAcc->GetSize(); // The header section of page offset hint table is 36 bytes. // The header section of shared object hint table is 24 bytes. // Hint table has at least 60 bytes. @@ -482,7 +482,7 @@ bool CPDF_HintTables::LoadHintStream(CPDF_Stream* pHintStream) { } CFX_BitStream bs; - bs.Init(acc.GetData(), size); + bs.Init(pAcc->GetData(), size); return ReadPageHintTable(&bs) && ReadSharedObjHintTable(&bs, shared_hint_table_offset); } diff --git a/core/fpdfapi/parser/cpdf_parser.cpp b/core/fpdfapi/parser/cpdf_parser.cpp index cac678762d..20e9418904 100644 --- a/core/fpdfapi/parser/cpdf_parser.cpp +++ b/core/fpdfapi/parser/cpdf_parser.cpp @@ -40,11 +40,11 @@ uint32_t GetVarInt(const uint8_t* p, int32_t n) { return result; } -int32_t GetStreamNCount(CPDF_StreamAcc* pObjStream) { +int32_t GetStreamNCount(const CFX_RetainPtr& pObjStream) { return pObjStream->GetDict()->GetIntegerFor("N"); } -int32_t GetStreamFirst(CPDF_StreamAcc* pObjStream) { +int32_t GetStreamFirst(const CFX_RetainPtr& pObjStream) { return pObjStream->GetDict()->GetIntegerFor("First"); } @@ -997,11 +997,11 @@ bool CPDF_Parser::LoadCrossRefV5(FX_FILESIZE* pos, bool bMainXRef) { return false; uint32_t totalWidth = dwAccWidth.ValueOrDie(); - CPDF_StreamAcc acc; - acc.LoadAllData(pStream); + auto pAcc = pdfium::MakeRetain(pStream); + pAcc->LoadAllData(); - const uint8_t* pData = acc.GetData(); - uint32_t dwTotalSize = acc.GetSize(); + const uint8_t* pData = pAcc->GetData(); + uint32_t dwTotalSize = pAcc->GetSize(); uint32_t segindex = 0; for (uint32_t i = 0; i < arrIndex.size(); i++) { int32_t startnum = arrIndex[i].first; @@ -1114,7 +1114,8 @@ std::unique_ptr CPDF_Parser::ParseIndirectObject( if (GetObjectType(objnum) != 2) return nullptr; - CPDF_StreamAcc* pObjStream = GetObjectStream(m_ObjectInfo[objnum].pos); + CFX_RetainPtr pObjStream = + GetObjectStream(m_ObjectInfo[objnum].pos); if (!pObjStream) return nullptr; @@ -1141,10 +1142,10 @@ std::unique_ptr CPDF_Parser::ParseIndirectObject( return syntax.GetObject(pObjList, 0, 0, true); } -CPDF_StreamAcc* CPDF_Parser::GetObjectStream(uint32_t objnum) { +CFX_RetainPtr CPDF_Parser::GetObjectStream(uint32_t objnum) { auto it = m_ObjectStreamMap.find(objnum); if (it != m_ObjectStreamMap.end()) - return it->second.get(); + return it->second; if (!m_pDocument) return nullptr; @@ -1154,9 +1155,9 @@ CPDF_StreamAcc* CPDF_Parser::GetObjectStream(uint32_t objnum) { if (!pStream) return nullptr; - CPDF_StreamAcc* pStreamAcc = new CPDF_StreamAcc; - pStreamAcc->LoadAllData(pStream); - m_ObjectStreamMap[objnum].reset(pStreamAcc); + auto pStreamAcc = pdfium::MakeRetain(pStream); + pStreamAcc->LoadAllData(); + m_ObjectStreamMap[objnum] = pStreamAcc; return pStreamAcc; } @@ -1190,7 +1191,8 @@ void CPDF_Parser::GetIndirectBinary(uint32_t objnum, return; if (GetObjectType(objnum) == 2) { - CPDF_StreamAcc* pObjStream = GetObjectStream(m_ObjectInfo[objnum].pos); + CFX_RetainPtr pObjStream = + GetObjectStream(m_ObjectInfo[objnum].pos); if (!pObjStream) return; diff --git a/core/fpdfapi/parser/cpdf_parser.h b/core/fpdfapi/parser/cpdf_parser.h index 0b73857649..47c2c9b885 100644 --- a/core/fpdfapi/parser/cpdf_parser.h +++ b/core/fpdfapi/parser/cpdf_parser.h @@ -140,7 +140,7 @@ class CPDF_Parser { bool LoadLinearizedCrossRefV4(FX_FILESIZE pos, uint32_t dwObjCount); bool LoadLinearizedAllCrossRefV5(FX_FILESIZE pos); Error LoadLinearizedMainXRefTable(); - CPDF_StreamAcc* GetObjectStream(uint32_t number); + CFX_RetainPtr GetObjectStream(uint32_t number); bool IsLinearizedFile( const CFX_RetainPtr& pFileAccess, uint32_t offset); @@ -165,8 +165,8 @@ class CPDF_Parser { std::unique_ptr m_pLinearized; uint32_t m_dwXrefStartObjNum; - // A map of object numbers to indirect streams. Map owns the streams. - std::map> m_ObjectStreamMap; + // A map of object numbers to indirect streams. + std::map> m_ObjectStreamMap; // Mapping of object numbers to offsets. The offsets are relative to the first // object in the stream. @@ -174,7 +174,7 @@ class CPDF_Parser { // Mapping of streams to their object caches. This is valid as long as the // streams in |m_ObjectStreamMap| are valid. - std::map m_ObjCache; + std::map, StreamObjectCache> m_ObjCache; // All indirect object numbers that are being parsed. std::set m_ParsingObjNums; diff --git a/core/fpdfapi/parser/cpdf_stream.cpp b/core/fpdfapi/parser/cpdf_stream.cpp index 532fd753f8..e4f279a32d 100644 --- a/core/fpdfapi/parser/cpdf_stream.cpp +++ b/core/fpdfapi/parser/cpdf_stream.cpp @@ -83,17 +83,17 @@ std::unique_ptr CPDF_Stream::CloneNonCyclic( bool bDirect, std::set* pVisited) const { pVisited->insert(this); - CPDF_StreamAcc acc; - acc.LoadAllData(this, true); + auto pAcc = pdfium::MakeRetain(this); + pAcc->LoadAllData(true); - uint32_t streamSize = acc.GetSize(); + uint32_t streamSize = pAcc->GetSize(); CPDF_Dictionary* pDict = GetDict(); std::unique_ptr pNewDict; if (pDict && !pdfium::ContainsKey(*pVisited, pDict)) { pNewDict = ToDictionary( static_cast(pDict)->CloneNonCyclic(bDirect, pVisited)); } - return pdfium::MakeUnique(acc.DetachData(), streamSize, + return pdfium::MakeUnique(pAcc->DetachData(), streamSize, std::move(pNewDict)); } @@ -127,7 +127,7 @@ bool CPDF_Stream::HasFilter() const { } CFX_WideString CPDF_Stream::GetUnicodeText() const { - CPDF_StreamAcc stream; - stream.LoadAllData(this, false); - return PDF_DecodeText(stream.GetData(), stream.GetSize()); + auto pAcc = pdfium::MakeRetain(this); + pAcc->LoadAllData(false); + return PDF_DecodeText(pAcc->GetData(), pAcc->GetSize()); } diff --git a/core/fpdfapi/parser/cpdf_stream_acc.cpp b/core/fpdfapi/parser/cpdf_stream_acc.cpp index 59ac289b7c..77ef2ff2bc 100644 --- a/core/fpdfapi/parser/cpdf_stream_acc.cpp +++ b/core/fpdfapi/parser/cpdf_stream_acc.cpp @@ -8,40 +8,38 @@ #include "core/fpdfapi/parser/fpdf_parser_decode.h" -CPDF_StreamAcc::CPDF_StreamAcc() +CPDF_StreamAcc::CPDF_StreamAcc(const CPDF_Stream* pStream) : m_pData(nullptr), m_dwSize(0), m_bNewBuf(false), m_pImageParam(nullptr), - m_pStream(nullptr), + m_pStream(pStream), m_pSrcData(nullptr) {} -void CPDF_StreamAcc::LoadAllData(const CPDF_Stream* pStream, - bool bRawAccess, +void CPDF_StreamAcc::LoadAllData(bool bRawAccess, uint32_t estimated_size, bool bImageAcc) { - if (!pStream) + if (!m_pStream) return; - m_pStream = pStream; - if (pStream->IsMemoryBased() && (!pStream->HasFilter() || bRawAccess)) { - m_dwSize = pStream->GetRawSize(); - m_pData = pStream->GetRawData(); + if (m_pStream->IsMemoryBased() && (!m_pStream->HasFilter() || bRawAccess)) { + m_dwSize = m_pStream->GetRawSize(); + m_pData = m_pStream->GetRawData(); return; } - uint32_t dwSrcSize = pStream->GetRawSize(); + uint32_t dwSrcSize = m_pStream->GetRawSize(); if (dwSrcSize == 0) return; uint8_t* pSrcData; - if (!pStream->IsMemoryBased()) { + if (!m_pStream->IsMemoryBased()) { pSrcData = m_pSrcData = FX_Alloc(uint8_t, dwSrcSize); - if (!pStream->ReadRawData(0, pSrcData, dwSrcSize)) + if (!m_pStream->ReadRawData(0, pSrcData, dwSrcSize)) return; } else { - pSrcData = pStream->GetRawData(); + pSrcData = m_pStream->GetRawData(); } - if (!pStream->HasFilter() || bRawAccess) { + if (!m_pStream->HasFilter() || bRawAccess) { m_pData = pSrcData; m_dwSize = dwSrcSize; } else if (!PDF_DataDecode(pSrcData, dwSrcSize, m_pStream->GetDict(), m_pData, @@ -50,10 +48,10 @@ void CPDF_StreamAcc::LoadAllData(const CPDF_Stream* pStream, m_pData = pSrcData; m_dwSize = dwSrcSize; } - if (pSrcData != pStream->GetRawData() && pSrcData != m_pData) + if (pSrcData != m_pStream->GetRawData() && pSrcData != m_pData) FX_Free(pSrcData); m_pSrcData = nullptr; - m_bNewBuf = m_pData != pStream->GetRawData(); + m_bNewBuf = m_pData != m_pStream->GetRawData(); } CPDF_StreamAcc::~CPDF_StreamAcc() { diff --git a/core/fpdfapi/parser/cpdf_stream_acc.h b/core/fpdfapi/parser/cpdf_stream_acc.h index 51a8a18332..5bfdd186c4 100644 --- a/core/fpdfapi/parser/cpdf_stream_acc.h +++ b/core/fpdfapi/parser/cpdf_stream_acc.h @@ -11,19 +11,19 @@ #include "core/fpdfapi/parser/cpdf_dictionary.h" #include "core/fpdfapi/parser/cpdf_stream.h" +#include "core/fxcrt/cfx_retain_ptr.h" #include "core/fxcrt/fx_string.h" #include "core/fxcrt/fx_system.h" -class CPDF_StreamAcc { +class CPDF_StreamAcc : public CFX_Retainable { public: - CPDF_StreamAcc(); - ~CPDF_StreamAcc(); + template + friend CFX_RetainPtr pdfium::MakeRetain(Args&&... args); CPDF_StreamAcc(const CPDF_StreamAcc&) = delete; CPDF_StreamAcc& operator=(const CPDF_StreamAcc&) = delete; - void LoadAllData(const CPDF_Stream* pStream, - bool bRawAccess = false, + void LoadAllData(bool bRawAccess = false, uint32_t estimated_size = 0, bool bImageAcc = false); @@ -39,12 +39,15 @@ class CPDF_StreamAcc { std::unique_ptr DetachData(); protected: + explicit CPDF_StreamAcc(const CPDF_Stream* pStream); + ~CPDF_StreamAcc() override; + uint8_t* m_pData; uint32_t m_dwSize; bool m_bNewBuf; CFX_ByteString m_ImageDecoder; CPDF_Dictionary* m_pImageParam; - const CPDF_Stream* m_pStream; + const CPDF_Stream* const m_pStream; uint8_t* m_pSrcData; }; diff --git a/core/fpdfapi/parser/fpdf_parser_utility.cpp b/core/fpdfapi/parser/fpdf_parser_utility.cpp index 10ba23125e..d6ea1a2c50 100644 --- a/core/fpdfapi/parser/fpdf_parser_utility.cpp +++ b/core/fpdfapi/parser/fpdf_parser_utility.cpp @@ -208,9 +208,9 @@ CFX_ByteTextBuf& operator<<(CFX_ByteTextBuf& buf, const CPDF_Object* pObj) { case CPDF_Object::STREAM: { const CPDF_Stream* p = pObj->AsStream(); buf << p->GetDict() << "stream\r\n"; - CPDF_StreamAcc acc; - acc.LoadAllData(p, true); - buf.AppendBlock(acc.GetData(), acc.GetSize()); + auto pAcc = pdfium::MakeRetain(p); + pAcc->LoadAllData(true); + buf.AppendBlock(pAcc->GetData(), pAcc->GetSize()); buf << "\r\nendstream"; break; } -- cgit v1.2.3