Coverage Report

Created: 2025-11-16 07:40

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