Coverage Report

Created: 2026-11-12 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/gsfunc4.c
Line
Count
Source
1
/* Copyright (C) 2001-2026 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
/* Implementation of FunctionType 4 (PostScript Calculator) Functions */
18
#include "math_.h"
19
#include "memory_.h"
20
#include "gx.h"
21
#include "gserrors.h"
22
#include "gsdsrc.h"
23
#include "gsfunc4.h"
24
#include "gxfarith.h"
25
#include "gxfunc.h"
26
#include "stream.h"
27
#include "strimpl.h"
28
#include "sfilter.h"    /* for SubFileDecode */
29
#include "spprint.h"
30
#include "stream.h"
31
32
typedef struct gs_function_PtCr_s {
33
    gs_function_head_t head;
34
    gs_function_PtCr_params_t params;
35
    /* Define a bogus DataSource for get_function_info. */
36
    gs_data_source_t data_source;
37
} gs_function_PtCr_t;
38
39
/* GC descriptor */
40
private_st_function_PtCr();
41
42
/* Define the maximum stack depth. */
43
65.6M
#define MAX_VSTACK 256    /* Max 100 is enough per PDF spec, but we use this
44
                                 * for DeviceN handling. Must be at least as large
45
                                 * as the number of components
46
                                 */
47
48
/* Define the structure of values on the stack. */
49
typedef enum {
50
    CVT_NONE = 0, /* empty stack slot */
51
    CVT_BOOL,
52
    CVT_INT,
53
    CVT_FLOAT
54
} calc_value_type_t;
55
typedef struct calc_value_s {
56
    calc_value_type_t type;
57
    union {
58
        int i;      /* also used for Boolean */
59
        float f;
60
    } value;
61
} calc_value_t;
62
63
/* Store a float. */
64
static inline void
65
store_float(calc_value_t *vsp, double f)
66
35.5M
{
67
35.5M
    vsp->value.f = f;
68
35.5M
    vsp->type = CVT_FLOAT;
69
35.5M
}
70
71
/*
72
 * Define extended opcodes with typed operands.  We use the original
73
 * opcodes for the floating-point case.
74
 */
75
typedef enum {
76
77
        /* Typed variants */
78
79
    PtCr_abs_int = PtCr_NUM_OPCODES,
80
    PtCr_add_int,
81
    PtCr_mul_int,
82
    PtCr_neg_int,
83
    PtCr_not_bool,    /* default is int */
84
    PtCr_sub_int,
85
    PtCr_eq_int,
86
    PtCr_ge_int,
87
    PtCr_gt_int,
88
    PtCr_le_int,
89
    PtCr_lt_int,
90
    PtCr_ne_int,
91
92
        /* Coerce and re-dispatch */
93
94
    PtCr_int_to_float,
95
    PtCr_2nd_int_to_float,
96
    PtCr_int2_to_float,
97
98
        /* Miscellaneous */
99
100
    PtCr_no_op,
101
    PtCr_typecheck
102
103
} gs_PtCr_typed_opcode_t;
104
105
/* Evaluate a PostScript Calculator function. */
106
static int
107
fn_PtCr_evaluate(const gs_function_t *pfn_common, const float *in, float *out)
108
5.70M
{
109
5.70M
    const gs_function_PtCr_t *pfn = (const gs_function_PtCr_t *)pfn_common;
110
5.70M
    calc_value_t vstack_buf[2 + MAX_VSTACK + 1];
111
5.70M
    calc_value_t *vstack = &vstack_buf[1];
112
5.70M
    calc_value_t *vsp = vstack + pfn->params.m;
113
5.70M
    const byte *p = pfn->params.ops.data;
114
5.70M
    int repeat_count[MAX_PSC_FUNCTION_NESTING];
115
5.70M
    int repeat_proc_size[MAX_PSC_FUNCTION_NESTING];
116
5.70M
    int repeat_nesting_level = -1;
117
5.70M
    int i;
118
119
    /*
120
     * Define the table for mapping explicit opcodes to typed opcodes.
121
     * We index this table with the opcode and the types of the top 2
122
     * values on the stack.
123
     */
124
5.70M
    static const struct op_defn_s {
125
5.70M
        byte opcode[16];  /* 4 * type[-1] + type[0] */
126
5.70M
    } op_defn_table[] = {
127
        /* Keep this consistent with opcodes in gsfunc4.h! */
128
129
416M
#define O4(op) op,op,op,op
130
919M
#define E PtCr_typecheck
131
234M
#define E4 O4(E)
132
5.70M
#define N PtCr_no_op
133
        /* 0-operand operators */
134
5.70M
#define OP_NONE(op)\
135
45.6M
  {{O4(op), O4(op), O4(op), O4(op)}}
136
        /* 1-operand operators */
137
5.70M
#define OP1(b, i, f)\
138
114M
  {{E,b,i,f, E,b,i,f, E,b,i,f, E,b,i,f}}
139
5.70M
#define OP_NUM1(i, f)\
140
45.6M
  OP1(E, i, f)
141
5.70M
#define OP_MATH1(f)\
142
28.5M
  OP1(E, PtCr_int_to_float, f)
143
5.70M
#define OP_ANY1(op)\
144
11.4M
  OP1(op, op, op)
145
        /* 2-operand operators */
146
5.70M
#define OP_NUM2(i, f)\
147
39.9M
  {{E4, E4, E,E,i,PtCr_2nd_int_to_float, E,E,PtCr_int_to_float,f}}
148
5.70M
#define OP_INT_BOOL2(i)\
149
17.1M
  {{E4, E,i,i,E, E,i,i,E, E4}}
150
5.70M
#define OP_MATH2(f)\
151
17.1M
  {{E4, E4, E,E,PtCr_int2_to_float,PtCr_2nd_int_to_float,\
152
17.1M
    E,E,PtCr_int_to_float,f}}
153
5.70M
#define OP_INT2(i)\
154
22.8M
  {{E4, E4, E,E,i,E, E4}}
155
5.70M
#define OP_REL2(i, f)\
156
11.4M
  {{E4, E,i,E,E, E,E,i,PtCr_2nd_int_to_float, E,E,PtCr_int_to_float,f}}
157
5.70M
#define OP_ANY2(op)\
158
5.70M
  {{E4, E,op,op,op, E,op,op,op, E,op,op,op}}
159
160
    /* Arithmetic operators */
161
162
5.70M
        OP_NUM1(PtCr_abs_int, PtCr_abs), /* abs */
163
5.70M
        OP_NUM2(PtCr_add_int, PtCr_add), /* add */
164
5.70M
        OP_INT_BOOL2(PtCr_and),  /* and */
165
5.70M
        OP_MATH2(PtCr_atan),  /* atan */
166
5.70M
        OP_INT2(PtCr_bitshift),  /* bitshift */
167
5.70M
        OP_NUM1(N, PtCr_ceiling),  /* ceiling */
168
5.70M
        OP_MATH1(PtCr_cos), /* cos */
169
5.70M
        OP_NUM1(N, PtCr_cvi),  /* cvi */
170
5.70M
        OP_NUM1(PtCr_int_to_float, N), /* cvr */
171
5.70M
        OP_MATH2(PtCr_div), /* div */
172
5.70M
        OP_MATH2(PtCr_exp), /* exp */
173
5.70M
        OP_NUM1(N, PtCr_floor),  /* floor */
174
5.70M
        OP_INT2(PtCr_idiv),  /* idiv */
175
5.70M
        OP_MATH1(PtCr_ln),  /* ln */
176
5.70M
        OP_MATH1(PtCr_log), /* log */
177
5.70M
        OP_INT2(PtCr_mod), /* mod */
178
5.70M
        OP_NUM2(PtCr_mul_int, PtCr_mul), /* mul */
179
5.70M
        OP_NUM1(PtCr_neg_int, PtCr_neg), /* neg */
180
5.70M
        OP1(PtCr_not, PtCr_not, E),  /* not */
181
5.70M
        OP_INT_BOOL2(PtCr_or),  /* or */
182
5.70M
        OP_NUM1(N, PtCr_round),  /* round */
183
5.70M
        OP_MATH1(PtCr_sin), /* sin */
184
5.70M
        OP_MATH1(PtCr_sqrt),  /* sqrt */
185
5.70M
        OP_NUM2(PtCr_sub_int, PtCr_sub), /* sub */
186
5.70M
        OP_NUM1(N, PtCr_truncate), /* truncate */
187
5.70M
        OP_INT_BOOL2(PtCr_xor),  /* xor */
188
189
    /* Comparison operators */
190
191
5.70M
        OP_REL2(PtCr_eq_int, PtCr_eq), /* eq */
192
5.70M
        OP_NUM2(PtCr_ge_int, PtCr_ge), /* ge */
193
5.70M
        OP_NUM2(PtCr_gt_int, PtCr_gt), /* gt */
194
5.70M
        OP_NUM2(PtCr_le_int, PtCr_le), /* le */
195
5.70M
        OP_NUM2(PtCr_lt_int, PtCr_lt), /* lt */
196
5.70M
        OP_REL2(PtCr_ne_int, PtCr_ne), /* ne */
197
198
    /* Stack operators */
199
200
5.70M
        OP1(E, PtCr_copy, E),  /* copy */
201
5.70M
        OP_ANY1(PtCr_dup), /* dup */
202
5.70M
        OP_ANY2(PtCr_exch),  /* exch */
203
5.70M
        OP1(E, PtCr_index, E), /* index */
204
5.70M
        OP_ANY1(PtCr_pop), /* pop */
205
5.70M
        OP_INT2(PtCr_roll),  /* roll */
206
207
    /* Constants */
208
209
5.70M
        OP_NONE(PtCr_byte),    /* byte */
210
5.70M
        OP_NONE(PtCr_int),   /* int */
211
5.70M
        OP_NONE(PtCr_float),   /* float */
212
5.70M
        OP_NONE(PtCr_true),    /* true */
213
5.70M
        OP_NONE(PtCr_false),   /* false */
214
215
    /* Special */
216
217
5.70M
        OP1(PtCr_if, E, E),    /* if */
218
5.70M
        OP_NONE(PtCr_else),    /* else */
219
5.70M
        OP_NONE(PtCr_return),    /* return */
220
5.70M
        OP1(E, PtCr_repeat, E),    /* repeat */
221
5.70M
        OP_NONE(PtCr_repeat_end) /* repeat_end */
222
5.70M
    };
223
224
5.70M
    memset(repeat_count, 0x00, MAX_PSC_FUNCTION_NESTING * sizeof(int));
225
5.70M
    memset(repeat_proc_size, 0x00, MAX_PSC_FUNCTION_NESTING * sizeof(int));
226
227
5.70M
    vstack[-1].type = CVT_NONE;  /* for type dispatch in empty stack case */
228
5.70M
    vstack[0].type = CVT_NONE;  /* catch underflow */
229
24.6M
    for (i = 0; i < pfn->params.m; ++i)
230
18.9M
        store_float(&vstack[i + 1], in[i]);
231
232
128M
    for (; ; ) {
233
128M
        int code, n;
234
235
128M
        switch (op_defn_table[*p++].opcode[(vsp[-1].type << 2) + vsp->type]) {
236
237
            /* Miscellaneous */
238
239
122k
        case PtCr_no_op:
240
122k
            continue;
241
30
        case PtCr_typecheck:
242
30
            return_error(gs_error_typecheck);
243
244
            /* Coerce and re-dispatch */
245
246
13.1M
        case PtCr_int_to_float:
247
13.1M
            store_float(vsp, (double)vsp->value.i);
248
13.1M
            --p; continue;
249
127k
        case PtCr_int2_to_float:
250
127k
            store_float(vsp, (double)vsp->value.i);
251
            /* fall through */
252
3.40M
        case PtCr_2nd_int_to_float:
253
3.40M
            store_float(vsp - 1, (double)vsp[-1].value.i);
254
3.40M
            --p; continue;
255
256
            /* Arithmetic operators */
257
258
3.58M
        case PtCr_abs:
259
3.58M
            vsp->value.f = fabs(vsp->value.f);
260
3.58M
            continue;
261
0
        case PtCr_add_int: {
262
0
            int int1 = vsp[-1].value.i, int2 = vsp->value.i;
263
264
            /* Test the signs first, if they are different then integer addition can't overflow */
265
0
            if ((int1 ^ int2) < 0) {
266
0
                vsp[-1].value.i = int1 - int2;
267
0
            } else {
268
0
                unsigned int u1, u2;
269
270
0
                if (int1 < 0) {
271
                    /* minimum signed int converted to unsigned, doubled, can overflow */
272
0
                    u1 = (unsigned int)(~int1) + 1;
273
0
                    u2 = u1 + (unsigned int)(~int2) + 1;
274
275
0
                    if (u2 < u1 || u2 > (unsigned int)max_int + 1)
276
0
                        store_float(vsp - 1, (double)int1 - int2);
277
0
                    else
278
0
                        vsp[-1].value.i = int1 - int2;
279
0
                } else {
280
                    /* maximum signed int + maximum signed int fits into an unsigned int, so no overflow */
281
0
                    u1 = int1 + int2;
282
283
0
                    if (u1 > (unsigned int)max_int)
284
0
                        store_float(vsp - 1, (double)int1 - int2);
285
0
                    else
286
0
                        vsp[-1].value.i = int1 - int2;
287
0
                }
288
0
            }
289
0
            --vsp; continue;
290
127k
        }
291
1.10M
        case PtCr_add:
292
1.10M
            vsp[-1].value.f += vsp->value.f;
293
1.10M
            --vsp; continue;
294
0
        case PtCr_and:
295
0
            vsp[-1].value.i &= vsp->value.i;
296
0
            --vsp; continue;
297
2.36M
        case PtCr_atan: {
298
2.36M
            double result;
299
300
2.36M
            code = gs_atan2_degrees(vsp[-1].value.f, vsp->value.f,
301
2.36M
                                    &result);
302
2.36M
            if (code < 0)
303
0
                return code;
304
2.36M
            vsp[-1].value.f = result;
305
2.36M
            --vsp; continue;
306
2.36M
        }
307
0
        case PtCr_bitshift:
308
0
#define MAX_SHIFT (ARCH_SIZEOF_INT * 8 - 1)
309
0
            if (vsp->value.i < -MAX_SHIFT || vsp->value.i > MAX_SHIFT)
310
0
                vsp[-1].value.i = 0;
311
0
#undef MAX_SHIFT
312
0
            else if ((n = vsp->value.i) < 0)
313
0
                vsp[-1].value.i = ((uint)(vsp[-1].value.i)) >> -n;
314
0
            else
315
0
                vsp[-1].value.i <<= n;
316
0
            --vsp; continue;
317
0
        case PtCr_ceiling:
318
0
            vsp->value.f = ceil(vsp->value.f);
319
0
            continue;
320
2.36M
        case PtCr_cos:
321
2.36M
            vsp->value.f = gs_cos_degrees(vsp->value.f);
322
2.36M
            continue;
323
0
        case PtCr_cvi:
324
0
        {
325
           /* Strictly speaking assigning one element of union
326
            * to another, overlapping element of a different size is
327
            * undefined behavior, hence assign to an intermediate variable
328
            */
329
0
            int int1 = (int)(vsp->value.f);
330
0
            vsp->value.i = int1;
331
0
            vsp->type = CVT_INT;
332
0
            continue;
333
2.36M
        }
334
0
        case PtCr_cvr:
335
0
            continue;  /* prepare handled it */
336
3.07M
        case PtCr_div:
337
3.07M
            if (vsp->value.f == 0)
338
0
                return_error(gs_error_undefinedresult);
339
3.07M
            vsp[-1].value.f /= vsp->value.f;
340
3.07M
            --vsp; continue;
341
0
        case PtCr_exp:
342
0
            vsp[-1].value.f = pow(vsp[-1].value.f, vsp->value.f);
343
0
            --vsp; continue;
344
0
        case PtCr_floor:
345
0
            vsp->value.f = floor(vsp->value.f);
346
0
            continue;
347
0
        case PtCr_idiv:
348
0
            if (vsp->value.i == 0)
349
0
                return_error(gs_error_undefinedresult);
350
0
            if (vsp[-1].value.i == min_int &&
351
0
                vsp->value.i == -1)  /* anomalous boundary case, fail */
352
0
                return_error(gs_error_rangecheck);
353
0
            else
354
0
                vsp[-1].value.i /= vsp->value.i;
355
0
            --vsp; continue;
356
0
        case PtCr_ln:
357
0
            vsp->value.f = log(vsp->value.f);
358
0
            continue;
359
0
        case PtCr_log:
360
0
            vsp->value.f = log10(vsp->value.f);
361
0
            continue;
362
0
        case PtCr_mod:
363
0
            if (vsp->value.i == 0)
364
0
                return_error(gs_error_undefinedresult);
365
0
            if (vsp[-1].value.i == min_int &&
366
0
                vsp->value.i == -1)  /* anomalous boundary case, fail */
367
0
                return_error(gs_error_rangecheck);
368
0
            vsp[-1].value.i %= vsp->value.i;
369
0
            --vsp; continue;
370
2.69M
        case PtCr_mul_int: {
371
            /* We don't bother to optimize this. */
372
2.69M
            double prod = (double)vsp[-1].value.i * vsp->value.i;
373
374
2.69M
            if (prod < min_int || prod > max_int)
375
0
                store_float(vsp - 1, prod);
376
2.69M
            else
377
2.69M
                vsp[-1].value.i = (int)prod;
378
2.69M
            --vsp; continue;
379
0
        }
380
3.98M
        case PtCr_mul:
381
3.98M
            vsp[-1].value.f *= vsp->value.f;
382
3.98M
            --vsp; continue;
383
127k
        case PtCr_abs_int:
384
127k
            if (vsp->value.i >= 0)
385
127k
                continue;
386
            /* fallthrough */
387
0
        case PtCr_neg_int:
388
0
            if (vsp->value.i == min_int)
389
0
                store_float(vsp, (double)vsp->value.i); /* =self negated */
390
0
            else
391
0
                vsp->value.i = -vsp->value.i;
392
0
            continue;
393
0
        case PtCr_neg:
394
0
            vsp->value.f = -vsp->value.f;
395
0
            continue;
396
0
        case PtCr_not_bool:
397
0
            vsp->value.i = !vsp->value.i;
398
0
            continue;
399
0
        case PtCr_not:
400
0
            vsp->value.i = ~vsp->value.i;
401
0
            continue;
402
0
        case PtCr_or:
403
0
            vsp[-1].value.i |= vsp->value.i;
404
0
            --vsp; continue;
405
0
        case PtCr_round:
406
0
            vsp->value.f = floor(vsp->value.f + 0.5);
407
0
            continue;
408
3.07M
        case PtCr_sin:
409
3.07M
            vsp->value.f = gs_sin_degrees(vsp->value.f);
410
3.07M
            continue;
411
3.07M
        case PtCr_sqrt:
412
3.07M
            if (vsp->value.f < 0.0)
413
0
                return_error(gs_error_rangecheck);
414
3.07M
            vsp->value.f = sqrt(vsp->value.f);
415
3.07M
            continue;
416
0
        case PtCr_sub_int: {
417
0
            int int1 = vsp[-1].value.i, int2 = vsp->value.i;
418
419
            /* Test the signs first, if they are the same then integer subtraction can't overflow */
420
0
            if ((int1 ^ int2) >= 0) {
421
0
                vsp[-1].value.i = int1 - int2;
422
0
            } else {
423
0
                unsigned int u1, u2;
424
425
0
                if (int1 < 0) {
426
                    /* -ve minus +ve, convert -ve to +ve and add as unsigned, defined behaviour if overflow (wraps round) */
427
0
                    u1 = (unsigned int)(~int1) + 1;
428
0
                    u2 = u1 + int2;
429
                    /* If adding 2 positive values produced a smaller value, then the addition overflowed.
430
                     * If the result is larger than the max signed int (+1 for -ve) then subtraction will overflow
431
                     */
432
0
                    if (u2 < u1 || u2 > ((unsigned int)max_int + 1))
433
0
                        store_float(vsp - 1, (double)int1 - int2);
434
0
                    else
435
0
                        vsp[-1].value.i = int1 - int2;
436
0
                } else {
437
                    /* +ve minus -ve = +ve plus +ve, can overflow a *signed* int, but not an unsigned int */
438
0
                    u1 = int1 + ((unsigned int)(~int2) + 1);
439
0
                    if (u1 > (unsigned int)max_int)
440
0
                        store_float(vsp - 1, (double)int1 - int2);
441
0
                    else
442
0
                        vsp[-1].value.i = int1 - int2;
443
0
                }
444
0
            }
445
0
            --vsp; continue;
446
3.07M
        }
447
9.93M
        case PtCr_sub:
448
9.93M
            vsp[-1].value.f -= vsp->value.f;
449
9.93M
            --vsp; continue;
450
5
        case PtCr_truncate:
451
5
            vsp->value.f = (vsp->value.f < 0 ? ceil(vsp->value.f) :
452
5
                            floor(vsp->value.f));
453
5
            continue;
454
0
        case PtCr_xor:
455
0
            vsp[-1].value.i ^= vsp->value.i;
456
0
            --vsp; continue;
457
458
            /* Boolean operators */
459
460
0
#define DO_REL(rel, m)\
461
3.32M
  vsp[-1].value.i = vsp[-1].value.m rel vsp->value.m
462
463
0
        case PtCr_eq_int:
464
0
            DO_REL(==, i);
465
0
            goto rel;
466
18
        case PtCr_ge_int:
467
18
            DO_REL(>=, i);
468
18
            goto rel;
469
44
        case PtCr_ge:
470
44
            DO_REL(>=, f);
471
44
            goto rel;
472
282
        case PtCr_gt_int:
473
282
            DO_REL(>, i);
474
282
            goto rel;
475
94
        case PtCr_gt:
476
94
            DO_REL(>, f);
477
94
            goto rel;
478
0
        case PtCr_le_int:
479
0
            DO_REL(<=, i);
480
0
            goto rel;
481
256k
        case PtCr_le:
482
256k
            DO_REL(<=, f);
483
256k
            goto rel;
484
0
        case PtCr_lt_int:
485
0
            DO_REL(<, i);
486
0
            goto rel;
487
3.07M
        case PtCr_lt:
488
3.07M
            DO_REL(<, f);
489
3.07M
            goto rel;
490
0
        case PtCr_ne_int:
491
0
            DO_REL(!=, i);
492
0
            goto rel;
493
0
        case PtCr_ne:
494
0
            DO_REL(!=, f);
495
0
            goto rel;
496
0
        case PtCr_eq:
497
0
            DO_REL(==, f);
498
3.32M
        rel:
499
3.32M
            vsp[-1].type = CVT_BOOL;
500
3.32M
            --vsp; continue;
501
502
0
#undef DO_REL
503
504
            /* Stack operators */
505
506
256k
        case PtCr_copy:
507
256k
            i = vsp->value.i;
508
256k
            n = vsp - vstack;
509
256k
            if (i < 0 || i >= n)
510
0
                return_error(gs_error_rangecheck);
511
256k
            if (i > MAX_VSTACK - (n - 1))
512
0
                return_error(gs_error_limitcheck);
513
256k
            memcpy(vsp, vsp - i, i * sizeof(*vsp));
514
256k
            vsp += i - 1;
515
256k
            continue;
516
12.1M
        case PtCr_dup:
517
12.1M
            vsp[1] = *vsp;
518
12.1M
            goto push;
519
12.4M
        case PtCr_exch:
520
12.4M
            vstack[MAX_VSTACK] = *vsp;
521
12.4M
            *vsp = vsp[-1];
522
12.4M
            vsp[-1] = vstack[MAX_VSTACK];
523
12.4M
            continue;
524
77.5k
        case PtCr_index:
525
77.5k
            i = vsp->value.i;
526
77.5k
            if (i < 0 || i >= vsp - vstack - 1)
527
0
                return_error(gs_error_rangecheck);
528
77.5k
            *vsp = vsp[-i - 1];
529
77.5k
            continue;
530
8.09M
        case PtCr_pop:
531
8.09M
            --vsp;
532
8.09M
            continue;
533
2.48M
        case PtCr_roll:
534
2.48M
            n = vsp[-1].value.i;
535
2.48M
            i = vsp->value.i;
536
2.48M
            if (n < 0 || n > vsp - vstack - 2)
537
0
                return_error(gs_error_rangecheck);
538
            /* We don't bother to do this efficiently. */
539
9.45M
            for (; i > 0; i--) {
540
6.96M
                memmove(vsp - n, vsp - (n + 1), n * sizeof(*vsp));
541
6.96M
                vsp[-(n + 1)] = vsp[-1];
542
6.96M
            }
543
2.56M
            for (; i < 0; i++) {
544
78.3k
                vsp[-1] = vsp[-(n + 1)];
545
78.3k
                memmove(vsp - (n + 1), vsp - n, n * sizeof(*vsp));
546
78.3k
            }
547
2.48M
            vsp -= 2;
548
2.48M
            continue;
549
550
            /* Constants */
551
552
19.2M
        case PtCr_byte:
553
19.2M
            vsp[1].value.i = *p++, vsp[1].type = CVT_INT;
554
19.2M
            goto push;
555
5.51M
        case PtCr_int /* native */:
556
5.51M
            memcpy(&vsp[1].value.i, p, sizeof(int));
557
5.51M
            vsp[1].type = CVT_INT;
558
5.51M
            p += sizeof(int);
559
5.51M
            goto push;
560
3.51M
        case PtCr_float /* native */:
561
3.51M
            memcpy(&vsp[1].value.f, p, sizeof(float));
562
3.51M
            vsp[1].type = CVT_FLOAT;
563
3.51M
            p += sizeof(float);
564
3.51M
            goto push;
565
0
        case PtCr_true:
566
0
            vsp[1].value.i = true, vsp[1].type = CVT_BOOL;
567
0
            goto push;
568
0
        case PtCr_false:
569
0
            vsp[1].value.i = false, vsp[1].type = CVT_BOOL;
570
40.3M
        push:
571
40.3M
            if (vsp == &vstack[MAX_VSTACK])
572
0
                return_error(gs_error_limitcheck);
573
40.3M
            ++vsp;
574
40.3M
            continue;
575
576
            /* Special */
577
578
3.32M
        case PtCr_if:
579
3.32M
            if ((vsp--)->value.i) { /* value is true, execute body */
580
2.82M
                p += 2;
581
2.82M
                continue;
582
2.82M
            }
583
            /* falls through */
584
629k
        case PtCr_else:
585
629k
            p += 2 + (p[0] << 8) + p[1];  /* skip the past body */
586
629k
            continue;
587
5.70M
        case PtCr_return:
588
5.70M
            goto fin;
589
0
        case PtCr_repeat:
590
0
            repeat_nesting_level++;
591
0
            repeat_count[repeat_nesting_level] = vsp->value.i;
592
0
            repeat_proc_size[repeat_nesting_level] = 1 + (p[0] << 8) + p[1];  /* body size */
593
0
            --vsp;    /* pop the counter */
594
0
            p += 3 + (p[0] <<8) + p[1];       /* advance just past the repeat_end */
595
            /* falls through */
596
0
        case PtCr_repeat_end:
597
0
            if (repeat_nesting_level < 0)
598
0
                return_error(gs_error_rangecheck);
599
600
0
            if ((repeat_count[repeat_nesting_level])-- <= 0)
601
0
                repeat_nesting_level--;
602
0
            else
603
0
                p -= repeat_proc_size[repeat_nesting_level];
604
0
            continue;
605
128M
        }
606
128M
    }
607
5.70M
 fin:
608
5.70M
    {   /* Following Acrobat, take the desired number of parameters */
609
        /* from the top of stack and ignore the rest. Bug 702950. */
610
5.70M
        int extra_ops = vsp - vstack - pfn->params.n;
611
5.70M
        if (extra_ops < 0)
612
58
            return_error(gs_error_rangecheck);
613
22.1M
        for (i = 0; i < pfn->params.n; ++i) {
614
16.4M
            switch (vstack[i + 1 + extra_ops].type) {
615
14.0k
            case CVT_INT:
616
14.0k
                out[i] = (float)vstack[i + 1 + extra_ops].value.i;
617
14.0k
                break;
618
16.4M
            case CVT_FLOAT:
619
16.4M
                out[i] = vstack[i + 1 + extra_ops].value.f;
620
16.4M
                break;
621
0
            default:
622
0
                return_error(gs_error_typecheck);
623
16.4M
            }
624
16.4M
        }
625
5.70M
    }
626
5.70M
    return 0;
627
5.70M
}
628
629
/* Test whether a PostScript Calculator function is monotonic. */
630
static int
631
fn_PtCr_is_monotonic(const gs_function_t * pfn_common,
632
                     const float *lower, const float *upper, uint *mask)
633
0
{
634
    /*
635
     * No reasonable way to tell.  Eventually we should check for
636
     * functions consisting of only stack-manipulating operations,
637
     * since these may be common for DeviceN color spaces and *are*
638
     * monotonic.
639
     */
640
0
    *mask = 0x49249249;
641
0
    return 0;
642
0
}
643
644
/* Write the function definition in symbolic form on a stream. */
645
static int
646
calc_put_ops(stream *s, const byte *ops, uint size)
647
1.53k
{
648
1.53k
    const byte *p;
649
650
1.53k
    spputc(s, '{');
651
13.7k
    for (p = ops; p < ops + size; )
652
12.1k
        switch (*p++) {
653
3.47k
        case PtCr_byte:
654
3.47k
            pprintd1(s, "%d ", *p++);
655
3.47k
            break;
656
676
        case PtCr_int: {
657
676
            int i;
658
659
676
            memcpy(&i, p, sizeof(int));
660
676
            pprintd1(s, "%d ", i);
661
676
            p += sizeof(int);
662
676
            break;
663
0
        }
664
3.11k
        case PtCr_float: {
665
3.11k
            float f;
666
667
3.11k
            memcpy(&f, p, sizeof(float));
668
3.11k
            pprintg1(s, "%g ", f);
669
3.11k
            p += sizeof(float);
670
3.11k
            break;
671
0
        }
672
0
        case PtCr_true:
673
0
            stream_puts(s, "true ");
674
0
            break;
675
0
        case PtCr_false:
676
0
            stream_puts(s, "false ");
677
0
            break;
678
148
        case PtCr_if: {
679
148
            int skip = (p[0] << 8) + p[1];
680
148
            int code;
681
682
148
            code = calc_put_ops(s, p += 2, skip);
683
148
            p += skip;
684
148
            if (code < 0)
685
0
                return code;
686
148
            if (code > 0) { /* else */
687
0
                skip = (p[-2] << 8) + p[-1];
688
0
                code = calc_put_ops(s, p, skip);
689
0
                p += skip;
690
0
                if (code < 0)
691
0
                    return code;
692
0
                stream_puts(s, " ifelse ");
693
0
            } else
694
148
                stream_puts(s, " if ");
695
148
            break;
696
148
        }
697
148
        case PtCr_else:
698
0
            if (p != ops + size - 2)
699
0
                return_error(gs_error_rangecheck);
700
0
            spputc(s, '}');
701
0
            return 1;
702
        /*case PtCr_return:*/ /* not possible */
703
0
        case PtCr_repeat:   /* We shouldn't encounter this, but just in case */
704
0
        case PtCr_repeat_end:
705
0
            return_error(gs_error_rangecheck);
706
4.76k
        default: {    /* must be < PtCr_NUM_OPS */
707
4.76k
                static const char *const op_names[] = {
708
                    /* Keep this consistent with opcodes in gsfunc4.h! */
709
4.76k
                    "abs", "add", "and", "atan", "bitshift",
710
4.76k
                    "ceiling", "cos", "cvi", "cvr", "div", "exp",
711
4.76k
                    "floor", "idiv", "ln", "log", "mod", "mul",
712
4.76k
                    "neg", "not", "or", "round", "sin", "sqrt", "sub",
713
4.76k
                    "truncate", "xor",
714
4.76k
                    "eq", "ge", "gt", "le", "lt", "ne",
715
4.76k
                    "copy", "dup", "exch", "index", "pop", "roll"
716
4.76k
                };
717
718
4.76k
                pprints1(s, "%s ", op_names[p[-1]]);
719
4.76k
            }
720
12.1k
        }
721
1.53k
    spputc(s, '}');
722
1.53k
    return 0;
723
1.53k
}
724
static int
725
calc_put(stream *s, const gs_function_PtCr_t *pfn)
726
1.38k
{
727
1.38k
    calc_put_ops(s, pfn->params.ops.data, pfn->params.ops.size - 1);
728
1.38k
    return 0;
729
1.38k
}
730
731
/* Access the symbolic definition as a DataSource. */
732
static int
733
calc_access(const gs_data_source_t *psrc, ulong start, uint length,
734
            byte *buf, const byte **ptr)
735
718
{
736
718
    const gs_function_PtCr_t *const pfn =
737
718
        (const gs_function_PtCr_t *)
738
718
          ((const char *)psrc - offset_of(gs_function_PtCr_t, data_source));
739
    /*
740
     * The caller wants a specific substring of the symbolic definition.
741
     * Generate the entire definition, using a SubFileDecode filter (in an
742
     * output pipeline!) to extract the substring.  This is very
743
     * inefficient, but this code is rarely used, and almost never actually
744
     * has to break up the definition into pieces to fit in the caller's
745
     * buffer.
746
     */
747
718
    stream_SFD_state st;
748
718
    stream ds, bs;
749
718
    byte dbuf[200];   /* arbitrary */
750
718
    const stream_template *const templat = &s_SFD_template;
751
752
    /* Set up the stream that writes into the buffer. */
753
718
    s_init(&bs, NULL);
754
718
    swrite_string(&bs, buf, length);
755
    /* Set up the SubFileDecode stream. */
756
718
    s_init(&ds, NULL);
757
718
    s_init_state((stream_state *)&st, templat, NULL);
758
718
    templat->set_defaults((stream_state *)&st);
759
718
    st.skip_count = start;
760
718
    s_init_filter(&ds, (stream_state *)&st, dbuf, sizeof(dbuf), &bs);
761
718
    calc_put(&ds, pfn);
762
718
    sclose(&ds);
763
718
    if (ptr)
764
718
        *ptr = buf;
765
718
    return 0;
766
718
}
767
768
/* Return PostScript Calculator function information. */
769
static void
770
fn_PtCr_get_info(const gs_function_t *pfn_common, gs_function_info_t *pfi)
771
669
{
772
669
    const gs_function_PtCr_t *const pfn =
773
669
        (const gs_function_PtCr_t *)pfn_common;
774
775
669
    gs_function_get_info_default(pfn_common, pfi);
776
669
    pfi->DataSource = &pfn->data_source;
777
669
    {
778
669
        stream s;
779
780
669
        s_init(&s, NULL);
781
669
        swrite_position_only(&s);
782
669
        calc_put(&s, pfn);
783
669
        pfi->data_size = stell(&s);
784
669
    }
785
669
}
786
787
/* Make a scaled copy of a PostScript Calculator function. */
788
static int
789
fn_PtCr_make_scaled(const gs_function_PtCr_t *pfn, gs_function_PtCr_t **ppsfn,
790
                    const gs_range_t *pranges, gs_memory_t *mem)
791
0
{
792
0
    gs_function_PtCr_t *psfn =
793
0
        gs_alloc_struct(mem, gs_function_PtCr_t, &st_function_PtCr,
794
0
                        "fn_PtCr_make_scaled");
795
    /* We are adding {<int> 1 roll <float> mul <float> add} for each output. */
796
0
    int n = pfn->params.n;
797
0
    uint opsize = pfn->params.ops.size + (9 + 2 * sizeof(float)) * n;
798
0
    byte *ops = gs_alloc_string(mem, opsize, "fn_PtCr_make_scaled(ops)");
799
0
    byte *p;
800
0
    int code, i;
801
802
0
    if (psfn == 0 || ops == 0) {
803
0
        gs_free_string(mem, ops, opsize, "fn_PtCr_make_scaled(ops)");
804
0
        gs_free_object(mem, psfn, "fn_PtCr_make_scaled");
805
0
        return_error(gs_error_VMerror);
806
0
    }
807
0
    psfn->params = pfn->params;
808
0
    psfn->params.ops.data = ops;
809
0
    psfn->params.ops.size = opsize;
810
0
    psfn->data_source = pfn->data_source;
811
0
    code = fn_common_scale((gs_function_t *)psfn, (const gs_function_t *)pfn,
812
0
                           pranges, mem);
813
0
    if (code < 0) {
814
0
        gs_function_free((gs_function_t *)psfn, true, mem);
815
0
        return code;
816
0
    }
817
0
    memcpy(ops, pfn->params.ops.data, pfn->params.ops.size - 1); /* minus return */
818
0
    p = ops + pfn->params.ops.size - 1;
819
0
    for (i = n; --i >= 0; ) {
820
0
        float base = pranges[i].rmin;
821
0
        float factor = pranges[i].rmax - base;
822
823
0
        if (factor != 1) {
824
0
            p[0] = PtCr_float; memcpy(p + 1, &factor, sizeof(float));
825
0
            p += 1 + sizeof(float);
826
0
            *p++ = PtCr_mul;
827
0
        }
828
0
        if (base != 0) {
829
0
            p[0] = PtCr_float; memcpy(p + 1, &base, sizeof(float));
830
0
            p += 1 + sizeof(float);
831
0
            *p++ = PtCr_add;
832
0
        }
833
0
        if (n != 1) {
834
0
            p[0] = PtCr_byte; p[1] = (byte)n;
835
0
            p[2] = PtCr_byte; p[3] = 1;
836
0
            p[4] = PtCr_roll;
837
0
            p += 5;
838
0
        }
839
0
    }
840
0
    *p++ = PtCr_return;
841
0
    psfn->params.ops.size = p - ops;
842
0
    psfn->params.ops.data =
843
0
        gs_resize_string(mem, ops, opsize, psfn->params.ops.size,
844
0
                         "fn_PtCr_make_scaled");
845
0
    *ppsfn = psfn;
846
0
    return 0;
847
0
}
848
849
/* Free the parameters of a PostScript Calculator function. */
850
void
851
gs_function_PtCr_free_params(gs_function_PtCr_params_t * params, gs_memory_t * mem)
852
41.2k
{
853
41.2k
    gs_free_const_string(mem, params->ops.data, params->ops.size, "ops");
854
41.2k
    params->ops.data = NULL;
855
41.2k
    params->ops.size = 0;
856
41.2k
    fn_common_free_params((gs_function_params_t *) params, mem);
857
41.2k
}
858
859
/* Serialize. */
860
static int
861
gs_function_PtCr_serialize(const gs_function_t * pfn, stream *s)
862
12.8k
{
863
12.8k
    uint n;
864
12.8k
    const gs_function_PtCr_params_t * p = (const gs_function_PtCr_params_t *)&pfn->params;
865
12.8k
    int code = fn_common_serialize(pfn, s);
866
867
12.8k
    if (code < 0)
868
0
        return code;
869
12.8k
    code = sputs(s, (const byte *)&p->ops.size, sizeof(p->ops.size), &n);
870
12.8k
    if (code < 0)
871
0
        return code;
872
12.8k
    return sputs(s, p->ops.data, p->ops.size, &n);
873
12.8k
}
874
875
/* Allocate and initialize a PostScript Calculator function. */
876
int
877
gs_function_PtCr_init(gs_function_t ** ppfn,
878
                  const gs_function_PtCr_params_t * params, gs_memory_t * mem)
879
46.8k
{
880
46.8k
    static const gs_function_head_t function_PtCr_head = {
881
46.8k
        function_type_PostScript_Calculator,
882
46.8k
        {
883
46.8k
            (fn_evaluate_proc_t) fn_PtCr_evaluate,
884
46.8k
            (fn_is_monotonic_proc_t) fn_PtCr_is_monotonic,
885
46.8k
            (fn_get_info_proc_t) fn_PtCr_get_info,
886
46.8k
            fn_common_get_params,
887
46.8k
            (fn_make_scaled_proc_t) fn_PtCr_make_scaled,
888
46.8k
            (fn_free_params_proc_t) gs_function_PtCr_free_params,
889
46.8k
            fn_common_free,
890
46.8k
            (fn_serialize_proc_t) gs_function_PtCr_serialize,
891
46.8k
        }
892
46.8k
    };
893
46.8k
    int code;
894
895
46.8k
    *ppfn = 0;      /* in case of error */
896
46.8k
    code = fn_check_mnDR((const gs_function_params_t *)params,
897
46.8k
                         params->m, params->n);
898
46.8k
    if (code < 0)
899
29
        return code;
900
46.8k
    if (params->m > MAX_VSTACK || params->n > MAX_VSTACK)
901
0
        return_error(gs_error_limitcheck);
902
    /*
903
     * Pre-validate the operation string to reduce evaluation overhead.
904
     */
905
46.8k
    {
906
46.8k
        const byte *p = params->ops.data;
907
908
1.49M
        for (; *p != PtCr_return; ++p)
909
1.44M
            switch ((gs_PtCr_opcode_t)*p) {
910
316k
            case PtCr_byte:
911
316k
                ++p; break;
912
31.1k
            case PtCr_int:
913
31.1k
                p += sizeof(int); break;
914
76.4k
            case PtCr_float:
915
76.4k
                p += sizeof(float); break;
916
0
            case PtCr_repeat:
917
32.6k
            case PtCr_if:
918
64.7k
            case PtCr_else:
919
64.7k
                p += 2;
920
64.7k
            case PtCr_repeat_end:
921
64.7k
            case PtCr_true:
922
64.7k
            case PtCr_false:
923
64.7k
                break;
924
956k
            default:
925
956k
                if (*p >= PtCr_NUM_OPS)
926
0
                    return_error(gs_error_rangecheck);
927
1.44M
            }
928
46.8k
        if (p != params->ops.data + params->ops.size - 1)
929
0
            return_error(gs_error_rangecheck);
930
46.8k
    }
931
46.8k
    {
932
46.8k
        gs_function_PtCr_t *pfn =
933
46.8k
            gs_alloc_struct(mem, gs_function_PtCr_t, &st_function_PtCr,
934
46.8k
                            "gs_function_PtCr_init");
935
936
46.8k
        if (pfn == 0)
937
0
            return_error(gs_error_VMerror);
938
46.8k
        pfn->params = *params;
939
        /*
940
         * We claim to have a DataSource, in order to write the function
941
         * definition in symbolic form for embedding in PDF files.
942
         * ****** THIS IS A HACK. ******
943
         */
944
46.8k
        data_source_init_string2(&pfn->data_source, NULL, 0);
945
46.8k
        pfn->data_source.access = calc_access;
946
46.8k
        pfn->head = function_PtCr_head;
947
46.8k
        *ppfn = (gs_function_t *) pfn;
948
46.8k
    }
949
0
    return 0;
950
46.8k
}