Coverage Report

Created: 2025-08-28 07:06

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