/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 | 261M | { |
41 | 261M | const byte *sp = str; |
42 | 261M | #define GET_NEXT(cvar, sp, end_action)\ |
43 | 1.05G | 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 | 261M | #define NUM_POWERS_10 6 |
50 | 261M | static const float powers_10[NUM_POWERS_10 + 1] = { |
51 | 261M | 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6 |
52 | 261M | }; |
53 | 261M | static const double neg_powers_10[NUM_POWERS_10 + 1] = { |
54 | 261M | 1e0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6 |
55 | 261M | }; |
56 | | |
57 | 261M | ps_int ival; |
58 | 261M | double dval; |
59 | 261M | int exp10; |
60 | 261M | int code = 0; |
61 | 261M | int c, d; |
62 | 261M | ps_int max_ps_int_scan, min_ps_int_scan; |
63 | 261M | const byte *const decoder = scan_char_decoder; |
64 | 261M | #define IS_DIGIT(d, c)\ |
65 | 699M | ((d = decoder[c]) < 10) |
66 | 261M | #define WOULD_OVERFLOW(val, d, maxv)\ |
67 | 261M | (val >= maxv / 10 && (val > maxv / 10 || d > (int64_t)(maxv % 10))) |
68 | | |
69 | 261M | GET_NEXT(c, sp, return_error(gs_error_syntaxerror)); |
70 | 261M | if (!IS_DIGIT(d, c)) { |
71 | 52.0M | if (c != '.') |
72 | 18.6k | return_error(gs_error_syntaxerror); |
73 | | /* Might be a number starting with '.'. */ |
74 | 52.0M | GET_NEXT(c, sp, return_error(gs_error_syntaxerror)); |
75 | 52.0M | if (!IS_DIGIT(d, c)) |
76 | 52.0M | return_error(gs_error_syntaxerror); |
77 | 4.18k | ival = 0; |
78 | 4.18k | goto i2r; |
79 | 52.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 | 209M | ival = d; |
85 | 209M | if (end - sp >= 3) { /* just check once */ |
86 | 204M | if (!IS_DIGIT(d, (c = *sp))) { |
87 | 43.7M | sp++; |
88 | 43.7M | goto ind; |
89 | 43.7M | } |
90 | 160M | ival = ival * 10 + d; |
91 | 160M | if (!IS_DIGIT(d, (c = sp[1]))) { |
92 | 155M | sp += 2; |
93 | 155M | goto ind; |
94 | 155M | } |
95 | 5.44M | ival = ival * 10 + d; |
96 | 5.44M | sp += 3; |
97 | 5.44M | if (!IS_DIGIT(d, (c = sp[-1]))) |
98 | 3.06M | goto ind; |
99 | 2.38M | ival = ival * 10 + d; |
100 | 2.38M | } |
101 | | |
102 | 7.44M | max_ps_int_scan = scanner_options & SCAN_CPSI_MODE ? MAX_PS_INT32 : MAX_PS_INT; |
103 | 7.44M | min_ps_int_scan = scanner_options & SCAN_CPSI_MODE ? MIN_PS_INT32 : MIN_PS_INT; |
104 | | |
105 | 11.5M | for (;; ival = ival * 10 + d) { |
106 | 11.5M | GET_NEXT(c, sp, goto iret); |
107 | 6.32M | if (!IS_DIGIT(d, c)) |
108 | 2.16M | break; |
109 | 4.16M | if (WOULD_OVERFLOW(((ps_uint)ival), d, max_ps_int_scan)) { |
110 | 7.31k | if (ival == max_ps_int_scan / 10 && d == (max_ps_int_scan % 10) + 1 && sign < 0) { |
111 | 529 | GET_NEXT(c, sp, c = EOFC); |
112 | 529 | dval = -(double)min_ps_int_scan; |
113 | 529 | if (c == 'e' || c == 'E') { |
114 | 0 | exp10 = 0; |
115 | 0 | goto fs; |
116 | 529 | } else if (c == '.') { |
117 | 4 | GET_NEXT(c, sp, c = EOFC); |
118 | 4 | exp10 = 0; |
119 | 4 | goto fd; |
120 | 525 | } else if (!IS_DIGIT(d, c)) { |
121 | 523 | ival = min_ps_int_scan; |
122 | 523 | break; |
123 | 523 | } |
124 | 529 | } else |
125 | 6.79k | dval = (double)ival; |
126 | 6.79k | goto l2d; |
127 | 7.31k | } |
128 | 4.16M | } |
129 | 204M | ind: /* We saw a non-digit while accumulating an integer in ival. */ |
130 | 204M | switch (c) { |
131 | 1.35M | case '.': |
132 | 1.35M | GET_NEXT(c, sp, c = EOFC); |
133 | 1.35M | goto i2r; |
134 | 51.0M | default: |
135 | 51.0M | *psp = sp; |
136 | 51.0M | code = 1; |
137 | 51.0M | break; |
138 | 13 | case EOFC: |
139 | 13 | break; |
140 | 1.46k | case 'e': |
141 | 2.24k | case 'E': |
142 | 2.24k | if (sign < 0) |
143 | 392 | ival = -ival; |
144 | 2.24k | dval = (double)ival; |
145 | 2.24k | exp10 = 0; |
146 | 2.24k | goto fe; |
147 | 151M | case '#': |
148 | 151M | { |
149 | 151M | const int radix = ival; |
150 | 151M | ps_int uval = 0, imax; |
151 | | |
152 | 151M | if (sign || radix < min_radix || radix > max_radix) |
153 | 1.76k | return_error(gs_error_syntaxerror); |
154 | | /* Avoid multiplies for power-of-2 radix. */ |
155 | 151M | if (!(radix & (radix - 1))) { |
156 | 151M | int shift; |
157 | | |
158 | 151M | switch (radix) { |
159 | 800 | case 2: |
160 | 800 | shift = 1, imax = MAX_PS_UINT >> 1; |
161 | 800 | break; |
162 | 36 | case 4: |
163 | 36 | shift = 2, imax = MAX_PS_UINT >> 2; |
164 | 36 | break; |
165 | 43 | case 8: |
166 | 43 | shift = 3, imax = MAX_PS_UINT >> 3; |
167 | 43 | break; |
168 | 151M | case 16: |
169 | 151M | shift = 4, imax = MAX_PS_UINT >> 4; |
170 | 151M | break; |
171 | 97 | case 32: |
172 | 97 | shift = 5, imax = MAX_PS_UINT >> 5; |
173 | 97 | break; |
174 | 0 | default: /* can't happen */ |
175 | 0 | return_error(gs_error_rangecheck); |
176 | 151M | } |
177 | 720M | for (;; uval = (uval << shift) + d) { |
178 | 720M | GET_NEXT(c, sp, break); |
179 | 644M | d = decoder[c]; |
180 | 644M | if (d >= radix) { |
181 | 75.6M | *psp = sp; |
182 | 75.6M | code = 1; |
183 | 75.6M | break; |
184 | 75.6M | } |
185 | 568M | if (uval > imax) |
186 | 5 | return_error(gs_error_limitcheck); |
187 | 568M | } |
188 | 151M | } else { |
189 | 1.49k | ps_int irem = MAX_PS_UINT % radix; |
190 | | |
191 | 1.49k | imax = MAX_PS_UINT / radix; |
192 | 2.98k | for (;; uval = uval * radix + d) { |
193 | 2.98k | GET_NEXT(c, sp, break); |
194 | 2.32k | d = decoder[c]; |
195 | 2.32k | if (d >= radix) { |
196 | 838 | *psp = sp; |
197 | 838 | code = 1; |
198 | 838 | break; |
199 | 838 | } |
200 | 1.49k | if (uval >= imax && |
201 | 1.49k | (uval > imax || d > irem) |
202 | 1.49k | ) |
203 | 8 | return_error(gs_error_limitcheck); |
204 | 1.49k | } |
205 | 1.49k | } |
206 | 151M | 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 | 151M | else |
212 | 151M | make_int(pref, uval); |
213 | | |
214 | 151M | return code; |
215 | 151M | } |
216 | 204M | } |
217 | 56.3M | iret: |
218 | 56.3M | if (scanner_options & SCAN_CPSI_MODE) { |
219 | 0 | make_int(pref, (sign < 0 ? (ps_int32)-ival : (ps_int32)ival)); |
220 | 0 | } |
221 | 56.3M | else { |
222 | 56.3M | make_int(pref, (sign < 0 ? (ps_int)-ival : (ps_int)ival)); |
223 | 56.3M | } |
224 | 56.3M | return code; |
225 | | |
226 | | /* Accumulate a double in dval. */ |
227 | 6.79k | l2d: |
228 | 6.79k | exp10 = 0; |
229 | 75.6k | for (;;) { |
230 | 75.6k | dval = dval * 10 + d; |
231 | 75.6k | GET_NEXT(c, sp, c = EOFC); |
232 | 75.6k | if (!IS_DIGIT(d, c)) |
233 | 6.79k | break; |
234 | 75.6k | } |
235 | 6.79k | switch (c) { |
236 | 103 | case '.': |
237 | 103 | GET_NEXT(c, sp, c = EOFC); |
238 | 103 | exp10 = 0; |
239 | 103 | goto fd; |
240 | 5.52k | default: |
241 | 5.52k | *psp = sp; |
242 | 5.52k | code = 1; |
243 | | /* falls through */ |
244 | 5.90k | case EOFC: |
245 | 5.90k | if (sign < 0) |
246 | 1.36k | dval = -dval; |
247 | 5.90k | goto rret; |
248 | 42 | case 'e': |
249 | 137 | case 'E': |
250 | 137 | exp10 = 0; |
251 | 137 | goto fs; |
252 | 644 | case '#': |
253 | 644 | return_error(gs_error_syntaxerror); |
254 | 6.79k | } |
255 | | |
256 | | /* We saw a '.' while accumulating an integer in ival. */ |
257 | 1.36M | i2r: |
258 | 1.36M | exp10 = 0; |
259 | 4.88M | 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 | 3.52M | if (c == '-') { |
268 | 45 | if ((SCAN_PDF_INV_NUM & scanner_options) == 0) |
269 | 45 | break; |
270 | 0 | do { |
271 | 0 | GET_NEXT(c, sp, c = EOFC); |
272 | 0 | } while (IS_DIGIT(d, c)); |
273 | 0 | break; |
274 | 45 | } |
275 | 3.52M | if (WOULD_OVERFLOW(ival, d, max_int)) { |
276 | 4.59k | dval = (double)ival; |
277 | 4.59k | goto fd; |
278 | 4.59k | } |
279 | 3.52M | ival = ival * 10 + d; |
280 | 3.52M | exp10--; |
281 | 3.52M | GET_NEXT(c, sp, c = EOFC); |
282 | 3.52M | } |
283 | 1.35M | if (sign < 0) |
284 | 108k | ival = -ival; |
285 | | /* Take a shortcut for the common case */ |
286 | 1.35M | if (!(c == 'e' || c == 'E' || exp10 < -NUM_POWERS_10)) { /* Check for trailing garbage */ |
287 | 1.35M | if (c != EOFC) |
288 | 1.16M | *psp = sp, code = 1; |
289 | 1.35M | make_real(pref, ival * neg_powers_10[-exp10]); |
290 | 1.35M | return code; |
291 | 1.35M | } |
292 | 342 | dval = (double)ival; |
293 | 342 | goto fe; |
294 | | |
295 | | /* Now we are accumulating a double in dval. */ |
296 | 4.70k | fd: |
297 | 25.1k | while (IS_DIGIT(d, c)) { |
298 | 20.4k | dval = dval * 10 + d; |
299 | 20.4k | exp10--; |
300 | 20.4k | GET_NEXT(c, sp, c = EOFC); |
301 | 20.4k | } |
302 | 4.84k | fs: |
303 | 4.84k | if (sign < 0) |
304 | 1.10k | dval = -dval; |
305 | 7.43k | fe: |
306 | | /* Now dval contains the value, negated if necessary. */ |
307 | 7.43k | switch (c) { |
308 | 1.57k | case 'e': |
309 | 2.47k | case 'E': |
310 | 2.47k | { /* Check for a following exponent. */ |
311 | 2.47k | int esign = 0; |
312 | 2.47k | int iexp; |
313 | | |
314 | 2.47k | GET_NEXT(c, sp, return_error(gs_error_syntaxerror)); |
315 | 2.37k | switch (c) { |
316 | 586 | case '-': |
317 | 586 | esign = 1; |
318 | | /* fall through */ |
319 | 1.06k | case '+': |
320 | 1.06k | GET_NEXT(c, sp, return_error(gs_error_syntaxerror)); |
321 | 2.37k | } |
322 | | /* Scan the exponent. We limit it arbitrarily to 999. */ |
323 | 2.19k | if (!IS_DIGIT(d, c)) |
324 | 253 | return_error(gs_error_syntaxerror); |
325 | 1.94k | iexp = d; |
326 | 3.32k | for (;; iexp = iexp * 10 + d) { |
327 | 3.32k | GET_NEXT(c, sp, break); |
328 | 2.74k | if (!IS_DIGIT(d, c)) { |
329 | 1.35k | *psp = sp; |
330 | 1.35k | code = 1; |
331 | 1.35k | break; |
332 | 1.35k | } |
333 | 1.39k | if (iexp > 99) |
334 | 14 | return_error(gs_error_limitcheck); |
335 | 1.39k | } |
336 | 1.93k | if (esign) |
337 | 554 | exp10 -= iexp; |
338 | 1.37k | else |
339 | 1.37k | exp10 += iexp; |
340 | 1.93k | break; |
341 | 1.94k | } |
342 | 4.81k | default: |
343 | 4.81k | *psp = sp; |
344 | 4.81k | code = 1; |
345 | 4.96k | case EOFC: |
346 | 4.96k | ; |
347 | 7.43k | } |
348 | | /* Compute dval * 10^exp10. */ |
349 | 6.89k | if (exp10 > 0) { |
350 | 3.12k | while (exp10 > NUM_POWERS_10) |
351 | 1.86k | dval *= powers_10[NUM_POWERS_10], |
352 | 1.86k | exp10 -= NUM_POWERS_10; |
353 | 1.25k | dval *= powers_10[exp10]; |
354 | 5.63k | } else if (exp10 < 0) { |
355 | 35.3k | while (exp10 < -NUM_POWERS_10) |
356 | 29.9k | dval /= powers_10[NUM_POWERS_10], |
357 | 29.9k | exp10 += NUM_POWERS_10; |
358 | 5.46k | dval /= powers_10[-exp10]; |
359 | 5.46k | } |
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 | 6.89k | if (dval >= 0) { |
366 | 5.44k | if (dval > MAX_FLOAT) |
367 | 6 | return_error(gs_error_limitcheck); |
368 | 5.44k | } else { |
369 | 1.45k | if (dval < -MAX_FLOAT) |
370 | 5 | return_error(gs_error_limitcheck); |
371 | 1.45k | } |
372 | 12.7k | rret: |
373 | 12.7k | make_real(pref, dval); |
374 | 12.7k | return code; |
375 | 6.89k | } |