Coverage Report

Created: 2025-08-28 07:06

/src/ghostpdl/pdf/pdf_check.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2019-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
/* Checks for transparency and spots for the PDF interpreter */
17
18
#include "pdf_int.h"
19
#include "pdf_stack.h"
20
#include "pdf_deref.h"
21
#include "pdf_page.h"
22
#include "pdf_file.h"
23
#include "pdf_dict.h"
24
#include "pdf_array.h"
25
#include "pdf_loop_detect.h"
26
#include "pdf_colour.h"
27
#include "pdf_trans.h"
28
#include "pdf_font_types.h"
29
#include "pdf_gstate.h"
30
#include "pdf_misc.h"
31
#include "pdf_check.h"
32
#include "pdf_device.h"
33
#include "gsdevice.h"       /* For gs_setdevice_no_erase */
34
#include "gspaint.h"        /* For gs_erasepage() */
35
36
/* For performance and resource reasons we do not want to install the transparency blending
37
 * compositor unless we need it. Similarly, if a device handles spot colours it can minimise
38
 * memory usage if it knows ahead of time how many spot colours there will be.
39
 *
40
 * The PDF interpreter written in PostScript performed these as two separate tasks, on opening
41
 * a PDF file it would count spot colour usage and then for each page it would chek if the page
42
 * used any transparency. The code below is used to check for both transparency and spot colours.
43
 * If the int pointer to num_spots is NULL then we aren't interested in spot colours (not supported
44
 * by the device), if the int pointer to transparent is NULL then we aren't interested in transparency
45
 * (-dNOTRANSPARENCY is set).
46
 *
47
 * Currently the code is run when we open the PDF file, the existence of transparency on any given
48
 * page is recorded in a bit array for later use. If it turns out that checking every page when we
49
 * open the file is a performance burden then we could do it on a per-page basis instead. NB if
50
 * the device supports spot colours then we do need to scan the file before starting any page.
51
 *
52
 * The technique is fairly straight-forward, we start with each page, and open its Resources
53
 * dictionary, we then check by type each possible resource. Some resources (eg Pattern, XObject)
54
 * can themselves contain Resources, in which case we recursively check that dictionary. Note; not
55
 * all Resource types need to be checked for both transparency and spot colours, some types can
56
 * only contain one or the other.
57
 *
58
 * Routines with the name pdfi_check_xxx_dict are intended to check a Resource dictionary entry, this
59
 * will be a dictioanry of names and values, where the values are objects of the given Resource type.
60
 *
61
 */
62
63
/* Structure for keeping track of resources as they are being checked */
64
/* CheckedResources is a bit of a hack, for the file Bug697655.pdf. That file has many (unused)
65
 * XObjects which use Resources and the Resources use other XObjects and so on nested
66
 * very deeply. This takes *hours* to check. Ghostscript gets around this because it
67
 * stores all the objects (which we don't want to do because its wasteful) and checking
68
 * to see if its already tested a given resource for spots/transparency.
69
 * This is a temporary allocation, big enough to hold all the objects in the file (1 per bit)
70
 * each time we have fully checked a resource we add it here, when checking a resource we
71
 * first check this list to see if its already been checked, in which case we can skip
72
 * it. When done we release the memory.
73
 */
74
typedef struct {
75
    bool transparent;
76
    bool BM_Not_Normal;
77
    bool has_overprint; /* Does it have OP or op in an ExtGState? */
78
    pdf_dict *spot_dict;
79
    pdf_array *font_array;
80
    uint32_t size;
81
    byte *CheckedResources;
82
} pdfi_check_tracker_t;
83
84
static int pdfi_check_Resources(pdf_context *ctx, pdf_dict *Resources_dict, pdf_dict *page_dict, pdfi_check_tracker_t *tracker);
85
86
static inline bool resource_is_checked(pdfi_check_tracker_t *tracker, pdf_obj *o)
87
6.59M
{
88
6.59M
    uint32_t byte_offset;
89
6.59M
    byte bit_offset;
90
6.59M
    int object_num;
91
92
6.59M
    if(tracker->CheckedResources == NULL)
93
147k
        return 0;
94
95
    /* objects with object number 0 are directly defined, we can't
96
     * store those so just return immediately
97
     */
98
6.45M
    object_num = pdf_object_num(o);
99
6.45M
    if (object_num > 0 && (object_num >> 3) < tracker->size) {
100
        /* CheckedResources is a byte array, each byte represents
101
         * 8 objects. So the object number / 8 is the byte offset
102
         * into the array, and then object number % 8 is the bit
103
         * within that byte that we want.
104
         */
105
2.78M
        bit_offset = 0x01 << (object_num % 8);
106
2.78M
        byte_offset = object_num >> 3;
107
108
        /* If its already set, then return that. */
109
2.78M
        if (tracker->CheckedResources[byte_offset] & bit_offset)
110
310k
            return true;
111
2.47M
        else
112
            /* Otherwise set it for futre reference */
113
2.47M
            tracker->CheckedResources[byte_offset] |= bit_offset;
114
2.78M
    }
115
6.14M
    return false;
116
6.45M
}
117
118
119
static int
120
pdfi_check_free_tracker(pdf_context *ctx, pdfi_check_tracker_t *tracker)
121
247k
{
122
247k
    gs_free_object(ctx->memory, tracker->CheckedResources, "pdfi_check_free_tracker(flags)");
123
247k
    pdfi_countdown(tracker->spot_dict);
124
247k
    pdfi_countdown(tracker->font_array);
125
247k
    memset(tracker, 0, sizeof(*tracker));
126
247k
    return 0;
127
247k
}
128
129
static int
130
pdfi_check_init_tracker(pdf_context *ctx, pdfi_check_tracker_t *tracker, pdf_array **fonts_array, pdf_array **spot_array)
131
247k
{
132
247k
    int code = 0;
133
134
247k
    memset(tracker, 0, sizeof(*tracker));
135
136
247k
    tracker->size = (ctx->xref_table->xref_size + 7) / 8;
137
247k
    tracker->CheckedResources = gs_alloc_bytes(ctx->memory, tracker->size,
138
247k
                                            "pdfi_check_init_tracker(flags)");
139
247k
    if (tracker->CheckedResources == NULL)
140
0
        return_error(gs_error_VMerror);
141
142
247k
    memset(tracker->CheckedResources, 0x00, tracker->size);
143
144
247k
    if (ctx->device_state.spot_capable ||
145
247k
        (ctx->pgs->device->icc_struct->overprint_control) == gs_overprint_control_simulate ||
146
247k
        spot_array != NULL)
147
31.1k
    {
148
31.1k
        code = pdfi_dict_alloc(ctx, 32, &tracker->spot_dict);
149
31.1k
        if (code < 0)
150
0
            goto cleanup;
151
31.1k
        pdfi_countup(tracker->spot_dict);
152
31.1k
    }
153
154
247k
    if (fonts_array != NULL) {
155
0
        code = pdfi_array_alloc(ctx, 0, &tracker->font_array);
156
0
        if (code < 0)
157
0
            goto cleanup;
158
0
        pdfi_countup(tracker->font_array);
159
0
    }
160
161
247k
    return 0;
162
163
0
cleanup:
164
0
    pdfi_check_free_tracker(ctx, tracker);
165
0
    return code;
166
247k
}
167
168
/*
169
 * Check the Resources dictionary ColorSpace entry. pdfi_check_ColorSpace_for_spots is defined
170
 * in pdf_colour.c
171
 */
172
static int pdfi_check_ColorSpace_dict(pdf_context *ctx, pdf_dict *cspace_dict,
173
                                      pdf_dict *page_dict, pdfi_check_tracker_t *tracker)
174
78.4k
{
175
78.4k
    int code;
176
78.4k
    uint64_t i, index;
177
78.4k
    pdf_obj *Key = NULL, *Value = NULL;
178
179
78.4k
    if (resource_is_checked(tracker, (pdf_obj *)cspace_dict))
180
24
        return 0;
181
182
78.4k
    if (pdfi_type_of(cspace_dict) != PDF_DICT)
183
66.9k
        return_error(gs_error_typecheck);
184
185
11.5k
    if (pdfi_dict_entries(cspace_dict) > 0) {
186
11.5k
        code = pdfi_loop_detector_mark(ctx); /* Mark the start of the ColorSpace dictionary loop */
187
11.5k
        if (code < 0)
188
0
            return code;
189
190
11.5k
        code = pdfi_dict_first(ctx, cspace_dict, &Key, &Value, &index);
191
11.5k
        if (code < 0)
192
1.89k
            goto error1;
193
194
9.62k
        i = 1;
195
13.3k
        do {
196
13.3k
            code = pdfi_check_ColorSpace_for_spots(ctx, Value, cspace_dict, page_dict, tracker->spot_dict);
197
13.3k
            if (code < 0)
198
132
                goto error2;
199
200
13.2k
            pdfi_countdown(Key);
201
13.2k
            Key = NULL;
202
13.2k
            pdfi_countdown(Value);
203
13.2k
            Value = NULL;
204
205
13.2k
            (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the Shading dictionary loop */
206
207
13.2k
            code = pdfi_loop_detector_mark(ctx); /* Mark the new start of the Shading dictionary loop */
208
13.2k
            if (code < 0)
209
0
                goto error1;
210
211
13.4k
            do {
212
13.4k
                if (i++ >= pdfi_dict_entries(cspace_dict)) {
213
9.48k
                    code = 0;
214
9.48k
                    goto transparency_exit;
215
9.48k
                }
216
217
3.98k
                code = pdfi_dict_next(ctx, cspace_dict, &Key, &Value, &index);
218
3.98k
                if (code == 0 && pdfi_type_of(Value) == PDF_ARRAY)
219
3.76k
                    break;
220
224
                pdfi_countdown(Key);
221
224
                Key = NULL;
222
224
                pdfi_countdown(Value);
223
224
                Value = NULL;
224
224
            } while(1);
225
13.2k
        }while (1);
226
9.62k
    }
227
14
    return 0;
228
229
9.48k
transparency_exit:
230
9.62k
error2:
231
9.62k
    pdfi_countdown(Key);
232
9.62k
    pdfi_countdown(Value);
233
234
11.5k
error1:
235
11.5k
    (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */
236
11.5k
    return code;
237
9.62k
}
238
239
/*
240
 * Process an individual Shading dictionary to see if it contains a ColorSpace with a spot colour
241
 */
242
static int pdfi_check_Shading(pdf_context *ctx, pdf_obj *shading,
243
                              pdf_dict *page_dict, pdfi_check_tracker_t *tracker)
244
17.7k
{
245
17.7k
    int code;
246
17.7k
    pdf_obj *o = NULL;
247
17.7k
    pdf_dict *shading_dict = NULL;
248
249
17.7k
    if (resource_is_checked(tracker, shading))
250
946
        return 0;
251
252
16.7k
    code = pdfi_dict_from_obj(ctx, shading, &shading_dict);
253
16.7k
    if (code < 0)
254
8
        return code;
255
256
16.7k
    if (pdfi_type_of(shading_dict) != PDF_DICT)
257
0
        return_error(gs_error_typecheck);
258
259
16.7k
    code = pdfi_dict_knownget(ctx, shading_dict, "ColorSpace", (pdf_obj **)&o);
260
16.7k
    if (code > 0) {
261
16.0k
        code = pdfi_check_ColorSpace_for_spots(ctx, o, shading_dict, page_dict, tracker->spot_dict);
262
16.0k
        pdfi_countdown(o);
263
16.0k
        return code;
264
16.0k
    }
265
704
    return 0;
266
16.7k
}
267
268
/*
269
 * Check the Resources dictionary Shading entry.
270
 */
271
static int pdfi_check_Shading_dict(pdf_context *ctx, pdf_dict *shading_dict,
272
                                   pdf_dict *page_dict, pdfi_check_tracker_t *tracker)
273
78.4k
{
274
78.4k
    int code;
275
78.4k
    uint64_t i, index;
276
78.4k
    pdf_obj *Key = NULL, *Value = NULL;
277
278
78.4k
    if (resource_is_checked(tracker, (pdf_obj *)shading_dict))
279
0
        return 0;
280
281
78.4k
    if (pdfi_type_of(shading_dict) != PDF_DICT)
282
75.1k
        return_error(gs_error_typecheck);
283
284
3.32k
    if (pdfi_dict_entries(shading_dict) > 0) {
285
3.32k
        code = pdfi_loop_detector_mark(ctx); /* Mark the start of the Shading dictionary loop */
286
3.32k
        if (code < 0)
287
0
            return code;
288
289
3.32k
        code = pdfi_dict_first(ctx, shading_dict, &Key, &Value, &index);
290
3.32k
        if (code < 0)
291
1.57k
            goto error2;
292
293
1.75k
        i = 1;
294
16.6k
        do {
295
16.6k
            if ((pdfi_type_of(Value) == PDF_DICT || pdfi_type_of(Value) == PDF_STREAM)) {
296
16.6k
                code = pdfi_check_Shading(ctx, Value, page_dict, tracker);
297
16.6k
                if (code < 0)
298
30
                    goto error2;
299
16.6k
            }
300
301
16.5k
            pdfi_countdown(Key);
302
16.5k
            Key = NULL;
303
16.5k
            pdfi_countdown(Value);
304
16.5k
            Value = NULL;
305
306
16.5k
            (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the Shading dictionary loop */
307
308
16.5k
            code = pdfi_loop_detector_mark(ctx); /* Mark the new start of the Shading dictionary loop */
309
16.5k
            if (code < 0)
310
0
                goto error1;
311
312
29.4k
            do {
313
29.4k
                if (i++ >= pdfi_dict_entries(shading_dict)) {
314
1.72k
                    code = 0;
315
1.72k
                    goto transparency_exit;
316
1.72k
                }
317
318
27.7k
                code = pdfi_dict_next(ctx, shading_dict, &Key, &Value, &index);
319
27.7k
                if (code == 0 && pdfi_type_of(Value) == PDF_DICT)
320
14.8k
                    break;
321
12.8k
                pdfi_countdown(Key);
322
12.8k
                Key = NULL;
323
12.8k
                pdfi_countdown(Value);
324
12.8k
                Value = NULL;
325
12.8k
            } while(1);
326
16.5k
        }while (1);
327
1.75k
    }
328
0
    return 0;
329
330
1.72k
transparency_exit:
331
3.32k
error2:
332
3.32k
    pdfi_countdown(Key);
333
3.32k
    pdfi_countdown(Value);
334
335
3.32k
error1:
336
3.32k
    (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */
337
3.32k
    return code;
338
3.32k
}
339
340
/*
341
 * This routine checks an XObject to see if it contains any spot
342
 * colour definitions, or transparency usage.
343
 */
344
static int pdfi_check_XObject(pdf_context *ctx, pdf_dict *xobject, pdf_dict *page_dict,
345
                              pdfi_check_tracker_t *tracker)
346
478k
{
347
478k
    int code = 0;
348
478k
    pdf_name *n = NULL;
349
478k
    bool known = false;
350
478k
    double f;
351
352
478k
    if (resource_is_checked(tracker, (pdf_obj *)xobject))
353
161k
        return 0;
354
355
317k
    if (pdfi_type_of(xobject) != PDF_DICT)
356
790
        return_error(gs_error_typecheck);
357
358
316k
    code = pdfi_dict_get_type(ctx, xobject, "Subtype", PDF_NAME, (pdf_obj **)&n);
359
316k
    if (code >= 0) {
360
315k
        if (pdfi_name_is((const pdf_name *)n, "Image")) {
361
165k
            pdf_obj *CS = NULL;
362
363
165k
            pdfi_countdown(n);
364
165k
            n = NULL;
365
165k
            code = pdfi_dict_known(ctx, xobject, "SMask", &known);
366
165k
            if (code >= 0) {
367
165k
                if (known == true) {
368
62.4k
                    tracker->transparent = true;
369
62.4k
                    if (tracker->spot_dict == NULL)
370
53.9k
                        goto transparency_exit;
371
62.4k
                }
372
111k
                code = pdfi_dict_knownget_number(ctx, xobject, "SMaskInData", &f);
373
111k
                if (code > 0) {
374
0
                    code = 0;
375
0
                    if (f != 0.0)
376
0
                        tracker->transparent = true;
377
0
                    if (tracker->spot_dict == NULL)
378
0
                        goto transparency_exit;
379
0
                }
380
                /* Check the image dictionary for a ColorSpace entry, if we are checking spot names */
381
111k
                if (tracker->spot_dict) {
382
20.9k
                    code = pdfi_dict_knownget(ctx, xobject, "ColorSpace", (pdf_obj **)&CS);
383
20.9k
                    if (code > 0) {
384
                        /* We don't care if there's an error here, it'll be picked up if we use the ColorSpace later */
385
18.0k
                        (void)pdfi_check_ColorSpace_for_spots(ctx, CS, xobject, page_dict, tracker->spot_dict);
386
18.0k
                        pdfi_countdown(CS);
387
18.0k
                    }
388
20.9k
                }
389
111k
            }
390
165k
        } else {
391
150k
            if (pdfi_name_is((const pdf_name *)n, "Form")) {
392
149k
                pdf_dict *group_dict = NULL, *resource_dict = NULL;
393
149k
                pdf_obj *CS = NULL;
394
395
149k
                pdfi_countdown(n);
396
149k
                code = pdfi_dict_knownget_type(ctx, xobject, "Group", PDF_DICT, (pdf_obj **)&group_dict);
397
149k
                if (code > 0) {
398
24.4k
                    tracker->transparent = true;
399
24.4k
                    if (tracker->spot_dict != NULL) {
400
                        /* Start a new loop detector group to avoid this being detected in the Resources check below */
401
4.00k
                        code = pdfi_loop_detector_mark(ctx); /* Mark the start of the XObject dictionary loop */
402
4.00k
                        if (code == 0) {
403
4.00k
                            code = pdfi_dict_knownget(ctx, group_dict, "CS", &CS);
404
4.00k
                            if (code > 0)
405
                                /* We don't care if there's an error here, it'll be picked up if we use the ColorSpace later */
406
3.15k
                                (void)pdfi_check_ColorSpace_for_spots(ctx, CS, group_dict, page_dict, tracker->spot_dict);
407
4.00k
                            (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the XObject dictionary loop */
408
4.00k
                        }
409
4.00k
                        pdfi_countdown(group_dict);
410
4.00k
                        pdfi_countdown(CS);
411
4.00k
                    }
412
20.4k
                    else if (tracker->BM_Not_Normal)
413
102
                    {
414
                        /* We already know it has a non-normal Blend Mode. No point in keeping searching. */
415
102
                        pdfi_countdown(group_dict);
416
102
                        goto transparency_exit;
417
102
                    }
418
20.3k
                    else
419
20.3k
                    {
420
20.3k
                        pdfi_countdown(group_dict);
421
20.3k
                    }
422
                    /* We need to keep checking Resources in case there are non-Normal blend mode things still to be found. */
423
24.4k
                }
424
425
149k
                code = pdfi_dict_knownget_type(ctx, xobject, "Resources", PDF_DICT, (pdf_obj **)&resource_dict);
426
149k
                if (code > 0) {
427
146k
                    if (ctx->loop_detection && pdf_object_num((pdf_obj *)resource_dict) != 0) {
428
91.4k
                        code = pdfi_loop_detector_add_object(ctx, resource_dict->object_num);
429
91.4k
                        if (code < 0) {
430
0
                            pdfi_countdown(resource_dict);
431
0
                            goto transparency_exit;
432
0
                        }
433
91.4k
                    }
434
146k
                    code = pdfi_check_Resources(ctx, resource_dict, page_dict, tracker);
435
146k
                    pdfi_countdown(resource_dict);
436
146k
                    if (code < 0)
437
0
                        goto transparency_exit;
438
146k
                }
439
149k
            } else
440
490
                pdfi_countdown(n);
441
150k
        }
442
315k
    }
443
444
262k
    return 0;
445
446
54.0k
transparency_exit:
447
54.0k
    return code;
448
316k
}
449
450
/*
451
 * Check the Resources dictionary XObject entry.
452
 */
453
static int pdfi_check_XObject_dict(pdf_context *ctx, pdf_dict *xobject_dict, pdf_dict *page_dict,
454
                                   pdfi_check_tracker_t *tracker)
455
771k
{
456
771k
    int code;
457
771k
    uint64_t i, index;
458
771k
    pdf_obj *Key = NULL, *Value = NULL;
459
771k
    pdf_dict *Value_dict = NULL;
460
461
771k
    if (resource_is_checked(tracker, (pdf_obj *)xobject_dict))
462
0
        return 0;
463
464
771k
    if (pdfi_type_of(xobject_dict) != PDF_DICT)
465
471k
        return_error(gs_error_typecheck);
466
467
300k
    if (pdfi_dict_entries(xobject_dict) > 0) {
468
298k
        code = pdfi_loop_detector_mark(ctx); /* Mark the start of the XObject dictionary loop */
469
298k
        if (code < 0)
470
0
            return code;
471
472
298k
        code = pdfi_dict_first(ctx, xobject_dict, &Key, &Value, &index);
473
298k
        if (code < 0)
474
43.7k
            goto error_exit;
475
476
254k
        i = 1;
477
479k
        do {
478
479k
            if (pdfi_type_of(Value) == PDF_STREAM) {
479
478k
                code = pdfi_dict_from_obj(ctx, Value, &Value_dict);
480
478k
                if (code < 0)
481
0
                    goto error_exit;
482
483
478k
                code = pdfi_check_XObject(ctx, Value_dict, page_dict, tracker);
484
478k
                if (code < 0)
485
0
                    goto error_exit;
486
478k
            }
487
488
479k
            (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the XObject dictionary loop */
489
490
479k
            code = pdfi_loop_detector_mark(ctx); /* Mark the new start of the XObject dictionary loop */
491
479k
            if (code < 0)
492
0
                goto error_exit;
493
494
479k
            pdfi_countdown(Key);
495
479k
            Key = NULL;
496
479k
            pdfi_countdown(Value);
497
479k
            Value = NULL;
498
479k
            Value_dict = NULL;
499
500
546k
            do {
501
546k
                if (i++ >= pdfi_dict_entries(xobject_dict)) {
502
254k
                    code = 0;
503
254k
                    goto transparency_exit;
504
254k
                }
505
506
291k
                code = pdfi_dict_next(ctx, xobject_dict, &Key, &Value, &index);
507
291k
                if (code == 0 && pdfi_type_of(Value) == PDF_STREAM)
508
224k
                    break;
509
66.6k
                pdfi_countdown(Key);
510
66.6k
                Key = NULL;
511
66.6k
                pdfi_countdown(Value);
512
66.6k
                Value = NULL;
513
66.6k
            } while(1);
514
479k
        }while(1);
515
254k
    }
516
1.66k
    return 0;
517
518
254k
transparency_exit:
519
298k
error_exit:
520
298k
    pdfi_countdown(Key);
521
298k
    pdfi_countdown(Value);
522
298k
    (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */
523
298k
    return code;
524
254k
}
525
526
/*
527
 * This routine checks an ExtGState dictionary to see if it contains transparency or OP
528
 */
529
static int pdfi_check_ExtGState(pdf_context *ctx, pdf_dict *extgstate_dict, pdf_dict *page_dict,
530
                                pdfi_check_tracker_t *tracker)
531
269k
{
532
269k
    int code;
533
269k
    pdf_obj *o = NULL;
534
269k
    double f;
535
269k
    bool overprint;
536
537
269k
    if (resource_is_checked(tracker, (pdf_obj *)extgstate_dict))
538
91.5k
        return 0;
539
540
177k
    if (pdfi_type_of(extgstate_dict) != PDF_DICT)
541
1.64k
        return_error(gs_error_typecheck);
542
543
175k
    if (pdfi_dict_entries(extgstate_dict) > 0) {
544
        /* See if /OP or /op is true */
545
175k
        code = pdfi_dict_get_bool(ctx, extgstate_dict, "OP", &overprint);
546
175k
        if (code == 0 && overprint)
547
7.64k
            tracker->has_overprint = true;
548
175k
        code = pdfi_dict_get_bool(ctx, extgstate_dict, "op", &overprint);
549
175k
        if (code == 0 && overprint)
550
8.35k
            tracker->has_overprint = true;
551
552
        /* Check Blend Mode *first* because we short-circuit transparency detection
553
         * when we find transparency, and we want to be able to record the use of
554
         * blend modes other than Compatible/Normal so that Patterns using these
555
         * always use the clist and not tiles. The tiles implementation can't work
556
         * if we change to a blend mode other than Normal or Compatible during
557
         * the course of a Pattern.
558
         */
559
175k
        code = pdfi_dict_knownget_type(ctx, extgstate_dict, "BM", PDF_NAME, &o);
560
175k
        if (code > 0) {
561
85.7k
            if (!pdfi_name_is((pdf_name *)o, "Normal")) {
562
9.32k
                if (!pdfi_name_is((pdf_name *)o, "Compatible")) {
563
9.26k
                    pdfi_countdown(o);
564
9.26k
                    tracker->transparent = true;
565
9.26k
                    tracker->BM_Not_Normal = true;
566
9.26k
                    return 0;
567
9.26k
                }
568
9.32k
            }
569
85.7k
        }
570
166k
        pdfi_countdown(o);
571
166k
        o = NULL;
572
573
        /* Check SMask */
574
166k
        code = pdfi_dict_knownget(ctx, extgstate_dict, "SMask", &o);
575
166k
        if (code > 0) {
576
25.7k
            switch (pdfi_type_of(o)) {
577
21.8k
                case PDF_NAME:
578
21.8k
                    if (!pdfi_name_is((pdf_name *)o, "None")) {
579
43
                        pdfi_countdown(o);
580
43
                        tracker->transparent = true;
581
43
                        return 0;
582
43
                    }
583
21.8k
                    break;
584
21.8k
                case PDF_DICT:
585
3.84k
                {
586
3.84k
                    pdf_obj *G = NULL;
587
588
3.84k
                    tracker->transparent = true;
589
590
3.84k
                    if (tracker->spot_dict != NULL) {
591
                        /* Check if the SMask has a /G (Group) */
592
961
                        code = pdfi_dict_knownget(ctx, (pdf_dict *)o, "G", &G);
593
961
                        if (code > 0) {
594
790
                            code = pdfi_check_XObject(ctx, (pdf_dict *)G, page_dict,
595
790
                                                      tracker);
596
790
                            pdfi_countdown(G);
597
790
                        }
598
961
                    }
599
3.84k
                    pdfi_countdown(o);
600
3.84k
                    return code;
601
21.8k
                }
602
0
                default:
603
0
                    break;
604
25.7k
            }
605
25.7k
        }
606
162k
        pdfi_countdown(o);
607
162k
        o = NULL;
608
609
162k
        code = pdfi_dict_knownget_number(ctx, extgstate_dict, "CA", &f);
610
162k
        if (code > 0) {
611
75.5k
            if (f != 1.0) {
612
17.4k
                tracker->transparent = true;
613
17.4k
                return 0;
614
17.4k
            }
615
75.5k
        }
616
617
145k
        code = pdfi_dict_knownget_number(ctx, extgstate_dict, "ca", &f);
618
145k
        if (code > 0) {
619
61.0k
            if (f != 1.0) {
620
1.47k
                tracker->transparent = true;
621
1.47k
                return 0;
622
1.47k
            }
623
61.0k
        }
624
625
145k
    }
626
143k
    return 0;
627
175k
}
628
629
/*
630
 * Check the Resources dictionary ExtGState entry.
631
 */
632
static int pdfi_check_ExtGState_dict(pdf_context *ctx, pdf_dict *extgstate_dict, pdf_dict *page_dict,
633
                                     pdfi_check_tracker_t *tracker)
634
771k
{
635
771k
    int code;
636
771k
    uint64_t i, index;
637
771k
    pdf_obj *Key = NULL, *Value = NULL;
638
639
771k
    if (resource_is_checked(tracker, (pdf_obj *)extgstate_dict))
640
166
        return 0;
641
642
771k
    if (pdfi_type_of(extgstate_dict) != PDF_DICT)
643
562k
        return_error(gs_error_typecheck);
644
645
208k
    if (pdfi_dict_entries(extgstate_dict) > 0) {
646
207k
        code = pdfi_loop_detector_mark(ctx); /* Mark the start of the ColorSpace dictionary loop */
647
207k
        if (code < 0)
648
0
            return code;
649
650
207k
        code = pdfi_dict_first(ctx, extgstate_dict, &Key, &Value, &index);
651
207k
        if (code < 0)
652
19.4k
            goto error1;
653
654
188k
        i = 1;
655
269k
        do {
656
657
269k
            (void)pdfi_check_ExtGState(ctx, (pdf_dict *)Value, page_dict, tracker);
658
269k
            if (tracker->transparent == true && tracker->spot_dict == NULL)
659
30.6k
                goto transparency_exit;
660
661
238k
            pdfi_countdown(Key);
662
238k
            Key = NULL;
663
238k
            pdfi_countdown(Value);
664
238k
            Value = NULL;
665
666
238k
            (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the ExtGState dictionary loop */
667
668
238k
            code = pdfi_loop_detector_mark(ctx); /* Mark the new start of the ExtGState dictionary loop */
669
238k
            if (code < 0)
670
0
                goto error1;
671
672
252k
            do {
673
252k
                if (i++ >= pdfi_dict_entries(extgstate_dict)) {
674
157k
                    code = 0;
675
157k
                    goto transparency_exit;
676
157k
                }
677
678
95.0k
                code = pdfi_dict_next(ctx, extgstate_dict, &Key, &Value, &index);
679
95.0k
                if (code == 0 && pdfi_type_of(Value) == PDF_DICT)
680
81.0k
                    break;
681
13.9k
                pdfi_countdown(Key);
682
13.9k
                Key = NULL;
683
13.9k
                pdfi_countdown(Value);
684
13.9k
                Value = NULL;
685
13.9k
            } while(1);
686
238k
        }while (1);
687
188k
    }
688
1.50k
    return 0;
689
690
188k
transparency_exit:
691
188k
    pdfi_countdown(Key);
692
188k
    pdfi_countdown(Value);
693
694
207k
error1:
695
207k
    (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */
696
207k
    return code;
697
188k
}
698
699
/*
700
 * This routine checks a Pattern dictionary to see if it contains any spot
701
 * colour definitions, or transparency usage (or blend modes other than Normal/Compatible).
702
 */
703
static int pdfi_check_Pattern(pdf_context *ctx, pdf_dict *pattern, pdf_dict *page_dict,
704
                              pdfi_check_tracker_t *tracker)
705
114k
{
706
114k
    int code = 0;
707
114k
    pdf_obj *o = NULL;
708
709
114k
    if (resource_is_checked(tracker, (pdf_obj *)pattern))
710
1.15k
        return 0;
711
712
113k
    if (pdfi_type_of(pattern) != PDF_DICT)
713
0
        return_error(gs_error_typecheck);
714
715
113k
    if (tracker->spot_dict != NULL) {
716
16.6k
        code = pdfi_dict_knownget(ctx, pattern, "Shading", &o);
717
16.6k
        if (code > 0)
718
1.14k
            (void)pdfi_check_Shading(ctx, o, page_dict, tracker);
719
16.6k
        pdfi_countdown(o);
720
16.6k
        o = NULL;
721
16.6k
    }
722
723
113k
    code = pdfi_dict_knownget_type(ctx, pattern, "Resources", PDF_DICT, &o);
724
113k
    if (code > 0)
725
102k
        (void)pdfi_check_Resources(ctx, (pdf_dict *)o, page_dict, tracker);
726
113k
    pdfi_countdown(o);
727
113k
    o = NULL;
728
113k
    if (tracker->transparent == true && tracker->spot_dict == NULL)
729
53.2k
        goto transparency_exit;
730
731
60.2k
    code = pdfi_dict_knownget_type(ctx, pattern, "ExtGState", PDF_DICT, &o);
732
60.2k
    if (code > 0)
733
0
        (void)pdfi_check_ExtGState(ctx, (pdf_dict *)o, page_dict, tracker);
734
60.2k
    pdfi_countdown(o);
735
60.2k
    o = NULL;
736
737
113k
transparency_exit:
738
113k
    return 0;
739
60.2k
}
740
741
/*
742
 * This routine checks a Pattern dictionary for transparency.
743
 */
744
int pdfi_check_Pattern_transparency(pdf_context *ctx, pdf_dict *pattern, pdf_dict *page_dict,
745
                                    bool *transparent, bool *BM_Not_Normal)
746
21.7k
{
747
21.7k
    int code;
748
21.7k
    pdfi_check_tracker_t tracker = {0, 0, 0, NULL, NULL, 0, NULL};
749
750
    /* NOTE: We use a "null" tracker that won't do any optimization to prevent
751
     * checking the same resource twice.
752
     * This means we don't get the optimization, but we also don't have the overhead
753
     * of setting it up.
754
     * If we do want the optimization, call pdfi_check_init_tracker() and
755
     * pdfi_check_free_tracker() as in pdfi_check_page(), below.
756
     */
757
21.7k
    code = pdfi_check_Pattern(ctx, pattern, page_dict, &tracker);
758
21.7k
    if (code == 0) {
759
21.7k
        *transparent = tracker.transparent;
760
21.7k
        *BM_Not_Normal = tracker.BM_Not_Normal;
761
21.7k
    }
762
0
    else
763
0
        *transparent = false;
764
21.7k
    return code;
765
21.7k
}
766
767
/*
768
 * Check the Resources dictionary Pattern entry.
769
 */
770
static int pdfi_check_Pattern_dict(pdf_context *ctx, pdf_dict *pattern_dict, pdf_dict *page_dict,
771
                                   pdfi_check_tracker_t *tracker)
772
771k
{
773
771k
    int code;
774
771k
    uint64_t i, index;
775
771k
    pdf_obj *Key = NULL, *Value = NULL;
776
771k
    pdf_dict *instance_dict = NULL;
777
778
771k
    if (resource_is_checked(tracker, (pdf_obj *)pattern_dict))
779
0
        return 0;
780
781
771k
    if (pdfi_type_of(pattern_dict) != PDF_DICT)
782
756k
        return_error(gs_error_typecheck);
783
784
15.6k
    if (pdfi_dict_entries(pattern_dict) > 0) {
785
14.1k
        code = pdfi_loop_detector_mark(ctx); /* Mark the start of the Pattern dictionary loop */
786
14.1k
        if (code < 0)
787
0
            return code;
788
789
14.1k
        code = pdfi_dict_first(ctx, pattern_dict, &Key, &Value, &index);
790
14.1k
        if (code < 0)
791
2.55k
            goto error1;
792
793
11.6k
        i = 1;
794
93.1k
        do {
795
93.1k
            if (pdfi_type_of(Value) == PDF_DICT || pdfi_type_of(Value) == PDF_STREAM) {
796
92.9k
                code = pdfi_dict_from_obj(ctx, Value, &instance_dict);
797
92.9k
                if (code < 0)
798
0
                    goto transparency_exit;
799
800
92.9k
                code = pdfi_check_Pattern(ctx, instance_dict, page_dict, tracker);
801
92.9k
                if (code < 0)
802
0
                    goto transparency_exit;
803
92.9k
            }
804
805
93.1k
            pdfi_countdown(Key);
806
93.1k
            Key = NULL;
807
93.1k
            pdfi_countdown(Value);
808
93.1k
            instance_dict = NULL;
809
93.1k
            Value = NULL;
810
93.1k
            (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the Shading dictionary loop */
811
812
93.1k
            code = pdfi_loop_detector_mark(ctx); /* Mark the new start of the Shading dictionary loop */
813
93.1k
            if (code < 0)
814
0
                goto error1;
815
816
206k
            do {
817
206k
                if (i++ >= pdfi_dict_entries(pattern_dict)) {
818
11.6k
                    code = 0;
819
11.6k
                    goto transparency_exit;
820
11.6k
                }
821
822
194k
                code = pdfi_dict_next(ctx, pattern_dict, &Key, &Value, &index);
823
194k
                if (code == 0 && (pdfi_type_of(Value) == PDF_DICT || pdfi_type_of(Value) == PDF_STREAM))
824
81.4k
                    break;
825
112k
                pdfi_countdown(Key);
826
112k
                Key = NULL;
827
112k
                pdfi_countdown(Value);
828
112k
                Value = NULL;
829
112k
            } while(1);
830
93.1k
        }while (1);
831
11.6k
    }
832
1.47k
    return 0;
833
834
11.6k
transparency_exit:
835
11.6k
    pdfi_countdown(Key);
836
11.6k
    pdfi_countdown(Value);
837
11.6k
    pdfi_countdown(instance_dict);
838
839
14.1k
error1:
840
14.1k
    (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */
841
14.1k
    return code;
842
11.6k
}
843
844
/*
845
 * This routine checks a Font dictionary to see if it contains any spot
846
 * colour definitions, or transparency usage. While we are here, if the tracker's font_array
847
 * is not NULL, pick up the font information and store it in the array.
848
 */
849
static int pdfi_check_Font(pdf_context *ctx, pdf_dict *font, pdf_dict *page_dict,
850
                           pdfi_check_tracker_t *tracker)
851
451k
{
852
451k
    int code = 0;
853
451k
    pdf_obj *o = NULL;
854
855
451k
    if (resource_is_checked(tracker, (pdf_obj *)font))
856
53.7k
        return 0;
857
858
397k
    if (pdfi_type_of(font) != PDF_DICT)
859
0
        return_error(gs_error_typecheck);
860
861
397k
    if (tracker->font_array != NULL) {
862
        /* If we get to here this is a font we have not seen before. We need
863
         * to make a new font array big enough to hold the existing entries +1
864
         * copy the existing entries to the new array and free the old array.
865
         * Finally create a dictionary with all the font information we want
866
         * and add it to the array.
867
         */
868
0
        pdf_array *new_fonts = NULL;
869
0
        int index = 0;
870
0
        pdf_obj *array_obj = NULL;
871
0
        pdf_dict *font_info_dict = NULL;
872
873
        /* Let's start by gathering the information we need and storing it in a dictionary */
874
0
        code = pdfi_dict_alloc(ctx, 4, &font_info_dict);
875
0
        if (code < 0)
876
0
            return code;
877
0
        pdfi_countup(font_info_dict);
878
879
0
        if (font->object_num != 0) {
880
0
            pdf_num *int_obj = NULL;
881
882
0
            code = pdfi_object_alloc(ctx, PDF_INT, 0, (pdf_obj **)&int_obj);
883
0
            if (code >= 0) {
884
0
                pdfi_countup(int_obj);
885
0
                int_obj->value.i = font->object_num;
886
0
                code = pdfi_dict_put(ctx, font_info_dict, "ObjectNum", (pdf_obj *)int_obj);
887
0
                pdfi_countdown(int_obj);
888
0
            }
889
0
            if (code < 0) {
890
0
                pdfi_countdown(font_info_dict);
891
0
                return code;
892
0
            }
893
0
        }
894
895
0
        code = pdfi_dict_get(ctx, font, "BaseFont", &array_obj);
896
0
        if (code >= 0) {
897
0
            code = pdfi_dict_put(ctx, font_info_dict, "BaseFont", array_obj);
898
0
            if (code < 0) {
899
0
                pdfi_countdown(array_obj);
900
0
                pdfi_countdown(font_info_dict);
901
0
                return code;
902
0
            }
903
0
        }
904
0
        pdfi_countdown(array_obj);
905
0
        array_obj = NULL;
906
907
0
        code = pdfi_dict_get(ctx, font, "ToUnicode", &array_obj);
908
0
        if (code >= 0)
909
0
            code = pdfi_dict_put(ctx, font_info_dict, "ToUnicode", PDF_TRUE_OBJ);
910
0
        else
911
0
            code = pdfi_dict_put(ctx, font_info_dict, "ToUnicode", PDF_FALSE_OBJ);
912
0
        pdfi_countdown(array_obj);
913
0
        array_obj = NULL;
914
0
        if (code < 0)
915
0
            return code;
916
917
0
        code = pdfi_dict_get(ctx, font, "FontDescriptor", &array_obj);
918
0
        if (code >= 0) {
919
0
            bool known = false;
920
921
0
            (void)pdfi_dict_known(ctx, (pdf_dict *)array_obj, "FontFile", &known);
922
0
            if (!known) {
923
0
                (void)pdfi_dict_known(ctx, (pdf_dict *)array_obj, "FontFile2", &known);
924
0
                if (!known) {
925
0
                    (void)pdfi_dict_known(ctx, (pdf_dict *)array_obj, "FontFile3", &known);
926
0
                }
927
0
            }
928
929
0
            if (known > 0)
930
0
                code = pdfi_dict_put(ctx, font_info_dict, "Embedded", PDF_TRUE_OBJ);
931
0
            else
932
0
                code = pdfi_dict_put(ctx, font_info_dict, "Embedded", PDF_FALSE_OBJ);
933
0
        } else
934
0
            code = pdfi_dict_put(ctx, font_info_dict, "Embedded", PDF_FALSE_OBJ);
935
936
0
        pdfi_countdown(array_obj);
937
0
        array_obj = NULL;
938
939
0
        if (code < 0)
940
0
            return code;
941
942
943
0
        code = pdfi_dict_knownget_type(ctx, font, "Subtype", PDF_NAME, &array_obj);
944
0
        if (code > 0) {
945
0
            code = pdfi_dict_put(ctx, font_info_dict, "Subtype", array_obj);
946
0
            if (code < 0) {
947
0
                pdfi_countdown(array_obj);
948
0
                pdfi_countdown(font_info_dict);
949
0
                return code;
950
0
            }
951
952
0
            if (pdfi_name_is((pdf_name *)array_obj, "Type3")) {
953
0
                pdfi_countdown(o);
954
0
                o = NULL;
955
956
0
                code = pdfi_dict_knownget_type(ctx, font, "Resources", PDF_DICT, &o);
957
0
                if (code > 0)
958
0
                    (void)pdfi_check_Resources(ctx, (pdf_dict *)o, page_dict, tracker);
959
0
            }
960
961
0
            if (pdfi_name_is((const pdf_name *)array_obj, "Type0")){
962
0
                pdf_array *descendants = NULL;
963
0
                pdf_dict *desc_font = NULL;
964
965
0
                code = pdfi_dict_get(ctx, font, "DescendantFonts", (pdf_obj **)&descendants);
966
0
                if (code >= 0) {
967
0
                    code = pdfi_array_get(ctx, descendants, 0, (pdf_obj **)&desc_font);
968
0
                    if (code >= 0){
969
0
                        pdf_array *desc_array = NULL;
970
971
0
                        code = pdfi_array_alloc(ctx, 0, &desc_array);
972
0
                        if (code >= 0) {
973
0
                            pdf_array *saved = tracker->font_array;
974
975
0
                            pdfi_countup(desc_array);
976
0
                            tracker->font_array = desc_array;
977
0
                            (void)pdfi_check_Font(ctx, desc_font, page_dict, tracker);
978
0
                            (void)pdfi_dict_put(ctx, font_info_dict, "Descendants", (pdf_obj *)tracker->font_array);
979
0
                            pdfi_countdown((pdf_obj *)tracker->font_array);
980
0
                            tracker->font_array = saved;
981
0
                        }
982
0
                        pdfi_countdown(descendants);
983
0
                        pdfi_countdown(desc_font);
984
0
                    }
985
0
                }
986
0
            }
987
0
        }
988
0
        pdfi_countdown(array_obj);
989
0
        array_obj = NULL;
990
991
0
        code = pdfi_array_alloc(ctx, pdfi_array_size(tracker->font_array) + 1, &new_fonts);
992
0
        if (code < 0) {
993
0
            pdfi_countdown(font_info_dict);
994
0
            return code;
995
0
        }
996
0
        pdfi_countup(new_fonts);
997
998
0
        for (index = 0; index < pdfi_array_size(tracker->font_array); index++) {
999
0
            code = pdfi_array_get(ctx, tracker->font_array, index, &array_obj);
1000
0
            if (code < 0) {
1001
0
                pdfi_countdown(font_info_dict);
1002
0
                pdfi_countdown(new_fonts);
1003
0
                return code;
1004
0
            }
1005
0
            code = pdfi_array_put(ctx, new_fonts, index, array_obj);
1006
0
            pdfi_countdown(array_obj);
1007
0
            if (code < 0) {
1008
0
                pdfi_countdown(font_info_dict);
1009
0
                pdfi_countdown(new_fonts);
1010
0
                return code;
1011
0
            }
1012
0
        }
1013
0
        code = pdfi_array_put(ctx, new_fonts, index, (pdf_obj *)font_info_dict);
1014
0
        if (code < 0) {
1015
0
            pdfi_countdown(font_info_dict);
1016
0
            pdfi_countdown(new_fonts);
1017
0
            return code;
1018
0
        }
1019
0
        pdfi_countdown(font_info_dict);
1020
0
        pdfi_countdown(tracker->font_array);
1021
0
        tracker->font_array = new_fonts;
1022
397k
    } else {
1023
397k
        code = pdfi_dict_knownget_type(ctx, font, "Subtype", PDF_NAME, &o);
1024
397k
        if (code > 0) {
1025
394k
            if (pdfi_name_is((pdf_name *)o, "Type3")) {
1026
8.68k
                pdfi_countdown(o);
1027
8.68k
                o = NULL;
1028
1029
8.68k
                code = pdfi_dict_knownget_type(ctx, font, "Resources", PDF_DICT, &o);
1030
8.68k
                if (code > 0)
1031
2.88k
                    (void)pdfi_check_Resources(ctx, (pdf_dict *)o, page_dict, tracker);
1032
8.68k
            }
1033
394k
        }
1034
1035
397k
        pdfi_countdown(o);
1036
397k
        o = NULL;
1037
397k
    }
1038
1039
397k
    return 0;
1040
397k
}
1041
1042
/*
1043
 * Check the Resources dictionary Font entry.
1044
 */
1045
static int pdfi_check_Font_dict(pdf_context *ctx, pdf_dict *font_dict, pdf_dict *page_dict,
1046
                                pdfi_check_tracker_t *tracker)
1047
771k
{
1048
771k
    int code = 0;
1049
771k
    uint64_t i, index;
1050
771k
    pdf_obj *Key = NULL, *Value = NULL;
1051
1052
771k
    if (resource_is_checked(tracker, (pdf_obj *)font_dict))
1053
126
        return 0;
1054
1055
771k
    if (pdfi_type_of(font_dict) != PDF_DICT)
1056
531k
        return_error(gs_error_typecheck);
1057
1058
240k
    if (pdfi_dict_entries(font_dict) > 0) {
1059
239k
        code = pdfi_loop_detector_mark(ctx); /* Mark the start of the Font dictionary loop */
1060
239k
        if (code < 0)
1061
0
            return code;
1062
1063
        /* We don't want to store the dereferenced Font objects in the Resources dictionary
1064
         * Because we later create a substitute object, if we use the object here we will not
1065
         * find the correct font if we reference it through the Resources dictionary.
1066
         */
1067
239k
        code = pdfi_dict_first_no_store_R(ctx, font_dict, &Key, &Value, &index);
1068
239k
        if (code < 0) {
1069
48.9k
            (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */
1070
48.9k
            goto error1;
1071
48.9k
        }
1072
1073
190k
        i = 1;
1074
460k
        do {
1075
460k
            if (pdfi_type_of(Value) == PDF_DICT)
1076
337k
                code = pdfi_check_Font(ctx, (pdf_dict *)Value, page_dict, tracker);
1077
123k
            else if (pdfi_type_of(Value) == PDF_FONT) {
1078
113k
                pdf_dict *d = ((pdf_font *)Value)->PDF_font;
1079
1080
113k
                code = pdfi_check_Font(ctx, d, page_dict, tracker);
1081
113k
            } else {
1082
9.62k
                pdfi_set_warning(ctx, 0, NULL, W_PDF_FONTRESOURCE_TYPE, "pdfi_check_Font_dict", "");
1083
9.62k
            }
1084
460k
            if (code < 0)
1085
0
                break;
1086
1087
460k
            pdfi_countdown(Key);
1088
460k
            Key = NULL;
1089
460k
            pdfi_countdown(Value);
1090
460k
            Value = NULL;
1091
1092
460k
            (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the Font dictionary loop */
1093
1094
460k
            code = pdfi_loop_detector_mark(ctx); /* Mark the new start of the Font dictionary loop */
1095
460k
            if (code < 0)
1096
0
                goto error1;
1097
1098
460k
            if (i++ >= pdfi_dict_entries(font_dict)) {
1099
167k
                code = 0;
1100
167k
                break;
1101
167k
            }
1102
1103
293k
            code = pdfi_dict_next_no_store_R(ctx, font_dict, &Key, &Value, &index);
1104
293k
            if (code < 0)
1105
23.3k
                break;
1106
293k
        }while (1);
1107
1108
190k
        (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */
1109
190k
    }
1110
1111
191k
    pdfi_countdown(Key);
1112
191k
    pdfi_countdown(Value);
1113
1114
240k
error1:
1115
240k
    return code;
1116
191k
}
1117
1118
static int pdfi_check_Resources(pdf_context *ctx, pdf_dict *Resources_dict,
1119
                                pdf_dict *page_dict, pdfi_check_tracker_t *tracker)
1120
773k
{
1121
773k
    int code;
1122
773k
    pdf_obj *d = NULL;
1123
1124
773k
    if (resource_is_checked(tracker, (pdf_obj *)Resources_dict))
1125
1.17k
        return 0;
1126
1127
771k
    if (pdfi_type_of(Resources_dict) != PDF_DICT)
1128
0
        return_error(gs_error_typecheck);
1129
1130
    /* First up, check any colour spaces, for new spot colours.
1131
     * We only do this if asked because its expensive. spot_dict being NULL
1132
     * means we aren't interested in spot colours (not a DeviceN or Separation device)
1133
     */
1134
771k
    if (tracker->spot_dict != NULL) {
1135
78.4k
        code = pdfi_dict_knownget_type(ctx, Resources_dict, "ColorSpace", PDF_DICT, &d);
1136
78.4k
        if (code < 0 && code != gs_error_undefined) {
1137
26
            if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_RESOURCE, "pdfi_check_Resources", "ColorSpace")) < 0)
1138
0
                return code;
1139
26
        }
1140
78.4k
        (void)pdfi_check_ColorSpace_dict(ctx, (pdf_dict *)d, page_dict, tracker);
1141
1142
78.4k
        pdfi_countdown(d);
1143
78.4k
        d = NULL;
1144
1145
        /* Put the Resources dictionary back in cache (or promote it) in case checking ColorSpace dict
1146
         * created so many cache entires it flushed Resources from cache
1147
         */
1148
78.4k
        code = pdfi_cache_object(ctx, (pdf_obj *)Resources_dict);
1149
78.4k
        if (code < 0) {
1150
0
            if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_CACHE_FAIL, "pdfi_check_Resources", "")) < 0)
1151
0
                return code;
1152
0
        }
1153
1154
78.4k
        code = pdfi_dict_knownget_type(ctx, Resources_dict, "Shading", PDF_DICT, &d);
1155
78.4k
        if (code < 0 && code != gs_error_undefined) {
1156
14
            if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_RESOURCE, "pdfi_check_Resources", "Shading")) < 0)
1157
0
                return code;
1158
14
        }
1159
78.4k
        (void)pdfi_check_Shading_dict(ctx, (pdf_dict *)d, page_dict, tracker);
1160
78.4k
        pdfi_countdown(d);
1161
78.4k
        d = NULL;
1162
1163
        /* Put the Resources dictionary back in cache (or promote it) in case checking Shading dict
1164
         * created so many cache entires it flushed Resources from cache
1165
         */
1166
78.4k
        code = pdfi_cache_object(ctx, (pdf_obj *)Resources_dict);
1167
78.4k
        if (code < 0) {
1168
0
            if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_CACHE_FAIL, "pdfi_check_Resources", "")) < 0)
1169
0
                return code;
1170
0
        }
1171
78.4k
    }
1172
1173
771k
    code = pdfi_dict_knownget_type(ctx, Resources_dict, "XObject", PDF_DICT, &d);
1174
771k
    if (code < 0 && code != gs_error_undefined) {
1175
194
        if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_RESOURCE, "pdfi_check_Resources", "XObject")) < 0)
1176
0
            return code;
1177
194
    }
1178
771k
    (void)pdfi_check_XObject_dict(ctx, (pdf_dict *)d, page_dict, tracker);
1179
771k
    pdfi_countdown(d);
1180
771k
    d = NULL;
1181
1182
    /* Put the Resources dictionary back in cache (or promote it) in case checking XObject dict
1183
     * created so many cache entires it flushed Resources from cache
1184
     */
1185
771k
    code = pdfi_cache_object(ctx, (pdf_obj *)Resources_dict);
1186
771k
    if (code < 0) {
1187
0
        if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_CACHE_FAIL, "pdfi_check_Resources", "")) < 0)
1188
0
            return code;
1189
0
    }
1190
1191
771k
    code = pdfi_dict_knownget_type(ctx, Resources_dict, "Pattern", PDF_DICT, &d);
1192
771k
    if (code < 0 && code != gs_error_undefined) {
1193
161
        if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_RESOURCE, "pdfi_check_Resources", "Pattern")) < 0) {
1194
0
            return code;
1195
0
        }
1196
161
    }
1197
771k
    (void)pdfi_check_Pattern_dict(ctx, (pdf_dict *)d, page_dict, tracker);
1198
771k
    pdfi_countdown(d);
1199
771k
    d = NULL;
1200
1201
    /* Put the Resources dictionary back in cache (or promote it) in case checking Pattern dict
1202
     * created so many cache entires it flushed Resources from cache
1203
     */
1204
771k
    code = pdfi_cache_object(ctx, (pdf_obj *)Resources_dict);
1205
771k
    if (code < 0) {
1206
0
        if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_CACHE_FAIL, "pdfi_check_Resources", "")) < 0)
1207
0
            return code;
1208
0
    }
1209
1210
771k
    code = pdfi_dict_knownget_type(ctx, Resources_dict, "Font", PDF_DICT, &d);
1211
771k
    if (code < 0) {
1212
7.53k
        if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_RESOURCE, "pdfi_check_Resources", "Font")) < 0)
1213
0
            return code;
1214
7.53k
    }
1215
771k
    (void)pdfi_check_Font_dict(ctx, (pdf_dict *)d, page_dict, tracker);
1216
    /* From this point onwards, if we detect transparency (or have already detected it) we
1217
     * can exit, we have already counted up any spot colours.
1218
     */
1219
771k
    pdfi_countdown(d);
1220
771k
    d = NULL;
1221
1222
    /* Put the Resources dictionary back in cache (or promote it) in case checking Font dict
1223
     * created so many cache entires it flushed Resources from cache
1224
     */
1225
771k
    code = pdfi_cache_object(ctx, (pdf_obj *)Resources_dict);
1226
771k
    if (code < 0) {
1227
0
        if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_CACHE_FAIL, "pdfi_check_Resources", "")) < 0)
1228
0
            return code;
1229
0
    }
1230
1231
771k
    code = pdfi_dict_knownget_type(ctx, Resources_dict, "ExtGState", PDF_DICT, &d);
1232
771k
    if (code < 0 && code != gs_error_undefined) {
1233
107
        if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_RESOURCE, "pdfi_check_Resources", "ExtGState")) < 0)
1234
0
            return code;
1235
107
    }
1236
771k
    (void)pdfi_check_ExtGState_dict(ctx, (pdf_dict *)d, page_dict, tracker);
1237
771k
    pdfi_countdown(d);
1238
771k
    d = NULL;
1239
1240
    /* Put the Resources dictionary back in cache (or promote it) in case checking ExtGState dict
1241
     * created so many cache entires it flushed Resources from cache
1242
     */
1243
771k
    code = pdfi_cache_object(ctx, (pdf_obj *)Resources_dict);
1244
771k
    if (code < 0) {
1245
0
        if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_CACHE_FAIL, "pdfi_check_Resources", "")) < 0)
1246
0
            return code;
1247
0
    }
1248
1249
771k
    return 0;
1250
771k
}
1251
1252
static int pdfi_check_annot_for_transparency(pdf_context *ctx, pdf_dict *annot, pdf_dict *page_dict,
1253
                                             pdfi_check_tracker_t *tracker)
1254
1.18M
{
1255
1.18M
    int code;
1256
1.18M
    pdf_name *n;
1257
1.18M
    pdf_obj *N = NULL;
1258
1.18M
    pdf_dict *ap = NULL;
1259
1.18M
    pdf_dict *Resources = NULL;
1260
1.18M
    double f;
1261
1262
1.18M
    if (resource_is_checked(tracker, (pdf_obj *)annot))
1263
108
        return 0;
1264
1265
1.18M
    if (pdfi_type_of(annot) != PDF_DICT)
1266
0
        return_error(gs_error_typecheck);
1267
1268
    /* Check #1 Does the (Normal) Appearnce stream use any Resources which include transparency.
1269
     * We check this first, because this also checks for spot colour spaces. Once we've done that we
1270
     * can exit the checks as soon as we detect transparency.
1271
     */
1272
1.18M
    code = pdfi_dict_knownget_type(ctx, annot, "AP", PDF_DICT, (pdf_obj **)&ap);
1273
1.18M
    if (code > 0)
1274
552k
    {
1275
        /* Fetch without resolving indirect ref because pdfmark wants it that way later */
1276
552k
        code = pdfi_dict_get_no_store_R(ctx, ap, "N", (pdf_obj **)&N);
1277
552k
        if (code >= 0) {
1278
430k
            pdf_dict *dict = NULL;
1279
1280
430k
            code = pdfi_dict_from_obj(ctx, N, &dict);
1281
430k
            if (code == 0)
1282
430k
                code = pdfi_dict_knownget_type(ctx, dict, "Resources", PDF_DICT, (pdf_obj **)&Resources);
1283
430k
            if (code > 0)
1284
288k
                code = pdfi_check_Resources(ctx, (pdf_dict *)Resources, page_dict, tracker);
1285
430k
        }
1286
552k
        if (code == gs_error_undefined)
1287
113k
            code = 0;
1288
552k
    }
1289
1.18M
    pdfi_countdown(ap);
1290
1.18M
    pdfi_countdown(N);
1291
1.18M
    pdfi_countdown(Resources);
1292
1293
1.18M
    if (code < 0)
1294
28.5k
        return code;
1295
    /* We've checked the Resources, and nothing else in an annotation can define spot colours, so
1296
     * if we detected transparency in the Resources we need not check further.
1297
     */
1298
1.15M
    if (tracker->transparent == true)
1299
17.9k
        return 0;
1300
1301
1.13M
    code = pdfi_dict_get_type(ctx, annot, "Subtype", PDF_NAME, (pdf_obj **)&n);
1302
1.13M
    if (code < 0) {
1303
1.41k
        if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_TYPE, "pdfi_check_annot_for_transparency", "")) < 0) {
1304
0
            return code;
1305
0
        }
1306
1.13M
    } else {
1307
        /* Check #2, Highlight annotations are always preformed with transparency */
1308
1.13M
        if (pdfi_name_is((const pdf_name *)n, "Highlight")) {
1309
3.48k
            pdfi_countdown(n);
1310
3.48k
            tracker->transparent = true;
1311
3.48k
            return 0;
1312
3.48k
        }
1313
1.13M
        pdfi_countdown(n);
1314
1.13M
        n = NULL;
1315
1316
        /* Check #3 Blend Mode (BM) not being 'Normal' or 'Compatible' */
1317
1.13M
        code = pdfi_dict_knownget_type(ctx, annot, "BM", PDF_NAME, (pdf_obj **)&n);
1318
1.13M
        if (code > 0) {
1319
0
            if (!pdfi_name_is((const pdf_name *)n, "Normal")) {
1320
0
                if (!pdfi_name_is((const pdf_name *)n, "Compatible")) {
1321
0
                    pdfi_countdown(n);
1322
0
                    tracker->transparent = true;
1323
0
                    return 0;
1324
0
                }
1325
0
            }
1326
0
            code = 0;
1327
0
        }
1328
1.13M
        pdfi_countdown(n);
1329
1.13M
        if (code < 0)
1330
0
            return code;
1331
1332
        /* Check #4 stroke constant alpha (CA) is not 1 (100% opaque) */
1333
1.13M
        code = pdfi_dict_knownget_number(ctx, annot, "CA", &f);
1334
1.13M
        if (code > 0) {
1335
2.17k
            if (f != 1.0) {
1336
254
                tracker->transparent = true;
1337
254
                return 0;
1338
254
            }
1339
2.17k
        }
1340
1.13M
        if (code < 0)
1341
0
            return code;
1342
1343
        /* Check #5 non-stroke constant alpha (ca) is not 1 (100% opaque) */
1344
1.13M
        code = pdfi_dict_knownget_number(ctx, annot, "ca", &f);
1345
1.13M
        if (code > 0) {
1346
0
            if (f != 1.0) {
1347
0
                tracker->transparent = true;
1348
0
                return 0;
1349
0
            }
1350
0
        }
1351
1.13M
        if (code < 0)
1352
0
            return code;
1353
1.13M
    }
1354
1355
1.13M
    return 0;
1356
1.13M
}
1357
1358
static int pdfi_check_Annots_for_transparency(pdf_context *ctx, pdf_array *annots_array,
1359
                                              pdf_dict *page_dict, pdfi_check_tracker_t *tracker)
1360
63.8k
{
1361
63.8k
    int i, code = 0;
1362
63.8k
    pdf_dict *annot = NULL;
1363
1364
63.8k
    if (resource_is_checked(tracker, (pdf_obj *)annots_array))
1365
0
        return 0;
1366
1367
63.8k
    if (pdfi_type_of(annots_array) != PDF_ARRAY)
1368
0
        return_error(gs_error_typecheck);
1369
1370
1.40M
    for (i=0; i < pdfi_array_size(annots_array); i++) {
1371
1.35M
        code = pdfi_array_get_type(ctx, annots_array, (uint64_t)i, PDF_DICT, (pdf_obj **)&annot);
1372
1.35M
        if (code >= 0) {
1373
1.18M
            code = pdfi_check_annot_for_transparency(ctx, annot, page_dict, tracker);
1374
1.18M
            if (code < 0 && (code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_INVALID_TRANS_XOBJECT, "pdfi_check_Annots_for_transparency", "")) < 0) {
1375
0
                goto exit;
1376
0
            }
1377
1378
            /* If we've found transparency, and don't need to continue checkign for spot colours
1379
             * just exit as fast as possible.
1380
             */
1381
1.18M
            if (tracker->transparent == true && tracker->spot_dict == NULL)
1382
12.4k
                goto exit;
1383
1384
1.17M
            pdfi_countdown(annot);
1385
1.17M
            annot = NULL;
1386
1.17M
        }
1387
171k
        else if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_INVALID_TRANS_XOBJECT, "pdfi_check_Annots_for_transparency", "")) < 0) {
1388
0
            goto exit;
1389
0
        }
1390
1.35M
    }
1391
63.8k
exit:
1392
63.8k
    pdfi_countdown(annot);
1393
63.8k
    return code;
1394
63.8k
}
1395
1396
/* Check for transparency and spots on page.
1397
 *
1398
 * Sets ctx->spot_capable_device
1399
 * Builds a dictionary of the unique spot names in spot_dict
1400
 * Set 'transparent' to true if there is transparency on the page
1401
 *
1402
 * From the original PDF interpreter written in PostScript:
1403
 * Note: we deliberately don't check to see whether a Group is defined,
1404
 * because Adobe Illustrator 10 (and possibly other applications) define
1405
 * a page-level group whether transparency is actually used or not.
1406
 * Ignoring the presence of Group is justified because, in the absence
1407
 * of any other transparency features, they have no effect.
1408
 */
1409
static int pdfi_check_page_inner(pdf_context *ctx, pdf_dict *page_dict,
1410
                                 pdfi_check_tracker_t *tracker)
1411
247k
{
1412
247k
    int code;
1413
247k
    pdf_dict *Resources = NULL;
1414
247k
    pdf_array *Annots = NULL;
1415
247k
    pdf_dict *Group = NULL;
1416
247k
    pdf_obj *CS = NULL;
1417
1418
247k
    tracker->transparent = false;
1419
1420
247k
    if (pdfi_type_of(page_dict) != PDF_DICT)
1421
4
        return_error(gs_error_typecheck);
1422
1423
1424
    /* Check if the page dictionary has a page Group entry (for spots).
1425
     * Page group should mean the page has transparency but we ignore it for the purposes
1426
     * of transparency detection. See above.
1427
     */
1428
247k
    if (tracker->spot_dict) {
1429
31.1k
        code = pdfi_dict_knownget_type(ctx, page_dict, "Group", PDF_DICT, (pdf_obj **)&Group);
1430
31.1k
        if (code > 0) {
1431
            /* If Group has a ColorSpace (CS), then check it for spot colours */
1432
4.85k
            code = pdfi_dict_knownget(ctx, Group, "CS", &CS);
1433
4.85k
            if (code > 0)
1434
4.75k
                code = pdfi_check_ColorSpace_for_spots(ctx, CS, Group, page_dict, tracker->spot_dict);
1435
1436
4.85k
            if (code < 0) {
1437
76
                if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_GS_LIB_ERROR, "pdfi_check_page_inner", "")) < 0) {
1438
0
                    goto exit;
1439
0
                }
1440
76
            }
1441
4.85k
        }
1442
31.1k
    }
1443
1444
    /* Now check any Resources dictionary in the Page dictionary */
1445
247k
    code = pdfi_dict_knownget_type(ctx, page_dict, "Resources", PDF_DICT, (pdf_obj **)&Resources);
1446
247k
    if (code > 0)
1447
232k
        code = pdfi_check_Resources(ctx, Resources, page_dict, tracker);
1448
1449
247k
    if (code == gs_error_pdf_stackoverflow || (code < 0 &&
1450
247k
       (code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_GS_LIB_ERROR, "pdfi_check_page_inner", "")) < 0)) {
1451
0
        goto exit;
1452
0
    }
1453
1454
    /* If we are drawing Annotations, check to see if the page uses any Annots */
1455
247k
    if (ctx->args.showannots) {
1456
247k
        code = pdfi_dict_knownget_type(ctx, page_dict, "Annots", PDF_ARRAY, (pdf_obj **)&Annots);
1457
247k
        if (code > 0)
1458
63.8k
            code = pdfi_check_Annots_for_transparency(ctx, Annots, page_dict,
1459
63.8k
                                                      tracker);
1460
247k
        if (code < 0) {
1461
2.86k
            if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_GS_LIB_ERROR, "pdfi_check_page_inner", "")) < 0) {
1462
0
               goto exit;
1463
0
            }
1464
2.86k
        }
1465
247k
    }
1466
1467
247k
    code = 0;
1468
247k
 exit:
1469
247k
    pdfi_countdown(Resources);
1470
247k
    pdfi_countdown(Annots);
1471
247k
    pdfi_countdown(CS);
1472
247k
    pdfi_countdown(Group);
1473
247k
    return code;
1474
247k
}
1475
1476
/* Checks page for transparency, and sets up device for spots, if applicable
1477
 * Sets ctx->page.has_transparency and ctx->page.num_spots
1478
 * do_setup -- indicates whether to actually set up the device with the spot count.
1479
 */
1480
int pdfi_check_page(pdf_context *ctx, pdf_dict *page_dict, pdf_array **fonts_array, pdf_array **spots_array, bool do_setup)
1481
247k
{
1482
247k
    int code;
1483
247k
    int spots = 0;
1484
247k
    pdfi_check_tracker_t tracker;
1485
247k
    pdf_dict *Resources = NULL;
1486
1487
247k
    ctx->page.num_spots = 0;
1488
247k
    ctx->page.has_transparency = false;
1489
1490
247k
    code = pdfi_check_init_tracker(ctx, &tracker, fonts_array, spots_array);
1491
247k
    if (code < 0)
1492
0
        goto exit;
1493
1494
    /* Check for spots and transparency in this page */
1495
247k
    code = pdfi_check_page_inner(ctx, page_dict, &tracker);
1496
247k
    if (code < 0)
1497
4
        goto exit;
1498
1499
    /* Count the spots */
1500
247k
    if (tracker.spot_dict)
1501
31.1k
        spots = pdfi_dict_entries(tracker.spot_dict);
1502
1503
    /* If setup requested, tell the device about spots and transparency */
1504
247k
    if (do_setup) {
1505
0
        gs_c_param_list list;
1506
0
        int a = 0;
1507
0
        pdf_name *Key = NULL;
1508
0
        pdf_obj *Value = NULL;
1509
0
        uint64_t index = 0;
1510
1511
0
        gs_c_param_list_write(&list, ctx->memory);
1512
1513
        /* If there are spot colours (and by inference, the device renders spot plates) then
1514
         * send the number of Spots to the device, so it can setup correctly.
1515
         */
1516
0
        if (tracker.spot_dict) {
1517
            /* There is some awkwardness here. If the SeparationColorNames setting
1518
             * fails, we want to ignore it (this can mean that we exceeded the maximum
1519
             * number of colourants and some will be converted to CMYK). But if that happens,
1520
             * any other parameters in the same list which haven't already been prcoessed
1521
             * will be lost. So we need to send two lists, the SeparationColorNames and
1522
             * 'everything else'.
1523
             */
1524
0
            if (spots > 0) {
1525
0
                gs_param_string_array sa;
1526
0
                gs_param_string *table = NULL;
1527
1528
0
                table = (gs_param_string *)gs_alloc_byte_array(ctx->memory, spots, sizeof(gs_param_string), "SeparationNames");
1529
0
                if (table != NULL)
1530
0
                {
1531
0
                    memset(table, 0x00, spots * sizeof(gs_param_string));
1532
1533
0
                    code = pdfi_dict_first(ctx, tracker.spot_dict, (pdf_obj **)&Key, &Value, &index);
1534
0
                    while (code >= 0)
1535
0
                    {
1536
0
                        if (pdfi_type_of(Key) == PDF_NAME) {
1537
0
                            table[a].data = ((pdf_string *)Key)->data;
1538
0
                            table[a].size = ((pdf_string *)Key)->length;
1539
0
                            table[a++].persistent = false;
1540
0
                        }
1541
                        /* Although we count down the returned PDF objects here, the pointers
1542
                         * to the name data remain valid and won't move. Provided we don't
1543
                         * retain the pointers after we free the tracker dictionary this is
1544
                         * safe to do.
1545
                         */
1546
0
                        pdfi_countdown(Key);
1547
0
                        Key = NULL;
1548
0
                        pdfi_countdown(Value);
1549
0
                        Value = NULL;
1550
0
                        code = pdfi_dict_next(ctx, tracker.spot_dict, (pdf_obj **)&Key, &Value, &index);
1551
0
                    }
1552
0
                    sa.data = table;
1553
0
                    sa.size = spots;
1554
0
                    sa.persistent = false;
1555
1556
0
                    (void)param_write_string_array((gs_param_list *)&list, "SeparationColorNames", &sa);
1557
0
                    gs_c_param_list_read(&list);
1558
0
                    code = gs_putdeviceparams(ctx->pgs->device, (gs_param_list *)&list);
1559
0
                    gs_c_param_list_release(&list);
1560
1561
0
                    gs_free_object(ctx->memory, table, "SeparationNames");
1562
0
                    if (code > 0) {
1563
                        /* The device was closed, we need to reopen it */
1564
0
                        code = gs_setdevice_no_erase(ctx->pgs, ctx->pgs->device);
1565
0
                        if (code < 0)
1566
0
                            goto exit;
1567
0
                        gs_erasepage(ctx->pgs);
1568
0
                    }
1569
1570
                    /* Reset the list back to being writeable */
1571
0
                    gs_c_param_list_write(&list, ctx->memory);
1572
0
                }
1573
0
                else {
1574
0
                    code = gs_note_error(gs_error_VMerror);
1575
0
                    goto exit;
1576
0
                }
1577
0
            }
1578
            /* Update the number of spots */
1579
0
            param_write_int((gs_param_list *)&list, "PageSpotColors", &spots);
1580
0
        }
1581
        /* Update the page transparency */
1582
0
        (void)param_write_bool((gs_param_list *)&list, "PageUsesTransparency",
1583
0
                                    &tracker.transparent);
1584
0
        gs_c_param_list_read(&list);
1585
0
        code = gs_putdeviceparams(ctx->pgs->device, (gs_param_list *)&list);
1586
0
        gs_c_param_list_release(&list);
1587
1588
0
        if (code > 0) {
1589
            /* The device was closed, we need to reopen it */
1590
0
            code = gs_setdevice_no_erase(ctx->pgs, ctx->pgs->device);
1591
0
            if (code < 0)
1592
0
                goto exit;
1593
0
            gs_erasepage(ctx->pgs);
1594
0
        }
1595
0
    }
1596
1597
    /* Set our values in the context, for caller */
1598
247k
    if (!ctx->args.notransparency)
1599
247k
        ctx->page.has_transparency = tracker.transparent;
1600
247k
    ctx->page.num_spots = spots;
1601
247k
    ctx->page.has_OP = tracker.has_overprint;
1602
1603
    /* High level devices do not render overprint */
1604
247k
    if (ctx->device_state.HighLevelDevice)
1605
118k
        ctx->page.has_OP = false;
1606
1607
247k
 exit:
1608
247k
    if (fonts_array != NULL) {
1609
0
        *fonts_array = tracker.font_array;
1610
0
        pdfi_countup(*fonts_array);
1611
0
    }
1612
1613
247k
    if (spots_array != NULL && tracker.spot_dict != NULL && pdfi_dict_entries(tracker.spot_dict) != 0) {
1614
0
        pdf_array *new_array = NULL;
1615
0
        pdf_name *Key = NULL;
1616
0
        pdf_obj *Value = NULL;
1617
0
        uint64_t index = 0, a_index = 0;
1618
1619
0
        index = pdfi_dict_entries(tracker.spot_dict);
1620
1621
0
        code = pdfi_array_alloc(ctx, index, &new_array);
1622
0
        if (code < 0)
1623
0
            goto error;
1624
0
        pdfi_countup(new_array);
1625
1626
0
        code = pdfi_dict_first(ctx, tracker.spot_dict, (pdf_obj **)&Key, &Value, &index);
1627
0
        while (code >= 0)
1628
0
        {
1629
0
            if (pdfi_type_of(Key) == PDF_NAME) {
1630
0
                code = pdfi_array_put(ctx, new_array, a_index++, (pdf_obj *)Key);
1631
0
                if (code < 0) {
1632
0
                    pdfi_countdown(new_array);
1633
0
                    pdfi_countdown(Key);
1634
0
                    pdfi_countdown(Value);
1635
0
                    goto error;
1636
0
                }
1637
0
            }
1638
1639
0
            pdfi_countdown(Key);
1640
0
            Key = NULL;
1641
0
            pdfi_countdown(Value);
1642
0
            Value = NULL;
1643
0
            code = pdfi_dict_next(ctx, tracker.spot_dict, (pdf_obj **)&Key, &Value, &index);
1644
0
        }
1645
0
        *spots_array = new_array;
1646
0
    }
1647
1648
    /* Put the Resources dictionary back in cache (or promote it) in case checking the page
1649
     * created so many cache entires it flushed Resources from cache
1650
     */
1651
247k
    code = pdfi_dict_knownget_type(ctx, page_dict, "Resources", PDF_DICT, (pdf_obj **)&Resources);
1652
247k
    if (code > 0) {
1653
232k
        code = pdfi_cache_object(ctx, (pdf_obj *)Resources);
1654
232k
        if (code < 0)
1655
0
            code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_CACHE_FAIL, "pdfi_check_Resources", "");
1656
232k
        pdfi_countdown(Resources);
1657
232k
    }
1658
14.1k
    else {
1659
14.1k
        if (code < 0) {
1660
10.3k
            if (code == gs_error_undefined)
1661
3.69k
                code = 0;
1662
6.68k
            else
1663
6.68k
                code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_PAGE_RESOURCES, "pdfi_check_page", NULL);
1664
10.3k
        }
1665
14.1k
    }
1666
1667
247k
error:
1668
247k
    (void)pdfi_check_free_tracker(ctx, &tracker);
1669
247k
    return code;
1670
247k
}