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