Coverage Report

Created: 2026-11-12 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/psi/zdict.c
Line
Count
Source
1
/* Copyright (C) 2001-2026 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
17
/* Dictionary operators */
18
#include "ghost.h"
19
#include "oper.h"
20
#include "iddict.h"
21
#include "dstack.h"
22
#include "ilevel.h"   /* for [count]dictstack */
23
#include "iname.h"    /* for dict_find_name */
24
#include "ipacked.h"    /* for inline dict lookup */
25
#include "ivmspace.h"
26
#include "store.h"
27
#include "iscan.h"              /* for SCAN_PDF_RULES */
28
29
/* <int> dict <dict> */
30
int
31
zdict(i_ctx_t *i_ctx_p)
32
37.7M
{
33
37.7M
    os_ptr op = osp;
34
35
37.7M
    check_op(1);
36
37.7M
    check_type(*op, t_integer);
37
37.7M
    if (op->value.intval < 0)
38
26
        return_error(gs_error_rangecheck);
39
37.7M
    return dict_create((uint) op->value.intval, op);
40
37.7M
}
41
42
/* <dict> maxlength <int> */
43
static int
44
zmaxlength(i_ctx_t *i_ctx_p)
45
94.8M
{
46
94.8M
    os_ptr op = osp;
47
48
94.8M
    check_op(1);
49
94.8M
    check_type(*op, t_dictionary);
50
94.8M
    check_dict_read(*op);
51
94.8M
    make_int(op, dict_maxlength(op));
52
94.8M
    return 0;
53
94.8M
}
54
55
/* <dict> begin - */
56
int
57
zbegin(i_ctx_t *i_ctx_p)
58
305M
{
59
305M
    os_ptr op = osp;
60
61
305M
    check_op(1);
62
305M
    check_type(*op, t_dictionary);
63
305M
    check_dict_read(*op);
64
305M
    if ( dsp == dstop ) {
65
413
        int code = ref_stack_extend(&d_stack, 1);
66
67
413
        if ( code < 0 ) {
68
0
            if (code == gs_error_dictstackoverflow) {
69
                /* Adobe doesn't restore the operand that caused stack */
70
                /* overflow. We do the same to match CET 20-02-02      */
71
0
                pop(1);
72
0
            }
73
0
            return code;
74
0
        }
75
413
    }
76
305M
    ++dsp;
77
305M
    ref_assign(dsp, op);
78
305M
    dict_set_top();
79
305M
    pop(1);
80
305M
    return 0;
81
305M
}
82
83
/* - end - */
84
int
85
zend(i_ctx_t *i_ctx_p)
86
305M
{
87
305M
    if (ref_stack_count_inline(&d_stack) == min_dstack_size) {
88
        /* We would underflow the d-stack. */
89
29.0k
        return_error(gs_error_dictstackunderflow);
90
29.0k
    }
91
305M
    while (dsp == dsbot) {
92
        /* We would underflow the current block. */
93
14
        ref_stack_pop_block(&d_stack);
94
14
    }
95
305M
    dsp--;
96
305M
    dict_set_top();
97
305M
    return 0;
98
305M
}
99
100
/* <key> <value> def - */
101
/*
102
 * We make this into a separate procedure because
103
 * the interpreter will almost always call it directly.
104
 */
105
int
106
zop_def(i_ctx_t *i_ctx_p)
107
341M
{
108
341M
    os_ptr op = osp;
109
341M
    os_ptr op1 = op - 1;
110
341M
    ref *pvslot;
111
112
341M
    check_op(2);
113
    /* The following combines a check_op(2) with a type check. */
114
341M
    switch (r_type(op1)) {
115
332M
        case t_name: {
116
            /* We can use the fast single-probe lookup here. */
117
332M
            uint nidx = name_index(imemory, op1);
118
332M
            uint htemp;
119
120
332M
            if_dict_find_name_by_index_top(nidx, htemp, pvslot) {
121
4.06M
                if (dtop_can_store(op))
122
4.06M
                    goto ra;
123
4.06M
            }
124
328M
            break;   /* handle all slower cases */
125
332M
            }
126
328M
        case t_null:
127
3
            return_error(gs_error_typecheck);
128
0
        case t__invalid:
129
0
            return_error(gs_error_stackunderflow);
130
341M
    }
131
    /*
132
     * Combine the check for a writable top dictionary with
133
     * the global/local store check.  See dstack.h for details.
134
     */
135
336M
    if (!dtop_can_store(op)) {
136
0
        check_dict_write(*dsp);
137
        /*
138
         * If the dictionary is writable, the problem must be
139
         * an invalid store.
140
         */
141
0
        return_error(gs_error_invalidaccess);
142
0
    }
143
    /*
144
     * Save a level of procedure call in the common (redefinition)
145
     * case.  With the current interfaces, we pay a double lookup
146
     * in the uncommon case.
147
     */
148
336M
    if (dict_find(dsp, op1, &pvslot) <= 0)
149
311M
        return idict_put(dsp, op1, op);
150
29.1M
ra:
151
29.1M
    if ((pvslot->tas.type_attrs & (&i_ctx_p->memory)->test_mask) == 0)
152
532k
        alloc_save_change(idmemory, &dsp->value.pdict->values, (ref_packed *)pvslot, "dict_put(value)");
153
29.1M
    ref_assign_new_inline(pvslot,op);
154
155
29.1M
    return 0;
156
336M
}
157
int
158
zdef(i_ctx_t *i_ctx_p)
159
0
{
160
0
    int code = zop_def(i_ctx_p);
161
162
0
    if (code >= 0) {
163
0
        pop(2);
164
0
    }
165
0
    return code;
166
0
}
167
168
/* <key> load <value> */
169
static int
170
zload(i_ctx_t *i_ctx_p)
171
304M
{
172
304M
    os_ptr op = osp;
173
304M
    ref *pvalue;
174
175
304M
    check_op(1);
176
304M
    switch (r_type(op)) {
177
304M
        case t_name:
178
            /* Use the fast lookup. */
179
304M
            if ((pvalue = dict_find_name(op)) == 0)
180
10
                return_error(gs_error_undefined);
181
304M
            ref_assign(op, pvalue);
182
304M
            return 0;
183
1
        case t_null:
184
1
            return_error(gs_error_typecheck);
185
0
        case t__invalid:
186
0
            return_error(gs_error_stackunderflow);
187
20
        default: {
188
                /* Use an explicit loop. */
189
20
                uint size = ref_stack_count(&d_stack);
190
20
                uint i;
191
192
80
                for (i = 0; i < size; i++) {
193
61
                    ref *dp = ref_stack_index(&d_stack, i);
194
195
61
                    if (dp == NULL)
196
0
                        return_error(gs_error_stackunderflow);
197
198
61
                    check_dict_read(*dp);
199
61
                    if (dict_find(dp, op, &pvalue) > 0) {
200
1
                        ref_assign(op, pvalue);
201
1
                        return 0;
202
1
                    }
203
61
                }
204
20
                return_error(gs_error_undefined);
205
20
            }
206
304M
    }
207
304M
}
208
209
/* get - implemented in zgeneric.c */
210
211
/* put - implemented in zgeneric.c */
212
213
/* <dict> <key> .undef - */
214
/* <dict> <key> undef - */
215
static int
216
zundef(i_ctx_t *i_ctx_p)
217
19.6M
{
218
19.6M
    os_ptr op = osp;
219
19.6M
    os_ptr op1 = op - 1;
220
19.6M
    int code;
221
222
19.6M
    check_op(2);
223
19.6M
    check_type(*op1, t_dictionary);
224
19.6M
    check_dict_write(*op1);
225
19.6M
    code = idict_undef(op1, op);
226
19.6M
    if (code < 0 && code != gs_error_undefined) /* ignore undefined error */
227
0
        return code;
228
19.6M
    pop(2);
229
19.6M
    return 0;
230
19.6M
}
231
232
/* <dict> <key> known <bool> */
233
static int
234
zknown(i_ctx_t *i_ctx_p)
235
363M
{
236
363M
    os_ptr op = osp;
237
363M
    register os_ptr op1 = op - 1;
238
363M
    ref *pvalue;
239
363M
    int code;
240
241
363M
    check_op(2);
242
363M
    check_type(*op1, t_dictionary);
243
363M
    check_dict_read(*op1);
244
363M
    code = dict_find(op1, op, &pvalue);
245
363M
    switch (code) {
246
17.4M
    case gs_error_dictfull:
247
17.4M
        code = 0;
248
363M
    case 0: case 1:
249
363M
        break;
250
0
    default:
251
0
        return code;
252
363M
    }
253
363M
    make_bool(op1, code);
254
363M
    pop(1);
255
363M
    return 0;
256
363M
}
257
258
/* <key> where <dict> true */
259
/* <key> where false */
260
int
261
zwhere(i_ctx_t *i_ctx_p)
262
14.8M
{
263
14.8M
    os_ptr op = osp;
264
14.8M
    ref_stack_enum_t rsenum;
265
266
14.8M
    check_op(1);
267
14.8M
    ref_stack_enum_begin(&rsenum, &d_stack);
268
14.9M
    do {
269
14.9M
        const ref *const bot = rsenum.ptr;
270
14.9M
        const ref *pdref = bot + rsenum.size;
271
14.9M
        ref *pvalue;
272
14.9M
        int code;
273
274
55.9M
        while (pdref-- > bot) {
275
48.4M
            check_dict_read(*pdref);
276
48.4M
            code = dict_find(pdref, op, &pvalue);
277
48.4M
            if (code < 0 && code != gs_error_dictfull)
278
0
                return code;
279
48.4M
            if (code > 0) {
280
7.38M
                push(1);
281
7.38M
                ref_assign(op - 1, pdref);
282
7.38M
                make_true(op);
283
7.38M
                return 0;
284
7.38M
            }
285
48.4M
        }
286
14.9M
    } while (ref_stack_enum_next(&rsenum));
287
7.50M
    make_false(op);
288
7.50M
    return 0;
289
14.8M
}
290
291
/* copy for dictionaries -- called from zcopy in zgeneric.c. */
292
/* Only the type of *op has been checked. */
293
int
294
zcopy_dict(i_ctx_t *i_ctx_p)
295
2.33M
{
296
2.33M
    os_ptr op = osp;
297
2.33M
    os_ptr op1 = op - 1;
298
2.33M
    int code;
299
300
2.33M
    check_op(2);
301
2.33M
    check_type(*op1, t_dictionary);
302
2.33M
    check_dict_read(*op1);
303
2.33M
    check_dict_write(*op);
304
2.33M
    if (!imemory->gs_lib_ctx->dict_auto_expand &&
305
0
        (dict_length(op) != 0 || dict_maxlength(op) < dict_length(op1))
306
2.33M
        )
307
0
        return_error(gs_error_rangecheck);
308
2.33M
    code = idict_copy(op1, op);
309
2.33M
    if (code < 0)
310
0
        return code;
311
    /*
312
     * In Level 1 systems, we must copy the access attributes too.
313
     * The only possible effect this can have is to make the
314
     * copy read-only if the original dictionary is read-only.
315
     */
316
2.33M
    if (!level2_enabled)
317
2.33M
        r_copy_attrs(dict_access_ref(op), a_write, dict_access_ref(op1));
318
2.33M
    ref_assign(op1, op);
319
2.33M
    pop(1);
320
2.33M
    return 0;
321
2.33M
}
322
323
/* - currentdict <dict> */
324
static int
325
zcurrentdict(i_ctx_t *i_ctx_p)
326
80.7M
{
327
80.7M
    os_ptr op = osp;
328
329
80.7M
    push(1);
330
80.7M
    ref_assign(op, dsp);
331
80.7M
    return 0;
332
80.7M
}
333
334
/* - countdictstack <int> */
335
static int
336
zcountdictstack(i_ctx_t *i_ctx_p)
337
228k
{
338
228k
    os_ptr op = osp;
339
228k
    uint count = ref_stack_count(&d_stack);
340
341
228k
    push(1);
342
228k
    if (!level2_enabled)
343
0
        count--;   /* see dstack.h */
344
228k
    make_int(op, count);
345
228k
    return 0;
346
228k
}
347
348
/* <array> dictstack <subarray> */
349
static int
350
zdictstack(i_ctx_t *i_ctx_p)
351
226k
{
352
226k
    os_ptr op = osp;
353
226k
    uint count = ref_stack_count(&d_stack);
354
355
226k
    check_op(1);
356
226k
    if (!level2_enabled)
357
0
        count--;   /* see dstack.h */
358
226k
    if (!r_is_array(op))
359
226k
        return_op_typecheck(op);
360
226k
    if (r_size(op) < count)
361
0
        return_error(gs_error_rangecheck);
362
226k
    if (!r_has_type_attrs(op, t_array, a_write))
363
0
        return_error(gs_error_invalidaccess);
364
226k
    return ref_stack_store(&d_stack, op, count, 0, 0, true, idmemory,
365
226k
                           "dictstack");
366
226k
}
367
368
/* - cleardictstack - */
369
static int
370
zcleardictstack(i_ctx_t *i_ctx_p)
371
29.0k
{
372
29.0k
    while (zend(i_ctx_p) >= 0)
373
29.0k
        DO_NOTHING;
374
29.0k
    return 0;
375
29.0k
}
376
377
/* ------ Extensions ------ */
378
379
/* -mark- <key0> <value0> <key1> <value1> ... .dicttomark <dict> */
380
/* This is the Level 2 >> operator. */
381
static int
382
zdicttomark(i_ctx_t *i_ctx_p)
383
51.1M
{
384
51.1M
    uint count2 = ref_stack_counttomark(&o_stack);
385
51.1M
    ref rdict;
386
51.1M
    int code;
387
51.1M
    uint idx;
388
389
51.1M
    if (count2 == 0)
390
31
        return_error(gs_error_unmatchedmark);
391
51.1M
    count2--;
392
51.1M
    if ((count2 & 1) != 0)
393
13
        return_error(gs_error_rangecheck);
394
51.1M
    code = dict_create(count2 >> 1, &rdict);
395
51.1M
    if (code < 0)
396
0
        return code;
397
51.1M
    if ((i_ctx_p->scanner_options & SCAN_PDF_RULES) != 0) {
398
0
       for (idx = count2; idx > 0; idx -= 2) {
399
0
           code = idict_put(&rdict,
400
0
                            ref_stack_index(&o_stack, idx - 1),
401
0
                            ref_stack_index(&o_stack, idx - 2));
402
0
           if (code < 0) {         /* There's no way to free the dictionary -- too bad. */
403
0
               return code;
404
0
           }
405
0
       }
406
0
    }
407
51.1M
    else {
408
       /* << /a 1 /a 2 >> => << /a 1 >>, i.e., */
409
       /* we must enter the keys in top-to-bottom order. */
410
1.69G
       for (idx = 0; idx < count2; idx += 2) {
411
1.63G
           code = idict_put(&rdict,
412
1.63G
                            ref_stack_index(&o_stack, idx + 1),
413
1.63G
                            ref_stack_index(&o_stack, idx));
414
1.63G
           if (code < 0) {         /* There's no way to free the dictionary -- too bad. */
415
0
               return code;
416
0
           }
417
1.63G
       }
418
51.1M
    }
419
51.1M
    ref_stack_pop(&o_stack, count2);
420
51.1M
    ref_assign(osp, &rdict);
421
51.1M
    return code;
422
51.1M
}
423
424
/* <dict> <key> .forceundef - */
425
/*
426
 * This forces an "undef" even if the dictionary is not writable.
427
 * Like .forceput, it is meant to be used only in a few special situations,
428
 * and should not be accessible by name after initialization.
429
 */
430
static int
431
zforceundef(i_ctx_t *i_ctx_p)
432
81.2M
{
433
81.2M
    os_ptr op = osp;
434
435
81.2M
    check_op(2);
436
81.2M
    check_type(op[-1], t_dictionary);
437
    /* Don't check_dict_write */
438
81.2M
    idict_undef(op - 1, op); /* ignore undefined error */
439
81.2M
    pop(2);
440
81.2M
    return 0;
441
81.2M
}
442
443
/* <dict> <key> .knownget <value> true */
444
/* <dict> <key> .knownget false */
445
static int
446
zknownget(i_ctx_t *i_ctx_p)
447
715M
{
448
715M
    os_ptr op = osp;
449
715M
    register os_ptr op1 = op - 1;
450
715M
    ref *pvalue;
451
452
715M
    check_op(2);
453
715M
    check_type(*op1, t_dictionary);
454
715M
    check_dict_read(*op1);
455
715M
    if (dict_find(op1, op, &pvalue) <= 0) {
456
166M
        make_false(op1);
457
166M
        pop(1);
458
548M
    } else {
459
548M
        ref_assign(op1, pvalue);
460
548M
        make_true(op);
461
548M
    }
462
715M
    return 0;
463
715M
}
464
465
/* <dict> <key> .knownundef <bool> */
466
static int
467
zknownundef(i_ctx_t *i_ctx_p)
468
0
{
469
0
    os_ptr op = osp;
470
0
    os_ptr op1 = op - 1;
471
0
    int code;
472
473
0
    check_op(2);
474
0
    check_type(*op1, t_dictionary);
475
0
    check_dict_write(*op1);
476
0
    code = idict_undef(op1, op);
477
0
    make_bool(op1, code == 0);
478
0
    pop(1);
479
0
    return 0;
480
0
}
481
482
/* <dict> <int> .setmaxlength - */
483
static int
484
zsetmaxlength(i_ctx_t *i_ctx_p)
485
11.4M
{
486
11.4M
    os_ptr op = osp;
487
11.4M
    os_ptr op1 = op - 1;
488
11.4M
    uint new_size;
489
11.4M
    int code;
490
491
11.4M
    check_op(2);
492
11.4M
    check_type(*op1, t_dictionary);
493
11.4M
    check_dict_write(*op1);
494
11.4M
    check_type(*op, t_integer);
495
11.4M
    if (op->value.intval < 0)
496
0
        return_error(gs_error_rangecheck);
497
11.4M
    new_size = (uint) op->value.intval;
498
11.4M
    if (dict_length(op - 1) > new_size)
499
0
        return_error(gs_error_dictfull);
500
11.4M
    code = idict_resize(op - 1, new_size);
501
11.4M
    if (code >= 0)
502
11.4M
        pop(2);
503
11.4M
    return code;
504
11.4M
}
505
506
/* ------ Initialization procedure ------ */
507
508
/* We need to split the table because of the 16-element limit. */
509
const op_def zdict1_op_defs[] = {
510
    {"0cleardictstack", zcleardictstack},
511
    {"1begin", zbegin},
512
    {"0countdictstack", zcountdictstack},
513
    {"0currentdict", zcurrentdict},
514
    {"2def", zdef},
515
    {"1dict", zdict},
516
    {"0dictstack", zdictstack},
517
    {"0end", zend},
518
    {"2known", zknown},
519
    {"1load", zload},
520
    {"1maxlength", zmaxlength},
521
    {"2.undef", zundef},  /* we need this even in Level 1 */
522
    {"1where", zwhere},
523
    op_def_end(0)
524
};
525
const op_def zdict2_op_defs[] = {
526
                /* Extensions */
527
    {"1.dicttomark", zdicttomark},
528
    {"2.forceundef", zforceundef},
529
    {"2.knownget", zknownget},
530
    {"1.knownundef", zknownundef},
531
    {"2.setmaxlength", zsetmaxlength},
532
        /*
533
         * In Level 2, >> is a synonym for .dicttomark, and undef for
534
         * .undef.  By giving the former their own entries, they will not be
535
         * "eq" to .dicttomark and .undef, but that doesn't matter, since
536
         * we're doing this only for the sake of Adobe- compatible error
537
         * stacks.
538
         */
539
    op_def_begin_level2(),
540
    {"1>>", zdicttomark},
541
    {"2undef", zundef},
542
    op_def_end(0)
543
};