blob: f3e1062d8333f791dcbc938c9db23a39687e41d2 (
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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
// 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_walker.h"
#include <utility>
#include "core/fpdfapi/parser/cpdf_array.h"
#include "core/fpdfapi/parser/cpdf_dictionary.h"
#include "core/fpdfapi/parser/cpdf_stream.h"
#include "third_party/base/ptr_util.h"
namespace {
class StreamIterator final : public CPDF_ObjectWalker::SubobjectIterator {
public:
explicit StreamIterator(const CPDF_Stream* stream)
: SubobjectIterator(stream) {}
~StreamIterator() override {}
bool IsFinished() const override { return IsStarted() && is_finished_; }
const CPDF_Object* IncrementImpl() override {
ASSERT(IsStarted());
ASSERT(!IsFinished());
is_finished_ = true;
return object()->GetDict();
}
void Start() override {}
private:
bool is_finished_ = false;
};
class DictionaryIterator final : public CPDF_ObjectWalker::SubobjectIterator {
public:
explicit DictionaryIterator(const CPDF_Dictionary* dictionary)
: SubobjectIterator(dictionary), locker_(dictionary) {}
~DictionaryIterator() override {}
bool IsFinished() const override {
return IsStarted() && dict_iterator_ == locker_.end();
}
const CPDF_Object* IncrementImpl() override {
ASSERT(IsStarted());
ASSERT(!IsFinished());
const CPDF_Object* result = dict_iterator_->second.get();
dict_key_ = dict_iterator_->first;
++dict_iterator_;
return result;
}
void Start() override {
ASSERT(!IsStarted());
dict_iterator_ = locker_.begin();
}
ByteString dict_key() const { return dict_key_; }
private:
CPDF_Dictionary::const_iterator dict_iterator_;
CPDF_DictionaryLocker locker_;
ByteString dict_key_;
};
class ArrayIterator final : public CPDF_ObjectWalker::SubobjectIterator {
public:
explicit ArrayIterator(const CPDF_Array* array)
: SubobjectIterator(array), locker_(array) {}
~ArrayIterator() override {}
bool IsFinished() const override {
return IsStarted() && arr_iterator_ == locker_.end();
}
const CPDF_Object* IncrementImpl() override {
ASSERT(IsStarted());
ASSERT(!IsFinished());
const CPDF_Object* result = arr_iterator_->get();
++arr_iterator_;
return result;
}
void Start() override { arr_iterator_ = locker_.begin(); }
public:
CPDF_Array::const_iterator arr_iterator_;
CPDF_ArrayLocker locker_;
};
} // namespace
CPDF_ObjectWalker::SubobjectIterator::~SubobjectIterator() {}
const CPDF_Object* CPDF_ObjectWalker::SubobjectIterator::Increment() {
if (!IsStarted()) {
Start();
is_started_ = true;
}
while (!IsFinished()) {
const CPDF_Object* result = IncrementImpl();
if (result)
return result;
}
return nullptr;
}
CPDF_ObjectWalker::SubobjectIterator::SubobjectIterator(
const CPDF_Object* object)
: object_(object) {
ASSERT(object_);
}
// static
std::unique_ptr<CPDF_ObjectWalker::SubobjectIterator>
CPDF_ObjectWalker::MakeIterator(const CPDF_Object* object) {
if (object->IsStream())
return pdfium::MakeUnique<StreamIterator>(object->AsStream());
if (object->IsDictionary())
return pdfium::MakeUnique<DictionaryIterator>(object->AsDictionary());
if (object->IsArray())
return pdfium::MakeUnique<ArrayIterator>(object->AsArray());
return nullptr;
}
CPDF_ObjectWalker::CPDF_ObjectWalker(const CPDF_Object* root)
: next_object_(root) {}
CPDF_ObjectWalker::~CPDF_ObjectWalker() = default;
const CPDF_Object* CPDF_ObjectWalker::GetNext() {
while (!stack_.empty() || next_object_) {
if (next_object_) {
auto new_iterator = MakeIterator(next_object_.Get());
if (new_iterator) {
// Schedule walk within composite objects.
stack_.push(std::move(new_iterator));
}
auto* result = next_object_.Get();
next_object_ = nullptr;
return result;
}
SubobjectIterator* it = stack_.top().get();
if (it->IsFinished()) {
stack_.pop();
} else {
next_object_ = it->Increment();
parent_object_ = it->object();
dict_key_ = parent_object_->IsDictionary()
? static_cast<DictionaryIterator*>(it)->dict_key()
: ByteString();
current_depth_ = stack_.size();
}
}
dict_key_ = ByteString();
current_depth_ = 0;
return nullptr;
}
void CPDF_ObjectWalker::SkipWalkIntoCurrentObject() {
if (stack_.empty() || stack_.top()->IsStarted())
return;
stack_.pop();
}
|