home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / SFILTER2.C < prev    next >
C/C++ Source or Header  |  1992-09-17  |  7KB  |  281 lines

  1. /* Copyright (C) 1991, 1992 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. /* sfilter2.c */
  21. /* Additional stream functions for filters */
  22. #include "stdio_.h"    /* includes std.h */
  23. #include "memory_.h"
  24. #include "scanchar.h"
  25. #include "stream.h"
  26.  
  27. /* Generic functions in sfilter.c */
  28. extern int s_filter_write_flush(P1(stream *));
  29. extern int s_filter_close(P1(stream *));
  30.  
  31. /* ------ ASCII85Encode ------ */
  32.  
  33. /* Flush the buffer */
  34. private int
  35. s_A85E_write_buf(register stream *s)
  36. {    register stream *strm = s->strm;
  37.     register byte *p = s->cbuf;
  38.     register int count = s->cptr + 1 - p;
  39.     while ( count >= 4 )
  40.       { ulong word =
  41.           ((ulong)(((uint)p[0] << 8) + p[1]) << 16) +
  42.           (((uint)p[2] << 8) + p[3]);
  43.         if ( word == 0 )
  44.           sputc(strm, 'z');
  45.         else
  46.           { ulong q = word / (85L*85*85*85);
  47.         ushort w1;
  48.         sputc(strm, q + '!');
  49.         word -= q * (85L*85*85*85);
  50.         q = word / (85L*85*85);
  51.         sputc(strm, q + '!');
  52.         word -= q * (85L*85*85);
  53.         q = word / (85*85);
  54.         sputc(strm, q + '!');
  55.         w1 = (ushort)(word - q * (85L*85));
  56.         sputc(strm, w1 / 85 + '!');
  57.         sputc(strm, w1 % 85 + '!');
  58.         if ( !(count & 60) )
  59.           sputc(strm, '\n');
  60.           }
  61.         count -= 4, p += 4;
  62.       }
  63.     memcpy(s->cbuf, p, count);
  64.     s->cptr = s->cbuf + count - 1;
  65.     return 0;
  66. }
  67.  
  68. /* Close the stream, flushing a partial word. */
  69. private int
  70. s_A85E_close(register stream *s)
  71. {    stream *strm = s->strm;
  72.     int count;
  73.     (*s->procs.write_buf)(s);
  74.     count = s->cptr - s->cbuf + 1;
  75.     if ( count > 0 )
  76.        {    /* Handle leftover bytes.  1 <= count <= 3. */
  77.         /* All the bytes are at the beginning of the buffer. */
  78.         byte ebuf[5];
  79.         stream sst;
  80.         s->cptr[1] = s->cptr[2] = s->cptr[3] = 0xff;
  81.         s->cptr = s->cbuf + 3;
  82.         swrite_string(&sst, ebuf, 5);
  83.         s->strm = &sst;
  84.         (*s->procs.write_buf)(s);    /* force out final codes */
  85.         sputs(strm, ebuf, count + 1);
  86.        }
  87.     sputs(strm, (byte *)"~>", 2);
  88.     return s_std_close(s);
  89. }
  90.  
  91. /* Stream procedures */
  92. const stream_procs s_A85E_procs =
  93.    {    s_std_noavailable, s_std_noseek, s_filter_write_flush, s_A85E_close,
  94.     NULL, s_A85E_write_buf
  95.    };
  96.  
  97. /* ------ ASCII85Decode ------ */
  98.  
  99. /* Refill the buffer */
  100. private int
  101. s_A85D_read_buf(register stream *s)
  102. {    register stream *strm = s->strm;
  103.     register byte *p = s->cbuf;
  104.     byte *limit = p + s->bsize - 4;
  105.     int ccount = 0;
  106.     ulong word = 0;
  107.     while ( p < limit )
  108.       { int ch = sgetc(strm);
  109.         uint ccode = ch - '!';
  110.         if ( ccode < 85 )        /* catches ch < '!' as well */
  111.           { word = word * 85 + ccode;
  112.         if ( ++ccount == 5 )
  113.          { p[0] = word >> 24;
  114.            p[1] = (byte)(word >> 16);
  115.            p[2] = (byte)((uint)word >> 8);
  116.            p[3] = (byte)word;
  117.            p += 4;
  118.            word = 0;
  119.            ccount = 0;
  120.          }
  121.           }
  122.         else if ( ch == 'z' && ccount == 0 )
  123.           p[0] = p[1] = p[2] = p[3] = 0,
  124.           p += 4;
  125.         else if ( scan_char_decoder[ch] == ctype_space )
  126.           ;
  127.         else if ( ch == '~' && sgetc(strm) == '>' )
  128.          { /* Handle odd bytes */
  129.            s->end_status = EOFC;
  130.            switch ( ccount )
  131.            {
  132.         case 0:
  133.             break;
  134.         case 1:            /* syntax error */
  135.             s->end_status = ERRC;
  136.             break;
  137.         case 2:            /* 1 odd byte */
  138.             word = word * (85L+85*85) + 0xffffffL;
  139.             goto o1;
  140.         case 3:            /* 2 odd bytes */
  141.             word = word * (85L*85) + 0xffffL;
  142.             goto o2;
  143.         case 4:            /* 3 odd bytes */
  144.             word = word * 85 + 0xffL;
  145.             p[2] = (byte)(word >> 8);
  146. o2:            p[1] = (byte)(word >> 16);
  147. o1:            p[0] = (byte)(word >> 24);
  148.             p += ccount - 1;
  149.            }
  150.            break;
  151.          }
  152.         else            /* syntax error or exception */
  153.          { s->end_status = (ch < 0 ? ch : ERRC);
  154.            break;
  155.          }
  156.       }
  157.     s->cptr = s->cbuf - 1;
  158.     s->endptr = p - 1;
  159.     return 0;
  160. }
  161.  
  162. /* Stream procedures */
  163. const stream_procs s_A85D_procs =
  164.    {    s_std_noavailable, s_std_noseek, s_std_read_flush, s_filter_close,
  165.     s_A85D_read_buf, NULL
  166.    };
  167.  
  168. /* ------ RunLengthEncode ------ */
  169.  
  170. /* Initialize */
  171. void
  172. s_RLE_init(register stream *s, uint rec_size)
  173. {    s->record_size = (rec_size == 0 ? max_uint : rec_size);
  174.     s->record_left = s->record_size;
  175. }
  176.  
  177. /* Empty the buffer */
  178. private int
  179. s_RLE_write_buf(register stream *s)
  180. {    register stream *strm = s->strm;
  181.     register byte *p = s->cbuf;
  182.     while ( p <= s->cptr )
  183.        {    byte *beg = p, *q;
  184.         uint count = s->cptr - p + 1;
  185.         if ( count > s->record_left)
  186.             count = s->record_left;
  187.         if ( count > 127 )
  188.             count = 127;
  189.         q = p + count;
  190.         if ( count > 2 && p[1] == p[0] )
  191.            {    /* Recognize leading repeated byte */
  192.             do { p++; }
  193.             while ( p + 1 < q && p[1] == p[0] );
  194.             p++;
  195.             sputc(strm, 257 - (p - beg));
  196.             sputc(strm, *beg);
  197.            }
  198.         else
  199.            {    while ( p + 2 < q && (p[1] != p[0] || p[2] != p[0]) )
  200.                 p++;
  201.             if ( p + 2 >= q ) p = q;
  202.             sputc(strm, p - beg - 1);
  203.             sputs(strm, beg, p - beg);
  204.            }
  205.         s->record_left -= p - beg;
  206.         if ( s->record_left == 0 )
  207.             s->record_left = s->record_size;
  208.        }
  209.     s->cptr = s->cbuf - 1;
  210.     return 0;
  211. }
  212.  
  213. /* Close */
  214. private int
  215. s_RLE_close(register stream *s)
  216. {    (*s->procs.write_buf)(s);
  217.     sputc(s->strm, 128);
  218.     return s_std_close(s);
  219. }
  220.  
  221. /* Stream procedures */
  222. const stream_procs s_RLE_procs =
  223.    {    s_std_noavailable, s_std_noseek, s_filter_write_flush, s_RLE_close,
  224.     NULL, s_RLE_write_buf
  225.    };
  226.  
  227. /* ------ RunLengthDecode ------ */
  228.  
  229. /* Initialize */
  230. void
  231. s_RLD_init(register stream *s)
  232. {    s->odd = -1;
  233. }
  234.  
  235. /* Refill the buffer */
  236. private int
  237. s_RLD_read_buf(register stream *s)
  238. {    register stream *strm = s->strm;
  239.     register byte *p = s->cbuf;
  240.     byte *limit = p + s->bsize;
  241.     int b = s->odd;
  242.     if ( b < 0 ) b = sgetc(strm);
  243.     for ( ; ; )
  244.        {    uint count;
  245.         if ( b < 0 ) break;    /* EOF/ERR */
  246.         if ( b < 128 )
  247.            {    if ( b >= limit - p )    /* data won't fit */
  248.                 break;
  249.             count = sgets(strm, p, b + 1);
  250.             p += count;
  251.             b = -1;
  252.             if ( count == 0 && strm->end_status )    /* EOF/ERR */
  253.                 break;
  254.            }
  255.         else if ( b == 128 )    /* end of data */
  256.            {    s->end_status = EOFC;
  257.             b = -1;
  258.             break;
  259.            }
  260.         else if ( (count = 257 - b) > limit - p )
  261.             break;        /* won't fit */
  262.         else
  263.            {    b = sgetc(strm);
  264.             if ( b < 0 ) break;    /* EOF/ERR */
  265.             memset(p, b, count);
  266.             p += count;
  267.            }
  268.         b = sgetc(strm);
  269.        }
  270.     s->cptr = s->cbuf - 1;
  271.     s->endptr = p - 1;
  272.     s->odd = b;
  273.     return 0;
  274. }
  275.  
  276. /* Stream procedures */
  277. const stream_procs s_RLD_procs =
  278.    {    s_std_noavailable, s_std_noseek, s_std_read_flush, s_std_close,
  279.     s_RLD_read_buf, NULL
  280.    };
  281.