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_document.h" 14 #include "core/fxcrt/string_pool_template.h" 15 #include "core/fxcrt/weak_ptr.h" 16 #include "third_party/base/span.h" 17 18 class CPDF_Dictionary; 19 class CPDF_Object; 20 class CPDF_Stream; 21 22 class CPDF_StreamParser { 23 public: 24 enum SyntaxType { EndOfData, Number, Keyword, Name, Others }; 25 26 explicit CPDF_StreamParser(pdfium::span<const uint8_t> span); 27 CPDF_StreamParser(pdfium::span<const uint8_t> span, 28 const WeakPtr<ByteStringPool>& pPool); 29 ~CPDF_StreamParser(); 30 31 SyntaxType ParseNextElement(); GetWord()32 ByteStringView GetWord() const { 33 return ByteStringView(m_WordBuffer, m_WordSize); 34 } GetPos()35 uint32_t GetPos() const { return m_Pos; } SetPos(uint32_t pos)36 void SetPos(uint32_t pos) { m_Pos = pos; } GetObject()37 const RetainPtr<CPDF_Object>& GetObject() const { return m_pLastObj; } 38 RetainPtr<CPDF_Object> ReadNextObject(bool bAllowNestedArray, 39 bool bInArray, 40 uint32_t dwRecursionLevel); 41 RetainPtr<CPDF_Stream> ReadInlineStream(CPDF_Document* pDoc, 42 RetainPtr<CPDF_Dictionary> pDict, 43 const CPDF_Object* pCSObj); 44 45 private: 46 friend class cpdf_streamparser_ReadHexString_Test; 47 static const uint32_t kMaxWordLength = 255; 48 49 void GetNextWord(bool& bIsNumber); 50 ByteString ReadString(); 51 ByteString ReadHexString(); 52 bool PositionIsInBounds() const; 53 bool WordBufferMatches(const char* pWord) const; 54 55 uint32_t m_Pos = 0; // Current byte position within |m_pBuf|. 56 uint32_t m_WordSize = 0; // Current byte position within |m_WordBuffer|. 57 WeakPtr<ByteStringPool> m_pPool; 58 RetainPtr<CPDF_Object> m_pLastObj; 59 pdfium::span<const uint8_t> m_pBuf; 60 uint8_t m_WordBuffer[kMaxWordLength + 1]; // Include space for NUL. 61 }; 62 63 #endif // CORE_FPDFAPI_PAGE_CPDF_STREAMPARSER_H_ 64