From 698c5716d005860360527e4cfe15b4a185589117 Mon Sep 17 00:00:00 2001 From: tsepez Date: Wed, 28 Sep 2016 16:47:07 -0700 Subject: Use string pools in some dictionaries, names, and strings. BUG=pdfium:597 Review-Url: https://codereview.chromium.org/2345063002 --- fpdfsdk/cpdfsdk_baannot.cpp | 26 ++++++++++++-------------- fpdfsdk/cpdfsdk_widget.cpp | 8 +++----- fpdfsdk/formfiller/cba_fontmap.cpp | 9 +++++---- fpdfsdk/fpdf_flatten.cpp | 21 +++++++++++---------- fpdfsdk/fpdf_transformpage.cpp | 15 ++++++++------- fpdfsdk/fpdfdoc_unittest.cpp | 8 +++++--- fpdfsdk/fpdfeditpage.cpp | 4 ++-- fpdfsdk/fpdfppo.cpp | 2 +- fpdfsdk/fpdfsave.cpp | 6 ++++-- 9 files changed, 51 insertions(+), 48 deletions(-) (limited to 'fpdfsdk') diff --git a/fpdfsdk/cpdfsdk_baannot.cpp b/fpdfsdk/cpdfsdk_baannot.cpp index 836298f50d..7f3b6f4e12 100644 --- a/fpdfsdk/cpdfsdk_baannot.cpp +++ b/fpdfsdk/cpdfsdk_baannot.cpp @@ -162,35 +162,33 @@ int CPDFSDK_BAAnnot::GetStructParent() const { // border void CPDFSDK_BAAnnot::SetBorderWidth(int nWidth) { CPDF_Array* pBorder = m_pAnnot->GetAnnotDict()->GetArrayFor("Border"); - if (pBorder) { pBorder->SetAt(2, new CPDF_Number(nWidth)); } else { CPDF_Dictionary* pBSDict = m_pAnnot->GetAnnotDict()->GetDictFor("BS"); - if (!pBSDict) { - pBSDict = new CPDF_Dictionary; + pBSDict = + new CPDF_Dictionary(m_pAnnot->GetDocument()->GetByteStringPool()); m_pAnnot->GetAnnotDict()->SetFor("BS", pBSDict); } - pBSDict->SetIntegerFor("W", nWidth); } } int CPDFSDK_BAAnnot::GetBorderWidth() const { - if (CPDF_Array* pBorder = m_pAnnot->GetAnnotDict()->GetArrayFor("Border")) { + if (CPDF_Array* pBorder = m_pAnnot->GetAnnotDict()->GetArrayFor("Border")) return pBorder->GetIntegerAt(2); - } - if (CPDF_Dictionary* pBSDict = m_pAnnot->GetAnnotDict()->GetDictFor("BS")) { + + if (CPDF_Dictionary* pBSDict = m_pAnnot->GetAnnotDict()->GetDictFor("BS")) return pBSDict->GetIntegerFor("W", 1); - } + return 1; } void CPDFSDK_BAAnnot::SetBorderStyle(BorderStyle nStyle) { CPDF_Dictionary* pBSDict = m_pAnnot->GetAnnotDict()->GetDictFor("BS"); if (!pBSDict) { - pBSDict = new CPDF_Dictionary; + pBSDict = new CPDF_Dictionary(m_pAnnot->GetDocument()->GetByteStringPool()); m_pAnnot->GetAnnotDict()->SetFor("BS", pBSDict); } @@ -297,22 +295,21 @@ void CPDFSDK_BAAnnot::WriteAppearance(const CFX_ByteString& sAPType, const CFX_ByteString& sContents, const CFX_ByteString& sAPState) { CPDF_Dictionary* pAPDict = m_pAnnot->GetAnnotDict()->GetDictFor("AP"); - if (!pAPDict) { - pAPDict = new CPDF_Dictionary; + pAPDict = new CPDF_Dictionary(m_pAnnot->GetDocument()->GetByteStringPool()); m_pAnnot->GetAnnotDict()->SetFor("AP", pAPDict); } CPDF_Stream* pStream = nullptr; CPDF_Dictionary* pParentDict = nullptr; - if (sAPState.IsEmpty()) { pParentDict = pAPDict; pStream = pAPDict->GetStreamFor(sAPType); } else { CPDF_Dictionary* pAPTypeDict = pAPDict->GetDictFor(sAPType); if (!pAPTypeDict) { - pAPTypeDict = new CPDF_Dictionary; + pAPTypeDict = + new CPDF_Dictionary(m_pAnnot->GetDocument()->GetByteStringPool()); pAPDict->SetFor(sAPType, pAPTypeDict); } pParentDict = pAPTypeDict; @@ -328,7 +325,8 @@ void CPDFSDK_BAAnnot::WriteAppearance(const CFX_ByteString& sAPType, CPDF_Dictionary* pStreamDict = pStream->GetDict(); if (!pStreamDict) { - pStreamDict = new CPDF_Dictionary; + pStreamDict = + new CPDF_Dictionary(m_pAnnot->GetDocument()->GetByteStringPool()); pStreamDict->SetNameFor("Type", "XObject"); pStreamDict->SetNameFor("Subtype", "Form"); pStreamDict->SetIntegerFor("FormType", 1); diff --git a/fpdfsdk/cpdfsdk_widget.cpp b/fpdfsdk/cpdfsdk_widget.cpp index cc2a767039..15e81f8824 100644 --- a/fpdfsdk/cpdfsdk_widget.cpp +++ b/fpdfsdk/cpdfsdk_widget.cpp @@ -1815,9 +1815,6 @@ CPWL_Color CPDFSDK_Widget::GetFillPWLColor() const { void CPDFSDK_Widget::AddImageToAppearance(const CFX_ByteString& sAPType, CPDF_Stream* pImage) { - CPDF_Document* pDoc = m_pPageView->GetPDFDocument(); - ASSERT(pDoc); - CPDF_Dictionary* pAPDict = m_pAnnot->GetAnnotDict()->GetDictFor("AP"); CPDF_Stream* pStream = pAPDict->GetStreamFor(sAPType); CPDF_Dictionary* pStreamDict = pStream->GetDict(); @@ -1829,13 +1826,14 @@ void CPDFSDK_Widget::AddImageToAppearance(const CFX_ByteString& sAPType, sImageAlias = "IMG"; } + CPDF_Document* pDoc = m_pPageView->GetPDFDocument(); CPDF_Dictionary* pStreamResList = pStreamDict->GetDictFor("Resources"); if (!pStreamResList) { - pStreamResList = new CPDF_Dictionary(); + pStreamResList = new CPDF_Dictionary(pDoc->GetByteStringPool()); pStreamDict->SetFor("Resources", pStreamResList); } - CPDF_Dictionary* pXObject = new CPDF_Dictionary; + CPDF_Dictionary* pXObject = new CPDF_Dictionary(pDoc->GetByteStringPool()); 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 04d6a72ec5..09c54ca6f3 100644 --- a/fpdfsdk/formfiller/cba_fontmap.cpp +++ b/fpdfsdk/formfiller/cba_fontmap.cpp @@ -155,7 +155,7 @@ void CBA_FontMap::AddFontToAnnotDict(CPDF_Font* pFont, CPDF_Dictionary* pAPDict = m_pAnnotDict->GetDictFor("AP"); if (!pAPDict) { - pAPDict = new CPDF_Dictionary; + pAPDict = new CPDF_Dictionary(m_pDocument->GetByteStringPool()); m_pAnnotDict->SetFor("AP", pAPDict); } @@ -173,19 +173,20 @@ void CBA_FontMap::AddFontToAnnotDict(CPDF_Font* pFont, CPDF_Dictionary* pStreamDict = pStream->GetDict(); if (!pStreamDict) { - pStreamDict = new CPDF_Dictionary; + pStreamDict = new CPDF_Dictionary(m_pDocument->GetByteStringPool()); pStream->InitStream(nullptr, 0, pStreamDict); } if (pStreamDict) { CPDF_Dictionary* pStreamResList = pStreamDict->GetDictFor("Resources"); if (!pStreamResList) { - pStreamResList = new CPDF_Dictionary(); + pStreamResList = new CPDF_Dictionary(m_pDocument->GetByteStringPool()); pStreamDict->SetFor("Resources", pStreamResList); } CPDF_Dictionary* pStreamResFontList = pStreamResList->GetDictFor("Font"); if (!pStreamResFontList) { - pStreamResFontList = new CPDF_Dictionary; + pStreamResFontList = + new CPDF_Dictionary(m_pDocument->GetByteStringPool()); pStreamResList->SetReferenceFor( "Font", m_pDocument, m_pDocument->AddIndirectObject(pStreamResFontList)); diff --git a/fpdfsdk/fpdf_flatten.cpp b/fpdfsdk/fpdf_flatten.cpp index 5ee485855b..4907173f22 100644 --- a/fpdfsdk/fpdf_flatten.cpp +++ b/fpdfsdk/fpdf_flatten.cpp @@ -193,8 +193,8 @@ void SetPageContents(CFX_ByteString key, if (!pContentsObj) { // Create a new contents dictionary if (!key.IsEmpty()) { - CPDF_Stream* pNewContents = - new CPDF_Stream(nullptr, 0, new CPDF_Dictionary); + 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()); @@ -205,7 +205,6 @@ void SetPageContents(CFX_ByteString key, } CPDF_Array* pContentsArray = nullptr; - switch (pContentsObj->GetType()) { case CPDF_Object::STREAM: { pContentsArray = new CPDF_Array; @@ -237,8 +236,8 @@ void SetPageContents(CFX_ByteString key, pDocument->AddIndirectObject(pContentsArray)); if (!key.IsEmpty()) { - CPDF_Stream* pNewContents = - new CPDF_Stream(nullptr, 0, new CPDF_Dictionary); + 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()); @@ -370,15 +369,17 @@ DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { CPDF_Dictionary* pRes = pPageDict->GetDictFor("Resources"); if (!pRes) { - pRes = new CPDF_Dictionary; + pRes = new CPDF_Dictionary(pDocument->GetByteStringPool()); pPageDict->SetFor("Resources", pRes); } - CPDF_Stream* pNewXObject = new CPDF_Stream(nullptr, 0, new CPDF_Dictionary); + CPDF_Stream* pNewXObject = new CPDF_Stream( + nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool())); + uint32_t dwObjNum = pDocument->AddIndirectObject(pNewXObject); CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject"); if (!pPageXObject) { - pPageXObject = new CPDF_Dictionary; + pPageXObject = new CPDF_Dictionary(pDocument->GetByteStringPool()); pRes->SetFor("XObject", pPageXObject); } @@ -402,7 +403,7 @@ DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { if (!key.IsEmpty()) { pPageXObject->SetReferenceFor(key, pDocument, dwObjNum); CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict(); - pNewXORes = new CPDF_Dictionary; + pNewXORes = new CPDF_Dictionary(pDocument->GetByteStringPool()); pNewOXbjectDic->SetFor("Resources", pNewXORes); pNewOXbjectDic->SetNameFor("Type", "XObject"); pNewOXbjectDic->SetNameFor("Subtype", "Form"); @@ -474,7 +475,7 @@ DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { CPDF_Dictionary* pXObject = pNewXORes->GetDictFor("XObject"); if (!pXObject) { - pXObject = new CPDF_Dictionary; + pXObject = new CPDF_Dictionary(pDocument->GetByteStringPool()); pNewXORes->SetFor("XObject", pXObject); } diff --git a/fpdfsdk/fpdf_transformpage.cpp b/fpdfsdk/fpdf_transformpage.cpp index 68007d3ca5..8841c96f23 100644 --- a/fpdfsdk/fpdf_transformpage.cpp +++ b/fpdfsdk/fpdf_transformpage.cpp @@ -125,15 +125,16 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page, if (!pContentObj) return FALSE; - CPDF_Dictionary* pDic = new CPDF_Dictionary; - CPDF_Stream* pStream = new CPDF_Stream(nullptr, 0, pDic); - pStream->SetData(textBuf.GetBuffer(), textBuf.GetSize()); CPDF_Document* pDoc = pPage->m_pDocument; if (!pDoc) return FALSE; + + 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()); - pDic = new CPDF_Dictionary; CPDF_Stream* pEndStream = new CPDF_Stream(nullptr, 0, pDic); pEndStream->SetData((const uint8_t*)" Q", 2); pDoc->AddIndirectObject(pEndStream); @@ -300,13 +301,13 @@ DLLEXPORT void STDCALL FPDFPage_InsertClipPath(FPDF_PAGE page, strClip << "W* n\n"; } } - CPDF_Dictionary* pDic = new CPDF_Dictionary; - CPDF_Stream* pStream = new CPDF_Stream(nullptr, 0, pDic); - pStream->SetData(strClip.GetBuffer(), strClip.GetSize()); CPDF_Document* pDoc = pPage->m_pDocument; if (!pDoc) return; + 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; diff --git a/fpdfsdk/fpdfdoc_unittest.cpp b/fpdfsdk/fpdfdoc_unittest.cpp index 59f9e31e25..7b185cc48f 100644 --- a/fpdfsdk/fpdfdoc_unittest.cpp +++ b/fpdfsdk/fpdfdoc_unittest.cpp @@ -64,7 +64,7 @@ class PDFDocTest : public testing::Test { m_pDoc.reset(new CPDF_TestPdfDocument()); m_pIndirectObjs = m_pDoc->GetHolder(); // Setup the root directory. - m_pRootObj.reset(new CPDF_Dictionary()); + m_pRootObj.reset(new CPDF_Dictionary(CFX_WeakPtr())); m_pDoc->SetRoot(m_pRootObj.get()); } @@ -79,7 +79,8 @@ class PDFDocTest : public testing::Test { std::vector info; for (int i = 0; i < num; ++i) { // Objects created will be released by the document. - CPDF_Dictionary* obj = new CPDF_Dictionary; + CPDF_Dictionary* obj( + new CPDF_Dictionary(CFX_WeakPtr())); info.push_back({m_pIndirectObjs->AddIndirectObject(obj), obj}); } return info; @@ -103,7 +104,8 @@ TEST_F(PDFDocTest, FindBookmark) { } { // Empty bookmark tree. - m_pRootObj->SetFor("Outlines", new CPDF_Dictionary()); + m_pRootObj->SetFor("Outlines", + new CPDF_Dictionary(CFX_WeakPtr())); std::unique_ptr title = GetFPDFWideString(L""); EXPECT_EQ(nullptr, FPDFBookmark_Find(m_pDoc.get(), title.get())); diff --git a/fpdfsdk/fpdfeditpage.cpp b/fpdfsdk/fpdfeditpage.cpp index e3777d4f84..9ab7f0718e 100644 --- a/fpdfsdk/fpdfeditpage.cpp +++ b/fpdfsdk/fpdfeditpage.cpp @@ -115,10 +115,10 @@ DLLEXPORT FPDF_PAGE STDCALL FPDFPage_New(FPDF_DOCUMENT document, pMediaBoxArray->Add(new CPDF_Number(0)); pMediaBoxArray->Add(new CPDF_Number(FX_FLOAT(width))); pMediaBoxArray->Add(new CPDF_Number(FX_FLOAT(height))); - pPageDict->SetFor("MediaBox", pMediaBoxArray); pPageDict->SetFor("Rotate", new CPDF_Number(0)); - pPageDict->SetFor("Resources", new CPDF_Dictionary); + pPageDict->SetFor("Resources", + new CPDF_Dictionary(pDoc->GetByteStringPool())); #ifdef PDF_ENABLE_XFA CPDFXFA_Page* pPage = diff --git a/fpdfsdk/fpdfppo.cpp b/fpdfsdk/fpdfppo.cpp index d901a61143..0a3e9714ce 100644 --- a/fpdfsdk/fpdfppo.cpp +++ b/fpdfsdk/fpdfppo.cpp @@ -70,7 +70,7 @@ FX_BOOL CPDF_PageOrganizer::PDFDocInit(CPDF_Document* pDestPDFDoc, CPDF_Dictionary* pNewPages = pElement ? ToDictionary(pElement->GetDirect()) : nullptr; if (!pNewPages) { - pNewPages = new CPDF_Dictionary; + pNewPages = new CPDF_Dictionary(pDestPDFDoc->GetByteStringPool()); pNewRoot->SetReferenceFor("Pages", pDestPDFDoc, pDestPDFDoc->AddIndirectObject(pNewPages)); } diff --git a/fpdfsdk/fpdfsave.cpp b/fpdfsdk/fpdfsave.cpp index 8293c371dc..aa03e6d13c 100644 --- a/fpdfsdk/fpdfsave.cpp +++ b/fpdfsdk/fpdfsave.cpp @@ -178,7 +178,8 @@ bool SaveXFADocumentData(CPDFXFA_Document* pDocument, // Datasets pContext->UpdateChecksum(pDsfileWrite.get()); pContext->FinishChecksum(); - CPDF_Dictionary* pDataDict = new CPDF_Dictionary; + CPDF_Dictionary* pDataDict = + new CPDF_Dictionary(pPDFDocument->GetByteStringPool()); if (iDataSetsIndex != -1) { if (pDataSetsStream) pDataSetsStream->InitStreamFromFile(pDsfileWrite.get(), pDataDict); @@ -201,7 +202,8 @@ bool SaveXFADocumentData(CPDFXFA_Document* pDocument, if (pXFADocView->GetDoc()->SavePackage(XFA_HASHCODE_Form, pfileWrite.get(), pContext.get()) && pfileWrite->GetSize() > 0) { - CPDF_Dictionary* pDataDict = new CPDF_Dictionary; + CPDF_Dictionary* pDataDict = + new CPDF_Dictionary(pPDFDocument->GetByteStringPool()); if (iFormIndex != -1) { if (pFormStream) pFormStream->InitStreamFromFile(pfileWrite.get(), pDataDict); -- cgit v1.2.3