Coverage Report

Created: 2025-06-10 07:27

/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
4.44G
{
41
4.44G
    const byte *sp = str;
42
4.44G
#define GET_NEXT(cvar, sp, end_action)\
43
18.0G
  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
4.44G
#define NUM_POWERS_10 6
50
4.44G
    static const float powers_10[NUM_POWERS_10 + 1] = {
51
4.44G
        1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6
52
4.44G
    };
53
4.44G
    static const double neg_powers_10[NUM_POWERS_10 + 1] = {
54
4.44G
        1e0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6
55
4.44G
    };
56
57
4.44G
    ps_int ival;
58
4.44G
    double dval;
59
4.44G
    int exp10;
60
4.44G
    int code = 0;
61
4.44G
    int c, d;
62
4.44G
    ps_int max_ps_int_scan, min_ps_int_scan;
63
4.44G
    const byte *const decoder = scan_char_decoder;
64
4.44G
#define IS_DIGIT(d, c)\
65
11.9G
  ((d = decoder[c]) < 10)
66
4.44G
#define WOULD_OVERFLOW(val, d, maxv)\
67
4.44G
  (val >= maxv / 10 && (val > maxv / 10 || d > (int64_t)(maxv % 10)))
68
69
4.44G
    GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
70
4.44G
    if (!IS_DIGIT(d, c)) {
71
870M
        if (c != '.')
72
703k
            return_error(gs_error_syntaxerror);
73
        /* Might be a number starting with '.'. */
74
869M
        GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
75
869M
        if (!IS_DIGIT(d, c))
76
869M
            return_error(gs_error_syntaxerror);
77
71.8k
        ival = 0;
78
71.8k
        goto i2r;
79
869M
    }
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
3.57G
    ival = d;
85
3.57G
    if (end - sp >= 3) { /* just check once */
86
3.48G
        if (!IS_DIGIT(d, (c = *sp))) {
87
725M
            sp++;
88
725M
            goto ind;
89
725M
        }
90
2.76G
        ival = ival * 10 + d;
91
2.76G
        if (!IS_DIGIT(d, (c = sp[1]))) {
92
2.67G
            sp += 2;
93
2.67G
            goto ind;
94
2.67G
        }
95
89.9M
        ival = ival * 10 + d;
96
89.9M
        sp += 3;
97
89.9M
        if (!IS_DIGIT(d, (c = sp[-1])))
98
50.5M
            goto ind;
99
39.3M
        ival = ival * 10 + d;
100
39.3M
    }
101
102
120M
    max_ps_int_scan = scanner_options & SCAN_CPSI_MODE ? MAX_PS_INT32 : MAX_PS_INT;
103
120M
    min_ps_int_scan = scanner_options & SCAN_CPSI_MODE ? MIN_PS_INT32 : MIN_PS_INT;
104
105
181M
    for (;; ival = ival * 10 + d) {
106
181M
        GET_NEXT(c, sp, goto iret);
107
95.2M
        if (!IS_DIGIT(d, c))
108
34.0M
            break;
109
61.1M
        if (WOULD_OVERFLOW(((ps_uint)ival), d, max_ps_int_scan)) {
110
51.7k
            if (ival == max_ps_int_scan / 10 && d == (max_ps_int_scan % 10) + 1 && sign < 0) {
111
4.69k
                GET_NEXT(c, sp, c = EOFC);
112
4.69k
                dval = -(double)min_ps_int_scan;
113
4.69k
                if (c == 'e' || c == 'E') {
114
11
                    exp10 = 0;
115
11
                    goto fs;
116
4.68k
                } else if (c == '.') {
117
55
                    GET_NEXT(c, sp, c = EOFC);
118
55
                    exp10 = 0;
119
55
                    goto fd;
120
4.63k
                } else if (!IS_DIGIT(d, c)) {
121
4.59k
                    ival = min_ps_int_scan;
122
4.59k
                    break;
123
4.59k
                }
124
4.69k
            } else
125
47.0k
                dval = (double)ival;
126
47.0k
            goto l2d;
127
51.7k
        }
128
61.1M
    }
129
3.48G
  ind:        /* We saw a non-digit while accumulating an integer in ival. */
130
3.48G
    switch (c) {
131
22.1M
        case '.':
132
22.1M
            GET_NEXT(c, sp, c = EOFC);
133
22.1M
            goto i2r;
134
843M
        default:
135
843M
            *psp = sp;
136
843M
            code = 1;
137
843M
            break;
138
143
        case EOFC:
139
143
            break;
140
342k
        case 'e':
141
355k
        case 'E':
142
355k
            if (sign < 0)
143
4.42k
                ival = -ival;
144
355k
            dval = (double)ival;
145
355k
            exp10 = 0;
146
355k
            goto fe;
147
2.61G
        case '#':
148
2.61G
            {
149
2.61G
                const int radix = ival;
150
2.61G
                ps_int uval = 0, imax;
151
152
2.61G
                if (sign || radix < min_radix || radix > max_radix)
153
13.3k
                    return_error(gs_error_syntaxerror);
154
                /* Avoid multiplies for power-of-2 radix. */
155
2.61G
                if (!(radix & (radix - 1))) {
156
2.61G
                    int shift;
157
158
2.61G
                    switch (radix) {
159
4.22k
                        case 2:
160
4.22k
                            shift = 1, imax = MAX_PS_UINT >> 1;
161
4.22k
                            break;
162
16.7k
                        case 4:
163
16.7k
                            shift = 2, imax = MAX_PS_UINT >> 2;
164
16.7k
                            break;
165
987
                        case 8:
166
987
                            shift = 3, imax = MAX_PS_UINT >> 3;
167
987
                            break;
168
2.61G
                        case 16:
169
2.61G
                            shift = 4, imax = MAX_PS_UINT >> 4;
170
2.61G
                            break;
171
1.06k
                        case 32:
172
1.06k
                            shift = 5, imax = MAX_PS_UINT >> 5;
173
1.06k
                            break;
174
0
                        default:  /* can't happen */
175
0
                            return_error(gs_error_rangecheck);
176
2.61G
                    }
177
12.4G
                    for (;; uval = (uval << shift) + d) {
178
12.4G
                        GET_NEXT(c, sp, break);
179
11.2G
                        d = decoder[c];
180
11.2G
                        if (d >= radix) {
181
1.35G
                            *psp = sp;
182
1.35G
                            code = 1;
183
1.35G
                            break;
184
1.35G
                        }
185
9.85G
                        if (uval > imax)
186
39
                            return_error(gs_error_limitcheck);
187
9.85G
                    }
188
2.61G
                } else {
189
8.00k
                    ps_int irem = MAX_PS_UINT % radix;
190
191
8.00k
                    imax = MAX_PS_UINT / radix;
192
18.8k
                    for (;; uval = uval * radix + d) {
193
18.8k
                        GET_NEXT(c, sp, break);
194
15.5k
                        d = decoder[c];
195
15.5k
                        if (d >= radix) {
196
4.61k
                            *psp = sp;
197
4.61k
                            code = 1;
198
4.61k
                            break;
199
4.61k
                        }
200
10.9k
                        if (uval >= imax &&
201
10.9k
                            (uval > imax || d > irem)
202
10.9k
                            )
203
102
                            return_error(gs_error_limitcheck);
204
10.9k
                    }
205
8.00k
                }
206
2.61G
                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
2.61G
                else
212
2.61G
                    make_int(pref, uval);
213
214
2.61G
                return code;
215
2.61G
            }
216
3.48G
    }
217
930M
iret:
218
930M
    if (scanner_options & SCAN_CPSI_MODE) {
219
0
        make_int(pref, (sign < 0 ? (ps_int32)-ival : (ps_int32)ival));
220
0
    }
221
930M
    else {
222
930M
        make_int(pref, (sign < 0 ? (ps_int)-ival : (ps_int)ival));
223
930M
    }
224
930M
    return code;
225
226
    /* Accumulate a double in dval. */
227
47.0k
l2d:
228
47.0k
    exp10 = 0;
229
507k
    for (;;) {
230
507k
        dval = dval * 10 + d;
231
507k
        GET_NEXT(c, sp, c = EOFC);
232
507k
        if (!IS_DIGIT(d, c))
233
47.0k
            break;
234
507k
    }
235
47.0k
    switch (c) {
236
2.18k
        case '.':
237
2.18k
            GET_NEXT(c, sp, c = EOFC);
238
2.18k
            exp10 = 0;
239
2.18k
            goto fd;
240
34.1k
        default:
241
34.1k
            *psp = sp;
242
34.1k
            code = 1;
243
            /* falls through */
244
40.8k
        case EOFC:
245
40.8k
            if (sign < 0)
246
9.39k
                dval = -dval;
247
40.8k
            goto rret;
248
1.40k
        case 'e':
249
2.43k
        case 'E':
250
2.43k
            exp10 = 0;
251
2.43k
            goto fs;
252
1.61k
        case '#':
253
1.61k
            return_error(gs_error_syntaxerror);
254
47.0k
    }
255
256
    /* We saw a '.' while accumulating an integer in ival. */
257
22.2M
i2r:
258
22.2M
    exp10 = 0;
259
81.6M
    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
59.4M
        if (c == '-') {
268
338
            if ((SCAN_PDF_INV_NUM & scanner_options) == 0)
269
338
                break;
270
0
            do {
271
0
                GET_NEXT(c, sp, c = EOFC);
272
0
            } while (IS_DIGIT(d, c));
273
0
            break;
274
338
        }
275
59.4M
        if (WOULD_OVERFLOW(ival, d, max_int)) {
276
51.4k
            dval = (double)ival;
277
51.4k
            goto fd;
278
51.4k
        }
279
59.3M
        ival = ival * 10 + d;
280
59.3M
        exp10--;
281
59.3M
        GET_NEXT(c, sp, c = EOFC);
282
59.3M
    }
283
22.1M
    if (sign < 0)
284
1.87M
        ival = -ival;
285
    /* Take a shortcut for the common case */
286
22.1M
    if (!(c == 'e' || c == 'E' || exp10 < -NUM_POWERS_10)) { /* Check for trailing garbage */
287
22.1M
        if (c != EOFC)
288
18.8M
            *psp = sp, code = 1;
289
22.1M
        make_real(pref, ival * neg_powers_10[-exp10]);
290
22.1M
        return code;
291
22.1M
    }
292
20.6k
    dval = (double)ival;
293
20.6k
    goto fe;
294
295
    /* Now we are accumulating a double in dval. */
296
53.7k
fd:
297
338k
    while (IS_DIGIT(d, c)) {
298
284k
        dval = dval * 10 + d;
299
284k
        exp10--;
300
284k
        GET_NEXT(c, sp, c = EOFC);
301
284k
    }
302
56.1k
fs:
303
56.1k
    if (sign < 0)
304
12.5k
        dval = -dval;
305
432k
fe:
306
    /* Now dval contains the value, negated if necessary. */
307
432k
    switch (c) {
308
354k
        case 'e':
309
370k
        case 'E':
310
370k
            {     /* Check for a following exponent. */
311
370k
                int esign = 0;
312
370k
                int iexp;
313
314
370k
                GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
315
213k
                switch (c) {
316
11.0k
                    case '-':
317
11.0k
                        esign = 1;
318
                        /* fall through */
319
19.4k
                    case '+':
320
19.4k
                        GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
321
213k
                }
322
                /* Scan the exponent.  We limit it arbitrarily to 999. */
323
211k
                if (!IS_DIGIT(d, c))
324
169k
                    return_error(gs_error_syntaxerror);
325
42.7k
                iexp = d;
326
62.0k
                for (;; iexp = iexp * 10 + d) {
327
62.0k
                    GET_NEXT(c, sp, break);
328
53.6k
                    if (!IS_DIGIT(d, c)) {
329
34.2k
                        *psp = sp;
330
34.2k
                        code = 1;
331
34.2k
                        break;
332
34.2k
                    }
333
19.4k
                    if (iexp > 99)
334
108
                        return_error(gs_error_limitcheck);
335
19.4k
                }
336
42.6k
                if (esign)
337
10.7k
                    exp10 -= iexp;
338
31.9k
                else
339
31.9k
                    exp10 += iexp;
340
42.6k
                break;
341
42.7k
            }
342
57.7k
        default:
343
57.7k
            *psp = sp;
344
57.7k
            code = 1;
345
61.5k
        case EOFC:
346
61.5k
            ;
347
432k
    }
348
    /* Compute dval * 10^exp10. */
349
104k
    if (exp10 > 0) {
350
60.8k
        while (exp10 > NUM_POWERS_10)
351
33.8k
            dval *= powers_10[NUM_POWERS_10],
352
33.8k
                exp10 -= NUM_POWERS_10;
353
26.9k
        dval *= powers_10[exp10];
354
77.1k
    } else if (exp10 < 0) {
355
415k
        while (exp10 < -NUM_POWERS_10)
356
342k
            dval /= powers_10[NUM_POWERS_10],
357
342k
                exp10 += NUM_POWERS_10;
358
72.7k
        dval /= powers_10[-exp10];
359
72.7k
    }
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
104k
    if (dval >= 0) {
366
86.0k
        if (dval > MAX_FLOAT)
367
178
            return_error(gs_error_limitcheck);
368
86.0k
    } else {
369
18.0k
        if (dval < -MAX_FLOAT)
370
53
            return_error(gs_error_limitcheck);
371
18.0k
    }
372
144k
rret:
373
144k
    make_real(pref, dval);
374
144k
    return code;
375
104k
}