From 9e05ee1e7bfb74d56d69620ad1e72b03e29b9237 Mon Sep 17 00:00:00 2001 From: tsepez Date: Mon, 21 Nov 2016 13:19:10 -0800 Subject: Make CPDF_Stream() take unique_ptr's to its dictionary. Review-Url: https://codereview.chromium.org/2520493002 --- fpdfsdk/cpdfsdk_baannot.cpp | 16 +++++++--------- fpdfsdk/formfiller/cba_fontmap.cpp | 34 ++++++++++++++++++---------------- fpdfsdk/fpdf_flatten.cpp | 6 ++++-- fpdfsdk/fpdf_transformpage.cpp | 16 +++++++++------- fpdfsdk/fpdfsave.cpp | 24 ++++++++++++++---------- 5 files changed, 52 insertions(+), 44 deletions(-) (limited to 'fpdfsdk') diff --git a/fpdfsdk/cpdfsdk_baannot.cpp b/fpdfsdk/cpdfsdk_baannot.cpp index 9fb1e88a84..3eedf1502c 100644 --- a/fpdfsdk/cpdfsdk_baannot.cpp +++ b/fpdfsdk/cpdfsdk_baannot.cpp @@ -7,6 +7,7 @@ #include "fpdfsdk/cpdfsdk_baannot.h" #include +#include #include "core/fpdfapi/parser/cpdf_array.h" #include "core/fpdfapi/parser/cpdf_document.h" @@ -323,19 +324,16 @@ void CPDFSDK_BAAnnot::WriteAppearance(const CFX_ByteString& sAPType, CPDF_Dictionary* pStreamDict = pStream->GetDict(); if (!pStreamDict) { - pStreamDict = - new CPDF_Dictionary(m_pAnnot->GetDocument()->GetByteStringPool()); + auto pNewDict = pdfium::MakeUnique( + m_pAnnot->GetDocument()->GetByteStringPool()); + pStreamDict = pNewDict.get(); pStreamDict->SetNewFor("Type", "XObject"); pStreamDict->SetNewFor("Subtype", "Form"); pStreamDict->SetNewFor("FormType", 1); - pStream->InitStream(nullptr, 0, pStreamDict); + pStream->InitStream(nullptr, 0, std::move(pNewDict)); } - - if (pStreamDict) { - pStreamDict->SetMatrixFor("Matrix", matrix); - pStreamDict->SetRectFor("BBox", rcBBox); - } - + pStreamDict->SetMatrixFor("Matrix", matrix); + pStreamDict->SetRectFor("BBox", rcBBox); pStream->SetData((uint8_t*)sContents.c_str(), sContents.GetLength()); } diff --git a/fpdfsdk/formfiller/cba_fontmap.cpp b/fpdfsdk/formfiller/cba_fontmap.cpp index 83e45791e2..750b41627e 100644 --- a/fpdfsdk/formfiller/cba_fontmap.cpp +++ b/fpdfsdk/formfiller/cba_fontmap.cpp @@ -6,6 +6,8 @@ #include "fpdfsdk/formfiller/cba_fontmap.h" +#include + #include "core/fpdfapi/font/cpdf_font.h" #include "core/fpdfapi/page/cpdf_page.h" #include "core/fpdfapi/parser/cpdf_document.h" @@ -171,24 +173,24 @@ void CBA_FontMap::AddFontToAnnotDict(CPDF_Font* pFont, CPDF_Dictionary* pStreamDict = pStream->GetDict(); if (!pStreamDict) { - pStreamDict = new CPDF_Dictionary(m_pDocument->GetByteStringPool()); - pStream->InitStream(nullptr, 0, pStreamDict); + auto pOwnedDict = + pdfium::MakeUnique(m_pDocument->GetByteStringPool()); + pStreamDict = pOwnedDict.get(); + pStream->InitStream(nullptr, 0, std::move(pOwnedDict)); } - if (pStreamDict) { - CPDF_Dictionary* pStreamResList = pStreamDict->GetDictFor("Resources"); - if (!pStreamResList) - pStreamResList = pStreamDict->SetNewFor("Resources"); - CPDF_Dictionary* pStreamResFontList = pStreamResList->GetDictFor("Font"); - if (!pStreamResFontList) { - pStreamResFontList = m_pDocument->NewIndirect(); - pStreamResList->SetNewFor( - "Font", m_pDocument, pStreamResFontList->GetObjNum()); - } - if (!pStreamResFontList->KeyExist(sAlias)) { - pStreamResFontList->SetNewFor( - sAlias, m_pDocument, pFont->GetFontDict()->GetObjNum()); - } + CPDF_Dictionary* pStreamResList = pStreamDict->GetDictFor("Resources"); + if (!pStreamResList) + pStreamResList = pStreamDict->SetNewFor("Resources"); + CPDF_Dictionary* pStreamResFontList = pStreamResList->GetDictFor("Font"); + if (!pStreamResFontList) { + pStreamResFontList = m_pDocument->NewIndirect(); + pStreamResList->SetNewFor("Font", m_pDocument, + pStreamResFontList->GetObjNum()); + } + if (!pStreamResFontList->KeyExist(sAlias)) { + pStreamResFontList->SetNewFor( + sAlias, m_pDocument, pFont->GetFontDict()->GetObjNum()); } } diff --git a/fpdfsdk/fpdf_flatten.cpp b/fpdfsdk/fpdf_flatten.cpp index ccbb7b8765..e649bacf49 100644 --- a/fpdfsdk/fpdf_flatten.cpp +++ b/fpdfsdk/fpdf_flatten.cpp @@ -178,7 +178,8 @@ CFX_FloatRect CalculateRect(std::vector* pRectArray) { uint32_t NewIndirectContentsStream(const CFX_ByteString& key, CPDF_Document* pDocument) { CPDF_Stream* pNewContents = pDocument->NewIndirect( - nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool())); + nullptr, 0, + pdfium::MakeUnique(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()); @@ -296,7 +297,8 @@ DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { pRes = pPageDict->SetNewFor("Resources"); CPDF_Stream* pNewXObject = pDocument->NewIndirect( - nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool())); + nullptr, 0, + pdfium::MakeUnique(pDocument->GetByteStringPool())); uint32_t dwObjNum = pNewXObject->GetObjNum(); CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject"); diff --git a/fpdfsdk/fpdf_transformpage.cpp b/fpdfsdk/fpdf_transformpage.cpp index b1e8da06b9..13d9756bd8 100644 --- a/fpdfsdk/fpdf_transformpage.cpp +++ b/fpdfsdk/fpdf_transformpage.cpp @@ -128,13 +128,14 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page, if (!pDoc) return false; - CPDF_Dictionary* pDic = new CPDF_Dictionary(pDoc->GetByteStringPool()); - CPDF_Stream* pStream = pDoc->NewIndirect(nullptr, 0, pDic); + CPDF_Stream* pStream = pDoc->NewIndirect( + nullptr, 0, + pdfium::MakeUnique(pDoc->GetByteStringPool())); pStream->SetData(textBuf.GetBuffer(), textBuf.GetSize()); - pDic = new CPDF_Dictionary(pDoc->GetByteStringPool()); - - CPDF_Stream* pEndStream = pDoc->NewIndirect(nullptr, 0, pDic); + CPDF_Stream* pEndStream = pDoc->NewIndirect( + nullptr, 0, + pdfium::MakeUnique(pDoc->GetByteStringPool())); pEndStream->SetData((const uint8_t*)" Q", 2); CPDF_Array* pContentArray = nullptr; @@ -302,8 +303,9 @@ DLLEXPORT void STDCALL FPDFPage_InsertClipPath(FPDF_PAGE page, if (!pDoc) return; - CPDF_Dictionary* pDic = new CPDF_Dictionary(pDoc->GetByteStringPool()); - CPDF_Stream* pStream = pDoc->NewIndirect(nullptr, 0, pDic); + CPDF_Stream* pStream = pDoc->NewIndirect( + nullptr, 0, + pdfium::MakeUnique(pDoc->GetByteStringPool())); pStream->SetData(strClip.GetBuffer(), strClip.GetSize()); CPDF_Array* pArray = ToArray(pContentObj); diff --git a/fpdfsdk/fpdfsave.cpp b/fpdfsdk/fpdfsave.cpp index 89b8b5839e..cb01ab697b 100644 --- a/fpdfsdk/fpdfsave.cpp +++ b/fpdfsdk/fpdfsave.cpp @@ -176,14 +176,16 @@ bool SaveXFADocumentData(CPDFXFA_Context* pContext, // Datasets pChecksum->UpdateChecksum(pDsfileWrite.get()); pChecksum->FinishChecksum(); - CPDF_Dictionary* pDataDict = - new CPDF_Dictionary(pPDFDocument->GetByteStringPool()); + auto pDataDict = pdfium::MakeUnique( + pPDFDocument->GetByteStringPool()); if (iDataSetsIndex != -1) { - if (pDataSetsStream) - pDataSetsStream->InitStreamFromFile(pDsfileWrite.get(), pDataDict); + if (pDataSetsStream) { + pDataSetsStream->InitStreamFromFile(pDsfileWrite.get(), + std::move(pDataDict)); + } } else { CPDF_Stream* pData = pPDFDocument->NewIndirect(); - pData->InitStreamFromFile(pDsfileWrite.get(), pDataDict); + pData->InitStreamFromFile(pDsfileWrite.get(), std::move(pDataDict)); iLast = pArray->GetCount() - 2; pArray->InsertNewAt(iLast, "datasets", false); pArray->InsertNewAt(iLast + 1, pPDFDocument, @@ -198,14 +200,16 @@ bool SaveXFADocumentData(CPDFXFA_Context* pContext, if (pXFADocView->GetDoc()->SavePackage(XFA_HASHCODE_Form, pfileWrite.get(), pChecksum.get()) && pfileWrite->GetSize() > 0) { - CPDF_Dictionary* pDataDict = - new CPDF_Dictionary(pPDFDocument->GetByteStringPool()); + auto pDataDict = pdfium::MakeUnique( + pPDFDocument->GetByteStringPool()); if (iFormIndex != -1) { - if (pFormStream) - pFormStream->InitStreamFromFile(pfileWrite.get(), pDataDict); + if (pFormStream) { + pFormStream->InitStreamFromFile(pfileWrite.get(), + std::move(pDataDict)); + } } else { CPDF_Stream* pData = pPDFDocument->NewIndirect(); - pData->InitStreamFromFile(pfileWrite.get(), pDataDict); + pData->InitStreamFromFile(pfileWrite.get(), std::move(pDataDict)); iLast = pArray->GetCount() - 2; pArray->InsertNewAt(iLast, "form", false); pArray->InsertNewAt(iLast + 1, pPDFDocument, -- cgit v1.2.3