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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
// Copyright 2017 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.
#include "core/fpdfapi/parser/cpdf_object_avail.h"
#include <utility>
#include "core/fpdfapi/parser/cpdf_dictionary.h"
#include "core/fpdfapi/parser/cpdf_indirect_object_holder.h"
#include "core/fpdfapi/parser/cpdf_object_walker.h"
#include "core/fpdfapi/parser/cpdf_read_validator.h"
#include "core/fpdfapi/parser/cpdf_reference.h"
CPDF_ObjectAvail::CPDF_ObjectAvail(CPDF_ReadValidator* validator,
CPDF_IndirectObjectHolder* holder,
const CPDF_Object* root)
: validator_(validator), holder_(holder), root_(root) {
ASSERT(validator_);
ASSERT(holder);
ASSERT(root_);
if (!root_->IsInline())
parsed_objnums_.insert(root_->GetObjNum());
}
CPDF_ObjectAvail::CPDF_ObjectAvail(CPDF_ReadValidator* validator,
CPDF_IndirectObjectHolder* holder,
uint32_t obj_num)
: validator_(validator),
holder_(holder),
root_(pdfium::MakeUnique<CPDF_Reference>(holder, obj_num)) {
ASSERT(validator_);
ASSERT(holder);
}
CPDF_ObjectAvail::~CPDF_ObjectAvail() {}
CPDF_DataAvail::DocAvailStatus CPDF_ObjectAvail::CheckAvail() {
if (!LoadRootObject())
return CPDF_DataAvail::DocAvailStatus::DataNotAvailable;
if (CheckObjects()) {
CleanMemory();
return CPDF_DataAvail::DocAvailStatus::DataAvailable;
}
return CPDF_DataAvail::DocAvailStatus::DataNotAvailable;
}
bool CPDF_ObjectAvail::LoadRootObject() {
if (!non_parsed_objects_.empty())
return true;
while (root_ && root_->IsReference()) {
const uint32_t ref_obj_num = root_->AsReference()->GetRefObjNum();
if (HasObjectParsed(ref_obj_num)) {
root_ = nullptr;
return true;
}
const CPDF_ReadValidator::Session parse_session(validator_.Get());
const CPDF_Object* direct = holder_->GetOrParseIndirectObject(ref_obj_num);
if (validator_->has_read_problems())
return false;
parsed_objnums_.insert(ref_obj_num);
root_ = direct;
}
std::stack<uint32_t> non_parsed_objects_in_root;
if (AppendObjectSubRefs(root_.Get(), &non_parsed_objects_in_root)) {
non_parsed_objects_ = std::move(non_parsed_objects_in_root);
return true;
}
return false;
}
bool CPDF_ObjectAvail::CheckObjects() {
std::stack<uint32_t> objects_to_check = std::move(non_parsed_objects_);
std::set<uint32_t> checked_objects;
while (!objects_to_check.empty()) {
const uint32_t obj_num = objects_to_check.top();
objects_to_check.pop();
if (HasObjectParsed(obj_num))
continue;
if (!checked_objects.insert(obj_num).second)
continue;
const CPDF_ReadValidator::Session parse_session(validator_.Get());
const CPDF_Object* direct = holder_->GetOrParseIndirectObject(obj_num);
if (direct == root_.Get())
continue;
if (validator_->has_read_problems() ||
!AppendObjectSubRefs(direct, &objects_to_check)) {
non_parsed_objects_.push(obj_num);
continue;
}
parsed_objnums_.insert(obj_num);
}
return non_parsed_objects_.empty();
}
bool CPDF_ObjectAvail::AppendObjectSubRefs(const CPDF_Object* object,
std::stack<uint32_t>* refs) const {
ASSERT(refs);
if (!object)
return true;
CPDF_ObjectWalker walker(object);
while (const CPDF_Object* obj = walker.GetNext()) {
const CPDF_ReadValidator::Session parse_session(validator_.Get());
// Skip if this object if it's an inlined root, the parent object or
// explicitily excluded.
const bool skip = (walker.GetParent() && obj == root_.Get()) ||
walker.dictionary_key() == "Parent" ||
(obj != root_.Get() && ExcludeObject(obj));
// We need to parse the object before we can do the exclusion check.
// This is because the exclusion check may check against a referenced
// field of the object which we need to make sure is loaded.
if (validator_->has_read_problems())
return false;
if (skip) {
walker.SkipWalkIntoCurrentObject();
continue;
}
if (obj->IsReference())
refs->push(obj->AsReference()->GetRefObjNum());
}
return true;
}
void CPDF_ObjectAvail::CleanMemory() {
root_.Reset();
parsed_objnums_.clear();
}
bool CPDF_ObjectAvail::ExcludeObject(const CPDF_Object* object) const {
return false;
}
bool CPDF_ObjectAvail::HasObjectParsed(uint32_t obj_num) const {
return parsed_objnums_.count(obj_num) > 0;
}
|