Coverage Report

Created: 2026-02-14 07:09

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
41.2k
{
36
41.2k
    pdf_obj *o, *o1;
37
41.2k
    pdf_dict *d;
38
41.2k
    int code;
39
40
41.2k
    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
41.2k
    d = ctx->Trailer;
50
41.2k
    pdfi_countup(d);
51
41.2k
    code = pdfi_dict_get(ctx, d, "Root", &o1);
52
41.2k
    if (code < 0) {
53
1.09k
        pdfi_countdown(d);
54
1.09k
        return code;
55
1.09k
    }
56
40.1k
    pdfi_countdown(d);
57
58
40.1k
    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
40.1k
    } else {
76
40.1k
        if (pdfi_type_of(o1) != PDF_DICT) {
77
230
            pdfi_countdown(o1);
78
230
            if (ctx->Root == NULL)
79
85
                return_error(gs_error_typecheck);
80
145
            return 0;
81
230
        }
82
40.1k
    }
83
84
39.9k
    code = pdfi_dict_get_type(ctx, (pdf_dict *)o1, "Type", PDF_NAME, &o);
85
39.9k
    if (code < 0) {
86
322
        bool known = false;
87
88
322
        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
322
        code = pdfi_dict_known(ctx, (pdf_dict *)o1, "Pages", &known);
95
322
        if (code < 0 || known == false) {
96
91
            pdfi_countdown(o1);
97
91
            return code;
98
91
        }
99
322
    }
100
39.5k
    else {
101
39.5k
        if (pdfi_name_strcmp((pdf_name *)o, "Catalog") != 0){
102
705
            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
705
            code = pdfi_dict_get_type(ctx, (pdf_dict *)o1, "Pages", PDF_DICT, &pages);
111
705
            if (code < 0) {
112
374
                pdfi_countdown(o);
113
374
                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
374
                if (ctx->Root == NULL) {
118
84
                    pdfi_set_error(ctx, 0, NULL, E_PDF_NO_ROOT, "pdfi_read_Root", NULL);
119
84
                    return_error(gs_error_syntaxerror);
120
84
                }
121
290
                return 0;
122
374
            }
123
331
            pdfi_countdown(pages);
124
331
            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
331
        }
130
39.2k
        pdfi_countdown(o);
131
39.2k
    }
132
133
39.4k
    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
39.4k
    pdfi_countdown(ctx->Root); /* If file was repaired it might be set already */
139
39.4k
    ctx->Root = (pdf_dict *)o1;
140
39.4k
    return 0;
141
39.9k
}
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.66k
{
147
4.66k
    int code = 0, i = 0;
148
4.66k
    pdf_obj *array_obj = NULL;
149
150
4.66k
    code = pdfi_loop_detector_mark(ctx);
151
4.66k
    if (code < 0)
152
0
        return code;
153
154
48.0k
    for (i = 0;i < pdfi_array_size(a); i++) {
155
44.0k
        code = pdfi_array_fetch_recursing(ctx, a, i, &array_obj, true, true);
156
44.0k
        if (code < 0)
157
337
            goto error;
158
159
43.6k
        switch(pdfi_type_of(array_obj)) {
160
1.08k
            case PDF_DICT:
161
1.08k
                if (array_obj->object_num != 0) {
162
1.04k
                    code = pdfi_loop_detector_add_object(ctx, array_obj->object_num);
163
1.04k
                    if (code < 0)
164
0
                        goto error;
165
1.04k
                }
166
1.08k
                code = Info_check_dict(ctx, (pdf_dict *)array_obj);
167
1.08k
                if (code < 0) {
168
248
                    if (array_obj->object_num != 0) {
169
248
                        pdf_indirect_ref *i_o;
170
171
248
                        code = pdfi_object_alloc(ctx, PDF_INDIRECT, 0, (pdf_obj **)&i_o);
172
248
                        if (code < 0)
173
0
                            goto error;
174
248
                        i_o->ref_generation_num = array_obj->generation_num;
175
248
                        i_o->ref_object_num = array_obj->object_num;
176
248
                        i_o->indirect_num = array_obj->object_num;
177
248
                        i_o->indirect_gen = array_obj->generation_num;
178
248
                        code = pdfi_array_put(ctx, a, i, (pdf_obj *)i_o);
179
248
                        if (code < 0)
180
0
                            return code;
181
248
                    }
182
248
                    goto error;
183
248
                }
184
839
                break;
185
839
            case PDF_ARRAY:
186
246
                if (array_obj->object_num != 0) {
187
50
                    code = pdfi_loop_detector_add_object(ctx, array_obj->object_num);
188
50
                    if (code < 0)
189
0
                        goto error;
190
50
                }
191
246
                code = Info_check_array(ctx, (pdf_array *)array_obj);
192
246
                if (code < 0) {
193
17
                    if (array_obj->object_num != 0) {
194
17
                        pdf_indirect_ref *i_o;
195
196
17
                        code = pdfi_object_alloc(ctx, PDF_INDIRECT, 0, (pdf_obj **)&i_o);
197
17
                        if (code < 0)
198
0
                            goto error;
199
17
                        i_o->ref_generation_num = array_obj->generation_num;
200
17
                        i_o->ref_object_num = array_obj->object_num;
201
17
                        i_o->indirect_num = array_obj->object_num;
202
17
                        i_o->indirect_gen = array_obj->generation_num;
203
17
                        code = pdfi_array_put(ctx, a, i, (pdf_obj *)i_o);
204
17
                        if (code < 0)
205
0
                            return code;
206
17
                    }
207
17
                    goto error;
208
17
                }
209
229
                break;
210
42.3k
            default:
211
42.3k
                break;
212
43.6k
        }
213
214
43.4k
        pdfi_countdown(array_obj);
215
43.4k
        array_obj = NULL;
216
43.4k
    }
217
4.66k
error:
218
4.66k
    pdfi_countdown(array_obj);
219
4.66k
    pdfi_loop_detector_cleartomark(ctx);
220
4.66k
    return code;
221
4.66k
}
222
223
static int Info_check_dict(pdf_context *ctx, pdf_dict *d)
224
3.89k
{
225
3.89k
    int code = 0;
226
3.89k
    uint64_t index = 0;
227
3.89k
    pdf_name *Key = NULL;
228
3.89k
    pdf_obj *Value = NULL;
229
230
3.89k
    code = pdfi_loop_detector_mark(ctx);
231
3.89k
    if (code < 0)
232
0
        return code;
233
234
3.89k
    code = pdfi_dict_first(ctx, d, (pdf_obj **)&Key, &Value, &index);
235
3.89k
    if (code < 0) {
236
453
        if (code == gs_error_undefined)
237
369
            code = 0;
238
453
        goto error;
239
453
    }
240
241
13.9k
    while (code >= 0) {
242
13.7k
        switch(pdfi_type_of(Value)) {
243
2.00k
            case PDF_DICT:
244
2.00k
                if (Value->object_num != 0) {
245
985
                    code = pdfi_loop_detector_add_object(ctx, Value->object_num);
246
985
                    if (code < 0)
247
0
                        goto error;
248
985
                }
249
2.00k
                code = Info_check_dict(ctx, (pdf_dict *)Value);
250
2.00k
                if (code < 0)
251
408
                    goto error;
252
1.60k
                break;
253
2.70k
            case PDF_ARRAY:
254
2.70k
                if (Value->object_num != 0) {
255
149
                    code = pdfi_loop_detector_add_object(ctx, Value->object_num);
256
149
                    if (code < 0)
257
0
                        goto error;
258
149
                }
259
2.70k
                code = Info_check_array(ctx, (pdf_array *)Value);
260
2.70k
                if (code < 0)
261
286
                    goto error;
262
2.41k
                break;
263
9.01k
            default:
264
9.01k
                break;
265
13.7k
        }
266
13.0k
        pdfi_countdown(Key);
267
13.0k
        Key = NULL;
268
13.0k
        pdfi_countdown(Value);
269
13.0k
        Value = NULL;
270
271
13.0k
        code = pdfi_dict_next(ctx, d, (pdf_obj **)&Key, &Value, &index);
272
13.0k
        if (code == gs_error_undefined) {
273
2.51k
            code = 0;
274
2.51k
            break;
275
2.51k
        }
276
13.0k
    }
277
3.89k
error:
278
3.89k
    pdfi_countdown(Key);
279
3.89k
    pdfi_countdown(Value);
280
3.89k
    pdfi_loop_detector_cleartomark(ctx);
281
3.89k
    return code;
282
3.43k
}
283
284
static int pdfi_sanitize_Info_references(pdf_context *ctx, pdf_dict *Info)
285
23.9k
{
286
23.9k
    int code = 0;
287
23.9k
    uint64_t index = 0;
288
23.9k
    pdf_name *Key = NULL;
289
23.9k
    pdf_obj *Value = NULL;
290
291
24.3k
restart_scan:
292
24.3k
    code = pdfi_loop_detector_mark(ctx);
293
24.3k
    if (code < 0)
294
0
        return code;
295
296
24.3k
    code = pdfi_dict_first(ctx, Info, (pdf_obj **)&Key, &Value, &index);
297
24.3k
    if (code == gs_error_undefined) {
298
532
        code = 0;
299
532
        goto error;
300
532
    }
301
302
116k
    while (code >= 0) {
303
116k
        switch(pdfi_type_of(Value)) {
304
795
            case PDF_DICT:
305
795
                code = Info_check_dict(ctx, (pdf_dict *)Value);
306
795
                break;
307
1.71k
            case PDF_ARRAY:
308
1.71k
                code = Info_check_array(ctx, (pdf_array *)Value);
309
1.71k
                break;
310
113k
            default:
311
113k
                code = 0;
312
113k
                break;
313
116k
        }
314
116k
        pdfi_countdown(Value);
315
116k
        Value = NULL;
316
116k
        if (code < 0) {
317
384
            code = pdfi_dict_delete_pair(ctx, Info, Key);
318
384
            if (code < 0)
319
0
                goto error;
320
384
            pdfi_countdown(Key);
321
384
            Key = NULL;
322
323
384
            pdfi_loop_detector_cleartomark(ctx);
324
384
            goto restart_scan;
325
384
        }
326
115k
        pdfi_countdown(Key);
327
115k
        Key = NULL;
328
329
115k
        pdfi_loop_detector_cleartomark(ctx);
330
115k
        code = pdfi_loop_detector_mark(ctx);
331
115k
        if (code < 0) {
332
0
            pdfi_countdown(Key);
333
0
            pdfi_countdown(Value);
334
0
            return code;
335
0
        }
336
337
115k
        code = pdfi_dict_next(ctx, Info, (pdf_obj **)&Key, &Value, &index);
338
115k
        if (code == gs_error_undefined) {
339
23.3k
            code = 0;
340
23.3k
            break;
341
23.3k
        }
342
115k
    }
343
23.9k
error:
344
23.9k
    pdfi_countdown(Key);
345
23.9k
    pdfi_countdown(Value);
346
23.9k
    pdfi_loop_detector_cleartomark(ctx);
347
23.9k
    return code;
348
23.8k
}
349
350
int pdfi_read_Info(pdf_context *ctx)
351
39.9k
{
352
39.9k
    pdf_dict *Info;
353
39.9k
    int code;
354
39.9k
    pdf_dict *d;
355
356
39.9k
    if (ctx->args.pdfdebug)
357
0
        outprintf(ctx->memory, "%% Reading Info dictionary\n");
358
359
    /* See comment in pdfi_read_Root() for details */
360
39.9k
    d = ctx->Trailer;
361
39.9k
    pdfi_countup(d);
362
39.9k
    code = pdfi_dict_get_type(ctx, ctx->Trailer, "Info", PDF_DICT, (pdf_obj **)&Info);
363
39.9k
    pdfi_countdown(d);
364
39.9k
    if (code < 0)
365
15.9k
        return code;
366
367
23.9k
    if (ctx->args.pdfdebug)
368
0
        outprintf(ctx->memory, "\n");
369
370
23.9k
    code = pdfi_loop_detector_mark(ctx);
371
23.9k
    if (code < 0)
372
0
        goto error;
373
23.9k
    if (Info->object_num != 0) {
374
23.9k
        code = pdfi_loop_detector_add_object(ctx, Info->object_num);
375
23.9k
        if (code < 0)
376
0
            goto error1;
377
23.9k
    } 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
23.9k
    code = pdfi_sanitize_Info_references(ctx, Info);
384
23.9k
    if (code < 0)
385
73
        goto error1;
386
387
23.9k
    (void)pdfi_loop_detector_cleartomark(ctx);
388
389
23.9k
    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
23.9k
    ctx->Info = Info;
395
23.9k
    return 0;
396
397
73
error1:
398
73
    pdfi_loop_detector_cleartomark(ctx);
399
73
error:
400
73
    pdfi_countdown(Info);
401
73
    return code;
402
73
}
403
404
int pdfi_read_Pages(pdf_context *ctx)
405
85.2k
{
406
85.2k
    pdf_obj *o, *o1;
407
85.2k
    pdf_array *a = NULL;
408
85.2k
    int code, pagecount = 0;
409
85.2k
    double d;
410
411
85.2k
    if (ctx->args.pdfdebug)
412
0
        outprintf(ctx->memory, "%% Reading Pages dictionary\n");
413
414
85.2k
    code = pdfi_dict_get(ctx, ctx->Root, "Pages", &o1);
415
85.2k
    if (code < 0)
416
2.63k
        return code;
417
418
82.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
82.5k
    } else {
440
82.5k
        if (pdfi_type_of(o1) != PDF_DICT) {
441
148
            pdfi_countdown(o1);
442
148
            return_error(gs_error_typecheck);
443
148
        }
444
82.5k
    }
445
446
82.4k
    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
82.4k
    code = pdfi_dict_get_number(ctx, (pdf_dict *)o1, "Count", &d);
457
82.4k
    if (code < 0) {
458
804
        if (code == gs_error_undefined) {
459
770
            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
770
            code = pdfi_dict_get_type(ctx, (pdf_dict *)o1, "Type", PDF_NAME, (pdf_obj **)&n);
465
770
            if (code == 0) {
466
720
                if(pdfi_name_is(n, "Page")) {
467
593
                    ctx->num_pages = 1;
468
593
                    code = 0;
469
593
                }
470
127
                else
471
127
                    code = gs_error_undefined;
472
720
                pdfi_countdown(n);
473
720
            }
474
770
        }
475
804
        pdfi_countdown(o1);
476
804
        return code;
477
804
    }
478
479
81.6k
    if (floor(d) != d) {
480
0
        pdfi_countdown(o1);
481
0
        return_error(gs_error_rangecheck);
482
81.6k
    } else {
483
81.6k
        ctx->num_pages = (int)floor(d);
484
81.6k
    }
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
81.6k
    code = pdfi_dict_knownget_type(ctx, (pdf_dict *)o1, "Kids", PDF_ARRAY, (pdf_obj **)&a);
492
81.6k
    if (code == 0)
493
32
        code = gs_note_error(gs_error_undefined);
494
81.6k
    if (code < 0) {
495
32
        pdfi_countdown(o1);
496
32
        return code;
497
32
    }
498
499
    /* Firstly check if the Kids array has enough nodes, in which case it's
500
     * probably flat (the common case)
501
     */
502
81.6k
    if (a->size != ctx->num_pages) {
503
1.41k
        int i = 0;
504
1.41k
        pdf_obj *p = NULL, *p1 = NULL;
505
1.41k
        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
10.5k
        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
9.16k
            code = pdfi_array_get_no_store_R(ctx, a, i, &p);
514
9.16k
            if (code < 0)
515
3.91k
                continue;
516
5.25k
            if (pdfi_type_of(p) != PDF_DICT) {
517
1.43k
                pdfi_countdown(p);
518
1.43k
                p = NULL;
519
1.43k
                continue;
520
1.43k
            }
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.81k
            if (p->object_num != 0 && p->object_num == o1->object_num) {
526
12
                pdfi_countdown(p);
527
12
                p = NULL;
528
12
                pdfi_countdown(a);
529
12
                pdfi_countdown(o1);
530
12
                ctx->num_pages = 0;
531
12
                return_error(gs_error_circular_reference);
532
12
            }
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.80k
            (void)pdfi_array_put(ctx, a, i, p);
538
3.80k
            (void)replace_cache_entry(ctx, p);
539
3.80k
            code = pdfi_dict_knownget_type(ctx, (pdf_dict *)p, "Type", PDF_NAME, (pdf_obj **)&p1);
540
3.80k
            if (code <= 0) {
541
64
                pdfi_countdown(p);
542
64
                p = NULL;
543
64
                continue;
544
64
            }
545
3.73k
            if (pdfi_name_is((pdf_name *)p1, "Page")) {
546
2.31k
                pagecount++;
547
2.31k
            } else {
548
1.42k
                if (pdfi_name_is((pdf_name *)p1, "Pages")) {
549
1.29k
                    code = pdfi_dict_knownget(ctx, (pdf_dict *)p, "Count", (pdf_obj **)&c);
550
1.29k
                    if (code >= 0) {
551
1.29k
                        if (pdfi_type_of(c) == PDF_INT)
552
1.28k
                            pagecount += c->value.i;
553
1.29k
                        if (pdfi_type_of(c) == PDF_REAL)
554
0
                            pagecount += (int)c->value.d;
555
1.29k
                        pdfi_countdown(c);
556
1.29k
                        c = NULL;
557
1.29k
                    }
558
1.29k
                }
559
1.42k
            }
560
3.73k
            pdfi_countdown(p1);
561
3.73k
            p1 = NULL;
562
3.73k
            pdfi_countdown(p);
563
3.73k
            p = NULL;
564
3.73k
        }
565
1.41k
    } else
566
80.1k
        pagecount = a->size;
567
568
81.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
81.5k
    if (pagecount != ctx->num_pages) {
580
921
        ctx->num_pages = pagecount;
581
921
        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
921
        code = pdfi_dict_put_int(ctx, (pdf_dict *)o1, "Count", ctx->num_pages);
584
921
        if (code < 0) {
585
0
            pdfi_countdown(o1);
586
0
            return code;
587
0
        }
588
921
    }
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
81.5k
    ctx->PagesTree = (pdf_dict *)o1;
594
81.5k
    return 0;
595
81.5k
}
596
597
/* Read optional things in from Root */
598
void pdfi_read_OptionalRoot(pdf_context *ctx)
599
82.1k
{
600
82.1k
    pdf_obj *obj = NULL;
601
82.1k
    int code;
602
82.1k
    bool known;
603
604
82.1k
    if (ctx->args.pdfdebug)
605
0
        outprintf(ctx->memory, "%% Reading other Root contents\n");
606
607
82.1k
    if (ctx->args.pdfdebug)
608
0
        outprintf(ctx->memory, "%% OCProperties\n");
609
82.1k
    code = pdfi_dict_get_type(ctx, ctx->Root, "OCProperties", PDF_DICT, &obj);
610
82.1k
    if (code == 0) {
611
3.70k
        ctx->OCProperties = (pdf_dict *)obj;
612
78.4k
    } else {
613
78.4k
        ctx->OCProperties = NULL;
614
78.4k
        if (ctx->args.pdfdebug)
615
0
            outprintf(ctx->memory, "%% (None)\n");
616
78.4k
    }
617
618
82.1k
    (void)pdfi_dict_known(ctx, ctx->Root, "Collection", &known);
619
620
82.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
82.1k
}
629
630
void pdfi_free_OptionalRoot(pdf_context *ctx)
631
105k
{
632
105k
    if (ctx->OCProperties) {
633
3.70k
        pdfi_countdown(ctx->OCProperties);
634
3.70k
        ctx->OCProperties = NULL;
635
3.70k
    }
636
105k
    if (ctx->Collection) {
637
0
        pdfi_countdown(ctx->Collection);
638
0
        ctx->Collection = NULL;
639
0
    }
640
105k
}
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
673k
{
645
673k
    pdf_indirect_ref *node = NULL;
646
673k
    pdf_dict *child = NULL;
647
673k
    pdf_name *Type = NULL;
648
673k
    pdf_dict *leaf_dict = NULL;
649
673k
    pdf_name *Key = NULL;
650
673k
    int code = 0;
651
652
673k
    code = pdfi_array_get_no_deref(ctx, Kids, i, (pdf_obj **)&node);
653
673k
    if (code < 0)
654
0
        goto errorExit;
655
656
673k
    if (pdfi_type_of(node) != PDF_INDIRECT && pdfi_type_of(node) != PDF_DICT) {
657
116
        code = gs_note_error(gs_error_typecheck);
658
116
        goto errorExit;
659
116
    }
660
661
673k
    if (pdfi_type_of(node) == PDF_INDIRECT) {
662
149k
        code = pdfi_dereference(ctx, node->ref_object_num, node->ref_generation_num,
663
149k
                                (pdf_obj **)&child);
664
149k
        if (code < 0) {
665
38.0k
            int code1 = pdfi_repair_file(ctx);
666
38.0k
            if (code1 < 0)
667
38.0k
                goto errorExit;
668
34
            code = pdfi_dereference(ctx, node->ref_object_num,
669
34
                                    node->ref_generation_num, (pdf_obj **)&child);
670
34
            if (code < 0)
671
20
                goto errorExit;
672
34
        }
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
111k
        if (pdfi_type_of(child) != PDF_DICT) {
677
1.02k
            pdf_dict *d1 = NULL;
678
679
1.02k
            if (pdfi_type_of(child) != PDF_STREAM) {
680
794
                code = gs_note_error(gs_error_typecheck);
681
794
                goto errorExit;
682
794
            }
683
234
            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
234
            code = pdfi_get_stream_dict(ctx, (pdf_stream *)child, &d1);
687
234
            if (code < 0)
688
0
                goto errorExit;
689
234
            pdfi_countdown(child);
690
234
            child = d1;
691
234
            code = replace_cache_entry(ctx, (pdf_obj *)d1);
692
234
            if (code < 0)
693
0
                goto errorExit;
694
234
        }
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
110k
        code = pdfi_dict_get_type(ctx, child, "Type", PDF_NAME, (pdf_obj **)&Type);
708
110k
        if (code < 0)
709
1.44k
            goto errorExit;
710
109k
        if (pdfi_name_is(Type, "Pages")) {
711
918
            code = pdfi_array_put(ctx, Kids, i, (pdf_obj *)child);
712
918
            if (code < 0)
713
0
                goto errorExit;
714
108k
        } else {
715
            /* Bizarrely, one of the QL FTS files (FTS_07_0704.pdf) has a page diciotnary with a /Type of /Template */
716
108k
            if (!pdfi_name_is(Type, "Page"))
717
854
                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
108k
            code = pdfi_dict_alloc(ctx, 0, &leaf_dict);
725
108k
            if (code < 0)
726
0
                goto errorExit;
727
108k
            code = pdfi_name_alloc(ctx, (byte *)"PageRef", 7, (pdf_obj **)&Key);
728
108k
            if (code < 0)
729
0
                goto errorExit;
730
108k
            pdfi_countup(Key);
731
732
108k
            code = pdfi_dict_put_obj(ctx, leaf_dict, (pdf_obj *)Key, (pdf_obj *)node, true);
733
108k
            if (code < 0)
734
0
                goto errorExit;
735
108k
            code = pdfi_dict_put(ctx, leaf_dict, "Type", (pdf_obj *)Key);
736
108k
            if (code < 0)
737
0
                goto errorExit;
738
108k
            code = pdfi_array_put(ctx, Kids, i, (pdf_obj *)leaf_dict);
739
108k
            if (code < 0)
740
0
                goto errorExit;
741
108k
            leaf_dict = NULL;
742
108k
        }
743
524k
    } else {
744
524k
        if (ctx->loop_detection != NULL) {
745
524k
            if (node->object_num != 0 && pdfi_loop_detector_check_object(ctx, node->object_num)) {
746
107
                code = gs_note_error(gs_error_circular_reference);
747
107
                goto errorExit;
748
107
            }
749
524k
            if (node->object_num > 0) {
750
35.4k
                code = pdfi_loop_detector_add_object(ctx, node->object_num);
751
35.4k
                if (code < 0)
752
0
                    goto errorExit;
753
35.4k
            }
754
524k
        }
755
524k
        child = (pdf_dict *)node;
756
524k
        pdfi_countup(child);
757
524k
    }
758
759
633k
    *pchild = child;
760
633k
    child = NULL;
761
762
673k
 errorExit:
763
673k
    pdfi_free_object((pdf_obj *)leaf_dict);
764
673k
    pdfi_countdown(child);
765
673k
    pdfi_countdown(node);
766
673k
    pdfi_countdown(Type);
767
673k
    pdfi_countdown(Key);
768
673k
    return code;
769
633k
}
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.07M
{
775
1.07M
    int code = 0;
776
1.07M
    pdf_obj *object = NULL;
777
1.07M
    bool known;
778
779
    /* Check for inheritable keys, if we find any copy them to the 'inheritable' dictionary at this level */
780
1.07M
    code = pdfi_dict_known(ctx, d, keyname, &known);
781
1.07M
    if (code < 0)
782
0
        goto exit;
783
1.07M
    if (known) {
784
63.8k
        code = pdfi_loop_detector_mark(ctx);
785
63.8k
        if (code < 0){
786
0
            goto exit;
787
0
        }
788
63.8k
        code = pdfi_dict_get(ctx, d, keyname, &object);
789
63.8k
        if (code < 0) {
790
176
            (void)pdfi_loop_detector_cleartomark(ctx);
791
176
            goto exit;
792
176
        }
793
63.6k
        code = pdfi_loop_detector_cleartomark(ctx);
794
63.6k
        if (code < 0) {
795
0
            goto exit;
796
0
        }
797
63.6k
        code = pdfi_dict_put(ctx, inheritable, keyname, object);
798
63.6k
    }
799
800
1.07M
 exit:
801
1.07M
    pdfi_countdown(object);
802
1.07M
    return code;
803
1.07M
}
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
269k
{
808
269k
    int i, code = 0;
809
269k
    pdf_array *Kids = NULL;
810
269k
    pdf_dict *child = NULL;
811
269k
    pdf_name *Type = NULL;
812
269k
    pdf_dict *inheritable = NULL;
813
269k
    int64_t num;
814
269k
    double dbl;
815
816
269k
    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
269k
    code = pdfi_dict_alloc(ctx, 0, &inheritable);
821
269k
    if (code < 0)
822
0
        return code;
823
269k
    pdfi_countup(inheritable);
824
825
269k
    code = pdfi_loop_detector_mark(ctx);
826
269k
    if (code < 0)
827
0
        return code;
828
829
    /* if we are being passed any inherited values from our parent, copy them now */
830
269k
    if (inherited != NULL) {
831
9.62k
        code = pdfi_dict_copy(ctx, inheritable, inherited);
832
9.62k
        if (code < 0)
833
0
            goto exit;
834
9.62k
    }
835
836
269k
    code = pdfi_dict_get_number(ctx, d, "Count", &dbl);
837
269k
    if (code < 0)
838
0
        goto exit;
839
269k
    if (dbl != floor(dbl)) {
840
0
        code = gs_note_error(gs_error_rangecheck);
841
0
        goto exit;
842
0
    }
843
269k
    num = (int)dbl;
844
845
269k
    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
269k
    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
269k
    code = pdfi_check_inherited_key(ctx, d, "Resources", inheritable);
858
269k
    if (code < 0)
859
176
        goto exit;
860
269k
    code = pdfi_check_inherited_key(ctx, d, "MediaBox", inheritable);
861
269k
    if (code < 0)
862
0
        goto exit;
863
269k
    code = pdfi_check_inherited_key(ctx, d, "CropBox", inheritable);
864
269k
    if (code < 0)
865
0
        goto exit;
866
269k
    code = pdfi_check_inherited_key(ctx, d, "Rotate", inheritable);
867
269k
    if (code < 0) {
868
0
        goto exit;
869
0
    }
870
871
    /* Get the Kids array */
872
269k
    code = pdfi_dict_get_type(ctx, d, "Kids", PDF_ARRAY, (pdf_obj **)&Kids);
873
269k
    if (code < 0) {
874
32
        goto exit;
875
32
    }
876
877
    /* Check each entry in the Kids array */
878
673k
    for (i = 0;i < pdfi_array_size(Kids);i++) {
879
673k
        pdfi_countdown(child);
880
673k
        child = NULL;
881
673k
        pdfi_countdown(Type);
882
673k
        Type = NULL;
883
884
673k
        code = pdfi_get_child(ctx, Kids, i, &child);
885
673k
        if (code < 0) {
886
40.5k
            goto exit;
887
40.5k
        }
888
889
        /* Check the type, if its a Pages entry, then recurse. If its a Page entry, is it the one we want */
890
633k
        code = pdfi_dict_get_type(ctx, child, "Type", PDF_NAME, (pdf_obj **)&Type);
891
633k
        if (code == 0) {
892
633k
            if (pdfi_name_is(Type, "Pages")) {
893
15.0k
                code = pdfi_dict_get_number(ctx, child, "Count", &dbl);
894
15.0k
                if (code == 0) {
895
14.9k
                    if (dbl != floor(dbl)) {
896
0
                        code = gs_note_error(gs_error_rangecheck);
897
0
                        goto exit;
898
0
                    }
899
14.9k
                    num = (int)dbl;
900
14.9k
                    if (num < 0 || (num + *page_offset) > ctx->num_pages) {
901
21
                        code = gs_note_error(gs_error_rangecheck);
902
21
                        goto exit;
903
14.9k
                    } else {
904
14.9k
                        if (num + *page_offset <= page_num) {
905
5.32k
                            *page_offset += num;
906
9.62k
                        } else {
907
9.62k
                            code = pdfi_get_page_dict(ctx, child, page_num, page_offset, target, inheritable);
908
9.62k
                            goto exit;
909
9.62k
                        }
910
14.9k
                    }
911
14.9k
                }
912
618k
            } else {
913
618k
                if (pdfi_name_is(Type, "PageRef")) {
914
488k
                    if ((*page_offset) == page_num) {
915
108k
                        pdf_dict *page_dict = NULL;
916
917
108k
                        code = pdfi_dict_get_no_store_R(ctx, child, "PageRef", (pdf_obj **)&page_dict);
918
108k
                        if (code < 0)
919
5
                            goto exit;
920
108k
                        if (pdfi_type_of(page_dict) != PDF_DICT) {
921
0
                            code = gs_note_error(gs_error_typecheck);
922
0
                            goto exit;
923
0
                        }
924
108k
                        code = pdfi_merge_dicts(ctx, page_dict, inheritable);
925
108k
                        *target = page_dict;
926
108k
                        pdfi_countup(*target);
927
108k
                        pdfi_countdown(page_dict);
928
108k
                        goto exit;
929
380k
                    } else {
930
380k
                        *page_offset += 1;
931
380k
                    }
932
488k
                } else {
933
129k
                    if (!pdfi_name_is(Type, "Page")) {
934
1.15k
                        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.15k
                    }
937
129k
                    if ((*page_offset) == page_num) {
938
110k
                        code = pdfi_merge_dicts(ctx, child, inheritable);
939
110k
                        *target = child;
940
110k
                        pdfi_countup(*target);
941
110k
                        goto exit;
942
110k
                    } else {
943
18.6k
                        *page_offset += 1;
944
18.6k
                    }
945
129k
                }
946
618k
            }
947
633k
        }
948
404k
        if (code < 0)
949
178
            goto exit;
950
404k
    }
951
    /* Positive return value indicates we did not find the target below this node, try the next one */
952
38
    code = 1;
953
954
269k
 exit:
955
269k
    pdfi_loop_detector_cleartomark(ctx);
956
269k
    pdfi_countdown(inheritable);
957
269k
    pdfi_countdown(Kids);
958
269k
    pdfi_countdown(child);
959
269k
    pdfi_countdown(Type);
960
269k
    return code;
961
38
}
962
963
int pdfi_doc_page_array_init(pdf_context *ctx)
964
82.1k
{
965
82.1k
    size_t size = ctx->num_pages*sizeof(uint32_t);
966
967
82.1k
    ctx->page_array = (uint32_t *)gs_alloc_bytes(ctx->memory, size,
968
82.1k
                                                 "pdfi_doc_page_array_init(page_array)");
969
82.1k
    if (ctx->page_array == NULL)
970
1
        return_error(gs_error_VMerror);
971
972
82.1k
    memset(ctx->page_array, 0, size);
973
82.1k
    return 0;
974
82.1k
}
975
976
void pdfi_doc_page_array_free(pdf_context *ctx)
977
105k
{
978
105k
    if (!ctx->page_array)
979
23.1k
        return;
980
82.1k
    gs_free_object(ctx->memory, ctx->page_array, "pdfi_doc_page_array_free(page_array)");
981
82.1k
    ctx->page_array = NULL;
982
82.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.33M
{
992
5.33M
    int code;
993
5.33M
    pdf_dict *Resources = NULL;
994
995
5.33M
    code = pdfi_dict_knownget_type(ctx, dict, "Resources", PDF_DICT, (pdf_obj **)&Resources);
996
5.33M
    if (code == 0)
997
2.20M
        code = pdfi_dict_knownget_type(ctx, dict, "DR", PDF_DICT, (pdf_obj **)&Resources);
998
5.33M
    if (code < 0)
999
188k
        goto exit;
1000
5.14M
    if (code > 0)
1001
2.93M
        code = pdfi_dict_knownget_type(ctx, Resources, (const char *)Type, PDF_DICT, (pdf_obj **)typedict);
1002
5.33M
 exit:
1003
5.33M
    pdfi_countdown(Resources);
1004
5.33M
    return code;
1005
5.14M
}
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.36M
{
1010
2.36M
    pdf_dict *typedict = NULL;
1011
2.36M
    pdf_dict *Parent = NULL;
1012
2.36M
    pdf_name *n = NULL;
1013
2.36M
    int code;
1014
2.36M
    bool known = false;
1015
1016
2.36M
    *o = NULL;
1017
1018
    /* Check the provided dict, stream_dict can be NULL if we are trying to find a Default* ColorSpace */
1019
2.36M
    if (dict != NULL) {
1020
2.36M
        bool deref_parent = true, dict_is_XObject = false;
1021
1022
2.36M
        code = pdfi_resource_knownget_typedict(ctx, Type, dict, &typedict);
1023
2.36M
        if (code < 0)
1024
5.05k
            goto exit;
1025
2.36M
        if (code > 0) {
1026
430k
            code = pdfi_dict_get_no_store_R_key(ctx, typedict, name, o);
1027
430k
            if (code != gs_error_undefined)
1028
339k
                goto exit;
1029
430k
        }
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
2.02M
        if (pdfi_dict_knownget_type(ctx, dict, "Type", PDF_NAME, (pdf_obj **)&n) > 0) {
1036
102k
            if (pdfi_name_is(n, "Page"))
1037
7
                deref_parent = false;
1038
102k
            if (pdfi_name_is(n, "XObject"))
1039
83.3k
                dict_is_XObject = true;
1040
102k
            pdfi_countdown(n);
1041
102k
        }
1042
1043
2.02M
        if (deref_parent) {
1044
2.02M
            code = pdfi_dict_known(ctx, dict, "Parent", &known);
1045
2.02M
            if (code >= 0 && known == true) {
1046
88.0k
                code = pdfi_dict_get_no_store_R(ctx, dict, "Parent", (pdf_obj **)&Parent);
1047
1048
88.0k
                if (code >= 0) {
1049
81.6k
                    if (pdfi_type_of(Parent) != PDF_DICT) {
1050
141
                        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
141
                        } else {
1061
141
                            pdfi_countdown(Parent);
1062
141
                            Parent = NULL;
1063
141
                        }
1064
141
                    }
1065
81.6k
                } else
1066
6.40k
                    Parent = NULL;
1067
88.0k
            }
1068
1069
2.02M
            if (Parent != NULL) {
1070
81.5k
                if (ctx->page.CurrentPageDict != NULL && Parent->object_num != ctx->page.CurrentPageDict->object_num) {
1071
81.5k
                    if (pdfi_loop_detector_check_object(ctx, Parent->object_num) == true) {
1072
11.1k
                        code = gs_note_error(gs_error_circular_reference);
1073
11.1k
                        goto exit;
1074
11.1k
                    }
1075
1076
70.4k
                    code = pdfi_loop_detector_mark(ctx);
1077
70.4k
                    if (code < 0)
1078
0
                        goto exit;
1079
1080
70.4k
                    code = pdfi_loop_detector_add_object(ctx, dict->object_num);
1081
70.4k
                    if (code < 0) {
1082
0
                        (void)pdfi_loop_detector_cleartomark(ctx);
1083
0
                        goto exit;
1084
0
                    }
1085
70.4k
                    code = pdfi_find_resource(ctx, Type, name, Parent, page_dict, o);
1086
70.4k
                    (void)pdfi_loop_detector_cleartomark(ctx);
1087
70.4k
                    if (code != gs_error_undefined) {
1088
561
                        if (dict_is_XObject)
1089
519
                            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
561
                        goto exit;
1091
561
                    }
1092
70.4k
                }
1093
81.5k
            }
1094
2.01M
            code = 0;
1095
2.01M
        }
1096
2.01M
        pdfi_countdown(typedict);
1097
2.01M
        typedict = NULL;
1098
2.01M
    }
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
2.01M
    if (page_dict != NULL) {
1111
2.01M
        code = pdfi_resource_knownget_typedict(ctx, Type, page_dict, &typedict);
1112
2.01M
        if (code < 0)
1113
209k
            goto exit;
1114
1115
1.80M
        if (code > 0) {
1116
1.66M
            code = pdfi_dict_get_no_store_R_key(ctx, typedict, name, o);
1117
1.66M
            if (code != gs_error_undefined)
1118
1.26M
                goto exit;
1119
1.66M
        }
1120
1.80M
    }
1121
1122
541k
    pdfi_countdown(typedict);
1123
541k
    typedict = NULL;
1124
1125
541k
    if (ctx->page.CurrentPageDict != NULL) {
1126
541k
        code = pdfi_resource_knownget_typedict(ctx, Type, ctx->page.CurrentPageDict, &typedict);
1127
541k
        if (code < 0)
1128
164
            goto exit;
1129
1130
541k
        if (code > 0) {
1131
418k
            code = pdfi_dict_get_no_store_R_key(ctx, typedict, name, o);
1132
418k
            if (code != gs_error_undefined)
1133
4.49k
                goto exit;
1134
418k
        }
1135
541k
    }
1136
1137
536k
    pdfi_countdown(typedict);
1138
536k
    typedict = NULL;
1139
1140
536k
    if (ctx->current_stream != NULL) {
1141
264k
        pdf_dict *stream_dict = NULL;
1142
264k
        pdf_stream *stream = ctx->current_stream;
1143
1144
411k
        do {
1145
411k
            code = pdfi_dict_from_obj(ctx, (pdf_obj *)stream, &stream_dict);
1146
411k
            if (code < 0)
1147
0
                goto exit;
1148
411k
            code = pdfi_resource_knownget_typedict(ctx, Type, stream_dict, &typedict);
1149
411k
            if (code < 0)
1150
0
                goto exit;
1151
411k
            if (code > 0) {
1152
150k
                code = pdfi_dict_get_no_store_R_key(ctx, typedict, name, o);
1153
150k
                if (code == 0) {
1154
14
                    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
14
                    goto exit;
1156
14
                }
1157
150k
            }
1158
411k
            pdfi_countdown(typedict);
1159
411k
            typedict = NULL;
1160
411k
            stream = pdfi_stream_parent(ctx, stream);
1161
411k
        }while(stream != NULL);
1162
264k
    }
1163
1164
    /* If we got all the way down there, we didn't find it */
1165
536k
    pdfi_set_warning(ctx, 0, NULL, W_PDF_MISSING_NAMED_RESOURCE, "pdfi_find_resource", NULL);
1166
536k
    code = gs_error_undefined;
1167
1168
2.36M
exit:
1169
2.36M
    pdfi_countdown(typedict);
1170
2.36M
    pdfi_countdown(Parent);
1171
2.36M
    return code;
1172
536k
}
1173
1174
/* Mark the actual outline */
1175
static int pdfi_doc_mark_the_outline(pdf_context *ctx, pdf_dict *outline)
1176
663
{
1177
663
    int code = 0;
1178
663
    pdf_dict *tempdict = NULL;
1179
663
    uint64_t dictsize;
1180
663
    uint64_t index;
1181
663
    pdf_name *Key = NULL;
1182
663
    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
663
    dictsize = pdfi_dict_entries(outline);
1190
663
    code = pdfi_dict_alloc(ctx, dictsize, &tempdict);
1191
663
    if (code < 0) goto exit;
1192
663
    pdfi_countup(tempdict);
1193
663
    code = pdfi_dict_copy(ctx, tempdict, outline);
1194
663
    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
663
    code = pdfi_dict_knownget_number(ctx, outline, "Count", &num);
1205
663
    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
663
    {
1213
663
        pdf_dict *current = NULL, *next = NULL;
1214
663
        int count = 0, code1;
1215
1216
663
        code1 = pdfi_dict_knownget_type(ctx, outline, "First", PDF_DICT, (pdf_obj **)&current);
1217
663
        if (code1 > 0) {
1218
70
            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
70
            count++;
1223
135
            do {
1224
135
                code1 = pdfi_dict_knownget_type(ctx, current, "Next", PDF_DICT, (pdf_obj **)&next);
1225
135
                if (code1 > 0) {
1226
65
                    pdfi_countdown(current);
1227
65
                    current = next;
1228
65
                    next = NULL;
1229
65
                    count++;
1230
65
                } else
1231
70
                    break;
1232
135
            } while (1);
1233
70
            pdfi_countdown(current);
1234
70
        }
1235
663
        if (num <= 0)
1236
592
            count *= -1;
1237
663
        pdfi_dict_put_int(ctx, tempdict, "Count", count);
1238
663
    }
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.64k
    while (code >= 0) {
1244
2.64k
        if (pdfi_name_is(Key, "Last") || pdfi_name_is(Key, "Next") || pdfi_name_is(Key, "First") ||
1245
2.33k
            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.09k
            code = pdfi_dict_delete_pair(ctx, tempdict, Key);
1250
1.55k
        } 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.54k
        } else if (pdfi_name_is(Key, "A")) {
1258
360
            code = pdfi_pdfmark_modA(ctx, tempdict);
1259
1.18k
        } else if (pdfi_name_is(Key, "Dest")) {
1260
300
            code = pdfi_pdfmark_modDest(ctx, tempdict);
1261
300
        }
1262
2.64k
        if (code < 0)
1263
90
            goto exit;
1264
1265
2.55k
        pdfi_countdown(Key);
1266
2.55k
        Key = NULL;
1267
1268
2.55k
        code = pdfi_dict_key_next(ctx, outline, (pdf_obj **)&Key, &index);
1269
2.55k
        if (code == gs_error_undefined) {
1270
573
            code = 0;
1271
573
            break;
1272
573
        }
1273
2.55k
    }
1274
573
    if (code < 0) goto exit;
1275
1276
    /* Write the pdfmark */
1277
573
    code = pdfi_pdfmark_from_dict(ctx, tempdict, NULL, "OUT");
1278
573
    if (code < 0)
1279
2
        goto exit;
1280
1281
663
 exit:
1282
663
    pdfi_countdown(tempdict);
1283
663
    pdfi_countdown(Key);
1284
663
    return code;
1285
573
}
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
663
{
1293
663
    int code = 0;
1294
663
    pdf_dict *child = NULL;
1295
663
    pdf_dict *Next = NULL;
1296
1297
663
    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
663
    code = pdfi_doc_mark_the_outline(ctx, outline);
1305
663
    if (code < 0)
1306
92
        goto exit1;
1307
1308
    /* Handle the children */
1309
571
    code = pdfi_loop_detector_mark(ctx);
1310
571
    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
571
    code = pdfi_dict_get_no_store_R(ctx, outline, "First", (pdf_obj **)&child);
1315
571
    if (code < 0 || pdfi_type_of(child) != PDF_DICT) {
1316
        /* TODO: flag a warning? */
1317
515
        code = 0;
1318
515
        goto exit;
1319
515
    }
1320
1321
56
    if (child->object_num != 0) {
1322
56
        code = pdfi_loop_detector_add_object(ctx, child->object_num);
1323
56
        if (code < 0)
1324
0
            goto exit;
1325
56
    }
1326
1327
102
    do {
1328
102
        code = pdfi_doc_mark_outline(ctx, child);
1329
102
        if (code < 0) goto exit;
1330
1331
1332
93
        code = pdfi_dict_get_no_store_R(ctx, child, "Next", (pdf_obj **)&Next);
1333
93
        if (code == gs_error_undefined) {
1334
46
            code = 0;
1335
46
            break;
1336
46
        }
1337
47
        if (code == gs_error_circular_reference) {
1338
0
            code = 0;
1339
0
            goto exit;
1340
0
        }
1341
47
        if (code < 0 || pdfi_type_of(Next) != PDF_DICT)
1342
1
            goto exit;
1343
1344
46
        pdfi_countdown(child);
1345
46
        child = Next;
1346
46
        Next = NULL;
1347
46
    } while (true);
1348
1349
571
 exit:
1350
571
    (void)pdfi_loop_detector_cleartomark(ctx);
1351
663
 exit1:
1352
663
    pdfi_countdown(child);
1353
663
    pdfi_countdown(Next);
1354
663
    return code;
1355
571
}
1356
1357
/* Do pdfmark for Outlines */
1358
static int pdfi_doc_Outlines(pdf_context *ctx)
1359
8.25k
{
1360
8.25k
    int code = 0;
1361
8.25k
    pdf_dict *Outlines = NULL;
1362
8.25k
    pdf_dict *outline = NULL;
1363
8.25k
    pdf_dict *Next = NULL;
1364
1365
8.25k
    if (ctx->args.no_pdfmark_outlines)
1366
0
        goto exit1;
1367
1368
8.25k
    code = pdfi_dict_knownget_type(ctx, ctx->Root, "Outlines", PDF_DICT, (pdf_obj **)&Outlines);
1369
8.25k
    if (code <= 0) {
1370
        /* TODO: flag a warning */
1371
7.67k
        code = 0;
1372
7.67k
        goto exit1;
1373
7.67k
    }
1374
1375
571
    code = pdfi_loop_detector_mark(ctx);
1376
571
    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
571
    code = pdfi_dict_get_no_store_R(ctx, Outlines, "First", (pdf_obj **)&outline);
1381
571
    if (code < 0 || pdfi_type_of(outline) != PDF_DICT) {
1382
        /* TODO: flag a warning? */
1383
129
        code = 0;
1384
129
        goto exit;
1385
129
    }
1386
1387
442
    if (pdfi_type_of(outline) != PDF_DICT)
1388
0
        goto exit; /* Exit with no error? */
1389
1390
442
    if (outline->object_num != 0) {
1391
442
        code = pdfi_loop_detector_add_object(ctx, outline->object_num);
1392
442
        if (code < 0)
1393
0
            goto exit;
1394
442
    }
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
561
    do {
1402
561
        code = pdfi_doc_mark_outline(ctx, outline);
1403
561
        if (code < 0) goto exit;
1404
1405
1406
468
        code = pdfi_dict_get_no_store_R(ctx, outline, "Next", (pdf_obj **)&Next);
1407
468
        if (code == gs_error_undefined) {
1408
346
            code = 0;
1409
346
            break;
1410
346
        }
1411
122
        if (code == gs_error_circular_reference) {
1412
0
            code = 0;
1413
0
            goto exit;
1414
0
        }
1415
122
        if (code < 0 || pdfi_type_of(Next) != PDF_DICT)
1416
3
            goto exit;
1417
1418
119
        pdfi_countdown(outline);
1419
119
        outline = Next;
1420
119
        Next = NULL;
1421
119
    } while (true);
1422
1423
571
 exit:
1424
571
    (void)pdfi_loop_detector_cleartomark(ctx);
1425
8.25k
 exit1:
1426
8.25k
    pdfi_countdown(Outlines);
1427
8.25k
    pdfi_countdown(outline);
1428
8.25k
    pdfi_countdown(Next);
1429
8.25k
    return code;
1430
571
}
1431
1432
/* Do pdfmark for Info */
1433
static int pdfi_doc_Info(pdf_context *ctx)
1434
8.25k
{
1435
8.25k
    int code = 0;
1436
8.25k
    pdf_dict *Info = NULL, *d = NULL;
1437
8.25k
    pdf_dict *tempdict = NULL;
1438
8.25k
    uint64_t dictsize;
1439
8.25k
    uint64_t index;
1440
8.25k
    pdf_name *Key = NULL;
1441
8.25k
    pdf_obj *Value = NULL;
1442
1443
    /* See comment in pdfi_read_Root() for details */
1444
8.25k
    d = ctx->Trailer;
1445
8.25k
    pdfi_countup(d);
1446
8.25k
    code = pdfi_dict_knownget_type(ctx, d, "Info", PDF_DICT, (pdf_obj **)&Info);
1447
8.25k
    pdfi_countdown(d);
1448
8.25k
    if (code <= 0) {
1449
        /* TODO: flag a warning */
1450
6.03k
        goto exit;
1451
6.03k
    }
1452
1453
    /* Make a temporary copy of the Info dict */
1454
2.21k
    dictsize = pdfi_dict_entries(Info);
1455
2.21k
    code = pdfi_dict_alloc(ctx, dictsize, &tempdict);
1456
2.21k
    if (code < 0) goto exit;
1457
2.21k
    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.21k
    code = pdfi_dict_first(ctx, Info, (pdf_obj **)&Key, &Value, &index);
1463
11.2k
    while (code >= 0) {
1464
11.1k
        if (pdfi_name_is(Key, "Author") || pdfi_name_is(Key, "Creator") ||
1465
8.44k
            pdfi_name_is(Key, "Title") || pdfi_name_is(Key, "Subject") ||
1466
7.27k
            pdfi_name_is(Key, "Keywords")) {
1467
1468
4.29k
            code = pdfi_dict_put_obj(ctx, tempdict, (pdf_obj *)Key, Value, true);
1469
4.29k
            if (code < 0)
1470
0
                goto exit;
1471
4.29k
        }
1472
11.1k
        pdfi_countdown(Key);
1473
11.1k
        Key = NULL;
1474
11.1k
        pdfi_countdown(Value);
1475
11.1k
        Value = NULL;
1476
1477
11.1k
        code = pdfi_dict_next(ctx, Info, (pdf_obj **)&Key, &Value, &index);
1478
11.1k
        if (code == gs_error_undefined) {
1479
2.17k
            code = 0;
1480
2.17k
            break;
1481
2.17k
        }
1482
11.1k
    }
1483
2.21k
    if (code < 0) goto exit;
1484
1485
    /* Write the pdfmark */
1486
2.17k
    code = pdfi_pdfmark_from_dict(ctx, tempdict, NULL, "DOCINFO");
1487
1488
8.25k
 exit:
1489
8.25k
    pdfi_countdown(Key);
1490
8.25k
    pdfi_countdown(Value);
1491
8.25k
    pdfi_countdown(Info);
1492
8.25k
    pdfi_countdown(tempdict);
1493
8.25k
    return code;
1494
2.17k
}
1495
1496
/* Handle PageLabels for pdfwrite device */
1497
static int pdfi_doc_PageLabels(pdf_context *ctx)
1498
82.1k
{
1499
82.1k
    int code;
1500
82.1k
    pdf_dict *PageLabels = NULL;
1501
1502
82.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
82.1k
    code = pdfi_dict_knownget_type(ctx, ctx->Root, "PageLabels", PDF_DICT, (pdf_obj **)&PageLabels);
1509
82.1k
    if (code <= 0) {
1510
79.0k
        if (ctx->loop_detection)
1511
0
            (void)pdfi_loop_detector_cleartomark(ctx);
1512
        /* TODO: flag a warning */
1513
79.0k
        goto exit;
1514
79.0k
    }
1515
1516
3.12k
    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
3.12k
    if (ctx->device_state.WantsPageLabels) {
1523
        /* This will send the PageLabels object as a 'pdfpagelabels' setdeviceparams */
1524
485
        code = pdfi_pdfmark_object(ctx, (pdf_obj *)PageLabels, "pdfpagelabels");
1525
485
        if (code < 0)
1526
33
            goto exit;
1527
485
    }
1528
1529
82.1k
 exit:
1530
82.1k
    pdfi_countdown(PageLabels);
1531
82.1k
    return code;
1532
3.12k
}
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
82.1k
{
1539
82.1k
    int code;
1540
82.1k
    pdf_array *OutputIntents = NULL;
1541
82.1k
    pdf_dict *intent = NULL;
1542
82.1k
    pdf_string *name = NULL;
1543
82.1k
    pdf_stream *DestOutputProfile = NULL;
1544
82.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
82.1k
    code = pdfi_dict_knownget_type(ctx, ctx->Root, "OutputIntents", PDF_ARRAY,
1551
82.1k
                                   (pdf_obj **)&OutputIntents);
1552
82.1k
    if (code <= 0) {
1553
81.6k
        goto exit;
1554
81.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
486
    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
486
    } 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
486
    } else {
1604
        /* No command line arg was specified, so nothing to do */
1605
486
        code = 0;
1606
486
        goto exit;
1607
486
    }
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
82.1k
 exit:
1622
82.1k
    pdfi_countdown(OutputIntents);
1623
82.1k
    pdfi_countdown(intent);
1624
82.1k
    pdfi_countdown(name);
1625
82.1k
    pdfi_countdown(DestOutputProfile);
1626
82.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
34
{
1632
34
    int code;
1633
34
    uint64_t arraysize;
1634
34
    uint64_t index;
1635
34
    pdf_string *name = NULL;
1636
34
    pdf_dict *filespec = NULL;
1637
1638
34
    arraysize = pdfi_array_size(names);
1639
34
    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
56
    for (index = 0; index < arraysize; index += 2) {
1648
33
        code = pdfi_array_get_type(ctx, names, index, PDF_STRING, (pdf_obj **)&name);
1649
33
        if (code < 0) goto exit;
1650
1651
33
        code = pdfi_array_get_type(ctx, names, index+1, PDF_DICT, (pdf_obj **)&filespec);
1652
33
        if (code < 0) goto exit;
1653
1654
29
        code = pdfi_pdfmark_embed_filespec(ctx, name, filespec);
1655
29
        if (code < 0) goto exit;
1656
1657
23
        pdfi_countdown(name);
1658
23
        name = NULL;
1659
23
        pdfi_countdown(filespec);
1660
23
        filespec = NULL;
1661
23
    }
1662
1663
1664
34
 exit:
1665
34
    pdfi_countdown(name);
1666
34
    pdfi_countdown(filespec);
1667
34
    return code;
1668
33
}
1669
1670
/* Handle PageLabels for pdfwrite device */
1671
static int pdfi_doc_EmbeddedFiles(pdf_context *ctx)
1672
8.25k
{
1673
8.25k
    int code;
1674
8.25k
    pdf_dict *Names = NULL;
1675
8.25k
    pdf_dict *EmbeddedFiles = NULL;
1676
8.25k
    pdf_array *Names_array = NULL;
1677
8.25k
    pdf_array *Kids = NULL;
1678
1679
8.25k
    code = pdfi_dict_knownget_type(ctx, ctx->Root, "Collection", PDF_DICT, (pdf_obj **)&Names);
1680
8.25k
    if (code < 0) goto exit;
1681
8.25k
    if (code > 0) {
1682
0
        code = 0;
1683
0
        goto exit;
1684
0
    }
1685
1686
8.25k
    code = pdfi_dict_knownget_type(ctx, ctx->Root, "Names", PDF_DICT, (pdf_obj **)&Names);
1687
8.25k
    if (code <= 0) goto exit;
1688
1689
794
    code = pdfi_dict_knownget_type(ctx, Names, "EmbeddedFiles", PDF_DICT, (pdf_obj **)&EmbeddedFiles);
1690
794
    if (code <= 0) goto exit;
1691
1692
34
    code = pdfi_dict_knownget_type(ctx, Names, "Kids", PDF_ARRAY, (pdf_obj **)&Kids);
1693
34
    if (code < 0) goto exit;
1694
34
    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
34
    code = pdfi_dict_knownget_type(ctx, EmbeddedFiles, "Names", PDF_ARRAY, (pdf_obj **)&Names_array);
1704
34
    if (code <= 0) goto exit;
1705
1706
34
    code = pdfi_doc_EmbeddedFiles_Names(ctx, Names_array);
1707
34
    if (code <= 0) goto exit;
1708
1709
8.25k
 exit:
1710
8.25k
    pdfi_countdown(Kids);
1711
8.25k
    pdfi_countdown(Names);
1712
8.25k
    pdfi_countdown(EmbeddedFiles);
1713
8.25k
    pdfi_countdown(Names_array);
1714
8.25k
    return code;
1715
34
}
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
82.1k
{
1727
82.1k
    int code = 0;
1728
82.1k
    pdf_dict *AcroForm = NULL;
1729
82.1k
    bool boolval = false;
1730
1731
82.1k
    code = pdfi_dict_knownget_type(ctx, ctx->Root, "AcroForm", PDF_DICT, (pdf_obj **)&AcroForm);
1732
82.1k
    if (code <= 0) goto exit;
1733
1734
6.64k
    code = pdfi_dict_get_bool(ctx, AcroForm, "NeedAppearances", &boolval);
1735
6.64k
    if (code < 0) {
1736
6.05k
        if (code == gs_error_undefined) {
1737
6.05k
            boolval = true;
1738
6.05k
            code = 0;
1739
6.05k
        }
1740
2
        else
1741
2
            goto exit;
1742
6.05k
    }
1743
6.64k
    ctx->NeedAppearances = boolval;
1744
1745
    /* Save this for efficiency later */
1746
6.64k
    ctx->AcroForm = AcroForm;
1747
6.64k
    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
82.1k
 exit:
1755
82.1k
    pdfi_countdown(AcroForm);
1756
82.1k
    return code;
1757
6.64k
}
1758
1759
1760
static int pdfi_doc_view(pdf_context *ctx)
1761
8.25k
{
1762
8.25k
    int code = 0;
1763
8.25k
    pdf_dict *tempdict = NULL;
1764
8.25k
    pdf_name *Mode = NULL, *Layout = NULL;
1765
8.25k
    pdf_obj *ActionDest = NULL;
1766
1767
8.25k
    code = pdfi_dict_knownget_type(ctx, ctx->Root, "PageMode", PDF_NAME, (pdf_obj **)&Mode);
1768
8.25k
    if (code < 0)
1769
0
        return code;
1770
1771
8.25k
    if (code != 0) {
1772
602
        code = pdfi_dict_alloc(ctx, 1, &tempdict);
1773
602
        if (code < 0)
1774
0
            goto exit;
1775
1776
602
        pdfi_countup(tempdict);
1777
1778
602
        code = pdfi_dict_put(ctx, tempdict, "PageMode", (pdf_obj *)Mode);
1779
602
        if (code < 0)
1780
0
            goto exit;
1781
1782
602
        code = pdfi_pdfmark_from_dict(ctx, tempdict, NULL, "DOCVIEW");
1783
602
        if (code < 0)
1784
0
            goto exit;
1785
602
        pdfi_countdown(tempdict);
1786
602
        tempdict = NULL;
1787
602
    }
1788
1789
8.25k
    code = pdfi_dict_knownget_type(ctx, ctx->Root, "PageLayout", PDF_NAME, (pdf_obj **)&Layout);
1790
8.25k
    if (code < 0)
1791
0
        goto exit;
1792
1793
8.25k
    if (code != 0) {
1794
530
        code = pdfi_dict_alloc(ctx, 1, &tempdict);
1795
530
        if (code < 0)
1796
0
            goto exit;
1797
1798
530
        pdfi_countup(tempdict);
1799
1800
530
        code = pdfi_dict_put(ctx, tempdict, "PageLayout", (pdf_obj *)Layout);
1801
530
        if (code < 0)
1802
0
            goto exit;
1803
1804
530
        code = pdfi_pdfmark_from_dict(ctx, tempdict, NULL, "DOCVIEW");
1805
530
        if (code < 0)
1806
0
            goto exit;
1807
530
        pdfi_countdown(tempdict);
1808
530
        tempdict = NULL;
1809
530
    }
1810
1811
8.25k
    code = pdfi_dict_knownget(ctx, ctx->Root, "OpenAction", &ActionDest);
1812
8.25k
    if (code < 0)
1813
29
        goto exit;
1814
1815
8.22k
    if (code != 0) {
1816
456
        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
435
        } else {
1833
435
            if (pdfi_type_of(ActionDest) == PDF_ARRAY) {
1834
                /* Array means this is a destination */
1835
435
                code = pdfi_dict_alloc(ctx, 1, &tempdict);
1836
435
                if (code < 0)
1837
0
                    goto exit;
1838
1839
435
                pdfi_countup(tempdict);
1840
1841
435
                code = pdfi_dict_put(ctx, tempdict, "Dest", (pdf_obj *)ActionDest);
1842
435
                if (code < 0)
1843
0
                    goto exit;
1844
435
                code = pdfi_pdfmark_modDest(ctx, tempdict);
1845
435
                if (code < 0)
1846
54
                    goto exit;
1847
381
                code = pdfi_pdfmark_from_dict(ctx, tempdict, NULL, "DOCVIEW");
1848
381
                if (code < 0)
1849
0
                    goto exit;
1850
381
            } else {
1851
0
                code = gs_note_error(gs_error_typecheck);
1852
0
                goto exit;
1853
0
            }
1854
435
        }
1855
456
    }
1856
1857
8.25k
exit:
1858
8.25k
    pdfi_countdown(ActionDest);
1859
8.25k
    pdfi_countdown(Layout);
1860
8.25k
    pdfi_countdown(Mode);
1861
8.25k
    pdfi_countdown(tempdict);
1862
8.25k
    return code;
1863
8.22k
}
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
82.1k
{
1872
82.1k
    int code = 0;
1873
1874
    /* Can't do this stuff with no Trailer */
1875
82.1k
    if (!ctx->Trailer) {
1876
43.0k
        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
43.0k
    }
1879
1880
82.1k
    if (ctx->device_state.writepdfmarks) {
1881
        /* Handle Outlines */
1882
8.25k
        code = pdfi_doc_Outlines(ctx);
1883
8.25k
        if (code < 0) {
1884
94
            if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_OUTLINES, "pdfi_doc_trailer", NULL)) < 0)
1885
0
                goto exit;
1886
94
        }
1887
1888
        /* Handle Docview pdfmark stuff */
1889
8.25k
        if (ctx->args.preservedocview) {
1890
8.25k
            code = pdfi_doc_view(ctx);
1891
8.25k
            if (code < 0) {
1892
84
                if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_VIEW, "pdfi_doc_view", NULL)) < 0)
1893
0
                    goto exit;
1894
84
            }
1895
8.25k
        }
1896
1897
        /* Handle Info */
1898
8.25k
        code = pdfi_doc_Info(ctx);
1899
8.25k
        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.04k
            if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_INFO, "pdfi_doc_trailer", NULL)) < 0)
1905
0
                goto exit;
1906
5.04k
        }
1907
1908
        /* Handle EmbeddedFiles */
1909
        /* TODO: add a configuration option to embed or omit */
1910
8.25k
        if (ctx->args.preserveembeddedfiles) {
1911
8.25k
            code = pdfi_doc_EmbeddedFiles(ctx);
1912
8.25k
            if (code < 0) {
1913
90
                if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_EMBEDDEDFILES, "pdfi_doc_trailer", NULL)) < 0)
1914
0
                    goto exit;
1915
90
            }
1916
8.25k
        }
1917
8.25k
    }
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
82.1k
    code = pdfi_doc_AcroForm(ctx);
1924
82.1k
    if (code < 0) {
1925
960
        if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_ACROFORM, "pdfi_doc_trailer", NULL)) < 0)
1926
0
            goto exit;
1927
960
    }
1928
1929
    /* Handle OutputIntent ICC Profile */
1930
82.1k
    code = pdfi_doc_OutputIntents(ctx);
1931
82.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
82.1k
    code = pdfi_doc_PageLabels(ctx);
1938
82.1k
    if (code < 0) {
1939
132
        if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_PAGELABELS, "pdfi_doc_trailer", NULL)) < 0)
1940
0
            goto exit;
1941
132
    }
1942
1943
82.1k
 exit:
1944
82.1k
    return code;
1945
82.1k
}