Coverage Report

Created: 2025-06-10 06:59

/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
203M
{
43
203M
    return (r_has_attr(rp, a_executable) &&
44
203M
            (r_btype(rp) == t_operator || r_type(rp) == t_oparray));
45
203M
}
46
static int
47
zbind(i_ctx_t *i_ctx_p)
48
15.1M
{
49
15.1M
    os_ptr op = osp;
50
15.1M
    uint depth = 1;
51
15.1M
    ref defn;
52
15.1M
    register os_ptr bsp;
53
54
15.1M
    check_op(1);
55
15.1M
    switch (r_type(op)) {
56
80.0k
        case t_array:
57
80.0k
            if (!r_has_attr(op, a_write)) {
58
3
                return 0; /* per PLRM3 */
59
3
            }
60
7.28M
        case t_mixedarray:
61
8.81M
        case t_shortarray:
62
8.81M
            defn = *op;
63
8.81M
            break;
64
1.40M
        case t_oparray:
65
1.40M
            defn = *op->value.const_refs;
66
1.40M
            break;
67
4.92M
        default:
68
4.92M
            return_op_typecheck(op);
69
15.1M
    }
70
15.1M
    push(1);
71
10.2M
    *op = defn;
72
10.2M
    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
142M
    while (depth) {
83
1.21G
        while (r_size(bsp)) {
84
1.07G
            ref_packed *const tpp = (ref_packed *)bsp->value.packed; /* break const */
85
86
1.07G
            r_dec_size(bsp, 1);
87
1.07G
            if (r_is_packed(tpp)) {
88
                /* Check for a packed executable name */
89
682M
                ushort elt = *tpp;
90
91
682M
                if (r_packed_is_exec_name(&elt)) {
92
157M
                    ref nref;
93
157M
                    ref *pvalue;
94
95
157M
                    name_index_ref(imemory, packed_name_index(&elt),
96
157M
                                   &nref);
97
157M
                    if ((pvalue = dict_find_name(&nref)) != 0 &&
98
157M
                        r_is_ex_oper(pvalue)
99
157M
                        ) {
100
130M
                        store_check_dest(bsp, pvalue);
101
                        /*
102
                         * Always save the change, since this can only
103
                         * happen once.
104
                         */
105
130M
                        ref_do_save(bsp, tpp, "bind");
106
130M
                        *tpp = pt_tag(pt_executable_operator) +
107
130M
                            op_index(pvalue);
108
130M
                    }
109
157M
                }
110
682M
                bsp->value.packed = tpp + 1;
111
682M
            } else {
112
395M
                ref *const tp = bsp->value.refs++;
113
114
395M
                switch (r_type(tp)) {
115
105M
                    case t_name:  /* bind the name if an operator */
116
105M
                        if (r_has_attr(tp, a_executable)) {
117
72.3M
                            ref *pvalue;
118
119
72.3M
                            if ((pvalue = dict_find_name(tp)) != 0 &&
120
72.3M
                                r_is_ex_oper(pvalue)
121
72.3M
                                ) {
122
37.1M
                                store_check_dest(bsp, pvalue);
123
37.1M
                                ref_assign_old(bsp, tp, pvalue, "bind");
124
37.1M
                            }
125
72.3M
                        }
126
105M
                        break;
127
105M
                    case t_array: /* push into array if writable */
128
1.52M
                        if (!r_has_attr(tp, a_write))
129
246k
                            break;
130
87.9M
                    case t_mixedarray:
131
122M
                    case t_shortarray:
132
122M
                        if (r_has_attr(tp, a_executable)) {
133
                            /* Make reference read-only */
134
122M
                            r_clear_attrs(tp, a_write);
135
122M
                            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
122M
                                *++bsp = *tp;
151
122M
                            depth++;
152
122M
                        }
153
395M
                }
154
395M
            }
155
1.07G
        }
156
132M
        bsp--;
157
132M
        depth--;
158
132M
        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
132M
    }
164
10.2M
    osp = bsp;
165
10.2M
    return 0;
166
10.2M
}
167
168
/* - serialnumber <int> */
169
static int
170
zserialnumber(i_ctx_t *i_ctx_p)
171
1
{
172
1
    os_ptr op = osp;
173
174
1
    push(1);
175
1
    make_int(op, gp_serialnumber());
176
1
    return 0;
177
1
}
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
25
{
199
25
    gs_context_state_t *current = (gs_context_state_t *)i_ctx_p;
200
25
    os_ptr op = osp;
201
25
    long secs_ns[2];
202
203
25
    gp_get_usertime(secs_ns);
204
25
    if (!current->usertime_inited) {
205
18
        current->usertime_inited = true;
206
18
        current->usertime_0[0] = secs_ns[0];
207
18
        current->usertime_0[1] = secs_ns[1];
208
18
    }
209
25
    secs_ns[0] -= current->usertime_0[0];
210
25
    secs_ns[1] -= current->usertime_0[1];
211
25
    push(1);
212
25
    make_int(op, secs_ns[0] * 1000 + secs_ns[1] / 1000000);
213
25
    return 0;
214
25
}
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
69.0k
{
223
69.0k
    os_ptr op = osp;
224
69.0k
    char *str;
225
69.0k
    byte *value;
226
69.0k
    int len = 0;
227
228
69.0k
    check_op(1);
229
69.0k
    check_read_type(*op, t_string);
230
69.0k
    str = ref_to_string(op, imemory, "getenv key");
231
69.0k
    if (str == 0)
232
0
        return_error(gs_error_VMerror);
233
69.0k
    if (gp_getenv(str, (char *)0, &len) > 0) { /* key missing */
234
69.0k
        ifree_string((byte *) str, r_size(op) + 1, "getenv key");
235
69.0k
        make_false(op);
236
69.0k
        return 0;
237
69.0k
    }
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
9.86k
{
259
9.86k
    os_ptr op = osp;
260
9.86k
    byte *value;
261
9.86k
    int len = 0, i;
262
263
9.86k
    if (gp_defaultpapersize((char *)0, &len) > 0) {
264
        /* no default paper size */
265
9.86k
        push(1);
266
9.86k
        make_false(op);
267
9.86k
        return 0;
268
9.86k
    }
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
1.81M
{
292
1.81M
    os_ptr op = osp;
293
1.81M
    op_array_table *opt;
294
1.81M
    uint count;
295
1.81M
    ref *tab;
296
297
1.81M
    check_op(2);
298
1.81M
    check_type(op[-1], t_name);
299
1.81M
    check_proc(*op);
300
1.81M
    switch (r_space(op)) {
301
1.81M
        case avm_global:
302
1.81M
            opt = &i_ctx_p->op_array_table_global;
303
1.81M
            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
1.81M
    }
310
1.81M
    count = opt->count;
311
1.81M
    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
1.81M
    while (count > 0 && r_has_type(&tab[count - 1], t_null))
321
0
        --count;
322
1.81M
    if (count == r_size(&opt->table))
323
0
        return_error(gs_error_limitcheck);
324
1.81M
    ref_assign_old(&opt->table, &tab[count], op, "makeoperator");
325
1.81M
    opt->nx_table[count] = name_index(imemory, op - 1);
326
1.81M
    op_index_ref(imemory, opt->base_index + count, op - 1);
327
1.81M
    opt->count = count + 1;
328
1.81M
    pop(1);
329
1.81M
    return 0;
330
1.81M
}
331
332
/* - .oserrno <int> */
333
static int
334
zoserrno(i_ctx_t *i_ctx_p)
335
2.99k
{
336
2.99k
    os_ptr op = osp;
337
338
2.99k
    push(1);
339
2.99k
    make_int(op, errno);
340
2.99k
    return 0;
341
2.99k
}
342
343
/* <int> .setoserrno - */
344
static int
345
zsetoserrno(i_ctx_t *i_ctx_p)
346
22.7k
{
347
22.7k
    os_ptr op = osp;
348
349
22.7k
    check_op(1);
350
22.7k
    check_type(*op, t_integer);
351
22.7k
    errno = op->value.intval;
352
22.7k
    pop(1);
353
22.7k
    return 0;
354
22.7k
}
355
356
/* <int> .oserrorstring <string> true */
357
/* <int> .oserrorstring false */
358
static int
359
zoserrorstring(i_ctx_t *i_ctx_p)
360
2.97k
{
361
2.97k
    os_ptr op = osp;
362
2.97k
    const char *str;
363
2.97k
    int code;
364
2.97k
    uint len;
365
2.97k
    byte ch;
366
367
2.97k
    check_op(1);
368
2.97k
    check_type(*op, t_integer);
369
2.97k
    str = gp_strerror((int)op->value.intval);
370
2.97k
    if (str == 0 || (len = strlen(str)) == 0) {
371
0
        make_false(op);
372
0
        return 0;
373
0
    }
374
2.97k
    check_ostack(1);
375
2.97k
    code = string_to_ref(str, op, iimemory, ".oserrorstring");
376
2.97k
    if (code < 0)
377
0
        return code;
378
    /* Strip trailing end-of-line characters. */
379
2.97k
    while ((len = r_size(op)) != 0 &&
380
2.97k
           ((ch = op->value.bytes[--len]) == '\r' || ch == '\n')
381
2.97k
        )
382
0
        r_dec_size(op, 1);
383
2.97k
    push(1);
384
2.97k
    make_true(op);
385
2.97k
    return 0;
386
2.97k
}
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
};