Coverage Report

Created: 2025-06-10 06:58

/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
240M
{
41
240M
    const byte *sp = str;
42
240M
#define GET_NEXT(cvar, sp, end_action)\
43
969M
  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
240M
#define NUM_POWERS_10 6
50
240M
    static const float powers_10[NUM_POWERS_10 + 1] = {
51
240M
        1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6
52
240M
    };
53
240M
    static const double neg_powers_10[NUM_POWERS_10 + 1] = {
54
240M
        1e0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6
55
240M
    };
56
57
240M
    ps_int ival;
58
240M
    double dval;
59
240M
    int exp10;
60
240M
    int code = 0;
61
240M
    int c, d;
62
240M
    ps_int max_ps_int_scan, min_ps_int_scan;
63
240M
    const byte *const decoder = scan_char_decoder;
64
240M
#define IS_DIGIT(d, c)\
65
642M
  ((d = decoder[c]) < 10)
66
240M
#define WOULD_OVERFLOW(val, d, maxv)\
67
240M
  (val >= maxv / 10 && (val > maxv / 10 || d > (int64_t)(maxv % 10)))
68
69
240M
    GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
70
240M
    if (!IS_DIGIT(d, c)) {
71
48.3M
        if (c != '.')
72
39.2k
            return_error(gs_error_syntaxerror);
73
        /* Might be a number starting with '.'. */
74
48.2M
        GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
75
48.2M
        if (!IS_DIGIT(d, c))
76
48.2M
            return_error(gs_error_syntaxerror);
77
441
        ival = 0;
78
441
        goto i2r;
79
48.2M
    }
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
192M
    ival = d;
85
192M
    if (end - sp >= 3) { /* just check once */
86
187M
        if (!IS_DIGIT(d, (c = *sp))) {
87
40.0M
            sp++;
88
40.0M
            goto ind;
89
40.0M
        }
90
147M
        ival = ival * 10 + d;
91
147M
        if (!IS_DIGIT(d, (c = sp[1]))) {
92
143M
            sp += 2;
93
143M
            goto ind;
94
143M
        }
95
4.49M
        ival = ival * 10 + d;
96
4.49M
        sp += 3;
97
4.49M
        if (!IS_DIGIT(d, (c = sp[-1])))
98
2.36M
            goto ind;
99
2.13M
        ival = ival * 10 + d;
100
2.13M
    }
101
102
6.53M
    max_ps_int_scan = scanner_options & SCAN_CPSI_MODE ? MAX_PS_INT32 : MAX_PS_INT;
103
6.53M
    min_ps_int_scan = scanner_options & SCAN_CPSI_MODE ? MIN_PS_INT32 : MIN_PS_INT;
104
105
9.58M
    for (;; ival = ival * 10 + d) {
106
9.58M
        GET_NEXT(c, sp, goto iret);
107
4.86M
        if (!IS_DIGIT(d, c))
108
1.81M
            break;
109
3.04M
        if (WOULD_OVERFLOW(((ps_uint)ival), d, max_ps_int_scan)) {
110
2.60k
            if (ival == max_ps_int_scan / 10 && d == (max_ps_int_scan % 10) + 1 && sign < 0) {
111
93
                GET_NEXT(c, sp, c = EOFC);
112
93
                dval = -(double)min_ps_int_scan;
113
93
                if (c == 'e' || c == 'E') {
114
0
                    exp10 = 0;
115
0
                    goto fs;
116
93
                } else if (c == '.') {
117
0
                    GET_NEXT(c, sp, c = EOFC);
118
0
                    exp10 = 0;
119
0
                    goto fd;
120
93
                } else if (!IS_DIGIT(d, c)) {
121
90
                    ival = min_ps_int_scan;
122
90
                    break;
123
90
                }
124
93
            } else
125
2.51k
                dval = (double)ival;
126
2.51k
            goto l2d;
127
2.60k
        }
128
3.04M
    }
129
187M
  ind:        /* We saw a non-digit while accumulating an integer in ival. */
130
187M
    switch (c) {
131
1.17M
        case '.':
132
1.17M
            GET_NEXT(c, sp, c = EOFC);
133
1.17M
            goto i2r;
134
46.0M
        default:
135
46.0M
            *psp = sp;
136
46.0M
            code = 1;
137
46.0M
            break;
138
6
        case EOFC:
139
6
            break;
140
2.11k
        case 'e':
141
4.39k
        case 'E':
142
4.39k
            if (sign < 0)
143
54
                ival = -ival;
144
4.39k
            dval = (double)ival;
145
4.39k
            exp10 = 0;
146
4.39k
            goto fe;
147
140M
        case '#':
148
140M
            {
149
140M
                const int radix = ival;
150
140M
                ps_int uval = 0, imax;
151
152
140M
                if (sign || radix < min_radix || radix > max_radix)
153
230
                    return_error(gs_error_syntaxerror);
154
                /* Avoid multiplies for power-of-2 radix. */
155
140M
                if (!(radix & (radix - 1))) {
156
140M
                    int shift;
157
158
140M
                    switch (radix) {
159
144
                        case 2:
160
144
                            shift = 1, imax = MAX_PS_UINT >> 1;
161
144
                            break;
162
49
                        case 4:
163
49
                            shift = 2, imax = MAX_PS_UINT >> 2;
164
49
                            break;
165
7
                        case 8:
166
7
                            shift = 3, imax = MAX_PS_UINT >> 3;
167
7
                            break;
168
140M
                        case 16:
169
140M
                            shift = 4, imax = MAX_PS_UINT >> 4;
170
140M
                            break;
171
76
                        case 32:
172
76
                            shift = 5, imax = MAX_PS_UINT >> 5;
173
76
                            break;
174
0
                        default:  /* can't happen */
175
0
                            return_error(gs_error_rangecheck);
176
140M
                    }
177
667M
                    for (;; uval = (uval << shift) + d) {
178
667M
                        GET_NEXT(c, sp, break);
179
597M
                        d = decoder[c];
180
597M
                        if (d >= radix) {
181
70.1M
                            *psp = sp;
182
70.1M
                            code = 1;
183
70.1M
                            break;
184
70.1M
                        }
185
526M
                        if (uval > imax)
186
2
                            return_error(gs_error_limitcheck);
187
526M
                    }
188
140M
                } else {
189
383
                    ps_int irem = MAX_PS_UINT % radix;
190
191
383
                    imax = MAX_PS_UINT / radix;
192
1.49k
                    for (;; uval = uval * radix + d) {
193
1.49k
                        GET_NEXT(c, sp, break);
194
1.45k
                        d = decoder[c];
195
1.45k
                        if (d >= radix) {
196
332
                            *psp = sp;
197
332
                            code = 1;
198
332
                            break;
199
332
                        }
200
1.11k
                        if (uval >= imax &&
201
1.11k
                            (uval > imax || d > irem)
202
1.11k
                            )
203
8
                            return_error(gs_error_limitcheck);
204
1.11k
                    }
205
383
                }
206
140M
                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
140M
                else
212
140M
                    make_int(pref, uval);
213
214
140M
                return code;
215
140M
            }
216
187M
    }
217
50.7M
iret:
218
50.7M
    if (scanner_options & SCAN_CPSI_MODE) {
219
0
        make_int(pref, (sign < 0 ? (ps_int32)-ival : (ps_int32)ival));
220
0
    }
221
50.7M
    else {
222
50.7M
        make_int(pref, (sign < 0 ? (ps_int)-ival : (ps_int)ival));
223
50.7M
    }
224
50.7M
    return code;
225
226
    /* Accumulate a double in dval. */
227
2.51k
l2d:
228
2.51k
    exp10 = 0;
229
25.0k
    for (;;) {
230
25.0k
        dval = dval * 10 + d;
231
25.0k
        GET_NEXT(c, sp, c = EOFC);
232
25.0k
        if (!IS_DIGIT(d, c))
233
2.51k
            break;
234
25.0k
    }
235
2.51k
    switch (c) {
236
27
        case '.':
237
27
            GET_NEXT(c, sp, c = EOFC);
238
27
            exp10 = 0;
239
27
            goto fd;
240
1.31k
        default:
241
1.31k
            *psp = sp;
242
1.31k
            code = 1;
243
            /* falls through */
244
1.80k
        case EOFC:
245
1.80k
            if (sign < 0)
246
150
                dval = -dval;
247
1.80k
            goto rret;
248
635
        case 'e':
249
677
        case 'E':
250
677
            exp10 = 0;
251
677
            goto fs;
252
7
        case '#':
253
7
            return_error(gs_error_syntaxerror);
254
2.51k
    }
255
256
    /* We saw a '.' while accumulating an integer in ival. */
257
1.17M
i2r:
258
1.17M
    exp10 = 0;
259
4.19M
    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
3.02M
        if (c == '-') {
268
43
            if ((SCAN_PDF_INV_NUM & scanner_options) == 0)
269
43
                break;
270
0
            do {
271
0
                GET_NEXT(c, sp, c = EOFC);
272
0
            } while (IS_DIGIT(d, c));
273
0
            break;
274
43
        }
275
3.02M
        if (WOULD_OVERFLOW(ival, d, max_int)) {
276
6.63k
            dval = (double)ival;
277
6.63k
            goto fd;
278
6.63k
        }
279
3.01M
        ival = ival * 10 + d;
280
3.01M
        exp10--;
281
3.01M
        GET_NEXT(c, sp, c = EOFC);
282
3.01M
    }
283
1.16M
    if (sign < 0)
284
93.9k
        ival = -ival;
285
    /* Take a shortcut for the common case */
286
1.16M
    if (!(c == 'e' || c == 'E' || exp10 < -NUM_POWERS_10)) { /* Check for trailing garbage */
287
1.16M
        if (c != EOFC)
288
987k
            *psp = sp, code = 1;
289
1.16M
        make_real(pref, ival * neg_powers_10[-exp10]);
290
1.16M
        return code;
291
1.16M
    }
292
193
    dval = (double)ival;
293
193
    goto fe;
294
295
    /* Now we are accumulating a double in dval. */
296
6.65k
fd:
297
18.9k
    while (IS_DIGIT(d, c)) {
298
12.3k
        dval = dval * 10 + d;
299
12.3k
        exp10--;
300
12.3k
        GET_NEXT(c, sp, c = EOFC);
301
12.3k
    }
302
7.33k
fs:
303
7.33k
    if (sign < 0)
304
385
        dval = -dval;
305
11.9k
fe:
306
    /* Now dval contains the value, negated if necessary. */
307
11.9k
    switch (c) {
308
2.76k
        case 'e':
309
5.18k
        case 'E':
310
5.18k
            {     /* Check for a following exponent. */
311
5.18k
                int esign = 0;
312
5.18k
                int iexp;
313
314
5.18k
                GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
315
4.71k
                switch (c) {
316
1.23k
                    case '-':
317
1.23k
                        esign = 1;
318
                        /* fall through */
319
1.60k
                    case '+':
320
1.60k
                        GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
321
4.71k
                }
322
                /* Scan the exponent.  We limit it arbitrarily to 999. */
323
4.58k
                if (!IS_DIGIT(d, c))
324
1.27k
                    return_error(gs_error_syntaxerror);
325
3.31k
                iexp = d;
326
5.18k
                for (;; iexp = iexp * 10 + d) {
327
5.18k
                    GET_NEXT(c, sp, break);
328
4.18k
                    if (!IS_DIGIT(d, c)) {
329
2.30k
                        *psp = sp;
330
2.30k
                        code = 1;
331
2.30k
                        break;
332
2.30k
                    }
333
1.88k
                    if (iexp > 99)
334
7
                        return_error(gs_error_limitcheck);
335
1.88k
                }
336
3.30k
                if (esign)
337
1.19k
                    exp10 -= iexp;
338
2.11k
                else
339
2.11k
                    exp10 += iexp;
340
3.30k
                break;
341
3.31k
            }
342
6.62k
        default:
343
6.62k
            *psp = sp;
344
6.62k
            code = 1;
345
6.73k
        case EOFC:
346
6.73k
            ;
347
11.9k
    }
348
    /* Compute dval * 10^exp10. */
349
10.0k
    if (exp10 > 0) {
350
6.02k
        while (exp10 > NUM_POWERS_10)
351
4.10k
            dval *= powers_10[NUM_POWERS_10],
352
4.10k
                exp10 -= NUM_POWERS_10;
353
1.92k
        dval *= powers_10[exp10];
354
8.12k
    } else if (exp10 < 0) {
355
20.4k
        while (exp10 < -NUM_POWERS_10)
356
12.4k
            dval /= powers_10[NUM_POWERS_10],
357
12.4k
                exp10 += NUM_POWERS_10;
358
7.92k
        dval /= powers_10[-exp10];
359
7.92k
    }
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
10.0k
    if (dval >= 0) {
366
9.49k
        if (dval > MAX_FLOAT)
367
9
            return_error(gs_error_limitcheck);
368
9.49k
    } else {
369
550
        if (dval < -MAX_FLOAT)
370
5
            return_error(gs_error_limitcheck);
371
550
    }
372
11.8k
rret:
373
11.8k
    make_real(pref, dval);
374
11.8k
    return code;
375
10.0k
}