Coverage Report

Created: 2026-04-09 07:06

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.13G
{
41
4.13G
    const byte *sp = str;
42
4.13G
#define GET_NEXT(cvar, sp, end_action)\
43
16.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
4.13G
#define NUM_POWERS_10 6
50
4.13G
    static const float powers_10[NUM_POWERS_10 + 1] = {
51
4.13G
        1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6
52
4.13G
    };
53
4.13G
    static const double neg_powers_10[NUM_POWERS_10 + 1] = {
54
4.13G
        1e0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6
55
4.13G
    };
56
57
4.13G
    ps_int ival;
58
4.13G
    double dval;
59
4.13G
    int exp10;
60
4.13G
    int code = 0;
61
4.13G
    int c, d;
62
4.13G
    ps_int max_ps_int_scan, min_ps_int_scan;
63
4.13G
    const byte *const decoder = scan_char_decoder;
64
4.13G
#define IS_DIGIT(d, c)\
65
11.1G
  ((d = decoder[c]) < 10)
66
4.13G
#define WOULD_OVERFLOW(val, d, maxv)\
67
4.13G
  (val >= maxv / 10 && (val > maxv / 10 || d > (int64_t)(maxv % 10)))
68
69
4.13G
    GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
70
4.13G
    if (!IS_DIGIT(d, c)) {
71
817M
        if (c != '.')
72
281k
            return_error(gs_error_syntaxerror);
73
        /* Might be a number starting with '.'. */
74
817M
        GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
75
817M
        if (!IS_DIGIT(d, c))
76
817M
            return_error(gs_error_syntaxerror);
77
72.1k
        ival = 0;
78
72.1k
        goto i2r;
79
817M
    }
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.31G
    ival = d;
85
3.31G
    if (end - sp >= 3) { /* just check once */
86
3.24G
        if (!IS_DIGIT(d, (c = *sp))) {
87
672M
            sp++;
88
672M
            goto ind;
89
672M
        }
90
2.57G
        ival = ival * 10 + d;
91
2.57G
        if (!IS_DIGIT(d, (c = sp[1]))) {
92
2.48G
            sp += 2;
93
2.48G
            goto ind;
94
2.48G
        }
95
86.0M
        ival = ival * 10 + d;
96
86.0M
        sp += 3;
97
86.0M
        if (!IS_DIGIT(d, (c = sp[-1])))
98
48.9M
            goto ind;
99
37.1M
        ival = ival * 10 + d;
100
37.1M
    }
101
102
111M
    max_ps_int_scan = scanner_options & SCAN_CPSI_MODE ? MAX_PS_INT32 : MAX_PS_INT;
103
111M
    min_ps_int_scan = scanner_options & SCAN_CPSI_MODE ? MIN_PS_INT32 : MIN_PS_INT;
104
105
167M
    for (;; ival = ival * 10 + d) {
106
167M
        GET_NEXT(c, sp, goto iret);
107
88.3M
        if (!IS_DIGIT(d, c))
108
32.1M
            break;
109
56.1M
        if (WOULD_OVERFLOW(((ps_uint)ival), d, max_ps_int_scan)) {
110
41.4k
            if (ival == max_ps_int_scan / 10 && d == (max_ps_int_scan % 10) + 1 && sign < 0) {
111
4.52k
                GET_NEXT(c, sp, c = EOFC);
112
4.52k
                dval = -(double)min_ps_int_scan;
113
4.52k
                if (c == 'e' || c == 'E') {
114
17
                    exp10 = 0;
115
17
                    goto fs;
116
4.50k
                } else if (c == '.') {
117
44
                    GET_NEXT(c, sp, c = EOFC);
118
44
                    exp10 = 0;
119
44
                    goto fd;
120
4.46k
                } else if (!IS_DIGIT(d, c)) {
121
4.43k
                    ival = min_ps_int_scan;
122
4.43k
                    break;
123
4.43k
                }
124
4.52k
            } else
125
36.8k
                dval = (double)ival;
126
36.9k
            goto l2d;
127
41.4k
        }
128
56.1M
    }
129
3.23G
  ind:        /* We saw a non-digit while accumulating an integer in ival. */
130
3.23G
    switch (c) {
131
22.7M
        case '.':
132
22.7M
            GET_NEXT(c, sp, c = EOFC);
133
22.7M
            goto i2r;
134
782M
        default:
135
782M
            *psp = sp;
136
782M
            code = 1;
137
782M
            break;
138
124
        case EOFC:
139
124
            break;
140
167k
        case 'e':
141
180k
        case 'E':
142
180k
            if (sign < 0)
143
4.23k
                ival = -ival;
144
180k
            dval = (double)ival;
145
180k
            exp10 = 0;
146
180k
            goto fe;
147
2.43G
        case '#':
148
2.43G
            {
149
2.43G
                const int radix = ival;
150
2.43G
                ps_int uval = 0, imax;
151
152
2.43G
                if (sign || radix < min_radix || radix > max_radix)
153
9.29k
                    return_error(gs_error_syntaxerror);
154
                /* Avoid multiplies for power-of-2 radix. */
155
2.43G
                if (!(radix & (radix - 1))) {
156
2.43G
                    int shift;
157
158
2.43G
                    switch (radix) {
159
3.49k
                        case 2:
160
3.49k
                            shift = 1, imax = MAX_PS_UINT >> 1;
161
3.49k
                            break;
162
15.5k
                        case 4:
163
15.5k
                            shift = 2, imax = MAX_PS_UINT >> 2;
164
15.5k
                            break;
165
496
                        case 8:
166
496
                            shift = 3, imax = MAX_PS_UINT >> 3;
167
496
                            break;
168
2.43G
                        case 16:
169
2.43G
                            shift = 4, imax = MAX_PS_UINT >> 4;
170
2.43G
                            break;
171
793
                        case 32:
172
793
                            shift = 5, imax = MAX_PS_UINT >> 5;
173
793
                            break;
174
0
                        default:  /* can't happen */
175
0
                            return_error(gs_error_rangecheck);
176
2.43G
                    }
177
11.5G
                    for (;; uval = (uval << shift) + d) {
178
11.5G
                        GET_NEXT(c, sp, break);
179
10.3G
                        d = decoder[c];
180
10.3G
                        if (d >= radix) {
181
1.23G
                            *psp = sp;
182
1.23G
                            code = 1;
183
1.23G
                            break;
184
1.23G
                        }
185
9.14G
                        if (uval > imax)
186
33
                            return_error(gs_error_limitcheck);
187
9.14G
                    }
188
2.43G
                } else {
189
7.55k
                    ps_int irem = MAX_PS_UINT % radix;
190
191
7.55k
                    imax = MAX_PS_UINT / radix;
192
17.1k
                    for (;; uval = uval * radix + d) {
193
17.1k
                        GET_NEXT(c, sp, break);
194
14.0k
                        d = decoder[c];
195
14.0k
                        if (d >= radix) {
196
4.33k
                            *psp = sp;
197
4.33k
                            code = 1;
198
4.33k
                            break;
199
4.33k
                        }
200
9.68k
                        if (uval >= imax &&
201
76
                            (uval > imax || d > irem)
202
9.68k
                            )
203
76
                            return_error(gs_error_limitcheck);
204
9.68k
                    }
205
7.55k
                }
206
2.43G
                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.43G
                else
212
2.43G
                    make_int(pref, uval);
213
214
2.43G
                return code;
215
2.43G
            }
216
3.23G
    }
217
861M
iret:
218
861M
    if (scanner_options & SCAN_CPSI_MODE) {
219
0
        make_int(pref, (sign < 0 ? (ps_int32)-ival : (ps_int32)ival));
220
0
    }
221
861M
    else {
222
861M
        make_int(pref, (sign < 0 ? (ps_int)-ival : (ps_int)ival));
223
861M
    }
224
861M
    return code;
225
226
    /* Accumulate a double in dval. */
227
36.9k
l2d:
228
36.9k
    exp10 = 0;
229
362k
    for (;;) {
230
362k
        dval = dval * 10 + d;
231
362k
        GET_NEXT(c, sp, c = EOFC);
232
362k
        if (!IS_DIGIT(d, c))
233
36.9k
            break;
234
362k
    }
235
36.9k
    switch (c) {
236
1.29k
        case '.':
237
1.29k
            GET_NEXT(c, sp, c = EOFC);
238
1.29k
            exp10 = 0;
239
1.29k
            goto fd;
240
26.1k
        default:
241
26.1k
            *psp = sp;
242
26.1k
            code = 1;
243
            /* falls through */
244
31.3k
        case EOFC:
245
31.3k
            if (sign < 0)
246
7.43k
                dval = -dval;
247
31.3k
            goto rret;
248
1.49k
        case 'e':
249
2.48k
        case 'E':
250
2.48k
            exp10 = 0;
251
2.48k
            goto fs;
252
1.82k
        case '#':
253
1.82k
            return_error(gs_error_syntaxerror);
254
36.9k
    }
255
256
    /* We saw a '.' while accumulating an integer in ival. */
257
22.8M
i2r:
258
22.8M
    exp10 = 0;
259
88.6M
    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
65.8M
        if (c == '-') {
268
470
            if ((SCAN_PDF_INV_NUM & scanner_options) == 0)
269
470
                break;
270
0
            do {
271
0
                GET_NEXT(c, sp, c = EOFC);
272
0
            } while (IS_DIGIT(d, c));
273
0
            break;
274
470
        }
275
65.8M
        if (WOULD_OVERFLOW(ival, d, max_int)) {
276
50.0k
            dval = (double)ival;
277
50.0k
            goto fd;
278
50.0k
        }
279
65.7M
        ival = ival * 10 + d;
280
65.7M
        exp10--;
281
65.7M
        GET_NEXT(c, sp, c = EOFC);
282
65.7M
    }
283
22.7M
    if (sign < 0)
284
1.80M
        ival = -ival;
285
    /* Take a shortcut for the common case */
286
22.7M
    if (!(c == 'e' || c == 'E' || exp10 < -NUM_POWERS_10)) { /* Check for trailing garbage */
287
22.7M
        if (c != EOFC)
288
19.6M
            *psp = sp, code = 1;
289
22.7M
        make_real(pref, ival * neg_powers_10[-exp10]);
290
22.7M
        return code;
291
22.7M
    }
292
19.4k
    dval = (double)ival;
293
19.4k
    goto fe;
294
295
    /* Now we are accumulating a double in dval. */
296
51.3k
fd:
297
450k
    while (IS_DIGIT(d, c)) {
298
399k
        dval = dval * 10 + d;
299
399k
        exp10--;
300
399k
        GET_NEXT(c, sp, c = EOFC);
301
399k
    }
302
53.8k
fs:
303
53.8k
    if (sign < 0)
304
14.5k
        dval = -dval;
305
253k
fe:
306
    /* Now dval contains the value, negated if necessary. */
307
253k
    switch (c) {
308
178k
        case 'e':
309
195k
        case 'E':
310
195k
            {     /* Check for a following exponent. */
311
195k
                int esign = 0;
312
195k
                int iexp;
313
314
195k
                GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
315
119k
                switch (c) {
316
9.14k
                    case '-':
317
9.14k
                        esign = 1;
318
                        /* fall through */
319
14.1k
                    case '+':
320
14.1k
                        GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
321
119k
                }
322
                /* Scan the exponent.  We limit it arbitrarily to 999. */
323
117k
                if (!IS_DIGIT(d, c))
324
82.3k
                    return_error(gs_error_syntaxerror);
325
34.9k
                iexp = d;
326
53.2k
                for (;; iexp = iexp * 10 + d) {
327
53.2k
                    GET_NEXT(c, sp, break);
328
46.5k
                    if (!IS_DIGIT(d, c)) {
329
28.2k
                        *psp = sp;
330
28.2k
                        code = 1;
331
28.2k
                        break;
332
28.2k
                    }
333
18.3k
                    if (iexp > 99)
334
65
                        return_error(gs_error_limitcheck);
335
18.3k
                }
336
34.8k
                if (esign)
337
8.86k
                    exp10 -= iexp;
338
26.0k
                else
339
26.0k
                    exp10 += iexp;
340
34.8k
                break;
341
34.9k
            }
342
55.2k
        default:
343
55.2k
            *psp = sp;
344
55.2k
            code = 1;
345
58.4k
        case EOFC:
346
58.4k
            ;
347
253k
    }
348
    /* Compute dval * 10^exp10. */
349
93.3k
    if (exp10 > 0) {
350
48.2k
        while (exp10 > NUM_POWERS_10)
351
26.9k
            dval *= powers_10[NUM_POWERS_10],
352
26.9k
                exp10 -= NUM_POWERS_10;
353
21.3k
        dval *= powers_10[exp10];
354
71.9k
    } else if (exp10 < 0) {
355
483k
        while (exp10 < -NUM_POWERS_10)
356
415k
            dval /= powers_10[NUM_POWERS_10],
357
415k
                exp10 += NUM_POWERS_10;
358
68.1k
        dval /= powers_10[-exp10];
359
68.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
93.3k
    if (dval >= 0) {
366
73.8k
        if (dval > MAX_FLOAT)
367
116
            return_error(gs_error_limitcheck);
368
73.8k
    } else {
369
19.5k
        if (dval < -MAX_FLOAT)
370
45
            return_error(gs_error_limitcheck);
371
19.5k
    }
372
124k
rret:
373
124k
    make_real(pref, dval);
374
124k
    return code;
375
93.3k
}