home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / IBNUM.C < prev    next >
C/C++ Source or Header  |  1994-08-01  |  6KB  |  197 lines

  1. /* Copyright (C) 1990, 1991, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* ibnum.c */
  20. /* Level 2 encoded number reading utilities for Ghostscript */
  21. #include "math_.h"
  22. #include "memory_.h"
  23. #include "ghost.h"
  24. #include "errors.h"
  25. #include "stream.h"
  26. #include "ibnum.h"
  27.  
  28. /* Define the number of bytes for a given format of encoded number. */
  29. const byte enc_num_bytes[] = { enc_num_bytes_values };
  30.  
  31. /* ------ Encoded number reading ------ */
  32.  
  33. /* Set up to read from an encoded number array/string. */
  34. /* Return <0 for error, or a num_format. */
  35. int
  36. sread_num_array(stream *s, ref *op)
  37. {    int num_format;
  38.     switch ( r_type(op) )
  39.        {
  40.     case t_string:
  41.        {    /* Check that this is a legitimate encoded number array. */
  42.         byte *bp = op->value.bytes;
  43.         short count;
  44.         int nshift;
  45.         if ( r_size(op) < 4 || bp[0] != bt_num_array_value ||
  46.             !num_is_valid(bp[1])
  47.             )
  48.             return_error(e_typecheck);
  49.         sread_string(s, bp + 4, r_size(op) - 4);
  50.         num_format = bp[1];
  51.         count = sdecodeshort(bp + 2, num_format);
  52.         nshift = ((bp[1] & 0x70) == 0x20 ? 1 : 2);
  53.         if ( count != (r_size(op) - 4) >> nshift )
  54.             return_error(e_typecheck);
  55.        }    break;
  56.     case t_array:
  57.         sread_string(s, (byte *)op->value.refs, r_size(op) * sizeof(ref));
  58.         num_format = num_array;
  59.         break;
  60.     default:
  61.         return_error(e_typecheck);
  62.        }
  63.     return num_format;
  64. }
  65.  
  66. /* Get the number of elements in an encoded number stream. */
  67. uint
  68. scount_num_stream(stream *s, int format)
  69. {    long avlong;
  70.     savailable(s, &avlong);
  71.     return (uint)(avlong / encoded_number_bytes(format));
  72. }
  73.  
  74. /* Read an encoded number from a stream according to the given num_format. */
  75. /* Put the value in np->value.{intval,realval}. */
  76. /* Return t_int if integer, t_real if real, t_null if end of stream, */
  77. /* or an error if the format is invalid. */
  78. /* Note that the stream is always reading a string or an array, */
  79. /* and we pre-checked the size, so sgets cannot return an error. */
  80. int
  81. sget_encoded_number(stream *s, int format, ref *np)
  82. {    byte nstr[sizeof(ref)];        /* worst case */
  83.     uint len;
  84.     sgets(s, nstr, encoded_number_bytes(format), &len);
  85.     if ( len == 0 )
  86.       return t_null;
  87.     else
  88.       return sdecode_number(nstr, 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. {    switch ( format & 0x170 )
  108.        {
  109.     case num_int32: case num_int32 + 16:
  110.         if ( (format & 31) == 0 )
  111.           {    np->value.intval = sdecodelong(str, format);
  112.             return t_integer;
  113.           }
  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.           }
  125.         else
  126.           {    np->value.realval =
  127.               sdecodeshort(str, format) *
  128.                 binary_scale[format & 15];
  129.             return t_real;
  130.           }
  131.     case num_float:
  132.         np->value.realval = sdecodefloat(str, format);
  133.         return t_real;
  134.     case num_array:
  135.         memcpy(np, str, sizeof(ref));
  136.         switch ( r_type(np) )
  137.            {
  138.         case t_integer:
  139.             return t_integer;
  140.         case t_real:
  141.             return t_real;
  142.         default:
  143.             return_error(e_typecheck);
  144.            }
  145.     default:
  146.         return_error(e_syntaxerror);    /* invalid num_format?? */
  147.        }
  148. }
  149.  
  150. /* ------ Decode number ------ */
  151.  
  152. /* Decode encoded numbers from a string according to format. */
  153.  
  154. /* Decode a (16-bit) short. */
  155. short
  156. sdecodeshort(register const byte *p, int format)
  157. {    int a = p[0], b = p[1];
  158.     return (short)(num_is_lsb(format) ? (b << 8) + a : (a << 8) + b);
  159. }
  160.  
  161. /* Decode a (32-bit) long. */
  162. long
  163. sdecodelong(register const byte *p, int format)
  164. {    int a = p[0], b = p[1], c = p[2], d = p[3];
  165.     return (long)(num_is_lsb(format) ?
  166.               ((long)d << 24) + ((long)c << 16) + (b << 8) + a :
  167.               ((long)a << 24) + ((long)b << 16) + (c << 8) + d);
  168. }
  169.  
  170. /* Decode a float.  We don't handle non-IEEE native representations yet. */
  171. float
  172. sdecodefloat(register const byte *p, int format)
  173. {    float fnum;
  174.     if ( format != num_float_native )
  175.     {    long lnum = sdecodelong(p, format);
  176.         /* We know IEEE floats take 32 bits. */
  177. #if !arch_floats_are_IEEE
  178.         /* Convert IEEE float to native float. */
  179.         int expt = (lnum >> 23) & 0xff;
  180.         long mant = lnum & 0x7fffff;
  181.         if ( expt == 0 && mant == 0 )
  182.             fnum = 0;
  183.         else
  184.         {    mant += 0x800000;
  185.             fnum = (float)ldexp((float)mant, expt - 127 - 24);
  186.         }
  187.         if ( lnum < 0 )
  188.             fnum = -fnum;
  189. #else
  190.         fnum = *(float *)&lnum;
  191. #endif
  192.     }
  193.     else
  194.         memcpy(&fnum, p, sizeof(float));
  195.     return fnum;
  196. }
  197.