summaryrefslogtreecommitdiff
path: root/core/fpdfapi/parser/cpdf_cross_ref_table.h
blob: d8bda7125fc1c79f0210702a19ea37ef1a386ae5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2018 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CORE_FPDFAPI_PARSER_CPDF_CROSS_REF_TABLE_H_
#define CORE_FPDFAPI_PARSER_CPDF_CROSS_REF_TABLE_H_

#include <map>
#include <memory>

#include "core/fxcrt/fx_system.h"

class CPDF_Dictionary;

class CPDF_CrossRefTable {
 public:
  enum class ObjectType : uint8_t {
    kFree = 0x00,
    kNormal = 0x01,
    kNotCompressed = kNormal,
    kCompressed = 0x02,
    kObjStream = 0xFF,
    kNull = kObjStream,
  };

  struct ObjectInfo {
    ObjectInfo() : pos(0), type(ObjectType::kFree), gennum(0) {}
    // if type is ObjectType::kCompressed the archive_obj_num should be used.
    // if type is ObjectType::kNotCompressed the pos should be used.
    // In other cases its are unused.
    union {
      FX_FILESIZE pos;
      uint32_t archive_obj_num;
    };
    ObjectType type;
    uint16_t gennum;
  };

  // Merge cross reference tables.  Apply top on current.
  static std::unique_ptr<CPDF_CrossRefTable> MergeUp(
      std::unique_ptr<CPDF_CrossRefTable> current,
      std::unique_ptr<CPDF_CrossRefTable> top);

  CPDF_CrossRefTable();
  explicit CPDF_CrossRefTable(std::unique_ptr<CPDF_Dictionary> trailer);
  ~CPDF_CrossRefTable();

  void AddCompressed(uint32_t obj_num, uint32_t archive_obj_num);
  void AddNormal(uint32_t obj_num, uint16_t gen_num, FX_FILESIZE pos);
  void SetFree(uint32_t obj_num);

  const CPDF_Dictionary* trailer() const { return trailer_.get(); }
  void SetTrailer(std::unique_ptr<CPDF_Dictionary> trailer);

  const ObjectInfo* GetObjectInfo(uint32_t obj_num) const;

  const std::map<uint32_t, ObjectInfo>& objects_info() const {
    return objects_info_;
  }

  void Update(std::unique_ptr<CPDF_CrossRefTable> new_cross_ref);

  void ShrinkObjectMap(uint32_t max_size);

  // The total number of entries in the file’s cross-reference table, as
  // defined by the combination of the original section and all update
  // section.
  uint32_t GetSize() const;

 private:
  void UpdateInfo(std::map<uint32_t, ObjectInfo>&& new_objects_info);
  void UpdateTrailer(std::unique_ptr<CPDF_Dictionary> new_trailer);

  std::unique_ptr<CPDF_Dictionary> trailer_;
  std::map<uint32_t, ObjectInfo> objects_info_;
};

#endif  // CORE_FPDFAPI_PARSER_CPDF_CROSS_REF_TABLE_H_