Coverage Report

Created: 2026-04-01 07:17

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
5.03G
{
41
5.03G
    const byte *sp = str;
42
5.03G
#define GET_NEXT(cvar, sp, end_action)\
43
20.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
5.03G
#define NUM_POWERS_10 6
50
5.03G
    static const float powers_10[NUM_POWERS_10 + 1] = {
51
5.03G
        1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6
52
5.03G
    };
53
5.03G
    static const double neg_powers_10[NUM_POWERS_10 + 1] = {
54
5.03G
        1e0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6
55
5.03G
    };
56
57
5.03G
    ps_int ival;
58
5.03G
    double dval;
59
5.03G
    int exp10;
60
5.03G
    int code = 0;
61
5.03G
    int c, d;
62
5.03G
    ps_int max_ps_int_scan, min_ps_int_scan;
63
5.03G
    const byte *const decoder = scan_char_decoder;
64
5.03G
#define IS_DIGIT(d, c)\
65
13.5G
  ((d = decoder[c]) < 10)
66
5.03G
#define WOULD_OVERFLOW(val, d, maxv)\
67
5.03G
  (val >= maxv / 10 && (val > maxv / 10 || d > (int64_t)(maxv % 10)))
68
69
5.03G
    GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
70
5.03G
    if (!IS_DIGIT(d, c)) {
71
987M
        if (c != '.')
72
502k
            return_error(gs_error_syntaxerror);
73
        /* Might be a number starting with '.'. */
74
987M
        GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
75
987M
        if (!IS_DIGIT(d, c))
76
987M
            return_error(gs_error_syntaxerror);
77
81.0k
        ival = 0;
78
81.0k
        goto i2r;
79
987M
    }
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
4.04G
    ival = d;
85
4.04G
    if (end - sp >= 3) { /* just check once */
86
3.95G
        if (!IS_DIGIT(d, (c = *sp))) {
87
810M
            sp++;
88
810M
            goto ind;
89
810M
        }
90
3.14G
        ival = ival * 10 + d;
91
3.14G
        if (!IS_DIGIT(d, (c = sp[1]))) {
92
3.04G
            sp += 2;
93
3.04G
            goto ind;
94
3.04G
        }
95
104M
        ival = ival * 10 + d;
96
104M
        sp += 3;
97
104M
        if (!IS_DIGIT(d, (c = sp[-1])))
98
59.6M
            goto ind;
99
44.7M
        ival = ival * 10 + d;
100
44.7M
    }
101
102
135M
    max_ps_int_scan = scanner_options & SCAN_CPSI_MODE ? MAX_PS_INT32 : MAX_PS_INT;
103
135M
    min_ps_int_scan = scanner_options & SCAN_CPSI_MODE ? MIN_PS_INT32 : MIN_PS_INT;
104
105
204M
    for (;; ival = ival * 10 + d) {
106
204M
        GET_NEXT(c, sp, goto iret);
107
107M
        if (!IS_DIGIT(d, c))
108
38.6M
            break;
109
69.3M
        if (WOULD_OVERFLOW(((ps_uint)ival), d, max_ps_int_scan)) {
110
51.5k
            if (ival == max_ps_int_scan / 10 && d == (max_ps_int_scan % 10) + 1 && sign < 0) {
111
5.69k
                GET_NEXT(c, sp, c = EOFC);
112
5.69k
                dval = -(double)min_ps_int_scan;
113
5.69k
                if (c == 'e' || c == 'E') {
114
22
                    exp10 = 0;
115
22
                    goto fs;
116
5.67k
                } else if (c == '.') {
117
71
                    GET_NEXT(c, sp, c = EOFC);
118
71
                    exp10 = 0;
119
71
                    goto fd;
120
5.60k
                } else if (!IS_DIGIT(d, c)) {
121
5.55k
                    ival = min_ps_int_scan;
122
5.55k
                    break;
123
5.55k
                }
124
5.69k
            } else
125
45.8k
                dval = (double)ival;
126
45.8k
            goto l2d;
127
51.5k
        }
128
69.3M
    }
129
3.94G
  ind:        /* We saw a non-digit while accumulating an integer in ival. */
130
3.94G
    switch (c) {
131
26.4M
        case '.':
132
26.4M
            GET_NEXT(c, sp, c = EOFC);
133
26.4M
            goto i2r;
134
945M
        default:
135
945M
            *psp = sp;
136
945M
            code = 1;
137
945M
            break;
138
162
        case EOFC:
139
162
            break;
140
340k
        case 'e':
141
356k
        case 'E':
142
356k
            if (sign < 0)
143
5.78k
                ival = -ival;
144
356k
            dval = (double)ival;
145
356k
            exp10 = 0;
146
356k
            goto fe;
147
2.97G
        case '#':
148
2.97G
            {
149
2.97G
                const int radix = ival;
150
2.97G
                ps_int uval = 0, imax;
151
152
2.97G
                if (sign || radix < min_radix || radix > max_radix)
153
12.1k
                    return_error(gs_error_syntaxerror);
154
                /* Avoid multiplies for power-of-2 radix. */
155
2.97G
                if (!(radix & (radix - 1))) {
156
2.97G
                    int shift;
157
158
2.97G
                    switch (radix) {
159
4.32k
                        case 2:
160
4.32k
                            shift = 1, imax = MAX_PS_UINT >> 1;
161
4.32k
                            break;
162
16.3k
                        case 4:
163
16.3k
                            shift = 2, imax = MAX_PS_UINT >> 2;
164
16.3k
                            break;
165
747
                        case 8:
166
747
                            shift = 3, imax = MAX_PS_UINT >> 3;
167
747
                            break;
168
2.97G
                        case 16:
169
2.97G
                            shift = 4, imax = MAX_PS_UINT >> 4;
170
2.97G
                            break;
171
1.04k
                        case 32:
172
1.04k
                            shift = 5, imax = MAX_PS_UINT >> 5;
173
1.04k
                            break;
174
0
                        default:  /* can't happen */
175
0
                            return_error(gs_error_rangecheck);
176
2.97G
                    }
177
14.1G
                    for (;; uval = (uval << shift) + d) {
178
14.1G
                        GET_NEXT(c, sp, break);
179
12.7G
                        d = decoder[c];
180
12.7G
                        if (d >= radix) {
181
1.53G
                            *psp = sp;
182
1.53G
                            code = 1;
183
1.53G
                            break;
184
1.53G
                        }
185
11.1G
                        if (uval > imax)
186
45
                            return_error(gs_error_limitcheck);
187
11.1G
                    }
188
2.97G
                } else {
189
9.04k
                    ps_int irem = MAX_PS_UINT % radix;
190
191
9.04k
                    imax = MAX_PS_UINT / radix;
192
20.4k
                    for (;; uval = uval * radix + d) {
193
20.4k
                        GET_NEXT(c, sp, break);
194
16.7k
                        d = decoder[c];
195
16.7k
                        if (d >= radix) {
196
5.22k
                            *psp = sp;
197
5.22k
                            code = 1;
198
5.22k
                            break;
199
5.22k
                        }
200
11.5k
                        if (uval >= imax &&
201
103
                            (uval > imax || d > irem)
202
11.5k
                            )
203
103
                            return_error(gs_error_limitcheck);
204
11.5k
                    }
205
9.04k
                }
206
2.97G
                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.97G
                else
212
2.97G
                    make_int(pref, uval);
213
214
2.97G
                return code;
215
2.97G
            }
216
3.94G
    }
217
1.04G
iret:
218
1.04G
    if (scanner_options & SCAN_CPSI_MODE) {
219
0
        make_int(pref, (sign < 0 ? (ps_int32)-ival : (ps_int32)ival));
220
0
    }
221
1.04G
    else {
222
1.04G
        make_int(pref, (sign < 0 ? (ps_int)-ival : (ps_int)ival));
223
1.04G
    }
224
1.04G
    return code;
225
226
    /* Accumulate a double in dval. */
227
45.8k
l2d:
228
45.8k
    exp10 = 0;
229
444k
    for (;;) {
230
444k
        dval = dval * 10 + d;
231
444k
        GET_NEXT(c, sp, c = EOFC);
232
444k
        if (!IS_DIGIT(d, c))
233
45.8k
            break;
234
444k
    }
235
45.8k
    switch (c) {
236
1.84k
        case '.':
237
1.84k
            GET_NEXT(c, sp, c = EOFC);
238
1.84k
            exp10 = 0;
239
1.84k
            goto fd;
240
31.7k
        default:
241
31.7k
            *psp = sp;
242
31.7k
            code = 1;
243
            /* falls through */
244
38.7k
        case EOFC:
245
38.7k
            if (sign < 0)
246
9.46k
                dval = -dval;
247
38.7k
            goto rret;
248
1.68k
        case 'e':
249
2.82k
        case 'E':
250
2.82k
            exp10 = 0;
251
2.82k
            goto fs;
252
2.48k
        case '#':
253
2.48k
            return_error(gs_error_syntaxerror);
254
45.8k
    }
255
256
    /* We saw a '.' while accumulating an integer in ival. */
257
26.5M
i2r:
258
26.5M
    exp10 = 0;
259
100M
    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
74.1M
        if (c == '-') {
268
593
            if ((SCAN_PDF_INV_NUM & scanner_options) == 0)
269
593
                break;
270
0
            do {
271
0
                GET_NEXT(c, sp, c = EOFC);
272
0
            } while (IS_DIGIT(d, c));
273
0
            break;
274
593
        }
275
74.1M
        if (WOULD_OVERFLOW(ival, d, max_int)) {
276
55.1k
            dval = (double)ival;
277
55.1k
            goto fd;
278
55.1k
        }
279
74.1M
        ival = ival * 10 + d;
280
74.1M
        exp10--;
281
74.1M
        GET_NEXT(c, sp, c = EOFC);
282
74.1M
    }
283
26.4M
    if (sign < 0)
284
2.12M
        ival = -ival;
285
    /* Take a shortcut for the common case */
286
26.4M
    if (!(c == 'e' || c == 'E' || exp10 < -NUM_POWERS_10)) { /* Check for trailing garbage */
287
26.4M
        if (c != EOFC)
288
22.7M
            *psp = sp, code = 1;
289
26.4M
        make_real(pref, ival * neg_powers_10[-exp10]);
290
26.4M
        return code;
291
26.4M
    }
292
22.2k
    dval = (double)ival;
293
22.2k
    goto fe;
294
295
    /* Now we are accumulating a double in dval. */
296
57.0k
fd:
297
503k
    while (IS_DIGIT(d, c)) {
298
446k
        dval = dval * 10 + d;
299
446k
        exp10--;
300
446k
        GET_NEXT(c, sp, c = EOFC);
301
446k
    }
302
59.8k
fs:
303
59.8k
    if (sign < 0)
304
15.7k
        dval = -dval;
305
438k
fe:
306
    /* Now dval contains the value, negated if necessary. */
307
438k
    switch (c) {
308
353k
        case 'e':
309
373k
        case 'E':
310
373k
            {     /* Check for a following exponent. */
311
373k
                int esign = 0;
312
373k
                int iexp;
313
314
373k
                GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
315
214k
                switch (c) {
316
12.6k
                    case '-':
317
12.6k
                        esign = 1;
318
                        /* fall through */
319
18.8k
                    case '+':
320
18.8k
                        GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
321
214k
                }
322
                /* Scan the exponent.  We limit it arbitrarily to 999. */
323
212k
                if (!IS_DIGIT(d, c))
324
167k
                    return_error(gs_error_syntaxerror);
325
44.7k
                iexp = d;
326
67.8k
                for (;; iexp = iexp * 10 + d) {
327
67.8k
                    GET_NEXT(c, sp, break);
328
58.4k
                    if (!IS_DIGIT(d, c)) {
329
35.3k
                        *psp = sp;
330
35.3k
                        code = 1;
331
35.3k
                        break;
332
35.3k
                    }
333
23.1k
                    if (iexp > 99)
334
99
                        return_error(gs_error_limitcheck);
335
23.1k
                }
336
44.6k
                if (esign)
337
12.2k
                    exp10 -= iexp;
338
32.3k
                else
339
32.3k
                    exp10 += iexp;
340
44.6k
                break;
341
44.7k
            }
342
60.6k
        default:
343
60.6k
            *psp = sp;
344
60.6k
            code = 1;
345
64.6k
        case EOFC:
346
64.6k
            ;
347
438k
    }
348
    /* Compute dval * 10^exp10. */
349
109k
    if (exp10 > 0) {
350
61.5k
        while (exp10 > NUM_POWERS_10)
351
35.5k
            dval *= powers_10[NUM_POWERS_10],
352
35.5k
                exp10 -= NUM_POWERS_10;
353
25.9k
        dval *= powers_10[exp10];
354
83.3k
    } else if (exp10 < 0) {
355
563k
        while (exp10 < -NUM_POWERS_10)
356
485k
            dval /= powers_10[NUM_POWERS_10],
357
485k
                exp10 += NUM_POWERS_10;
358
78.1k
        dval /= powers_10[-exp10];
359
78.1k
    }
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
109k
    if (dval >= 0) {
366
86.8k
        if (dval > MAX_FLOAT)
367
161
            return_error(gs_error_limitcheck);
368
86.8k
    } else {
369
22.5k
        if (dval < -MAX_FLOAT)
370
61
            return_error(gs_error_limitcheck);
371
22.5k
    }
372
147k
rret:
373
147k
    make_real(pref, dval);
374
147k
    return code;
375
109k
}