diff options
author | tsepez <tsepez@chromium.org> | 2016-11-15 11:33:44 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-11-15 11:33:44 -0800 |
commit | 70c4afd5c3c5c44cd24f814423a23a6ef05bba02 (patch) | |
tree | cbe593d0b6c0bfc6fd5038bf60a77a94e49c69c9 | |
parent | f16f6b8b52277348f5d571b7641bb0bbd5239589 (diff) | |
download | pdfium-70c4afd5c3c5c44cd24f814423a23a6ef05bba02.tar.xz |
Make AddIndirectObject() take a unique_ptr.
Add convenience routines to create and add object in
one step.
Review-Url: https://codereview.chromium.org/2489283003
23 files changed, 211 insertions, 207 deletions
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp index c27ca044e6..8e7e7b3504 100644 --- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp +++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp @@ -52,10 +52,9 @@ void CPDF_PageContentGenerator::GenerateContent() { if (pContent) pPageDict->RemoveFor("Contents"); - CPDF_Stream* pStream = new CPDF_Stream; + CPDF_Stream* pStream = m_pDocument->NewIndirect<CPDF_Stream>(); pStream->SetData(buf.GetBuffer(), buf.GetLength()); - pPageDict->SetReferenceFor("Contents", m_pDocument, - m_pDocument->AddIndirectObject(pStream)); + pPageDict->SetReferenceFor("Contents", m_pDocument, pStream); } CFX_ByteString CPDF_PageContentGenerator::RealizeResource( @@ -63,11 +62,10 @@ CFX_ByteString CPDF_PageContentGenerator::RealizeResource( const CFX_ByteString& bsType) { ASSERT(dwResourceObjNum); if (!m_pPage->m_pResources) { - 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)); + m_pPage->m_pResources = m_pDocument->NewIndirect<CPDF_Dictionary>( + m_pDocument->GetByteStringPool()); + m_pPage->m_pFormDict->SetReferenceFor("Resources", m_pDocument, + m_pPage->m_pResources); } CPDF_Dictionary* pResList = m_pPage->m_pResources->GetDictFor(bsType); if (!pResList) { @@ -133,11 +131,10 @@ void CPDF_PageContentGenerator::ProcessForm(CFX_ByteTextBuf& buf, pFormDict->SetNameFor("Subtype", "Form"); pFormDict->SetRectFor("BBox", bbox); - CPDF_Stream* pStream = new CPDF_Stream; + CPDF_Stream* pStream = m_pDocument->NewIndirect<CPDF_Stream>(); pStream->InitStream(data, size, pFormDict); - CFX_ByteString name = - RealizeResource(m_pDocument->AddIndirectObject(pStream), "XObject"); + CFX_ByteString name = RealizeResource(pStream->GetObjNum(), "XObject"); buf << "/" << PDF_NameEncode(name) << " Do Q\n"; } @@ -181,8 +178,7 @@ void CPDF_PageContentGenerator::TransformContent(CFX_Matrix& matrix) { contentStream.LoadAllData(pStream); ProcessForm(buf, contentStream.GetData(), contentStream.GetSize(), matrix); } - CPDF_Stream* pStream = new CPDF_Stream; + CPDF_Stream* pStream = m_pDocument->NewIndirect<CPDF_Stream>(); pStream->SetData(buf.GetBuffer(), buf.GetLength()); - m_pPage->m_pFormDict->SetReferenceFor( - "Contents", m_pDocument, m_pDocument->AddIndirectObject(pStream)); + m_pPage->m_pFormDict->SetReferenceFor("Contents", m_pDocument, pStream); } diff --git a/core/fpdfapi/page/cpdf_docpagedata.cpp b/core/fpdfapi/page/cpdf_docpagedata.cpp index 7c45a04af1..2c9f4bb1f7 100644 --- a/core/fpdfapi/page/cpdf_docpagedata.cpp +++ b/core/fpdfapi/page/cpdf_docpagedata.cpp @@ -175,7 +175,8 @@ CPDF_Font* CPDF_DocPageData::GetStandardFont(const CFX_ByteString& fontName, return fontData->AddRef(); } - CPDF_Dictionary* pDict = new CPDF_Dictionary(m_pPDFDoc->GetByteStringPool()); + CPDF_Dictionary* pDict = + m_pPDFDoc->NewIndirect<CPDF_Dictionary>(m_pPDFDoc->GetByteStringPool()); pDict->SetNameFor("Type", "Font"); pDict->SetNameFor("Subtype", "Type1"); pDict->SetNameFor("BaseFont", fontName); @@ -183,7 +184,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 4048c9bb4e..e21d38a56f 100644 --- a/core/fpdfapi/page/cpdf_image.cpp +++ b/core/fpdfapi/page/cpdf_image.cpp @@ -78,7 +78,7 @@ void CPDF_Image::ConvertStreamToIndirectObject() { return; ASSERT(m_pOwnedStream); - m_pDocument->AddIndirectObject(m_pOwnedStream.release()); + m_pDocument->AddIndirectObject(std::move(m_pOwnedStream)); } CPDF_Dictionary* CPDF_Image::InitJPEG(uint8_t* pData, uint32_t size) { @@ -212,7 +212,7 @@ void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap, int32_t iCompress) { } else if (bpp == 8) { int32_t iPalette = pBitmap->GetPaletteSize(); if (iPalette > 0) { - CPDF_Array* pCS = new CPDF_Array; + CPDF_Array* pCS = m_pDocument->NewIndirect<CPDF_Array>(); pCS->AddName("Indexed"); pCS->AddName("DeviceRGB"); pCS->AddInteger(iPalette - 1); @@ -225,12 +225,11 @@ void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap, int32_t iCompress) { ptr[2] = (uint8_t)argb; ptr += 3; } - CPDF_Stream* pCTS = new CPDF_Stream( + CPDF_Stream* pCTS = m_pDocument->NewIndirect<CPDF_Stream>( pColorTable, iPalette * 3, new CPDF_Dictionary(m_pDocument->GetByteStringPool())); - pCS->AddReference(m_pDocument, m_pDocument->AddIndirectObject(pCTS)); - pDict->SetReferenceFor("ColorSpace", m_pDocument, - m_pDocument->AddIndirectObject(pCS)); + pCS->AddReference(m_pDocument, pCTS); + pDict->SetReferenceFor("ColorSpace", m_pDocument, pCS); } else { pDict->SetNameFor("ColorSpace", "DeviceGray"); } @@ -282,9 +281,9 @@ void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap, int32_t iCompress) { } } pMaskDict->SetIntegerFor("Length", mask_size); - pDict->SetReferenceFor("SMask", m_pDocument, - m_pDocument->AddIndirectObject(new CPDF_Stream( - mask_buf, mask_size, pMaskDict))); + pDict->SetReferenceFor( + "SMask", m_pDocument, + m_pDocument->NewIndirect<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 f54e598506..d76ae1e642 100644 --- a/core/fpdfapi/parser/cfdf_document.cpp +++ b/core/fpdfapi/parser/cfdf_document.cpp @@ -26,8 +26,8 @@ CFDF_Document::~CFDF_Document() { CFDF_Document* CFDF_Document::CreateNewDoc() { CFDF_Document* pDoc = new CFDF_Document; - pDoc->m_pRootDict = new CPDF_Dictionary(pDoc->GetByteStringPool()); - pDoc->AddIndirectObject(pDoc->m_pRootDict); + pDoc->m_pRootDict = + pDoc->NewIndirect<CPDF_Dictionary>(pDoc->GetByteStringPool()); pDoc->m_pRootDict->SetFor("FDF", new CPDF_Dictionary(pDoc->GetByteStringPool())); return pDoc; diff --git a/core/fpdfapi/parser/cpdf_array.cpp b/core/fpdfapi/parser/cpdf_array.cpp index af9b544f0a..f8ec46acc9 100644 --- a/core/fpdfapi/parser/cpdf_array.cpp +++ b/core/fpdfapi/parser/cpdf_array.cpp @@ -155,8 +155,8 @@ void CPDF_Array::ConvertToIndirectObjectAt(size_t i, if (!pObj || pObj->IsReference()) return; - uint32_t dwObjNum = pHolder->AddIndirectObject(pObj); - m_Objects[i] = new CPDF_Reference(pHolder, dwObjNum); + CPDF_Object* pNew = pHolder->AddIndirectObject(pdfium::WrapUnique(pObj)); + m_Objects[i] = new CPDF_Reference(pHolder, pNew->GetObjNum()); } void CPDF_Array::SetAt(size_t i, CPDF_Object* pObj) { @@ -209,3 +209,9 @@ void CPDF_Array::AddReference(CPDF_IndirectObjectHolder* pDoc, uint32_t objnum) { Add(new CPDF_Reference(pDoc, objnum)); } + +void CPDF_Array::AddReference(CPDF_IndirectObjectHolder* pDoc, + CPDF_Object* pObj) { + ASSERT(!pObj->IsInline()); + Add(new CPDF_Reference(pDoc, pObj->GetObjNum())); +} diff --git a/core/fpdfapi/parser/cpdf_array.h b/core/fpdfapi/parser/cpdf_array.h index 5a9b10cb3c..bf4b8a626d 100644 --- a/core/fpdfapi/parser/cpdf_array.h +++ b/core/fpdfapi/parser/cpdf_array.h @@ -55,6 +55,7 @@ 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, CPDF_Object* pObj); iterator begin() { return m_Objects.begin(); } iterator end() { return m_Objects.end(); } diff --git a/core/fpdfapi/parser/cpdf_dictionary.cpp b/core/fpdfapi/parser/cpdf_dictionary.cpp index 403c29fa0d..02cdfa37da 100644 --- a/core/fpdfapi/parser/cpdf_dictionary.cpp +++ b/core/fpdfapi/parser/cpdf_dictionary.cpp @@ -199,8 +199,9 @@ void CPDF_Dictionary::ConvertToIndirectObjectFor( if (it == m_Map.end() || it->second->IsReference()) return; - uint32_t objnum = pHolder->AddIndirectObject(it->second); - it->second = new CPDF_Reference(pHolder, objnum); + CPDF_Object* pObj = + pHolder->AddIndirectObject(pdfium::WrapUnique(it->second)); + it->second = new CPDF_Reference(pHolder, pObj->GetObjNum()); } void CPDF_Dictionary::RemoveFor(const CFX_ByteString& key) { @@ -251,6 +252,13 @@ 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, + CPDF_Object* pObj) { + ASSERT(!pObj->IsInline()); + SetFor(key, new CPDF_Reference(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 fffe03463c..ebfd7e92ee 100644 --- a/core/fpdfapi/parser/cpdf_dictionary.h +++ b/core/fpdfapi/parser/cpdf_dictionary.h @@ -61,6 +61,7 @@ class CPDF_Dictionary : public CPDF_Object { // Set* functions invalidate iterators for the element with the key |key|. void SetFor(const CFX_ByteString& key, CPDF_Object* pObj); + void SetBooleanFor(const CFX_ByteString& key, bool bValue); void SetNameFor(const CFX_ByteString& key, const CFX_ByteString& name); void SetStringFor(const CFX_ByteString& key, const CFX_ByteString& str); void SetIntegerFor(const CFX_ByteString& key, int i); @@ -68,9 +69,12 @@ 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, + CPDF_Object* pObj); + 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); void ConvertToIndirectObjectFor(const CFX_ByteString& key, CPDF_IndirectObjectHolder* pHolder); diff --git a/core/fpdfapi/parser/cpdf_document.cpp b/core/fpdfapi/parser/cpdf_document.cpp index ebd3156e35..35c18997b9 100644 --- a/core/fpdfapi/parser/cpdf_document.cpp +++ b/core/fpdfapi/parser/cpdf_document.cpp @@ -310,15 +310,16 @@ void ProcessNonbCJK(CPDF_Dictionary* pBaseDict, pBaseDict->SetFor("Widths", pWidths); } -CPDF_Dictionary* CalculateFontDesc(CPDF_Document* pDoc, - CFX_ByteString basefont, - int flags, - int italicangle, - int ascend, - int descend, - CPDF_Array* bbox, - int32_t stemV) { - CPDF_Dictionary* pFontDesc = new CPDF_Dictionary(pDoc->GetByteStringPool()); +std::unique_ptr<CPDF_Dictionary> CalculateFontDesc(CPDF_Document* pDoc, + CFX_ByteString basefont, + int flags, + int italicangle, + int ascend, + int descend, + CPDF_Array* bbox, + int32_t stemV) { + auto pFontDesc = + pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool()); pFontDesc->SetNameFor("Type", "FontDescriptor"); pFontDesc->SetNameFor("FontName", basefont); pFontDesc->SetIntegerFor("Flags", flags); @@ -648,23 +649,21 @@ CPDF_Image* CPDF_Document::LoadImageFromPageData(uint32_t dwStreamObjNum) { void CPDF_Document::CreateNewDoc() { ASSERT(!m_pRootDict && !m_pInfoDict); - m_pRootDict = new CPDF_Dictionary(m_pByteStringPool); + m_pRootDict = NewIndirect<CPDF_Dictionary>(m_pByteStringPool); m_pRootDict->SetNameFor("Type", "Catalog"); - AddIndirectObject(m_pRootDict); - CPDF_Dictionary* pPages = new CPDF_Dictionary(m_pByteStringPool); + CPDF_Dictionary* pPages = NewIndirect<CPDF_Dictionary>(m_pByteStringPool); pPages->SetNameFor("Type", "Pages"); pPages->SetNumberFor("Count", 0); pPages->SetFor("Kids", new CPDF_Array); - m_pRootDict->SetReferenceFor("Pages", this, AddIndirectObject(pPages)); - m_pInfoDict = new CPDF_Dictionary(m_pByteStringPool); - AddIndirectObject(m_pInfoDict); + m_pRootDict->SetReferenceFor("Pages", this, pPages); + m_pInfoDict = NewIndirect<CPDF_Dictionary>(m_pByteStringPool); } CPDF_Dictionary* CPDF_Document::CreateNewPage(int iPage) { - CPDF_Dictionary* pDict = new CPDF_Dictionary(m_pByteStringPool); + CPDF_Dictionary* pDict = NewIndirect<CPDF_Dictionary>(m_pByteStringPool); pDict->SetNameFor("Type", "Page"); - uint32_t dwObjNum = AddIndirectObject(pDict); + uint32_t dwObjNum = pDict->GetObjNum(); if (!InsertNewPage(iPage, pDict)) { DeleteIndirectObject(dwObjNum); return nullptr; @@ -780,19 +779,21 @@ size_t CPDF_Document::CalculateEncodingDict(int charset, } if (i == FX_ArraySize(g_FX_CharsetUnicodes)) return i; - CPDF_Dictionary* pEncodingDict = new CPDF_Dictionary(m_pByteStringPool); + + CPDF_Dictionary* pEncodingDict = + NewIndirect<CPDF_Dictionary>(m_pByteStringPool); pEncodingDict->SetNameFor("BaseEncoding", "WinAnsiEncoding"); + CPDF_Array* pArray = new CPDF_Array; pArray->AddInteger(128); + const uint16_t* pUnicodes = g_FX_CharsetUnicodes[i].m_pUnicodes; for (int j = 0; j < 128; j++) { CFX_ByteString name = PDF_AdobeNameFromUnicode(pUnicodes[j]); pArray->AddName(name.IsEmpty() ? ".notdef" : name); } pEncodingDict->SetFor("Differences", pArray); - pBaseDict->SetReferenceFor("Encoding", this, - AddIndirectObject(pEncodingDict)); - + pBaseDict->SetReferenceFor("Encoding", this, pEncodingDict); return i; } @@ -802,7 +803,7 @@ CPDF_Dictionary* CPDF_Document::ProcessbCJK( bool bVert, CFX_ByteString basefont, std::function<void(FX_WCHAR, FX_WCHAR, CPDF_Array*)> Insert) { - CPDF_Dictionary* pFontDict = new CPDF_Dictionary(m_pByteStringPool); + CPDF_Dictionary* pFontDict = NewIndirect<CPDF_Dictionary>(m_pByteStringPool); CFX_ByteString cmap; CFX_ByteString ordering; int supplement = 0; @@ -859,7 +860,7 @@ CPDF_Dictionary* CPDF_Document::ProcessbCJK( pFontDict->SetFor("CIDSystemInfo", pCIDSysInfo); CPDF_Array* pArray = new CPDF_Array; pBaseDict->SetFor("DescendantFonts", pArray); - pArray->AddReference(this, AddIndirectObject(pFontDict)); + pArray->AddReference(this, pFontDict->GetObjNum()); return pFontDict; } @@ -877,7 +878,7 @@ CPDF_Font* CPDF_Document::AddFont(CFX_Font* pFont, int charset, bool bVert) { CalculateFlags(pFont->IsBold(), pFont->IsItalic(), pFont->IsFixedWidth(), false, false, charset == FXFONT_SYMBOL_CHARSET); - CPDF_Dictionary* pBaseDict = new CPDF_Dictionary(m_pByteStringPool); + CPDF_Dictionary* pBaseDict = NewIndirect<CPDF_Dictionary>(m_pByteStringPool); pBaseDict->SetNameFor("Type", "Font"); std::unique_ptr<CFX_UnicodeEncoding> pEncoding( new CFX_UnicodeEncoding(pFont)); @@ -918,7 +919,6 @@ CPDF_Font* CPDF_Document::AddFont(CFX_Font* pFont, int charset, bool bVert) { end, widthArr); }); } - AddIndirectObject(pBaseDict); int italicangle = pFont->GetSubstFont() ? pFont->GetSubstFont()->m_ItalicAngle : 0; FX_RECT bbox; @@ -943,11 +943,10 @@ CPDF_Font* CPDF_Document::AddFont(CFX_Font* pFont, int charset, bool bVert) { nStemV = width; } } - CPDF_Dictionary* pFontDesc = + CPDF_Dictionary* pFontDesc = ToDictionary(AddIndirectObject( CalculateFontDesc(this, basefont, flags, italicangle, pFont->GetAscent(), - pFont->GetDescent(), pBBox, nStemV); - pFontDict->SetReferenceFor("FontDescriptor", this, - AddIndirectObject(pFontDesc)); + pFont->GetDescent(), pBBox, nStemV))); + pFontDict->SetReferenceFor("FontDescriptor", this, pFontDesc); return LoadFont(pBaseDict); } @@ -1009,7 +1008,7 @@ CPDF_Font* CPDF_Document::AddWindowsFont(LOGFONTA* pLogFont, ptm->otmrcFontBox.right, ptm->otmrcFontBox.top}; FX_Free(tm_buf); basefont.Replace(" ", ""); - CPDF_Dictionary* pBaseDict = new CPDF_Dictionary(m_pByteStringPool); + CPDF_Dictionary* pBaseDict = NewIndirect<CPDF_Dictionary>(m_pByteStringPool); pBaseDict->SetNameFor("Type", "Font"); CPDF_Dictionary* pFontDict = pBaseDict; if (!bCJK) { @@ -1034,16 +1033,15 @@ 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]); - CPDF_Dictionary* pFontDesc = + std::unique_ptr<CPDF_Dictionary> pFontDesc = CalculateFontDesc(this, basefont, flags, italicangle, ascend, descend, pBBox, pLogFont->lfWeight / 5); pFontDesc->SetIntegerFor("CapHeight", capheight); pFontDict->SetReferenceFor("FontDescriptor", this, - AddIndirectObject(pFontDesc)); + AddIndirectObject(std::move(pFontDesc))); hFont = SelectObject(hDC, hFont); DeleteObject(hFont); DeleteDC(hDC); diff --git a/core/fpdfapi/parser/cpdf_document_unittest.cpp b/core/fpdfapi/parser/cpdf_document_unittest.cpp index f27e7403f6..048a9fead2 100644 --- a/core/fpdfapi/parser/cpdf_document_unittest.cpp +++ b/core/fpdfapi/parser/cpdf_document_unittest.cpp @@ -13,24 +13,25 @@ #include "core/fpdfapi/parser/cpdf_parser.h" #include "core/fxcrt/fx_memory.h" #include "testing/gtest/include/gtest/gtest.h" +#include "third_party/base/ptr_util.h" namespace { -CPDF_Dictionary* CreatePageTreeNode(CPDF_Array* kids, +CPDF_Dictionary* CreatePageTreeNode(std::unique_ptr<CPDF_Array> kids, CPDF_Document* pDoc, int count) { - CPDF_Dictionary* pageNode = new CPDF_Dictionary(); + CPDF_Array* pUnowned = pDoc->AddIndirectObject(std::move(kids))->AsArray(); + CPDF_Dictionary* pageNode = pDoc->NewIndirect<CPDF_Dictionary>(); pageNode->SetStringFor("Type", "Pages"); - pageNode->SetReferenceFor("Kids", pDoc, pDoc->AddIndirectObject(kids)); + pageNode->SetReferenceFor("Kids", pDoc, pUnowned); pageNode->SetIntegerFor("Count", count); - uint32_t pageNodeRef = pDoc->AddIndirectObject(pageNode); - for (size_t i = 0; i < kids->GetCount(); i++) - kids->GetDictAt(i)->SetReferenceFor("Parent", pDoc, pageNodeRef); + for (size_t i = 0; i < pUnowned->GetCount(); i++) + pUnowned->GetDictAt(i)->SetReferenceFor("Parent", pDoc, pageNode); return pageNode; } -CPDF_Dictionary* CreateNumberedPage(size_t number) { - CPDF_Dictionary* page = new CPDF_Dictionary(); +std::unique_ptr<CPDF_Dictionary> CreateNumberedPage(size_t number) { + auto page = pdfium::MakeUnique<CPDF_Dictionary>(); page->SetStringFor("Type", "Page"); page->SetIntegerFor("PageNumbering", number); return page; @@ -40,34 +41,37 @@ class CPDF_TestDocumentForPages : public CPDF_Document { public: CPDF_TestDocumentForPages() : CPDF_Document(nullptr) { // Set up test - CPDF_Array* zeroToTwo = new CPDF_Array(); + auto zeroToTwo = pdfium::MakeUnique<CPDF_Array>(); zeroToTwo->AddReference(this, AddIndirectObject(CreateNumberedPage(0))); zeroToTwo->AddReference(this, AddIndirectObject(CreateNumberedPage(1))); zeroToTwo->AddReference(this, AddIndirectObject(CreateNumberedPage(2))); - CPDF_Dictionary* branch1 = CreatePageTreeNode(zeroToTwo, this, 3); + CPDF_Dictionary* branch1 = + CreatePageTreeNode(std::move(zeroToTwo), this, 3); - CPDF_Array* zeroToThree = new CPDF_Array(); + auto zeroToThree = pdfium::MakeUnique<CPDF_Array>(); zeroToThree->AddReference(this, branch1->GetObjNum()); zeroToThree->AddReference(this, AddIndirectObject(CreateNumberedPage(3))); - CPDF_Dictionary* branch2 = CreatePageTreeNode(zeroToThree, this, 4); + CPDF_Dictionary* branch2 = + CreatePageTreeNode(std::move(zeroToThree), this, 4); - CPDF_Array* fourFive = new CPDF_Array(); + auto fourFive = pdfium::MakeUnique<CPDF_Array>(); fourFive->AddReference(this, AddIndirectObject(CreateNumberedPage(4))); fourFive->AddReference(this, AddIndirectObject(CreateNumberedPage(5))); - CPDF_Dictionary* branch3 = CreatePageTreeNode(fourFive, this, 2); + CPDF_Dictionary* branch3 = CreatePageTreeNode(std::move(fourFive), this, 2); - CPDF_Array* justSix = new CPDF_Array(); + auto justSix = pdfium::MakeUnique<CPDF_Array>(); justSix->AddReference(this, AddIndirectObject(CreateNumberedPage(6))); - CPDF_Dictionary* branch4 = CreatePageTreeNode(justSix, this, 1); + CPDF_Dictionary* branch4 = CreatePageTreeNode(std::move(justSix), this, 1); - CPDF_Array* allPages = new CPDF_Array(); - allPages->AddReference(this, branch2->GetObjNum()); - allPages->AddReference(this, branch3->GetObjNum()); - allPages->AddReference(this, branch4->GetObjNum()); - CPDF_Dictionary* pagesDict = CreatePageTreeNode(allPages, this, 7); + auto allPages = pdfium::MakeUnique<CPDF_Array>(); + allPages->AddReference(this, branch2); + allPages->AddReference(this, branch3); + allPages->AddReference(this, branch4); + CPDF_Dictionary* pagesDict = + CreatePageTreeNode(std::move(allPages), this, 7); - m_pOwnedRootDict.reset(new CPDF_Dictionary()); - m_pOwnedRootDict->SetReferenceFor("Pages", this, pagesDict->GetObjNum()); + m_pOwnedRootDict = pdfium::MakeUnique<CPDF_Dictionary>(); + m_pOwnedRootDict->SetReferenceFor("Pages", this, pagesDict); m_pRootDict = m_pOwnedRootDict.get(); m_PageList.SetSize(7); } @@ -80,13 +84,13 @@ class CPDF_TestDocumentWithPageWithoutPageNum : public CPDF_Document { public: CPDF_TestDocumentWithPageWithoutPageNum() : CPDF_Document(nullptr) { // Set up test - CPDF_Array* allPages = new CPDF_Array(); + auto allPages = pdfium::MakeUnique<CPDF_Array>(); allPages->AddReference(this, AddIndirectObject(CreateNumberedPage(0))); allPages->AddReference(this, AddIndirectObject(CreateNumberedPage(1))); // Page without pageNum. - allPages->Add(CreateNumberedPage(2)); - CPDF_Dictionary* pagesDict = CreatePageTreeNode(allPages, this, 3); - + allPages->Add(CreateNumberedPage(2).release()); + CPDF_Dictionary* pagesDict = + CreatePageTreeNode(std::move(allPages), this, 3); m_pOwnedRootDict.reset(new CPDF_Dictionary()); m_pOwnedRootDict->SetReferenceFor("Pages", this, pagesDict->GetObjNum()); m_pRootDict = m_pOwnedRootDict.get(); @@ -186,15 +190,14 @@ TEST_F(cpdf_document_test, UseCachedPageObjNumIfHaveNotPagesDict) { TestLinearized linearized(dict.get()); document.LoadLinearizedDoc(&linearized); ASSERT_EQ(page_count, document.GetPageCount()); - CPDF_Object* page_stub = new CPDF_Dictionary(); - const uint32_t obj_num = document.AddIndirectObject(page_stub); + CPDF_Object* page_stub = document.NewIndirect<CPDF_Dictionary>(); + const uint32_t obj_num = page_stub->GetObjNum(); const int test_page_num = 33; EXPECT_FALSE(document.IsPageLoaded(test_page_num)); EXPECT_EQ(nullptr, document.GetPage(test_page_num)); document.SetPageObjNum(test_page_num, obj_num); - EXPECT_TRUE(document.IsPageLoaded(test_page_num)); EXPECT_EQ(page_stub, document.GetPage(test_page_num)); } diff --git a/core/fpdfapi/parser/cpdf_indirect_object_holder.cpp b/core/fpdfapi/parser/cpdf_indirect_object_holder.cpp index 529eda21fb..12b1e9f089 100644 --- a/core/fpdfapi/parser/cpdf_indirect_object_holder.cpp +++ b/core/fpdfapi/parser/cpdf_indirect_object_holder.cpp @@ -43,13 +43,14 @@ std::unique_ptr<CPDF_Object> CPDF_IndirectObjectHolder::ParseIndirectObject( return nullptr; } -uint32_t CPDF_IndirectObjectHolder::AddIndirectObject(CPDF_Object* pObj) { +CPDF_Object* CPDF_IndirectObjectHolder::AddIndirectObject( + std::unique_ptr<CPDF_Object> pObj) { CHECK(!pObj->m_ObjNum); - m_LastObjNum++; + CPDF_Object* pUnowned = pObj.get(); + pObj->m_ObjNum = ++m_LastObjNum; m_IndirectObjs[m_LastObjNum].release(); // TODO(tsepez): stop this leak. - m_IndirectObjs[m_LastObjNum].reset(pObj); - pObj->m_ObjNum = m_LastObjNum; - return m_LastObjNum; + m_IndirectObjs[m_LastObjNum] = std::move(pObj); + return pUnowned; } bool CPDF_IndirectObjectHolder::ReplaceIndirectObjectIfHigherGeneration( diff --git a/core/fpdfapi/parser/cpdf_indirect_object_holder.h b/core/fpdfapi/parser/cpdf_indirect_object_holder.h index db6f4acbf4..428bfa05ec 100644 --- a/core/fpdfapi/parser/cpdf_indirect_object_holder.h +++ b/core/fpdfapi/parser/cpdf_indirect_object_holder.h @@ -11,6 +11,7 @@ #include <memory> #include "core/fxcrt/fx_system.h" +#include "third_party/base/ptr_util.h" class CPDF_Object; @@ -26,8 +27,17 @@ class CPDF_IndirectObjectHolder { CPDF_Object* GetOrParseIndirectObject(uint32_t objnum); void DeleteIndirectObject(uint32_t objnum); - // Take ownership of |pObj|. - uint32_t AddIndirectObject(CPDF_Object* pObj); + // Creates and adds a new object owned by the indirect object holder, + // and returns an unowned pointer to it. + template <typename T, typename... Args> + T* NewIndirect(Args... args) { + return static_cast<T*>(AddIndirectObject(pdfium::MakeUnique<T>(args...))); + } + + // Takes ownership of |pObj|, returns unowned pointer to it. + CPDF_Object* AddIndirectObject(std::unique_ptr<CPDF_Object> pObj); + + // Always takes ownership of |pObj|, return true if higher generation number. bool ReplaceIndirectObjectIfHigherGeneration( uint32_t objnum, std::unique_ptr<CPDF_Object> pObj); diff --git a/core/fpdfapi/parser/cpdf_object_unittest.cpp b/core/fpdfapi/parser/cpdf_object_unittest.cpp index b40a8e373d..4145f248fa 100644 --- a/core/fpdfapi/parser/cpdf_object_unittest.cpp +++ b/core/fpdfapi/parser/cpdf_object_unittest.cpp @@ -90,16 +90,17 @@ class PDFObjectsTest : public testing::Test { m_DirectObjs.emplace_back(objs[i]); // Indirect references to indirect objects. - m_ObjHolder.reset(new CPDF_IndirectObjectHolder()); - m_IndirectObjs = { - boolean_true_obj->Clone().release(), number_int_obj->Clone().release(), - str_spec_obj->Clone().release(), name_obj->Clone().release(), - m_ArrayObj->Clone().release(), m_DictObj->Clone().release(), - stream_obj->Clone().release()}; - for (size_t i = 0; i < m_IndirectObjs.size(); ++i) { - m_ObjHolder->AddIndirectObject(m_IndirectObjs[i]); - m_RefObjs.emplace_back(new CPDF_Reference( - m_ObjHolder.get(), m_IndirectObjs[i]->GetObjNum())); + m_ObjHolder = pdfium::MakeUnique<CPDF_IndirectObjectHolder>(); + m_IndirectObjs = {m_ObjHolder->AddIndirectObject(boolean_true_obj->Clone()), + m_ObjHolder->AddIndirectObject(number_int_obj->Clone()), + m_ObjHolder->AddIndirectObject(str_spec_obj->Clone()), + m_ObjHolder->AddIndirectObject(name_obj->Clone()), + m_ObjHolder->AddIndirectObject(m_ArrayObj->Clone()), + m_ObjHolder->AddIndirectObject(m_DictObj->Clone()), + m_ObjHolder->AddIndirectObject(stream_obj->Clone())}; + for (CPDF_Object* pObj : m_IndirectObjs) { + m_RefObjs.emplace_back( + new CPDF_Reference(m_ObjHolder.get(), pObj->GetObjNum())); } } @@ -837,14 +838,13 @@ TEST(PDFObjectTest, CloneCheckLoop) { { CPDF_IndirectObjectHolder objects_holder; // Create an object with a reference loop. - 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); + CPDF_Dictionary* dict_obj = objects_holder.NewIndirect<CPDF_Dictionary>(); + std::unique_ptr<CPDF_Array> arr_obj = pdfium::MakeUnique<CPDF_Array>(); arr_obj->InsertAt( 0, new CPDF_Reference(&objects_holder, dict_obj->GetObjNum())); CPDF_Object* elem0 = arr_obj->GetObjectAt(0); + dict_obj->SetFor("arr", arr_obj.release()); + EXPECT_EQ(1u, dict_obj->GetObjNum()); ASSERT_TRUE(elem0); ASSERT_TRUE(elem0->IsReference()); EXPECT_EQ(1u, elem0->AsReference()->GetRefObjNum()); diff --git a/core/fpdfdoc/cpdf_formfield_unittest.cpp b/core/fpdfdoc/cpdf_formfield_unittest.cpp index 11cccf19b8..f69df1d064 100644 --- a/core/fpdfdoc/cpdf_formfield_unittest.cpp +++ b/core/fpdfdoc/cpdf_formfield_unittest.cpp @@ -12,15 +12,13 @@ TEST(cpdf_formfield, FPDF_GetFullName) { EXPECT_TRUE(name.IsEmpty()); CPDF_IndirectObjectHolder obj_holder; - CPDF_Dictionary* root = new CPDF_Dictionary(); - obj_holder.AddIndirectObject(root); + CPDF_Dictionary* root = obj_holder.NewIndirect<CPDF_Dictionary>(); root->SetNameFor("T", "foo"); name = FPDF_GetFullName(root); EXPECT_STREQ("foo", name.UTF8Encode().c_str()); - CPDF_Dictionary* dict1 = new CPDF_Dictionary(); - root->SetReferenceFor("Parent", &obj_holder, - obj_holder.AddIndirectObject(dict1)); + CPDF_Dictionary* dict1 = obj_holder.NewIndirect<CPDF_Dictionary>(); + root->SetReferenceFor("Parent", &obj_holder, dict1); dict1->SetNameFor("T", "bar"); name = FPDF_GetFullName(root); EXPECT_STREQ("bar.foo", name.UTF8Encode().c_str()); @@ -30,9 +28,9 @@ TEST(cpdf_formfield, FPDF_GetFullName) { name = FPDF_GetFullName(root); EXPECT_STREQ("bar.foo", name.UTF8Encode().c_str()); - CPDF_Dictionary* dict3 = new CPDF_Dictionary(); - dict2->SetReferenceFor("Parent", &obj_holder, - obj_holder.AddIndirectObject(dict3)); + CPDF_Dictionary* dict3 = obj_holder.NewIndirect<CPDF_Dictionary>(); + dict2->SetReferenceFor("Parent", &obj_holder, 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 2a02e890c3..64d15a0d88 100644 --- a/core/fpdfdoc/cpdf_interform.cpp +++ b/core/fpdfdoc/cpdf_interform.cpp @@ -59,9 +59,10 @@ void InitDict(CPDF_Dictionary*& pFormDict, CPDF_Document* pDocument) { return; if (!pFormDict) { - pFormDict = new CPDF_Dictionary(pDocument->GetByteStringPool()); - pDocument->GetRoot()->SetReferenceFor( - "AcroForm", pDocument, pDocument->AddIndirectObject(pFormDict)); + pFormDict = + pDocument->NewIndirect<CPDF_Dictionary>(pDocument->GetByteStringPool()); + pDocument->GetRoot()->SetReferenceFor("AcroForm", pDocument, + pFormDict->GetObjNum()); } CFX_ByteString csDA; diff --git a/core/fpdfdoc/cpvt_generateap.cpp b/core/fpdfdoc/cpvt_generateap.cpp index 0802e907fd..c8bba64b1e 100644 --- a/core/fpdfdoc/cpvt_generateap.cpp +++ b/core/fpdfdoc/cpvt_generateap.cpp @@ -61,16 +61,12 @@ bool GenerateWidgetAP(CPDF_Document* pDoc, CPDF_Dictionary* pFontDict = pDRFontDict->GetDictFor(sFontName.Mid(1)); if (!pFontDict) { - auto pNewFontDict = - pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool()); - pNewFontDict->SetNameFor("Type", "Font"); - pNewFontDict->SetNameFor("Subtype", "Type1"); - pNewFontDict->SetNameFor("BaseFont", "Helvetica"); - pNewFontDict->SetNameFor("Encoding", "WinAnsiEncoding"); - // Ownership passes to AddIndirectObject(). - pFontDict = pNewFontDict.release(); - pDRFontDict->SetReferenceFor(sFontName.Mid(1), pDoc, - pDoc->AddIndirectObject(pFontDict)); + pFontDict = pDoc->NewIndirect<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); } CPDF_Font* pDefFont = pDoc->LoadFont(pFontDict); if (!pDefFont) @@ -177,10 +173,8 @@ bool GenerateWidgetAP(CPDF_Document* pDoc, } CPDF_Stream* pNormalStream = pAPDict->GetStreamFor("N"); if (!pNormalStream) { - auto pNewNormalStream = pdfium::MakeUnique<CPDF_Stream>(); - // Ownership passes to AddIndirectObject(). - pNormalStream = pNewNormalStream.release(); - pAPDict->SetReferenceFor("N", pDoc, pDoc->AddIndirectObject(pNormalStream)); + pNormalStream = pDoc->NewIndirect<CPDF_Stream>(); + pAPDict->SetReferenceFor("N", pDoc, pNormalStream); } CPDF_Dictionary* pStreamDict = pNormalStream->GetDict(); if (pStreamDict) { @@ -197,8 +191,7 @@ bool GenerateWidgetAP(CPDF_Document* pDoc, pStreamResList->SetFor("Font", pStreamResFontList); } if (!pStreamResFontList->KeyExist(sFontName)) - pStreamResFontList->SetReferenceFor(sFontName, pDoc, - pFontDict->GetObjNum()); + pStreamResFontList->SetReferenceFor(sFontName, pDoc, pFontDict); } else { pStreamDict->SetFor("Resources", pFormDict->GetDictFor("DR")->Clone().release()); @@ -452,8 +445,7 @@ bool GenerateWidgetAP(CPDF_Document* pDoc, pStreamResList->SetFor("Font", pStreamResFontList); } if (!pStreamResFontList->KeyExist(sFontName)) - pStreamResFontList->SetReferenceFor(sFontName, pDoc, - pFontDict->GetObjNum()); + pStreamResFontList->SetReferenceFor(sFontName, pDoc, pFontDict); } else { pStreamDict->SetFor("Resources", pFormDict->GetDictFor("DR")->Clone().release()); @@ -580,8 +572,8 @@ std::unique_ptr<CPDF_Dictionary> GenerateExtGStateDict( std::unique_ptr<CPDF_Dictionary> GenerateResourceFontDict( CPDF_Document* pDoc, const CFX_ByteString& sFontDictName) { - auto pFontDict = - pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool()); + CPDF_Dictionary* pFontDict = + pDoc->NewIndirect<CPDF_Dictionary>(pDoc->GetByteStringPool()); pFontDict->SetNameFor("Type", "Font"); pFontDict->SetNameFor("Subtype", "Type1"); pFontDict->SetNameFor("BaseFont", "Helvetica"); @@ -589,8 +581,7 @@ std::unique_ptr<CPDF_Dictionary> GenerateResourceFontDict( auto pResourceFontDict = pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool()); - pResourceFontDict->SetReferenceFor( - sFontDictName, pDoc, pDoc->AddIndirectObject(pFontDict.release())); + pResourceFontDict->SetReferenceFor(sFontDictName, pDoc, pFontDict); return pResourceFontDict; } @@ -612,15 +603,14 @@ void GenerateAndSetAPDict(CPDF_Document* pDoc, const CFX_ByteTextBuf& sAppStream, std::unique_ptr<CPDF_Dictionary> pResourceDict, bool bIsTextMarkupAnnotation) { - auto pNormalStream = pdfium::MakeUnique<CPDF_Stream>(); + CPDF_Stream* pNormalStream = pDoc->NewIndirect<CPDF_Stream>(); pNormalStream->SetData(sAppStream.GetBuffer(), sAppStream.GetSize()); - CPDF_Dictionary* pStreamDict = pNormalStream->GetDict(); auto pAPDict = pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool()); - pAPDict->SetReferenceFor("N", pDoc, - pDoc->AddIndirectObject(pNormalStream.release())); + pAPDict->SetReferenceFor("N", pDoc, pNormalStream); pAnnotDict->SetFor("AP", pAPDict.release()); + CPDF_Dictionary* pStreamDict = pNormalStream->GetDict(); pStreamDict->SetIntegerFor("FormType", 1); pStreamDict->SetStringFor("Subtype", "Form"); pStreamDict->SetMatrixFor("Matrix", CFX_Matrix()); diff --git a/fpdfsdk/cpdfsdk_baannot.cpp b/fpdfsdk/cpdfsdk_baannot.cpp index 0e6c53cc78..9436b3ba62 100644 --- a/fpdfsdk/cpdfsdk_baannot.cpp +++ b/fpdfsdk/cpdfsdk_baannot.cpp @@ -316,10 +316,9 @@ void CPDFSDK_BAAnnot::WriteAppearance(const CFX_ByteString& sAPType, } if (!pStream) { - pStream = new CPDF_Stream; CPDF_Document* pDoc = m_pPageView->GetPDFDocument(); - pParentDict->SetReferenceFor(sAPType, pDoc, - pDoc->AddIndirectObject(pStream)); + pStream = pDoc->NewIndirect<CPDF_Stream>(); + pParentDict->SetReferenceFor(sAPType, pDoc, pStream); } CPDF_Dictionary* pStreamDict = pStream->GetDict(); @@ -354,8 +353,9 @@ 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(); - m_pAnnot->GetAnnotDict()->SetReferenceFor("A", pDoc, - pDoc->AddIndirectObject(pDict)); + if (pDict->IsInline()) + pDict = pDoc->AddIndirectObject(pDict->Clone())->AsDictionary(); + m_pAnnot->GetAnnotDict()->SetReferenceFor("A", pDoc, pDict); } } diff --git a/fpdfsdk/formfiller/cba_fontmap.cpp b/fpdfsdk/formfiller/cba_fontmap.cpp index 26feaf765d..2d962ad276 100644 --- a/fpdfsdk/formfiller/cba_fontmap.cpp +++ b/fpdfsdk/formfiller/cba_fontmap.cpp @@ -166,9 +166,8 @@ void CBA_FontMap::AddFontToAnnotDict(CPDF_Font* pFont, CPDF_Stream* pStream = pAPDict->GetStreamFor(m_sAPType); if (!pStream) { - pStream = new CPDF_Stream; - pAPDict->SetReferenceFor(m_sAPType, m_pDocument, - m_pDocument->AddIndirectObject(pStream)); + pStream = m_pDocument->NewIndirect<CPDF_Stream>(); + pAPDict->SetReferenceFor(m_sAPType, m_pDocument, pStream); } CPDF_Dictionary* pStreamDict = pStream->GetDict(); @@ -185,11 +184,9 @@ void CBA_FontMap::AddFontToAnnotDict(CPDF_Font* pFont, } CPDF_Dictionary* pStreamResFontList = pStreamResList->GetDictFor("Font"); if (!pStreamResFontList) { - pStreamResFontList = - new CPDF_Dictionary(m_pDocument->GetByteStringPool()); - pStreamResList->SetReferenceFor( - "Font", m_pDocument, - m_pDocument->AddIndirectObject(pStreamResFontList)); + pStreamResFontList = m_pDocument->NewIndirect<CPDF_Dictionary>( + m_pDocument->GetByteStringPool()); + pStreamResList->SetReferenceFor("Font", m_pDocument, pStreamResFontList); } if (!pStreamResFontList->KeyExist(sAlias)) { pStreamResFontList->SetReferenceFor(sAlias, m_pDocument, diff --git a/fpdfsdk/fpdf_flatten.cpp b/fpdfsdk/fpdf_flatten.cpp index 1b93bb4d88..b11e4a621d 100644 --- a/fpdfsdk/fpdf_flatten.cpp +++ b/fpdfsdk/fpdf_flatten.cpp @@ -172,12 +172,12 @@ CFX_FloatRect CalculateRect(std::vector<CFX_FloatRect>* pRectArray) { uint32_t NewIndirectContentsStream(const CFX_ByteString& key, CPDF_Document* pDocument) { - CPDF_Stream* pNewContents = new CPDF_Stream( + CPDF_Stream* pNewContents = pDocument->NewIndirect<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 pDocument->AddIndirectObject(pNewContents); + return pNewContents->GetObjNum(); } void SetPageContents(const CFX_ByteString& key, @@ -197,7 +197,7 @@ void SetPageContents(const CFX_ByteString& key, } pPage->ConvertToIndirectObjectFor("Contents", pDocument); if (!pContentsArray) { - pContentsArray = new CPDF_Array; + pContentsArray = pDocument->NewIndirect<CPDF_Array>(); CPDF_StreamAcc acc; acc.LoadAllData(pContentsStream); CFX_ByteString sStream = "q\n"; @@ -206,8 +206,7 @@ void SetPageContents(const CFX_ByteString& key, sStream = sStream + sBody + "\nQ"; pContentsStream->SetData(sStream.raw_str(), sStream.GetLength()); pContentsArray->AddReference(pDocument, pContentsStream->GetObjNum()); - pPage->SetReferenceFor("Contents", pDocument, - pDocument->AddIndirectObject(pContentsArray)); + pPage->SetReferenceFor("Contents", pDocument, pContentsArray); } if (!key.IsEmpty()) { pContentsArray->AddReference(pDocument, @@ -293,10 +292,10 @@ DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { pPageDict->SetFor("Resources", pRes); } - CPDF_Stream* pNewXObject = new CPDF_Stream( + CPDF_Stream* pNewXObject = pDocument->NewIndirect<CPDF_Stream>( nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool())); - uint32_t dwObjNum = pDocument->AddIndirectObject(pNewXObject); + uint32_t dwObjNum = pNewXObject->GetObjNum(); CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject"); if (!pPageXObject) { pPageXObject = new CPDF_Dictionary(pDocument->GetByteStringPool()); @@ -383,7 +382,7 @@ DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { if (pObj->IsInline()) { std::unique_ptr<CPDF_Object> pNew = pObj->Clone(); pObj = pNew.get(); - pDocument->AddIndirectObject(pNew.release()); + pDocument->AddIndirectObject(std::move(pNew)); } CPDF_Dictionary* pObjDic = pObj->GetDict(); diff --git a/fpdfsdk/fpdf_transformpage.cpp b/fpdfsdk/fpdf_transformpage.cpp index 63a46dfe7a..76be4de092 100644 --- a/fpdfsdk/fpdf_transformpage.cpp +++ b/fpdfsdk/fpdf_transformpage.cpp @@ -130,14 +130,13 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page, return false; CPDF_Dictionary* pDic = new CPDF_Dictionary(pDoc->GetByteStringPool()); - CPDF_Stream* pStream = new CPDF_Stream(nullptr, 0, pDic); + CPDF_Stream* pStream = pDoc->NewIndirect<CPDF_Stream>(nullptr, 0, pDic); pStream->SetData(textBuf.GetBuffer(), textBuf.GetSize()); - pDoc->AddIndirectObject(pStream); + pDic = new CPDF_Dictionary(pDoc->GetByteStringPool()); - CPDF_Stream* pEndStream = new CPDF_Stream(nullptr, 0, pDic); + CPDF_Stream* pEndStream = pDoc->NewIndirect<CPDF_Stream>(nullptr, 0, pDic); pEndStream->SetData((const uint8_t*)" Q", 2); - pDoc->AddIndirectObject(pEndStream); CPDF_Array* pContentArray = nullptr; CPDF_Array* pArray = ToArray(pContentObj); @@ -156,12 +155,11 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page, pContentArray->InsertAt(0, pRef); pContentArray->AddReference(pDoc, pEndStream->GetObjNum()); } else if (pDirectObj->IsStream()) { - pContentArray = new CPDF_Array(); + pContentArray = pDoc->NewIndirect<CPDF_Array>(); pContentArray->AddReference(pDoc, pStream->GetObjNum()); pContentArray->AddReference(pDoc, pDirectObj->GetObjNum()); pContentArray->AddReference(pDoc, pEndStream->GetObjNum()); - pPageDic->SetReferenceFor("Contents", pDoc, - pDoc->AddIndirectObject(pContentArray)); + pPageDic->SetReferenceFor("Contents", pDoc, pContentArray); } } } @@ -306,9 +304,8 @@ DLLEXPORT void STDCALL FPDFPage_InsertClipPath(FPDF_PAGE page, return; CPDF_Dictionary* pDic = new CPDF_Dictionary(pDoc->GetByteStringPool()); - CPDF_Stream* pStream = new CPDF_Stream(nullptr, 0, pDic); + CPDF_Stream* pStream = pDoc->NewIndirect<CPDF_Stream>(nullptr, 0, pDic); pStream->SetData(strClip.GetBuffer(), strClip.GetSize()); - pDoc->AddIndirectObject(pStream); CPDF_Array* pContentArray = nullptr; CPDF_Array* pArray = ToArray(pContentObj); @@ -325,11 +322,10 @@ 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 = new CPDF_Array(); + pContentArray = pDoc->NewIndirect<CPDF_Array>(); pContentArray->AddReference(pDoc, pStream->GetObjNum()); pContentArray->AddReference(pDoc, pDirectObj->GetObjNum()); - pPageDic->SetReferenceFor("Contents", pDoc, - pDoc->AddIndirectObject(pContentArray)); + pPageDic->SetReferenceFor("Contents", pDoc, pContentArray); } } } diff --git a/fpdfsdk/fpdfdoc_unittest.cpp b/fpdfsdk/fpdfdoc_unittest.cpp index fc902570b4..7d1d6b4ba8 100644 --- a/fpdfsdk/fpdfdoc_unittest.cpp +++ b/fpdfsdk/fpdfdoc_unittest.cpp @@ -81,8 +81,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 = new CPDF_Dictionary(); - info.push_back({m_pIndirectObjs->AddIndirectObject(obj), obj}); + CPDF_Dictionary* obj = m_pIndirectObjs->NewIndirect<CPDF_Dictionary>(); + info.push_back({obj->GetObjNum(), obj}); } return info; } diff --git a/fpdfsdk/fpdfppo.cpp b/fpdfsdk/fpdfppo.cpp index 705c7e21da..610aae7abd 100644 --- a/fpdfsdk/fpdfppo.cpp +++ b/fpdfsdk/fpdfppo.cpp @@ -170,9 +170,9 @@ bool CPDF_PageOrganizer::PDFDocInit() { CPDF_Dictionary* pNewPages = pElement ? ToDictionary(pElement->GetDirect()) : nullptr; if (!pNewPages) { - pNewPages = new CPDF_Dictionary(m_pDestPDFDoc->GetByteStringPool()); - pNewRoot->SetReferenceFor("Pages", m_pDestPDFDoc, - m_pDestPDFDoc->AddIndirectObject(pNewPages)); + pNewPages = m_pDestPDFDoc->NewIndirect<CPDF_Dictionary>( + m_pDestPDFDoc->GetByteStringPool()); + pNewRoot->SetReferenceFor("Pages", m_pDestPDFDoc, pNewPages); } CFX_ByteString cbPageType = pNewPages->GetStringFor("Type", ""); @@ -181,9 +181,8 @@ bool CPDF_PageOrganizer::PDFDocInit() { if (!pNewPages->GetArrayFor("Kids")) { pNewPages->SetIntegerFor("Count", 0); - pNewPages->SetReferenceFor( - "Kids", m_pDestPDFDoc, - m_pDestPDFDoc->AddIndirectObject(new CPDF_Array)); + pNewPages->SetReferenceFor("Kids", m_pDestPDFDoc, + m_pDestPDFDoc->NewIndirect<CPDF_Array>()); } return true; @@ -331,8 +330,9 @@ uint32_t CPDF_PageOrganizer::GetNewObjId(ObjectNumberMap* pObjNumberMap, return 0; } } - CPDF_Object* pUnownedClone = pClone.get(); - dwNewObjNum = m_pDestPDFDoc->AddIndirectObject(pClone.release()); + CPDF_Object* pUnownedClone = + m_pDestPDFDoc->AddIndirectObject(std::move(pClone)); + dwNewObjNum = pUnownedClone->GetObjNum(); (*pObjNumberMap)[dwObjnum] = dwNewObjNum; if (!UpdateReference(pUnownedClone, pObjNumberMap)) return 0; diff --git a/fpdfsdk/fpdfsave.cpp b/fpdfsdk/fpdfsave.cpp index 1b639b7cc4..e5d7a409ae 100644 --- a/fpdfsdk/fpdfsave.cpp +++ b/fpdfsdk/fpdfsave.cpp @@ -180,14 +180,12 @@ bool SaveXFADocumentData(CPDFXFA_Context* pContext, if (pDataSetsStream) pDataSetsStream->InitStreamFromFile(pDsfileWrite.get(), pDataDict); } else { - CPDF_Stream* pData = new CPDF_Stream; + CPDF_Stream* pData = pPDFDocument->NewIndirect<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, - pPDFDocument->AddIndirectObject(pData))); + pArray->InsertAt(iLast + 1, + new CPDF_Reference(pPDFDocument, pData->GetObjNum())); } fileList->push_back(std::move(pDsfileWrite)); } @@ -204,14 +202,12 @@ bool SaveXFADocumentData(CPDFXFA_Context* pContext, if (pFormStream) pFormStream->InitStreamFromFile(pfileWrite.get(), pDataDict); } else { - CPDF_Stream* pData = new CPDF_Stream; + CPDF_Stream* pData = pPDFDocument->NewIndirect<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, - pPDFDocument->AddIndirectObject(pData))); + pArray->InsertAt(iLast + 1, + new CPDF_Reference(pPDFDocument, pData->GetObjNum())); } fileList->push_back(std::move(pfileWrite)); } |