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