Coverage Report

Created: 2025-06-10 07:15

/src/ghostpdl/psi/iscannum.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2024 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
/* Number scanner for Ghostscript interpreter */
18
#include "math_.h"
19
#include "ghost.h"
20
#include "ierrors.h"
21
#include "scommon.h"
22
#include "iscan.h"
23
#include "iscannum.h"   /* defines interface */
24
#include "scanchar.h"
25
#include "store.h"
26
27
/*
28
 * Warning: this file has a "spaghetti" control structure.  But since this
29
 * code accounts for over 10% of the execution time of some PostScript
30
 * files, this is one of the few places we feel this is justified.
31
 */
32
33
/*
34
 * Scan a number.  If the number consumes the entire string, return 0;
35
 * if not, set *psp to the first character beyond the number and return 1.
36
 */
37
int
38
scan_number(const byte * str, const byte * end, int sign,
39
            ref * pref, const byte ** psp, int scanner_options)
40
346M
{
41
346M
    const byte *sp = str;
42
346M
#define GET_NEXT(cvar, sp, end_action)\
43
1.39G
  if (sp >= end) { end_action; } else cvar = *sp++
44
45
    /*
46
     * Powers of 10 up to 6 can be represented accurately as
47
     * a single-precision float.
48
     */
49
346M
#define NUM_POWERS_10 6
50
346M
    static const float powers_10[NUM_POWERS_10 + 1] = {
51
346M
        1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6
52
346M
    };
53
346M
    static const double neg_powers_10[NUM_POWERS_10 + 1] = {
54
346M
        1e0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6
55
346M
    };
56
57
346M
    ps_int ival;
58
346M
    double dval;
59
346M
    int exp10;
60
346M
    int code = 0;
61
346M
    int c, d;
62
346M
    ps_int max_ps_int_scan, min_ps_int_scan;
63
346M
    const byte *const decoder = scan_char_decoder;
64
346M
#define IS_DIGIT(d, c)\
65
927M
  ((d = decoder[c]) < 10)
66
346M
#define WOULD_OVERFLOW(val, d, maxv)\
67
346M
  (val >= maxv / 10 && (val > maxv / 10 || d > (int64_t)(maxv % 10)))
68
69
346M
    GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
70
346M
    if (!IS_DIGIT(d, c)) {
71
69.2M
        if (c != '.')
72
37.1k
            return_error(gs_error_syntaxerror);
73
        /* Might be a number starting with '.'. */
74
69.1M
        GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
75
69.1M
        if (!IS_DIGIT(d, c))
76
69.1M
            return_error(gs_error_syntaxerror);
77
3.35k
        ival = 0;
78
3.35k
        goto i2r;
79
69.1M
    }
80
    /* Accumulate an integer in ival. */
81
    /* Do up to 4 digits without a loop, */
82
    /* since we know this can't overflow and since */
83
    /* most numbers have 4 (integer) digits or fewer. */
84
277M
    ival = d;
85
277M
    if (end - sp >= 3) { /* just check once */
86
270M
        if (!IS_DIGIT(d, (c = *sp))) {
87
57.8M
            sp++;
88
57.8M
            goto ind;
89
57.8M
        }
90
212M
        ival = ival * 10 + d;
91
212M
        if (!IS_DIGIT(d, (c = sp[1]))) {
92
205M
            sp += 2;
93
205M
            goto ind;
94
205M
        }
95
7.06M
        ival = ival * 10 + d;
96
7.06M
        sp += 3;
97
7.06M
        if (!IS_DIGIT(d, (c = sp[-1])))
98
3.92M
            goto ind;
99
3.13M
        ival = ival * 10 + d;
100
3.13M
    }
101
102
9.60M
    max_ps_int_scan = scanner_options & SCAN_CPSI_MODE ? MAX_PS_INT32 : MAX_PS_INT;
103
9.60M
    min_ps_int_scan = scanner_options & SCAN_CPSI_MODE ? MIN_PS_INT32 : MIN_PS_INT;
104
105
14.6M
    for (;; ival = ival * 10 + d) {
106
14.6M
        GET_NEXT(c, sp, goto iret);
107
7.73M
        if (!IS_DIGIT(d, c))
108
2.73M
            break;
109
5.00M
        if (WOULD_OVERFLOW(((ps_uint)ival), d, max_ps_int_scan)) {
110
5.25k
            if (ival == max_ps_int_scan / 10 && d == (max_ps_int_scan % 10) + 1 && sign < 0) {
111
421
                GET_NEXT(c, sp, c = EOFC);
112
421
                dval = -(double)min_ps_int_scan;
113
421
                if (c == 'e' || c == 'E') {
114
3
                    exp10 = 0;
115
3
                    goto fs;
116
418
                } else if (c == '.') {
117
8
                    GET_NEXT(c, sp, c = EOFC);
118
8
                    exp10 = 0;
119
8
                    goto fd;
120
410
                } else if (!IS_DIGIT(d, c)) {
121
407
                    ival = min_ps_int_scan;
122
407
                    break;
123
407
                }
124
421
            } else
125
4.83k
                dval = (double)ival;
126
4.83k
            goto l2d;
127
5.25k
        }
128
5.00M
    }
129
270M
  ind:        /* We saw a non-digit while accumulating an integer in ival. */
130
270M
    switch (c) {
131
1.86M
        case '.':
132
1.86M
            GET_NEXT(c, sp, c = EOFC);
133
1.86M
            goto i2r;
134
67.1M
        default:
135
67.1M
            *psp = sp;
136
67.1M
            code = 1;
137
67.1M
            break;
138
10
        case EOFC:
139
10
            break;
140
1.76k
        case 'e':
141
2.62k
        case 'E':
142
2.62k
            if (sign < 0)
143
409
                ival = -ival;
144
2.62k
            dval = (double)ival;
145
2.62k
            exp10 = 0;
146
2.62k
            goto fe;
147
201M
        case '#':
148
201M
            {
149
201M
                const int radix = ival;
150
201M
                ps_int uval = 0, imax;
151
152
201M
                if (sign || radix < min_radix || radix > max_radix)
153
1.16k
                    return_error(gs_error_syntaxerror);
154
                /* Avoid multiplies for power-of-2 radix. */
155
201M
                if (!(radix & (radix - 1))) {
156
201M
                    int shift;
157
158
201M
                    switch (radix) {
159
515
                        case 2:
160
515
                            shift = 1, imax = MAX_PS_UINT >> 1;
161
515
                            break;
162
78
                        case 4:
163
78
                            shift = 2, imax = MAX_PS_UINT >> 2;
164
78
                            break;
165
20
                        case 8:
166
20
                            shift = 3, imax = MAX_PS_UINT >> 3;
167
20
                            break;
168
201M
                        case 16:
169
201M
                            shift = 4, imax = MAX_PS_UINT >> 4;
170
201M
                            break;
171
103
                        case 32:
172
103
                            shift = 5, imax = MAX_PS_UINT >> 5;
173
103
                            break;
174
0
                        default:  /* can't happen */
175
0
                            return_error(gs_error_rangecheck);
176
201M
                    }
177
956M
                    for (;; uval = (uval << shift) + d) {
178
956M
                        GET_NEXT(c, sp, break);
179
856M
                        d = decoder[c];
180
856M
                        if (d >= radix) {
181
100M
                            *psp = sp;
182
100M
                            code = 1;
183
100M
                            break;
184
100M
                        }
185
755M
                        if (uval > imax)
186
5
                            return_error(gs_error_limitcheck);
187
755M
                    }
188
201M
                } else {
189
928
                    ps_int irem = MAX_PS_UINT % radix;
190
191
928
                    imax = MAX_PS_UINT / radix;
192
1.84k
                    for (;; uval = uval * radix + d) {
193
1.84k
                        GET_NEXT(c, sp, break);
194
1.42k
                        d = decoder[c];
195
1.42k
                        if (d >= radix) {
196
498
                            *psp = sp;
197
498
                            code = 1;
198
498
                            break;
199
498
                        }
200
926
                        if (uval >= imax &&
201
926
                            (uval > imax || d > irem)
202
926
                            )
203
6
                            return_error(gs_error_limitcheck);
204
926
                    }
205
928
                }
206
201M
                if (scanner_options & SCAN_CPSI_MODE) {
207
0
                    ps_uint32 int1 = 0;
208
0
                    int1 |= (uval & 0xffffffff);
209
0
                    make_int(pref, (ps_int)((ps_int32)int1));
210
0
                }
211
201M
                else
212
201M
                    make_int(pref, uval);
213
214
201M
                return code;
215
201M
            }
216
270M
    }
217
73.9M
iret:
218
73.9M
    if (scanner_options & SCAN_CPSI_MODE) {
219
0
        make_int(pref, (sign < 0 ? (ps_int32)-ival : (ps_int32)ival));
220
0
    }
221
73.9M
    else {
222
73.9M
        make_int(pref, (sign < 0 ? (ps_int)-ival : (ps_int)ival));
223
73.9M
    }
224
73.9M
    return code;
225
226
    /* Accumulate a double in dval. */
227
4.83k
l2d:
228
4.83k
    exp10 = 0;
229
36.7k
    for (;;) {
230
36.7k
        dval = dval * 10 + d;
231
36.7k
        GET_NEXT(c, sp, c = EOFC);
232
36.7k
        if (!IS_DIGIT(d, c))
233
4.83k
            break;
234
36.7k
    }
235
4.83k
    switch (c) {
236
203
        case '.':
237
203
            GET_NEXT(c, sp, c = EOFC);
238
203
            exp10 = 0;
239
203
            goto fd;
240
3.73k
        default:
241
3.73k
            *psp = sp;
242
3.73k
            code = 1;
243
            /* falls through */
244
4.20k
        case EOFC:
245
4.20k
            if (sign < 0)
246
994
                dval = -dval;
247
4.20k
            goto rret;
248
83
        case 'e':
249
93
        case 'E':
250
93
            exp10 = 0;
251
93
            goto fs;
252
335
        case '#':
253
335
            return_error(gs_error_syntaxerror);
254
4.83k
    }
255
256
    /* We saw a '.' while accumulating an integer in ival. */
257
1.86M
i2r:
258
1.86M
    exp10 = 0;
259
6.85M
    while (IS_DIGIT(d, c) || c == '-') {
260
        /*
261
         * PostScript gives an error on numbers with a '-' following a '.'
262
         * Adobe Acrobat Reader (PDF) apparently doesn't treat this as an
263
         * error. Experiments show that the numbers following the '-' are
264
         * ignored, so we swallow the fractional part. SCAN_PDF_INV_NUM
265
         *  enables this compatibility kloodge.
266
         */
267
4.99M
        if (c == '-') {
268
27
            if ((SCAN_PDF_INV_NUM & scanner_options) == 0)
269
27
                break;
270
0
            do {
271
0
                GET_NEXT(c, sp, c = EOFC);
272
0
            } while (IS_DIGIT(d, c));
273
0
            break;
274
27
        }
275
4.99M
        if (WOULD_OVERFLOW(ival, d, max_int)) {
276
5.37k
            dval = (double)ival;
277
5.37k
            goto fd;
278
5.37k
        }
279
4.98M
        ival = ival * 10 + d;
280
4.98M
        exp10--;
281
4.98M
        GET_NEXT(c, sp, c = EOFC);
282
4.98M
    }
283
1.85M
    if (sign < 0)
284
197k
        ival = -ival;
285
    /* Take a shortcut for the common case */
286
1.85M
    if (!(c == 'e' || c == 'E' || exp10 < -NUM_POWERS_10)) { /* Check for trailing garbage */
287
1.85M
        if (c != EOFC)
288
1.59M
            *psp = sp, code = 1;
289
1.85M
        make_real(pref, ival * neg_powers_10[-exp10]);
290
1.85M
        return code;
291
1.85M
    }
292
1.02k
    dval = (double)ival;
293
1.02k
    goto fe;
294
295
    /* Now we are accumulating a double in dval. */
296
5.58k
fd:
297
34.3k
    while (IS_DIGIT(d, c)) {
298
28.7k
        dval = dval * 10 + d;
299
28.7k
        exp10--;
300
28.7k
        GET_NEXT(c, sp, c = EOFC);
301
28.7k
    }
302
5.67k
fs:
303
5.67k
    if (sign < 0)
304
1.61k
        dval = -dval;
305
9.33k
fe:
306
    /* Now dval contains the value, negated if necessary. */
307
9.33k
    switch (c) {
308
2.26k
        case 'e':
309
3.42k
        case 'E':
310
3.42k
            {     /* Check for a following exponent. */
311
3.42k
                int esign = 0;
312
3.42k
                int iexp;
313
314
3.42k
                GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
315
3.01k
                switch (c) {
316
583
                    case '-':
317
583
                        esign = 1;
318
                        /* fall through */
319
1.01k
                    case '+':
320
1.01k
                        GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
321
3.01k
                }
322
                /* Scan the exponent.  We limit it arbitrarily to 999. */
323
2.93k
                if (!IS_DIGIT(d, c))
324
470
                    return_error(gs_error_syntaxerror);
325
2.46k
                iexp = d;
326
3.57k
                for (;; iexp = iexp * 10 + d) {
327
3.57k
                    GET_NEXT(c, sp, break);
328
3.05k
                    if (!IS_DIGIT(d, c)) {
329
1.93k
                        *psp = sp;
330
1.93k
                        code = 1;
331
1.93k
                        break;
332
1.93k
                    }
333
1.11k
                    if (iexp > 99)
334
8
                        return_error(gs_error_limitcheck);
335
1.11k
                }
336
2.45k
                if (esign)
337
577
                    exp10 -= iexp;
338
1.88k
                else
339
1.88k
                    exp10 += iexp;
340
2.45k
                break;
341
2.46k
            }
342
5.67k
        default:
343
5.67k
            *psp = sp;
344
5.67k
            code = 1;
345
5.90k
        case EOFC:
346
5.90k
            ;
347
9.33k
    }
348
    /* Compute dval * 10^exp10. */
349
8.35k
    if (exp10 > 0) {
350
3.63k
        while (exp10 > NUM_POWERS_10)
351
2.32k
            dval *= powers_10[NUM_POWERS_10],
352
2.32k
                exp10 -= NUM_POWERS_10;
353
1.30k
        dval *= powers_10[exp10];
354
7.05k
    } else if (exp10 < 0) {
355
31.6k
        while (exp10 < -NUM_POWERS_10)
356
24.8k
            dval /= powers_10[NUM_POWERS_10],
357
24.8k
                exp10 += NUM_POWERS_10;
358
6.75k
        dval /= powers_10[-exp10];
359
6.75k
    }
360
    /*
361
     * Check for an out-of-range result.  Currently we don't check for
362
     * absurdly large numbers of digits in the accumulation loops,
363
     * but we should.
364
     */
365
8.35k
    if (dval >= 0) {
366
6.08k
        if (dval > MAX_FLOAT)
367
12
            return_error(gs_error_limitcheck);
368
6.08k
    } else {
369
2.27k
        if (dval < -MAX_FLOAT)
370
3
            return_error(gs_error_limitcheck);
371
2.27k
    }
372
12.5k
rret:
373
12.5k
    make_real(pref, dval);
374
12.5k
    return code;
375
8.35k
}