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