From 7e28208d26764438bef62e051d2e1fed13e1e0ec Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Wed, 9 May 2018 22:02:32 +0000 Subject: Add proper const/non-const versions of CPDF_Object methods. Instead of having const methods that return non-const pointers. BUG=pdfium:234 Change-Id: I598e9b4f267f702e2e71001cfe3aa3c9e9d8c12f Reviewed-on: https://pdfium-review.googlesource.com/32182 Commit-Queue: Lei Zhang Reviewed-by: Henrique Nakashima --- core/fpdfapi/page/cpdf_colorspace.cpp | 2 +- core/fpdfapi/page/cpdf_stitchfunc.cpp | 2 +- core/fpdfapi/parser/cpdf_array.cpp | 4 ++-- core/fpdfapi/parser/cpdf_array.h | 4 ++-- core/fpdfapi/parser/cpdf_dictionary.cpp | 10 ++++++---- core/fpdfapi/parser/cpdf_dictionary.h | 3 ++- core/fpdfapi/parser/cpdf_object.cpp | 14 +++++++++++--- core/fpdfapi/parser/cpdf_object.h | 6 ++++-- core/fpdfapi/parser/cpdf_reference.cpp | 27 +++++++++++++++++++++------ core/fpdfapi/parser/cpdf_reference.h | 9 ++++++--- core/fpdfapi/parser/cpdf_stream.cpp | 13 +++++++++---- core/fpdfapi/parser/cpdf_stream.h | 4 ++-- core/fpdfapi/parser/cpdf_stream_acc.cpp | 2 +- core/fpdfapi/parser/cpdf_stream_acc.h | 2 +- 14 files changed, 69 insertions(+), 33 deletions(-) (limited to 'core') diff --git a/core/fpdfapi/page/cpdf_colorspace.cpp b/core/fpdfapi/page/cpdf_colorspace.cpp index aa664d4e0f..4f68329862 100644 --- a/core/fpdfapi/page/cpdf_colorspace.cpp +++ b/core/fpdfapi/page/cpdf_colorspace.cpp @@ -458,7 +458,7 @@ std::unique_ptr CPDF_ColorSpace::Load( return pdfium::WrapUnique(ColorspaceFromName(pObj->GetString())); if (const CPDF_Stream* pStream = pObj->AsStream()) { - CPDF_Dictionary* pDict = pStream->GetDict(); + const CPDF_Dictionary* pDict = pStream->GetDict(); if (!pDict) return nullptr; diff --git a/core/fpdfapi/page/cpdf_stitchfunc.cpp b/core/fpdfapi/page/cpdf_stitchfunc.cpp index 2083a91623..042e090062 100644 --- a/core/fpdfapi/page/cpdf_stitchfunc.cpp +++ b/core/fpdfapi/page/cpdf_stitchfunc.cpp @@ -27,7 +27,7 @@ bool CPDF_StitchFunc::v_Init(const CPDF_Object* pObj, if (m_nInputs != kRequiredNumInputs) return false; - CPDF_Dictionary* pDict = pObj->GetDict(); + const CPDF_Dictionary* pDict = pObj->GetDict(); if (!pDict) return false; diff --git a/core/fpdfapi/parser/cpdf_array.cpp b/core/fpdfapi/parser/cpdf_array.cpp index 5a5cdc9be0..9293101b32 100644 --- a/core/fpdfapi/parser/cpdf_array.cpp +++ b/core/fpdfapi/parser/cpdf_array.cpp @@ -66,7 +66,7 @@ std::unique_ptr CPDF_Array::CloneNonCyclic( return std::move(pCopy); } -CFX_FloatRect CPDF_Array::GetRect() { +CFX_FloatRect CPDF_Array::GetRect() const { CFX_FloatRect rect; if (!IsArray() || m_Objects.size() != 4) return rect; @@ -78,7 +78,7 @@ CFX_FloatRect CPDF_Array::GetRect() { return rect; } -CFX_Matrix CPDF_Array::GetMatrix() { +CFX_Matrix CPDF_Array::GetMatrix() const { CFX_Matrix matrix; if (!IsArray() || m_Objects.size() != 6) return CFX_Matrix(); diff --git a/core/fpdfapi/parser/cpdf_array.h b/core/fpdfapi/parser/cpdf_array.h index bf8cb426c4..442b85cc80 100644 --- a/core/fpdfapi/parser/cpdf_array.h +++ b/core/fpdfapi/parser/cpdf_array.h @@ -47,8 +47,8 @@ class CPDF_Array : public CPDF_Object { CPDF_Stream* GetStreamAt(size_t index) const; CPDF_Array* GetArrayAt(size_t index) const; float GetFloatAt(size_t index) const { return GetNumberAt(index); } - CFX_Matrix GetMatrix(); - CFX_FloatRect GetRect(); + CFX_Matrix GetMatrix() const; + CFX_FloatRect GetRect() const; // Takes ownership of |pObj|, returns unowned pointer to it. CPDF_Object* Add(std::unique_ptr pObj); diff --git a/core/fpdfapi/parser/cpdf_dictionary.cpp b/core/fpdfapi/parser/cpdf_dictionary.cpp index b3195b58af..29571d6c51 100644 --- a/core/fpdfapi/parser/cpdf_dictionary.cpp +++ b/core/fpdfapi/parser/cpdf_dictionary.cpp @@ -42,10 +42,12 @@ CPDF_Object::Type CPDF_Dictionary::GetType() const { return DICTIONARY; } -CPDF_Dictionary* CPDF_Dictionary::GetDict() const { - // The method should be made non-const if we want to not be const. - // See bug #234. - return const_cast(this); +CPDF_Dictionary* CPDF_Dictionary::GetDict() { + return this; +} + +const CPDF_Dictionary* CPDF_Dictionary::GetDict() const { + return this; } bool CPDF_Dictionary::IsDictionary() const { diff --git a/core/fpdfapi/parser/cpdf_dictionary.h b/core/fpdfapi/parser/cpdf_dictionary.h index 688e88e510..8ecbe2d6fc 100644 --- a/core/fpdfapi/parser/cpdf_dictionary.h +++ b/core/fpdfapi/parser/cpdf_dictionary.h @@ -33,7 +33,8 @@ class CPDF_Dictionary : public CPDF_Object { // CPDF_Object: Type GetType() const override; std::unique_ptr Clone() const override; - CPDF_Dictionary* GetDict() const override; + CPDF_Dictionary* GetDict() override; + const CPDF_Dictionary* GetDict() const override; bool IsDictionary() const override; CPDF_Dictionary* AsDictionary() override; const CPDF_Dictionary* AsDictionary() const override; diff --git a/core/fpdfapi/parser/cpdf_object.cpp b/core/fpdfapi/parser/cpdf_object.cpp index 67632a040c..8190d3429f 100644 --- a/core/fpdfapi/parser/cpdf_object.cpp +++ b/core/fpdfapi/parser/cpdf_object.cpp @@ -19,8 +19,12 @@ CPDF_Object::~CPDF_Object() {} -CPDF_Object* CPDF_Object::GetDirect() const { - return const_cast(this); +CPDF_Object* CPDF_Object::GetDirect() { + return this; +} + +const CPDF_Object* CPDF_Object::GetDirect() const { + return this; } std::unique_ptr CPDF_Object::CloneObjectNonCyclic( @@ -55,7 +59,11 @@ int CPDF_Object::GetInteger() const { return 0; } -CPDF_Dictionary* CPDF_Object::GetDict() const { +CPDF_Dictionary* CPDF_Object::GetDict() { + return nullptr; +} + +const CPDF_Dictionary* CPDF_Object::GetDict() const { return nullptr; } diff --git a/core/fpdfapi/parser/cpdf_object.h b/core/fpdfapi/parser/cpdf_object.h index 7a14492d70..21eec62b0d 100644 --- a/core/fpdfapi/parser/cpdf_object.h +++ b/core/fpdfapi/parser/cpdf_object.h @@ -56,12 +56,14 @@ class CPDF_Object { // copied to the object it points to directly. virtual std::unique_ptr CloneDirectObject() const; - virtual CPDF_Object* GetDirect() const; + virtual CPDF_Object* GetDirect(); + virtual const CPDF_Object* GetDirect() const; virtual ByteString GetString() const; virtual WideString GetUnicodeText() const; virtual float GetNumber() const; virtual int GetInteger() const; - virtual CPDF_Dictionary* GetDict() const; + virtual CPDF_Dictionary* GetDict(); + virtual const CPDF_Dictionary* GetDict() const; virtual void SetString(const ByteString& str); diff --git a/core/fpdfapi/parser/cpdf_reference.cpp b/core/fpdfapi/parser/cpdf_reference.cpp index 2ca0bddc4f..3f45a96c6b 100644 --- a/core/fpdfapi/parser/cpdf_reference.cpp +++ b/core/fpdfapi/parser/cpdf_reference.cpp @@ -21,25 +21,30 @@ CPDF_Object::Type CPDF_Reference::GetType() const { } ByteString CPDF_Reference::GetString() const { - CPDF_Object* obj = SafeGetDirect(); + const CPDF_Object* obj = SafeGetDirect(); return obj ? obj->GetString() : ByteString(); } float CPDF_Reference::GetNumber() const { - CPDF_Object* obj = SafeGetDirect(); + const CPDF_Object* obj = SafeGetDirect(); return obj ? obj->GetNumber() : 0; } int CPDF_Reference::GetInteger() const { - CPDF_Object* obj = SafeGetDirect(); + const CPDF_Object* obj = SafeGetDirect(); return obj ? obj->GetInteger() : 0; } -CPDF_Dictionary* CPDF_Reference::GetDict() const { +CPDF_Dictionary* CPDF_Reference::GetDict() { CPDF_Object* obj = SafeGetDirect(); return obj ? obj->GetDict() : nullptr; } +const CPDF_Dictionary* CPDF_Reference::GetDict() const { + const CPDF_Object* obj = SafeGetDirect(); + return obj ? obj->GetDict() : nullptr; +} + bool CPDF_Reference::IsReference() const { return true; } @@ -69,17 +74,27 @@ std::unique_ptr CPDF_Reference::CloneNonCyclic( return pdfium::MakeUnique(m_pObjList.Get(), m_RefObjNum); } -CPDF_Object* CPDF_Reference::SafeGetDirect() const { +CPDF_Object* CPDF_Reference::SafeGetDirect() { CPDF_Object* obj = GetDirect(); return (obj && !obj->IsReference()) ? obj : nullptr; } +const CPDF_Object* CPDF_Reference::SafeGetDirect() const { + const CPDF_Object* obj = GetDirect(); + return (obj && !obj->IsReference()) ? obj : nullptr; +} + void CPDF_Reference::SetRef(CPDF_IndirectObjectHolder* pDoc, uint32_t objnum) { m_pObjList = pDoc; m_RefObjNum = objnum; } -CPDF_Object* CPDF_Reference::GetDirect() const { +CPDF_Object* CPDF_Reference::GetDirect() { + return m_pObjList ? m_pObjList->GetOrParseIndirectObject(m_RefObjNum) + : nullptr; +} + +const CPDF_Object* CPDF_Reference::GetDirect() const { return m_pObjList ? m_pObjList->GetOrParseIndirectObject(m_RefObjNum) : nullptr; } diff --git a/core/fpdfapi/parser/cpdf_reference.h b/core/fpdfapi/parser/cpdf_reference.h index b472b4a3ac..4cfb5c26e3 100644 --- a/core/fpdfapi/parser/cpdf_reference.h +++ b/core/fpdfapi/parser/cpdf_reference.h @@ -23,11 +23,13 @@ class CPDF_Reference : public CPDF_Object { // CPDF_Object: Type GetType() const override; std::unique_ptr Clone() const override; - CPDF_Object* GetDirect() const override; + CPDF_Object* GetDirect() override; + const CPDF_Object* GetDirect() const override; ByteString GetString() const override; float GetNumber() const override; int GetInteger() const override; - CPDF_Dictionary* GetDict() const override; + CPDF_Dictionary* GetDict() override; + const CPDF_Dictionary* GetDict() const override; bool IsReference() const override; CPDF_Reference* AsReference() override; const CPDF_Reference* AsReference() const override; @@ -41,7 +43,8 @@ class CPDF_Reference : public CPDF_Object { std::unique_ptr CloneNonCyclic( bool bDirect, std::set* pVisited) const override; - CPDF_Object* SafeGetDirect() const; + CPDF_Object* SafeGetDirect(); + const CPDF_Object* SafeGetDirect() const; UnownedPtr m_pObjList; uint32_t m_RefObjNum; diff --git a/core/fpdfapi/parser/cpdf_stream.cpp b/core/fpdfapi/parser/cpdf_stream.cpp index 45d2ebb30d..fdbe308878 100644 --- a/core/fpdfapi/parser/cpdf_stream.cpp +++ b/core/fpdfapi/parser/cpdf_stream.cpp @@ -37,7 +37,11 @@ CPDF_Object::Type CPDF_Stream::GetType() const { return STREAM; } -CPDF_Dictionary* CPDF_Stream::GetDict() const { +CPDF_Dictionary* CPDF_Stream::GetDict() { + return m_pDict.get(); +} + +const CPDF_Dictionary* CPDF_Stream::GetDict() const { return m_pDict.get(); } @@ -84,11 +88,12 @@ std::unique_ptr CPDF_Stream::CloneNonCyclic( pAcc->LoadAllDataRaw(); uint32_t streamSize = pAcc->GetSize(); - CPDF_Dictionary* pDict = GetDict(); + const CPDF_Dictionary* pDict = GetDict(); std::unique_ptr pNewDict; if (pDict && !pdfium::ContainsKey(*pVisited, pDict)) { - pNewDict = ToDictionary( - static_cast(pDict)->CloneNonCyclic(bDirect, pVisited)); + pNewDict = + ToDictionary(static_cast(pDict)->CloneNonCyclic( + bDirect, pVisited)); } return pdfium::MakeUnique(pAcc->DetachData(), streamSize, std::move(pNewDict)); diff --git a/core/fpdfapi/parser/cpdf_stream.h b/core/fpdfapi/parser/cpdf_stream.h index f8b09af7e2..cc3dcac227 100644 --- a/core/fpdfapi/parser/cpdf_stream.h +++ b/core/fpdfapi/parser/cpdf_stream.h @@ -19,7 +19,6 @@ class CPDF_Stream : public CPDF_Object { public: CPDF_Stream(); - // Takes ownership of |pData|. CPDF_Stream(std::unique_ptr pData, uint32_t size, std::unique_ptr pDict); @@ -29,7 +28,8 @@ class CPDF_Stream : public CPDF_Object { // CPDF_Object: Type GetType() const override; std::unique_ptr Clone() const override; - CPDF_Dictionary* GetDict() const override; + CPDF_Dictionary* GetDict() override; + const CPDF_Dictionary* GetDict() const override; WideString GetUnicodeText() const override; bool IsStream() const override; CPDF_Stream* AsStream() override; diff --git a/core/fpdfapi/parser/cpdf_stream_acc.cpp b/core/fpdfapi/parser/cpdf_stream_acc.cpp index 9cce894fff..9cf9a1f464 100644 --- a/core/fpdfapi/parser/cpdf_stream_acc.cpp +++ b/core/fpdfapi/parser/cpdf_stream_acc.cpp @@ -64,7 +64,7 @@ void CPDF_StreamAcc::LoadAllDataRaw() { LoadAllData(true, 0, false); } -CPDF_Dictionary* CPDF_StreamAcc::GetDict() const { +const CPDF_Dictionary* CPDF_StreamAcc::GetDict() const { return m_pStream ? m_pStream->GetDict() : nullptr; } diff --git a/core/fpdfapi/parser/cpdf_stream_acc.h b/core/fpdfapi/parser/cpdf_stream_acc.h index 89a454a0f0..1c9d5ef096 100644 --- a/core/fpdfapi/parser/cpdf_stream_acc.h +++ b/core/fpdfapi/parser/cpdf_stream_acc.h @@ -29,7 +29,7 @@ class CPDF_StreamAcc : public Retainable { void LoadAllDataRaw(); const CPDF_Stream* GetStream() const { return m_pStream.Get(); } - CPDF_Dictionary* GetDict() const; + const CPDF_Dictionary* GetDict() const; uint8_t* GetData() const; uint32_t GetSize() const; -- cgit v1.2.3