1 // Copyright 2016 PDFium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #ifndef CORE_FPDFAPI_PAGE_CPDF_STREAMPARSER_H_ 8 #define CORE_FPDFAPI_PAGE_CPDF_STREAMPARSER_H_ 9 10 #include <memory> 11 #include <utility> 12 13 #include "core/fpdfapi/parser/cpdf_dictionary.h" 14 #include "core/fpdfapi/parser/cpdf_document.h" 15 #include "core/fpdfapi/parser/cpdf_object.h" 16 #include "core/fpdfapi/parser/cpdf_stream.h" 17 #include "core/fxcrt/cfx_string_pool_template.h" 18 #include "core/fxcrt/cfx_weak_ptr.h" 19 20 class CPDF_StreamParser { 21 public: 22 enum SyntaxType { EndOfData, Number, Keyword, Name, Others }; 23 24 CPDF_StreamParser(const uint8_t* pData, uint32_t dwSize); 25 CPDF_StreamParser(const uint8_t* pData, 26 uint32_t dwSize, 27 const CFX_WeakPtr<CFX_ByteStringPool>& pPool); 28 ~CPDF_StreamParser(); 29 30 SyntaxType ParseNextElement(); GetWord()31 CFX_ByteStringC GetWord() const { 32 return CFX_ByteStringC(m_WordBuffer, m_WordSize); 33 } GetPos()34 uint32_t GetPos() const { return m_Pos; } SetPos(uint32_t pos)35 void SetPos(uint32_t pos) { m_Pos = pos; } GetObject()36 std::unique_ptr<CPDF_Object> GetObject() { return std::move(m_pLastObj); } 37 std::unique_ptr<CPDF_Object> ReadNextObject(bool bAllowNestedArray, 38 bool bInArray, 39 uint32_t dwRecursionLevel); 40 std::unique_ptr<CPDF_Stream> ReadInlineStream( 41 CPDF_Document* pDoc, 42 std::unique_ptr<CPDF_Dictionary> pDict, 43 CPDF_Object* pCSObj); 44 45 private: 46 friend class cpdf_streamparser_ReadHexString_Test; 47 48 void GetNextWord(bool& bIsNumber); 49 CFX_ByteString ReadString(); 50 CFX_ByteString ReadHexString(); 51 bool PositionIsInBounds() const; 52 53 const uint8_t* m_pBuf; 54 uint32_t m_Size; // Length in bytes of m_pBuf. 55 uint32_t m_Pos; // Current byte position within m_pBuf. 56 uint8_t m_WordBuffer[256]; 57 uint32_t m_WordSize; 58 std::unique_ptr<CPDF_Object> m_pLastObj; 59 CFX_WeakPtr<CFX_ByteStringPool> m_pPool; 60 }; 61 62 #endif // CORE_FPDFAPI_PAGE_CPDF_STREAMPARSER_H_ 63