From b1ec280837cc6e1932754ef40de26d12b77aa910 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Fri, 25 May 2018 21:55:24 +0000 Subject: Add proper const/non-const versions of CPDF_Dictionary::GetDictFor(). BUG=pdfium:234 Change-Id: I6fde00c976ad4bb9cab632f465cf292f5b1da3d2 Reviewed-on: https://pdfium-review.googlesource.com/32914 Commit-Queue: Lei Zhang Reviewed-by: dsinclair --- core/fpdfapi/font/cpdf_font.cpp | 3 ++- core/fpdfapi/page/cpdf_docpagedata.cpp | 4 +-- core/fpdfapi/parser/cpdf_dictionary.cpp | 14 +++++++++- core/fpdfapi/parser/cpdf_dictionary.h | 3 ++- core/fpdfapi/parser/cpdf_document.cpp | 21 +++++++++------ core/fpdfapi/parser/cpdf_document.h | 7 ++--- core/fpdfapi/parser/cpdf_security_handler.cpp | 12 ++++----- fpdfsdk/formfiller/cba_fontmap.cpp | 10 ++++---- fpdfsdk/formfiller/cba_fontmap.h | 2 +- fpdfsdk/fpdf_edit_embeddertest.cpp | 12 +++++---- fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp | 37 ++++++++++++++------------- 11 files changed, 74 insertions(+), 51 deletions(-) diff --git a/core/fpdfapi/font/cpdf_font.cpp b/core/fpdfapi/font/cpdf_font.cpp index cc867d69d9..0bba1057e7 100644 --- a/core/fpdfapi/font/cpdf_font.cpp +++ b/core/fpdfapi/font/cpdf_font.cpp @@ -324,7 +324,8 @@ std::unique_ptr CPDF_Font::Create(CPDF_Document* pDoc, ByteString tag = pFontDict->GetStringFor("BaseFont").Left(4); for (size_t i = 0; i < FX_ArraySize(kChineseFontNames); ++i) { if (tag == ByteString(kChineseFontNames[i], 4)) { - CPDF_Dictionary* pFontDesc = pFontDict->GetDictFor("FontDescriptor"); + const CPDF_Dictionary* pFontDesc = + pFontDict->GetDictFor("FontDescriptor"); if (!pFontDesc || !pFontDesc->KeyExist("FontFile2")) pFont = pdfium::MakeUnique(); break; diff --git a/core/fpdfapi/page/cpdf_docpagedata.cpp b/core/fpdfapi/page/cpdf_docpagedata.cpp index 92dca138cf..37283b9d64 100644 --- a/core/fpdfapi/page/cpdf_docpagedata.cpp +++ b/core/fpdfapi/page/cpdf_docpagedata.cpp @@ -241,7 +241,7 @@ CPDF_ColorSpace* CPDF_DocPageData::GetColorSpaceInternal( ByteString name = pCSObj->GetString(); CPDF_ColorSpace* pCS = CPDF_ColorSpace::ColorspaceFromName(name); if (!pCS && pResources) { - CPDF_Dictionary* pList = pResources->GetDictFor("ColorSpace"); + const CPDF_Dictionary* pList = pResources->GetDictFor("ColorSpace"); if (pList) { return GetColorSpaceInternal(pList->GetDirectObjectFor(name), nullptr, pVisited, pVisitedInternal); @@ -250,7 +250,7 @@ CPDF_ColorSpace* CPDF_DocPageData::GetColorSpaceInternal( if (!pCS || !pResources) return pCS; - CPDF_Dictionary* pColorSpaces = pResources->GetDictFor("ColorSpace"); + const CPDF_Dictionary* pColorSpaces = pResources->GetDictFor("ColorSpace"); if (!pColorSpaces) return pCS; diff --git a/core/fpdfapi/parser/cpdf_dictionary.cpp b/core/fpdfapi/parser/cpdf_dictionary.cpp index 1ede5e707a..5257a20586 100644 --- a/core/fpdfapi/parser/cpdf_dictionary.cpp +++ b/core/fpdfapi/parser/cpdf_dictionary.cpp @@ -130,7 +130,19 @@ bool CPDF_Dictionary::GetBooleanFor(const ByteString& key, return ToBoolean(p) ? p->GetInteger() != 0 : bDefault; } -CPDF_Dictionary* CPDF_Dictionary::GetDictFor(const ByteString& key) const { +const CPDF_Dictionary* CPDF_Dictionary::GetDictFor( + const ByteString& key) const { + CPDF_Object* p = GetDirectObjectFor(key); + if (!p) + return nullptr; + if (CPDF_Dictionary* pDict = p->AsDictionary()) + return pDict; + if (CPDF_Stream* pStream = p->AsStream()) + return pStream->GetDict(); + return nullptr; +} + +CPDF_Dictionary* CPDF_Dictionary::GetDictFor(const ByteString& key) { CPDF_Object* p = GetDirectObjectFor(key); if (!p) return nullptr; diff --git a/core/fpdfapi/parser/cpdf_dictionary.h b/core/fpdfapi/parser/cpdf_dictionary.h index df789aa7b4..5240ab3b5c 100644 --- a/core/fpdfapi/parser/cpdf_dictionary.h +++ b/core/fpdfapi/parser/cpdf_dictionary.h @@ -51,7 +51,8 @@ class CPDF_Dictionary : public CPDF_Object { int GetIntegerFor(const ByteString& key, int default_int) const; bool GetBooleanFor(const ByteString& key, bool bDefault = false) const; float GetNumberFor(const ByteString& key) const; - CPDF_Dictionary* GetDictFor(const ByteString& key) const; + const CPDF_Dictionary* GetDictFor(const ByteString& key) const; + CPDF_Dictionary* GetDictFor(const ByteString& key); const CPDF_Stream* GetStreamFor(const ByteString& key) const; CPDF_Stream* GetStreamFor(const ByteString& key); const CPDF_Array* GetArrayFor(const ByteString& key) const; diff --git a/core/fpdfapi/parser/cpdf_document.cpp b/core/fpdfapi/parser/cpdf_document.cpp index 5264d4e795..5479a06174 100644 --- a/core/fpdfapi/parser/cpdf_document.cpp +++ b/core/fpdfapi/parser/cpdf_document.cpp @@ -322,11 +322,16 @@ void CPDF_Document::ResetTraversal() { m_pTreeTraversal.clear(); } -CPDF_Dictionary* CPDF_Document::GetPagesDict() const { +const CPDF_Dictionary* CPDF_Document::GetPagesDict() const { const CPDF_Dictionary* pRoot = GetRoot(); return pRoot ? pRoot->GetDictFor("Pages") : nullptr; } +CPDF_Dictionary* CPDF_Document::GetPagesDict() { + CPDF_Dictionary* pRoot = GetRoot(); + return pRoot ? pRoot->GetDictFor("Pages") : nullptr; +} + bool CPDF_Document::IsPageLoaded(int iPage) const { return !!m_PageList[iPage]; } @@ -363,7 +368,7 @@ void CPDF_Document::SetPageObjNum(int iPage, uint32_t objNum) { m_PageList[iPage] = objNum; } -int CPDF_Document::FindPageIndex(CPDF_Dictionary* pNode, +int CPDF_Document::FindPageIndex(const CPDF_Dictionary* pNode, uint32_t* skip_count, uint32_t objnum, int* index, @@ -379,7 +384,7 @@ int CPDF_Document::FindPageIndex(CPDF_Dictionary* pNode, return -1; } - CPDF_Array* pKidList = pNode->GetArrayFor("Kids"); + const CPDF_Array* pKidList = pNode->GetArrayFor("Kids"); if (!pKidList) return -1; @@ -395,14 +400,14 @@ int CPDF_Document::FindPageIndex(CPDF_Dictionary* pNode, if (count && count == pKidList->GetCount()) { for (size_t i = 0; i < count; i++) { - CPDF_Reference* pKid = ToReference(pKidList->GetObjectAt(i)); + const CPDF_Reference* pKid = ToReference(pKidList->GetObjectAt(i)); if (pKid && pKid->GetRefObjNum() == objnum) return static_cast(*index + i); } } for (size_t i = 0; i < pKidList->GetCount(); i++) { - CPDF_Dictionary* pKid = pKidList->GetDictAt(i); + const CPDF_Dictionary* pKid = pKidList->GetDictAt(i); if (!pKid || pKid == pNode) continue; @@ -426,7 +431,7 @@ int CPDF_Document::GetPageIndex(uint32_t objnum) { bSkipped = true; } } - CPDF_Dictionary* pPages = GetPagesDict(); + const CPDF_Dictionary* pPages = GetPagesDict(); if (!pPages) return -1; @@ -445,7 +450,7 @@ int CPDF_Document::GetPageCount() const { return pdfium::CollectionSize(m_PageList); } -int CPDF_Document::RetrievePageCount() const { +int CPDF_Document::RetrievePageCount() { CPDF_Dictionary* pPages = GetPagesDict(); if (!pPages) return 0; @@ -576,7 +581,7 @@ bool CPDF_Document::InsertDeletePDFPage(CPDF_Dictionary* pPages, } bool CPDF_Document::InsertNewPage(int iPage, CPDF_Dictionary* pPageDict) { - const CPDF_Dictionary* pRoot = GetRoot(); + CPDF_Dictionary* pRoot = GetRoot(); CPDF_Dictionary* pPages = pRoot ? pRoot->GetDictFor("Pages") : nullptr; if (!pPages) return false; diff --git a/core/fpdfapi/parser/cpdf_document.h b/core/fpdfapi/parser/cpdf_document.h index b169175668..356d341302 100644 --- a/core/fpdfapi/parser/cpdf_document.h +++ b/core/fpdfapi/parser/cpdf_document.h @@ -115,10 +115,10 @@ class CPDF_Document : public CPDF_IndirectObjectHolder { protected: // Retrieve page count information by getting count value from the tree nodes - int RetrievePageCount() const; + int RetrievePageCount(); // When this method is called, m_pTreeTraversal[level] exists. CPDF_Dictionary* TraversePDFPages(int iPage, int* nPagesToGo, size_t level); - int FindPageIndex(CPDF_Dictionary* pNode, + int FindPageIndex(const CPDF_Dictionary* pNode, uint32_t* skip_count, uint32_t objnum, int* index, @@ -126,7 +126,8 @@ class CPDF_Document : public CPDF_IndirectObjectHolder { std::unique_ptr ParseIndirectObject(uint32_t objnum) override; void LoadDocInternal(); size_t CalculateEncodingDict(int charset, CPDF_Dictionary* pBaseDict); - CPDF_Dictionary* GetPagesDict() const; + const CPDF_Dictionary* GetPagesDict() const; + CPDF_Dictionary* GetPagesDict(); CPDF_Dictionary* ProcessbCJK( CPDF_Dictionary* pBaseDict, int charset, diff --git a/core/fpdfapi/parser/cpdf_security_handler.cpp b/core/fpdfapi/parser/cpdf_security_handler.cpp index 7eb5c42f72..eeba53ef34 100644 --- a/core/fpdfapi/parser/cpdf_security_handler.cpp +++ b/core/fpdfapi/parser/cpdf_security_handler.cpp @@ -130,17 +130,17 @@ static bool LoadCryptInfo(const CPDF_Dictionary* pEncryptDict, cipher = FXCIPHER_RC4; keylen = 0; if (Version >= 4) { - CPDF_Dictionary* pCryptFilters = pEncryptDict->GetDictFor("CF"); - if (!pCryptFilters) { + const CPDF_Dictionary* pCryptFilters = pEncryptDict->GetDictFor("CF"); + if (!pCryptFilters) return false; - } + if (name == "Identity") { cipher = FXCIPHER_NONE; } else { - CPDF_Dictionary* pDefFilter = pCryptFilters->GetDictFor(name); - if (!pDefFilter) { + const CPDF_Dictionary* pDefFilter = pCryptFilters->GetDictFor(name); + if (!pDefFilter) return false; - } + int nKeyBits = 0; if (Version == 4) { nKeyBits = pDefFilter->GetIntegerFor("Length", 0); diff --git a/fpdfsdk/formfiller/cba_fontmap.cpp b/fpdfsdk/formfiller/cba_fontmap.cpp index 6b31de7b37..d219ca40b7 100644 --- a/fpdfsdk/formfiller/cba_fontmap.cpp +++ b/fpdfsdk/formfiller/cba_fontmap.cpp @@ -93,11 +93,11 @@ CPDF_Font* CBA_FontMap::FindFontSameCharset(ByteString* sFontAlias, if (!pRootDict) return nullptr; - CPDF_Dictionary* pAcroFormDict = pRootDict->GetDictFor("AcroForm"); + const CPDF_Dictionary* pAcroFormDict = pRootDict->GetDictFor("AcroForm"); if (!pAcroFormDict) return nullptr; - CPDF_Dictionary* pDRDict = pAcroFormDict->GetDictFor("DR"); + const CPDF_Dictionary* pDRDict = pAcroFormDict->GetDictFor("DR"); if (!pDRDict) return nullptr; @@ -108,13 +108,13 @@ CPDF_Document* CBA_FontMap::GetDocument() { return m_pDocument.Get(); } -CPDF_Font* CBA_FontMap::FindResFontSameCharset(CPDF_Dictionary* pResDict, +CPDF_Font* CBA_FontMap::FindResFontSameCharset(const CPDF_Dictionary* pResDict, ByteString* sFontAlias, int32_t nCharset) { if (!pResDict) return nullptr; - CPDF_Dictionary* pFonts = pResDict->GetDictFor("Font"); + const CPDF_Dictionary* pFonts = pResDict->GetDictFor("Font"); if (!pFonts) return nullptr; @@ -197,7 +197,7 @@ CPDF_Font* CBA_FontMap::GetAnnotDefaultFont(ByteString* sAlias) { CPDF_Dictionary* pAcroFormDict = nullptr; const bool bWidget = (m_pAnnotDict->GetStringFor("Subtype") == "Widget"); if (bWidget) { - const CPDF_Dictionary* pRootDict = m_pDocument->GetRoot(); + CPDF_Dictionary* pRootDict = m_pDocument->GetRoot(); if (pRootDict) pAcroFormDict = pRootDict->GetDictFor("AcroForm"); } diff --git a/fpdfsdk/formfiller/cba_fontmap.h b/fpdfsdk/formfiller/cba_fontmap.h index 45df8c82ad..9d9f2e002a 100644 --- a/fpdfsdk/formfiller/cba_fontmap.h +++ b/fpdfsdk/formfiller/cba_fontmap.h @@ -30,7 +30,7 @@ class CBA_FontMap : public CPWL_FontMap { int32_t nCharset) override; void AddedFont(CPDF_Font* pFont, const ByteString& sFontAlias) override; - CPDF_Font* FindResFontSameCharset(CPDF_Dictionary* pResDict, + CPDF_Font* FindResFontSameCharset(const CPDF_Dictionary* pResDict, ByteString* sFontAlias, int32_t nCharset); CPDF_Font* GetAnnotDefaultFont(ByteString* csNameTag); diff --git a/fpdfsdk/fpdf_edit_embeddertest.cpp b/fpdfsdk/fpdf_edit_embeddertest.cpp index ebf356af45..458ae0022a 100644 --- a/fpdfsdk/fpdf_edit_embeddertest.cpp +++ b/fpdfsdk/fpdf_edit_embeddertest.cpp @@ -38,7 +38,7 @@ class FPDFEditEmbeddertest : public EmbedderTest { bool italic, uint32_t size, const uint8_t* data) { - CPDF_Dictionary* font_desc = font_dict->GetDictFor("FontDescriptor"); + const CPDF_Dictionary* font_desc = font_dict->GetDictFor("FontDescriptor"); ASSERT_TRUE(font_desc); EXPECT_EQ("FontDescriptor", font_desc->GetStringFor("Type")); EXPECT_EQ(font_dict->GetStringFor("BaseFont"), @@ -54,7 +54,7 @@ class FPDFEditEmbeddertest : public EmbedderTest { EXPECT_TRUE(FontStyleIsNonSymbolic(font_flags)); ASSERT_TRUE(font_desc->KeyExist("FontBBox")); - CPDF_Array* fontBBox = font_desc->GetArrayFor("FontBBox"); + const CPDF_Array* fontBBox = font_desc->GetArrayFor("FontBBox"); ASSERT_TRUE(fontBBox); EXPECT_EQ(4U, fontBBox->GetCount()); // Check that the coordinates are in the preferred order according to spec @@ -75,7 +75,7 @@ class FPDFEditEmbeddertest : public EmbedderTest { EXPECT_FALSE(font_desc->KeyExist(absent)); // Check that the font stream is the one that was provided - CPDF_Stream* font_stream = font_desc->GetStreamFor(present); + const CPDF_Stream* font_stream = font_desc->GetStreamFor(present); ASSERT_EQ(size, font_stream->GetRawSize()); if (font_type == FPDF_FONT_TRUETYPE) { ASSERT_EQ(static_cast(size), @@ -1078,7 +1078,8 @@ TEST_F(FPDFEditEmbeddertest, LoadCIDType0Font) { EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type")); EXPECT_EQ("CIDFontType0", cidfont_dict->GetStringFor("Subtype")); EXPECT_EQ("Times New Roman", cidfont_dict->GetStringFor("BaseFont")); - CPDF_Dictionary* cidinfo_dict = cidfont_dict->GetDictFor("CIDSystemInfo"); + const CPDF_Dictionary* cidinfo_dict = + cidfont_dict->GetDictFor("CIDSystemInfo"); ASSERT_TRUE(cidinfo_dict); EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry")); EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering")); @@ -1121,7 +1122,8 @@ TEST_F(FPDFEditEmbeddertest, LoadCIDType2Font) { EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type")); EXPECT_EQ("CIDFontType2", cidfont_dict->GetStringFor("Subtype")); EXPECT_EQ("Arial Italic", cidfont_dict->GetStringFor("BaseFont")); - CPDF_Dictionary* cidinfo_dict = cidfont_dict->GetDictFor("CIDSystemInfo"); + const CPDF_Dictionary* cidinfo_dict = + cidfont_dict->GetDictFor("CIDSystemInfo"); ASSERT_TRUE(cidinfo_dict); EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry")); EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering")); diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp b/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp index cc7a8898e8..f9f5981efb 100644 --- a/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp +++ b/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp @@ -462,24 +462,24 @@ void CPDFXFA_DocEnvironment::ExportData(CXFA_FFDoc* hDoc, if (!pRoot) return; - CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm"); + const CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm"); if (!pAcroForm) return; - CPDF_Array* pArray = ToArray(pAcroForm->GetObjectFor("XFA")); + const CPDF_Array* pArray = ToArray(pAcroForm->GetObjectFor("XFA")); if (!pArray) return; int size = pArray->GetCount(); for (int i = 1; i < size; i += 2) { - CPDF_Object* pPDFObj = pArray->GetObjectAt(i); - CPDF_Object* pPrePDFObj = pArray->GetObjectAt(i - 1); + const CPDF_Object* pPDFObj = pArray->GetObjectAt(i); + const CPDF_Object* pPrePDFObj = pArray->GetObjectAt(i - 1); if (!pPrePDFObj->IsString()) continue; if (!pPDFObj->IsReference()) continue; - CPDF_Stream* pStream = ToStream(pPDFObj->GetDirect()); + const CPDF_Stream* pStream = ToStream(pPDFObj->GetDirect()); if (!pStream) continue; if (pPrePDFObj->GetString() == "form") { @@ -761,13 +761,13 @@ bool CPDFXFA_DocEnvironment::ExportSubmitFile(FPDF_FILEHANDLER* pFileHandler, return false; } - CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm"); + const CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm"); if (!pAcroForm) { fileStream->Flush(); return false; } - CPDF_Array* pArray = ToArray(pAcroForm->GetObjectFor("XFA")); + const CPDF_Array* pArray = ToArray(pAcroForm->GetObjectFor("XFA")); if (!pArray) { fileStream->Flush(); return false; @@ -775,32 +775,33 @@ bool CPDFXFA_DocEnvironment::ExportSubmitFile(FPDF_FILEHANDLER* pFileHandler, int size = pArray->GetCount(); for (int i = 1; i < size; i += 2) { - CPDF_Object* pPDFObj = pArray->GetObjectAt(i); - CPDF_Object* pPrePDFObj = pArray->GetObjectAt(i - 1); + const CPDF_Object* pPDFObj = pArray->GetObjectAt(i); + const CPDF_Object* pPrePDFObj = pArray->GetObjectAt(i - 1); if (!pPrePDFObj->IsString()) continue; if (!pPDFObj->IsReference()) continue; - CPDF_Object* pDirectObj = pPDFObj->GetDirect(); + const CPDF_Object* pDirectObj = pPDFObj->GetDirect(); if (!pDirectObj->IsStream()) continue; - if (pPrePDFObj->GetString() == "config" && !(flag & FXFA_CONFIG)) + ByteString bsType = pPrePDFObj->GetString(); + if (bsType == "config" && !(flag & FXFA_CONFIG)) continue; - if (pPrePDFObj->GetString() == "template" && !(flag & FXFA_TEMPLATE)) + if (bsType == "template" && !(flag & FXFA_TEMPLATE)) continue; - if (pPrePDFObj->GetString() == "localeSet" && !(flag & FXFA_LOCALESET)) + if (bsType == "localeSet" && !(flag & FXFA_LOCALESET)) continue; - if (pPrePDFObj->GetString() == "datasets" && !(flag & FXFA_DATASETS)) + if (bsType == "datasets" && !(flag & FXFA_DATASETS)) continue; - if (pPrePDFObj->GetString() == "xmpmeta" && !(flag & FXFA_XMPMETA)) + if (bsType == "xmpmeta" && !(flag & FXFA_XMPMETA)) continue; - if (pPrePDFObj->GetString() == "xfdf" && !(flag & FXFA_XFDF)) + if (bsType == "xfdf" && !(flag & FXFA_XFDF)) continue; - if (pPrePDFObj->GetString() == "form" && !(flag & FXFA_FORM)) + if (bsType == "form" && !(flag & FXFA_FORM)) continue; - if (pPrePDFObj->GetString() == "form") { + if (bsType == "form") { ffdoc->SavePackage( ToNode(ffdoc->GetXFADoc()->GetXFAObject(XFA_HASHCODE_Form)), fileStream); -- cgit v1.2.3