summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Sepez <tsepez@chromium.org>2017-04-04 14:37:18 -0700
committerChromium commit bot <commit-bot@chromium.org>2017-04-04 21:48:48 +0000
commitafd0d1f488ea55da545b3310fd8f22e45522a695 (patch)
tree89136eba4669421b6f828edb9f3d69ee558110d0
parent4f8b044b95dc282959dfe41453e2a9c1ec7e9354 (diff)
downloadpdfium-afd0d1f488ea55da545b3310fd8f22e45522a695.tar.xz
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 <tsepez@chromium.org> Reviewed-by: Lei Zhang <thestig@chromium.org>
-rw-r--r--core/fpdfapi/edit/fpdf_edit_create.cpp28
-rw-r--r--core/fpdfapi/font/cpdf_cidfont.cpp10
-rw-r--r--core/fpdfapi/font/cpdf_cidfont.h2
-rw-r--r--core/fpdfapi/font/cpdf_font.cpp4
-rw-r--r--core/fpdfapi/font/cpdf_font.h4
-rw-r--r--core/fpdfapi/font/fpdf_font.cpp6
-rw-r--r--core/fpdfapi/page/cpdf_colorspace.cpp6
-rw-r--r--core/fpdfapi/page/cpdf_contentparser.cpp13
-rw-r--r--core/fpdfapi/page/cpdf_contentparser.h5
-rw-r--r--core/fpdfapi/page/cpdf_docpagedata.cpp52
-rw-r--r--core/fpdfapi/page/cpdf_docpagedata.h7
-rw-r--r--core/fpdfapi/page/cpdf_meshstream.cpp9
-rw-r--r--core/fpdfapi/page/cpdf_meshstream.h3
-rw-r--r--core/fpdfapi/page/fpdf_page_func.cpp12
-rw-r--r--core/fpdfapi/page/pageint.h9
-rw-r--r--core/fpdfapi/parser/cpdf_document.cpp3
-rw-r--r--core/fpdfapi/parser/cpdf_document.h2
-rw-r--r--core/fpdfapi/parser/cpdf_hint_tables.cpp8
-rw-r--r--core/fpdfapi/parser/cpdf_parser.cpp28
-rw-r--r--core/fpdfapi/parser/cpdf_parser.h8
-rw-r--r--core/fpdfapi/parser/cpdf_stream.cpp14
-rw-r--r--core/fpdfapi/parser/cpdf_stream_acc.cpp30
-rw-r--r--core/fpdfapi/parser/cpdf_stream_acc.h15
-rw-r--r--core/fpdfapi/parser/fpdf_parser_utility.cpp6
-rw-r--r--core/fpdfapi/render/cpdf_dibsource.cpp20
-rw-r--r--core/fpdfapi/render/cpdf_dibsource.h4
-rw-r--r--core/fpdfdoc/cpdf_metadata.cpp6
-rw-r--r--core/fxcodec/codec/ccodec_jbig2module.h8
-rw-r--r--core/fxcodec/codec/fx_codec_jbig.cpp4
-rw-r--r--core/fxcodec/jbig2/JBig2_BitStream.cpp3
-rw-r--r--core/fxcodec/jbig2/JBig2_BitStream.h3
-rw-r--r--core/fxcodec/jbig2/JBig2_BitStream_unittest.cpp36
-rw-r--r--core/fxcodec/jbig2/JBig2_Context.cpp11
-rw-r--r--core/fxcodec/jbig2/JBig2_Context.h4
-rw-r--r--fpdfsdk/fpdf_flatten.cpp14
-rw-r--r--fpdfsdk/fpdfsave.cpp10
-rw-r--r--fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp4
-rw-r--r--testing/libfuzzer/pdf_codec_jbig2_fuzzer.cc7
-rw-r--r--xfa/fxfa/cxfa_ffapp.cpp6
-rw-r--r--xfa/fxfa/cxfa_ffdoc.cpp8
40 files changed, 208 insertions, 224 deletions
diff --git a/core/fpdfapi/edit/fpdf_edit_create.cpp b/core/fpdfapi/edit/fpdf_edit_create.cpp
index 59d3de35a9..d08e4b5302 100644
--- a/core/fpdfapi/edit/fpdf_edit_create.cpp
+++ b/core/fpdfapi/edit/fpdf_edit_create.cpp
@@ -182,12 +182,12 @@ int32_t PDF_CreatorAppendObject(const CPDF_Object* pObj,
return -1;
}
offset += 8;
- CPDF_StreamAcc acc;
- acc.LoadAllData(p, true);
- if (pFile->AppendBlock(acc.GetData(), acc.GetSize()) < 0) {
+ auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(p);
+ pAcc->LoadAllData(true);
+ if (pFile->AppendBlock(pAcc->GetData(), pAcc->GetSize()) < 0) {
return -1;
}
- offset += acc.GetSize();
+ offset += pAcc->GetSize();
if ((len = pFile->AppendString("\r\nendstream")) < 0) {
return -1;
}
@@ -411,7 +411,7 @@ class CPDF_FlateEncoder {
uint32_t m_dwSize;
CFX_MaybeOwned<uint8_t, FxFreeDeleter> m_pData;
CFX_MaybeOwned<CPDF_Dictionary> m_pDict;
- CPDF_StreamAcc m_Acc;
+ CFX_RetainPtr<CPDF_StreamAcc> m_pAcc;
};
void CPDF_FlateEncoder::CloneDict() {
@@ -422,27 +422,27 @@ void CPDF_FlateEncoder::CloneDict() {
}
CPDF_FlateEncoder::CPDF_FlateEncoder(CPDF_Stream* pStream, bool bFlateEncode)
- : m_dwSize(0) {
- m_Acc.LoadAllData(pStream, true);
+ : m_dwSize(0), m_pAcc(pdfium::MakeRetain<CPDF_StreamAcc>(pStream)) {
+ m_pAcc->LoadAllData(true);
bool bHasFilter = pStream && pStream->HasFilter();
if (bHasFilter && !bFlateEncode) {
- CPDF_StreamAcc destAcc;
- destAcc.LoadAllData(pStream);
- m_dwSize = destAcc.GetSize();
- m_pData = destAcc.DetachData();
+ auto pDestAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pStream);
+ pDestAcc->LoadAllData();
+ m_dwSize = pDestAcc->GetSize();
+ m_pData = pDestAcc->DetachData();
m_pDict = ToDictionary(pStream->GetDict()->Clone());
m_pDict->RemoveFor("Filter");
return;
}
if (bHasFilter || !bFlateEncode) {
- m_pData = const_cast<uint8_t*>(m_Acc.GetData());
- m_dwSize = m_Acc.GetSize();
+ m_pData = const_cast<uint8_t*>(m_pAcc->GetData());
+ m_dwSize = m_pAcc->GetSize();
m_pDict = pStream->GetDict();
return;
}
// TODO(thestig): Move to Init() and check return value.
uint8_t* buffer = nullptr;
- ::FlateEncode(m_Acc.GetData(), m_Acc.GetSize(), &buffer, &m_dwSize);
+ ::FlateEncode(m_pAcc->GetData(), m_pAcc->GetSize(), &buffer, &m_dwSize);
m_pData = std::unique_ptr<uint8_t, FxFreeDeleter>(buffer);
m_pDict = ToDictionary(pStream->GetDict()->Clone());
m_pDict->SetNewFor<CPDF_Number>("Length", static_cast<int>(m_dwSize));
diff --git a/core/fpdfapi/font/cpdf_cidfont.cpp b/core/fpdfapi/font/cpdf_cidfont.cpp
index 5aad5194cd..c309ac1d26 100644
--- a/core/fpdfapi/font/cpdf_cidfont.cpp
+++ b/core/fpdfapi/font/cpdf_cidfont.cpp
@@ -365,10 +365,10 @@ bool CPDF_CIDFont::Load() {
if (!m_pCMap)
return false;
} else if (CPDF_Stream* pStream = pEncoding->AsStream()) {
- CPDF_StreamAcc acc;
- acc.LoadAllData(pStream, false);
+ auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pStream);
+ pAcc->LoadAllData(false);
m_pCMap = pdfium::MakeRetain<CPDF_CMap>();
- m_pCMap->LoadEmbedded(acc.GetData(), acc.GetSize());
+ m_pCMap->LoadEmbedded(pAcc->GetData(), pAcc->GetSize());
} else {
return false;
}
@@ -403,8 +403,8 @@ bool CPDF_CIDFont::Load() {
CPDF_Object* pmap = pCIDFontDict->GetDirectObjectFor("CIDToGIDMap");
if (pmap) {
if (CPDF_Stream* pStream = pmap->AsStream()) {
- m_pStreamAcc = pdfium::MakeUnique<CPDF_StreamAcc>();
- m_pStreamAcc->LoadAllData(pStream, false);
+ m_pStreamAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pStream);
+ m_pStreamAcc->LoadAllData(false);
} else if (pmap->GetString() == "Identity") {
#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
if (m_pFontFile)
diff --git a/core/fpdfapi/font/cpdf_cidfont.h b/core/fpdfapi/font/cpdf_cidfont.h
index 938b537b1b..cc9bec1612 100644
--- a/core/fpdfapi/font/cpdf_cidfont.h
+++ b/core/fpdfapi/font/cpdf_cidfont.h
@@ -78,7 +78,7 @@ class CPDF_CIDFont : public CPDF_Font {
bool m_bType1;
bool m_bCIDIsGID;
uint16_t m_DefaultWidth;
- std::unique_ptr<CPDF_StreamAcc> m_pStreamAcc;
+ CFX_RetainPtr<CPDF_StreamAcc> m_pStreamAcc;
bool m_bAnsiWidthsFixed;
FX_RECT m_CharBBox[256];
std::vector<uint32_t> m_WidthList;
diff --git a/core/fpdfapi/font/cpdf_font.cpp b/core/fpdfapi/font/cpdf_font.cpp
index f0b5ac16b7..60eb1b361d 100644
--- a/core/fpdfapi/font/cpdf_font.cpp
+++ b/core/fpdfapi/font/cpdf_font.cpp
@@ -61,7 +61,7 @@ CPDF_Font::CPDF_Font()
CPDF_Font::~CPDF_Font() {
if (m_pFontFile) {
- m_pDocument->GetPageData()->ReleaseFontFileStreamAcc(
+ m_pDocument->GetPageData()->MaybePurgeFontFileStreamAcc(
m_pFontFile->GetStream()->AsStream());
}
}
@@ -219,7 +219,7 @@ void CPDF_Font::LoadFontDescriptor(CPDF_Dictionary* pFontDesc) {
const uint8_t* pFontData = m_pFontFile->GetData();
uint32_t dwFontSize = m_pFontFile->GetSize();
if (!m_Font.LoadEmbedded(pFontData, dwFontSize)) {
- m_pDocument->GetPageData()->ReleaseFontFileStreamAcc(
+ m_pDocument->GetPageData()->MaybePurgeFontFileStreamAcc(
m_pFontFile->GetStream()->AsStream());
m_pFontFile = nullptr;
}
diff --git a/core/fpdfapi/font/cpdf_font.h b/core/fpdfapi/font/cpdf_font.h
index 6dac3f2778..8ef68e6031 100644
--- a/core/fpdfapi/font/cpdf_font.h
+++ b/core/fpdfapi/font/cpdf_font.h
@@ -10,6 +10,7 @@
#include <memory>
#include <vector>
+#include "core/fpdfapi/parser/cpdf_stream_acc.h"
#include "core/fxcrt/fx_string.h"
#include "core/fxcrt/fx_system.h"
#include "core/fxge/fx_font.h"
@@ -19,7 +20,6 @@ class CPDF_CIDFont;
class CPDF_Dictionary;
class CPDF_Document;
class CPDF_Object;
-class CPDF_StreamAcc;
class CPDF_TrueTypeFont;
class CPDF_Type1Font;
class CPDF_Type3Font;
@@ -101,7 +101,7 @@ class CPDF_Font {
int charcode);
CFX_ByteString m_BaseFont;
- CPDF_StreamAcc* m_pFontFile;
+ CFX_RetainPtr<CPDF_StreamAcc> m_pFontFile;
CPDF_Dictionary* m_pFontDict;
mutable std::unique_ptr<CPDF_ToUnicodeMap> m_pToUnicodeMap;
mutable bool m_bToUnicodeLoaded;
diff --git a/core/fpdfapi/font/fpdf_font.cpp b/core/fpdfapi/font/fpdf_font.cpp
index 23b46759d8..a5616ea963 100644
--- a/core/fpdfapi/font/fpdf_font.cpp
+++ b/core/fpdfapi/font/fpdf_font.cpp
@@ -208,9 +208,9 @@ uint32_t CPDF_ToUnicodeMap::GetUnicode() {
void CPDF_ToUnicodeMap::Load(CPDF_Stream* pStream) {
CIDSet cid_set = CIDSET_UNKNOWN;
- CPDF_StreamAcc stream;
- stream.LoadAllData(pStream, false);
- CPDF_SimpleParser parser(stream.GetData(), stream.GetSize());
+ auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pStream);
+ pAcc->LoadAllData(false);
+ CPDF_SimpleParser parser(pAcc->GetData(), pAcc->GetSize());
while (1) {
CFX_ByteStringC word = parser.GetWord();
if (word.IsEmpty()) {
diff --git a/core/fpdfapi/page/cpdf_colorspace.cpp b/core/fpdfapi/page/cpdf_colorspace.cpp
index 500de26c56..400b7a9150 100644
--- a/core/fpdfapi/page/cpdf_colorspace.cpp
+++ b/core/fpdfapi/page/cpdf_colorspace.cpp
@@ -1084,9 +1084,9 @@ bool CPDF_IndexedCS::v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) {
if (CPDF_String* pString = pTableObj->AsString()) {
m_Table = pString->GetString();
} else if (CPDF_Stream* pStream = pTableObj->AsStream()) {
- CPDF_StreamAcc acc;
- acc.LoadAllData(pStream, false);
- m_Table = CFX_ByteStringC(acc.GetData(), acc.GetSize());
+ auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pStream);
+ pAcc->LoadAllData(false);
+ m_Table = CFX_ByteStringC(pAcc->GetData(), pAcc->GetSize());
}
return true;
}
diff --git a/core/fpdfapi/page/cpdf_contentparser.cpp b/core/fpdfapi/page/cpdf_contentparser.cpp
index 46e2c52c75..96bc7a2221 100644
--- a/core/fpdfapi/page/cpdf_contentparser.cpp
+++ b/core/fpdfapi/page/cpdf_contentparser.cpp
@@ -55,8 +55,8 @@ void CPDF_ContentParser::Start(CPDF_Page* pPage) {
}
if (CPDF_Stream* pStream = pContent->AsStream()) {
m_nStreams = 0;
- m_pSingleStream = pdfium::MakeUnique<CPDF_StreamAcc>();
- m_pSingleStream->LoadAllData(pStream, false);
+ m_pSingleStream = pdfium::MakeRetain<CPDF_StreamAcc>(pStream);
+ m_pSingleStream->LoadAllData(false);
} else if (CPDF_Array* pArray = pContent->AsArray()) {
m_nStreams = pArray->GetCount();
if (m_nStreams)
@@ -114,8 +114,8 @@ void CPDF_ContentParser::Start(CPDF_Form* pForm,
pState->SetSoftMask(nullptr);
}
m_nStreams = 0;
- m_pSingleStream = pdfium::MakeUnique<CPDF_StreamAcc>();
- m_pSingleStream->LoadAllData(pForm->m_pFormStream, false);
+ m_pSingleStream = pdfium::MakeRetain<CPDF_StreamAcc>(pForm->m_pFormStream);
+ m_pSingleStream->LoadAllData(false);
m_pData = (uint8_t*)m_pSingleStream->GetData();
m_Size = m_pSingleStream->GetSize();
m_Status = ToBeContinued;
@@ -156,10 +156,11 @@ void CPDF_ContentParser::Continue(IFX_Pause* pPause) {
} else {
CPDF_Array* pContent =
m_pObjectHolder->m_pFormDict->GetArrayFor("Contents");
- m_StreamArray[m_CurrentOffset] = pdfium::MakeUnique<CPDF_StreamAcc>();
CPDF_Stream* pStreamObj = ToStream(
pContent ? pContent->GetDirectObjectAt(m_CurrentOffset) : nullptr);
- m_StreamArray[m_CurrentOffset]->LoadAllData(pStreamObj, false);
+ m_StreamArray[m_CurrentOffset] =
+ pdfium::MakeRetain<CPDF_StreamAcc>(pStreamObj);
+ m_StreamArray[m_CurrentOffset]->LoadAllData(false);
m_CurrentOffset++;
}
}
diff --git a/core/fpdfapi/page/cpdf_contentparser.h b/core/fpdfapi/page/cpdf_contentparser.h
index 5fd11f6c0b..402fd2fdd0 100644
--- a/core/fpdfapi/page/cpdf_contentparser.h
+++ b/core/fpdfapi/page/cpdf_contentparser.h
@@ -12,6 +12,7 @@
#include "core/fpdfapi/page/cpdf_pageobjectholder.h"
#include "core/fpdfapi/page/cpdf_streamcontentparser.h"
+#include "core/fpdfapi/parser/cpdf_stream_acc.h"
class CPDF_AllStates;
class CPDF_Form;
@@ -48,8 +49,8 @@ class CPDF_ContentParser {
bool m_bForm;
CPDF_Type3Char* m_pType3Char;
uint32_t m_nStreams;
- std::unique_ptr<CPDF_StreamAcc> m_pSingleStream;
- std::vector<std::unique_ptr<CPDF_StreamAcc>> m_StreamArray;
+ CFX_RetainPtr<CPDF_StreamAcc> m_pSingleStream;
+ std::vector<CFX_RetainPtr<CPDF_StreamAcc>> m_StreamArray;
uint8_t* m_pData;
uint32_t m_Size;
uint32_t m_CurrentOffset;
diff --git a/core/fpdfapi/page/cpdf_docpagedata.cpp b/core/fpdfapi/page/cpdf_docpagedata.cpp
index a57c42d3cd..5e13740c21 100644
--- a/core/fpdfapi/page/cpdf_docpagedata.cpp
+++ b/core/fpdfapi/page/cpdf_docpagedata.cpp
@@ -96,15 +96,8 @@ void CPDF_DocPageData::Clear(bool bForceRelease) {
for (auto it = m_FontFileMap.begin(); it != m_FontFileMap.end();) {
auto curr_it = it++;
- CPDF_CountedStreamAcc* pCountedFont = curr_it->second;
- if (!pCountedFont->get())
- continue;
-
- if (bForceRelease || pCountedFont->use_count() < 2) {
- delete pCountedFont->get();
- delete pCountedFont;
+ if (bForceRelease || curr_it->second->HasOneRef())
m_FontFileMap.erase(curr_it);
- }
}
m_ImageMap.clear();
@@ -408,11 +401,11 @@ CFX_RetainPtr<CPDF_IccProfile> CPDF_DocPageData::GetIccProfile(
if (it != m_IccProfileMap.end())
return it->second;
- CPDF_StreamAcc accessor;
- accessor.LoadAllData(pProfileStream, false);
+ auto pAccessor = pdfium::MakeRetain<CPDF_StreamAcc>(pProfileStream);
+ pAccessor->LoadAllData(false);
uint8_t digest[20];
- CRYPT_SHA1Generate(accessor.GetData(), accessor.GetSize(), digest);
+ CRYPT_SHA1Generate(pAccessor->GetData(), pAccessor->GetSize(), digest);
CFX_ByteString bsDigest(digest, 20);
auto hash_it = m_HashProfileMap.find(bsDigest);
@@ -422,7 +415,7 @@ CFX_RetainPtr<CPDF_IccProfile> CPDF_DocPageData::GetIccProfile(
return it_copied_stream->second;
}
auto pProfile = pdfium::MakeRetain<CPDF_IccProfile>(
- pProfileStream, accessor.GetData(), accessor.GetSize());
+ pProfileStream, pAccessor->GetData(), pAccessor->GetSize());
m_IccProfileMap[pProfileStream] = pProfile;
m_HashProfileMap[bsDigest] = pProfileStream;
return pProfile;
@@ -435,13 +428,12 @@ void CPDF_DocPageData::MaybePurgeIccProfile(CPDF_Stream* pProfileStream) {
m_IccProfileMap.erase(it);
}
-CPDF_StreamAcc* CPDF_DocPageData::GetFontFileStreamAcc(
+CFX_RetainPtr<CPDF_StreamAcc> CPDF_DocPageData::GetFontFileStreamAcc(
CPDF_Stream* pFontStream) {
ASSERT(pFontStream);
-
auto it = m_FontFileMap.find(pFontStream);
if (it != m_FontFileMap.end())
- return it->second->AddRef();
+ return it->second;
CPDF_Dictionary* pFontDict = pFontStream->GetDict();
int32_t org_size = pFontDict->GetIntegerFor("Length1") +
@@ -449,36 +441,20 @@ CPDF_StreamAcc* CPDF_DocPageData::GetFontFileStreamAcc(
pFontDict->GetIntegerFor("Length3");
org_size = std::max(org_size, 0);
- auto pFontAcc = pdfium::MakeUnique<CPDF_StreamAcc>();
- pFontAcc->LoadAllData(pFontStream, false, org_size);
-
- CPDF_CountedStreamAcc* pCountedFont =
- new CPDF_CountedStreamAcc(std::move(pFontAcc));
- m_FontFileMap[pFontStream] = pCountedFont;
- return pCountedFont->AddRef();
+ auto pFontAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pFontStream);
+ pFontAcc->LoadAllData(false, org_size);
+ m_FontFileMap[pFontStream] = pFontAcc;
+ return pFontAcc;
}
-void CPDF_DocPageData::ReleaseFontFileStreamAcc(
+void CPDF_DocPageData::MaybePurgeFontFileStreamAcc(
const CPDF_Stream* pFontStream) {
if (!pFontStream)
return;
auto it = m_FontFileMap.find(pFontStream);
- if (it == m_FontFileMap.end())
- return;
-
- CPDF_CountedStreamAcc* pCountedStream = it->second;
- if (!pCountedStream)
- return;
-
- pCountedStream->RemoveRef();
- if (pCountedStream->use_count() > 1)
- return;
-
- // We have item only in m_FontFileMap cache. Clean it.
- delete pCountedStream->get();
- delete pCountedStream;
- m_FontFileMap.erase(it);
+ if (it != m_FontFileMap.end() && it->second->HasOneRef())
+ m_FontFileMap.erase(it);
}
CPDF_CountedColorSpace* CPDF_DocPageData::FindColorSpacePtr(
diff --git a/core/fpdfapi/page/cpdf_docpagedata.h b/core/fpdfapi/page/cpdf_docpagedata.h
index 24e0006efd..f5ac303290 100644
--- a/core/fpdfapi/page/cpdf_docpagedata.h
+++ b/core/fpdfapi/page/cpdf_docpagedata.h
@@ -53,15 +53,14 @@ class CPDF_DocPageData {
CFX_RetainPtr<CPDF_IccProfile> GetIccProfile(CPDF_Stream* pProfileStream);
void MaybePurgeIccProfile(CPDF_Stream* pProfileStream);
- CPDF_StreamAcc* GetFontFileStreamAcc(CPDF_Stream* pFontStream);
- void ReleaseFontFileStreamAcc(const CPDF_Stream* pFontStream);
+ CFX_RetainPtr<CPDF_StreamAcc> GetFontFileStreamAcc(CPDF_Stream* pFontStream);
+ void MaybePurgeFontFileStreamAcc(const CPDF_Stream* pFontStream);
CPDF_CountedColorSpace* FindColorSpacePtr(CPDF_Object* pCSObj) const;
CPDF_CountedPattern* FindPatternPtr(CPDF_Object* pPatternObj) const;
private:
using CPDF_CountedFont = CPDF_CountedObject<CPDF_Font>;
- using CPDF_CountedStreamAcc = CPDF_CountedObject<CPDF_StreamAcc>;
CPDF_ColorSpace* GetColorSpaceImpl(CPDF_Object* pCSObj,
const CPDF_Dictionary* pResources,
@@ -71,7 +70,7 @@ class CPDF_DocPageData {
bool m_bForceClear;
std::map<CFX_ByteString, CPDF_Stream*> m_HashProfileMap;
std::map<const CPDF_Object*, CPDF_CountedColorSpace*> m_ColorSpaceMap;
- std::map<const CPDF_Stream*, CPDF_CountedStreamAcc*> m_FontFileMap;
+ std::map<const CPDF_Stream*, CFX_RetainPtr<CPDF_StreamAcc>> m_FontFileMap;
std::map<const CPDF_Dictionary*, CPDF_CountedFont*> m_FontMap;
std::map<const CPDF_Stream*, CFX_RetainPtr<CPDF_IccProfile>> m_IccProfileMap;
std::map<uint32_t, CFX_RetainPtr<CPDF_Image>> m_ImageMap;
diff --git a/core/fpdfapi/page/cpdf_meshstream.cpp b/core/fpdfapi/page/cpdf_meshstream.cpp
index b0a21c627a..c173bc633a 100644
--- a/core/fpdfapi/page/cpdf_meshstream.cpp
+++ b/core/fpdfapi/page/cpdf_meshstream.cpp
@@ -107,14 +107,17 @@ CPDF_MeshStream::CPDF_MeshStream(
m_xmin(0),
m_xmax(0),
m_ymin(0),
- m_ymax(0) {
+ m_ymax(0),
+ m_pStream(pdfium::MakeRetain<CPDF_StreamAcc>(pShadingStream)) {
memset(&m_ColorMin, 0, sizeof(m_ColorMin));
memset(&m_ColorMax, 0, sizeof(m_ColorMax));
}
+CPDF_MeshStream::~CPDF_MeshStream() {}
+
bool CPDF_MeshStream::Load() {
- m_Stream.LoadAllData(m_pShadingStream);
- m_BitStream.Init(m_Stream.GetData(), m_Stream.GetSize());
+ m_pStream->LoadAllData();
+ m_BitStream.Init(m_pStream->GetData(), m_pStream->GetSize());
CPDF_Dictionary* pDict = m_pShadingStream->GetDict();
m_nCoordBits = pDict->GetIntegerFor("BitsPerCoordinate");
m_nComponentBits = pDict->GetIntegerFor("BitsPerComponent");
diff --git a/core/fpdfapi/page/cpdf_meshstream.h b/core/fpdfapi/page/cpdf_meshstream.h
index e58e354b35..e02c3bb4e8 100644
--- a/core/fpdfapi/page/cpdf_meshstream.h
+++ b/core/fpdfapi/page/cpdf_meshstream.h
@@ -39,6 +39,7 @@ class CPDF_MeshStream {
const std::vector<std::unique_ptr<CPDF_Function>>& funcs,
CPDF_Stream* pShadingStream,
CPDF_ColorSpace* pCS);
+ ~CPDF_MeshStream();
bool Load();
@@ -80,7 +81,7 @@ class CPDF_MeshStream {
float m_ymax;
float m_ColorMin[kMaxComponents];
float m_ColorMax[kMaxComponents];
- CPDF_StreamAcc m_Stream;
+ CFX_RetainPtr<CPDF_StreamAcc> m_pStream;
CFX_BitStream m_BitStream;
};
diff --git a/core/fpdfapi/page/fpdf_page_func.cpp b/core/fpdfapi/page/fpdf_page_func.cpp
index 9be1edb7ff..bc66adf726 100644
--- a/core/fpdfapi/page/fpdf_page_func.cpp
+++ b/core/fpdfapi/page/fpdf_page_func.cpp
@@ -90,10 +90,10 @@ class CPDF_PSFunc : public CPDF_Function {
};
bool CPDF_PSFunc::v_Init(CPDF_Object* pObj) {
- CPDF_StreamAcc acc;
- acc.LoadAllData(pObj->AsStream(), false);
- return m_PS.Parse(reinterpret_cast<const char*>(acc.GetData()),
- acc.GetSize());
+ auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pObj->AsStream());
+ pAcc->LoadAllData(false);
+ return m_PS.Parse(reinterpret_cast<const char*>(pAcc->GetData()),
+ pAcc->GetSize());
}
bool CPDF_PSFunc::v_Call(float* inputs, float* results) const {
@@ -487,8 +487,8 @@ bool CPDF_SampledFunc::v_Init(CPDF_Object* pObj) {
return false;
m_SampleMax = 0xffffffff >> (32 - m_nBitsPerSample);
- m_pSampleStream = pdfium::MakeUnique<CPDF_StreamAcc>();
- m_pSampleStream->LoadAllData(pStream, false);
+ m_pSampleStream = pdfium::MakeRetain<CPDF_StreamAcc>(pStream);
+ m_pSampleStream->LoadAllData(false);
FX_SAFE_UINT32 nTotalSampleBits = 1;
m_EncodeInfo.resize(m_nInputs);
for (uint32_t i = 0; i < m_nInputs; i++) {
diff --git a/core/fpdfapi/page/pageint.h b/core/fpdfapi/page/pageint.h
index a7a00f02ee..05865ca58e 100644
--- a/core/fpdfapi/page/pageint.h
+++ b/core/fpdfapi/page/pageint.h
@@ -12,14 +12,13 @@
#include "core/fpdfapi/page/cpdf_colorspace.h"
#include "core/fpdfapi/page/cpdf_countedobject.h"
+#include "core/fpdfapi/parser/cpdf_stream_acc.h"
#include "core/fxcrt/cfx_retain_ptr.h"
class CPDF_ExpIntFunc;
class CPDF_Pattern;
class CPDF_SampledFunc;
class CPDF_StitchFunc;
-class CPDF_Stream;
-class CPDF_StreamAcc;
class CPDF_Function {
public:
@@ -102,8 +101,8 @@ class CPDF_SampledFunc : public CPDF_Function {
return m_EncodeInfo;
}
uint32_t GetBitsPerSample() const { return m_nBitsPerSample; }
- const CPDF_StreamAcc* GetSampleStream() const {
- return m_pSampleStream.get();
+ CFX_RetainPtr<CPDF_StreamAcc> GetSampleStream() const {
+ return m_pSampleStream;
}
private:
@@ -111,7 +110,7 @@ class CPDF_SampledFunc : public CPDF_Function {
std::vector<SampleDecodeInfo> m_DecodeInfo;
uint32_t m_nBitsPerSample;
uint32_t m_SampleMax;
- std::unique_ptr<CPDF_StreamAcc> m_pSampleStream;
+ CFX_RetainPtr<CPDF_StreamAcc> m_pSampleStream;
};
class CPDF_StitchFunc : public CPDF_Function {
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_StreamAcc> 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<CPDF_Image> LoadImageFromPageData(uint32_t dwStreamObjNum);
- CPDF_StreamAcc* LoadFontFile(CPDF_Stream* pStream);
+ CFX_RetainPtr<CPDF_StreamAcc> LoadFontFile(CPDF_Stream* pStream);
CFX_RetainPtr<CPDF_IccProfile> 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<CPDF_StreamAcc>(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<CPDF_StreamAcc>& pObjStream) {
return pObjStream->GetDict()->GetIntegerFor("N");
}
-int32_t GetStreamFirst(CPDF_StreamAcc* pObjStream) {
+int32_t GetStreamFirst(const CFX_RetainPtr<CPDF_StreamAcc>& 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<CPDF_StreamAcc>(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_Object> CPDF_Parser::ParseIndirectObject(
if (GetObjectType(objnum) != 2)
return nullptr;
- CPDF_StreamAcc* pObjStream = GetObjectStream(m_ObjectInfo[objnum].pos);
+ CFX_RetainPtr<CPDF_StreamAcc> pObjStream =
+ GetObjectStream(m_ObjectInfo[objnum].pos);
if (!pObjStream)
return nullptr;
@@ -1141,10 +1142,10 @@ std::unique_ptr<CPDF_Object> CPDF_Parser::ParseIndirectObject(
return syntax.GetObject(pObjList, 0, 0, true);
}
-CPDF_StreamAcc* CPDF_Parser::GetObjectStream(uint32_t objnum) {
+CFX_RetainPtr<CPDF_StreamAcc> 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<CPDF_StreamAcc>(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<CPDF_StreamAcc> 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<CPDF_StreamAcc> GetObjectStream(uint32_t number);
bool IsLinearizedFile(
const CFX_RetainPtr<IFX_SeekableReadStream>& pFileAccess,
uint32_t offset);
@@ -165,8 +165,8 @@ class CPDF_Parser {
std::unique_ptr<CPDF_LinearizedHeader> m_pLinearized;
uint32_t m_dwXrefStartObjNum;
- // A map of object numbers to indirect streams. Map owns the streams.
- std::map<uint32_t, std::unique_ptr<CPDF_StreamAcc>> m_ObjectStreamMap;
+ // A map of object numbers to indirect streams.
+ std::map<uint32_t, CFX_RetainPtr<CPDF_StreamAcc>> 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<CPDF_StreamAcc*, StreamObjectCache> m_ObjCache;
+ std::map<CFX_RetainPtr<CPDF_StreamAcc>, StreamObjectCache> m_ObjCache;
// All indirect object numbers that are being parsed.
std::set<uint32_t> 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_Object> CPDF_Stream::CloneNonCyclic(
bool bDirect,
std::set<const CPDF_Object*>* pVisited) const {
pVisited->insert(this);
- CPDF_StreamAcc acc;
- acc.LoadAllData(this, true);
+ auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(this);
+ pAcc->LoadAllData(true);
- uint32_t streamSize = acc.GetSize();
+ uint32_t streamSize = pAcc->GetSize();
CPDF_Dictionary* pDict = GetDict();
std::unique_ptr<CPDF_Dictionary> pNewDict;
if (pDict && !pdfium::ContainsKey(*pVisited, pDict)) {
pNewDict = ToDictionary(
static_cast<CPDF_Object*>(pDict)->CloneNonCyclic(bDirect, pVisited));
}
- return pdfium::MakeUnique<CPDF_Stream>(acc.DetachData(), streamSize,
+ return pdfium::MakeUnique<CPDF_Stream>(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<CPDF_StreamAcc>(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 <typename T, typename... Args>
+ friend CFX_RetainPtr<T> 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<uint8_t, FxFreeDeleter> 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<CPDF_StreamAcc>(p);
+ pAcc->LoadAllData(true);
+ buf.AppendBlock(pAcc->GetData(), pAcc->GetSize());
buf << "\r\nendstream";
break;
}
diff --git a/core/fpdfapi/render/cpdf_dibsource.cpp b/core/fpdfapi/render/cpdf_dibsource.cpp
index 12bfadb19d..e60d0329d0 100644
--- a/core/fpdfapi/render/cpdf_dibsource.cpp
+++ b/core/fpdfapi/render/cpdf_dibsource.cpp
@@ -165,8 +165,8 @@ bool CPDF_DIBSource::Load(CPDF_Document* pDoc, const CPDF_Stream* pStream) {
if (!src_size.IsValid())
return false;
- m_pStreamAcc = pdfium::MakeUnique<CPDF_StreamAcc>();
- m_pStreamAcc->LoadAllData(pStream, false, src_size.ValueOrDie(), true);
+ m_pStreamAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pStream);
+ m_pStreamAcc->LoadAllData(false, src_size.ValueOrDie(), true);
if (m_pStreamAcc->GetSize() == 0 || !m_pStreamAcc->GetData())
return false;
@@ -278,8 +278,8 @@ int CPDF_DIBSource::StartLoadDIBSource(CPDF_Document* pDoc,
if (!src_size.IsValid()) {
return 0;
}
- m_pStreamAcc = pdfium::MakeUnique<CPDF_StreamAcc>();
- m_pStreamAcc->LoadAllData(pStream, false, src_size.ValueOrDie(), true);
+ m_pStreamAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pStream);
+ m_pStreamAcc->LoadAllData(false, src_size.ValueOrDie(), true);
if (m_pStreamAcc->GetSize() == 0 || !m_pStreamAcc->GetData()) {
return 0;
}
@@ -325,17 +325,17 @@ int CPDF_DIBSource::ContinueLoadDIBSource(IFX_Pause* pPause) {
CPDF_Stream* pGlobals =
m_pStreamAcc->GetImageParam()->GetStreamFor("JBIG2Globals");
if (pGlobals) {
- m_pGlobalStream = pdfium::MakeUnique<CPDF_StreamAcc>();
- m_pGlobalStream->LoadAllData(pGlobals, false);
+ m_pGlobalStream = pdfium::MakeRetain<CPDF_StreamAcc>(pGlobals);
+ m_pGlobalStream->LoadAllData(false);
}
}
ret = pJbig2Module->StartDecode(
m_pJbig2Context.get(), m_pDocument->CodecContext(), m_Width, m_Height,
- m_pStreamAcc.get(), m_pGlobalStream.get(),
- m_pCachedBitmap->GetBuffer(), m_pCachedBitmap->GetPitch(), pPause);
+ m_pStreamAcc, m_pGlobalStream, m_pCachedBitmap->GetBuffer(),
+ m_pCachedBitmap->GetPitch(), pPause);
if (ret < 0) {
m_pCachedBitmap.Reset();
- m_pGlobalStream.reset();
+ m_pGlobalStream.Reset();
m_pJbig2Context.reset();
return 0;
}
@@ -358,7 +358,7 @@ int CPDF_DIBSource::ContinueLoadDIBSource(IFX_Pause* pPause) {
ret = pJbig2Module->ContinueDecode(m_pJbig2Context.get(), pPause);
if (ret < 0) {
m_pCachedBitmap.Reset();
- m_pGlobalStream.reset();
+ m_pGlobalStream.Reset();
m_pJbig2Context.reset();
return 0;
}
diff --git a/core/fpdfapi/render/cpdf_dibsource.h b/core/fpdfapi/render/cpdf_dibsource.h
index c77aac1776..f484f57d75 100644
--- a/core/fpdfapi/render/cpdf_dibsource.h
+++ b/core/fpdfapi/render/cpdf_dibsource.h
@@ -118,7 +118,7 @@ class CPDF_DIBSource : public CFX_DIBSource {
CPDF_Document* m_pDocument;
const CPDF_Stream* m_pStream;
- std::unique_ptr<CPDF_StreamAcc> m_pStreamAcc;
+ CFX_RetainPtr<CPDF_StreamAcc> m_pStreamAcc;
const CPDF_Dictionary* m_pDict;
CPDF_ColorSpace* m_pColorSpace;
uint32_t m_Family;
@@ -139,8 +139,8 @@ class CPDF_DIBSource : public CFX_DIBSource {
uint8_t* m_pMaskedLine;
CFX_RetainPtr<CFX_DIBitmap> m_pCachedBitmap;
CFX_RetainPtr<CPDF_DIBSource> m_pMask;
+ CFX_RetainPtr<CPDF_StreamAcc> m_pGlobalStream;
std::unique_ptr<CCodec_ScanlineDecoder> m_pDecoder;
- std::unique_ptr<CPDF_StreamAcc> m_pGlobalStream;
std::unique_ptr<CCodec_Jbig2Context> m_pJbig2Context;
CPDF_Stream* m_pMaskStream;
int m_Status;
diff --git a/core/fpdfdoc/cpdf_metadata.cpp b/core/fpdfdoc/cpdf_metadata.cpp
index 3780d1587d..5379275544 100644
--- a/core/fpdfdoc/cpdf_metadata.cpp
+++ b/core/fpdfdoc/cpdf_metadata.cpp
@@ -20,9 +20,9 @@ CPDF_Metadata::CPDF_Metadata(CPDF_Document* pDoc) {
if (!pStream)
return;
- CPDF_StreamAcc acc;
- acc.LoadAllData(pStream, false);
- m_pXmlElement = CXML_Element::Parse(acc.GetData(), acc.GetSize());
+ auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pStream);
+ pAcc->LoadAllData(false);
+ m_pXmlElement = CXML_Element::Parse(pAcc->GetData(), pAcc->GetSize());
}
CPDF_Metadata::~CPDF_Metadata() {}
diff --git a/core/fxcodec/codec/ccodec_jbig2module.h b/core/fxcodec/codec/ccodec_jbig2module.h
index c74ed5fb02..e4e0af35da 100644
--- a/core/fxcodec/codec/ccodec_jbig2module.h
+++ b/core/fxcodec/codec/ccodec_jbig2module.h
@@ -25,8 +25,8 @@ class CCodec_Jbig2Context {
uint32_t m_width;
uint32_t m_height;
- CPDF_StreamAcc* m_pGlobalStream;
- CPDF_StreamAcc* m_pSrcStream;
+ CFX_RetainPtr<CPDF_StreamAcc> m_pGlobalStream;
+ CFX_RetainPtr<CPDF_StreamAcc> m_pSrcStream;
uint8_t* m_dest_buf;
uint32_t m_dest_pitch;
IFX_Pause* m_pPause;
@@ -43,8 +43,8 @@ class CCodec_Jbig2Module {
std::unique_ptr<JBig2_DocumentContext>* pContextHolder,
uint32_t width,
uint32_t height,
- CPDF_StreamAcc* src_stream,
- CPDF_StreamAcc* global_stream,
+ const CFX_RetainPtr<CPDF_StreamAcc>& src_stream,
+ const CFX_RetainPtr<CPDF_StreamAcc>& global_stream,
uint8_t* dest_buf,
uint32_t dest_pitch,
IFX_Pause* pPause);
diff --git a/core/fxcodec/codec/fx_codec_jbig.cpp b/core/fxcodec/codec/fx_codec_jbig.cpp
index dc82305616..951fec4813 100644
--- a/core/fxcodec/codec/fx_codec_jbig.cpp
+++ b/core/fxcodec/codec/fx_codec_jbig.cpp
@@ -45,8 +45,8 @@ FXCODEC_STATUS CCodec_Jbig2Module::StartDecode(
std::unique_ptr<JBig2_DocumentContext>* pContextHolder,
uint32_t width,
uint32_t height,
- CPDF_StreamAcc* src_stream,
- CPDF_StreamAcc* global_stream,
+ const CFX_RetainPtr<CPDF_StreamAcc>& src_stream,
+ const CFX_RetainPtr<CPDF_StreamAcc>& global_stream,
uint8_t* dest_buf,
uint32_t dest_pitch,
IFX_Pause* pPause) {
diff --git a/core/fxcodec/jbig2/JBig2_BitStream.cpp b/core/fxcodec/jbig2/JBig2_BitStream.cpp
index 67d82f49a7..921fa2a661 100644
--- a/core/fxcodec/jbig2/JBig2_BitStream.cpp
+++ b/core/fxcodec/jbig2/JBig2_BitStream.cpp
@@ -11,7 +11,8 @@
#include "core/fpdfapi/parser/cpdf_stream.h"
#include "core/fpdfapi/parser/cpdf_stream_acc.h"
-CJBig2_BitStream::CJBig2_BitStream(CPDF_StreamAcc* pSrcStream)
+CJBig2_BitStream::CJBig2_BitStream(
+ const CFX_RetainPtr<CPDF_StreamAcc>& pSrcStream)
: m_pBuf(pSrcStream->GetData()),
m_dwLength(pSrcStream->GetSize()),
m_dwByteIdx(0),
diff --git a/core/fxcodec/jbig2/JBig2_BitStream.h b/core/fxcodec/jbig2/JBig2_BitStream.h
index 551b7df9b1..7517eb0c6c 100644
--- a/core/fxcodec/jbig2/JBig2_BitStream.h
+++ b/core/fxcodec/jbig2/JBig2_BitStream.h
@@ -7,13 +7,14 @@
#ifndef CORE_FXCODEC_JBIG2_JBIG2_BITSTREAM_H_
#define CORE_FXCODEC_JBIG2_JBIG2_BITSTREAM_H_
+#include "core/fxcrt/cfx_retain_ptr.h"
#include "core/fxcrt/fx_basic.h"
class CPDF_StreamAcc;
class CJBig2_BitStream {
public:
- explicit CJBig2_BitStream(CPDF_StreamAcc* pSrcStream);
+ explicit CJBig2_BitStream(const CFX_RetainPtr<CPDF_StreamAcc>& pSrcStream);
~CJBig2_BitStream();
// TODO(thestig): readFoo() should return bool.
diff --git a/core/fxcodec/jbig2/JBig2_BitStream_unittest.cpp b/core/fxcodec/jbig2/JBig2_BitStream_unittest.cpp
index f9d69b9c53..7b91eff982 100644
--- a/core/fxcodec/jbig2/JBig2_BitStream_unittest.cpp
+++ b/core/fxcodec/jbig2/JBig2_BitStream_unittest.cpp
@@ -16,10 +16,10 @@ TEST(JBig2_BitStream, ReadNBits) {
data.get()[0] = 0xb1; // 10110001
auto in_stream = pdfium::MakeUnique<CPDF_Stream>(std::move(data), 1, nullptr);
- auto acc = pdfium::MakeUnique<CPDF_StreamAcc>();
- acc->LoadAllData(in_stream.get());
+ auto acc = pdfium::MakeRetain<CPDF_StreamAcc>(in_stream.get());
+ acc->LoadAllData();
- CJBig2_BitStream stream(acc.get());
+ CJBig2_BitStream stream(acc);
uint32_t val1;
EXPECT_EQ(0, stream.readNBits(1, &val1));
@@ -41,10 +41,10 @@ TEST(JBig2_BitStream, ReadNBitsLargerThenData) {
data.get()[0] = 0xb1;
auto in_stream = pdfium::MakeUnique<CPDF_Stream>(std::move(data), 1, nullptr);
- auto acc = pdfium::MakeUnique<CPDF_StreamAcc>();
- acc->LoadAllData(in_stream.get());
+ auto acc = pdfium::MakeRetain<CPDF_StreamAcc>(in_stream.get());
+ acc->LoadAllData();
- CJBig2_BitStream stream(acc.get());
+ CJBig2_BitStream stream(acc);
uint32_t val1;
EXPECT_EQ(0, stream.readNBits(10, &val1));
@@ -53,10 +53,10 @@ TEST(JBig2_BitStream, ReadNBitsLargerThenData) {
TEST(JBig2_BitStream, ReadNBitsNullStream) {
auto in_stream = pdfium::MakeUnique<CPDF_Stream>(nullptr, 0, nullptr);
- auto acc = pdfium::MakeUnique<CPDF_StreamAcc>();
- acc->LoadAllData(in_stream.get());
+ auto acc = pdfium::MakeRetain<CPDF_StreamAcc>(in_stream.get());
+ acc->LoadAllData();
- CJBig2_BitStream stream(acc.get());
+ CJBig2_BitStream stream(acc);
uint32_t val1;
EXPECT_EQ(-1, stream.readNBits(1, &val1));
@@ -69,10 +69,10 @@ TEST(JBig2_BitStream, ReadNBitsEmptyStream) {
std::unique_ptr<uint8_t, FxFreeDeleter> data(FX_Alloc(uint8_t, 1));
auto in_stream = pdfium::MakeUnique<CPDF_Stream>(std::move(data), 0, nullptr);
- auto acc = pdfium::MakeUnique<CPDF_StreamAcc>();
- acc->LoadAllData(in_stream.get());
+ auto acc = pdfium::MakeRetain<CPDF_StreamAcc>(in_stream.get());
+ acc->LoadAllData();
- CJBig2_BitStream stream(acc.get());
+ CJBig2_BitStream stream(acc);
uint32_t val1;
EXPECT_EQ(-1, stream.readNBits(1, &val1));
@@ -86,10 +86,10 @@ TEST(JBig2_BitStream, ReadNBitsOutOfBounds) {
data.get()[0] = 0xb1; // 10110001
auto in_stream = pdfium::MakeUnique<CPDF_Stream>(std::move(data), 1, nullptr);
- auto acc = pdfium::MakeUnique<CPDF_StreamAcc>();
- acc->LoadAllData(in_stream.get());
+ auto acc = pdfium::MakeRetain<CPDF_StreamAcc>(in_stream.get());
+ acc->LoadAllData();
- CJBig2_BitStream stream(acc.get());
+ CJBig2_BitStream stream(acc);
uint32_t val1;
EXPECT_EQ(0, stream.readNBits(8, &val1));
@@ -107,10 +107,10 @@ TEST(JBig2_BitStream, ReadNBitsWhereNIs36) {
data.get()[4] = 0x40;
auto in_stream = pdfium::MakeUnique<CPDF_Stream>(std::move(data), 5, nullptr);
- auto acc = pdfium::MakeUnique<CPDF_StreamAcc>();
- acc->LoadAllData(in_stream.get());
+ auto acc = pdfium::MakeRetain<CPDF_StreamAcc>(in_stream.get());
+ acc->LoadAllData();
- CJBig2_BitStream stream(acc.get());
+ CJBig2_BitStream stream(acc);
// This will shift off the top two bits and they end up lost.
uint32_t val1;
diff --git a/core/fxcodec/jbig2/JBig2_Context.cpp b/core/fxcodec/jbig2/JBig2_Context.cpp
index 9261d16bfc..116abb7754 100644
--- a/core/fxcodec/jbig2/JBig2_Context.cpp
+++ b/core/fxcodec/jbig2/JBig2_Context.cpp
@@ -46,11 +46,12 @@ size_t GetRefAggContextSize(bool val) {
// difference for typical JBIG2 documents.
static const int kSymbolDictCacheMaxSize = 2;
-CJBig2_Context::CJBig2_Context(CPDF_StreamAcc* pGlobalStream,
- CPDF_StreamAcc* pSrcStream,
- std::list<CJBig2_CachePair>* pSymbolDictCache,
- IFX_Pause* pPause,
- bool bIsGlobal)
+CJBig2_Context::CJBig2_Context(
+ const CFX_RetainPtr<CPDF_StreamAcc>& pGlobalStream,
+ const CFX_RetainPtr<CPDF_StreamAcc>& pSrcStream,
+ std::list<CJBig2_CachePair>* pSymbolDictCache,
+ IFX_Pause* pPause,
+ bool bIsGlobal)
: m_nSegmentDecoded(0),
m_bInPage(false),
m_bBufSpecified(false),
diff --git a/core/fxcodec/jbig2/JBig2_Context.h b/core/fxcodec/jbig2/JBig2_Context.h
index cd44a82c61..31865d4d46 100644
--- a/core/fxcodec/jbig2/JBig2_Context.h
+++ b/core/fxcodec/jbig2/JBig2_Context.h
@@ -40,8 +40,8 @@ using CJBig2_CachePair =
class CJBig2_Context {
public:
- CJBig2_Context(CPDF_StreamAcc* pGlobalStream,
- CPDF_StreamAcc* pSrcStream,
+ CJBig2_Context(const CFX_RetainPtr<CPDF_StreamAcc>& pGlobalStream,
+ const CFX_RetainPtr<CPDF_StreamAcc>& pSrcStream,
std::list<CJBig2_CachePair>* pSymbolDictCache,
IFX_Pause* pPause,
bool bIsGlobal);
diff --git a/fpdfsdk/fpdf_flatten.cpp b/fpdfsdk/fpdf_flatten.cpp
index 8e181eb380..31b838cd94 100644
--- a/fpdfsdk/fpdf_flatten.cpp
+++ b/fpdfsdk/fpdf_flatten.cpp
@@ -204,11 +204,11 @@ void SetPageContents(const CFX_ByteString& key,
pPage->ConvertToIndirectObjectFor("Contents", pDocument);
if (!pContentsArray) {
pContentsArray = pDocument->NewIndirect<CPDF_Array>();
- CPDF_StreamAcc acc;
- acc.LoadAllData(pContentsStream);
+ auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pContentsStream);
+ pAcc->LoadAllData();
CFX_ByteString sStream = "q\n";
CFX_ByteString sBody =
- CFX_ByteString((const char*)acc.GetData(), acc.GetSize());
+ CFX_ByteString((const char*)pAcc->GetData(), pAcc->GetSize());
sStream = sStream + sBody + "\nQ";
pContentsStream->SetData(sStream.raw_str(), sStream.GetLength());
pContentsArray->AddNew<CPDF_Reference>(pDocument,
@@ -402,11 +402,9 @@ DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) {
pXObject->SetNewFor<CPDF_Reference>(sFormName, pDocument,
pObj->GetObjNum());
- CPDF_StreamAcc acc;
- acc.LoadAllData(pNewXObject);
-
- const uint8_t* pData = acc.GetData();
- CFX_ByteString sStream(pData, acc.GetSize());
+ auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pNewXObject);
+ pAcc->LoadAllData();
+ CFX_ByteString sStream(pAcc->GetData(), pAcc->GetSize());
CFX_Matrix matrix = pAPDic->GetMatrixFor("Matrix");
if (matrix.IsIdentity()) {
matrix.a = 1.0f;
diff --git a/fpdfsdk/fpdfsave.cpp b/fpdfsdk/fpdfsave.cpp
index 6894f8c2ed..004c921a52 100644
--- a/fpdfsdk/fpdfsave.cpp
+++ b/fpdfsdk/fpdfsave.cpp
@@ -137,12 +137,10 @@ bool SaveXFADocumentData(
// template
if (iTemplate > -1) {
CPDF_Stream* pTemplateStream = pArray->GetStreamAt(iTemplate);
- CPDF_StreamAcc streamAcc;
- streamAcc.LoadAllData(pTemplateStream);
- uint8_t* pData = (uint8_t*)streamAcc.GetData();
- uint32_t dwSize2 = streamAcc.GetSize();
- CFX_RetainPtr<IFX_SeekableStream> pTemplate =
- IFX_MemoryStream::Create(pData, dwSize2);
+ auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pTemplateStream);
+ pAcc->LoadAllData();
+ CFX_RetainPtr<IFX_SeekableStream> pTemplate = IFX_MemoryStream::Create(
+ const_cast<uint8_t*>(pAcc->GetData()), pAcc->GetSize());
pChecksum->UpdateChecksum(pTemplate);
}
CPDF_Stream* pFormStream = nullptr;
diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp b/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp
index 4c8591e2eb..bd16f43ffc 100644
--- a/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp
+++ b/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp
@@ -497,8 +497,8 @@ void CPDFXFA_DocEnvironment::ExportData(CXFA_FFDoc* hDoc,
fileWrite->WriteBlock(content.c_str(), fileWrite->GetSize(),
content.GetLength());
}
- auto pAcc = pdfium::MakeUnique<CPDF_StreamAcc>();
- pAcc->LoadAllData(pStream);
+ auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pStream);
+ pAcc->LoadAllData();
fileWrite->WriteBlock(pAcc->GetData(), fileWrite->GetSize(),
pAcc->GetSize());
}
diff --git a/testing/libfuzzer/pdf_codec_jbig2_fuzzer.cc b/testing/libfuzzer/pdf_codec_jbig2_fuzzer.cc
index d23e3e07fa..9378141ffa 100644
--- a/testing/libfuzzer/pdf_codec_jbig2_fuzzer.cc
+++ b/testing/libfuzzer/pdf_codec_jbig2_fuzzer.cc
@@ -33,14 +33,15 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
auto stream = pdfium::MakeUnique<CPDF_Stream>();
stream->AsStream()->SetData(data, size);
- CPDF_StreamAcc src_stream;
- src_stream.LoadAllData(stream->AsStream(), true);
+
+ auto src_stream = pdfium::MakeRetain<CPDF_StreamAcc>(stream->AsStream());
+ src_stream->LoadAllData(true);
CCodec_Jbig2Module module;
CCodec_Jbig2Context jbig2_context;
std::unique_ptr<JBig2_DocumentContext> document_context;
FXCODEC_STATUS status = module.StartDecode(
- &jbig2_context, &document_context, width, height, &src_stream, nullptr,
+ &jbig2_context, &document_context, width, height, src_stream, nullptr,
bitmap->GetBuffer(), bitmap->GetPitch(), nullptr);
while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE)
diff --git a/xfa/fxfa/cxfa_ffapp.cpp b/xfa/fxfa/cxfa_ffapp.cpp
index 78de94e938..7b88626c91 100644
--- a/xfa/fxfa/cxfa_ffapp.cpp
+++ b/xfa/fxfa/cxfa_ffapp.cpp
@@ -35,13 +35,13 @@ class CXFA_FileRead : public IFX_SeekableReadStream {
bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override;
private:
- std::vector<std::unique_ptr<CPDF_StreamAcc>> m_Data;
+ std::vector<CFX_RetainPtr<CPDF_StreamAcc>> m_Data;
};
CXFA_FileRead::CXFA_FileRead(const std::vector<CPDF_Stream*>& streams) {
for (CPDF_Stream* pStream : streams) {
- m_Data.push_back(pdfium::MakeUnique<CPDF_StreamAcc>());
- m_Data.back()->LoadAllData(pStream);
+ m_Data.push_back(pdfium::MakeRetain<CPDF_StreamAcc>(pStream));
+ m_Data.back()->LoadAllData();
}
}
diff --git a/xfa/fxfa/cxfa_ffdoc.cpp b/xfa/fxfa/cxfa_ffdoc.cpp
index f48ae57fb2..993f5bc664 100644
--- a/xfa/fxfa/cxfa_ffdoc.cpp
+++ b/xfa/fxfa/cxfa_ffdoc.cpp
@@ -388,12 +388,12 @@ CFX_RetainPtr<CFX_DIBitmap> CXFA_FFDoc::GetPDFNamedImage(
if (!pStream)
return nullptr;
- CPDF_StreamAcc streamAcc;
- streamAcc.LoadAllData(pStream);
+ auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pStream);
+ pAcc->LoadAllData();
CFX_RetainPtr<IFX_SeekableReadStream> pImageFileRead =
- IFX_MemoryStream::Create((uint8_t*)streamAcc.GetData(),
- streamAcc.GetSize());
+ IFX_MemoryStream::Create(const_cast<uint8_t*>(pAcc->GetData()),
+ pAcc->GetSize());
CFX_RetainPtr<CFX_DIBitmap> pDibSource = XFA_LoadImageFromBuffer(
pImageFileRead, FXCODEC_IMAGE_UNKNOWN, iImageXDpi, iImageYDpi);