Coverage Report

Created: 2026-07-24 07:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/psi/iscannum.c
Line
Count
Source
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.79G
{
41
4.79G
    const byte *sp = str;
42
4.79G
#define GET_NEXT(cvar, sp, end_action)\
43
19.5G
  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.79G
#define NUM_POWERS_10 6
50
4.79G
    static const float powers_10[NUM_POWERS_10 + 1] = {
51
4.79G
        1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6
52
4.79G
    };
53
4.79G
    static const double neg_powers_10[NUM_POWERS_10 + 1] = {
54
4.79G
        1e0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6
55
4.79G
    };
56
57
4.79G
    ps_int ival;
58
4.79G
    double dval;
59
4.79G
    int exp10;
60
4.79G
    int code = 0;
61
4.79G
    int c, d;
62
4.79G
    ps_int max_ps_int_scan, min_ps_int_scan;
63
4.79G
    const byte *const decoder = scan_char_decoder;
64
4.79G
#define IS_DIGIT(d, c)\
65
12.9G
  ((d = decoder[c]) < 10)
66
4.79G
#define WOULD_OVERFLOW(val, d, maxv)\
67
4.79G
  (val >= maxv / 10 && (val > maxv / 10 || d > (int64_t)(maxv % 10)))
68
69
4.79G
    GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
70
4.79G
    if (!IS_DIGIT(d, c)) {
71
932M
        if (c != '.')
72
472k
            return_error(gs_error_syntaxerror);
73
        /* Might be a number starting with '.'. */
74
932M
        GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
75
932M
        if (!IS_DIGIT(d, c))
76
932M
            return_error(gs_error_syntaxerror);
77
72.5k
        ival = 0;
78
72.5k
        goto i2r;
79
932M
    }
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.86G
    ival = d;
85
3.86G
    if (end - sp >= 3) { /* just check once */
86
3.77G
        if (!IS_DIGIT(d, (c = *sp))) {
87
769M
            sp++;
88
769M
            goto ind;
89
769M
        }
90
3.00G
        ival = ival * 10 + d;
91
3.00G
        if (!IS_DIGIT(d, (c = sp[1]))) {
92
2.90G
            sp += 2;
93
2.90G
            goto ind;
94
2.90G
        }
95
97.7M
        ival = ival * 10 + d;
96
97.7M
        sp += 3;
97
97.7M
        if (!IS_DIGIT(d, (c = sp[-1])))
98
55.2M
            goto ind;
99
42.5M
        ival = ival * 10 + d;
100
42.5M
    }
101
102
128M
    max_ps_int_scan = scanner_options & SCAN_CPSI_MODE ? MAX_PS_INT32 : MAX_PS_INT;
103
128M
    min_ps_int_scan = scanner_options & SCAN_CPSI_MODE ? MIN_PS_INT32 : MIN_PS_INT;
104
105
192M
    for (;; ival = ival * 10 + d) {
106
192M
        GET_NEXT(c, sp, goto iret);
107
100M
        if (!IS_DIGIT(d, c))
108
36.6M
            break;
109
63.9M
        if (WOULD_OVERFLOW(((ps_uint)ival), d, max_ps_int_scan)) {
110
43.0k
            if (ival == max_ps_int_scan / 10 && d == (max_ps_int_scan % 10) + 1 && sign < 0) {
111
5.07k
                GET_NEXT(c, sp, c = EOFC);
112
5.07k
                dval = -(double)min_ps_int_scan;
113
5.07k
                if (c == 'e' || c == 'E') {
114
14
                    exp10 = 0;
115
14
                    goto fs;
116
5.06k
                } else if (c == '.') {
117
60
                    GET_NEXT(c, sp, c = EOFC);
118
60
                    exp10 = 0;
119
60
                    goto fd;
120
5.00k
                } else if (!IS_DIGIT(d, c)) {
121
4.96k
                    ival = min_ps_int_scan;
122
4.96k
                    break;
123
4.96k
                }
124
5.07k
            } else
125
37.9k
                dval = (double)ival;
126
38.0k
            goto l2d;
127
43.0k
        }
128
63.9M
    }
129
3.76G
  ind:        /* We saw a non-digit while accumulating an integer in ival. */
130
3.76G
    switch (c) {
131
25.2M
        case '.':
132
25.2M
            GET_NEXT(c, sp, c = EOFC);
133
25.2M
            goto i2r;
134
895M
        default:
135
895M
            *psp = sp;
136
895M
            code = 1;
137
895M
            break;
138
147
        case EOFC:
139
147
            break;
140
344k
        case 'e':
141
357k
        case 'E':
142
357k
            if (sign < 0)
143
4.65k
                ival = -ival;
144
357k
            dval = (double)ival;
145
357k
            exp10 = 0;
146
357k
            goto fe;
147
2.84G
        case '#':
148
2.84G
            {
149
2.84G
                const int radix = ival;
150
2.84G
                ps_int uval = 0, imax;
151
152
2.84G
                if (sign || radix < min_radix || radix > max_radix)
153
11.6k
                    return_error(gs_error_syntaxerror);
154
                /* Avoid multiplies for power-of-2 radix. */
155
2.84G
                if (!(radix & (radix - 1))) {
156
2.84G
                    int shift;
157
158
2.84G
                    switch (radix) {
159
4.06k
                        case 2:
160
4.06k
                            shift = 1, imax = MAX_PS_UINT >> 1;
161
4.06k
                            break;
162
15.9k
                        case 4:
163
15.9k
                            shift = 2, imax = MAX_PS_UINT >> 2;
164
15.9k
                            break;
165
640
                        case 8:
166
640
                            shift = 3, imax = MAX_PS_UINT >> 3;
167
640
                            break;
168
2.84G
                        case 16:
169
2.84G
                            shift = 4, imax = MAX_PS_UINT >> 4;
170
2.84G
                            break;
171
880
                        case 32:
172
880
                            shift = 5, imax = MAX_PS_UINT >> 5;
173
880
                            break;
174
0
                        default:  /* can't happen */
175
0
                            return_error(gs_error_rangecheck);
176
2.84G
                    }
177
13.5G
                    for (;; uval = (uval << shift) + d) {
178
13.5G
                        GET_NEXT(c, sp, break);
179
12.1G
                        d = decoder[c];
180
12.1G
                        if (d >= radix) {
181
1.46G
                            *psp = sp;
182
1.46G
                            code = 1;
183
1.46G
                            break;
184
1.46G
                        }
185
10.7G
                        if (uval > imax)
186
34
                            return_error(gs_error_limitcheck);
187
10.7G
                    }
188
2.84G
                } else {
189
8.17k
                    ps_int irem = MAX_PS_UINT % radix;
190
191
8.17k
                    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.83k
                            *psp = sp;
197
4.83k
                            code = 1;
198
4.83k
                            break;
199
4.83k
                        }
200
10.7k
                        if (uval >= imax &&
201
84
                            (uval > imax || d > irem)
202
10.7k
                            )
203
84
                            return_error(gs_error_limitcheck);
204
10.7k
                    }
205
8.17k
                }
206
2.84G
                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.84G
                else
212
2.84G
                    make_int(pref, uval);
213
214
2.84G
                return code;
215
2.84G
            }
216
3.76G
    }
217
986M
iret:
218
986M
    if (scanner_options & SCAN_CPSI_MODE) {
219
0
        make_int(pref, (sign < 0 ? (ps_int32)-ival : (ps_int32)ival));
220
0
    }
221
986M
    else {
222
986M
        make_int(pref, (sign < 0 ? (ps_int)-ival : (ps_int)ival));
223
986M
    }
224
986M
    return code;
225
226
    /* Accumulate a double in dval. */
227
38.0k
l2d:
228
38.0k
    exp10 = 0;
229
371k
    for (;;) {
230
371k
        dval = dval * 10 + d;
231
371k
        GET_NEXT(c, sp, c = EOFC);
232
371k
        if (!IS_DIGIT(d, c))
233
38.0k
            break;
234
371k
    }
235
38.0k
    switch (c) {
236
1.51k
        case '.':
237
1.51k
            GET_NEXT(c, sp, c = EOFC);
238
1.51k
            exp10 = 0;
239
1.51k
            goto fd;
240
26.2k
        default:
241
26.2k
            *psp = sp;
242
26.2k
            code = 1;
243
            /* falls through */
244
31.8k
        case EOFC:
245
31.8k
            if (sign < 0)
246
8.22k
                dval = -dval;
247
31.8k
            goto rret;
248
1.27k
        case 'e':
249
2.32k
        case 'E':
250
2.32k
            exp10 = 0;
251
2.32k
            goto fs;
252
2.33k
        case '#':
253
2.33k
            return_error(gs_error_syntaxerror);
254
38.0k
    }
255
256
    /* We saw a '.' while accumulating an integer in ival. */
257
25.2M
i2r:
258
25.2M
    exp10 = 0;
259
96.3M
    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
71.1M
        if (c == '-') {
268
545
            if ((SCAN_PDF_INV_NUM & scanner_options) == 0)
269
545
                break;
270
0
            do {
271
0
                GET_NEXT(c, sp, c = EOFC);
272
0
            } while (IS_DIGIT(d, c));
273
0
            break;
274
545
        }
275
71.1M
        if (WOULD_OVERFLOW(ival, d, max_int)) {
276
86.5k
            dval = (double)ival;
277
86.5k
            goto fd;
278
86.5k
        }
279
71.0M
        ival = ival * 10 + d;
280
71.0M
        exp10--;
281
71.0M
        GET_NEXT(c, sp, c = EOFC);
282
71.0M
    }
283
25.2M
    if (sign < 0)
284
2.02M
        ival = -ival;
285
    /* Take a shortcut for the common case */
286
25.2M
    if (!(c == 'e' || c == 'E' || exp10 < -NUM_POWERS_10)) { /* Check for trailing garbage */
287
25.1M
        if (c != EOFC)
288
21.6M
            *psp = sp, code = 1;
289
25.1M
        make_real(pref, ival * neg_powers_10[-exp10]);
290
25.1M
        return code;
291
25.1M
    }
292
19.4k
    dval = (double)ival;
293
19.4k
    goto fe;
294
295
    /* Now we are accumulating a double in dval. */
296
88.0k
fd:
297
766k
    while (IS_DIGIT(d, c)) {
298
678k
        dval = dval * 10 + d;
299
678k
        exp10--;
300
678k
        GET_NEXT(c, sp, c = EOFC);
301
678k
    }
302
90.4k
fs:
303
90.4k
    if (sign < 0)
304
33.7k
        dval = -dval;
305
467k
fe:
306
    /* Now dval contains the value, negated if necessary. */
307
467k
    switch (c) {
308
358k
        case 'e':
309
376k
        case 'E':
310
376k
            {     /* Check for a following exponent. */
311
376k
                int esign = 0;
312
376k
                int iexp;
313
314
376k
                GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
315
213k
                switch (c) {
316
13.7k
                    case '-':
317
13.7k
                        esign = 1;
318
                        /* fall through */
319
19.2k
                    case '+':
320
19.2k
                        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
170k
                    return_error(gs_error_syntaxerror);
325
41.6k
                iexp = d;
326
65.3k
                for (;; iexp = iexp * 10 + d) {
327
65.3k
                    GET_NEXT(c, sp, break);
328
57.2k
                    if (!IS_DIGIT(d, c)) {
329
33.4k
                        *psp = sp;
330
33.4k
                        code = 1;
331
33.4k
                        break;
332
33.4k
                    }
333
23.8k
                    if (iexp > 99)
334
100
                        return_error(gs_error_limitcheck);
335
23.8k
                }
336
41.5k
                if (esign)
337
13.5k
                    exp10 -= iexp;
338
27.9k
                else
339
27.9k
                    exp10 += iexp;
340
41.5k
                break;
341
41.6k
            }
342
87.4k
        default:
343
87.4k
            *psp = sp;
344
87.4k
            code = 1;
345
91.4k
        case EOFC:
346
91.4k
            ;
347
467k
    }
348
    /* Compute dval * 10^exp10. */
349
132k
    if (exp10 > 0) {
350
53.1k
        while (exp10 > NUM_POWERS_10)
351
31.1k
            dval *= powers_10[NUM_POWERS_10],
352
31.1k
                exp10 -= NUM_POWERS_10;
353
21.9k
        dval *= powers_10[exp10];
354
110k
    } else if (exp10 < 0) {
355
634k
        while (exp10 < -NUM_POWERS_10)
356
528k
            dval /= powers_10[NUM_POWERS_10],
357
528k
                exp10 += NUM_POWERS_10;
358
106k
        dval /= powers_10[-exp10];
359
106k
    }
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
132k
    if (dval >= 0) {
366
93.6k
        if (dval > MAX_FLOAT)
367
150
            return_error(gs_error_limitcheck);
368
93.6k
    } else {
369
39.3k
        if (dval < -MAX_FLOAT)
370
58
            return_error(gs_error_limitcheck);
371
39.3k
    }
372
164k
rret:
373
164k
    make_real(pref, dval);
374
164k
    return code;
375
132k
}