Coverage Report

Created: 2026-02-14 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/psi/zrelbit.c
Line
Count
Source
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
10.0G
    switch ( r_type(opp) ) {\
35
267M
        case t_string:\
36
267M
            check_read(*(opp));\
37
267M
            break;\
38
9.73G
        default:\
39
14.6G
            dflt;\
40
10.0G
  }
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
5.00G
{
49
5.00G
    os_ptr op = osp;
50
5.00G
    check_op(2);
51
5.00G
    EQ_CHECK_READ(op - 1, check_op(2));
52
5.00G
    EQ_CHECK_READ(op, DO_NOTHING);
53
5.00G
    make_bool(op - 1, (obj_eq(imemory, op - 1, op) ? 1 : 0));
54
5.00G
    pop(1);
55
5.00G
    return 0;
56
5.00G
}
57
58
/* <obj1> <obj2> ne <bool> */
59
int
60
zne(i_ctx_t *i_ctx_p)
61
1.11G
{ /* We'll just be lazy and use eq. */
62
1.11G
    int code = zeq(i_ctx_p);
63
64
1.11G
    if (!code)
65
1.11G
        osp->value.boolval ^= 1;
66
1.11G
    return code;
67
1.11G
}
68
69
/* <num1> <num2> ge <bool> */
70
/* <str1> <str2> ge <bool> */
71
int
72
zge(i_ctx_t *i_ctx_p)
73
10.3M
{
74
10.3M
    os_ptr op = osp;
75
10.3M
    int code;
76
77
10.3M
    check_op(2);
78
10.3M
    code = obj_le(op, op - 1);
79
10.3M
    if (code < 0)
80
18
        return code;
81
10.3M
    make_bool(op - 1, code);
82
10.3M
    pop(1);
83
10.3M
    return 0;
84
10.3M
}
85
86
/* <num1> <num2> gt <bool> */
87
/* <str1> <str2> gt <bool> */
88
int
89
zgt(i_ctx_t *i_ctx_p)
90
388M
{
91
388M
    os_ptr op = osp;
92
388M
    int code;
93
94
388M
    check_op(2);
95
388M
    code = obj_le(op - 1, op);
96
388M
    if (code < 0)
97
25
        return code;
98
388M
    make_bool(op - 1, code ^ 1);
99
388M
    pop(1);
100
388M
    return 0;
101
388M
}
102
103
/* <num1> <num2> le <bool> */
104
/* <str1> <str2> le <bool> */
105
int
106
zle(i_ctx_t *i_ctx_p)
107
29.2M
{
108
29.2M
    os_ptr op = osp;
109
29.2M
    int code;
110
111
29.2M
    check_op(2);
112
29.2M
    code = obj_le(op - 1, op);
113
29.2M
    if (code < 0)
114
21
        return code;
115
29.2M
    make_bool(op - 1, code);
116
29.2M
    pop(1);
117
29.2M
    return 0;
118
29.2M
}
119
120
/* <num1> <num2> lt <bool> */
121
/* <str1> <str2> lt <bool> */
122
int
123
zlt(i_ctx_t *i_ctx_p)
124
23.3M
{
125
23.3M
    os_ptr op = osp;
126
23.3M
    int code;
127
128
23.3M
    check_op(2);
129
23.3M
    code = obj_le(op, op - 1);
130
23.3M
    if (code < 0)
131
21
        return code;
132
23.3M
    make_bool(op - 1, code ^ 1);
133
23.3M
    pop(1);
134
23.3M
    return 0;
135
23.3M
}
136
137
/* <num1> <num2> .max <num> */
138
/* <str1> <str2> .max <str> */
139
static int
140
zmax(i_ctx_t *i_ctx_p)
141
1.38M
{
142
1.38M
    os_ptr op = osp;
143
1.38M
    int code;
144
145
1.38M
    check_op(2);
146
1.38M
    code = obj_le(op - 1, op);
147
1.38M
    if (code < 0)
148
8
        return code;
149
1.38M
    if (code) {
150
783k
        ref_assign(op - 1, op);
151
783k
    }
152
1.38M
    pop(1);
153
1.38M
    return 0;
154
1.38M
}
155
156
/* <num1> <num2> .min <num> */
157
/* <str1> <str2> .min <str> */
158
static int
159
zmin(i_ctx_t *i_ctx_p)
160
2.84M
{
161
2.84M
    os_ptr op = osp;
162
2.84M
    int code;
163
164
2.84M
    check_op(2);
165
2.84M
    code = obj_le(op - 1, op);
166
2.84M
    if (code < 0)
167
8
        return code;
168
2.84M
    if (!code) {
169
7
        ref_assign(op - 1, op);
170
7
    }
171
2.84M
    pop(1);
172
2.84M
    return 0;
173
2.84M
}
174
175
/* <bool1> <bool2> and <bool> */
176
/* <int1> <int2> and <int> */
177
int
178
zand(i_ctx_t *i_ctx_p)
179
670M
{
180
670M
    os_ptr op = osp;
181
182
670M
    check_op(2);
183
670M
    switch (r_type(op)) {
184
670M
        case t_boolean:
185
670M
            check_type(op[-1], t_boolean);
186
670M
            op[-1].value.boolval &= op->value.boolval;
187
670M
            break;
188
1.00k
        case t_integer:
189
1.00k
            check_type(op[-1], t_integer);
190
997
            op[-1].value.intval &= op->value.intval;
191
997
            break;
192
16
        default:
193
16
            return_op_typecheck(op);
194
670M
    }
195
670M
    pop(1);
196
670M
    return 0;
197
670M
}
198
199
/* <bool> not <bool> */
200
/* <int> not <int> */
201
int
202
znot(i_ctx_t *i_ctx_p)
203
543M
{
204
543M
    os_ptr op = osp;
205
206
543M
    check_op(1);
207
543M
    switch (r_type(op)) {
208
543M
        case t_boolean:
209
543M
            op->value.boolval = !op->value.boolval;
210
543M
            break;
211
122
        case t_integer:
212
122
            op->value.intval = ~op->value.intval;
213
122
            break;
214
18
        default:
215
18
            return_op_typecheck(op);
216
543M
    }
217
543M
    return 0;
218
543M
}
219
220
/* <bool1> <bool2> or <bool> */
221
/* <int1> <int2> or <int> */
222
int
223
zor(i_ctx_t *i_ctx_p)
224
163M
{
225
163M
    os_ptr op = osp;
226
227
163M
    check_op(2);
228
163M
    switch (r_type(op)) {
229
163M
        case t_boolean:
230
163M
            check_type(op[-1], t_boolean);
231
163M
            op[-1].value.boolval |= op->value.boolval;
232
163M
            break;
233
4.37k
        case t_integer:
234
4.37k
            check_type(op[-1], t_integer);
235
4.35k
            op[-1].value.intval |= op->value.intval;
236
4.35k
            break;
237
25
        default:
238
25
            return_op_typecheck(op);
239
163M
    }
240
163M
    pop(1);
241
163M
    return 0;
242
163M
}
243
244
/* <bool1> <bool2> xor <bool> */
245
/* <int1> <int2> xor <int> */
246
int
247
zxor(i_ctx_t *i_ctx_p)
248
48
{
249
48
    os_ptr op = osp;
250
251
48
    check_op(2);
252
31
    switch (r_type(op)) {
253
5
        case t_boolean:
254
5
            check_type(op[-1], t_boolean);
255
5
            op[-1].value.boolval ^= op->value.boolval;
256
5
            break;
257
18
        case t_integer:
258
18
            check_type(op[-1], t_integer);
259
13
            op[-1].value.intval ^= op->value.intval;
260
13
            break;
261
8
        default:
262
8
            return_op_typecheck(op);
263
31
    }
264
18
    pop(1);
265
18
    return 0;
266
31
}
267
268
/* <int> <shift> bitshift <int> */
269
int
270
zbitshift(i_ctx_t *i_ctx_p)
271
419
{
272
419
    os_ptr op = osp;
273
419
    int shift;
274
419
    short max_shift = (sizeof(ps_int) * 8) - 1;
275
419
    short max_shift32 = (sizeof(ps_int32) * 8) - 1;
276
277
419
    check_op(2);
278
398
    check_type(*op, t_integer);
279
385
    check_type(op[-1], t_integer);
280
373
    if ((op->value.intval < -max_shift) || (op->value.intval > max_shift))
281
37
        op[-1].value.intval = 0;
282
336
    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
336
    else if ((shift = op->value.intval) < 0) {
285
63
        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
63
        else {
290
63
            op[-1].value.intval = ((ps_int)(op[-1].value.intval)) >> -shift;
291
63
        }
292
63
    }
293
273
    else {
294
273
        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
273
        else
300
273
           op[-1].value.intval <<= shift;
301
273
    }
302
373
    pop(1);
303
373
    return 0;
304
385
}
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
455M
{
369
455M
    switch (r_type(op1)) {
370
294M
        case t_integer:
371
294M
            switch (r_type(op)) {
372
292M
                case t_integer:
373
292M
                    return (op1->value.intval <= op->value.intval);
374
2.43M
                case t_real:
375
2.43M
                    return ((double)op1->value.intval <= op->value.realval);
376
13
                default:
377
13
                    return_op_typecheck(op);
378
294M
            }
379
32.3M
        case t_real:
380
32.3M
            switch (r_type(op)) {
381
32.3M
                case t_real:
382
32.3M
                    return (op1->value.realval <= op->value.realval);
383
23.5k
                case t_integer:
384
23.5k
                    return (op1->value.realval <= (double)op->value.intval);
385
10
                default:
386
10
                    return_op_typecheck(op);
387
32.3M
            }
388
128M
        case t_string:
389
128M
            check_read(*op1);
390
128M
            check_read_type(*op, t_string);
391
128M
            return (bytes_compare(op1->value.bytes, r_size(op1),
392
128M
                                  op->value.bytes, r_size(op)) <= 0);
393
70
        default:
394
70
            return_op_typecheck(op1);
395
455M
    }
396
455M
}