Coverage Report

Created: 2026-08-08 08:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/psi/zmisc.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
/* Miscellaneous operators */
18
19
#include "errno_.h"
20
#include "memory_.h"
21
#include "string_.h"
22
#include "ctype_.h"
23
#include "ghost.h"
24
#include "gp.h"
25
#include "oper.h"
26
#include "ialloc.h"
27
#include "idict.h"
28
#include "dstack.h"   /* for name lookup in bind */
29
#include "iname.h"
30
#include "ipacked.h"
31
#include "ivmspace.h"
32
#include "store.h"
33
#include "igstate.h"            /* for gs_currentcpsimode */
34
#include "memento.h"
35
#include "iscan.h"
36
37
/**********************************************************************/
38
39
/* <proc> bind <proc> */
40
static inline bool
41
r_is_ex_oper(const ref *rp)
42
3.13G
{
43
3.13G
    return (r_has_attr(rp, a_executable) &&
44
2.83G
            (r_btype(rp) == t_operator || r_type(rp) == t_oparray));
45
3.13G
}
46
static int
47
zbind(i_ctx_t *i_ctx_p)
48
235M
{
49
235M
    os_ptr op = osp;
50
235M
    uint depth = 1;
51
235M
    ref defn;
52
235M
    register os_ptr bsp;
53
54
235M
    check_op(1);
55
235M
    switch (r_type(op)) {
56
1.25M
        case t_array:
57
1.25M
            if (!r_has_attr(op, a_write)) {
58
3.33k
                return 0; /* per PLRM3 */
59
3.33k
            }
60
112M
        case t_mixedarray:
61
136M
        case t_shortarray:
62
136M
            defn = *op;
63
136M
            break;
64
21.7M
        case t_oparray:
65
21.7M
            defn = *op->value.const_refs;
66
21.7M
            break;
67
77.0M
        default:
68
77.0M
            return_op_typecheck(op);
69
235M
    }
70
235M
    push(1);
71
158M
    *op = defn;
72
158M
    bsp = op;
73
    /*
74
     * We must not make the top-level procedure read-only,
75
     * but we must bind it even if it is read-only already.
76
     *
77
     * Here are the invariants for the following loop:
78
     *      `depth' elements have been pushed on the ostack;
79
     *      For i < depth, p = ref_stack_index(&o_stack, i):
80
     *        *p is an array (or packedarray) ref.
81
     */
82
2.20G
    while (depth) {
83
18.7G
        while (r_size(bsp)) {
84
16.6G
            ref_packed *const tpp = (ref_packed *)bsp->value.packed; /* break const */
85
86
16.6G
            r_dec_size(bsp, 1);
87
16.6G
            if (r_is_packed(tpp)) {
88
                /* Check for a packed executable name */
89
10.6G
                ushort elt = *tpp;
90
91
10.6G
                if (r_packed_is_exec_name(&elt)) {
92
2.42G
                    ref nref;
93
2.42G
                    ref *pvalue;
94
95
2.42G
                    name_index_ref(imemory, packed_name_index(&elt),
96
2.42G
                                   &nref);
97
2.42G
                    if ((pvalue = dict_find_name(&nref)) != 0 &&
98
2.37G
                        r_is_ex_oper(pvalue)
99
2.42G
                        ) {
100
2.00G
                        store_check_dest(bsp, pvalue);
101
                        /*
102
                         * Always save the change, since this can only
103
                         * happen once.
104
                         */
105
2.00G
                        ref_do_save(bsp, tpp, "bind");
106
2.00G
                        *tpp = pt_tag(pt_executable_operator) +
107
2.00G
                            op_index(pvalue);
108
2.00G
                    }
109
2.42G
                }
110
10.6G
                bsp->value.packed = tpp + 1;
111
10.6G
            } else {
112
6.08G
                ref *const tp = bsp->value.refs++;
113
114
6.08G
                switch (r_type(tp)) {
115
1.61G
                    case t_name:  /* bind the name if an operator */
116
1.61G
                        if (r_has_attr(tp, a_executable)) {
117
1.10G
                            ref *pvalue;
118
119
1.10G
                            if ((pvalue = dict_find_name(tp)) != 0 &&
120
754M
                                r_is_ex_oper(pvalue)
121
1.10G
                                ) {
122
560M
                                store_check_dest(bsp, pvalue);
123
560M
                                ref_assign_old(bsp, tp, pvalue, "bind");
124
560M
                            }
125
1.10G
                        }
126
1.61G
                        break;
127
1.61G
                    case t_array: /* push into array if writable */
128
20.6M
                        if (!r_has_attr(tp, a_write))
129
3.85M
                            break;
130
1.35G
                    case t_mixedarray:
131
1.89G
                    case t_shortarray:
132
1.89G
                        if (r_has_attr(tp, a_executable)) {
133
                            /* Make reference read-only */
134
1.89G
                            r_clear_attrs(tp, a_write);
135
1.89G
                            if (bsp >= ostop) {
136
                                /* Push a new stack block. */
137
0
                                ref temp;
138
0
                                int code;
139
140
0
                                temp = *tp;
141
0
                                osp = bsp;
142
0
                                code = ref_stack_push(&o_stack, 1);
143
0
                                if (code < 0) {
144
0
                                    ref_stack_pop(&o_stack, depth);
145
0
                                    return_error(code);
146
0
                                }
147
0
                                bsp = osp;
148
0
                                *bsp = temp;
149
0
                            } else
150
1.89G
                                *++bsp = *tp;
151
1.89G
                            depth++;
152
1.89G
                        }
153
6.08G
                }
154
6.08G
            }
155
16.6G
        }
156
2.05G
        bsp--;
157
2.05G
        depth--;
158
2.05G
        if (bsp < osbot) { /* Pop back to the previous stack block. */
159
0
            osp = bsp;
160
0
            ref_stack_pop_block(&o_stack);
161
0
            bsp = osp;
162
0
        }
163
2.05G
    }
164
158M
    osp = bsp;
165
158M
    return 0;
166
158M
}
167
168
/* - serialnumber <int> */
169
static int
170
zserialnumber(i_ctx_t *i_ctx_p)
171
23
{
172
23
    os_ptr op = osp;
173
174
23
    push(1);
175
23
    make_int(op, gp_serialnumber());
176
23
    return 0;
177
23
}
178
179
/* - realtime <int> */
180
static int
181
zrealtime(i_ctx_t *i_ctx_p)
182
12
{
183
12
    os_ptr op = osp;
184
12
    long secs_ns[2];
185
12
    gs_lib_ctx_t *libctx = gs_lib_ctx_get_interp_instance(imemory);
186
187
12
    gp_get_realtime(secs_ns);
188
12
    secs_ns[1] -= libctx->real_time_0[1];
189
12
    secs_ns[0] -= libctx->real_time_0[0];
190
12
    push(1);
191
12
    make_int(op, secs_ns[0] * 1000 + secs_ns[1] / 1000000);
192
12
    return 0;
193
12
}
194
195
/* - usertime <int> */
196
static int
197
zusertime(i_ctx_t *i_ctx_p)
198
1.23k
{
199
1.23k
    gs_context_state_t *current = (gs_context_state_t *)i_ctx_p;
200
1.23k
    os_ptr op = osp;
201
1.23k
    long secs_ns[2];
202
203
1.23k
    gp_get_usertime(secs_ns);
204
1.23k
    if (!current->usertime_inited) {
205
487
        current->usertime_inited = true;
206
487
        current->usertime_0[0] = secs_ns[0];
207
487
        current->usertime_0[1] = secs_ns[1];
208
487
    }
209
1.23k
    secs_ns[0] -= current->usertime_0[0];
210
1.23k
    secs_ns[1] -= current->usertime_0[1];
211
1.23k
    push(1);
212
1.23k
    make_int(op, secs_ns[0] * 1000 + secs_ns[1] / 1000000);
213
1.23k
    return 0;
214
1.23k
}
215
216
/* ---------------- Non-standard operators ---------------- */
217
218
/* <string> getenv <value_string> true */
219
/* <string> getenv false */
220
static int
221
zgetenv(i_ctx_t *i_ctx_p)
222
617k
{
223
617k
    os_ptr op = osp;
224
617k
    char *str;
225
617k
    byte *value;
226
617k
    int len = 0;
227
228
617k
    check_op(1);
229
617k
    check_read_type(*op, t_string);
230
617k
    str = ref_to_string(op, imemory, "getenv key");
231
617k
    if (str == 0)
232
0
        return_error(gs_error_VMerror);
233
234
617k
    if (gs_is_path_control_active(imemory)) {
235
0
        ifree_string((byte *) str, r_size(op) + 1, "getenv key");
236
0
        make_false(op);
237
0
        return 0;
238
0
    }
239
240
617k
    if (gp_getenv(str, (char *)0, &len) > 0) { /* key missing */
241
617k
        ifree_string((byte *) str, r_size(op) + 1, "getenv key");
242
617k
        make_false(op);
243
617k
        return 0;
244
617k
    }
245
0
    value = ialloc_string(len, "getenv value");
246
0
    if (value == 0) {
247
0
        ifree_string((byte *) str, r_size(op) + 1, "getenv key");
248
0
        return_error(gs_error_VMerror);
249
0
    }
250
0
    DISCARD(gp_getenv(str, (char *)value, &len));  /* can't fail */
251
0
    ifree_string((byte *) str, r_size(op) + 1, "getenv key");
252
    /* Delete the stupid C string terminator. */
253
0
    value = iresize_string(value, len, len - 1,
254
0
                           "getenv value"); /* can't fail */
255
0
    push(1);
256
0
    make_string(op - 1, a_all | icurrent_space, len - 1, value);
257
0
    make_true(op);
258
0
    return 0;
259
0
}
260
261
/* - .defaultpapersize <string> true */
262
/* - .defaultpapersize false */
263
static int
264
zdefaultpapersize(i_ctx_t *i_ctx_p)
265
154k
{
266
154k
    os_ptr op = osp;
267
154k
    byte *value;
268
154k
    int len = 0, i;
269
270
154k
    if (gp_defaultpapersize((char *)0, &len) > 0) {
271
        /* no default paper size */
272
154k
        push(1);
273
154k
        make_false(op);
274
154k
        return 0;
275
154k
    }
276
277
0
    value = ialloc_string(len, "defaultpapersize value");
278
0
    if (value == 0) {
279
0
        return_error(gs_error_VMerror);
280
0
    }
281
0
    DISCARD(gp_defaultpapersize((char *)value, &len)); /* can't fail */
282
    /* Note 'len' includes the NULL terminator, which we can ignore */
283
0
    for (i = 0;i < (len - 1); i++)
284
0
        value[i] = tolower(value[i]);
285
286
    /* Delete the stupid C string terminator. */
287
0
    value = iresize_string(value, len, len - 1,
288
0
                           "defaultpapersize value"); /* can't fail */
289
0
    push(2);
290
0
    make_string(op - 1, a_all | icurrent_space, len - 1, value);
291
0
    make_true(op);
292
0
    return 0;
293
0
}
294
295
/* <name> <proc> .makeoperator <oper> */
296
static int
297
zmakeoperator(i_ctx_t *i_ctx_p)
298
28.2M
{
299
28.2M
    os_ptr op = osp;
300
28.2M
    op_array_table *opt;
301
28.2M
    uint count;
302
28.2M
    ref *tab;
303
304
28.2M
    check_op(2);
305
28.2M
    check_type(op[-1], t_name);
306
28.2M
    check_proc(*op);
307
28.2M
    switch (r_space(op)) {
308
28.2M
        case avm_global:
309
28.2M
            opt = &i_ctx_p->op_array_table_global;
310
28.2M
            break;
311
0
        case avm_local:
312
0
            opt = &i_ctx_p->op_array_table_local;
313
0
            break;
314
0
        default:
315
0
            return_error(gs_error_invalidaccess);
316
28.2M
    }
317
28.2M
    count = opt->count;
318
28.2M
    tab = opt->table.value.refs;
319
    /*
320
     * restore doesn't reset op_array_table.count, but it does
321
     * remove entries from op_array_table.table.  Since we fill
322
     * the table in order, we can detect that a restore has occurred
323
     * by checking whether what should be the most recent entry
324
     * is occupied.  If not, we scan backwards over the vacated entries
325
     * to find the true end of the table.
326
     */
327
28.2M
    while (count > 0 && r_has_type(&tab[count - 1], t_null))
328
0
        --count;
329
28.2M
    if (count == r_size(&opt->table))
330
0
        return_error(gs_error_limitcheck);
331
28.2M
    ref_assign_old(&opt->table, &tab[count], op, "makeoperator");
332
28.2M
    opt->nx_table[count] = name_index(imemory, op - 1);
333
28.2M
    op_index_ref(imemory, opt->base_index + count, op - 1);
334
28.2M
    opt->count = count + 1;
335
28.2M
    pop(1);
336
28.2M
    return 0;
337
28.2M
}
338
339
/* - .oserrno <int> */
340
static int
341
zoserrno(i_ctx_t *i_ctx_p)
342
36.2k
{
343
36.2k
    os_ptr op = osp;
344
345
36.2k
    push(1);
346
36.2k
    make_int(op, errno);
347
36.2k
    return 0;
348
36.2k
}
349
350
/* <int> .setoserrno - */
351
static int
352
zsetoserrno(i_ctx_t *i_ctx_p)
353
345k
{
354
345k
    os_ptr op = osp;
355
356
345k
    check_op(1);
357
345k
    check_type(*op, t_integer);
358
345k
    errno = op->value.intval;
359
345k
    pop(1);
360
345k
    return 0;
361
345k
}
362
363
/* <int> .oserrorstring <string> true */
364
/* <int> .oserrorstring false */
365
static int
366
zoserrorstring(i_ctx_t *i_ctx_p)
367
30.6k
{
368
30.6k
    os_ptr op = osp;
369
30.6k
    const char *str;
370
30.6k
    int code;
371
30.6k
    uint len;
372
30.6k
    byte ch;
373
374
30.6k
    check_op(1);
375
30.6k
    check_type(*op, t_integer);
376
30.6k
    str = gp_strerror((int)op->value.intval);
377
30.6k
    if (str == 0 || (len = strlen(str)) == 0) {
378
0
        make_false(op);
379
0
        return 0;
380
0
    }
381
30.6k
    check_ostack(1);
382
30.6k
    code = string_to_ref(str, op, iimemory, ".oserrorstring");
383
30.6k
    if (code < 0)
384
0
        return code;
385
    /* Strip trailing end-of-line characters. */
386
30.6k
    while ((len = r_size(op)) != 0 &&
387
30.6k
           ((ch = op->value.bytes[--len]) == '\r' || ch == '\n')
388
30.6k
        )
389
0
        r_dec_size(op, 1);
390
30.6k
    push(1);
391
30.6k
    make_true(op);
392
30.6k
    return 0;
393
30.6k
}
394
395
/* <string> <bool> .setdebug - */
396
static int
397
zsetdebug(i_ctx_t *i_ctx_p)
398
0
{
399
0
    os_ptr op = osp;
400
0
    check_op(2);
401
0
    check_read_type(op[-1], t_string);
402
0
    check_type(*op, t_boolean);
403
0
    {
404
0
        int i;
405
406
0
        for (i = 0; i < r_size(op - 1); i++)
407
0
            gs_debug[op[-1].value.bytes[i] & 127] =
408
0
                op->value.boolval;
409
0
    }
410
0
    pop(2);
411
0
    return 0;
412
0
}
413
414
/* .mementolistnew - */
415
static int
416
zmementolistnewblocks(i_ctx_t *i_ctx_p)
417
0
{
418
0
    Memento_listNewBlocks();
419
0
    return 0;
420
0
}
421
422
/* There are a few cases where a customer/user might want CPSI behavior
423
 * instead of the GS default behavior. cmyk_to_rgb and Type 1 char fill
424
 * method are two that have come up so far. This operator allows a PS
425
 * program to control the behavior without needing to recompile.
426
 */
427
/* <bool> .setCPSImode - */
428
static int
429
zsetCPSImode(i_ctx_t *i_ctx_p)
430
0
{
431
0
    os_ptr op = osp;
432
0
    check_op(1);
433
0
    check_type(*op, t_boolean);
434
0
    gs_setcpsimode(imemory, op->value.boolval);
435
0
    if (op->value.boolval) {
436
0
        i_ctx_p->scanner_options |= SCAN_CPSI_MODE;
437
0
    }
438
0
    else {
439
0
        i_ctx_p->scanner_options &= ~(int)SCAN_CPSI_MODE;
440
0
    }
441
0
    pop(1);
442
0
    return 0;
443
0
}
444
445
/* - .getCPSImode <bool> */
446
static int
447
zgetCPSImode(i_ctx_t *i_ctx_p)
448
27
{
449
27
    os_ptr op = osp;
450
451
27
    push(1);
452
27
    make_bool(op, gs_currentcpsimode(imemory));
453
27
    return 0;
454
27
}
455
456
/* <int> .setscanconverter - */
457
static int
458
zsetscanconverter(i_ctx_t *i_ctx_p)
459
0
{
460
0
    int val;
461
462
0
    os_ptr op = osp;
463
0
    check_op(1);
464
0
    if (r_has_type(op, t_boolean))
465
0
        val = (int)op->value.boolval;
466
0
    else if (r_has_type(op, t_integer))
467
0
        val = op->value.intval;
468
0
    else
469
0
        return_op_typecheck(op);
470
471
0
    gs_setscanconverter(igs, val);
472
0
    pop(1);
473
0
    return 0;
474
0
}
475
476
/* - .getscanconverter <int> */
477
static int
478
zgetscanconverter(i_ctx_t *i_ctx_p)
479
0
{
480
0
    os_ptr op = osp;
481
482
0
    push(1);
483
0
    make_int(op, gs_getscanconverter(imemory));
484
0
    return 0;
485
0
}
486
/* ------ Initialization procedure ------ */
487
488
const op_def zmisc_a_op_defs[] =
489
{
490
    {"1bind", zbind},
491
    {"1getenv", zgetenv},
492
    {"0.defaultpapersize", zdefaultpapersize},
493
    {"2.makeoperator", zmakeoperator},
494
    {"0.oserrno", zoserrno},
495
    {"1.oserrorstring", zoserrorstring},
496
    {"0realtime", zrealtime},
497
    {"0serialnumber", zserialnumber},
498
    {"2.setdebug", zsetdebug},
499
    {"0.mementolistnewblocks", zmementolistnewblocks},
500
    {"1.setoserrno", zsetoserrno},
501
    {"0usertime", zusertime},
502
    op_def_end(0)
503
};
504
505
const op_def zmisc_b_op_defs[] =
506
{
507
    {"1.setCPSImode", zsetCPSImode},
508
    {"0.getCPSImode", zgetCPSImode},
509
    {"1.setscanconverter", zsetscanconverter},
510
    {"0.getscanconverter", zgetscanconverter},
511
    op_def_end(0)
512
};