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_PARSER_CPDF_SYNTAX_PARSER_H_ 8 #define CORE_FPDFAPI_PARSER_CPDF_SYNTAX_PARSER_H_ 9 10 #include <memory> 11 12 #include "core/fxcrt/cfx_string_pool_template.h" 13 #include "core/fxcrt/cfx_weak_ptr.h" 14 #include "core/fxcrt/fx_basic.h" 15 16 class CPDF_CryptoHandler; 17 class CPDF_Dictionary; 18 class CPDF_IndirectObjectHolder; 19 class CPDF_Object; 20 class CPDF_Stream; 21 class IFX_SeekableReadStream; 22 23 class CPDF_SyntaxParser { 24 public: 25 CPDF_SyntaxParser(); 26 explicit CPDF_SyntaxParser(const CFX_WeakPtr<CFX_ByteStringPool>& pPool); 27 ~CPDF_SyntaxParser(); 28 29 void InitParser(const CFX_RetainPtr<IFX_SeekableReadStream>& pFileAccess, 30 uint32_t HeaderOffset); 31 SavePos()32 FX_FILESIZE SavePos() const { return m_Pos; } RestorePos(FX_FILESIZE pos)33 void RestorePos(FX_FILESIZE pos) { m_Pos = pos; } 34 35 std::unique_ptr<CPDF_Object> GetObject(CPDF_IndirectObjectHolder* pObjList, 36 uint32_t objnum, 37 uint32_t gennum, 38 bool bDecrypt); 39 40 std::unique_ptr<CPDF_Object> GetObjectForStrict( 41 CPDF_IndirectObjectHolder* pObjList, 42 uint32_t objnum, 43 uint32_t gennum); 44 45 CFX_ByteString GetKeyword(); 46 void ToNextLine(); 47 void ToNextWord(); 48 bool SearchWord(const CFX_ByteStringC& word, 49 bool bWholeWord, 50 bool bForward, 51 FX_FILESIZE limit); 52 53 FX_FILESIZE FindTag(const CFX_ByteStringC& tag, FX_FILESIZE limit); 54 void SetEncrypt(std::unique_ptr<CPDF_CryptoHandler> pCryptoHandler); 55 bool ReadBlock(uint8_t* pBuf, uint32_t size); 56 bool GetCharAt(FX_FILESIZE pos, uint8_t& ch); 57 CFX_ByteString GetNextWord(bool* bIsNumber); 58 59 private: 60 friend class CPDF_Parser; 61 friend class CPDF_DataAvail; 62 friend class cpdf_syntax_parser_ReadHexString_Test; 63 64 static const int kParserMaxRecursionDepth = 64; 65 static int s_CurrentRecursionDepth; 66 67 uint32_t GetDirectNum(); 68 bool ReadChar(FX_FILESIZE read_pos, uint32_t read_size); 69 bool GetNextChar(uint8_t& ch); 70 bool GetCharAtBackward(FX_FILESIZE pos, uint8_t& ch); 71 void GetNextWordInternal(bool* bIsNumber); 72 bool IsWholeWord(FX_FILESIZE startpos, 73 FX_FILESIZE limit, 74 const CFX_ByteStringC& tag, 75 bool checkKeyword); 76 77 CFX_ByteString ReadString(); 78 CFX_ByteString ReadHexString(); 79 unsigned int ReadEOLMarkers(FX_FILESIZE pos); 80 std::unique_ptr<CPDF_Stream> ReadStream( 81 std::unique_ptr<CPDF_Dictionary> pDict, 82 uint32_t objnum, 83 uint32_t gennum); 84 CheckPosition(FX_FILESIZE pos)85 inline bool CheckPosition(FX_FILESIZE pos) { 86 return m_BufOffset >= pos || 87 static_cast<FX_FILESIZE>(m_BufOffset + m_BufSize) <= pos; 88 } 89 90 FX_FILESIZE m_Pos; 91 uint32_t m_MetadataObjnum; 92 CFX_RetainPtr<IFX_SeekableReadStream> m_pFileAccess; 93 FX_FILESIZE m_HeaderOffset; 94 FX_FILESIZE m_FileLen; 95 uint8_t* m_pFileBuf; 96 uint32_t m_BufSize; 97 FX_FILESIZE m_BufOffset; 98 std::unique_ptr<CPDF_CryptoHandler> m_pCryptoHandler; 99 uint8_t m_WordBuffer[257]; 100 uint32_t m_WordSize; 101 CFX_WeakPtr<CFX_ByteStringPool> m_pPool; 102 }; 103 104 #endif // CORE_FPDFAPI_PARSER_CPDF_SYNTAX_PARSER_H_ 105