/src/qpdf/libqpdf/QPDF_optimization.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // See the "Optimization" section of the manual. |
2 | | |
3 | | #include <qpdf/assert_debug.h> |
4 | | |
5 | | #include <qpdf/QPDF_private.hh> |
6 | | |
7 | | #include <qpdf/QPDFExc.hh> |
8 | | #include <qpdf/QPDFObjectHandle_private.hh> |
9 | | #include <qpdf/QPDFWriter_private.hh> |
10 | | #include <qpdf/QTC.hh> |
11 | | |
12 | | QPDF::ObjUser::ObjUser(user_e type) : |
13 | 0 | ou_type(type) |
14 | 0 | { |
15 | 0 | qpdf_assert_debug(type == ou_root); |
16 | 0 | } |
17 | | |
18 | | QPDF::ObjUser::ObjUser(user_e type, size_t pageno) : |
19 | 0 | ou_type(type), |
20 | 0 | pageno(pageno) |
21 | 0 | { |
22 | 0 | qpdf_assert_debug((type == ou_page) || (type == ou_thumb)); |
23 | 0 | } |
24 | | |
25 | | QPDF::ObjUser::ObjUser(user_e type, std::string const& key) : |
26 | 0 | ou_type(type), |
27 | 0 | key(key) |
28 | 0 | { |
29 | 0 | qpdf_assert_debug((type == ou_trailer_key) || (type == ou_root_key)); |
30 | 0 | } |
31 | | |
32 | | bool |
33 | | QPDF::ObjUser::operator<(ObjUser const& rhs) const |
34 | 0 | { |
35 | 0 | if (ou_type < rhs.ou_type) { |
36 | 0 | return true; |
37 | 0 | } |
38 | 0 | if (ou_type == rhs.ou_type) { |
39 | 0 | if (pageno < rhs.pageno) { |
40 | 0 | return true; |
41 | 0 | } |
42 | 0 | if (pageno == rhs.pageno) { |
43 | 0 | return key < rhs.key; |
44 | 0 | } |
45 | 0 | } |
46 | 0 | return false; |
47 | 0 | } |
48 | | |
49 | | QPDF::UpdateObjectMapsFrame::UpdateObjectMapsFrame( |
50 | | QPDF::ObjUser const& ou, QPDFObjectHandle oh, bool top) : |
51 | 0 | ou(ou), |
52 | 0 | oh(oh), |
53 | 0 | top(top) |
54 | 0 | { |
55 | 0 | } |
56 | | |
57 | | void |
58 | | QPDF::optimize( |
59 | | std::map<int, int> const& object_stream_data, |
60 | | bool allow_changes, |
61 | | std::function<int(QPDFObjectHandle&)> skip_stream_parameters) |
62 | 0 | { |
63 | 0 | optimize_internal(object_stream_data, allow_changes, skip_stream_parameters); |
64 | 0 | } |
65 | | |
66 | | void |
67 | | QPDF::optimize( |
68 | | QPDFWriter::ObjTable const& obj, std::function<int(QPDFObjectHandle&)> skip_stream_parameters) |
69 | 0 | { |
70 | 0 | optimize_internal(obj, true, skip_stream_parameters); |
71 | 0 | } |
72 | | |
73 | | template <typename T> |
74 | | void |
75 | | QPDF::optimize_internal( |
76 | | T const& object_stream_data, |
77 | | bool allow_changes, |
78 | | std::function<int(QPDFObjectHandle&)> skip_stream_parameters) |
79 | 0 | { |
80 | 0 | if (!m->obj_user_to_objects.empty()) { |
81 | | // already optimized |
82 | 0 | return; |
83 | 0 | } |
84 | | |
85 | | // The PDF specification indicates that /Outlines is supposed to be an indirect reference. Force |
86 | | // it to be so if it exists and is direct. (This has been seen in the wild.) |
87 | 0 | QPDFObjectHandle root = getRoot(); |
88 | 0 | if (root.getKey("/Outlines").isDictionary()) { |
89 | 0 | QPDFObjectHandle outlines = root.getKey("/Outlines"); |
90 | 0 | if (!outlines.isIndirect()) { |
91 | 0 | QTC::TC("qpdf", "QPDF_optimization indirect outlines"); |
92 | 0 | root.replaceKey("/Outlines", makeIndirectObject(outlines)); |
93 | 0 | } |
94 | 0 | } |
95 | | |
96 | | // Traverse pages tree pushing all inherited resources down to the page level. This also |
97 | | // initializes m->all_pages. |
98 | 0 | pushInheritedAttributesToPage(allow_changes, false); |
99 | | |
100 | | // Traverse pages |
101 | 0 | size_t n = m->all_pages.size(); |
102 | 0 | for (size_t pageno = 0; pageno < n; ++pageno) { |
103 | 0 | updateObjectMaps( |
104 | 0 | ObjUser(ObjUser::ou_page, pageno), m->all_pages.at(pageno), skip_stream_parameters); |
105 | 0 | } |
106 | | |
107 | | // Traverse document-level items |
108 | 0 | for (auto const& [key, value]: m->trailer.as_dictionary()) { |
109 | 0 | if (key == "/Root") { |
110 | | // handled separately |
111 | 0 | } else { |
112 | 0 | if (!value.null()) { |
113 | 0 | updateObjectMaps( |
114 | 0 | ObjUser(ObjUser::ou_trailer_key, key), value, skip_stream_parameters); |
115 | 0 | } |
116 | 0 | } |
117 | 0 | } |
118 | |
|
119 | 0 | for (auto const& [key, value]: root.as_dictionary()) { |
120 | | // Technically, /I keys from /Thread dictionaries are supposed to be handled separately, but |
121 | | // we are going to disregard that specification for now. There is loads of evidence that |
122 | | // pdlin and Acrobat both disregard things like this from time to time, so this is almost |
123 | | // certain not to cause any problems. |
124 | 0 | if (!value.null()) { |
125 | 0 | updateObjectMaps(ObjUser(ObjUser::ou_root_key, key), value, skip_stream_parameters); |
126 | 0 | } |
127 | 0 | } |
128 | |
|
129 | 0 | ObjUser root_ou = ObjUser(ObjUser::ou_root); |
130 | 0 | auto root_og = QPDFObjGen(root.getObjGen()); |
131 | 0 | m->obj_user_to_objects[root_ou].insert(root_og); |
132 | 0 | m->object_to_obj_users[root_og].insert(root_ou); |
133 | |
|
134 | 0 | filterCompressedObjects(object_stream_data); |
135 | 0 | } Unexecuted instantiation: void QPDF::optimize_internal<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&, bool, std::__1::function<int (QPDFObjectHandle&)>) Unexecuted instantiation: void QPDF::optimize_internal<QPDFWriter::ObjTable>(QPDFWriter::ObjTable const&, bool, std::__1::function<int (QPDFObjectHandle&)>) |
136 | | |
137 | | void |
138 | | QPDF::pushInheritedAttributesToPage() |
139 | 0 | { |
140 | | // Public API should not have access to allow_changes. |
141 | 0 | pushInheritedAttributesToPage(true, false); |
142 | 0 | } |
143 | | |
144 | | void |
145 | | QPDF::pushInheritedAttributesToPage(bool allow_changes, bool warn_skipped_keys) |
146 | 3.60k | { |
147 | | // Traverse pages tree pushing all inherited resources down to the page level. |
148 | | |
149 | | // The record of whether we've done this is cleared by updateAllPagesCache(). If we're warning |
150 | | // for skipped keys, re-traverse unconditionally. |
151 | 3.60k | if (m->pushed_inherited_attributes_to_pages && (!warn_skipped_keys)) { |
152 | 0 | return; |
153 | 0 | } |
154 | | |
155 | | // Calling getAllPages() resolves any duplicated page objects, repairs broken nodes, and detects |
156 | | // loops, so we don't have to do those activities here. |
157 | 3.60k | getAllPages(); |
158 | | |
159 | | // key_ancestors is a mapping of page attribute keys to a stack of Pages nodes that contain |
160 | | // values for them. |
161 | 3.60k | std::map<std::string, std::vector<QPDFObjectHandle>> key_ancestors; |
162 | 3.60k | pushInheritedAttributesToPageInternal( |
163 | 3.60k | m->trailer.getKey("/Root").getKey("/Pages"), |
164 | 3.60k | key_ancestors, |
165 | 3.60k | allow_changes, |
166 | 3.60k | warn_skipped_keys); |
167 | 3.60k | if (!key_ancestors.empty()) { |
168 | 0 | throw std::logic_error( |
169 | 0 | "key_ancestors not empty after pushing inherited attributes to pages"); |
170 | 0 | } |
171 | 3.60k | m->pushed_inherited_attributes_to_pages = true; |
172 | 3.60k | m->ever_pushed_inherited_attributes_to_pages = true; |
173 | 3.60k | } |
174 | | |
175 | | void |
176 | | QPDF::pushInheritedAttributesToPageInternal( |
177 | | QPDFObjectHandle cur_pages, |
178 | | std::map<std::string, std::vector<QPDFObjectHandle>>& key_ancestors, |
179 | | bool allow_changes, |
180 | | bool warn_skipped_keys) |
181 | 4.68k | { |
182 | | // Make a list of inheritable keys. Only the keys /MediaBox, /CropBox, /Resources, and /Rotate |
183 | | // are inheritable attributes. Push this object onto the stack of pages nodes that have values |
184 | | // for this attribute. |
185 | | |
186 | 4.68k | std::set<std::string> inheritable_keys; |
187 | 18.4k | for (auto const& key: cur_pages.getKeys()) { |
188 | 18.4k | if ((key == "/MediaBox") || (key == "/CropBox") || (key == "/Resources") || |
189 | 18.4k | (key == "/Rotate")) { |
190 | 1.21k | if (!allow_changes) { |
191 | 0 | throw QPDFExc( |
192 | 0 | qpdf_e_internal, |
193 | 0 | m->file->getName(), |
194 | 0 | m->last_object_description, |
195 | 0 | m->file->getLastOffset(), |
196 | 0 | "optimize detected an inheritable attribute when called in no-change mode"); |
197 | 0 | } |
198 | | |
199 | | // This is an inheritable resource |
200 | 1.21k | inheritable_keys.insert(key); |
201 | 1.21k | QPDFObjectHandle oh = cur_pages.getKey(key); |
202 | 1.21k | QTC::TC("qpdf", "QPDF opt direct pages resource", oh.isIndirect() ? 0 : 1); |
203 | 1.21k | if (!oh.isIndirect()) { |
204 | 1.19k | if (!oh.isScalar()) { |
205 | | // Replace shared direct object non-scalar resources with indirect objects to |
206 | | // avoid copying large structures around. |
207 | 669 | cur_pages.replaceKey(key, makeIndirectObject(oh)); |
208 | 669 | oh = cur_pages.getKey(key); |
209 | 669 | } else { |
210 | | // It's okay to copy scalars. |
211 | 521 | QTC::TC("qpdf", "QPDF opt inherited scalar"); |
212 | 521 | } |
213 | 1.19k | } |
214 | 1.21k | key_ancestors[key].push_back(oh); |
215 | 1.21k | if (key_ancestors[key].size() > 1) { |
216 | 223 | QTC::TC("qpdf", "QPDF opt key ancestors depth > 1"); |
217 | 223 | } |
218 | | // Remove this resource from this node. It will be reattached at the page level. |
219 | 1.21k | cur_pages.removeKey(key); |
220 | 17.2k | } else if (!((key == "/Type") || (key == "/Parent") || (key == "/Kids") || |
221 | 17.2k | (key == "/Count"))) { |
222 | | // Warn when flattening, but not if the key is at the top level (i.e. "/Parent" not |
223 | | // set), as we don't change these; but flattening removes intermediate /Pages nodes. |
224 | 4.62k | if ((warn_skipped_keys) && (cur_pages.hasKey("/Parent"))) { |
225 | 843 | QTC::TC("qpdf", "QPDF unknown key not inherited"); |
226 | 843 | setLastObjectDescription("Pages object", cur_pages.getObjGen()); |
227 | 843 | warn( |
228 | 843 | qpdf_e_pages, |
229 | 843 | m->last_object_description, |
230 | 843 | 0, |
231 | 843 | ("Unknown key " + key + |
232 | 843 | " in /Pages object is being discarded as a result of flattening the /Pages " |
233 | 843 | "tree")); |
234 | 843 | } |
235 | 4.62k | } |
236 | 18.4k | } |
237 | | |
238 | | // Process descendant nodes. This method does not perform loop detection because all code paths |
239 | | // that lead here follow a call to getAllPages, which already throws an exception in the event |
240 | | // of a loop in the pages tree. |
241 | 34.5k | for (auto& kid: cur_pages.getKey("/Kids").aitems()) { |
242 | 34.5k | if (kid.isDictionaryOfType("/Pages")) { |
243 | 1.08k | pushInheritedAttributesToPageInternal( |
244 | 1.08k | kid, key_ancestors, allow_changes, warn_skipped_keys); |
245 | 33.4k | } else { |
246 | | // Add all available inheritable attributes not present in this object to this object. |
247 | 33.4k | for (auto const& iter: key_ancestors) { |
248 | 3.94k | std::string const& key = iter.first; |
249 | 3.94k | if (!kid.hasKey(key)) { |
250 | 3.58k | QTC::TC("qpdf", "QPDF opt resource inherited"); |
251 | 3.58k | kid.replaceKey(key, iter.second.back()); |
252 | 3.58k | } else { |
253 | 359 | QTC::TC("qpdf", "QPDF opt page resource hides ancestor"); |
254 | 359 | } |
255 | 3.94k | } |
256 | 33.4k | } |
257 | 34.5k | } |
258 | | |
259 | | // For each inheritable key, pop the stack. If the stack becomes empty, remove it from the map. |
260 | | // That way, the invariant that the list of keys in key_ancestors is exactly those keys for |
261 | | // which inheritable attributes are available. |
262 | | |
263 | 4.68k | if (!inheritable_keys.empty()) { |
264 | 595 | QTC::TC("qpdf", "QPDF opt inheritable keys"); |
265 | 931 | for (auto const& key: inheritable_keys) { |
266 | 931 | key_ancestors[key].pop_back(); |
267 | 931 | if (key_ancestors[key].empty()) { |
268 | 731 | QTC::TC("qpdf", "QPDF opt erase empty key ancestor"); |
269 | 731 | key_ancestors.erase(key); |
270 | 731 | } |
271 | 931 | } |
272 | 4.09k | } else { |
273 | 4.09k | QTC::TC("qpdf", "QPDF opt no inheritable keys"); |
274 | 4.09k | } |
275 | 4.68k | } |
276 | | |
277 | | void |
278 | | QPDF::updateObjectMaps( |
279 | | ObjUser const& first_ou, |
280 | | QPDFObjectHandle first_oh, |
281 | | std::function<int(QPDFObjectHandle&)> skip_stream_parameters) |
282 | 0 | { |
283 | 0 | QPDFObjGen::set visited; |
284 | 0 | std::vector<UpdateObjectMapsFrame> pending; |
285 | 0 | pending.emplace_back(first_ou, first_oh, true); |
286 | | // Traverse the object tree from this point taking care to avoid crossing page boundaries. |
287 | 0 | std::unique_ptr<ObjUser> thumb_ou; |
288 | 0 | while (!pending.empty()) { |
289 | 0 | auto cur = pending.back(); |
290 | 0 | pending.pop_back(); |
291 | |
|
292 | 0 | bool is_page_node = false; |
293 | |
|
294 | 0 | if (cur.oh.isDictionaryOfType("/Page")) { |
295 | 0 | is_page_node = true; |
296 | 0 | if (!cur.top) { |
297 | 0 | continue; |
298 | 0 | } |
299 | 0 | } |
300 | | |
301 | 0 | if (cur.oh.isIndirect()) { |
302 | 0 | QPDFObjGen og(cur.oh.getObjGen()); |
303 | 0 | if (!visited.add(og)) { |
304 | 0 | QTC::TC("qpdf", "QPDF opt loop detected"); |
305 | 0 | continue; |
306 | 0 | } |
307 | 0 | m->obj_user_to_objects[cur.ou].insert(og); |
308 | 0 | m->object_to_obj_users[og].insert(cur.ou); |
309 | 0 | } |
310 | | |
311 | 0 | if (cur.oh.isArray()) { |
312 | 0 | for (auto const& item: cur.oh.as_array()) { |
313 | 0 | pending.emplace_back(cur.ou, item, false); |
314 | 0 | } |
315 | 0 | } else if (cur.oh.isDictionary() || cur.oh.isStream()) { |
316 | 0 | QPDFObjectHandle dict = cur.oh; |
317 | 0 | bool is_stream = cur.oh.isStream(); |
318 | 0 | int ssp = 0; |
319 | 0 | if (is_stream) { |
320 | 0 | dict = cur.oh.getDict(); |
321 | 0 | if (skip_stream_parameters) { |
322 | 0 | ssp = skip_stream_parameters(cur.oh); |
323 | 0 | } |
324 | 0 | } |
325 | |
|
326 | 0 | for (auto& [key, value]: dict.as_dictionary()) { |
327 | 0 | if (value.null()) { |
328 | 0 | continue; |
329 | 0 | } |
330 | | |
331 | 0 | if (is_page_node && (key == "/Thumb")) { |
332 | | // Traverse page thumbnail dictionaries as a special case. There can only ever |
333 | | // be one /Thumb key on a page, and we see at most one page node per call. |
334 | 0 | thumb_ou = std::make_unique<ObjUser>(ObjUser::ou_thumb, cur.ou.pageno); |
335 | 0 | pending.emplace_back(*thumb_ou, dict.getKey(key), false); |
336 | 0 | } else if (is_page_node && (key == "/Parent")) { |
337 | | // Don't traverse back up the page tree |
338 | 0 | } else if ( |
339 | 0 | ((ssp >= 1) && (key == "/Length")) || |
340 | 0 | ((ssp >= 2) && ((key == "/Filter") || (key == "/DecodeParms")))) { |
341 | | // Don't traverse into stream parameters that we are not going to write. |
342 | 0 | } else { |
343 | 0 | pending.emplace_back(cur.ou, value, false); |
344 | 0 | } |
345 | 0 | } |
346 | 0 | } |
347 | 0 | } |
348 | 0 | } |
349 | | |
350 | | void |
351 | | QPDF::filterCompressedObjects(std::map<int, int> const& object_stream_data) |
352 | 0 | { |
353 | 0 | if (object_stream_data.empty()) { |
354 | 0 | return; |
355 | 0 | } |
356 | | |
357 | | // Transform object_to_obj_users and obj_user_to_objects so that they refer only to uncompressed |
358 | | // objects. If something is a user of a compressed object, then it is really a user of the |
359 | | // object stream that contains it. |
360 | | |
361 | 0 | std::map<ObjUser, std::set<QPDFObjGen>> t_obj_user_to_objects; |
362 | 0 | std::map<QPDFObjGen, std::set<ObjUser>> t_object_to_obj_users; |
363 | |
|
364 | 0 | for (auto const& i1: m->obj_user_to_objects) { |
365 | 0 | ObjUser const& ou = i1.first; |
366 | | // Loop over objects. |
367 | 0 | for (auto const& og: i1.second) { |
368 | 0 | auto i2 = object_stream_data.find(og.getObj()); |
369 | 0 | if (i2 == object_stream_data.end()) { |
370 | 0 | t_obj_user_to_objects[ou].insert(og); |
371 | 0 | } else { |
372 | 0 | t_obj_user_to_objects[ou].insert(QPDFObjGen(i2->second, 0)); |
373 | 0 | } |
374 | 0 | } |
375 | 0 | } |
376 | |
|
377 | 0 | for (auto const& i1: m->object_to_obj_users) { |
378 | 0 | QPDFObjGen const& og = i1.first; |
379 | | // Loop over obj_users. |
380 | 0 | for (auto const& ou: i1.second) { |
381 | 0 | auto i2 = object_stream_data.find(og.getObj()); |
382 | 0 | if (i2 == object_stream_data.end()) { |
383 | 0 | t_object_to_obj_users[og].insert(ou); |
384 | 0 | } else { |
385 | 0 | t_object_to_obj_users[QPDFObjGen(i2->second, 0)].insert(ou); |
386 | 0 | } |
387 | 0 | } |
388 | 0 | } |
389 | |
|
390 | 0 | m->obj_user_to_objects = t_obj_user_to_objects; |
391 | 0 | m->object_to_obj_users = t_object_to_obj_users; |
392 | 0 | } |
393 | | |
394 | | void |
395 | | QPDF::filterCompressedObjects(QPDFWriter::ObjTable const& obj) |
396 | 0 | { |
397 | 0 | if (obj.getStreamsEmpty()) { |
398 | 0 | return; |
399 | 0 | } |
400 | | |
401 | | // Transform object_to_obj_users and obj_user_to_objects so that they refer only to uncompressed |
402 | | // objects. If something is a user of a compressed object, then it is really a user of the |
403 | | // object stream that contains it. |
404 | | |
405 | 0 | std::map<ObjUser, std::set<QPDFObjGen>> t_obj_user_to_objects; |
406 | 0 | std::map<QPDFObjGen, std::set<ObjUser>> t_object_to_obj_users; |
407 | |
|
408 | 0 | for (auto const& i1: m->obj_user_to_objects) { |
409 | 0 | ObjUser const& ou = i1.first; |
410 | | // Loop over objects. |
411 | 0 | for (auto const& og: i1.second) { |
412 | 0 | if (obj.contains(og)) { |
413 | 0 | if (auto const& i2 = obj[og].object_stream; i2 <= 0) { |
414 | 0 | t_obj_user_to_objects[ou].insert(og); |
415 | 0 | } else { |
416 | 0 | t_obj_user_to_objects[ou].insert(QPDFObjGen(i2, 0)); |
417 | 0 | } |
418 | 0 | } |
419 | 0 | } |
420 | 0 | } |
421 | |
|
422 | 0 | for (auto const& i1: m->object_to_obj_users) { |
423 | 0 | QPDFObjGen const& og = i1.first; |
424 | 0 | if (obj.contains(og)) { |
425 | | // Loop over obj_users. |
426 | 0 | for (auto const& ou: i1.second) { |
427 | 0 | if (auto i2 = obj[og].object_stream; i2 <= 0) { |
428 | 0 | t_object_to_obj_users[og].insert(ou); |
429 | 0 | } else { |
430 | 0 | t_object_to_obj_users[QPDFObjGen(i2, 0)].insert(ou); |
431 | 0 | } |
432 | 0 | } |
433 | 0 | } |
434 | 0 | } |
435 | |
|
436 | 0 | m->obj_user_to_objects = t_obj_user_to_objects; |
437 | 0 | m->object_to_obj_users = t_object_to_obj_users; |
438 | 0 | } |