Coverage Report

Created: 2025-06-10 07:27

/src/ghostpdl/psi/zrelbit.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
/* Relational, boolean, and bit operators */
18
#include "ghost.h"
19
#include "oper.h"
20
#include "gsutil.h"
21
#include "idict.h"
22
#include "store.h"
23
#include "gsstate.h"
24
25
/*
26
 * Many of the procedures in this file are public only so they can be
27
 * called from the FunctionType 4 interpreter (zfunc4.c).
28
 */
29
30
/* ------ Standard operators ------ */
31
32
/* Define the type test for eq and its relatives. */
33
#define EQ_CHECK_READ(opp, dflt)\
34
571M
    switch ( r_type(opp) ) {\
35
15.4M
        case t_string:\
36
15.4M
            check_read(*(opp));\
37
15.4M
            break;\
38
555M
        default:\
39
833M
            dflt;\
40
571M
  }
41
42
/* Forward references */
43
static int obj_le(os_ptr, os_ptr);
44
45
/* <obj1> <obj2> eq <bool> */
46
int
47
zeq(i_ctx_t *i_ctx_p)
48
285M
{
49
285M
    os_ptr op = osp;
50
285M
    check_op(2);
51
285M
    EQ_CHECK_READ(op - 1, check_op(2));
52
285M
    EQ_CHECK_READ(op, DO_NOTHING);
53
285M
    make_bool(op - 1, (obj_eq(imemory, op - 1, op) ? 1 : 0));
54
285M
    pop(1);
55
285M
    return 0;
56
285M
}
57
58
/* <obj1> <obj2> ne <bool> */
59
int
60
zne(i_ctx_t *i_ctx_p)
61
62.6M
{ /* We'll just be lazy and use eq. */
62
62.6M
    int code = zeq(i_ctx_p);
63
64
62.6M
    if (!code)
65
62.6M
        osp->value.boolval ^= 1;
66
62.6M
    return code;
67
62.6M
}
68
69
/* <num1> <num2> ge <bool> */
70
/* <str1> <str2> ge <bool> */
71
int
72
zge(i_ctx_t *i_ctx_p)
73
795k
{
74
795k
    os_ptr op = osp;
75
795k
    int code;
76
77
795k
    check_op(2);
78
795k
    code = obj_le(op, op - 1);
79
795k
    if (code < 0)
80
0
        return code;
81
795k
    make_bool(op - 1, code);
82
795k
    pop(1);
83
795k
    return 0;
84
795k
}
85
86
/* <num1> <num2> gt <bool> */
87
/* <str1> <str2> gt <bool> */
88
int
89
zgt(i_ctx_t *i_ctx_p)
90
24.5M
{
91
24.5M
    os_ptr op = osp;
92
24.5M
    int code;
93
94
24.5M
    check_op(2);
95
24.5M
    code = obj_le(op - 1, op);
96
24.5M
    if (code < 0)
97
1
        return code;
98
24.5M
    make_bool(op - 1, code ^ 1);
99
24.5M
    pop(1);
100
24.5M
    return 0;
101
24.5M
}
102
103
/* <num1> <num2> le <bool> */
104
/* <str1> <str2> le <bool> */
105
int
106
zle(i_ctx_t *i_ctx_p)
107
1.37M
{
108
1.37M
    os_ptr op = osp;
109
1.37M
    int code;
110
111
1.37M
    check_op(2);
112
1.37M
    code = obj_le(op - 1, op);
113
1.37M
    if (code < 0)
114
1
        return code;
115
1.37M
    make_bool(op - 1, code);
116
1.37M
    pop(1);
117
1.37M
    return 0;
118
1.37M
}
119
120
/* <num1> <num2> lt <bool> */
121
/* <str1> <str2> lt <bool> */
122
int
123
zlt(i_ctx_t *i_ctx_p)
124
3.25M
{
125
3.25M
    os_ptr op = osp;
126
3.25M
    int code;
127
128
3.25M
    check_op(2);
129
3.25M
    code = obj_le(op, op - 1);
130
3.25M
    if (code < 0)
131
0
        return code;
132
3.25M
    make_bool(op - 1, code ^ 1);
133
3.25M
    pop(1);
134
3.25M
    return 0;
135
3.25M
}
136
137
/* <num1> <num2> .max <num> */
138
/* <str1> <str2> .max <str> */
139
static int
140
zmax(i_ctx_t *i_ctx_p)
141
955k
{
142
955k
    os_ptr op = osp;
143
955k
    int code;
144
145
955k
    check_op(2);
146
955k
    code = obj_le(op - 1, op);
147
955k
    if (code < 0)
148
0
        return code;
149
955k
    if (code) {
150
52.5k
        ref_assign(op - 1, op);
151
52.5k
    }
152
955k
    pop(1);
153
955k
    return 0;
154
955k
}
155
156
/* <num1> <num2> .min <num> */
157
/* <str1> <str2> .min <str> */
158
static int
159
zmin(i_ctx_t *i_ctx_p)
160
209k
{
161
209k
    os_ptr op = osp;
162
209k
    int code;
163
164
209k
    check_op(2);
165
209k
    code = obj_le(op - 1, op);
166
209k
    if (code < 0)
167
0
        return code;
168
209k
    if (!code) {
169
0
        ref_assign(op - 1, op);
170
0
    }
171
209k
    pop(1);
172
209k
    return 0;
173
209k
}
174
175
/* <bool1> <bool2> and <bool> */
176
/* <int1> <int2> and <int> */
177
int
178
zand(i_ctx_t *i_ctx_p)
179
41.2M
{
180
41.2M
    os_ptr op = osp;
181
182
41.2M
    check_op(2);
183
41.2M
    switch (r_type(op)) {
184
41.2M
        case t_boolean:
185
41.2M
            check_type(op[-1], t_boolean);
186
41.2M
            op[-1].value.boolval &= op->value.boolval;
187
41.2M
            break;
188
8
        case t_integer:
189
8
            check_type(op[-1], t_integer);
190
8
            op[-1].value.intval &= op->value.intval;
191
8
            break;
192
1
        default:
193
1
            return_op_typecheck(op);
194
41.2M
    }
195
41.2M
    pop(1);
196
41.2M
    return 0;
197
41.2M
}
198
199
/* <bool> not <bool> */
200
/* <int> not <int> */
201
int
202
znot(i_ctx_t *i_ctx_p)
203
18.8M
{
204
18.8M
    os_ptr op = osp;
205
206
18.8M
    check_op(1);
207
18.8M
    switch (r_type(op)) {
208
18.8M
        case t_boolean:
209
18.8M
            op->value.boolval = !op->value.boolval;
210
18.8M
            break;
211
0
        case t_integer:
212
0
            op->value.intval = ~op->value.intval;
213
0
            break;
214
1
        default:
215
1
            return_op_typecheck(op);
216
18.8M
    }
217
18.8M
    return 0;
218
18.8M
}
219
220
/* <bool1> <bool2> or <bool> */
221
/* <int1> <int2> or <int> */
222
int
223
zor(i_ctx_t *i_ctx_p)
224
14.7M
{
225
14.7M
    os_ptr op = osp;
226
227
14.7M
    check_op(2);
228
14.7M
    switch (r_type(op)) {
229
14.7M
        case t_boolean:
230
14.7M
            check_type(op[-1], t_boolean);
231
14.7M
            op[-1].value.boolval |= op->value.boolval;
232
14.7M
            break;
233
614
        case t_integer:
234
614
            check_type(op[-1], t_integer);
235
613
            op[-1].value.intval |= op->value.intval;
236
613
            break;
237
1
        default:
238
1
            return_op_typecheck(op);
239
14.7M
    }
240
14.7M
    pop(1);
241
14.7M
    return 0;
242
14.7M
}
243
244
/* <bool1> <bool2> xor <bool> */
245
/* <int1> <int2> xor <int> */
246
int
247
zxor(i_ctx_t *i_ctx_p)
248
1
{
249
1
    os_ptr op = osp;
250
251
1
    check_op(2);
252
0
    switch (r_type(op)) {
253
0
        case t_boolean:
254
0
            check_type(op[-1], t_boolean);
255
0
            op[-1].value.boolval ^= op->value.boolval;
256
0
            break;
257
0
        case t_integer:
258
0
            check_type(op[-1], t_integer);
259
0
            op[-1].value.intval ^= op->value.intval;
260
0
            break;
261
0
        default:
262
0
            return_op_typecheck(op);
263
0
    }
264
0
    pop(1);
265
0
    return 0;
266
0
}
267
268
/* <int> <shift> bitshift <int> */
269
int
270
zbitshift(i_ctx_t *i_ctx_p)
271
1
{
272
1
    os_ptr op = osp;
273
1
    int shift;
274
1
    short max_shift = (sizeof(ps_int) * 8) - 1;
275
1
    short max_shift32 = (sizeof(ps_int32) * 8) - 1;
276
277
1
    check_op(2);
278
0
    check_type(*op, t_integer);
279
0
    check_type(op[-1], t_integer);
280
0
    if ((op->value.intval < -max_shift) || (op->value.intval > max_shift))
281
0
        op[-1].value.intval = 0;
282
0
    else if (sizeof(ps_int) != 4 && gs_currentcpsimode(imemory) && (op->value.intval < -max_shift32 || op->value.intval > max_shift32))
283
0
        op[-1].value.intval = 0;
284
0
    else if ((shift = op->value.intval) < 0) {
285
0
        if (sizeof(ps_int) != 4 && gs_currentcpsimode(imemory)) {
286
0
            ps_int32 val = (ps_int32)(op[-1].value.intval);
287
0
            op[-1].value.intval = (ps_int)((uint)(val)) >> -shift;
288
0
        }
289
0
        else {
290
0
            op[-1].value.intval = ((ps_int)(op[-1].value.intval)) >> -shift;
291
0
        }
292
0
    }
293
0
    else {
294
0
        if (sizeof(ps_int) != 4 && gs_currentcpsimode(imemory)) {
295
0
            ps_int32 val = (ps_int32)(op[-1].value.intval);
296
297
0
            op[-1].value.intval = (ps_int)(val << shift);
298
0
        }
299
0
        else
300
0
           op[-1].value.intval <<= shift;
301
0
    }
302
0
    pop(1);
303
0
    return 0;
304
0
}
305
306
/* ------ Extensions ------ */
307
308
/* <obj1> <obj2> .identeq <bool> */
309
static int
310
zidenteq(i_ctx_t *i_ctx_p)
311
0
{
312
0
    os_ptr op = osp;
313
314
0
    check_op(2);
315
0
    EQ_CHECK_READ(op - 1, check_op(2));
316
0
    EQ_CHECK_READ(op, DO_NOTHING);
317
0
    make_bool(op - 1, (obj_ident_eq(imemory, op - 1, op) ? 1 : 0));
318
0
    pop(1);
319
0
    return 0;
320
321
0
}
322
323
/* <obj1> <obj2> .identne <bool> */
324
static int
325
zidentne(i_ctx_t *i_ctx_p)
326
0
{
327
        /* We'll just be lazy and use .identeq. */
328
0
    os_ptr op = osp;
329
0
    int code;
330
331
0
    check_op(1);
332
0
    code = zidenteq(i_ctx_p);
333
0
    if (!code)
334
0
        osp->value.boolval ^= 1;
335
0
    return code;
336
0
}
337
338
/* ------ Initialization procedure ------ */
339
340
const op_def zrelbit_op_defs[] =
341
{
342
    {"2and", zand},
343
    {"2bitshift", zbitshift},
344
    {"2eq", zeq},
345
    {"2ge", zge},
346
    {"2gt", zgt},
347
    {"2le", zle},
348
    {"2lt", zlt},
349
    {"2.max", zmax},
350
    {"2.min", zmin},
351
    {"2ne", zne},
352
    {"1not", znot},
353
    {"2or", zor},
354
    {"2xor", zxor},
355
                /* Extensions */
356
    {"2.identeq", zidenteq},
357
    {"2.identne", zidentne},
358
    op_def_end(0)
359
};
360
361
/* ------ Internal routines ------ */
362
363
/* Compare two operands (both numeric, or both strings). */
364
/* Return 1 if op[-1] <= op[0], 0 if op[-1] > op[0], */
365
/* or a (negative) error code. */
366
static int
367
obj_le(register os_ptr op1, register os_ptr op)
368
31.1M
{
369
31.1M
    switch (r_type(op1)) {
370
18.0M
        case t_integer:
371
18.0M
            switch (r_type(op)) {
372
17.8M
                case t_integer:
373
17.8M
                    return (op1->value.intval <= op->value.intval);
374
168k
                case t_real:
375
168k
                    return ((double)op1->value.intval <= op->value.realval);
376
0
                default:
377
0
                    return_op_typecheck(op);
378
18.0M
            }
379
5.60M
        case t_real:
380
5.60M
            switch (r_type(op)) {
381
5.60M
                case t_real:
382
5.60M
                    return (op1->value.realval <= op->value.realval);
383
1.39k
                case t_integer:
384
1.39k
                    return (op1->value.realval <= (double)op->value.intval);
385
0
                default:
386
0
                    return_op_typecheck(op);
387
5.60M
            }
388
7.50M
        case t_string:
389
7.50M
            check_read(*op1);
390
7.50M
            check_read_type(*op, t_string);
391
7.50M
            return (bytes_compare(op1->value.bytes, r_size(op1),
392
7.50M
                                  op->value.bytes, r_size(op)) <= 0);
393
2
        default:
394
2
            return_op_typecheck(op1);
395
31.1M
    }
396
31.1M
}