From 1aff265f7a210738fc817675c4391f8fe240b81d Mon Sep 17 00:00:00 2001 From: tsepez Date: Tue, 22 Nov 2016 10:45:59 -0800 Subject: Continue splitting pageint.h into per-class files Add cpdf_streamparser.h and cpdf_contentparser.h since there are already corresponding .cpp files with the same name. Review-Url: https://codereview.chromium.org/2521123003 --- BUILD.gn | 2 + core/fpdfapi/font/cpdf_type3font.cpp | 4 +- core/fpdfapi/page/cpdf_allstates.cpp | 2 + core/fpdfapi/page/cpdf_contentparser.cpp | 4 +- core/fpdfapi/page/cpdf_contentparser.h | 59 +++++++++++++ core/fpdfapi/page/cpdf_form.cpp | 1 + core/fpdfapi/page/cpdf_page.cpp | 1 + core/fpdfapi/page/cpdf_pageobjectholder.cpp | 1 + core/fpdfapi/page/cpdf_streamcontentparser.cpp | 1 + core/fpdfapi/page/cpdf_streamparser.cpp | 2 +- core/fpdfapi/page/cpdf_streamparser.h | 60 +++++++++++++ core/fpdfapi/page/cpdf_streamparser_unittest.cpp | 2 +- core/fpdfapi/page/pageint.h | 107 +---------------------- testing/libfuzzer/pdf_streamparser_fuzzer.cc | 2 +- 14 files changed, 138 insertions(+), 110 deletions(-) create mode 100644 core/fpdfapi/page/cpdf_contentparser.h create mode 100644 core/fpdfapi/page/cpdf_streamparser.h diff --git a/BUILD.gn b/BUILD.gn index dff610528e..81b2c61733 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -438,6 +438,7 @@ static_library("fpdfapi") { "core/fpdfapi/page/cpdf_contentmarkitem.cpp", "core/fpdfapi/page/cpdf_contentmarkitem.h", "core/fpdfapi/page/cpdf_contentparser.cpp", + "core/fpdfapi/page/cpdf_contentparser.h", "core/fpdfapi/page/cpdf_countedobject.h", "core/fpdfapi/page/cpdf_docpagedata.cpp", "core/fpdfapi/page/cpdf_docpagedata.h", @@ -478,6 +479,7 @@ static_library("fpdfapi") { "core/fpdfapi/page/cpdf_streamcontentparser.cpp", "core/fpdfapi/page/cpdf_streamcontentparser.h", "core/fpdfapi/page/cpdf_streamparser.cpp", + "core/fpdfapi/page/cpdf_streamparser.h", "core/fpdfapi/page/cpdf_textobject.cpp", "core/fpdfapi/page/cpdf_textobject.h", "core/fpdfapi/page/cpdf_textstate.cpp", diff --git a/core/fpdfapi/font/cpdf_type3font.cpp b/core/fpdfapi/font/cpdf_type3font.cpp index e79f16a001..8a0a115992 100644 --- a/core/fpdfapi/font/cpdf_type3font.cpp +++ b/core/fpdfapi/font/cpdf_type3font.cpp @@ -17,6 +17,8 @@ #include "core/fxcrt/fx_system.h" #include "third_party/base/stl_util.h" +#define FPDF_MAX_TYPE3_FORM_LEVEL 4 + CPDF_Type3Font::CPDF_Type3Font() : m_pCharProcs(nullptr), m_pPageResources(nullptr), @@ -90,7 +92,7 @@ void CPDF_Type3Font::CheckType3FontMetrics() { } CPDF_Type3Char* CPDF_Type3Font::LoadChar(uint32_t charcode) { - if (m_CharLoadingDepth >= _FPDF_MAX_TYPE3_FORM_LEVEL_) + if (m_CharLoadingDepth >= FPDF_MAX_TYPE3_FORM_LEVEL) return nullptr; auto it = m_CacheMap.find(charcode); diff --git a/core/fpdfapi/page/cpdf_allstates.cpp b/core/fpdfapi/page/cpdf_allstates.cpp index f88342d7d7..94bc7b466f 100644 --- a/core/fpdfapi/page/cpdf_allstates.cpp +++ b/core/fpdfapi/page/cpdf_allstates.cpp @@ -8,6 +8,8 @@ #include +#include "core/fpdfapi/page/cpdf_pageobjectholder.h" +#include "core/fpdfapi/page/cpdf_streamcontentparser.h" #include "core/fpdfapi/page/pageint.h" #include "core/fpdfapi/parser/cpdf_array.h" #include "core/fpdfapi/parser/cpdf_dictionary.h" diff --git a/core/fpdfapi/page/cpdf_contentparser.cpp b/core/fpdfapi/page/cpdf_contentparser.cpp index f581047835..0b8574bfd4 100644 --- a/core/fpdfapi/page/cpdf_contentparser.cpp +++ b/core/fpdfapi/page/cpdf_contentparser.cpp @@ -4,7 +4,7 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com -#include "core/fpdfapi/page/pageint.h" +#include "core/fpdfapi/page/cpdf_contentparser.h" #include "core/fpdfapi/font/cpdf_type3char.h" #include "core/fpdfapi/page/cpdf_allstates.h" @@ -18,6 +18,8 @@ #include "core/fpdfapi/parser/cpdf_stream_acc.h" #include "core/fxcrt/fx_safe_types.h" +#define PARSE_STEP_LIMIT 100 + CPDF_ContentParser::CPDF_ContentParser() : m_Status(Ready), m_InternalStage(STAGE_GETCONTENT), diff --git a/core/fpdfapi/page/cpdf_contentparser.h b/core/fpdfapi/page/cpdf_contentparser.h new file mode 100644 index 0000000000..5fd11f6c0b --- /dev/null +++ b/core/fpdfapi/page/cpdf_contentparser.h @@ -0,0 +1,59 @@ +// 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_FPDFAPI_PAGE_CPDF_CONTENTPARSER_H_ +#define CORE_FPDFAPI_PAGE_CPDF_CONTENTPARSER_H_ + +#include +#include + +#include "core/fpdfapi/page/cpdf_pageobjectholder.h" +#include "core/fpdfapi/page/cpdf_streamcontentparser.h" + +class CPDF_AllStates; +class CPDF_Form; +class CPDF_Page; +class CPDF_StreamAcc; +class CPDF_Type3Char; + +class CPDF_ContentParser { + public: + enum ParseStatus { Ready, ToBeContinued, Done }; + + CPDF_ContentParser(); + ~CPDF_ContentParser(); + + ParseStatus GetStatus() const { return m_Status; } + void Start(CPDF_Page* pPage); + void Start(CPDF_Form* pForm, + CPDF_AllStates* pGraphicStates, + const CFX_Matrix* pParentMatrix, + CPDF_Type3Char* pType3Char, + int level); + void Continue(IFX_Pause* pPause); + + private: + enum InternalStage { + STAGE_GETCONTENT = 1, + STAGE_PARSE, + STAGE_CHECKCLIP, + }; + + ParseStatus m_Status; + InternalStage m_InternalStage; + CPDF_PageObjectHolder* m_pObjectHolder; + bool m_bForm; + CPDF_Type3Char* m_pType3Char; + uint32_t m_nStreams; + std::unique_ptr m_pSingleStream; + std::vector> m_StreamArray; + uint8_t* m_pData; + uint32_t m_Size; + uint32_t m_CurrentOffset; + std::unique_ptr m_pParser; +}; + +#endif // CORE_FPDFAPI_PAGE_CPDF_CONTENTPARSER_H_ diff --git a/core/fpdfapi/page/cpdf_form.cpp b/core/fpdfapi/page/cpdf_form.cpp index 7291c88191..3a5fa0a74a 100644 --- a/core/fpdfapi/page/cpdf_form.cpp +++ b/core/fpdfapi/page/cpdf_form.cpp @@ -6,6 +6,7 @@ #include "core/fpdfapi/page/cpdf_form.h" +#include "core/fpdfapi/page/cpdf_contentparser.h" #include "core/fpdfapi/page/cpdf_pageobject.h" #include "core/fpdfapi/page/cpdf_pageobjectholder.h" #include "core/fpdfapi/page/pageint.h" diff --git a/core/fpdfapi/page/cpdf_page.cpp b/core/fpdfapi/page/cpdf_page.cpp index a29d9713dd..b4e2e67939 100644 --- a/core/fpdfapi/page/cpdf_page.cpp +++ b/core/fpdfapi/page/cpdf_page.cpp @@ -10,6 +10,7 @@ #include #include "core/fpdfapi/cpdf_pagerendercontext.h" +#include "core/fpdfapi/page/cpdf_contentparser.h" #include "core/fpdfapi/page/cpdf_pageobject.h" #include "core/fpdfapi/page/pageint.h" #include "core/fpdfapi/parser/cpdf_array.h" diff --git a/core/fpdfapi/page/cpdf_pageobjectholder.cpp b/core/fpdfapi/page/cpdf_pageobjectholder.cpp index 9726fa216f..3304d4eec4 100644 --- a/core/fpdfapi/page/cpdf_pageobjectholder.cpp +++ b/core/fpdfapi/page/cpdf_pageobjectholder.cpp @@ -8,6 +8,7 @@ #include +#include "core/fpdfapi/page/cpdf_contentparser.h" #include "core/fpdfapi/page/cpdf_pageobject.h" #include "core/fpdfapi/page/pageint.h" #include "core/fpdfapi/parser/cpdf_dictionary.h" diff --git a/core/fpdfapi/page/cpdf_streamcontentparser.cpp b/core/fpdfapi/page/cpdf_streamcontentparser.cpp index 2bc80cc5c9..df0d5f1117 100644 --- a/core/fpdfapi/page/cpdf_streamcontentparser.cpp +++ b/core/fpdfapi/page/cpdf_streamcontentparser.cpp @@ -23,6 +23,7 @@ #include "core/fpdfapi/page/cpdf_pathobject.h" #include "core/fpdfapi/page/cpdf_shadingobject.h" #include "core/fpdfapi/page/cpdf_shadingpattern.h" +#include "core/fpdfapi/page/cpdf_streamparser.h" #include "core/fpdfapi/page/cpdf_textobject.h" #include "core/fpdfapi/page/pageint.h" #include "core/fpdfapi/parser/cpdf_array.h" diff --git a/core/fpdfapi/page/cpdf_streamparser.cpp b/core/fpdfapi/page/cpdf_streamparser.cpp index 7b0bf6ae03..cabf3d3cb3 100644 --- a/core/fpdfapi/page/cpdf_streamparser.cpp +++ b/core/fpdfapi/page/cpdf_streamparser.cpp @@ -4,7 +4,7 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com -#include "core/fpdfapi/page/pageint.h" +#include "core/fpdfapi/page/cpdf_streamparser.h" #include diff --git a/core/fpdfapi/page/cpdf_streamparser.h b/core/fpdfapi/page/cpdf_streamparser.h new file mode 100644 index 0000000000..ce01dd04ee --- /dev/null +++ b/core/fpdfapi/page/cpdf_streamparser.h @@ -0,0 +1,60 @@ +// 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_FPDFAPI_PAGE_CPDF_STREAMPARSER_H_ +#define CORE_FPDFAPI_PAGE_CPDF_STREAMPARSER_H_ + +#include +#include + +#include "core/fpdfapi/parser/cpdf_dictionary.h" +#include "core/fpdfapi/parser/cpdf_document.h" +#include "core/fpdfapi/parser/cpdf_object.h" +#include "core/fpdfapi/parser/cpdf_stream.h" +#include "core/fxcrt/cfx_string_pool_template.h" +#include "core/fxcrt/cfx_weak_ptr.h" + +class CPDF_StreamParser { + public: + enum SyntaxType { EndOfData, Number, Keyword, Name, Others }; + + CPDF_StreamParser(const uint8_t* pData, uint32_t dwSize); + CPDF_StreamParser(const uint8_t* pData, + uint32_t dwSize, + const CFX_WeakPtr& pPool); + ~CPDF_StreamParser(); + + SyntaxType ParseNextElement(); + const uint8_t* GetWordBuf() const { return m_WordBuffer; } + uint32_t GetWordSize() const { return m_WordSize; } + uint32_t GetPos() const { return m_Pos; } + void SetPos(uint32_t pos) { m_Pos = pos; } + std::unique_ptr GetObject() { return std::move(m_pLastObj); } + std::unique_ptr ReadNextObject(bool bAllowNestedArray, + uint32_t dwInArrayLevel); + std::unique_ptr ReadInlineStream( + CPDF_Document* pDoc, + std::unique_ptr pDict, + CPDF_Object* pCSObj); + + private: + friend class cpdf_streamparser_ReadHexString_Test; + + void GetNextWord(bool& bIsNumber); + CFX_ByteString ReadString(); + CFX_ByteString ReadHexString(); + bool PositionIsInBounds() const; + + const uint8_t* m_pBuf; + uint32_t m_Size; // Length in bytes of m_pBuf. + uint32_t m_Pos; // Current byte position within m_pBuf. + uint8_t m_WordBuffer[256]; + uint32_t m_WordSize; + std::unique_ptr m_pLastObj; + CFX_WeakPtr m_pPool; +}; + +#endif // CORE_FPDFAPI_PAGE_CPDF_STREAMPARSER_H_ diff --git a/core/fpdfapi/page/cpdf_streamparser_unittest.cpp b/core/fpdfapi/page/cpdf_streamparser_unittest.cpp index f2a5a542f8..40a41befe1 100644 --- a/core/fpdfapi/page/cpdf_streamparser_unittest.cpp +++ b/core/fpdfapi/page/cpdf_streamparser_unittest.cpp @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "core/fpdfapi/page/pageint.h" +#include "core/fpdfapi/page/cpdf_streamparser.h" #include "testing/gtest/include/gtest/gtest.h" TEST(cpdf_streamparser, ReadHexString) { diff --git a/core/fpdfapi/page/pageint.h b/core/fpdfapi/page/pageint.h index 49a4845e96..6700633604 100644 --- a/core/fpdfapi/page/pageint.h +++ b/core/fpdfapi/page/pageint.h @@ -7,120 +7,17 @@ #ifndef CORE_FPDFAPI_PAGE_PAGEINT_H_ #define CORE_FPDFAPI_PAGE_PAGEINT_H_ -#include #include -#include -#include -#include #include -#include "core/fpdfapi/page/cpdf_contentmark.h" +#include "core/fpdfapi/page/cpdf_colorspace.h" #include "core/fpdfapi/page/cpdf_countedobject.h" -#include "core/fpdfapi/page/cpdf_pageobjectholder.h" -#include "core/fpdfapi/page/cpdf_streamcontentparser.h" -#include "core/fpdfapi/parser/cpdf_stream.h" -#include "core/fxcrt/cfx_string_pool_template.h" -#include "core/fxcrt/cfx_weak_ptr.h" -#include "core/fxge/cfx_pathdata.h" -#include "core/fxge/cfx_renderdevice.h" - -class CPDF_AllStates; -class CPDF_ColorSpace; + class CPDF_ExpIntFunc; -class CPDF_Font; -class CPDF_FontEncoding; -class CPDF_Form; -class CPDF_IccProfile; -class CPDF_Image; -class CPDF_ImageObject; -class CPDF_Page; class CPDF_Pattern; class CPDF_SampledFunc; class CPDF_StitchFunc; class CPDF_StreamAcc; -class CPDF_TextObject; -class CPDF_Type3Char; - -#define PARSE_STEP_LIMIT 100 - -class CPDF_StreamParser { - public: - enum SyntaxType { EndOfData, Number, Keyword, Name, Others }; - - CPDF_StreamParser(const uint8_t* pData, uint32_t dwSize); - CPDF_StreamParser(const uint8_t* pData, - uint32_t dwSize, - const CFX_WeakPtr& pPool); - ~CPDF_StreamParser(); - - SyntaxType ParseNextElement(); - uint8_t* GetWordBuf() { return m_WordBuffer; } - uint32_t GetWordSize() const { return m_WordSize; } - uint32_t GetPos() const { return m_Pos; } - void SetPos(uint32_t pos) { m_Pos = pos; } - std::unique_ptr GetObject() { return std::move(m_pLastObj); } - std::unique_ptr ReadNextObject(bool bAllowNestedArray, - uint32_t dwInArrayLevel); - std::unique_ptr ReadInlineStream( - CPDF_Document* pDoc, - std::unique_ptr pDict, - CPDF_Object* pCSObj); - - private: - friend class cpdf_streamparser_ReadHexString_Test; - - void GetNextWord(bool& bIsNumber); - CFX_ByteString ReadString(); - CFX_ByteString ReadHexString(); - bool PositionIsInBounds() const; - - const uint8_t* m_pBuf; - uint32_t m_Size; // Length in bytes of m_pBuf. - uint32_t m_Pos; // Current byte position within m_pBuf. - uint8_t m_WordBuffer[256]; - uint32_t m_WordSize; - std::unique_ptr m_pLastObj; - CFX_WeakPtr m_pPool; -}; - -#define _FPDF_MAX_TYPE3_FORM_LEVEL_ 4 - -class CPDF_ContentParser { - public: - enum ParseStatus { Ready, ToBeContinued, Done }; - - CPDF_ContentParser(); - ~CPDF_ContentParser(); - - ParseStatus GetStatus() const { return m_Status; } - void Start(CPDF_Page* pPage); - void Start(CPDF_Form* pForm, - CPDF_AllStates* pGraphicStates, - const CFX_Matrix* pParentMatrix, - CPDF_Type3Char* pType3Char, - int level); - void Continue(IFX_Pause* pPause); - - private: - enum InternalStage { - STAGE_GETCONTENT = 1, - STAGE_PARSE, - STAGE_CHECKCLIP, - }; - - ParseStatus m_Status; - InternalStage m_InternalStage; - CPDF_PageObjectHolder* m_pObjectHolder; - bool m_bForm; - CPDF_Type3Char* m_pType3Char; - uint32_t m_nStreams; - std::unique_ptr m_pSingleStream; - std::vector> m_StreamArray; - uint8_t* m_pData; - uint32_t m_Size; - uint32_t m_CurrentOffset; - std::unique_ptr m_pParser; -}; class CPDF_Function { public: diff --git a/testing/libfuzzer/pdf_streamparser_fuzzer.cc b/testing/libfuzzer/pdf_streamparser_fuzzer.cc index 277acc0a12..5cfa318c60 100644 --- a/testing/libfuzzer/pdf_streamparser_fuzzer.cc +++ b/testing/libfuzzer/pdf_streamparser_fuzzer.cc @@ -5,7 +5,7 @@ #include #include -#include "core/fpdfapi/page/pageint.h" +#include "core/fpdfapi/page/cpdf_streamparser.h" #include "core/fpdfapi/parser/cpdf_object.h" extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { -- cgit v1.2.3