Coverage Report

Created: 2026-04-01 07:17

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