1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "core/fpdfapi/parser/cpdf_reference.h"
8
9 #include "core/fpdfapi/parser/cpdf_indirect_object_holder.h"
10 #include "third_party/base/ptr_util.h"
11 #include "third_party/base/stl_util.h"
12
CPDF_Reference(CPDF_IndirectObjectHolder * pDoc,uint32_t objnum)13 CPDF_Reference::CPDF_Reference(CPDF_IndirectObjectHolder* pDoc, uint32_t objnum)
14 : m_pObjList(pDoc), m_RefObjNum(objnum) {}
15
~CPDF_Reference()16 CPDF_Reference::~CPDF_Reference() {}
17
GetType() const18 CPDF_Object::Type CPDF_Reference::GetType() const {
19 return REFERENCE;
20 }
21
GetString() const22 CFX_ByteString CPDF_Reference::GetString() const {
23 CPDF_Object* obj = SafeGetDirect();
24 return obj ? obj->GetString() : CFX_ByteString();
25 }
26
GetNumber() const27 FX_FLOAT CPDF_Reference::GetNumber() const {
28 CPDF_Object* obj = SafeGetDirect();
29 return obj ? obj->GetNumber() : 0;
30 }
31
GetInteger() const32 int CPDF_Reference::GetInteger() const {
33 CPDF_Object* obj = SafeGetDirect();
34 return obj ? obj->GetInteger() : 0;
35 }
36
GetDict() const37 CPDF_Dictionary* CPDF_Reference::GetDict() const {
38 CPDF_Object* obj = SafeGetDirect();
39 return obj ? obj->GetDict() : nullptr;
40 }
41
IsReference() const42 bool CPDF_Reference::IsReference() const {
43 return true;
44 }
45
AsReference()46 CPDF_Reference* CPDF_Reference::AsReference() {
47 return this;
48 }
49
AsReference() const50 const CPDF_Reference* CPDF_Reference::AsReference() const {
51 return this;
52 }
53
Clone() const54 std::unique_ptr<CPDF_Object> CPDF_Reference::Clone() const {
55 return CloneObjectNonCyclic(false);
56 }
57
CloneNonCyclic(bool bDirect,std::set<const CPDF_Object * > * pVisited) const58 std::unique_ptr<CPDF_Object> CPDF_Reference::CloneNonCyclic(
59 bool bDirect,
60 std::set<const CPDF_Object*>* pVisited) const {
61 pVisited->insert(this);
62 if (bDirect) {
63 auto* pDirect = GetDirect();
64 return pDirect && !pdfium::ContainsKey(*pVisited, pDirect)
65 ? pDirect->CloneNonCyclic(true, pVisited)
66 : nullptr;
67 }
68 return pdfium::MakeUnique<CPDF_Reference>(m_pObjList, m_RefObjNum);
69 }
70
SafeGetDirect() const71 CPDF_Object* CPDF_Reference::SafeGetDirect() const {
72 CPDF_Object* obj = GetDirect();
73 return (obj && !obj->IsReference()) ? obj : nullptr;
74 }
75
SetRef(CPDF_IndirectObjectHolder * pDoc,uint32_t objnum)76 void CPDF_Reference::SetRef(CPDF_IndirectObjectHolder* pDoc, uint32_t objnum) {
77 m_pObjList = pDoc;
78 m_RefObjNum = objnum;
79 }
80
GetDirect() const81 CPDF_Object* CPDF_Reference::GetDirect() const {
82 return m_pObjList ? m_pObjList->GetOrParseIndirectObject(m_RefObjNum)
83 : nullptr;
84 }
85