Coverage Report

Created: 2026-11-12 07:09

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.44G
{
41
4.44G
    const byte *sp = str;
42
4.44G
#define GET_NEXT(cvar, sp, end_action)\
43
18.2G
  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
827M
        if (c != '.')
72
235k
            return_error(gs_error_syntaxerror);
73
        /* Might be a number starting with '.'. */
74
826M
        GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
75
826M
        if (!IS_DIGIT(d, c))
76
826M
            return_error(gs_error_syntaxerror);
77
68.2k
        ival = 0;
78
68.2k
        goto i2r;
79
826M
    }
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.62G
    ival = d;
85
3.62G
    if (end - sp >= 3) { /* just check once */
86
3.54G
        if (!IS_DIGIT(d, (c = *sp))) {
87
721M
            sp++;
88
721M
            goto ind;
89
721M
        }
90
2.81G
        ival = ival * 10 + d;
91
2.81G
        if (!IS_DIGIT(d, (c = sp[1]))) {
92
2.73G
            sp += 2;
93
2.73G
            goto ind;
94
2.73G
        }
95
89.1M
        ival = ival * 10 + d;
96
89.1M
        sp += 3;
97
89.1M
        if (!IS_DIGIT(d, (c = sp[-1])))
98
51.0M
            goto ind;
99
38.0M
        ival = ival * 10 + d;
100
38.0M
    }
101
102
119M
    max_ps_int_scan = scanner_options & SCAN_CPSI_MODE ? MAX_PS_INT32 : MAX_PS_INT;
103
119M
    min_ps_int_scan = scanner_options & SCAN_CPSI_MODE ? MIN_PS_INT32 : MIN_PS_INT;
104
105
174M
    for (;; ival = ival * 10 + d) {
106
174M
        GET_NEXT(c, sp, goto iret);
107
87.2M
        if (!IS_DIGIT(d, c))
108
32.2M
            break;
109
54.9M
        if (WOULD_OVERFLOW(((ps_uint)ival), d, max_ps_int_scan)) {
110
40.4k
            if (ival == max_ps_int_scan / 10 && d == (max_ps_int_scan % 10) + 1 && sign < 0) {
111
4.86k
                GET_NEXT(c, sp, c = EOFC);
112
4.86k
                dval = -(double)min_ps_int_scan;
113
4.86k
                if (c == 'e' || c == 'E') {
114
12
                    exp10 = 0;
115
12
                    goto fs;
116
4.85k
                } else if (c == '.') {
117
59
                    GET_NEXT(c, sp, c = EOFC);
118
59
                    exp10 = 0;
119
59
                    goto fd;
120
4.79k
                } else if (!IS_DIGIT(d, c)) {
121
4.75k
                    ival = min_ps_int_scan;
122
4.75k
                    break;
123
4.75k
                }
124
4.86k
            } else
125
35.5k
                dval = (double)ival;
126
35.5k
            goto l2d;
127
40.4k
        }
128
54.9M
    }
129
3.53G
  ind:        /* We saw a non-digit while accumulating an integer in ival. */
130
3.53G
    switch (c) {
131
23.8M
        case '.':
132
23.8M
            GET_NEXT(c, sp, c = EOFC);
133
23.8M
            goto i2r;
134
834M
        default:
135
834M
            *psp = sp;
136
834M
            code = 1;
137
834M
            break;
138
133
        case EOFC:
139
133
            break;
140
170k
        case 'e':
141
183k
        case 'E':
142
183k
            if (sign < 0)
143
4.67k
                ival = -ival;
144
183k
            dval = (double)ival;
145
183k
            exp10 = 0;
146
183k
            goto fe;
147
2.67G
        case '#':
148
2.67G
            {
149
2.67G
                const int radix = ival;
150
2.67G
                ps_int uval = 0, imax;
151
152
2.67G
                if (sign || radix < min_radix || radix > max_radix)
153
10.4k
                    return_error(gs_error_syntaxerror);
154
                /* Avoid multiplies for power-of-2 radix. */
155
2.67G
                if (!(radix & (radix - 1))) {
156
2.67G
                    int shift;
157
158
2.67G
                    switch (radix) {
159
3.73k
                        case 2:
160
3.73k
                            shift = 1, imax = MAX_PS_UINT >> 1;
161
3.73k
                            break;
162
16.1k
                        case 4:
163
16.1k
                            shift = 2, imax = MAX_PS_UINT >> 2;
164
16.1k
                            break;
165
438
                        case 8:
166
438
                            shift = 3, imax = MAX_PS_UINT >> 3;
167
438
                            break;
168
2.67G
                        case 16:
169
2.67G
                            shift = 4, imax = MAX_PS_UINT >> 4;
170
2.67G
                            break;
171
824
                        case 32:
172
824
                            shift = 5, imax = MAX_PS_UINT >> 5;
173
824
                            break;
174
0
                        default:  /* can't happen */
175
0
                            return_error(gs_error_rangecheck);
176
2.67G
                    }
177
12.7G
                    for (;; uval = (uval << shift) + d) {
178
12.7G
                        GET_NEXT(c, sp, break);
179
11.4G
                        d = decoder[c];
180
11.4G
                        if (d >= radix) {
181
1.35G
                            *psp = sp;
182
1.35G
                            code = 1;
183
1.35G
                            break;
184
1.35G
                        }
185
10.0G
                        if (uval > imax)
186
29
                            return_error(gs_error_limitcheck);
187
10.0G
                    }
188
2.67G
                } else {
189
8.89k
                    ps_int irem = MAX_PS_UINT % radix;
190
191
8.89k
                    imax = MAX_PS_UINT / radix;
192
20.4k
                    for (;; uval = uval * radix + d) {
193
20.4k
                        GET_NEXT(c, sp, break);
194
16.6k
                        d = decoder[c];
195
16.6k
                        if (d >= radix) {
196
5.08k
                            *psp = sp;
197
5.08k
                            code = 1;
198
5.08k
                            break;
199
5.08k
                        }
200
11.6k
                        if (uval >= imax &&
201
87
                            (uval > imax || d > irem)
202
11.6k
                            )
203
87
                            return_error(gs_error_limitcheck);
204
11.6k
                    }
205
8.89k
                }
206
2.67G
                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.67G
                else
212
2.67G
                    make_int(pref, uval);
213
214
2.67G
                return code;
215
2.67G
            }
216
3.53G
    }
217
921M
iret:
218
921M
    if (scanner_options & SCAN_CPSI_MODE) {
219
0
        make_int(pref, (sign < 0 ? (ps_int32)-ival : (ps_int32)ival));
220
0
    }
221
921M
    else {
222
921M
        make_int(pref, (sign < 0 ? (ps_int)-ival : (ps_int)ival));
223
921M
    }
224
921M
    return code;
225
226
    /* Accumulate a double in dval. */
227
35.5k
l2d:
228
35.5k
    exp10 = 0;
229
359k
    for (;;) {
230
359k
        dval = dval * 10 + d;
231
359k
        GET_NEXT(c, sp, c = EOFC);
232
359k
        if (!IS_DIGIT(d, c))
233
35.5k
            break;
234
359k
    }
235
35.5k
    switch (c) {
236
1.67k
        case '.':
237
1.67k
            GET_NEXT(c, sp, c = EOFC);
238
1.67k
            exp10 = 0;
239
1.67k
            goto fd;
240
23.8k
        default:
241
23.8k
            *psp = sp;
242
23.8k
            code = 1;
243
            /* falls through */
244
29.1k
        case EOFC:
245
29.1k
            if (sign < 0)
246
7.79k
                dval = -dval;
247
29.1k
            goto rret;
248
1.21k
        case 'e':
249
2.52k
        case 'E':
250
2.52k
            exp10 = 0;
251
2.52k
            goto fs;
252
2.23k
        case '#':
253
2.23k
            return_error(gs_error_syntaxerror);
254
35.5k
    }
255
256
    /* We saw a '.' while accumulating an integer in ival. */
257
23.9M
i2r:
258
23.9M
    exp10 = 0;
259
90.4M
    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
66.5M
        if (c == '-') {
268
554
            if ((SCAN_PDF_INV_NUM & scanner_options) == 0)
269
554
                break;
270
0
            do {
271
0
                GET_NEXT(c, sp, c = EOFC);
272
0
            } while (IS_DIGIT(d, c));
273
0
            break;
274
554
        }
275
66.5M
        if (WOULD_OVERFLOW(ival, d, max_int)) {
276
77.3k
            dval = (double)ival;
277
77.3k
            goto fd;
278
77.3k
        }
279
66.5M
        ival = ival * 10 + d;
280
66.5M
        exp10--;
281
66.5M
        GET_NEXT(c, sp, c = EOFC);
282
66.5M
    }
283
23.8M
    if (sign < 0)
284
1.92M
        ival = -ival;
285
    /* Take a shortcut for the common case */
286
23.8M
    if (!(c == 'e' || c == 'E' || exp10 < -NUM_POWERS_10)) { /* Check for trailing garbage */
287
23.8M
        if (c != EOFC)
288
20.4M
            *psp = sp, code = 1;
289
23.8M
        make_real(pref, ival * neg_powers_10[-exp10]);
290
23.8M
        return code;
291
23.8M
    }
292
16.8k
    dval = (double)ival;
293
16.8k
    goto fe;
294
295
    /* Now we are accumulating a double in dval. */
296
79.0k
fd:
297
658k
    while (IS_DIGIT(d, c)) {
298
579k
        dval = dval * 10 + d;
299
579k
        exp10--;
300
579k
        GET_NEXT(c, sp, c = EOFC);
301
579k
    }
302
81.5k
fs:
303
81.5k
    if (sign < 0)
304
27.5k
        dval = -dval;
305
281k
fe:
306
    /* Now dval contains the value, negated if necessary. */
307
281k
    switch (c) {
308
181k
        case 'e':
309
198k
        case 'E':
310
198k
            {     /* Check for a following exponent. */
311
198k
                int esign = 0;
312
198k
                int iexp;
313
314
198k
                GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
315
122k
                switch (c) {
316
12.4k
                    case '-':
317
12.4k
                        esign = 1;
318
                        /* fall through */
319
17.9k
                    case '+':
320
17.9k
                        GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
321
122k
                }
322
                /* Scan the exponent.  We limit it arbitrarily to 999. */
323
120k
                if (!IS_DIGIT(d, c))
324
82.3k
                    return_error(gs_error_syntaxerror);
325
38.5k
                iexp = d;
326
60.0k
                for (;; iexp = iexp * 10 + d) {
327
60.0k
                    GET_NEXT(c, sp, break);
328
52.6k
                    if (!IS_DIGIT(d, c)) {
329
31.0k
                        *psp = sp;
330
31.0k
                        code = 1;
331
31.0k
                        break;
332
31.0k
                    }
333
21.5k
                    if (iexp > 99)
334
82
                        return_error(gs_error_limitcheck);
335
21.5k
                }
336
38.4k
                if (esign)
337
12.1k
                    exp10 -= iexp;
338
26.2k
                else
339
26.2k
                    exp10 += iexp;
340
38.4k
                break;
341
38.5k
            }
342
78.7k
        default:
343
78.7k
            *psp = sp;
344
78.7k
            code = 1;
345
82.6k
        case EOFC:
346
82.6k
            ;
347
281k
    }
348
    /* Compute dval * 10^exp10. */
349
121k
    if (exp10 > 0) {
350
47.8k
        while (exp10 > NUM_POWERS_10)
351
27.3k
            dval *= powers_10[NUM_POWERS_10],
352
27.3k
                exp10 -= NUM_POWERS_10;
353
20.5k
        dval *= powers_10[exp10];
354
100k
    } else if (exp10 < 0) {
355
546k
        while (exp10 < -NUM_POWERS_10)
356
450k
            dval /= powers_10[NUM_POWERS_10],
357
450k
                exp10 += NUM_POWERS_10;
358
95.9k
        dval /= powers_10[-exp10];
359
95.9k
    }
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
121k
    if (dval >= 0) {
366
87.1k
        if (dval > MAX_FLOAT)
367
133
            return_error(gs_error_limitcheck);
368
87.1k
    } else {
369
33.8k
        if (dval < -MAX_FLOAT)
370
47
            return_error(gs_error_limitcheck);
371
33.8k
    }
372
150k
rret:
373
150k
    make_real(pref, dval);
374
150k
    return code;
375
121k
}