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