Coverage Report

Created: 2025-06-10 07:27

/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
2.99k
{
36
2.99k
    pdf_obj *o, *o1;
37
2.99k
    pdf_dict *d;
38
2.99k
    int code;
39
40
2.99k
    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
2.99k
    d = ctx->Trailer;
50
2.99k
    pdfi_countup(d);
51
2.99k
    code = pdfi_dict_get(ctx, d, "Root", &o1);
52
2.99k
    if (code < 0) {
53
73
        pdfi_countdown(d);
54
73
        return code;
55
73
    }
56
2.92k
    pdfi_countdown(d);
57
58
2.92k
    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
2.92k
    } else {
76
2.92k
        if (pdfi_type_of(o1) != PDF_DICT) {
77
14
            pdfi_countdown(o1);
78
14
            if (ctx->Root == NULL)
79
6
                return_error(gs_error_typecheck);
80
8
            return 0;
81
14
        }
82
2.92k
    }
83
84
2.91k
    code = pdfi_dict_get_type(ctx, (pdf_dict *)o1, "Type", PDF_NAME, &o);
85
2.91k
    if (code < 0) {
86
28
        bool known = false;
87
88
28
        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
28
        code = pdfi_dict_known(ctx, (pdf_dict *)o1, "Pages", &known);
95
28
        if (code < 0 || known == false) {
96
7
            pdfi_countdown(o1);
97
7
            return code;
98
7
        }
99
28
    }
100
2.88k
    else {
101
2.88k
        if (pdfi_name_strcmp((pdf_name *)o, "Catalog") != 0){
102
18
            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
18
            code = pdfi_dict_get_type(ctx, (pdf_dict *)o1, "Pages", PDF_DICT, &pages);
111
18
            if (code < 0) {
112
8
                pdfi_countdown(o);
113
8
                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
8
                if (ctx->Root == NULL) {
118
3
                    pdfi_set_error(ctx, 0, NULL, E_PDF_NO_ROOT, "pdfi_read_Root", NULL);
119
3
                    return_error(gs_error_syntaxerror);
120
3
                }
121
5
                return 0;
122
8
            }
123
10
            pdfi_countdown(pages);
124
10
            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
10
        }
130
2.87k
        pdfi_countdown(o);
131
2.87k
    }
132
133
2.89k
    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
2.89k
    pdfi_countdown(ctx->Root); /* If file was repaired it might be set already */
139
2.89k
    ctx->Root = (pdf_dict *)o1;
140
2.89k
    return 0;
141
2.91k
}
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
222
{
147
222
    int code = 0, i = 0;
148
222
    pdf_obj *array_obj = NULL;
149
150
222
    code = pdfi_loop_detector_mark(ctx);
151
222
    if (code < 0)
152
0
        return code;
153
154
3.43k
    for (i = 0;i < pdfi_array_size(a); i++) {
155
3.27k
        code = pdfi_array_fetch_recursing(ctx, a, i, &array_obj, true, true);
156
3.27k
        if (code < 0)
157
31
            goto error;
158
159
3.23k
        switch(pdfi_type_of(array_obj)) {
160
34
            case PDF_DICT:
161
34
                if (array_obj->object_num != 0) {
162
31
                    code = pdfi_loop_detector_add_object(ctx, array_obj->object_num);
163
31
                    if (code < 0)
164
0
                        goto error;
165
31
                }
166
34
                code = Info_check_dict(ctx, (pdf_dict *)array_obj);
167
34
                if (code < 0) {
168
23
                    if (array_obj->object_num != 0) {
169
23
                        pdf_indirect_ref *i_o;
170
171
23
                        code = pdfi_object_alloc(ctx, PDF_INDIRECT, 0, (pdf_obj **)&i_o);
172
23
                        if (code < 0)
173
0
                            goto error;
174
23
                        i_o->ref_generation_num = array_obj->generation_num;
175
23
                        i_o->ref_object_num = array_obj->object_num;
176
23
                        i_o->indirect_num = array_obj->object_num;
177
23
                        i_o->indirect_gen = array_obj->generation_num;
178
23
                        code = pdfi_array_put(ctx, a, i, (pdf_obj *)i_o);
179
23
                        if (code < 0)
180
0
                            return code;
181
23
                    }
182
23
                    goto error;
183
23
                }
184
11
                break;
185
11
            case PDF_ARRAY:
186
6
                if (array_obj->object_num != 0) {
187
2
                    code = pdfi_loop_detector_add_object(ctx, array_obj->object_num);
188
2
                    if (code < 0)
189
0
                        goto error;
190
2
                }
191
6
                code = Info_check_array(ctx, (pdf_array *)array_obj);
192
6
                if (code < 0) {
193
2
                    if (array_obj->object_num != 0) {
194
2
                        pdf_indirect_ref *i_o;
195
196
2
                        code = pdfi_object_alloc(ctx, PDF_INDIRECT, 0, (pdf_obj **)&i_o);
197
2
                        if (code < 0)
198
0
                            goto error;
199
2
                        i_o->ref_generation_num = array_obj->generation_num;
200
2
                        i_o->ref_object_num = array_obj->object_num;
201
2
                        i_o->indirect_num = array_obj->object_num;
202
2
                        i_o->indirect_gen = array_obj->generation_num;
203
2
                        code = pdfi_array_put(ctx, a, i, (pdf_obj *)i_o);
204
2
                        if (code < 0)
205
0
                            return code;
206
2
                    }
207
2
                    goto error;
208
2
                }
209
4
                break;
210
3.19k
            default:
211
3.19k
                break;
212
3.23k
        }
213
214
3.21k
        pdfi_countdown(array_obj);
215
3.21k
        array_obj = NULL;
216
3.21k
    }
217
222
error:
218
222
    pdfi_countdown(array_obj);
219
222
    pdfi_loop_detector_cleartomark(ctx);
220
222
    return code;
221
222
}
222
223
static int Info_check_dict(pdf_context *ctx, pdf_dict *d)
224
170
{
225
170
    int code = 0;
226
170
    uint64_t index = 0;
227
170
    pdf_name *Key = NULL;
228
170
    pdf_obj *Value = NULL;
229
230
170
    code = pdfi_loop_detector_mark(ctx);
231
170
    if (code < 0)
232
0
        return code;
233
234
170
    code = pdfi_dict_first(ctx, d, (pdf_obj **)&Key, &Value, &index);
235
170
    if (code < 0) {
236
4
        if (code == gs_error_undefined)
237
0
            code = 0;
238
4
        goto error;
239
4
    }
240
241
726
    while (code >= 0) {
242
712
        switch(pdfi_type_of(Value)) {
243
93
            case PDF_DICT:
244
93
                if (Value->object_num != 0) {
245
63
                    code = pdfi_loop_detector_add_object(ctx, Value->object_num);
246
63
                    if (code < 0)
247
0
                        goto error;
248
63
                }
249
93
                code = Info_check_dict(ctx, (pdf_dict *)Value);
250
93
                if (code < 0)
251
21
                    goto error;
252
72
                break;
253
122
            case PDF_ARRAY:
254
122
                if (Value->object_num != 0) {
255
15
                    code = pdfi_loop_detector_add_object(ctx, Value->object_num);
256
15
                    if (code < 0)
257
0
                        goto error;
258
15
                }
259
122
                code = Info_check_array(ctx, (pdf_array *)Value);
260
122
                if (code < 0)
261
26
                    goto error;
262
96
                break;
263
497
            default:
264
497
                break;
265
712
        }
266
665
        pdfi_countdown(Key);
267
665
        Key = NULL;
268
665
        pdfi_countdown(Value);
269
665
        Value = NULL;
270
271
665
        code = pdfi_dict_next(ctx, d, (pdf_obj **)&Key, &Value, &index);
272
665
        if (code == gs_error_undefined) {
273
105
            code = 0;
274
105
            break;
275
105
        }
276
665
    }
277
170
error:
278
170
    pdfi_countdown(Key);
279
170
    pdfi_countdown(Value);
280
170
    pdfi_loop_detector_cleartomark(ctx);
281
170
    return code;
282
166
}
283
284
static int pdfi_sanitize_Info_references(pdf_context *ctx, pdf_dict *Info)
285
1.66k
{
286
1.66k
    int code = 0;
287
1.66k
    uint64_t index = 0;
288
1.66k
    pdf_name *Key = NULL;
289
1.66k
    pdf_obj *Value = NULL;
290
291
1.68k
restart_scan:
292
1.68k
    code = pdfi_loop_detector_mark(ctx);
293
1.68k
    if (code < 0)
294
0
        return code;
295
296
1.68k
    code = pdfi_dict_first(ctx, Info, (pdf_obj **)&Key, &Value, &index);
297
1.68k
    if (code == gs_error_undefined) {
298
27
        code = 0;
299
27
        goto error;
300
27
    }
301
302
7.87k
    while (code >= 0) {
303
7.87k
        switch(pdfi_type_of(Value)) {
304
43
            case PDF_DICT:
305
43
                code = Info_check_dict(ctx, (pdf_dict *)Value);
306
43
                break;
307
94
            case PDF_ARRAY:
308
94
                code = Info_check_array(ctx, (pdf_array *)Value);
309
94
                break;
310
7.73k
            default:
311
7.73k
                code = 0;
312
7.73k
                break;
313
7.87k
        }
314
7.87k
        pdfi_countdown(Value);
315
7.87k
        Value = NULL;
316
7.87k
        if (code < 0) {
317
24
            code = pdfi_dict_delete_pair(ctx, Info, Key);
318
24
            if (code < 0)
319
0
                goto error;
320
24
            pdfi_countdown(Key);
321
24
            Key = NULL;
322
323
24
            pdfi_loop_detector_cleartomark(ctx);
324
24
            goto restart_scan;
325
24
        }
326
7.84k
        pdfi_countdown(Key);
327
7.84k
        Key = NULL;
328
329
7.84k
        pdfi_loop_detector_cleartomark(ctx);
330
7.84k
        code = pdfi_loop_detector_mark(ctx);
331
7.84k
        if (code < 0) {
332
0
            pdfi_countdown(Key);
333
0
            pdfi_countdown(Value);
334
0
            return code;
335
0
        }
336
337
7.84k
        code = pdfi_dict_next(ctx, Info, (pdf_obj **)&Key, &Value, &index);
338
7.84k
        if (code == gs_error_undefined) {
339
1.62k
            code = 0;
340
1.62k
            break;
341
1.62k
        }
342
7.84k
    }
343
1.66k
error:
344
1.66k
    pdfi_countdown(Key);
345
1.66k
    pdfi_countdown(Value);
346
1.66k
    pdfi_loop_detector_cleartomark(ctx);
347
1.66k
    return code;
348
1.66k
}
349
350
int pdfi_read_Info(pdf_context *ctx)
351
2.91k
{
352
2.91k
    pdf_dict *Info;
353
2.91k
    int code;
354
2.91k
    pdf_dict *d;
355
356
2.91k
    if (ctx->args.pdfdebug)
357
0
        outprintf(ctx->memory, "%% Reading Info dictionary\n");
358
359
    /* See comment in pdfi_read_Root() for details */
360
2.91k
    d = ctx->Trailer;
361
2.91k
    pdfi_countup(d);
362
2.91k
    code = pdfi_dict_get_type(ctx, ctx->Trailer, "Info", PDF_DICT, (pdf_obj **)&Info);
363
2.91k
    pdfi_countdown(d);
364
2.91k
    if (code < 0)
365
1.25k
        return code;
366
367
1.66k
    if (ctx->args.pdfdebug)
368
0
        outprintf(ctx->memory, "\n");
369
370
1.66k
    code = pdfi_loop_detector_mark(ctx);
371
1.66k
    if (code < 0)
372
0
        goto error;
373
1.66k
    if (Info->object_num != 0) {
374
1.66k
        code = pdfi_loop_detector_add_object(ctx, Info->object_num);
375
1.66k
        if (code < 0)
376
0
            goto error1;
377
1.66k
    } 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
1.66k
    code = pdfi_sanitize_Info_references(ctx, Info);
384
1.66k
    if (code < 0)
385
7
        goto error1;
386
387
1.65k
    (void)pdfi_loop_detector_cleartomark(ctx);
388
389
1.65k
    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
1.65k
    ctx->Info = Info;
395
1.65k
    return 0;
396
397
7
error1:
398
7
    pdfi_loop_detector_cleartomark(ctx);
399
7
error:
400
7
    pdfi_countdown(Info);
401
7
    return code;
402
7
}
403
404
int pdfi_read_Pages(pdf_context *ctx)
405
4.61k
{
406
4.61k
    pdf_obj *o, *o1;
407
4.61k
    pdf_array *a = NULL;
408
4.61k
    int code, pagecount = 0;
409
4.61k
    double d;
410
411
4.61k
    if (ctx->args.pdfdebug)
412
0
        outprintf(ctx->memory, "%% Reading Pages dictionary\n");
413
414
4.61k
    code = pdfi_dict_get(ctx, ctx->Root, "Pages", &o1);
415
4.61k
    if (code < 0)
416
155
        return code;
417
418
4.46k
    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
4.46k
    } else {
440
4.46k
        if (pdfi_type_of(o1) != PDF_DICT) {
441
6
            pdfi_countdown(o1);
442
6
            return_error(gs_error_typecheck);
443
6
        }
444
4.46k
    }
445
446
4.45k
    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
4.45k
    code = pdfi_dict_get_number(ctx, (pdf_dict *)o1, "Count", &d);
457
4.45k
    if (code < 0) {
458
56
        if (code == gs_error_undefined) {
459
54
            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
54
            code = pdfi_dict_get_type(ctx, (pdf_dict *)o1, "Type", PDF_NAME, (pdf_obj **)&n);
465
54
            if (code == 0) {
466
51
                if(pdfi_name_is(n, "Page")) {
467
42
                    ctx->num_pages = 1;
468
42
                    code = 0;
469
42
                }
470
9
                else
471
9
                    code = gs_error_undefined;
472
51
                pdfi_countdown(n);
473
51
            }
474
54
        }
475
56
        pdfi_countdown(o1);
476
56
        return code;
477
56
    }
478
479
4.40k
    if (floor(d) != d) {
480
0
        pdfi_countdown(o1);
481
0
        return_error(gs_error_rangecheck);
482
4.40k
    } else {
483
4.40k
        ctx->num_pages = (int)floor(d);
484
4.40k
    }
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
4.40k
    code = pdfi_dict_knownget_type(ctx, (pdf_dict *)o1, "Kids", PDF_ARRAY, (pdf_obj **)&a);
492
4.40k
    if (code == 0)
493
3
        code = gs_note_error(gs_error_undefined);
494
4.40k
    if (code < 0) {
495
3
        pdfi_countdown(o1);
496
3
        return code;
497
3
    }
498
499
    /* Firstly check if the Kids array has enough nodes, in which case it's
500
     * probably flat (the common case)
501
     */
502
4.39k
    if (a->size != ctx->num_pages) {
503
111
        int i = 0;
504
111
        pdf_obj *p = NULL, *p1 = NULL;
505
111
        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
416
        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
305
            code = pdfi_array_get_no_store_R(ctx, a, i, &p);
514
305
            if (code < 0)
515
66
                continue;
516
239
            if (pdfi_type_of(p) != PDF_DICT) {
517
13
                pdfi_countdown(p);
518
13
                p = NULL;
519
13
                continue;
520
13
            }
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
226
            if (p->object_num != 0 && p->object_num == o1->object_num) {
526
0
                pdfi_countdown(p);
527
0
                p = NULL;
528
0
                pdfi_countdown(a);
529
0
                pdfi_countdown(o1);
530
0
                ctx->num_pages = 0;
531
0
                return_error(gs_error_circular_reference);
532
0
            }
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
226
            (void)pdfi_array_put(ctx, a, i, p);
538
226
            (void)replace_cache_entry(ctx, p);
539
226
            code = pdfi_dict_knownget_type(ctx, (pdf_dict *)p, "Type", PDF_NAME, (pdf_obj **)&p1);
540
226
            if (code <= 0) {
541
2
                pdfi_countdown(p);
542
2
                p = NULL;
543
2
                continue;
544
2
            }
545
224
            if (pdfi_name_is((pdf_name *)p1, "Page")) {
546
29
                pagecount++;
547
195
            } else {
548
195
                if (pdfi_name_is((pdf_name *)p1, "Pages")) {
549
194
                    code = pdfi_dict_knownget(ctx, (pdf_dict *)p, "Count", (pdf_obj **)&c);
550
194
                    if (code >= 0) {
551
194
                        if (pdfi_type_of(c) == PDF_INT)
552
194
                            pagecount += c->value.i;
553
194
                        if (pdfi_type_of(c) == PDF_REAL)
554
0
                            pagecount += (int)c->value.d;
555
194
                        pdfi_countdown(c);
556
194
                        c = NULL;
557
194
                    }
558
194
                }
559
195
            }
560
224
            pdfi_countdown(p1);
561
224
            p1 = NULL;
562
224
            pdfi_countdown(p);
563
224
            p = NULL;
564
224
        }
565
111
    } else
566
4.28k
        pagecount = a->size;
567
568
4.39k
    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
4.39k
    if (pagecount != ctx->num_pages) {
580
39
        ctx->num_pages = pagecount;
581
39
        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
39
        code = pdfi_dict_put_int(ctx, (pdf_dict *)o1, "Count", ctx->num_pages);
584
39
        if (code < 0) {
585
0
            pdfi_countdown(o1);
586
0
            return code;
587
0
        }
588
39
    }
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
4.39k
    ctx->PagesTree = (pdf_dict *)o1;
594
4.39k
    return 0;
595
4.39k
}
596
597
/* Read optional things in from Root */
598
void pdfi_read_OptionalRoot(pdf_context *ctx)
599
4.44k
{
600
4.44k
    pdf_obj *obj = NULL;
601
4.44k
    int code;
602
4.44k
    bool known;
603
604
4.44k
    if (ctx->args.pdfdebug)
605
0
        outprintf(ctx->memory, "%% Reading other Root contents\n");
606
607
4.44k
    if (ctx->args.pdfdebug)
608
0
        outprintf(ctx->memory, "%% OCProperties\n");
609
4.44k
    code = pdfi_dict_get_type(ctx, ctx->Root, "OCProperties", PDF_DICT, &obj);
610
4.44k
    if (code == 0) {
611
204
        ctx->OCProperties = (pdf_dict *)obj;
612
4.23k
    } else {
613
4.23k
        ctx->OCProperties = NULL;
614
4.23k
        if (ctx->args.pdfdebug)
615
0
            outprintf(ctx->memory, "%% (None)\n");
616
4.23k
    }
617
618
4.44k
    (void)pdfi_dict_known(ctx, ctx->Root, "Collection", &known);
619
620
4.44k
    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
4.44k
}
629
630
void pdfi_free_OptionalRoot(pdf_context *ctx)
631
5.54k
{
632
5.54k
    if (ctx->OCProperties) {
633
204
        pdfi_countdown(ctx->OCProperties);
634
204
        ctx->OCProperties = NULL;
635
204
    }
636
5.54k
    if (ctx->Collection) {
637
0
        pdfi_countdown(ctx->Collection);
638
0
        ctx->Collection = NULL;
639
0
    }
640
5.54k
}
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
41.6k
{
645
41.6k
    pdf_indirect_ref *node = NULL;
646
41.6k
    pdf_dict *child = NULL;
647
41.6k
    pdf_name *Type = NULL;
648
41.6k
    pdf_dict *leaf_dict = NULL;
649
41.6k
    pdf_name *Key = NULL;
650
41.6k
    int code = 0;
651
652
41.6k
    code = pdfi_array_get_no_deref(ctx, Kids, i, (pdf_obj **)&node);
653
41.6k
    if (code < 0)
654
0
        goto errorExit;
655
656
41.6k
    if (pdfi_type_of(node) != PDF_INDIRECT && pdfi_type_of(node) != PDF_DICT) {
657
1
        code = gs_note_error(gs_error_typecheck);
658
1
        goto errorExit;
659
1
    }
660
661
41.6k
    if (pdfi_type_of(node) == PDF_INDIRECT) {
662
8.73k
        code = pdfi_dereference(ctx, node->ref_object_num, node->ref_generation_num,
663
8.73k
                                (pdf_obj **)&child);
664
8.73k
        if (code < 0) {
665
2.53k
            int code1 = pdfi_repair_file(ctx);
666
2.53k
            if (code1 < 0)
667
2.53k
                goto errorExit;
668
4
            code = pdfi_dereference(ctx, node->ref_object_num,
669
4
                                    node->ref_generation_num, (pdf_obj **)&child);
670
4
            if (code < 0)
671
3
                goto errorExit;
672
4
        }
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
6.20k
        if (pdfi_type_of(child) != PDF_DICT) {
677
60
            pdf_dict *d1 = NULL;
678
679
60
            if (pdfi_type_of(child) != PDF_STREAM) {
680
49
                code = gs_note_error(gs_error_typecheck);
681
49
                goto errorExit;
682
49
            }
683
11
            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
11
            code = pdfi_get_stream_dict(ctx, (pdf_stream *)child, &d1);
687
11
            if (code < 0)
688
0
                goto errorExit;
689
11
            pdfi_countdown(child);
690
11
            child = d1;
691
11
            code = replace_cache_entry(ctx, (pdf_obj *)d1);
692
11
            if (code < 0)
693
0
                goto errorExit;
694
11
        }
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
6.15k
        code = pdfi_dict_get_type(ctx, child, "Type", PDF_NAME, (pdf_obj **)&Type);
708
6.15k
        if (code < 0)
709
62
            goto errorExit;
710
6.08k
        if (pdfi_name_is(Type, "Pages")) {
711
44
            code = pdfi_array_put(ctx, Kids, i, (pdf_obj *)child);
712
44
            if (code < 0)
713
0
                goto errorExit;
714
6.04k
        } else {
715
            /* Bizarrely, one of the QL FTS files (FTS_07_0704.pdf) has a page diciotnary with a /Type of /Template */
716
6.04k
            if (!pdfi_name_is(Type, "Page"))
717
32
                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
6.04k
            code = pdfi_dict_alloc(ctx, 0, &leaf_dict);
725
6.04k
            if (code < 0)
726
0
                goto errorExit;
727
6.04k
            code = pdfi_name_alloc(ctx, (byte *)"PageRef", 7, (pdf_obj **)&Key);
728
6.04k
            if (code < 0)
729
0
                goto errorExit;
730
6.04k
            pdfi_countup(Key);
731
732
6.04k
            code = pdfi_dict_put_obj(ctx, leaf_dict, (pdf_obj *)Key, (pdf_obj *)node, true);
733
6.04k
            if (code < 0)
734
0
                goto errorExit;
735
6.04k
            code = pdfi_dict_put(ctx, leaf_dict, "Type", (pdf_obj *)Key);
736
6.04k
            if (code < 0)
737
0
                goto errorExit;
738
6.04k
            code = pdfi_array_put(ctx, Kids, i, (pdf_obj *)leaf_dict);
739
6.04k
            if (code < 0)
740
0
                goto errorExit;
741
6.04k
            leaf_dict = NULL;
742
6.04k
        }
743
32.9k
    } else {
744
32.9k
        if (ctx->loop_detection != NULL) {
745
32.9k
            if (node->object_num != 0 && pdfi_loop_detector_check_object(ctx, node->object_num)) {
746
0
                code = gs_note_error(gs_error_circular_reference);
747
0
                goto errorExit;
748
0
            }
749
32.9k
            if (node->object_num > 0) {
750
2.59k
                code = pdfi_loop_detector_add_object(ctx, node->object_num);
751
2.59k
                if (code < 0)
752
0
                    goto errorExit;
753
2.59k
            }
754
32.9k
        }
755
32.9k
        child = (pdf_dict *)node;
756
32.9k
        pdfi_countup(child);
757
32.9k
    }
758
759
39.0k
    *pchild = child;
760
39.0k
    child = NULL;
761
762
41.6k
 errorExit:
763
41.6k
    pdfi_free_object((pdf_obj *)leaf_dict);
764
41.6k
    pdfi_countdown(child);
765
41.6k
    pdfi_countdown(node);
766
41.6k
    pdfi_countdown(Type);
767
41.6k
    pdfi_countdown(Key);
768
41.6k
    return code;
769
39.0k
}
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
65.8k
{
775
65.8k
    int code = 0;
776
65.8k
    pdf_obj *object = NULL;
777
65.8k
    bool known;
778
779
    /* Check for inheritable keys, if we find any copy them to the 'inheritable' dictionary at this level */
780
65.8k
    code = pdfi_dict_known(ctx, d, keyname, &known);
781
65.8k
    if (code < 0)
782
0
        goto exit;
783
65.8k
    if (known) {
784
3.16k
        code = pdfi_loop_detector_mark(ctx);
785
3.16k
        if (code < 0){
786
0
            goto exit;
787
0
        }
788
3.16k
        code = pdfi_dict_get(ctx, d, keyname, &object);
789
3.16k
        if (code < 0) {
790
5
            (void)pdfi_loop_detector_cleartomark(ctx);
791
5
            goto exit;
792
5
        }
793
3.16k
        code = pdfi_loop_detector_cleartomark(ctx);
794
3.16k
        if (code < 0) {
795
0
            goto exit;
796
0
        }
797
3.16k
        code = pdfi_dict_put(ctx, inheritable, keyname, object);
798
3.16k
    }
799
800
65.8k
 exit:
801
65.8k
    pdfi_countdown(object);
802
65.8k
    return code;
803
65.8k
}
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
16.4k
{
808
16.4k
    int i, code = 0;
809
16.4k
    pdf_array *Kids = NULL;
810
16.4k
    pdf_dict *child = NULL;
811
16.4k
    pdf_name *Type = NULL;
812
16.4k
    pdf_dict *inheritable = NULL;
813
16.4k
    int64_t num;
814
16.4k
    double dbl;
815
816
16.4k
    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
16.4k
    code = pdfi_dict_alloc(ctx, 0, &inheritable);
821
16.4k
    if (code < 0)
822
0
        return code;
823
16.4k
    pdfi_countup(inheritable);
824
825
16.4k
    code = pdfi_loop_detector_mark(ctx);
826
16.4k
    if (code < 0)
827
0
        return code;
828
829
    /* if we are being passed any inherited values from our parent, copy them now */
830
16.4k
    if (inherited != NULL) {
831
1.72k
        code = pdfi_dict_copy(ctx, inheritable, inherited);
832
1.72k
        if (code < 0)
833
0
            goto exit;
834
1.72k
    }
835
836
16.4k
    code = pdfi_dict_get_number(ctx, d, "Count", &dbl);
837
16.4k
    if (code < 0)
838
0
        goto exit;
839
16.4k
    if (dbl != floor(dbl)) {
840
0
        code = gs_note_error(gs_error_rangecheck);
841
0
        goto exit;
842
0
    }
843
16.4k
    num = (int)dbl;
844
845
16.4k
    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
16.4k
    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
16.4k
    code = pdfi_check_inherited_key(ctx, d, "Resources", inheritable);
858
16.4k
    if (code < 0)
859
5
        goto exit;
860
16.4k
    code = pdfi_check_inherited_key(ctx, d, "MediaBox", inheritable);
861
16.4k
    if (code < 0)
862
0
        goto exit;
863
16.4k
    code = pdfi_check_inherited_key(ctx, d, "CropBox", inheritable);
864
16.4k
    if (code < 0)
865
0
        goto exit;
866
16.4k
    code = pdfi_check_inherited_key(ctx, d, "Rotate", inheritable);
867
16.4k
    if (code < 0) {
868
0
        goto exit;
869
0
    }
870
871
    /* Get the Kids array */
872
16.4k
    code = pdfi_dict_get_type(ctx, d, "Kids", PDF_ARRAY, (pdf_obj **)&Kids);
873
16.4k
    if (code < 0) {
874
0
        goto exit;
875
0
    }
876
877
    /* Check each entry in the Kids array */
878
41.6k
    for (i = 0;i < pdfi_array_size(Kids);i++) {
879
41.6k
        pdfi_countdown(child);
880
41.6k
        child = NULL;
881
41.6k
        pdfi_countdown(Type);
882
41.6k
        Type = NULL;
883
884
41.6k
        code = pdfi_get_child(ctx, Kids, i, &child);
885
41.6k
        if (code < 0) {
886
2.64k
            goto exit;
887
2.64k
        }
888
889
        /* Check the type, if its a Pages entry, then recurse. If its a Page entry, is it the one we want */
890
39.0k
        code = pdfi_dict_get_type(ctx, child, "Type", PDF_NAME, (pdf_obj **)&Type);
891
39.0k
        if (code == 0) {
892
38.9k
            if (pdfi_name_is(Type, "Pages")) {
893
2.57k
                code = pdfi_dict_get_number(ctx, child, "Count", &dbl);
894
2.57k
                if (code == 0) {
895
2.57k
                    if (dbl != floor(dbl)) {
896
0
                        code = gs_note_error(gs_error_rangecheck);
897
0
                        goto exit;
898
0
                    }
899
2.57k
                    num = (int)dbl;
900
2.57k
                    if (num < 0 || (num + *page_offset) > ctx->num_pages) {
901
0
                        code = gs_note_error(gs_error_rangecheck);
902
0
                        goto exit;
903
2.57k
                    } else {
904
2.57k
                        if (num + *page_offset <= page_num) {
905
852
                            *page_offset += num;
906
1.72k
                        } else {
907
1.72k
                            code = pdfi_get_page_dict(ctx, child, page_num, page_offset, target, inheritable);
908
1.72k
                            goto exit;
909
1.72k
                        }
910
2.57k
                    }
911
2.57k
                }
912
36.4k
            } else {
913
36.4k
                if (pdfi_name_is(Type, "PageRef")) {
914
30.3k
                    if ((*page_offset) == page_num) {
915
6.00k
                        pdf_dict *page_dict = NULL;
916
917
6.00k
                        code = pdfi_dict_get_no_store_R(ctx, child, "PageRef", (pdf_obj **)&page_dict);
918
6.00k
                        if (code < 0)
919
0
                            goto exit;
920
6.00k
                        code = pdfi_merge_dicts(ctx, page_dict, inheritable);
921
6.00k
                        *target = page_dict;
922
6.00k
                        pdfi_countup(*target);
923
6.00k
                        pdfi_countdown(page_dict);
924
6.00k
                        goto exit;
925
24.3k
                    } else {
926
24.3k
                        *page_offset += 1;
927
24.3k
                    }
928
30.3k
                } else {
929
6.09k
                    if (!pdfi_name_is(Type, "Page")) {
930
32
                        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
32
                    }
933
6.09k
                    if ((*page_offset) == page_num) {
934
6.08k
                        code = pdfi_merge_dicts(ctx, child, inheritable);
935
6.08k
                        *target = child;
936
6.08k
                        pdfi_countup(*target);
937
6.08k
                        goto exit;
938
6.08k
                    } else {
939
12
                        *page_offset += 1;
940
12
                    }
941
6.09k
                }
942
36.4k
            }
943
38.9k
        }
944
25.1k
        if (code < 0)
945
9
            goto exit;
946
25.1k
    }
947
    /* Positive return value indicates we did not find the target below this node, try the next one */
948
0
    code = 1;
949
950
16.4k
 exit:
951
16.4k
    pdfi_loop_detector_cleartomark(ctx);
952
16.4k
    pdfi_countdown(inheritable);
953
16.4k
    pdfi_countdown(Kids);
954
16.4k
    pdfi_countdown(child);
955
16.4k
    pdfi_countdown(Type);
956
16.4k
    return code;
957
0
}
958
959
int pdfi_doc_page_array_init(pdf_context *ctx)
960
4.44k
{
961
4.44k
    size_t size = ctx->num_pages*sizeof(uint32_t);
962
963
4.44k
    ctx->page_array = (uint32_t *)gs_alloc_bytes(ctx->memory, size,
964
4.44k
                                                 "pdfi_doc_page_array_init(page_array)");
965
4.44k
    if (ctx->page_array == NULL)
966
0
        return_error(gs_error_VMerror);
967
968
4.44k
    memset(ctx->page_array, 0, size);
969
4.44k
    return 0;
970
4.44k
}
971
972
void pdfi_doc_page_array_free(pdf_context *ctx)
973
5.54k
{
974
5.54k
    if (!ctx->page_array)
975
1.10k
        return;
976
4.44k
    gs_free_object(ctx->memory, ctx->page_array, "pdfi_doc_page_array_free(page_array)");
977
4.44k
    ctx->page_array = NULL;
978
4.44k
}
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
489k
{
988
489k
    int code;
989
489k
    pdf_dict *Resources = NULL;
990
991
489k
    code = pdfi_dict_knownget_type(ctx, dict, "Resources", PDF_DICT, (pdf_obj **)&Resources);
992
489k
    if (code == 0)
993
203k
        code = pdfi_dict_knownget_type(ctx, dict, "DR", PDF_DICT, (pdf_obj **)&Resources);
994
489k
    if (code < 0)
995
18.4k
        goto exit;
996
471k
    if (code > 0)
997
268k
        code = pdfi_dict_knownget_type(ctx, Resources, (const char *)Type, PDF_DICT, (pdf_obj **)typedict);
998
489k
 exit:
999
489k
    pdfi_countdown(Resources);
1000
489k
    return code;
1001
471k
}
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
210k
{
1006
210k
    pdf_dict *typedict = NULL;
1007
210k
    pdf_dict *Parent = NULL;
1008
210k
    pdf_name *n = NULL;
1009
210k
    int code;
1010
210k
    bool known = false;
1011
1012
210k
    *o = NULL;
1013
1014
    /* Check the provided dict, stream_dict can be NULL if we are trying to find a Default* ColorSpace */
1015
210k
    if (dict != NULL) {
1016
210k
        bool deref_parent = true, dict_is_XObject = false;
1017
1018
210k
        code = pdfi_resource_knownget_typedict(ctx, Type, dict, &typedict);
1019
210k
        if (code < 0)
1020
80
            goto exit;
1021
210k
        if (code > 0) {
1022
36.6k
            code = pdfi_dict_get_no_store_R_key(ctx, typedict, name, o);
1023
36.6k
            if (code != gs_error_undefined)
1024
25.8k
                goto exit;
1025
36.6k
        }
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
184k
        if (pdfi_dict_knownget_type(ctx, dict, "Type", PDF_NAME, (pdf_obj **)&n) > 0) {
1032
12.7k
            if (pdfi_name_is(n, "Page"))
1033
0
                deref_parent = false;
1034
12.7k
            if (pdfi_name_is(n, "XObject"))
1035
10.5k
                dict_is_XObject = true;
1036
12.7k
            pdfi_countdown(n);
1037
12.7k
        }
1038
1039
184k
        if (deref_parent) {
1040
184k
            code = pdfi_dict_known(ctx, dict, "Parent", &known);
1041
184k
            if (code >= 0 && known == true) {
1042
12.4k
                code = pdfi_dict_get_no_store_R(ctx, dict, "Parent", (pdf_obj **)&Parent);
1043
1044
12.4k
                if (code >= 0) {
1045
11.4k
                    if (pdfi_type_of(Parent) != PDF_DICT) {
1046
0
                        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
0
                        } else {
1057
0
                            pdfi_countdown(Parent);
1058
0
                            Parent = NULL;
1059
0
                        }
1060
0
                    }
1061
11.4k
                } else
1062
941
                    Parent = NULL;
1063
12.4k
            }
1064
1065
184k
            if (Parent != NULL) {
1066
11.4k
                if (ctx->page.CurrentPageDict != NULL && Parent->object_num != ctx->page.CurrentPageDict->object_num) {
1067
11.4k
                    if (pdfi_loop_detector_check_object(ctx, Parent->object_num) == true) {
1068
1.22k
                        code = gs_note_error(gs_error_circular_reference);
1069
1.22k
                        goto exit;
1070
1.22k
                    }
1071
1072
10.2k
                    code = pdfi_loop_detector_mark(ctx);
1073
10.2k
                    if (code < 0)
1074
0
                        goto exit;
1075
1076
10.2k
                    code = pdfi_loop_detector_add_object(ctx, dict->object_num);
1077
10.2k
                    if (code < 0) {
1078
0
                        (void)pdfi_loop_detector_cleartomark(ctx);
1079
0
                        goto exit;
1080
0
                    }
1081
10.2k
                    code = pdfi_find_resource(ctx, Type, name, Parent, page_dict, o);
1082
10.2k
                    (void)pdfi_loop_detector_cleartomark(ctx);
1083
10.2k
                    if (code != gs_error_undefined) {
1084
81
                        if (dict_is_XObject)
1085
73
                            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
81
                        goto exit;
1087
81
                    }
1088
10.2k
                }
1089
11.4k
            }
1090
183k
            code = 0;
1091
183k
        }
1092
183k
        pdfi_countdown(typedict);
1093
183k
        typedict = NULL;
1094
183k
    }
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
183k
    if (page_dict != NULL) {
1107
183k
        code = pdfi_resource_knownget_typedict(ctx, Type, page_dict, &typedict);
1108
183k
        if (code < 0)
1109
22.0k
            goto exit;
1110
1111
161k
        if (code > 0) {
1112
143k
            code = pdfi_dict_get_no_store_R_key(ctx, typedict, name, o);
1113
143k
            if (code != gs_error_undefined)
1114
114k
                goto exit;
1115
143k
        }
1116
161k
    }
1117
1118
46.6k
    pdfi_countdown(typedict);
1119
46.6k
    typedict = NULL;
1120
1121
46.6k
    if (ctx->page.CurrentPageDict != NULL) {
1122
46.6k
        code = pdfi_resource_knownget_typedict(ctx, Type, ctx->page.CurrentPageDict, &typedict);
1123
46.6k
        if (code < 0)
1124
10
            goto exit;
1125
1126
46.6k
        if (code > 0) {
1127
30.2k
            code = pdfi_dict_get_no_store_R_key(ctx, typedict, name, o);
1128
30.2k
            if (code != gs_error_undefined)
1129
190
                goto exit;
1130
30.2k
        }
1131
46.6k
    }
1132
1133
46.4k
    pdfi_countdown(typedict);
1134
46.4k
    typedict = NULL;
1135
1136
46.4k
    if (ctx->current_stream != NULL) {
1137
29.1k
        pdf_dict *stream_dict = NULL;
1138
29.1k
        pdf_stream *stream = ctx->current_stream;
1139
1140
48.9k
        do {
1141
48.9k
            code = pdfi_dict_from_obj(ctx, (pdf_obj *)stream, &stream_dict);
1142
48.9k
            if (code < 0)
1143
0
                goto exit;
1144
48.9k
            code = pdfi_resource_knownget_typedict(ctx, Type, stream_dict, &typedict);
1145
48.9k
            if (code < 0)
1146
0
                goto exit;
1147
48.9k
            if (code > 0) {
1148
20.9k
                code = pdfi_dict_get_no_store_R_key(ctx, typedict, name, o);
1149
20.9k
                if (code == 0) {
1150
3
                    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
3
                    goto exit;
1152
3
                }
1153
20.9k
            }
1154
48.9k
            pdfi_countdown(typedict);
1155
48.9k
            typedict = NULL;
1156
48.9k
            stream = pdfi_stream_parent(ctx, stream);
1157
48.9k
        }while(stream != NULL);
1158
29.1k
    }
1159
1160
    /* If we got all the way down there, we didn't find it */
1161
46.4k
    pdfi_set_warning(ctx, 0, NULL, W_PDF_MISSING_NAMED_RESOURCE, "pdfi_find_resource", NULL);
1162
46.4k
    code = gs_error_undefined;
1163
1164
210k
exit:
1165
210k
    pdfi_countdown(typedict);
1166
210k
    pdfi_countdown(Parent);
1167
210k
    return code;
1168
46.4k
}
1169
1170
/* Mark the actual outline */
1171
static int pdfi_doc_mark_the_outline(pdf_context *ctx, pdf_dict *outline)
1172
0
{
1173
0
    int code = 0;
1174
0
    pdf_dict *tempdict = NULL;
1175
0
    uint64_t dictsize;
1176
0
    uint64_t index;
1177
0
    pdf_name *Key = NULL;
1178
0
    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
0
    dictsize = pdfi_dict_entries(outline);
1186
0
    code = pdfi_dict_alloc(ctx, dictsize, &tempdict);
1187
0
    if (code < 0) goto exit;
1188
0
    pdfi_countup(tempdict);
1189
0
    code = pdfi_dict_copy(ctx, tempdict, outline);
1190
0
    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
0
    code = pdfi_dict_knownget_number(ctx, outline, "Count", &num);
1201
0
    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
0
    {
1209
0
        pdf_dict *current = NULL, *next = NULL;
1210
0
        int count = 0, code1;
1211
1212
0
        code1 = pdfi_dict_knownget_type(ctx, outline, "First", PDF_DICT, (pdf_obj **)&current);
1213
0
        if (code1 > 0) {
1214
0
            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
0
            count++;
1219
0
            do {
1220
0
                code1 = pdfi_dict_knownget_type(ctx, current, "Next", PDF_DICT, (pdf_obj **)&next);
1221
0
                if (code1 > 0) {
1222
0
                    pdfi_countdown(current);
1223
0
                    current = next;
1224
0
                    next = NULL;
1225
0
                    count++;
1226
0
                } else
1227
0
                    break;
1228
0
            } while (1);
1229
0
            pdfi_countdown(current);
1230
0
        }
1231
0
        if (num <= 0)
1232
0
            count *= -1;
1233
0
        pdfi_dict_put_int(ctx, tempdict, "Count", count);
1234
0
    }
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
0
    while (code >= 0) {
1240
0
        if (pdfi_name_is(Key, "Last") || pdfi_name_is(Key, "Next") || pdfi_name_is(Key, "First") ||
1241
0
            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
0
            code = pdfi_dict_delete_pair(ctx, tempdict, Key);
1246
0
        } 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
0
            code = pdfi_dict_delete_pair(ctx, tempdict, Key);
1253
0
        } else if (pdfi_name_is(Key, "A")) {
1254
0
            code = pdfi_pdfmark_modA(ctx, tempdict);
1255
0
        } else if (pdfi_name_is(Key, "Dest")) {
1256
0
            code = pdfi_pdfmark_modDest(ctx, tempdict);
1257
0
        }
1258
0
        if (code < 0)
1259
0
            goto exit;
1260
1261
0
        pdfi_countdown(Key);
1262
0
        Key = NULL;
1263
1264
0
        code = pdfi_dict_key_next(ctx, outline, (pdf_obj **)&Key, &index);
1265
0
        if (code == gs_error_undefined) {
1266
0
            code = 0;
1267
0
            break;
1268
0
        }
1269
0
    }
1270
0
    if (code < 0) goto exit;
1271
1272
    /* Write the pdfmark */
1273
0
    code = pdfi_pdfmark_from_dict(ctx, tempdict, NULL, "OUT");
1274
0
    if (code < 0)
1275
0
        goto exit;
1276
1277
0
 exit:
1278
0
    pdfi_countdown(tempdict);
1279
0
    pdfi_countdown(Key);
1280
0
    return code;
1281
0
}
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
0
{
1289
0
    int code = 0;
1290
0
    pdf_dict *child = NULL;
1291
0
    pdf_dict *Next = NULL;
1292
1293
0
    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
0
    code = pdfi_doc_mark_the_outline(ctx, outline);
1301
0
    if (code < 0)
1302
0
        goto exit1;
1303
1304
    /* Handle the children */
1305
0
    code = pdfi_loop_detector_mark(ctx);
1306
0
    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
0
    code = pdfi_dict_get_no_store_R(ctx, outline, "First", (pdf_obj **)&child);
1311
0
    if (code < 0 || pdfi_type_of(child) != PDF_DICT) {
1312
        /* TODO: flag a warning? */
1313
0
        code = 0;
1314
0
        goto exit;
1315
0
    }
1316
1317
0
    if (child->object_num != 0) {
1318
0
        code = pdfi_loop_detector_add_object(ctx, child->object_num);
1319
0
        if (code < 0)
1320
0
            goto exit;
1321
0
    }
1322
1323
0
    do {
1324
0
        code = pdfi_doc_mark_outline(ctx, child);
1325
0
        if (code < 0) goto exit;
1326
1327
1328
0
        code = pdfi_dict_get_no_store_R(ctx, child, "Next", (pdf_obj **)&Next);
1329
0
        if (code == gs_error_undefined) {
1330
0
            code = 0;
1331
0
            break;
1332
0
        }
1333
0
        if (code == gs_error_circular_reference) {
1334
0
            code = 0;
1335
0
            goto exit;
1336
0
        }
1337
0
        if (code < 0 || pdfi_type_of(Next) != PDF_DICT)
1338
0
            goto exit;
1339
1340
0
        pdfi_countdown(child);
1341
0
        child = Next;
1342
0
        Next = NULL;
1343
0
    } while (true);
1344
1345
0
 exit:
1346
0
    (void)pdfi_loop_detector_cleartomark(ctx);
1347
0
 exit1:
1348
0
    pdfi_countdown(child);
1349
0
    pdfi_countdown(Next);
1350
0
    return code;
1351
0
}
1352
1353
/* Do pdfmark for Outlines */
1354
static int pdfi_doc_Outlines(pdf_context *ctx)
1355
0
{
1356
0
    int code = 0;
1357
0
    pdf_dict *Outlines = NULL;
1358
0
    pdf_dict *outline = NULL;
1359
0
    pdf_dict *Next = NULL;
1360
1361
0
    if (ctx->args.no_pdfmark_outlines)
1362
0
        goto exit1;
1363
1364
0
    code = pdfi_dict_knownget_type(ctx, ctx->Root, "Outlines", PDF_DICT, (pdf_obj **)&Outlines);
1365
0
    if (code <= 0) {
1366
        /* TODO: flag a warning */
1367
0
        code = 0;
1368
0
        goto exit1;
1369
0
    }
1370
1371
0
    code = pdfi_loop_detector_mark(ctx);
1372
0
    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
0
    code = pdfi_dict_get_no_store_R(ctx, Outlines, "First", (pdf_obj **)&outline);
1377
0
    if (code < 0 || pdfi_type_of(outline) != PDF_DICT) {
1378
        /* TODO: flag a warning? */
1379
0
        code = 0;
1380
0
        goto exit;
1381
0
    }
1382
1383
0
    if (pdfi_type_of(outline) != PDF_DICT)
1384
0
        goto exit; /* Exit with no error? */
1385
1386
0
    if (outline->object_num != 0) {
1387
0
        code = pdfi_loop_detector_add_object(ctx, outline->object_num);
1388
0
        if (code < 0)
1389
0
            goto exit;
1390
0
    }
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
0
    do {
1398
0
        code = pdfi_doc_mark_outline(ctx, outline);
1399
0
        if (code < 0) goto exit;
1400
1401
1402
0
        code = pdfi_dict_get_no_store_R(ctx, outline, "Next", (pdf_obj **)&Next);
1403
0
        if (code == gs_error_undefined) {
1404
0
            code = 0;
1405
0
            break;
1406
0
        }
1407
0
        if (code == gs_error_circular_reference) {
1408
0
            code = 0;
1409
0
            goto exit;
1410
0
        }
1411
0
        if (code < 0 || pdfi_type_of(Next) != PDF_DICT)
1412
0
            goto exit;
1413
1414
0
        pdfi_countdown(outline);
1415
0
        outline = Next;
1416
0
        Next = NULL;
1417
0
    } while (true);
1418
1419
0
 exit:
1420
0
    (void)pdfi_loop_detector_cleartomark(ctx);
1421
0
 exit1:
1422
0
    pdfi_countdown(Outlines);
1423
0
    pdfi_countdown(outline);
1424
0
    pdfi_countdown(Next);
1425
0
    return code;
1426
0
}
1427
1428
/* Do pdfmark for Info */
1429
static int pdfi_doc_Info(pdf_context *ctx)
1430
0
{
1431
0
    int code = 0;
1432
0
    pdf_dict *Info = NULL, *d = NULL;
1433
0
    pdf_dict *tempdict = NULL;
1434
0
    uint64_t dictsize;
1435
0
    uint64_t index;
1436
0
    pdf_name *Key = NULL;
1437
0
    pdf_obj *Value = NULL;
1438
1439
    /* See comment in pdfi_read_Root() for details */
1440
0
    d = ctx->Trailer;
1441
0
    pdfi_countup(d);
1442
0
    code = pdfi_dict_knownget_type(ctx, d, "Info", PDF_DICT, (pdf_obj **)&Info);
1443
0
    pdfi_countdown(d);
1444
0
    if (code <= 0) {
1445
        /* TODO: flag a warning */
1446
0
        goto exit;
1447
0
    }
1448
1449
    /* Make a temporary copy of the Info dict */
1450
0
    dictsize = pdfi_dict_entries(Info);
1451
0
    code = pdfi_dict_alloc(ctx, dictsize, &tempdict);
1452
0
    if (code < 0) goto exit;
1453
0
    pdfi_countup(tempdict);
1454
1455
    /* Copy only certain keys from Info to tempdict
1456
     * NOTE: pdfwrite will set /Producer, /CreationDate and /ModDate
1457
     */
1458
0
    code = pdfi_dict_first(ctx, Info, (pdf_obj **)&Key, &Value, &index);
1459
0
    while (code >= 0) {
1460
0
        if (pdfi_name_is(Key, "Author") || pdfi_name_is(Key, "Creator") ||
1461
0
            pdfi_name_is(Key, "Title") || pdfi_name_is(Key, "Subject") ||
1462
0
            pdfi_name_is(Key, "Keywords")) {
1463
1464
0
            code = pdfi_dict_put_obj(ctx, tempdict, (pdf_obj *)Key, Value, true);
1465
0
            if (code < 0)
1466
0
                goto exit;
1467
0
        }
1468
0
        pdfi_countdown(Key);
1469
0
        Key = NULL;
1470
0
        pdfi_countdown(Value);
1471
0
        Value = NULL;
1472
1473
0
        code = pdfi_dict_next(ctx, Info, (pdf_obj **)&Key, &Value, &index);
1474
0
        if (code == gs_error_undefined) {
1475
0
            code = 0;
1476
0
            break;
1477
0
        }
1478
0
    }
1479
0
    if (code < 0) goto exit;
1480
1481
    /* Write the pdfmark */
1482
0
    code = pdfi_pdfmark_from_dict(ctx, tempdict, NULL, "DOCINFO");
1483
1484
0
 exit:
1485
0
    pdfi_countdown(Key);
1486
0
    pdfi_countdown(Value);
1487
0
    pdfi_countdown(Info);
1488
0
    pdfi_countdown(tempdict);
1489
0
    return code;
1490
0
}
1491
1492
/* Handle PageLabels for pdfwrite device */
1493
static int pdfi_doc_PageLabels(pdf_context *ctx)
1494
4.44k
{
1495
4.44k
    int code;
1496
4.44k
    pdf_dict *PageLabels = NULL;
1497
1498
4.44k
    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
4.44k
    code = pdfi_dict_knownget_type(ctx, ctx->Root, "PageLabels", PDF_DICT, (pdf_obj **)&PageLabels);
1505
4.44k
    if (code <= 0) {
1506
4.24k
        if (ctx->loop_detection)
1507
0
            (void)pdfi_loop_detector_cleartomark(ctx);
1508
        /* TODO: flag a warning */
1509
4.24k
        goto exit;
1510
4.24k
    }
1511
1512
198
    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
198
    if (ctx->device_state.WantsPageLabels) {
1519
        /* This will send the PageLabels object as a 'pdfpagelabels' setdeviceparams */
1520
0
        code = pdfi_pdfmark_object(ctx, (pdf_obj *)PageLabels, "pdfpagelabels");
1521
0
        if (code < 0)
1522
0
            goto exit;
1523
0
    }
1524
1525
4.44k
 exit:
1526
4.44k
    pdfi_countdown(PageLabels);
1527
4.44k
    return code;
1528
198
}
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
4.44k
{
1535
4.44k
    int code;
1536
4.44k
    pdf_array *OutputIntents = NULL;
1537
4.44k
    pdf_dict *intent = NULL;
1538
4.44k
    pdf_string *name = NULL;
1539
4.44k
    pdf_stream *DestOutputProfile = NULL;
1540
4.44k
    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
4.44k
    code = pdfi_dict_knownget_type(ctx, ctx->Root, "OutputIntents", PDF_ARRAY,
1547
4.44k
                                   (pdf_obj **)&OutputIntents);
1548
4.44k
    if (code <= 0) {
1549
4.41k
        goto exit;
1550
4.41k
    }
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
21
    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
21
    } 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
21
    } else {
1600
        /* No command line arg was specified, so nothing to do */
1601
21
        code = 0;
1602
21
        goto exit;
1603
21
    }
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
4.44k
 exit:
1618
4.44k
    pdfi_countdown(OutputIntents);
1619
4.44k
    pdfi_countdown(intent);
1620
4.44k
    pdfi_countdown(name);
1621
4.44k
    pdfi_countdown(DestOutputProfile);
1622
4.44k
    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
0
{
1628
0
    int code;
1629
0
    uint64_t arraysize;
1630
0
    uint64_t index;
1631
0
    pdf_string *name = NULL;
1632
0
    pdf_dict *filespec = NULL;
1633
1634
0
    arraysize = pdfi_array_size(names);
1635
0
    if ((arraysize % 2) != 0) {
1636
0
        code = gs_note_error(gs_error_syntaxerror);
1637
0
        goto exit;
1638
0
    }
1639
1640
    /* This is supposed to be an array of
1641
     * [ (filename1) (filespec1) (filename2) (filespec2) ... ]
1642
     */
1643
0
    for (index = 0; index < arraysize; index += 2) {
1644
0
        code = pdfi_array_get_type(ctx, names, index, PDF_STRING, (pdf_obj **)&name);
1645
0
        if (code < 0) goto exit;
1646
1647
0
        code = pdfi_array_get_type(ctx, names, index+1, PDF_DICT, (pdf_obj **)&filespec);
1648
0
        if (code < 0) goto exit;
1649
1650
0
        code = pdfi_pdfmark_embed_filespec(ctx, name, filespec);
1651
0
        if (code < 0) goto exit;
1652
1653
0
        pdfi_countdown(name);
1654
0
        name = NULL;
1655
0
        pdfi_countdown(filespec);
1656
0
        filespec = NULL;
1657
0
    }
1658
1659
1660
0
 exit:
1661
0
    pdfi_countdown(name);
1662
0
    pdfi_countdown(filespec);
1663
0
    return code;
1664
0
}
1665
1666
/* Handle PageLabels for pdfwrite device */
1667
static int pdfi_doc_EmbeddedFiles(pdf_context *ctx)
1668
0
{
1669
0
    int code;
1670
0
    pdf_dict *Names = NULL;
1671
0
    pdf_dict *EmbeddedFiles = NULL;
1672
0
    pdf_array *Names_array = NULL;
1673
0
    pdf_array *Kids = NULL;
1674
1675
0
    code = pdfi_dict_knownget_type(ctx, ctx->Root, "Collection", PDF_DICT, (pdf_obj **)&Names);
1676
0
    if (code < 0) goto exit;
1677
0
    if (code > 0) {
1678
0
        code = 0;
1679
0
        goto exit;
1680
0
    }
1681
1682
0
    code = pdfi_dict_knownget_type(ctx, ctx->Root, "Names", PDF_DICT, (pdf_obj **)&Names);
1683
0
    if (code <= 0) goto exit;
1684
1685
0
    code = pdfi_dict_knownget_type(ctx, Names, "EmbeddedFiles", PDF_DICT, (pdf_obj **)&EmbeddedFiles);
1686
0
    if (code <= 0) goto exit;
1687
1688
0
    code = pdfi_dict_knownget_type(ctx, Names, "Kids", PDF_ARRAY, (pdf_obj **)&Kids);
1689
0
    if (code < 0) goto exit;
1690
0
    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
0
    code = pdfi_dict_knownget_type(ctx, EmbeddedFiles, "Names", PDF_ARRAY, (pdf_obj **)&Names_array);
1700
0
    if (code <= 0) goto exit;
1701
1702
0
    code = pdfi_doc_EmbeddedFiles_Names(ctx, Names_array);
1703
0
    if (code <= 0) goto exit;
1704
1705
0
 exit:
1706
0
    pdfi_countdown(Kids);
1707
0
    pdfi_countdown(Names);
1708
0
    pdfi_countdown(EmbeddedFiles);
1709
0
    pdfi_countdown(Names_array);
1710
0
    return code;
1711
0
}
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
4.44k
{
1723
4.44k
    int code = 0;
1724
4.44k
    pdf_dict *AcroForm = NULL;
1725
4.44k
    bool boolval = false;
1726
1727
4.44k
    code = pdfi_dict_knownget_type(ctx, ctx->Root, "AcroForm", PDF_DICT, (pdf_obj **)&AcroForm);
1728
4.44k
    if (code <= 0) goto exit;
1729
1730
374
    code = pdfi_dict_get_bool(ctx, AcroForm, "NeedAppearances", &boolval);
1731
374
    if (code < 0) {
1732
327
        if (code == gs_error_undefined) {
1733
326
            boolval = true;
1734
326
            code = 0;
1735
326
        }
1736
1
        else
1737
1
            goto exit;
1738
327
    }
1739
373
    ctx->NeedAppearances = boolval;
1740
1741
    /* Save this for efficiency later */
1742
373
    ctx->AcroForm = AcroForm;
1743
373
    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
4.44k
 exit:
1751
4.44k
    pdfi_countdown(AcroForm);
1752
4.44k
    return code;
1753
373
}
1754
1755
1756
static int pdfi_doc_view(pdf_context *ctx)
1757
0
{
1758
0
    int code = 0;
1759
0
    pdf_dict *tempdict = NULL;
1760
0
    pdf_name *Mode = NULL, *Layout = NULL;
1761
0
    pdf_obj *ActionDest = NULL;
1762
1763
0
    code = pdfi_dict_knownget_type(ctx, ctx->Root, "PageMode", PDF_NAME, (pdf_obj **)&Mode);
1764
0
    if (code < 0)
1765
0
        return code;
1766
1767
0
    if (code != 0) {
1768
0
        code = pdfi_dict_alloc(ctx, 1, &tempdict);
1769
0
        if (code < 0)
1770
0
            goto exit;
1771
1772
0
        pdfi_countup(tempdict);
1773
1774
0
        code = pdfi_dict_put(ctx, tempdict, "PageMode", (pdf_obj *)Mode);
1775
0
        if (code < 0)
1776
0
            goto exit;
1777
1778
0
        code = pdfi_pdfmark_from_dict(ctx, tempdict, NULL, "DOCVIEW");
1779
0
        if (code < 0)
1780
0
            goto exit;
1781
0
        pdfi_countdown(tempdict);
1782
0
        tempdict = NULL;
1783
0
    }
1784
1785
0
    code = pdfi_dict_knownget_type(ctx, ctx->Root, "PageLayout", PDF_NAME, (pdf_obj **)&Layout);
1786
0
    if (code < 0)
1787
0
        goto exit;
1788
1789
0
    if (code != 0) {
1790
0
        code = pdfi_dict_alloc(ctx, 1, &tempdict);
1791
0
        if (code < 0)
1792
0
            goto exit;
1793
1794
0
        pdfi_countup(tempdict);
1795
1796
0
        code = pdfi_dict_put(ctx, tempdict, "PageLayout", (pdf_obj *)Layout);
1797
0
        if (code < 0)
1798
0
            goto exit;
1799
1800
0
        code = pdfi_pdfmark_from_dict(ctx, tempdict, NULL, "DOCVIEW");
1801
0
        if (code < 0)
1802
0
            goto exit;
1803
0
        pdfi_countdown(tempdict);
1804
0
        tempdict = NULL;
1805
0
    }
1806
1807
0
    code = pdfi_dict_knownget(ctx, ctx->Root, "OpenAction", &ActionDest);
1808
0
    if (code < 0)
1809
0
        goto exit;
1810
1811
0
    if (code != 0) {
1812
0
        if (pdfi_type_of(ActionDest) == PDF_DICT) {
1813
            /* Dictionary means this is an action */
1814
0
            code = pdfi_dict_alloc(ctx, 1, &tempdict);
1815
0
            if (code < 0)
1816
0
                goto exit;
1817
1818
0
            pdfi_countup(tempdict);
1819
1820
0
            code = pdfi_dict_put(ctx, tempdict, "A", (pdf_obj *)ActionDest);
1821
0
            if (code < 0)
1822
0
                goto exit;
1823
1824
0
            code = pdfi_pdfmark_modA(ctx, tempdict);
1825
0
            if (code < 0) goto exit;
1826
1827
0
            code = pdfi_pdfmark_from_dict(ctx, tempdict, NULL, "DOCVIEW");
1828
0
        } else {
1829
0
            if (pdfi_type_of(ActionDest) == PDF_ARRAY) {
1830
                /* Array means this is a destination */
1831
0
                code = pdfi_dict_alloc(ctx, 1, &tempdict);
1832
0
                if (code < 0)
1833
0
                    goto exit;
1834
1835
0
                pdfi_countup(tempdict);
1836
1837
0
                code = pdfi_dict_put(ctx, tempdict, "Dest", (pdf_obj *)ActionDest);
1838
0
                if (code < 0)
1839
0
                    goto exit;
1840
0
                code = pdfi_pdfmark_modDest(ctx, tempdict);
1841
0
                if (code < 0)
1842
0
                    goto exit;
1843
0
                code = pdfi_pdfmark_from_dict(ctx, tempdict, NULL, "DOCVIEW");
1844
0
                if (code < 0)
1845
0
                    goto exit;
1846
0
            } else {
1847
0
                code = gs_note_error(gs_error_typecheck);
1848
0
                goto exit;
1849
0
            }
1850
0
        }
1851
0
    }
1852
1853
0
exit:
1854
0
    pdfi_countdown(ActionDest);
1855
0
    pdfi_countdown(Layout);
1856
0
    pdfi_countdown(Mode);
1857
0
    pdfi_countdown(tempdict);
1858
0
    return code;
1859
0
}
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
4.44k
{
1868
4.44k
    int code = 0;
1869
1870
    /* Can't do this stuff with no Trailer */
1871
4.44k
    if (!ctx->Trailer) {
1872
1.59k
        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
1.59k
    }
1875
1876
4.44k
    if (ctx->device_state.writepdfmarks) {
1877
        /* Handle Outlines */
1878
0
        code = pdfi_doc_Outlines(ctx);
1879
0
        if (code < 0) {
1880
0
            if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_OUTLINES, "pdfi_doc_trailer", NULL)) < 0)
1881
0
                goto exit;
1882
0
        }
1883
1884
        /* Handle Docview pdfmark stuff */
1885
0
        if (ctx->args.preservedocview) {
1886
0
            code = pdfi_doc_view(ctx);
1887
0
            if (code < 0) {
1888
0
                if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_VIEW, "pdfi_doc_view", NULL)) < 0)
1889
0
                    goto exit;
1890
0
            }
1891
0
        }
1892
1893
        /* Handle Info */
1894
0
        code = pdfi_doc_Info(ctx);
1895
0
        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
0
            if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_INFO, "pdfi_doc_trailer", NULL)) < 0)
1901
0
                goto exit;
1902
0
        }
1903
1904
        /* Handle EmbeddedFiles */
1905
        /* TODO: add a configuration option to embed or omit */
1906
0
        if (ctx->args.preserveembeddedfiles) {
1907
0
            code = pdfi_doc_EmbeddedFiles(ctx);
1908
0
            if (code < 0) {
1909
0
                if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_EMBEDDEDFILES, "pdfi_doc_trailer", NULL)) < 0)
1910
0
                    goto exit;
1911
0
            }
1912
0
        }
1913
0
    }
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
4.44k
    code = pdfi_doc_AcroForm(ctx);
1920
4.44k
    if (code < 0) {
1921
42
        if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_ACROFORM, "pdfi_doc_trailer", NULL)) < 0)
1922
0
            goto exit;
1923
42
    }
1924
1925
    /* Handle OutputIntent ICC Profile */
1926
4.44k
    code = pdfi_doc_OutputIntents(ctx);
1927
4.44k
    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
4.44k
    code = pdfi_doc_PageLabels(ctx);
1934
4.44k
    if (code < 0) {
1935
6
        if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_PAGELABELS, "pdfi_doc_trailer", NULL)) < 0)
1936
0
            goto exit;
1937
6
    }
1938
1939
4.44k
 exit:
1940
4.44k
    return code;
1941
4.44k
}