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