Coverage Report

Created: 2025-06-10 06:56

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