ibnum.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* Copyright (C) 1990, 1996, 1998, 1999 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: ibnum.c,v 1.8 2004/08/04 19:36:12 stefan Exp $ */
  14. /* Level 2 encoded number reading utilities for Ghostscript */
  15. #include "math_.h"
  16. #include "memory_.h"
  17. #include "ghost.h"
  18. #include "ierrors.h"
  19. #include "stream.h"
  20. #include "ibnum.h"
  21. #include "imemory.h" /* for iutil.h */
  22. #include "iutil.h"
  23. /* Define the number of bytes for a given format of encoded number. */
  24. const byte enc_num_bytes[] = {
  25. enc_num_bytes_values
  26. };
  27. /* ------ Encoded number reading ------ */
  28. /* Set up to read from an encoded number array/string. */
  29. /* Return <0 for error, or a number format. */
  30. int
  31. num_array_format(const ref * op)
  32. {
  33. switch (r_type(op)) {
  34. case t_string:
  35. {
  36. /* Check that this is a legitimate encoded number string. */
  37. const byte *bp = op->value.bytes;
  38. int format;
  39. if (r_size(op) < 4 || bp[0] != bt_num_array_value)
  40. return_error(e_rangecheck);
  41. format = bp[1];
  42. if (!num_is_valid(format) ||
  43. sdecodeshort(bp + 2, format) !=
  44. (r_size(op) - 4) / encoded_number_bytes(format)
  45. )
  46. return_error(e_rangecheck);
  47. return format;
  48. }
  49. case t_array:
  50. case t_mixedarray:
  51. case t_shortarray:
  52. return num_array;
  53. default:
  54. return_error(e_typecheck);
  55. }
  56. }
  57. /* Get the number of elements in an encoded number array/string. */
  58. uint
  59. num_array_size(const ref * op, int format)
  60. {
  61. return (format == num_array ? r_size(op) :
  62. (r_size(op) - 4) / encoded_number_bytes(format));
  63. }
  64. /* Get an encoded number from an array/string according to the given format. */
  65. /* Put the value in np->value.{intval,realval}. */
  66. /* Return t_int if integer, t_real if real, t_null if end of stream, */
  67. /* or an error if the format is invalid. */
  68. int
  69. num_array_get(const gs_memory_t *mem, const ref * op, int format, uint index, ref * np)
  70. {
  71. if (format == num_array) {
  72. int code = array_get(mem, op, (long)index, np);
  73. if (code < 0)
  74. return t_null;
  75. switch (r_type(np)) {
  76. case t_integer:
  77. return t_integer;
  78. case t_real:
  79. return t_real;
  80. default:
  81. return_error(e_rangecheck);
  82. }
  83. } else {
  84. uint nbytes = encoded_number_bytes(format);
  85. if (index >= (r_size(op) - 4) / nbytes)
  86. return t_null;
  87. return sdecode_number(op->value.bytes + 4 + index * nbytes,
  88. format, np);
  89. }
  90. }
  91. /* Internal routine to decode a number in a given format. */
  92. /* Same returns as sget_encoded_number. */
  93. static const double binary_scale[32] = {
  94. #define EXPN2(n) (0.5 / (1L << (n-1)))
  95. 1.0, EXPN2(1), EXPN2(2), EXPN2(3),
  96. EXPN2(4), EXPN2(5), EXPN2(6), EXPN2(7),
  97. EXPN2(8), EXPN2(9), EXPN2(10), EXPN2(11),
  98. EXPN2(12), EXPN2(13), EXPN2(14), EXPN2(15),
  99. EXPN2(16), EXPN2(17), EXPN2(18), EXPN2(19),
  100. EXPN2(20), EXPN2(21), EXPN2(22), EXPN2(23),
  101. EXPN2(24), EXPN2(25), EXPN2(26), EXPN2(27),
  102. EXPN2(28), EXPN2(29), EXPN2(30), EXPN2(31)
  103. #undef EXPN2
  104. };
  105. int
  106. sdecode_number(const byte * str, int format, ref * np)
  107. {
  108. switch (format & 0x170) {
  109. case num_int32:
  110. case num_int32 + 16:
  111. if ((format & 31) == 0) {
  112. np->value.intval = sdecodelong(str, format);
  113. return t_integer;
  114. } else {
  115. np->value.realval =
  116. (double)sdecodelong(str, format) *
  117. binary_scale[format & 31];
  118. return t_real;
  119. }
  120. case num_int16:
  121. if ((format & 15) == 0) {
  122. np->value.intval = sdecodeshort(str, format);
  123. return t_integer;
  124. } else {
  125. np->value.realval =
  126. sdecodeshort(str, format) *
  127. binary_scale[format & 15];
  128. return t_real;
  129. }
  130. case num_float:
  131. np->value.realval = sdecodefloat(str, format);
  132. return t_real;
  133. default:
  134. return_error(e_syntaxerror); /* invalid format?? */
  135. }
  136. }
  137. /* ------ Decode number ------ */
  138. /* Decode encoded numbers from a string according to format. */
  139. /* Decode a (16-bit, signed or unsigned) short. */
  140. uint
  141. sdecodeushort(const byte * p, int format)
  142. {
  143. int a = p[0], b = p[1];
  144. return (num_is_lsb(format) ? (b << 8) + a : (a << 8) + b);
  145. }
  146. int
  147. sdecodeshort(const byte * p, int format)
  148. {
  149. int v = (int)sdecodeushort(p, format);
  150. return (v & 0x7fff) - (v & 0x8000);
  151. }
  152. /* Decode a (32-bit, signed) long. */
  153. long
  154. sdecodelong(const byte * p, int format)
  155. {
  156. int a = p[0], b = p[1], c = p[2], d = p[3];
  157. long v = (num_is_lsb(format) ?
  158. ((long)d << 24) + ((long)c << 16) + (b << 8) + a :
  159. ((long)a << 24) + ((long)b << 16) + (c << 8) + d);
  160. #if arch_sizeof_long > 4
  161. /* Propagate bit 31 as the sign. */
  162. v = (v ^ 0x80000000L) - 0x80000000L;
  163. #endif
  164. return v;
  165. }
  166. /* Decode a float. We assume that native floats occupy 32 bits. */
  167. float
  168. sdecodefloat(const byte * p, int format)
  169. {
  170. bits32 lnum;
  171. float fnum;
  172. if ((format & ~(num_msb | num_lsb)) == num_float_native) {
  173. /*
  174. * Just read 4 bytes and interpret them as a float, ignoring
  175. * any indication of byte ordering.
  176. */
  177. memcpy(&lnum, p, 4);
  178. fnum = *(float *)&lnum;
  179. } else {
  180. lnum = (bits32) sdecodelong(p, format);
  181. #if !arch_floats_are_IEEE
  182. {
  183. /* We know IEEE floats take 32 bits. */
  184. /* Convert IEEE float to native float. */
  185. int sign_expt = lnum >> 23;
  186. int expt = sign_expt & 0xff;
  187. long mant = lnum & 0x7fffff;
  188. if (expt == 0 && mant == 0)
  189. fnum = 0;
  190. else {
  191. mant += 0x800000;
  192. fnum = (float)ldexp((float)mant, expt - 127 - 23);
  193. }
  194. if (sign_expt & 0x100)
  195. fnum = -fnum;
  196. }
  197. #else
  198. fnum = *(float *)&lnum;
  199. #endif
  200. }
  201. return fnum;
  202. }