Coverage Report

Created: 2025-12-31 07:31

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.07G
{
41
5.07G
    const byte *sp = str;
42
5.07G
#define GET_NEXT(cvar, sp, end_action)\
43
20.6G
  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.07G
#define NUM_POWERS_10 6
50
5.07G
    static const float powers_10[NUM_POWERS_10 + 1] = {
51
5.07G
        1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6
52
5.07G
    };
53
5.07G
    static const double neg_powers_10[NUM_POWERS_10 + 1] = {
54
5.07G
        1e0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6
55
5.07G
    };
56
57
5.07G
    ps_int ival;
58
5.07G
    double dval;
59
5.07G
    int exp10;
60
5.07G
    int code = 0;
61
5.07G
    int c, d;
62
5.07G
    ps_int max_ps_int_scan, min_ps_int_scan;
63
5.07G
    const byte *const decoder = scan_char_decoder;
64
5.07G
#define IS_DIGIT(d, c)\
65
13.6G
  ((d = decoder[c]) < 10)
66
5.07G
#define WOULD_OVERFLOW(val, d, maxv)\
67
5.07G
  (val >= maxv / 10 && (val > maxv / 10 || d > (int64_t)(maxv % 10)))
68
69
5.07G
    GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
70
5.07G
    if (!IS_DIGIT(d, c)) {
71
993M
        if (c != '.')
72
716k
            return_error(gs_error_syntaxerror);
73
        /* Might be a number starting with '.'. */
74
993M
        GET_NEXT(c, sp, return_error(gs_error_syntaxerror));
75
993M
        if (!IS_DIGIT(d, c))
76
993M
            return_error(gs_error_syntaxerror);
77
97.1k
        ival = 0;
78
97.1k
        goto i2r;
79
993M
    }
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.07G
    ival = d;
85
4.07G
    if (end - sp >= 3) { /* just check once */
86
3.98G
        if (!IS_DIGIT(d, (c = *sp))) {
87
813M
            sp++;
88
813M
            goto ind;
89
813M
        }
90
3.17G
        ival = ival * 10 + d;
91
3.17G
        if (!IS_DIGIT(d, (c = sp[1]))) {
92
3.06G
            sp += 2;
93
3.06G
            goto ind;
94
3.06G
        }
95
106M
        ival = ival * 10 + d;
96
106M
        sp += 3;
97
106M
        if (!IS_DIGIT(d, (c = sp[-1])))
98
61.0M
            goto ind;
99
45.2M
        ival = ival * 10 + d;
100
45.2M
    }
101
102
136M
    max_ps_int_scan = scanner_options & SCAN_CPSI_MODE ? MAX_PS_INT32 : MAX_PS_INT;
103
136M
    min_ps_int_scan = scanner_options & SCAN_CPSI_MODE ? MIN_PS_INT32 : MIN_PS_INT;
104
105
207M
    for (;; ival = ival * 10 + d) {
106
207M
        GET_NEXT(c, sp, goto iret);
107
111M
        if (!IS_DIGIT(d, c))
108
39.7M
            break;
109
71.4M
        if (WOULD_OVERFLOW(((ps_uint)ival), d, max_ps_int_scan)) {
110
65.3k
            if (ival == max_ps_int_scan / 10 && d == (max_ps_int_scan % 10) + 1 && sign < 0) {
111
5.35k
                GET_NEXT(c, sp, c = EOFC);
112
5.35k
                dval = -(double)min_ps_int_scan;
113
5.35k
                if (c == 'e' || c == 'E') {
114
15
                    exp10 = 0;
115
15
                    goto fs;
116
5.33k
                } else if (c == '.') {
117
58
                    GET_NEXT(c, sp, c = EOFC);
118
58
                    exp10 = 0;
119
58
                    goto fd;
120
5.27k
                } else if (!IS_DIGIT(d, c)) {
121
5.25k
                    ival = min_ps_int_scan;
122
5.25k
                    break;
123
5.25k
                }
124
5.35k
            } else
125
60.0k
                dval = (double)ival;
126
60.0k
            goto l2d;
127
65.3k
        }
128
71.4M
    }
129
3.98G
  ind:        /* We saw a non-digit while accumulating an integer in ival. */
130
3.98G
    switch (c) {
131
27.3M
        case '.':
132
27.3M
            GET_NEXT(c, sp, c = EOFC);
133
27.3M
            goto i2r;
134
949M
        default:
135
949M
            *psp = sp;
136
949M
            code = 1;
137
949M
            break;
138
162
        case EOFC:
139
162
            break;
140
409k
        case 'e':
141
422k
        case 'E':
142
422k
            if (sign < 0)
143
3.57k
                ival = -ival;
144
422k
            dval = (double)ival;
145
422k
            exp10 = 0;
146
422k
            goto fe;
147
3.00G
        case '#':
148
3.00G
            {
149
3.00G
                const int radix = ival;
150
3.00G
                ps_int uval = 0, imax;
151
152
3.00G
                if (sign || radix < min_radix || radix > max_radix)
153
16.3k
                    return_error(gs_error_syntaxerror);
154
                /* Avoid multiplies for power-of-2 radix. */
155
3.00G
                if (!(radix & (radix - 1))) {
156
3.00G
                    int shift;
157
158
3.00G
                    switch (radix) {
159
4.61k
                        case 2:
160
4.61k
                            shift = 1, imax = MAX_PS_UINT >> 1;
161
4.61k
                            break;
162
17.0k
                        case 4:
163
17.0k
                            shift = 2, imax = MAX_PS_UINT >> 2;
164
17.0k
                            break;
165
1.06k
                        case 8:
166
1.06k
                            shift = 3, imax = MAX_PS_UINT >> 3;
167
1.06k
                            break;
168
3.00G
                        case 16:
169
3.00G
                            shift = 4, imax = MAX_PS_UINT >> 4;
170
3.00G
                            break;
171
883
                        case 32:
172
883
                            shift = 5, imax = MAX_PS_UINT >> 5;
173
883
                            break;
174
0
                        default:  /* can't happen */
175
0
                            return_error(gs_error_rangecheck);
176
3.00G
                    }
177
14.2G
                    for (;; uval = (uval << shift) + d) {
178
14.2G
                        GET_NEXT(c, sp, break);
179
12.8G
                        d = decoder[c];
180
12.8G
                        if (d >= radix) {
181
1.55G
                            *psp = sp;
182
1.55G
                            code = 1;
183
1.55G
                            break;
184
1.55G
                        }
185
11.2G
                        if (uval > imax)
186
43
                            return_error(gs_error_limitcheck);
187
11.2G
                    }
188
3.00G
                } else {
189
7.98k
                    ps_int irem = MAX_PS_UINT % radix;
190
191
7.98k
                    imax = MAX_PS_UINT / radix;
192
18.1k
                    for (;; uval = uval * radix + d) {
193
18.1k
                        GET_NEXT(c, sp, break);
194
14.9k
                        d = decoder[c];
195
14.9k
                        if (d >= radix) {
196
4.74k
                            *psp = sp;
197
4.74k
                            code = 1;
198
4.74k
                            break;
199
4.74k
                        }
200
10.2k
                        if (uval >= imax &&
201
102
                            (uval > imax || d > irem)
202
10.2k
                            )
203
102
                            return_error(gs_error_limitcheck);
204
10.2k
                    }
205
7.98k
                }
206
3.00G
                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.00G
                else
212
3.00G
                    make_int(pref, uval);
213
214
3.00G
                return code;
215
3.00G
            }
216
3.98G
    }
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
60.0k
l2d:
228
60.0k
    exp10 = 0;
229
546k
    for (;;) {
230
546k
        dval = dval * 10 + d;
231
546k
        GET_NEXT(c, sp, c = EOFC);
232
546k
        if (!IS_DIGIT(d, c))
233
60.0k
            break;
234
546k
    }
235
60.0k
    switch (c) {
236
4.51k
        case '.':
237
4.51k
            GET_NEXT(c, sp, c = EOFC);
238
4.51k
            exp10 = 0;
239
4.51k
            goto fd;
240
43.7k
        default:
241
43.7k
            *psp = sp;
242
43.7k
            code = 1;
243
            /* falls through */
244
51.0k
        case EOFC:
245
51.0k
            if (sign < 0)
246
9.45k
                dval = -dval;
247
51.0k
            goto rret;
248
1.51k
        case 'e':
249
2.72k
        case 'E':
250
2.72k
            exp10 = 0;
251
2.72k
            goto fs;
252
1.72k
        case '#':
253
1.72k
            return_error(gs_error_syntaxerror);
254
60.0k
    }
255
256
    /* We saw a '.' while accumulating an integer in ival. */
257
27.4M
i2r:
258
27.4M
    exp10 = 0;
259
105M
    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
78.0M
        if (c == '-') {
268
724
            if ((SCAN_PDF_INV_NUM & scanner_options) == 0)
269
724
                break;
270
0
            do {
271
0
                GET_NEXT(c, sp, c = EOFC);
272
0
            } while (IS_DIGIT(d, c));
273
0
            break;
274
724
        }
275
78.0M
        if (WOULD_OVERFLOW(ival, d, max_int)) {
276
110k
            dval = (double)ival;
277
110k
            goto fd;
278
110k
        }
279
77.9M
        ival = ival * 10 + d;
280
77.9M
        exp10--;
281
77.9M
        GET_NEXT(c, sp, c = EOFC);
282
77.9M
    }
283
27.3M
    if (sign < 0)
284
2.18M
        ival = -ival;
285
    /* Take a shortcut for the common case */
286
27.3M
    if (!(c == 'e' || c == 'E' || exp10 < -NUM_POWERS_10)) { /* Check for trailing garbage */
287
27.2M
        if (c != EOFC)
288
23.5M
            *psp = sp, code = 1;
289
27.2M
        make_real(pref, ival * neg_powers_10[-exp10]);
290
27.2M
        return code;
291
27.2M
    }
292
23.7k
    dval = (double)ival;
293
23.7k
    goto fe;
294
295
    /* Now we are accumulating a double in dval. */
296
115k
fd:
297
576k
    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
117k
fs:
303
117k
    if (sign < 0)
304
13.2k
        dval = -dval;
305
563k
fe:
306
    /* Now dval contains the value, negated if necessary. */
307
563k
    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
9.98k
                    case '-':
317
9.98k
                        esign = 1;
318
                        /* fall through */
319
15.9k
                    case '+':
320
15.9k
                        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.4k
                iexp = d;
326
60.6k
                for (;; iexp = iexp * 10 + d) {
327
60.6k
                    GET_NEXT(c, sp, break);
328
51.6k
                    if (!IS_DIGIT(d, c)) {
329
32.2k
                        *psp = sp;
330
32.2k
                        code = 1;
331
32.2k
                        break;
332
32.2k
                    }
333
19.3k
                    if (iexp > 99)
334
94
                        return_error(gs_error_limitcheck);
335
19.3k
                }
336
41.3k
                if (esign)
337
9.66k
                    exp10 -= iexp;
338
31.6k
                else
339
31.6k
                    exp10 += iexp;
340
41.3k
                break;
341
41.4k
            }
342
100k
        default:
343
100k
            *psp = sp;
344
100k
            code = 1;
345
123k
        case EOFC:
346
123k
            ;
347
563k
    }
348
    /* Compute dval * 10^exp10. */
349
164k
    if (exp10 > 0) {
350
64.7k
        while (exp10 > NUM_POWERS_10)
351
39.5k
            dval *= powers_10[NUM_POWERS_10],
352
39.5k
                exp10 -= NUM_POWERS_10;
353
25.2k
        dval *= powers_10[exp10];
354
139k
    } else if (exp10 < 0) {
355
539k
        while (exp10 < -NUM_POWERS_10)
356
406k
            dval /= powers_10[NUM_POWERS_10],
357
406k
                exp10 += NUM_POWERS_10;
358
133k
        dval /= powers_10[-exp10];
359
133k
    }
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
164k
    if (dval >= 0) {
366
146k
        if (dval > MAX_FLOAT)
367
210
            return_error(gs_error_limitcheck);
368
146k
    } else {
369
17.9k
        if (dval < -MAX_FLOAT)
370
59
            return_error(gs_error_limitcheck);
371
17.9k
    }
372
215k
rret:
373
215k
    make_real(pref, dval);
374
215k
    return code;
375
164k
}