Coverage Report

Created: 2025-06-10 06:56

/src/ghostpdl/psi/zmisc.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2023 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
36.8M
{
43
36.8M
    return (r_has_attr(rp, a_executable) &&
44
36.8M
            (r_btype(rp) == t_operator || r_type(rp) == t_oparray));
45
36.8M
}
46
static int
47
zbind(i_ctx_t *i_ctx_p)
48
2.73M
{
49
2.73M
    os_ptr op = osp;
50
2.73M
    uint depth = 1;
51
2.73M
    ref defn;
52
2.73M
    register os_ptr bsp;
53
54
2.73M
    check_op(1);
55
2.73M
    switch (r_type(op)) {
56
14.2k
        case t_array:
57
14.2k
            if (!r_has_attr(op, a_write)) {
58
0
                return 0; /* per PLRM3 */
59
0
            }
60
1.31M
        case t_mixedarray:
61
1.59M
        case t_shortarray:
62
1.59M
            defn = *op;
63
1.59M
            break;
64
253k
        case t_oparray:
65
253k
            defn = *op->value.const_refs;
66
253k
            break;
67
890k
        default:
68
890k
            return_op_typecheck(op);
69
2.73M
    }
70
2.73M
    push(1);
71
1.84M
    *op = defn;
72
1.84M
    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
25.8M
    while (depth) {
83
218M
        while (r_size(bsp)) {
84
194M
            ref_packed *const tpp = (ref_packed *)bsp->value.packed; /* break const */
85
86
194M
            r_dec_size(bsp, 1);
87
194M
            if (r_is_packed(tpp)) {
88
                /* Check for a packed executable name */
89
123M
                ushort elt = *tpp;
90
91
123M
                if (r_packed_is_exec_name(&elt)) {
92
28.5M
                    ref nref;
93
28.5M
                    ref *pvalue;
94
95
28.5M
                    name_index_ref(imemory, packed_name_index(&elt),
96
28.5M
                                   &nref);
97
28.5M
                    if ((pvalue = dict_find_name(&nref)) != 0 &&
98
28.5M
                        r_is_ex_oper(pvalue)
99
28.5M
                        ) {
100
23.6M
                        store_check_dest(bsp, pvalue);
101
                        /*
102
                         * Always save the change, since this can only
103
                         * happen once.
104
                         */
105
23.6M
                        ref_do_save(bsp, tpp, "bind");
106
23.6M
                        *tpp = pt_tag(pt_executable_operator) +
107
23.6M
                            op_index(pvalue);
108
23.6M
                    }
109
28.5M
                }
110
123M
                bsp->value.packed = tpp + 1;
111
123M
            } else {
112
71.4M
                ref *const tp = bsp->value.refs++;
113
114
71.4M
                switch (r_type(tp)) {
115
19.1M
                    case t_name:  /* bind the name if an operator */
116
19.1M
                        if (r_has_attr(tp, a_executable)) {
117
13.0M
                            ref *pvalue;
118
119
13.0M
                            if ((pvalue = dict_find_name(tp)) != 0 &&
120
13.0M
                                r_is_ex_oper(pvalue)
121
13.0M
                                ) {
122
6.71M
                                store_check_dest(bsp, pvalue);
123
6.71M
                                ref_assign_old(bsp, tp, pvalue, "bind");
124
6.71M
                            }
125
13.0M
                        }
126
19.1M
                        break;
127
19.1M
                    case t_array: /* push into array if writable */
128
276k
                        if (!r_has_attr(tp, a_write))
129
44.6k
                            break;
130
15.9M
                    case t_mixedarray:
131
22.2M
                    case t_shortarray:
132
22.2M
                        if (r_has_attr(tp, a_executable)) {
133
                            /* Make reference read-only */
134
22.1M
                            r_clear_attrs(tp, a_write);
135
22.1M
                            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
22.1M
                                *++bsp = *tp;
151
22.1M
                            depth++;
152
22.1M
                        }
153
71.4M
                }
154
71.4M
            }
155
194M
        }
156
23.9M
        bsp--;
157
23.9M
        depth--;
158
23.9M
        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
23.9M
    }
164
1.84M
    osp = bsp;
165
1.84M
    return 0;
166
1.84M
}
167
168
/* - serialnumber <int> */
169
static int
170
zserialnumber(i_ctx_t *i_ctx_p)
171
0
{
172
0
    os_ptr op = osp;
173
174
0
    push(1);
175
0
    make_int(op, gp_serialnumber());
176
0
    return 0;
177
0
}
178
179
/* - realtime <int> */
180
static int
181
zrealtime(i_ctx_t *i_ctx_p)
182
0
{
183
0
    os_ptr op = osp;
184
0
    long secs_ns[2];
185
0
    gs_lib_ctx_t *libctx = gs_lib_ctx_get_interp_instance(imemory);
186
187
0
    gp_get_realtime(secs_ns);
188
0
    secs_ns[1] -= libctx->real_time_0[1];
189
0
    secs_ns[0] -= libctx->real_time_0[0];
190
0
    push(1);
191
0
    make_int(op, secs_ns[0] * 1000 + secs_ns[1] / 1000000);
192
0
    return 0;
193
0
}
194
195
/* - usertime <int> */
196
static int
197
zusertime(i_ctx_t *i_ctx_p)
198
1
{
199
1
    gs_context_state_t *current = (gs_context_state_t *)i_ctx_p;
200
1
    os_ptr op = osp;
201
1
    long secs_ns[2];
202
203
1
    gp_get_usertime(secs_ns);
204
1
    if (!current->usertime_inited) {
205
1
        current->usertime_inited = true;
206
1
        current->usertime_0[0] = secs_ns[0];
207
1
        current->usertime_0[1] = secs_ns[1];
208
1
    }
209
1
    secs_ns[0] -= current->usertime_0[0];
210
1
    secs_ns[1] -= current->usertime_0[1];
211
1
    push(1);
212
1
    make_int(op, secs_ns[0] * 1000 + secs_ns[1] / 1000000);
213
1
    return 0;
214
1
}
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
12.4k
{
223
12.4k
    os_ptr op = osp;
224
12.4k
    char *str;
225
12.4k
    byte *value;
226
12.4k
    int len = 0;
227
228
12.4k
    check_op(1);
229
12.4k
    check_read_type(*op, t_string);
230
12.4k
    str = ref_to_string(op, imemory, "getenv key");
231
12.4k
    if (str == 0)
232
0
        return_error(gs_error_VMerror);
233
12.4k
    if (gp_getenv(str, (char *)0, &len) > 0) { /* key missing */
234
12.4k
        ifree_string((byte *) str, r_size(op) + 1, "getenv key");
235
12.4k
        make_false(op);
236
12.4k
        return 0;
237
12.4k
    }
238
0
    value = ialloc_string(len, "getenv value");
239
0
    if (value == 0) {
240
0
        ifree_string((byte *) str, r_size(op) + 1, "getenv key");
241
0
        return_error(gs_error_VMerror);
242
0
    }
243
0
    DISCARD(gp_getenv(str, (char *)value, &len));  /* can't fail */
244
0
    ifree_string((byte *) str, r_size(op) + 1, "getenv key");
245
    /* Delete the stupid C string terminator. */
246
0
    value = iresize_string(value, len, len - 1,
247
0
                           "getenv value"); /* can't fail */
248
0
    push(1);
249
0
    make_string(op - 1, a_all | icurrent_space, len - 1, value);
250
0
    make_true(op);
251
0
    return 0;
252
0
}
253
254
/* - .defaultpapersize <string> true */
255
/* - .defaultpapersize false */
256
static int
257
zdefaultpapersize(i_ctx_t *i_ctx_p)
258
1.78k
{
259
1.78k
    os_ptr op = osp;
260
1.78k
    byte *value;
261
1.78k
    int len = 0, i;
262
263
1.78k
    if (gp_defaultpapersize((char *)0, &len) > 0) {
264
        /* no default paper size */
265
1.78k
        push(1);
266
1.78k
        make_false(op);
267
1.78k
        return 0;
268
1.78k
    }
269
270
0
    value = ialloc_string(len, "defaultpapersize value");
271
0
    if (value == 0) {
272
0
        return_error(gs_error_VMerror);
273
0
    }
274
0
    DISCARD(gp_defaultpapersize((char *)value, &len)); /* can't fail */
275
    /* Note 'len' includes the NULL terminator, which we can ignore */
276
0
    for (i = 0;i < (len - 1); i++)
277
0
        value[i] = tolower(value[i]);
278
279
    /* Delete the stupid C string terminator. */
280
0
    value = iresize_string(value, len, len - 1,
281
0
                           "defaultpapersize value"); /* can't fail */
282
0
    push(2);
283
0
    make_string(op - 1, a_all | icurrent_space, len - 1, value);
284
0
    make_true(op);
285
0
    return 0;
286
0
}
287
288
/* <name> <proc> .makeoperator <oper> */
289
static int
290
zmakeoperator(i_ctx_t *i_ctx_p)
291
328k
{
292
328k
    os_ptr op = osp;
293
328k
    op_array_table *opt;
294
328k
    uint count;
295
328k
    ref *tab;
296
297
328k
    check_op(2);
298
328k
    check_type(op[-1], t_name);
299
328k
    check_proc(*op);
300
328k
    switch (r_space(op)) {
301
328k
        case avm_global:
302
328k
            opt = &i_ctx_p->op_array_table_global;
303
328k
            break;
304
0
        case avm_local:
305
0
            opt = &i_ctx_p->op_array_table_local;
306
0
            break;
307
0
        default:
308
0
            return_error(gs_error_invalidaccess);
309
328k
    }
310
328k
    count = opt->count;
311
328k
    tab = opt->table.value.refs;
312
    /*
313
     * restore doesn't reset op_array_table.count, but it does
314
     * remove entries from op_array_table.table.  Since we fill
315
     * the table in order, we can detect that a restore has occurred
316
     * by checking whether what should be the most recent entry
317
     * is occupied.  If not, we scan backwards over the vacated entries
318
     * to find the true end of the table.
319
     */
320
328k
    while (count > 0 && r_has_type(&tab[count - 1], t_null))
321
0
        --count;
322
328k
    if (count == r_size(&opt->table))
323
0
        return_error(gs_error_limitcheck);
324
328k
    ref_assign_old(&opt->table, &tab[count], op, "makeoperator");
325
328k
    opt->nx_table[count] = name_index(imemory, op - 1);
326
328k
    op_index_ref(imemory, opt->base_index + count, op - 1);
327
328k
    opt->count = count + 1;
328
328k
    pop(1);
329
328k
    return 0;
330
328k
}
331
332
/* - .oserrno <int> */
333
static int
334
zoserrno(i_ctx_t *i_ctx_p)
335
85
{
336
85
    os_ptr op = osp;
337
338
85
    push(1);
339
85
    make_int(op, errno);
340
85
    return 0;
341
85
}
342
343
/* <int> .setoserrno - */
344
static int
345
zsetoserrno(i_ctx_t *i_ctx_p)
346
3.65k
{
347
3.65k
    os_ptr op = osp;
348
349
3.65k
    check_op(1);
350
3.65k
    check_type(*op, t_integer);
351
3.65k
    errno = op->value.intval;
352
3.65k
    pop(1);
353
3.65k
    return 0;
354
3.65k
}
355
356
/* <int> .oserrorstring <string> true */
357
/* <int> .oserrorstring false */
358
static int
359
zoserrorstring(i_ctx_t *i_ctx_p)
360
85
{
361
85
    os_ptr op = osp;
362
85
    const char *str;
363
85
    int code;
364
85
    uint len;
365
85
    byte ch;
366
367
85
    check_op(1);
368
85
    check_type(*op, t_integer);
369
85
    str = gp_strerror((int)op->value.intval);
370
85
    if (str == 0 || (len = strlen(str)) == 0) {
371
0
        make_false(op);
372
0
        return 0;
373
0
    }
374
85
    check_ostack(1);
375
85
    code = string_to_ref(str, op, iimemory, ".oserrorstring");
376
85
    if (code < 0)
377
0
        return code;
378
    /* Strip trailing end-of-line characters. */
379
85
    while ((len = r_size(op)) != 0 &&
380
85
           ((ch = op->value.bytes[--len]) == '\r' || ch == '\n')
381
85
        )
382
0
        r_dec_size(op, 1);
383
85
    push(1);
384
85
    make_true(op);
385
85
    return 0;
386
85
}
387
388
/* <string> <bool> .setdebug - */
389
static int
390
zsetdebug(i_ctx_t *i_ctx_p)
391
0
{
392
0
    os_ptr op = osp;
393
0
    check_op(2);
394
0
    check_read_type(op[-1], t_string);
395
0
    check_type(*op, t_boolean);
396
0
    {
397
0
        int i;
398
399
0
        for (i = 0; i < r_size(op - 1); i++)
400
0
            gs_debug[op[-1].value.bytes[i] & 127] =
401
0
                op->value.boolval;
402
0
    }
403
0
    pop(2);
404
0
    return 0;
405
0
}
406
407
/* .mementolistnew - */
408
static int
409
zmementolistnewblocks(i_ctx_t *i_ctx_p)
410
0
{
411
0
    Memento_listNewBlocks();
412
0
    return 0;
413
0
}
414
415
/* There are a few cases where a customer/user might want CPSI behavior
416
 * instead of the GS default behavior. cmyk_to_rgb and Type 1 char fill
417
 * method are two that have come up so far. This operator allows a PS
418
 * program to control the behavior without needing to recompile.
419
 */
420
/* <bool> .setCPSImode - */
421
static int
422
zsetCPSImode(i_ctx_t *i_ctx_p)
423
0
{
424
0
    os_ptr op = osp;
425
0
    check_op(1);
426
0
    check_type(*op, t_boolean);
427
0
    gs_setcpsimode(imemory, op->value.boolval);
428
0
    if (op->value.boolval) {
429
0
        i_ctx_p->scanner_options |= SCAN_CPSI_MODE;
430
0
    }
431
0
    else {
432
0
        i_ctx_p->scanner_options &= ~(int)SCAN_CPSI_MODE;
433
0
    }
434
0
    pop(1);
435
0
    return 0;
436
0
}
437
438
/* - .getCPSImode <bool> */
439
static int
440
zgetCPSImode(i_ctx_t *i_ctx_p)
441
0
{
442
0
    os_ptr op = osp;
443
444
0
    push(1);
445
0
    make_bool(op, gs_currentcpsimode(imemory));
446
0
    return 0;
447
0
}
448
449
/* <int> .setscanconverter - */
450
static int
451
zsetscanconverter(i_ctx_t *i_ctx_p)
452
0
{
453
0
    int val;
454
455
0
    os_ptr op = osp;
456
0
    check_op(1);
457
0
    if (r_has_type(op, t_boolean))
458
0
        val = (int)op->value.boolval;
459
0
    else if (r_has_type(op, t_integer))
460
0
        val = op->value.intval;
461
0
    else
462
0
        return_op_typecheck(op);
463
464
0
    gs_setscanconverter(igs, val);
465
0
    pop(1);
466
0
    return 0;
467
0
}
468
469
/* - .getscanconverter <int> */
470
static int
471
zgetscanconverter(i_ctx_t *i_ctx_p)
472
0
{
473
0
    os_ptr op = osp;
474
475
0
    push(1);
476
0
    make_int(op, gs_getscanconverter(imemory));
477
0
    return 0;
478
0
}
479
/* ------ Initialization procedure ------ */
480
481
const op_def zmisc_a_op_defs[] =
482
{
483
    {"1bind", zbind},
484
    {"1getenv", zgetenv},
485
    {"0.defaultpapersize", zdefaultpapersize},
486
    {"2.makeoperator", zmakeoperator},
487
    {"0.oserrno", zoserrno},
488
    {"1.oserrorstring", zoserrorstring},
489
    {"0realtime", zrealtime},
490
    {"0serialnumber", zserialnumber},
491
    {"2.setdebug", zsetdebug},
492
    {"0.mementolistnewblocks", zmementolistnewblocks},
493
    {"1.setoserrno", zsetoserrno},
494
    {"0usertime", zusertime},
495
    op_def_end(0)
496
};
497
498
const op_def zmisc_b_op_defs[] =
499
{
500
    {"1.setCPSImode", zsetCPSImode},
501
    {"0.getCPSImode", zgetCPSImode},
502
    {"1.setscanconverter", zsetscanconverter},
503
    {"0.getscanconverter", zgetscanconverter},
504
    op_def_end(0)
505
};