/src/qpdf/libqpdf/QPDF_linearization.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // See doc/linearization. |
2 | | |
3 | | #include <qpdf/QPDF_private.hh> |
4 | | |
5 | | #include <qpdf/BitStream.hh> |
6 | | #include <qpdf/BitWriter.hh> |
7 | | #include <qpdf/InputSource_private.hh> |
8 | | #include <qpdf/Pipeline_private.hh> |
9 | | #include <qpdf/Pl_Buffer.hh> |
10 | | #include <qpdf/Pl_Flate.hh> |
11 | | #include <qpdf/Pl_String.hh> |
12 | | #include <qpdf/QPDFExc.hh> |
13 | | #include <qpdf/QPDFObjectHandle_private.hh> |
14 | | #include <qpdf/QPDFWriter_private.hh> |
15 | | #include <qpdf/QTC.hh> |
16 | | #include <qpdf/QUtil.hh> |
17 | | #include <qpdf/Util.hh> |
18 | | |
19 | | #include <algorithm> |
20 | | #include <cmath> |
21 | | #include <cstring> |
22 | | |
23 | | using namespace qpdf; |
24 | | using namespace std::literals; |
25 | | |
26 | | template <class T, class int_type> |
27 | | static void |
28 | | load_vector_int( |
29 | | BitStream& bit_stream, int nitems, std::vector<T>& vec, int bits_wanted, int_type T::* field) |
30 | 0 | { |
31 | 0 | bool append = vec.empty(); |
32 | | // nitems times, read bits_wanted from the given bit stream, storing results in the ith vector |
33 | | // entry. |
34 | |
|
35 | 0 | for (size_t i = 0; i < QIntC::to_size(nitems); ++i) { |
36 | 0 | if (append) { |
37 | 0 | vec.push_back(T()); |
38 | 0 | } |
39 | 0 | vec.at(i).*field = bit_stream.getBitsInt(QIntC::to_size(bits_wanted)); |
40 | 0 | } |
41 | 0 | if (QIntC::to_int(vec.size()) != nitems) { |
42 | 0 | throw std::logic_error("vector has wrong size in load_vector_int"); |
43 | 0 | } |
44 | | // The PDF spec says that each hint table starts at a byte boundary. Each "row" actually must |
45 | | // start on a byte boundary. |
46 | 0 | bit_stream.skipToNextByte(); |
47 | 0 | } Unexecuted instantiation: QPDF_linearization.cc:void load_vector_int<QPDF::HPageOffsetEntry, int>(BitStream&, int, std::__1::vector<QPDF::HPageOffsetEntry, std::__1::allocator<QPDF::HPageOffsetEntry> >&, int, int QPDF::HPageOffsetEntry::*) Unexecuted instantiation: QPDF_linearization.cc:void load_vector_int<QPDF::HPageOffsetEntry, long long>(BitStream&, int, std::__1::vector<QPDF::HPageOffsetEntry, std::__1::allocator<QPDF::HPageOffsetEntry> >&, int, long long QPDF::HPageOffsetEntry::*) Unexecuted instantiation: QPDF_linearization.cc:void load_vector_int<QPDF::HSharedObjectEntry, int>(BitStream&, int, std::__1::vector<QPDF::HSharedObjectEntry, std::__1::allocator<QPDF::HSharedObjectEntry> >&, int, int QPDF::HSharedObjectEntry::*) |
48 | | |
49 | | template <class T> |
50 | | static void |
51 | | load_vector_vector( |
52 | | BitStream& bit_stream, |
53 | | int nitems1, |
54 | | std::vector<T>& vec1, |
55 | | int T::* nitems2, |
56 | | int bits_wanted, |
57 | | std::vector<int> T::* vec2) |
58 | 0 | { |
59 | | // nitems1 times, read nitems2 (from the ith element of vec1) items into the vec2 vector field |
60 | | // of the ith item of vec1. |
61 | 0 | for (size_t i1 = 0; i1 < QIntC::to_size(nitems1); ++i1) { |
62 | 0 | for (int i2 = 0; i2 < vec1.at(i1).*nitems2; ++i2) { |
63 | 0 | (vec1.at(i1).*vec2).push_back(bit_stream.getBitsInt(QIntC::to_size(bits_wanted))); |
64 | 0 | } |
65 | 0 | } |
66 | 0 | bit_stream.skipToNextByte(); |
67 | 0 | } |
68 | | |
69 | | void |
70 | | QPDF::linearizationWarning(std::string_view msg) |
71 | 0 | { |
72 | 0 | m->linearization_warnings = true; |
73 | 0 | warn(qpdf_e_linearization, "", 0, std::string(msg)); |
74 | 0 | } |
75 | | |
76 | | bool |
77 | | QPDF::checkLinearization() |
78 | 0 | { |
79 | 0 | bool result = false; |
80 | 0 | try { |
81 | 0 | readLinearizationData(); |
82 | 0 | result = checkLinearizationInternal(); |
83 | 0 | } catch (std::runtime_error& e) { |
84 | 0 | linearizationWarning( |
85 | 0 | "error encountered while checking linearization data: " + std::string(e.what())); |
86 | 0 | } |
87 | 0 | return result; |
88 | 0 | } |
89 | | |
90 | | bool |
91 | | QPDF::isLinearized() |
92 | 0 | { |
93 | | // If the first object in the file is a dictionary with a suitable /Linearized key and has an /L |
94 | | // key that accurately indicates the file size, initialize m->lindict and return true. |
95 | | |
96 | | // A linearized PDF spec's first object will be contained within the first 1024 bytes of the |
97 | | // file and will be a dictionary with a valid /Linearized key. This routine looks for that and |
98 | | // does no additional validation. |
99 | | |
100 | | // The PDF spec says the linearization dictionary must be completely contained within the first |
101 | | // 1024 bytes of the file. Add a byte for a null terminator. |
102 | 0 | auto buffer = m->file->read(1024, 0); |
103 | 0 | size_t pos = 0; |
104 | 0 | while (true) { |
105 | | // Find a digit or end of buffer |
106 | 0 | pos = buffer.find_first_of("0123456789"sv, pos); |
107 | 0 | if (pos == std::string::npos) { |
108 | 0 | return false; |
109 | 0 | } |
110 | | // Seek to the digit. Then skip over digits for a potential |
111 | | // next iteration. |
112 | 0 | m->file->seek(toO(pos), SEEK_SET); |
113 | |
|
114 | 0 | auto t1 = readToken(*m->file, 20); |
115 | 0 | if (!(t1.isInteger() && readToken(*m->file, 6).isInteger() && |
116 | 0 | readToken(*m->file, 4).isWord("obj"))) { |
117 | 0 | pos = buffer.find_first_not_of("0123456789"sv, pos); |
118 | 0 | if (pos == std::string::npos) { |
119 | 0 | return false; |
120 | 0 | } |
121 | 0 | continue; |
122 | 0 | } |
123 | | |
124 | 0 | auto candidate = getObject(toI(QUtil::string_to_ll(t1.getValue().data())), 0); |
125 | 0 | if (!candidate.isDictionary()) { |
126 | 0 | return false; |
127 | 0 | } |
128 | | |
129 | 0 | auto linkey = candidate.getKey("/Linearized"); |
130 | 0 | if (!(linkey.isNumber() && toI(floor(linkey.getNumericValue())) == 1)) { |
131 | 0 | return false; |
132 | 0 | } |
133 | | |
134 | 0 | auto L = candidate.getKey("/L"); |
135 | 0 | if (!L.isInteger()) { |
136 | 0 | return false; |
137 | 0 | } |
138 | 0 | qpdf_offset_t Li = L.getIntValue(); |
139 | 0 | m->file->seek(0, SEEK_END); |
140 | 0 | if (Li != m->file->tell()) { |
141 | 0 | QTC::TC("qpdf", "QPDF /L mismatch"); |
142 | 0 | return false; |
143 | 0 | } |
144 | 0 | m->linp.file_size = Li; |
145 | 0 | m->lindict = candidate; |
146 | 0 | return true; |
147 | 0 | } |
148 | 0 | } |
149 | | |
150 | | void |
151 | | QPDF::readLinearizationData() |
152 | 0 | { |
153 | | // This function throws an exception (which is trapped by checkLinearization()) for any errors |
154 | | // that prevent loading. |
155 | |
|
156 | 0 | if (!isLinearized()) { |
157 | 0 | throw std::logic_error("called readLinearizationData for file that is not linearized"); |
158 | 0 | } |
159 | | |
160 | | // /L is read and stored in linp by isLinearized() |
161 | 0 | QPDFObjectHandle H = m->lindict.getKey("/H"); |
162 | 0 | QPDFObjectHandle O = m->lindict.getKey("/O"); |
163 | 0 | QPDFObjectHandle E = m->lindict.getKey("/E"); |
164 | 0 | QPDFObjectHandle N = m->lindict.getKey("/N"); |
165 | 0 | QPDFObjectHandle T = m->lindict.getKey("/T"); |
166 | 0 | QPDFObjectHandle P = m->lindict.getKey("/P"); |
167 | |
|
168 | 0 | if (!(H.isArray() && O.isInteger() && E.isInteger() && N.isInteger() && T.isInteger() && |
169 | 0 | (P.isInteger() || P.isNull()))) { |
170 | 0 | throw damagedPDF( |
171 | 0 | "linearization dictionary", |
172 | 0 | "some keys in linearization dictionary are of the wrong type"); |
173 | 0 | } |
174 | | |
175 | | // Hint table array: offset length [ offset length ] |
176 | 0 | size_t n_H_items = toS(H.getArrayNItems()); |
177 | 0 | if (!((n_H_items == 2) || (n_H_items == 4))) { |
178 | 0 | throw damagedPDF("linearization dictionary", "H has the wrong number of items"); |
179 | 0 | } |
180 | | |
181 | 0 | std::vector<int> H_items; |
182 | 0 | for (size_t i = 0; i < n_H_items; ++i) { |
183 | 0 | QPDFObjectHandle oh(H.getArrayItem(toI(i))); |
184 | 0 | if (oh.isInteger()) { |
185 | 0 | H_items.push_back(oh.getIntValueAsInt()); |
186 | 0 | } else { |
187 | 0 | throw damagedPDF("linearization dictionary", "some H items are of the wrong type"); |
188 | 0 | } |
189 | 0 | } |
190 | | |
191 | | // H: hint table offset/length for primary and overflow hint tables |
192 | 0 | int H0_offset = H_items.at(0); |
193 | 0 | int H0_length = H_items.at(1); |
194 | 0 | int H1_offset = 0; |
195 | 0 | int H1_length = 0; |
196 | 0 | if (H_items.size() == 4) { |
197 | | // Acrobat doesn't read or write these (as PDF 1.4), so we don't have a way to generate a |
198 | | // test case. |
199 | | // QTC::TC("qpdf", "QPDF overflow hint table"); |
200 | 0 | H1_offset = H_items.at(2); |
201 | 0 | H1_length = H_items.at(3); |
202 | 0 | } |
203 | | |
204 | | // P: first page number |
205 | 0 | int first_page = 0; |
206 | 0 | if (P.isInteger()) { |
207 | 0 | QTC::TC("qpdf", "QPDF P present in lindict"); |
208 | 0 | first_page = P.getIntValueAsInt(); |
209 | 0 | } else { |
210 | 0 | QTC::TC("qpdf", "QPDF P absent in lindict"); |
211 | 0 | } |
212 | | |
213 | | // Store linearization parameter data |
214 | | |
215 | | // Various places in the code use linp.npages, which is initialized from N, to pre-allocate |
216 | | // memory, so make sure it's accurate and bail right now if it's not. |
217 | 0 | if (N.getIntValue() != static_cast<long long>(getAllPages().size())) { |
218 | 0 | throw damagedPDF("linearization hint table", "/N does not match number of pages"); |
219 | 0 | } |
220 | | |
221 | | // file_size initialized by isLinearized() |
222 | 0 | m->linp.first_page_object = O.getIntValueAsInt(); |
223 | 0 | m->linp.first_page_end = E.getIntValue(); |
224 | 0 | m->linp.npages = N.getIntValueAsInt(); |
225 | 0 | m->linp.xref_zero_offset = T.getIntValue(); |
226 | 0 | m->linp.first_page = first_page; |
227 | 0 | m->linp.H_offset = H0_offset; |
228 | 0 | m->linp.H_length = H0_length; |
229 | | |
230 | | // Read hint streams |
231 | |
|
232 | 0 | Pl_Buffer pb("hint buffer"); |
233 | 0 | QPDFObjectHandle H0 = readHintStream(pb, H0_offset, toS(H0_length)); |
234 | 0 | if (H1_offset) { |
235 | 0 | (void)readHintStream(pb, H1_offset, toS(H1_length)); |
236 | 0 | } |
237 | | |
238 | | // PDF 1.4 hint tables that we ignore: |
239 | | |
240 | | // /T thumbnail |
241 | | // /A thread information |
242 | | // /E named destination |
243 | | // /V interactive form |
244 | | // /I information dictionary |
245 | | // /C logical structure |
246 | | // /L page label |
247 | | |
248 | | // Individual hint table offsets |
249 | 0 | QPDFObjectHandle HS = H0.getKey("/S"); // shared object |
250 | 0 | QPDFObjectHandle HO = H0.getKey("/O"); // outline |
251 | |
|
252 | 0 | auto hbp = pb.getBufferSharedPointer(); |
253 | 0 | Buffer* hb = hbp.get(); |
254 | 0 | unsigned char const* h_buf = hb->getBuffer(); |
255 | 0 | size_t h_size = hb->getSize(); |
256 | |
|
257 | 0 | readHPageOffset(BitStream(h_buf, h_size)); |
258 | |
|
259 | 0 | int HSi = HS.getIntValueAsInt(); |
260 | 0 | if ((HSi < 0) || (toS(HSi) >= h_size)) { |
261 | 0 | throw damagedPDF("linearization hint table", "/S (shared object) offset is out of bounds"); |
262 | 0 | } |
263 | 0 | readHSharedObject(BitStream(h_buf + HSi, h_size - toS(HSi))); |
264 | |
|
265 | 0 | if (HO.isInteger()) { |
266 | 0 | int HOi = HO.getIntValueAsInt(); |
267 | 0 | if ((HOi < 0) || (toS(HOi) >= h_size)) { |
268 | 0 | throw damagedPDF("linearization hint table", "/O (outline) offset is out of bounds"); |
269 | 0 | } |
270 | 0 | readHGeneric(BitStream(h_buf + HOi, h_size - toS(HOi)), m->outline_hints); |
271 | 0 | } |
272 | 0 | } |
273 | | |
274 | | QPDFObjectHandle |
275 | | QPDF::readHintStream(Pipeline& pl, qpdf_offset_t offset, size_t length) |
276 | 0 | { |
277 | 0 | auto H = readObjectAtOffset(offset, "linearization hint stream", false); |
278 | 0 | ObjCache& oc = m->obj_cache[H]; |
279 | 0 | qpdf_offset_t min_end_offset = oc.end_before_space; |
280 | 0 | qpdf_offset_t max_end_offset = oc.end_after_space; |
281 | 0 | if (!H.isStream()) { |
282 | 0 | throw damagedPDF("linearization dictionary", "hint table is not a stream"); |
283 | 0 | } |
284 | | |
285 | 0 | QPDFObjectHandle Hdict = H.getDict(); |
286 | | |
287 | | // Some versions of Acrobat make /Length indirect and place it immediately after the stream, |
288 | | // increasing length to cover it, even though the specification says all objects in the |
289 | | // linearization parameter dictionary must be direct. We have to get the file position of the |
290 | | // end of length in this case. |
291 | 0 | QPDFObjectHandle length_obj = Hdict.getKey("/Length"); |
292 | 0 | if (length_obj.isIndirect()) { |
293 | 0 | QTC::TC("qpdf", "QPDF hint table length indirect"); |
294 | | // Force resolution |
295 | 0 | (void)length_obj.getIntValue(); |
296 | 0 | ObjCache& oc2 = m->obj_cache[length_obj.getObjGen()]; |
297 | 0 | min_end_offset = oc2.end_before_space; |
298 | 0 | max_end_offset = oc2.end_after_space; |
299 | 0 | } else { |
300 | 0 | QTC::TC("qpdf", "QPDF hint table length direct"); |
301 | 0 | } |
302 | 0 | qpdf_offset_t computed_end = offset + toO(length); |
303 | 0 | if ((computed_end < min_end_offset) || (computed_end > max_end_offset)) { |
304 | 0 | linearizationWarning( |
305 | 0 | "expected = " + std::to_string(computed_end) + |
306 | 0 | "; actual = " + std::to_string(min_end_offset) + ".." + std::to_string(max_end_offset)); |
307 | 0 | throw damagedPDF("linearization dictionary", "hint table length mismatch"); |
308 | 0 | } |
309 | 0 | H.pipeStreamData(&pl, 0, qpdf_dl_specialized); |
310 | 0 | return Hdict; |
311 | 0 | } |
312 | | |
313 | | void |
314 | | QPDF::readHPageOffset(BitStream h) |
315 | 0 | { |
316 | | // All comments referring to the PDF spec refer to the spec for version 1.4. |
317 | |
|
318 | 0 | HPageOffset& t = m->page_offset_hints; |
319 | |
|
320 | 0 | t.min_nobjects = h.getBitsInt(32); // 1 |
321 | 0 | t.first_page_offset = h.getBitsInt(32); // 2 |
322 | 0 | t.nbits_delta_nobjects = h.getBitsInt(16); // 3 |
323 | 0 | t.min_page_length = h.getBitsInt(32); // 4 |
324 | 0 | t.nbits_delta_page_length = h.getBitsInt(16); // 5 |
325 | 0 | t.min_content_offset = h.getBitsInt(32); // 6 |
326 | 0 | t.nbits_delta_content_offset = h.getBitsInt(16); // 7 |
327 | 0 | t.min_content_length = h.getBitsInt(32); // 8 |
328 | 0 | t.nbits_delta_content_length = h.getBitsInt(16); // 9 |
329 | 0 | t.nbits_nshared_objects = h.getBitsInt(16); // 10 |
330 | 0 | t.nbits_shared_identifier = h.getBitsInt(16); // 11 |
331 | 0 | t.nbits_shared_numerator = h.getBitsInt(16); // 12 |
332 | 0 | t.shared_denominator = h.getBitsInt(16); // 13 |
333 | |
|
334 | 0 | std::vector<HPageOffsetEntry>& entries = t.entries; |
335 | 0 | entries.clear(); |
336 | 0 | int nitems = m->linp.npages; |
337 | 0 | load_vector_int(h, nitems, entries, t.nbits_delta_nobjects, &HPageOffsetEntry::delta_nobjects); |
338 | 0 | load_vector_int( |
339 | 0 | h, nitems, entries, t.nbits_delta_page_length, &HPageOffsetEntry::delta_page_length); |
340 | 0 | load_vector_int( |
341 | 0 | h, nitems, entries, t.nbits_nshared_objects, &HPageOffsetEntry::nshared_objects); |
342 | 0 | load_vector_vector( |
343 | 0 | h, |
344 | 0 | nitems, |
345 | 0 | entries, |
346 | 0 | &HPageOffsetEntry::nshared_objects, |
347 | 0 | t.nbits_shared_identifier, |
348 | 0 | &HPageOffsetEntry::shared_identifiers); |
349 | 0 | load_vector_vector( |
350 | 0 | h, |
351 | 0 | nitems, |
352 | 0 | entries, |
353 | 0 | &HPageOffsetEntry::nshared_objects, |
354 | 0 | t.nbits_shared_numerator, |
355 | 0 | &HPageOffsetEntry::shared_numerators); |
356 | 0 | load_vector_int( |
357 | 0 | h, nitems, entries, t.nbits_delta_content_offset, &HPageOffsetEntry::delta_content_offset); |
358 | 0 | load_vector_int( |
359 | 0 | h, nitems, entries, t.nbits_delta_content_length, &HPageOffsetEntry::delta_content_length); |
360 | 0 | } |
361 | | |
362 | | void |
363 | | QPDF::readHSharedObject(BitStream h) |
364 | 0 | { |
365 | 0 | HSharedObject& t = m->shared_object_hints; |
366 | |
|
367 | 0 | t.first_shared_obj = h.getBitsInt(32); // 1 |
368 | 0 | t.first_shared_offset = h.getBitsInt(32); // 2 |
369 | 0 | t.nshared_first_page = h.getBitsInt(32); // 3 |
370 | 0 | t.nshared_total = h.getBitsInt(32); // 4 |
371 | 0 | t.nbits_nobjects = h.getBitsInt(16); // 5 |
372 | 0 | t.min_group_length = h.getBitsInt(32); // 6 |
373 | 0 | t.nbits_delta_group_length = h.getBitsInt(16); // 7 |
374 | |
|
375 | 0 | QTC::TC( |
376 | 0 | "qpdf", |
377 | 0 | "QPDF lin nshared_total > nshared_first_page", |
378 | 0 | (t.nshared_total > t.nshared_first_page) ? 1 : 0); |
379 | |
|
380 | 0 | std::vector<HSharedObjectEntry>& entries = t.entries; |
381 | 0 | entries.clear(); |
382 | 0 | int nitems = t.nshared_total; |
383 | 0 | load_vector_int( |
384 | 0 | h, nitems, entries, t.nbits_delta_group_length, &HSharedObjectEntry::delta_group_length); |
385 | 0 | load_vector_int(h, nitems, entries, 1, &HSharedObjectEntry::signature_present); |
386 | 0 | for (size_t i = 0; i < toS(nitems); ++i) { |
387 | 0 | if (entries.at(i).signature_present) { |
388 | | // Skip 128-bit MD5 hash. These are not supported by acrobat, so they should probably |
389 | | // never be there. We have no test case for this. |
390 | 0 | for (int j = 0; j < 4; ++j) { |
391 | 0 | (void)h.getBits(32); |
392 | 0 | } |
393 | 0 | } |
394 | 0 | } |
395 | 0 | load_vector_int(h, nitems, entries, t.nbits_nobjects, &HSharedObjectEntry::nobjects_minus_one); |
396 | 0 | } |
397 | | |
398 | | void |
399 | | QPDF::readHGeneric(BitStream h, HGeneric& t) |
400 | 0 | { |
401 | 0 | t.first_object = h.getBitsInt(32); // 1 |
402 | 0 | t.first_object_offset = h.getBitsInt(32); // 2 |
403 | 0 | t.nobjects = h.getBitsInt(32); // 3 |
404 | 0 | t.group_length = h.getBitsInt(32); // 4 |
405 | 0 | } |
406 | | |
407 | | bool |
408 | | QPDF::checkLinearizationInternal() |
409 | 0 | { |
410 | | // All comments referring to the PDF spec refer to the spec for version 1.4. |
411 | | |
412 | | // Check all values in linearization parameter dictionary |
413 | |
|
414 | 0 | LinParameters& p = m->linp; |
415 | | |
416 | | // L: file size in bytes -- checked by isLinearized |
417 | | |
418 | | // O: object number of first page |
419 | 0 | std::vector<QPDFObjectHandle> const& pages = getAllPages(); |
420 | 0 | if (p.first_page_object != pages.at(0).getObjectID()) { |
421 | 0 | QTC::TC("qpdf", "QPDF err /O mismatch"); |
422 | 0 | linearizationWarning("first page object (/O) mismatch"); |
423 | 0 | } |
424 | | |
425 | | // N: number of pages |
426 | 0 | int npages = toI(pages.size()); |
427 | 0 | if (p.npages != npages) { |
428 | | // Not tested in the test suite |
429 | 0 | linearizationWarning("page count (/N) mismatch"); |
430 | 0 | } |
431 | |
|
432 | 0 | for (size_t i = 0; i < toS(npages); ++i) { |
433 | 0 | QPDFObjectHandle const& page = pages.at(i); |
434 | 0 | QPDFObjGen og(page.getObjGen()); |
435 | 0 | if (m->xref_table[og].getType() == 2) { |
436 | 0 | linearizationWarning( |
437 | 0 | "page dictionary for page " + std::to_string(i) + " is compressed"); |
438 | 0 | } |
439 | 0 | } |
440 | | |
441 | | // T: offset of whitespace character preceding xref entry for object 0 |
442 | 0 | m->file->seek(p.xref_zero_offset, SEEK_SET); |
443 | 0 | while (true) { |
444 | 0 | char ch; |
445 | 0 | m->file->read(&ch, 1); |
446 | 0 | if (!((ch == ' ') || (ch == '\r') || (ch == '\n'))) { |
447 | 0 | m->file->seek(-1, SEEK_CUR); |
448 | 0 | break; |
449 | 0 | } |
450 | 0 | } |
451 | 0 | if (m->file->tell() != m->first_xref_item_offset) { |
452 | 0 | QTC::TC("qpdf", "QPDF err /T mismatch"); |
453 | 0 | linearizationWarning( |
454 | 0 | "space before first xref item (/T) mismatch (computed = " + |
455 | 0 | std::to_string(m->first_xref_item_offset) + |
456 | 0 | "; file = " + std::to_string(m->file->tell())); |
457 | 0 | } |
458 | | |
459 | | // P: first page number -- Implementation note 124 says Acrobat ignores this value, so we will |
460 | | // too. |
461 | | |
462 | | // Check numbering of compressed objects in each xref section. For linearized files, all |
463 | | // compressed objects are supposed to be at the end of the containing xref section if any object |
464 | | // streams are in use. |
465 | |
|
466 | 0 | if (m->uncompressed_after_compressed) { |
467 | 0 | linearizationWarning( |
468 | 0 | "linearized file contains an uncompressed object after a compressed " |
469 | 0 | "one in a cross-reference stream"); |
470 | 0 | } |
471 | | |
472 | | // Further checking requires optimization and order calculation. Don't allow optimization to |
473 | | // make changes. If it has to, then the file is not properly linearized. We use the xref table |
474 | | // to figure out which objects are compressed and which are uncompressed. |
475 | 0 | { // local scope |
476 | 0 | std::map<int, int> object_stream_data; |
477 | 0 | for (auto const& iter: m->xref_table) { |
478 | 0 | QPDFObjGen const& og = iter.first; |
479 | 0 | QPDFXRefEntry const& entry = iter.second; |
480 | 0 | if (entry.getType() == 2) { |
481 | 0 | object_stream_data[og.getObj()] = entry.getObjStreamNumber(); |
482 | 0 | } |
483 | 0 | } |
484 | 0 | optimize_internal(object_stream_data, false, nullptr); |
485 | 0 | calculateLinearizationData(object_stream_data); |
486 | 0 | } |
487 | | |
488 | | // E: offset of end of first page -- Implementation note 123 says Acrobat includes on extra |
489 | | // object here by mistake. pdlin fails to place thumbnail images in section 9, so when |
490 | | // thumbnails are present, it also gets the wrong value for /E. It also doesn't count outlines |
491 | | // here when it should even though it places them in part 6. This code fails to put thread |
492 | | // information dictionaries in part 9, so it actually gets the wrong value for E when threads |
493 | | // are present. In that case, it would probably agree with pdlin. As of this writing, the test |
494 | | // suite doesn't contain any files with threads. |
495 | |
|
496 | 0 | if (m->part6.empty()) { |
497 | 0 | stopOnError("linearization part 6 unexpectedly empty"); |
498 | 0 | } |
499 | 0 | qpdf_offset_t min_E = -1; |
500 | 0 | qpdf_offset_t max_E = -1; |
501 | 0 | for (auto const& oh: m->part6) { |
502 | 0 | QPDFObjGen og(oh.getObjGen()); |
503 | 0 | if (!m->obj_cache.contains(og)) { |
504 | | // All objects have to have been dereferenced to be classified. |
505 | 0 | throw std::logic_error("linearization part6 object not in cache"); |
506 | 0 | } |
507 | 0 | ObjCache const& oc = m->obj_cache[og]; |
508 | 0 | min_E = std::max(min_E, oc.end_before_space); |
509 | 0 | max_E = std::max(max_E, oc.end_after_space); |
510 | 0 | } |
511 | 0 | if ((p.first_page_end < min_E) || (p.first_page_end > max_E)) { |
512 | 0 | QTC::TC("qpdf", "QPDF warn /E mismatch"); |
513 | 0 | linearizationWarning( |
514 | 0 | "end of first page section (/E) mismatch: /E = " + std::to_string(p.first_page_end) + |
515 | 0 | "; computed = " + std::to_string(min_E) + ".." + std::to_string(max_E)); |
516 | 0 | } |
517 | | |
518 | | // Check hint tables |
519 | |
|
520 | 0 | std::map<int, int> shared_idx_to_obj; |
521 | 0 | checkHSharedObject(pages, shared_idx_to_obj); |
522 | 0 | checkHPageOffset(pages, shared_idx_to_obj); |
523 | 0 | checkHOutlines(); |
524 | |
|
525 | 0 | return !m->linearization_warnings; |
526 | 0 | } |
527 | | |
528 | | qpdf_offset_t |
529 | | QPDF::maxEnd(ObjUser const& ou) |
530 | 0 | { |
531 | 0 | if (!m->obj_user_to_objects.contains(ou)) { |
532 | 0 | stopOnError("no entry in object user table for requested object user"); |
533 | 0 | } |
534 | 0 | qpdf_offset_t end = 0; |
535 | 0 | for (auto const& og: m->obj_user_to_objects[ou]) { |
536 | 0 | if (!m->obj_cache.contains(og)) { |
537 | 0 | stopOnError("unknown object referenced in object user table"); |
538 | 0 | } |
539 | 0 | end = std::max(end, m->obj_cache[og].end_after_space); |
540 | 0 | } |
541 | 0 | return end; |
542 | 0 | } |
543 | | |
544 | | qpdf_offset_t |
545 | | QPDF::getLinearizationOffset(QPDFObjGen og) |
546 | 0 | { |
547 | 0 | QPDFXRefEntry entry = m->xref_table[og]; |
548 | 0 | qpdf_offset_t result = 0; |
549 | 0 | switch (entry.getType()) { |
550 | 0 | case 1: |
551 | 0 | result = entry.getOffset(); |
552 | 0 | break; |
553 | | |
554 | 0 | case 2: |
555 | | // For compressed objects, return the offset of the object stream that contains them. |
556 | 0 | result = getLinearizationOffset(QPDFObjGen(entry.getObjStreamNumber(), 0)); |
557 | 0 | break; |
558 | | |
559 | 0 | default: |
560 | 0 | stopOnError("getLinearizationOffset called for xref entry not of type 1 or 2"); |
561 | 0 | break; |
562 | 0 | } |
563 | 0 | return result; |
564 | 0 | } |
565 | | |
566 | | QPDFObjectHandle |
567 | | QPDF::getUncompressedObject(QPDFObjectHandle& obj, std::map<int, int> const& object_stream_data) |
568 | 0 | { |
569 | 0 | if (obj.null() || (!object_stream_data.contains(obj.getObjectID()))) { |
570 | 0 | return obj; |
571 | 0 | } else { |
572 | 0 | int repl = (*(object_stream_data.find(obj.getObjectID()))).second; |
573 | 0 | return getObject(repl, 0); |
574 | 0 | } |
575 | 0 | } |
576 | | |
577 | | QPDFObjectHandle |
578 | | QPDF::getUncompressedObject(QPDFObjectHandle& oh, QPDFWriter::ObjTable const& obj) |
579 | 119k | { |
580 | 119k | if (obj.contains(oh)) { |
581 | 119k | if (auto id = obj[oh].object_stream; id > 0) { |
582 | 1.11k | return oh.isNull() ? oh : getObject(id, 0); |
583 | 1.11k | } |
584 | 119k | } |
585 | 118k | return oh; |
586 | 119k | } |
587 | | |
588 | | int |
589 | | QPDF::lengthNextN(int first_object, int n) |
590 | 0 | { |
591 | 0 | int length = 0; |
592 | 0 | for (int i = 0; i < n; ++i) { |
593 | 0 | QPDFObjGen og(first_object + i, 0); |
594 | 0 | if (!m->xref_table.contains(og)) { |
595 | 0 | linearizationWarning( |
596 | 0 | "no xref table entry for " + std::to_string(first_object + i) + " 0"); |
597 | 0 | } else { |
598 | 0 | if (!m->obj_cache.contains(og)) { |
599 | 0 | stopOnError("found unknown object while calculating length for linearization data"); |
600 | 0 | } |
601 | 0 | length += toI(m->obj_cache[og].end_after_space - getLinearizationOffset(og)); |
602 | 0 | } |
603 | 0 | } |
604 | 0 | return length; |
605 | 0 | } |
606 | | |
607 | | void |
608 | | QPDF::checkHPageOffset( |
609 | | std::vector<QPDFObjectHandle> const& pages, std::map<int, int>& shared_idx_to_obj) |
610 | 0 | { |
611 | | // Implementation note 126 says Acrobat always sets delta_content_offset and |
612 | | // delta_content_length in the page offset header dictionary to 0. It also states that |
613 | | // min_content_offset in the per-page information is always 0, which is an incorrect value. |
614 | | |
615 | | // Implementation note 127 explains that Acrobat always sets item 8 (min_content_length) to |
616 | | // zero, item 9 (nbits_delta_content_length) to the value of item 5 (nbits_delta_page_length), |
617 | | // and item 7 of each per-page hint table (delta_content_length) to item 2 (delta_page_length) |
618 | | // of that entry. Acrobat ignores these values when reading files. |
619 | | |
620 | | // Empirically, it also seems that Acrobat sometimes puts items under a page's /Resources |
621 | | // dictionary in with shared objects even when they are private. |
622 | |
|
623 | 0 | int npages = toI(pages.size()); |
624 | 0 | qpdf_offset_t table_offset = adjusted_offset(m->page_offset_hints.first_page_offset); |
625 | 0 | QPDFObjGen first_page_og(pages.at(0).getObjGen()); |
626 | 0 | if (!m->xref_table.contains(first_page_og)) { |
627 | 0 | stopOnError("supposed first page object is not known"); |
628 | 0 | } |
629 | 0 | qpdf_offset_t offset = getLinearizationOffset(first_page_og); |
630 | 0 | if (table_offset != offset) { |
631 | 0 | linearizationWarning("first page object offset mismatch"); |
632 | 0 | } |
633 | |
|
634 | 0 | for (int pageno = 0; pageno < npages; ++pageno) { |
635 | 0 | QPDFObjGen page_og(pages.at(toS(pageno)).getObjGen()); |
636 | 0 | int first_object = page_og.getObj(); |
637 | 0 | if (!m->xref_table.contains(page_og)) { |
638 | 0 | stopOnError("unknown object in page offset hint table"); |
639 | 0 | } |
640 | 0 | offset = getLinearizationOffset(page_og); |
641 | |
|
642 | 0 | HPageOffsetEntry& he = m->page_offset_hints.entries.at(toS(pageno)); |
643 | 0 | CHPageOffsetEntry& ce = m->c_page_offset_data.entries.at(toS(pageno)); |
644 | 0 | int h_nobjects = he.delta_nobjects + m->page_offset_hints.min_nobjects; |
645 | 0 | if (h_nobjects != ce.nobjects) { |
646 | | // This happens with pdlin when there are thumbnails. |
647 | 0 | linearizationWarning( |
648 | 0 | "object count mismatch for page " + std::to_string(pageno) + ": hint table = " + |
649 | 0 | std::to_string(h_nobjects) + "; computed = " + std::to_string(ce.nobjects)); |
650 | 0 | } |
651 | | |
652 | | // Use value for number of objects in hint table rather than computed value if there is a |
653 | | // discrepancy. |
654 | 0 | int length = lengthNextN(first_object, h_nobjects); |
655 | 0 | int h_length = toI(he.delta_page_length + m->page_offset_hints.min_page_length); |
656 | 0 | if (length != h_length) { |
657 | | // This condition almost certainly indicates a bad hint table or a bug in this code. |
658 | 0 | linearizationWarning( |
659 | 0 | "page length mismatch for page " + std::to_string(pageno) + ": hint table = " + |
660 | 0 | std::to_string(h_length) + "; computed length = " + std::to_string(length) + |
661 | 0 | " (offset = " + std::to_string(offset) + ")"); |
662 | 0 | } |
663 | |
|
664 | 0 | offset += h_length; |
665 | | |
666 | | // Translate shared object indexes to object numbers. |
667 | 0 | std::set<int> hint_shared; |
668 | 0 | std::set<int> computed_shared; |
669 | |
|
670 | 0 | if ((pageno == 0) && (he.nshared_objects > 0)) { |
671 | | // pdlin and Acrobat both do this even though the spec states clearly and unambiguously |
672 | | // that they should not. |
673 | 0 | linearizationWarning("page 0 has shared identifier entries"); |
674 | 0 | } |
675 | |
|
676 | 0 | for (size_t i = 0; i < toS(he.nshared_objects); ++i) { |
677 | 0 | int idx = he.shared_identifiers.at(i); |
678 | 0 | if (!shared_idx_to_obj.contains(idx)) { |
679 | 0 | stopOnError("unable to get object for item in shared objects hint table"); |
680 | 0 | } |
681 | 0 | hint_shared.insert(shared_idx_to_obj[idx]); |
682 | 0 | } |
683 | |
|
684 | 0 | for (size_t i = 0; i < toS(ce.nshared_objects); ++i) { |
685 | 0 | int idx = ce.shared_identifiers.at(i); |
686 | 0 | if (idx >= m->c_shared_object_data.nshared_total) { |
687 | 0 | stopOnError("index out of bounds for shared object hint table"); |
688 | 0 | } |
689 | 0 | int obj = m->c_shared_object_data.entries.at(toS(idx)).object; |
690 | 0 | computed_shared.insert(obj); |
691 | 0 | } |
692 | |
|
693 | 0 | for (int iter: hint_shared) { |
694 | 0 | if (!computed_shared.contains(iter)) { |
695 | | // pdlin puts thumbnails here even though it shouldn't |
696 | 0 | linearizationWarning( |
697 | 0 | "page " + std::to_string(pageno) + ": shared object " + std::to_string(iter) + |
698 | 0 | ": in hint table but not computed list"); |
699 | 0 | } |
700 | 0 | } |
701 | |
|
702 | 0 | for (int iter: computed_shared) { |
703 | 0 | if (!hint_shared.contains(iter)) { |
704 | | // Acrobat does not put some things including at least built-in fonts and procsets |
705 | | // here, at least in some cases. |
706 | 0 | linearizationWarning( |
707 | 0 | ("page " + std::to_string(pageno) + ": shared object " + std::to_string(iter) + |
708 | 0 | ": in computed list but not hint table")); |
709 | 0 | } |
710 | 0 | } |
711 | 0 | } |
712 | 0 | } |
713 | | |
714 | | void |
715 | | QPDF::checkHSharedObject(std::vector<QPDFObjectHandle> const& pages, std::map<int, int>& idx_to_obj) |
716 | 0 | { |
717 | | // Implementation note 125 says shared object groups always contain only one object. |
718 | | // Implementation note 128 says that Acrobat always nbits_nobjects to zero. Implementation note |
719 | | // 130 says that Acrobat does not support more than one shared object per group. These are all |
720 | | // consistent. |
721 | | |
722 | | // Implementation note 129 states that MD5 signatures are not implemented in Acrobat, so |
723 | | // signature_present must always be zero. |
724 | | |
725 | | // Implementation note 131 states that first_shared_obj and first_shared_offset have meaningless |
726 | | // values for single-page files. |
727 | | |
728 | | // Empirically, Acrobat and pdlin generate incorrect values for these whenever there are no |
729 | | // shared objects not referenced by the first page (i.e., nshared_total == nshared_first_page). |
730 | |
|
731 | 0 | HSharedObject& so = m->shared_object_hints; |
732 | 0 | if (so.nshared_total < so.nshared_first_page) { |
733 | 0 | linearizationWarning("shared object hint table: ntotal < nfirst_page"); |
734 | 0 | } else { |
735 | | // The first nshared_first_page objects are consecutive objects starting with the first page |
736 | | // object. The rest are consecutive starting from the first_shared_obj object. |
737 | 0 | int cur_object = pages.at(0).getObjectID(); |
738 | 0 | for (int i = 0; i < so.nshared_total; ++i) { |
739 | 0 | if (i == so.nshared_first_page) { |
740 | 0 | QTC::TC("qpdf", "QPDF lin check shared past first page"); |
741 | 0 | if (m->part8.empty()) { |
742 | 0 | linearizationWarning("part 8 is empty but nshared_total > nshared_first_page"); |
743 | 0 | } else { |
744 | 0 | int obj = m->part8.at(0).getObjectID(); |
745 | 0 | if (obj != so.first_shared_obj) { |
746 | 0 | linearizationWarning( |
747 | 0 | "first shared object number mismatch: hint table = " + |
748 | 0 | std::to_string(so.first_shared_obj) + |
749 | 0 | "; computed = " + std::to_string(obj)); |
750 | 0 | } |
751 | 0 | } |
752 | |
|
753 | 0 | cur_object = so.first_shared_obj; |
754 | |
|
755 | 0 | QPDFObjGen og(cur_object, 0); |
756 | 0 | if (!m->xref_table.contains(og)) { |
757 | 0 | stopOnError("unknown object in shared object hint table"); |
758 | 0 | } |
759 | 0 | qpdf_offset_t offset = getLinearizationOffset(og); |
760 | 0 | qpdf_offset_t h_offset = adjusted_offset(so.first_shared_offset); |
761 | 0 | if (offset != h_offset) { |
762 | 0 | linearizationWarning( |
763 | 0 | "first shared object offset mismatch: hint table = " + |
764 | 0 | std::to_string(h_offset) + "; computed = " + std::to_string(offset)); |
765 | 0 | } |
766 | 0 | } |
767 | |
|
768 | 0 | idx_to_obj[i] = cur_object; |
769 | 0 | HSharedObjectEntry& se = so.entries.at(toS(i)); |
770 | 0 | int nobjects = se.nobjects_minus_one + 1; |
771 | 0 | int length = lengthNextN(cur_object, nobjects); |
772 | 0 | int h_length = so.min_group_length + se.delta_group_length; |
773 | 0 | if (length != h_length) { |
774 | 0 | linearizationWarning( |
775 | 0 | "shared object " + std::to_string(i) + " length mismatch: hint table = " + |
776 | 0 | std::to_string(h_length) + "; computed = " + std::to_string(length)); |
777 | 0 | } |
778 | 0 | cur_object += nobjects; |
779 | 0 | } |
780 | 0 | } |
781 | 0 | } |
782 | | |
783 | | void |
784 | | QPDF::checkHOutlines() |
785 | 0 | { |
786 | | // Empirically, Acrobat generates the correct value for the object number but incorrectly stores |
787 | | // the next object number's offset as the offset, at least when outlines appear in part 6. It |
788 | | // also generates an incorrect value for length (specifically, the length that would cover the |
789 | | // correct number of objects from the wrong starting place). pdlin appears to generate correct |
790 | | // values in those cases. |
791 | |
|
792 | 0 | if (m->c_outline_data.nobjects == m->outline_hints.nobjects) { |
793 | 0 | if (m->c_outline_data.nobjects == 0) { |
794 | 0 | return; |
795 | 0 | } |
796 | | |
797 | 0 | if (m->c_outline_data.first_object == m->outline_hints.first_object) { |
798 | | // Check length and offset. Acrobat gets these wrong. |
799 | 0 | QPDFObjectHandle outlines = getRoot().getKey("/Outlines"); |
800 | 0 | if (!outlines.isIndirect()) { |
801 | | // This case is not exercised in test suite since not permitted by the spec, but if |
802 | | // this does occur, the code below would fail. |
803 | 0 | linearizationWarning("/Outlines key of root dictionary is not indirect"); |
804 | 0 | return; |
805 | 0 | } |
806 | 0 | QPDFObjGen og(outlines.getObjGen()); |
807 | 0 | if (!m->xref_table.contains(og)) { |
808 | 0 | stopOnError("unknown object in outlines hint table"); |
809 | 0 | } |
810 | 0 | qpdf_offset_t offset = getLinearizationOffset(og); |
811 | 0 | ObjUser ou(ObjUser::ou_root_key, "/Outlines"); |
812 | 0 | int length = toI(maxEnd(ou) - offset); |
813 | 0 | qpdf_offset_t table_offset = adjusted_offset(m->outline_hints.first_object_offset); |
814 | 0 | if (offset != table_offset) { |
815 | 0 | linearizationWarning( |
816 | 0 | "incorrect offset in outlines table: hint table = " + |
817 | 0 | std::to_string(table_offset) + "; computed = " + std::to_string(offset)); |
818 | 0 | } |
819 | 0 | int table_length = m->outline_hints.group_length; |
820 | 0 | if (length != table_length) { |
821 | 0 | linearizationWarning( |
822 | 0 | "incorrect length in outlines table: hint table = " + |
823 | 0 | std::to_string(table_length) + "; computed = " + std::to_string(length)); |
824 | 0 | } |
825 | 0 | } else { |
826 | 0 | linearizationWarning("incorrect first object number in outline hints table."); |
827 | 0 | } |
828 | 0 | } else { |
829 | 0 | linearizationWarning("incorrect object count in outline hint table"); |
830 | 0 | } |
831 | 0 | } |
832 | | |
833 | | void |
834 | | QPDF::showLinearizationData() |
835 | 0 | { |
836 | 0 | try { |
837 | 0 | readLinearizationData(); |
838 | 0 | checkLinearizationInternal(); |
839 | 0 | dumpLinearizationDataInternal(); |
840 | 0 | } catch (QPDFExc& e) { |
841 | 0 | linearizationWarning(e.what()); |
842 | 0 | } |
843 | 0 | } |
844 | | |
845 | | void |
846 | | QPDF::dumpLinearizationDataInternal() |
847 | 0 | { |
848 | 0 | *m->log->getInfo() << m->file->getName() << ": linearization data:\n\n"; |
849 | |
|
850 | 0 | *m->log->getInfo() << "file_size: " << m->linp.file_size << "\n" |
851 | 0 | << "first_page_object: " << m->linp.first_page_object << "\n" |
852 | 0 | << "first_page_end: " << m->linp.first_page_end << "\n" |
853 | 0 | << "npages: " << m->linp.npages << "\n" |
854 | 0 | << "xref_zero_offset: " << m->linp.xref_zero_offset << "\n" |
855 | 0 | << "first_page: " << m->linp.first_page << "\n" |
856 | 0 | << "H_offset: " << m->linp.H_offset << "\n" |
857 | 0 | << "H_length: " << m->linp.H_length << "\n" |
858 | 0 | << "\n"; |
859 | |
|
860 | 0 | *m->log->getInfo() << "Page Offsets Hint Table\n\n"; |
861 | 0 | dumpHPageOffset(); |
862 | 0 | *m->log->getInfo() << "\nShared Objects Hint Table\n\n"; |
863 | 0 | dumpHSharedObject(); |
864 | |
|
865 | 0 | if (m->outline_hints.nobjects > 0) { |
866 | 0 | *m->log->getInfo() << "\nOutlines Hint Table\n\n"; |
867 | 0 | dumpHGeneric(m->outline_hints); |
868 | 0 | } |
869 | 0 | } |
870 | | |
871 | | qpdf_offset_t |
872 | | QPDF::adjusted_offset(qpdf_offset_t offset) |
873 | 0 | { |
874 | | // All offsets >= H_offset have to be increased by H_length since all hint table location values |
875 | | // disregard the hint table itself. |
876 | 0 | if (offset >= m->linp.H_offset) { |
877 | 0 | return offset + m->linp.H_length; |
878 | 0 | } |
879 | 0 | return offset; |
880 | 0 | } |
881 | | |
882 | | void |
883 | | QPDF::dumpHPageOffset() |
884 | 0 | { |
885 | 0 | HPageOffset& t = m->page_offset_hints; |
886 | 0 | *m->log->getInfo() << "min_nobjects: " << t.min_nobjects << "\n" |
887 | 0 | << "first_page_offset: " << adjusted_offset(t.first_page_offset) << "\n" |
888 | 0 | << "nbits_delta_nobjects: " << t.nbits_delta_nobjects << "\n" |
889 | 0 | << "min_page_length: " << t.min_page_length << "\n" |
890 | 0 | << "nbits_delta_page_length: " << t.nbits_delta_page_length << "\n" |
891 | 0 | << "min_content_offset: " << t.min_content_offset << "\n" |
892 | 0 | << "nbits_delta_content_offset: " << t.nbits_delta_content_offset << "\n" |
893 | 0 | << "min_content_length: " << t.min_content_length << "\n" |
894 | 0 | << "nbits_delta_content_length: " << t.nbits_delta_content_length << "\n" |
895 | 0 | << "nbits_nshared_objects: " << t.nbits_nshared_objects << "\n" |
896 | 0 | << "nbits_shared_identifier: " << t.nbits_shared_identifier << "\n" |
897 | 0 | << "nbits_shared_numerator: " << t.nbits_shared_numerator << "\n" |
898 | 0 | << "shared_denominator: " << t.shared_denominator << "\n"; |
899 | |
|
900 | 0 | for (size_t i1 = 0; i1 < toS(m->linp.npages); ++i1) { |
901 | 0 | HPageOffsetEntry& pe = t.entries.at(i1); |
902 | 0 | *m->log->getInfo() << "Page " << i1 << ":\n" |
903 | 0 | << " nobjects: " << pe.delta_nobjects + t.min_nobjects << "\n" |
904 | 0 | << " length: " << pe.delta_page_length + t.min_page_length |
905 | 0 | << "\n" |
906 | | // content offset is relative to page, not file |
907 | 0 | << " content_offset: " << pe.delta_content_offset + t.min_content_offset |
908 | 0 | << "\n" |
909 | 0 | << " content_length: " << pe.delta_content_length + t.min_content_length |
910 | 0 | << "\n" |
911 | 0 | << " nshared_objects: " << pe.nshared_objects << "\n"; |
912 | 0 | for (size_t i2 = 0; i2 < toS(pe.nshared_objects); ++i2) { |
913 | 0 | *m->log->getInfo() << " identifier " << i2 << ": " << pe.shared_identifiers.at(i2) |
914 | 0 | << "\n"; |
915 | 0 | *m->log->getInfo() << " numerator " << i2 << ": " << pe.shared_numerators.at(i2) |
916 | 0 | << "\n"; |
917 | 0 | } |
918 | 0 | } |
919 | 0 | } |
920 | | |
921 | | void |
922 | | QPDF::dumpHSharedObject() |
923 | 0 | { |
924 | 0 | HSharedObject& t = m->shared_object_hints; |
925 | 0 | *m->log->getInfo() << "first_shared_obj: " << t.first_shared_obj << "\n" |
926 | 0 | << "first_shared_offset: " << adjusted_offset(t.first_shared_offset) << "\n" |
927 | 0 | << "nshared_first_page: " << t.nshared_first_page << "\n" |
928 | 0 | << "nshared_total: " << t.nshared_total << "\n" |
929 | 0 | << "nbits_nobjects: " << t.nbits_nobjects << "\n" |
930 | 0 | << "min_group_length: " << t.min_group_length << "\n" |
931 | 0 | << "nbits_delta_group_length: " << t.nbits_delta_group_length << "\n"; |
932 | |
|
933 | 0 | for (size_t i = 0; i < toS(t.nshared_total); ++i) { |
934 | 0 | HSharedObjectEntry& se = t.entries.at(i); |
935 | 0 | *m->log->getInfo() << "Shared Object " << i << ":\n" |
936 | 0 | << " group length: " << se.delta_group_length + t.min_group_length |
937 | 0 | << "\n"; |
938 | | // PDF spec says signature present nobjects_minus_one are always 0, so print them only if |
939 | | // they have a non-zero value. |
940 | 0 | if (se.signature_present) { |
941 | 0 | *m->log->getInfo() << " signature present\n"; |
942 | 0 | } |
943 | 0 | if (se.nobjects_minus_one != 0) { |
944 | 0 | *m->log->getInfo() << " nobjects: " << se.nobjects_minus_one + 1 << "\n"; |
945 | 0 | } |
946 | 0 | } |
947 | 0 | } |
948 | | |
949 | | void |
950 | | QPDF::dumpHGeneric(HGeneric& t) |
951 | 0 | { |
952 | 0 | *m->log->getInfo() << "first_object: " << t.first_object << "\n" |
953 | 0 | << "first_object_offset: " << adjusted_offset(t.first_object_offset) << "\n" |
954 | 0 | << "nobjects: " << t.nobjects << "\n" |
955 | 0 | << "group_length: " << t.group_length << "\n"; |
956 | 0 | } |
957 | | |
958 | | template <typename T> |
959 | | void |
960 | | QPDF::calculateLinearizationData(T const& object_stream_data) |
961 | 33.1k | { |
962 | | // This function calculates the ordering of objects, divides them into the appropriate parts, |
963 | | // and computes some values for the linearization parameter dictionary and hint tables. The |
964 | | // file must be optimized (via calling optimize()) prior to calling this function. Note that |
965 | | // actual offsets and lengths are not computed here, but anything related to object ordering is. |
966 | | |
967 | 33.1k | if (m->object_to_obj_users.empty()) { |
968 | | // Note that we can't call optimize here because we don't know whether it should be called |
969 | | // with or without allow changes. |
970 | 0 | throw std::logic_error( |
971 | 0 | "INTERNAL ERROR: QPDF::calculateLinearizationData called before optimize()"); |
972 | 0 | } |
973 | | |
974 | | // Separate objects into the categories sufficient for us to determine which part of the |
975 | | // linearized file should contain the object. This categorization is useful for other purposes |
976 | | // as well. Part numbers refer to version 1.4 of the PDF spec. |
977 | | |
978 | | // Parts 1, 3, 5, 10, and 11 don't contain any objects from the original file (except the |
979 | | // trailer dictionary in part 11). |
980 | | |
981 | | // Part 4 is the document catalog (root) and the following root keys: /ViewerPreferences, |
982 | | // /PageMode, /Threads, /OpenAction, /AcroForm, /Encrypt. Note that Thread information |
983 | | // dictionaries are supposed to appear in part 9, but we are disregarding that recommendation |
984 | | // for now. |
985 | | |
986 | | // Part 6 is the first page section. It includes all remaining objects referenced by the first |
987 | | // page including shared objects but not including thumbnails. Additionally, if /PageMode is |
988 | | // /Outlines, then information from /Outlines also appears here. |
989 | | |
990 | | // Part 7 contains remaining objects private to pages other than the first page. |
991 | | |
992 | | // Part 8 contains all remaining shared objects except those that are shared only within |
993 | | // thumbnails. |
994 | | |
995 | | // Part 9 contains all remaining objects. |
996 | | |
997 | | // We sort objects into the following categories: |
998 | | |
999 | | // * open_document: part 4 |
1000 | | |
1001 | | // * first_page_private: part 6 |
1002 | | |
1003 | | // * first_page_shared: part 6 |
1004 | | |
1005 | | // * other_page_private: part 7 |
1006 | | |
1007 | | // * other_page_shared: part 8 |
1008 | | |
1009 | | // * thumbnail_private: part 9 |
1010 | | |
1011 | | // * thumbnail_shared: part 9 |
1012 | | |
1013 | | // * other: part 9 |
1014 | | |
1015 | | // * outlines: part 6 or 9 |
1016 | | |
1017 | 33.1k | m->part4.clear(); |
1018 | 33.1k | m->part6.clear(); |
1019 | 33.1k | m->part7.clear(); |
1020 | 33.1k | m->part8.clear(); |
1021 | 33.1k | m->part9.clear(); |
1022 | 33.1k | m->c_linp = LinParameters(); |
1023 | 33.1k | m->c_page_offset_data = CHPageOffset(); |
1024 | 33.1k | m->c_shared_object_data = CHSharedObject(); |
1025 | 33.1k | m->c_outline_data = HGeneric(); |
1026 | | |
1027 | 33.1k | QPDFObjectHandle root = getRoot(); |
1028 | 33.1k | bool outlines_in_first_page = false; |
1029 | 33.1k | QPDFObjectHandle pagemode = root.getKey("/PageMode"); |
1030 | 33.1k | QTC::TC("qpdf", "QPDF categorize pagemode present", pagemode.isName() ? 1 : 0); |
1031 | 33.1k | if (pagemode.isName()) { |
1032 | 1.84k | if (pagemode.getName() == "/UseOutlines") { |
1033 | 1.22k | if (root.hasKey("/Outlines")) { |
1034 | 614 | outlines_in_first_page = true; |
1035 | 614 | } else { |
1036 | 610 | QTC::TC("qpdf", "QPDF UseOutlines but no Outlines"); |
1037 | 610 | } |
1038 | 1.22k | } |
1039 | 1.84k | QTC::TC("qpdf", "QPDF categorize pagemode outlines", outlines_in_first_page ? 1 : 0); |
1040 | 1.84k | } |
1041 | | |
1042 | 33.1k | std::set<std::string> open_document_keys; |
1043 | 33.1k | open_document_keys.insert("/ViewerPreferences"); |
1044 | 33.1k | open_document_keys.insert("/PageMode"); |
1045 | 33.1k | open_document_keys.insert("/Threads"); |
1046 | 33.1k | open_document_keys.insert("/OpenAction"); |
1047 | 33.1k | open_document_keys.insert("/AcroForm"); |
1048 | | |
1049 | 33.1k | std::set<QPDFObjGen> lc_open_document; |
1050 | 33.1k | std::set<QPDFObjGen> lc_first_page_private; |
1051 | 33.1k | std::set<QPDFObjGen> lc_first_page_shared; |
1052 | 33.1k | std::set<QPDFObjGen> lc_other_page_private; |
1053 | 33.1k | std::set<QPDFObjGen> lc_other_page_shared; |
1054 | 33.1k | std::set<QPDFObjGen> lc_thumbnail_private; |
1055 | 33.1k | std::set<QPDFObjGen> lc_thumbnail_shared; |
1056 | 33.1k | std::set<QPDFObjGen> lc_other; |
1057 | 33.1k | std::set<QPDFObjGen> lc_outlines; |
1058 | 33.1k | std::set<QPDFObjGen> lc_root; |
1059 | | |
1060 | 471k | for (auto& oiter: m->object_to_obj_users) { |
1061 | 471k | QPDFObjGen const& og = oiter.first; |
1062 | 471k | std::set<ObjUser>& ous = oiter.second; |
1063 | | |
1064 | 471k | bool in_open_document = false; |
1065 | 471k | bool in_first_page = false; |
1066 | 471k | int other_pages = 0; |
1067 | 471k | int thumbs = 0; |
1068 | 471k | int others = 0; |
1069 | 471k | bool in_outlines = false; |
1070 | 471k | bool is_root = false; |
1071 | | |
1072 | 943k | for (auto const& ou: ous) { |
1073 | 943k | switch (ou.ou_type) { |
1074 | 57.5k | case ObjUser::ou_trailer_key: |
1075 | 57.5k | if (ou.key == "/Encrypt") { |
1076 | 2.64k | in_open_document = true; |
1077 | 54.9k | } else { |
1078 | 54.9k | ++others; |
1079 | 54.9k | } |
1080 | 57.5k | break; |
1081 | | |
1082 | 37.6k | case ObjUser::ou_thumb: |
1083 | 37.6k | ++thumbs; |
1084 | 37.6k | break; |
1085 | | |
1086 | 307k | case ObjUser::ou_root_key: |
1087 | 307k | if (open_document_keys.contains(ou.key)) { |
1088 | 37.3k | in_open_document = true; |
1089 | 269k | } else if (ou.key == "/Outlines") { |
1090 | 14.2k | in_outlines = true; |
1091 | 255k | } else { |
1092 | 255k | ++others; |
1093 | 255k | } |
1094 | 307k | break; |
1095 | | |
1096 | 507k | case ObjUser::ou_page: |
1097 | 507k | if (ou.pageno == 0) { |
1098 | 209k | in_first_page = true; |
1099 | 298k | } else { |
1100 | 298k | ++other_pages; |
1101 | 298k | } |
1102 | 507k | break; |
1103 | | |
1104 | 33.1k | case ObjUser::ou_root: |
1105 | 33.1k | is_root = true; |
1106 | 33.1k | break; |
1107 | | |
1108 | 0 | case ObjUser::ou_bad: |
1109 | 0 | stopOnError("INTERNAL ERROR: QPDF::calculateLinearizationData: invalid user type"); |
1110 | 0 | break; |
1111 | 943k | } |
1112 | 943k | } |
1113 | | |
1114 | 471k | if (is_root) { |
1115 | 33.1k | lc_root.insert(og); |
1116 | 438k | } else if (in_outlines) { |
1117 | 14.1k | lc_outlines.insert(og); |
1118 | 424k | } else if (in_open_document) { |
1119 | 39.7k | lc_open_document.insert(og); |
1120 | 384k | } else if ((in_first_page) && (others == 0) && (other_pages == 0) && (thumbs == 0)) { |
1121 | 146k | lc_first_page_private.insert(og); |
1122 | 237k | } else if (in_first_page) { |
1123 | 49.0k | lc_first_page_shared.insert(og); |
1124 | 188k | } else if ((other_pages == 1) && (others == 0) && (thumbs == 0)) { |
1125 | 62.7k | lc_other_page_private.insert(og); |
1126 | 125k | } else if (other_pages > 1) { |
1127 | 15.7k | lc_other_page_shared.insert(og); |
1128 | 110k | } else if ((thumbs == 1) && (others == 0)) { |
1129 | 11.1k | lc_thumbnail_private.insert(og); |
1130 | 99.0k | } else if (thumbs > 1) { |
1131 | 4.63k | lc_thumbnail_shared.insert(og); |
1132 | 94.4k | } else { |
1133 | 94.4k | lc_other.insert(og); |
1134 | 94.4k | } |
1135 | 471k | } |
1136 | | |
1137 | | // Generate ordering for objects in the output file. Sometimes we just dump right from a set |
1138 | | // into a vector. Rather than optimizing this by going straight into the vector, we'll leave |
1139 | | // these phases separate for now. That way, this section can be concerned only with ordering, |
1140 | | // and the above section can be considered only with categorization. Note that sets of |
1141 | | // QPDFObjGens are sorted by QPDFObjGen. In a linearized file, objects appear in sequence with |
1142 | | // the possible exception of hints tables which we won't see here anyway. That means that |
1143 | | // running calculateLinearizationData() on a linearized file should give results identical to |
1144 | | // the original file ordering. |
1145 | | |
1146 | | // We seem to traverse the page tree a lot in this code, but we can address this for a future |
1147 | | // code optimization if necessary. Premature optimization is the root of all evil. |
1148 | 33.1k | std::vector<QPDFObjectHandle> pages; |
1149 | 33.1k | { // local scope |
1150 | | // Map all page objects to the containing object stream. This should be a no-op in a |
1151 | | // properly linearized file. |
1152 | 61.3k | for (auto oh: getAllPages()) { |
1153 | 61.3k | pages.push_back(getUncompressedObject(oh, object_stream_data)); |
1154 | 61.3k | } |
1155 | 33.1k | } |
1156 | 33.1k | int npages = toI(pages.size()); |
1157 | | |
1158 | | // We will be initializing some values of the computed hint tables. Specifically, we can |
1159 | | // initialize any items that deal with object numbers or counts but not any items that deal with |
1160 | | // lengths or offsets. The code that writes linearized files will have to fill in these values |
1161 | | // during the first pass. The validation code can compute them relatively easily given the rest |
1162 | | // of the information. |
1163 | | |
1164 | | // npages is the size of the existing pages vector, which has been created by traversing the |
1165 | | // pages tree, and as such is a reasonable size. |
1166 | 33.1k | m->c_linp.npages = npages; |
1167 | 33.1k | m->c_page_offset_data.entries = std::vector<CHPageOffsetEntry>(toS(npages)); |
1168 | | |
1169 | | // Part 4: open document objects. We don't care about the order. |
1170 | | |
1171 | 33.1k | if (lc_root.size() != 1) { |
1172 | 0 | stopOnError("found other than one root while calculating linearization data"); |
1173 | 0 | } |
1174 | 33.1k | m->part4.push_back(getObject(*(lc_root.begin()))); |
1175 | 39.7k | for (auto const& og: lc_open_document) { |
1176 | 39.7k | m->part4.push_back(getObject(og)); |
1177 | 39.7k | } |
1178 | | |
1179 | | // Part 6: first page objects. Note: implementation note 124 states that Acrobat always treats |
1180 | | // page 0 as the first page for linearization regardless of /OpenAction. pdlin doesn't provide |
1181 | | // any option to set this and also disregards /OpenAction. We will do the same. |
1182 | | |
1183 | | // First, place the actual first page object itself. |
1184 | 33.1k | if (pages.empty()) { |
1185 | 142 | stopOnError("no pages found while calculating linearization data"); |
1186 | 142 | } |
1187 | 33.1k | QPDFObjGen first_page_og(pages.at(0).getObjGen()); |
1188 | 33.1k | if (!lc_first_page_private.contains(first_page_og)) { |
1189 | 2.39k | stopOnError( |
1190 | 2.39k | "INTERNAL ERROR: QPDF::calculateLinearizationData: first page " |
1191 | 2.39k | "object not in lc_first_page_private"); |
1192 | 2.39k | } |
1193 | 33.1k | lc_first_page_private.erase(first_page_og); |
1194 | 33.1k | m->c_linp.first_page_object = pages.at(0).getObjectID(); |
1195 | 33.1k | m->part6.push_back(pages.at(0)); |
1196 | | |
1197 | | // The PDF spec "recommends" an order for the rest of the objects, but we are going to disregard |
1198 | | // it except to the extent that it groups private and shared objects contiguously for the sake |
1199 | | // of hint tables. |
1200 | | |
1201 | 116k | for (auto const& og: lc_first_page_private) { |
1202 | 116k | m->part6.push_back(getObject(og)); |
1203 | 116k | } |
1204 | | |
1205 | 40.4k | for (auto const& og: lc_first_page_shared) { |
1206 | 40.4k | m->part6.push_back(getObject(og)); |
1207 | 40.4k | } |
1208 | | |
1209 | | // Place the outline dictionary if it goes in the first page section. |
1210 | 33.1k | if (outlines_in_first_page) { |
1211 | 607 | pushOutlinesToPart(m->part6, lc_outlines, object_stream_data); |
1212 | 607 | } |
1213 | | |
1214 | | // Fill in page offset hint table information for the first page. The PDF spec says that |
1215 | | // nshared_objects should be zero for the first page. pdlin does not appear to obey this, but |
1216 | | // it fills in garbage values for all the shared object identifiers on the first page. |
1217 | | |
1218 | 33.1k | m->c_page_offset_data.entries.at(0).nobjects = toI(m->part6.size()); |
1219 | | |
1220 | | // Part 7: other pages' private objects |
1221 | | |
1222 | | // For each page in order: |
1223 | 60.7k | for (size_t i = 1; i < toS(npages); ++i) { |
1224 | | // Place this page's page object |
1225 | | |
1226 | 27.6k | QPDFObjGen page_og(pages.at(i).getObjGen()); |
1227 | 27.6k | if (!lc_other_page_private.contains(page_og)) { |
1228 | 109 | stopOnError( |
1229 | 109 | "INTERNAL ERROR: QPDF::calculateLinearizationData: page object for page " + |
1230 | 109 | std::to_string(i) + " not in lc_other_page_private"); |
1231 | 109 | } |
1232 | 27.6k | lc_other_page_private.erase(page_og); |
1233 | 27.6k | m->part7.push_back(pages.at(i)); |
1234 | | |
1235 | | // Place all non-shared objects referenced by this page, updating the page object count for |
1236 | | // the hint table. |
1237 | | |
1238 | 27.6k | m->c_page_offset_data.entries.at(i).nobjects = 1; |
1239 | | |
1240 | 27.6k | ObjUser ou(ObjUser::ou_page, toI(i)); |
1241 | 27.6k | if (!m->obj_user_to_objects.contains(ou)) { |
1242 | 0 | stopOnError("found unreferenced page while calculating linearization data"); |
1243 | 0 | } |
1244 | 294k | for (auto const& og: m->obj_user_to_objects[ou]) { |
1245 | 294k | if (lc_other_page_private.contains(og)) { |
1246 | 34.3k | lc_other_page_private.erase(og); |
1247 | 34.3k | m->part7.push_back(getObject(og)); |
1248 | 34.3k | ++m->c_page_offset_data.entries.at(i).nobjects; |
1249 | 34.3k | } |
1250 | 294k | } |
1251 | 27.6k | } |
1252 | | // That should have covered all part7 objects. |
1253 | 33.1k | if (!lc_other_page_private.empty()) { |
1254 | 0 | stopOnError( |
1255 | 0 | "INTERNAL ERROR: QPDF::calculateLinearizationData:" |
1256 | 0 | " lc_other_page_private is not empty after generation of part7"); |
1257 | 0 | } |
1258 | | |
1259 | | // Part 8: other pages' shared objects |
1260 | | |
1261 | | // Order is unimportant. |
1262 | 33.1k | for (auto const& og: lc_other_page_shared) { |
1263 | 15.6k | m->part8.push_back(getObject(og)); |
1264 | 15.6k | } |
1265 | | |
1266 | | // Part 9: other objects |
1267 | | |
1268 | | // The PDF specification makes recommendations on ordering here. We follow them only to a |
1269 | | // limited extent. Specifically, we put the pages tree first, then private thumbnail objects in |
1270 | | // page order, then shared thumbnail objects, and then outlines (unless in part 6). After that, |
1271 | | // we throw all remaining objects in arbitrary order. |
1272 | | |
1273 | | // Place the pages tree. |
1274 | 33.1k | std::set<QPDFObjGen> pages_ogs = |
1275 | 33.1k | m->obj_user_to_objects[ObjUser(ObjUser::ou_root_key, "/Pages")]; |
1276 | 33.1k | if (pages_ogs.empty()) { |
1277 | 278 | stopOnError("found empty pages tree while calculating linearization data"); |
1278 | 278 | } |
1279 | 54.4k | for (auto const& og: pages_ogs) { |
1280 | 54.4k | if (lc_other.contains(og)) { |
1281 | 38.7k | lc_other.erase(og); |
1282 | 38.7k | m->part9.push_back(getObject(og)); |
1283 | 38.7k | } |
1284 | 54.4k | } |
1285 | | |
1286 | | // Place private thumbnail images in page order. Slightly more information would be required if |
1287 | | // we were going to bother with thumbnail hint tables. |
1288 | 89.8k | for (size_t i = 0; i < toS(npages); ++i) { |
1289 | 56.6k | QPDFObjectHandle thumb = pages.at(i).getKey("/Thumb"); |
1290 | 56.6k | thumb = getUncompressedObject(thumb, object_stream_data); |
1291 | 56.6k | if (!thumb.isNull()) { |
1292 | | // Output the thumbnail itself |
1293 | 5.07k | QPDFObjGen thumb_og(thumb.getObjGen()); |
1294 | 5.07k | if (lc_thumbnail_private.contains(thumb_og)) { |
1295 | 3.87k | lc_thumbnail_private.erase(thumb_og); |
1296 | 3.87k | m->part9.push_back(thumb); |
1297 | 3.87k | } else { |
1298 | | // No internal error this time...there's nothing to stop this object from having |
1299 | | // been referred to somewhere else outside of a page's /Thumb, and if it had been, |
1300 | | // there's nothing to prevent it from having been in some set other than |
1301 | | // lc_thumbnail_private. |
1302 | 1.19k | } |
1303 | 5.07k | std::set<QPDFObjGen>& ogs = m->obj_user_to_objects[ObjUser(ObjUser::ou_thumb, toI(i))]; |
1304 | 34.9k | for (auto const& og: ogs) { |
1305 | 34.9k | if (lc_thumbnail_private.contains(og)) { |
1306 | 6.75k | lc_thumbnail_private.erase(og); |
1307 | 6.75k | m->part9.push_back(getObject(og)); |
1308 | 6.75k | } |
1309 | 34.9k | } |
1310 | 5.07k | } |
1311 | 56.6k | } |
1312 | 33.1k | if (!lc_thumbnail_private.empty()) { |
1313 | 28 | stopOnError( |
1314 | 28 | "INTERNAL ERROR: QPDF::calculateLinearizationData: lc_thumbnail_private not " |
1315 | 28 | "empty after placing thumbnails"); |
1316 | 28 | } |
1317 | | |
1318 | | // Place shared thumbnail objects |
1319 | 33.1k | for (auto const& og: lc_thumbnail_shared) { |
1320 | 4.34k | m->part9.push_back(getObject(og)); |
1321 | 4.34k | } |
1322 | | |
1323 | | // Place outlines unless in first page |
1324 | 33.1k | if (!outlines_in_first_page) { |
1325 | 29.6k | pushOutlinesToPart(m->part9, lc_outlines, object_stream_data); |
1326 | 29.6k | } |
1327 | | |
1328 | | // Place all remaining objects |
1329 | 53.1k | for (auto const& og: lc_other) { |
1330 | 53.1k | m->part9.push_back(getObject(og)); |
1331 | 53.1k | } |
1332 | | |
1333 | | // Make sure we got everything exactly once. |
1334 | | |
1335 | 33.1k | size_t num_placed = |
1336 | 33.1k | m->part4.size() + m->part6.size() + m->part7.size() + m->part8.size() + m->part9.size(); |
1337 | 33.1k | size_t num_wanted = m->object_to_obj_users.size(); |
1338 | 33.1k | if (num_placed != num_wanted) { |
1339 | 139 | stopOnError( |
1340 | 139 | "INTERNAL ERROR: QPDF::calculateLinearizationData: wrong " |
1341 | 139 | "number of objects placed (num_placed = " + |
1342 | 139 | std::to_string(num_placed) + "; number of objects: " + std::to_string(num_wanted)); |
1343 | 139 | } |
1344 | | |
1345 | | // Calculate shared object hint table information including references to shared objects from |
1346 | | // page offset hint data. |
1347 | | |
1348 | | // The shared object hint table consists of all part 6 (whether shared or not) in order followed |
1349 | | // by all part 8 objects in order. Add the objects to shared object data keeping a map of |
1350 | | // object number to index. Then populate the shared object information for the pages. |
1351 | | |
1352 | | // Note that two objects never have the same object number, so we can map from object number |
1353 | | // only without regards to generation. |
1354 | 33.1k | std::map<int, int> obj_to_index; |
1355 | | |
1356 | 33.1k | m->c_shared_object_data.nshared_first_page = toI(m->part6.size()); |
1357 | 33.1k | m->c_shared_object_data.nshared_total = |
1358 | 33.1k | m->c_shared_object_data.nshared_first_page + toI(m->part8.size()); |
1359 | | |
1360 | 33.1k | std::vector<CHSharedObjectEntry>& shared = m->c_shared_object_data.entries; |
1361 | 191k | for (auto& oh: m->part6) { |
1362 | 191k | int obj = oh.getObjectID(); |
1363 | 191k | obj_to_index[obj] = toI(shared.size()); |
1364 | 191k | shared.emplace_back(obj); |
1365 | 191k | } |
1366 | 33.1k | QTC::TC("qpdf", "QPDF lin part 8 empty", m->part8.empty() ? 1 : 0); |
1367 | 33.1k | if (!m->part8.empty()) { |
1368 | 598 | m->c_shared_object_data.first_shared_obj = m->part8.at(0).getObjectID(); |
1369 | 14.4k | for (auto& oh: m->part8) { |
1370 | 14.4k | int obj = oh.getObjectID(); |
1371 | 14.4k | obj_to_index[obj] = toI(shared.size()); |
1372 | 14.4k | shared.emplace_back(obj); |
1373 | 14.4k | } |
1374 | 598 | } |
1375 | 33.1k | if (static_cast<size_t>(m->c_shared_object_data.nshared_total) != |
1376 | 33.1k | m->c_shared_object_data.entries.size()) { |
1377 | 0 | stopOnError("shared object hint table has wrong number of entries"); |
1378 | 0 | } |
1379 | | |
1380 | | // Now compute the list of shared objects for each page after the first page. |
1381 | | |
1382 | 59.2k | for (size_t i = 1; i < toS(npages); ++i) { |
1383 | 26.1k | CHPageOffsetEntry& pe = m->c_page_offset_data.entries.at(i); |
1384 | 26.1k | ObjUser ou(ObjUser::ou_page, toI(i)); |
1385 | 26.1k | if (!m->obj_user_to_objects.contains(ou)) { |
1386 | 0 | stopOnError("found unreferenced page while calculating linearization data"); |
1387 | 0 | } |
1388 | 257k | for (auto const& og: m->obj_user_to_objects[ou]) { |
1389 | 257k | if ((m->object_to_obj_users[og].size() > 1) && (obj_to_index.contains(og.getObj()))) { |
1390 | 187k | int idx = obj_to_index[og.getObj()]; |
1391 | 187k | ++pe.nshared_objects; |
1392 | 187k | pe.shared_identifiers.push_back(idx); |
1393 | 187k | } |
1394 | 257k | } |
1395 | 26.1k | } |
1396 | 33.1k | } Unexecuted instantiation: void QPDF::calculateLinearizationData<std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<int const, int> > > >(std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<int const, int> > > const&) void QPDF::calculateLinearizationData<QPDFWriter::ObjTable>(QPDFWriter::ObjTable const&) Line | Count | Source | 961 | 33.1k | { | 962 | | // This function calculates the ordering of objects, divides them into the appropriate parts, | 963 | | // and computes some values for the linearization parameter dictionary and hint tables. The | 964 | | // file must be optimized (via calling optimize()) prior to calling this function. Note that | 965 | | // actual offsets and lengths are not computed here, but anything related to object ordering is. | 966 | | | 967 | 33.1k | if (m->object_to_obj_users.empty()) { | 968 | | // Note that we can't call optimize here because we don't know whether it should be called | 969 | | // with or without allow changes. | 970 | 0 | throw std::logic_error( | 971 | 0 | "INTERNAL ERROR: QPDF::calculateLinearizationData called before optimize()"); | 972 | 0 | } | 973 | | | 974 | | // Separate objects into the categories sufficient for us to determine which part of the | 975 | | // linearized file should contain the object. This categorization is useful for other purposes | 976 | | // as well. Part numbers refer to version 1.4 of the PDF spec. | 977 | | | 978 | | // Parts 1, 3, 5, 10, and 11 don't contain any objects from the original file (except the | 979 | | // trailer dictionary in part 11). | 980 | | | 981 | | // Part 4 is the document catalog (root) and the following root keys: /ViewerPreferences, | 982 | | // /PageMode, /Threads, /OpenAction, /AcroForm, /Encrypt. Note that Thread information | 983 | | // dictionaries are supposed to appear in part 9, but we are disregarding that recommendation | 984 | | // for now. | 985 | | | 986 | | // Part 6 is the first page section. It includes all remaining objects referenced by the first | 987 | | // page including shared objects but not including thumbnails. Additionally, if /PageMode is | 988 | | // /Outlines, then information from /Outlines also appears here. | 989 | | | 990 | | // Part 7 contains remaining objects private to pages other than the first page. | 991 | | | 992 | | // Part 8 contains all remaining shared objects except those that are shared only within | 993 | | // thumbnails. | 994 | | | 995 | | // Part 9 contains all remaining objects. | 996 | | | 997 | | // We sort objects into the following categories: | 998 | | | 999 | | // * open_document: part 4 | 1000 | | | 1001 | | // * first_page_private: part 6 | 1002 | | | 1003 | | // * first_page_shared: part 6 | 1004 | | | 1005 | | // * other_page_private: part 7 | 1006 | | | 1007 | | // * other_page_shared: part 8 | 1008 | | | 1009 | | // * thumbnail_private: part 9 | 1010 | | | 1011 | | // * thumbnail_shared: part 9 | 1012 | | | 1013 | | // * other: part 9 | 1014 | | | 1015 | | // * outlines: part 6 or 9 | 1016 | | | 1017 | 33.1k | m->part4.clear(); | 1018 | 33.1k | m->part6.clear(); | 1019 | 33.1k | m->part7.clear(); | 1020 | 33.1k | m->part8.clear(); | 1021 | 33.1k | m->part9.clear(); | 1022 | 33.1k | m->c_linp = LinParameters(); | 1023 | 33.1k | m->c_page_offset_data = CHPageOffset(); | 1024 | 33.1k | m->c_shared_object_data = CHSharedObject(); | 1025 | 33.1k | m->c_outline_data = HGeneric(); | 1026 | | | 1027 | 33.1k | QPDFObjectHandle root = getRoot(); | 1028 | 33.1k | bool outlines_in_first_page = false; | 1029 | 33.1k | QPDFObjectHandle pagemode = root.getKey("/PageMode"); | 1030 | 33.1k | QTC::TC("qpdf", "QPDF categorize pagemode present", pagemode.isName() ? 1 : 0); | 1031 | 33.1k | if (pagemode.isName()) { | 1032 | 1.84k | if (pagemode.getName() == "/UseOutlines") { | 1033 | 1.22k | if (root.hasKey("/Outlines")) { | 1034 | 614 | outlines_in_first_page = true; | 1035 | 614 | } else { | 1036 | 610 | QTC::TC("qpdf", "QPDF UseOutlines but no Outlines"); | 1037 | 610 | } | 1038 | 1.22k | } | 1039 | 1.84k | QTC::TC("qpdf", "QPDF categorize pagemode outlines", outlines_in_first_page ? 1 : 0); | 1040 | 1.84k | } | 1041 | | | 1042 | 33.1k | std::set<std::string> open_document_keys; | 1043 | 33.1k | open_document_keys.insert("/ViewerPreferences"); | 1044 | 33.1k | open_document_keys.insert("/PageMode"); | 1045 | 33.1k | open_document_keys.insert("/Threads"); | 1046 | 33.1k | open_document_keys.insert("/OpenAction"); | 1047 | 33.1k | open_document_keys.insert("/AcroForm"); | 1048 | | | 1049 | 33.1k | std::set<QPDFObjGen> lc_open_document; | 1050 | 33.1k | std::set<QPDFObjGen> lc_first_page_private; | 1051 | 33.1k | std::set<QPDFObjGen> lc_first_page_shared; | 1052 | 33.1k | std::set<QPDFObjGen> lc_other_page_private; | 1053 | 33.1k | std::set<QPDFObjGen> lc_other_page_shared; | 1054 | 33.1k | std::set<QPDFObjGen> lc_thumbnail_private; | 1055 | 33.1k | std::set<QPDFObjGen> lc_thumbnail_shared; | 1056 | 33.1k | std::set<QPDFObjGen> lc_other; | 1057 | 33.1k | std::set<QPDFObjGen> lc_outlines; | 1058 | 33.1k | std::set<QPDFObjGen> lc_root; | 1059 | | | 1060 | 471k | for (auto& oiter: m->object_to_obj_users) { | 1061 | 471k | QPDFObjGen const& og = oiter.first; | 1062 | 471k | std::set<ObjUser>& ous = oiter.second; | 1063 | | | 1064 | 471k | bool in_open_document = false; | 1065 | 471k | bool in_first_page = false; | 1066 | 471k | int other_pages = 0; | 1067 | 471k | int thumbs = 0; | 1068 | 471k | int others = 0; | 1069 | 471k | bool in_outlines = false; | 1070 | 471k | bool is_root = false; | 1071 | | | 1072 | 943k | for (auto const& ou: ous) { | 1073 | 943k | switch (ou.ou_type) { | 1074 | 57.5k | case ObjUser::ou_trailer_key: | 1075 | 57.5k | if (ou.key == "/Encrypt") { | 1076 | 2.64k | in_open_document = true; | 1077 | 54.9k | } else { | 1078 | 54.9k | ++others; | 1079 | 54.9k | } | 1080 | 57.5k | break; | 1081 | | | 1082 | 37.6k | case ObjUser::ou_thumb: | 1083 | 37.6k | ++thumbs; | 1084 | 37.6k | break; | 1085 | | | 1086 | 307k | case ObjUser::ou_root_key: | 1087 | 307k | if (open_document_keys.contains(ou.key)) { | 1088 | 37.3k | in_open_document = true; | 1089 | 269k | } else if (ou.key == "/Outlines") { | 1090 | 14.2k | in_outlines = true; | 1091 | 255k | } else { | 1092 | 255k | ++others; | 1093 | 255k | } | 1094 | 307k | break; | 1095 | | | 1096 | 507k | case ObjUser::ou_page: | 1097 | 507k | if (ou.pageno == 0) { | 1098 | 209k | in_first_page = true; | 1099 | 298k | } else { | 1100 | 298k | ++other_pages; | 1101 | 298k | } | 1102 | 507k | break; | 1103 | | | 1104 | 33.1k | case ObjUser::ou_root: | 1105 | 33.1k | is_root = true; | 1106 | 33.1k | break; | 1107 | | | 1108 | 0 | case ObjUser::ou_bad: | 1109 | 0 | stopOnError("INTERNAL ERROR: QPDF::calculateLinearizationData: invalid user type"); | 1110 | 0 | break; | 1111 | 943k | } | 1112 | 943k | } | 1113 | | | 1114 | 471k | if (is_root) { | 1115 | 33.1k | lc_root.insert(og); | 1116 | 438k | } else if (in_outlines) { | 1117 | 14.1k | lc_outlines.insert(og); | 1118 | 424k | } else if (in_open_document) { | 1119 | 39.7k | lc_open_document.insert(og); | 1120 | 384k | } else if ((in_first_page) && (others == 0) && (other_pages == 0) && (thumbs == 0)) { | 1121 | 146k | lc_first_page_private.insert(og); | 1122 | 237k | } else if (in_first_page) { | 1123 | 49.0k | lc_first_page_shared.insert(og); | 1124 | 188k | } else if ((other_pages == 1) && (others == 0) && (thumbs == 0)) { | 1125 | 62.7k | lc_other_page_private.insert(og); | 1126 | 125k | } else if (other_pages > 1) { | 1127 | 15.7k | lc_other_page_shared.insert(og); | 1128 | 110k | } else if ((thumbs == 1) && (others == 0)) { | 1129 | 11.1k | lc_thumbnail_private.insert(og); | 1130 | 99.0k | } else if (thumbs > 1) { | 1131 | 4.63k | lc_thumbnail_shared.insert(og); | 1132 | 94.4k | } else { | 1133 | 94.4k | lc_other.insert(og); | 1134 | 94.4k | } | 1135 | 471k | } | 1136 | | | 1137 | | // Generate ordering for objects in the output file. Sometimes we just dump right from a set | 1138 | | // into a vector. Rather than optimizing this by going straight into the vector, we'll leave | 1139 | | // these phases separate for now. That way, this section can be concerned only with ordering, | 1140 | | // and the above section can be considered only with categorization. Note that sets of | 1141 | | // QPDFObjGens are sorted by QPDFObjGen. In a linearized file, objects appear in sequence with | 1142 | | // the possible exception of hints tables which we won't see here anyway. That means that | 1143 | | // running calculateLinearizationData() on a linearized file should give results identical to | 1144 | | // the original file ordering. | 1145 | | | 1146 | | // We seem to traverse the page tree a lot in this code, but we can address this for a future | 1147 | | // code optimization if necessary. Premature optimization is the root of all evil. | 1148 | 33.1k | std::vector<QPDFObjectHandle> pages; | 1149 | 33.1k | { // local scope | 1150 | | // Map all page objects to the containing object stream. This should be a no-op in a | 1151 | | // properly linearized file. | 1152 | 61.3k | for (auto oh: getAllPages()) { | 1153 | 61.3k | pages.push_back(getUncompressedObject(oh, object_stream_data)); | 1154 | 61.3k | } | 1155 | 33.1k | } | 1156 | 33.1k | int npages = toI(pages.size()); | 1157 | | | 1158 | | // We will be initializing some values of the computed hint tables. Specifically, we can | 1159 | | // initialize any items that deal with object numbers or counts but not any items that deal with | 1160 | | // lengths or offsets. The code that writes linearized files will have to fill in these values | 1161 | | // during the first pass. The validation code can compute them relatively easily given the rest | 1162 | | // of the information. | 1163 | | | 1164 | | // npages is the size of the existing pages vector, which has been created by traversing the | 1165 | | // pages tree, and as such is a reasonable size. | 1166 | 33.1k | m->c_linp.npages = npages; | 1167 | 33.1k | m->c_page_offset_data.entries = std::vector<CHPageOffsetEntry>(toS(npages)); | 1168 | | | 1169 | | // Part 4: open document objects. We don't care about the order. | 1170 | | | 1171 | 33.1k | if (lc_root.size() != 1) { | 1172 | 0 | stopOnError("found other than one root while calculating linearization data"); | 1173 | 0 | } | 1174 | 33.1k | m->part4.push_back(getObject(*(lc_root.begin()))); | 1175 | 39.7k | for (auto const& og: lc_open_document) { | 1176 | 39.7k | m->part4.push_back(getObject(og)); | 1177 | 39.7k | } | 1178 | | | 1179 | | // Part 6: first page objects. Note: implementation note 124 states that Acrobat always treats | 1180 | | // page 0 as the first page for linearization regardless of /OpenAction. pdlin doesn't provide | 1181 | | // any option to set this and also disregards /OpenAction. We will do the same. | 1182 | | | 1183 | | // First, place the actual first page object itself. | 1184 | 33.1k | if (pages.empty()) { | 1185 | 142 | stopOnError("no pages found while calculating linearization data"); | 1186 | 142 | } | 1187 | 33.1k | QPDFObjGen first_page_og(pages.at(0).getObjGen()); | 1188 | 33.1k | if (!lc_first_page_private.contains(first_page_og)) { | 1189 | 2.39k | stopOnError( | 1190 | 2.39k | "INTERNAL ERROR: QPDF::calculateLinearizationData: first page " | 1191 | 2.39k | "object not in lc_first_page_private"); | 1192 | 2.39k | } | 1193 | 33.1k | lc_first_page_private.erase(first_page_og); | 1194 | 33.1k | m->c_linp.first_page_object = pages.at(0).getObjectID(); | 1195 | 33.1k | m->part6.push_back(pages.at(0)); | 1196 | | | 1197 | | // The PDF spec "recommends" an order for the rest of the objects, but we are going to disregard | 1198 | | // it except to the extent that it groups private and shared objects contiguously for the sake | 1199 | | // of hint tables. | 1200 | | | 1201 | 116k | for (auto const& og: lc_first_page_private) { | 1202 | 116k | m->part6.push_back(getObject(og)); | 1203 | 116k | } | 1204 | | | 1205 | 40.4k | for (auto const& og: lc_first_page_shared) { | 1206 | 40.4k | m->part6.push_back(getObject(og)); | 1207 | 40.4k | } | 1208 | | | 1209 | | // Place the outline dictionary if it goes in the first page section. | 1210 | 33.1k | if (outlines_in_first_page) { | 1211 | 607 | pushOutlinesToPart(m->part6, lc_outlines, object_stream_data); | 1212 | 607 | } | 1213 | | | 1214 | | // Fill in page offset hint table information for the first page. The PDF spec says that | 1215 | | // nshared_objects should be zero for the first page. pdlin does not appear to obey this, but | 1216 | | // it fills in garbage values for all the shared object identifiers on the first page. | 1217 | | | 1218 | 33.1k | m->c_page_offset_data.entries.at(0).nobjects = toI(m->part6.size()); | 1219 | | | 1220 | | // Part 7: other pages' private objects | 1221 | | | 1222 | | // For each page in order: | 1223 | 60.7k | for (size_t i = 1; i < toS(npages); ++i) { | 1224 | | // Place this page's page object | 1225 | | | 1226 | 27.6k | QPDFObjGen page_og(pages.at(i).getObjGen()); | 1227 | 27.6k | if (!lc_other_page_private.contains(page_og)) { | 1228 | 109 | stopOnError( | 1229 | 109 | "INTERNAL ERROR: QPDF::calculateLinearizationData: page object for page " + | 1230 | 109 | std::to_string(i) + " not in lc_other_page_private"); | 1231 | 109 | } | 1232 | 27.6k | lc_other_page_private.erase(page_og); | 1233 | 27.6k | m->part7.push_back(pages.at(i)); | 1234 | | | 1235 | | // Place all non-shared objects referenced by this page, updating the page object count for | 1236 | | // the hint table. | 1237 | | | 1238 | 27.6k | m->c_page_offset_data.entries.at(i).nobjects = 1; | 1239 | | | 1240 | 27.6k | ObjUser ou(ObjUser::ou_page, toI(i)); | 1241 | 27.6k | if (!m->obj_user_to_objects.contains(ou)) { | 1242 | 0 | stopOnError("found unreferenced page while calculating linearization data"); | 1243 | 0 | } | 1244 | 294k | for (auto const& og: m->obj_user_to_objects[ou]) { | 1245 | 294k | if (lc_other_page_private.contains(og)) { | 1246 | 34.3k | lc_other_page_private.erase(og); | 1247 | 34.3k | m->part7.push_back(getObject(og)); | 1248 | 34.3k | ++m->c_page_offset_data.entries.at(i).nobjects; | 1249 | 34.3k | } | 1250 | 294k | } | 1251 | 27.6k | } | 1252 | | // That should have covered all part7 objects. | 1253 | 33.1k | if (!lc_other_page_private.empty()) { | 1254 | 0 | stopOnError( | 1255 | 0 | "INTERNAL ERROR: QPDF::calculateLinearizationData:" | 1256 | 0 | " lc_other_page_private is not empty after generation of part7"); | 1257 | 0 | } | 1258 | | | 1259 | | // Part 8: other pages' shared objects | 1260 | | | 1261 | | // Order is unimportant. | 1262 | 33.1k | for (auto const& og: lc_other_page_shared) { | 1263 | 15.6k | m->part8.push_back(getObject(og)); | 1264 | 15.6k | } | 1265 | | | 1266 | | // Part 9: other objects | 1267 | | | 1268 | | // The PDF specification makes recommendations on ordering here. We follow them only to a | 1269 | | // limited extent. Specifically, we put the pages tree first, then private thumbnail objects in | 1270 | | // page order, then shared thumbnail objects, and then outlines (unless in part 6). After that, | 1271 | | // we throw all remaining objects in arbitrary order. | 1272 | | | 1273 | | // Place the pages tree. | 1274 | 33.1k | std::set<QPDFObjGen> pages_ogs = | 1275 | 33.1k | m->obj_user_to_objects[ObjUser(ObjUser::ou_root_key, "/Pages")]; | 1276 | 33.1k | if (pages_ogs.empty()) { | 1277 | 278 | stopOnError("found empty pages tree while calculating linearization data"); | 1278 | 278 | } | 1279 | 54.4k | for (auto const& og: pages_ogs) { | 1280 | 54.4k | if (lc_other.contains(og)) { | 1281 | 38.7k | lc_other.erase(og); | 1282 | 38.7k | m->part9.push_back(getObject(og)); | 1283 | 38.7k | } | 1284 | 54.4k | } | 1285 | | | 1286 | | // Place private thumbnail images in page order. Slightly more information would be required if | 1287 | | // we were going to bother with thumbnail hint tables. | 1288 | 89.8k | for (size_t i = 0; i < toS(npages); ++i) { | 1289 | 56.6k | QPDFObjectHandle thumb = pages.at(i).getKey("/Thumb"); | 1290 | 56.6k | thumb = getUncompressedObject(thumb, object_stream_data); | 1291 | 56.6k | if (!thumb.isNull()) { | 1292 | | // Output the thumbnail itself | 1293 | 5.07k | QPDFObjGen thumb_og(thumb.getObjGen()); | 1294 | 5.07k | if (lc_thumbnail_private.contains(thumb_og)) { | 1295 | 3.87k | lc_thumbnail_private.erase(thumb_og); | 1296 | 3.87k | m->part9.push_back(thumb); | 1297 | 3.87k | } else { | 1298 | | // No internal error this time...there's nothing to stop this object from having | 1299 | | // been referred to somewhere else outside of a page's /Thumb, and if it had been, | 1300 | | // there's nothing to prevent it from having been in some set other than | 1301 | | // lc_thumbnail_private. | 1302 | 1.19k | } | 1303 | 5.07k | std::set<QPDFObjGen>& ogs = m->obj_user_to_objects[ObjUser(ObjUser::ou_thumb, toI(i))]; | 1304 | 34.9k | for (auto const& og: ogs) { | 1305 | 34.9k | if (lc_thumbnail_private.contains(og)) { | 1306 | 6.75k | lc_thumbnail_private.erase(og); | 1307 | 6.75k | m->part9.push_back(getObject(og)); | 1308 | 6.75k | } | 1309 | 34.9k | } | 1310 | 5.07k | } | 1311 | 56.6k | } | 1312 | 33.1k | if (!lc_thumbnail_private.empty()) { | 1313 | 28 | stopOnError( | 1314 | 28 | "INTERNAL ERROR: QPDF::calculateLinearizationData: lc_thumbnail_private not " | 1315 | 28 | "empty after placing thumbnails"); | 1316 | 28 | } | 1317 | | | 1318 | | // Place shared thumbnail objects | 1319 | 33.1k | for (auto const& og: lc_thumbnail_shared) { | 1320 | 4.34k | m->part9.push_back(getObject(og)); | 1321 | 4.34k | } | 1322 | | | 1323 | | // Place outlines unless in first page | 1324 | 33.1k | if (!outlines_in_first_page) { | 1325 | 29.6k | pushOutlinesToPart(m->part9, lc_outlines, object_stream_data); | 1326 | 29.6k | } | 1327 | | | 1328 | | // Place all remaining objects | 1329 | 53.1k | for (auto const& og: lc_other) { | 1330 | 53.1k | m->part9.push_back(getObject(og)); | 1331 | 53.1k | } | 1332 | | | 1333 | | // Make sure we got everything exactly once. | 1334 | | | 1335 | 33.1k | size_t num_placed = | 1336 | 33.1k | m->part4.size() + m->part6.size() + m->part7.size() + m->part8.size() + m->part9.size(); | 1337 | 33.1k | size_t num_wanted = m->object_to_obj_users.size(); | 1338 | 33.1k | if (num_placed != num_wanted) { | 1339 | 139 | stopOnError( | 1340 | 139 | "INTERNAL ERROR: QPDF::calculateLinearizationData: wrong " | 1341 | 139 | "number of objects placed (num_placed = " + | 1342 | 139 | std::to_string(num_placed) + "; number of objects: " + std::to_string(num_wanted)); | 1343 | 139 | } | 1344 | | | 1345 | | // Calculate shared object hint table information including references to shared objects from | 1346 | | // page offset hint data. | 1347 | | | 1348 | | // The shared object hint table consists of all part 6 (whether shared or not) in order followed | 1349 | | // by all part 8 objects in order. Add the objects to shared object data keeping a map of | 1350 | | // object number to index. Then populate the shared object information for the pages. | 1351 | | | 1352 | | // Note that two objects never have the same object number, so we can map from object number | 1353 | | // only without regards to generation. | 1354 | 33.1k | std::map<int, int> obj_to_index; | 1355 | | | 1356 | 33.1k | m->c_shared_object_data.nshared_first_page = toI(m->part6.size()); | 1357 | 33.1k | m->c_shared_object_data.nshared_total = | 1358 | 33.1k | m->c_shared_object_data.nshared_first_page + toI(m->part8.size()); | 1359 | | | 1360 | 33.1k | std::vector<CHSharedObjectEntry>& shared = m->c_shared_object_data.entries; | 1361 | 191k | for (auto& oh: m->part6) { | 1362 | 191k | int obj = oh.getObjectID(); | 1363 | 191k | obj_to_index[obj] = toI(shared.size()); | 1364 | 191k | shared.emplace_back(obj); | 1365 | 191k | } | 1366 | 33.1k | QTC::TC("qpdf", "QPDF lin part 8 empty", m->part8.empty() ? 1 : 0); | 1367 | 33.1k | if (!m->part8.empty()) { | 1368 | 598 | m->c_shared_object_data.first_shared_obj = m->part8.at(0).getObjectID(); | 1369 | 14.4k | for (auto& oh: m->part8) { | 1370 | 14.4k | int obj = oh.getObjectID(); | 1371 | 14.4k | obj_to_index[obj] = toI(shared.size()); | 1372 | 14.4k | shared.emplace_back(obj); | 1373 | 14.4k | } | 1374 | 598 | } | 1375 | 33.1k | if (static_cast<size_t>(m->c_shared_object_data.nshared_total) != | 1376 | 33.1k | m->c_shared_object_data.entries.size()) { | 1377 | 0 | stopOnError("shared object hint table has wrong number of entries"); | 1378 | 0 | } | 1379 | | | 1380 | | // Now compute the list of shared objects for each page after the first page. | 1381 | | | 1382 | 59.2k | for (size_t i = 1; i < toS(npages); ++i) { | 1383 | 26.1k | CHPageOffsetEntry& pe = m->c_page_offset_data.entries.at(i); | 1384 | 26.1k | ObjUser ou(ObjUser::ou_page, toI(i)); | 1385 | 26.1k | if (!m->obj_user_to_objects.contains(ou)) { | 1386 | 0 | stopOnError("found unreferenced page while calculating linearization data"); | 1387 | 0 | } | 1388 | 257k | for (auto const& og: m->obj_user_to_objects[ou]) { | 1389 | 257k | if ((m->object_to_obj_users[og].size() > 1) && (obj_to_index.contains(og.getObj()))) { | 1390 | 187k | int idx = obj_to_index[og.getObj()]; | 1391 | 187k | ++pe.nshared_objects; | 1392 | 187k | pe.shared_identifiers.push_back(idx); | 1393 | 187k | } | 1394 | 257k | } | 1395 | 26.1k | } | 1396 | 33.1k | } |
|
1397 | | |
1398 | | template <typename T> |
1399 | | void |
1400 | | QPDF::pushOutlinesToPart( |
1401 | | std::vector<QPDFObjectHandle>& part, |
1402 | | std::set<QPDFObjGen>& lc_outlines, |
1403 | | T const& object_stream_data) |
1404 | 30.2k | { |
1405 | 30.2k | QPDFObjectHandle root = getRoot(); |
1406 | 30.2k | QPDFObjectHandle outlines = root.getKey("/Outlines"); |
1407 | 30.2k | if (outlines.isNull()) { |
1408 | 28.7k | return; |
1409 | 28.7k | } |
1410 | 1.42k | outlines = getUncompressedObject(outlines, object_stream_data); |
1411 | 1.42k | QPDFObjGen outlines_og(outlines.getObjGen()); |
1412 | 1.42k | QTC::TC( |
1413 | 1.42k | "qpdf", |
1414 | 1.42k | "QPDF lin outlines in part", |
1415 | 1.42k | ((&part == (&m->part6)) ? 0 |
1416 | 1.42k | : (&part == (&m->part9)) ? 1 |
1417 | 816 | : 9999)); // can't happen |
1418 | 1.42k | m->c_outline_data.first_object = outlines_og.getObj(); |
1419 | 1.42k | m->c_outline_data.nobjects = 1; |
1420 | 1.42k | lc_outlines.erase(outlines_og); |
1421 | 1.42k | part.push_back(outlines); |
1422 | 12.7k | for (auto const& og: lc_outlines) { |
1423 | 12.7k | part.push_back(getObject(og)); |
1424 | 12.7k | ++m->c_outline_data.nobjects; |
1425 | 12.7k | } |
1426 | 1.42k | } Unexecuted instantiation: void QPDF::pushOutlinesToPart<std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<int const, int> > > >(std::__1::vector<QPDFObjectHandle, std::__1::allocator<QPDFObjectHandle> >&, std::__1::set<QPDFObjGen, std::__1::less<QPDFObjGen>, std::__1::allocator<QPDFObjGen> >&, std::__1::map<int, int, std::__1::less<int>, std::__1::allocator<std::__1::pair<int const, int> > > const&) void QPDF::pushOutlinesToPart<QPDFWriter::ObjTable>(std::__1::vector<QPDFObjectHandle, std::__1::allocator<QPDFObjectHandle> >&, std::__1::set<QPDFObjGen, std::__1::less<QPDFObjGen>, std::__1::allocator<QPDFObjGen> >&, QPDFWriter::ObjTable const&) Line | Count | Source | 1404 | 30.2k | { | 1405 | 30.2k | QPDFObjectHandle root = getRoot(); | 1406 | 30.2k | QPDFObjectHandle outlines = root.getKey("/Outlines"); | 1407 | 30.2k | if (outlines.isNull()) { | 1408 | 28.7k | return; | 1409 | 28.7k | } | 1410 | 1.42k | outlines = getUncompressedObject(outlines, object_stream_data); | 1411 | 1.42k | QPDFObjGen outlines_og(outlines.getObjGen()); | 1412 | 1.42k | QTC::TC( | 1413 | 1.42k | "qpdf", | 1414 | 1.42k | "QPDF lin outlines in part", | 1415 | 1.42k | ((&part == (&m->part6)) ? 0 | 1416 | 1.42k | : (&part == (&m->part9)) ? 1 | 1417 | 816 | : 9999)); // can't happen | 1418 | 1.42k | m->c_outline_data.first_object = outlines_og.getObj(); | 1419 | 1.42k | m->c_outline_data.nobjects = 1; | 1420 | 1.42k | lc_outlines.erase(outlines_og); | 1421 | 1.42k | part.push_back(outlines); | 1422 | 12.7k | for (auto const& og: lc_outlines) { | 1423 | 12.7k | part.push_back(getObject(og)); | 1424 | 12.7k | ++m->c_outline_data.nobjects; | 1425 | 12.7k | } | 1426 | 1.42k | } |
|
1427 | | |
1428 | | void |
1429 | | QPDF::getLinearizedParts( |
1430 | | QPDFWriter::ObjTable const& obj, |
1431 | | std::vector<QPDFObjectHandle>& part4, |
1432 | | std::vector<QPDFObjectHandle>& part6, |
1433 | | std::vector<QPDFObjectHandle>& part7, |
1434 | | std::vector<QPDFObjectHandle>& part8, |
1435 | | std::vector<QPDFObjectHandle>& part9) |
1436 | 33.1k | { |
1437 | 33.1k | calculateLinearizationData(obj); |
1438 | 33.1k | part4 = m->part4; |
1439 | 33.1k | part6 = m->part6; |
1440 | 33.1k | part7 = m->part7; |
1441 | 33.1k | part8 = m->part8; |
1442 | 33.1k | part9 = m->part9; |
1443 | 33.1k | } |
1444 | | |
1445 | | static inline int |
1446 | | nbits(int val) |
1447 | 445k | { |
1448 | 445k | return (val == 0 ? 0 : (1 + nbits(val >> 1))); |
1449 | 445k | } |
1450 | | |
1451 | | int |
1452 | | QPDF::outputLengthNextN( |
1453 | | int in_object, int n, QPDFWriter::NewObjTable const& new_obj, QPDFWriter::ObjTable const& obj) |
1454 | 276k | { |
1455 | | // Figure out the length of a series of n consecutive objects in the output file starting with |
1456 | | // whatever object in_object from the input file mapped to. |
1457 | | |
1458 | 276k | int first = obj[in_object].renumber; |
1459 | 276k | int last = first + n; |
1460 | 276k | if (first <= 0) { |
1461 | 0 | stopOnError("found object that is not renumbered while writing linearization data"); |
1462 | 0 | } |
1463 | 276k | qpdf_offset_t length = 0; |
1464 | 854k | for (int i = first; i < last; ++i) { |
1465 | 578k | auto l = new_obj[i].length; |
1466 | 578k | if (l == 0) { |
1467 | 0 | stopOnError("found item with unknown length while writing linearization data"); |
1468 | 0 | } |
1469 | 578k | length += l; |
1470 | 578k | } |
1471 | 276k | return toI(length); |
1472 | 276k | } |
1473 | | |
1474 | | void |
1475 | | QPDF::calculateHPageOffset(QPDFWriter::NewObjTable const& new_obj, QPDFWriter::ObjTable const& obj) |
1476 | 27.1k | { |
1477 | | // Page Offset Hint Table |
1478 | | |
1479 | | // We are purposely leaving some values set to their initial zero values. |
1480 | | |
1481 | 27.1k | std::vector<QPDFObjectHandle> const& pages = getAllPages(); |
1482 | 27.1k | size_t npages = pages.size(); |
1483 | 27.1k | CHPageOffset& cph = m->c_page_offset_data; |
1484 | 27.1k | std::vector<CHPageOffsetEntry>& cphe = cph.entries; |
1485 | | |
1486 | | // Calculate minimum and maximum values for number of objects per page and page length. |
1487 | | |
1488 | 27.1k | int min_nobjects = cphe.at(0).nobjects; |
1489 | 27.1k | int max_nobjects = min_nobjects; |
1490 | 27.1k | int min_length = outputLengthNextN(pages.at(0).getObjectID(), min_nobjects, new_obj, obj); |
1491 | 27.1k | int max_length = min_length; |
1492 | 27.1k | int max_shared = cphe.at(0).nshared_objects; |
1493 | | |
1494 | 27.1k | HPageOffset& ph = m->page_offset_hints; |
1495 | 27.1k | std::vector<HPageOffsetEntry>& phe = ph.entries; |
1496 | | // npages is the size of the existing pages array. |
1497 | 27.1k | phe = std::vector<HPageOffsetEntry>(npages); |
1498 | | |
1499 | 78.5k | for (unsigned int i = 0; i < npages; ++i) { |
1500 | | // Calculate values for each page, assigning full values to the delta items. They will be |
1501 | | // adjusted later. |
1502 | | |
1503 | | // Repeat calculations for page 0 so we can assign to phe[i] without duplicating those |
1504 | | // assignments. |
1505 | | |
1506 | 51.4k | int nobjects = cphe.at(i).nobjects; |
1507 | 51.4k | int length = outputLengthNextN(pages.at(i).getObjectID(), nobjects, new_obj, obj); |
1508 | 51.4k | int nshared = cphe.at(i).nshared_objects; |
1509 | | |
1510 | 51.4k | min_nobjects = std::min(min_nobjects, nobjects); |
1511 | 51.4k | max_nobjects = std::max(max_nobjects, nobjects); |
1512 | 51.4k | min_length = std::min(min_length, length); |
1513 | 51.4k | max_length = std::max(max_length, length); |
1514 | 51.4k | max_shared = std::max(max_shared, nshared); |
1515 | | |
1516 | 51.4k | phe.at(i).delta_nobjects = nobjects; |
1517 | 51.4k | phe.at(i).delta_page_length = length; |
1518 | 51.4k | phe.at(i).nshared_objects = nshared; |
1519 | 51.4k | } |
1520 | | |
1521 | 27.1k | ph.min_nobjects = min_nobjects; |
1522 | 27.1k | ph.first_page_offset = new_obj[obj[pages.at(0)].renumber].xref.getOffset(); |
1523 | 27.1k | ph.nbits_delta_nobjects = nbits(max_nobjects - min_nobjects); |
1524 | 27.1k | ph.min_page_length = min_length; |
1525 | 27.1k | ph.nbits_delta_page_length = nbits(max_length - min_length); |
1526 | 27.1k | ph.nbits_nshared_objects = nbits(max_shared); |
1527 | 27.1k | ph.nbits_shared_identifier = nbits(m->c_shared_object_data.nshared_total); |
1528 | 27.1k | ph.shared_denominator = 4; // doesn't matter |
1529 | | |
1530 | | // It isn't clear how to compute content offset and content length. Since we are not |
1531 | | // interleaving page objects with the content stream, we'll use the same values for content |
1532 | | // length as page length. We will use 0 as content offset because this is what Adobe does |
1533 | | // (implementation note 127) and pdlin as well. |
1534 | 27.1k | ph.nbits_delta_content_length = ph.nbits_delta_page_length; |
1535 | 27.1k | ph.min_content_length = ph.min_page_length; |
1536 | | |
1537 | 78.5k | for (size_t i = 0; i < npages; ++i) { |
1538 | | // Adjust delta entries |
1539 | 51.4k | if ((phe.at(i).delta_nobjects < min_nobjects) || |
1540 | 51.4k | (phe.at(i).delta_page_length < min_length)) { |
1541 | 0 | stopOnError( |
1542 | 0 | "found too small delta nobjects or delta page length while writing " |
1543 | 0 | "linearization data"); |
1544 | 0 | } |
1545 | 51.4k | phe.at(i).delta_nobjects -= min_nobjects; |
1546 | 51.4k | phe.at(i).delta_page_length -= min_length; |
1547 | 51.4k | phe.at(i).delta_content_length = phe.at(i).delta_page_length; |
1548 | | |
1549 | 210k | for (size_t j = 0; j < toS(cphe.at(i).nshared_objects); ++j) { |
1550 | 158k | phe.at(i).shared_identifiers.push_back(cphe.at(i).shared_identifiers.at(j)); |
1551 | 158k | phe.at(i).shared_numerators.push_back(0); |
1552 | 158k | } |
1553 | 51.4k | } |
1554 | 27.1k | } |
1555 | | |
1556 | | void |
1557 | | QPDF::calculateHSharedObject( |
1558 | | QPDFWriter::NewObjTable const& new_obj, QPDFWriter::ObjTable const& obj) |
1559 | 27.1k | { |
1560 | 27.1k | CHSharedObject& cso = m->c_shared_object_data; |
1561 | 27.1k | std::vector<CHSharedObjectEntry>& csoe = cso.entries; |
1562 | 27.1k | HSharedObject& so = m->shared_object_hints; |
1563 | 27.1k | std::vector<HSharedObjectEntry>& soe = so.entries; |
1564 | 27.1k | soe.clear(); |
1565 | | |
1566 | 27.1k | int min_length = outputLengthNextN(csoe.at(0).object, 1, new_obj, obj); |
1567 | 27.1k | int max_length = min_length; |
1568 | | |
1569 | 196k | for (size_t i = 0; i < toS(cso.nshared_total); ++i) { |
1570 | | // Assign absolute numbers to deltas; adjust later |
1571 | 169k | int length = outputLengthNextN(csoe.at(i).object, 1, new_obj, obj); |
1572 | 169k | min_length = std::min(min_length, length); |
1573 | 169k | max_length = std::max(max_length, length); |
1574 | 169k | soe.emplace_back(); |
1575 | 169k | soe.at(i).delta_group_length = length; |
1576 | 169k | } |
1577 | 27.1k | if (soe.size() != toS(cso.nshared_total)) { |
1578 | 0 | stopOnError("soe has wrong size after initialization"); |
1579 | 0 | } |
1580 | | |
1581 | 27.1k | so.nshared_total = cso.nshared_total; |
1582 | 27.1k | so.nshared_first_page = cso.nshared_first_page; |
1583 | 27.1k | if (so.nshared_total > so.nshared_first_page) { |
1584 | 428 | so.first_shared_obj = obj[cso.first_shared_obj].renumber; |
1585 | 428 | so.min_group_length = min_length; |
1586 | 428 | so.first_shared_offset = new_obj[so.first_shared_obj].xref.getOffset(); |
1587 | 428 | } |
1588 | 27.1k | so.min_group_length = min_length; |
1589 | 27.1k | so.nbits_delta_group_length = nbits(max_length - min_length); |
1590 | | |
1591 | 196k | for (size_t i = 0; i < toS(cso.nshared_total); ++i) { |
1592 | | // Adjust deltas |
1593 | 169k | if (soe.at(i).delta_group_length < min_length) { |
1594 | 0 | stopOnError("found too small group length while writing linearization data"); |
1595 | 0 | } |
1596 | 169k | soe.at(i).delta_group_length -= min_length; |
1597 | 169k | } |
1598 | 27.1k | } |
1599 | | |
1600 | | void |
1601 | | QPDF::calculateHOutline(QPDFWriter::NewObjTable const& new_obj, QPDFWriter::ObjTable const& obj) |
1602 | 27.1k | { |
1603 | 27.1k | HGeneric& cho = m->c_outline_data; |
1604 | | |
1605 | 27.1k | if (cho.nobjects == 0) { |
1606 | 25.9k | return; |
1607 | 25.9k | } |
1608 | | |
1609 | 1.15k | HGeneric& ho = m->outline_hints; |
1610 | | |
1611 | 1.15k | ho.first_object = obj[cho.first_object].renumber; |
1612 | 1.15k | ho.first_object_offset = new_obj[ho.first_object].xref.getOffset(); |
1613 | 1.15k | ho.nobjects = cho.nobjects; |
1614 | 1.15k | ho.group_length = outputLengthNextN(cho.first_object, ho.nobjects, new_obj, obj); |
1615 | 1.15k | } |
1616 | | |
1617 | | template <class T, class int_type> |
1618 | | static void |
1619 | | write_vector_int(BitWriter& w, int nitems, std::vector<T>& vec, int bits, int_type T::* field) |
1620 | 216k | { |
1621 | | // nitems times, write bits bits from the given field of the ith vector to the given bit writer. |
1622 | | |
1623 | 983k | for (size_t i = 0; i < QIntC::to_size(nitems); ++i) { |
1624 | 766k | w.writeBits(QIntC::to_ulonglong(vec.at(i).*field), QIntC::to_size(bits)); |
1625 | 766k | } |
1626 | | // The PDF spec says that each hint table starts at a byte boundary. Each "row" actually must |
1627 | | // start on a byte boundary. |
1628 | 216k | w.flush(); |
1629 | 216k | } QPDF_linearization.cc:void write_vector_int<QPDF::HPageOffsetEntry, int>(BitWriter&, int, std::__1::vector<QPDF::HPageOffsetEntry, std::__1::allocator<QPDF::HPageOffsetEntry> >&, int, int QPDF::HPageOffsetEntry::*) Line | Count | Source | 1620 | 54.2k | { | 1621 | | // nitems times, write bits bits from the given field of the ith vector to the given bit writer. | 1622 | | | 1623 | 157k | for (size_t i = 0; i < QIntC::to_size(nitems); ++i) { | 1624 | 102k | w.writeBits(QIntC::to_ulonglong(vec.at(i).*field), QIntC::to_size(bits)); | 1625 | 102k | } | 1626 | | // The PDF spec says that each hint table starts at a byte boundary. Each "row" actually must | 1627 | | // start on a byte boundary. | 1628 | 54.2k | w.flush(); | 1629 | 54.2k | } |
QPDF_linearization.cc:void write_vector_int<QPDF::HPageOffsetEntry, long long>(BitWriter&, int, std::__1::vector<QPDF::HPageOffsetEntry, std::__1::allocator<QPDF::HPageOffsetEntry> >&, int, long long QPDF::HPageOffsetEntry::*) Line | Count | Source | 1620 | 81.3k | { | 1621 | | // nitems times, write bits bits from the given field of the ith vector to the given bit writer. | 1622 | | | 1623 | 235k | for (size_t i = 0; i < QIntC::to_size(nitems); ++i) { | 1624 | 154k | w.writeBits(QIntC::to_ulonglong(vec.at(i).*field), QIntC::to_size(bits)); | 1625 | 154k | } | 1626 | | // The PDF spec says that each hint table starts at a byte boundary. Each "row" actually must | 1627 | | // start on a byte boundary. | 1628 | 81.3k | w.flush(); | 1629 | 81.3k | } |
QPDF_linearization.cc:void write_vector_int<QPDF::HSharedObjectEntry, int>(BitWriter&, int, std::__1::vector<QPDF::HSharedObjectEntry, std::__1::allocator<QPDF::HSharedObjectEntry> >&, int, int QPDF::HSharedObjectEntry::*) Line | Count | Source | 1620 | 81.3k | { | 1621 | | // nitems times, write bits bits from the given field of the ith vector to the given bit writer. | 1622 | | | 1623 | 590k | for (size_t i = 0; i < QIntC::to_size(nitems); ++i) { | 1624 | 509k | w.writeBits(QIntC::to_ulonglong(vec.at(i).*field), QIntC::to_size(bits)); | 1625 | 509k | } | 1626 | | // The PDF spec says that each hint table starts at a byte boundary. Each "row" actually must | 1627 | | // start on a byte boundary. | 1628 | 81.3k | w.flush(); | 1629 | 81.3k | } |
|
1630 | | |
1631 | | template <class T> |
1632 | | static void |
1633 | | write_vector_vector( |
1634 | | BitWriter& w, |
1635 | | int nitems1, |
1636 | | std::vector<T>& vec1, |
1637 | | int T::* nitems2, |
1638 | | int bits, |
1639 | | std::vector<int> T::* vec2) |
1640 | 54.2k | { |
1641 | | // nitems1 times, write nitems2 (from the ith element of vec1) items from the vec2 vector field |
1642 | | // of the ith item of vec1. |
1643 | 157k | for (size_t i1 = 0; i1 < QIntC::to_size(nitems1); ++i1) { |
1644 | 420k | for (size_t i2 = 0; i2 < QIntC::to_size(vec1.at(i1).*nitems2); ++i2) { |
1645 | 317k | w.writeBits(QIntC::to_ulonglong((vec1.at(i1).*vec2).at(i2)), QIntC::to_size(bits)); |
1646 | 317k | } |
1647 | 102k | } |
1648 | 54.2k | w.flush(); |
1649 | 54.2k | } |
1650 | | |
1651 | | void |
1652 | | QPDF::writeHPageOffset(BitWriter& w) |
1653 | 27.1k | { |
1654 | 27.1k | HPageOffset& t = m->page_offset_hints; |
1655 | | |
1656 | 27.1k | w.writeBitsInt(t.min_nobjects, 32); // 1 |
1657 | 27.1k | w.writeBits(toULL(t.first_page_offset), 32); // 2 |
1658 | 27.1k | w.writeBitsInt(t.nbits_delta_nobjects, 16); // 3 |
1659 | 27.1k | w.writeBitsInt(t.min_page_length, 32); // 4 |
1660 | 27.1k | w.writeBitsInt(t.nbits_delta_page_length, 16); // 5 |
1661 | 27.1k | w.writeBits(toULL(t.min_content_offset), 32); // 6 |
1662 | 27.1k | w.writeBitsInt(t.nbits_delta_content_offset, 16); // 7 |
1663 | 27.1k | w.writeBitsInt(t.min_content_length, 32); // 8 |
1664 | 27.1k | w.writeBitsInt(t.nbits_delta_content_length, 16); // 9 |
1665 | 27.1k | w.writeBitsInt(t.nbits_nshared_objects, 16); // 10 |
1666 | 27.1k | w.writeBitsInt(t.nbits_shared_identifier, 16); // 11 |
1667 | 27.1k | w.writeBitsInt(t.nbits_shared_numerator, 16); // 12 |
1668 | 27.1k | w.writeBitsInt(t.shared_denominator, 16); // 13 |
1669 | | |
1670 | 27.1k | int nitems = toI(getAllPages().size()); |
1671 | 27.1k | std::vector<HPageOffsetEntry>& entries = t.entries; |
1672 | | |
1673 | 27.1k | write_vector_int(w, nitems, entries, t.nbits_delta_nobjects, &HPageOffsetEntry::delta_nobjects); |
1674 | 27.1k | write_vector_int( |
1675 | 27.1k | w, nitems, entries, t.nbits_delta_page_length, &HPageOffsetEntry::delta_page_length); |
1676 | 27.1k | write_vector_int( |
1677 | 27.1k | w, nitems, entries, t.nbits_nshared_objects, &HPageOffsetEntry::nshared_objects); |
1678 | 27.1k | write_vector_vector( |
1679 | 27.1k | w, |
1680 | 27.1k | nitems, |
1681 | 27.1k | entries, |
1682 | 27.1k | &HPageOffsetEntry::nshared_objects, |
1683 | 27.1k | t.nbits_shared_identifier, |
1684 | 27.1k | &HPageOffsetEntry::shared_identifiers); |
1685 | 27.1k | write_vector_vector( |
1686 | 27.1k | w, |
1687 | 27.1k | nitems, |
1688 | 27.1k | entries, |
1689 | 27.1k | &HPageOffsetEntry::nshared_objects, |
1690 | 27.1k | t.nbits_shared_numerator, |
1691 | 27.1k | &HPageOffsetEntry::shared_numerators); |
1692 | 27.1k | write_vector_int( |
1693 | 27.1k | w, nitems, entries, t.nbits_delta_content_offset, &HPageOffsetEntry::delta_content_offset); |
1694 | 27.1k | write_vector_int( |
1695 | 27.1k | w, nitems, entries, t.nbits_delta_content_length, &HPageOffsetEntry::delta_content_length); |
1696 | 27.1k | } |
1697 | | |
1698 | | void |
1699 | | QPDF::writeHSharedObject(BitWriter& w) |
1700 | 27.1k | { |
1701 | 27.1k | HSharedObject& t = m->shared_object_hints; |
1702 | | |
1703 | 27.1k | w.writeBitsInt(t.first_shared_obj, 32); // 1 |
1704 | 27.1k | w.writeBits(toULL(t.first_shared_offset), 32); // 2 |
1705 | 27.1k | w.writeBitsInt(t.nshared_first_page, 32); // 3 |
1706 | 27.1k | w.writeBitsInt(t.nshared_total, 32); // 4 |
1707 | 27.1k | w.writeBitsInt(t.nbits_nobjects, 16); // 5 |
1708 | 27.1k | w.writeBitsInt(t.min_group_length, 32); // 6 |
1709 | 27.1k | w.writeBitsInt(t.nbits_delta_group_length, 16); // 7 |
1710 | | |
1711 | 27.1k | QTC::TC( |
1712 | 27.1k | "qpdf", |
1713 | 27.1k | "QPDF lin write nshared_total > nshared_first_page", |
1714 | 27.1k | (t.nshared_total > t.nshared_first_page) ? 1 : 0); |
1715 | | |
1716 | 27.1k | int nitems = t.nshared_total; |
1717 | 27.1k | std::vector<HSharedObjectEntry>& entries = t.entries; |
1718 | | |
1719 | 27.1k | write_vector_int( |
1720 | 27.1k | w, nitems, entries, t.nbits_delta_group_length, &HSharedObjectEntry::delta_group_length); |
1721 | 27.1k | write_vector_int(w, nitems, entries, 1, &HSharedObjectEntry::signature_present); |
1722 | 196k | for (size_t i = 0; i < toS(nitems); ++i) { |
1723 | | // If signature were present, we'd have to write a 128-bit hash. |
1724 | 169k | if (entries.at(i).signature_present != 0) { |
1725 | 0 | stopOnError("found unexpected signature present while writing linearization data"); |
1726 | 0 | } |
1727 | 169k | } |
1728 | 27.1k | write_vector_int(w, nitems, entries, t.nbits_nobjects, &HSharedObjectEntry::nobjects_minus_one); |
1729 | 27.1k | } |
1730 | | |
1731 | | void |
1732 | | QPDF::writeHGeneric(BitWriter& w, HGeneric& t) |
1733 | 1.15k | { |
1734 | 1.15k | w.writeBitsInt(t.first_object, 32); // 1 |
1735 | 1.15k | w.writeBits(toULL(t.first_object_offset), 32); // 2 |
1736 | 1.15k | w.writeBitsInt(t.nobjects, 32); // 3 |
1737 | 1.15k | w.writeBitsInt(t.group_length, 32); // 4 |
1738 | 1.15k | } |
1739 | | |
1740 | | void |
1741 | | QPDF::generateHintStream( |
1742 | | QPDFWriter::NewObjTable const& new_obj, |
1743 | | QPDFWriter::ObjTable const& obj, |
1744 | | std::string& hint_buffer, |
1745 | | int& S, |
1746 | | int& O, |
1747 | | bool compressed) |
1748 | 27.1k | { |
1749 | | // Populate actual hint table values |
1750 | 27.1k | calculateHPageOffset(new_obj, obj); |
1751 | 27.1k | calculateHSharedObject(new_obj, obj); |
1752 | 27.1k | calculateHOutline(new_obj, obj); |
1753 | | |
1754 | | // Write the hint stream itself into a compressed memory buffer. Write through a counter so we |
1755 | | // can get offsets. |
1756 | 27.1k | std::string b; |
1757 | 27.1k | auto c = compressed |
1758 | 27.1k | ? std::make_unique<pl::Count>( |
1759 | 27.1k | 0, b, pl::create<Pl_Flate>(pl::create<pl::String>(hint_buffer), Pl_Flate::a_deflate)) |
1760 | 27.1k | : std::make_unique<pl::Count>(0, hint_buffer); |
1761 | | |
1762 | 27.1k | BitWriter w(c.get()); |
1763 | | |
1764 | 27.1k | writeHPageOffset(w); |
1765 | 27.1k | S = toI(c->getCount()); |
1766 | 27.1k | writeHSharedObject(w); |
1767 | 27.1k | O = 0; |
1768 | 27.1k | if (m->outline_hints.nobjects > 0) { |
1769 | 1.15k | O = toI(c->getCount()); |
1770 | 1.15k | writeHGeneric(w, m->outline_hints); |
1771 | 1.15k | } |
1772 | 27.1k | c->finish(); |
1773 | 27.1k | } |