home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / ghostscr / i2num.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-31  |  5.4 KB  |  206 lines

  1. /* Copyright (C) 1990 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* i2num.c */
  21. /* Level 2 encoded number reading utilities for Ghostscript */
  22. #include "ghost.h"
  23. #include "arch.h"
  24. #include "errors.h"
  25. #include "stream.h"
  26. #include "i2num.h"
  27. #include "i2btoken.h"
  28.  
  29. /* ------ Encoded number reading ------ */
  30.  
  31. /* Set up to read from an encoded number array/string. */
  32. int
  33. sread_num_array(stream *s, ref *op)
  34. {    switch ( r_type(op) )
  35.        {
  36.     case t_string:
  37.        {    /* Check that this is a legitimate encoded number array */
  38.         byte *bp = op->value.bytes;
  39.         short count;
  40.         int nshift;
  41.         if ( op->size < 4 || bp[0] != bt_num_array ||
  42.             !num_is_valid(bp[1])
  43.             )
  44.             return e_typecheck;
  45.         sread_string(s, bp + 2, op->size - 2);
  46.         s->num_format = bp[1];
  47.         sgetshort(s, &count);
  48.         nshift = ((bp[1] & 0x70) == 0x20 ? 1 : 2);
  49.         if ( count != (op->size - 4) >> nshift )
  50.             return e_typecheck;
  51.        }    break;
  52.     case t_array:
  53.         sread_string(s, (byte *)op->value.refs, op->size * sizeof(ref));
  54.         s->num_format = num_array;
  55.         break;
  56.     default:
  57.         return e_typecheck;
  58.        }
  59.     return 0;
  60. }
  61.  
  62. /* Read an encoded number from a stream according to its num_format. */
  63. /* Put the value in np->value.{intval,realval}.  Return t_int, t_real, */
  64. /* t_null if end of stream, or e_syntaxerror or e_typecheck. */
  65. static double binary_scale[32] = {
  66. #define expn2(n) (0.5 / (1L << (n-1)))
  67.     1.0, expn2(1), expn2(2), expn2(3),
  68.     expn2(4), expn2(5), expn2(6), expn2(7),
  69.     expn2(8), expn2(9), expn2(10), expn2(11),
  70.     expn2(12), expn2(13), expn2(14), expn2(15),
  71.     expn2(16), expn2(17), expn2(18), expn2(19),
  72.     expn2(20), expn2(21), expn2(22), expn2(23),
  73.     expn2(24), expn2(25), expn2(26), expn2(27),
  74.     expn2(28), expn2(29), expn2(30), expn2(31)
  75. #undef expn2
  76. };
  77. int
  78. sget_encoded_number(stream *s, ref *np)
  79. {    int format = s->num_format;
  80.     short snum;
  81.     long lnum;
  82.     int code, type;
  83.     switch ( format & 0x170 )
  84.        {
  85.     case num_int32: case num_int32 + 16:
  86.         if ( (format & 31) == 0 )
  87.             type = t_integer,
  88.             code = sgetlong(s, &np->value.intval);
  89.         else
  90.            {    type = t_real;
  91.             code = sgetlong(s, &lnum);
  92.             if ( !code )
  93.                 np->value.realval =
  94.                   (double)lnum * binary_scale[format & 31];
  95.            }
  96.         break;
  97.     case num_int16:
  98.         code = sgetshort(s, &snum);
  99.         if ( (format & 15) == 0 )
  100.            {    type = t_integer;
  101.             np->value.intval = snum;
  102.            }
  103.         else
  104.            {    type = t_real;
  105.             if ( !code )
  106.                 np->value.realval =
  107.                   (double)snum * binary_scale[format & 15];
  108.            }
  109.         break;
  110.     case num_float:
  111.         type = t_real;
  112.         code = sgetfloat(s, &np->value.realval);
  113.         break;
  114.     case num_array:
  115.        {    uint count = sgets(s, (byte *)np, sizeof(ref));
  116.         code = (count == 0 ? 1 : count == sizeof(ref) ? 0 :
  117.             e_syntaxerror);
  118.         if ( !code )
  119.          switch ( r_type(np) )
  120.            {
  121.         case t_integer: return t_integer;
  122.         case t_real: return t_real;
  123.         default: return e_typecheck;
  124.            }
  125.        }    break;
  126.     default:
  127.         return e_syntaxerror;    /* invalid num_format?? */
  128.        }
  129.     switch ( code )
  130.        {
  131.     case 0: return type;
  132.     case 1: return t_null;        /* end of stream */
  133.     default: return code;
  134.        }
  135. }
  136.  
  137. /* ------ Get/put number ------ */
  138.  
  139. /* Get/put encoded numbers on a stream according to num_format. */
  140. /* 1 means end of stream, 0 means not end, <0 means error. */
  141. #define s_is_lsb(s) num_is_lsb(s->num_format)
  142.  
  143. /* Get/put a short. */
  144. int
  145. sgetshort(register stream *s, short *p)
  146. {    int a = sgetc(s), b;
  147.     if ( a < 0 ) return 1;
  148.     b = sgetc(s);
  149.     if ( b < 0 ) return e_syntaxerror;
  150.     *p = (short)(s_is_lsb(s) ? (b << 8) + a : (a << 8) + b);
  151.     return 0;
  152. }
  153. void
  154. sputshort(register stream *s, short num)
  155. {    byte a = num & 0xff;
  156.     byte b = (byte)(num >> 8);
  157.     if ( s_is_msb(s) )
  158.        {    byte t = a; a = b; b = t;
  159.        }
  160.     sputc(s, a);
  161.     sputc(s, b);
  162. }
  163.  
  164. /* Get/put a long. */
  165. int
  166. sgetlong(register stream *s, long *p)
  167. {    int a = sgetc(s), b, c, d;
  168.     if ( a < 0 ) return 1;
  169.     b = sgetc(s);
  170.     c = sgetc(s);
  171.     d = sgetc(s);
  172.     if ( (b | c | d) < 0 ) return e_syntaxerror;
  173.     *p =  (long)(s_is_lsb(s) ?
  174.              ((long)d << 24) + ((long)c << 16) + (b << 8) + a :
  175.              ((long)a << 24) + ((long)b << 16) + (c << 8) + d);
  176.     return 0;
  177. }
  178. void
  179. sputlong(register stream *s, long num)
  180. {    byte a = num & 0xff;
  181.     byte b = (byte)(num >> 8);
  182.     byte c = (byte)(num >> 16);
  183.     byte d = (byte)(num >> 24);
  184.     if ( s_is_msb(s) )
  185.        {    byte t = a; a = d; d = t;
  186.         t = b; b = c; c = t;
  187.        }
  188.     sputc(s, a);
  189.     sputc(s, b);
  190.     sputc(s, c);
  191.     sputc(s, d);
  192. }
  193.  
  194. /* Get/put a float.  We don't handle non-IEEE native representations yet. */
  195. int
  196. sgetfloat(register stream *s, float *p)
  197. {    /* Hack: we know floats and longs are the same size. */
  198.     return sgetlong(s, (long *)p);
  199. }
  200. void
  201. sputfloat(register stream *s, floatp num)
  202. {    /* Hack: we know floats and longs are the same size. */
  203.     float f = num;            /* coerce from double */
  204.     sputlong(s, *(long *)&f);
  205. }
  206.