1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
/* Copyright (C) 1989, 1995, 1997, 1998, 1999 Aladdin Enterprises. All rights reserved.
This file is part of AFPL Ghostscript.
AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
distributor accepts any responsibility for the consequences of using it, or
for whether it serves any particular purpose or works at all, unless he or
she says so in writing. Refer to the Aladdin Free Public License (the
"License") for full details.
Every copy of AFPL Ghostscript must include a copy of the License, normally
in a plain ASCII text file named PUBLIC. The License grants you the right
to copy, modify and redistribute AFPL Ghostscript, but only under certain
conditions described in the License. Among other things, the License
requires that the copyright notice and this notice be preserved on all
copies.
*/
/*$Id$ */
/* Relational, boolean, and bit operators */
#include "ghost.h"
#include "oper.h"
#include "gsutil.h"
#include "idict.h"
#include "store.h"
/*
* Many of the procedures in this file are public only so they can be
* called from the FunctionType 4 interpreter (zfunc4.c).
*/
/* ------ Standard operators ------ */
/* Define the type test for eq and its relatives. */
#define EQ_CHECK_READ(opp, dflt)\
switch ( r_type(opp) ) {\
case t_string:\
check_read(*(opp));\
break;\
default:\
dflt;\
}
/* Forward references */
private int obj_le(P2(os_ptr, os_ptr));
/* <obj1> <obj2> eq <bool> */
int
zeq(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
EQ_CHECK_READ(op - 1, check_op(2));
EQ_CHECK_READ(op, DO_NOTHING);
make_bool(op - 1, (obj_eq(op - 1, op) ? 1 : 0));
pop(1);
return 0;
}
/* <obj1> <obj2> ne <bool> */
int
zne(i_ctx_t *i_ctx_p)
{ /* We'll just be lazy and use eq. */
int code = zeq(i_ctx_p);
if (!code)
osp->value.boolval ^= 1;
return code;
}
/* <num1> <num2> ge <bool> */
/* <str1> <str2> ge <bool> */
int
zge(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
int code = obj_le(op, op - 1);
if (code < 0)
return code;
make_bool(op - 1, code);
pop(1);
return 0;
}
/* <num1> <num2> gt <bool> */
/* <str1> <str2> gt <bool> */
int
zgt(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
int code = obj_le(op - 1, op);
if (code < 0)
return code;
make_bool(op - 1, code ^ 1);
pop(1);
return 0;
}
/* <num1> <num2> le <bool> */
/* <str1> <str2> le <bool> */
int
zle(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
int code = obj_le(op - 1, op);
if (code < 0)
return code;
make_bool(op - 1, code);
pop(1);
return 0;
}
/* <num1> <num2> lt <bool> */
/* <str1> <str2> lt <bool> */
int
zlt(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
int code = obj_le(op, op - 1);
if (code < 0)
return code;
make_bool(op - 1, code ^ 1);
pop(1);
return 0;
}
/* <num1> <num2> .max <num> */
/* <str1> <str2> .max <str> */
private int
zmax(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
int code = obj_le(op - 1, op);
if (code < 0)
return code;
if (code) {
ref_assign(op - 1, op);
}
pop(1);
return 0;
}
/* <num1> <num2> .min <num> */
/* <str1> <str2> .min <str> */
private int
zmin(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
int code = obj_le(op - 1, op);
if (code < 0)
return code;
if (!code) {
ref_assign(op - 1, op);
}
pop(1);
return 0;
}
/* <bool1> <bool2> and <bool> */
/* <int1> <int2> and <int> */
int
zand(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
switch (r_type(op)) {
case t_boolean:
check_type(op[-1], t_boolean);
op[-1].value.boolval &= op->value.boolval;
break;
case t_integer:
check_type(op[-1], t_integer);
op[-1].value.intval &= op->value.intval;
break;
default:
return_op_typecheck(op);
}
pop(1);
return 0;
}
/* <bool> not <bool> */
/* <int> not <int> */
int
znot(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
switch (r_type(op)) {
case t_boolean:
op->value.boolval = !op->value.boolval;
break;
case t_integer:
op->value.intval = ~op->value.intval;
break;
default:
return_op_typecheck(op);
}
return 0;
}
/* <bool1> <bool2> or <bool> */
/* <int1> <int2> or <int> */
int
zor(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
switch (r_type(op)) {
case t_boolean:
check_type(op[-1], t_boolean);
op[-1].value.boolval |= op->value.boolval;
break;
case t_integer:
check_type(op[-1], t_integer);
op[-1].value.intval |= op->value.intval;
break;
default:
return_op_typecheck(op);
}
pop(1);
return 0;
}
/* <bool1> <bool2> xor <bool> */
/* <int1> <int2> xor <int> */
int
zxor(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
switch (r_type(op)) {
case t_boolean:
check_type(op[-1], t_boolean);
op[-1].value.boolval ^= op->value.boolval;
break;
case t_integer:
check_type(op[-1], t_integer);
op[-1].value.intval ^= op->value.intval;
break;
default:
return_op_typecheck(op);
}
pop(1);
return 0;
}
/* <int> <shift> bitshift <int> */
int
zbitshift(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
int shift;
check_type(*op, t_integer);
check_type(op[-1], t_integer);
#define MAX_SHIFT (arch_sizeof_long * 8 - 1)
if (op->value.intval < -MAX_SHIFT || op->value.intval > MAX_SHIFT)
op[-1].value.intval = 0;
#undef MAX_SHIFT
else if ((shift = op->value.intval) < 0)
op[-1].value.intval = ((ulong)(op[-1].value.intval)) >> -shift;
else
op[-1].value.intval <<= shift;
pop(1);
return 0;
}
/* ------ Extensions ------ */
/* <obj1> <obj2> .identeq <bool> */
private int
zidenteq(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
EQ_CHECK_READ(op - 1, check_op(2));
EQ_CHECK_READ(op, DO_NOTHING);
make_bool(op - 1, (obj_ident_eq(op - 1, op) ? 1 : 0));
pop(1);
return 0;
}
/* <obj1> <obj2> .identne <bool> */
private int
zidentne(i_ctx_t *i_ctx_p)
{
/* We'll just be lazy and use .identeq. */
int code = zidenteq(i_ctx_p);
if (!code)
osp->value.boolval ^= 1;
return code;
}
/* ------ Initialization procedure ------ */
const op_def zrelbit_op_defs[] =
{
{"2and", zand},
{"2bitshift", zbitshift},
{"2eq", zeq},
{"2ge", zge},
{"2gt", zgt},
{"2le", zle},
{"2lt", zlt},
{"2.max", zmax},
{"2.min", zmin},
{"2ne", zne},
{"1not", znot},
{"2or", zor},
{"2xor", zxor},
/* Extensions */
{"2.identeq", zidenteq},
{"2.identne", zidentne},
op_def_end(0)
};
/* ------ Internal routines ------ */
/* Compare two operands (both numeric, or both strings). */
/* Return 1 if op[-1] <= op[0], 0 if op[-1] > op[0], */
/* or a (negative) error code. */
private int
obj_le(register os_ptr op1, register os_ptr op)
{
switch (r_type(op1)) {
case t_integer:
switch (r_type(op)) {
case t_integer:
return (op1->value.intval <= op->value.intval);
case t_real:
return ((double)op1->value.intval <= op->value.realval);
default:
return_op_typecheck(op);
}
case t_real:
switch (r_type(op)) {
case t_real:
return (op1->value.realval <= op->value.realval);
case t_integer:
return (op1->value.realval <= (double)op->value.intval);
default:
return_op_typecheck(op);
}
case t_string:
check_read(*op1);
check_read_type(*op, t_string);
return (bytes_compare(op1->value.bytes, r_size(op1),
op->value.bytes, r_size(op)) <= 0);
default:
return_op_typecheck(op1);
}
}
|