iscannum.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /* Copyright (C) 1994, 1995, 1997, 1998, 1999 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: iscannum.c,v 1.3 2001/03/29 13:27:59 igorm Exp $ */
  16. /* Number scanner for Ghostscript interpreter */
  17. #include "math_.h"
  18. #include "ghost.h"
  19. #include "errors.h"
  20. #include "scommon.h"
  21. #include "iscannum.h" /* defines interface */
  22. #include "scanchar.h"
  23. #include "store.h"
  24. /*
  25. * Warning: this file has a "spaghetti" control structure. But since this
  26. * code accounts for over 10% of the execution time of some PostScript
  27. * files, this is one of the few places we feel this is justified.
  28. */
  29. /*
  30. * Scan a number. If the number consumes the entire string, return 0;
  31. * if not, set *psp to the first character beyond the number and return 1.
  32. */
  33. int
  34. scan_number(const byte * str, const byte * end, int sign,
  35. ref * pref, const byte ** psp)
  36. {
  37. const byte *sp = str;
  38. #define GET_NEXT(cvar, sp, end_action)\
  39. if (sp >= end) { end_action; } else cvar = *sp++
  40. /*
  41. * Powers of 10 up to 6 can be represented accurately as
  42. * a single-precision float.
  43. */
  44. #define NUM_POWERS_10 6
  45. static const float powers_10[NUM_POWERS_10 + 1] = {
  46. 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6
  47. };
  48. static const double neg_powers_10[NUM_POWERS_10 + 1] = {
  49. 1e0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6
  50. };
  51. int ival;
  52. long lval;
  53. double dval;
  54. int exp10;
  55. int code = 0;
  56. int c, d;
  57. const byte *const decoder = scan_char_decoder;
  58. #define IS_DIGIT(d, c)\
  59. ((d = decoder[c]) < 10)
  60. #define WOULD_OVERFLOW(val, d, maxv)\
  61. (val >= maxv / 10 && (val > maxv / 10 || d > (int)(maxv % 10)))
  62. GET_NEXT(c, sp, return_error(e_syntaxerror));
  63. if (!IS_DIGIT(d, c)) {
  64. if (c != '.')
  65. return_error(e_syntaxerror);
  66. /* Might be a number starting with '.'. */
  67. GET_NEXT(c, sp, return_error(e_syntaxerror));
  68. if (!IS_DIGIT(d, c))
  69. return_error(e_syntaxerror);
  70. ival = 0;
  71. goto i2r;
  72. }
  73. /* Accumulate an integer in ival. */
  74. /* Do up to 4 digits without a loop, */
  75. /* since we know this can't overflow and since */
  76. /* most numbers have 4 (integer) digits or fewer. */
  77. ival = d;
  78. if (end - sp >= 3) { /* just check once */
  79. if (!IS_DIGIT(d, (c = *sp))) {
  80. sp++;
  81. goto ind;
  82. }
  83. ival = ival * 10 + d;
  84. if (!IS_DIGIT(d, (c = sp[1]))) {
  85. sp += 2;
  86. goto ind;
  87. }
  88. ival = ival * 10 + d;
  89. sp += 3;
  90. if (!IS_DIGIT(d, (c = sp[-1])))
  91. goto ind;
  92. ival = ival * 10 + d;
  93. }
  94. for (;; ival = ival * 10 + d) {
  95. GET_NEXT(c, sp, goto iret);
  96. if (!IS_DIGIT(d, c))
  97. break;
  98. if (WOULD_OVERFLOW(ival, d, max_int))
  99. goto i2l;
  100. }
  101. ind: /* We saw a non-digit while accumulating an integer in ival. */
  102. switch (c) {
  103. case '.':
  104. GET_NEXT(c, sp, c = EOFC);
  105. goto i2r;
  106. default:
  107. *psp = sp;
  108. code = 1;
  109. break;
  110. case 'e':
  111. case 'E':
  112. if (sign < 0)
  113. ival = -ival;
  114. dval = ival;
  115. exp10 = 0;
  116. goto fe;
  117. case '#':
  118. {
  119. const uint radix = (uint)ival;
  120. ulong uval = 0, lmax;
  121. if (sign || radix < min_radix || radix > max_radix)
  122. return_error(e_syntaxerror);
  123. /* Avoid multiplies for power-of-2 radix. */
  124. if (!(radix & (radix - 1))) {
  125. int shift;
  126. switch (radix) {
  127. case 2:
  128. shift = 1, lmax = max_ulong >> 1;
  129. break;
  130. case 4:
  131. shift = 2, lmax = max_ulong >> 2;
  132. break;
  133. case 8:
  134. shift = 3, lmax = max_ulong >> 3;
  135. break;
  136. case 16:
  137. shift = 4, lmax = max_ulong >> 4;
  138. break;
  139. case 32:
  140. shift = 5, lmax = max_ulong >> 5;
  141. break;
  142. default: /* can't happen */
  143. return_error(e_rangecheck);
  144. }
  145. for (;; uval = (uval << shift) + d) {
  146. GET_NEXT(c, sp, break);
  147. d = decoder[c];
  148. if (d >= radix) {
  149. *psp = sp;
  150. code = 1;
  151. break;
  152. }
  153. if (uval > lmax)
  154. return_error(e_limitcheck);
  155. }
  156. } else {
  157. int lrem = max_ulong % radix;
  158. lmax = max_ulong / radix;
  159. for (;; uval = uval * radix + d) {
  160. GET_NEXT(c, sp, break);
  161. d = decoder[c];
  162. if (d >= radix) {
  163. *psp = sp;
  164. code = 1;
  165. break;
  166. }
  167. if (uval >= lmax &&
  168. (uval > lmax || d > lrem)
  169. )
  170. return_error(e_limitcheck);
  171. }
  172. }
  173. make_int(pref, uval);
  174. return code;
  175. }
  176. }
  177. iret:
  178. make_int(pref, (sign < 0 ? -ival : ival));
  179. return code;
  180. /* Accumulate a long in lval. */
  181. i2l:
  182. for (lval = ival;;) {
  183. if (WOULD_OVERFLOW(lval, d, max_long)) {
  184. /* Make a special check for entering the smallest */
  185. /* (most negative) integer. */
  186. if (lval == max_long / 10 &&
  187. d == (int)(max_long % 10) + 1 && sign < 0
  188. ) {
  189. GET_NEXT(c, sp, c = EOFC);
  190. dval = -(double)min_long;
  191. if (c == 'e' || c == 'E' || c == '.') {
  192. exp10 = 0;
  193. goto fs;
  194. } else if (!IS_DIGIT(d, c)) {
  195. lval = min_long;
  196. break;
  197. }
  198. } else
  199. dval = lval;
  200. goto l2d;
  201. }
  202. lval = lval * 10 + d;
  203. GET_NEXT(c, sp, goto lret);
  204. if (!IS_DIGIT(d, c))
  205. break;
  206. }
  207. switch (c) {
  208. case '.':
  209. GET_NEXT(c, sp, c = EOFC);
  210. exp10 = 0;
  211. goto l2r;
  212. case EOFC:
  213. break;
  214. default:
  215. *psp = sp;
  216. code = 1;
  217. break;
  218. case 'e':
  219. case 'E':
  220. exp10 = 0;
  221. goto le;
  222. case '#':
  223. return_error(e_syntaxerror);
  224. }
  225. lret:
  226. make_int(pref, (sign < 0 ? -lval : lval));
  227. return code;
  228. /* Accumulate a double in dval. */
  229. l2d:
  230. exp10 = 0;
  231. for (;;) {
  232. dval = dval * 10 + d;
  233. GET_NEXT(c, sp, c = EOFC);
  234. if (!IS_DIGIT(d, c))
  235. break;
  236. }
  237. switch (c) {
  238. case '.':
  239. GET_NEXT(c, sp, c = EOFC);
  240. exp10 = 0;
  241. goto fd;
  242. default:
  243. *psp = sp;
  244. code = 1;
  245. /* falls through */
  246. case EOFC:
  247. if (sign < 0)
  248. dval = -dval;
  249. goto rret;
  250. case 'e':
  251. case 'E':
  252. exp10 = 0;
  253. goto fs;
  254. case '#':
  255. return_error(e_syntaxerror);
  256. }
  257. /* We saw a '.' while accumulating an integer in ival. */
  258. i2r:
  259. exp10 = 0;
  260. while (IS_DIGIT(d, c)) {
  261. if (WOULD_OVERFLOW(ival, d, max_int)) {
  262. lval = ival;
  263. goto l2r;
  264. }
  265. ival = ival * 10 + d;
  266. exp10--;
  267. GET_NEXT(c, sp, c = EOFC);
  268. }
  269. if (sign < 0)
  270. ival = -ival;
  271. /* Take a shortcut for the common case */
  272. if (!(c == 'e' || c == 'E' || exp10 < -NUM_POWERS_10)) { /* Check for trailing garbage */
  273. if (c != EOFC)
  274. *psp = sp, code = 1;
  275. make_real(pref, ival * neg_powers_10[-exp10]);
  276. return code;
  277. }
  278. dval = ival;
  279. goto fe;
  280. /* We saw a '.' while accumulating a long in lval. */
  281. l2r:
  282. while (IS_DIGIT(d, c)) {
  283. if (WOULD_OVERFLOW(lval, d, max_long)) {
  284. dval = lval;
  285. goto fd;
  286. }
  287. lval = lval * 10 + d;
  288. exp10--;
  289. GET_NEXT(c, sp, c = EOFC);
  290. }
  291. le:
  292. if (sign < 0)
  293. lval = -lval;
  294. dval = lval;
  295. goto fe;
  296. /* Now we are accumulating a double in dval. */
  297. fd:
  298. while (IS_DIGIT(d, c)) {
  299. dval = dval * 10 + d;
  300. exp10--;
  301. GET_NEXT(c, sp, c = EOFC);
  302. }
  303. fs:
  304. if (sign < 0)
  305. dval = -dval;
  306. fe:
  307. /* Now dval contains the value, negated if necessary. */
  308. switch (c) {
  309. case 'e':
  310. case 'E':
  311. { /* Check for a following exponent. */
  312. int esign = 0;
  313. int iexp;
  314. GET_NEXT(c, sp, return_error(e_syntaxerror));
  315. switch (c) {
  316. case '-':
  317. esign = 1;
  318. case '+':
  319. GET_NEXT(c, sp, return_error(e_syntaxerror));
  320. }
  321. /* Scan the exponent. We limit it arbitrarily to 999. */
  322. if (!IS_DIGIT(d, c))
  323. return_error(e_syntaxerror);
  324. iexp = d;
  325. for (;; iexp = iexp * 10 + d) {
  326. GET_NEXT(c, sp, break);
  327. if (!IS_DIGIT(d, c)) {
  328. *psp = sp;
  329. code = 1;
  330. break;
  331. }
  332. if (iexp > 99)
  333. return_error(e_limitcheck);
  334. }
  335. if (esign)
  336. exp10 -= iexp;
  337. else
  338. exp10 += iexp;
  339. break;
  340. }
  341. default:
  342. *psp = sp;
  343. code = 1;
  344. case EOFC:
  345. ;
  346. }
  347. /* Compute dval * 10^exp10. */
  348. if (exp10 > 0) {
  349. while (exp10 > NUM_POWERS_10)
  350. dval *= powers_10[NUM_POWERS_10],
  351. exp10 -= NUM_POWERS_10;
  352. if (exp10 > 0)
  353. dval *= powers_10[exp10];
  354. } else if (exp10 < 0) {
  355. while (exp10 < -NUM_POWERS_10)
  356. dval /= powers_10[NUM_POWERS_10],
  357. exp10 += NUM_POWERS_10;
  358. if (exp10 < 0)
  359. dval /= powers_10[-exp10];
  360. }
  361. /*
  362. * Check for an out-of-range result. Currently we don't check for
  363. * absurdly large numbers of digits in the accumulation loops,
  364. * but we should.
  365. */
  366. if (dval >= 0) {
  367. if (dval > MAX_FLOAT)
  368. return_error(e_limitcheck);
  369. } else {
  370. if (dval < -MAX_FLOAT)
  371. return_error(e_limitcheck);
  372. }
  373. rret:
  374. make_real(pref, dval);
  375. return code;
  376. }