diff options
author | Tom Sepez <tsepez@chromium.org> | 2016-10-14 17:45:56 -0700 |
---|---|---|
committer | Tom Sepez <tsepez@chromium.org> | 2016-10-14 17:45:56 -0700 |
commit | c25a4219431c90a95233a08f25eecc921abbf3ed (patch) | |
tree | bd2ad923d527440785c19623fe2d19cc1a70a92d | |
parent | 62f367348ff8ff1e64c6f52c1ee5f77c3a89edc1 (diff) | |
download | pdfium-c25a4219431c90a95233a08f25eecc921abbf3ed.tar.xz |
Revert "Update CPDF_IndirectObjectHolder APIs for unique objects."
This reverts commit 3ba098595ae56b64eacc0c25ab76b89a4d78d920.
TBR=thestig@chromium.org,weili@chromium.org
Review URL: https://codereview.chromium.org/2424533003 .
28 files changed, 181 insertions, 222 deletions
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp index 654b69ec4a..ad1ffaf687 100644 --- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp +++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp @@ -52,9 +52,10 @@ void CPDF_PageContentGenerator::GenerateContent() { if (pContent) pPageDict->RemoveFor("Contents"); - CPDF_Stream* pStream = m_pDocument->AddIndirectStream(); + CPDF_Stream* pStream = new CPDF_Stream; pStream->SetData(buf.GetBuffer(), buf.GetLength()); - pPageDict->SetReferenceFor("Contents", m_pDocument, pStream); + pPageDict->SetReferenceFor("Contents", m_pDocument, + m_pDocument->AddIndirectObject(pStream)); } CFX_ByteString CPDF_PageContentGenerator::RealizeResource( @@ -62,9 +63,10 @@ CFX_ByteString CPDF_PageContentGenerator::RealizeResource( const CFX_ByteString& bsType) { if (!m_pPage->m_pResources) { m_pPage->m_pResources = - m_pDocument->AddIndirectDictionary(m_pDocument->GetByteStringPool()); - m_pPage->m_pFormDict->SetReferenceFor("Resources", m_pDocument, - m_pPage->m_pResources); + new CPDF_Dictionary(m_pDocument->GetByteStringPool()); + m_pPage->m_pFormDict->SetReferenceFor( + "Resources", m_pDocument, + m_pDocument->AddIndirectObject(m_pPage->m_pResources)); } CPDF_Dictionary* pResList = m_pPage->m_pResources->GetDictFor(bsType); if (!pResList) { @@ -80,9 +82,8 @@ CFX_ByteString CPDF_PageContentGenerator::RealizeResource( } idnum++; } - // TODO(tsepez): check |pResourceObj| ownership. - pResList->SetReferenceFor(name, m_pDocument, m_pDocument->AddIndirectObject( - UniqueObject(pResourceObj))); + pResList->SetReferenceFor(name, m_pDocument, + m_pDocument->AddIndirectObject(pResourceObj)); return name; } @@ -169,7 +170,8 @@ void CPDF_PageContentGenerator::TransformContent(CFX_Matrix& matrix) { contentStream.LoadAllData(pStream); ProcessForm(buf, contentStream.GetData(), contentStream.GetSize(), matrix); } - CPDF_Stream* pStream = m_pDocument->AddIndirectStream(); + CPDF_Stream* pStream = new CPDF_Stream; pStream->SetData(buf.GetBuffer(), buf.GetLength()); - m_pPage->m_pFormDict->SetReferenceFor("Contents", m_pDocument, pStream); + m_pPage->m_pFormDict->SetReferenceFor( + "Contents", m_pDocument, m_pDocument->AddIndirectObject(pStream)); } diff --git a/core/fpdfapi/edit/fpdf_edit_create.cpp b/core/fpdfapi/edit/fpdf_edit_create.cpp index 5622b1dae7..fae2b14449 100644 --- a/core/fpdfapi/edit/fpdf_edit_create.cpp +++ b/core/fpdfapi/edit/fpdf_edit_create.cpp @@ -1244,10 +1244,12 @@ int32_t CPDF_Creator::WriteOldIndirectObject(uint32_t objnum) { m_ObjectOffset[objnum] = 0; return 0; } - if (WriteIndirectObj(pObj)) + if (WriteIndirectObj(pObj)) { return -1; - if (!bExistInMap) - m_pDocument->DeleteIndirectObject(objnum); + } + if (!bExistInMap) { + m_pDocument->ReleaseIndirectObject(objnum); + } } else { uint8_t* pBuffer; uint32_t size; diff --git a/core/fpdfapi/page/cpdf_docpagedata.cpp b/core/fpdfapi/page/cpdf_docpagedata.cpp index 0114c680f2..5f1f561ebb 100644 --- a/core/fpdfapi/page/cpdf_docpagedata.cpp +++ b/core/fpdfapi/page/cpdf_docpagedata.cpp @@ -175,8 +175,7 @@ CPDF_Font* CPDF_DocPageData::GetStandardFont(const CFX_ByteString& fontName, return fontData->AddRef(); } - CPDF_Dictionary* pDict = - m_pPDFDoc->AddIndirectDictionary(m_pPDFDoc->GetByteStringPool()); + CPDF_Dictionary* pDict = new CPDF_Dictionary(m_pPDFDoc->GetByteStringPool()); pDict->SetNameFor("Type", "Font"); pDict->SetNameFor("Subtype", "Type1"); pDict->SetNameFor("BaseFont", fontName); @@ -184,6 +183,7 @@ CPDF_Font* CPDF_DocPageData::GetStandardFont(const CFX_ByteString& fontName, pDict->SetFor("Encoding", pEncoding->Realize(m_pPDFDoc->GetByteStringPool())); } + m_pPDFDoc->AddIndirectObject(pDict); std::unique_ptr<CPDF_Font> pFont = CPDF_Font::Create(m_pPDFDoc, pDict); if (!pFont) return nullptr; diff --git a/core/fpdfapi/page/cpdf_image.cpp b/core/fpdfapi/page/cpdf_image.cpp index 2755381de4..50768c5058 100644 --- a/core/fpdfapi/page/cpdf_image.cpp +++ b/core/fpdfapi/page/cpdf_image.cpp @@ -204,7 +204,7 @@ void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap, int32_t iCompress) { } else if (bpp == 8) { int32_t iPalette = pBitmap->GetPaletteSize(); if (iPalette > 0) { - UniqueArray pCS(new CPDF_Array); + CPDF_Array* pCS = new CPDF_Array; pCS->AddName("Indexed"); pCS->AddName("DeviceRGB"); pCS->AddInteger(iPalette - 1); @@ -217,12 +217,12 @@ void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap, int32_t iCompress) { ptr[2] = (uint8_t)argb; ptr += 3; } - CPDF_Stream* pCTS = m_pDocument->AddIndirectStream( + CPDF_Stream* pCTS = new CPDF_Stream( pColorTable, iPalette * 3, new CPDF_Dictionary(m_pDocument->GetByteStringPool())); - pCS->AddReference(m_pDocument, pCTS->GetObjNum()); + pCS->AddReference(m_pDocument, m_pDocument->AddIndirectObject(pCTS)); pDict->SetReferenceFor("ColorSpace", m_pDocument, - m_pDocument->AddIndirectObject(std::move(pCS))); + m_pDocument->AddIndirectObject(pCS)); } else { pDict->SetNameFor("ColorSpace", "DeviceGray"); } @@ -274,9 +274,9 @@ void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap, int32_t iCompress) { } } pMaskDict->SetIntegerFor("Length", mask_size); - pDict->SetReferenceFor( - "SMask", m_pDocument, - m_pDocument->AddIndirectStream(mask_buf, mask_size, pMaskDict)); + pDict->SetReferenceFor("SMask", m_pDocument, + m_pDocument->AddIndirectObject(new CPDF_Stream( + mask_buf, mask_size, pMaskDict))); if (bDeleteMask) delete pMaskBitmap; } diff --git a/core/fpdfapi/parser/cfdf_document.cpp b/core/fpdfapi/parser/cfdf_document.cpp index 1239fcd3ce..a1518dfed0 100644 --- a/core/fpdfapi/parser/cfdf_document.cpp +++ b/core/fpdfapi/parser/cfdf_document.cpp @@ -26,7 +26,8 @@ CFDF_Document::~CFDF_Document() { CFDF_Document* CFDF_Document::CreateNewDoc() { CFDF_Document* pDoc = new CFDF_Document; - pDoc->m_pRootDict = pDoc->AddIndirectDictionary(pDoc->GetByteStringPool()); + pDoc->m_pRootDict = new CPDF_Dictionary(pDoc->GetByteStringPool()); + pDoc->AddIndirectObject(pDoc->m_pRootDict); pDoc->m_pRootDict->SetFor("FDF", new CPDF_Dictionary(pDoc->GetByteStringPool())); return pDoc; @@ -68,8 +69,7 @@ void CFDF_Document::ParseStream(IFX_FileRead* pFile, FX_BOOL bOwnFile) { if (!pObj) break; - // TODO(tsepez): check |pObj| ownership. - ReplaceIndirectObjectIfHigherGeneration(objnum, UniqueObject(pObj)); + ReplaceIndirectObjectIfHigherGeneration(objnum, pObj); word = parser.GetNextWord(nullptr); if (word != "endobj") break; diff --git a/core/fpdfapi/parser/cpdf_array.cpp b/core/fpdfapi/parser/cpdf_array.cpp index db3b18be3e..807f62d7f3 100644 --- a/core/fpdfapi/parser/cpdf_array.cpp +++ b/core/fpdfapi/parser/cpdf_array.cpp @@ -152,9 +152,8 @@ void CPDF_Array::ConvertToIndirectObjectAt(size_t i, if (!m_Objects[i] || m_Objects[i]->IsReference()) return; - CPDF_Object* pUnowned = pHolder->AddIndirectObject(std::move(m_Objects[i])); - m_Objects[i] = - UniqueObject(new CPDF_Reference(pHolder, pUnowned->GetObjNum())); + uint32_t dwObjNum = pHolder->AddIndirectObject(m_Objects[i].release()); + m_Objects[i] = UniqueObject(new CPDF_Reference(pHolder, dwObjNum)); } void CPDF_Array::SetAt(size_t i, CPDF_Object* pObj) { @@ -206,8 +205,3 @@ void CPDF_Array::AddReference(CPDF_IndirectObjectHolder* pDoc, uint32_t objnum) { Add(new CPDF_Reference(pDoc, objnum)); } - -void CPDF_Array::AddReference(CPDF_IndirectObjectHolder* pDoc, - const CPDF_Object* pObj) { - AddReference(pDoc, pObj->GetObjNum()); -} diff --git a/core/fpdfapi/parser/cpdf_array.h b/core/fpdfapi/parser/cpdf_array.h index 18f1c00753..f473e26800 100644 --- a/core/fpdfapi/parser/cpdf_array.h +++ b/core/fpdfapi/parser/cpdf_array.h @@ -54,7 +54,6 @@ class CPDF_Array : public CPDF_Object { void AddString(const CFX_ByteString& str); void AddName(const CFX_ByteString& str); void AddReference(CPDF_IndirectObjectHolder* pDoc, uint32_t objnum); - void AddReference(CPDF_IndirectObjectHolder* pDoc, const CPDF_Object* pObj); iterator begin() { return m_Objects.begin(); } iterator end() { return m_Objects.end(); } diff --git a/core/fpdfapi/parser/cpdf_array_unittest.cpp b/core/fpdfapi/parser/cpdf_array_unittest.cpp index a3a27fc4ce..bc9f578021 100644 --- a/core/fpdfapi/parser/cpdf_array_unittest.cpp +++ b/core/fpdfapi/parser/cpdf_array_unittest.cpp @@ -113,11 +113,10 @@ TEST(cpdf_array, Clone) { for (size_t i = 0; i < kNumOfRows; ++i) { CPDF_Array* arr_elem = new CPDF_Array; for (size_t j = 0; j < kNumOfRowElems; ++j) { - UniqueObject obj(new CPDF_Number(elems[i][j])); - // Object number starts from 1. + CPDF_Number* obj = new CPDF_Number(elems[i][j]); + // Starts object number from 1. int obj_num = i * kNumOfRowElems + j + 1; - obj_holder->ReplaceIndirectObjectIfHigherGeneration(obj_num, - std::move(obj)); + obj_holder->ReplaceIndirectObjectIfHigherGeneration(obj_num, obj); arr_elem->InsertAt(j, new CPDF_Reference(obj_holder.get(), obj_num)); } arr->InsertAt(i, arr_elem); diff --git a/core/fpdfapi/parser/cpdf_data_avail.cpp b/core/fpdfapi/parser/cpdf_data_avail.cpp index 9629c204bb..980c94d325 100644 --- a/core/fpdfapi/parser/cpdf_data_avail.cpp +++ b/core/fpdfapi/parser/cpdf_data_avail.cpp @@ -1771,9 +1771,8 @@ CPDF_Dictionary* CPDF_DataAvail::GetPage(int index) { if (!pPageDict) return nullptr; - // TODO(tsepez): check |pPageDict| ownership. - if (!m_pDocument->ReplaceIndirectObjectIfHigherGeneration( - dwObjNum, UniqueObject(pPageDict))) { + if (!m_pDocument->ReplaceIndirectObjectIfHigherGeneration(dwObjNum, + pPageDict)) { return nullptr; } return pPageDict->GetDict(); diff --git a/core/fpdfapi/parser/cpdf_dictionary.cpp b/core/fpdfapi/parser/cpdf_dictionary.cpp index 0f03947ebd..435eee7248 100644 --- a/core/fpdfapi/parser/cpdf_dictionary.cpp +++ b/core/fpdfapi/parser/cpdf_dictionary.cpp @@ -188,8 +188,8 @@ void CPDF_Dictionary::ConvertToIndirectObjectFor( if (it == m_Map.end() || it->second->IsReference()) return; - CPDF_Object* pObj = pHolder->AddIndirectObject(std::move(it->second)); - it->second = UniqueReference(new CPDF_Reference(pHolder, pObj->GetObjNum())); + uint32_t objnum = pHolder->AddIndirectObject(it->second.release()); + it->second = UniqueReference(new CPDF_Reference(pHolder, objnum)); } void CPDF_Dictionary::RemoveFor(const CFX_ByteString& key) { @@ -229,12 +229,6 @@ void CPDF_Dictionary::SetReferenceFor(const CFX_ByteString& key, SetFor(key, new CPDF_Reference(pDoc, objnum)); } -void CPDF_Dictionary::SetReferenceFor(const CFX_ByteString& key, - CPDF_IndirectObjectHolder* pDoc, - const CPDF_Object* pObj) { - SetReferenceFor(key, pDoc, pObj->GetObjNum()); -} - void CPDF_Dictionary::SetNumberFor(const CFX_ByteString& key, FX_FLOAT f) { SetFor(key, new CPDF_Number(f)); } diff --git a/core/fpdfapi/parser/cpdf_dictionary.h b/core/fpdfapi/parser/cpdf_dictionary.h index 3e55812794..6265522997 100644 --- a/core/fpdfapi/parser/cpdf_dictionary.h +++ b/core/fpdfapi/parser/cpdf_dictionary.h @@ -67,9 +67,6 @@ class CPDF_Dictionary : public CPDF_Object { void SetReferenceFor(const CFX_ByteString& key, CPDF_IndirectObjectHolder* pDoc, uint32_t objnum); - void SetReferenceFor(const CFX_ByteString& key, - CPDF_IndirectObjectHolder* pDoc, - const CPDF_Object* objnum); void SetRectFor(const CFX_ByteString& key, const CFX_FloatRect& rect); void SetMatrixFor(const CFX_ByteString& key, const CFX_Matrix& matrix); void SetBooleanFor(const CFX_ByteString& key, bool bValue); diff --git a/core/fpdfapi/parser/cpdf_document.cpp b/core/fpdfapi/parser/cpdf_document.cpp index fdd395e351..c5f64a790c 100644 --- a/core/fpdfapi/parser/cpdf_document.cpp +++ b/core/fpdfapi/parser/cpdf_document.cpp @@ -256,7 +256,7 @@ int InsertDeletePDFPage(CPDF_Document* pDoc, if (nPagesToGo == 0) { if (bInsert) { pKidList->InsertAt(i, new CPDF_Reference(pDoc, pPage->GetObjNum())); - pPage->SetReferenceFor("Parent", pDoc, pPages); + pPage->SetReferenceFor("Parent", pDoc, pPages->GetObjNum()); } else { pKidList->RemoveAt(i); } @@ -307,7 +307,7 @@ int InsertNewPage(CPDF_Document* pDoc, } pPagesList->Add(new CPDF_Reference(pDoc, pPageDict->GetObjNum())); pPages->SetIntegerFor("Count", nPages + 1); - pPageDict->SetReferenceFor("Parent", pDoc, pPages); + pPageDict->SetReferenceFor("Parent", pDoc, pPages->GetObjNum()); } else { std::set<CPDF_Dictionary*> stack = {pPages}; if (InsertDeletePDFPage(pDoc, pPages, iPage, pPageDict, TRUE, &stack) < 0) @@ -693,23 +693,25 @@ CPDF_Image* CPDF_Document::LoadImageF(CPDF_Object* pObj) { void CPDF_Document::CreateNewDoc() { ASSERT(!m_pRootDict && !m_pInfoDict); - m_pRootDict = AddIndirectDictionary(m_pByteStringPool); + m_pRootDict = new CPDF_Dictionary(m_pByteStringPool); m_pRootDict->SetNameFor("Type", "Catalog"); + AddIndirectObject(m_pRootDict); - CPDF_Dictionary* pPages = AddIndirectDictionary(m_pByteStringPool); + CPDF_Dictionary* pPages = new CPDF_Dictionary(m_pByteStringPool); pPages->SetNameFor("Type", "Pages"); pPages->SetNumberFor("Count", 0); pPages->SetFor("Kids", new CPDF_Array); - - m_pRootDict->SetReferenceFor("Pages", this, pPages); - m_pInfoDict = AddIndirectDictionary(m_pByteStringPool); + m_pRootDict->SetReferenceFor("Pages", this, AddIndirectObject(pPages)); + m_pInfoDict = new CPDF_Dictionary(m_pByteStringPool); + AddIndirectObject(m_pInfoDict); } CPDF_Dictionary* CPDF_Document::CreateNewPage(int iPage) { - CPDF_Dictionary* pDict = AddIndirectDictionary(m_pByteStringPool); + CPDF_Dictionary* pDict = new CPDF_Dictionary(m_pByteStringPool); pDict->SetNameFor("Type", "Page"); + uint32_t dwObjNum = AddIndirectObject(pDict); if (InsertNewPage(this, iPage, pDict, m_PageList) < 0) { - DeleteIndirectObject(pDict->GetObjNum()); + ReleaseIndirectObject(dwObjNum); return nullptr; } return pDict; @@ -748,8 +750,7 @@ size_t CPDF_Document::CalculateEncodingDict(int charset, } if (i == FX_ArraySize(g_FX_CharsetUnicodes)) return i; - - CPDF_Dictionary* pEncodingDict = AddIndirectDictionary(m_pByteStringPool); + CPDF_Dictionary* pEncodingDict = new CPDF_Dictionary(m_pByteStringPool); pEncodingDict->SetNameFor("BaseEncoding", "WinAnsiEncoding"); CPDF_Array* pArray = new CPDF_Array; pArray->AddInteger(128); @@ -759,7 +760,9 @@ size_t CPDF_Document::CalculateEncodingDict(int charset, pArray->AddName(name.IsEmpty() ? ".notdef" : name); } pEncodingDict->SetFor("Differences", pArray); - pBaseDict->SetReferenceFor("Encoding", this, pEncodingDict); + pBaseDict->SetReferenceFor("Encoding", this, + AddIndirectObject(pEncodingDict)); + return i; } @@ -769,7 +772,7 @@ CPDF_Dictionary* CPDF_Document::ProcessbCJK( FX_BOOL bVert, CFX_ByteString basefont, std::function<void(FX_WCHAR, FX_WCHAR, CPDF_Array*)> Insert) { - CPDF_Dictionary* pFontDict = AddIndirectDictionary(m_pByteStringPool); + CPDF_Dictionary* pFontDict = new CPDF_Dictionary(m_pByteStringPool); CFX_ByteString cmap; CFX_ByteString ordering; int supplement = 0; @@ -819,17 +822,14 @@ CPDF_Dictionary* CPDF_Document::ProcessbCJK( pFontDict->SetNameFor("Type", "Font"); pFontDict->SetNameFor("Subtype", "CIDFontType2"); pFontDict->SetNameFor("BaseFont", basefont); - CPDF_Dictionary* pCIDSysInfo = new CPDF_Dictionary(m_pByteStringPool); pCIDSysInfo->SetStringFor("Registry", "Adobe"); pCIDSysInfo->SetStringFor("Ordering", ordering); pCIDSysInfo->SetIntegerFor("Supplement", supplement); pFontDict->SetFor("CIDSystemInfo", pCIDSysInfo); - CPDF_Array* pArray = new CPDF_Array; pBaseDict->SetFor("DescendantFonts", pArray); - - pArray->AddReference(this, pFontDict); + pArray->AddReference(this, AddIndirectObject(pFontDict)); return pFontDict; } @@ -847,7 +847,7 @@ CPDF_Font* CPDF_Document::AddFont(CFX_Font* pFont, int charset, FX_BOOL bVert) { CalculateFlags(pFont->IsBold(), pFont->IsItalic(), pFont->IsFixedWidth(), false, false, charset == FXFONT_SYMBOL_CHARSET); - CPDF_Dictionary* pBaseDict = AddIndirectDictionary(m_pByteStringPool); + CPDF_Dictionary* pBaseDict = new CPDF_Dictionary(m_pByteStringPool); pBaseDict->SetNameFor("Type", "Font"); std::unique_ptr<CFX_UnicodeEncoding> pEncoding( new CFX_UnicodeEncoding(pFont)); @@ -888,6 +888,7 @@ CPDF_Font* CPDF_Document::AddFont(CFX_Font* pFont, int charset, FX_BOOL bVert) { end, widthArr); }); } + AddIndirectObject(pBaseDict); int italicangle = pFont->GetSubstFont() ? pFont->GetSubstFont()->m_ItalicAngle : 0; FX_RECT bbox; @@ -915,11 +916,8 @@ CPDF_Font* CPDF_Document::AddFont(CFX_Font* pFont, int charset, FX_BOOL bVert) { CPDF_Dictionary* pFontDesc = CalculateFontDesc(this, basefont, flags, italicangle, pFont->GetAscent(), pFont->GetDescent(), pBBox, nStemV); - - // TODO(tsepez): check |pFontDesc| ownership. pFontDict->SetReferenceFor("FontDescriptor", this, - AddIndirectObject(UniqueDictionary(pFontDesc))); - + AddIndirectObject(pFontDesc)); return LoadFont(pBaseDict); } @@ -981,7 +979,7 @@ CPDF_Font* CPDF_Document::AddWindowsFont(LOGFONTA* pLogFont, ptm->otmrcFontBox.right, ptm->otmrcFontBox.top}; FX_Free(tm_buf); basefont.Replace(" ", ""); - CPDF_Dictionary* pBaseDict = AddIndirectDictionary(m_pByteStringPool); + CPDF_Dictionary* pBaseDict = new CPDF_Dictionary(m_pByteStringPool); pBaseDict->SetNameFor("Type", "Font"); CPDF_Dictionary* pFontDict = pBaseDict; if (!bCJK) { @@ -1006,6 +1004,7 @@ CPDF_Font* CPDF_Document::AddWindowsFont(LOGFONTA* pLogFont, InsertWidthArray(hDC, start, end, widthArr); }); } + AddIndirectObject(pBaseDict); CPDF_Array* pBBox = new CPDF_Array; for (int i = 0; i < 4; i++) pBBox->AddInteger(bbox[i]); @@ -1013,11 +1012,8 @@ CPDF_Font* CPDF_Document::AddWindowsFont(LOGFONTA* pLogFont, CalculateFontDesc(this, basefont, flags, italicangle, ascend, descend, pBBox, pLogFont->lfWeight / 5); pFontDesc->SetIntegerFor("CapHeight", capheight); - - // TODO(tsepez): check |pFontDesc| ownership. pFontDict->SetReferenceFor("FontDescriptor", this, - AddIndirectObject(UniqueObject(pFontDesc))); - + AddIndirectObject(pFontDesc)); hFont = SelectObject(hDC, hFont); DeleteObject(hFont); DeleteDC(hDC); diff --git a/core/fpdfapi/parser/cpdf_indirect_object_holder.cpp b/core/fpdfapi/parser/cpdf_indirect_object_holder.cpp index 49567e50a1..6e549de5a7 100644 --- a/core/fpdfapi/parser/cpdf_indirect_object_holder.cpp +++ b/core/fpdfapi/parser/cpdf_indirect_object_holder.cpp @@ -6,11 +6,8 @@ #include "core/fpdfapi/parser/cpdf_indirect_object_holder.h" -#include "core/fpdfapi/parser/cpdf_array.h" -#include "core/fpdfapi/parser/cpdf_dictionary.h" #include "core/fpdfapi/parser/cpdf_object.h" #include "core/fpdfapi/parser/cpdf_parser.h" -#include "core/fpdfapi/parser/cpdf_stream.h" CPDF_IndirectObjectHolder::CPDF_IndirectObjectHolder() : m_LastObjNum(0) {} @@ -45,59 +42,35 @@ CPDF_Object* CPDF_IndirectObjectHolder::ParseIndirectObject(uint32_t objnum) { return nullptr; } -CPDF_Object* CPDF_IndirectObjectHolder::AddIndirectObject(UniqueObject pObj) { +uint32_t CPDF_IndirectObjectHolder::AddIndirectObject(CPDF_Object* pObj) { if (pObj->m_ObjNum) - return pObj.release(); // TODO(tsepez): shouldn't happen, stop this leak. + return pObj->m_ObjNum; - pObj->m_ObjNum = ++m_LastObjNum; + m_LastObjNum++; m_IndirectObjs[m_LastObjNum].release(); // TODO(tsepez): stop this leak. - m_IndirectObjs[m_LastObjNum].reset(pObj.release()); // Changes deleters. - return m_IndirectObjs[m_LastObjNum].get(); -} - -CPDF_Array* CPDF_IndirectObjectHolder::AddIndirectArray() { - return ToArray(AddIndirectObject(UniqueObject(new CPDF_Array()))); -} - -CPDF_Dictionary* CPDF_IndirectObjectHolder::AddIndirectDictionary() { - return ToDictionary(AddIndirectObject(UniqueObject(new CPDF_Dictionary()))); -} - -CPDF_Dictionary* CPDF_IndirectObjectHolder::AddIndirectDictionary( - const CFX_WeakPtr<CFX_ByteStringPool>& pPool) { - return ToDictionary( - AddIndirectObject(UniqueObject(new CPDF_Dictionary(pPool)))); -} - -CPDF_Stream* CPDF_IndirectObjectHolder::AddIndirectStream() { - return ToStream(AddIndirectObject(UniqueObject(new CPDF_Stream()))); -} - -CPDF_Stream* CPDF_IndirectObjectHolder::AddIndirectStream( - uint8_t* pData, - uint32_t size, - CPDF_Dictionary* pDict) { - return ToStream( - AddIndirectObject(UniqueObject(new CPDF_Stream(pData, size, pDict)))); + m_IndirectObjs[m_LastObjNum].reset(pObj); + pObj->m_ObjNum = m_LastObjNum; + return m_LastObjNum; } bool CPDF_IndirectObjectHolder::ReplaceIndirectObjectIfHigherGeneration( uint32_t objnum, - UniqueObject pObj) { + CPDF_Object* pObj) { if (!objnum || !pObj) return false; CPDF_Object* pOldObj = GetIndirectObject(objnum); - if (pOldObj && pObj->GetGenNum() <= pOldObj->GetGenNum()) + if (pOldObj && pObj->GetGenNum() <= pOldObj->GetGenNum()) { + delete pObj; return false; - + } pObj->m_ObjNum = objnum; - m_IndirectObjs[objnum].reset(pObj.release()); // Changes deleters. + m_IndirectObjs[objnum].reset(pObj); m_LastObjNum = std::max(m_LastObjNum, objnum); return true; } -void CPDF_IndirectObjectHolder::DeleteIndirectObject(uint32_t objnum) { +void CPDF_IndirectObjectHolder::ReleaseIndirectObject(uint32_t objnum) { CPDF_Object* pObj = GetIndirectObject(objnum); if (!pObj || pObj->GetObjNum() == CPDF_Object::kInvalidObjNum) return; diff --git a/core/fpdfapi/parser/cpdf_indirect_object_holder.h b/core/fpdfapi/parser/cpdf_indirect_object_holder.h index 074e428d43..da4e942b5d 100644 --- a/core/fpdfapi/parser/cpdf_indirect_object_holder.h +++ b/core/fpdfapi/parser/cpdf_indirect_object_holder.h @@ -10,14 +10,9 @@ #include <map> #include <memory> -#include "core/fpdfapi/parser/cpdf_object.h" -#include "core/fxcrt/cfx_string_pool_template.h" -#include "core/fxcrt/cfx_weak_ptr.h" #include "core/fxcrt/fx_system.h" -class CPDF_Array; -class CPDF_Dictionary; -class CPDF_Stream; +class CPDF_Object; class CPDF_IndirectObjectHolder { public: @@ -29,23 +24,12 @@ class CPDF_IndirectObjectHolder { CPDF_Object* GetIndirectObject(uint32_t objnum) const; CPDF_Object* GetOrParseIndirectObject(uint32_t objnum); - void DeleteIndirectObject(uint32_t objnum); - - // Take ownership of |pObj|, returns unowned pointer to it. - CPDF_Object* AddIndirectObject(UniqueObject pObj); - - // Adds and owns a new object, returns unowned pointer to it. - CPDF_Array* AddIndirectArray(); - CPDF_Dictionary* AddIndirectDictionary(); - CPDF_Dictionary* AddIndirectDictionary( - const CFX_WeakPtr<CFX_ByteStringPool>& pPool); - CPDF_Stream* AddIndirectStream(); - CPDF_Stream* AddIndirectStream(uint8_t* pData, - uint32_t size, - CPDF_Dictionary* pDict); + void ReleaseIndirectObject(uint32_t objnum); + // Take ownership of |pObj|. + uint32_t AddIndirectObject(CPDF_Object* pObj); bool ReplaceIndirectObjectIfHigherGeneration(uint32_t objnum, - UniqueObject pObj); + CPDF_Object* pObj); uint32_t GetLastObjNum() const { return m_LastObjNum; } void SetLastObjNum(uint32_t objnum) { m_LastObjNum = objnum; } @@ -58,8 +42,6 @@ class CPDF_IndirectObjectHolder { private: uint32_t m_LastObjNum; - - // Ordinary deleter, not Release(). std::map<uint32_t, std::unique_ptr<CPDF_Object>> m_IndirectObjs; }; diff --git a/core/fpdfapi/parser/cpdf_object_unittest.cpp b/core/fpdfapi/parser/cpdf_object_unittest.cpp index b47a06fbfc..e5a5f54915 100644 --- a/core/fpdfapi/parser/cpdf_object_unittest.cpp +++ b/core/fpdfapi/parser/cpdf_object_unittest.cpp @@ -101,7 +101,7 @@ class PDFObjectsTest : public testing::Test { m_ArrayObj->Clone(), m_DictObj->Clone(), stream_obj->Clone()}; for (size_t i = 0; i < m_IndirectObjs.size(); ++i) { - m_ObjHolder->AddIndirectObject(UniqueObject(m_IndirectObjs[i])); + m_ObjHolder->AddIndirectObject(m_IndirectObjs[i]); m_RefObjs.emplace_back(new CPDF_Reference( m_ObjHolder.get(), m_IndirectObjs[i]->GetObjNum())); } @@ -730,8 +730,8 @@ TEST(PDFArrayTest, AddReferenceAndGetObjectAt) { // Create two arrays of references by different AddReference() APIs. for (size_t i = 0; i < FX_ArraySize(indirect_objs); ++i) { // All the indirect objects inserted will be owned by holder. - holder->ReplaceIndirectObjectIfHigherGeneration( - obj_nums[i], UniqueObject(indirect_objs[i])); + holder->ReplaceIndirectObjectIfHigherGeneration(obj_nums[i], + indirect_objs[i]); arr->AddReference(holder.get(), obj_nums[i]); arr1->AddReference(holder.get(), indirect_objs[i]->GetObjNum()); } @@ -841,8 +841,9 @@ TEST(PDFObjectTest, CloneCheckLoop) { { CPDF_IndirectObjectHolder objects_holder; // Create an object with a reference loop. - CPDF_Dictionary* dict_obj = objects_holder.AddIndirectDictionary(); + CPDF_Dictionary* dict_obj = new CPDF_Dictionary(); CPDF_Array* arr_obj = new CPDF_Array; + objects_holder.AddIndirectObject(dict_obj); EXPECT_EQ(1u, dict_obj->GetObjNum()); dict_obj->SetFor("arr", arr_obj); arr_obj->InsertAt( diff --git a/core/fpdfapi/parser/cpdf_parser.cpp b/core/fpdfapi/parser/cpdf_parser.cpp index d9c892f133..40a615fa47 100644 --- a/core/fpdfapi/parser/cpdf_parser.cpp +++ b/core/fpdfapi/parser/cpdf_parser.cpp @@ -955,18 +955,21 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { } FX_BOOL CPDF_Parser::LoadCrossRefV5(FX_FILESIZE* pos, FX_BOOL bMainXRef) { - UniqueObject pObject(ParseIndirectObjectAt(m_pDocument, *pos, 0)); + std::unique_ptr<CPDF_Object> pObject( + ParseIndirectObjectAt(m_pDocument, *pos, 0)); if (!pObject) return FALSE; CPDF_Object* pUnownedObject = pObject.get(); + if (m_pDocument) { CPDF_Dictionary* pRootDict = m_pDocument->GetRoot(); if (pRootDict && pRootDict->GetObjNum() == pObject->m_ObjNum) return FALSE; + // Takes ownership of object (std::move someday). uint32_t objnum = pObject->m_ObjNum; if (!m_pDocument->ReplaceIndirectObjectIfHigherGeneration( - objnum, std::move(pObject))) { + objnum, pObject.release())) { return FALSE; } } diff --git a/core/fpdfdoc/cpdf_formfield_unittest.cpp b/core/fpdfdoc/cpdf_formfield_unittest.cpp index a9d3d2403a..11cccf19b8 100644 --- a/core/fpdfdoc/cpdf_formfield_unittest.cpp +++ b/core/fpdfdoc/cpdf_formfield_unittest.cpp @@ -12,13 +12,15 @@ TEST(cpdf_formfield, FPDF_GetFullName) { EXPECT_TRUE(name.IsEmpty()); CPDF_IndirectObjectHolder obj_holder; - CPDF_Dictionary* root = obj_holder.AddIndirectDictionary(); + CPDF_Dictionary* root = new CPDF_Dictionary(); + obj_holder.AddIndirectObject(root); root->SetNameFor("T", "foo"); name = FPDF_GetFullName(root); EXPECT_STREQ("foo", name.UTF8Encode().c_str()); - CPDF_Dictionary* dict1 = obj_holder.AddIndirectDictionary(); - root->SetReferenceFor("Parent", &obj_holder, dict1); + CPDF_Dictionary* dict1 = new CPDF_Dictionary(); + root->SetReferenceFor("Parent", &obj_holder, + obj_holder.AddIndirectObject(dict1)); dict1->SetNameFor("T", "bar"); name = FPDF_GetFullName(root); EXPECT_STREQ("bar.foo", name.UTF8Encode().c_str()); @@ -28,8 +30,9 @@ TEST(cpdf_formfield, FPDF_GetFullName) { name = FPDF_GetFullName(root); EXPECT_STREQ("bar.foo", name.UTF8Encode().c_str()); - CPDF_Dictionary* dict3 = obj_holder.AddIndirectDictionary(); - dict2->SetReferenceFor("Parent", &obj_holder, dict3); + CPDF_Dictionary* dict3 = new CPDF_Dictionary(); + dict2->SetReferenceFor("Parent", &obj_holder, + obj_holder.AddIndirectObject(dict3)); dict3->SetNameFor("T", "qux"); name = FPDF_GetFullName(root); EXPECT_STREQ("qux.bar.foo", name.UTF8Encode().c_str()); diff --git a/core/fpdfdoc/cpdf_interform.cpp b/core/fpdfdoc/cpdf_interform.cpp index cafc912569..3bef85ef9c 100644 --- a/core/fpdfdoc/cpdf_interform.cpp +++ b/core/fpdfdoc/cpdf_interform.cpp @@ -59,9 +59,9 @@ void InitDict(CPDF_Dictionary*& pFormDict, CPDF_Document* pDocument) { return; if (!pFormDict) { - pFormDict = - pDocument->AddIndirectDictionary(pDocument->GetByteStringPool()); - pDocument->GetRoot()->SetReferenceFor("AcroForm", pDocument, pFormDict); + pFormDict = new CPDF_Dictionary(pDocument->GetByteStringPool()); + pDocument->GetRoot()->SetReferenceFor( + "AcroForm", pDocument, pDocument->AddIndirectObject(pFormDict)); } CFX_ByteString csDA; @@ -274,7 +274,8 @@ void AddFont(CPDF_Dictionary*& pFormDict, csNameTag.Remove(' '); csNameTag = CPDF_InterForm::GenerateNewResourceName(pDR, "Font", 4, csNameTag.c_str()); - pFonts->SetReferenceFor(csNameTag, pDocument, pFont->GetFontDict()); + pFonts->SetReferenceFor(csNameTag, pDocument, + pFont->GetFontDict()->GetObjNum()); } CPDF_Font* AddNativeFont(CPDF_Dictionary*& pFormDict, diff --git a/core/fpdfdoc/cpvt_fontmap.cpp b/core/fpdfdoc/cpvt_fontmap.cpp index 5960fc1215..a47595a6ac 100644 --- a/core/fpdfdoc/cpvt_fontmap.cpp +++ b/core/fpdfdoc/cpvt_fontmap.cpp @@ -38,9 +38,10 @@ void CPVT_FontMap::GetAnnotSysPDFFont(CPDF_Document* pDoc, return; CPDF_Dictionary* pFontList = pResDict->GetDictFor("Font"); - if (pFontList && !pFontList->KeyExist(sSysFontAlias)) - pFontList->SetReferenceFor(sSysFontAlias, pDoc, pPDFFont->GetFontDict()); - + if (pFontList && !pFontList->KeyExist(sSysFontAlias)) { + pFontList->SetReferenceFor(sSysFontAlias, pDoc, + pPDFFont->GetFontDict()->GetObjNum()); + } pSysFont = pPDFFont; } diff --git a/core/fpdfdoc/cpvt_generateap.cpp b/core/fpdfdoc/cpvt_generateap.cpp index 24827e3f22..28b436a05c 100644 --- a/core/fpdfdoc/cpvt_generateap.cpp +++ b/core/fpdfdoc/cpvt_generateap.cpp @@ -58,12 +58,13 @@ bool GenerateWidgetAP(CPDF_Document* pDoc, CPDF_Dictionary* pFontDict = pDRFontDict->GetDictFor(sFontName.Mid(1)); if (!pFontDict) { - pFontDict = pDoc->AddIndirectDictionary(pDoc->GetByteStringPool()); + pFontDict = new CPDF_Dictionary(pDoc->GetByteStringPool()); pFontDict->SetNameFor("Type", "Font"); pFontDict->SetNameFor("Subtype", "Type1"); pFontDict->SetNameFor("BaseFont", "Helvetica"); pFontDict->SetNameFor("Encoding", "WinAnsiEncoding"); - pDRFontDict->SetReferenceFor(sFontName.Mid(1), pDoc, pFontDict); + pDRFontDict->SetReferenceFor(sFontName.Mid(1), pDoc, + pDoc->AddIndirectObject(pFontDict)); } CPDF_Font* pDefFont = pDoc->LoadFont(pFontDict); if (!pDefFont) @@ -167,8 +168,8 @@ bool GenerateWidgetAP(CPDF_Document* pDoc, } CPDF_Stream* pNormalStream = pAPDict->GetStreamFor("N"); if (!pNormalStream) { - pNormalStream = pDoc->AddIndirectStream(); - pAPDict->SetReferenceFor("N", pDoc, pNormalStream); + pNormalStream = new CPDF_Stream; + pAPDict->SetReferenceFor("N", pDoc, pDoc->AddIndirectObject(pNormalStream)); } CPDF_Dictionary* pStreamDict = pNormalStream->GetDict(); if (pStreamDict) { @@ -182,7 +183,8 @@ bool GenerateWidgetAP(CPDF_Document* pDoc, pStreamResList->SetFor("Font", pStreamResFontList); } if (!pStreamResFontList->KeyExist(sFontName)) - pStreamResFontList->SetReferenceFor(sFontName, pDoc, pFontDict); + pStreamResFontList->SetReferenceFor(sFontName, pDoc, + pFontDict->GetObjNum()); } else { pStreamDict->SetFor("Resources", pFormDict->GetDictFor("DR")->Clone()); pStreamResList = pStreamDict->GetDictFor("Resources"); @@ -432,7 +434,8 @@ bool GenerateWidgetAP(CPDF_Document* pDoc, pStreamResList->SetFor("Font", pStreamResFontList); } if (!pStreamResFontList->KeyExist(sFontName)) - pStreamResFontList->SetReferenceFor(sFontName, pDoc, pFontDict); + pStreamResFontList->SetReferenceFor(sFontName, pDoc, + pFontDict->GetObjNum()); } else { pStreamDict->SetFor("Resources", pFormDict->GetDictFor("DR")->Clone()); pStreamResList = pStreamDict->GetDictFor("Resources"); @@ -556,8 +559,7 @@ CPDF_Dictionary* GenerateExtGStateDict(const CPDF_Dictionary& pAnnotDict, CPDF_Dictionary* GenerateResourceFontDict(CPDF_Document* pDoc, const CFX_ByteString& sFontDictName) { - CPDF_Dictionary* pFontDict = - pDoc->AddIndirectDictionary(pDoc->GetByteStringPool()); + CPDF_Dictionary* pFontDict = new CPDF_Dictionary(pDoc->GetByteStringPool()); pFontDict->SetNameFor("Type", "Font"); pFontDict->SetNameFor("Subtype", "Type1"); pFontDict->SetNameFor("BaseFont", "Helvetica"); @@ -565,7 +567,8 @@ CPDF_Dictionary* GenerateResourceFontDict(CPDF_Document* pDoc, CPDF_Dictionary* pResourceFontDict = new CPDF_Dictionary(pDoc->GetByteStringPool()); - pResourceFontDict->SetReferenceFor(sFontDictName, pDoc, pFontDict); + pResourceFontDict->SetReferenceFor(sFontDictName, pDoc, + pDoc->AddIndirectObject(pFontDict)); return pResourceFontDict; } @@ -593,9 +596,9 @@ void GenerateAndSetAPDict(CPDF_Document* pDoc, CPDF_Dictionary* pAPDict = new CPDF_Dictionary(pDoc->GetByteStringPool()); pAnnotDict->SetFor("AP", pAPDict); - CPDF_Stream* pNormalStream = pDoc->AddIndirectStream(); + CPDF_Stream* pNormalStream = new CPDF_Stream; pNormalStream->SetData(sAppStream.GetBuffer(), sAppStream.GetSize()); - pAPDict->SetReferenceFor("N", pDoc, pNormalStream); + pAPDict->SetReferenceFor("N", pDoc, pDoc->AddIndirectObject(pNormalStream)); CPDF_Dictionary* pStreamDict = pNormalStream->GetDict(); pStreamDict->SetIntegerFor("FormType", 1); diff --git a/fpdfsdk/cpdfsdk_baannot.cpp b/fpdfsdk/cpdfsdk_baannot.cpp index e704822e4d..72468e180f 100644 --- a/fpdfsdk/cpdfsdk_baannot.cpp +++ b/fpdfsdk/cpdfsdk_baannot.cpp @@ -316,9 +316,10 @@ void CPDFSDK_BAAnnot::WriteAppearance(const CFX_ByteString& sAPType, } if (!pStream) { + pStream = new CPDF_Stream; CPDF_Document* pDoc = m_pPageView->GetPDFDocument(); - pStream = pDoc->AddIndirectStream(); - pParentDict->SetReferenceFor(sAPType, pDoc, pStream); + pParentDict->SetReferenceFor(sAPType, pDoc, + pDoc->AddIndirectObject(pStream)); } CPDF_Dictionary* pStreamDict = pStream->GetDict(); @@ -353,10 +354,8 @@ void CPDFSDK_BAAnnot::SetAction(const CPDF_Action& action) { CPDF_Dictionary* pDict = action.GetDict(); if (pDict != m_pAnnot->GetAnnotDict()->GetDictFor("A")) { CPDF_Document* pDoc = m_pPageView->GetPDFDocument(); - - // TODO(tsepez): check |pDict| ownership. - m_pAnnot->GetAnnotDict()->SetReferenceFor( - "A", pDoc, pDoc->AddIndirectObject(UniqueDictionary(pDict))); + m_pAnnot->GetAnnotDict()->SetReferenceFor("A", pDoc, + pDoc->AddIndirectObject(pDict)); } } diff --git a/fpdfsdk/cpdfsdk_widget.cpp b/fpdfsdk/cpdfsdk_widget.cpp index 62bfcf7bd7..e1624a08b5 100644 --- a/fpdfsdk/cpdfsdk_widget.cpp +++ b/fpdfsdk/cpdfsdk_widget.cpp @@ -1811,7 +1811,7 @@ void CPDFSDK_Widget::AddImageToAppearance(const CFX_ByteString& sAPType, } CPDF_Dictionary* pXObject = new CPDF_Dictionary(pDoc->GetByteStringPool()); - pXObject->SetReferenceFor(sImageAlias, pDoc, pImage); + pXObject->SetReferenceFor(sImageAlias, pDoc, pImage->GetObjNum()); pStreamResList->SetFor("XObject", pXObject); } diff --git a/fpdfsdk/formfiller/cba_fontmap.cpp b/fpdfsdk/formfiller/cba_fontmap.cpp index af518fe956..7178b902d0 100644 --- a/fpdfsdk/formfiller/cba_fontmap.cpp +++ b/fpdfsdk/formfiller/cba_fontmap.cpp @@ -166,8 +166,9 @@ void CBA_FontMap::AddFontToAnnotDict(CPDF_Font* pFont, CPDF_Stream* pStream = pAPDict->GetStreamFor(m_sAPType); if (!pStream) { - pStream = m_pDocument->AddIndirectStream(); - pAPDict->SetReferenceFor(m_sAPType, m_pDocument, pStream); + pStream = new CPDF_Stream; + pAPDict->SetReferenceFor(m_sAPType, m_pDocument, + m_pDocument->AddIndirectObject(pStream)); } CPDF_Dictionary* pStreamDict = pStream->GetDict(); @@ -185,12 +186,14 @@ void CBA_FontMap::AddFontToAnnotDict(CPDF_Font* pFont, CPDF_Dictionary* pStreamResFontList = pStreamResList->GetDictFor("Font"); if (!pStreamResFontList) { pStreamResFontList = - m_pDocument->AddIndirectDictionary(m_pDocument->GetByteStringPool()); - pStreamResList->SetReferenceFor("Font", m_pDocument, pStreamResFontList); + new CPDF_Dictionary(m_pDocument->GetByteStringPool()); + pStreamResList->SetReferenceFor( + "Font", m_pDocument, + m_pDocument->AddIndirectObject(pStreamResFontList)); } if (!pStreamResFontList->KeyExist(sAlias)) { pStreamResFontList->SetReferenceFor(sAlias, m_pDocument, - pFont->GetFontDict()); + pFont->GetFontDict()->GetObjNum()); } } } diff --git a/fpdfsdk/fpdf_flatten.cpp b/fpdfsdk/fpdf_flatten.cpp index 3a1135130e..abfc69b092 100644 --- a/fpdfsdk/fpdf_flatten.cpp +++ b/fpdfsdk/fpdf_flatten.cpp @@ -186,12 +186,12 @@ CFX_FloatRect CalculateRect(CPDF_RectArray* pRectArray) { uint32_t NewIndirectContentsStream(const CFX_ByteString& key, CPDF_Document* pDocument) { - CPDF_Stream* pNewContents = pDocument->AddIndirectStream( + CPDF_Stream* pNewContents = new CPDF_Stream( nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool())); CFX_ByteString sStream; sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str()); pNewContents->SetData(sStream.raw_str(), sStream.GetLength()); - return pNewContents->GetObjNum(); + return pDocument->AddIndirectObject(pNewContents); } void SetPageContents(const CFX_ByteString& key, @@ -211,15 +211,17 @@ void SetPageContents(const CFX_ByteString& key, } pPage->ConvertToIndirectObjectFor("Contents", pDocument); if (!pContentsArray) { - pContentsArray = pDocument->AddIndirectArray(); + pContentsArray = new CPDF_Array; CPDF_StreamAcc acc; acc.LoadAllData(pContentsStream); CFX_ByteString sStream = "q\n"; - CFX_ByteString sBody = CFX_ByteString(acc.GetData(), acc.GetSize()); + CFX_ByteString sBody = + CFX_ByteString((const FX_CHAR*)acc.GetData(), acc.GetSize()); sStream = sStream + sBody + "\nQ"; pContentsStream->SetData(sStream.raw_str(), sStream.GetLength()); pContentsArray->AddReference(pDocument, pContentsStream->GetObjNum()); - pPage->SetReferenceFor("Contents", pDocument, pContentsArray); + pPage->SetReferenceFor("Contents", pDocument, + pDocument->AddIndirectObject(pContentsArray)); } if (!key.IsEmpty()) { pContentsArray->AddReference(pDocument, @@ -316,10 +318,10 @@ DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { pPageDict->SetFor("Resources", pRes); } - CPDF_Stream* pNewXObject = pDocument->AddIndirectStream( + CPDF_Stream* pNewXObject = new CPDF_Stream( nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool())); - uint32_t dwObjNum = pNewXObject->GetObjNum(); + uint32_t dwObjNum = pDocument->AddIndirectObject(pNewXObject); CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject"); if (!pPageXObject) { pPageXObject = new CPDF_Dictionary(pDocument->GetByteStringPool()); @@ -424,10 +426,8 @@ DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { CFX_ByteString sFormName; sFormName.Format("F%d", i); - - // TODO(tsepez): check |pObj| ownership. pXObject->SetReferenceFor(sFormName, pDocument, - pDocument->AddIndirectObject(UniqueObject(pObj))); + pDocument->AddIndirectObject(pObj)); CPDF_StreamAcc acc; acc.LoadAllData(pNewXObject); diff --git a/fpdfsdk/fpdf_transformpage.cpp b/fpdfsdk/fpdf_transformpage.cpp index ce09da32a2..47b6cec9ea 100644 --- a/fpdfsdk/fpdf_transformpage.cpp +++ b/fpdfsdk/fpdf_transformpage.cpp @@ -129,13 +129,15 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page, if (!pDoc) return FALSE; - CPDF_Stream* pStream = pDoc->AddIndirectStream( - nullptr, 0, new CPDF_Dictionary(pDoc->GetByteStringPool())); + CPDF_Dictionary* pDic = new CPDF_Dictionary(pDoc->GetByteStringPool()); + CPDF_Stream* pStream = new CPDF_Stream(nullptr, 0, pDic); pStream->SetData(textBuf.GetBuffer(), textBuf.GetSize()); + pDoc->AddIndirectObject(pStream); + pDic = new CPDF_Dictionary(pDoc->GetByteStringPool()); - CPDF_Stream* pEndStream = pDoc->AddIndirectStream( - nullptr, 0, new CPDF_Dictionary(pDoc->GetByteStringPool())); + CPDF_Stream* pEndStream = new CPDF_Stream(nullptr, 0, pDic); pEndStream->SetData((const uint8_t*)" Q", 2); + pDoc->AddIndirectObject(pEndStream); CPDF_Array* pContentArray = nullptr; CPDF_Array* pArray = ToArray(pContentObj); @@ -154,11 +156,12 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page, pContentArray->InsertAt(0, pRef); pContentArray->AddReference(pDoc, pEndStream->GetObjNum()); } else if (pDirectObj->IsStream()) { - pContentArray = pDoc->AddIndirectArray(); + pContentArray = new CPDF_Array(); pContentArray->AddReference(pDoc, pStream->GetObjNum()); pContentArray->AddReference(pDoc, pDirectObj->GetObjNum()); pContentArray->AddReference(pDoc, pEndStream->GetObjNum()); - pPageDic->SetReferenceFor("Contents", pDoc, pContentArray); + pPageDic->SetReferenceFor("Contents", pDoc, + pDoc->AddIndirectObject(pContentArray)); } } } @@ -302,9 +305,10 @@ DLLEXPORT void STDCALL FPDFPage_InsertClipPath(FPDF_PAGE page, if (!pDoc) return; - CPDF_Stream* pStream = pDoc->AddIndirectStream( - nullptr, 0, new CPDF_Dictionary(pDoc->GetByteStringPool())); + CPDF_Dictionary* pDic = new CPDF_Dictionary(pDoc->GetByteStringPool()); + CPDF_Stream* pStream = new CPDF_Stream(nullptr, 0, pDic); pStream->SetData(strClip.GetBuffer(), strClip.GetSize()); + pDoc->AddIndirectObject(pStream); CPDF_Array* pContentArray = nullptr; CPDF_Array* pArray = ToArray(pContentObj); @@ -321,10 +325,11 @@ DLLEXPORT void STDCALL FPDFPage_InsertClipPath(FPDF_PAGE page, CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum()); pContentArray->InsertAt(0, pRef); } else if (pDirectObj->IsStream()) { - pContentArray = pDoc->AddIndirectArray(); - pContentArray->AddReference(pDoc, pStream); - pContentArray->AddReference(pDoc, pDirectObj); - pPageDic->SetReferenceFor("Contents", pDoc, pContentArray); + pContentArray = new CPDF_Array(); + pContentArray->AddReference(pDoc, pStream->GetObjNum()); + pContentArray->AddReference(pDoc, pDirectObj->GetObjNum()); + pPageDic->SetReferenceFor("Contents", pDoc, + pDoc->AddIndirectObject(pContentArray)); } } } diff --git a/fpdfsdk/fpdfdoc_unittest.cpp b/fpdfsdk/fpdfdoc_unittest.cpp index 40457ee447..fc85404dba 100644 --- a/fpdfsdk/fpdfdoc_unittest.cpp +++ b/fpdfsdk/fpdfdoc_unittest.cpp @@ -80,8 +80,8 @@ class PDFDocTest : public testing::Test { std::vector<DictObjInfo> info; for (int i = 0; i < num; ++i) { // Objects created will be released by the document. - CPDF_Dictionary* obj = m_pIndirectObjs->AddIndirectDictionary(); - info.push_back({obj->GetObjNum(), obj}); + CPDF_Dictionary* obj = new CPDF_Dictionary(); + info.push_back({m_pIndirectObjs->AddIndirectObject(obj), obj}); } return info; } diff --git a/fpdfsdk/fpdfppo.cpp b/fpdfsdk/fpdfppo.cpp index 023c41a750..8a46ea4067 100644 --- a/fpdfsdk/fpdfppo.cpp +++ b/fpdfsdk/fpdfppo.cpp @@ -70,9 +70,9 @@ FX_BOOL CPDF_PageOrganizer::PDFDocInit(CPDF_Document* pDestPDFDoc, CPDF_Dictionary* pNewPages = pElement ? ToDictionary(pElement->GetDirect()) : nullptr; if (!pNewPages) { - pNewPages = - pDestPDFDoc->AddIndirectDictionary(pDestPDFDoc->GetByteStringPool()); - pNewRoot->SetReferenceFor("Pages", pDestPDFDoc, pNewPages); + pNewPages = new CPDF_Dictionary(pDestPDFDoc->GetByteStringPool()); + pNewRoot->SetReferenceFor("Pages", pDestPDFDoc, + pDestPDFDoc->AddIndirectObject(pNewPages)); } CFX_ByteString cbPageType = pNewPages->GetStringFor("Type", ""); @@ -83,7 +83,7 @@ FX_BOOL CPDF_PageOrganizer::PDFDocInit(CPDF_Document* pDestPDFDoc, if (!pNewPages->GetArrayFor("Kids")) { pNewPages->SetIntegerFor("Count", 0); pNewPages->SetReferenceFor("Kids", pDestPDFDoc, - pDestPDFDoc->AddIndirectArray()); + pDestPDFDoc->AddIndirectObject(new CPDF_Array)); } return TRUE; @@ -277,7 +277,7 @@ uint32_t CPDF_PageOrganizer::GetNewObjId(CPDF_Document* pDoc, if (!pDirect) return 0; - UniqueObject pClone(pDirect->Clone()); + CPDF_Object* pClone = pDirect->Clone(); if (!pClone) return 0; @@ -294,11 +294,10 @@ uint32_t CPDF_PageOrganizer::GetNewObjId(CPDF_Document* pDoc, } } } - CPDF_Object* pUnowned = pDoc->AddIndirectObject(std::move(pClone)); - dwNewObjNum = pUnowned->GetObjNum(); + dwNewObjNum = pDoc->AddIndirectObject(pClone); (*pObjNumberMap)[dwObjnum] = dwNewObjNum; - if (!UpdateReference(pUnowned, pDoc, pObjNumberMap)) { - pDoc->DeleteIndirectObject(dwNewObjNum); + if (!UpdateReference(pClone, pDoc, pObjNumberMap)) { + pClone->Release(); return 0; } return dwNewObjNum; diff --git a/fpdfsdk/fpdfsave.cpp b/fpdfsdk/fpdfsave.cpp index b594084218..364f4d2d6f 100644 --- a/fpdfsdk/fpdfsave.cpp +++ b/fpdfsdk/fpdfsave.cpp @@ -184,12 +184,14 @@ bool SaveXFADocumentData(CPDFXFA_Document* pDocument, if (pDataSetsStream) pDataSetsStream->InitStreamFromFile(pDsfileWrite.get(), pDataDict); } else { - CPDF_Stream* pData = pPDFDocument->AddIndirectStream(); + CPDF_Stream* pData = new CPDF_Stream; pData->InitStreamFromFile(pDsfileWrite.get(), pDataDict); iLast = pArray->GetCount() - 2; - pArray->InsertAt(iLast, new CPDF_String("datasets", false)); - pArray->InsertAt(iLast + 1, - new CPDF_Reference(pPDFDocument, pData->GetObjNum())); + pArray->InsertAt(iLast, new CPDF_String("datasets", FALSE)); + pArray->InsertAt( + iLast + 1, + new CPDF_Reference(pPDFDocument, + pPDFDocument->AddIndirectObject(pData))); } fileList->push_back(std::move(pDsfileWrite)); } @@ -206,12 +208,14 @@ bool SaveXFADocumentData(CPDFXFA_Document* pDocument, if (pFormStream) pFormStream->InitStreamFromFile(pfileWrite.get(), pDataDict); } else { - CPDF_Stream* pData = pPDFDocument->AddIndirectStream(); + CPDF_Stream* pData = new CPDF_Stream; pData->InitStreamFromFile(pfileWrite.get(), pDataDict); iLast = pArray->GetCount() - 2; pArray->InsertAt(iLast, new CPDF_String("form", FALSE)); - pArray->InsertAt(iLast + 1, - new CPDF_Reference(pPDFDocument, pData->GetObjNum())); + pArray->InsertAt( + iLast + 1, + new CPDF_Reference(pPDFDocument, + pPDFDocument->AddIndirectObject(pData))); } fileList->push_back(std::move(pfileWrite)); } |