1 // Copyright 2017 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 #ifndef CORE_FPDFAPI_PARSER_CPDF_CROSS_REF_AVAIL_H_
6 #define CORE_FPDFAPI_PARSER_CPDF_CROSS_REF_AVAIL_H_
7 
8 #include <memory>
9 #include <queue>
10 #include <set>
11 
12 #include "core/fpdfapi/parser/cpdf_data_avail.h"
13 #include "core/fxcrt/unowned_ptr.h"
14 
15 class CPDF_SyntaxParser;
16 
17 class CPDF_CrossRefAvail {
18  public:
19   CPDF_CrossRefAvail(CPDF_SyntaxParser* parser,
20                      FX_FILESIZE last_crossref_offset);
21   ~CPDF_CrossRefAvail();
22 
last_crossref_offset()23   FX_FILESIZE last_crossref_offset() const { return last_crossref_offset_; }
24 
25   CPDF_DataAvail::DocAvailStatus CheckAvail();
26 
27  private:
28   enum class State {
29     kCrossRefCheck,
30     kCrossRefV4ItemCheck,
31     kCrossRefV4TrailerCheck,
32     kDone,
33   };
34 
35   bool CheckReadProblems();
36   bool CheckCrossRef();
37   bool CheckCrossRefV4();
38   bool CheckCrossRefV4Item();
39   bool CheckCrossRefV4Trailer();
40   bool CheckCrossRefStream();
41 
42   void AddCrossRefForCheck(FX_FILESIZE crossref_offset);
43 
44   fxcrt::RetainPtr<CPDF_ReadValidator> GetValidator();
45 
46   fxcrt::UnownedPtr<CPDF_SyntaxParser> parser_;
47   const FX_FILESIZE last_crossref_offset_ = 0;
48   CPDF_DataAvail::DocAvailStatus current_status_ =
49       CPDF_DataAvail::DataNotAvailable;
50   State current_state_ = State::kCrossRefCheck;
51   FX_FILESIZE current_offset_ = 0;
52   std::queue<FX_FILESIZE> cross_refs_for_check_;
53   std::set<FX_FILESIZE> registered_crossrefs_;
54 };
55 
56 #endif  // CORE_FPDFAPI_PARSER_CPDF_CROSS_REF_AVAIL_H_
57