From 5fc239a0e64a5e16a23b99e128f88cc494d5b65c Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Thu, 10 Mar 2016 14:10:38 -0800 Subject: Split fpdf_parser_objects.cpp into per-class .cpp/.h files. R=dsinclair@chromium.org Review URL: https://codereview.chromium.org/1776913007 . --- core/include/fpdfapi/cfdf_document.h | 6 +- core/include/fpdfapi/cpdf_array.h | 65 ++ core/include/fpdfapi/cpdf_boolean.h | 35 + core/include/fpdfapi/cpdf_dictionary.h | 99 ++ core/include/fpdfapi/cpdf_document.h | 5 +- core/include/fpdfapi/cpdf_indirect_object_holder.h | 44 + core/include/fpdfapi/cpdf_name.h | 35 + core/include/fpdfapi/cpdf_null.h | 21 + core/include/fpdfapi/cpdf_number.h | 44 + core/include/fpdfapi/cpdf_object.h | 153 +++ core/include/fpdfapi/cpdf_reference.h | 51 + core/include/fpdfapi/cpdf_stream.h | 99 ++ core/include/fpdfapi/cpdf_string.h | 40 + core/include/fpdfapi/fpdf_objects.h | 553 ---------- core/include/fpdfapi/fpdf_resource.h | 2 +- core/include/fpdfdoc/fpdf_doc.h | 1 + core/src/fpdfapi/fpdf_edit/fpdf_edit_content.cpp | 2 + core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp | 4 + core/src/fpdfapi/fpdf_edit/fpdf_edit_doc.cpp | 3 + core/src/fpdfapi/fpdf_edit/fpdf_edit_image.cpp | 4 + core/src/fpdfapi/fpdf_font/fpdf_font.cpp | 4 + core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp | 2 + core/src/fpdfapi/fpdf_page/fpdf_page.cpp | 2 + core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp | 3 + core/src/fpdfapi/fpdf_page/fpdf_page_doc.cpp | 2 + core/src/fpdfapi/fpdf_page/fpdf_page_func.cpp | 2 + .../fpdfapi/fpdf_page/fpdf_page_graph_state.cpp | 2 + core/src/fpdfapi/fpdf_page/fpdf_page_image.cpp | 1 + core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp | 5 + .../src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp | 7 + core/src/fpdfapi/fpdf_page/fpdf_page_pattern.cpp | 2 + core/src/fpdfapi/fpdf_parser/cfdf_document.cpp | 1 + core/src/fpdfapi/fpdf_parser/cpdf_array.cpp | 207 ++++ core/src/fpdfapi/fpdf_parser/cpdf_boolean.cpp | 45 + core/src/fpdfapi/fpdf_parser/cpdf_data_avail.cpp | 7 +- core/src/fpdfapi/fpdf_parser/cpdf_dictionary.cpp | 271 +++++ core/src/fpdfapi/fpdf_parser/cpdf_document.cpp | 3 + core/src/fpdfapi/fpdf_parser/cpdf_hint_tables.cpp | 4 +- .../fpdf_parser/cpdf_indirect_object_holder.cpp | 80 ++ core/src/fpdfapi/fpdf_parser/cpdf_name.cpp | 53 + core/src/fpdfapi/fpdf_parser/cpdf_null.cpp | 17 + core/src/fpdfapi/fpdf_parser/cpdf_number.cpp | 56 + core/src/fpdfapi/fpdf_parser/cpdf_object.cpp | 158 +++ .../fpdfapi/fpdf_parser/cpdf_object_unittest.cpp | 776 +++++++++++++ core/src/fpdfapi/fpdf_parser/cpdf_parser.cpp | 5 + core/src/fpdfapi/fpdf_parser/cpdf_reference.cpp | 72 ++ .../fpdf_parser/cpdf_standard_security_handler.cpp | 4 +- core/src/fpdfapi/fpdf_parser/cpdf_stream.cpp | 232 ++++ core/src/fpdfapi/fpdf_parser/cpdf_string.cpp | 56 + .../src/fpdfapi/fpdf_parser/cpdf_syntax_parser.cpp | 10 +- .../src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp | 3 +- .../fpdfapi/fpdf_parser/fpdf_parser_objects.cpp | 1148 -------------------- .../fpdf_parser/fpdf_parser_objects_unittest.cpp | 767 ------------- .../fpdfapi/fpdf_parser/fpdf_parser_utility.cpp | 7 +- core/src/fpdfapi/fpdf_render/fpdf_render.cpp | 2 + core/src/fpdfapi/fpdf_render/fpdf_render_image.cpp | 2 + .../fpdfapi/fpdf_render/fpdf_render_loadimage.cpp | 2 + .../fpdfapi/fpdf_render/fpdf_render_pattern.cpp | 2 + core/src/fpdfapi/fpdf_render/fpdf_render_text.cpp | 1 + core/src/fpdfdoc/doc_action.cpp | 1 + core/src/fpdfdoc/doc_annot.cpp | 2 + core/src/fpdfdoc/doc_ap.cpp | 1 + core/src/fpdfdoc/doc_basic.cpp | 1 + core/src/fpdfdoc/doc_basic_unittest.cpp | 2 + core/src/fpdfdoc/doc_bookmark.cpp | 2 + core/src/fpdfdoc/doc_form.cpp | 4 +- core/src/fpdfdoc/doc_formcontrol.cpp | 1 + core/src/fpdfdoc/doc_formfield.cpp | 4 + core/src/fpdfdoc/doc_link.cpp | 2 + core/src/fpdfdoc/doc_ocg.cpp | 1 + core/src/fpdfdoc/doc_tagged.cpp | 3 + core/src/fpdfdoc/doc_utils.cpp | 1 + core/src/fpdftext/fpdf_text_int.cpp | 2 + core/src/fxcodec/jbig2/JBig2_BitStream.cpp | 2 +- core/src/fxcodec/jbig2/JBig2_Context.cpp | 1 + core/src/fxcodec/jbig2/JBig2_Context.h | 3 +- 76 files changed, 2840 insertions(+), 2482 deletions(-) create mode 100644 core/include/fpdfapi/cpdf_array.h create mode 100644 core/include/fpdfapi/cpdf_boolean.h create mode 100644 core/include/fpdfapi/cpdf_dictionary.h create mode 100644 core/include/fpdfapi/cpdf_indirect_object_holder.h create mode 100644 core/include/fpdfapi/cpdf_name.h create mode 100644 core/include/fpdfapi/cpdf_null.h create mode 100644 core/include/fpdfapi/cpdf_number.h create mode 100644 core/include/fpdfapi/cpdf_object.h create mode 100644 core/include/fpdfapi/cpdf_reference.h create mode 100644 core/include/fpdfapi/cpdf_stream.h create mode 100644 core/include/fpdfapi/cpdf_string.h delete mode 100644 core/include/fpdfapi/fpdf_objects.h create mode 100644 core/src/fpdfapi/fpdf_parser/cpdf_array.cpp create mode 100644 core/src/fpdfapi/fpdf_parser/cpdf_boolean.cpp create mode 100644 core/src/fpdfapi/fpdf_parser/cpdf_dictionary.cpp create mode 100644 core/src/fpdfapi/fpdf_parser/cpdf_indirect_object_holder.cpp create mode 100644 core/src/fpdfapi/fpdf_parser/cpdf_name.cpp create mode 100644 core/src/fpdfapi/fpdf_parser/cpdf_null.cpp create mode 100644 core/src/fpdfapi/fpdf_parser/cpdf_number.cpp create mode 100644 core/src/fpdfapi/fpdf_parser/cpdf_object.cpp create mode 100644 core/src/fpdfapi/fpdf_parser/cpdf_object_unittest.cpp create mode 100644 core/src/fpdfapi/fpdf_parser/cpdf_reference.cpp create mode 100644 core/src/fpdfapi/fpdf_parser/cpdf_stream.cpp create mode 100644 core/src/fpdfapi/fpdf_parser/cpdf_string.cpp delete mode 100644 core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp delete mode 100644 core/src/fpdfapi/fpdf_parser/fpdf_parser_objects_unittest.cpp (limited to 'core') diff --git a/core/include/fpdfapi/cfdf_document.h b/core/include/fpdfapi/cfdf_document.h index 6fb8ab7ba2..85852cd8cf 100644 --- a/core/include/fpdfapi/cfdf_document.h +++ b/core/include/fpdfapi/cfdf_document.h @@ -7,9 +7,9 @@ #ifndef CORE_INCLUDE_FPDFAPI_CFDF_DOCUMENT_H_ #define CORE_INCLUDE_FPDFAPI_CFDF_DOCUMENT_H_ -#include "core/include/fpdfapi/fpdf_objects.h" -#include "core/include/fxcrt/fx_stream.h" -#include "core/include/fxcrt/fx_system.h" +#include "core/include/fpdfapi/cpdf_indirect_object_holder.h" +#include "core/include/fpdfapi/cpdf_object.h" +#include "core/include/fxcrt/fx_basic.h" class CPDF_Dictionary; diff --git a/core/include/fpdfapi/cpdf_array.h b/core/include/fpdfapi/cpdf_array.h new file mode 100644 index 0000000000..8e47d9fef5 --- /dev/null +++ b/core/include/fpdfapi/cpdf_array.h @@ -0,0 +1,65 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_INCLUDE_FPDFAPI_CPDF_ARRAY_H_ +#define CORE_INCLUDE_FPDFAPI_CPDF_ARRAY_H_ + +#include "core/include/fpdfapi/cpdf_indirect_object_holder.h" +#include "core/include/fpdfapi/cpdf_object.h" +#include "core/include/fxcrt/fx_basic.h" +#include "core/include/fxcrt/fx_coordinates.h" + +class CPDF_Array : public CPDF_Object { + public: + CPDF_Array(); + + // CPDF_Object. + Type GetType() const override; + CPDF_Object* Clone(FX_BOOL bDirect = FALSE) const override; + CPDF_Array* GetArray() const override; + bool IsArray() const override; + CPDF_Array* AsArray() override; + const CPDF_Array* AsArray() const override; + + FX_DWORD GetCount() const { return m_Objects.GetSize(); } + CPDF_Object* GetElement(FX_DWORD index) const; + CPDF_Object* GetElementValue(FX_DWORD index) const; + CFX_Matrix GetMatrix(); + CFX_FloatRect GetRect(); + CFX_ByteString GetStringAt(FX_DWORD index) const; + CFX_ByteStringC GetConstStringAt(FX_DWORD index) const; + int GetIntegerAt(FX_DWORD index) const; + FX_FLOAT GetNumberAt(FX_DWORD index) const; + CPDF_Dictionary* GetDictAt(FX_DWORD index) const; + CPDF_Stream* GetStreamAt(FX_DWORD index) const; + CPDF_Array* GetArrayAt(FX_DWORD index) const; + FX_FLOAT GetFloatAt(FX_DWORD index) const { return GetNumberAt(index); } + + void SetAt(FX_DWORD index, + CPDF_Object* pObj, + CPDF_IndirectObjectHolder* pObjs = nullptr); + void InsertAt(FX_DWORD index, + CPDF_Object* pObj, + CPDF_IndirectObjectHolder* pObjs = nullptr); + void RemoveAt(FX_DWORD index, int nCount = 1); + + void Add(CPDF_Object* pObj, CPDF_IndirectObjectHolder* pObjs = nullptr); + void AddNumber(FX_FLOAT f); + void AddInteger(int i); + void AddString(const CFX_ByteString& str); + void AddName(const CFX_ByteString& str); + void AddReference(CPDF_IndirectObjectHolder* pDoc, FX_DWORD objnum); + void AddReference(CPDF_IndirectObjectHolder* pDoc, CPDF_Object* obj) { + AddReference(pDoc, obj->GetObjNum()); + } + + protected: + ~CPDF_Array() override; + + CFX_ArrayTemplate m_Objects; +}; + +#endif // CORE_INCLUDE_FPDFAPI_CPDF_ARRAY_H_ diff --git a/core/include/fpdfapi/cpdf_boolean.h b/core/include/fpdfapi/cpdf_boolean.h new file mode 100644 index 0000000000..0621c0441c --- /dev/null +++ b/core/include/fpdfapi/cpdf_boolean.h @@ -0,0 +1,35 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_INCLUDE_FPDFAPI_CPDF_BOOLEAN_H_ +#define CORE_INCLUDE_FPDFAPI_CPDF_BOOLEAN_H_ + +#include "core/include/fpdfapi/cpdf_object.h" +#include "core/include/fxcrt/fx_string.h" +#include "core/include/fxcrt/fx_system.h" + +class CPDF_Boolean : public CPDF_Object { + public: + CPDF_Boolean(); + explicit CPDF_Boolean(FX_BOOL value); + + // CPDF_Object. + Type GetType() const override; + CPDF_Object* Clone(FX_BOOL bDirect = FALSE) const override; + CFX_ByteString GetString() const override; + int GetInteger() const override; + void SetString(const CFX_ByteString& str) override; + bool IsBoolean() const override; + CPDF_Boolean* AsBoolean() override; + const CPDF_Boolean* AsBoolean() const override; + + protected: + ~CPDF_Boolean() override; + + FX_BOOL m_bValue; +}; + +#endif // CORE_INCLUDE_FPDFAPI_CPDF_BOOLEAN_H_ diff --git a/core/include/fpdfapi/cpdf_dictionary.h b/core/include/fpdfapi/cpdf_dictionary.h new file mode 100644 index 0000000000..e547f75729 --- /dev/null +++ b/core/include/fpdfapi/cpdf_dictionary.h @@ -0,0 +1,99 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_INCLUDE_FPDFAPI_CPDF_DICTIONARY_H_ +#define CORE_INCLUDE_FPDFAPI_CPDF_DICTIONARY_H_ + +#include + +#include "core/include/fpdfapi/cpdf_object.h" +#include "core/include/fxcrt/fx_coordinates.h" +#include "core/include/fxcrt/fx_string.h" + +class CPDF_IndirectObjectHolder; + +class CPDF_Dictionary : public CPDF_Object { + public: + using iterator = std::map::iterator; + using const_iterator = std::map::const_iterator; + + CPDF_Dictionary(); + + // CPDF_Object. + Type GetType() const override; + CPDF_Object* Clone(FX_BOOL bDirect = FALSE) const override; + CPDF_Dictionary* GetDict() const override; + bool IsDictionary() const override; + CPDF_Dictionary* AsDictionary() override; + const CPDF_Dictionary* AsDictionary() const override; + + size_t GetCount() const { return m_Map.size(); } + CPDF_Object* GetElement(const CFX_ByteStringC& key) const; + CPDF_Object* GetElementValue(const CFX_ByteStringC& key) const; + CFX_ByteString GetStringBy(const CFX_ByteStringC& key) const; + CFX_ByteStringC GetConstStringBy(const CFX_ByteStringC& key) const; + CFX_ByteString GetStringBy(const CFX_ByteStringC& key, + const CFX_ByteStringC& default_str) const; + CFX_ByteStringC GetConstStringBy(const CFX_ByteStringC& key, + const CFX_ByteStringC& default_str) const; + CFX_WideString GetUnicodeTextBy(const CFX_ByteStringC& key) const; + int GetIntegerBy(const CFX_ByteStringC& key) const; + int GetIntegerBy(const CFX_ByteStringC& key, int default_int) const; + FX_BOOL GetBooleanBy(const CFX_ByteStringC& key, + FX_BOOL bDefault = FALSE) const; + FX_FLOAT GetNumberBy(const CFX_ByteStringC& key) const; + CPDF_Dictionary* GetDictBy(const CFX_ByteStringC& key) const; + CPDF_Stream* GetStreamBy(const CFX_ByteStringC& key) const; + CPDF_Array* GetArrayBy(const CFX_ByteStringC& key) const; + CFX_FloatRect GetRectBy(const CFX_ByteStringC& key) const; + CFX_Matrix GetMatrixBy(const CFX_ByteStringC& key) const; + FX_FLOAT GetFloatBy(const CFX_ByteStringC& key) const { + return GetNumberBy(key); + } + + FX_BOOL KeyExist(const CFX_ByteStringC& key) const; + bool IsSignatureDict() const; + + // Set* functions invalidate iterators for the element with the key |key|. + void SetAt(const CFX_ByteStringC& key, CPDF_Object* pObj); + void SetAtName(const CFX_ByteStringC& key, const CFX_ByteString& name); + void SetAtString(const CFX_ByteStringC& key, const CFX_ByteString& str); + void SetAtInteger(const CFX_ByteStringC& key, int i); + void SetAtNumber(const CFX_ByteStringC& key, FX_FLOAT f); + void SetAtReference(const CFX_ByteStringC& key, + CPDF_IndirectObjectHolder* pDoc, + FX_DWORD objnum); + void SetAtReference(const CFX_ByteStringC& key, + CPDF_IndirectObjectHolder* pDoc, + CPDF_Object* obj) { + SetAtReference(key, pDoc, obj->GetObjNum()); + } + void SetAtRect(const CFX_ByteStringC& key, const CFX_FloatRect& rect); + void SetAtMatrix(const CFX_ByteStringC& key, const CFX_Matrix& matrix); + void SetAtBoolean(const CFX_ByteStringC& key, FX_BOOL bValue); + + void AddReference(const CFX_ByteStringC& key, + CPDF_IndirectObjectHolder* pDoc, + FX_DWORD objnum); + + // Invalidates iterators for the element with the key |key|. + void RemoveAt(const CFX_ByteStringC& key); + + // Invalidates iterators for the element with the key |oldkey|. + void ReplaceKey(const CFX_ByteStringC& oldkey, const CFX_ByteStringC& newkey); + + iterator begin() { return m_Map.begin(); } + iterator end() { return m_Map.end(); } + const_iterator begin() const { return m_Map.begin(); } + const_iterator end() const { return m_Map.end(); } + + protected: + ~CPDF_Dictionary() override; + + std::map m_Map; +}; + +#endif // CORE_INCLUDE_FPDFAPI_CPDF_DICTIONARY_H_ diff --git a/core/include/fpdfapi/cpdf_document.h b/core/include/fpdfapi/cpdf_document.h index c759861cd7..af7e2ab787 100644 --- a/core/include/fpdfapi/cpdf_document.h +++ b/core/include/fpdfapi/cpdf_document.h @@ -7,10 +7,12 @@ #ifndef CORE_INCLUDE_FPDFAPI_CPDF_DOCUMENT_H_ #define CORE_INCLUDE_FPDFAPI_CPDF_DOCUMENT_H_ -#include "core/include/fpdfapi/fpdf_objects.h" +#include "core/include/fpdfapi/cpdf_indirect_object_holder.h" +#include "core/include/fpdfapi/cpdf_object.h" #include "core/include/fxcrt/fx_basic.h" class CFX_Font; +class CFX_Matrix; class CPDF_ColorSpace; class CPDF_DocPageData; class CPDF_DocRenderData; @@ -19,6 +21,7 @@ class CPDF_FontEncoding; class CPDF_IccProfile; class CPDF_Image; class CPDF_Pattern; +class CPDF_StreamAcc; #define FPDFPERM_PRINT 0x0004 #define FPDFPERM_MODIFY 0x0008 diff --git a/core/include/fpdfapi/cpdf_indirect_object_holder.h b/core/include/fpdfapi/cpdf_indirect_object_holder.h new file mode 100644 index 0000000000..9f12323669 --- /dev/null +++ b/core/include/fpdfapi/cpdf_indirect_object_holder.h @@ -0,0 +1,44 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_INCLUDE_FPDFAPI_CPDF_INDIRECT_OBJECT_HOLDER_H_ +#define CORE_INCLUDE_FPDFAPI_CPDF_INDIRECT_OBJECT_HOLDER_H_ + +#include + +#include "core/include/fxcrt/fx_system.h" + +class CPDF_Object; +class CPDF_Parser; + +class CPDF_IndirectObjectHolder { + public: + using iterator = std::map::iterator; + using const_iterator = std::map::const_iterator; + + explicit CPDF_IndirectObjectHolder(CPDF_Parser* pParser); + ~CPDF_IndirectObjectHolder(); + + CPDF_Object* GetIndirectObject(FX_DWORD objnum); + FX_DWORD AddIndirectObject(CPDF_Object* pObj); + void ReleaseIndirectObject(FX_DWORD objnum); + + // Takes ownership of |pObj|. + FX_BOOL InsertIndirectObject(FX_DWORD objnum, CPDF_Object* pObj); + + FX_DWORD GetLastObjNum() const { return m_LastObjNum; } + iterator begin() { return m_IndirectObjs.begin(); } + const_iterator begin() const { return m_IndirectObjs.begin(); } + iterator end() { return m_IndirectObjs.end(); } + const_iterator end() const { return m_IndirectObjs.end(); } + + protected: + CPDF_Parser* m_pParser; + FX_DWORD m_LastObjNum; + std::map m_IndirectObjs; +}; + +#endif // CORE_INCLUDE_FPDFAPI_CPDF_INDIRECT_OBJECT_HOLDER_H_ diff --git a/core/include/fpdfapi/cpdf_name.h b/core/include/fpdfapi/cpdf_name.h new file mode 100644 index 0000000000..c956244112 --- /dev/null +++ b/core/include/fpdfapi/cpdf_name.h @@ -0,0 +1,35 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_INCLUDE_FPDFAPI_CPDF_NAME_H_ +#define CORE_INCLUDE_FPDFAPI_CPDF_NAME_H_ + +#include "core/include/fpdfapi/cpdf_object.h" + +class CPDF_Name : public CPDF_Object { + public: + explicit CPDF_Name(const CFX_ByteString& str); + explicit CPDF_Name(const CFX_ByteStringC& str); + explicit CPDF_Name(const FX_CHAR* str); + + // CPDF_Object. + Type GetType() const override; + CPDF_Object* Clone(FX_BOOL bDirect = FALSE) const override; + CFX_ByteString GetString() const override; + CFX_ByteStringC GetConstString() const override; + CFX_WideString GetUnicodeText() const override; + void SetString(const CFX_ByteString& str) override; + bool IsName() const override; + CPDF_Name* AsName() override; + const CPDF_Name* AsName() const override; + + protected: + ~CPDF_Name() override; + + CFX_ByteString m_Name; +}; + +#endif // CORE_INCLUDE_FPDFAPI_CPDF_NAME_H_ diff --git a/core/include/fpdfapi/cpdf_null.h b/core/include/fpdfapi/cpdf_null.h new file mode 100644 index 0000000000..654c2a4029 --- /dev/null +++ b/core/include/fpdfapi/cpdf_null.h @@ -0,0 +1,21 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_INCLUDE_FPDFAPI_CPDF_NULL_H_ +#define CORE_INCLUDE_FPDFAPI_CPDF_NULL_H_ + +#include "core/include/fpdfapi/cpdf_object.h" + +class CPDF_Null : public CPDF_Object { + public: + CPDF_Null(); + + // CPDF_Object. + Type GetType() const override; + CPDF_Object* Clone(FX_BOOL bDirect = FALSE) const override; +}; + +#endif // CORE_INCLUDE_FPDFAPI_CPDF_NULL_H_ diff --git a/core/include/fpdfapi/cpdf_number.h b/core/include/fpdfapi/cpdf_number.h new file mode 100644 index 0000000000..8bab95ebc2 --- /dev/null +++ b/core/include/fpdfapi/cpdf_number.h @@ -0,0 +1,44 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_INCLUDE_FPDFAPI_CPDF_NUMBER_H_ +#define CORE_INCLUDE_FPDFAPI_CPDF_NUMBER_H_ + +#include "core/include/fpdfapi/cpdf_object.h" +#include "core/include/fxcrt/fx_string.h" +#include "core/include/fxcrt/fx_system.h" + +class CPDF_Number : public CPDF_Object { + public: + CPDF_Number(); + explicit CPDF_Number(int value); + explicit CPDF_Number(FX_FLOAT value); + explicit CPDF_Number(const CFX_ByteStringC& str); + + // CPDF_Object. + Type GetType() const override; + CPDF_Object* Clone(FX_BOOL bDirect = FALSE) const override; + CFX_ByteString GetString() const override; + FX_FLOAT GetNumber() const override; + int GetInteger() const override; + void SetString(const CFX_ByteString& str) override; + bool IsNumber() const override; + CPDF_Number* AsNumber() override; + const CPDF_Number* AsNumber() const override; + + FX_BOOL IsInteger() { return m_bInteger; } + + protected: + ~CPDF_Number() override; + + FX_BOOL m_bInteger; + union { + int m_Integer; + FX_FLOAT m_Float; + }; +}; + +#endif // CORE_INCLUDE_FPDFAPI_CPDF_NUMBER_H_ diff --git a/core/include/fpdfapi/cpdf_object.h b/core/include/fpdfapi/cpdf_object.h new file mode 100644 index 0000000000..2add5524a8 --- /dev/null +++ b/core/include/fpdfapi/cpdf_object.h @@ -0,0 +1,153 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_INCLUDE_FPDFAPI_CPDF_OBJECT_H_ +#define CORE_INCLUDE_FPDFAPI_CPDF_OBJECT_H_ + +#include "core/include/fxcrt/fx_string.h" +#include "core/include/fxcrt/fx_system.h" + +class CPDF_Array; +class CPDF_Boolean; +class CPDF_Dictionary; +class CPDF_Name; +class CPDF_Null; +class CPDF_Number; +class CPDF_Reference; +class CPDF_Stream; +class CPDF_String; + +class CPDF_Object { + public: + enum Type { + BOOLEAN = 1, + NUMBER, + STRING, + NAME, + ARRAY, + DICTIONARY, + STREAM, + NULLOBJ, + REFERENCE + }; + + virtual Type GetType() const = 0; + FX_DWORD GetObjNum() const { return m_ObjNum; } + FX_DWORD GetGenNum() const { return m_GenNum; } + + virtual CPDF_Object* Clone(FX_BOOL bDirect = FALSE) const = 0; + virtual CPDF_Object* GetDirect() const; + + FX_BOOL IsModified() const { return FALSE; } + void Release(); + + virtual CFX_ByteString GetString() const; + virtual CFX_ByteStringC GetConstString() const; + virtual CFX_WideString GetUnicodeText() const; + virtual FX_FLOAT GetNumber() const; + virtual int GetInteger() const; + virtual CPDF_Dictionary* GetDict() const; + virtual CPDF_Array* GetArray() const; + + virtual void SetString(const CFX_ByteString& str); + + virtual bool IsArray() const; + virtual bool IsBoolean() const; + virtual bool IsDictionary() const; + virtual bool IsName() const; + virtual bool IsNumber() const; + virtual bool IsReference() const; + virtual bool IsStream() const; + virtual bool IsString() const; + + virtual CPDF_Array* AsArray(); + virtual const CPDF_Array* AsArray() const; + virtual CPDF_Boolean* AsBoolean(); + virtual const CPDF_Boolean* AsBoolean() const; + virtual CPDF_Dictionary* AsDictionary(); + virtual const CPDF_Dictionary* AsDictionary() const; + virtual CPDF_Name* AsName(); + virtual const CPDF_Name* AsName() const; + virtual CPDF_Number* AsNumber(); + virtual const CPDF_Number* AsNumber() const; + virtual CPDF_Reference* AsReference(); + virtual const CPDF_Reference* AsReference() const; + virtual CPDF_Stream* AsStream(); + virtual const CPDF_Stream* AsStream() const; + virtual CPDF_String* AsString(); + virtual const CPDF_String* AsString() const; + + protected: + CPDF_Object() : m_ObjNum(0), m_GenNum(0) {} + virtual ~CPDF_Object(); + void Destroy() { delete this; } + + FX_DWORD m_ObjNum; + FX_DWORD m_GenNum; + + friend class CPDF_IndirectObjectHolder; + friend class CPDF_Parser; + + private: + CPDF_Object(const CPDF_Object& src) {} +}; + +inline CPDF_Boolean* ToBoolean(CPDF_Object* obj) { + return obj ? obj->AsBoolean() : nullptr; +} + +inline const CPDF_Boolean* ToBoolean(const CPDF_Object* obj) { + return obj ? obj->AsBoolean() : nullptr; +} + +inline CPDF_Number* ToNumber(CPDF_Object* obj) { + return obj ? obj->AsNumber() : nullptr; +} + +inline const CPDF_Number* ToNumber(const CPDF_Object* obj) { + return obj ? obj->AsNumber() : nullptr; +} + +inline CPDF_String* ToString(CPDF_Object* obj) { + return obj ? obj->AsString() : nullptr; +} + +inline const CPDF_String* ToString(const CPDF_Object* obj) { + return obj ? obj->AsString() : nullptr; +} + +inline CPDF_Name* ToName(CPDF_Object* obj) { + return obj ? obj->AsName() : nullptr; +} + +inline const CPDF_Name* ToName(const CPDF_Object* obj) { + return obj ? obj->AsName() : nullptr; +} + +inline CPDF_Array* ToArray(CPDF_Object* obj) { + return obj ? obj->AsArray() : nullptr; +} + +inline const CPDF_Array* ToArray(const CPDF_Object* obj) { + return obj ? obj->AsArray() : nullptr; +} + +inline CPDF_Dictionary* ToDictionary(CPDF_Object* obj) { + return obj ? obj->AsDictionary() : nullptr; +} + +inline const CPDF_Dictionary* ToDictionary(const CPDF_Object* obj) { + return obj ? obj->AsDictionary() : nullptr; +} +inline CPDF_Reference* ToReference(CPDF_Object* obj) { + return obj ? obj->AsReference() : nullptr; +} + +inline const CPDF_Reference* ToReference(const CPDF_Object* obj) { + return obj ? obj->AsReference() : nullptr; +} + +#endif // CORE_INCLUDE_FPDFAPI_CPDF_OBJECT_H_ diff --git a/core/include/fpdfapi/cpdf_reference.h b/core/include/fpdfapi/cpdf_reference.h new file mode 100644 index 0000000000..2444aad4fd --- /dev/null +++ b/core/include/fpdfapi/cpdf_reference.h @@ -0,0 +1,51 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_INCLUDE_FPDFAPI_CPDF_REFERENCE_H_ +#define CORE_INCLUDE_FPDFAPI_CPDF_REFERENCE_H_ + +#include "core/include/fpdfapi/cpdf_object.h" + +class CPDF_IndirectObjectHolder; + +class CPDF_Reference : public CPDF_Object { + public: + CPDF_Reference(CPDF_IndirectObjectHolder* pDoc, int objnum); + + // CPDF_Object. + Type GetType() const override; + CPDF_Object* Clone(FX_BOOL bDirect = FALSE) const override; + CPDF_Object* GetDirect() const override; + CFX_ByteString GetString() const override; + CFX_ByteStringC GetConstString() const override; + FX_FLOAT GetNumber() const override; + int GetInteger() const override; + CPDF_Dictionary* GetDict() const override; + + // TODO(weili): check whether GetUnicodeText() and GetArray() are needed. + bool IsReference() const override; + CPDF_Reference* AsReference() override; + const CPDF_Reference* AsReference() const override; + + CPDF_IndirectObjectHolder* GetObjList() const { return m_pObjList; } + FX_DWORD GetRefObjNum() const { return m_RefObjNum; } + + void SetRef(CPDF_IndirectObjectHolder* pDoc, FX_DWORD objnum); + + protected: + ~CPDF_Reference() override; + CPDF_Object* SafeGetDirect() const { + CPDF_Object* obj = GetDirect(); + if (!obj || obj->IsReference()) + return nullptr; + return obj; + } + + CPDF_IndirectObjectHolder* m_pObjList; + FX_DWORD m_RefObjNum; +}; + +#endif // CORE_INCLUDE_FPDFAPI_CPDF_REFERENCE_H_ diff --git a/core/include/fpdfapi/cpdf_stream.h b/core/include/fpdfapi/cpdf_stream.h new file mode 100644 index 0000000000..fd3b8c0047 --- /dev/null +++ b/core/include/fpdfapi/cpdf_stream.h @@ -0,0 +1,99 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_INCLUDE_FPDFAPI_CPDF_STREAM_H_ +#define CORE_INCLUDE_FPDFAPI_CPDF_STREAM_H_ + +#include "core/include/fpdfapi/cpdf_object.h" +#include "core/include/fxcrt/fx_stream.h" + +class CPDF_Stream : public CPDF_Object { + public: + CPDF_Stream(uint8_t* pData, FX_DWORD size, CPDF_Dictionary* pDict); + + // CPDF_Object. + Type GetType() const override; + CPDF_Object* Clone(FX_BOOL bDirect = FALSE) const override; + CPDF_Dictionary* GetDict() const override; + CFX_WideString GetUnicodeText() const override; + bool IsStream() const override; + CPDF_Stream* AsStream() override; + const CPDF_Stream* AsStream() const override; + + FX_DWORD GetRawSize() const { return m_dwSize; } + uint8_t* GetRawData() const { return m_pDataBuf; } + + void SetData(const uint8_t* pData, + FX_DWORD size, + FX_BOOL bCompressed, + FX_BOOL bKeepBuf); + + void InitStream(uint8_t* pData, FX_DWORD size, CPDF_Dictionary* pDict); + void InitStreamFromFile(IFX_FileRead* pFile, CPDF_Dictionary* pDict); + + FX_BOOL ReadRawData(FX_FILESIZE start_pos, + uint8_t* pBuf, + FX_DWORD buf_size) const; + + bool IsMemoryBased() const { return m_GenNum == kMemoryBasedGenNum; } + + protected: + static const FX_DWORD kMemoryBasedGenNum = (FX_DWORD)-1; + + ~CPDF_Stream() override; + + void InitStreamInternal(CPDF_Dictionary* pDict); + + CPDF_Dictionary* m_pDict; + FX_DWORD m_dwSize; + FX_DWORD m_GenNum; + + union { + uint8_t* m_pDataBuf; + IFX_FileRead* m_pFile; + }; +}; + +inline CPDF_Stream* ToStream(CPDF_Object* obj) { + return obj ? obj->AsStream() : nullptr; +} + +inline const CPDF_Stream* ToStream(const CPDF_Object* obj) { + return obj ? obj->AsStream() : nullptr; +} + +class CPDF_StreamAcc { + public: + CPDF_StreamAcc(); + ~CPDF_StreamAcc(); + + void LoadAllData(const CPDF_Stream* pStream, + FX_BOOL bRawAccess = FALSE, + FX_DWORD estimated_size = 0, + FX_BOOL bImageAcc = FALSE); + + const CPDF_Stream* GetStream() const { return m_pStream; } + CPDF_Dictionary* GetDict() const { + return m_pStream ? m_pStream->GetDict() : nullptr; + } + const uint8_t* GetData() const; + FX_DWORD GetSize() const; + const CFX_ByteString& GetImageDecoder() const { return m_ImageDecoder; } + const CPDF_Dictionary* GetImageParam() const { return m_pImageParam; } + + uint8_t* DetachData(); + + protected: + uint8_t* m_pData; + FX_DWORD m_dwSize; + FX_BOOL m_bNewBuf; + CFX_ByteString m_ImageDecoder; + CPDF_Dictionary* m_pImageParam; + const CPDF_Stream* m_pStream; + uint8_t* m_pSrcData; +}; + +#endif // CORE_INCLUDE_FPDFAPI_CPDF_STREAM_H_ diff --git a/core/include/fpdfapi/cpdf_string.h b/core/include/fpdfapi/cpdf_string.h new file mode 100644 index 0000000000..895513813c --- /dev/null +++ b/core/include/fpdfapi/cpdf_string.h @@ -0,0 +1,40 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_INCLUDE_FPDFAPI_CPDF_STRING_H_ +#define CORE_INCLUDE_FPDFAPI_CPDF_STRING_H_ + +#include "core/include/fpdfapi/cpdf_object.h" +#include "core/include/fxcrt/fx_string.h" +#include "core/include/fxcrt/fx_system.h" + +class CPDF_String : public CPDF_Object { + public: + CPDF_String(); + CPDF_String(const CFX_ByteString& str, FX_BOOL bHex); + explicit CPDF_String(const CFX_WideString& str); + + // CPDF_Object. + Type GetType() const override; + CPDF_Object* Clone(FX_BOOL bDirect = FALSE) const override; + CFX_ByteString GetString() const override; + CFX_ByteStringC GetConstString() const override; + CFX_WideString GetUnicodeText() const override; + void SetString(const CFX_ByteString& str) override; + bool IsString() const override; + CPDF_String* AsString() override; + const CPDF_String* AsString() const override; + + FX_BOOL IsHex() const { return m_bHex; } + + protected: + ~CPDF_String() override; + + CFX_ByteString m_String; + FX_BOOL m_bHex; +}; + +#endif // CORE_INCLUDE_FPDFAPI_CPDF_STRING_H_ diff --git a/core/include/fpdfapi/fpdf_objects.h b/core/include/fpdfapi/fpdf_objects.h deleted file mode 100644 index 42282a1dcc..0000000000 --- a/core/include/fpdfapi/fpdf_objects.h +++ /dev/null @@ -1,553 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#ifndef CORE_INCLUDE_FPDFAPI_FPDF_OBJECTS_H_ -#define CORE_INCLUDE_FPDFAPI_FPDF_OBJECTS_H_ - -#include -#include - -#include "core/include/fxcrt/fx_coordinates.h" -#include "core/include/fxcrt/fx_system.h" - -class CPDF_Array; -class CPDF_Boolean; -class CPDF_Dictionary; -class CPDF_IndirectObjectHolder; -class CPDF_Name; -class CPDF_Null; -class CPDF_Number; -class CPDF_Parser; -class CPDF_Reference; -class CPDF_Stream; -class CPDF_StreamAcc; -class CPDF_String; -class IFX_FileRead; - -class CPDF_Object { - public: - enum Type { - BOOLEAN = 1, - NUMBER, - STRING, - NAME, - ARRAY, - DICTIONARY, - STREAM, - NULLOBJ, - REFERENCE - }; - - virtual Type GetType() const = 0; - FX_DWORD GetObjNum() const { return m_ObjNum; } - FX_DWORD GetGenNum() const { return m_GenNum; } - - virtual CPDF_Object* Clone(FX_BOOL bDirect = FALSE) const = 0; - virtual CPDF_Object* GetDirect() const; - - FX_BOOL IsModified() const { return FALSE; } - void Release(); - - virtual CFX_ByteString GetString() const; - virtual CFX_ByteStringC GetConstString() const; - virtual CFX_WideString GetUnicodeText() const; - virtual FX_FLOAT GetNumber() const; - virtual int GetInteger() const; - virtual CPDF_Dictionary* GetDict() const; - virtual CPDF_Array* GetArray() const; - - virtual void SetString(const CFX_ByteString& str); - - virtual bool IsArray() const; - virtual bool IsBoolean() const; - virtual bool IsDictionary() const; - virtual bool IsName() const; - virtual bool IsNumber() const; - virtual bool IsReference() const; - virtual bool IsStream() const; - virtual bool IsString() const; - - virtual CPDF_Array* AsArray(); - virtual const CPDF_Array* AsArray() const; - virtual CPDF_Boolean* AsBoolean(); - virtual const CPDF_Boolean* AsBoolean() const; - virtual CPDF_Dictionary* AsDictionary(); - virtual const CPDF_Dictionary* AsDictionary() const; - virtual CPDF_Name* AsName(); - virtual const CPDF_Name* AsName() const; - virtual CPDF_Number* AsNumber(); - virtual const CPDF_Number* AsNumber() const; - virtual CPDF_Reference* AsReference(); - virtual const CPDF_Reference* AsReference() const; - virtual CPDF_Stream* AsStream(); - virtual const CPDF_Stream* AsStream() const; - virtual CPDF_String* AsString(); - virtual const CPDF_String* AsString() const; - - protected: - CPDF_Object() : m_ObjNum(0), m_GenNum(0) {} - virtual ~CPDF_Object(); - void Destroy() { delete this; } - - FX_DWORD m_ObjNum; - FX_DWORD m_GenNum; - - friend class CPDF_IndirectObjectHolder; - friend class CPDF_Parser; - - private: - CPDF_Object(const CPDF_Object& src) {} -}; - -class CPDF_Boolean : public CPDF_Object { - public: - CPDF_Boolean() : m_bValue(false) {} - explicit CPDF_Boolean(FX_BOOL value) : m_bValue(value) {} - - // CPDF_Object. - Type GetType() const override; - CPDF_Object* Clone(FX_BOOL bDirect = FALSE) const override; - CFX_ByteString GetString() const override; - int GetInteger() const override; - void SetString(const CFX_ByteString& str) override; - bool IsBoolean() const override; - CPDF_Boolean* AsBoolean() override; - const CPDF_Boolean* AsBoolean() const override; - - protected: - ~CPDF_Boolean() override; - - FX_BOOL m_bValue; -}; - -inline CPDF_Boolean* ToBoolean(CPDF_Object* obj) { - return obj ? obj->AsBoolean() : nullptr; -} - -inline const CPDF_Boolean* ToBoolean(const CPDF_Object* obj) { - return obj ? obj->AsBoolean() : nullptr; -} - -class CPDF_Number : public CPDF_Object { - public: - CPDF_Number() : m_bInteger(TRUE), m_Integer(0) {} - explicit CPDF_Number(int value) : m_bInteger(TRUE), m_Integer(value) {} - explicit CPDF_Number(FX_FLOAT value) : m_bInteger(FALSE), m_Float(value) {} - explicit CPDF_Number(const CFX_ByteStringC& str); - - // CPDF_Object. - Type GetType() const override; - CPDF_Object* Clone(FX_BOOL bDirect = FALSE) const override; - CFX_ByteString GetString() const override; - FX_FLOAT GetNumber() const override; - int GetInteger() const override; - void SetString(const CFX_ByteString& str) override; - bool IsNumber() const override; - CPDF_Number* AsNumber() override; - const CPDF_Number* AsNumber() const override; - - FX_BOOL IsInteger() { return m_bInteger; } - - protected: - ~CPDF_Number() override; - - FX_BOOL m_bInteger; - union { - int m_Integer; - FX_FLOAT m_Float; - }; -}; - -inline CPDF_Number* ToNumber(CPDF_Object* obj) { - return obj ? obj->AsNumber() : nullptr; -} - -inline const CPDF_Number* ToNumber(const CPDF_Object* obj) { - return obj ? obj->AsNumber() : nullptr; -} - -class CPDF_String : public CPDF_Object { - public: - CPDF_String() : m_bHex(FALSE) {} - CPDF_String(const CFX_ByteString& str, FX_BOOL bHex) - : m_String(str), m_bHex(bHex) {} - explicit CPDF_String(const CFX_WideString& str); - - // CPDF_Object. - Type GetType() const override; - CPDF_Object* Clone(FX_BOOL bDirect = FALSE) const override; - CFX_ByteString GetString() const override; - CFX_ByteStringC GetConstString() const override; - CFX_WideString GetUnicodeText() const override; - void SetString(const CFX_ByteString& str) override; - bool IsString() const override; - CPDF_String* AsString() override; - const CPDF_String* AsString() const override; - - FX_BOOL IsHex() const { return m_bHex; } - - protected: - ~CPDF_String() override; - - CFX_ByteString m_String; - FX_BOOL m_bHex; -}; - -inline CPDF_String* ToString(CPDF_Object* obj) { - return obj ? obj->AsString() : nullptr; -} - -inline const CPDF_String* ToString(const CPDF_Object* obj) { - return obj ? obj->AsString() : nullptr; -} - -class CPDF_Name : public CPDF_Object { - public: - explicit CPDF_Name(const CFX_ByteString& str) : m_Name(str) {} - explicit CPDF_Name(const CFX_ByteStringC& str) : m_Name(str) {} - explicit CPDF_Name(const FX_CHAR* str) : m_Name(str) {} - - // CPDF_Object. - Type GetType() const override; - CPDF_Object* Clone(FX_BOOL bDirect = FALSE) const override; - CFX_ByteString GetString() const override; - CFX_ByteStringC GetConstString() const override; - CFX_WideString GetUnicodeText() const override; - void SetString(const CFX_ByteString& str) override; - bool IsName() const override; - CPDF_Name* AsName() override; - const CPDF_Name* AsName() const override; - - protected: - ~CPDF_Name() override; - - CFX_ByteString m_Name; -}; - -inline CPDF_Name* ToName(CPDF_Object* obj) { - return obj ? obj->AsName() : nullptr; -} - -inline const CPDF_Name* ToName(const CPDF_Object* obj) { - return obj ? obj->AsName() : nullptr; -} - -class CPDF_Array : public CPDF_Object { - public: - CPDF_Array(); - - // CPDF_Object. - Type GetType() const override; - CPDF_Object* Clone(FX_BOOL bDirect = FALSE) const override; - CPDF_Array* GetArray() const override; - bool IsArray() const override; - CPDF_Array* AsArray() override; - const CPDF_Array* AsArray() const override; - - FX_DWORD GetCount() const { return m_Objects.GetSize(); } - CPDF_Object* GetElement(FX_DWORD index) const; - CPDF_Object* GetElementValue(FX_DWORD index) const; - CFX_Matrix GetMatrix(); - CFX_FloatRect GetRect(); - CFX_ByteString GetStringAt(FX_DWORD index) const; - CFX_ByteStringC GetConstStringAt(FX_DWORD index) const; - int GetIntegerAt(FX_DWORD index) const; - FX_FLOAT GetNumberAt(FX_DWORD index) const; - CPDF_Dictionary* GetDictAt(FX_DWORD index) const; - CPDF_Stream* GetStreamAt(FX_DWORD index) const; - CPDF_Array* GetArrayAt(FX_DWORD index) const; - FX_FLOAT GetFloatAt(FX_DWORD index) const { return GetNumberAt(index); } - - void SetAt(FX_DWORD index, - CPDF_Object* pObj, - CPDF_IndirectObjectHolder* pObjs = nullptr); - void InsertAt(FX_DWORD index, - CPDF_Object* pObj, - CPDF_IndirectObjectHolder* pObjs = nullptr); - void RemoveAt(FX_DWORD index, int nCount = 1); - - void Add(CPDF_Object* pObj, CPDF_IndirectObjectHolder* pObjs = nullptr); - void AddNumber(FX_FLOAT f); - void AddInteger(int i); - void AddString(const CFX_ByteString& str); - void AddName(const CFX_ByteString& str); - void AddReference(CPDF_IndirectObjectHolder* pDoc, FX_DWORD objnum); - void AddReference(CPDF_IndirectObjectHolder* pDoc, CPDF_Object* obj) { - AddReference(pDoc, obj->GetObjNum()); - } - - protected: - ~CPDF_Array() override; - - CFX_ArrayTemplate m_Objects; -}; - -inline CPDF_Array* ToArray(CPDF_Object* obj) { - return obj ? obj->AsArray() : nullptr; -} - -inline const CPDF_Array* ToArray(const CPDF_Object* obj) { - return obj ? obj->AsArray() : nullptr; -} - -class CPDF_Dictionary : public CPDF_Object { - public: - using iterator = std::map::iterator; - using const_iterator = std::map::const_iterator; - - CPDF_Dictionary(); - - // CPDF_Object. - Type GetType() const override; - CPDF_Object* Clone(FX_BOOL bDirect = FALSE) const override; - CPDF_Dictionary* GetDict() const override; - bool IsDictionary() const override; - CPDF_Dictionary* AsDictionary() override; - const CPDF_Dictionary* AsDictionary() const override; - - size_t GetCount() const { return m_Map.size(); } - CPDF_Object* GetElement(const CFX_ByteStringC& key) const; - CPDF_Object* GetElementValue(const CFX_ByteStringC& key) const; - CFX_ByteString GetStringBy(const CFX_ByteStringC& key) const; - CFX_ByteStringC GetConstStringBy(const CFX_ByteStringC& key) const; - CFX_ByteString GetStringBy(const CFX_ByteStringC& key, - const CFX_ByteStringC& default_str) const; - CFX_ByteStringC GetConstStringBy(const CFX_ByteStringC& key, - const CFX_ByteStringC& default_str) const; - CFX_WideString GetUnicodeTextBy(const CFX_ByteStringC& key) const; - int GetIntegerBy(const CFX_ByteStringC& key) const; - int GetIntegerBy(const CFX_ByteStringC& key, int default_int) const; - FX_BOOL GetBooleanBy(const CFX_ByteStringC& key, - FX_BOOL bDefault = FALSE) const; - FX_FLOAT GetNumberBy(const CFX_ByteStringC& key) const; - CPDF_Dictionary* GetDictBy(const CFX_ByteStringC& key) const; - CPDF_Stream* GetStreamBy(const CFX_ByteStringC& key) const; - CPDF_Array* GetArrayBy(const CFX_ByteStringC& key) const; - CFX_FloatRect GetRectBy(const CFX_ByteStringC& key) const; - CFX_Matrix GetMatrixBy(const CFX_ByteStringC& key) const; - FX_FLOAT GetFloatBy(const CFX_ByteStringC& key) const { - return GetNumberBy(key); - } - - FX_BOOL KeyExist(const CFX_ByteStringC& key) const; - bool IsSignatureDict() const; - - // Set* functions invalidate iterators for the element with the key |key|. - void SetAt(const CFX_ByteStringC& key, CPDF_Object* pObj); - void SetAtName(const CFX_ByteStringC& key, const CFX_ByteString& name); - void SetAtString(const CFX_ByteStringC& key, const CFX_ByteString& str); - void SetAtInteger(const CFX_ByteStringC& key, int i); - void SetAtNumber(const CFX_ByteStringC& key, FX_FLOAT f); - void SetAtReference(const CFX_ByteStringC& key, - CPDF_IndirectObjectHolder* pDoc, - FX_DWORD objnum); - void SetAtReference(const CFX_ByteStringC& key, - CPDF_IndirectObjectHolder* pDoc, - CPDF_Object* obj) { - SetAtReference(key, pDoc, obj->GetObjNum()); - } - void SetAtRect(const CFX_ByteStringC& key, const CFX_FloatRect& rect); - void SetAtMatrix(const CFX_ByteStringC& key, const CFX_Matrix& matrix); - void SetAtBoolean(const CFX_ByteStringC& key, FX_BOOL bValue); - - void AddReference(const CFX_ByteStringC& key, - CPDF_IndirectObjectHolder* pDoc, - FX_DWORD objnum); - - // Invalidates iterators for the element with the key |key|. - void RemoveAt(const CFX_ByteStringC& key); - - // Invalidates iterators for the element with the key |oldkey|. - void ReplaceKey(const CFX_ByteStringC& oldkey, const CFX_ByteStringC& newkey); - - iterator begin() { return m_Map.begin(); } - iterator end() { return m_Map.end(); } - const_iterator begin() const { return m_Map.begin(); } - const_iterator end() const { return m_Map.end(); } - - protected: - ~CPDF_Dictionary() override; - - std::map m_Map; -}; - -inline CPDF_Dictionary* ToDictionary(CPDF_Object* obj) { - return obj ? obj->AsDictionary() : nullptr; -} - -inline const CPDF_Dictionary* ToDictionary(const CPDF_Object* obj) { - return obj ? obj->AsDictionary() : nullptr; -} - -class CPDF_Stream : public CPDF_Object { - public: - CPDF_Stream(uint8_t* pData, FX_DWORD size, CPDF_Dictionary* pDict); - - // CPDF_Object. - Type GetType() const override; - CPDF_Object* Clone(FX_BOOL bDirect = FALSE) const override; - CPDF_Dictionary* GetDict() const override; - CFX_WideString GetUnicodeText() const override; - bool IsStream() const override; - CPDF_Stream* AsStream() override; - const CPDF_Stream* AsStream() const override; - - FX_DWORD GetRawSize() const { return m_dwSize; } - uint8_t* GetRawData() const { return m_pDataBuf; } - - void SetData(const uint8_t* pData, - FX_DWORD size, - FX_BOOL bCompressed, - FX_BOOL bKeepBuf); - - void InitStream(uint8_t* pData, FX_DWORD size, CPDF_Dictionary* pDict); - void InitStreamFromFile(IFX_FileRead* pFile, CPDF_Dictionary* pDict); - - FX_BOOL ReadRawData(FX_FILESIZE start_pos, - uint8_t* pBuf, - FX_DWORD buf_size) const; - - bool IsMemoryBased() const { return m_GenNum == kMemoryBasedGenNum; } - - protected: - static const FX_DWORD kMemoryBasedGenNum = (FX_DWORD)-1; - - ~CPDF_Stream() override; - - void InitStreamInternal(CPDF_Dictionary* pDict); - - CPDF_Dictionary* m_pDict; - FX_DWORD m_dwSize; - FX_DWORD m_GenNum; - - union { - uint8_t* m_pDataBuf; - IFX_FileRead* m_pFile; - }; -}; - -inline CPDF_Stream* ToStream(CPDF_Object* obj) { - return obj ? obj->AsStream() : nullptr; -} - -inline const CPDF_Stream* ToStream(const CPDF_Object* obj) { - return obj ? obj->AsStream() : nullptr; -} - -class CPDF_StreamAcc { - public: - CPDF_StreamAcc(); - ~CPDF_StreamAcc(); - - void LoadAllData(const CPDF_Stream* pStream, - FX_BOOL bRawAccess = FALSE, - FX_DWORD estimated_size = 0, - FX_BOOL bImageAcc = FALSE); - - const CPDF_Stream* GetStream() const { return m_pStream; } - CPDF_Dictionary* GetDict() const { - return m_pStream ? m_pStream->GetDict() : nullptr; - } - const uint8_t* GetData() const; - FX_DWORD GetSize() const; - const CFX_ByteString& GetImageDecoder() const { return m_ImageDecoder; } - const CPDF_Dictionary* GetImageParam() const { return m_pImageParam; } - - uint8_t* DetachData(); - - protected: - uint8_t* m_pData; - FX_DWORD m_dwSize; - FX_BOOL m_bNewBuf; - CFX_ByteString m_ImageDecoder; - CPDF_Dictionary* m_pImageParam; - const CPDF_Stream* m_pStream; - uint8_t* m_pSrcData; -}; - -class CPDF_Null : public CPDF_Object { - public: - CPDF_Null() {} - - // CPDF_Object. - Type GetType() const override; - CPDF_Object* Clone(FX_BOOL bDirect = FALSE) const override; -}; - -class CPDF_Reference : public CPDF_Object { - public: - CPDF_Reference(CPDF_IndirectObjectHolder* pDoc, int objnum) - : m_pObjList(pDoc), m_RefObjNum(objnum) {} - - // CPDF_Object. - Type GetType() const override; - CPDF_Object* Clone(FX_BOOL bDirect = FALSE) const override; - CPDF_Object* GetDirect() const override; - CFX_ByteString GetString() const override; - CFX_ByteStringC GetConstString() const override; - FX_FLOAT GetNumber() const override; - int GetInteger() const override; - CPDF_Dictionary* GetDict() const override; - - // TODO(weili): check whether GetUnicodeText() and GetArray() are needed. - bool IsReference() const override; - CPDF_Reference* AsReference() override; - const CPDF_Reference* AsReference() const override; - - CPDF_IndirectObjectHolder* GetObjList() const { return m_pObjList; } - FX_DWORD GetRefObjNum() const { return m_RefObjNum; } - - void SetRef(CPDF_IndirectObjectHolder* pDoc, FX_DWORD objnum); - - protected: - ~CPDF_Reference() override; - CPDF_Object* SafeGetDirect() const { - CPDF_Object* obj = GetDirect(); - if (!obj || obj->IsReference()) - return nullptr; - return obj; - } - - CPDF_IndirectObjectHolder* m_pObjList; - FX_DWORD m_RefObjNum; -}; - -inline CPDF_Reference* ToReference(CPDF_Object* obj) { - return obj ? obj->AsReference() : nullptr; -} - -inline const CPDF_Reference* ToReference(const CPDF_Object* obj) { - return obj ? obj->AsReference() : nullptr; -} - -class CPDF_IndirectObjectHolder { - public: - using iterator = std::map::iterator; - using const_iterator = std::map::const_iterator; - - explicit CPDF_IndirectObjectHolder(CPDF_Parser* pParser); - ~CPDF_IndirectObjectHolder(); - - CPDF_Object* GetIndirectObject(FX_DWORD objnum); - FX_DWORD AddIndirectObject(CPDF_Object* pObj); - void ReleaseIndirectObject(FX_DWORD objnum); - - // Takes ownership of |pObj|. - FX_BOOL InsertIndirectObject(FX_DWORD objnum, CPDF_Object* pObj); - - FX_DWORD GetLastObjNum() const { return m_LastObjNum; } - iterator begin() { return m_IndirectObjs.begin(); } - const_iterator begin() const { return m_IndirectObjs.begin(); } - iterator end() { return m_IndirectObjs.end(); } - const_iterator end() const { return m_IndirectObjs.end(); } - - protected: - CPDF_Parser* m_pParser; - FX_DWORD m_LastObjNum; - std::map m_IndirectObjs; -}; - -#endif // CORE_INCLUDE_FPDFAPI_FPDF_OBJECTS_H_ diff --git a/core/include/fpdfapi/fpdf_resource.h b/core/include/fpdfapi/fpdf_resource.h index f4dd6331de..06b1245b4b 100644 --- a/core/include/fpdfapi/fpdf_resource.h +++ b/core/include/fpdfapi/fpdf_resource.h @@ -9,7 +9,7 @@ #include -#include "core/include/fpdfapi/fpdf_objects.h" +#include "core/include/fpdfapi/cpdf_stream.h" #include "core/include/fxcrt/fx_system.h" #include "core/include/fxge/fx_font.h" diff --git a/core/include/fpdfdoc/fpdf_doc.h b/core/include/fpdfdoc/fpdf_doc.h index 6ba533160a..60a0939211 100644 --- a/core/include/fpdfdoc/fpdf_doc.h +++ b/core/include/fpdfdoc/fpdf_doc.h @@ -11,6 +11,7 @@ #include #include +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/fpdf_parser_decode.h" #include "core/include/fpdfapi/fpdf_render.h" diff --git a/core/src/fpdfapi/fpdf_edit/fpdf_edit_content.cpp b/core/src/fpdfapi/fpdf_edit/fpdf_edit_content.cpp index cb3f8810ac..a8fddf6cfc 100644 --- a/core/src/fpdfapi/fpdf_edit/fpdf_edit_content.cpp +++ b/core/src/fpdfapi/fpdf_edit/fpdf_edit_content.cpp @@ -4,6 +4,8 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/cpdf_document.h" #include "core/include/fpdfapi/fpdf_module.h" #include "core/include/fpdfapi/fpdf_page.h" diff --git a/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp b/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp index 099a5c208e..9a318eb824 100644 --- a/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp +++ b/core/src/fpdfapi/fpdf_edit/fpdf_edit_create.cpp @@ -8,8 +8,12 @@ #include +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/cpdf_document.h" #include "core/include/fpdfapi/cpdf_parser.h" +#include "core/include/fpdfapi/cpdf_string.h" +#include "core/include/fpdfapi/cpdf_reference.h" #include "core/include/fpdfapi/fpdf_parser_decode.h" #include "core/include/fpdfapi/fpdf_serial.h" #include "core/include/fpdfapi/ipdf_crypto_handler.h" diff --git a/core/src/fpdfapi/fpdf_edit/fpdf_edit_doc.cpp b/core/src/fpdfapi/fpdf_edit/fpdf_edit_doc.cpp index df9c13714d..1d21727f72 100644 --- a/core/src/fpdfapi/fpdf_edit/fpdf_edit_doc.cpp +++ b/core/src/fpdfapi/fpdf_edit/fpdf_edit_doc.cpp @@ -6,7 +6,10 @@ #include +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/cpdf_document.h" +#include "core/include/fpdfapi/cpdf_reference.h" #include "core/include/fpdfapi/fpdf_module.h" #include "core/include/fpdfapi/fpdf_page.h" #include "core/src/fpdfapi/fpdf_page/pageint.h" diff --git a/core/src/fpdfapi/fpdf_edit/fpdf_edit_image.cpp b/core/src/fpdfapi/fpdf_edit/fpdf_edit_image.cpp index 0d2f83682e..47e8367420 100644 --- a/core/src/fpdfapi/fpdf_edit/fpdf_edit_image.cpp +++ b/core/src/fpdfapi/fpdf_edit/fpdf_edit_image.cpp @@ -4,7 +4,11 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_boolean.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/cpdf_document.h" +#include "core/include/fpdfapi/cpdf_string.h" #include "core/include/fpdfapi/fpdf_module.h" #include "core/include/fpdfapi/fpdf_page.h" #include "core/include/fpdfapi/fpdf_render.h" diff --git a/core/src/fpdfapi/fpdf_font/fpdf_font.cpp b/core/src/fpdfapi/fpdf_font/fpdf_font.cpp index 9b4c39aa84..ae6ebdd9f8 100644 --- a/core/src/fpdfapi/fpdf_font/fpdf_font.cpp +++ b/core/src/fpdfapi/fpdf_font/fpdf_font.cpp @@ -6,7 +6,11 @@ #include "core/src/fpdfapi/fpdf_font/font_int.h" +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/cpdf_document.h" +#include "core/include/fpdfapi/cpdf_name.h" +#include "core/include/fpdfapi/cpdf_number.h" #include "core/include/fpdfapi/cpdf_simple_parser.h" #include "core/include/fpdfapi/fpdf_module.h" #include "core/include/fpdfapi/fpdf_page.h" diff --git a/core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp b/core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp index 1ea686e9e0..b86c2778db 100644 --- a/core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp +++ b/core/src/fpdfapi/fpdf_font/fpdf_font_cid.cpp @@ -6,6 +6,8 @@ #include "core/src/fpdfapi/fpdf_font/font_int.h" +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/cpdf_simple_parser.h" #include "core/include/fpdfapi/fpdf_module.h" #include "core/include/fpdfapi/fpdf_page.h" diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page.cpp index 1868d263d3..c7ebc67452 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page.cpp @@ -8,6 +8,8 @@ #include +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/cpdf_document.h" #include "core/include/fpdfapi/fpdf_module.h" #include "core/include/fpdfapi/fpdf_page.h" diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp index 058cbe77d4..9a4c41017d 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp @@ -10,7 +10,10 @@ #include +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/cpdf_document.h" +#include "core/include/fpdfapi/cpdf_string.h" #include "core/include/fpdfapi/fpdf_module.h" #include "core/include/fpdfapi/fpdf_page.h" #include "core/include/fxcodec/fx_codec.h" diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_doc.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_doc.cpp index fbf236d472..7c5a0eab42 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_doc.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_doc.cpp @@ -7,6 +7,8 @@ #include "core/src/fpdfapi/fpdf_page/pageint.h" #include "core/include/fdrm/fx_crypt.h" +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/cpdf_document.h" #include "core/include/fpdfapi/fpdf_module.h" #include "core/include/fpdfapi/fpdf_page.h" diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_func.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_func.cpp index e9ea7eb74f..1d7bf64903 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_func.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_func.cpp @@ -12,6 +12,8 @@ #include #include +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/cpdf_simple_parser.h" #include "core/include/fpdfapi/fpdf_module.h" #include "core/include/fpdfapi/fpdf_page.h" diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_graph_state.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_graph_state.cpp index eff2ff4009..1659cacc33 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_graph_state.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_graph_state.cpp @@ -8,6 +8,8 @@ #include +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/cpdf_document.h" #include "core/include/fpdfapi/fpdf_module.h" #include "core/include/fpdfapi/fpdf_page.h" diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_image.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_image.cpp index 64d3264128..b7e3e5e116 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_image.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_image.cpp @@ -6,6 +6,7 @@ #include "core/src/fpdfapi/fpdf_page/pageint.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/cpdf_document.h" #include "core/include/fpdfapi/fpdf_page.h" #include "core/include/fpdfapi/fpdf_pageobj.h" diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp index 5ef5b48af3..1b6e5ac965 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp @@ -8,7 +8,12 @@ #include +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/cpdf_document.h" +#include "core/include/fpdfapi/cpdf_name.h" +#include "core/include/fpdfapi/cpdf_number.h" +#include "core/include/fpdfapi/cpdf_reference.h" #include "core/include/fpdfapi/fpdf_module.h" #include "core/include/fpdfapi/fpdf_page.h" #include "core/include/fpdfapi/fpdf_parser_decode.h" diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp index 8bfb8b2f6b..feb2cde346 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp @@ -8,7 +8,14 @@ #include +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_boolean.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/cpdf_document.h" +#include "core/include/fpdfapi/cpdf_name.h" +#include "core/include/fpdfapi/cpdf_null.h" +#include "core/include/fpdfapi/cpdf_number.h" +#include "core/include/fpdfapi/cpdf_string.h" #include "core/include/fpdfapi/fpdf_module.h" #include "core/include/fpdfapi/fpdf_page.h" #include "core/include/fpdfapi/fpdf_parser_decode.h" diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_pattern.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_pattern.cpp index 7e8b2e525b..0db1c403a4 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_pattern.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_pattern.cpp @@ -8,6 +8,8 @@ #include +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/cpdf_document.h" #include "core/include/fpdfapi/fpdf_page.h" diff --git a/core/src/fpdfapi/fpdf_parser/cfdf_document.cpp b/core/src/fpdfapi/fpdf_parser/cfdf_document.cpp index c2a6e33604..2d1a04f081 100644 --- a/core/src/fpdfapi/fpdf_parser/cfdf_document.cpp +++ b/core/src/fpdfapi/fpdf_parser/cfdf_document.cpp @@ -6,6 +6,7 @@ #include "core/include/fpdfapi/cfdf_document.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/fpdf_serial.h" #include "core/src/fpdfapi/fpdf_parser/cpdf_syntax_parser.h" diff --git a/core/src/fpdfapi/fpdf_parser/cpdf_array.cpp b/core/src/fpdfapi/fpdf_parser/cpdf_array.cpp new file mode 100644 index 0000000000..3b21a45ae2 --- /dev/null +++ b/core/src/fpdfapi/fpdf_parser/cpdf_array.cpp @@ -0,0 +1,207 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/include/fpdfapi/cpdf_array.h" + +#include "core/include/fpdfapi/cpdf_name.h" +#include "core/include/fpdfapi/cpdf_number.h" +#include "core/include/fpdfapi/cpdf_reference.h" +#include "core/include/fpdfapi/cpdf_stream.h" +#include "core/include/fpdfapi/cpdf_string.h" + +CPDF_Array::CPDF_Array() {} + +CPDF_Array::~CPDF_Array() { + int size = m_Objects.GetSize(); + CPDF_Object** pList = m_Objects.GetData(); + for (int i = 0; i < size; i++) { + if (pList[i]) + pList[i]->Release(); + } +} + +CPDF_Object::Type CPDF_Array::GetType() const { + return ARRAY; +} + +CPDF_Array* CPDF_Array::GetArray() const { + // The method should be made non-const if we want to not be const. + // See bug #234. + return const_cast(this); +} + +bool CPDF_Array::IsArray() const { + return true; +} + +CPDF_Array* CPDF_Array::AsArray() { + return this; +} + +const CPDF_Array* CPDF_Array::AsArray() const { + return this; +} + +CPDF_Object* CPDF_Array::Clone(FX_BOOL bDirect) const { + CPDF_Array* pCopy = new CPDF_Array(); + for (int i = 0; i < GetCount(); i++) { + CPDF_Object* value = m_Objects.GetAt(i); + pCopy->m_Objects.Add(value->Clone(bDirect)); + } + return pCopy; +} + +CFX_FloatRect CPDF_Array::GetRect() { + CFX_FloatRect rect; + if (!IsArray() || m_Objects.GetSize() != 4) + return rect; + + rect.left = GetNumberAt(0); + rect.bottom = GetNumberAt(1); + rect.right = GetNumberAt(2); + rect.top = GetNumberAt(3); + return rect; +} + +CFX_Matrix CPDF_Array::GetMatrix() { + CFX_Matrix matrix; + if (!IsArray() || m_Objects.GetSize() != 6) + return matrix; + + matrix.Set(GetNumberAt(0), GetNumberAt(1), GetNumberAt(2), GetNumberAt(3), + GetNumberAt(4), GetNumberAt(5)); + return matrix; +} + +CPDF_Object* CPDF_Array::GetElement(FX_DWORD i) const { + if (i >= (FX_DWORD)m_Objects.GetSize()) + return nullptr; + return m_Objects.GetAt(i); +} + +CPDF_Object* CPDF_Array::GetElementValue(FX_DWORD i) const { + if (i >= (FX_DWORD)m_Objects.GetSize()) + return nullptr; + return m_Objects.GetAt(i)->GetDirect(); +} + +CFX_ByteString CPDF_Array::GetStringAt(FX_DWORD i) const { + if (i >= (FX_DWORD)m_Objects.GetSize()) + return CFX_ByteString(); + return m_Objects.GetAt(i)->GetString(); +} + +CFX_ByteStringC CPDF_Array::GetConstStringAt(FX_DWORD i) const { + if (i >= (FX_DWORD)m_Objects.GetSize()) + return CFX_ByteStringC(); + return m_Objects.GetAt(i)->GetConstString(); +} + +int CPDF_Array::GetIntegerAt(FX_DWORD i) const { + if (i >= (FX_DWORD)m_Objects.GetSize()) + return 0; + return m_Objects.GetAt(i)->GetInteger(); +} + +FX_FLOAT CPDF_Array::GetNumberAt(FX_DWORD i) const { + if (i >= (FX_DWORD)m_Objects.GetSize()) + return 0; + return m_Objects.GetAt(i)->GetNumber(); +} + +CPDF_Dictionary* CPDF_Array::GetDictAt(FX_DWORD i) const { + CPDF_Object* p = GetElementValue(i); + if (!p) + return NULL; + if (CPDF_Dictionary* pDict = p->AsDictionary()) + return pDict; + if (CPDF_Stream* pStream = p->AsStream()) + return pStream->GetDict(); + return NULL; +} + +CPDF_Stream* CPDF_Array::GetStreamAt(FX_DWORD i) const { + return ToStream(GetElementValue(i)); +} + +CPDF_Array* CPDF_Array::GetArrayAt(FX_DWORD i) const { + return ToArray(GetElementValue(i)); +} + +void CPDF_Array::RemoveAt(FX_DWORD i, int nCount) { + if (i >= (FX_DWORD)m_Objects.GetSize()) + return; + + if (nCount <= 0 || nCount > m_Objects.GetSize() - i) + return; + + for (int j = 0; j < nCount; ++j) { + if (CPDF_Object* p = m_Objects.GetAt(i + j)) + p->Release(); + } + m_Objects.RemoveAt(i, nCount); +} + +void CPDF_Array::SetAt(FX_DWORD i, + CPDF_Object* pObj, + CPDF_IndirectObjectHolder* pObjs) { + ASSERT(IsArray()); + ASSERT(i < (FX_DWORD)m_Objects.GetSize()); + if (i >= (FX_DWORD)m_Objects.GetSize()) + return; + if (CPDF_Object* pOld = m_Objects.GetAt(i)) + pOld->Release(); + if (pObj->GetObjNum()) { + ASSERT(pObjs); + pObj = new CPDF_Reference(pObjs, pObj->GetObjNum()); + } + m_Objects.SetAt(i, pObj); +} + +void CPDF_Array::InsertAt(FX_DWORD index, + CPDF_Object* pObj, + CPDF_IndirectObjectHolder* pObjs) { + if (pObj->GetObjNum()) { + ASSERT(pObjs); + pObj = new CPDF_Reference(pObjs, pObj->GetObjNum()); + } + m_Objects.InsertAt(index, pObj); +} + +void CPDF_Array::Add(CPDF_Object* pObj, CPDF_IndirectObjectHolder* pObjs) { + if (pObj->GetObjNum()) { + ASSERT(pObjs); + pObj = new CPDF_Reference(pObjs, pObj->GetObjNum()); + } + m_Objects.Add(pObj); +} + +void CPDF_Array::AddName(const CFX_ByteString& str) { + ASSERT(IsArray()); + Add(new CPDF_Name(str)); +} + +void CPDF_Array::AddString(const CFX_ByteString& str) { + ASSERT(IsArray()); + Add(new CPDF_String(str, FALSE)); +} + +void CPDF_Array::AddInteger(int i) { + ASSERT(IsArray()); + Add(new CPDF_Number(i)); +} + +void CPDF_Array::AddNumber(FX_FLOAT f) { + ASSERT(IsArray()); + CPDF_Number* pNumber = new CPDF_Number(f); + Add(pNumber); +} + +void CPDF_Array::AddReference(CPDF_IndirectObjectHolder* pDoc, + FX_DWORD objnum) { + ASSERT(IsArray()); + Add(new CPDF_Reference(pDoc, objnum)); +} diff --git a/core/src/fpdfapi/fpdf_parser/cpdf_boolean.cpp b/core/src/fpdfapi/fpdf_parser/cpdf_boolean.cpp new file mode 100644 index 0000000000..75f2203954 --- /dev/null +++ b/core/src/fpdfapi/fpdf_parser/cpdf_boolean.cpp @@ -0,0 +1,45 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/include/fpdfapi/cpdf_boolean.h" + +CPDF_Boolean::CPDF_Boolean() : m_bValue(false) {} + +CPDF_Boolean::CPDF_Boolean(FX_BOOL value) : m_bValue(value) {} + +CPDF_Boolean::~CPDF_Boolean() {} + +CPDF_Object::Type CPDF_Boolean::GetType() const { + return BOOLEAN; +} + +CPDF_Object* CPDF_Boolean::Clone(FX_BOOL bDirect) const { + return new CPDF_Boolean(m_bValue); +} + +CFX_ByteString CPDF_Boolean::GetString() const { + return m_bValue ? "true" : "false"; +} + +int CPDF_Boolean::GetInteger() const { + return m_bValue; +} + +void CPDF_Boolean::SetString(const CFX_ByteString& str) { + m_bValue = (str == "true"); +} + +bool CPDF_Boolean::IsBoolean() const { + return true; +} + +CPDF_Boolean* CPDF_Boolean::AsBoolean() { + return this; +} + +const CPDF_Boolean* CPDF_Boolean::AsBoolean() const { + return this; +} diff --git a/core/src/fpdfapi/fpdf_parser/cpdf_data_avail.cpp b/core/src/fpdfapi/fpdf_parser/cpdf_data_avail.cpp index 19905f871f..5ab15a3b76 100644 --- a/core/src/fpdfapi/fpdf_parser/cpdf_data_avail.cpp +++ b/core/src/fpdfapi/fpdf_parser/cpdf_data_avail.cpp @@ -4,8 +4,13 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/cpdf_document.h" -#include "core/include/fpdfapi/fpdf_objects.h" +#include "core/include/fpdfapi/cpdf_name.h" +#include "core/include/fpdfapi/cpdf_number.h" +#include "core/include/fpdfapi/cpdf_reference.h" +#include "core/include/fpdfapi/cpdf_stream.h" #include "core/include/fxcrt/fx_ext.h" #include "core/include/fxcrt/fx_safe_types.h" #include "core/src/fpdfapi/fpdf_parser/cpdf_data_avail.h" diff --git a/core/src/fpdfapi/fpdf_parser/cpdf_dictionary.cpp b/core/src/fpdfapi/fpdf_parser/cpdf_dictionary.cpp new file mode 100644 index 0000000000..864cd59744 --- /dev/null +++ b/core/src/fpdfapi/fpdf_parser/cpdf_dictionary.cpp @@ -0,0 +1,271 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/include/fpdfapi/cpdf_dictionary.h" + +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_boolean.h" +#include "core/include/fpdfapi/cpdf_name.h" +#include "core/include/fpdfapi/cpdf_number.h" +#include "core/include/fpdfapi/cpdf_reference.h" +#include "core/include/fpdfapi/cpdf_stream.h" +#include "core/include/fpdfapi/cpdf_string.h" +#include "third_party/base/stl_util.h" + +CPDF_Dictionary::CPDF_Dictionary() {} + +CPDF_Dictionary::~CPDF_Dictionary() { + for (const auto& it : m_Map) + it.second->Release(); +} + +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); +} + +bool CPDF_Dictionary::IsDictionary() const { + return true; +} + +CPDF_Dictionary* CPDF_Dictionary::AsDictionary() { + return this; +} + +const CPDF_Dictionary* CPDF_Dictionary::AsDictionary() const { + return this; +} + +CPDF_Object* CPDF_Dictionary::Clone(FX_BOOL bDirect) const { + CPDF_Dictionary* pCopy = new CPDF_Dictionary(); + for (const auto& it : *this) + pCopy->m_Map.insert(std::make_pair(it.first, it.second->Clone(bDirect))); + return pCopy; +} + +CPDF_Object* CPDF_Dictionary::GetElement(const CFX_ByteStringC& key) const { + auto it = m_Map.find(key); + if (it == m_Map.end()) + return nullptr; + return it->second; +} +CPDF_Object* CPDF_Dictionary::GetElementValue( + const CFX_ByteStringC& key) const { + CPDF_Object* p = GetElement(key); + return p ? p->GetDirect() : nullptr; +} + +CFX_ByteString CPDF_Dictionary::GetStringBy(const CFX_ByteStringC& key) const { + CPDF_Object* p = GetElement(key); + return p ? p->GetString() : CFX_ByteString(); +} + +CFX_ByteStringC CPDF_Dictionary::GetConstStringBy( + const CFX_ByteStringC& key) const { + CPDF_Object* p = GetElement(key); + return p ? p->GetConstString() : CFX_ByteStringC(); +} + +CFX_WideString CPDF_Dictionary::GetUnicodeTextBy( + const CFX_ByteStringC& key) const { + CPDF_Object* p = GetElement(key); + if (CPDF_Reference* pRef = ToReference(p)) + p = pRef->GetDirect(); + return p ? p->GetUnicodeText() : CFX_WideString(); +} + +CFX_ByteString CPDF_Dictionary::GetStringBy(const CFX_ByteStringC& key, + const CFX_ByteStringC& def) const { + CPDF_Object* p = GetElement(key); + return p ? p->GetString() : CFX_ByteString(def); +} + +CFX_ByteStringC CPDF_Dictionary::GetConstStringBy( + const CFX_ByteStringC& key, + const CFX_ByteStringC& def) const { + CPDF_Object* p = GetElement(key); + return p ? p->GetConstString() : CFX_ByteStringC(def); +} + +int CPDF_Dictionary::GetIntegerBy(const CFX_ByteStringC& key) const { + CPDF_Object* p = GetElement(key); + return p ? p->GetInteger() : 0; +} + +int CPDF_Dictionary::GetIntegerBy(const CFX_ByteStringC& key, int def) const { + CPDF_Object* p = GetElement(key); + return p ? p->GetInteger() : def; +} + +FX_FLOAT CPDF_Dictionary::GetNumberBy(const CFX_ByteStringC& key) const { + CPDF_Object* p = GetElement(key); + return p ? p->GetNumber() : 0; +} + +FX_BOOL CPDF_Dictionary::GetBooleanBy(const CFX_ByteStringC& key, + FX_BOOL bDefault) const { + CPDF_Object* p = GetElement(key); + return ToBoolean(p) ? p->GetInteger() : bDefault; +} + +CPDF_Dictionary* CPDF_Dictionary::GetDictBy(const CFX_ByteStringC& key) const { + CPDF_Object* p = GetElementValue(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_Array* CPDF_Dictionary::GetArrayBy(const CFX_ByteStringC& key) const { + return ToArray(GetElementValue(key)); +} + +CPDF_Stream* CPDF_Dictionary::GetStreamBy(const CFX_ByteStringC& key) const { + return ToStream(GetElementValue(key)); +} + +CFX_FloatRect CPDF_Dictionary::GetRectBy(const CFX_ByteStringC& key) const { + CFX_FloatRect rect; + CPDF_Array* pArray = GetArrayBy(key); + if (pArray) + rect = pArray->GetRect(); + return rect; +} + +CFX_Matrix CPDF_Dictionary::GetMatrixBy(const CFX_ByteStringC& key) const { + CFX_Matrix matrix; + CPDF_Array* pArray = GetArrayBy(key); + if (pArray) + matrix = pArray->GetMatrix(); + return matrix; +} + +FX_BOOL CPDF_Dictionary::KeyExist(const CFX_ByteStringC& key) const { + return pdfium::ContainsKey(m_Map, key); +} + +bool CPDF_Dictionary::IsSignatureDict() const { + CPDF_Object* pType = GetElementValue("Type"); + if (!pType) + pType = GetElementValue("FT"); + return pType && pType->GetString() == "Sig"; +} + +void CPDF_Dictionary::SetAt(const CFX_ByteStringC& key, CPDF_Object* pObj) { + ASSERT(IsDictionary()); + // Avoid 2 constructions of CFX_ByteString. + CFX_ByteString key_bytestring = key; + auto it = m_Map.find(key_bytestring); + if (it == m_Map.end()) { + if (pObj) + m_Map.insert(std::make_pair(key_bytestring, pObj)); + return; + } + + if (it->second == pObj) + return; + it->second->Release(); + + if (pObj) + it->second = pObj; + else + m_Map.erase(it); +} + +void CPDF_Dictionary::RemoveAt(const CFX_ByteStringC& key) { + auto it = m_Map.find(key); + if (it == m_Map.end()) + return; + + it->second->Release(); + m_Map.erase(it); +} + +void CPDF_Dictionary::ReplaceKey(const CFX_ByteStringC& oldkey, + const CFX_ByteStringC& newkey) { + auto old_it = m_Map.find(oldkey); + if (old_it == m_Map.end()) + return; + + // Avoid 2 constructions of CFX_ByteString. + CFX_ByteString newkey_bytestring = newkey; + auto new_it = m_Map.find(newkey_bytestring); + if (new_it == old_it) + return; + + if (new_it != m_Map.end()) { + new_it->second->Release(); + new_it->second = old_it->second; + } else { + m_Map.insert(std::make_pair(newkey_bytestring, old_it->second)); + } + m_Map.erase(old_it); +} + +void CPDF_Dictionary::SetAtInteger(const CFX_ByteStringC& key, int i) { + SetAt(key, new CPDF_Number(i)); +} + +void CPDF_Dictionary::SetAtName(const CFX_ByteStringC& key, + const CFX_ByteString& name) { + SetAt(key, new CPDF_Name(name)); +} + +void CPDF_Dictionary::SetAtString(const CFX_ByteStringC& key, + const CFX_ByteString& str) { + SetAt(key, new CPDF_String(str, FALSE)); +} + +void CPDF_Dictionary::SetAtReference(const CFX_ByteStringC& key, + CPDF_IndirectObjectHolder* pDoc, + FX_DWORD objnum) { + SetAt(key, new CPDF_Reference(pDoc, objnum)); +} + +void CPDF_Dictionary::AddReference(const CFX_ByteStringC& key, + CPDF_IndirectObjectHolder* pDoc, + FX_DWORD objnum) { + SetAt(key, new CPDF_Reference(pDoc, objnum)); +} + +void CPDF_Dictionary::SetAtNumber(const CFX_ByteStringC& key, FX_FLOAT f) { + CPDF_Number* pNumber = new CPDF_Number(f); + SetAt(key, pNumber); +} + +void CPDF_Dictionary::SetAtBoolean(const CFX_ByteStringC& key, FX_BOOL bValue) { + SetAt(key, new CPDF_Boolean(bValue)); +} + +void CPDF_Dictionary::SetAtRect(const CFX_ByteStringC& key, + const CFX_FloatRect& rect) { + CPDF_Array* pArray = new CPDF_Array; + pArray->AddNumber(rect.left); + pArray->AddNumber(rect.bottom); + pArray->AddNumber(rect.right); + pArray->AddNumber(rect.top); + SetAt(key, pArray); +} + +void CPDF_Dictionary::SetAtMatrix(const CFX_ByteStringC& key, + const CFX_Matrix& matrix) { + CPDF_Array* pArray = new CPDF_Array; + pArray->AddNumber(matrix.a); + pArray->AddNumber(matrix.b); + pArray->AddNumber(matrix.c); + pArray->AddNumber(matrix.d); + pArray->AddNumber(matrix.e); + pArray->AddNumber(matrix.f); + SetAt(key, pArray); +} diff --git a/core/src/fpdfapi/fpdf_parser/cpdf_document.cpp b/core/src/fpdfapi/fpdf_parser/cpdf_document.cpp index 394b17334e..b5789410e5 100644 --- a/core/src/fpdfapi/fpdf_parser/cpdf_document.cpp +++ b/core/src/fpdfapi/fpdf_parser/cpdf_document.cpp @@ -8,7 +8,10 @@ #include +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/cpdf_parser.h" +#include "core/include/fpdfapi/cpdf_reference.h" #include "core/include/fpdfapi/fpdf_module.h" #include "core/include/fxge/fx_font.h" #include "core/src/fpdfapi/fpdf_render/render_int.h" diff --git a/core/src/fpdfapi/fpdf_parser/cpdf_hint_tables.cpp b/core/src/fpdfapi/fpdf_parser/cpdf_hint_tables.cpp index 09148ba76a..6256f4c526 100644 --- a/core/src/fpdfapi/fpdf_parser/cpdf_hint_tables.cpp +++ b/core/src/fpdfapi/fpdf_parser/cpdf_hint_tables.cpp @@ -7,7 +7,9 @@ #include "core/src/fpdfapi/fpdf_parser/cpdf_hint_tables.h" #include "core/include/fxcrt/fx_safe_types.h" -#include "core/include/fpdfapi/fpdf_objects.h" +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" +#include "core/include/fpdfapi/cpdf_stream.h" #include "core/src/fpdfapi/fpdf_parser/cpdf_data_avail.h" namespace { diff --git a/core/src/fpdfapi/fpdf_parser/cpdf_indirect_object_holder.cpp b/core/src/fpdfapi/fpdf_parser/cpdf_indirect_object_holder.cpp new file mode 100644 index 0000000000..b1606f9857 --- /dev/null +++ b/core/src/fpdfapi/fpdf_parser/cpdf_indirect_object_holder.cpp @@ -0,0 +1,80 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/include/fpdfapi/cpdf_indirect_object_holder.h" +#include "core/include/fpdfapi/cpdf_object.h" +#include "core/include/fpdfapi/cpdf_parser.h" + +CPDF_IndirectObjectHolder::CPDF_IndirectObjectHolder(CPDF_Parser* pParser) + : m_pParser(pParser), m_LastObjNum(0) { + if (pParser) + m_LastObjNum = m_pParser->GetLastObjNum(); +} + +CPDF_IndirectObjectHolder::~CPDF_IndirectObjectHolder() { + for (const auto& pair : m_IndirectObjs) + pair.second->Destroy(); +} + +CPDF_Object* CPDF_IndirectObjectHolder::GetIndirectObject(FX_DWORD objnum) { + if (objnum == 0) + return nullptr; + + auto it = m_IndirectObjs.find(objnum); + if (it != m_IndirectObjs.end()) + return it->second->GetObjNum() != -1 ? it->second : nullptr; + + if (!m_pParser) + return nullptr; + + CPDF_Object* pObj = m_pParser->ParseIndirectObject(this, objnum); + if (!pObj) + return nullptr; + + pObj->m_ObjNum = objnum; + m_LastObjNum = std::max(m_LastObjNum, objnum); + if (m_IndirectObjs[objnum]) + m_IndirectObjs[objnum]->Destroy(); + + m_IndirectObjs[objnum] = pObj; + return pObj; +} + +FX_DWORD CPDF_IndirectObjectHolder::AddIndirectObject(CPDF_Object* pObj) { + if (pObj->m_ObjNum) + return pObj->m_ObjNum; + + m_LastObjNum++; + m_IndirectObjs[m_LastObjNum] = pObj; + pObj->m_ObjNum = m_LastObjNum; + return m_LastObjNum; +} + +void CPDF_IndirectObjectHolder::ReleaseIndirectObject(FX_DWORD objnum) { + auto it = m_IndirectObjs.find(objnum); + if (it == m_IndirectObjs.end() || it->second->GetObjNum() == -1) + return; + it->second->Destroy(); + m_IndirectObjs.erase(it); +} + +FX_BOOL CPDF_IndirectObjectHolder::InsertIndirectObject(FX_DWORD objnum, + CPDF_Object* pObj) { + if (!objnum || !pObj) + return FALSE; + auto it = m_IndirectObjs.find(objnum); + if (it != m_IndirectObjs.end()) { + if (pObj->GetGenNum() <= it->second->GetGenNum()) { + pObj->Destroy(); + return FALSE; + } + it->second->Destroy(); + } + pObj->m_ObjNum = objnum; + m_IndirectObjs[objnum] = pObj; + m_LastObjNum = std::max(m_LastObjNum, objnum); + return TRUE; +} diff --git a/core/src/fpdfapi/fpdf_parser/cpdf_name.cpp b/core/src/fpdfapi/fpdf_parser/cpdf_name.cpp new file mode 100644 index 0000000000..03d523db19 --- /dev/null +++ b/core/src/fpdfapi/fpdf_parser/cpdf_name.cpp @@ -0,0 +1,53 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/include/fpdfapi/cpdf_name.h" + +#include "core/include/fpdfapi/fpdf_parser_decode.h" + +CPDF_Name::CPDF_Name(const CFX_ByteString& str) : m_Name(str) {} + +CPDF_Name::CPDF_Name(const CFX_ByteStringC& str) : m_Name(str) {} + +CPDF_Name::CPDF_Name(const FX_CHAR* str) : m_Name(str) {} + +CPDF_Name::~CPDF_Name() {} + +CPDF_Object::Type CPDF_Name::GetType() const { + return NAME; +} + +CPDF_Object* CPDF_Name::Clone(FX_BOOL bDirect) const { + return new CPDF_Name(m_Name); +} + +CFX_ByteString CPDF_Name::GetString() const { + return m_Name; +} + +CFX_ByteStringC CPDF_Name::GetConstString() const { + return CFX_ByteStringC(m_Name); +} + +void CPDF_Name::SetString(const CFX_ByteString& str) { + m_Name = str; +} + +bool CPDF_Name::IsName() const { + return true; +} + +CPDF_Name* CPDF_Name::AsName() { + return this; +} + +const CPDF_Name* CPDF_Name::AsName() const { + return this; +} + +CFX_WideString CPDF_Name::GetUnicodeText() const { + return PDF_DecodeText(m_Name); +} diff --git a/core/src/fpdfapi/fpdf_parser/cpdf_null.cpp b/core/src/fpdfapi/fpdf_parser/cpdf_null.cpp new file mode 100644 index 0000000000..c18449d08f --- /dev/null +++ b/core/src/fpdfapi/fpdf_parser/cpdf_null.cpp @@ -0,0 +1,17 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/include/fpdfapi/cpdf_null.h" + +CPDF_Null::CPDF_Null() {} + +CPDF_Object::Type CPDF_Null::GetType() const { + return NULLOBJ; +} + +CPDF_Object* CPDF_Null::Clone(FX_BOOL bDirect) const { + return new CPDF_Null; +} diff --git a/core/src/fpdfapi/fpdf_parser/cpdf_number.cpp b/core/src/fpdfapi/fpdf_parser/cpdf_number.cpp new file mode 100644 index 0000000000..ddc7aa11a2 --- /dev/null +++ b/core/src/fpdfapi/fpdf_parser/cpdf_number.cpp @@ -0,0 +1,56 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/include/fpdfapi/cpdf_number.h" + +CPDF_Number::CPDF_Number() : m_bInteger(TRUE), m_Integer(0) {} + +CPDF_Number::CPDF_Number(int value) : m_bInteger(TRUE), m_Integer(value) {} + +CPDF_Number::CPDF_Number(FX_FLOAT value) : m_bInteger(FALSE), m_Float(value) {} + +CPDF_Number::CPDF_Number(const CFX_ByteStringC& str) { + FX_atonum(str, m_bInteger, &m_Integer); +} + +CPDF_Number::~CPDF_Number() {} + +CPDF_Object::Type CPDF_Number::GetType() const { + return NUMBER; +} + +CPDF_Object* CPDF_Number::Clone(FX_BOOL bDirect) const { + return m_bInteger ? new CPDF_Number(m_Integer) : new CPDF_Number(m_Float); +} + +FX_FLOAT CPDF_Number::GetNumber() const { + return m_bInteger ? static_cast(m_Integer) : m_Float; +} + +int CPDF_Number::GetInteger() const { + return m_bInteger ? m_Integer : static_cast(m_Float); +} + +bool CPDF_Number::IsNumber() const { + return true; +} + +CPDF_Number* CPDF_Number::AsNumber() { + return this; +} + +const CPDF_Number* CPDF_Number::AsNumber() const { + return this; +} + +void CPDF_Number::SetString(const CFX_ByteString& str) { + FX_atonum(str, m_bInteger, &m_Integer); +} + +CFX_ByteString CPDF_Number::GetString() const { + return m_bInteger ? CFX_ByteString::FormatInteger(m_Integer, FXFORMAT_SIGNED) + : CFX_ByteString::FormatFloat(m_Float); +} diff --git a/core/src/fpdfapi/fpdf_parser/cpdf_object.cpp b/core/src/fpdfapi/fpdf_parser/cpdf_object.cpp new file mode 100644 index 0000000000..208bca8e6c --- /dev/null +++ b/core/src/fpdfapi/fpdf_parser/cpdf_object.cpp @@ -0,0 +1,158 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/include/fpdfapi/cpdf_object.h" + +#include + +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" +#include "core/include/fpdfapi/cpdf_indirect_object_holder.h" +#include "core/include/fpdfapi/cpdf_parser.h" +#include "core/include/fpdfapi/fpdf_parser_decode.h" +#include "core/include/fxcrt/fx_string.h" +#include "third_party/base/stl_util.h" + +CPDF_Object::~CPDF_Object() {} + +CPDF_Object* CPDF_Object::GetDirect() const { + return const_cast(this); +} + +void CPDF_Object::Release() { + if (m_ObjNum) + return; + + Destroy(); +} + +CFX_ByteString CPDF_Object::GetString() const { + return CFX_ByteString(); +} + +CFX_ByteStringC CPDF_Object::GetConstString() const { + return CFX_ByteStringC(); +} + +CFX_WideString CPDF_Object::GetUnicodeText() const { + return CFX_WideString(); +} + +FX_FLOAT CPDF_Object::GetNumber() const { + return 0; +} + +int CPDF_Object::GetInteger() const { + return 0; +} + +CPDF_Dictionary* CPDF_Object::GetDict() const { + return nullptr; +} + +CPDF_Array* CPDF_Object::GetArray() const { + return nullptr; +} + +void CPDF_Object::SetString(const CFX_ByteString& str) { + ASSERT(FALSE); +} + +bool CPDF_Object::IsArray() const { + return false; +} + +bool CPDF_Object::IsBoolean() const { + return false; +} + +bool CPDF_Object::IsDictionary() const { + return false; +} + +bool CPDF_Object::IsName() const { + return false; +} + +bool CPDF_Object::IsNumber() const { + return false; +} + +bool CPDF_Object::IsReference() const { + return false; +} + +bool CPDF_Object::IsStream() const { + return false; +} + +bool CPDF_Object::IsString() const { + return false; +} + +CPDF_Array* CPDF_Object::AsArray() { + return nullptr; +} + +const CPDF_Array* CPDF_Object::AsArray() const { + return nullptr; +} + +CPDF_Boolean* CPDF_Object::AsBoolean() { + return nullptr; +} + +const CPDF_Boolean* CPDF_Object::AsBoolean() const { + return nullptr; +} + +CPDF_Dictionary* CPDF_Object::AsDictionary() { + return nullptr; +} + +const CPDF_Dictionary* CPDF_Object::AsDictionary() const { + return nullptr; +} + +CPDF_Name* CPDF_Object::AsName() { + return nullptr; +} + +const CPDF_Name* CPDF_Object::AsName() const { + return nullptr; +} + +CPDF_Number* CPDF_Object::AsNumber() { + return nullptr; +} + +const CPDF_Number* CPDF_Object::AsNumber() const { + return nullptr; +} + +CPDF_Reference* CPDF_Object::AsReference() { + return nullptr; +} + +const CPDF_Reference* CPDF_Object::AsReference() const { + return nullptr; +} + +CPDF_Stream* CPDF_Object::AsStream() { + return nullptr; +} + +const CPDF_Stream* CPDF_Object::AsStream() const { + return nullptr; +} + +CPDF_String* CPDF_Object::AsString() { + return nullptr; +} + +const CPDF_String* CPDF_Object::AsString() const { + return nullptr; +} diff --git a/core/src/fpdfapi/fpdf_parser/cpdf_object_unittest.cpp b/core/src/fpdfapi/fpdf_parser/cpdf_object_unittest.cpp new file mode 100644 index 0000000000..87ef34fd6a --- /dev/null +++ b/core/src/fpdfapi/fpdf_parser/cpdf_object_unittest.cpp @@ -0,0 +1,776 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_boolean.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" +#include "core/include/fpdfapi/cpdf_name.h" +#include "core/include/fpdfapi/cpdf_null.h" +#include "core/include/fpdfapi/cpdf_number.h" +#include "core/include/fpdfapi/cpdf_reference.h" +#include "core/include/fpdfapi/cpdf_string.h" +#include "core/include/fpdfapi/cpdf_stream.h" + +#include +#include +#include + +#include "core/include/fpdfapi/cpdf_indirect_object_holder.h" +#include "core/include/fxcrt/fx_basic.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +using ScopedArray = std::unique_ptr>; + +void TestArrayAccessors(const CPDF_Array* arr, + size_t index, + const char* str_val, + const char* const_str_val, + int int_val, + float float_val, + CPDF_Array* arr_val, + CPDF_Dictionary* dict_val, + CPDF_Stream* stream_val) { + EXPECT_STREQ(str_val, arr->GetStringAt(index).c_str()); + EXPECT_STREQ(const_str_val, arr->GetConstStringAt(index).GetCStr()); + EXPECT_EQ(int_val, arr->GetIntegerAt(index)); + EXPECT_EQ(float_val, arr->GetNumberAt(index)); + EXPECT_EQ(float_val, arr->GetFloatAt(index)); + EXPECT_EQ(arr_val, arr->GetArrayAt(index)); + EXPECT_EQ(dict_val, arr->GetDictAt(index)); + EXPECT_EQ(stream_val, arr->GetStreamAt(index)); +} + +} // namespace + +class PDFObjectsTest : public testing::Test { + public: + void SetUp() override { + // Initialize different kinds of objects. + // Boolean objects. + CPDF_Boolean* boolean_false_obj = new CPDF_Boolean(false); + CPDF_Boolean* boolean_true_obj = new CPDF_Boolean(true); + // Number objects. + CPDF_Number* number_int_obj = new CPDF_Number(1245); + CPDF_Number* number_float_obj = new CPDF_Number(9.00345f); + // String objects. + CPDF_String* str_reg_obj = new CPDF_String(L"A simple test"); + CPDF_String* str_spec_obj = new CPDF_String(L"\t\n"); + // Name object. + CPDF_Name* name_obj = new CPDF_Name("space"); + // Array object. + m_ArrayObj = new CPDF_Array; + m_ArrayObj->InsertAt(0, new CPDF_Number(8902)); + m_ArrayObj->InsertAt(1, new CPDF_Name("address")); + // Dictionary object. + m_DictObj = new CPDF_Dictionary; + m_DictObj->SetAt("bool", new CPDF_Boolean(false)); + m_DictObj->SetAt("num", new CPDF_Number(0.23f)); + // Stream object. + const char content[] = "abcdefghijklmnopqrstuvwxyz"; + size_t buf_len = FX_ArraySize(content); + uint8_t* buf = reinterpret_cast(malloc(buf_len)); + memcpy(buf, content, buf_len); + m_StreamDictObj = new CPDF_Dictionary; + m_StreamDictObj->SetAt("key1", new CPDF_String(L" test dict")); + m_StreamDictObj->SetAt("key2", new CPDF_Number(-1)); + CPDF_Stream* stream_obj = new CPDF_Stream(buf, buf_len, m_StreamDictObj); + // Null Object. + CPDF_Null* null_obj = new CPDF_Null; + // All direct objects. + CPDF_Object* objs[] = {boolean_false_obj, boolean_true_obj, number_int_obj, + number_float_obj, str_reg_obj, str_spec_obj, + name_obj, m_ArrayObj, m_DictObj, + stream_obj, null_obj}; + m_DirectObjTypes = { + CPDF_Object::BOOLEAN, CPDF_Object::BOOLEAN, CPDF_Object::NUMBER, + CPDF_Object::NUMBER, CPDF_Object::STRING, CPDF_Object::STRING, + CPDF_Object::NAME, CPDF_Object::ARRAY, CPDF_Object::DICTIONARY, + CPDF_Object::STREAM, CPDF_Object::NULLOBJ}; + for (size_t i = 0; i < FX_ArraySize(objs); ++i) + m_DirectObjs.emplace_back(objs[i]); + + // Indirect references to indirect objects. + m_ObjHolder.reset(new CPDF_IndirectObjectHolder(nullptr)); + m_IndirectObjs = {boolean_true_obj, number_int_obj, str_spec_obj, name_obj, + m_ArrayObj, m_DictObj, stream_obj}; + for (size_t i = 0; i < m_IndirectObjs.size(); ++i) { + m_ObjHolder->AddIndirectObject(m_IndirectObjs[i]); + m_RefObjs.emplace_back(new CPDF_Reference( + m_ObjHolder.get(), m_IndirectObjs[i]->GetObjNum())); + } + } + + bool Equal(CPDF_Object* obj1, CPDF_Object* obj2) { + if (obj1 == obj2) + return true; + if (!obj1 || !obj2 || obj1->GetType() != obj2->GetType()) + return false; + switch (obj1->GetType()) { + case CPDF_Object::BOOLEAN: + return obj1->GetInteger() == obj2->GetInteger(); + case CPDF_Object::NUMBER: + return obj1->AsNumber()->IsInteger() == obj2->AsNumber()->IsInteger() && + obj1->GetInteger() == obj2->GetInteger(); + case CPDF_Object::STRING: + case CPDF_Object::NAME: + return obj1->GetString() == obj2->GetString(); + case CPDF_Object::ARRAY: { + const CPDF_Array* array1 = obj1->AsArray(); + const CPDF_Array* array2 = obj2->AsArray(); + if (array1->GetCount() != array2->GetCount()) + return false; + for (size_t i = 0; i < array1->GetCount(); ++i) { + if (!Equal(array1->GetElement(i), array2->GetElement(i))) + return false; + } + return true; + } + case CPDF_Object::DICTIONARY: { + const CPDF_Dictionary* dict1 = obj1->AsDictionary(); + const CPDF_Dictionary* dict2 = obj2->AsDictionary(); + if (dict1->GetCount() != dict2->GetCount()) + return false; + for (CPDF_Dictionary::const_iterator it = dict1->begin(); + it != dict1->end(); ++it) { + if (!Equal(it->second, dict2->GetElement(it->first))) + return false; + } + return true; + } + case CPDF_Object::NULLOBJ: + return true; + case CPDF_Object::STREAM: { + const CPDF_Stream* stream1 = obj1->AsStream(); + const CPDF_Stream* stream2 = obj2->AsStream(); + if (!stream1->GetDict() && !stream2->GetDict()) + return true; + // Compare dictionaries. + if (!Equal(stream1->GetDict(), stream2->GetDict())) + return false; + // Compare sizes. + if (stream1->GetRawSize() != stream2->GetRawSize()) + return false; + // Compare contents. + // Since this function is used for testing Clone(), only memory based + // streams need to be handled. + if (!stream1->IsMemoryBased() || !stream2->IsMemoryBased()) + return false; + return FXSYS_memcmp(stream1->GetRawData(), stream2->GetRawData(), + stream1->GetRawSize()) == 0; + } + case CPDF_Object::REFERENCE: + return obj1->AsReference()->GetRefObjNum() == + obj2->AsReference()->GetRefObjNum(); + } + return false; + } + + protected: + using ScopedObj = std::unique_ptr>; + + // m_ObjHolder needs to be declared first and destructed last since it also + // refers to some objects in m_DirectObjs. + std::unique_ptr m_ObjHolder; + std::vector m_DirectObjs; + std::vector m_DirectObjTypes; + std::vector m_RefObjs; + CPDF_Dictionary* m_DictObj; + CPDF_Dictionary* m_StreamDictObj; + CPDF_Array* m_ArrayObj; + std::vector m_IndirectObjs; +}; + +TEST_F(PDFObjectsTest, GetString) { + const char* direct_obj_results[] = { + "false", "true", "1245", "9.00345", "A simple test", "\t\n", "space", + "", "", "", ""}; + // Check for direct objects. + for (size_t i = 0; i < m_DirectObjs.size(); ++i) + EXPECT_STREQ(direct_obj_results[i], m_DirectObjs[i]->GetString().c_str()); + + // Check indirect references. + const char* indirect_obj_results[] = {"true", "1245", "\t\n", "space", + "", "", ""}; + for (size_t i = 0; i < m_RefObjs.size(); ++i) { + EXPECT_STREQ(indirect_obj_results[i], m_RefObjs[i]->GetString().c_str()); + } +} + +TEST_F(PDFObjectsTest, GetConstString) { + const char* direct_obj_results[] = { + nullptr, nullptr, nullptr, nullptr, "A simple test", "\t\n", + "space", nullptr, nullptr, nullptr, nullptr}; + // Check for direct objects. + for (size_t i = 0; i < m_DirectObjs.size(); ++i) { + if (!direct_obj_results[i]) { + EXPECT_EQ(direct_obj_results[i], + m_DirectObjs[i]->GetConstString().GetCStr()); + } else { + EXPECT_STREQ(direct_obj_results[i], + m_DirectObjs[i]->GetConstString().GetCStr()); + } + } + // Check indirect references. + const char* indirect_obj_results[] = {nullptr, nullptr, "\t\n", "space", + nullptr, nullptr, nullptr}; + for (size_t i = 0; i < m_RefObjs.size(); ++i) { + if (!indirect_obj_results[i]) { + EXPECT_EQ(nullptr, m_RefObjs[i]->GetConstString().GetCStr()); + } else { + EXPECT_STREQ(indirect_obj_results[i], + m_RefObjs[i]->GetConstString().GetCStr()); + } + } +} + +TEST_F(PDFObjectsTest, GetUnicodeText) { + const wchar_t* direct_obj_results[] = { + L"", L"", L"", L"", L"A simple test", + L"\t\n", L"space", L"", L"", L"abcdefghijklmnopqrstuvwxyz", + L""}; + // Check for direct objects. + for (size_t i = 0; i < m_DirectObjs.size(); ++i) + EXPECT_STREQ(direct_obj_results[i], + m_DirectObjs[i]->GetUnicodeText().c_str()); + + // Check indirect references. + for (const auto& it : m_RefObjs) + EXPECT_STREQ(L"", it->GetUnicodeText().c_str()); +} + +TEST_F(PDFObjectsTest, GetNumber) { + const FX_FLOAT direct_obj_results[] = {0, 0, 1245, 9.00345f, 0, 0, + 0, 0, 0, 0, 0}; + // Check for direct objects. + for (size_t i = 0; i < m_DirectObjs.size(); ++i) + EXPECT_EQ(direct_obj_results[i], m_DirectObjs[i]->GetNumber()); + + // Check indirect references. + const FX_FLOAT indirect_obj_results[] = {0, 1245, 0, 0, 0, 0, 0}; + for (size_t i = 0; i < m_RefObjs.size(); ++i) + EXPECT_EQ(indirect_obj_results[i], m_RefObjs[i]->GetNumber()); +} + +TEST_F(PDFObjectsTest, GetInteger) { + const int direct_obj_results[] = {0, 1, 1245, 9, 0, 0, 0, 0, 0, 0, 0}; + // Check for direct objects. + for (size_t i = 0; i < m_DirectObjs.size(); ++i) + EXPECT_EQ(direct_obj_results[i], m_DirectObjs[i]->GetInteger()); + + // Check indirect references. + const int indirect_obj_results[] = {1, 1245, 0, 0, 0, 0, 0}; + for (size_t i = 0; i < m_RefObjs.size(); ++i) + EXPECT_EQ(indirect_obj_results[i], m_RefObjs[i]->GetInteger()); +} + +TEST_F(PDFObjectsTest, GetDict) { + const CPDF_Dictionary* direct_obj_results[] = { + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, m_DictObj, m_StreamDictObj, nullptr}; + // Check for direct objects. + for (size_t i = 0; i < m_DirectObjs.size(); ++i) + EXPECT_EQ(direct_obj_results[i], m_DirectObjs[i]->GetDict()); + + // Check indirect references. + const CPDF_Dictionary* indirect_obj_results[] = { + nullptr, nullptr, nullptr, nullptr, nullptr, m_DictObj, m_StreamDictObj}; + for (size_t i = 0; i < m_RefObjs.size(); ++i) + EXPECT_EQ(indirect_obj_results[i], m_RefObjs[i]->GetDict()); +} + +TEST_F(PDFObjectsTest, GetArray) { + const CPDF_Array* direct_obj_results[] = { + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, m_ArrayObj, nullptr, nullptr, nullptr}; + // Check for direct objects. + for (size_t i = 0; i < m_DirectObjs.size(); ++i) + EXPECT_EQ(direct_obj_results[i], m_DirectObjs[i]->GetArray()); + + // Check indirect references. + for (const auto& it : m_RefObjs) + EXPECT_EQ(nullptr, it->GetArray()); +} + +TEST_F(PDFObjectsTest, Clone) { + // Check for direct objects. + for (size_t i = 0; i < m_DirectObjs.size(); ++i) { + ScopedObj obj(m_DirectObjs[i]->Clone()); + EXPECT_TRUE(Equal(m_DirectObjs[i].get(), obj.get())); + } + + // Check indirect references. + for (const auto& it : m_RefObjs) { + ScopedObj obj(it->Clone()); + EXPECT_TRUE(Equal(it.get(), obj.get())); + } +} + +TEST_F(PDFObjectsTest, GetType) { + // Check for direct objects. + for (size_t i = 0; i < m_DirectObjs.size(); ++i) + EXPECT_EQ(m_DirectObjTypes[i], m_DirectObjs[i]->GetType()); + + // Check indirect references. + for (const auto& it : m_RefObjs) + EXPECT_EQ(CPDF_Object::REFERENCE, it->GetType()); +} + +TEST_F(PDFObjectsTest, GetDirect) { + // Check for direct objects. + for (size_t i = 0; i < m_DirectObjs.size(); ++i) + EXPECT_EQ(m_DirectObjs[i].get(), m_DirectObjs[i]->GetDirect()); + + // Check indirect references. + for (size_t i = 0; i < m_RefObjs.size(); ++i) + EXPECT_EQ(m_IndirectObjs[i], m_RefObjs[i]->GetDirect()); +} + +TEST_F(PDFObjectsTest, SetString) { + // Check for direct objects. + const char* const set_values[] = {"true", "fake", "3.125f", "097", + "changed", "", "NewName"}; + const char* expected[] = {"true", "false", "3.125", "97", + "changed", "", "NewName"}; + for (size_t i = 0; i < FX_ArraySize(set_values); ++i) { + m_DirectObjs[i]->SetString(set_values[i]); + EXPECT_STREQ(expected[i], m_DirectObjs[i]->GetString().c_str()); + } +} + +TEST_F(PDFObjectsTest, IsTypeAndAsType) { + // Check for direct objects. + for (size_t i = 0; i < m_DirectObjs.size(); ++i) { + if (m_DirectObjTypes[i] == CPDF_Object::ARRAY) { + EXPECT_TRUE(m_DirectObjs[i]->IsArray()); + EXPECT_EQ(m_DirectObjs[i].get(), m_DirectObjs[i]->AsArray()); + } else { + EXPECT_FALSE(m_DirectObjs[i]->IsArray()); + EXPECT_EQ(nullptr, m_DirectObjs[i]->AsArray()); + } + + if (m_DirectObjTypes[i] == CPDF_Object::BOOLEAN) { + EXPECT_TRUE(m_DirectObjs[i]->IsBoolean()); + EXPECT_EQ(m_DirectObjs[i].get(), m_DirectObjs[i]->AsBoolean()); + } else { + EXPECT_FALSE(m_DirectObjs[i]->IsBoolean()); + EXPECT_EQ(nullptr, m_DirectObjs[i]->AsBoolean()); + } + + if (m_DirectObjTypes[i] == CPDF_Object::NAME) { + EXPECT_TRUE(m_DirectObjs[i]->IsName()); + EXPECT_EQ(m_DirectObjs[i].get(), m_DirectObjs[i]->AsName()); + } else { + EXPECT_FALSE(m_DirectObjs[i]->IsName()); + EXPECT_EQ(nullptr, m_DirectObjs[i]->AsName()); + } + + if (m_DirectObjTypes[i] == CPDF_Object::NUMBER) { + EXPECT_TRUE(m_DirectObjs[i]->IsNumber()); + EXPECT_EQ(m_DirectObjs[i].get(), m_DirectObjs[i]->AsNumber()); + } else { + EXPECT_FALSE(m_DirectObjs[i]->IsNumber()); + EXPECT_EQ(nullptr, m_DirectObjs[i]->AsNumber()); + } + + if (m_DirectObjTypes[i] == CPDF_Object::STRING) { + EXPECT_TRUE(m_DirectObjs[i]->IsString()); + EXPECT_EQ(m_DirectObjs[i].get(), m_DirectObjs[i]->AsString()); + } else { + EXPECT_FALSE(m_DirectObjs[i]->IsString()); + EXPECT_EQ(nullptr, m_DirectObjs[i]->AsString()); + } + + if (m_DirectObjTypes[i] == CPDF_Object::DICTIONARY) { + EXPECT_TRUE(m_DirectObjs[i]->IsDictionary()); + EXPECT_EQ(m_DirectObjs[i].get(), m_DirectObjs[i]->AsDictionary()); + } else { + EXPECT_FALSE(m_DirectObjs[i]->IsDictionary()); + EXPECT_EQ(nullptr, m_DirectObjs[i]->AsDictionary()); + } + + if (m_DirectObjTypes[i] == CPDF_Object::STREAM) { + EXPECT_TRUE(m_DirectObjs[i]->IsStream()); + EXPECT_EQ(m_DirectObjs[i].get(), m_DirectObjs[i]->AsStream()); + } else { + EXPECT_FALSE(m_DirectObjs[i]->IsStream()); + EXPECT_EQ(nullptr, m_DirectObjs[i]->AsStream()); + } + + EXPECT_FALSE(m_DirectObjs[i]->IsReference()); + EXPECT_EQ(nullptr, m_DirectObjs[i]->AsReference()); + } + // Check indirect references. + for (size_t i = 0; i < m_RefObjs.size(); ++i) { + EXPECT_TRUE(m_RefObjs[i]->IsReference()); + EXPECT_EQ(m_RefObjs[i].get(), m_RefObjs[i]->AsReference()); + } +} + +TEST(PDFArrayTest, GetMatrix) { + float elems[][6] = {{0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}, + {1, 2, 3, 4, 5, 6}, + {2.3f, 4.05f, 3, -2, -3, 0.0f}, + {0.05f, 0.1f, 0.56f, 0.67f, 1.34f, 99.9f}}; + for (size_t i = 0; i < FX_ArraySize(elems); ++i) { + ScopedArray arr(new CPDF_Array); + CFX_Matrix matrix(elems[i][0], elems[i][1], elems[i][2], elems[i][3], + elems[i][4], elems[i][5]); + for (size_t j = 0; j < 6; ++j) + arr->AddNumber(elems[i][j]); + CFX_Matrix arr_matrix = arr->GetMatrix(); + EXPECT_EQ(matrix.GetA(), arr_matrix.GetA()); + EXPECT_EQ(matrix.GetB(), arr_matrix.GetB()); + EXPECT_EQ(matrix.GetC(), arr_matrix.GetC()); + EXPECT_EQ(matrix.GetD(), arr_matrix.GetD()); + EXPECT_EQ(matrix.GetE(), arr_matrix.GetE()); + EXPECT_EQ(matrix.GetF(), arr_matrix.GetF()); + } +} + +TEST(PDFArrayTest, GetRect) { + float elems[][4] = {{0.0f, 0.0f, 0.0f, 0.0f}, + {1, 2, 5, 6}, + {2.3f, 4.05f, -3, 0.0f}, + {0.05f, 0.1f, 1.34f, 99.9f}}; + for (size_t i = 0; i < FX_ArraySize(elems); ++i) { + ScopedArray arr(new CPDF_Array); + CFX_FloatRect rect(elems[i]); + for (size_t j = 0; j < 4; ++j) + arr->AddNumber(elems[i][j]); + CFX_FloatRect arr_rect = arr->GetRect(); + EXPECT_EQ(rect.left, arr_rect.left); + EXPECT_EQ(rect.right, arr_rect.right); + EXPECT_EQ(rect.bottom, arr_rect.bottom); + EXPECT_EQ(rect.top, arr_rect.top); + } +} + +TEST(PDFArrayTest, GetTypeAt) { + { + // Boolean array. + bool vals[] = {true, false, false, true, true}; + ScopedArray arr(new CPDF_Array); + for (size_t i = 0; i < FX_ArraySize(vals); ++i) + arr->InsertAt(i, new CPDF_Boolean(vals[i])); + for (size_t i = 0; i < FX_ArraySize(vals); ++i) { + TestArrayAccessors(arr.get(), i, // Array and index. + vals[i] ? "true" : "false", // String value. + nullptr, // Const string value. + vals[i] ? 1 : 0, // Integer value. + 0, // Float value. + nullptr, // Array value. + nullptr, // Dictionary value. + nullptr); // Stream value. + } + } + { + // Integer array. + int vals[] = {10, 0, -345, 2089345456, -1000000000, 567, 93658767}; + ScopedArray arr(new CPDF_Array); + for (size_t i = 0; i < FX_ArraySize(vals); ++i) + arr->InsertAt(i, new CPDF_Number(vals[i])); + for (size_t i = 0; i < FX_ArraySize(vals); ++i) { + char buf[33]; + TestArrayAccessors(arr.get(), i, // Array and index. + FXSYS_itoa(vals[i], buf, 10), // String value. + nullptr, // Const string value. + vals[i], // Integer value. + vals[i], // Float value. + nullptr, // Array value. + nullptr, // Dictionary value. + nullptr); // Stream value. + } + } + { + // Float array. + float vals[] = {0.0f, 0, 10, 10.0f, 0.0345f, + 897.34f, -2.5f, -1.0f, -345.0f, -0.0f}; + const char* expected_str[] = {"0", "0", "10", "10", "0.0345", + "897.34", "-2.5", "-1", "-345", "0"}; + ScopedArray arr(new CPDF_Array); + for (size_t i = 0; i < FX_ArraySize(vals); ++i) { + arr->InsertAt(i, new CPDF_Number(vals[i])); + } + for (size_t i = 0; i < FX_ArraySize(vals); ++i) { + TestArrayAccessors(arr.get(), i, // Array and index. + expected_str[i], // String value. + nullptr, // Const string value. + vals[i], // Integer value. + vals[i], // Float value. + nullptr, // Array value. + nullptr, // Dictionary value. + nullptr); // Stream value. + } + } + { + // String and name array + const char* const vals[] = {"this", "adsde$%^", "\r\t", "\"012", + ".", "EYREW", "It is a joke :)"}; + ScopedArray string_array(new CPDF_Array); + ScopedArray name_array(new CPDF_Array); + for (size_t i = 0; i < FX_ArraySize(vals); ++i) { + string_array->InsertAt(i, new CPDF_String(vals[i], false)); + name_array->InsertAt(i, new CPDF_Name(vals[i])); + } + for (size_t i = 0; i < FX_ArraySize(vals); ++i) { + TestArrayAccessors(string_array.get(), i, // Array and index. + vals[i], // String value. + vals[i], // Const string value. + 0, // Integer value. + 0, // Float value. + nullptr, // Array value. + nullptr, // Dictionary value. + nullptr); // Stream value. + TestArrayAccessors(name_array.get(), i, // Array and index. + vals[i], // String value. + vals[i], // Const string value. + 0, // Integer value. + 0, // Float value. + nullptr, // Array value. + nullptr, // Dictionary value. + nullptr); // Stream value. + } + } + { + // Null element array. + ScopedArray arr(new CPDF_Array); + for (size_t i = 0; i < 3; ++i) + arr->InsertAt(i, new CPDF_Null); + for (size_t i = 0; i < 3; ++i) { + TestArrayAccessors(arr.get(), i, // Array and index. + "", // String value. + nullptr, // Const string value. + 0, // Integer value. + 0, // Float value. + nullptr, // Array value. + nullptr, // Dictionary value. + nullptr); // Stream value. + } + } + { + // Array of array. + CPDF_Array* vals[3]; + ScopedArray arr(new CPDF_Array); + for (size_t i = 0; i < 3; ++i) { + vals[i] = new CPDF_Array; + for (size_t j = 0; j < 3; ++j) { + int value = j + 100; + vals[i]->InsertAt(i, new CPDF_Number(value)); + } + arr->InsertAt(i, vals[i]); + } + for (size_t i = 0; i < 3; ++i) { + TestArrayAccessors(arr.get(), i, // Array and index. + "", // String value. + nullptr, // Const string value. + 0, // Integer value. + 0, // Float value. + vals[i], // Array value. + nullptr, // Dictionary value. + nullptr); // Stream value. + } + } + { + // Dictionary array. + CPDF_Dictionary* vals[3]; + ScopedArray arr(new CPDF_Array); + for (size_t i = 0; i < 3; ++i) { + vals[i] = new CPDF_Dictionary; + for (size_t j = 0; j < 3; ++j) { + std::string key("key"); + char buf[33]; + key.append(FXSYS_itoa(j, buf, 10)); + int value = j + 200; + vals[i]->SetAt(CFX_ByteStringC(key.c_str()), new CPDF_Number(value)); + } + arr->InsertAt(i, vals[i]); + } + for (size_t i = 0; i < 3; ++i) { + TestArrayAccessors(arr.get(), i, // Array and index. + "", // String value. + nullptr, // Const string value. + 0, // Integer value. + 0, // Float value. + nullptr, // Array value. + vals[i], // Dictionary value. + nullptr); // Stream value. + } + } + { + // Stream array. + CPDF_Dictionary* vals[3]; + CPDF_Stream* stream_vals[3]; + ScopedArray arr(new CPDF_Array); + for (size_t i = 0; i < 3; ++i) { + vals[i] = new CPDF_Dictionary; + for (size_t j = 0; j < 3; ++j) { + std::string key("key"); + char buf[33]; + key.append(FXSYS_itoa(j, buf, 10)); + int value = j + 200; + vals[i]->SetAt(CFX_ByteStringC(key.c_str()), new CPDF_Number(value)); + } + uint8_t content[] = "content: this is a stream"; + size_t data_size = FX_ArraySize(content); + uint8_t* data = reinterpret_cast(malloc(data_size)); + memcpy(data, content, data_size); + stream_vals[i] = new CPDF_Stream(data, data_size, vals[i]); + arr->InsertAt(i, stream_vals[i]); + } + for (size_t i = 0; i < 3; ++i) { + TestArrayAccessors(arr.get(), i, // Array and index. + "", // String value. + nullptr, // Const string value. + 0, // Integer value. + 0, // Float value. + nullptr, // Array value. + vals[i], // Dictionary value. + stream_vals[i]); // Stream value. + } + } + { + // Mixed array. + ScopedArray arr(new CPDF_Array); + // Array arr will take ownership of all the objects inserted. + arr->InsertAt(0, new CPDF_Boolean(true)); + arr->InsertAt(1, new CPDF_Boolean(false)); + arr->InsertAt(2, new CPDF_Number(0)); + arr->InsertAt(3, new CPDF_Number(-1234)); + arr->InsertAt(4, new CPDF_Number(2345.0f)); + arr->InsertAt(5, new CPDF_Number(0.05f)); + arr->InsertAt(6, new CPDF_String("", false)); + arr->InsertAt(7, new CPDF_String("It is a test!", false)); + arr->InsertAt(8, new CPDF_Name("NAME")); + arr->InsertAt(9, new CPDF_Name("test")); + arr->InsertAt(10, new CPDF_Null()); + CPDF_Array* arr_val = new CPDF_Array; + arr_val->AddNumber(1); + arr_val->AddNumber(2); + arr->InsertAt(11, arr_val); + CPDF_Dictionary* dict_val = new CPDF_Dictionary; + dict_val->SetAt("key1", new CPDF_String("Linda", false)); + dict_val->SetAt("key2", new CPDF_String("Zoe", false)); + arr->InsertAt(12, dict_val); + CPDF_Dictionary* stream_dict = new CPDF_Dictionary; + stream_dict->SetAt("key1", new CPDF_String("John", false)); + stream_dict->SetAt("key2", new CPDF_String("King", false)); + uint8_t data[] = "A stream for test"; + // The data buffer will be owned by stream object, so it needs to be + // dynamically allocated. + size_t buf_size = sizeof(data); + uint8_t* buf = reinterpret_cast(malloc(buf_size)); + memcpy(buf, data, buf_size); + CPDF_Stream* stream_val = new CPDF_Stream(buf, buf_size, stream_dict); + arr->InsertAt(13, stream_val); + const char* const expected_str[] = { + "true", "false", "0", "-1234", "2345", "0.05", "", + "It is a test!", "NAME", "test", "", "", "", ""}; + const char* const expected_cstr[] = { + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + "It is a test!", "NAME", "test", nullptr, nullptr, nullptr, nullptr}; + const int expected_int[] = {1, 0, 0, -1234, 2345, 0, 0, + 0, 0, 0, 0, 0, 0, 0}; + const float expected_float[] = {0, 0, 0, -1234, 2345, 0.05f, 0, + 0, 0, 0, 0, 0, 0, 0}; + for (size_t i = 0; i < arr->GetCount(); ++i) { + EXPECT_STREQ(expected_str[i], arr->GetStringAt(i).c_str()); + EXPECT_STREQ(expected_cstr[i], arr->GetConstStringAt(i).GetCStr()); + EXPECT_EQ(expected_int[i], arr->GetIntegerAt(i)); + EXPECT_EQ(expected_float[i], arr->GetNumberAt(i)); + EXPECT_EQ(expected_float[i], arr->GetFloatAt(i)); + if (i == 11) + EXPECT_EQ(arr_val, arr->GetArrayAt(i)); + else + EXPECT_EQ(nullptr, arr->GetArrayAt(i)); + if (i == 13) { + EXPECT_EQ(stream_dict, arr->GetDictAt(i)); + EXPECT_EQ(stream_val, arr->GetStreamAt(i)); + } else { + EXPECT_EQ(nullptr, arr->GetStreamAt(i)); + if (i == 12) + EXPECT_EQ(dict_val, arr->GetDictAt(i)); + else + EXPECT_EQ(nullptr, arr->GetDictAt(i)); + } + } + } +} + +TEST(PDFArrayTest, AddNumber) { + float vals[] = {1.0f, -1.0f, 0, 0.456734f, + 12345.54321f, 0.5f, 1000, 0.000045f}; + ScopedArray arr(new CPDF_Array); + for (size_t i = 0; i < FX_ArraySize(vals); ++i) + arr->AddNumber(vals[i]); + for (size_t i = 0; i < FX_ArraySize(vals); ++i) { + EXPECT_EQ(CPDF_Object::NUMBER, arr->GetElement(i)->GetType()); + EXPECT_EQ(vals[i], arr->GetElement(i)->GetNumber()); + } +} + +TEST(PDFArrayTest, AddInteger) { + int vals[] = {0, 1, 934435456, 876, 10000, -1, -24354656, -100}; + ScopedArray arr(new CPDF_Array); + for (size_t i = 0; i < FX_ArraySize(vals); ++i) + arr->AddInteger(vals[i]); + for (size_t i = 0; i < FX_ArraySize(vals); ++i) { + EXPECT_EQ(CPDF_Object::NUMBER, arr->GetElement(i)->GetType()); + EXPECT_EQ(vals[i], arr->GetElement(i)->GetNumber()); + } +} + +TEST(PDFArrayTest, AddStringAndName) { + const char* vals[] = {"", "a", "ehjhRIOYTTFdfcdnv", "122323", + "$#%^&**", " ", "This is a test.\r\n"}; + ScopedArray string_array(new CPDF_Array); + ScopedArray name_array(new CPDF_Array); + for (size_t i = 0; i < FX_ArraySize(vals); ++i) { + string_array->AddString(vals[i]); + name_array->AddName(vals[i]); + } + for (size_t i = 0; i < FX_ArraySize(vals); ++i) { + EXPECT_EQ(CPDF_Object::STRING, string_array->GetElement(i)->GetType()); + EXPECT_STREQ(vals[i], string_array->GetElement(i)->GetString().c_str()); + EXPECT_EQ(CPDF_Object::NAME, name_array->GetElement(i)->GetType()); + EXPECT_STREQ(vals[i], name_array->GetElement(i)->GetString().c_str()); + } +} + +TEST(PDFArrayTest, AddReferenceAndGetElement) { + std::unique_ptr holder( + new CPDF_IndirectObjectHolder(nullptr)); + CPDF_Boolean* boolean_obj = new CPDF_Boolean(true); + CPDF_Number* int_obj = new CPDF_Number(-1234); + CPDF_Number* float_obj = new CPDF_Number(2345.089f); + CPDF_String* str_obj = new CPDF_String("Adsfdsf 343434 %&&*\n", false); + CPDF_Name* name_obj = new CPDF_Name("Title:"); + CPDF_Null* null_obj = new CPDF_Null(); + CPDF_Object* indirect_objs[] = {boolean_obj, int_obj, float_obj, + str_obj, name_obj, null_obj}; + unsigned int obj_nums[] = {2, 4, 7, 2345, 799887, 1}; + ScopedArray arr(new CPDF_Array); + ScopedArray arr1(new CPDF_Array); + // Create two arrays of references by different AddReference() APIs. + for (size_t i = 0; i < FX_ArraySize(indirect_objs); ++i) { + // All the indirect objects inserted will be owned by holder. + holder->InsertIndirectObject(obj_nums[i], indirect_objs[i]); + arr->AddReference(holder.get(), obj_nums[i]); + arr1->AddReference(holder.get(), indirect_objs[i]); + } + // Check indirect objects. + for (size_t i = 0; i < FX_ArraySize(obj_nums); ++i) + EXPECT_EQ(indirect_objs[i], holder->GetIndirectObject(obj_nums[i])); + // Check arrays. + EXPECT_EQ(arr->GetCount(), arr1->GetCount()); + for (size_t i = 0; i < arr->GetCount(); ++i) { + EXPECT_EQ(CPDF_Object::REFERENCE, arr->GetElement(i)->GetType()); + EXPECT_EQ(indirect_objs[i], arr->GetElement(i)->GetDirect()); + EXPECT_EQ(indirect_objs[i], arr->GetElementValue(i)); + EXPECT_EQ(CPDF_Object::REFERENCE, arr1->GetElement(i)->GetType()); + EXPECT_EQ(indirect_objs[i], arr1->GetElement(i)->GetDirect()); + EXPECT_EQ(indirect_objs[i], arr1->GetElementValue(i)); + } +} diff --git a/core/src/fpdfapi/fpdf_parser/cpdf_parser.cpp b/core/src/fpdfapi/fpdf_parser/cpdf_parser.cpp index 2835c6ff38..c9a2d8d496 100644 --- a/core/src/fpdfapi/fpdf_parser/cpdf_parser.cpp +++ b/core/src/fpdfapi/fpdf_parser/cpdf_parser.cpp @@ -8,7 +8,12 @@ #include +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/cpdf_document.h" +#include "core/include/fpdfapi/cpdf_number.h" +#include "core/include/fpdfapi/cpdf_stream.h" +#include "core/include/fpdfapi/cpdf_reference.h" #include "core/include/fpdfapi/ipdf_crypto_handler.h" #include "core/include/fxcrt/fx_ext.h" #include "core/include/fxcrt/fx_safe_types.h" diff --git a/core/src/fpdfapi/fpdf_parser/cpdf_reference.cpp b/core/src/fpdfapi/fpdf_parser/cpdf_reference.cpp new file mode 100644 index 0000000000..4aede7d07a --- /dev/null +++ b/core/src/fpdfapi/fpdf_parser/cpdf_reference.cpp @@ -0,0 +1,72 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/include/fpdfapi/cpdf_reference.h" + +#include "core/include/fpdfapi/cpdf_indirect_object_holder.h" + +CPDF_Reference::CPDF_Reference(CPDF_IndirectObjectHolder* pDoc, int objnum) + : m_pObjList(pDoc), m_RefObjNum(objnum) {} + +CPDF_Reference::~CPDF_Reference() {} + +CPDF_Object::Type CPDF_Reference::GetType() const { + return REFERENCE; +} + +CFX_ByteString CPDF_Reference::GetString() const { + CPDF_Object* obj = SafeGetDirect(); + return obj ? obj->GetString() : CFX_ByteString(); +} + +CFX_ByteStringC CPDF_Reference::GetConstString() const { + CPDF_Object* obj = SafeGetDirect(); + return obj ? obj->GetConstString() : CFX_ByteStringC(); +} + +FX_FLOAT CPDF_Reference::GetNumber() const { + CPDF_Object* obj = SafeGetDirect(); + return obj ? obj->GetNumber() : 0; +} + +int CPDF_Reference::GetInteger() const { + CPDF_Object* obj = SafeGetDirect(); + return obj ? obj->GetInteger() : 0; +} + +CPDF_Dictionary* CPDF_Reference::GetDict() const { + CPDF_Object* obj = SafeGetDirect(); + return obj ? obj->GetDict() : nullptr; +} + +bool CPDF_Reference::IsReference() const { + return true; +} + +CPDF_Reference* CPDF_Reference::AsReference() { + return this; +} + +const CPDF_Reference* CPDF_Reference::AsReference() const { + return this; +} + +CPDF_Object* CPDF_Reference::Clone(FX_BOOL bDirect) const { + if (bDirect) { + auto* pDirect = GetDirect(); + return pDirect ? pDirect->Clone(TRUE) : nullptr; + } + return new CPDF_Reference(m_pObjList, m_RefObjNum); +} + +void CPDF_Reference::SetRef(CPDF_IndirectObjectHolder* pDoc, FX_DWORD objnum) { + m_pObjList = pDoc; + m_RefObjNum = objnum; +} + +CPDF_Object* CPDF_Reference::GetDirect() const { + return m_pObjList ? m_pObjList->GetIndirectObject(m_RefObjNum) : nullptr; +} diff --git a/core/src/fpdfapi/fpdf_parser/cpdf_standard_security_handler.cpp b/core/src/fpdfapi/fpdf_parser/cpdf_standard_security_handler.cpp index a89b2fb119..feb152ba4a 100644 --- a/core/src/fpdfapi/fpdf_parser/cpdf_standard_security_handler.cpp +++ b/core/src/fpdfapi/fpdf_parser/cpdf_standard_security_handler.cpp @@ -9,8 +9,10 @@ #include #include "core/include/fdrm/fx_crypt.h" +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/cpdf_parser.h" -#include "core/include/fpdfapi/fpdf_objects.h" +#include "core/include/fpdfapi/cpdf_object.h" #include "core/src/fpdfapi/fpdf_parser/cpdf_standard_crypto_handler.h" namespace { diff --git a/core/src/fpdfapi/fpdf_parser/cpdf_stream.cpp b/core/src/fpdfapi/fpdf_parser/cpdf_stream.cpp new file mode 100644 index 0000000000..45c946ab5f --- /dev/null +++ b/core/src/fpdfapi/fpdf_parser/cpdf_stream.cpp @@ -0,0 +1,232 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/include/fpdfapi/cpdf_stream.h" + +#include "core/include/fpdfapi/cpdf_dictionary.h" +#include "core/include/fpdfapi/fpdf_parser_decode.h" + +CPDF_Stream::CPDF_Stream(uint8_t* pData, FX_DWORD size, CPDF_Dictionary* pDict) + : m_pDict(pDict), + m_dwSize(size), + m_GenNum(kMemoryBasedGenNum), + m_pDataBuf(pData) {} + +CPDF_Stream::~CPDF_Stream() { + if (IsMemoryBased()) + FX_Free(m_pDataBuf); + + if (m_pDict) + m_pDict->Release(); +} + +CPDF_Object::Type CPDF_Stream::GetType() const { + return STREAM; +} + +CPDF_Dictionary* CPDF_Stream::GetDict() const { + return m_pDict; +} + +bool CPDF_Stream::IsStream() const { + return true; +} + +CPDF_Stream* CPDF_Stream::AsStream() { + return this; +} + +const CPDF_Stream* CPDF_Stream::AsStream() const { + return this; +} + +void CPDF_Stream::InitStreamInternal(CPDF_Dictionary* pDict) { + if (pDict) { + if (m_pDict) + m_pDict->Release(); + m_pDict = pDict; + } + if (IsMemoryBased()) + FX_Free(m_pDataBuf); + + m_GenNum = 0; + m_pFile = nullptr; +} + +void CPDF_Stream::InitStream(uint8_t* pData, + FX_DWORD size, + CPDF_Dictionary* pDict) { + InitStreamInternal(pDict); + m_GenNum = kMemoryBasedGenNum; + m_pDataBuf = FX_Alloc(uint8_t, size); + if (pData) + FXSYS_memcpy(m_pDataBuf, pData, size); + + m_dwSize = size; + if (m_pDict) + m_pDict->SetAtInteger("Length", size); +} + +CPDF_Object* CPDF_Stream::Clone(FX_BOOL bDirect) const { + CPDF_StreamAcc acc; + acc.LoadAllData(this, TRUE); + FX_DWORD streamSize = acc.GetSize(); + CPDF_Dictionary* pDict = GetDict(); + if (pDict) + pDict = ToDictionary(pDict->Clone(bDirect)); + + return new CPDF_Stream(acc.DetachData(), streamSize, pDict); +} + +void CPDF_Stream::SetData(const uint8_t* pData, + FX_DWORD size, + FX_BOOL bCompressed, + FX_BOOL bKeepBuf) { + if (IsMemoryBased()) + FX_Free(m_pDataBuf); + m_GenNum = kMemoryBasedGenNum; + + if (bKeepBuf) { + m_pDataBuf = const_cast(pData); + } else { + m_pDataBuf = FX_Alloc(uint8_t, size); + if (pData) { + FXSYS_memcpy(m_pDataBuf, pData, size); + } + } + m_dwSize = size; + if (!m_pDict) + m_pDict = new CPDF_Dictionary; + m_pDict->SetAtInteger("Length", size); + if (!bCompressed) { + m_pDict->RemoveAt("Filter"); + m_pDict->RemoveAt("DecodeParms"); + } +} + +FX_BOOL CPDF_Stream::ReadRawData(FX_FILESIZE offset, + uint8_t* buf, + FX_DWORD size) const { + if (!IsMemoryBased() && m_pFile) + return m_pFile->ReadBlock(buf, offset, size); + + if (m_pDataBuf) + FXSYS_memcpy(buf, m_pDataBuf + offset, size); + + return TRUE; +} + +void CPDF_Stream::InitStreamFromFile(IFX_FileRead* pFile, + CPDF_Dictionary* pDict) { + InitStreamInternal(pDict); + m_pFile = pFile; + m_dwSize = (FX_DWORD)pFile->GetSize(); + if (m_pDict) + m_pDict->SetAtInteger("Length", m_dwSize); +} + +CFX_WideString CPDF_Stream::GetUnicodeText() const { + CPDF_StreamAcc stream; + stream.LoadAllData(this, FALSE); + return PDF_DecodeText(stream.GetData(), stream.GetSize()); +} + +CPDF_StreamAcc::CPDF_StreamAcc() + : m_pData(nullptr), + m_dwSize(0), + m_bNewBuf(FALSE), + m_pImageParam(nullptr), + m_pStream(nullptr), + m_pSrcData(nullptr) {} + +void CPDF_StreamAcc::LoadAllData(const CPDF_Stream* pStream, + FX_BOOL bRawAccess, + FX_DWORD estimated_size, + FX_BOOL bImageAcc) { + if (!pStream) + return; + + m_pStream = pStream; + if (pStream->IsMemoryBased() && + (!pStream->GetDict()->KeyExist("Filter") || bRawAccess)) { + m_dwSize = pStream->GetRawSize(); + m_pData = pStream->GetRawData(); + return; + } + uint8_t* pSrcData; + FX_DWORD dwSrcSize = pStream->GetRawSize(); + if (dwSrcSize == 0) + return; + + if (!pStream->IsMemoryBased()) { + pSrcData = m_pSrcData = FX_Alloc(uint8_t, dwSrcSize); + if (!pStream->ReadRawData(0, pSrcData, dwSrcSize)) + return; + } else { + pSrcData = pStream->GetRawData(); + } + uint8_t* pDecryptedData = pSrcData; + FX_DWORD dwDecryptedSize = dwSrcSize; + if (!pStream->GetDict()->KeyExist("Filter") || bRawAccess) { + m_pData = pDecryptedData; + m_dwSize = dwDecryptedSize; + } else { + FX_BOOL bRet = PDF_DataDecode( + pDecryptedData, dwDecryptedSize, m_pStream->GetDict(), m_pData, + m_dwSize, m_ImageDecoder, m_pImageParam, estimated_size, bImageAcc); + if (!bRet) { + m_pData = pDecryptedData; + m_dwSize = dwDecryptedSize; + } + } + if (pSrcData != pStream->GetRawData() && pSrcData != m_pData) { + FX_Free(pSrcData); + } + if (pDecryptedData != pSrcData && pDecryptedData != m_pData) { + FX_Free(pDecryptedData); + } + m_pSrcData = nullptr; + m_bNewBuf = m_pData != pStream->GetRawData(); +} + +CPDF_StreamAcc::~CPDF_StreamAcc() { + if (m_bNewBuf) { + FX_Free(m_pData); + } + FX_Free(m_pSrcData); +} + +const uint8_t* CPDF_StreamAcc::GetData() const { + if (m_bNewBuf) { + return m_pData; + } + if (!m_pStream) { + return nullptr; + } + return m_pStream->GetRawData(); +} + +FX_DWORD CPDF_StreamAcc::GetSize() const { + if (m_bNewBuf) { + return m_dwSize; + } + if (!m_pStream) { + return 0; + } + return m_pStream->GetRawSize(); +} + +uint8_t* CPDF_StreamAcc::DetachData() { + if (m_bNewBuf) { + uint8_t* p = m_pData; + m_pData = nullptr; + m_dwSize = 0; + return p; + } + uint8_t* p = FX_Alloc(uint8_t, m_dwSize); + FXSYS_memcpy(p, m_pData, m_dwSize); + return p; +} diff --git a/core/src/fpdfapi/fpdf_parser/cpdf_string.cpp b/core/src/fpdfapi/fpdf_parser/cpdf_string.cpp new file mode 100644 index 0000000000..1d0bf0b829 --- /dev/null +++ b/core/src/fpdfapi/fpdf_parser/cpdf_string.cpp @@ -0,0 +1,56 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/include/fpdfapi/cpdf_string.h" + +#include "core/include/fpdfapi/fpdf_parser_decode.h" + +CPDF_String::CPDF_String() : m_bHex(FALSE) {} + +CPDF_String::CPDF_String(const CFX_ByteString& str, FX_BOOL bHex) + : m_String(str), m_bHex(bHex) {} + +CPDF_String::CPDF_String(const CFX_WideString& str) : m_bHex(FALSE) { + m_String = PDF_EncodeText(str); +} + +CPDF_String::~CPDF_String() {} + +CPDF_Object::Type CPDF_String::GetType() const { + return STRING; +} + +CPDF_Object* CPDF_String::Clone(FX_BOOL bDirect) const { + return new CPDF_String(m_String, m_bHex); +} + +CFX_ByteString CPDF_String::GetString() const { + return m_String; +} + +CFX_ByteStringC CPDF_String::GetConstString() const { + return CFX_ByteStringC(m_String); +} + +void CPDF_String::SetString(const CFX_ByteString& str) { + m_String = str; +} + +bool CPDF_String::IsString() const { + return true; +} + +CPDF_String* CPDF_String::AsString() { + return this; +} + +const CPDF_String* CPDF_String::AsString() const { + return this; +} + +CFX_WideString CPDF_String::GetUnicodeText() const { + return PDF_DecodeText(m_String); +} diff --git a/core/src/fpdfapi/fpdf_parser/cpdf_syntax_parser.cpp b/core/src/fpdfapi/fpdf_parser/cpdf_syntax_parser.cpp index e5b782ca1c..b414827f95 100644 --- a/core/src/fpdfapi/fpdf_parser/cpdf_syntax_parser.cpp +++ b/core/src/fpdfapi/fpdf_parser/cpdf_syntax_parser.cpp @@ -8,8 +8,16 @@ #include +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_boolean.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" +#include "core/include/fpdfapi/cpdf_name.h" +#include "core/include/fpdfapi/cpdf_null.h" +#include "core/include/fpdfapi/cpdf_number.h" +#include "core/include/fpdfapi/cpdf_reference.h" +#include "core/include/fpdfapi/cpdf_stream.h" +#include "core/include/fpdfapi/cpdf_string.h" #include "core/include/fpdfapi/fpdf_module.h" -#include "core/include/fpdfapi/fpdf_objects.h" #include "core/include/fpdfapi/fpdf_parser_decode.h" #include "core/include/fpdfapi/ipdf_crypto_handler.h" #include "core/include/fxcrt/fx_ext.h" diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp index 7a38209979..e6de7cb7ff 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp @@ -9,8 +9,9 @@ #include #include +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/fpdf_module.h" -#include "core/include/fpdfapi/fpdf_objects.h" #include "core/include/fxcodec/fx_codec.h" #include "core/include/fxcrt/fx_ext.h" #include "core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.h" diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp deleted file mode 100644 index 05d85e6398..0000000000 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp +++ /dev/null @@ -1,1148 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#include "core/include/fpdfapi/fpdf_objects.h" - -#include - -#include "core/include/fpdfapi/cpdf_parser.h" -#include "core/include/fpdfapi/fpdf_parser_decode.h" -#include "core/include/fxcrt/fx_string.h" -#include "third_party/base/stl_util.h" - -CPDF_Object::~CPDF_Object() {} - -CPDF_Object* CPDF_Object::GetDirect() const { - return const_cast(this); -} - -void CPDF_Object::Release() { - if (m_ObjNum) { - return; - } - Destroy(); -} - -CFX_ByteString CPDF_Object::GetString() const { - return CFX_ByteString(); -} - -CFX_ByteStringC CPDF_Object::GetConstString() const { - return CFX_ByteStringC(); -} - -CFX_WideString CPDF_Object::GetUnicodeText() const { - return CFX_WideString(); -} - -FX_FLOAT CPDF_Object::GetNumber() const { - return 0; -} - -int CPDF_Object::GetInteger() const { - return 0; -} - -CPDF_Dictionary* CPDF_Object::GetDict() const { - return nullptr; -} - -CPDF_Array* CPDF_Object::GetArray() const { - return nullptr; -} - -void CPDF_Object::SetString(const CFX_ByteString& str) { - ASSERT(FALSE); -} - -bool CPDF_Object::IsArray() const { - return false; -} - -bool CPDF_Object::IsBoolean() const { - return false; -} - -bool CPDF_Object::IsDictionary() const { - return false; -} - -bool CPDF_Object::IsName() const { - return false; -} - -bool CPDF_Object::IsNumber() const { - return false; -} - -bool CPDF_Object::IsReference() const { - return false; -} - -bool CPDF_Object::IsStream() const { - return false; -} - -bool CPDF_Object::IsString() const { - return false; -} - -CPDF_Array* CPDF_Object::AsArray() { - return nullptr; -} - -const CPDF_Array* CPDF_Object::AsArray() const { - return nullptr; -} - -CPDF_Boolean* CPDF_Object::AsBoolean() { - return nullptr; -} - -const CPDF_Boolean* CPDF_Object::AsBoolean() const { - return nullptr; -} - -CPDF_Dictionary* CPDF_Object::AsDictionary() { - return nullptr; -} - -const CPDF_Dictionary* CPDF_Object::AsDictionary() const { - return nullptr; -} - -CPDF_Name* CPDF_Object::AsName() { - return nullptr; -} - -const CPDF_Name* CPDF_Object::AsName() const { - return nullptr; -} - -CPDF_Number* CPDF_Object::AsNumber() { - return nullptr; -} - -const CPDF_Number* CPDF_Object::AsNumber() const { - return nullptr; -} - -CPDF_Reference* CPDF_Object::AsReference() { - return nullptr; -} - -const CPDF_Reference* CPDF_Object::AsReference() const { - return nullptr; -} - -CPDF_Stream* CPDF_Object::AsStream() { - return nullptr; -} - -const CPDF_Stream* CPDF_Object::AsStream() const { - return nullptr; -} - -CPDF_String* CPDF_Object::AsString() { - return nullptr; -} - -const CPDF_String* CPDF_Object::AsString() const { - return nullptr; -} - -CPDF_Boolean::~CPDF_Boolean() {} - -CPDF_Object::Type CPDF_Boolean::GetType() const { - return BOOLEAN; -} - -CPDF_Object* CPDF_Boolean::Clone(FX_BOOL bDirect) const { - return new CPDF_Boolean(m_bValue); -} - -CFX_ByteString CPDF_Boolean::GetString() const { - return m_bValue ? "true" : "false"; -} - -int CPDF_Boolean::GetInteger() const { - return m_bValue; -} - -void CPDF_Boolean::SetString(const CFX_ByteString& str) { - m_bValue = (str == "true"); -} - -bool CPDF_Boolean::IsBoolean() const { - return true; -} - -CPDF_Boolean* CPDF_Boolean::AsBoolean() { - return this; -} - -const CPDF_Boolean* CPDF_Boolean::AsBoolean() const { - return this; -} - -CPDF_Number::CPDF_Number(const CFX_ByteStringC& str) { - FX_atonum(str, m_bInteger, &m_Integer); -} - -CPDF_Number::~CPDF_Number() {} - -CPDF_Object::Type CPDF_Number::GetType() const { - return NUMBER; -} - -CPDF_Object* CPDF_Number::Clone(FX_BOOL bDirect) const { - return m_bInteger ? new CPDF_Number(m_Integer) : new CPDF_Number(m_Float); -} - -FX_FLOAT CPDF_Number::GetNumber() const { - return m_bInteger ? static_cast(m_Integer) : m_Float; -} - -int CPDF_Number::GetInteger() const { - return m_bInteger ? m_Integer : static_cast(m_Float); -} - -bool CPDF_Number::IsNumber() const { - return true; -} - -CPDF_Number* CPDF_Number::AsNumber() { - return this; -} - -const CPDF_Number* CPDF_Number::AsNumber() const { - return this; -} - -void CPDF_Number::SetString(const CFX_ByteString& str) { - FX_atonum(str, m_bInteger, &m_Integer); -} - -CFX_ByteString CPDF_Number::GetString() const { - return m_bInteger ? CFX_ByteString::FormatInteger(m_Integer, FXFORMAT_SIGNED) - : CFX_ByteString::FormatFloat(m_Float); -} - -CPDF_String::CPDF_String(const CFX_WideString& str) : m_bHex(FALSE) { - m_String = PDF_EncodeText(str); -} - -CPDF_String::~CPDF_String() {} - -CPDF_Object::Type CPDF_String::GetType() const { - return STRING; -} - -CPDF_Object* CPDF_String::Clone(FX_BOOL bDirect) const { - return new CPDF_String(m_String, m_bHex); -} - -CFX_ByteString CPDF_String::GetString() const { - return m_String; -} - -CFX_ByteStringC CPDF_String::GetConstString() const { - return CFX_ByteStringC(m_String); -} - -void CPDF_String::SetString(const CFX_ByteString& str) { - m_String = str; -} - -bool CPDF_String::IsString() const { - return true; -} - -CPDF_String* CPDF_String::AsString() { - return this; -} - -const CPDF_String* CPDF_String::AsString() const { - return this; -} - -CFX_WideString CPDF_String::GetUnicodeText() const { - return PDF_DecodeText(m_String); -} - -CPDF_Name::~CPDF_Name() {} - -CPDF_Object::Type CPDF_Name::GetType() const { - return NAME; -} - -CPDF_Object* CPDF_Name::Clone(FX_BOOL bDirect) const { - return new CPDF_Name(m_Name); -} - -CFX_ByteString CPDF_Name::GetString() const { - return m_Name; -} - -CFX_ByteStringC CPDF_Name::GetConstString() const { - return CFX_ByteStringC(m_Name); -} - -void CPDF_Name::SetString(const CFX_ByteString& str) { - m_Name = str; -} - -bool CPDF_Name::IsName() const { - return true; -} - -CPDF_Name* CPDF_Name::AsName() { - return this; -} - -const CPDF_Name* CPDF_Name::AsName() const { - return this; -} - -CFX_WideString CPDF_Name::GetUnicodeText() const { - return PDF_DecodeText(m_Name); -} - -CPDF_Array::CPDF_Array() {} - -CPDF_Array::~CPDF_Array() { - int size = m_Objects.GetSize(); - CPDF_Object** pList = m_Objects.GetData(); - for (int i = 0; i < size; i++) { - if (pList[i]) - pList[i]->Release(); - } -} - -CPDF_Object::Type CPDF_Array::GetType() const { - return ARRAY; -} - -CPDF_Array* CPDF_Array::GetArray() const { - // The method should be made non-const if we want to not be const. - // See bug #234. - return const_cast(this); -} - -bool CPDF_Array::IsArray() const { - return true; -} - -CPDF_Array* CPDF_Array::AsArray() { - return this; -} - -const CPDF_Array* CPDF_Array::AsArray() const { - return this; -} - -CPDF_Object* CPDF_Array::Clone(FX_BOOL bDirect) const { - CPDF_Array* pCopy = new CPDF_Array(); - for (int i = 0; i < GetCount(); i++) { - CPDF_Object* value = m_Objects.GetAt(i); - pCopy->m_Objects.Add(value->Clone(bDirect)); - } - return pCopy; -} - -CFX_FloatRect CPDF_Array::GetRect() { - CFX_FloatRect rect; - if (!IsArray() || m_Objects.GetSize() != 4) - return rect; - - rect.left = GetNumberAt(0); - rect.bottom = GetNumberAt(1); - rect.right = GetNumberAt(2); - rect.top = GetNumberAt(3); - return rect; -} - -CFX_Matrix CPDF_Array::GetMatrix() { - CFX_Matrix matrix; - if (!IsArray() || m_Objects.GetSize() != 6) - return matrix; - - matrix.Set(GetNumberAt(0), GetNumberAt(1), GetNumberAt(2), GetNumberAt(3), - GetNumberAt(4), GetNumberAt(5)); - return matrix; -} - -CPDF_Object* CPDF_Array::GetElement(FX_DWORD i) const { - if (i >= (FX_DWORD)m_Objects.GetSize()) - return nullptr; - return m_Objects.GetAt(i); -} - -CPDF_Object* CPDF_Array::GetElementValue(FX_DWORD i) const { - if (i >= (FX_DWORD)m_Objects.GetSize()) - return nullptr; - return m_Objects.GetAt(i)->GetDirect(); -} - -CFX_ByteString CPDF_Array::GetStringAt(FX_DWORD i) const { - if (i >= (FX_DWORD)m_Objects.GetSize()) - return CFX_ByteString(); - return m_Objects.GetAt(i)->GetString(); -} - -CFX_ByteStringC CPDF_Array::GetConstStringAt(FX_DWORD i) const { - if (i >= (FX_DWORD)m_Objects.GetSize()) - return CFX_ByteStringC(); - return m_Objects.GetAt(i)->GetConstString(); -} - -int CPDF_Array::GetIntegerAt(FX_DWORD i) const { - if (i >= (FX_DWORD)m_Objects.GetSize()) - return 0; - return m_Objects.GetAt(i)->GetInteger(); -} - -FX_FLOAT CPDF_Array::GetNumberAt(FX_DWORD i) const { - if (i >= (FX_DWORD)m_Objects.GetSize()) - return 0; - return m_Objects.GetAt(i)->GetNumber(); -} - -CPDF_Dictionary* CPDF_Array::GetDictAt(FX_DWORD i) const { - CPDF_Object* p = GetElementValue(i); - if (!p) - return NULL; - if (CPDF_Dictionary* pDict = p->AsDictionary()) - return pDict; - if (CPDF_Stream* pStream = p->AsStream()) - return pStream->GetDict(); - return NULL; -} - -CPDF_Stream* CPDF_Array::GetStreamAt(FX_DWORD i) const { - return ToStream(GetElementValue(i)); -} - -CPDF_Array* CPDF_Array::GetArrayAt(FX_DWORD i) const { - return ToArray(GetElementValue(i)); -} - -void CPDF_Array::RemoveAt(FX_DWORD i, int nCount) { - if (i >= (FX_DWORD)m_Objects.GetSize()) - return; - - if (nCount <= 0 || nCount > m_Objects.GetSize() - i) - return; - - for (int j = 0; j < nCount; ++j) { - if (CPDF_Object* p = m_Objects.GetAt(i + j)) - p->Release(); - } - m_Objects.RemoveAt(i, nCount); -} - -void CPDF_Array::SetAt(FX_DWORD i, - CPDF_Object* pObj, - CPDF_IndirectObjectHolder* pObjs) { - ASSERT(IsArray()); - ASSERT(i < (FX_DWORD)m_Objects.GetSize()); - if (i >= (FX_DWORD)m_Objects.GetSize()) - return; - if (CPDF_Object* pOld = m_Objects.GetAt(i)) - pOld->Release(); - if (pObj->GetObjNum()) { - ASSERT(pObjs); - pObj = new CPDF_Reference(pObjs, pObj->GetObjNum()); - } - m_Objects.SetAt(i, pObj); -} - -void CPDF_Array::InsertAt(FX_DWORD index, - CPDF_Object* pObj, - CPDF_IndirectObjectHolder* pObjs) { - if (pObj->GetObjNum()) { - ASSERT(pObjs); - pObj = new CPDF_Reference(pObjs, pObj->GetObjNum()); - } - m_Objects.InsertAt(index, pObj); -} - -void CPDF_Array::Add(CPDF_Object* pObj, CPDF_IndirectObjectHolder* pObjs) { - if (pObj->GetObjNum()) { - ASSERT(pObjs); - pObj = new CPDF_Reference(pObjs, pObj->GetObjNum()); - } - m_Objects.Add(pObj); -} - -void CPDF_Array::AddName(const CFX_ByteString& str) { - ASSERT(IsArray()); - Add(new CPDF_Name(str)); -} - -void CPDF_Array::AddString(const CFX_ByteString& str) { - ASSERT(IsArray()); - Add(new CPDF_String(str, FALSE)); -} - -void CPDF_Array::AddInteger(int i) { - ASSERT(IsArray()); - Add(new CPDF_Number(i)); -} - -void CPDF_Array::AddNumber(FX_FLOAT f) { - ASSERT(IsArray()); - CPDF_Number* pNumber = new CPDF_Number(f); - Add(pNumber); -} - -void CPDF_Array::AddReference(CPDF_IndirectObjectHolder* pDoc, - FX_DWORD objnum) { - ASSERT(IsArray()); - Add(new CPDF_Reference(pDoc, objnum)); -} - -CPDF_Dictionary::CPDF_Dictionary() {} - -CPDF_Dictionary::~CPDF_Dictionary() { - for (const auto& it : m_Map) { - it.second->Release(); - } -} - -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); -} - -bool CPDF_Dictionary::IsDictionary() const { - return true; -} - -CPDF_Dictionary* CPDF_Dictionary::AsDictionary() { - return this; -} - -const CPDF_Dictionary* CPDF_Dictionary::AsDictionary() const { - return this; -} - -CPDF_Object* CPDF_Dictionary::Clone(FX_BOOL bDirect) const { - CPDF_Dictionary* pCopy = new CPDF_Dictionary(); - for (const auto& it : *this) - pCopy->m_Map.insert(std::make_pair(it.first, it.second->Clone(bDirect))); - return pCopy; -} - -CPDF_Object* CPDF_Dictionary::GetElement(const CFX_ByteStringC& key) const { - auto it = m_Map.find(key); - if (it == m_Map.end()) - return nullptr; - return it->second; -} -CPDF_Object* CPDF_Dictionary::GetElementValue( - const CFX_ByteStringC& key) const { - CPDF_Object* p = GetElement(key); - return p ? p->GetDirect() : nullptr; -} - -CFX_ByteString CPDF_Dictionary::GetStringBy(const CFX_ByteStringC& key) const { - CPDF_Object* p = GetElement(key); - if (p) { - return p->GetString(); - } - return CFX_ByteString(); -} - -CFX_ByteStringC CPDF_Dictionary::GetConstStringBy( - const CFX_ByteStringC& key) const { - CPDF_Object* p = GetElement(key); - if (p) { - return p->GetConstString(); - } - return CFX_ByteStringC(); -} - -CFX_WideString CPDF_Dictionary::GetUnicodeTextBy( - const CFX_ByteStringC& key) const { - CPDF_Object* p = GetElement(key); - if (CPDF_Reference* pRef = ToReference(p)) - p = pRef->GetDirect(); - return p ? p->GetUnicodeText() : CFX_WideString(); -} - -CFX_ByteString CPDF_Dictionary::GetStringBy(const CFX_ByteStringC& key, - const CFX_ByteStringC& def) const { - CPDF_Object* p = GetElement(key); - if (p) { - return p->GetString(); - } - return CFX_ByteString(def); -} - -CFX_ByteStringC CPDF_Dictionary::GetConstStringBy( - const CFX_ByteStringC& key, - const CFX_ByteStringC& def) const { - CPDF_Object* p = GetElement(key); - if (p) { - return p->GetConstString(); - } - return CFX_ByteStringC(def); -} - -int CPDF_Dictionary::GetIntegerBy(const CFX_ByteStringC& key) const { - CPDF_Object* p = GetElement(key); - if (p) { - return p->GetInteger(); - } - return 0; -} - -int CPDF_Dictionary::GetIntegerBy(const CFX_ByteStringC& key, int def) const { - CPDF_Object* p = GetElement(key); - if (p) { - return p->GetInteger(); - } - return def; -} - -FX_FLOAT CPDF_Dictionary::GetNumberBy(const CFX_ByteStringC& key) const { - CPDF_Object* p = GetElement(key); - if (p) { - return p->GetNumber(); - } - return 0; -} - -FX_BOOL CPDF_Dictionary::GetBooleanBy(const CFX_ByteStringC& key, - FX_BOOL bDefault) const { - CPDF_Object* p = GetElement(key); - if (ToBoolean(p)) - return p->GetInteger(); - return bDefault; -} - -CPDF_Dictionary* CPDF_Dictionary::GetDictBy(const CFX_ByteStringC& key) const { - CPDF_Object* p = GetElementValue(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_Array* CPDF_Dictionary::GetArrayBy(const CFX_ByteStringC& key) const { - return ToArray(GetElementValue(key)); -} - -CPDF_Stream* CPDF_Dictionary::GetStreamBy(const CFX_ByteStringC& key) const { - return ToStream(GetElementValue(key)); -} - -CFX_FloatRect CPDF_Dictionary::GetRectBy(const CFX_ByteStringC& key) const { - CFX_FloatRect rect; - CPDF_Array* pArray = GetArrayBy(key); - if (pArray) - rect = pArray->GetRect(); - return rect; -} - -CFX_Matrix CPDF_Dictionary::GetMatrixBy(const CFX_ByteStringC& key) const { - CFX_Matrix matrix; - CPDF_Array* pArray = GetArrayBy(key); - if (pArray) - matrix = pArray->GetMatrix(); - return matrix; -} - -FX_BOOL CPDF_Dictionary::KeyExist(const CFX_ByteStringC& key) const { - return pdfium::ContainsKey(m_Map, key); -} - -bool CPDF_Dictionary::IsSignatureDict() const { - CPDF_Object* pType = GetElementValue("Type"); - if (!pType) - pType = GetElementValue("FT"); - return pType && pType->GetString() == "Sig"; -} - -void CPDF_Dictionary::SetAt(const CFX_ByteStringC& key, CPDF_Object* pObj) { - ASSERT(IsDictionary()); - // Avoid 2 constructions of CFX_ByteString. - CFX_ByteString key_bytestring = key; - auto it = m_Map.find(key_bytestring); - if (it == m_Map.end()) { - if (pObj) { - m_Map.insert(std::make_pair(key_bytestring, pObj)); - } - return; - } - - if (it->second == pObj) - return; - it->second->Release(); - - if (pObj) - it->second = pObj; - else - m_Map.erase(it); -} - -void CPDF_Dictionary::RemoveAt(const CFX_ByteStringC& key) { - auto it = m_Map.find(key); - if (it == m_Map.end()) - return; - - it->second->Release(); - m_Map.erase(it); -} - -void CPDF_Dictionary::ReplaceKey(const CFX_ByteStringC& oldkey, - const CFX_ByteStringC& newkey) { - auto old_it = m_Map.find(oldkey); - if (old_it == m_Map.end()) - return; - - // Avoid 2 constructions of CFX_ByteString. - CFX_ByteString newkey_bytestring = newkey; - auto new_it = m_Map.find(newkey_bytestring); - if (new_it == old_it) - return; - - if (new_it != m_Map.end()) { - new_it->second->Release(); - new_it->second = old_it->second; - } else { - m_Map.insert(std::make_pair(newkey_bytestring, old_it->second)); - } - m_Map.erase(old_it); -} - -void CPDF_Dictionary::SetAtInteger(const CFX_ByteStringC& key, int i) { - SetAt(key, new CPDF_Number(i)); -} - -void CPDF_Dictionary::SetAtName(const CFX_ByteStringC& key, - const CFX_ByteString& name) { - SetAt(key, new CPDF_Name(name)); -} - -void CPDF_Dictionary::SetAtString(const CFX_ByteStringC& key, - const CFX_ByteString& str) { - SetAt(key, new CPDF_String(str, FALSE)); -} - -void CPDF_Dictionary::SetAtReference(const CFX_ByteStringC& key, - CPDF_IndirectObjectHolder* pDoc, - FX_DWORD objnum) { - SetAt(key, new CPDF_Reference(pDoc, objnum)); -} - -void CPDF_Dictionary::AddReference(const CFX_ByteStringC& key, - CPDF_IndirectObjectHolder* pDoc, - FX_DWORD objnum) { - SetAt(key, new CPDF_Reference(pDoc, objnum)); -} - -void CPDF_Dictionary::SetAtNumber(const CFX_ByteStringC& key, FX_FLOAT f) { - CPDF_Number* pNumber = new CPDF_Number(f); - SetAt(key, pNumber); -} - -void CPDF_Dictionary::SetAtBoolean(const CFX_ByteStringC& key, FX_BOOL bValue) { - SetAt(key, new CPDF_Boolean(bValue)); -} - -void CPDF_Dictionary::SetAtRect(const CFX_ByteStringC& key, - const CFX_FloatRect& rect) { - CPDF_Array* pArray = new CPDF_Array; - pArray->AddNumber(rect.left); - pArray->AddNumber(rect.bottom); - pArray->AddNumber(rect.right); - pArray->AddNumber(rect.top); - SetAt(key, pArray); -} - -void CPDF_Dictionary::SetAtMatrix(const CFX_ByteStringC& key, - const CFX_Matrix& matrix) { - CPDF_Array* pArray = new CPDF_Array; - pArray->AddNumber(matrix.a); - pArray->AddNumber(matrix.b); - pArray->AddNumber(matrix.c); - pArray->AddNumber(matrix.d); - pArray->AddNumber(matrix.e); - pArray->AddNumber(matrix.f); - SetAt(key, pArray); -} - -CPDF_Stream::CPDF_Stream(uint8_t* pData, FX_DWORD size, CPDF_Dictionary* pDict) - : m_pDict(pDict), - m_dwSize(size), - m_GenNum(kMemoryBasedGenNum), - m_pDataBuf(pData) {} - -CPDF_Stream::~CPDF_Stream() { - if (IsMemoryBased()) - FX_Free(m_pDataBuf); - - if (m_pDict) - m_pDict->Release(); -} - -CPDF_Object::Type CPDF_Stream::GetType() const { - return STREAM; -} - -CPDF_Dictionary* CPDF_Stream::GetDict() const { - return m_pDict; -} - -bool CPDF_Stream::IsStream() const { - return true; -} - -CPDF_Stream* CPDF_Stream::AsStream() { - return this; -} - -const CPDF_Stream* CPDF_Stream::AsStream() const { - return this; -} - -void CPDF_Stream::InitStreamInternal(CPDF_Dictionary* pDict) { - if (pDict) { - if (m_pDict) - m_pDict->Release(); - m_pDict = pDict; - } - if (IsMemoryBased()) - FX_Free(m_pDataBuf); - - m_GenNum = 0; - m_pFile = nullptr; -} - -void CPDF_Stream::InitStream(uint8_t* pData, - FX_DWORD size, - CPDF_Dictionary* pDict) { - InitStreamInternal(pDict); - m_GenNum = kMemoryBasedGenNum; - m_pDataBuf = FX_Alloc(uint8_t, size); - if (pData) { - FXSYS_memcpy(m_pDataBuf, pData, size); - } - m_dwSize = size; - if (m_pDict) { - m_pDict->SetAtInteger("Length", size); - } -} - -CPDF_Object* CPDF_Stream::Clone(FX_BOOL bDirect) const { - CPDF_StreamAcc acc; - acc.LoadAllData(this, TRUE); - FX_DWORD streamSize = acc.GetSize(); - CPDF_Dictionary* pDict = GetDict(); - if (pDict) { - pDict = ToDictionary(pDict->Clone(bDirect)); - } - return new CPDF_Stream(acc.DetachData(), streamSize, pDict); -} - -void CPDF_Stream::SetData(const uint8_t* pData, - FX_DWORD size, - FX_BOOL bCompressed, - FX_BOOL bKeepBuf) { - if (IsMemoryBased()) - FX_Free(m_pDataBuf); - m_GenNum = kMemoryBasedGenNum; - - if (bKeepBuf) { - m_pDataBuf = const_cast(pData); - } else { - m_pDataBuf = FX_Alloc(uint8_t, size); - if (pData) { - FXSYS_memcpy(m_pDataBuf, pData, size); - } - } - m_dwSize = size; - if (!m_pDict) - m_pDict = new CPDF_Dictionary; - m_pDict->SetAtInteger("Length", size); - if (!bCompressed) { - m_pDict->RemoveAt("Filter"); - m_pDict->RemoveAt("DecodeParms"); - } -} - -FX_BOOL CPDF_Stream::ReadRawData(FX_FILESIZE offset, - uint8_t* buf, - FX_DWORD size) const { - if (!IsMemoryBased() && m_pFile) - return m_pFile->ReadBlock(buf, offset, size); - - if (m_pDataBuf) - FXSYS_memcpy(buf, m_pDataBuf + offset, size); - return TRUE; -} -void CPDF_Stream::InitStreamFromFile(IFX_FileRead* pFile, - CPDF_Dictionary* pDict) { - InitStreamInternal(pDict); - m_pFile = pFile; - m_dwSize = (FX_DWORD)pFile->GetSize(); - if (m_pDict) { - m_pDict->SetAtInteger("Length", m_dwSize); - } -} - -CFX_WideString CPDF_Stream::GetUnicodeText() const { - CPDF_StreamAcc stream; - stream.LoadAllData(this, FALSE); - return PDF_DecodeText(stream.GetData(), stream.GetSize()); -} - -CPDF_StreamAcc::CPDF_StreamAcc() { - m_bNewBuf = FALSE; - m_pData = NULL; - m_dwSize = 0; - m_pImageParam = NULL; - m_pStream = NULL; - m_pSrcData = NULL; -} -void CPDF_StreamAcc::LoadAllData(const CPDF_Stream* pStream, - FX_BOOL bRawAccess, - FX_DWORD estimated_size, - FX_BOOL bImageAcc) { - if (!pStream) - return; - - m_pStream = pStream; - if (pStream->IsMemoryBased() && - (!pStream->GetDict()->KeyExist("Filter") || bRawAccess)) { - m_dwSize = pStream->GetRawSize(); - m_pData = pStream->GetRawData(); - return; - } - uint8_t* pSrcData; - FX_DWORD dwSrcSize = pStream->GetRawSize(); - if (dwSrcSize == 0) - return; - - if (!pStream->IsMemoryBased()) { - pSrcData = m_pSrcData = FX_Alloc(uint8_t, dwSrcSize); - if (!pStream->ReadRawData(0, pSrcData, dwSrcSize)) - return; - } else { - pSrcData = pStream->GetRawData(); - } - uint8_t* pDecryptedData = pSrcData; - FX_DWORD dwDecryptedSize = dwSrcSize; - if (!pStream->GetDict()->KeyExist("Filter") || bRawAccess) { - m_pData = pDecryptedData; - m_dwSize = dwDecryptedSize; - } else { - FX_BOOL bRet = PDF_DataDecode( - pDecryptedData, dwDecryptedSize, m_pStream->GetDict(), m_pData, - m_dwSize, m_ImageDecoder, m_pImageParam, estimated_size, bImageAcc); - if (!bRet) { - m_pData = pDecryptedData; - m_dwSize = dwDecryptedSize; - } - } - if (pSrcData != pStream->GetRawData() && pSrcData != m_pData) { - FX_Free(pSrcData); - } - if (pDecryptedData != pSrcData && pDecryptedData != m_pData) { - FX_Free(pDecryptedData); - } - m_pSrcData = NULL; - m_bNewBuf = m_pData != pStream->GetRawData(); -} - -CPDF_StreamAcc::~CPDF_StreamAcc() { - if (m_bNewBuf) { - FX_Free(m_pData); - } - FX_Free(m_pSrcData); -} - -const uint8_t* CPDF_StreamAcc::GetData() const { - if (m_bNewBuf) { - return m_pData; - } - if (!m_pStream) { - return NULL; - } - return m_pStream->GetRawData(); -} - -FX_DWORD CPDF_StreamAcc::GetSize() const { - if (m_bNewBuf) { - return m_dwSize; - } - if (!m_pStream) { - return 0; - } - return m_pStream->GetRawSize(); -} - -uint8_t* CPDF_StreamAcc::DetachData() { - if (m_bNewBuf) { - uint8_t* p = m_pData; - m_pData = NULL; - m_dwSize = 0; - return p; - } - uint8_t* p = FX_Alloc(uint8_t, m_dwSize); - FXSYS_memcpy(p, m_pData, m_dwSize); - return p; -} - -CPDF_Object::Type CPDF_Null::GetType() const { - return NULLOBJ; -} - -CPDF_Object* CPDF_Null::Clone(FX_BOOL bDirect) const { - return new CPDF_Null; -} - -CPDF_Reference::~CPDF_Reference() {} - -CPDF_Object::Type CPDF_Reference::GetType() const { - return REFERENCE; -} - -CFX_ByteString CPDF_Reference::GetString() const { - CPDF_Object* obj = SafeGetDirect(); - return obj ? obj->GetString() : CFX_ByteString(); -} - -CFX_ByteStringC CPDF_Reference::GetConstString() const { - CPDF_Object* obj = SafeGetDirect(); - return obj ? obj->GetConstString() : CFX_ByteStringC(); -} - -FX_FLOAT CPDF_Reference::GetNumber() const { - CPDF_Object* obj = SafeGetDirect(); - return obj ? obj->GetNumber() : 0; -} - -int CPDF_Reference::GetInteger() const { - CPDF_Object* obj = SafeGetDirect(); - return obj ? obj->GetInteger() : 0; -} - -CPDF_Dictionary* CPDF_Reference::GetDict() const { - CPDF_Object* obj = SafeGetDirect(); - return obj ? obj->GetDict() : nullptr; -} - -bool CPDF_Reference::IsReference() const { - return true; -} - -CPDF_Reference* CPDF_Reference::AsReference() { - return this; -} - -const CPDF_Reference* CPDF_Reference::AsReference() const { - return this; -} - -CPDF_Object* CPDF_Reference::Clone(FX_BOOL bDirect) const { - if (bDirect) { - auto* pDirect = GetDirect(); - return pDirect ? pDirect->Clone(TRUE) : nullptr; - } - return new CPDF_Reference(m_pObjList, m_RefObjNum); -} - -void CPDF_Reference::SetRef(CPDF_IndirectObjectHolder* pDoc, FX_DWORD objnum) { - m_pObjList = pDoc; - m_RefObjNum = objnum; -} - -CPDF_Object* CPDF_Reference::GetDirect() const { - return m_pObjList ? m_pObjList->GetIndirectObject(m_RefObjNum) : nullptr; -} - -CPDF_IndirectObjectHolder::CPDF_IndirectObjectHolder(CPDF_Parser* pParser) - : m_pParser(pParser), m_LastObjNum(0) { - if (pParser) - m_LastObjNum = m_pParser->GetLastObjNum(); -} - -CPDF_IndirectObjectHolder::~CPDF_IndirectObjectHolder() { - for (const auto& pair : m_IndirectObjs) { - pair.second->Destroy(); - } -} - -CPDF_Object* CPDF_IndirectObjectHolder::GetIndirectObject(FX_DWORD objnum) { - if (objnum == 0) - return nullptr; - - auto it = m_IndirectObjs.find(objnum); - if (it != m_IndirectObjs.end()) - return it->second->GetObjNum() != -1 ? it->second : nullptr; - - if (!m_pParser) - return nullptr; - - CPDF_Object* pObj = m_pParser->ParseIndirectObject(this, objnum); - if (!pObj) - return nullptr; - - pObj->m_ObjNum = objnum; - m_LastObjNum = std::max(m_LastObjNum, objnum); - if (m_IndirectObjs[objnum]) - m_IndirectObjs[objnum]->Destroy(); - - m_IndirectObjs[objnum] = pObj; - return pObj; -} - -FX_DWORD CPDF_IndirectObjectHolder::AddIndirectObject(CPDF_Object* pObj) { - if (pObj->m_ObjNum) { - return pObj->m_ObjNum; - } - m_LastObjNum++; - m_IndirectObjs[m_LastObjNum] = pObj; - pObj->m_ObjNum = m_LastObjNum; - return m_LastObjNum; -} - -void CPDF_IndirectObjectHolder::ReleaseIndirectObject(FX_DWORD objnum) { - auto it = m_IndirectObjs.find(objnum); - if (it == m_IndirectObjs.end() || it->second->GetObjNum() == -1) - return; - it->second->Destroy(); - m_IndirectObjs.erase(it); -} - -FX_BOOL CPDF_IndirectObjectHolder::InsertIndirectObject(FX_DWORD objnum, - CPDF_Object* pObj) { - if (!objnum || !pObj) - return FALSE; - auto it = m_IndirectObjs.find(objnum); - if (it != m_IndirectObjs.end()) { - if (pObj->GetGenNum() <= it->second->GetGenNum()) { - pObj->Destroy(); - return FALSE; - } - it->second->Destroy(); - } - pObj->m_ObjNum = objnum; - m_IndirectObjs[objnum] = pObj; - m_LastObjNum = std::max(m_LastObjNum, objnum); - return TRUE; -} diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects_unittest.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects_unittest.cpp deleted file mode 100644 index a377a92e44..0000000000 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects_unittest.cpp +++ /dev/null @@ -1,767 +0,0 @@ -// Copyright 2016 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "core/include/fpdfapi/fpdf_objects.h" - -#include -#include -#include - -#include "core/include/fxcrt/fx_basic.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace { - -using ScopedArray = std::unique_ptr>; - -void TestArrayAccessors(const CPDF_Array* arr, - size_t index, - const char* str_val, - const char* const_str_val, - int int_val, - float float_val, - CPDF_Array* arr_val, - CPDF_Dictionary* dict_val, - CPDF_Stream* stream_val) { - EXPECT_STREQ(str_val, arr->GetStringAt(index).c_str()); - EXPECT_STREQ(const_str_val, arr->GetConstStringAt(index).GetCStr()); - EXPECT_EQ(int_val, arr->GetIntegerAt(index)); - EXPECT_EQ(float_val, arr->GetNumberAt(index)); - EXPECT_EQ(float_val, arr->GetFloatAt(index)); - EXPECT_EQ(arr_val, arr->GetArrayAt(index)); - EXPECT_EQ(dict_val, arr->GetDictAt(index)); - EXPECT_EQ(stream_val, arr->GetStreamAt(index)); -} - -} // namespace - -class PDFObjectsTest : public testing::Test { - public: - void SetUp() override { - // Initialize different kinds of objects. - // Boolean objects. - CPDF_Boolean* boolean_false_obj = new CPDF_Boolean(false); - CPDF_Boolean* boolean_true_obj = new CPDF_Boolean(true); - // Number objects. - CPDF_Number* number_int_obj = new CPDF_Number(1245); - CPDF_Number* number_float_obj = new CPDF_Number(9.00345f); - // String objects. - CPDF_String* str_reg_obj = new CPDF_String(L"A simple test"); - CPDF_String* str_spec_obj = new CPDF_String(L"\t\n"); - // Name object. - CPDF_Name* name_obj = new CPDF_Name("space"); - // Array object. - m_ArrayObj = new CPDF_Array; - m_ArrayObj->InsertAt(0, new CPDF_Number(8902)); - m_ArrayObj->InsertAt(1, new CPDF_Name("address")); - // Dictionary object. - m_DictObj = new CPDF_Dictionary; - m_DictObj->SetAt("bool", new CPDF_Boolean(false)); - m_DictObj->SetAt("num", new CPDF_Number(0.23f)); - // Stream object. - const char content[] = "abcdefghijklmnopqrstuvwxyz"; - size_t buf_len = FX_ArraySize(content); - uint8_t* buf = reinterpret_cast(malloc(buf_len)); - memcpy(buf, content, buf_len); - m_StreamDictObj = new CPDF_Dictionary; - m_StreamDictObj->SetAt("key1", new CPDF_String(L" test dict")); - m_StreamDictObj->SetAt("key2", new CPDF_Number(-1)); - CPDF_Stream* stream_obj = new CPDF_Stream(buf, buf_len, m_StreamDictObj); - // Null Object. - CPDF_Null* null_obj = new CPDF_Null; - // All direct objects. - CPDF_Object* objs[] = {boolean_false_obj, boolean_true_obj, number_int_obj, - number_float_obj, str_reg_obj, str_spec_obj, - name_obj, m_ArrayObj, m_DictObj, - stream_obj, null_obj}; - m_DirectObjTypes = { - CPDF_Object::BOOLEAN, CPDF_Object::BOOLEAN, CPDF_Object::NUMBER, - CPDF_Object::NUMBER, CPDF_Object::STRING, CPDF_Object::STRING, - CPDF_Object::NAME, CPDF_Object::ARRAY, CPDF_Object::DICTIONARY, - CPDF_Object::STREAM, CPDF_Object::NULLOBJ}; - for (size_t i = 0; i < FX_ArraySize(objs); ++i) - m_DirectObjs.emplace_back(objs[i]); - - // Indirect references to indirect objects. - m_ObjHolder.reset(new CPDF_IndirectObjectHolder(nullptr)); - m_IndirectObjs = {boolean_true_obj, number_int_obj, str_spec_obj, name_obj, - m_ArrayObj, m_DictObj, stream_obj}; - for (size_t i = 0; i < m_IndirectObjs.size(); ++i) { - m_ObjHolder->AddIndirectObject(m_IndirectObjs[i]); - m_RefObjs.emplace_back(new CPDF_Reference( - m_ObjHolder.get(), m_IndirectObjs[i]->GetObjNum())); - } - } - - bool Equal(CPDF_Object* obj1, CPDF_Object* obj2) { - if (obj1 == obj2) - return true; - if (!obj1 || !obj2 || obj1->GetType() != obj2->GetType()) - return false; - switch (obj1->GetType()) { - case CPDF_Object::BOOLEAN: - return obj1->GetInteger() == obj2->GetInteger(); - case CPDF_Object::NUMBER: - return obj1->AsNumber()->IsInteger() == obj2->AsNumber()->IsInteger() && - obj1->GetInteger() == obj2->GetInteger(); - case CPDF_Object::STRING: - case CPDF_Object::NAME: - return obj1->GetString() == obj2->GetString(); - case CPDF_Object::ARRAY: { - const CPDF_Array* array1 = obj1->AsArray(); - const CPDF_Array* array2 = obj2->AsArray(); - if (array1->GetCount() != array2->GetCount()) - return false; - for (size_t i = 0; i < array1->GetCount(); ++i) { - if (!Equal(array1->GetElement(i), array2->GetElement(i))) - return false; - } - return true; - } - case CPDF_Object::DICTIONARY: { - const CPDF_Dictionary* dict1 = obj1->AsDictionary(); - const CPDF_Dictionary* dict2 = obj2->AsDictionary(); - if (dict1->GetCount() != dict2->GetCount()) - return false; - for (CPDF_Dictionary::const_iterator it = dict1->begin(); - it != dict1->end(); ++it) { - if (!Equal(it->second, dict2->GetElement(it->first))) - return false; - } - return true; - } - case CPDF_Object::NULLOBJ: - return true; - case CPDF_Object::STREAM: { - const CPDF_Stream* stream1 = obj1->AsStream(); - const CPDF_Stream* stream2 = obj2->AsStream(); - if (!stream1->GetDict() && !stream2->GetDict()) - return true; - // Compare dictionaries. - if (!Equal(stream1->GetDict(), stream2->GetDict())) - return false; - // Compare sizes. - if (stream1->GetRawSize() != stream2->GetRawSize()) - return false; - // Compare contents. - // Since this function is used for testing Clone(), only memory based - // streams need to be handled. - if (!stream1->IsMemoryBased() || !stream2->IsMemoryBased()) - return false; - return FXSYS_memcmp(stream1->GetRawData(), stream2->GetRawData(), - stream1->GetRawSize()) == 0; - } - case CPDF_Object::REFERENCE: - return obj1->AsReference()->GetRefObjNum() == - obj2->AsReference()->GetRefObjNum(); - } - return false; - } - - protected: - using ScopedObj = std::unique_ptr>; - - // m_ObjHolder needs to be declared first and destructed last since it also - // refers to some objects in m_DirectObjs. - std::unique_ptr m_ObjHolder; - std::vector m_DirectObjs; - std::vector m_DirectObjTypes; - std::vector m_RefObjs; - CPDF_Dictionary* m_DictObj; - CPDF_Dictionary* m_StreamDictObj; - CPDF_Array* m_ArrayObj; - std::vector m_IndirectObjs; -}; - -TEST_F(PDFObjectsTest, GetString) { - const char* direct_obj_results[] = { - "false", "true", "1245", "9.00345", "A simple test", "\t\n", "space", - "", "", "", ""}; - // Check for direct objects. - for (size_t i = 0; i < m_DirectObjs.size(); ++i) - EXPECT_STREQ(direct_obj_results[i], m_DirectObjs[i]->GetString().c_str()); - - // Check indirect references. - const char* indirect_obj_results[] = {"true", "1245", "\t\n", "space", - "", "", ""}; - for (size_t i = 0; i < m_RefObjs.size(); ++i) { - EXPECT_STREQ(indirect_obj_results[i], m_RefObjs[i]->GetString().c_str()); - } -} - -TEST_F(PDFObjectsTest, GetConstString) { - const char* direct_obj_results[] = { - nullptr, nullptr, nullptr, nullptr, "A simple test", "\t\n", - "space", nullptr, nullptr, nullptr, nullptr}; - // Check for direct objects. - for (size_t i = 0; i < m_DirectObjs.size(); ++i) { - if (!direct_obj_results[i]) { - EXPECT_EQ(direct_obj_results[i], - m_DirectObjs[i]->GetConstString().GetCStr()); - } else { - EXPECT_STREQ(direct_obj_results[i], - m_DirectObjs[i]->GetConstString().GetCStr()); - } - } - // Check indirect references. - const char* indirect_obj_results[] = {nullptr, nullptr, "\t\n", "space", - nullptr, nullptr, nullptr}; - for (size_t i = 0; i < m_RefObjs.size(); ++i) { - if (!indirect_obj_results[i]) { - EXPECT_EQ(nullptr, m_RefObjs[i]->GetConstString().GetCStr()); - } else { - EXPECT_STREQ(indirect_obj_results[i], - m_RefObjs[i]->GetConstString().GetCStr()); - } - } -} - -TEST_F(PDFObjectsTest, GetUnicodeText) { - const wchar_t* direct_obj_results[] = { - L"", L"", L"", L"", L"A simple test", - L"\t\n", L"space", L"", L"", L"abcdefghijklmnopqrstuvwxyz", - L""}; - // Check for direct objects. - for (size_t i = 0; i < m_DirectObjs.size(); ++i) - EXPECT_STREQ(direct_obj_results[i], - m_DirectObjs[i]->GetUnicodeText().c_str()); - - // Check indirect references. - for (const auto& it : m_RefObjs) - EXPECT_STREQ(L"", it->GetUnicodeText().c_str()); -} - -TEST_F(PDFObjectsTest, GetNumber) { - const FX_FLOAT direct_obj_results[] = {0, 0, 1245, 9.00345f, 0, 0, - 0, 0, 0, 0, 0}; - // Check for direct objects. - for (size_t i = 0; i < m_DirectObjs.size(); ++i) - EXPECT_EQ(direct_obj_results[i], m_DirectObjs[i]->GetNumber()); - - // Check indirect references. - const FX_FLOAT indirect_obj_results[] = {0, 1245, 0, 0, 0, 0, 0}; - for (size_t i = 0; i < m_RefObjs.size(); ++i) - EXPECT_EQ(indirect_obj_results[i], m_RefObjs[i]->GetNumber()); -} - -TEST_F(PDFObjectsTest, GetInteger) { - const int direct_obj_results[] = {0, 1, 1245, 9, 0, 0, 0, 0, 0, 0, 0}; - // Check for direct objects. - for (size_t i = 0; i < m_DirectObjs.size(); ++i) - EXPECT_EQ(direct_obj_results[i], m_DirectObjs[i]->GetInteger()); - - // Check indirect references. - const int indirect_obj_results[] = {1, 1245, 0, 0, 0, 0, 0}; - for (size_t i = 0; i < m_RefObjs.size(); ++i) - EXPECT_EQ(indirect_obj_results[i], m_RefObjs[i]->GetInteger()); -} - -TEST_F(PDFObjectsTest, GetDict) { - const CPDF_Dictionary* direct_obj_results[] = { - nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, - nullptr, nullptr, m_DictObj, m_StreamDictObj, nullptr}; - // Check for direct objects. - for (size_t i = 0; i < m_DirectObjs.size(); ++i) - EXPECT_EQ(direct_obj_results[i], m_DirectObjs[i]->GetDict()); - - // Check indirect references. - const CPDF_Dictionary* indirect_obj_results[] = { - nullptr, nullptr, nullptr, nullptr, nullptr, m_DictObj, m_StreamDictObj}; - for (size_t i = 0; i < m_RefObjs.size(); ++i) - EXPECT_EQ(indirect_obj_results[i], m_RefObjs[i]->GetDict()); -} - -TEST_F(PDFObjectsTest, GetArray) { - const CPDF_Array* direct_obj_results[] = { - nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, - nullptr, m_ArrayObj, nullptr, nullptr, nullptr}; - // Check for direct objects. - for (size_t i = 0; i < m_DirectObjs.size(); ++i) - EXPECT_EQ(direct_obj_results[i], m_DirectObjs[i]->GetArray()); - - // Check indirect references. - for (const auto& it : m_RefObjs) - EXPECT_EQ(nullptr, it->GetArray()); -} - -TEST_F(PDFObjectsTest, Clone) { - // Check for direct objects. - for (size_t i = 0; i < m_DirectObjs.size(); ++i) { - ScopedObj obj(m_DirectObjs[i]->Clone()); - EXPECT_TRUE(Equal(m_DirectObjs[i].get(), obj.get())); - } - - // Check indirect references. - for (const auto& it : m_RefObjs) { - ScopedObj obj(it->Clone()); - EXPECT_TRUE(Equal(it.get(), obj.get())); - } -} - -TEST_F(PDFObjectsTest, GetType) { - // Check for direct objects. - for (size_t i = 0; i < m_DirectObjs.size(); ++i) - EXPECT_EQ(m_DirectObjTypes[i], m_DirectObjs[i]->GetType()); - - // Check indirect references. - for (const auto& it : m_RefObjs) - EXPECT_EQ(CPDF_Object::REFERENCE, it->GetType()); -} - -TEST_F(PDFObjectsTest, GetDirect) { - // Check for direct objects. - for (size_t i = 0; i < m_DirectObjs.size(); ++i) - EXPECT_EQ(m_DirectObjs[i].get(), m_DirectObjs[i]->GetDirect()); - - // Check indirect references. - for (size_t i = 0; i < m_RefObjs.size(); ++i) - EXPECT_EQ(m_IndirectObjs[i], m_RefObjs[i]->GetDirect()); -} - -TEST_F(PDFObjectsTest, SetString) { - // Check for direct objects. - const char* const set_values[] = {"true", "fake", "3.125f", "097", - "changed", "", "NewName"}; - const char* expected[] = {"true", "false", "3.125", "97", - "changed", "", "NewName"}; - for (size_t i = 0; i < FX_ArraySize(set_values); ++i) { - m_DirectObjs[i]->SetString(set_values[i]); - EXPECT_STREQ(expected[i], m_DirectObjs[i]->GetString().c_str()); - } -} - -TEST_F(PDFObjectsTest, IsTypeAndAsType) { - // Check for direct objects. - for (size_t i = 0; i < m_DirectObjs.size(); ++i) { - if (m_DirectObjTypes[i] == CPDF_Object::ARRAY) { - EXPECT_TRUE(m_DirectObjs[i]->IsArray()); - EXPECT_EQ(m_DirectObjs[i].get(), m_DirectObjs[i]->AsArray()); - } else { - EXPECT_FALSE(m_DirectObjs[i]->IsArray()); - EXPECT_EQ(nullptr, m_DirectObjs[i]->AsArray()); - } - - if (m_DirectObjTypes[i] == CPDF_Object::BOOLEAN) { - EXPECT_TRUE(m_DirectObjs[i]->IsBoolean()); - EXPECT_EQ(m_DirectObjs[i].get(), m_DirectObjs[i]->AsBoolean()); - } else { - EXPECT_FALSE(m_DirectObjs[i]->IsBoolean()); - EXPECT_EQ(nullptr, m_DirectObjs[i]->AsBoolean()); - } - - if (m_DirectObjTypes[i] == CPDF_Object::NAME) { - EXPECT_TRUE(m_DirectObjs[i]->IsName()); - EXPECT_EQ(m_DirectObjs[i].get(), m_DirectObjs[i]->AsName()); - } else { - EXPECT_FALSE(m_DirectObjs[i]->IsName()); - EXPECT_EQ(nullptr, m_DirectObjs[i]->AsName()); - } - - if (m_DirectObjTypes[i] == CPDF_Object::NUMBER) { - EXPECT_TRUE(m_DirectObjs[i]->IsNumber()); - EXPECT_EQ(m_DirectObjs[i].get(), m_DirectObjs[i]->AsNumber()); - } else { - EXPECT_FALSE(m_DirectObjs[i]->IsNumber()); - EXPECT_EQ(nullptr, m_DirectObjs[i]->AsNumber()); - } - - if (m_DirectObjTypes[i] == CPDF_Object::STRING) { - EXPECT_TRUE(m_DirectObjs[i]->IsString()); - EXPECT_EQ(m_DirectObjs[i].get(), m_DirectObjs[i]->AsString()); - } else { - EXPECT_FALSE(m_DirectObjs[i]->IsString()); - EXPECT_EQ(nullptr, m_DirectObjs[i]->AsString()); - } - - if (m_DirectObjTypes[i] == CPDF_Object::DICTIONARY) { - EXPECT_TRUE(m_DirectObjs[i]->IsDictionary()); - EXPECT_EQ(m_DirectObjs[i].get(), m_DirectObjs[i]->AsDictionary()); - } else { - EXPECT_FALSE(m_DirectObjs[i]->IsDictionary()); - EXPECT_EQ(nullptr, m_DirectObjs[i]->AsDictionary()); - } - - if (m_DirectObjTypes[i] == CPDF_Object::STREAM) { - EXPECT_TRUE(m_DirectObjs[i]->IsStream()); - EXPECT_EQ(m_DirectObjs[i].get(), m_DirectObjs[i]->AsStream()); - } else { - EXPECT_FALSE(m_DirectObjs[i]->IsStream()); - EXPECT_EQ(nullptr, m_DirectObjs[i]->AsStream()); - } - - EXPECT_FALSE(m_DirectObjs[i]->IsReference()); - EXPECT_EQ(nullptr, m_DirectObjs[i]->AsReference()); - } - // Check indirect references. - for (size_t i = 0; i < m_RefObjs.size(); ++i) { - EXPECT_TRUE(m_RefObjs[i]->IsReference()); - EXPECT_EQ(m_RefObjs[i].get(), m_RefObjs[i]->AsReference()); - } -} - -TEST(PDFArrayTest, GetMatrix) { - float elems[][6] = {{0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}, - {1, 2, 3, 4, 5, 6}, - {2.3f, 4.05f, 3, -2, -3, 0.0f}, - {0.05f, 0.1f, 0.56f, 0.67f, 1.34f, 99.9f}}; - for (size_t i = 0; i < FX_ArraySize(elems); ++i) { - ScopedArray arr(new CPDF_Array); - CFX_Matrix matrix(elems[i][0], elems[i][1], elems[i][2], elems[i][3], - elems[i][4], elems[i][5]); - for (size_t j = 0; j < 6; ++j) - arr->AddNumber(elems[i][j]); - CFX_Matrix arr_matrix = arr->GetMatrix(); - EXPECT_EQ(matrix.GetA(), arr_matrix.GetA()); - EXPECT_EQ(matrix.GetB(), arr_matrix.GetB()); - EXPECT_EQ(matrix.GetC(), arr_matrix.GetC()); - EXPECT_EQ(matrix.GetD(), arr_matrix.GetD()); - EXPECT_EQ(matrix.GetE(), arr_matrix.GetE()); - EXPECT_EQ(matrix.GetF(), arr_matrix.GetF()); - } -} - -TEST(PDFArrayTest, GetRect) { - float elems[][4] = {{0.0f, 0.0f, 0.0f, 0.0f}, - {1, 2, 5, 6}, - {2.3f, 4.05f, -3, 0.0f}, - {0.05f, 0.1f, 1.34f, 99.9f}}; - for (size_t i = 0; i < FX_ArraySize(elems); ++i) { - ScopedArray arr(new CPDF_Array); - CFX_FloatRect rect(elems[i]); - for (size_t j = 0; j < 4; ++j) - arr->AddNumber(elems[i][j]); - CFX_FloatRect arr_rect = arr->GetRect(); - EXPECT_EQ(rect.left, arr_rect.left); - EXPECT_EQ(rect.right, arr_rect.right); - EXPECT_EQ(rect.bottom, arr_rect.bottom); - EXPECT_EQ(rect.top, arr_rect.top); - } -} - -TEST(PDFArrayTest, GetTypeAt) { - { - // Boolean array. - bool vals[] = {true, false, false, true, true}; - ScopedArray arr(new CPDF_Array); - for (size_t i = 0; i < FX_ArraySize(vals); ++i) - arr->InsertAt(i, new CPDF_Boolean(vals[i])); - for (size_t i = 0; i < FX_ArraySize(vals); ++i) { - TestArrayAccessors(arr.get(), i, // Array and index. - vals[i] ? "true" : "false", // String value. - nullptr, // Const string value. - vals[i] ? 1 : 0, // Integer value. - 0, // Float value. - nullptr, // Array value. - nullptr, // Dictionary value. - nullptr); // Stream value. - } - } - { - // Integer array. - int vals[] = {10, 0, -345, 2089345456, -1000000000, 567, 93658767}; - ScopedArray arr(new CPDF_Array); - for (size_t i = 0; i < FX_ArraySize(vals); ++i) - arr->InsertAt(i, new CPDF_Number(vals[i])); - for (size_t i = 0; i < FX_ArraySize(vals); ++i) { - char buf[33]; - TestArrayAccessors(arr.get(), i, // Array and index. - FXSYS_itoa(vals[i], buf, 10), // String value. - nullptr, // Const string value. - vals[i], // Integer value. - vals[i], // Float value. - nullptr, // Array value. - nullptr, // Dictionary value. - nullptr); // Stream value. - } - } - { - // Float array. - float vals[] = {0.0f, 0, 10, 10.0f, 0.0345f, - 897.34f, -2.5f, -1.0f, -345.0f, -0.0f}; - const char* expected_str[] = {"0", "0", "10", "10", "0.0345", - "897.34", "-2.5", "-1", "-345", "0"}; - ScopedArray arr(new CPDF_Array); - for (size_t i = 0; i < FX_ArraySize(vals); ++i) { - arr->InsertAt(i, new CPDF_Number(vals[i])); - } - for (size_t i = 0; i < FX_ArraySize(vals); ++i) { - TestArrayAccessors(arr.get(), i, // Array and index. - expected_str[i], // String value. - nullptr, // Const string value. - vals[i], // Integer value. - vals[i], // Float value. - nullptr, // Array value. - nullptr, // Dictionary value. - nullptr); // Stream value. - } - } - { - // String and name array - const char* const vals[] = {"this", "adsde$%^", "\r\t", "\"012", - ".", "EYREW", "It is a joke :)"}; - ScopedArray string_array(new CPDF_Array); - ScopedArray name_array(new CPDF_Array); - for (size_t i = 0; i < FX_ArraySize(vals); ++i) { - string_array->InsertAt(i, new CPDF_String(vals[i], false)); - name_array->InsertAt(i, new CPDF_Name(vals[i])); - } - for (size_t i = 0; i < FX_ArraySize(vals); ++i) { - TestArrayAccessors(string_array.get(), i, // Array and index. - vals[i], // String value. - vals[i], // Const string value. - 0, // Integer value. - 0, // Float value. - nullptr, // Array value. - nullptr, // Dictionary value. - nullptr); // Stream value. - TestArrayAccessors(name_array.get(), i, // Array and index. - vals[i], // String value. - vals[i], // Const string value. - 0, // Integer value. - 0, // Float value. - nullptr, // Array value. - nullptr, // Dictionary value. - nullptr); // Stream value. - } - } - { - // Null element array. - ScopedArray arr(new CPDF_Array); - for (size_t i = 0; i < 3; ++i) - arr->InsertAt(i, new CPDF_Null); - for (size_t i = 0; i < 3; ++i) { - TestArrayAccessors(arr.get(), i, // Array and index. - "", // String value. - nullptr, // Const string value. - 0, // Integer value. - 0, // Float value. - nullptr, // Array value. - nullptr, // Dictionary value. - nullptr); // Stream value. - } - } - { - // Array of array. - CPDF_Array* vals[3]; - ScopedArray arr(new CPDF_Array); - for (size_t i = 0; i < 3; ++i) { - vals[i] = new CPDF_Array; - for (size_t j = 0; j < 3; ++j) { - int value = j + 100; - vals[i]->InsertAt(i, new CPDF_Number(value)); - } - arr->InsertAt(i, vals[i]); - } - for (size_t i = 0; i < 3; ++i) { - TestArrayAccessors(arr.get(), i, // Array and index. - "", // String value. - nullptr, // Const string value. - 0, // Integer value. - 0, // Float value. - vals[i], // Array value. - nullptr, // Dictionary value. - nullptr); // Stream value. - } - } - { - // Dictionary array. - CPDF_Dictionary* vals[3]; - ScopedArray arr(new CPDF_Array); - for (size_t i = 0; i < 3; ++i) { - vals[i] = new CPDF_Dictionary; - for (size_t j = 0; j < 3; ++j) { - std::string key("key"); - char buf[33]; - key.append(FXSYS_itoa(j, buf, 10)); - int value = j + 200; - vals[i]->SetAt(CFX_ByteStringC(key.c_str()), new CPDF_Number(value)); - } - arr->InsertAt(i, vals[i]); - } - for (size_t i = 0; i < 3; ++i) { - TestArrayAccessors(arr.get(), i, // Array and index. - "", // String value. - nullptr, // Const string value. - 0, // Integer value. - 0, // Float value. - nullptr, // Array value. - vals[i], // Dictionary value. - nullptr); // Stream value. - } - } - { - // Stream array. - CPDF_Dictionary* vals[3]; - CPDF_Stream* stream_vals[3]; - ScopedArray arr(new CPDF_Array); - for (size_t i = 0; i < 3; ++i) { - vals[i] = new CPDF_Dictionary; - for (size_t j = 0; j < 3; ++j) { - std::string key("key"); - char buf[33]; - key.append(FXSYS_itoa(j, buf, 10)); - int value = j + 200; - vals[i]->SetAt(CFX_ByteStringC(key.c_str()), new CPDF_Number(value)); - } - uint8_t content[] = "content: this is a stream"; - size_t data_size = FX_ArraySize(content); - uint8_t* data = reinterpret_cast(malloc(data_size)); - memcpy(data, content, data_size); - stream_vals[i] = new CPDF_Stream(data, data_size, vals[i]); - arr->InsertAt(i, stream_vals[i]); - } - for (size_t i = 0; i < 3; ++i) { - TestArrayAccessors(arr.get(), i, // Array and index. - "", // String value. - nullptr, // Const string value. - 0, // Integer value. - 0, // Float value. - nullptr, // Array value. - vals[i], // Dictionary value. - stream_vals[i]); // Stream value. - } - } - { - // Mixed array. - ScopedArray arr(new CPDF_Array); - // Array arr will take ownership of all the objects inserted. - arr->InsertAt(0, new CPDF_Boolean(true)); - arr->InsertAt(1, new CPDF_Boolean(false)); - arr->InsertAt(2, new CPDF_Number(0)); - arr->InsertAt(3, new CPDF_Number(-1234)); - arr->InsertAt(4, new CPDF_Number(2345.0f)); - arr->InsertAt(5, new CPDF_Number(0.05f)); - arr->InsertAt(6, new CPDF_String("", false)); - arr->InsertAt(7, new CPDF_String("It is a test!", false)); - arr->InsertAt(8, new CPDF_Name("NAME")); - arr->InsertAt(9, new CPDF_Name("test")); - arr->InsertAt(10, new CPDF_Null()); - CPDF_Array* arr_val = new CPDF_Array; - arr_val->AddNumber(1); - arr_val->AddNumber(2); - arr->InsertAt(11, arr_val); - CPDF_Dictionary* dict_val = new CPDF_Dictionary; - dict_val->SetAt("key1", new CPDF_String("Linda", false)); - dict_val->SetAt("key2", new CPDF_String("Zoe", false)); - arr->InsertAt(12, dict_val); - CPDF_Dictionary* stream_dict = new CPDF_Dictionary; - stream_dict->SetAt("key1", new CPDF_String("John", false)); - stream_dict->SetAt("key2", new CPDF_String("King", false)); - uint8_t data[] = "A stream for test"; - // The data buffer will be owned by stream object, so it needs to be - // dynamically allocated. - size_t buf_size = sizeof(data); - uint8_t* buf = reinterpret_cast(malloc(buf_size)); - memcpy(buf, data, buf_size); - CPDF_Stream* stream_val = new CPDF_Stream(buf, buf_size, stream_dict); - arr->InsertAt(13, stream_val); - const char* const expected_str[] = { - "true", "false", "0", "-1234", "2345", "0.05", "", - "It is a test!", "NAME", "test", "", "", "", ""}; - const char* const expected_cstr[] = { - nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, - "It is a test!", "NAME", "test", nullptr, nullptr, nullptr, nullptr}; - const int expected_int[] = {1, 0, 0, -1234, 2345, 0, 0, - 0, 0, 0, 0, 0, 0, 0}; - const float expected_float[] = {0, 0, 0, -1234, 2345, 0.05f, 0, - 0, 0, 0, 0, 0, 0, 0}; - for (size_t i = 0; i < arr->GetCount(); ++i) { - EXPECT_STREQ(expected_str[i], arr->GetStringAt(i).c_str()); - EXPECT_STREQ(expected_cstr[i], arr->GetConstStringAt(i).GetCStr()); - EXPECT_EQ(expected_int[i], arr->GetIntegerAt(i)); - EXPECT_EQ(expected_float[i], arr->GetNumberAt(i)); - EXPECT_EQ(expected_float[i], arr->GetFloatAt(i)); - if (i == 11) - EXPECT_EQ(arr_val, arr->GetArrayAt(i)); - else - EXPECT_EQ(nullptr, arr->GetArrayAt(i)); - if (i == 13) { - EXPECT_EQ(stream_dict, arr->GetDictAt(i)); - EXPECT_EQ(stream_val, arr->GetStreamAt(i)); - } else { - EXPECT_EQ(nullptr, arr->GetStreamAt(i)); - if (i == 12) - EXPECT_EQ(dict_val, arr->GetDictAt(i)); - else - EXPECT_EQ(nullptr, arr->GetDictAt(i)); - } - } - } -} - -TEST(PDFArrayTest, AddNumber) { - float vals[] = {1.0f, -1.0f, 0, 0.456734f, - 12345.54321f, 0.5f, 1000, 0.000045f}; - ScopedArray arr(new CPDF_Array); - for (size_t i = 0; i < FX_ArraySize(vals); ++i) - arr->AddNumber(vals[i]); - for (size_t i = 0; i < FX_ArraySize(vals); ++i) { - EXPECT_EQ(CPDF_Object::NUMBER, arr->GetElement(i)->GetType()); - EXPECT_EQ(vals[i], arr->GetElement(i)->GetNumber()); - } -} - -TEST(PDFArrayTest, AddInteger) { - int vals[] = {0, 1, 934435456, 876, 10000, -1, -24354656, -100}; - ScopedArray arr(new CPDF_Array); - for (size_t i = 0; i < FX_ArraySize(vals); ++i) - arr->AddInteger(vals[i]); - for (size_t i = 0; i < FX_ArraySize(vals); ++i) { - EXPECT_EQ(CPDF_Object::NUMBER, arr->GetElement(i)->GetType()); - EXPECT_EQ(vals[i], arr->GetElement(i)->GetNumber()); - } -} - -TEST(PDFArrayTest, AddStringAndName) { - const char* vals[] = {"", "a", "ehjhRIOYTTFdfcdnv", "122323", - "$#%^&**", " ", "This is a test.\r\n"}; - ScopedArray string_array(new CPDF_Array); - ScopedArray name_array(new CPDF_Array); - for (size_t i = 0; i < FX_ArraySize(vals); ++i) { - string_array->AddString(vals[i]); - name_array->AddName(vals[i]); - } - for (size_t i = 0; i < FX_ArraySize(vals); ++i) { - EXPECT_EQ(CPDF_Object::STRING, string_array->GetElement(i)->GetType()); - EXPECT_STREQ(vals[i], string_array->GetElement(i)->GetString().c_str()); - EXPECT_EQ(CPDF_Object::NAME, name_array->GetElement(i)->GetType()); - EXPECT_STREQ(vals[i], name_array->GetElement(i)->GetString().c_str()); - } -} - -TEST(PDFArrayTest, AddReferenceAndGetElement) { - std::unique_ptr holder( - new CPDF_IndirectObjectHolder(nullptr)); - CPDF_Boolean* boolean_obj = new CPDF_Boolean(true); - CPDF_Number* int_obj = new CPDF_Number(-1234); - CPDF_Number* float_obj = new CPDF_Number(2345.089f); - CPDF_String* str_obj = new CPDF_String("Adsfdsf 343434 %&&*\n", false); - CPDF_Name* name_obj = new CPDF_Name("Title:"); - CPDF_Null* null_obj = new CPDF_Null(); - CPDF_Object* indirect_objs[] = {boolean_obj, int_obj, float_obj, - str_obj, name_obj, null_obj}; - unsigned int obj_nums[] = {2, 4, 7, 2345, 799887, 1}; - ScopedArray arr(new CPDF_Array); - ScopedArray arr1(new CPDF_Array); - // Create two arrays of references by different AddReference() APIs. - for (size_t i = 0; i < FX_ArraySize(indirect_objs); ++i) { - // All the indirect objects inserted will be owned by holder. - holder->InsertIndirectObject(obj_nums[i], indirect_objs[i]); - arr->AddReference(holder.get(), obj_nums[i]); - arr1->AddReference(holder.get(), indirect_objs[i]); - } - // Check indirect objects. - for (size_t i = 0; i < FX_ArraySize(obj_nums); ++i) - EXPECT_EQ(indirect_objs[i], holder->GetIndirectObject(obj_nums[i])); - // Check arrays. - EXPECT_EQ(arr->GetCount(), arr1->GetCount()); - for (size_t i = 0; i < arr->GetCount(); ++i) { - EXPECT_EQ(CPDF_Object::REFERENCE, arr->GetElement(i)->GetType()); - EXPECT_EQ(indirect_objs[i], arr->GetElement(i)->GetDirect()); - EXPECT_EQ(indirect_objs[i], arr->GetElementValue(i)); - EXPECT_EQ(CPDF_Object::REFERENCE, arr1->GetElement(i)->GetType()); - EXPECT_EQ(indirect_objs[i], arr1->GetElement(i)->GetDirect()); - EXPECT_EQ(indirect_objs[i], arr1->GetElementValue(i)); - } -} diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp index 0c948c5062..c5d21f4bdd 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp @@ -6,7 +6,12 @@ #include "core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.h" -#include "core/include/fpdfapi/fpdf_objects.h" +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" +#include "core/include/fpdfapi/cpdf_number.h" +#include "core/include/fpdfapi/cpdf_reference.h" +#include "core/include/fpdfapi/cpdf_stream.h" +#include "core/include/fpdfapi/cpdf_string.h" #include "core/include/fpdfapi/fpdf_parser_decode.h" #include "core/include/fxcrt/fx_ext.h" diff --git a/core/src/fpdfapi/fpdf_render/fpdf_render.cpp b/core/src/fpdfapi/fpdf_render/fpdf_render.cpp index c054f95abd..1a7a83a55f 100644 --- a/core/src/fpdfapi/fpdf_render/fpdf_render.cpp +++ b/core/src/fpdfapi/fpdf_render/fpdf_render.cpp @@ -6,6 +6,8 @@ #include "core/src/fpdfapi/fpdf_render/render_int.h" +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/cpdf_document.h" #include "core/include/fpdfapi/fpdf_module.h" #include "core/include/fpdfapi/fpdf_render.h" diff --git a/core/src/fpdfapi/fpdf_render/fpdf_render_image.cpp b/core/src/fpdfapi/fpdf_render/fpdf_render_image.cpp index 3be74c5ce8..237be06c82 100644 --- a/core/src/fpdfapi/fpdf_render/fpdf_render_image.cpp +++ b/core/src/fpdfapi/fpdf_render/fpdf_render_image.cpp @@ -9,6 +9,8 @@ #include #include +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/cpdf_document.h" #include "core/include/fpdfapi/fpdf_module.h" #include "core/include/fpdfapi/fpdf_pageobj.h" diff --git a/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp b/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp index 72472ef8fb..6618a13709 100644 --- a/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp +++ b/core/src/fpdfapi/fpdf_render/fpdf_render_loadimage.cpp @@ -10,6 +10,8 @@ #include #include +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/cpdf_document.h" #include "core/include/fpdfapi/fpdf_module.h" #include "core/include/fpdfapi/fpdf_pageobj.h" diff --git a/core/src/fpdfapi/fpdf_render/fpdf_render_pattern.cpp b/core/src/fpdfapi/fpdf_render/fpdf_render_pattern.cpp index 92e0ff4d12..7e45399eef 100644 --- a/core/src/fpdfapi/fpdf_render/fpdf_render_pattern.cpp +++ b/core/src/fpdfapi/fpdf_render/fpdf_render_pattern.cpp @@ -6,6 +6,8 @@ #include "core/src/fpdfapi/fpdf_render/render_int.h" +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/fpdf_pageobj.h" #include "core/include/fpdfapi/fpdf_render.h" #include "core/include/fxge/fx_ge.h" diff --git a/core/src/fpdfapi/fpdf_render/fpdf_render_text.cpp b/core/src/fpdfapi/fpdf_render/fpdf_render_text.cpp index 7051537ef3..c817c7c374 100644 --- a/core/src/fpdfapi/fpdf_render/fpdf_render_text.cpp +++ b/core/src/fpdfapi/fpdf_render/fpdf_render_text.cpp @@ -6,6 +6,7 @@ #include "core/src/fpdfapi/fpdf_render/render_int.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/cpdf_document.h" #include "core/include/fpdfapi/fpdf_pageobj.h" #include "core/include/fpdfapi/fpdf_render.h" diff --git a/core/src/fpdfdoc/doc_action.cpp b/core/src/fpdfdoc/doc_action.cpp index 12ec4c1907..0af219e47e 100644 --- a/core/src/fpdfdoc/doc_action.cpp +++ b/core/src/fpdfdoc/doc_action.cpp @@ -6,6 +6,7 @@ #include +#include "core/include/fpdfapi/cpdf_array.h" #include "core/include/fpdfapi/cpdf_document.h" #include "core/include/fpdfdoc/fpdf_doc.h" diff --git a/core/src/fpdfdoc/doc_annot.cpp b/core/src/fpdfdoc/doc_annot.cpp index 07fca72cab..b17347cce3 100644 --- a/core/src/fpdfdoc/doc_annot.cpp +++ b/core/src/fpdfdoc/doc_annot.cpp @@ -4,7 +4,9 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +#include "core/include/fpdfapi/cpdf_array.h" #include "core/include/fpdfapi/cpdf_document.h" +#include "core/include/fpdfapi/cpdf_reference.h" #include "core/include/fpdfapi/fpdf_pageobj.h" #include "core/include/fpdfdoc/fpdf_doc.h" diff --git a/core/src/fpdfdoc/doc_ap.cpp b/core/src/fpdfdoc/doc_ap.cpp index c252b31e7a..d91b4678ca 100644 --- a/core/src/fpdfdoc/doc_ap.cpp +++ b/core/src/fpdfdoc/doc_ap.cpp @@ -4,6 +4,7 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +#include "core/include/fpdfapi/cpdf_array.h" #include "core/include/fpdfapi/cpdf_document.h" #include "core/include/fpdfapi/cpdf_simple_parser.h" #include "core/include/fpdfdoc/fpdf_ap.h" diff --git a/core/src/fpdfdoc/doc_basic.cpp b/core/src/fpdfdoc/doc_basic.cpp index 72f8ea42f6..40c61d22bf 100644 --- a/core/src/fpdfdoc/doc_basic.cpp +++ b/core/src/fpdfdoc/doc_basic.cpp @@ -4,6 +4,7 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +#include "core/include/fpdfapi/cpdf_array.h" #include "core/include/fpdfapi/cpdf_document.h" #include "core/include/fpdfdoc/fpdf_doc.h" #include "core/src/fpdfdoc/doc_utils.h" diff --git a/core/src/fpdfdoc/doc_basic_unittest.cpp b/core/src/fpdfdoc/doc_basic_unittest.cpp index c2fd3ff2b2..60bc1345ca 100644 --- a/core/src/fpdfdoc/doc_basic_unittest.cpp +++ b/core/src/fpdfdoc/doc_basic_unittest.cpp @@ -7,6 +7,8 @@ #include #include +#include "core/include/fpdfapi/cpdf_string.h" +#include "core/include/fpdfapi/cpdf_name.h" #include "testing/gtest/include/gtest/gtest.h" #include "testing/test_support.h" diff --git a/core/src/fpdfdoc/doc_bookmark.cpp b/core/src/fpdfdoc/doc_bookmark.cpp index 71c8f039f0..c338e5d48c 100644 --- a/core/src/fpdfdoc/doc_bookmark.cpp +++ b/core/src/fpdfdoc/doc_bookmark.cpp @@ -7,7 +7,9 @@ #include #include +#include "core/include/fpdfapi/cpdf_array.h" #include "core/include/fpdfapi/cpdf_document.h" +#include "core/include/fpdfapi/cpdf_string.h" #include "core/include/fpdfdoc/fpdf_doc.h" CPDF_Bookmark CPDF_BookmarkTree::GetFirstChild( diff --git a/core/src/fpdfdoc/doc_form.cpp b/core/src/fpdfdoc/doc_form.cpp index 716d283450..076d3b6363 100644 --- a/core/src/fpdfdoc/doc_form.cpp +++ b/core/src/fpdfdoc/doc_form.cpp @@ -6,8 +6,10 @@ #include -#include "core/include/fpdfapi/cpdf_document.h" #include "core/include/fpdfapi/cfdf_document.h" +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_document.h" +#include "core/include/fpdfapi/cpdf_string.h" #include "core/include/fpdfdoc/fpdf_doc.h" #include "core/src/fpdfdoc/doc_utils.h" #include "third_party/base/stl_util.h" diff --git a/core/src/fpdfdoc/doc_formcontrol.cpp b/core/src/fpdfdoc/doc_formcontrol.cpp index 665da0acef..15ff7b5e64 100644 --- a/core/src/fpdfdoc/doc_formcontrol.cpp +++ b/core/src/fpdfdoc/doc_formcontrol.cpp @@ -6,6 +6,7 @@ #include +#include "core/include/fpdfapi/cpdf_array.h" #include "core/include/fpdfapi/cpdf_document.h" #include "core/include/fpdfdoc/fpdf_doc.h" diff --git a/core/src/fpdfdoc/doc_formfield.cpp b/core/src/fpdfdoc/doc_formfield.cpp index 2096d0ad24..09393bc30d 100644 --- a/core/src/fpdfdoc/doc_formfield.cpp +++ b/core/src/fpdfdoc/doc_formfield.cpp @@ -4,8 +4,12 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +#include "core/include/fpdfapi/cfdf_document.h" +#include "core/include/fpdfapi/cpdf_array.h" #include "core/include/fpdfapi/cpdf_document.h" +#include "core/include/fpdfapi/cpdf_number.h" #include "core/include/fpdfapi/cpdf_simple_parser.h" +#include "core/include/fpdfapi/cpdf_string.h" #include "core/include/fpdfdoc/fpdf_doc.h" #include "core/src/fpdfdoc/doc_utils.h" diff --git a/core/src/fpdfdoc/doc_link.cpp b/core/src/fpdfdoc/doc_link.cpp index d2fe67df43..a32d4c7a2d 100644 --- a/core/src/fpdfdoc/doc_link.cpp +++ b/core/src/fpdfdoc/doc_link.cpp @@ -8,6 +8,8 @@ #include +#include "core/include/fpdfapi/cpdf_array.h" + CPDF_LinkList::CPDF_LinkList() { } diff --git a/core/src/fpdfdoc/doc_ocg.cpp b/core/src/fpdfdoc/doc_ocg.cpp index 6f6e54675b..3af745580c 100644 --- a/core/src/fpdfdoc/doc_ocg.cpp +++ b/core/src/fpdfdoc/doc_ocg.cpp @@ -4,6 +4,7 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +#include "core/include/fpdfapi/cpdf_array.h" #include "core/include/fpdfapi/cpdf_document.h" #include "core/include/fpdfdoc/fpdf_doc.h" diff --git a/core/src/fpdfdoc/doc_tagged.cpp b/core/src/fpdfdoc/doc_tagged.cpp index a671875688..523800b43b 100644 --- a/core/src/fpdfdoc/doc_tagged.cpp +++ b/core/src/fpdfdoc/doc_tagged.cpp @@ -6,7 +6,10 @@ #include +#include "core/include/fpdfapi/cpdf_array.h" +#include "core/include/fpdfapi/cpdf_dictionary.h" #include "core/include/fpdfapi/cpdf_document.h" +#include "core/include/fpdfapi/cpdf_reference.h" #include "core/include/fpdfapi/fpdf_page.h" #include "core/include/fpdfdoc/fpdf_tagged.h" #include "core/src/fpdfdoc/doc_utils.h" diff --git a/core/src/fpdfdoc/doc_utils.cpp b/core/src/fpdfdoc/doc_utils.cpp index cccd49ada4..5005e213ab 100644 --- a/core/src/fpdfdoc/doc_utils.cpp +++ b/core/src/fpdfdoc/doc_utils.cpp @@ -7,6 +7,7 @@ #include #include +#include "core/include/fpdfapi/cpdf_array.h" #include "core/include/fpdfapi/cpdf_document.h" #include "core/include/fpdfapi/cpdf_simple_parser.h" #include "core/include/fpdfdoc/fpdf_doc.h" diff --git a/core/src/fpdftext/fpdf_text_int.cpp b/core/src/fpdftext/fpdf_text_int.cpp index 75677332ec..16e2c8529b 100644 --- a/core/src/fpdftext/fpdf_text_int.cpp +++ b/core/src/fpdftext/fpdf_text_int.cpp @@ -13,6 +13,8 @@ #include #include +#include "core/include/fpdfapi/cpdf_dictionary.h" +#include "core/include/fpdfapi/cpdf_string.h" #include "core/include/fpdfapi/fpdf_module.h" #include "core/include/fpdfapi/fpdf_page.h" #include "core/include/fpdfapi/fpdf_pageobj.h" diff --git a/core/src/fxcodec/jbig2/JBig2_BitStream.cpp b/core/src/fxcodec/jbig2/JBig2_BitStream.cpp index 5e99e80e7c..1fe7a26aa8 100644 --- a/core/src/fxcodec/jbig2/JBig2_BitStream.cpp +++ b/core/src/fxcodec/jbig2/JBig2_BitStream.cpp @@ -8,7 +8,7 @@ #include -#include "core/include/fpdfapi/fpdf_objects.h" +#include "core/include/fpdfapi/cpdf_stream.h" CJBig2_BitStream::CJBig2_BitStream(CPDF_StreamAcc* pSrcStream) : m_pBuf(pSrcStream->GetData()), diff --git a/core/src/fxcodec/jbig2/JBig2_Context.cpp b/core/src/fxcodec/jbig2/JBig2_Context.cpp index 399ff68657..9279971b46 100644 --- a/core/src/fxcodec/jbig2/JBig2_Context.cpp +++ b/core/src/fxcodec/jbig2/JBig2_Context.cpp @@ -11,6 +11,7 @@ #include #include +#include "core/include/fpdfapi/cpdf_stream.h" #include "core/src/fxcodec/jbig2/JBig2_ArithDecoder.h" #include "core/src/fxcodec/jbig2/JBig2_BitStream.h" #include "core/src/fxcodec/jbig2/JBig2_GrdProc.h" diff --git a/core/src/fxcodec/jbig2/JBig2_Context.h b/core/src/fxcodec/jbig2/JBig2_Context.h index ff62062c1f..68c575392a 100644 --- a/core/src/fxcodec/jbig2/JBig2_Context.h +++ b/core/src/fxcodec/jbig2/JBig2_Context.h @@ -11,7 +11,7 @@ #include #include -#include "core/include/fpdfapi/fpdf_objects.h" +#include "core/include/fpdfapi/cpdf_object.h" #include "core/include/fxcodec/fx_codec_def.h" #include "core/src/fxcodec/jbig2/JBig2_List.h" #include "core/src/fxcodec/jbig2/JBig2_Page.h" @@ -19,6 +19,7 @@ class CJBig2_ArithDecoder; class CJBig2_GRDProc; +class CPDF_StreamAcc; class IFX_Pause; // Cache is keyed by the ObjNum of a stream and an index within the stream. -- cgit v1.2.3