1 // Copyright 2016 The PDFium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #ifndef CORE_FPDFAPI_PAGE_CPDF_CONTENTPARSER_H_ 8 #define CORE_FPDFAPI_PAGE_CPDF_CONTENTPARSER_H_ 9 10 #include <stdint.h> 11 12 #include <memory> 13 #include <vector> 14 15 #include "core/fpdfapi/page/cpdf_form.h" 16 #include "core/fpdfapi/page/cpdf_streamcontentparser.h" 17 #include "core/fxcrt/fixed_try_alloc_zeroed_data_vector.h" 18 #include "core/fxcrt/retain_ptr.h" 19 #include "core/fxcrt/unowned_ptr.h" 20 #include "third_party/abseil-cpp/absl/types/variant.h" 21 22 class CPDF_AllStates; 23 class CPDF_Array; 24 class CPDF_Page; 25 class CPDF_PageObjectHolder; 26 class CPDF_Stream; 27 class CPDF_StreamAcc; 28 class CPDF_Type3Char; 29 class PauseIndicatorIface; 30 31 class CPDF_ContentParser { 32 public: 33 explicit CPDF_ContentParser(CPDF_Page* pPage); 34 CPDF_ContentParser(RetainPtr<const CPDF_Stream> pStream, 35 CPDF_PageObjectHolder* pPageObjectHolder, 36 const CPDF_AllStates* pGraphicStates, 37 const CFX_Matrix* pParentMatrix, 38 CPDF_Type3Char* pType3Char, 39 CPDF_Form::RecursionState* recursion_state); 40 ~CPDF_ContentParser(); 41 GetCurStates()42 const CPDF_AllStates* GetCurStates() const { 43 return m_pParser ? m_pParser->GetCurStates() : nullptr; 44 } 45 // Returns whether to continue or not. 46 bool Continue(PauseIndicatorIface* pPause); 47 48 private: 49 enum class Stage : uint8_t { 50 kGetContent = 1, 51 kPrepareContent, 52 kParse, 53 kCheckClip, 54 kComplete, 55 }; 56 57 Stage GetContent(); 58 Stage PrepareContent(); 59 Stage Parse(); 60 Stage CheckClip(); 61 62 void HandlePageContentStream(const CPDF_Stream* pStream); 63 bool HandlePageContentArray(const CPDF_Array* pArray); 64 void HandlePageContentFailure(); 65 is_owned()66 bool is_owned() const { 67 return absl::holds_alternative<FixedTryAllocZeroedDataVector<uint8_t>>( 68 m_Data); 69 } 70 pdfium::span<const uint8_t> GetData() const; 71 72 Stage m_CurrentStage; 73 UnownedPtr<CPDF_PageObjectHolder> const m_pPageObjectHolder; 74 UnownedPtr<CPDF_Type3Char> m_pType3Char; // Only used when parsing forms. 75 RetainPtr<CPDF_StreamAcc> m_pSingleStream; 76 std::vector<RetainPtr<CPDF_StreamAcc>> m_StreamArray; 77 std::vector<uint32_t> m_StreamSegmentOffsets; 78 absl::variant<pdfium::span<const uint8_t>, 79 FixedTryAllocZeroedDataVector<uint8_t>> 80 m_Data; 81 uint32_t m_nStreams = 0; 82 uint32_t m_CurrentOffset = 0; 83 // Only used when parsing pages. 84 CPDF_Form::RecursionState m_RecursionState; 85 86 // Must not outlive |m_RecursionState|. 87 std::unique_ptr<CPDF_StreamContentParser> m_pParser; 88 }; 89 90 #endif // CORE_FPDFAPI_PAGE_CPDF_CONTENTPARSER_H_ 91