Coverage Report

Created: 2025-06-24 07:01

/src/ghostpdl/pdf/pdf_optcontent.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
/* Optional Content routines */
17
18
#include "pdf_int.h"
19
#include "pdf_stack.h"
20
#include "pdf_misc.h"
21
#include "pdf_font_types.h"
22
#include "pdf_gstate.h"
23
#include "pdf_dict.h"
24
#include "pdf_array.h"
25
#include "pdf_doc.h"
26
#include "pdf_mark.h"
27
#include "pdf_optcontent.h"
28
#include "pdf_loop_detect.h"
29
30
31
/* Find the default value for an ocdict, based on contents of OCProperties */
32
/* NOTE: the spec says that if BaseState is present, it won't be set to "OFF",
33
 * but this doesn't seem to be the case (Bug 691491).  Also, the spec
34
 * says the ON and OFF arrays are redundant in certain cases.  We just
35
 * look at everything anyway.
36
 * Default is going to be visible unless anything here indicates that it
37
 * should be turned off.
38
 */
39
static bool
40
pdfi_get_default_OCG_val(pdf_context *ctx, pdf_dict *ocdict)
41
71.1k
{
42
71.1k
    bool is_visible = true;
43
71.1k
    pdf_dict *D = NULL;
44
71.1k
    pdf_obj *BaseState = NULL;
45
71.1k
    pdf_array *OFF = NULL;
46
71.1k
    pdf_array *ON = NULL;
47
71.1k
    int code;
48
49
71.1k
    if (ctx->OCProperties == NULL)
50
4.52k
        return is_visible;
51
52
66.6k
    code = pdfi_dict_knownget_type(ctx, ctx->OCProperties, "D", PDF_DICT, (pdf_obj **)&D);
53
66.6k
    if (code <= 0)
54
17
        goto cleanup;
55
56
66.6k
    code = pdfi_dict_knownget_type(ctx, D, "BaseState", PDF_NAME, &BaseState);
57
66.6k
    if (code < 0) {
58
0
        goto cleanup;
59
0
    }
60
66.6k
    if (code > 0) {
61
28
        if (pdfi_name_is((pdf_name *)BaseState, "OFF")) {
62
0
            is_visible = false;
63
0
        }
64
28
    }
65
66
66.6k
    if (!is_visible) {
67
0
        code = pdfi_dict_knownget_type(ctx, D, "ON", PDF_ARRAY, (pdf_obj **)&ON);
68
0
        if (code < 0)
69
0
            goto cleanup;
70
0
        if (code > 0) {
71
0
            if (pdfi_array_known(ctx, ON, (pdf_obj *)ocdict, NULL))
72
0
                is_visible = true;
73
0
        }
74
0
    }
75
76
66.6k
    if (is_visible) {
77
66.6k
        code = pdfi_dict_knownget_type(ctx, D, "OFF", PDF_ARRAY, (pdf_obj **)&OFF);
78
66.6k
        if (code < 0)
79
1.48k
            goto cleanup;
80
65.1k
        if (code > 0) {
81
64.0k
            if (pdfi_array_known(ctx, OFF, (pdf_obj *)ocdict, NULL))
82
61.9k
                is_visible = false;
83
64.0k
        }
84
65.1k
    }
85
86
87
66.6k
 cleanup:
88
66.6k
    pdfi_countdown(BaseState);
89
66.6k
    pdfi_countdown(D);
90
66.6k
    pdfi_countdown(OFF);
91
66.6k
    pdfi_countdown(ON);
92
66.6k
    return is_visible;
93
66.6k
}
94
95
/* Check Usage for an OCG */
96
static bool
97
pdfi_oc_check_OCG_usage(pdf_context *ctx, pdf_dict *ocdict)
98
4.25k
{
99
4.25k
    bool is_visible = true;
100
4.25k
    int code;
101
4.25k
    pdf_dict *Usage = NULL;
102
4.25k
    pdf_dict *dict = NULL;
103
4.25k
    pdf_obj *name = NULL;
104
105
    /* Check Usage to see if it has additional info */
106
4.25k
    code = pdfi_dict_knownget_type(ctx, ocdict, "Usage", PDF_DICT, (pdf_obj **)&Usage);
107
4.25k
    if (code <= 0) {
108
        /* No Usage, so we're done */
109
3.20k
        goto cleanup;
110
3.20k
    }
111
112
1.05k
    if (ctx->args.printed) {
113
1.05k
        code = pdfi_dict_knownget_type(ctx, Usage, "Print", PDF_DICT, (pdf_obj **)&dict);
114
1.05k
        if (code <= 0)
115
1.03k
            goto cleanup;
116
18
        code = pdfi_dict_knownget_type(ctx, dict, "PrintState", PDF_NAME, &name);
117
18
        if (code <= 0)
118
0
            goto cleanup;
119
18
    } else {
120
0
        code = pdfi_dict_knownget_type(ctx, Usage, "View", PDF_DICT, (pdf_obj **)&dict);
121
0
        if (code <= 0)
122
0
            goto cleanup;
123
0
        code = pdfi_dict_knownget_type(ctx, dict, "ViewState", PDF_NAME, &name);
124
0
        if (code <= 0)
125
0
            goto cleanup;
126
0
    }
127
18
    if (pdfi_name_strcmp((pdf_name *)name, "OFF") == 0) {
128
0
        is_visible = false;
129
0
    }
130
131
4.25k
 cleanup:
132
4.25k
    pdfi_countdown(Usage);
133
4.25k
    pdfi_countdown(dict);
134
4.25k
    pdfi_countdown(name);
135
136
4.25k
    return is_visible;
137
18
}
138
139
typedef enum {
140
    P_AnyOn,
141
    P_AllOn,
142
    P_AllOff,
143
    P_AnyOff
144
} ocmd_p_type;
145
146
static bool pdfi_oc_default_visibility(pdf_context *ctx)
147
3.36k
{
148
3.36k
    int code;
149
3.36k
    pdf_dict *D_dict = NULL;
150
3.36k
    pdf_name *B = NULL;
151
152
3.36k
    code = pdfi_dict_knownget_type(ctx, ctx->OCProperties, "D", PDF_DICT, (pdf_obj **)&D_dict);
153
3.36k
    if (code < 0 || D_dict == NULL)
154
198
        return true;
155
156
3.16k
    code = pdfi_dict_knownget_type(ctx, D_dict, "BaseState", PDF_NAME, (pdf_obj **)&B);
157
3.16k
    (void)pdfi_countdown(D_dict);
158
3.16k
    D_dict = NULL;
159
3.16k
    if (code < 0 || B == NULL)
160
3.16k
        return true;
161
162
0
    if (pdfi_name_is(B, "OFF")) {
163
0
        (void)pdfi_countdown(B);
164
0
        return false;
165
0
    }
166
167
0
    (void)pdfi_countdown(B);
168
0
    return true;
169
0
}
170
171
static bool
172
pdfi_oc_check_OCMD_array(pdf_context *ctx, pdf_array *array, ocmd_p_type type)
173
69.8k
{
174
69.8k
    bool is_visible, hit = false;
175
69.8k
    uint64_t i;
176
69.8k
    int code;
177
69.8k
    pdf_obj *val = NULL;
178
179
    /* Setup default */
180
69.8k
    switch (type) {
181
69.8k
    case P_AnyOn:
182
69.8k
    case P_AnyOff:
183
69.8k
        is_visible = false;
184
69.8k
        break;
185
0
    case P_AllOn:
186
0
    case P_AllOff:
187
0
        is_visible = true;
188
0
        break;
189
69.8k
    }
190
191
134k
    for (i=0; i<pdfi_array_size(array); i++) {
192
69.8k
        bool vis;
193
194
69.8k
        code = pdfi_array_get(ctx, array, i, &val);
195
69.8k
        if (code < 0) continue;
196
66.8k
        if (pdfi_type_of(val) != PDF_DICT) {
197
195
            dbgprintf1("WARNING: OCMD array contains item type %d, expected PDF_DICT or PDF_NULL\n", pdfi_type_of(val));
198
195
            pdfi_countdown(val);
199
195
            val = NULL;
200
195
            continue;
201
195
        }
202
203
66.6k
        hit = true;
204
66.6k
        vis = pdfi_get_default_OCG_val(ctx, (pdf_dict *)val);
205
66.6k
        switch (type) {
206
66.6k
        case P_AnyOn:
207
            /* visible if any is on */
208
66.6k
            if (vis) {
209
4.81k
                is_visible = true;
210
4.81k
                goto cleanup;
211
4.81k
            }
212
61.8k
            break;
213
61.8k
        case P_AllOn:
214
            /* visible if all on */
215
0
            if (!vis) {
216
0
                is_visible = false;
217
0
                goto cleanup;
218
0
            }
219
0
            break;
220
0
        case P_AllOff:
221
            /* visible if all are off */
222
0
            if (vis) {
223
0
                is_visible = false;
224
0
                goto cleanup;
225
0
            }
226
0
            break;
227
0
        case P_AnyOff:
228
            /* visible if any is off */
229
0
            if (!vis) {
230
0
                is_visible = true;
231
0
                goto cleanup;
232
0
            }
233
0
            break;
234
66.6k
        }
235
61.8k
        pdfi_countdown(val);
236
61.8k
        val = NULL;
237
61.8k
    }
238
239
    /* If the array was empty, or contained only null or deleted entries, then it has no effect
240
     * PDF Reference 1.7 p366, table 4.49, OCGs entry. I'm interpreting this to mean that we should use
241
     * the OCProperties 'D' dictionary, set the visibility state to the BaseState. Since this is an OCMD
242
     * I'm assuming that the ON and OFF arrays (which apply to Optional Content Groups) shouold not be
243
     * consulted. We need to cater for the fact that BaseState is optional (but default is ON). D is
244
     * specified as 'Required' but it seems best not to take any chances on that!
245
     */
246
65.0k
    if (!hit)
247
3.20k
        is_visible = pdfi_oc_default_visibility(ctx);
248
249
69.8k
cleanup:
250
69.8k
    pdfi_countdown(val);
251
69.8k
    return is_visible;
252
65.0k
}
253
254
static bool
255
pdfi_oc_check_OCMD(pdf_context *ctx, pdf_dict *ocdict)
256
70.5k
{
257
70.5k
    bool is_visible = true;
258
70.5k
    int code;
259
70.5k
    pdf_obj *VE = NULL;
260
70.5k
    pdf_obj *obj = NULL;
261
70.5k
    pdf_obj *Pname = NULL;
262
70.5k
    pdf_dict *OCGs_dict = NULL; /* alias, don't need to free */
263
70.5k
    pdf_array *OCGs_array = NULL; /* alias, don't need to free */
264
70.5k
    pdf_dict *UsageDict = NULL, *StateDict = NULL;
265
70.5k
    pdf_obj *State = NULL;
266
70.5k
    ocmd_p_type Ptype = P_AnyOn;
267
268
    /* TODO: We don't support this, so log a warning and ignore */
269
70.5k
    code = pdfi_dict_knownget_type(ctx, ocdict, "VE", PDF_ARRAY, &VE);
270
70.5k
    if (code > 0) {
271
235
        dbgprintf("WARNING: OCMD contains VE, which is not supported (ignoring)\n");
272
235
    }
273
274
70.5k
    code = pdfi_dict_knownget(ctx, ocdict, "OCGs", &obj);
275
70.5k
    if (code <= 0)
276
328
        goto cleanup;
277
70.2k
    if (pdfi_type_of(obj) == PDF_ARRAY) {
278
69.8k
        OCGs_array = (pdf_array *)obj;
279
69.8k
    } else if (pdfi_type_of(obj) == PDF_DICT) {
280
223
        OCGs_dict = (pdf_dict *)obj;
281
223
    } else {
282
157
        is_visible = pdfi_oc_default_visibility(ctx);
283
157
        goto cleanup;
284
157
    }
285
286
70.0k
    code = pdfi_dict_knownget_type(ctx, ocdict, "P", PDF_NAME, &Pname);
287
70.0k
    if (code < 0)
288
0
        goto cleanup;
289
70.0k
    if (code == 0 || pdfi_name_is((pdf_name *)Pname, "AnyOn")) {
290
70.0k
        Ptype = P_AnyOn;
291
70.0k
    } else if (pdfi_name_is((pdf_name *)Pname, "AllOn")) {
292
0
        Ptype = P_AllOn;
293
0
    } else if (pdfi_name_is((pdf_name *)Pname, "AnyOff")) {
294
0
        Ptype = P_AnyOff;
295
0
    } else if (pdfi_name_is((pdf_name *)Pname, "AllOff")) {
296
0
        Ptype = P_AllOff;
297
0
    } else {
298
0
        Ptype = P_AnyOn;
299
0
    }
300
301
70.0k
    if (OCGs_dict) {
302
223
        switch (Ptype) {
303
223
        case P_AnyOn:
304
223
        case P_AllOn:
305
223
            is_visible = pdfi_get_default_OCG_val(ctx, OCGs_dict);
306
223
            break;
307
0
        case P_AllOff:
308
0
        case P_AnyOff:
309
0
            is_visible = !pdfi_get_default_OCG_val(ctx, OCGs_dict);
310
0
            break;
311
223
        }
312
69.8k
    } else {
313
69.8k
        is_visible = pdfi_oc_check_OCMD_array(ctx, OCGs_array, Ptype);
314
69.8k
    }
315
316
70.0k
    if (OCGs_dict) {
317
223
        code = pdfi_dict_knownget_type(ctx, OCGs_dict, "Usage", PDF_DICT, (pdf_obj **)&UsageDict);
318
223
        if (code < 0)
319
0
            goto cleanup;
320
321
223
        if (UsageDict != NULL) {
322
111
            if (ctx->args.printed) {
323
111
                code = pdfi_dict_knownget_type(ctx, UsageDict, "Print", PDF_DICT, (pdf_obj **)&StateDict);
324
111
                if (code < 0)
325
0
                    goto cleanup;
326
111
                if (StateDict) {
327
26
                    code = pdfi_dict_knownget_type(ctx, StateDict, "PrintState", PDF_NAME, &State);
328
26
                    if (code < 0)
329
0
                        goto cleanup;
330
26
                }
331
111
            } else {
332
0
                code = pdfi_dict_knownget_type(ctx, UsageDict, "View", PDF_DICT, (pdf_obj **)&StateDict);
333
0
                if (code < 0)
334
0
                    goto cleanup;
335
0
                if (StateDict) {
336
0
                    code = pdfi_dict_knownget_type(ctx, StateDict, "ViewState", PDF_NAME, &State);
337
0
                    if (code < 0)
338
0
                        goto cleanup;
339
0
                }
340
0
            }
341
111
            if (State) {
342
26
                if (pdfi_name_is((const pdf_name *)State, "ON"))
343
26
                    is_visible = true;
344
0
                else
345
0
                    if (pdfi_name_is((const pdf_name *)State, "OFF"))
346
0
                        is_visible = false;
347
0
                    else
348
0
                        pdfi_set_error(ctx, 0, NULL, E_PDF_BAD_VALUE, "pdfi_oc_check_OCMD", "Usage Dictionary State is neither ON nor OFF");
349
26
            }
350
111
        }
351
223
    }
352
353
70.5k
 cleanup:
354
70.5k
    pdfi_countdown(State);
355
70.5k
    pdfi_countdown(StateDict);
356
70.5k
    pdfi_countdown(UsageDict);
357
70.5k
    pdfi_countdown(VE);
358
70.5k
    pdfi_countdown(obj);
359
70.5k
    pdfi_countdown(Pname);
360
361
70.5k
    return is_visible;
362
70.0k
}
363
364
/* Check if an OCG or OCMD is visible, passing in OC dict */
365
bool
366
pdfi_oc_is_ocg_visible(pdf_context *ctx, pdf_dict *ocdict)
367
75.3k
{
368
75.3k
    pdf_name *type = NULL;
369
75.3k
    bool is_visible = true;
370
75.3k
    int code;
371
372
    /* Type can be either OCMD or OCG.
373
     */
374
75.3k
    code = pdfi_dict_knownget_type(ctx, ocdict, "Type", PDF_NAME, (pdf_obj **)&type);
375
75.3k
    if (code <= 0)
376
80
        goto cleanup;
377
378
75.2k
    if (pdfi_name_is(type, "OCMD")) {
379
70.5k
        is_visible = pdfi_oc_check_OCMD(ctx, ocdict);
380
70.5k
    } else if (pdfi_name_is(type, "OCG")) {
381
4.31k
        is_visible = pdfi_get_default_OCG_val(ctx, ocdict);
382
4.31k
        if (is_visible)
383
4.25k
            is_visible = pdfi_oc_check_OCG_usage(ctx, ocdict);
384
4.31k
    } else {
385
381
        char str[100];
386
381
        memcpy(str, (const char *)type->data, type->length < 100 ? type->length : 99);
387
381
        str[type->length < 100 ? type->length : 99] = '\0';
388
381
        dbgprintf1("WARNING: OC dict type is %s, expected OCG or OCMD\n", str);
389
381
    }
390
391
75.3k
 cleanup:
392
75.3k
    pdfi_countdown(type);
393
394
75.3k
    if (ctx->args.pdfdebug) {
395
0
        outprintf(ctx->memory, "OCG: OC Dict %d %s visible\n", ocdict->object_num,
396
0
                  is_visible ? "IS" : "IS NOT");
397
0
    }
398
75.3k
    return is_visible;
399
75.2k
}
400
401
210k
#define NUM_CONTENT_LEVELS 100
402
typedef struct {
403
    byte *flags;
404
    uint64_t num_off;
405
    uint64_t max_flags;
406
} pdfi_oc_levels_t;
407
408
static int pdfi_oc_levels_init(pdf_context *ctx, pdfi_oc_levels_t **levels)
409
105k
{
410
105k
    byte *data;
411
105k
    pdfi_oc_levels_t *new;
412
413
105k
    *levels = NULL;
414
415
105k
    new = (pdfi_oc_levels_t *)gs_alloc_bytes(ctx->memory, sizeof(pdfi_oc_levels_t),
416
105k
                                             "pdfi_oc_levels_init (levels)");
417
105k
    if (!new)
418
0
        return_error(gs_error_VMerror);
419
420
105k
    data = (byte *)gs_alloc_bytes(ctx->memory, NUM_CONTENT_LEVELS, "pdfi_oc_levels_init (data)");
421
105k
    if (!data) {
422
0
        gs_free_object(ctx->memory, new, "pdfi_oc_levels_init (levels (error))");
423
0
        return_error(gs_error_VMerror);
424
0
    }
425
105k
    memset(data, 0, NUM_CONTENT_LEVELS);
426
427
105k
    new->flags = data;
428
105k
    new->num_off = 0;
429
105k
    new->max_flags = NUM_CONTENT_LEVELS;
430
105k
    *levels = new;
431
432
105k
    return 0;
433
105k
}
434
435
static int pdfi_oc_levels_free(pdf_context *ctx, pdfi_oc_levels_t *levels)
436
127k
{
437
127k
    if (!levels)
438
22.2k
        return 0;
439
105k
    gs_free_object(ctx->memory, levels->flags, "pdfi_oc_levels_free (flags)");
440
105k
    gs_free_object(ctx->memory, levels, "pdfi_oc_levels_free (levels)");
441
442
105k
    return 0;
443
127k
}
444
445
static int pdfi_oc_levels_set(pdf_context *ctx, pdfi_oc_levels_t *levels, uint64_t index)
446
61.8k
{
447
61.8k
    byte *new = NULL;
448
61.8k
    uint64_t newmax;
449
450
61.8k
    if (index > levels->max_flags - 1) {
451
        /* Expand the flags buffer */
452
1
        newmax = levels->max_flags + NUM_CONTENT_LEVELS;
453
1
        if (index > newmax)
454
0
            return_error(gs_error_Fatal); /* shouldn't happen */
455
1
        new = gs_alloc_bytes(ctx->memory, newmax, "pdfi_oc_levels_set (new data)");
456
1
        if (!new)
457
0
            return_error(gs_error_VMerror);
458
1
        memset(new, 0, newmax);
459
1
        memcpy(new, levels->flags, levels->max_flags);
460
1
        gs_free_object(ctx->memory, levels->flags, "pdfi_oc_levels_set (old data)");
461
1
        levels->flags = new;
462
1
        levels->max_flags += NUM_CONTENT_LEVELS;
463
1
    }
464
465
61.8k
    if (levels->flags[index] == 0)
466
61.8k
        levels->num_off ++;
467
61.8k
    levels->flags[index] = 1;
468
61.8k
    return 0;
469
61.8k
}
470
471
static int pdfi_oc_levels_clear(pdf_context *ctx, pdfi_oc_levels_t *levels, uint64_t index)
472
523k
{
473
523k
    if (index > levels->max_flags - 1)
474
273
        return -1;
475
523k
    if (levels->flags[index] != 0)
476
61.7k
        levels->num_off --;
477
523k
    levels->flags[index] = 0;
478
523k
    return 0;
479
523k
}
480
481
482
/* Test if content is turned off for this element.
483
 */
484
bool pdfi_oc_is_off(pdf_context *ctx)
485
11.8M
{
486
11.8M
    pdfi_oc_levels_t *levels = (pdfi_oc_levels_t *)ctx->OFFlevels;
487
11.8M
    uint64_t num_off = levels->num_off;
488
489
11.8M
    return (num_off != 0);
490
11.8M
}
491
492
int pdfi_oc_init(pdf_context *ctx)
493
105k
{
494
105k
    int code;
495
496
105k
    ctx->BMClevel = 0;
497
105k
    if (ctx->OFFlevels) {
498
32.7k
        pdfi_oc_levels_free(ctx, ctx->OFFlevels);
499
32.7k
        ctx->OFFlevels = NULL;
500
32.7k
    }
501
105k
    code = pdfi_oc_levels_init(ctx, (pdfi_oc_levels_t **)&ctx->OFFlevels);
502
105k
    if (code < 0)
503
0
        return code;
504
505
105k
    return 0;
506
105k
}
507
508
int pdfi_oc_free(pdf_context *ctx)
509
94.6k
{
510
94.6k
    int code;
511
512
94.6k
    code = pdfi_oc_levels_free(ctx, (pdfi_oc_levels_t *)ctx->OFFlevels);
513
94.6k
    ctx->OFFlevels = NULL;
514
94.6k
    return code;
515
94.6k
}
516
517
int pdfi_op_MP(pdf_context *ctx)
518
2.86k
{
519
2.86k
    pdf_obj *o = NULL;
520
2.86k
    int code = 0;
521
522
2.86k
    if (pdfi_count_stack(ctx) < 1)
523
76
        return_error(gs_error_stackunderflow);
524
525
2.78k
    if (!ctx->device_state.writepdfmarks || !ctx->args.preservemarkedcontent) {
526
2.74k
        pdfi_pop(ctx, 1);
527
2.74k
        goto exit;
528
2.74k
    }
529
530
39
    o = ctx->stack_top[-1];
531
39
    pdfi_countup(o);
532
39
    pdfi_pop(ctx, 1);
533
534
39
    if (pdfi_type_of(o) != PDF_NAME) {
535
0
        code = gs_note_error(gs_error_typecheck);
536
0
        goto exit;
537
0
    }
538
539
39
    code = pdfi_pdfmark_from_objarray(ctx, &o, 1, NULL, "MP");
540
39
    ctx->BMClevel ++;
541
542
2.78k
exit:
543
2.78k
    pdfi_countdown(o);
544
2.78k
    return code;
545
39
}
546
547
int pdfi_op_DP(pdf_context *ctx, pdf_dict *stream_dict, pdf_dict *page_dict)
548
335
{
549
335
    pdf_name *properties = NULL;
550
335
    int code = 0;
551
335
    pdf_obj **objarray = NULL, *o = NULL;
552
553
335
    if (pdfi_count_stack(ctx) < 2) {
554
12
        pdfi_clearstack(ctx);
555
12
        return gs_note_error(gs_error_stackunderflow);
556
12
    }
557
558
323
    if (!ctx->device_state.writepdfmarks || !ctx->args.preservemarkedcontent) {
559
322
        pdfi_pop(ctx, 2); /* pop args */
560
322
        goto exit;
561
322
    }
562
563
1
    if (pdfi_type_of(ctx->stack_top[-2]) != PDF_NAME) {
564
0
        pdfi_pop(ctx, 2); /* pop args */
565
0
        code = gs_note_error(gs_error_typecheck);
566
0
        goto exit;
567
0
    }
568
569
1
    objarray = (pdf_obj **)gs_alloc_bytes(ctx->memory, 2 * sizeof(pdf_obj *), "pdfi_op_DP");
570
1
    if (objarray == NULL) {
571
0
        code = gs_note_error(gs_error_VMerror);
572
0
        goto exit;
573
0
    }
574
575
1
    objarray[0] = ctx->stack_top[-2];
576
1
    pdfi_countup(objarray[0]);
577
1
    o = ctx->stack_top[-1];
578
1
    pdfi_countup(o);
579
1
    pdfi_pop(ctx, 2); /* pop args */
580
581
1
    switch (pdfi_type_of(o)) {
582
0
        case PDF_NAME:
583
0
            code = pdfi_find_resource(ctx, (unsigned char *)"Properties", (pdf_name *)o, stream_dict, page_dict, (pdf_obj **)&properties);
584
0
            if(code < 0)
585
0
                goto exit;
586
0
            if (pdfi_type_of(properties) != PDF_DICT) {
587
0
                code = gs_note_error(gs_error_typecheck);
588
0
                goto exit;
589
0
            }
590
0
            objarray[1] = (pdf_obj *)properties;
591
0
            break;
592
1
        case PDF_DICT:
593
1
            objarray[1] = o;
594
1
            break;
595
0
        default:
596
0
            code = gs_note_error(gs_error_VMerror);
597
0
            goto exit;
598
1
    }
599
600
1
    code = pdfi_pdfmark_from_objarray(ctx, objarray, 2, NULL, "DP");
601
602
323
 exit:
603
323
    if (objarray != NULL) {
604
1
        pdfi_countdown(objarray[0]);
605
1
        gs_free_object(ctx->memory, objarray, "free pdfi_op_DP");
606
1
    }
607
323
    pdfi_countdown(o);
608
323
    pdfi_countdown(properties);
609
323
    return code;
610
1
}
611
612
/* begin marked content sequence */
613
int pdfi_op_BMC(pdf_context *ctx)
614
23.3k
{
615
23.3k
    pdf_obj *o = NULL;
616
23.3k
    int code = 0;
617
618
    /* This will also prevent us writing out an EMC if the BMC is in any way invalid */
619
23.3k
    ctx->BDCWasOC = true;
620
621
23.3k
    if (pdfi_count_stack(ctx) < 1)
622
14
        return_error(gs_error_stackunderflow);
623
624
23.3k
    if (!ctx->device_state.writepdfmarks || !ctx->args.preservemarkedcontent) {
625
        /* Need to increment the BMCLevel anyway, as the EMC will count it down.
626
         * If we already have a BDC, then that effectively will turn it on.
627
         */
628
22.9k
        ctx->BMClevel ++;
629
22.9k
        pdfi_pop(ctx, 1);
630
22.9k
        goto exit;
631
22.9k
    }
632
633
371
    o = ctx->stack_top[-1];
634
371
    pdfi_countup(o);
635
371
    pdfi_pop(ctx, 1);
636
637
371
    if (pdfi_type_of(o) != PDF_NAME) {
638
1
        code = gs_note_error(gs_error_typecheck);
639
1
        goto exit;
640
1
    }
641
642
370
    ctx->BDCWasOC = false;
643
370
    code = pdfi_pdfmark_from_objarray(ctx, &o, 1, NULL, "BMC");
644
370
    ctx->BMClevel ++;
645
646
23.3k
exit:
647
23.3k
    pdfi_countdown(o);
648
23.3k
    return code;
649
370
}
650
651
/* begin marked content sequence with property list */
652
int pdfi_op_BDC(pdf_context *ctx, pdf_dict *stream_dict, pdf_dict *page_dict)
653
509k
{
654
509k
    pdf_name *tag = NULL;
655
509k
    pdf_name *properties = NULL;
656
509k
    pdf_dict *oc_dict = NULL;
657
509k
    int code = 0;
658
509k
    bool ocg_is_visible;
659
509k
    pdf_obj **objarray = NULL, *o = NULL;
660
509k
    pdf_indirect_ref *dictref = NULL;
661
662
    /* This will also prevent us writing out an EMC if the BDC is in any way invalid */
663
509k
    ctx->BDCWasOC = true;
664
665
509k
    if (pdfi_count_stack(ctx) < 2) {
666
9.07k
        pdfi_clearstack(ctx);
667
9.07k
        return gs_note_error(gs_error_stackunderflow);
668
9.07k
    }
669
670
500k
    ctx->BMClevel ++;
671
672
500k
    tag = (pdf_name *)ctx->stack_top[-2];
673
500k
    pdfi_countup(tag);
674
500k
    o = ctx->stack_top[-1];
675
500k
    pdfi_countup(o);
676
500k
    pdfi_pop(ctx, 2);
677
678
500k
    if (pdfi_type_of(tag) != PDF_NAME)
679
3.71k
        goto exit;
680
681
497k
    if (!pdfi_name_is(tag, "OC"))
682
333k
        ctx->BDCWasOC = false;
683
684
497k
    if (ctx->device_state.writepdfmarks && ctx->args.preservemarkedcontent && (!ctx->BDCWasOC || ctx->device_state.WantsOptionalContent)) {
685
12.3k
        objarray = (pdf_obj **)gs_alloc_bytes(ctx->memory, 2 * sizeof(pdf_obj *), "pdfi_op_BDC");
686
12.3k
        if (objarray == NULL) {
687
0
            code = gs_note_error(gs_error_VMerror);
688
0
            goto exit;
689
0
        }
690
691
12.3k
        objarray[0] = (pdf_obj *)tag;
692
693
12.3k
        switch (pdfi_type_of(o)) {
694
6.67k
            case PDF_NAME:
695
6.67k
                code = pdfi_find_resource(ctx, (unsigned char *)"Properties", (pdf_name *)o, stream_dict, page_dict, (pdf_obj **)&oc_dict);
696
6.67k
                if(code < 0)
697
4.99k
                    goto exit;
698
1.68k
                if (pdfi_type_of(oc_dict) != PDF_DICT) {
699
13
                    code = gs_note_error(gs_error_typecheck);
700
13
                    goto exit;
701
13
                }
702
                /* If we are producing PDF/A we must not include any Metadata, as that
703
                 * requires us to modify the XMP Metadata, which we don't know how to do.
704
                 */
705
1.66k
                if (ctx->args.PDFA > 0) {
706
0
                    uint64_t index = 0;
707
0
                    pdf_name *Key = NULL;
708
0
                    pdf_obj *Value = NULL;
709
710
0
                    code = pdfi_dict_first(ctx, oc_dict, (pdf_obj **)&Key, &Value, &index);
711
0
                    if (code < 0) {
712
0
                        if (code == gs_error_undefined)
713
0
                            code = 0;
714
0
                        goto exit;
715
0
                    }
716
0
                    while (code >= 0) {
717
0
                        if (pdfi_name_is(Key, "Metadata")) {
718
0
                            pdfi_dict_delete_pair(ctx, oc_dict, Key);
719
0
                        }
720
0
                        pdfi_countdown(Key);
721
0
                        Key = NULL;
722
0
                        pdfi_countdown(Value);
723
0
                        Value = NULL;
724
725
0
                        code = pdfi_dict_next(ctx, oc_dict, (pdf_obj **)&Key, &Value, &index);
726
0
                        if (code == gs_error_undefined) {
727
0
                            code = 0;
728
0
                            break;
729
0
                        }
730
0
                    }
731
0
                }
732
1.66k
                if (pdfi_dict_entries(oc_dict) == 0)
733
0
                    goto exit;
734
735
1.66k
                code = pdfi_pdfmark_dict(ctx, oc_dict);
736
1.66k
                if (code < 0)
737
24
                    goto exit;
738
739
                /* Create an indirect ref for the dict */
740
1.64k
                code = pdfi_object_alloc(ctx, PDF_INDIRECT, 0, (pdf_obj **)&dictref);
741
1.64k
                if (code < 0) goto exit;
742
1.64k
                pdfi_countup(dictref);
743
1.64k
                dictref->ref_object_num = oc_dict->object_num;
744
1.64k
                dictref->ref_generation_num = oc_dict->generation_num;
745
1.64k
                dictref->is_marking = true;
746
747
1.64k
                objarray[1] = (pdf_obj *)dictref;
748
1.64k
                break;
749
5.65k
            case PDF_DICT:
750
5.65k
                objarray[1] = o;
751
5.65k
                break;
752
19
            default:
753
19
                code = gs_note_error(gs_error_VMerror);
754
19
                goto exit;
755
12.3k
        }
756
757
7.29k
        code = pdfi_pdfmark_from_objarray(ctx, objarray, 2, NULL, "BDC");
758
7.29k
        goto exit;
759
12.3k
    }
760
761
484k
    if (pdfi_name_is(tag, "OC")) {
762
        /* Check if first arg is a name and handle it if so */
763
        /* TODO: spec says it could also be an inline dict that we should be able to handle,
764
         * but I am just matching what gs does for now, and it doesn't handle that case.
765
         */
766
157k
        properties = (pdf_name *)o;
767
157k
        if (pdfi_type_of(properties) != PDF_NAME)
768
18
            goto exit;
769
770
157k
        code = pdfi_loop_detector_mark(ctx);
771
        /* If it's a name, look it up in Properties */
772
157k
        code = pdfi_find_resource(ctx, (unsigned char *)"Properties", properties,
773
157k
                                  (pdf_dict *)stream_dict, page_dict, (pdf_obj **)&oc_dict);
774
157k
        (void)pdfi_loop_detector_cleartomark(ctx);
775
157k
        if (code != 0)
776
80.6k
            goto exit;
777
77.0k
        if (pdfi_type_of(oc_dict) != PDF_DICT)
778
2.66k
            goto exit;
779
780
74.4k
        ocg_is_visible = pdfi_oc_is_ocg_visible(ctx, oc_dict);
781
74.4k
        if (!ocg_is_visible)
782
61.8k
            code = pdfi_oc_levels_set(ctx, ctx->OFFlevels, ctx->BMClevel);
783
784
74.4k
    }
785
786
500k
exit:
787
500k
    if (objarray != NULL)
788
12.3k
        gs_free_object(ctx->memory, objarray, "free pdfi_op_BDC");
789
500k
    pdfi_countdown(dictref);
790
500k
    pdfi_countdown(o);
791
500k
    pdfi_countdown(tag);
792
500k
    pdfi_countdown(oc_dict);
793
500k
    return code;
794
484k
}
795
796
/* end marked content sequence */
797
int pdfi_op_EMC(pdf_context *ctx)
798
523k
{
799
523k
    int code, code1 = 0;
800
801
523k
    if (ctx->device_state.writepdfmarks && ctx->args.preservemarkedcontent && (!ctx->BDCWasOC || ctx->device_state.WantsOptionalContent))
802
12.0k
        code1 = pdfi_pdfmark_from_objarray(ctx, NULL, 0, NULL, "EMC");
803
804
523k
    code = pdfi_oc_levels_clear(ctx, ctx->OFFlevels, ctx->BMClevel);
805
523k
    if (code == 0)
806
523k
        code = code1;
807
808
    /* TODO: Should we flag error on too many EMC? */
809
523k
    if (ctx->BMClevel > 0)
810
511k
        ctx->BMClevel --;
811
523k
    return code;
812
523k
}