Coverage Report

Created: 2025-06-10 06:59

/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
248M
{
41
248M
    const byte *sp = str;
42
248M
#define GET_NEXT(cvar, sp, end_action)\
43
1.00G
  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
248M
#define NUM_POWERS_10 6
50
248M
    static const float powers_10[NUM_POWERS_10 + 1] = {
51
248M
        1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6
52
248M
    };
53
248M
    static const double neg_powers_10[NUM_POWERS_10 + 1] = {
54
248M
        1e0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6
55
248M
    };
56
57
248M
    ps_int ival;
58
248M
    double dval;
59
248M
    int exp10;
60
248M
    int code = 0;
61
248M
    int c, d;
62
248M
    ps_int max_ps_int_scan, min_ps_int_scan;
63
248M
    const byte *const decoder = scan_char_decoder;
64
248M
#define IS_DIGIT(d, c)\
65
665M
  ((d = decoder[c]) < 10)
66
248M
#define WOULD_OVERFLOW(val, d, maxv)\
67
248M
  (val >= maxv / 10 && (val > maxv / 10 || d > (int64_t)(maxv % 10)))
68
69
248M
    GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
70
248M
    if (!IS_DIGIT(d, c)) {
71
49.7M
        if (c != '.')
72
4.21k
            return_error(gs_error_syntaxerror);
73
        /* Might be a number starting with '.'. */
74
49.7M
        GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
75
49.7M
        if (!IS_DIGIT(d, c))
76
49.7M
            return_error(gs_error_syntaxerror);
77
4.66k
        ival = 0;
78
4.66k
        goto i2r;
79
49.7M
    }
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
198M
    ival = d;
85
198M
    if (end - sp >= 3) { /* just check once */
86
194M
        if (!IS_DIGIT(d, (c = *sp))) {
87
41.3M
            sp++;
88
41.3M
            goto ind;
89
41.3M
        }
90
153M
        ival = ival * 10 + d;
91
153M
        if (!IS_DIGIT(d, (c = sp[1]))) {
92
147M
            sp += 2;
93
147M
            goto ind;
94
147M
        }
95
5.05M
        ival = ival * 10 + d;
96
5.05M
        sp += 3;
97
5.05M
        if (!IS_DIGIT(d, (c = sp[-1])))
98
2.78M
            goto ind;
99
2.26M
        ival = ival * 10 + d;
100
2.26M
    }
101
102
6.88M
    max_ps_int_scan = scanner_options & SCAN_CPSI_MODE ? MAX_PS_INT32 : MAX_PS_INT;
103
6.88M
    min_ps_int_scan = scanner_options & SCAN_CPSI_MODE ? MIN_PS_INT32 : MIN_PS_INT;
104
105
10.3M
    for (;; ival = ival * 10 + d) {
106
10.3M
        GET_NEXT(c, sp, goto iret);
107
5.36M
        if (!IS_DIGIT(d, c))
108
1.93M
            break;
109
3.43M
        if (WOULD_OVERFLOW(((ps_uint)ival), d, max_ps_int_scan)) {
110
1.68k
            if (ival == max_ps_int_scan / 10 && d == (max_ps_int_scan % 10) + 1 && sign < 0) {
111
101
                GET_NEXT(c, sp, c = EOFC);
112
101
                dval = -(double)min_ps_int_scan;
113
101
                if (c == 'e' || c == 'E') {
114
0
                    exp10 = 0;
115
0
                    goto fs;
116
101
                } else if (c == '.') {
117
2
                    GET_NEXT(c, sp, c = EOFC);
118
2
                    exp10 = 0;
119
2
                    goto fd;
120
99
                } else if (!IS_DIGIT(d, c)) {
121
96
                    ival = min_ps_int_scan;
122
96
                    break;
123
96
                }
124
101
            } else
125
1.58k
                dval = (double)ival;
126
1.58k
            goto l2d;
127
1.68k
        }
128
3.43M
    }
129
194M
  ind:        /* We saw a non-digit while accumulating an integer in ival. */
130
194M
    switch (c) {
131
1.25M
        case '.':
132
1.25M
            GET_NEXT(c, sp, c = EOFC);
133
1.25M
            goto i2r;
134
48.0M
        default:
135
48.0M
            *psp = sp;
136
48.0M
            code = 1;
137
48.0M
            break;
138
7
        case EOFC:
139
7
            break;
140
2.60k
        case 'e':
141
3.08k
        case 'E':
142
3.08k
            if (sign < 0)
143
231
                ival = -ival;
144
3.08k
            dval = (double)ival;
145
3.08k
            exp10 = 0;
146
3.08k
            goto fe;
147
144M
        case '#':
148
144M
            {
149
144M
                const int radix = ival;
150
144M
                ps_int uval = 0, imax;
151
152
144M
                if (sign || radix < min_radix || radix > max_radix)
153
428
                    return_error(gs_error_syntaxerror);
154
                /* Avoid multiplies for power-of-2 radix. */
155
144M
                if (!(radix & (radix - 1))) {
156
144M
                    int shift;
157
158
144M
                    switch (radix) {
159
45
                        case 2:
160
45
                            shift = 1, imax = MAX_PS_UINT >> 1;
161
45
                            break;
162
1.52k
                        case 4:
163
1.52k
                            shift = 2, imax = MAX_PS_UINT >> 2;
164
1.52k
                            break;
165
20
                        case 8:
166
20
                            shift = 3, imax = MAX_PS_UINT >> 3;
167
20
                            break;
168
144M
                        case 16:
169
144M
                            shift = 4, imax = MAX_PS_UINT >> 4;
170
144M
                            break;
171
30
                        case 32:
172
30
                            shift = 5, imax = MAX_PS_UINT >> 5;
173
30
                            break;
174
0
                        default:  /* can't happen */
175
0
                            return_error(gs_error_rangecheck);
176
144M
                    }
177
688M
                    for (;; uval = (uval << shift) + d) {
178
688M
                        GET_NEXT(c, sp, break);
179
615M
                        d = decoder[c];
180
615M
                        if (d >= radix) {
181
72.3M
                            *psp = sp;
182
72.3M
                            code = 1;
183
72.3M
                            break;
184
72.3M
                        }
185
543M
                        if (uval > imax)
186
3
                            return_error(gs_error_limitcheck);
187
543M
                    }
188
144M
                } else {
189
173
                    ps_int irem = MAX_PS_UINT % radix;
190
191
173
                    imax = MAX_PS_UINT / radix;
192
620
                    for (;; uval = uval * radix + d) {
193
620
                        GET_NEXT(c, sp, break);
194
555
                        d = decoder[c];
195
555
                        if (d >= radix) {
196
105
                            *psp = sp;
197
105
                            code = 1;
198
105
                            break;
199
105
                        }
200
450
                        if (uval >= imax &&
201
450
                            (uval > imax || d > irem)
202
450
                            )
203
3
                            return_error(gs_error_limitcheck);
204
450
                    }
205
173
                }
206
144M
                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
144M
                else
212
144M
                    make_int(pref, uval);
213
214
144M
                return code;
215
144M
            }
216
194M
    }
217
52.9M
iret:
218
52.9M
    if (scanner_options & SCAN_CPSI_MODE) {
219
0
        make_int(pref, (sign < 0 ? (ps_int32)-ival : (ps_int32)ival));
220
0
    }
221
52.9M
    else {
222
52.9M
        make_int(pref, (sign < 0 ? (ps_int)-ival : (ps_int)ival));
223
52.9M
    }
224
52.9M
    return code;
225
226
    /* Accumulate a double in dval. */
227
1.58k
l2d:
228
1.58k
    exp10 = 0;
229
11.7k
    for (;;) {
230
11.7k
        dval = dval * 10 + d;
231
11.7k
        GET_NEXT(c, sp, c = EOFC);
232
11.7k
        if (!IS_DIGIT(d, c))
233
1.58k
            break;
234
11.7k
    }
235
1.58k
    switch (c) {
236
118
        case '.':
237
118
            GET_NEXT(c, sp, c = EOFC);
238
118
            exp10 = 0;
239
118
            goto fd;
240
926
        default:
241
926
            *psp = sp;
242
926
            code = 1;
243
            /* falls through */
244
1.36k
        case EOFC:
245
1.36k
            if (sign < 0)
246
699
                dval = -dval;
247
1.36k
            goto rret;
248
18
        case 'e':
249
100
        case 'E':
250
100
            exp10 = 0;
251
100
            goto fs;
252
7
        case '#':
253
7
            return_error(gs_error_syntaxerror);
254
1.58k
    }
255
256
    /* We saw a '.' while accumulating an integer in ival. */
257
1.25M
i2r:
258
1.25M
    exp10 = 0;
259
4.64M
    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.38M
        if (c == '-') {
268
5
            if ((SCAN_PDF_INV_NUM & scanner_options) == 0)
269
5
                break;
270
0
            do {
271
0
                GET_NEXT(c, sp, c = EOFC);
272
0
            } while (IS_DIGIT(d, c));
273
0
            break;
274
5
        }
275
3.38M
        if (WOULD_OVERFLOW(ival, d, max_int)) {
276
2.36k
            dval = (double)ival;
277
2.36k
            goto fd;
278
2.36k
        }
279
3.38M
        ival = ival * 10 + d;
280
3.38M
        exp10--;
281
3.38M
        GET_NEXT(c, sp, c = EOFC);
282
3.38M
    }
283
1.25M
    if (sign < 0)
284
103k
        ival = -ival;
285
    /* Take a shortcut for the common case */
286
1.25M
    if (!(c == 'e' || c == 'E' || exp10 < -NUM_POWERS_10)) { /* Check for trailing garbage */
287
1.25M
        if (c != EOFC)
288
1.06M
            *psp = sp, code = 1;
289
1.25M
        make_real(pref, ival * neg_powers_10[-exp10]);
290
1.25M
        return code;
291
1.25M
    }
292
1.49k
    dval = (double)ival;
293
1.49k
    goto fe;
294
295
    /* Now we are accumulating a double in dval. */
296
2.48k
fd:
297
20.5k
    while (IS_DIGIT(d, c)) {
298
18.0k
        dval = dval * 10 + d;
299
18.0k
        exp10--;
300
18.0k
        GET_NEXT(c, sp, c = EOFC);
301
18.0k
    }
302
2.58k
fs:
303
2.58k
    if (sign < 0)
304
809
        dval = -dval;
305
7.16k
fe:
306
    /* Now dval contains the value, negated if necessary. */
307
7.16k
    switch (c) {
308
2.66k
        case 'e':
309
3.39k
        case 'E':
310
3.39k
            {     /* Check for a following exponent. */
311
3.39k
                int esign = 0;
312
3.39k
                int iexp;
313
314
3.39k
                GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
315
3.27k
                switch (c) {
316
777
                    case '-':
317
777
                        esign = 1;
318
                        /* fall through */
319
1.27k
                    case '+':
320
1.27k
                        GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
321
3.27k
                }
322
                /* Scan the exponent.  We limit it arbitrarily to 999. */
323
3.00k
                if (!IS_DIGIT(d, c))
324
386
                    return_error(gs_error_syntaxerror);
325
2.62k
                iexp = d;
326
3.64k
                for (;; iexp = iexp * 10 + d) {
327
3.64k
                    GET_NEXT(c, sp, break);
328
2.71k
                    if (!IS_DIGIT(d, c)) {
329
1.69k
                        *psp = sp;
330
1.69k
                        code = 1;
331
1.69k
                        break;
332
1.69k
                    }
333
1.02k
                    if (iexp > 99)
334
3
                        return_error(gs_error_limitcheck);
335
1.02k
                }
336
2.61k
                if (esign)
337
749
                    exp10 -= iexp;
338
1.86k
                else
339
1.86k
                    exp10 += iexp;
340
2.61k
                break;
341
2.62k
            }
342
3.14k
        default:
343
3.14k
            *psp = sp;
344
3.14k
            code = 1;
345
3.76k
        case EOFC:
346
3.76k
            ;
347
7.16k
    }
348
    /* Compute dval * 10^exp10. */
349
6.38k
    if (exp10 > 0) {
350
3.81k
        while (exp10 > NUM_POWERS_10)
351
2.21k
            dval *= powers_10[NUM_POWERS_10],
352
2.21k
                exp10 -= NUM_POWERS_10;
353
1.60k
        dval *= powers_10[exp10];
354
4.78k
    } else if (exp10 < 0) {
355
16.1k
        while (exp10 < -NUM_POWERS_10)
356
11.6k
            dval /= powers_10[NUM_POWERS_10],
357
11.6k
                exp10 += NUM_POWERS_10;
358
4.43k
        dval /= powers_10[-exp10];
359
4.43k
    }
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
6.38k
    if (dval >= 0) {
366
5.23k
        if (dval > MAX_FLOAT)
367
13
            return_error(gs_error_limitcheck);
368
5.23k
    } else {
369
1.14k
        if (dval < -MAX_FLOAT)
370
3
            return_error(gs_error_limitcheck);
371
1.14k
    }
372
7.73k
rret:
373
7.73k
    make_real(pref, dval);
374
7.73k
    return code;
375
6.38k
}