Coverage Report

Created: 2026-04-09 07:06

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