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 #ifndef CORE_FPDFAPI_PARSER_CPDF_OBJECT_H_ 8 #define CORE_FPDFAPI_PARSER_CPDF_OBJECT_H_ 9 10 #include <memory> 11 #include <set> 12 #include <type_traits> 13 14 #include "core/fxcrt/fx_string.h" 15 #include "core/fxcrt/fx_system.h" 16 17 class CPDF_Array; 18 class CPDF_Boolean; 19 class CPDF_Dictionary; 20 class CPDF_Encryptor; 21 class CPDF_IndirectObjectHolder; 22 class CPDF_Name; 23 class CPDF_Null; 24 class CPDF_Number; 25 class CPDF_Reference; 26 class CPDF_Stream; 27 class CPDF_String; 28 class IFX_ArchiveStream; 29 30 class CPDF_Object : public Retainable { 31 public: 32 static const uint32_t kInvalidObjNum = static_cast<uint32_t>(-1); 33 enum Type { 34 kBoolean = 1, 35 kNumber, 36 kString, 37 kName, 38 kArray, 39 kDictionary, 40 kStream, 41 kNullobj, 42 kReference 43 }; 44 45 virtual Type GetType() const = 0; GetObjNum()46 uint32_t GetObjNum() const { return m_ObjNum; } SetObjNum(uint32_t objnum)47 void SetObjNum(uint32_t objnum) { m_ObjNum = objnum; } GetGenNum()48 uint32_t GetGenNum() const { return m_GenNum; } SetGenNum(uint32_t gennum)49 void SetGenNum(uint32_t gennum) { m_GenNum = gennum; } IsInline()50 bool IsInline() const { return m_ObjNum == 0; } 51 52 // Create a deep copy of the object. 53 virtual RetainPtr<CPDF_Object> Clone() const = 0; 54 55 // Create a deep copy of the object except any reference object be 56 // copied to the object it points to directly. 57 virtual RetainPtr<CPDF_Object> CloneDirectObject() const; 58 59 virtual CPDF_Object* GetDirect(); 60 virtual const CPDF_Object* GetDirect() const; 61 virtual ByteString GetString() const; 62 virtual WideString GetUnicodeText() const; 63 virtual float GetNumber() const; 64 virtual int GetInteger() const; 65 virtual CPDF_Dictionary* GetDict(); 66 virtual const CPDF_Dictionary* GetDict() const; 67 68 virtual void SetString(const ByteString& str); 69 70 virtual bool IsArray() const; 71 virtual bool IsBoolean() const; 72 virtual bool IsDictionary() const; 73 virtual bool IsName() const; 74 virtual bool IsNumber() const; 75 virtual bool IsReference() const; 76 virtual bool IsStream() const; 77 virtual bool IsString() const; 78 virtual bool IsNull() const; 79 80 virtual CPDF_Array* AsArray(); 81 virtual const CPDF_Array* AsArray() const; 82 virtual CPDF_Boolean* AsBoolean(); 83 virtual const CPDF_Boolean* AsBoolean() const; 84 virtual CPDF_Dictionary* AsDictionary(); 85 virtual const CPDF_Dictionary* AsDictionary() const; 86 virtual CPDF_Name* AsName(); 87 virtual const CPDF_Name* AsName() const; 88 virtual CPDF_Number* AsNumber(); 89 virtual const CPDF_Number* AsNumber() const; 90 virtual CPDF_Reference* AsReference(); 91 virtual const CPDF_Reference* AsReference() const; 92 virtual CPDF_Stream* AsStream(); 93 virtual const CPDF_Stream* AsStream() const; 94 virtual CPDF_String* AsString(); 95 virtual const CPDF_String* AsString() const; 96 97 virtual bool WriteTo(IFX_ArchiveStream* archive, 98 const CPDF_Encryptor* encryptor) const = 0; 99 100 // Create a deep copy of the object with the option to either 101 // copy a reference object or directly copy the object it refers to 102 // when |bDirect| is true. 103 // Also check cyclic reference against |pVisited|, no copy if it is found. 104 // Complex objects should implement their own CloneNonCyclic() 105 // function to properly check for possible loop. 106 virtual RetainPtr<CPDF_Object> CloneNonCyclic( 107 bool bDirect, 108 std::set<const CPDF_Object*>* pVisited) const; 109 110 // Return a reference to itself. 111 // The object must be direct (!IsInlined). 112 virtual RetainPtr<CPDF_Object> MakeReference( 113 CPDF_IndirectObjectHolder* holder) const; 114 115 protected: 116 CPDF_Object() = default; 117 CPDF_Object(const CPDF_Object& src) = delete; 118 ~CPDF_Object() override; 119 120 RetainPtr<CPDF_Object> CloneObjectNonCyclic(bool bDirect) const; 121 122 uint32_t m_ObjNum = 0; 123 uint32_t m_GenNum = 0; 124 }; 125 126 template <typename T> 127 struct CanInternStrings { 128 static const bool value = std::is_same<T, CPDF_Array>::value || 129 std::is_same<T, CPDF_Dictionary>::value || 130 std::is_same<T, CPDF_Name>::value || 131 std::is_same<T, CPDF_String>::value; 132 }; 133 134 #endif // CORE_FPDFAPI_PARSER_CPDF_OBJECT_H_ 135