Debian Codegraph @

pdf/pdf_doc.c

raw
1 /* Copyright (C) 2020-2026 Artifex Software, Inc.
2 All Rights Reserved.
3
4 This software is provided AS-IS with no warranty, either express or
5 implied.
6
7 This software is distributed under license and may not be copied,
8 modified or distributed except as expressly authorized under the terms
9 of the license contained in the file LICENSE in this distribution.
10
11 Refer to licensing information at http://www.artifex.com or contact
12 Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
13 CA 94129, USA, for further information.
14 */
15
16 /* Functions to deal with PDF structure, such as retrieving
17 * the Info, Catalog, Root dictionaries, and finding resources
18 * and page dictionaries.
19 */
20
21 #include "ghostpdf.h"
22 #include "pdf_stack.h"
23 #include "pdf_deref.h"
24 #include "pdf_array.h"
25 #include "pdf_dict.h"
26 #include "pdf_loop_detect.h"
27 #include "pdf_misc.h"
28 #include "pdf_repair.h"
29 #include "pdf_doc.h"
30 #include "pdf_mark.h"
31 #include "pdf_colour.h"
32 #include "pdf_device.h"
33
34 int pdfi_read_Root(pdf_context *ctx)
35 {
36 pdf_obj *o, *o1;
37 pdf_dict *d;
38 int code;
39
40 if (ctx->args.pdfdebug)
41 outprintf(ctx->memory, "%% Reading Root dictionary\n");
42
43 /* Unusual code. This is because if the entry in the trailer dictionary causes
44 * us to repair the file, the Trailer dictionary in the context can be replaced.
45 * This counts it down and frees it, potentially while pdfi_dict_get is still
46 * using it! Rather than countup and down in the dict_get routine, which is
47 * normally unnecessary, count it up and down round the access here.
48 */
49 d = ctx->Trailer;
50 pdfi_countup(d);
51 code = pdfi_dict_get(ctx, d, "Root", &o1);
52 if (code < 0) {
53 pdfi_countdown(d);
54 return code;
55 }
56 pdfi_countdown(d);
57
58 if (pdfi_type_of(o1) == PDF_INDIRECT) {
59 code = pdfi_dereference(ctx, ((pdf_indirect_ref *)o1)->ref_object_num, ((pdf_indirect_ref *)o1)->ref_generation_num, &o);
60 pdfi_countdown(o1);
61 if (code < 0)
62 return code;
63
64 if (pdfi_type_of(o) != PDF_DICT) {
65 pdfi_countdown(o);
66 return_error(gs_error_typecheck);
67 }
68
69 code = pdfi_dict_put(ctx, ctx->Trailer, "Root", o);
70 if (code < 0) {
71 pdfi_countdown(o);
72 return code;
73 }
74 o1 = o;
75 } else {
76 if (pdfi_type_of(o1) != PDF_DICT) {
77 pdfi_countdown(o1);
78 if (ctx->Root == NULL)
79 return_error(gs_error_typecheck);
80 return 0;
81 }
82 }
83
84 code = pdfi_dict_get_type(ctx, (pdf_dict *)o1, "Type", PDF_NAME, &o);
85 if (code < 0) {
86 bool known = false;
87
88 if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_MISSINGTYPE, "pdfi_read_Root", NULL)) < 0) {
89 pdfi_countdown(o1);
90 return code;
91 }
92
93 /* Missing the *required* /Type key! See if it has /Pages at least, if it does carry on */
94 code = pdfi_dict_known(ctx, (pdf_dict *)o1, "Pages", &known);
95 if (code < 0 || known == false) {
96 pdfi_countdown(o1);
97 return code;
98 }
99 }
100 else {
101 if (pdfi_name_strcmp((pdf_name *)o, "Catalog") != 0){
102 pdf_obj *pages = NULL;
103
104 /* Bug #706038 has a Root dictionary with /Type /Calalog which (of course) Acrobat
105 * happily opens :-( So if we find a /Type and it's not /Catalog, try and see if
106 * we have a /Pages key (the only other required entry). If we do assume that the
107 * Type is wrong and use this dictionary, otherwise fall back to seeing if we
108 * have a repaired Root. See above for handling a missing /Type.....
109 */
110 code = pdfi_dict_get_type(ctx, (pdf_dict *)o1, "Pages", PDF_DICT, &pages);
111 if (code < 0) {
112 pdfi_countdown(o);
113 pdfi_countdown(o1);
114 /* If we repaired the file, we may already have spotted a potential Root dictionary
115 * so if the one we found here isn't valid, try the one we found when scanning
116 */
117 if (ctx->Root == NULL) {
118 pdfi_set_error(ctx, 0, NULL, E_PDF_NO_ROOT, "pdfi_read_Root", NULL);
119 return_error(gs_error_syntaxerror);
120 }
121 return 0;
122 }
123 pdfi_countdown(pages);
124 if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_typecheck), NULL, E_PDF_BAD_ROOT_TYPE, "pdfi_read_Root", NULL)) < 0) {
125 pdfi_countdown(o);
126 pdfi_countdown(o1);
127 return code;
128 }
129 }
130 pdfi_countdown(o);
131 }
132
133 if (ctx->args.pdfdebug)
134 outprintf(ctx->memory, "\n");
135 /* We don't pdfi_countdown(o1) now, because we've transferred our
136 * reference to the pointer in the pdf_context structure.
137 */
138 pdfi_countdown(ctx->Root); /* If file was repaired it might be set already */
139 ctx->Root = (pdf_dict *)o1;
140 return 0;
141 }
142
143 static int Info_check_dict(pdf_context *ctx, pdf_dict *d);
144
145 static int Info_check_array(pdf_context *ctx, pdf_array *a)
146 {
147 int code = 0, i = 0;
148 pdf_obj *array_obj = NULL;
149
150 code = pdfi_loop_detector_mark(ctx);
151 if (code < 0)
152 return code;
153
154 for (i = 0;i < pdfi_array_size(a); i++) {
155 code = pdfi_array_fetch_recursing(ctx, a, i, &array_obj, true, true);
156 if (code < 0)
157 goto error;
158
159 switch(pdfi_type_of(array_obj)) {
160 case PDF_DICT:
161 if (array_obj->object_num != 0) {
162 code = pdfi_loop_detector_add_object(ctx, array_obj->object_num);
163 if (code < 0)
164 goto error;
165 }
166 code = Info_check_dict(ctx, (pdf_dict *)array_obj);
167 if (code < 0) {
168 if (array_obj->object_num != 0) {
169 pdf_indirect_ref *i_o;
170
171 code = pdfi_object_alloc(ctx, PDF_INDIRECT, 0, (pdf_obj **)&i_o);
172 if (code < 0)
173 goto error;
174 i_o->ref_generation_num = array_obj->generation_num;
175 i_o->ref_object_num = array_obj->object_num;
176 i_o->indirect_num = array_obj->object_num;
177 i_o->indirect_gen = array_obj->generation_num;
178 code = pdfi_array_put(ctx, a, i, (pdf_obj *)i_o);
179 if (code < 0)
180 return code;
181 }
182 goto error;
183 }
184 break;
185 case PDF_ARRAY:
186 if (array_obj->object_num != 0) {
187 code = pdfi_loop_detector_add_object(ctx, array_obj->object_num);
188 if (code < 0)
189 goto error;
190 }
191 code = Info_check_array(ctx, (pdf_array *)array_obj);
192 if (code < 0) {
193 if (array_obj->object_num != 0) {
194 pdf_indirect_ref *i_o;
195
196 code = pdfi_object_alloc(ctx, PDF_INDIRECT, 0, (pdf_obj **)&i_o);
197 if (code < 0)
198 goto error;
199 i_o->ref_generation_num = array_obj->generation_num;
200 i_o->ref_object_num = array_obj->object_num;
201 i_o->indirect_num = array_obj->object_num;
202 i_o->indirect_gen = array_obj->generation_num;
203 code = pdfi_array_put(ctx, a, i, (pdf_obj *)i_o);
204 if (code < 0)
205 return code;
206 }
207 goto error;
208 }
209 break;
210 default:
211 break;
212 }
213
214 pdfi_countdown(array_obj);
215 array_obj = NULL;
216 }
217 error:
218 pdfi_countdown(array_obj);
219 pdfi_loop_detector_cleartomark(ctx);
220 return code;
221 }
222
223 static int Info_check_dict(pdf_context *ctx, pdf_dict *d)
224 {
225 int code = 0;
226 uint64_t index = 0;
227 pdf_name *Key = NULL;
228 pdf_obj *Value = NULL;
229
230 code = pdfi_loop_detector_mark(ctx);
231 if (code < 0)
232 return code;
233
234 code = pdfi_dict_first(ctx, d, (pdf_obj **)&Key, &Value, &index);
235 if (code < 0) {
236 if (code == gs_error_undefined)
237 code = 0;
238 goto error;
239 }
240
241 while (code >= 0) {
242 switch(pdfi_type_of(Value)) {
243 case PDF_DICT:
244 if (Value->object_num != 0) {
245 code = pdfi_loop_detector_add_object(ctx, Value->object_num);
246 if (code < 0)
247 goto error;
248 }
249 code = Info_check_dict(ctx, (pdf_dict *)Value);
250 if (code < 0)
251 goto error;
252 break;
253 case PDF_ARRAY:
254 if (Value->object_num != 0) {
255 code = pdfi_loop_detector_add_object(ctx, Value->object_num);
256 if (code < 0)
257 goto error;
258 }
259 code = Info_check_array(ctx, (pdf_array *)Value);
260 if (code < 0)
261 goto error;
262 break;
263 default:
264 break;
265 }
266 pdfi_countdown(Key);
267 Key = NULL;
268 pdfi_countdown(Value);
269 Value = NULL;
270
271 code = pdfi_dict_next(ctx, d, (pdf_obj **)&Key, &Value, &index);
272 if (code == gs_error_undefined) {
273 code = 0;
274 break;
275 }
276 }
277 error:
278 pdfi_countdown(Key);
279 pdfi_countdown(Value);
280 pdfi_loop_detector_cleartomark(ctx);
281 return code;
282 }
283
284 static int pdfi_sanitize_Info_references(pdf_context *ctx, pdf_dict *Info)
285 {
286 int code = 0;
287 uint64_t index = 0;
288 pdf_name *Key = NULL;
289 pdf_obj *Value = NULL;
290
291 restart_scan:
292 code = pdfi_loop_detector_mark(ctx);
293 if (code < 0)
294 return code;
295
296 code = pdfi_dict_first(ctx, Info, (pdf_obj **)&Key, &Value, &index);
297 if (code == gs_error_undefined) {
298 code = 0;
299 goto error;
300 }
301
302 while (code >= 0) {
303 switch(pdfi_type_of(Value)) {
304 case PDF_DICT:
305 code = Info_check_dict(ctx, (pdf_dict *)Value);
306 break;
307 case PDF_ARRAY:
308 code = Info_check_array(ctx, (pdf_array *)Value);
309 break;
310 default:
311 code = 0;
312 break;
313 }
314 pdfi_countdown(Value);
315 Value = NULL;
316 if (code < 0) {
317 code = pdfi_dict_delete_pair(ctx, Info, Key);
318 if (code < 0)
319 goto error;
320 pdfi_countdown(Key);
321 Key = NULL;
322
323 pdfi_loop_detector_cleartomark(ctx);
324 goto restart_scan;
325 }
326 pdfi_countdown(Key);
327 Key = NULL;
328
329 pdfi_loop_detector_cleartomark(ctx);
330 code = pdfi_loop_detector_mark(ctx);
331 if (code < 0) {
332 pdfi_countdown(Key);
333 pdfi_countdown(Value);
334 return code;
335 }
336
337 code = pdfi_dict_next(ctx, Info, (pdf_obj **)&Key, &Value, &index);
338 if (code == gs_error_undefined) {
339 code = 0;
340 break;
341 }
342 }
343 error:
344 pdfi_countdown(Key);
345 pdfi_countdown(Value);
346 pdfi_loop_detector_cleartomark(ctx);
347 return code;
348 }
349
350 int pdfi_read_Info(pdf_context *ctx)
351 {
352 pdf_dict *Info;
353 int code;
354 pdf_dict *d;
355
356 if (ctx->args.pdfdebug)
357 outprintf(ctx->memory, "%% Reading Info dictionary\n");
358
359 /* See comment in pdfi_read_Root() for details */
360 d = ctx->Trailer;
361 pdfi_countup(d);
362 code = pdfi_dict_get_type(ctx, ctx->Trailer, "Info", PDF_DICT, (pdf_obj **)&Info);
363 pdfi_countdown(d);
364 if (code < 0)
365 return code;
366
367 if (ctx->args.pdfdebug)
368 outprintf(ctx->memory, "\n");
369
370 code = pdfi_loop_detector_mark(ctx);
371 if (code < 0)
372 goto error;
373 if (Info->object_num != 0) {
374 code = pdfi_loop_detector_add_object(ctx, Info->object_num);
375 if (code < 0)
376 goto error1;
377 } else {
378 if ((code = pdfi_set_warning_stop(ctx, 0, NULL, W_PDF_INFO_NOT_INDIRECT, "pdfi_read_Info", "")) < 0)
379 goto error1;
380 }
381
382 /* sanitize Info for circular references */
383 code = pdfi_sanitize_Info_references(ctx, Info);
384 if (code < 0)
385 goto error1;
386
387 (void)pdfi_loop_detector_cleartomark(ctx);
388
389 pdfi_pdfmark_write_docinfo(ctx, Info);
390
391 /* We don't pdfi_countdown(Info) now, because we've transferred our
392 * reference to the pointer in the pdf_context structure.
393 */
394 ctx->Info = Info;
395 return 0;
396
397 error1:
398 pdfi_loop_detector_cleartomark(ctx);
399 error:
400 pdfi_countdown(Info);
401 return code;
402 }
403
404 int pdfi_read_Pages(pdf_context *ctx)
405 {
406 pdf_obj *o, *o1;
407 pdf_array *a = NULL;
408 int code, pagecount = 0;
409 double d;
410
411 if (ctx->args.pdfdebug)
412 outprintf(ctx->memory, "%% Reading Pages dictionary\n");
413
414 code = pdfi_dict_get(ctx, ctx->Root, "Pages", &o1);
415 if (code < 0)
416 return code;
417
418 if (pdfi_type_of(o1) == PDF_INDIRECT) {
419 code = pdfi_dereference(ctx, ((pdf_indirect_ref *)o1)->ref_object_num, ((pdf_indirect_ref *)o1)->ref_generation_num, &o);
420 pdfi_countdown(o1);
421 if (code < 0)
422 return code;
423
424 if (pdfi_type_of(o) != PDF_DICT) {
425 pdfi_countdown(o);
426 if (pdfi_type_of(o) == PDF_INDIRECT)
427 pdfi_set_error(ctx, 0, NULL, E_PDF_BADPAGEDICT, "pdfi_read_Pages", (char *)"*** Error: Something is wrong with the Pages dictionary. Giving up.");
428 else
429 pdfi_set_error(ctx, 0, NULL, E_PDF_BADPAGEDICT, "pdfi_read_Pages", (char *)"*** Error: Something is wrong with the Pages dictionary. Giving up.\n Double indirect reference. Loop in Pages tree?");
430 return_error(gs_error_typecheck);
431 }
432
433 code = pdfi_dict_put(ctx, ctx->Root, "Pages", o);
434 if (code < 0) {
435 pdfi_countdown(o);
436 return code;
437 }
438 o1 = o;
439 } else {
440 if (pdfi_type_of(o1) != PDF_DICT) {
441 pdfi_countdown(o1);
442 return_error(gs_error_typecheck);
443 }
444 }
445
446 if (ctx->args.pdfdebug)
447 outprintf(ctx->memory, "\n");
448
449 /* Acrobat allows the Pages Count to be a floating point number (!) */
450 /* sample w_a.PDF from Bug688419 (not on the cluster, maybe it should be?) has no /Count entry because
451 * The Root dictionary Pages key points directly to a single dictionary of type /Page. This is plainly
452 * illegal but Acrobat can deal with it. We do so by ignoring the error her, and adding logic in
453 * pdfi_get_page_dict() which notes that ctx->PagesTree is NULL and tries to get the single Page
454 * dictionary from the Root instead of using the PagesTree.
455 */
456 code = pdfi_dict_get_number(ctx, (pdf_dict *)o1, "Count", &d);
457 if (code < 0) {
458 if (code == gs_error_undefined) {
459 pdf_name *n = NULL;
460 /* It may be that the Root dictionary Pages entry points directly to a sinlge Page dictionary
461 * See if the dictionary has a Type of /Page, if so don't throw an error and the pdf_page.c
462 * logic in pdfi_get_page_dict() logic will take care of it.
463 */
464 code = pdfi_dict_get_type(ctx, (pdf_dict *)o1, "Type", PDF_NAME, (pdf_obj **)&n);
465 if (code == 0) {
466 if(pdfi_name_is(n, "Page")) {
467 ctx->num_pages = 1;
468 code = 0;
469 }
470 else
471 code = gs_error_undefined;
472 pdfi_countdown(n);
473 }
474 }
475 pdfi_countdown(o1);
476 return code;
477 }
478
479 if (floor(d) != d) {
480 pdfi_countdown(o1);
481 return_error(gs_error_rangecheck);
482 } else {
483 ctx->num_pages = (int)floor(d);
484 }
485
486 /* A simple confidence check in the value of Count. We only do this because
487 * the OSS-fuzz tool keeps on coming up with files that time out because the
488 * initial Count is insanely huge, and we spend much time trying to find
489 * millions of pages which don't exist.
490 */
491 code = pdfi_dict_knownget_type(ctx, (pdf_dict *)o1, "Kids", PDF_ARRAY, (pdf_obj **)&a);
492 if (code == 0)
493 code = gs_note_error(gs_error_undefined);
494 if (code < 0) {
495 pdfi_countdown(o1);
496 return code;
497 }
498
499 /* Firstly check if the Kids array has enough nodes, in which case it's
500 * probably flat (the common case)
501 */
502 if (a->size != ctx->num_pages) {
503 int i = 0;
504 pdf_obj *p = NULL, *p1 = NULL;
505 pdf_num *c = NULL;
506
507 /* Either its not a flat tree, or the top node /Count is incorrect.
508 * Get each entry in the Kids array in turn and total the /Count of
509 * each node and add any leaf nodes.
510 */
511 for (i=0;i < a->size; i++) {
512 /* We must not allow the entry in the array to be replaced, in case it's a circular reference */
513 code = pdfi_array_get_no_store_R(ctx, a, i, &p);
514 if (code < 0)
515 continue;
516 if (pdfi_type_of(p) != PDF_DICT) {
517 pdfi_countdown(p);
518 p = NULL;
519 continue;
520 }
521 /* Explicit check that the root node Kids array entry is not a self-reference
522 * back to the root node. We only check one level of the Kids array. so we don't
523 * need a full loop detection setup here.
524 */
525 if (p->object_num != 0 && p->object_num == o1->object_num) {
526 pdfi_countdown(p);
527 p = NULL;
528 pdfi_countdown(a);
529 pdfi_countdown(o1);
530 ctx->num_pages = 0;
531 return_error(gs_error_circular_reference);
532 }
533 /* Now we've checked for circular reference we can replace the entry in the
534 * array, and add the object to the cache.
535 * These are optimisations, so we don't especially care whether they succeed
536 */
537 (void)pdfi_array_put(ctx, a, i, p);
538 (void)replace_cache_entry(ctx, p);
539 code = pdfi_dict_knownget_type(ctx, (pdf_dict *)p, "Type", PDF_NAME, (pdf_obj **)&p1);
540 if (code <= 0) {
541 pdfi_countdown(p);
542 p = NULL;
543 continue;
544 }
545 if (pdfi_name_is((pdf_name *)p1, "Page")) {
546 pagecount++;
547 } else {
548 if (pdfi_name_is((pdf_name *)p1, "Pages")) {
549 code = pdfi_dict_knownget(ctx, (pdf_dict *)p, "Count", (pdf_obj **)&c);
550 if (code >= 0) {
551 if (pdfi_type_of(c) == PDF_INT)
552 pagecount += c->value.i;
553 if (pdfi_type_of(c) == PDF_REAL)
554 pagecount += (int)c->value.d;
555 pdfi_countdown(c);
556 c = NULL;
557 }
558 }
559 }
560 pdfi_countdown(p1);
561 p1 = NULL;
562 pdfi_countdown(p);
563 p = NULL;
564 }
565 } else
566 pagecount = a->size;
567
568 pdfi_countdown(a);
569
570 /* If the count of the top level of the tree doesn't match the /Count
571 * of the root node then something is wrong. We could abort right now
572 * and will if this continues to be a problem, but initially let's assume
573 * the count of the top level is correct and the root node /Count is wrong.
574 * This will allow us to recover if only the root /Count gets corrupted.
575 * In future we could also try validating the entire tree at this point,
576 * though I suspect that's pointless; if the tree is corrupted we aren't
577 * likely to get much that's usable from it.
578 */
579 if (pagecount != ctx->num_pages) {
580 ctx->num_pages = pagecount;
581 if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, E_PDF_BADPAGECOUNT, "pdfi_read_Pages", NULL)) < 0)
582 return code;
583 code = pdfi_dict_put_int(ctx, (pdf_dict *)o1, "Count", ctx->num_pages);
584 if (code < 0) {
585 pdfi_countdown(o1);
586 return code;
587 }
588 }
589
590 /* We don't pdfi_countdown(o1) now, because we've transferred our
591 * reference to the pointer in the pdf_context structure.
592 */
593 ctx->PagesTree = (pdf_dict *)o1;
594 return 0;
595 }
596
597 /* Read optional things in from Root */
598 void pdfi_read_OptionalRoot(pdf_context *ctx)
599 {
600 pdf_obj *obj = NULL;
601 int code;
602 bool known;
603
604 if (ctx->args.pdfdebug)
605 outprintf(ctx->memory, "%% Reading other Root contents\n");
606
607 if (ctx->args.pdfdebug)
608 outprintf(ctx->memory, "%% OCProperties\n");
609 code = pdfi_dict_get_type(ctx, ctx->Root, "OCProperties", PDF_DICT, &obj);
610 if (code == 0) {
611 ctx->OCProperties = (pdf_dict *)obj;
612 } else {
613 ctx->OCProperties = NULL;
614 if (ctx->args.pdfdebug)
615 outprintf(ctx->memory, "%% (None)\n");
616 }
617
618 (void)pdfi_dict_known(ctx, ctx->Root, "Collection", &known);
619
620 if (known) {
621 if (ctx->args.pdfdebug)
622 outprintf(ctx->memory, "%% Collection\n");
623 code = pdfi_dict_get(ctx, ctx->Root, "Collection", (pdf_obj **)&ctx->Collection);
624 if (code < 0) {
625 (void)pdfi_set_warning_stop(ctx, 0, NULL, W_PDF_BAD_COLLECTION, "pdfi_read_OptionalRoot", "");
626 }
627 }
628 }
629
630 void pdfi_free_OptionalRoot(pdf_context *ctx)
631 {
632 if (ctx->OCProperties) {
633 pdfi_countdown(ctx->OCProperties);
634 ctx->OCProperties = NULL;
635 }
636 if (ctx->Collection) {
637 pdfi_countdown(ctx->Collection);
638 ctx->Collection = NULL;
639 }
640 }
641
642 /* Handle child node processing for page_dict */
643 static int pdfi_get_child(pdf_context *ctx, pdf_array *Kids, int i, pdf_dict **pchild)
644 {
645 pdf_indirect_ref *node = NULL;
646 pdf_dict *child = NULL;
647 pdf_name *Type = NULL;
648 pdf_dict *leaf_dict = NULL;
649 pdf_name *Key = NULL;
650 int code = 0;
651
652 code = pdfi_array_get_no_deref(ctx, Kids, i, (pdf_obj **)&node);
653 if (code < 0)
654 goto errorExit;
655
656 if (pdfi_type_of(node) != PDF_INDIRECT && pdfi_type_of(node) != PDF_DICT) {
657 code = gs_note_error(gs_error_typecheck);
658 goto errorExit;
659 }
660
661 if (pdfi_type_of(node) == PDF_INDIRECT) {
662 code = pdfi_dereference(ctx, node->ref_object_num, node->ref_generation_num,
663 (pdf_obj **)&child);
664 if (code < 0) {
665 int code1 = pdfi_repair_file(ctx);
666 if (code1 < 0)
667 goto errorExit;
668 code = pdfi_dereference(ctx, node->ref_object_num,
669 node->ref_generation_num, (pdf_obj **)&child);
670 if (code < 0)
671 goto errorExit;
672 }
673 /* It makes no sense for the Page dictionary to be a stream, but safedocs/DialectDictIsStream.pdf
674 * has this (stream length is 0, not that it matters) and Acrobat happily opens it....
675 */
676 if (pdfi_type_of(child) != PDF_DICT) {
677 pdf_dict *d1 = NULL;
678
679 if (pdfi_type_of(child) != PDF_STREAM) {
680 code = gs_note_error(gs_error_typecheck);
681 goto errorExit;
682 }
683 if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_typecheck), NULL, E_PDF_DICT_IS_STREAM, "pdfi_get_child", NULL)) < 0)
684 goto errorExit;
685
686 code = pdfi_get_stream_dict(ctx, (pdf_stream *)child, &d1);
687 if (code < 0)
688 goto errorExit;
689 pdfi_countdown(child);
690 child = d1;
691 code = replace_cache_entry(ctx, (pdf_obj *)d1);
692 if (code < 0)
693 goto errorExit;
694 }
695 /* If its an intermediate node, store it in the page_table, if its a leaf node
696 * then don't store it. Instead we create a special dictionary of our own which
697 * has a /Type of /PageRef and a /PageRef key which is the indirect reference
698 * to the page. However in this case we pass on the actual page dictionary to
699 * the Kids processing below. If we didn't then we'd fall foul of the loop
700 * detection by dereferencing the same object twice.
701 * This is tedious, but it means we don't store all the page dictionaries in
702 * the Pages tree, because page dictionaries can be large and we generally
703 * only use them once. If processed in order we only dereference each page
704 * dictionary once, any other order will dereference each page twice. (or more
705 * if we render the same page multiple times).
706 */
707 code = pdfi_dict_get_type(ctx, child, "Type", PDF_NAME, (pdf_obj **)&Type);
708 if (code < 0)
709 goto errorExit;
710 if (pdfi_name_is(Type, "Pages")) {
711 code = pdfi_array_put(ctx, Kids, i, (pdf_obj *)child);
712 if (code < 0)
713 goto errorExit;
714 } else {
715 /* Bizarrely, one of the QL FTS files (FTS_07_0704.pdf) has a page diciotnary with a /Type of /Template */
716 if (!pdfi_name_is(Type, "Page"))
717 if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_typecheck), NULL, E_PDF_BADPAGETYPE, "pdfi_get_child", NULL)) < 0)
718 goto errorExit;
719 /* Make a 'PageRef' entry (just stores an indirect reference to the actual page)
720 * and store that in the Kids array for future reference. But pass on the
721 * dereferenced Page dictionary, in case this is the target page.
722 */
723
724 code = pdfi_dict_alloc(ctx, 0, &leaf_dict);
725 if (code < 0)
726 goto errorExit;
727 code = pdfi_name_alloc(ctx, (byte *)"PageRef", 7, (pdf_obj **)&Key);
728 if (code < 0)
729 goto errorExit;
730 pdfi_countup(Key);
731
732 code = pdfi_dict_put_obj(ctx, leaf_dict, (pdf_obj *)Key, (pdf_obj *)node, true);
733 if (code < 0)
734 goto errorExit;
735 code = pdfi_dict_put(ctx, leaf_dict, "Type", (pdf_obj *)Key);
736 if (code < 0)
737 goto errorExit;
738 code = pdfi_array_put(ctx, Kids, i, (pdf_obj *)leaf_dict);
739 if (code < 0)
740 goto errorExit;
741 leaf_dict = NULL;
742 }
743 } else {
744 if (ctx->loop_detection != NULL) {
745 if (node->object_num != 0 && pdfi_loop_detector_check_object(ctx, node->object_num)) {
746 code = gs_note_error(gs_error_circular_reference);
747 goto errorExit;
748 }
749 }
750 child = (pdf_dict *)node;
751 pdfi_countup(child);
752 }
753
754 *pchild = child;
755 child = NULL;
756
757 errorExit:
758 pdfi_free_object((pdf_obj *)leaf_dict);
759 pdfi_countdown(child);
760 pdfi_countdown(node);
761 pdfi_countdown(Type);
762 pdfi_countdown(Key);
763 return code;
764 }
765
766 /* Check if key is in the dictionary, and if so, copy it into the inheritable dict.
767 */
768 static int pdfi_check_inherited_key(pdf_context *ctx, pdf_dict *d, const char *keyname, pdf_dict *inheritable)
769 {
770 int code = 0;
771 pdf_obj *object = NULL;
772 bool known;
773
774 /* Check for inheritable keys, if we find any copy them to the 'inheritable' dictionary at this level */
775 code = pdfi_dict_known(ctx, d, keyname, &known);
776 if (code < 0)
777 goto exit;
778 if (known) {
779 code = pdfi_loop_detector_mark(ctx);
780 if (code < 0){
781 goto exit;
782 }
783 code = pdfi_dict_get(ctx, d, keyname, &object);
784 if (code < 0) {
785 (void)pdfi_loop_detector_cleartomark(ctx);
786 goto exit;
787 }
788 code = pdfi_loop_detector_cleartomark(ctx);
789 if (code < 0) {
790 goto exit;
791 }
792 code = pdfi_dict_put(ctx, inheritable, keyname, object);
793 }
794
795 exit:
796 pdfi_countdown(object);
797 return code;
798 }
799
800 int pdfi_get_page_dict(pdf_context *ctx, pdf_dict *d, uint64_t page_num, uint64_t *page_offset,
801 pdf_dict **target, pdf_dict *inherited)
802 {
803 int i, code = 0;
804 pdf_array *Kids = NULL;
805 pdf_dict *child = NULL;
806 pdf_name *Type = NULL;
807 pdf_dict *inheritable = NULL;
808 int64_t num;
809 double dbl;
810
811 if (ctx->args.pdfdebug)
812 outprintf(ctx->memory, "%% Finding page dictionary for page %"PRIi64"\n", page_num + 1);
813
814 /* Allocated inheritable dict (it might stay empty) */
815 code = pdfi_dict_alloc(ctx, 0, &inheritable);
816 if (code < 0)
817 return code;
818 pdfi_countup(inheritable);
819
820 code = pdfi_loop_detector_mark(ctx);
821 if (code < 0)
822 return code;
823
824 /* if we are being passed any inherited values from our parent, copy them now */
825 if (inherited != NULL) {
826 code = pdfi_dict_copy(ctx, inheritable, inherited);
827 if (code < 0)
828 goto exit;
829 }
830
831 code = pdfi_dict_get_number(ctx, d, "Count", &dbl);
832 if (code < 0)
833 goto exit;
834 if (dbl != floor(dbl)) {
835 code = gs_note_error(gs_error_rangecheck);
836 goto exit;
837 }
838 num = (int)dbl;
839
840 if (num < 0 || (num + *page_offset) > ctx->num_pages) {
841 code = gs_note_error(gs_error_rangecheck);
842 goto exit;
843 }
844 if (num + *page_offset < page_num) {
845 *page_offset += num;
846 code = 1;
847 goto exit;
848 }
849 /* The requested page is a descendant of this node */
850
851 /* Check for inheritable keys, if we find any copy them to the 'inheritable' dictionary at this level */
852 code = pdfi_check_inherited_key(ctx, d, "Resources", inheritable);
853 if (code < 0)
854 goto exit;
855 code = pdfi_check_inherited_key(ctx, d, "MediaBox", inheritable);
856 if (code < 0)
857 goto exit;
858 code = pdfi_check_inherited_key(ctx, d, "CropBox", inheritable);
859 if (code < 0)
860 goto exit;
861 code = pdfi_check_inherited_key(ctx, d, "Rotate", inheritable);
862 if (code < 0) {
863 goto exit;
864 }
865
866 /* Get the Kids array */
867 code = pdfi_dict_get_type(ctx, d, "Kids", PDF_ARRAY, (pdf_obj **)&Kids);
868 if (code < 0) {
869 goto exit;
870 }
871
872 /* Check each entry in the Kids array */
873 for (i = 0;i < pdfi_array_size(Kids);i++) {
874 pdfi_countdown(child);
875 child = NULL;
876 pdfi_countdown(Type);
877 Type = NULL;
878
879 code = pdfi_get_child(ctx, Kids, i, &child);
880 if (code < 0) {
881 goto exit;
882 }
883
884 /* Check the type, if its a Pages entry, then recurse. If its a Page entry, is it the one we want */
885 code = pdfi_dict_get_type(ctx, child, "Type", PDF_NAME, (pdf_obj **)&Type);
886 if (code == 0) {
887 if (pdfi_name_is(Type, "Pages")) {
888 code = pdfi_dict_get_number(ctx, child, "Count", &dbl);
889 if (code == 0) {
890 if (dbl != floor(dbl)) {
891 code = gs_note_error(gs_error_rangecheck);
892 goto exit;
893 }
894 num = (int)dbl;
895 if (num < 0 || (num + *page_offset) > ctx->num_pages) {
896 code = gs_note_error(gs_error_rangecheck);
897 goto exit;
898 } else {
899 if (num + *page_offset <= page_num) {
900 *page_offset += num;
901 } else {
902 if (child->object_num > 0) {
903 code = pdfi_loop_detector_mark(ctx);
904 if (code < 0)
905 goto exit;;
906
907 code = pdfi_loop_detector_add_object(ctx, child->object_num);
908 if (code < 0)
909 goto exit;
910 }
911 code = pdfi_get_page_dict(ctx, child, page_num, page_offset, target, inheritable);
912 if (child->object_num > 0)
913 pdfi_loop_detector_cleartomark(ctx);
914 goto exit;
915 }
916 }
917 }
918 } else {
919 if (pdfi_name_is(Type, "PageRef")) {
920 if ((*page_offset) == page_num) {
921 pdf_dict *page_dict = NULL;
922
923 code = pdfi_dict_get_no_store_R(ctx, child, "PageRef", (pdf_obj **)&page_dict);
924 if (code < 0)
925 goto exit;
926 if (pdfi_type_of(page_dict) != PDF_DICT) {
927 code = gs_note_error(gs_error_typecheck);
928 goto exit;
929 }
930 code = pdfi_merge_dicts(ctx, page_dict, inheritable);
931 *target = page_dict;
932 pdfi_countup(*target);
933 pdfi_countdown(page_dict);
934 goto exit;
935 } else {
936 *page_offset += 1;
937 }
938 } else {
939 if (!pdfi_name_is(Type, "Page")) {
940 if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_typecheck), NULL, E_PDF_BADPAGETYPE, "pdfi_get_page_dict", NULL)) < 0)
941 goto exit;
942 }
943 if ((*page_offset) == page_num) {
944 code = pdfi_merge_dicts(ctx, child, inheritable);
945 *target = child;
946 pdfi_countup(*target);
947 goto exit;
948 } else {
949 *page_offset += 1;
950 }
951 }
952 }
953 }
954 if (code < 0)
955 goto exit;
956 }
957 /* Positive return value indicates we did not find the target below this node, try the next one */
958 code = 1;
959
960 exit:
961 pdfi_loop_detector_cleartomark(ctx);
962 pdfi_countdown(inheritable);
963 pdfi_countdown(Kids);
964 pdfi_countdown(child);
965 pdfi_countdown(Type);
966 return code;
967 }
968
969 int pdfi_doc_page_array_init(pdf_context *ctx)
970 {
971 size_t size = ctx->num_pages*sizeof(uint32_t);
972
973 ctx->page_array = (uint32_t *)gs_alloc_bytes(ctx->memory, size,
974 "pdfi_doc_page_array_init(page_array)");
975 if (ctx->page_array == NULL)
976 return_error(gs_error_VMerror);
977
978 memset(ctx->page_array, 0, size);
979 return 0;
980 }
981
982 void pdfi_doc_page_array_free(pdf_context *ctx)
983 {
984 if (!ctx->page_array)
985 return;
986 gs_free_object(ctx->memory, ctx->page_array, "pdfi_doc_page_array_free(page_array)");
987 ctx->page_array = NULL;
988 }
989
990 /*
991 * Checks for both "Resource" and "RD" in the specified dict.
992 * And then gets the typedict of Type (e.g. Font or XObject).
993 * Returns 0 if undefined, >0 if found, <0 if error
994 */
995 static int pdfi_resource_knownget_typedict(pdf_context *ctx, unsigned char *Type,
996 pdf_dict *dict, pdf_dict **typedict)
997 {
998 int code;
999 pdf_dict *Resources = NULL;
1000
1001 code = pdfi_dict_knownget_type(ctx, dict, "Resources", PDF_DICT, (pdf_obj **)&Resources);
1002 if (code == 0)
1003 code = pdfi_dict_knownget_type(ctx, dict, "DR", PDF_DICT, (pdf_obj **)&Resources);
1004 if (code < 0)
1005 goto exit;
1006 if (code > 0)
1007 code = pdfi_dict_knownget_type(ctx, Resources, (const char *)Type, PDF_DICT, (pdf_obj **)typedict);
1008 exit:
1009 pdfi_countdown(Resources);
1010 return code;
1011 }
1012
1013 int pdfi_find_resource(pdf_context *ctx, unsigned char *Type, pdf_name *name,
1014 pdf_dict *dict, pdf_dict *page_dict, pdf_obj **o)
1015 {
1016 pdf_dict *typedict = NULL;
1017 pdf_dict *Parent = NULL;
1018 pdf_name *n = NULL;
1019 int code;
1020 bool known = false;
1021
1022 *o = NULL;
1023
1024 /* Check the provided dict, stream_dict can be NULL if we are trying to find a Default* ColorSpace */
1025 if (dict != NULL) {
1026 bool deref_parent = true, dict_is_XObject = false;
1027
1028 code = pdfi_resource_knownget_typedict(ctx, Type, dict, &typedict);
1029 if (code < 0)
1030 goto exit;
1031 if (code > 0) {
1032 code = pdfi_dict_get_no_store_R_key(ctx, typedict, name, o);
1033 if (code != gs_error_undefined)
1034 goto exit;
1035 }
1036
1037 /* Check the Parents, if any */
1038 /* If the current dictionary is a Page dictionary, do NOT dereference it's Parent, as that
1039 * will be the Pages tree, and we will end up with circular references, causing a memory leak.
1040 */
1041 if (pdfi_dict_knownget_type(ctx, dict, "Type", PDF_NAME, (pdf_obj **)&n) > 0) {
1042 if (pdfi_name_is(n, "Page"))
1043 deref_parent = false;
1044 if (pdfi_name_is(n, "XObject"))
1045 dict_is_XObject = true;
1046 pdfi_countdown(n);
1047 }
1048
1049 if (deref_parent) {
1050 code = pdfi_dict_known(ctx, dict, "Parent", &known);
1051 if (code >= 0 && known == true) {
1052 code = pdfi_dict_get_no_store_R(ctx, dict, "Parent", (pdf_obj **)&Parent);
1053
1054 if (code >= 0) {
1055 if (pdfi_type_of(Parent) != PDF_DICT) {
1056 if (pdfi_type_of(Parent) == PDF_INDIRECT) {
1057 pdf_indirect_ref *o = (pdf_indirect_ref *)Parent;
1058
1059 Parent = NULL;
1060 code = pdfi_dereference(ctx, o->ref_object_num, o->ref_generation_num, (pdf_obj **)&Parent);
1061 pdfi_countdown(o);
1062 if (code >= 0 && pdfi_type_of(Parent) != PDF_DICT) {
1063 pdfi_countdown(Parent);
1064 Parent = NULL;
1065 }
1066 } else {
1067 pdfi_countdown(Parent);
1068 Parent = NULL;
1069 }
1070 }
1071 } else
1072 Parent = NULL;
1073 }
1074
1075 if (Parent != NULL) {
1076 if (ctx->page.CurrentPageDict != NULL && Parent->object_num != ctx->page.CurrentPageDict->object_num) {
1077 if (pdfi_loop_detector_check_object(ctx, Parent->object_num) == true) {
1078 code = gs_note_error(gs_error_circular_reference);
1079 goto exit;
1080 }
1081
1082 code = pdfi_loop_detector_mark(ctx);
1083 if (code < 0)
1084 goto exit;
1085
1086 code = pdfi_loop_detector_add_object(ctx, dict->object_num);
1087 if (code < 0) {
1088 (void)pdfi_loop_detector_cleartomark(ctx);
1089 goto exit;
1090 }
1091 code = pdfi_find_resource(ctx, Type, name, Parent, page_dict, o);
1092 (void)pdfi_loop_detector_cleartomark(ctx);
1093 if (code != gs_error_undefined) {
1094 if (dict_is_XObject)
1095 pdfi_set_warning(ctx, 0, NULL, W_PDF_INHERITED_STREAM_RESOURCE, "pdfi_find_resource", (char *)"Couldn't find named resource in supplied dictionary, matching name located in Page Resource");
1096 goto exit;
1097 }
1098 }
1099 }
1100 code = 0;
1101 }
1102 pdfi_countdown(typedict);
1103 typedict = NULL;
1104 }
1105
1106 /* Normally page_dict can't be (or shouldn't be) NULL. However, if we are processing
1107 * a TYpe 3 font, then the 'page dict' is the Resources dictionary of that font. If
1108 * the font inherits Resources from its page (which it should not) then its possible
1109 * that the 'page dict' could be NULL here. We need to guard against that. Its possible
1110 * there may be other, similar, cases (eg Patterns within Patterns). In addition we
1111 * do need to be able to check the real page dictionary for inhereited resources, and
1112 * in the case of a type 3 font BuildChar at least there is no easy way to do that.
1113 * So we'll store the page dictionary for the current page in the context as a
1114 * last-ditch resource to check.
1115 */
1116 if (page_dict != NULL) {
1117 code = pdfi_resource_knownget_typedict(ctx, Type, page_dict, &typedict);
1118 if (code < 0)
1119 goto exit;
1120
1121 if (code > 0) {
1122 code = pdfi_dict_get_no_store_R_key(ctx, typedict, name, o);
1123 if (code != gs_error_undefined)
1124 goto exit;
1125 }
1126 }
1127
1128 pdfi_countdown(typedict);
1129 typedict = NULL;
1130
1131 if (ctx->page.CurrentPageDict != NULL) {
1132 code = pdfi_resource_knownget_typedict(ctx, Type, ctx->page.CurrentPageDict, &typedict);
1133 if (code < 0)
1134 goto exit;
1135
1136 if (code > 0) {
1137 code = pdfi_dict_get_no_store_R_key(ctx, typedict, name, o);
1138 if (code != gs_error_undefined)
1139 goto exit;
1140 }
1141 }
1142
1143 pdfi_countdown(typedict);
1144 typedict = NULL;
1145
1146 if (ctx->current_stream != NULL) {
1147 pdf_dict *stream_dict = NULL;
1148 pdf_stream *stream = ctx->current_stream;
1149
1150 do {
1151 code = pdfi_dict_from_obj(ctx, (pdf_obj *)stream, &stream_dict);
1152 if (code < 0)
1153 goto exit;
1154 code = pdfi_resource_knownget_typedict(ctx, Type, stream_dict, &typedict);
1155 if (code < 0)
1156 goto exit;
1157 if (code > 0) {
1158 code = pdfi_dict_get_no_store_R_key(ctx, typedict, name, o);
1159 if (code == 0) {
1160 pdfi_set_error(ctx, 0, NULL, E_PDF_INHERITED_STREAM_RESOURCE, "pdfi_find_resource", (char *)"Couldn't find named resource in supplied dictionary, or Parents, or Pages, matching name located in earlier stream Resource");
1161 goto exit;
1162 }
1163 }
1164 pdfi_countdown(typedict);
1165 typedict = NULL;
1166 stream = pdfi_stream_parent(ctx, stream);
1167 }while(stream != NULL);
1168 }
1169
1170 /* If we got all the way down there, we didn't find it */
1171 pdfi_set_warning(ctx, 0, NULL, W_PDF_MISSING_NAMED_RESOURCE, "pdfi_find_resource", NULL);
1172 code = gs_error_undefined;
1173
1174 exit:
1175 pdfi_countdown(typedict);
1176 pdfi_countdown(Parent);
1177 return code;
1178 }
1179
1180 /* Mark the actual outline */
1181 static int pdfi_doc_mark_the_outline(pdf_context *ctx, pdf_dict *outline)
1182 {
1183 int code = 0;
1184 pdf_dict *tempdict = NULL;
1185 uint64_t dictsize;
1186 uint64_t index;
1187 pdf_name *Key = NULL;
1188 double num = 0;
1189
1190 /* Basically we only do /Count, /Title, /A, /C, /F
1191 * The /First, /Last, /Next, /Parent get written magically by pdfwrite
1192 */
1193
1194 /* Make a temporary copy of the outline dict */
1195 dictsize = pdfi_dict_entries(outline);
1196 code = pdfi_dict_alloc(ctx, dictsize, &tempdict);
1197 if (code < 0) goto exit;
1198 pdfi_countup(tempdict);
1199 code = pdfi_dict_copy(ctx, tempdict, outline);
1200 if (code < 0) goto exit;
1201
1202 /* Due to some craziness on the part of Adobe, the /Count in an Outline entry
1203 * in a PDF file, and the /Count value in an /OUT pdfmark mean different things(!)
1204 * In a PDF file it is the number of outline entries beneath the current entry
1205 * (all child descsndants) whereas in a pdfmark it is the number of entries
1206 * in just the next level. So we cannot use the /Count from the PDF file, we
1207 * need to go to the /First entry of the next level, and then count all
1208 * the entries at that level by following each /Next.
1209 */
1210 code = pdfi_dict_knownget_number(ctx, outline, "Count", &num);
1211 if (code < 0)
1212 goto exit;
1213
1214 /* We can't rely on Count being present to indicate that the Outline has descendants
1215 * see bug #707228. Instead, look for the presence of a /First key. If there is no count
1216 * key, or it is 0, then assume the entry is closed.
1217 */
1218 {
1219 pdf_dict *current = NULL, *next = NULL;
1220 int count = 0, code1;
1221
1222 code1 = pdfi_dict_knownget_type(ctx, outline, "First", PDF_DICT, (pdf_obj **)&current);
1223 if (code1 > 0) {
1224 if (code <= 0) {
1225 if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_OUTLINECHILD_NO_COUNT, "pdfi_doc_mark_the_outline", NULL)) < 0)
1226 goto exit;
1227 }
1228 count++;
1229 do {
1230 code1 = pdfi_dict_knownget_type(ctx, current, "Next", PDF_DICT, (pdf_obj **)&next);
1231 if (code1 > 0) {
1232 pdfi_countdown(current);
1233 current = next;
1234 next = NULL;
1235 count++;
1236 } else
1237 break;
1238 } while (1);
1239 pdfi_countdown(current);
1240 }
1241 if (num <= 0)
1242 count *= -1;
1243 pdfi_dict_put_int(ctx, tempdict, "Count", count);
1244 }
1245
1246 /* Go through the dict, removing some keys and doing special handling for others.
1247 */
1248 code = pdfi_dict_key_first(ctx, outline, (pdf_obj **)&Key, &index);
1249 while (code >= 0) {
1250 if (pdfi_name_is(Key, "Last") || pdfi_name_is(Key, "Next") || pdfi_name_is(Key, "First") ||
1251 pdfi_name_is(Key, "Prev") || pdfi_name_is(Key, "Parent")) {
1252 /* Delete some keys
1253 * These are handled in pdfwrite and can lead to circular refs
1254 */
1255 code = pdfi_dict_delete_pair(ctx, tempdict, Key);
1256 } else if (pdfi_name_is(Key, "SE")) {
1257 /* TODO: Not sure what to do with SE, delete for now */
1258 /* Seems to be okay to just delete it, since there should also be a /Dest
1259 * See sample fts_29_2903.pdf
1260 * Currently we are same as gs
1261 */
1262 code = pdfi_dict_delete_pair(ctx, tempdict, Key);
1263 } else if (pdfi_name_is(Key, "A")) {
1264 code = pdfi_pdfmark_modA(ctx, tempdict);
1265 } else if (pdfi_name_is(Key, "Dest")) {
1266 code = pdfi_pdfmark_modDest(ctx, tempdict);
1267 }
1268 if (code < 0)
1269 goto exit;
1270
1271 pdfi_countdown(Key);
1272 Key = NULL;
1273
1274 code = pdfi_dict_key_next(ctx, outline, (pdf_obj **)&Key, &index);
1275 if (code == gs_error_undefined) {
1276 code = 0;
1277 break;
1278 }
1279 }
1280 if (code < 0) goto exit;
1281
1282 /* Write the pdfmark */
1283 code = pdfi_pdfmark_from_dict(ctx, tempdict, NULL, "OUT");
1284 if (code < 0)
1285 goto exit;
1286
1287 exit:
1288 pdfi_countdown(tempdict);
1289 pdfi_countdown(Key);
1290 return code;
1291 }
1292
1293 /* Do pdfmark on an outline entry (recursive)
1294 * Note: the logic here is wonky. It is relying on the behavior of the pdfwrite driver.
1295 * See pdf_main.ps/writeoutline()
1296 */
1297 static int pdfi_doc_mark_outline(pdf_context *ctx, pdf_dict *outline)
1298 {
1299 int code = 0;
1300 pdf_dict *child = NULL;
1301 pdf_dict *Next = NULL;
1302
1303 if (outline == (pdf_dict *)PDF_NULL_OBJ)
1304 return 0;
1305
1306 /* Mark the outline */
1307 /* NOTE: I think the pdfmark for this needs to be written before the children
1308 * because I think pdfwrite relies on the order of things.
1309 */
1310 code = pdfi_doc_mark_the_outline(ctx, outline);
1311 if (code < 0)
1312 goto exit1;
1313
1314 /* Handle the children */
1315 code = pdfi_loop_detector_mark(ctx);
1316 if (code < 0)
1317 goto exit1;
1318
1319 /* Handle any children (don't deref them, we don't want to leave them hanging around) */
1320 code = pdfi_dict_get_no_store_R(ctx, outline, "First", (pdf_obj **)&child);
1321 if (code < 0 || pdfi_type_of(child) != PDF_DICT) {
1322 /* TODO: flag a warning? */
1323 code = 0;
1324 goto exit;
1325 }
1326
1327 if (child->object_num != 0) {
1328 code = pdfi_loop_detector_add_object(ctx, child->object_num);
1329 if (code < 0)
1330 goto exit;
1331 }
1332
1333 do {
1334 code = pdfi_doc_mark_outline(ctx, child);
1335 if (code < 0) goto exit;
1336
1337
1338 code = pdfi_dict_get_no_store_R(ctx, child, "Next", (pdf_obj **)&Next);
1339 if (code == gs_error_undefined) {
1340 code = 0;
1341 break;
1342 }
1343 if (code == gs_error_circular_reference) {
1344 code = 0;
1345 goto exit;
1346 }
1347 if (code < 0 || pdfi_type_of(Next) != PDF_DICT)
1348 goto exit;
1349
1350 pdfi_countdown(child);
1351 child = Next;
1352 Next = NULL;
1353 } while (true);
1354
1355 exit:
1356 (void)pdfi_loop_detector_cleartomark(ctx);
1357 exit1:
1358 pdfi_countdown(child);
1359 pdfi_countdown(Next);
1360 return code;
1361 }
1362
1363 /* Do pdfmark for Outlines */
1364 static int pdfi_doc_Outlines(pdf_context *ctx)
1365 {
1366 int code = 0;
1367 pdf_dict *Outlines = NULL;
1368 pdf_dict *outline = NULL;
1369 pdf_dict *Next = NULL;
1370
1371 if (ctx->args.no_pdfmark_outlines)
1372 goto exit1;
1373
1374 code = pdfi_dict_knownget_type(ctx, ctx->Root, "Outlines", PDF_DICT, (pdf_obj **)&Outlines);
1375 if (code <= 0) {
1376 /* TODO: flag a warning */
1377 code = 0;
1378 goto exit1;
1379 }
1380
1381 code = pdfi_loop_detector_mark(ctx);
1382 if (code < 0)
1383 goto exit1;
1384
1385 /* Handle any children (don't deref them, we don't want to leave them hanging around) */
1386 code = pdfi_dict_get_no_store_R(ctx, Outlines, "First", (pdf_obj **)&outline);
1387 if (code < 0 || pdfi_type_of(outline) != PDF_DICT) {
1388 /* TODO: flag a warning? */
1389 code = 0;
1390 goto exit;
1391 }
1392
1393 if (pdfi_type_of(outline) != PDF_DICT)
1394 goto exit; /* Exit with no error? */
1395
1396 if (outline->object_num != 0) {
1397 code = pdfi_loop_detector_add_object(ctx, outline->object_num);
1398 if (code < 0)
1399 goto exit;
1400 }
1401
1402 /* Loop through all the top-level outline entries
1403 * First one is in Outlines, and if there are more, they are the Next of the
1404 * current outline item. (see spec)
1405 * (basically we are walking a linked list)
1406 */
1407 do {
1408 code = pdfi_doc_mark_outline(ctx, outline);
1409 if (code < 0) goto exit;
1410
1411
1412 code = pdfi_dict_get_no_store_R(ctx, outline, "Next", (pdf_obj **)&Next);
1413 if (code == gs_error_undefined) {
1414 code = 0;
1415 break;
1416 }
1417 if (code == gs_error_circular_reference) {
1418 code = 0;
1419 goto exit;
1420 }
1421 if (code < 0 || pdfi_type_of(Next) != PDF_DICT)
1422 goto exit;
1423
1424 pdfi_countdown(outline);
1425 outline = Next;
1426 Next = NULL;
1427 } while (true);
1428
1429 exit:
1430 (void)pdfi_loop_detector_cleartomark(ctx);
1431 exit1:
1432 pdfi_countdown(Outlines);
1433 pdfi_countdown(outline);
1434 pdfi_countdown(Next);
1435 return code;
1436 }
1437
1438 /* Do pdfmark for Info */
1439 static int pdfi_doc_Info(pdf_context *ctx)
1440 {
1441 int code = 0;
1442 pdf_dict *Info = NULL, *d = NULL;
1443 pdf_dict *tempdict = NULL;
1444 uint64_t dictsize;
1445 uint64_t index;
1446 pdf_name *Key = NULL;
1447 pdf_obj *Value = NULL;
1448
1449 /* See comment in pdfi_read_Root() for details */
1450 d = ctx->Trailer;
1451 pdfi_countup(d);
1452 code = pdfi_dict_knownget_type(ctx, d, "Info", PDF_DICT, (pdf_obj **)&Info);
1453 pdfi_countdown(d);
1454 if (code <= 0) {
1455 /* TODO: flag a warning */
1456 goto exit;
1457 }
1458
1459 /* Make a temporary copy of the Info dict */
1460 dictsize = pdfi_dict_entries(Info);
1461 code = pdfi_dict_alloc(ctx, dictsize, &tempdict);
1462 if (code < 0) goto exit;
1463 pdfi_countup(tempdict);
1464
1465 /* Copy only certain keys from Info to tempdict
1466 * NOTE: pdfwrite will set /Producer, /CreationDate and /ModDate
1467 */
1468 code = pdfi_dict_first(ctx, Info, (pdf_obj **)&Key, &Value, &index);
1469 while (code >= 0) {
1470 if (pdfi_name_is(Key, "Author") || pdfi_name_is(Key, "Creator") ||
1471 pdfi_name_is(Key, "Title") || pdfi_name_is(Key, "Subject") ||
1472 pdfi_name_is(Key, "Keywords")) {
1473
1474 code = pdfi_dict_put_obj(ctx, tempdict, (pdf_obj *)Key, Value, true);
1475 if (code < 0)
1476 goto exit;
1477 }
1478 pdfi_countdown(Key);
1479 Key = NULL;
1480 pdfi_countdown(Value);
1481 Value = NULL;
1482
1483 code = pdfi_dict_next(ctx, Info, (pdf_obj **)&Key, &Value, &index);
1484 if (code == gs_error_undefined) {
1485 code = 0;
1486 break;
1487 }
1488 }
1489 if (code < 0) goto exit;
1490
1491 /* Write the pdfmark */
1492 code = pdfi_pdfmark_from_dict(ctx, tempdict, NULL, "DOCINFO");
1493
1494 exit:
1495 pdfi_countdown(Key);
1496 pdfi_countdown(Value);
1497 pdfi_countdown(Info);
1498 pdfi_countdown(tempdict);
1499 return code;
1500 }
1501
1502 /* Handle PageLabels for pdfwrite device */
1503 static int pdfi_doc_PageLabels(pdf_context *ctx)
1504 {
1505 int code;
1506 pdf_dict *PageLabels = NULL;
1507
1508 if (ctx->loop_detection) {
1509 code = pdfi_loop_detector_mark(ctx);
1510 if (code < 0)
1511 return code;
1512 }
1513
1514 code = pdfi_dict_knownget_type(ctx, ctx->Root, "PageLabels", PDF_DICT, (pdf_obj **)&PageLabels);
1515 if (code <= 0) {
1516 if (ctx->loop_detection)
1517 (void)pdfi_loop_detector_cleartomark(ctx);
1518 /* TODO: flag a warning */
1519 goto exit;
1520 }
1521
1522 if (ctx->loop_detection) {
1523 code = pdfi_loop_detector_cleartomark(ctx);
1524 if (code < 0)
1525 goto exit;
1526 }
1527
1528 if (ctx->device_state.WantsPageLabels) {
1529 /* This will send the PageLabels object as a 'pdfpagelabels' setdeviceparams */
1530 code = pdfi_pdfmark_object(ctx, (pdf_obj *)PageLabels, "pdfpagelabels");
1531 if (code < 0)
1532 goto exit;
1533 }
1534
1535 exit:
1536 pdfi_countdown(PageLabels);
1537 return code;
1538 }
1539
1540 /* Handle OutputIntents stuff
1541 * (bottom of pdf_main.ps/process_trailer_attrs)
1542 */
1543 static int pdfi_doc_OutputIntents(pdf_context *ctx)
1544 {
1545 int code;
1546 pdf_array *OutputIntents = NULL;
1547 pdf_dict *intent = NULL;
1548 pdf_string *name = NULL;
1549 pdf_stream *DestOutputProfile = NULL;
1550 uint64_t index;
1551
1552 /* NOTE: subtle difference in error handling -- we are checking for OutputIntents first,
1553 * so this will just ignore UsePDFX3Profile or UseOutputIntent params without warning,
1554 * if OutputIntents doesn't exist. Seems fine to me.
1555 */
1556 code = pdfi_dict_knownget_type(ctx, ctx->Root, "OutputIntents", PDF_ARRAY,
1557 (pdf_obj **)&OutputIntents);
1558 if (code <= 0) {
1559 goto exit;
1560 }
1561
1562 /* TODO: Implement writeoutputintents if somebody ever complains...
1563 * See pdf_main.ps/writeoutputintents
1564 * I am not aware of a device that supports "/OutputIntent" so
1565 * couldn't figure out what to do for this.
1566 */
1567
1568 /* Handle UsePDFX3Profile and UseOutputIntent command line options */
1569 if (ctx->args.UsePDFX3Profile) {
1570 /* This is an index into the array */
1571 code = pdfi_array_get_type(ctx, OutputIntents, ctx->args.PDFX3Profile_num,
1572 PDF_DICT, (pdf_obj **)&intent);
1573 if (code < 0) {
1574 code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_OUTPUTINTENT_INDEX, "pdfi_doc_OutputIntents", "");
1575 goto exit;
1576 }
1577 } else if (ctx->args.UseOutputIntent != NULL) {
1578 /* This is a name to look up in the array */
1579 for (index=0; index<pdfi_array_size(OutputIntents); index ++) {
1580 code = pdfi_array_get_type(ctx, OutputIntents, index, PDF_DICT, (pdf_obj **)&intent);
1581 if (code < 0) goto exit;
1582
1583 code = pdfi_dict_knownget_type(ctx, intent, "OutputConditionIdentifier", PDF_STRING,
1584 (pdf_obj **)&name);
1585 if (code < 0) goto exit;
1586 if (code == 0)
1587 continue;
1588
1589 /* If the ID is "Custom" then check "Info" instead */
1590 if (pdfi_string_is(name, "Custom")) {
1591 pdfi_countdown(name);
1592 name = NULL;
1593 code = pdfi_dict_knownget_type(ctx, intent, "Info", PDF_STRING, (pdf_obj **)&name);
1594 if (code < 0) goto exit;
1595 if (code == 0)
1596 continue;
1597 }
1598
1599 /* Check for a match */
1600 if (pdfi_string_is(name, ctx->args.UseOutputIntent))
1601 break;
1602
1603 pdfi_countdown(intent);
1604 intent = NULL;
1605 pdfi_countdown(name);
1606 name = NULL;
1607 }
1608 code = 0;
1609 } else {
1610 /* No command line arg was specified, so nothing to do */
1611 code = 0;
1612 goto exit;
1613 }
1614
1615 /* Now if intent is non-null, we found the selected intent dictionary */
1616 if (intent == NULL)
1617 goto exit;
1618
1619 /* Load the profile, if it exists */
1620 code = pdfi_dict_knownget_type(ctx, intent, "DestOutputProfile", PDF_STREAM, (pdf_obj **)&DestOutputProfile);
1621 /* TODO: Flag an error if it doesn't exist? Only required in some cases */
1622 if (code <= 0) goto exit;
1623
1624 /* Set the intent to the profile */
1625 code = pdfi_color_setoutputintent(ctx, intent, DestOutputProfile);
1626
1627 exit:
1628 pdfi_countdown(OutputIntents);
1629 pdfi_countdown(intent);
1630 pdfi_countdown(name);
1631 pdfi_countdown(DestOutputProfile);
1632 return code;
1633 }
1634
1635 /* Handled an embedded files Names array for pdfwrite device */
1636 static int pdfi_doc_EmbeddedFiles_Names(pdf_context *ctx, pdf_array *names)
1637 {
1638 int code;
1639 uint64_t arraysize;
1640 uint64_t index;
1641 pdf_string *name = NULL;
1642 pdf_dict *filespec = NULL;
1643
1644 arraysize = pdfi_array_size(names);
1645 if ((arraysize % 2) != 0) {
1646 code = gs_note_error(gs_error_syntaxerror);
1647 goto exit;
1648 }
1649
1650 /* This is supposed to be an array of
1651 * [ (filename1) (filespec1) (filename2) (filespec2) ... ]
1652 */
1653 for (index = 0; index < arraysize; index += 2) {
1654 code = pdfi_array_get_type(ctx, names, index, PDF_STRING, (pdf_obj **)&name);
1655 if (code < 0) goto exit;
1656
1657 code = pdfi_array_get_type(ctx, names, index+1, PDF_DICT, (pdf_obj **)&filespec);
1658 if (code < 0) goto exit;
1659
1660 code = pdfi_pdfmark_embed_filespec(ctx, name, filespec);
1661 if (code < 0) goto exit;
1662
1663 pdfi_countdown(name);
1664 name = NULL;
1665 pdfi_countdown(filespec);
1666 filespec = NULL;
1667 }
1668
1669
1670 exit:
1671 pdfi_countdown(name);
1672 pdfi_countdown(filespec);
1673 return code;
1674 }
1675
1676 /* Handle PageLabels for pdfwrite device */
1677 static int pdfi_doc_EmbeddedFiles(pdf_context *ctx)
1678 {
1679 int code;
1680 pdf_dict *Names = NULL;
1681 pdf_dict *EmbeddedFiles = NULL;
1682 pdf_array *Names_array = NULL;
1683 pdf_array *Kids = NULL;
1684
1685 code = pdfi_dict_knownget_type(ctx, ctx->Root, "Collection", PDF_DICT, (pdf_obj **)&Names);
1686 if (code < 0) goto exit;
1687 if (code > 0) {
1688 code = 0;
1689 goto exit;
1690 }
1691
1692 code = pdfi_dict_knownget_type(ctx, ctx->Root, "Names", PDF_DICT, (pdf_obj **)&Names);
1693 if (code <= 0) goto exit;
1694
1695 code = pdfi_dict_knownget_type(ctx, Names, "EmbeddedFiles", PDF_DICT, (pdf_obj **)&EmbeddedFiles);
1696 if (code <= 0) goto exit;
1697
1698 code = pdfi_dict_knownget_type(ctx, Names, "Kids", PDF_ARRAY, (pdf_obj **)&Kids);
1699 if (code < 0) goto exit;
1700 if (code > 0) {
1701 /* TODO: Need to implement */
1702 errprintf(ctx->memory, "*** WARNING Kids array in EmbeddedFiles not implemented\n");
1703 }
1704
1705 /* TODO: This is a name tree.
1706 * Can contain a Names array, or some complicated Kids.
1707 * Just handling Names array for now
1708 */
1709 code = pdfi_dict_knownget_type(ctx, EmbeddedFiles, "Names", PDF_ARRAY, (pdf_obj **)&Names_array);
1710 if (code <= 0) goto exit;
1711
1712 code = pdfi_doc_EmbeddedFiles_Names(ctx, Names_array);
1713 if (code <= 0) goto exit;
1714
1715 exit:
1716 pdfi_countdown(Kids);
1717 pdfi_countdown(Names);
1718 pdfi_countdown(EmbeddedFiles);
1719 pdfi_countdown(Names_array);
1720 return code;
1721 }
1722
1723 /* Handle some bookkeeping related to AcroForm (and annotations)
1724 * See pdf_main.ps/process_trailer_attrs/AcroForm
1725 *
1726 * Mainly we preload AcroForm and NeedAppearances in the context
1727 *
1728 * TODO: gs code also seems to do something to link up parents in fields/annotations (ParentField)
1729 * We are going to avoid doing that for now.
1730 */
1731 static int pdfi_doc_AcroForm(pdf_context *ctx)
1732 {
1733 int code = 0;
1734 pdf_dict *AcroForm = NULL;
1735 bool boolval = false;
1736
1737 code = pdfi_dict_knownget_type(ctx, ctx->Root, "AcroForm", PDF_DICT, (pdf_obj **)&AcroForm);
1738 if (code <= 0) goto exit;
1739
1740 code = pdfi_dict_get_bool(ctx, AcroForm, "NeedAppearances", &boolval);
1741 if (code < 0) {
1742 if (code == gs_error_undefined) {
1743 boolval = true;
1744 code = 0;
1745 }
1746 else
1747 goto exit;
1748 }
1749 ctx->NeedAppearances = boolval;
1750
1751 /* Save this for efficiency later */
1752 ctx->AcroForm = AcroForm;
1753 pdfi_countup(AcroForm);
1754
1755 /* TODO: Link up ParentField (but hopefully we can avoid doing this hacky mess).
1756 * Also: Something to do with Bug692447.pdf?
1757 */
1758
1759
1760 exit:
1761 pdfi_countdown(AcroForm);
1762 return code;
1763 }
1764
1765
1766 static int pdfi_doc_view(pdf_context *ctx)
1767 {
1768 int code = 0;
1769 pdf_dict *tempdict = NULL;
1770 pdf_name *Mode = NULL, *Layout = NULL;
1771 pdf_obj *ActionDest = NULL;
1772
1773 code = pdfi_dict_knownget_type(ctx, ctx->Root, "PageMode", PDF_NAME, (pdf_obj **)&Mode);
1774 if (code < 0)
1775 return code;
1776
1777 if (code != 0) {
1778 code = pdfi_dict_alloc(ctx, 1, &tempdict);
1779 if (code < 0)
1780 goto exit;
1781
1782 pdfi_countup(tempdict);
1783
1784 code = pdfi_dict_put(ctx, tempdict, "PageMode", (pdf_obj *)Mode);
1785 if (code < 0)
1786 goto exit;
1787
1788 code = pdfi_pdfmark_from_dict(ctx, tempdict, NULL, "DOCVIEW");
1789 if (code < 0)
1790 goto exit;
1791 pdfi_countdown(tempdict);
1792 tempdict = NULL;
1793 }
1794
1795 code = pdfi_dict_knownget_type(ctx, ctx->Root, "PageLayout", PDF_NAME, (pdf_obj **)&Layout);
1796 if (code < 0)
1797 goto exit;
1798
1799 if (code != 0) {
1800 code = pdfi_dict_alloc(ctx, 1, &tempdict);
1801 if (code < 0)
1802 goto exit;
1803
1804 pdfi_countup(tempdict);
1805
1806 code = pdfi_dict_put(ctx, tempdict, "PageLayout", (pdf_obj *)Layout);
1807 if (code < 0)
1808 goto exit;
1809
1810 code = pdfi_pdfmark_from_dict(ctx, tempdict, NULL, "DOCVIEW");
1811 if (code < 0)
1812 goto exit;
1813 pdfi_countdown(tempdict);
1814 tempdict = NULL;
1815 }
1816
1817 code = pdfi_dict_knownget(ctx, ctx->Root, "OpenAction", &ActionDest);
1818 if (code < 0)
1819 goto exit;
1820
1821 if (code != 0) {
1822 if (pdfi_type_of(ActionDest) == PDF_DICT) {
1823 /* Dictionary means this is an action */
1824 code = pdfi_dict_alloc(ctx, 1, &tempdict);
1825 if (code < 0)
1826 goto exit;
1827
1828 pdfi_countup(tempdict);
1829
1830 code = pdfi_dict_put(ctx, tempdict, "A", (pdf_obj *)ActionDest);
1831 if (code < 0)
1832 goto exit;
1833
1834 code = pdfi_pdfmark_modA(ctx, tempdict);
1835 if (code < 0) goto exit;
1836
1837 code = pdfi_pdfmark_from_dict(ctx, tempdict, NULL, "DOCVIEW");
1838 } else {
1839 if (pdfi_type_of(ActionDest) == PDF_ARRAY) {
1840 /* Array means this is a destination */
1841 code = pdfi_dict_alloc(ctx, 1, &tempdict);
1842 if (code < 0)
1843 goto exit;
1844
1845 pdfi_countup(tempdict);
1846
1847 code = pdfi_dict_put(ctx, tempdict, "Dest", (pdf_obj *)ActionDest);
1848 if (code < 0)
1849 goto exit;
1850 code = pdfi_pdfmark_modDest(ctx, tempdict);
1851 if (code < 0)
1852 goto exit;
1853 code = pdfi_pdfmark_from_dict(ctx, tempdict, NULL, "DOCVIEW");
1854 if (code < 0)
1855 goto exit;
1856 } else {
1857 code = gs_note_error(gs_error_typecheck);
1858 goto exit;
1859 }
1860 }
1861 }
1862
1863 exit:
1864 pdfi_countdown(ActionDest);
1865 pdfi_countdown(Layout);
1866 pdfi_countdown(Mode);
1867 pdfi_countdown(tempdict);
1868 return code;
1869 }
1870
1871
1872 /* See pdf_main.ps/process_trailer_attrs()
1873 * Some of this stuff is about pdfmarks, and some of it is just handling
1874 * random things in the trailer.
1875 */
1876 int pdfi_doc_trailer(pdf_context *ctx)
1877 {
1878 int code = 0;
1879
1880 /* Can't do this stuff with no Trailer */
1881 if (!ctx->Trailer) {
1882 if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_undefined), NULL, W_PDF_BAD_TRAILER, "pdfi_doc_trailer", NULL)) < 0)
1883 goto exit;
1884 }
1885
1886 if (ctx->device_state.writepdfmarks) {
1887 /* Handle Outlines */
1888 code = pdfi_doc_Outlines(ctx);
1889 if (code < 0) {
1890 if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_OUTLINES, "pdfi_doc_trailer", NULL)) < 0)
1891 goto exit;
1892 }
1893
1894 /* Handle Docview pdfmark stuff */
1895 if (ctx->args.preservedocview) {
1896 code = pdfi_doc_view(ctx);
1897 if (code < 0) {
1898 if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_VIEW, "pdfi_doc_view", NULL)) < 0)
1899 goto exit;
1900 }
1901 }
1902
1903 /* Handle Info */
1904 code = pdfi_doc_Info(ctx);
1905 if (code < 0) {
1906 /* pdfwrite will set a Fatal error when processing the DOCINFO if it has been
1907 * told to create a PDF/A. the PDFA compatibility is 2, and the file info
1908 * cannot be handled. In that case, abort immediately.
1909 */
1910 if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_INFO, "pdfi_doc_trailer", NULL)) < 0)
1911 goto exit;
1912 }
1913
1914 /* Handle EmbeddedFiles */
1915 /* TODO: add a configuration option to embed or omit */
1916 if (ctx->args.preserveembeddedfiles) {
1917 code = pdfi_doc_EmbeddedFiles(ctx);
1918 if (code < 0) {
1919 if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_EMBEDDEDFILES, "pdfi_doc_trailer", NULL)) < 0)
1920 goto exit;
1921 }
1922 }
1923 }
1924
1925 /* Handle OCProperties */
1926 /* NOTE: Apparently already handled by pdfi_read_OptionalRoot() */
1927
1928 /* Handle AcroForm -- this is some bookkeeping once per doc, not rendering them yet */
1929 code = pdfi_doc_AcroForm(ctx);
1930 if (code < 0) {
1931 if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_ACROFORM, "pdfi_doc_trailer", NULL)) < 0)
1932 goto exit;
1933 }
1934
1935 /* Handle OutputIntent ICC Profile */
1936 code = pdfi_doc_OutputIntents(ctx);
1937 if (code < 0) {
1938 if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_OUTPUTINTENTS, "pdfi_doc_trailer", NULL)) < 0)
1939 goto exit;
1940 }
1941
1942 /* Handle PageLabels */
1943 code = pdfi_doc_PageLabels(ctx);
1944 if (code < 0) {
1945 if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_PAGELABELS, "pdfi_doc_trailer", NULL)) < 0)
1946 goto exit;
1947 }
1948
1949 exit:
1950 return code;
1951 }