-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpdf_file_parser.cpp
More file actions
199 lines (152 loc) · 4.93 KB
/
Copy pathpdf_file_parser.cpp
File metadata and controls
199 lines (152 loc) · 4.93 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <odr/internal/pdf/pdf_file_parser.hpp>
#include <odr/internal/pdf/pdf_file_object.hpp>
#include <odr/internal/util/stream_util.hpp>
#include <odr/internal/util/string_util.hpp>
#include <stdexcept>
namespace odr::internal::pdf {
FileParser::FileParser(std::istream &in) : m_parser(in) {}
std::istream &FileParser::in() { return m_parser.in(); }
std::streambuf &FileParser::sb() { return m_parser.sb(); }
ObjectParser &FileParser::parser() { return m_parser; }
IndirectObject FileParser::read_indirect_object() {
IndirectObject result;
result.reference.id = m_parser.read_unsigned_integer();
m_parser.skip_whitespace();
result.reference.gen = m_parser.read_unsigned_integer();
m_parser.skip_whitespace();
if (const std::string line = m_parser.read_line(); line != "obj") {
throw std::runtime_error("expected obj");
}
result.object = m_parser.read_object();
m_parser.skip_whitespace();
const std::string next = m_parser.read_line();
if (next == "endobj") {
m_parser.skip_whitespace();
return result;
}
if (next == "stream") {
result.has_stream = true;
result.stream_position = in().tellg();
m_parser.skip_whitespace();
return result;
}
throw std::runtime_error("expected stream");
}
Trailer FileParser::read_trailer() {
m_parser.expect_characters("trailer");
m_parser.skip_whitespace();
Trailer result;
result.dictionary = m_parser.read_dictionary();
result.size = result.dictionary["Size"].as_integer();
m_parser.skip_line();
m_parser.skip_whitespace();
return result;
}
Xref FileParser::read_xref() {
if (const std::string line = m_parser.read_line(); line != "xref") {
throw std::runtime_error("expected xref");
}
Xref result;
while (true) {
if (!m_parser.peek_number()) {
m_parser.skip_whitespace();
return result;
}
const std::uint32_t first_id = m_parser.read_integer();
m_parser.skip_whitespace();
const std::uint32_t entry_count = m_parser.read_integer();
m_parser.skip_line();
for (std::uint32_t i = 0; i < entry_count; ++i) {
Xref::Entry entry;
entry.position = m_parser.read_unsigned_integer();
m_parser.skip_whitespace();
const std::uint64_t generation = m_parser.read_unsigned_integer();
m_parser.skip_whitespace();
entry.in_use = m_parser.read_line().at(0) == 'n';
result.table.emplace(ObjectReference(first_id + i, generation), entry);
}
}
}
StartXref FileParser::read_start_xref() {
if (const std::string line = m_parser.read_line(); line != "startxref") {
throw std::runtime_error("expected startxref");
}
StartXref result;
result.start = m_parser.read_unsigned_integer();
m_parser.skip_line();
m_parser.skip_whitespace();
return result;
}
std::string FileParser::read_stream(const std::int32_t size) {
std::string result;
if (size >= 0) {
result = util::stream::read(in(), size);
m_parser.skip_line();
if (const std::string line = m_parser.read_line(); line != "endstream") {
throw std::runtime_error("expected endstream");
}
} else {
// TODO improve poor solution
while (true) {
std::string line = m_parser.read_line(true);
if (line == "endstream\n") {
result.pop_back();
break;
}
result += line;
}
}
if (const std::string line = m_parser.read_line(); line != "endobj") {
throw std::runtime_error("expected endobj");
}
m_parser.skip_whitespace();
return result;
}
void FileParser::read_header() {
const std::string header1 = m_parser.read_line();
const std::string header2 = m_parser.read_line();
if (!util::string::starts_with(header1, "%PDF-")) {
throw std::runtime_error("illegal header");
}
m_parser.skip_whitespace();
}
Entry FileParser::read_entry() {
std::uint32_t position = in().tellg();
const std::string entry_header = m_parser.read_line();
in().seekg(position);
if (util::string::ends_with(entry_header, "obj")) {
return {read_indirect_object(), position};
}
if (entry_header == "xref") {
return {read_xref(), position};
}
if (entry_header == "trailer") {
return {read_trailer(), position};
}
if (entry_header == "startxref") {
return {read_start_xref(), position};
}
if (entry_header == "%PDF-") {
read_header();
return {Header{}, position};
}
if (entry_header == "%%EOF") {
return {Eof{}, position};
}
throw std::runtime_error("unknown entry");
}
void FileParser::seek_start_xref(const std::uint32_t margin) {
in().seekg(0, std::ios::end);
const std::int64_t size = in().tellg();
in().seekg(std::max(static_cast<std::int64_t>(0), size - margin),
std::ios::beg);
while (!m_parser.in().eof()) {
const std::uint32_t position = m_parser.in().tellg();
if (const std::string line = m_parser.read_line(); line == "startxref") {
m_parser.in().seekg(position);
return;
}
}
throw std::runtime_error("unexpected stream exhaust");
}
} // namespace odr::internal::pdf