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 / STREAM.H < prev    next >
C/C++ Source or Header  |  1992-08-10  |  11KB  |  303 lines

  1. /* Copyright (C) 1989, 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. /* stream.h */
  21. /* Definitions for Ghostscript stream package */
  22. /* Requires stdio.h */
  23.  
  24. /*
  25.  * Note that the stream package works with bytes, not chars.
  26.  * This is to ensure unsigned representation on all systems.
  27.  * A stream currently can only be read or written, not both.
  28.  * Note also that the read procedure returns an int,
  29.  * not a char or a byte, so we can use negative values for EOFC and ERRC.
  30.  * We distinguish "data" from "signal" results (EOFC, ERRC) with a macro:
  31.  */
  32.  
  33. #define char_is_data(c) ((c) >= 0)
  34. #define char_is_signal(c) ((c) < 0)
  35. typedef struct stream_s stream;
  36.  
  37. /*
  38.  * We cast EOFC and ERRC to int explicitly, because some compilers
  39.  * don't do this if the other arm of a conditional is a byte.
  40.  * Clients should use char_is_data and char_is_signal (see above)
  41.  * to test for exceptional results.
  42.  */
  43. #define EOFC ((int)(-1))
  44. #define ERRC ((int)(-2))
  45. /****** ERRC IS NOT RECOGNIZED EVERYWHERE YET ******/
  46.  
  47. /* ------ Define the "virtual" stream procedures ------ */
  48.  
  49. typedef struct {
  50.  
  51.         /* Store # available for reading. */
  52.         /* Return 0 if OK, ERRC if error or not implemented. */
  53.     int (*available)(P2(stream *, long *));
  54.  
  55.         /* Set position. */
  56.         /* Return 0 if OK, ERRC if error or not implemented. */
  57.     int (*seek)(P2(stream *, long));
  58.  
  59.         /* Flush buffered data. */
  60.         /* Return 0 if OK, ERRC if error. */
  61.     int (*flush)(P1(stream *));
  62.  
  63.         /* Flush data (if writing) & close stream. */
  64.         /* Return 0 if OK, ERRC if error. */
  65.     int (*close)(P1(stream *));
  66.  
  67.         /* Refill buffer (setting endptr) and reset cptr. */
  68.         /* Return ERRC if not implemented; */
  69.         /* otherwise, set end_status appropriately and return 0. */
  70.     int (*read_buf)(P1(stream *));
  71.  
  72.         /* Write buffer and reset cptr. */
  73.         /* Return 0 if OK, ERRC if error or not implemented. */
  74.     int (*write_buf)(P1(stream *));
  75.  
  76. } stream_procs;
  77.  
  78. /* ------ Structs for specialized streams -- see below. ------ */
  79.  
  80. typedef struct CCITTFax_state_s {
  81.     /* The following are set before initialization. */
  82.     int Uncompressed;    /* boolean */
  83.     int K;
  84.     int EndOfLine;        /* boolean */
  85.     int EncodedByteAlign;    /* boolean */
  86.     int Columns;
  87.     int Rows;
  88.     int EndOfBlock;        /* boolean */
  89.     int BlackIs1;        /* boolean */
  90.     int DamagedRowsBeforeError;
  91.     uint raster;
  92.     /* The following are updated dynamically. */
  93.     int k_left;        /* number of next rows to encode in 2-D */
  94.                 /* (only if K > 0) */
  95.     int cbit;        /* bits left to fill in current decoded */
  96.                 /* byte at *cptr (0..7, input only) */
  97.     uint bits;        /* most recent bits of input or */
  98.                 /* current bits of output */
  99.     int bits_left;        /* # of valid low bits (input) or */
  100.                 /* unused low bits (output) in above */
  101.     uint prev_pos;        /* position of previous line in buffer */
  102.     /* The following are input-only and not used yet. */
  103.     int uncomp_run;        /* non-0 iff we are in an uncompressed */
  104.                 /* run straddling a scan line (-1 if white, */
  105.                 /* 1 if black) */
  106.     int uncomp_left;    /* # of bits left in the run */
  107.     int uncomp_exit;    /* non-0 iff this is an exit run */
  108.                 /* (-1 if next run white, 1 if black) */
  109. } CCITTFax_state;
  110.  
  111. struct lzw_decode_table_s;
  112. struct lzw_encode_table_s;
  113. typedef struct LZW_state_s {
  114.     /* The following are set at initialization. */
  115.     int enhanced;            /* if true, use Aladdin's */
  116.                     /* enhanced compression algorithm */
  117.     /* The following are updated dynamically. */
  118.     struct lzw_decode_table_s *decode_table;    /* decoding table */
  119.     struct lzw_encode_table_s *encode_table;    /* encoding table */
  120.     uint next_code;            /* next code to be assigned */
  121.     int code_size;            /* current # of bits per code */
  122.     int prev_code;            /* previous code recognized */
  123.                     /* or assigned */
  124.     byte bits;        /* most recent byte of input or */
  125.                 /* current byte of output */
  126.     int bits_left;        /* # of unused low bits in above, [0..7] */
  127. } LZW_state;
  128.  
  129. typedef struct SubFile_state_s {
  130.     ulong count;        /* # of EODs to scan over */
  131.     const byte *eod_string;
  132.     uint string_size;
  133.     uint match;        /* # of matched chars preceding end of buffer */
  134. } SubFile_state;
  135.  
  136. /* ------ The actual stream structure ------ */
  137.  
  138. struct stream_s {
  139.     byte *cptr;            /* pointer to last byte */
  140.                     /* read or written */
  141.     byte *endptr;            /* pointer to last byte */
  142.                     /* containing data for reading, */
  143.                     /* or to be filled for writing */
  144.     byte *cbuf;            /* base of buffer */
  145.     uint bsize;            /* size of buffer, 0 if closed */
  146.     uint cbsize;            /* size of buffer */
  147.     uint modes;            /* (not byte, to keep alignment) */
  148. #define s_mode_read 1
  149. #define s_mode_write 2
  150. #define s_mode_seek 4
  151. #define s_is_valid(s) ((s)->modes != 0)
  152. #define s_is_reading(s) (((s)->modes & s_mode_read) != 0)
  153. #define s_is_writing(s) (((s)->modes & s_mode_write) != 0)
  154. #define s_can_seek(s) (((s)->modes & s_mode_seek) != 0)
  155.     int end_status;            /* EOFC if at EOF when buffer */
  156.                     /* becomes empty, ERRC if error */
  157.     long position;            /* file position of beginning of */
  158.                     /* buffer */
  159.     stream_procs procs;
  160.     int num_format;            /* format for Level 2 */
  161.                     /* encoded number reader */
  162.                     /* (only used locally) */
  163.     stream *strm;            /* the underlying stream, non-zero */
  164.                     /* iff this is a filter stream */
  165.     int strm_is_temp;        /* if true, strm is a temporary */
  166.                     /* stream and should be freed */
  167.                     /* when this stream is closed */
  168.     ushort read_id;            /* "unique" serial # for detecting */
  169.                     /* references to closed streams */
  170.                     /* and for validating read access */
  171.     ushort write_id;        /* ditto to validate write access */
  172.     int char_temp;            /* temporary for inline access */
  173.                     /* (see spgetc_inline below) */
  174.     /*
  175.      * If were were able to program in a real object-oriented style, 
  176.      * the remaining data would be per-subclass.  It's just too much
  177.      * of a nuisance to do this in C, so we allocate space for the
  178.      * private data of ALL subclasses.
  179.      */
  180.     /* The following are for file streams. */
  181.     FILE *file;            /* file handle for C library */
  182.     int can_close;            /* 0 for stdin/out/err, */
  183.                     /* -1 for line/statementedit, */
  184.                     /* 1 for other files */
  185.     stream *prev, *next;        /* keep track of all files */
  186.     /*
  187.      * The remaining members are only for filters.
  188.      * Again, we simply allocate space for all of them,
  189.      * rather than use a union.
  190.      */
  191.     /* The following is used by several decoding filters. */
  192.     int odd;            /* odd digit */
  193.     /* The following is for RunLengthEncode. */
  194.     ulong record_size;
  195.     /* The following is for RunLengthEncode and PFBDecode. */
  196.     ulong record_left;        /* bytes left in current record */
  197.     /* The following are for PFBDecode. */
  198.     int record_type;
  199.     int binary_to_hex;
  200.     /* The following are for eexecDecode. */
  201.     ushort cstate;            /* encryption state */
  202.     ushort _skip;            /* (keep int alignment) */
  203.     int binary;            /* true=binary, false=hex */
  204.     /* The following are for various filters. */
  205.     CCITTFax_state cfs;
  206.     LZW_state lzws;
  207.     SubFile_state sfs;
  208. };
  209.  
  210. /* ------ Stream functions ------ */
  211.  
  212. /* Some of these are macros -- beware. */
  213. /* Note that unlike the C stream library, */
  214. /* ALL stream procedures take the stream as the first argument. */
  215. #define sendbufp(s) ((s)->cptr >= (s)->endptr)    /* not for clients */
  216.  
  217. /* Following are valid for all streams. */
  218. /* flush is NOT a no-op for read streams -- it discards data until EOF. */
  219. /* close is NOT a no-op for non-file streams -- */
  220. /* it actively disables them. */
  221. /* The close routine must do a flush if needed. */
  222. #define sseekable(s) s_can_seek(s)
  223. #define serrorp(s) ((s)->cptr >= (s)->endptr && (s)->end_status == ERRC)
  224. #define savailable(s,pl) (*(s)->procs.available)(s,pl)
  225. #define sflush(s) (*(s)->procs.flush)(s)
  226. #define sclose(s) (*(s)->procs.close)(s)
  227.  
  228. /* Following are only valid for read streams. */
  229. extern int spgetc(P1(stream *));
  230. /* The first alternative should read */
  231. /*    (int)(*++((s)->cptr))    */
  232. /* but the Borland compiler generates truly atrocious code for this. */
  233. #define sgetc(s) (!sendbufp(s) ? (++((s)->cptr), (int)*(s)->cptr) : spgetc(s))
  234. extern uint sgets(P3(stream *, byte *, uint));
  235. extern int sreadhex(P6(stream *, byte *, uint, uint *, int *, int));
  236. extern int sungetc(P2(stream *, byte));    /* ERRC on error, 0 if OK */
  237. #define sputback(s) ((s)->cptr--)    /* can only do this once! */
  238. #define seofp(s) (sendbufp(s) && (s)->end_status == EOFC)
  239. extern int spskip(P2(stream *, long));
  240. #define sskip(s,n) spskip(s,(long)(n))
  241.  
  242. /* Following are only valid for write streams. */
  243. extern int spputc(P2(stream *, byte));
  244. /* The first alternative should read */
  245. /*    ((int)(*++((s)->cptr)=(c)))    */
  246. /* but the Borland compiler generates truly atrocious code for this. */
  247. #define sputc(s,c)\
  248.   (!sendbufp(s) ? (++((s)->cptr), (int)(*(s)->cptr=(c))) : spputc((s),(c)))
  249. extern uint sputs(P3(stream *, const byte *, uint));
  250.  
  251. /* Following are only valid for positionable streams. */
  252. #define stell(s) ((s)->cptr + 1 - (s)->cbuf + (s)->position)
  253. #define sseek(s,pos) (*(s)->procs.seek)(s,(long)(pos))
  254.  
  255. /* Following are for high-performance clients. */
  256. /* bufptr points to the next item, bufend points beyond the last item. */
  257. #define sbufptr(s) ((s)->cptr + 1)
  258. #define sbufend(s) ((s)->endptr + 1)
  259. #define ssetbufptr(s,ptr) ((s)->cptr = (ptr) - 1)
  260. #define sbufskip(s,n) ((s)->cptr += (n))
  261. #define sbufavailable(s) ((s)->endptr - (s)->cptr)
  262.  
  263. /* The following are for very high-performance clients of read streams, */
  264. /* who unpack the stream state into local variables. */
  265. /* Note that any non-inline operations must do a s_end_inline before, */
  266. /* and a s_begin_inline after. */
  267. #define s_declare_inline(s, cp, ep)\
  268.   register byte *cp;\
  269.   byte *ep
  270. #define s_begin_inline(s, cp, ep)\
  271.   cp = (s)->cptr, ep = (s)->endptr
  272. #define s_end_inline(s, cp, ep)\
  273.   (s)->cptr = cp
  274. #define sendbufp_inline(s, cp, ep)\
  275.   (cp >= ep)
  276. #define sgetc_inline(s, cp, ep)\
  277.   (sendbufp_inline(s, cp, ep) ? spgetc_inline(s, cp, ep) : *++cp)
  278. #define spgetc_inline(s, cp, ep)\
  279.   (s_end_inline(s, cp, ep), (s)->char_temp = spgetc(s),\
  280.    s_begin_inline(s, cp, ep), (s)->char_temp)
  281. #define sputback_inline(s, cp, ep)\
  282.   --cp
  283.  
  284. /* Stream creation procedures */
  285. extern    void    sread_string(P3(stream *, const byte *, uint)),
  286.         swrite_string(P3(stream *, byte *, uint));
  287. extern    void    sread_file(P4(stream *, FILE *, byte *, uint)),
  288.         swrite_file(P4(stream *, FILE *, byte *, uint));
  289.  
  290. /* Standard stream initialization */
  291. extern    void    s_std_init(P5(stream *, byte *, uint, const stream_procs *, int /*mode*/));
  292.  
  293. /* Standard stream finalization */
  294. extern    void    s_disable(P1(stream *));
  295.  
  296. /* Generic stream procedures exported for filters */
  297. extern    int    s_std_null(P1(stream *)),
  298.         s_std_read_flush(P1(stream *)),
  299.         s_std_write_flush(P1(stream *)),
  300.         s_std_noavailable(P2(stream *, long *)),
  301.         s_std_noseek(P2(stream *, long)),
  302.         s_std_close(P1(stream *));
  303.