/src/qpdf/libqpdf/QPDF_objects.cc
Line | Count | Source |
1 | | #include <qpdf/qpdf-config.h> // include first for large file support |
2 | | |
3 | | #include <qpdf/QPDF_private.hh> |
4 | | |
5 | | #include <qpdf/InputSource_private.hh> |
6 | | #include <qpdf/OffsetInputSource.hh> |
7 | | #include <qpdf/Pipeline.hh> |
8 | | #include <qpdf/QPDFExc.hh> |
9 | | #include <qpdf/QPDFLogger.hh> |
10 | | #include <qpdf/QPDFObjectHandle_private.hh> |
11 | | #include <qpdf/QPDFObject_private.hh> |
12 | | #include <qpdf/QPDFParser.hh> |
13 | | #include <qpdf/QTC.hh> |
14 | | #include <qpdf/QUtil.hh> |
15 | | #include <qpdf/Util.hh> |
16 | | |
17 | | #include <array> |
18 | | #include <atomic> |
19 | | #include <cstring> |
20 | | #include <limits> |
21 | | #include <map> |
22 | | #include <vector> |
23 | | |
24 | | using namespace qpdf; |
25 | | using namespace std::literals; |
26 | | |
27 | | using Objects = QPDF::Doc::Objects; |
28 | | using Parser = impl::Parser; |
29 | | |
30 | 5.11M | QPDFXRefEntry::QPDFXRefEntry() = default; |
31 | | |
32 | | QPDFXRefEntry::QPDFXRefEntry(int type, qpdf_offset_t field1, int field2) : |
33 | 0 | type(type), |
34 | 0 | field1(field1), |
35 | 0 | field2(field2) |
36 | 0 | { |
37 | 0 | util::assertion(type == 1 || type == 2, "invalid xref type " + std::to_string(type)); |
38 | 0 | } |
39 | | |
40 | | int |
41 | | QPDFXRefEntry::getType() const |
42 | 1.70M | { |
43 | 1.70M | return type; |
44 | 1.70M | } |
45 | | |
46 | | qpdf_offset_t |
47 | | QPDFXRefEntry::getOffset() const |
48 | 1.14M | { |
49 | 1.14M | util::assertion(type == 1, "getOffset called for xref entry of type != 1"); |
50 | 1.14M | return this->field1; |
51 | 1.14M | } |
52 | | |
53 | | int |
54 | | QPDFXRefEntry::getObjStreamNumber() const |
55 | 397k | { |
56 | 397k | util::assertion(type == 2, "getObjStreamNumber called for xref entry of type != 2"); |
57 | 397k | return QIntC::to_int(field1); |
58 | 397k | } |
59 | | |
60 | | int |
61 | | QPDFXRefEntry::getObjStreamIndex() const |
62 | 53.3k | { |
63 | 53.3k | util::assertion(type == 2, "getObjStreamIndex called for xref entry of type != 2"); |
64 | 53.3k | return field2; |
65 | 53.3k | } |
66 | | |
67 | | namespace |
68 | | { |
69 | | class InvalidInputSource: public InputSource |
70 | | { |
71 | | public: |
72 | | ~InvalidInputSource() override = default; |
73 | | qpdf_offset_t |
74 | | findAndSkipNextEOL() override |
75 | 0 | { |
76 | 0 | throwException(); |
77 | 0 | return 0; |
78 | 0 | } |
79 | | std::string const& |
80 | | getName() const override |
81 | 0 | { |
82 | 0 | static std::string name("closed input source"); |
83 | 0 | return name; |
84 | 0 | } |
85 | | qpdf_offset_t |
86 | | tell() override |
87 | 0 | { |
88 | 0 | throwException(); |
89 | 0 | return 0; |
90 | 0 | } |
91 | | void |
92 | | seek(qpdf_offset_t offset, int whence) override |
93 | 0 | { |
94 | 0 | throwException(); |
95 | 0 | } |
96 | | void |
97 | | rewind() override |
98 | 0 | { |
99 | 0 | throwException(); |
100 | 0 | } |
101 | | size_t |
102 | | read(char* buffer, size_t length) override |
103 | 0 | { |
104 | 0 | throwException(); |
105 | 0 | return 0; |
106 | 0 | } |
107 | | void |
108 | | unreadCh(char ch) override |
109 | 0 | { |
110 | 0 | throwException(); |
111 | 0 | } |
112 | | |
113 | | private: |
114 | | void |
115 | | throwException() |
116 | 0 | { |
117 | 0 | throw std::logic_error( |
118 | 0 | "QPDF operation attempted on a QPDF object with no input " |
119 | 0 | "source. QPDF operations are invalid before processFile (or " |
120 | 0 | "another process method) or after closeInputSource"); |
121 | 0 | } |
122 | | }; |
123 | | } // namespace |
124 | | |
125 | | class QPDF::ResolveRecorder final |
126 | | { |
127 | | public: |
128 | | ResolveRecorder(QPDF& qpdf, QPDFObjGen const& og) : |
129 | 505k | qpdf(qpdf), |
130 | 505k | iter(qpdf.m->resolving.insert(og).first) |
131 | 505k | { |
132 | 505k | } |
133 | | ~ResolveRecorder() |
134 | 505k | { |
135 | 505k | qpdf.m->resolving.erase(iter); |
136 | 505k | } |
137 | | |
138 | | private: |
139 | | QPDF& qpdf; |
140 | | std::set<QPDFObjGen>::const_iterator iter; |
141 | | }; |
142 | | |
143 | | class Objects::PatternFinder final: public InputSource::Finder |
144 | | { |
145 | | public: |
146 | | PatternFinder(Objects& o, bool (Objects::*checker)()) : |
147 | 72.3k | o(o), |
148 | 72.3k | checker(checker) |
149 | 72.3k | { |
150 | 72.3k | } |
151 | | ~PatternFinder() final = default; |
152 | | bool |
153 | | check() final |
154 | 54.3k | { |
155 | 54.3k | return (this->o.*checker)(); |
156 | 54.3k | } |
157 | | |
158 | | private: |
159 | | Objects& o; |
160 | | bool (Objects::*checker)(); |
161 | | }; |
162 | | |
163 | | bool |
164 | | Objects::validatePDFVersion(char const*& p, std::string& version) |
165 | 8.27k | { |
166 | 8.27k | if (!util::is_digit(*p)) { |
167 | 2.56k | return false; |
168 | 2.56k | } |
169 | 14.2k | while (util::is_digit(*p)) { |
170 | 8.50k | version.append(1, *p++); |
171 | 8.50k | } |
172 | 5.70k | if (!(*p == '.' && util::is_digit(*(p + 1)))) { |
173 | 1.22k | return false; |
174 | 1.22k | } |
175 | 4.48k | version.append(1, *p++); |
176 | 11.2k | while (util::is_digit(*p)) { |
177 | 6.77k | version.append(1, *p++); |
178 | 6.77k | } |
179 | 4.48k | return true; |
180 | 5.70k | } |
181 | | |
182 | | bool |
183 | | Objects::findHeader() |
184 | 8.27k | { |
185 | 8.27k | qpdf_offset_t global_offset = m->file->tell(); |
186 | 8.27k | std::string line = m->file->readLine(1024); |
187 | 8.27k | char const* p = line.data(); |
188 | 8.27k | util::assertion(strncmp(p, "%PDF-", 5) == 0, "findHeader is not looking at %PDF-"); |
189 | 8.27k | p += 5; |
190 | 8.27k | std::string version; |
191 | | // Note: The string returned by line.data() is always null-terminated. The code below never |
192 | | // overruns the buffer because a null character always short-circuits further advancement. |
193 | 8.27k | if (!validatePDFVersion(p, version)) { |
194 | 3.79k | return false; |
195 | 3.79k | } |
196 | 4.48k | m->pdf_version = version; |
197 | 4.48k | if (global_offset != 0) { |
198 | | // Empirical evidence strongly suggests (codified in PDF 2.0 spec) that when there is |
199 | | // leading material prior to the PDF header, all explicit offsets in the file are such that |
200 | | // 0 points to the beginning of the header. |
201 | 2.51k | m->file = std::make_shared<OffsetInputSource>(m->file, global_offset); |
202 | 2.51k | } |
203 | 4.48k | return true; |
204 | 8.27k | } |
205 | | |
206 | | bool |
207 | | Objects::findStartxref() |
208 | 8.93k | { |
209 | 8.93k | if (readToken(*m->file).isWord("startxref") && readToken(*m->file).isInteger()) { |
210 | | // Position in front of offset token |
211 | 6.12k | m->file->seek(m->file->getLastOffset(), SEEK_SET); |
212 | 6.12k | return true; |
213 | 6.12k | } |
214 | 2.81k | return false; |
215 | 8.93k | } |
216 | | |
217 | | void |
218 | | Objects::parse(char const* password) |
219 | 23.1k | { |
220 | 23.1k | if (password) { |
221 | 0 | m->encp->provided_password = password; |
222 | 0 | } |
223 | | |
224 | | // Find the header anywhere in the first 1024 bytes of the file. |
225 | 23.1k | PatternFinder hf(*this, &Objects::findHeader); |
226 | 23.1k | if (!m->file->findFirst("%PDF-", 0, 1024, hf)) { |
227 | 18.7k | warn(damagedPDF("", -1, "can't find PDF header")); |
228 | | // QPDFWriter writes files that usually require at least version 1.2 for /FlateDecode |
229 | 18.7k | m->pdf_version = "1.2"; |
230 | 18.7k | } |
231 | | |
232 | | // PDF spec says %%EOF must be found within the last 1024 bytes of/ the file. We add an extra |
233 | | // 30 characters to leave room for the startxref stuff. |
234 | 23.1k | m->file->seek(0, SEEK_END); |
235 | 23.1k | qpdf_offset_t end_offset = m->file->tell(); |
236 | 23.1k | m->xref_table_max_offset = end_offset; |
237 | | // Sanity check on object ids. All objects must appear in xref table / stream. In all realistic |
238 | | // scenarios at least 3 bytes are required. |
239 | 23.1k | if (m->xref_table_max_id > m->xref_table_max_offset / 3) { |
240 | 23.1k | m->xref_table_max_id = static_cast<int>(m->xref_table_max_offset / 3); |
241 | 23.1k | } |
242 | 23.1k | qpdf_offset_t start_offset = (end_offset > 1054 ? end_offset - 1054 : 0); |
243 | 23.1k | PatternFinder sf(*this, &Objects::findStartxref); |
244 | 23.1k | qpdf_offset_t xref_offset = 0; |
245 | 23.1k | if (m->file->findLast("startxref", start_offset, 0, sf)) { |
246 | 5.55k | xref_offset = QUtil::string_to_ll(readToken(*m->file).getValue().c_str()); |
247 | 5.55k | } |
248 | | |
249 | 23.1k | try { |
250 | 23.1k | if (xref_offset == 0) { |
251 | 17.7k | throw damagedPDF("", -1, "can't find startxref"); |
252 | 17.7k | } |
253 | 5.46k | try { |
254 | 5.46k | read_xref(xref_offset); |
255 | 5.46k | } catch (QPDFExc&) { |
256 | 4.36k | throw; |
257 | 4.36k | } catch (std::exception& e) { |
258 | 425 | throw damagedPDF("", -1, std::string("error reading xref: ") + e.what()); |
259 | 425 | } |
260 | 22.5k | } catch (QPDFExc& e) { |
261 | 22.5k | if (global::Options::inspection_mode()) { |
262 | 0 | try { |
263 | 0 | reconstruct_xref(e, xref_offset > 0); |
264 | 0 | } catch (std::exception& er) { |
265 | 0 | warn(damagedPDF("", -1, "error reconstructing xref: "s + er.what())); |
266 | 0 | } |
267 | 0 | if (!m->trailer) { |
268 | 0 | m->trailer = Dictionary::empty(); |
269 | 0 | } |
270 | 0 | return; |
271 | 0 | } |
272 | 22.5k | if (cf.surpress_recovery()) { |
273 | 0 | throw; |
274 | 0 | } |
275 | 22.5k | reconstruct_xref(e, xref_offset > 0); |
276 | 22.5k | } |
277 | | |
278 | 10.4k | m->encp->initialize(qpdf); |
279 | 10.4k | m->parsed = true; |
280 | 10.4k | if (!m->xref_table.empty() && !qpdf.getRoot().getKey("/Pages").isDictionary()) { |
281 | | // QPDFs created from JSON have an empty xref table and no root object yet. |
282 | 6 | throw damagedPDF("", -1, "unable to find page tree"); |
283 | 6 | } |
284 | 10.4k | } |
285 | | |
286 | | void |
287 | | Objects::inParse(bool v) |
288 | 405k | { |
289 | 405k | util::internal_error_if( |
290 | 405k | m->in_parse == v, "QPDF: re-entrant parsing detected" |
291 | | // This happens if QPDFParser::parse tries to resolve an indirect object while it is |
292 | | // parsing. |
293 | 405k | ); |
294 | 405k | m->in_parse = v; |
295 | 405k | } |
296 | | |
297 | | void |
298 | | Objects::setTrailer(QPDFObjectHandle obj) |
299 | 7.20k | { |
300 | 7.20k | if (m->trailer) { |
301 | 337 | return; |
302 | 337 | } |
303 | 6.86k | m->trailer = obj; |
304 | 6.86k | } |
305 | | |
306 | | void |
307 | | Objects::reconstruct_xref(QPDFExc& e, bool found_startxref) |
308 | 22.8k | { |
309 | 22.8k | if (m->reconstructed_xref) { |
310 | | // Avoid xref reconstruction infinite loops. This is getting very hard to reproduce because |
311 | | // qpdf is throwing many fewer exceptions while parsing. Most situations are warnings now. |
312 | 192 | throw e; |
313 | 192 | } |
314 | | |
315 | | // If recovery generates more than 1000 warnings, the file is so severely damaged that there |
316 | | // probably is no point trying to continue. |
317 | 22.6k | const auto max_warnings = m->warnings.size() + 1000U; |
318 | 3.62M | auto check_warnings = [this, max_warnings]() { |
319 | 3.62M | if (m->warnings.size() > max_warnings) { |
320 | 0 | throw damagedPDF("", -1, "too many errors while reconstructing cross-reference table"); |
321 | 0 | } |
322 | 3.62M | }; |
323 | | |
324 | 22.6k | m->reconstructed_xref = true; |
325 | | // We may find more objects, which may contain dangling references. |
326 | 22.6k | m->fixed_dangling_refs = false; |
327 | | |
328 | 22.6k | warn(damagedPDF("", -1, "file is damaged")); |
329 | 22.6k | warn(e); |
330 | 22.6k | warn(damagedPDF("", -1, "Attempting to reconstruct cross-reference table")); |
331 | | |
332 | | // Delete all references to type 1 (uncompressed) objects |
333 | 22.6k | std::vector<QPDFObjGen> to_delete; |
334 | 90.5k | for (auto const& iter: m->xref_table) { |
335 | 90.5k | if (iter.second.getType() == 1) { |
336 | 67.2k | to_delete.emplace_back(iter.first); |
337 | 67.2k | } |
338 | 90.5k | } |
339 | 67.2k | for (auto const& iter: to_delete) { |
340 | 67.2k | m->xref_table.erase(iter); |
341 | 67.2k | } |
342 | | |
343 | 22.6k | std::vector<std::tuple<int, int, qpdf_offset_t>> found_objects; |
344 | 22.6k | std::vector<qpdf_offset_t> trailers; |
345 | 22.6k | std::vector<qpdf_offset_t> startxrefs; |
346 | | |
347 | 22.6k | m->file->seek(0, SEEK_END); |
348 | 22.6k | qpdf_offset_t eof = m->file->tell(); |
349 | 22.6k | m->file->seek(0, SEEK_SET); |
350 | | // Don't allow very long tokens here during recovery. All the interesting tokens are covered. |
351 | 22.6k | static size_t const MAX_LEN = 10; |
352 | 3.43M | while (m->file->tell() < eof) { |
353 | 3.41M | QPDFTokenizer::Token t1 = m->objects.readToken(*m->file, MAX_LEN); |
354 | 3.41M | qpdf_offset_t token_start = m->file->tell() - toO(t1.getValue().length()); |
355 | 3.41M | if (t1.isInteger()) { |
356 | 856k | auto pos = m->file->tell(); |
357 | 856k | auto t2 = m->objects.readToken(*m->file, MAX_LEN); |
358 | 856k | if (t2.isInteger() && m->objects.readToken(*m->file, MAX_LEN).isWord("obj")) { |
359 | 163k | int obj = QUtil::string_to_int(t1.getValue().c_str()); |
360 | 163k | int gen = QUtil::string_to_int(t2.getValue().c_str()); |
361 | 163k | if (obj <= m->xref_table_max_id) { |
362 | 162k | found_objects.emplace_back(obj, gen, token_start); |
363 | 162k | } else { |
364 | 1.07k | warn(damagedPDF( |
365 | 1.07k | "", -1, "ignoring object with impossibly large id " + std::to_string(obj))); |
366 | 1.07k | } |
367 | 163k | } |
368 | 856k | m->file->seek(pos, SEEK_SET); |
369 | 2.55M | } else if (!m->trailer && t1.isWord("trailer")) { |
370 | 34.8k | trailers.emplace_back(m->file->tell()); |
371 | 2.52M | } else if (!found_startxref && t1.isWord("startxref")) { |
372 | 1.76k | startxrefs.emplace_back(m->file->tell()); |
373 | 1.76k | } |
374 | 3.41M | check_warnings(); |
375 | 3.41M | m->file->findAndSkipNextEOL(); |
376 | 3.41M | } |
377 | | |
378 | 22.6k | if (!found_startxref && !startxrefs.empty() && !found_objects.empty() && |
379 | 799 | startxrefs.back() > std::get<2>(found_objects.back())) { |
380 | 351 | auto xref_backup{m->xref_table}; |
381 | 351 | try { |
382 | 351 | m->file->seek(startxrefs.back(), SEEK_SET); |
383 | 351 | if (auto offset = QUtil::string_to_ll(readToken(*m->file).getValue().data())) { |
384 | 181 | read_xref(offset); |
385 | | |
386 | 181 | if (qpdf.getRoot().getKey("/Pages").isDictionary()) { |
387 | 5 | warn(damagedPDF( |
388 | 5 | "", -1, "startxref was more than 1024 bytes before end of file")); |
389 | 5 | m->encp->initialize(qpdf); |
390 | 5 | m->parsed = true; |
391 | 5 | m->reconstructed_xref = false; |
392 | 5 | return; |
393 | 5 | } |
394 | 181 | } |
395 | 351 | } catch (...) { |
396 | | // ok, bad luck. Do recovery. |
397 | 176 | } |
398 | 346 | m->xref_table = std::move(xref_backup); |
399 | 346 | } |
400 | | |
401 | 22.6k | auto rend = found_objects.rend(); |
402 | 184k | for (auto it = found_objects.rbegin(); it != rend; it++) { |
403 | 161k | auto [obj, gen, token_start] = *it; |
404 | 161k | insertXrefEntry(obj, 1, token_start, gen); |
405 | 161k | check_warnings(); |
406 | 161k | } |
407 | 22.6k | m->deleted_objects.clear(); |
408 | | |
409 | | // Search at most the last 100 trailer candidates. If none of them are valid, odds are this file |
410 | | // is deliberately broken. |
411 | 22.6k | int end_index = trailers.size() > 100 ? static_cast<int>(trailers.size()) - 100 : 0; |
412 | 37.6k | for (auto it = trailers.rbegin(); it != std::prev(trailers.rend(), end_index); it++) { |
413 | 17.1k | m->file->seek(*it, SEEK_SET); |
414 | 17.1k | auto t = readTrailer(); |
415 | 17.1k | if (!t.isDictionary()) { |
416 | | // Oh well. It was worth a try. |
417 | 14.1k | } else { |
418 | 2.92k | if (t.hasKey("/Root")) { |
419 | 2.12k | m->trailer = t; |
420 | 2.12k | break; |
421 | 2.12k | } |
422 | 795 | warn(damagedPDF("trailer", *it, "recovered trailer has no /Root entry")); |
423 | 795 | } |
424 | 14.9k | check_warnings(); |
425 | 14.9k | } |
426 | | |
427 | 22.6k | if (!m->trailer) { |
428 | 19.7k | qpdf_offset_t max_offset{0}; |
429 | 19.7k | size_t max_size{0}; |
430 | | // If there are any xref streams, take the last one to appear. |
431 | 81.8k | for (auto const& iter: m->xref_table) { |
432 | 81.8k | auto entry = iter.second; |
433 | 81.8k | if (entry.getType() != 1) { |
434 | 605 | continue; |
435 | 605 | } |
436 | 81.2k | auto oh = qpdf.getObject(iter.first); |
437 | 81.2k | try { |
438 | 81.2k | if (!oh.isStreamOfType("/XRef")) { |
439 | 72.4k | continue; |
440 | 72.4k | } |
441 | 81.2k | } catch (std::exception&) { |
442 | 2.69k | continue; |
443 | 2.69k | } |
444 | 6.04k | auto offset = entry.getOffset(); |
445 | 6.04k | auto size = oh.getDict().getKey("/Size").getUIntValueAsUInt(); |
446 | 6.04k | if (size > max_size || (size == max_size && offset > max_offset)) { |
447 | 5.98k | max_offset = offset; |
448 | 5.98k | setTrailer(oh.getDict()); |
449 | 5.98k | } |
450 | 6.04k | check_warnings(); |
451 | 6.04k | } |
452 | 19.7k | if (max_offset > 0) { |
453 | 5.64k | try { |
454 | 5.64k | read_xref(max_offset, true); |
455 | 5.64k | } catch (std::exception&) { |
456 | 3.13k | warn(damagedPDF( |
457 | 3.13k | "", -1, "error decoding candidate xref stream while recovering damaged file")); |
458 | 3.13k | } |
459 | 5.64k | QTC::TC("qpdf", "QPDF recover xref stream"); |
460 | 5.60k | } |
461 | 19.7k | } |
462 | | |
463 | 22.6k | if (!m->trailer || (!m->parsed && !m->trailer.getKey("/Root").isDictionary())) { |
464 | | // Try to find a Root dictionary. As a quick fix try the one with the highest object id. |
465 | 20.1k | QPDFObjectHandle root; |
466 | 261k | for (auto const& iter: m->obj_cache) { |
467 | 261k | try { |
468 | 261k | if (QPDFObjectHandle(iter.second.object).isDictionaryOfType("/Catalog")) { |
469 | 8.92k | root = iter.second.object; |
470 | 8.92k | } |
471 | 261k | } catch (std::exception&) { |
472 | 3.48k | continue; |
473 | 3.48k | } |
474 | 261k | } |
475 | 20.1k | if (root) { |
476 | 8.60k | if (!m->trailer) { |
477 | 6.82k | warn(damagedPDF( |
478 | 6.82k | "", -1, "unable to find trailer dictionary while recovering damaged file")); |
479 | 6.82k | m->trailer = QPDFObjectHandle::newDictionary(); |
480 | 6.82k | } |
481 | 8.60k | m->trailer.replaceKey("/Root", root); |
482 | 8.60k | } |
483 | 20.1k | } |
484 | | |
485 | 22.6k | if (!m->trailer) { |
486 | | // We could check the last encountered object to see if it was an xref stream. If so, we |
487 | | // could try to get the trailer from there. This may make it possible to recover files with |
488 | | // bad startxref pointers even when they have object streams. |
489 | | |
490 | 7.32k | throw damagedPDF("", -1, "unable to find trailer dictionary while recovering damaged file"); |
491 | 7.32k | } |
492 | 15.3k | if (m->xref_table.empty()) { |
493 | | // We cannot check for an empty xref table in parse because empty tables are valid when |
494 | | // creating QPDF objects from JSON. |
495 | 316 | throw damagedPDF("", -1, "unable to find objects while recovering damaged file"); |
496 | 316 | } |
497 | 14.9k | check_warnings(); |
498 | 14.9k | if (!m->parsed) { |
499 | 14.8k | m->parsed = !m->pages.empty(); |
500 | 14.8k | if (!m->parsed) { |
501 | 611 | throw damagedPDF("", -1, "unable to find any pages while recovering damaged file"); |
502 | 611 | } |
503 | 14.2k | check_warnings(); |
504 | 14.2k | } |
505 | | |
506 | | // We could iterate through the objects looking for streams and try to find objects inside of |
507 | | // them, but it's probably not worth the trouble. Acrobat can't recover files with any errors |
508 | | // in an xref stream, and this would be a real long shot anyway. If we wanted to do anything |
509 | | // that involved looking at stream contents, we'd also have to call initializeEncryption() here. |
510 | | // It's safe to call it more than once. |
511 | 14.9k | } |
512 | | |
513 | | void |
514 | | Objects::read_xref(qpdf_offset_t xref_offset, bool in_stream_recovery) |
515 | 11.2k | { |
516 | 11.2k | std::map<int, int> free_table; |
517 | 11.2k | std::set<qpdf_offset_t> visited; |
518 | 23.2k | while (xref_offset) { |
519 | 12.2k | visited.insert(xref_offset); |
520 | 12.2k | char buf[7]; |
521 | 12.2k | memset(buf, 0, sizeof(buf)); |
522 | 12.2k | m->file->seek(xref_offset, SEEK_SET); |
523 | | // Some files miss the mark a little with startxref. We could do a better job of searching |
524 | | // in the neighborhood for something that looks like either an xref table or stream, but the |
525 | | // simple heuristic of skipping whitespace can help with the xref table case and is harmless |
526 | | // with the stream case. |
527 | 12.2k | bool done = false; |
528 | 12.2k | bool skipped_space = false; |
529 | 29.2k | while (!done) { |
530 | 17.0k | char ch; |
531 | 17.0k | if (1 == m->file->read(&ch, 1)) { |
532 | 16.0k | if (util::is_space(ch)) { |
533 | 5.06k | skipped_space = true; |
534 | 11.0k | } else { |
535 | 11.0k | m->file->unreadCh(ch); |
536 | 11.0k | done = true; |
537 | 11.0k | } |
538 | 16.0k | } else { |
539 | 963 | QTC::TC("qpdf", "QPDF eof skipping spaces before xref", skipped_space ? 0 : 1); |
540 | 963 | done = true; |
541 | 963 | } |
542 | 17.0k | } |
543 | | |
544 | 12.2k | m->file->read(buf, sizeof(buf) - 1); |
545 | | // The PDF spec says xref must be followed by a line terminator, but files exist in the wild |
546 | | // where it is terminated by arbitrary whitespace. |
547 | 12.2k | if ((strncmp(buf, "xref", 4) == 0) && util::is_space(buf[4])) { |
548 | 1.65k | if (skipped_space) { |
549 | 98 | warn(damagedPDF("", -1, "extraneous whitespace seen before xref")); |
550 | 98 | } |
551 | 1.65k | QTC::TC( |
552 | 1.65k | "qpdf", |
553 | 1.65k | "QPDF xref space", |
554 | 1.65k | ((buf[4] == '\n') ? 0 |
555 | 1.65k | : (buf[4] == '\r') ? 1 |
556 | 1.19k | : (buf[4] == ' ') ? 2 |
557 | 366 | : 9999)); |
558 | 1.65k | int skip = 4; |
559 | | // buf is null-terminated, and util::is_space('\0') is false, so this won't overrun. |
560 | 3.48k | while (util::is_space(buf[skip])) { |
561 | 1.83k | ++skip; |
562 | 1.83k | } |
563 | 1.65k | xref_offset = read_xrefTable(xref_offset + skip); |
564 | 10.5k | } else { |
565 | 10.5k | xref_offset = read_xrefStream(xref_offset, in_stream_recovery); |
566 | 10.5k | } |
567 | 12.2k | if (visited.contains(xref_offset)) { |
568 | 228 | throw damagedPDF("", -1, "loop detected following xref tables"); |
569 | 228 | } |
570 | 12.2k | } |
571 | | |
572 | 11.0k | if (!m->trailer) { |
573 | 0 | throw damagedPDF("", -1, "unable to find trailer while reading xref"); |
574 | 0 | } |
575 | 11.0k | int size = m->trailer.getKey("/Size").getIntValueAsInt(); |
576 | 11.0k | int max_obj = 0; |
577 | 11.0k | if (!m->xref_table.empty()) { |
578 | 2.84k | max_obj = m->xref_table.rbegin()->first.getObj(); |
579 | 2.84k | } |
580 | 11.0k | if (!m->deleted_objects.empty()) { |
581 | 1.41k | max_obj = std::max(max_obj, *(m->deleted_objects.rbegin())); |
582 | 1.41k | } |
583 | 11.0k | if (size < 1 || (size - 1) != max_obj) { |
584 | 2.29k | if (size == (max_obj + 2) && qpdf.getObject(max_obj + 1, 0).isStreamOfType("/XRef")) { |
585 | 1 | warn(damagedPDF( |
586 | 1 | "", |
587 | 1 | -1, |
588 | 1 | "xref entry for the xref stream itself is missing - a common error handled " |
589 | 1 | "correctly by qpdf and most other applications")); |
590 | 2.29k | } else { |
591 | 2.29k | warn(damagedPDF( |
592 | 2.29k | "", |
593 | 2.29k | -1, |
594 | 2.29k | ("reported number of objects (" + std::to_string(size) + |
595 | 2.29k | ") is not one plus the highest object number (" + std::to_string(max_obj) + ")"))); |
596 | 2.29k | } |
597 | 2.29k | } |
598 | | |
599 | | // We no longer need the deleted_objects table, so go ahead and clear it out to make sure we |
600 | | // never depend on its being set. |
601 | 11.0k | m->deleted_objects.clear(); |
602 | | |
603 | | // Make sure we keep only the highest generation for any object. |
604 | 11.0k | QPDFObjGen last_og{-1, 0}; |
605 | 387k | for (auto const& item: m->xref_table) { |
606 | 387k | auto id = item.first.getObj(); |
607 | 387k | if (id == last_og.getObj() && id > 0) { |
608 | 2.46k | qpdf.removeObject(last_og); |
609 | 2.46k | } |
610 | 387k | last_og = item.first; |
611 | 387k | } |
612 | 11.0k | } |
613 | | |
614 | | bool |
615 | | Objects::parse_xrefFirst(std::string const& line, int& obj, int& num, int& bytes) |
616 | 8.13k | { |
617 | | // is_space and is_digit both return false on '\0', so this will not overrun the null-terminated |
618 | | // buffer. |
619 | 8.13k | char const* p = line.c_str(); |
620 | 8.13k | char const* start = line.c_str(); |
621 | | |
622 | | // Skip zero or more spaces |
623 | 10.0k | while (util::is_space(*p)) { |
624 | 1.90k | ++p; |
625 | 1.90k | } |
626 | | // Require digit |
627 | 8.13k | if (!util::is_digit(*p)) { |
628 | 181 | return false; |
629 | 181 | } |
630 | | // Gather digits |
631 | 7.95k | std::string obj_str; |
632 | 33.9k | while (util::is_digit(*p)) { |
633 | 26.0k | obj_str.append(1, *p++); |
634 | 26.0k | } |
635 | | // Require space |
636 | 7.95k | if (!util::is_space(*p)) { |
637 | 102 | return false; |
638 | 102 | } |
639 | | // Skip spaces |
640 | 27.1k | while (util::is_space(*p)) { |
641 | 19.3k | ++p; |
642 | 19.3k | } |
643 | | // Require digit |
644 | 7.84k | if (!util::is_digit(*p)) { |
645 | 93 | return false; |
646 | 93 | } |
647 | | // Gather digits |
648 | 7.75k | std::string num_str; |
649 | 35.8k | while (util::is_digit(*p)) { |
650 | 28.1k | num_str.append(1, *p++); |
651 | 28.1k | } |
652 | | // Skip any space including line terminators |
653 | 23.2k | while (util::is_space(*p)) { |
654 | 15.5k | ++p; |
655 | 15.5k | } |
656 | 7.75k | bytes = toI(p - start); |
657 | 7.75k | obj = QUtil::string_to_int(obj_str.c_str()); |
658 | 7.75k | num = QUtil::string_to_int(num_str.c_str()); |
659 | 7.75k | return true; |
660 | 7.84k | } |
661 | | |
662 | | bool |
663 | | Objects::read_bad_xrefEntry(qpdf_offset_t& f1, int& f2, char& type) |
664 | 7.29k | { |
665 | | // Reposition after initial read attempt and reread. |
666 | 7.29k | m->file->seek(m->file->getLastOffset(), SEEK_SET); |
667 | 7.29k | auto line = m->file->readLine(30); |
668 | | |
669 | | // is_space and is_digit both return false on '\0', so this will not overrun the null-terminated |
670 | | // buffer. |
671 | 7.29k | char const* p = line.data(); |
672 | | |
673 | | // Skip zero or more spaces. There aren't supposed to be any. |
674 | 7.29k | bool invalid = false; |
675 | 18.0k | while (util::is_space(*p)) { |
676 | 10.7k | ++p; |
677 | 10.7k | invalid = true; |
678 | 10.7k | } |
679 | | // Require digit |
680 | 7.29k | if (!util::is_digit(*p)) { |
681 | 21 | return false; |
682 | 21 | } |
683 | | // Gather digits |
684 | 7.27k | std::string f1_str; |
685 | 33.2k | while (util::is_digit(*p)) { |
686 | 26.0k | f1_str.append(1, *p++); |
687 | 26.0k | } |
688 | | // Require space |
689 | 7.27k | if (!util::is_space(*p)) { |
690 | 23 | return false; |
691 | 23 | } |
692 | 7.25k | if (util::is_space(*(p + 1))) { |
693 | 2.07k | invalid = true; |
694 | 2.07k | } |
695 | | // Skip spaces |
696 | 24.7k | while (util::is_space(*p)) { |
697 | 17.4k | ++p; |
698 | 17.4k | } |
699 | | // Require digit |
700 | 7.25k | if (!util::is_digit(*p)) { |
701 | 40 | return false; |
702 | 40 | } |
703 | | // Gather digits |
704 | 7.21k | std::string f2_str; |
705 | 29.3k | while (util::is_digit(*p)) { |
706 | 22.1k | f2_str.append(1, *p++); |
707 | 22.1k | } |
708 | | // Require space |
709 | 7.21k | if (!util::is_space(*p)) { |
710 | 49 | return false; |
711 | 49 | } |
712 | 7.16k | if (util::is_space(*(p + 1))) { |
713 | 1.48k | invalid = true; |
714 | 1.48k | } |
715 | | // Skip spaces |
716 | 17.1k | while (util::is_space(*p)) { |
717 | 9.97k | ++p; |
718 | 9.97k | } |
719 | 7.16k | if ((*p == 'f') || (*p == 'n')) { |
720 | 7.03k | type = *p; |
721 | 7.03k | } else { |
722 | 129 | return false; |
723 | 129 | } |
724 | 7.03k | if ((f1_str.length() != 10) || (f2_str.length() != 5)) { |
725 | 6.67k | invalid = true; |
726 | 6.67k | } |
727 | | |
728 | 7.03k | if (invalid) { |
729 | 6.69k | warn(damagedPDF("xref table", "accepting invalid xref table entry")); |
730 | 6.69k | } |
731 | | |
732 | 7.03k | f1 = QUtil::string_to_ll(f1_str.c_str()); |
733 | 7.03k | f2 = QUtil::string_to_int(f2_str.c_str()); |
734 | | |
735 | 7.03k | return true; |
736 | 7.16k | } |
737 | | |
738 | | // Optimistically read and parse xref entry. If entry is bad, call read_bad_xrefEntry and return |
739 | | // result. |
740 | | bool |
741 | | Objects::read_xrefEntry(qpdf_offset_t& f1, int& f2, char& type) |
742 | 16.0k | { |
743 | 16.0k | std::array<char, 21> line; |
744 | 16.0k | if (m->file->read(line.data(), 20) != 20) { |
745 | | // C++20: [[unlikely]] |
746 | 195 | return false; |
747 | 195 | } |
748 | 15.8k | line[20] = '\0'; |
749 | 15.8k | char const* p = line.data(); |
750 | | |
751 | 15.8k | int f1_len = 0; |
752 | 15.8k | int f2_len = 0; |
753 | | |
754 | | // is_space and is_digit both return false on '\0', so this will not overrun the null-terminated |
755 | | // buffer. |
756 | | |
757 | | // Gather f1 digits. NB No risk of overflow as 9'999'999'999 < max long long. |
758 | 75.4k | while (*p == '0') { |
759 | 59.5k | ++f1_len; |
760 | 59.5k | ++p; |
761 | 59.5k | } |
762 | 57.5k | while (util::is_digit(*p) && f1_len++ < 10) { |
763 | 41.7k | f1 *= 10; |
764 | 41.7k | f1 += *p++ - '0'; |
765 | 41.7k | } |
766 | | // Require space |
767 | 15.8k | if (!util::is_space(*p++)) { |
768 | | // Entry doesn't start with space or digit. |
769 | | // C++20: [[unlikely]] |
770 | 73 | return false; |
771 | 73 | } |
772 | | // Gather digits. NB No risk of overflow as 99'999 < max int. |
773 | 49.7k | while (*p == '0') { |
774 | 33.9k | ++f2_len; |
775 | 33.9k | ++p; |
776 | 33.9k | } |
777 | 38.4k | while (util::is_digit(*p) && f2_len++ < 5) { |
778 | 22.7k | f2 *= 10; |
779 | 22.7k | f2 += static_cast<int>(*p++ - '0'); |
780 | 22.7k | } |
781 | 15.7k | if (util::is_space(*p++) && (*p == 'f' || *p == 'n')) { |
782 | | // C++20: [[likely]] |
783 | 11.8k | type = *p; |
784 | | // No test for valid line[19]. |
785 | 11.8k | if (*(++p) && *(++p) && (*p == '\n' || *p == '\r') && f1_len == 10 && f2_len == 5) { |
786 | | // C++20: [[likely]] |
787 | 8.45k | return true; |
788 | 8.45k | } |
789 | 11.8k | } |
790 | 7.29k | return read_bad_xrefEntry(f1, f2, type); |
791 | 15.7k | } |
792 | | |
793 | | // Read a single cross-reference table section and associated trailer. |
794 | | qpdf_offset_t |
795 | | Objects::read_xrefTable(qpdf_offset_t xref_offset) |
796 | 1.65k | { |
797 | 1.65k | m->file->seek(xref_offset, SEEK_SET); |
798 | 1.65k | std::string line; |
799 | 8.16k | while (true) { |
800 | 8.13k | line.assign(50, '\0'); |
801 | 8.13k | m->file->read(line.data(), line.size()); |
802 | 8.13k | int obj = 0; |
803 | 8.13k | int num = 0; |
804 | 8.13k | int bytes = 0; |
805 | 8.13k | if (!parse_xrefFirst(line, obj, num, bytes)) { |
806 | 376 | throw damagedPDF("xref table", "xref syntax invalid"); |
807 | 376 | } |
808 | 7.75k | m->file->seek(m->file->getLastOffset() + bytes, SEEK_SET); |
809 | 23.2k | for (qpdf_offset_t i = obj; i - num < obj; ++i) { |
810 | 16.0k | if (i == 0) { |
811 | | // This is needed by checkLinearization() |
812 | 499 | first_xref_item_offset_ = m->file->tell(); |
813 | 499 | } |
814 | | // For xref_table, these will always be small enough to be ints |
815 | 16.0k | qpdf_offset_t f1 = 0; |
816 | 16.0k | int f2 = 0; |
817 | 16.0k | char type = '\0'; |
818 | 16.0k | if (!read_xrefEntry(f1, f2, type)) { |
819 | 530 | throw damagedPDF( |
820 | 530 | "xref table", "invalid xref entry (obj=" + std::to_string(i) + ")"); |
821 | 530 | } |
822 | 15.4k | if (type == 'f') { |
823 | 5.48k | insertFreeXrefEntry(QPDFObjGen(toI(i), f2)); |
824 | 10.0k | } else { |
825 | 10.0k | insertXrefEntry(toI(i), 1, f1, f2); |
826 | 10.0k | } |
827 | 15.4k | } |
828 | 7.22k | qpdf_offset_t pos = m->file->tell(); |
829 | 7.22k | if (readToken(*m->file).isWord("trailer")) { |
830 | 714 | break; |
831 | 6.51k | } else { |
832 | 6.51k | m->file->seek(pos, SEEK_SET); |
833 | 6.51k | } |
834 | 7.22k | } |
835 | | |
836 | | // Set offset to previous xref table if any |
837 | 746 | QPDFObjectHandle cur_trailer = m->objects.readTrailer(); |
838 | 746 | if (!cur_trailer.isDictionary()) { |
839 | 78 | throw damagedPDF("", "expected trailer dictionary"); |
840 | 78 | } |
841 | | |
842 | 668 | if (!m->trailer) { |
843 | 586 | setTrailer(cur_trailer); |
844 | | |
845 | 586 | if (!m->trailer.hasKey("/Size")) { |
846 | 212 | throw damagedPDF("trailer", "trailer dictionary lacks /Size key"); |
847 | 212 | } |
848 | 374 | if (!m->trailer.getKey("/Size").isInteger()) { |
849 | 4 | throw damagedPDF("trailer", "/Size key in trailer dictionary is not an integer"); |
850 | 4 | } |
851 | 374 | } |
852 | | |
853 | 452 | if (cur_trailer.hasKey("/XRefStm")) { |
854 | 26 | if (cf.ignore_xref_streams()) { |
855 | 0 | QTC::TC("qpdf", "QPDF ignoring XRefStm in trailer"); |
856 | 26 | } else { |
857 | 26 | if (cur_trailer.getKey("/XRefStm").isInteger()) { |
858 | | // Read the xref stream but disregard any return value -- we'll use our trailer's |
859 | | // /Prev key instead of the xref stream's. |
860 | 25 | (void)read_xrefStream(cur_trailer.getKey("/XRefStm").getIntValue()); |
861 | 25 | } else { |
862 | 1 | throw damagedPDF("xref stream", xref_offset, "invalid /XRefStm"); |
863 | 1 | } |
864 | 26 | } |
865 | 26 | } |
866 | | |
867 | 451 | if (cur_trailer.hasKey("/Prev")) { |
868 | 132 | if (!cur_trailer.getKey("/Prev").isInteger()) { |
869 | 1 | throw damagedPDF("trailer", "/Prev key in trailer dictionary is not an integer"); |
870 | 1 | } |
871 | 131 | return cur_trailer.getKey("/Prev").getIntValue(); |
872 | 132 | } |
873 | | |
874 | 319 | return 0; |
875 | 451 | } |
876 | | |
877 | | // Read a single cross-reference stream. |
878 | | qpdf_offset_t |
879 | | Objects::read_xrefStream(qpdf_offset_t xref_offset, bool in_stream_recovery) |
880 | 10.3k | { |
881 | 10.3k | if (!cf.ignore_xref_streams()) { |
882 | 10.3k | QPDFObjectHandle xref_obj; |
883 | 10.3k | try { |
884 | 10.3k | m->in_read_xref_stream = true; |
885 | 10.3k | xref_obj = readObjectAtOffset(xref_offset, "xref stream", true); |
886 | 10.3k | } catch (QPDFExc&) { |
887 | | // ignore -- report error below |
888 | 1.97k | } |
889 | 10.3k | m->in_read_xref_stream = false; |
890 | 10.3k | if (xref_obj.isStreamOfType("/XRef")) { |
891 | 7.36k | return processXRefStream(xref_offset, xref_obj, in_stream_recovery); |
892 | 7.36k | } |
893 | 10.3k | } |
894 | | |
895 | 2.93k | throw damagedPDF("", xref_offset, "xref not found"); |
896 | 0 | return 0; // unreachable |
897 | 10.3k | } |
898 | | |
899 | | // Return the entry size of the xref stream and the processed W array. |
900 | | std::pair<int, std::array<int, 3>> |
901 | | Objects::processXRefW(QPDFObjectHandle& dict, std::function<QPDFExc(std::string_view)> damaged) |
902 | 7.36k | { |
903 | 7.36k | auto W_obj = dict.getKey("/W"); |
904 | 7.36k | if (!(W_obj.size() >= 3 && W_obj.getArrayItem(0).isInteger() && |
905 | 7.08k | W_obj.getArrayItem(1).isInteger() && W_obj.getArrayItem(2).isInteger())) { |
906 | 343 | throw damaged("Cross-reference stream does not have a proper /W key"); |
907 | 343 | } |
908 | | |
909 | 7.02k | std::array<int, 3> W; |
910 | 7.02k | int entry_size = 0; |
911 | 7.02k | auto w_vector = W_obj.getArrayAsVector(); |
912 | 7.02k | int max_bytes = sizeof(qpdf_offset_t); |
913 | 27.9k | for (size_t i = 0; i < 3; ++i) { |
914 | 20.9k | W[i] = w_vector[i].getIntValueAsInt(); |
915 | 20.9k | if (W[i] > max_bytes) { |
916 | 17 | throw damaged("Cross-reference stream's /W contains impossibly large values"); |
917 | 17 | } |
918 | 20.9k | if (W[i] < 0) { |
919 | 49 | throw damaged("Cross-reference stream's /W contains negative values"); |
920 | 49 | } |
921 | 20.9k | entry_size += W[i]; |
922 | 20.9k | } |
923 | 6.95k | if (entry_size == 0) { |
924 | 4 | throw damaged("Cross-reference stream's /W indicates entry size of 0"); |
925 | 4 | } |
926 | 6.95k | return {entry_size, W}; |
927 | 6.95k | } |
928 | | |
929 | | // Validate Size key and return the maximum number of entries that the xref stream can contain. |
930 | | int |
931 | | Objects::processXRefSize( |
932 | | QPDFObjectHandle& dict, int entry_size, std::function<QPDFExc(std::string_view)> damaged) |
933 | 6.94k | { |
934 | | // Number of entries is limited by the highest possible object id and stream size. |
935 | 6.94k | auto max_num_entries = std::numeric_limits<int>::max(); |
936 | 6.94k | if (max_num_entries > (std::numeric_limits<qpdf_offset_t>::max() / entry_size)) { |
937 | 0 | max_num_entries = toI(std::numeric_limits<qpdf_offset_t>::max() / entry_size); |
938 | 0 | } |
939 | | |
940 | 6.94k | auto Size_obj = dict.getKey("/Size"); |
941 | 6.94k | long long size; |
942 | 6.94k | if (!dict.getKey("/Size").getValueAsInt(size)) { |
943 | 89 | throw damaged("Cross-reference stream does not have a proper /Size key"); |
944 | 6.86k | } else if (size < 0) { |
945 | 64 | throw damaged("Cross-reference stream has a negative /Size key"); |
946 | 6.79k | } else if (size >= max_num_entries) { |
947 | 89 | throw damaged("Cross-reference stream has an impossibly large /Size key"); |
948 | 89 | } |
949 | | // We are not validating that Size <= (Size key of parent xref / trailer). |
950 | 6.70k | return max_num_entries; |
951 | 6.94k | } |
952 | | |
953 | | // Return the number of entries of the xref stream and the processed Index array. |
954 | | std::pair<int, std::vector<std::pair<int, int>>> |
955 | | Objects::processXRefIndex( |
956 | | QPDFObjectHandle& dict, int max_num_entries, std::function<QPDFExc(std::string_view)> damaged) |
957 | 6.70k | { |
958 | 6.70k | auto size = dict.getKey("/Size").getIntValueAsInt(); |
959 | 6.70k | auto Index_obj = dict.getKey("/Index"); |
960 | | |
961 | 6.70k | if (Index_obj.isArray()) { |
962 | 1.26k | std::vector<std::pair<int, int>> indx; |
963 | 1.26k | int num_entries = 0; |
964 | 1.26k | auto index_vec = Index_obj.getArrayAsVector(); |
965 | 1.26k | if ((index_vec.size() % 2) || index_vec.size() < 2) { |
966 | 19 | throw damaged("Cross-reference stream's /Index has an invalid number of values"); |
967 | 19 | } |
968 | | |
969 | 1.24k | int i = 0; |
970 | 1.24k | long long first = 0; |
971 | 9.32k | for (auto& val: index_vec) { |
972 | 9.32k | if (val.isInteger()) { |
973 | 9.30k | if (i % 2) { |
974 | 4.59k | auto count = val.getIntValue(); |
975 | 4.59k | if (count <= 0) { |
976 | 61 | throw damaged( |
977 | 61 | "Cross-reference stream section claims to contain " + |
978 | 61 | std::to_string(count) + " entries"); |
979 | 61 | } |
980 | | // We are guarding against the possibility of num_entries * entry_size |
981 | | // overflowing. We are not checking that entries are in ascending order as |
982 | | // required by the spec, which probably should generate a warning. We are also |
983 | | // not checking that for each subsection first object number + number of entries |
984 | | // <= /Size. The spec requires us to ignore object number > /Size. |
985 | 4.53k | if (first > (max_num_entries - count) || |
986 | 4.46k | count > (max_num_entries - num_entries)) { |
987 | 87 | throw damaged( |
988 | 87 | "Cross-reference stream claims to contain too many entries: " + |
989 | 87 | std::to_string(first) + " " + std::to_string(max_num_entries) + " " + |
990 | 87 | std::to_string(num_entries)); |
991 | 87 | } |
992 | 4.44k | indx.emplace_back(static_cast<int>(first), static_cast<int>(count)); |
993 | 4.44k | num_entries += static_cast<int>(count); |
994 | 4.71k | } else { |
995 | 4.71k | first = val.getIntValue(); |
996 | 4.71k | if (first < 0) { |
997 | 49 | throw damaged( |
998 | 49 | "Cross-reference stream's /Index contains a negative object id"); |
999 | 4.66k | } else if (first > max_num_entries) { |
1000 | 60 | throw damaged( |
1001 | 60 | "Cross-reference stream's /Index contains an impossibly " |
1002 | 60 | "large object id"); |
1003 | 60 | } |
1004 | 4.71k | } |
1005 | 9.30k | } else { |
1006 | 22 | throw damaged( |
1007 | 22 | "Cross-reference stream's /Index's item " + std::to_string(i) + |
1008 | 22 | " is not an integer"); |
1009 | 22 | } |
1010 | 9.04k | i++; |
1011 | 9.04k | } |
1012 | 966 | QTC::TC("qpdf", "QPDF xref /Index is array", index_vec.size() == 2 ? 0 : 1); |
1013 | 966 | return {num_entries, indx}; |
1014 | 5.44k | } else if (Index_obj.null()) { |
1015 | 5.43k | return {size, {{0, size}}}; |
1016 | 5.43k | } else { |
1017 | 4 | throw damaged("Cross-reference stream does not have a proper /Index key"); |
1018 | 4 | } |
1019 | 6.70k | } |
1020 | | |
1021 | | qpdf_offset_t |
1022 | | Objects::processXRefStream( |
1023 | | qpdf_offset_t xref_offset, QPDFObjectHandle& xref_obj, bool in_stream_recovery) |
1024 | 7.36k | { |
1025 | 7.36k | auto damaged = [this, xref_offset](std::string_view msg) -> QPDFExc { |
1026 | 4.90k | return damagedPDF("xref stream", xref_offset, msg.data()); |
1027 | 4.90k | }; |
1028 | | |
1029 | 7.36k | auto dict = xref_obj.getDict(); |
1030 | | |
1031 | 7.36k | auto [entry_size, W] = processXRefW(dict, damaged); |
1032 | 7.36k | int max_num_entries = processXRefSize(dict, entry_size, damaged); |
1033 | 7.36k | auto [num_entries, indx] = processXRefIndex(dict, max_num_entries, damaged); |
1034 | | |
1035 | 7.36k | std::shared_ptr<Buffer> bp = xref_obj.getStreamData(qpdf_dl_specialized); |
1036 | 7.36k | size_t actual_size = bp->getSize(); |
1037 | 7.36k | auto expected_size = toS(entry_size) * toS(num_entries); |
1038 | | |
1039 | 7.36k | if (expected_size != actual_size) { |
1040 | 3.94k | QPDFExc x = damaged( |
1041 | 3.94k | "Cross-reference stream data has the wrong size; expected = " + |
1042 | 3.94k | std::to_string(expected_size) + "; actual = " + std::to_string(actual_size)); |
1043 | 3.94k | if (expected_size > actual_size) { |
1044 | 613 | throw x; |
1045 | 3.33k | } else { |
1046 | 3.33k | warn(x); |
1047 | 3.33k | } |
1048 | 3.94k | } |
1049 | | |
1050 | 6.75k | bool saw_first_compressed_object = false; |
1051 | | |
1052 | | // Actual size vs. expected size check above ensures that we will not overflow any buffers here. |
1053 | | // We know that entry_size * num_entries is less or equal to the size of the buffer. |
1054 | 6.75k | auto p = bp->getBuffer(); |
1055 | 6.75k | for (auto [obj, sec_entries]: indx) { |
1056 | | // Process a subsection. |
1057 | 1.14M | for (int i = 0; i < sec_entries; ++i) { |
1058 | | // Read this entry |
1059 | 1.14M | std::array<qpdf_offset_t, 3> fields{}; |
1060 | 1.14M | if (W[0] == 0) { |
1061 | 98.3k | fields[0] = 1; |
1062 | 98.3k | } |
1063 | 4.56M | for (size_t j = 0; j < 3; ++j) { |
1064 | 8.06M | for (int k = 0; k < W[j]; ++k) { |
1065 | 4.64M | fields[j] <<= 8; |
1066 | 4.64M | fields[j] |= *p++; |
1067 | 4.64M | } |
1068 | 3.42M | } |
1069 | | |
1070 | | // Get the generation number. The generation number is 0 unless this is an uncompressed |
1071 | | // object record, in which case the generation number appears as the third field. |
1072 | 1.14M | if (saw_first_compressed_object) { |
1073 | 966k | if (fields[0] != 2) { |
1074 | 466k | uncompressed_after_compressed_ = true; |
1075 | 466k | } |
1076 | 966k | } else if (fields[0] == 2) { |
1077 | 2.52k | saw_first_compressed_object = true; |
1078 | 2.52k | } |
1079 | 1.14M | if (obj == 0) { |
1080 | | // This is needed by checkLinearization() |
1081 | 3.41k | first_xref_item_offset_ = xref_offset; |
1082 | 1.13M | } else if (fields[0] == 0) { |
1083 | | // Ignore fields[2], which we don't care about in this case. This works around the |
1084 | | // issue of some PDF files that put invalid values, like -1, here for deleted |
1085 | | // objects. |
1086 | 248k | insertFreeXrefEntry(QPDFObjGen(obj, 0)); |
1087 | 888k | } else { |
1088 | 888k | auto typ = toI(fields[0]); |
1089 | 888k | if (!in_stream_recovery || typ == 2) { |
1090 | | // If we are in xref stream recovery all actual uncompressed objects have |
1091 | | // already been inserted into the xref table. Avoid adding junk data into the |
1092 | | // xref table. |
1093 | 670k | insertXrefEntry(obj, toI(fields[0]), fields[1], toI(fields[2])); |
1094 | 670k | } |
1095 | 888k | } |
1096 | 1.14M | ++obj; |
1097 | 1.14M | } |
1098 | 4.92k | } |
1099 | | |
1100 | 6.75k | if (!m->trailer) { |
1101 | 628 | setTrailer(dict); |
1102 | 628 | } |
1103 | | |
1104 | 6.75k | if (dict.hasKey("/Prev")) { |
1105 | 1.11k | if (!dict.getKey("/Prev").isInteger()) { |
1106 | 46 | throw damagedPDF( |
1107 | 46 | "xref stream", "/Prev key in xref stream dictionary is not an integer"); |
1108 | 46 | } |
1109 | 1.06k | return dict.getKey("/Prev").getIntValue(); |
1110 | 5.63k | } else { |
1111 | 5.63k | return 0; |
1112 | 5.63k | } |
1113 | 6.75k | } |
1114 | | |
1115 | | void |
1116 | | Objects::insertXrefEntry(int obj, int f0, qpdf_offset_t f1, int f2) |
1117 | 842k | { |
1118 | | // Populate the xref table in such a way that the first reference to an object that we see, |
1119 | | // which is the one in the latest xref table in which it appears, is the one that gets stored. |
1120 | | // This works because we are reading more recent appends before older ones. |
1121 | | |
1122 | | // If there is already an entry for this object and generation in the table, it means that a |
1123 | | // later xref table has registered this object. Disregard this one. |
1124 | 842k | int new_gen = f0 == 2 ? 0 : f2; |
1125 | | |
1126 | 842k | if (!(f0 == 1 || f0 == 2)) { |
1127 | 20.0k | return; |
1128 | 20.0k | } |
1129 | | |
1130 | 822k | if (!(obj > 0 && obj <= m->xref_table_max_id && 0 <= f2 && new_gen < 65535)) { |
1131 | | // We are ignoring invalid objgens. Most will arrive here from xref reconstruction. There |
1132 | | // is probably no point having another warning but we could count invalid items in order to |
1133 | | // decide when to give up. |
1134 | | // ignore impossibly large object ids or object ids > Size. |
1135 | 179k | return; |
1136 | 179k | } |
1137 | | |
1138 | 642k | if (m->deleted_objects.contains(obj)) { |
1139 | 1.52k | return; |
1140 | 1.52k | } |
1141 | | |
1142 | 640k | if (f0 == 2) { |
1143 | 398k | if (f1 == obj) { |
1144 | 646 | warn( |
1145 | 646 | damagedPDF("xref stream", "self-referential object stream " + std::to_string(obj))); |
1146 | 646 | return; |
1147 | 646 | } |
1148 | 397k | if (f1 > m->xref_table_max_id) { |
1149 | | // ignore impossibly large object stream ids |
1150 | 10.5k | warn(damagedPDF( |
1151 | 10.5k | "xref stream", |
1152 | 10.5k | "object stream id " + std::to_string(f1) + " for object " + std::to_string(obj) + |
1153 | 10.5k | " is impossibly large")); |
1154 | 10.5k | return; |
1155 | 10.5k | } |
1156 | 397k | } |
1157 | | |
1158 | 629k | auto [iter, created] = m->xref_table.try_emplace(QPDFObjGen(obj, (f0 == 2 ? 0 : f2))); |
1159 | 629k | if (!created) { |
1160 | 58.3k | return; |
1161 | 58.3k | } |
1162 | | |
1163 | 571k | switch (f0) { |
1164 | 193k | case 1: |
1165 | | // f2 is generation |
1166 | 193k | QTC::TC("qpdf", "QPDF xref gen > 0", ((f2 > 0) ? 1 : 0)); |
1167 | 193k | iter->second = QPDFXRefEntry(f1); |
1168 | 193k | break; |
1169 | | |
1170 | 378k | case 2: |
1171 | 378k | iter->second = QPDFXRefEntry(toI(f1), f2); |
1172 | 378k | break; |
1173 | | |
1174 | 0 | default: |
1175 | 0 | throw damagedPDF("xref stream", "unknown xref stream entry type " + std::to_string(f0)); |
1176 | 0 | break; |
1177 | 571k | } |
1178 | 571k | } |
1179 | | |
1180 | | void |
1181 | | Objects::insertFreeXrefEntry(QPDFObjGen og) |
1182 | 254k | { |
1183 | 254k | if (!m->xref_table.contains(og) && og.getObj() <= m->xref_table_max_id) { |
1184 | 92.0k | m->deleted_objects.insert(og.getObj()); |
1185 | 92.0k | } |
1186 | 254k | } |
1187 | | |
1188 | | void |
1189 | | QPDF::showXRefTable() |
1190 | 0 | { |
1191 | 0 | auto& cout = *m->cf.log()->getInfo(); |
1192 | 0 | for (auto const& iter: m->xref_table) { |
1193 | 0 | QPDFObjGen const& og = iter.first; |
1194 | 0 | QPDFXRefEntry const& entry = iter.second; |
1195 | 0 | cout << og.unparse('/') << ": "; |
1196 | 0 | switch (entry.getType()) { |
1197 | 0 | case 1: |
1198 | 0 | cout << "uncompressed; offset = " << entry.getOffset(); |
1199 | 0 | break; |
1200 | | |
1201 | 0 | case 2: |
1202 | 0 | *m->cf.log()->getInfo() << "compressed; stream = " << entry.getObjStreamNumber() |
1203 | 0 | << ", index = " << entry.getObjStreamIndex(); |
1204 | 0 | break; |
1205 | | |
1206 | 0 | default: |
1207 | 0 | throw std::logic_error("unknown cross-reference table type while showing xref_table"); |
1208 | 0 | break; |
1209 | 0 | } |
1210 | 0 | m->cf.log()->info("\n"); |
1211 | 0 | } |
1212 | 0 | } |
1213 | | |
1214 | | // Resolve all objects in the xref table. If this triggers a xref table reconstruction abort and |
1215 | | // return false. Otherwise return true. |
1216 | | bool |
1217 | | Objects::resolveXRefTable() |
1218 | 10.0k | { |
1219 | 10.0k | bool may_change = !m->reconstructed_xref; |
1220 | 359k | for (auto& iter: m->xref_table) { |
1221 | 359k | if (isUnresolved(iter.first)) { |
1222 | 230k | resolve(iter.first); |
1223 | 230k | if (may_change && m->reconstructed_xref) { |
1224 | 46 | return false; |
1225 | 46 | } |
1226 | 230k | } |
1227 | 359k | } |
1228 | 10.0k | return true; |
1229 | 10.0k | } |
1230 | | |
1231 | | // Ensure all objects in the pdf file, including those in indirect references, appear in the object |
1232 | | // cache. |
1233 | | void |
1234 | | QPDF::fixDanglingReferences(bool force) |
1235 | 23.5k | { |
1236 | 23.5k | if (m->fixed_dangling_refs) { |
1237 | 13.5k | return; |
1238 | 13.5k | } |
1239 | 10.0k | if (!m->objects.resolveXRefTable()) { |
1240 | 46 | m->objects.resolveXRefTable(); |
1241 | 46 | } |
1242 | 10.0k | m->fixed_dangling_refs = true; |
1243 | 10.0k | } |
1244 | | |
1245 | | size_t |
1246 | | QPDF::getObjectCount() |
1247 | 13.9k | { |
1248 | | // This method returns the next available indirect object number. makeIndirectObject uses it for |
1249 | | // this purpose. After fixDanglingReferences is called, all objects in the xref table will also |
1250 | | // be in obj_cache. |
1251 | 13.9k | fixDanglingReferences(); |
1252 | 13.9k | QPDFObjGen og; |
1253 | 13.9k | if (!m->obj_cache.empty()) { |
1254 | 13.9k | og = (*(m->obj_cache.rbegin())).first; |
1255 | 13.9k | } |
1256 | 13.9k | return QIntC::to_size(og.getObj()); |
1257 | 13.9k | } |
1258 | | |
1259 | | std::vector<QPDFObjectHandle> |
1260 | | QPDF::getAllObjects() |
1261 | 0 | { |
1262 | | // After fixDanglingReferences is called, all objects are in the object cache. |
1263 | 0 | fixDanglingReferences(); |
1264 | 0 | std::vector<QPDFObjectHandle> result; |
1265 | 0 | for (auto const& iter: m->obj_cache) { |
1266 | 0 | result.emplace_back(m->objects.newIndirect(iter.first, iter.second.object)); |
1267 | 0 | } |
1268 | 0 | return result; |
1269 | 0 | } |
1270 | | |
1271 | | void |
1272 | | Objects::setLastObjectDescription(std::string const& description, QPDFObjGen og) |
1273 | 256k | { |
1274 | 256k | m->last_object_description.clear(); |
1275 | 256k | if (!description.empty()) { |
1276 | 8.40k | m->last_object_description += description; |
1277 | 8.40k | if (og.isIndirect()) { |
1278 | 8.40k | m->last_object_description += ": "; |
1279 | 8.40k | } |
1280 | 8.40k | } |
1281 | 256k | if (og.isIndirect()) { |
1282 | 256k | m->last_object_description += "object " + og.unparse(' '); |
1283 | 256k | } |
1284 | 256k | } |
1285 | | |
1286 | | QPDFObjectHandle |
1287 | | Objects::readTrailer() |
1288 | 17.8k | { |
1289 | 17.8k | qpdf_offset_t offset = m->file->tell(); |
1290 | 17.8k | auto object = |
1291 | 17.8k | Parser::parse(*m->file, "trailer", m->tokenizer, nullptr, qpdf, m->reconstructed_xref); |
1292 | 17.8k | if (object.isDictionary() && m->objects.readToken(*m->file).isWord("stream")) { |
1293 | 157 | warn(damagedPDF("trailer", m->file->tell(), "stream keyword found in trailer")); |
1294 | 157 | } |
1295 | | // Override last_offset so that it points to the beginning of the object we just read |
1296 | 17.8k | m->file->setLastOffset(offset); |
1297 | 17.8k | return object; |
1298 | 17.8k | } |
1299 | | |
1300 | | QPDFObjectHandle |
1301 | | Objects::readObject(std::string const& description, QPDFObjGen og) |
1302 | 132k | { |
1303 | 132k | setLastObjectDescription(description, og); |
1304 | 132k | qpdf_offset_t offset = m->file->tell(); |
1305 | | |
1306 | 132k | StringDecrypter decrypter{&qpdf, og}; |
1307 | 132k | StringDecrypter* decrypter_ptr = m->encp->encrypted ? &decrypter : nullptr; |
1308 | 132k | auto object = Parser::parse( |
1309 | 132k | *m->file, |
1310 | 132k | m->last_object_description, |
1311 | 132k | m->tokenizer, |
1312 | 132k | decrypter_ptr, |
1313 | 132k | qpdf, |
1314 | 132k | m->reconstructed_xref || m->in_read_xref_stream); |
1315 | 132k | if (!object) { |
1316 | 9.85k | return {}; |
1317 | 9.85k | } |
1318 | 122k | auto token = readToken(*m->file); |
1319 | 122k | if (object.isDictionary() && token.isWord("stream")) { |
1320 | 54.5k | readStream(object, og, offset); |
1321 | 54.5k | token = readToken(*m->file); |
1322 | 54.5k | } |
1323 | 122k | if (!token.isWord("endobj")) { |
1324 | 29.7k | warn(damagedPDF("expected endobj")); |
1325 | 29.7k | } |
1326 | 122k | return object; |
1327 | 132k | } |
1328 | | |
1329 | | // After reading stream dictionary and stream keyword, read rest of stream. |
1330 | | void |
1331 | | Objects::readStream(QPDFObjectHandle& object, QPDFObjGen og, qpdf_offset_t offset) |
1332 | 54.5k | { |
1333 | 54.5k | validateStreamLineEnd(object, og, offset); |
1334 | | |
1335 | | // Must get offset before accessing any additional objects since resolving a previously |
1336 | | // unresolved indirect object will change file position. |
1337 | 54.5k | qpdf_offset_t stream_offset = m->file->tell(); |
1338 | 54.5k | size_t length = 0; |
1339 | | |
1340 | 54.5k | try { |
1341 | 54.5k | auto length_obj = object.getKey("/Length"); |
1342 | | |
1343 | 54.5k | if (!length_obj.isInteger()) { |
1344 | 17.9k | if (length_obj.null()) { |
1345 | 17.6k | throw damagedPDF(offset, "stream dictionary lacks /Length key"); |
1346 | 17.6k | } |
1347 | 270 | throw damagedPDF(offset, "/Length key in stream dictionary is not an integer"); |
1348 | 17.9k | } |
1349 | | |
1350 | 36.6k | length = toS(length_obj.getUIntValue()); |
1351 | | // Seek in two steps to avoid potential integer overflow |
1352 | 36.6k | m->file->seek(stream_offset, SEEK_SET); |
1353 | 36.6k | m->file->seek(toO(length), SEEK_CUR); |
1354 | 36.6k | if (!readToken(*m->file).isWord("endstream")) { |
1355 | 9.25k | throw damagedPDF("expected endstream"); |
1356 | 9.25k | } |
1357 | 36.6k | } catch (QPDFExc& e) { |
1358 | 35.2k | if (!cf.surpress_recovery()) { |
1359 | 35.2k | warn(e); |
1360 | 35.2k | length = recoverStreamLength(m->file, og, stream_offset); |
1361 | 35.2k | } else { |
1362 | 0 | throw; |
1363 | 0 | } |
1364 | 35.2k | } |
1365 | 45.0k | object = QPDFObjectHandle(qpdf::Stream(qpdf, og, object, stream_offset, length)); |
1366 | 45.0k | } |
1367 | | |
1368 | | void |
1369 | | Objects::validateStreamLineEnd(QPDFObjectHandle& object, QPDFObjGen og, qpdf_offset_t offset) |
1370 | 54.5k | { |
1371 | | // The PDF specification states that the word "stream" should be followed by either a carriage |
1372 | | // return and a newline or by a newline alone. It specifically disallowed following it by a |
1373 | | // carriage return alone since, in that case, there would be no way to tell whether the NL in a |
1374 | | // CR NL sequence was part of the stream data. However, some readers, including Adobe reader, |
1375 | | // accept a carriage return by itself when followed by a non-newline character, so that's what |
1376 | | // we do here. We have also seen files that have extraneous whitespace between the stream |
1377 | | // keyword and the newline. |
1378 | 61.1k | while (true) { |
1379 | 61.0k | char ch; |
1380 | 61.0k | if (m->file->read(&ch, 1) == 0) { |
1381 | | // A premature EOF here will result in some other problem that will get reported at |
1382 | | // another time. |
1383 | 167 | return; |
1384 | 167 | } |
1385 | 60.9k | if (ch == '\n') { |
1386 | | // ready to read stream data |
1387 | 31.6k | return; |
1388 | 31.6k | } |
1389 | 29.2k | if (ch == '\r') { |
1390 | | // Read another character |
1391 | 17.8k | if (m->file->read(&ch, 1) != 0) { |
1392 | 17.8k | if (ch == '\n') { |
1393 | | // Ready to read stream data |
1394 | 16.7k | QTC::TC("qpdf", "QPDF stream with CRNL"); |
1395 | 16.7k | } else { |
1396 | | // Treat the \r by itself as the whitespace after endstream and start reading |
1397 | | // stream data in spite of not having seen a newline. |
1398 | 1.01k | m->file->unreadCh(ch); |
1399 | 1.01k | warn(damagedPDF( |
1400 | 1.01k | m->file->tell(), "stream keyword followed by carriage return only")); |
1401 | 1.01k | } |
1402 | 17.8k | } |
1403 | 17.8k | return; |
1404 | 17.8k | } |
1405 | 11.4k | if (!util::is_space(ch)) { |
1406 | 4.79k | m->file->unreadCh(ch); |
1407 | 4.79k | warn(damagedPDF( |
1408 | 4.79k | m->file->tell(), "stream keyword not followed by proper line terminator")); |
1409 | 4.79k | return; |
1410 | 4.79k | } |
1411 | 6.61k | warn(damagedPDF(m->file->tell(), "stream keyword followed by extraneous whitespace")); |
1412 | 6.61k | } |
1413 | 54.5k | } |
1414 | | |
1415 | | bool |
1416 | | Objects::findEndstream() |
1417 | 37.1k | { |
1418 | | // Find endstream or endobj. Position the input at that token. |
1419 | 37.1k | auto t = readToken(*m->file, 20); |
1420 | 37.1k | if (t.isWord("endobj") || t.isWord("endstream")) { |
1421 | 25.2k | m->file->seek(m->file->getLastOffset(), SEEK_SET); |
1422 | 25.2k | return true; |
1423 | 25.2k | } |
1424 | 11.9k | return false; |
1425 | 37.1k | } |
1426 | | |
1427 | | size_t |
1428 | | Objects::recoverStreamLength( |
1429 | | std::shared_ptr<InputSource> input, QPDFObjGen og, qpdf_offset_t stream_offset) |
1430 | 26.0k | { |
1431 | | // Try to reconstruct stream length by looking for endstream or endobj |
1432 | 26.0k | warn(damagedPDF(*input, stream_offset, "attempting to recover stream length")); |
1433 | | |
1434 | 26.0k | PatternFinder ef(*this, &Objects::findEndstream); |
1435 | 26.0k | size_t length = 0; |
1436 | 26.0k | if (m->file->findFirst("end", stream_offset, 0, ef)) { |
1437 | 25.2k | length = toS(m->file->tell() - stream_offset); |
1438 | | // Reread endstream but, if it was endobj, don't skip that. |
1439 | 25.2k | QPDFTokenizer::Token t = readToken(*m->file); |
1440 | 25.2k | if (t.getValue() == "endobj") { |
1441 | 14.2k | m->file->seek(m->file->getLastOffset(), SEEK_SET); |
1442 | 14.2k | } |
1443 | 25.2k | } |
1444 | | |
1445 | 26.0k | if (length) { |
1446 | 24.8k | auto end = stream_offset + toO(length); |
1447 | 24.8k | qpdf_offset_t found_offset = 0; |
1448 | 24.8k | QPDFObjGen found_og; |
1449 | | |
1450 | | // Make sure this is inside this object |
1451 | 648k | for (auto const& [current_og, entry]: m->xref_table) { |
1452 | 648k | if (entry.getType() == 1) { |
1453 | 579k | qpdf_offset_t obj_offset = entry.getOffset(); |
1454 | 579k | if (found_offset < obj_offset && obj_offset < end) { |
1455 | 141k | found_offset = obj_offset; |
1456 | 141k | found_og = current_og; |
1457 | 141k | } |
1458 | 579k | } |
1459 | 648k | } |
1460 | 24.8k | if (!found_offset || found_og == og) { |
1461 | | // If we are trying to recover an XRef stream the xref table will not contain and |
1462 | | // won't contain any entries, therefore we cannot check the found length. Otherwise we |
1463 | | // found endstream\nendobj within the space allowed for this object, so we're probably |
1464 | | // in good shape. |
1465 | 23.0k | } else { |
1466 | 1.81k | length = 0; |
1467 | 1.81k | } |
1468 | 24.8k | } |
1469 | | |
1470 | 26.0k | if (length == 0) { |
1471 | 2.93k | warn(damagedPDF( |
1472 | 2.93k | *input, stream_offset, "unable to recover stream data; treating stream as empty")); |
1473 | 23.0k | } else { |
1474 | 23.0k | warn(damagedPDF( |
1475 | 23.0k | *input, stream_offset, "recovered stream length: " + std::to_string(length))); |
1476 | 23.0k | } |
1477 | | |
1478 | 26.0k | return length; |
1479 | 26.0k | } |
1480 | | |
1481 | | QPDFTokenizer::Token |
1482 | | Objects::readToken(InputSource& input, size_t max_len) |
1483 | 5.62M | { |
1484 | 5.62M | return m->tokenizer.readToken(input, m->last_object_description, true, max_len); |
1485 | 5.62M | } |
1486 | | |
1487 | | QPDFObjGen |
1488 | | Objects::read_object_start(qpdf_offset_t offset) |
1489 | 134k | { |
1490 | 134k | m->file->seek(offset, SEEK_SET); |
1491 | 134k | QPDFTokenizer::Token tobjid = readToken(*m->file); |
1492 | 134k | bool objidok = tobjid.isInteger(); |
1493 | 134k | if (!objidok) { |
1494 | 1.94k | throw damagedPDF(offset, "expected n n obj"); |
1495 | 1.94k | } |
1496 | 132k | QPDFTokenizer::Token tgen = readToken(*m->file); |
1497 | 132k | bool genok = tgen.isInteger(); |
1498 | 132k | if (!genok) { |
1499 | 100 | throw damagedPDF(offset, "expected n n obj"); |
1500 | 100 | } |
1501 | 132k | QPDFTokenizer::Token tobj = readToken(*m->file); |
1502 | | |
1503 | 132k | bool objok = tobj.isWord("obj"); |
1504 | | |
1505 | 132k | if (!objok) { |
1506 | 157 | throw damagedPDF(offset, "expected n n obj"); |
1507 | 157 | } |
1508 | 132k | int objid = QUtil::string_to_int(tobjid.getValue().c_str()); |
1509 | 132k | int generation = QUtil::string_to_int(tgen.getValue().c_str()); |
1510 | 132k | if (objid == 0) { |
1511 | 12 | throw damagedPDF(offset, "object with ID 0"); |
1512 | 12 | } |
1513 | 132k | return {objid, generation}; |
1514 | 132k | } |
1515 | | |
1516 | | void |
1517 | | Objects::readObjectAtOffset( |
1518 | | bool try_recovery, qpdf_offset_t offset, std::string const& description, QPDFObjGen exp_og) |
1519 | 124k | { |
1520 | 124k | QPDFObjGen og; |
1521 | 124k | setLastObjectDescription(description, exp_og); |
1522 | | |
1523 | 124k | if (cf.surpress_recovery()) { |
1524 | 0 | try_recovery = false; |
1525 | 0 | } |
1526 | | |
1527 | | // Special case: if offset is 0, just return null. Some PDF writers, in particular |
1528 | | // "Mac OS X 10.7.5 Quartz PDFContext", may store deleted objects in the xref table as |
1529 | | // "0000000000 00000 n", which is not correct, but it won't hurt anything for us to ignore |
1530 | | // these. |
1531 | 124k | if (offset == 0) { |
1532 | 154 | warn(damagedPDF( |
1533 | 154 | -1, |
1534 | 154 | "object has offset 0 - a common error handled correctly by qpdf and most other " |
1535 | 154 | "applications")); |
1536 | 154 | return; |
1537 | 154 | } |
1538 | | |
1539 | 124k | try { |
1540 | 124k | og = read_object_start(offset); |
1541 | 124k | if (exp_og != og) { |
1542 | 31 | QPDFExc e = damagedPDF(offset, "expected " + exp_og.unparse(' ') + " obj"); |
1543 | 31 | if (try_recovery) { |
1544 | | // Will be retried below |
1545 | 31 | throw e; |
1546 | 31 | } else { |
1547 | | // We can try reading the object anyway even if the ID doesn't match. |
1548 | 0 | warn(e); |
1549 | 0 | } |
1550 | 31 | } |
1551 | 124k | } catch (QPDFExc& e) { |
1552 | 346 | if (!try_recovery) { |
1553 | 0 | throw; |
1554 | 0 | } |
1555 | | // Try again after reconstructing xref table |
1556 | 346 | reconstruct_xref(e); |
1557 | 346 | if (m->xref_table.contains(exp_og) && m->xref_table[exp_og].getType() == 1) { |
1558 | 49 | qpdf_offset_t new_offset = m->xref_table[exp_og].getOffset(); |
1559 | 49 | readObjectAtOffset(false, new_offset, description, exp_og); |
1560 | 49 | return; |
1561 | 49 | } |
1562 | 297 | warn(damagedPDF( |
1563 | 297 | "", |
1564 | 297 | -1, |
1565 | 297 | ("object " + exp_og.unparse(' ') + |
1566 | 297 | " not found in file after regenerating cross reference table"))); |
1567 | 297 | return; |
1568 | 346 | } |
1569 | | |
1570 | 123k | if (auto oh = readObject(description, og)) { |
1571 | | // Determine the end offset of this object before and after white space. We use these |
1572 | | // numbers to validate linearization hint tables. Offsets and lengths of objects may imply |
1573 | | // the end of an object to be anywhere between these values. |
1574 | 100k | qpdf_offset_t end_before_space = m->file->tell(); |
1575 | | |
1576 | | // skip over spaces |
1577 | 226k | while (true) { |
1578 | 226k | char ch; |
1579 | 226k | if (!m->file->read(&ch, 1)) { |
1580 | 950 | throw damagedPDF(m->file->tell(), "EOF after endobj"); |
1581 | 950 | } |
1582 | 225k | if (!isspace(static_cast<unsigned char>(ch))) { |
1583 | 99.9k | m->file->seek(-1, SEEK_CUR); |
1584 | 99.9k | break; |
1585 | 99.9k | } |
1586 | 225k | } |
1587 | 99.9k | m->objects.updateCache(og, oh.obj_sp(), end_before_space, m->file->tell()); |
1588 | 99.9k | } |
1589 | 123k | } |
1590 | | |
1591 | | QPDFObjectHandle |
1592 | | Objects::readObjectAtOffset( |
1593 | | qpdf_offset_t offset, std::string const& description, bool skip_cache_if_in_xref) |
1594 | 10.3k | { |
1595 | 10.3k | auto og = read_object_start(offset); |
1596 | 10.3k | auto oh = readObject(description, og); |
1597 | | |
1598 | 10.3k | if (!oh || !m->objects.isUnresolved(og)) { |
1599 | 5.94k | return oh; |
1600 | 5.94k | } |
1601 | | |
1602 | 4.42k | if (skip_cache_if_in_xref && m->xref_table.contains(og)) { |
1603 | | // In the special case of the xref stream and linearization hint tables, the offset comes |
1604 | | // from another source. For the specific case of xref streams, the xref stream is read and |
1605 | | // loaded into the object cache very early in parsing. Ordinarily, when a file is updated by |
1606 | | // appending, items inserted into the xref table in later updates take precedence over |
1607 | | // earlier items. In the special case of reusing the object number previously used as the |
1608 | | // xref stream, we have the following order of events: |
1609 | | // |
1610 | | // * reused object gets loaded into the xref table |
1611 | | // * old object is read here while reading xref streams |
1612 | | // * original xref entry is ignored (since already in xref table) |
1613 | | // |
1614 | | // It is the second step that causes a problem. Even though the xref table is correct in |
1615 | | // this case, the old object is already in the cache and so effectively prevails over the |
1616 | | // reused object. To work around this issue, we have a special case for the xref stream (via |
1617 | | // the skip_cache_if_in_xref): if the object is already in the xref stream, don't cache what |
1618 | | // we read here. |
1619 | | // |
1620 | | // It is likely that the same bug may exist for linearization hint tables, but the existing |
1621 | | // code uses end_before_space and end_after_space from the cache, so fixing that would |
1622 | | // require more significant rework. The chances of a linearization hint stream being reused |
1623 | | // seems smaller because the xref stream is probably the highest object in the file and the |
1624 | | // linearization hint stream would be some random place in the middle, so I'm leaving that |
1625 | | // bug unfixed for now. If the bug were to be fixed, we could use !check_og in place of |
1626 | | // skip_cache_if_in_xref. |
1627 | 12 | QTC::TC("qpdf", "QPDF skipping cache for known unchecked object"); |
1628 | 12 | return oh; |
1629 | 12 | } |
1630 | | |
1631 | | // Determine the end offset of this object before and after white space. We use these |
1632 | | // numbers to validate linearization hint tables. Offsets and lengths of objects may imply |
1633 | | // the end of an object to be anywhere between these values. |
1634 | 4.41k | qpdf_offset_t end_before_space = m->file->tell(); |
1635 | | |
1636 | | // skip over spaces |
1637 | 7.08k | while (true) { |
1638 | 5.11k | char ch; |
1639 | 5.11k | if (!m->file->read(&ch, 1)) { |
1640 | 58 | throw damagedPDF(m->file->tell(), "EOF after endobj"); |
1641 | 58 | } |
1642 | 5.05k | if (!isspace(static_cast<unsigned char>(ch))) { |
1643 | 2.37k | m->file->seek(-1, SEEK_CUR); |
1644 | 2.37k | break; |
1645 | 2.37k | } |
1646 | 5.05k | } |
1647 | 4.35k | m->objects.updateCache(og, oh.obj_sp(), end_before_space, m->file->tell()); |
1648 | | |
1649 | 4.35k | return oh; |
1650 | 4.41k | } |
1651 | | |
1652 | | std::shared_ptr<QPDFObject> const& |
1653 | | Objects::resolve(QPDFObjGen og) |
1654 | 505k | { |
1655 | 505k | if (!isUnresolved(og)) { |
1656 | 0 | return m->obj_cache[og].object; |
1657 | 0 | } |
1658 | | |
1659 | 505k | if (m->resolving.contains(og)) { |
1660 | | // This can happen if an object references itself directly or indirectly in some key that |
1661 | | // has to be resolved during object parsing, such as stream length. |
1662 | 114 | warn(damagedPDF("", "loop detected resolving object " + og.unparse(' '))); |
1663 | 114 | updateCache(og, QPDFObject::create<QPDF_Null>(), -1, -1); |
1664 | 114 | return m->obj_cache[og].object; |
1665 | 114 | } |
1666 | 505k | ResolveRecorder rr(qpdf, og); |
1667 | | |
1668 | 505k | if (m->xref_table.contains(og)) { |
1669 | 375k | QPDFXRefEntry const& entry = m->xref_table[og]; |
1670 | 375k | try { |
1671 | 375k | switch (entry.getType()) { |
1672 | 124k | case 1: |
1673 | | // Object stored in cache by readObjectAtOffset |
1674 | 124k | readObjectAtOffset(true, entry.getOffset(), "", og); |
1675 | 124k | break; |
1676 | | |
1677 | 251k | case 2: |
1678 | 251k | resolveObjectsInStream(entry.getObjStreamNumber()); |
1679 | 251k | break; |
1680 | | |
1681 | 3 | default: |
1682 | 3 | throw damagedPDF( |
1683 | 3 | "", -1, ("object " + og.unparse('/') + " has unexpected xref entry type")); |
1684 | 375k | } |
1685 | 375k | } catch (QPDFExc& e) { |
1686 | 36.8k | warn(e); |
1687 | 36.8k | } catch (std::exception& e) { |
1688 | 200 | warn(damagedPDF( |
1689 | 200 | "", -1, ("object " + og.unparse('/') + ": error reading object: " + e.what()))); |
1690 | 200 | } |
1691 | 375k | } |
1692 | | |
1693 | 488k | if (isUnresolved(og)) { |
1694 | | // PDF spec says unknown objects resolve to the null object. |
1695 | 387k | updateCache(og, QPDFObject::create<QPDF_Null>(), -1, -1); |
1696 | 387k | } |
1697 | | |
1698 | 488k | auto& result(m->obj_cache[og].object); |
1699 | 488k | result->setDefaultDescription(&qpdf, og); |
1700 | 488k | return result; |
1701 | 505k | } |
1702 | | |
1703 | | void |
1704 | | Objects::resolveObjectsInStream(int obj_stream_number) |
1705 | 251k | { |
1706 | 251k | auto damaged = |
1707 | 251k | [this, obj_stream_number](int id, qpdf_offset_t offset, std::string const& msg) -> QPDFExc { |
1708 | 20.4k | return { |
1709 | 20.4k | qpdf_e_damaged_pdf, |
1710 | 20.4k | m->file->getName() + " object stream " + std::to_string(obj_stream_number), |
1711 | 20.4k | +"object " + std::to_string(id) + " 0", |
1712 | 20.4k | offset, |
1713 | 20.4k | msg, |
1714 | 20.4k | true}; |
1715 | 20.4k | }; |
1716 | | |
1717 | 251k | if (m->resolved_object_streams.contains(obj_stream_number)) { |
1718 | 227k | return; |
1719 | 227k | } |
1720 | 23.6k | m->resolved_object_streams.insert(obj_stream_number); |
1721 | | // Force resolution of object stream |
1722 | 23.6k | Stream obj_stream = qpdf.getObject(obj_stream_number, 0); |
1723 | 23.6k | if (!obj_stream) { |
1724 | 18.5k | throw damagedPDF( |
1725 | 18.5k | "object " + std::to_string(obj_stream_number) + " 0", |
1726 | 18.5k | "supposed object stream " + std::to_string(obj_stream_number) + " is not a stream"); |
1727 | 18.5k | } |
1728 | | |
1729 | | // For linearization data in the object, use the data from the object stream for the objects in |
1730 | | // the stream. |
1731 | 5.06k | QPDFObjGen stream_og(obj_stream_number, 0); |
1732 | 5.06k | qpdf_offset_t end_before_space = m->obj_cache[stream_og].end_before_space; |
1733 | 5.06k | qpdf_offset_t end_after_space = m->obj_cache[stream_og].end_after_space; |
1734 | | |
1735 | 5.06k | QPDFObjectHandle dict = obj_stream.getDict(); |
1736 | 5.06k | if (!dict.isDictionaryOfType("/ObjStm")) { |
1737 | 627 | warn(damagedPDF( |
1738 | 627 | "object " + std::to_string(obj_stream_number) + " 0", |
1739 | 627 | "supposed object stream " + std::to_string(obj_stream_number) + " has wrong type")); |
1740 | 627 | } |
1741 | | |
1742 | 5.06k | unsigned int n{0}; |
1743 | 5.06k | int first{0}; |
1744 | 5.06k | if (!(dict.getKey("/N").getValueAsUInt(n) && dict.getKey("/First").getValueAsInt(first))) { |
1745 | 205 | throw damagedPDF( |
1746 | 205 | "object " + std::to_string(obj_stream_number) + " 0", |
1747 | 205 | "object stream " + std::to_string(obj_stream_number) + " has incorrect keys"); |
1748 | 205 | } |
1749 | | |
1750 | | // id, offset, size |
1751 | 4.86k | std::vector<std::tuple<int, qpdf_offset_t, size_t>> offsets; |
1752 | | |
1753 | 4.86k | auto stream_data = obj_stream.getStreamData(qpdf_dl_specialized); |
1754 | | |
1755 | 4.86k | is::OffsetBuffer input("", stream_data); |
1756 | | |
1757 | 4.86k | const auto b_size = stream_data.size(); |
1758 | 4.86k | const auto end_offset = static_cast<qpdf_offset_t>(b_size); |
1759 | 4.86k | auto b_start = stream_data.data(); |
1760 | | |
1761 | 4.86k | if (first >= end_offset) { |
1762 | 40 | throw damagedPDF( |
1763 | 40 | "object " + std::to_string(obj_stream_number) + " 0", |
1764 | 40 | "object stream " + std::to_string(obj_stream_number) + " has invalid /First entry"); |
1765 | 40 | } |
1766 | | |
1767 | 4.82k | int id = 0; |
1768 | 4.82k | long long last_offset = -1; |
1769 | 4.82k | bool is_first = true; |
1770 | 88.2k | for (unsigned int i = 0; i < n; ++i) { |
1771 | 83.5k | auto tnum = readToken(input); |
1772 | 83.5k | auto id_offset = input.getLastOffset(); |
1773 | 83.5k | auto toffset = readToken(input); |
1774 | 83.5k | if (!(tnum.isInteger() && toffset.isInteger())) { |
1775 | 179 | throw damaged(0, input.getLastOffset(), "expected integer in object stream header"); |
1776 | 179 | } |
1777 | | |
1778 | 83.4k | int num = QUtil::string_to_int(tnum.getValue().c_str()); |
1779 | 83.4k | long long offset = QUtil::string_to_int(toffset.getValue().c_str()); |
1780 | | |
1781 | 83.4k | if (num == obj_stream_number) { |
1782 | 615 | warn(damaged(num, id_offset, "object stream claims to contain itself")); |
1783 | 615 | continue; |
1784 | 615 | } |
1785 | | |
1786 | 82.7k | if (num < 1) { |
1787 | 1.12k | warn(damaged(num, id_offset, "object id is invalid"s)); |
1788 | 1.12k | continue; |
1789 | 1.12k | } |
1790 | | |
1791 | 81.6k | if (offset <= last_offset) { |
1792 | 7.98k | warn(damaged( |
1793 | 7.98k | num, |
1794 | 7.98k | input.getLastOffset(), |
1795 | 7.98k | "offset " + std::to_string(offset) + |
1796 | 7.98k | " is invalid (must be larger than previous offset " + |
1797 | 7.98k | std::to_string(last_offset) + ")")); |
1798 | 7.98k | continue; |
1799 | 7.98k | } |
1800 | | |
1801 | 73.6k | if (num > m->xref_table_max_id) { |
1802 | 2.25k | continue; |
1803 | 2.25k | } |
1804 | | |
1805 | 71.4k | if (first + offset >= end_offset) { |
1806 | 10.5k | warn(damaged( |
1807 | 10.5k | num, input.getLastOffset(), "offset " + std::to_string(offset) + " is too large")); |
1808 | 10.5k | continue; |
1809 | 10.5k | } |
1810 | | |
1811 | 60.9k | if (is_first) { |
1812 | 1.38k | is_first = false; |
1813 | 59.5k | } else { |
1814 | 59.5k | offsets.emplace_back( |
1815 | 59.5k | id, last_offset + first, static_cast<size_t>(offset - last_offset)); |
1816 | 59.5k | } |
1817 | | |
1818 | 60.9k | last_offset = offset; |
1819 | 60.9k | id = num; |
1820 | 60.9k | } |
1821 | | |
1822 | 4.64k | if (!is_first) { |
1823 | | // We found at least one valid entry. |
1824 | 1.19k | offsets.emplace_back( |
1825 | 1.19k | id, last_offset + first, b_size - static_cast<size_t>(last_offset + first)); |
1826 | 1.19k | } |
1827 | | |
1828 | | // To avoid having to read the object stream multiple times, store all objects that would be |
1829 | | // found here in the cache. Remember that some objects stored here might have been overridden |
1830 | | // by new objects appended to the file, so it is necessary to recheck the xref table and only |
1831 | | // cache what would actually be resolved here. |
1832 | 55.6k | for (auto const& [obj_id, obj_offset, obj_size]: offsets) { |
1833 | 55.6k | QPDFObjGen og(obj_id, 0); |
1834 | 55.6k | auto entry = m->xref_table.find(og); |
1835 | 55.6k | if (entry != m->xref_table.end() && entry->second.getType() == 2 && |
1836 | 53.2k | entry->second.getObjStreamNumber() == obj_stream_number) { |
1837 | 52.5k | is::OffsetBuffer in("", {b_start + obj_offset, obj_size}, obj_offset); |
1838 | 52.5k | if (auto oh = Parser::parse(in, obj_stream_number, obj_id, m->tokenizer, qpdf)) { |
1839 | 48.3k | updateCache(og, oh.obj_sp(), end_before_space, end_after_space); |
1840 | 48.3k | } |
1841 | 52.5k | } else { |
1842 | 3.05k | QTC::TC("qpdf", "QPDF not caching overridden objstm object"); |
1843 | 3.05k | } |
1844 | 55.6k | } |
1845 | 4.64k | } |
1846 | | |
1847 | | QPDFObjectHandle |
1848 | | Objects::newIndirect(QPDFObjGen og, std::shared_ptr<QPDFObject> const& obj) |
1849 | 3.17k | { |
1850 | 3.17k | obj->setDefaultDescription(&qpdf, og); |
1851 | 3.17k | return {obj}; |
1852 | 3.17k | } |
1853 | | |
1854 | | void |
1855 | | Objects::updateCache( |
1856 | | QPDFObjGen og, |
1857 | | std::shared_ptr<QPDFObject> const& object, |
1858 | | qpdf_offset_t end_before_space, |
1859 | | qpdf_offset_t end_after_space, |
1860 | | bool destroy) |
1861 | 537k | { |
1862 | 537k | object->setObjGen(&qpdf, og); |
1863 | 537k | if (isCached(og)) { |
1864 | 305k | auto& cache = m->obj_cache[og]; |
1865 | 305k | object->move_to(cache.object, destroy); |
1866 | 305k | cache.end_before_space = end_before_space; |
1867 | 305k | cache.end_after_space = end_after_space; |
1868 | 305k | } else { |
1869 | 232k | m->obj_cache[og] = ObjCache(object, end_before_space, end_after_space); |
1870 | 232k | } |
1871 | 537k | } |
1872 | | |
1873 | | bool |
1874 | | Objects::isCached(QPDFObjGen og) |
1875 | 1.89M | { |
1876 | 1.89M | return m->obj_cache.contains(og); |
1877 | 1.89M | } |
1878 | | |
1879 | | bool |
1880 | | Objects::isUnresolved(QPDFObjGen og) |
1881 | 1.36M | { |
1882 | 1.36M | return !isCached(og) || m->obj_cache[og].object->isUnresolved(); |
1883 | 1.36M | } |
1884 | | |
1885 | | QPDFObjGen |
1886 | | Objects::nextObjGen() |
1887 | 3.18k | { |
1888 | 3.18k | int max_objid = toI(qpdf.getObjectCount()); |
1889 | 3.18k | if (max_objid == std::numeric_limits<int>::max()) { |
1890 | 1 | throw std::range_error("max object id is too high to create new objects"); |
1891 | 1 | } |
1892 | 3.17k | return {max_objid + 1, 0}; |
1893 | 3.18k | } |
1894 | | |
1895 | | QPDFObjectHandle |
1896 | | Objects::makeIndirectFromQPDFObject(std::shared_ptr<QPDFObject> const& obj) |
1897 | 3.18k | { |
1898 | 3.18k | QPDFObjGen next{nextObjGen()}; |
1899 | 3.18k | m->obj_cache[next] = ObjCache(obj, -1, -1); |
1900 | 3.18k | return newIndirect(next, m->obj_cache[next].object); |
1901 | 3.18k | } |
1902 | | |
1903 | | QPDFObjectHandle |
1904 | | QPDF::makeIndirectObject(QPDFObjectHandle oh) |
1905 | 3.18k | { |
1906 | 3.18k | if (!oh) { |
1907 | 0 | throw std::logic_error("attempted to make an uninitialized QPDFObjectHandle indirect"); |
1908 | 0 | } |
1909 | 3.18k | return m->objects.makeIndirectFromQPDFObject(oh.obj_sp()); |
1910 | 3.18k | } |
1911 | | |
1912 | | std::shared_ptr<QPDFObject> |
1913 | | Objects::getObjectForParser(int id, int gen, bool parse_pdf) |
1914 | 475k | { |
1915 | | // This method is called by the parser and therefore must not resolve any objects. |
1916 | 475k | auto og = QPDFObjGen(id, gen); |
1917 | 475k | if (auto iter = m->obj_cache.find(og); iter != m->obj_cache.end()) { |
1918 | 199k | return iter->second.object; |
1919 | 199k | } |
1920 | 276k | if (m->xref_table.contains(og) || (!m->parsed && og.getObj() < m->xref_table_max_id)) { |
1921 | 257k | return m->obj_cache.insert({og, QPDFObject::create<QPDF_Unresolved>(&qpdf, og)}) |
1922 | 257k | .first->second.object; |
1923 | 257k | } |
1924 | 19.0k | if (parse_pdf) { |
1925 | 19.0k | return QPDFObject::create<QPDF_Null>(); |
1926 | 19.0k | } |
1927 | 0 | return m->obj_cache.insert({og, QPDFObject::create<QPDF_Null>(&qpdf, og)}).first->second.object; |
1928 | 19.0k | } |
1929 | | |
1930 | | std::shared_ptr<QPDFObject> |
1931 | | Objects::getObjectForJSON(int id, int gen) |
1932 | 0 | { |
1933 | 0 | auto og = QPDFObjGen(id, gen); |
1934 | 0 | auto [it, inserted] = m->obj_cache.try_emplace(og); |
1935 | 0 | auto& obj = it->second.object; |
1936 | 0 | if (inserted) { |
1937 | 0 | obj = (m->parsed && !m->xref_table.contains(og)) |
1938 | 0 | ? QPDFObject::create<QPDF_Null>(&qpdf, og) |
1939 | 0 | : QPDFObject::create<QPDF_Unresolved>(&qpdf, og); |
1940 | 0 | } |
1941 | 0 | return obj; |
1942 | 0 | } |
1943 | | |
1944 | | QPDFObjectHandle |
1945 | | QPDF::getObject(QPDFObjGen og) |
1946 | 270k | { |
1947 | 270k | if (auto it = m->obj_cache.find(og); it != m->obj_cache.end()) { |
1948 | 207k | return {it->second.object}; |
1949 | 207k | } else if (m->parsed && !m->xref_table.contains(og)) { |
1950 | 5.56k | return QPDFObject::create<QPDF_Null>(); |
1951 | 57.6k | } else { |
1952 | 57.6k | auto result = |
1953 | 57.6k | m->obj_cache.try_emplace(og, QPDFObject::create<QPDF_Unresolved>(this, og), -1, -1); |
1954 | 57.6k | return {result.first->second.object}; |
1955 | 57.6k | } |
1956 | 270k | } |
1957 | | |
1958 | | void |
1959 | | QPDF::replaceObject(int objid, int generation, QPDFObjectHandle oh) |
1960 | 0 | { |
1961 | 0 | replaceObject(QPDFObjGen(objid, generation), oh); |
1962 | 0 | } |
1963 | | |
1964 | | void |
1965 | | QPDF::replaceObject(QPDFObjGen og, QPDFObjectHandle oh) |
1966 | 0 | { |
1967 | 0 | if (!oh || (oh.isIndirect() && !(oh.isStream() && oh.getObjGen() == og))) { |
1968 | 0 | throw std::logic_error("QPDF::replaceObject called with indirect object handle"); |
1969 | 0 | } |
1970 | 0 | m->objects.updateCache(og, oh.obj_sp(), -1, -1, false); |
1971 | 0 | } |
1972 | | |
1973 | | void |
1974 | | QPDF::removeObject(QPDFObjGen og) |
1975 | 3.65k | { |
1976 | 3.65k | m->xref_table.erase(og); |
1977 | 3.65k | if (auto cached = m->obj_cache.find(og); cached != m->obj_cache.end()) { |
1978 | | // Take care of any object handles that may be floating around. |
1979 | 1.63k | cached->second.object->assign_null(); |
1980 | 1.63k | cached->second.object->setObjGen(nullptr, QPDFObjGen()); |
1981 | 1.63k | m->obj_cache.erase(cached); |
1982 | 1.63k | } |
1983 | 3.65k | } |
1984 | | |
1985 | | void |
1986 | | QPDF::replaceReserved(QPDFObjectHandle reserved, QPDFObjectHandle replacement) |
1987 | 0 | { |
1988 | 0 | QTC::TC("qpdf", "QPDF replaceReserved"); |
1989 | 0 | auto tc = reserved.getTypeCode(); |
1990 | 0 | if (!(tc == ::ot_reserved || tc == ::ot_null)) { |
1991 | 0 | throw std::logic_error("replaceReserved called with non-reserved object"); |
1992 | 0 | } |
1993 | 0 | replaceObject(reserved.getObjGen(), replacement); |
1994 | 0 | } |
1995 | | |
1996 | | void |
1997 | | QPDF::swapObjects(int objid1, int generation1, int objid2, int generation2) |
1998 | 0 | { |
1999 | 0 | swapObjects(QPDFObjGen(objid1, generation1), QPDFObjGen(objid2, generation2)); |
2000 | 0 | } |
2001 | | |
2002 | | void |
2003 | | QPDF::swapObjects(QPDFObjGen og1, QPDFObjGen og2) |
2004 | 0 | { |
2005 | | // Force objects to be read from the input source if needed, then swap them in the cache. |
2006 | 0 | m->objects.resolve(og1); |
2007 | 0 | m->objects.resolve(og2); |
2008 | 0 | m->obj_cache[og1].object->swapWith(m->obj_cache[og2].object); |
2009 | 0 | } |
2010 | | |
2011 | | size_t |
2012 | | Objects::table_size() |
2013 | 9.64k | { |
2014 | | // If obj_cache is dense, accommodate all object in tables,else accommodate only original |
2015 | | // objects. |
2016 | 9.64k | auto max_xref = !m->xref_table.empty() ? m->xref_table.crbegin()->first.getObj() : 0; |
2017 | 9.64k | auto max_obj = !m->obj_cache.empty() ? m->obj_cache.crbegin()->first.getObj() : 0; |
2018 | 9.64k | auto max_id = std::numeric_limits<int>::max() - 1; |
2019 | 9.64k | if (max_obj >= max_id || max_xref >= max_id) { |
2020 | | // Temporary fix. Long-term solution is |
2021 | | // - QPDFObjGen to enforce objgens are valid and sensible |
2022 | | // - xref table and obj cache to protect against insertion of impossibly large obj ids |
2023 | 1 | stopOnError("Impossibly large object id encountered."); |
2024 | 1 | } |
2025 | 9.64k | if (max_obj < 1.1 * std::max(toI(m->obj_cache.size()), max_xref)) { |
2026 | 7.48k | return toS(++max_obj); |
2027 | 7.48k | } |
2028 | 2.16k | return toS(++max_xref); |
2029 | 9.64k | } |
2030 | | |
2031 | | std::vector<QPDFObjGen> |
2032 | | Objects::compressible_vector() |
2033 | 0 | { |
2034 | 0 | return compressible<QPDFObjGen>(); |
2035 | 0 | } |
2036 | | |
2037 | | std::vector<bool> |
2038 | | Objects::compressible_set() |
2039 | 1.23k | { |
2040 | 1.23k | return compressible<bool>(); |
2041 | 1.23k | } |
2042 | | |
2043 | | template <typename T> |
2044 | | std::vector<T> |
2045 | | Objects::compressible() |
2046 | 1.23k | { |
2047 | | // Return a list of objects that are allowed to be in object streams. Walk through the objects |
2048 | | // by traversing the document from the root, including a traversal of the pages tree. This |
2049 | | // makes that objects that are on the same page are more likely to be in the same object stream, |
2050 | | // which is slightly more efficient, particularly with linearized files. This is better than |
2051 | | // iterating through the xref table since it avoids preserving orphaned items. |
2052 | | |
2053 | | // Exclude encryption dictionary, if any |
2054 | 1.23k | QPDFObjectHandle encryption_dict = m->trailer.getKey("/Encrypt"); |
2055 | 1.23k | QPDFObjGen encryption_dict_og = encryption_dict.getObjGen(); |
2056 | | |
2057 | 1.23k | const size_t max_obj = qpdf.getObjectCount(); |
2058 | 1.23k | std::vector<bool> visited(max_obj, false); |
2059 | 1.23k | std::vector<QPDFObjectHandle> queue; |
2060 | 1.23k | queue.reserve(512); |
2061 | 1.23k | queue.emplace_back(m->trailer); |
2062 | 1.23k | std::vector<T> result; |
2063 | 1.23k | if constexpr (std::is_same_v<T, QPDFObjGen>) { |
2064 | 0 | result.reserve(m->obj_cache.size()); |
2065 | 1.23k | } else { |
2066 | 1.23k | qpdf_static_expect(std::is_same_v<T, bool>); |
2067 | 1.23k | result.resize(max_obj + 1U, false); |
2068 | 1.23k | } |
2069 | 516k | while (!queue.empty()) { |
2070 | 515k | auto obj = queue.back(); |
2071 | 515k | queue.pop_back(); |
2072 | 515k | if (obj.getObjectID() > 0) { |
2073 | 106k | QPDFObjGen og = obj.getObjGen(); |
2074 | 106k | const size_t id = toS(og.getObj() - 1); |
2075 | 106k | if (id >= max_obj) { |
2076 | 0 | throw std::logic_error( |
2077 | 0 | "unexpected object id encountered in getCompressibleObjGens"); |
2078 | 0 | } |
2079 | 106k | if (visited[id]) { |
2080 | 32.9k | continue; |
2081 | 32.9k | } |
2082 | | |
2083 | | // Check whether this is the current object. If not, remove it (which changes it into a |
2084 | | // direct null and therefore stops us from revisiting it) and move on to the next object |
2085 | | // in the queue. |
2086 | 73.1k | auto upper = m->obj_cache.upper_bound(og); |
2087 | 73.1k | if (upper != m->obj_cache.end() && upper->first.getObj() == og.getObj()) { |
2088 | 1.19k | qpdf.removeObject(og); |
2089 | 1.19k | continue; |
2090 | 1.19k | } |
2091 | | |
2092 | 72.0k | visited[id] = true; |
2093 | | |
2094 | 72.0k | if (og == encryption_dict_og) { |
2095 | 119 | QTC::TC("qpdf", "QPDF exclude encryption dictionary"); |
2096 | 71.8k | } else if (!(obj.isStream() || |
2097 | 67.8k | (obj.isDictionaryOfType("/Sig") && obj.hasKey("/ByteRange") && |
2098 | 67.8k | obj.hasKey("/Contents")))) { |
2099 | 67.8k | if constexpr (std::is_same_v<T, QPDFObjGen>) { |
2100 | 0 | result.push_back(og); |
2101 | 67.8k | } else if constexpr (std::is_same_v<T, bool>) { |
2102 | 67.8k | result[id + 1U] = true; |
2103 | 67.8k | } |
2104 | 67.8k | } |
2105 | 72.0k | } |
2106 | 481k | if (obj.isStream()) { |
2107 | 4.06k | auto dict = obj.getDict().as_dictionary(); |
2108 | 4.06k | auto end = dict.crend(); |
2109 | 30.9k | for (auto iter = dict.crbegin(); iter != end; ++iter) { |
2110 | 26.8k | std::string const& key = iter->first; |
2111 | 26.8k | QPDFObjectHandle const& value = iter->second; |
2112 | 26.8k | if (!value.null()) { |
2113 | 23.4k | if (key == "/Length") { |
2114 | | // omit stream lengths |
2115 | 3.61k | if (value.isIndirect()) { |
2116 | 166 | QTC::TC("qpdf", "QPDF exclude indirect length"); |
2117 | 166 | } |
2118 | 19.8k | } else { |
2119 | 19.8k | queue.emplace_back(value); |
2120 | 19.8k | } |
2121 | 23.4k | } |
2122 | 26.8k | } |
2123 | 477k | } else if (obj.isDictionary()) { |
2124 | 42.0k | auto dict = obj.as_dictionary(); |
2125 | 42.0k | auto end = dict.crend(); |
2126 | 210k | for (auto iter = dict.crbegin(); iter != end; ++iter) { |
2127 | 168k | if (!iter->second.null()) { |
2128 | 141k | queue.emplace_back(iter->second); |
2129 | 141k | } |
2130 | 168k | } |
2131 | 435k | } else if (auto items = obj.as_array()) { |
2132 | 435k | queue.insert(queue.end(), items.crbegin(), items.crend()); |
2133 | 435k | } |
2134 | 481k | } |
2135 | | |
2136 | 1.23k | return result; |
2137 | 1.23k | } Unexecuted instantiation: std::__1::vector<QPDFObjGen, std::__1::allocator<QPDFObjGen> > QPDF::Doc::Objects::compressible<QPDFObjGen>() std::__1::vector<bool, std::__1::allocator<bool> > QPDF::Doc::Objects::compressible<bool>() Line | Count | Source | 2046 | 1.23k | { | 2047 | | // Return a list of objects that are allowed to be in object streams. Walk through the objects | 2048 | | // by traversing the document from the root, including a traversal of the pages tree. This | 2049 | | // makes that objects that are on the same page are more likely to be in the same object stream, | 2050 | | // which is slightly more efficient, particularly with linearized files. This is better than | 2051 | | // iterating through the xref table since it avoids preserving orphaned items. | 2052 | | | 2053 | | // Exclude encryption dictionary, if any | 2054 | 1.23k | QPDFObjectHandle encryption_dict = m->trailer.getKey("/Encrypt"); | 2055 | 1.23k | QPDFObjGen encryption_dict_og = encryption_dict.getObjGen(); | 2056 | | | 2057 | 1.23k | const size_t max_obj = qpdf.getObjectCount(); | 2058 | 1.23k | std::vector<bool> visited(max_obj, false); | 2059 | 1.23k | std::vector<QPDFObjectHandle> queue; | 2060 | 1.23k | queue.reserve(512); | 2061 | 1.23k | queue.emplace_back(m->trailer); | 2062 | 1.23k | std::vector<T> result; | 2063 | | if constexpr (std::is_same_v<T, QPDFObjGen>) { | 2064 | | result.reserve(m->obj_cache.size()); | 2065 | 1.23k | } else { | 2066 | 1.23k | qpdf_static_expect(std::is_same_v<T, bool>); | 2067 | 1.23k | result.resize(max_obj + 1U, false); | 2068 | 1.23k | } | 2069 | 516k | while (!queue.empty()) { | 2070 | 515k | auto obj = queue.back(); | 2071 | 515k | queue.pop_back(); | 2072 | 515k | if (obj.getObjectID() > 0) { | 2073 | 106k | QPDFObjGen og = obj.getObjGen(); | 2074 | 106k | const size_t id = toS(og.getObj() - 1); | 2075 | 106k | if (id >= max_obj) { | 2076 | 0 | throw std::logic_error( | 2077 | 0 | "unexpected object id encountered in getCompressibleObjGens"); | 2078 | 0 | } | 2079 | 106k | if (visited[id]) { | 2080 | 32.9k | continue; | 2081 | 32.9k | } | 2082 | | | 2083 | | // Check whether this is the current object. If not, remove it (which changes it into a | 2084 | | // direct null and therefore stops us from revisiting it) and move on to the next object | 2085 | | // in the queue. | 2086 | 73.1k | auto upper = m->obj_cache.upper_bound(og); | 2087 | 73.1k | if (upper != m->obj_cache.end() && upper->first.getObj() == og.getObj()) { | 2088 | 1.19k | qpdf.removeObject(og); | 2089 | 1.19k | continue; | 2090 | 1.19k | } | 2091 | | | 2092 | 72.0k | visited[id] = true; | 2093 | | | 2094 | 72.0k | if (og == encryption_dict_og) { | 2095 | 119 | QTC::TC("qpdf", "QPDF exclude encryption dictionary"); | 2096 | 71.8k | } else if (!(obj.isStream() || | 2097 | 67.8k | (obj.isDictionaryOfType("/Sig") && obj.hasKey("/ByteRange") && | 2098 | 67.8k | obj.hasKey("/Contents")))) { | 2099 | | if constexpr (std::is_same_v<T, QPDFObjGen>) { | 2100 | | result.push_back(og); | 2101 | 67.8k | } else if constexpr (std::is_same_v<T, bool>) { | 2102 | 67.8k | result[id + 1U] = true; | 2103 | 67.8k | } | 2104 | 67.8k | } | 2105 | 72.0k | } | 2106 | 481k | if (obj.isStream()) { | 2107 | 4.06k | auto dict = obj.getDict().as_dictionary(); | 2108 | 4.06k | auto end = dict.crend(); | 2109 | 30.9k | for (auto iter = dict.crbegin(); iter != end; ++iter) { | 2110 | 26.8k | std::string const& key = iter->first; | 2111 | 26.8k | QPDFObjectHandle const& value = iter->second; | 2112 | 26.8k | if (!value.null()) { | 2113 | 23.4k | if (key == "/Length") { | 2114 | | // omit stream lengths | 2115 | 3.61k | if (value.isIndirect()) { | 2116 | 166 | QTC::TC("qpdf", "QPDF exclude indirect length"); | 2117 | 166 | } | 2118 | 19.8k | } else { | 2119 | 19.8k | queue.emplace_back(value); | 2120 | 19.8k | } | 2121 | 23.4k | } | 2122 | 26.8k | } | 2123 | 477k | } else if (obj.isDictionary()) { | 2124 | 42.0k | auto dict = obj.as_dictionary(); | 2125 | 42.0k | auto end = dict.crend(); | 2126 | 210k | for (auto iter = dict.crbegin(); iter != end; ++iter) { | 2127 | 168k | if (!iter->second.null()) { | 2128 | 141k | queue.emplace_back(iter->second); | 2129 | 141k | } | 2130 | 168k | } | 2131 | 435k | } else if (auto items = obj.as_array()) { | 2132 | 435k | queue.insert(queue.end(), items.crbegin(), items.crend()); | 2133 | 435k | } | 2134 | 481k | } | 2135 | | | 2136 | 1.23k | return result; | 2137 | 1.23k | } |
|