Coverage Report

Created: 2026-04-09 07:06

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