home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises. All rights reserved.
- Distributed by Free Software Foundation, Inc.
-
- This file is part of Ghostscript.
-
- Ghostscript is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
- to anyone for the consequences of using it or for whether it serves any
- particular purpose or works at all, unless he says so in writing. Refer
- to the Ghostscript General Public License for full details.
-
- Everyone is granted permission to copy, modify and redistribute
- Ghostscript, but only under the conditions described in the Ghostscript
- General Public License. A copy of this license is supposed to have been
- given to you along with Ghostscript so you can know your rights and
- responsibilities. It should be in a file named COPYING. Among other
- things, the copyright notice and this notice must be preserved on all
- copies. */
-
- /* stream.h */
- /* Definitions for standard stream package for Ghostscript */
- /* Requires stdio.h */
-
- /* Note that the stream package works with bytes, not chars. */
- /* This is to ensure unsigned representation on all systems. */
- /* A stream can only be read or written, not both. */
- /* Note also that the read procedure returns an int, */
- /* not a char or a byte, because EOFC is -1. */
- typedef struct stream_s stream;
- typedef struct {
- /* Store # available for reading */
- int (*available)(P2(stream *, long *));
- /* Set position */
- int (*seek)(P2(stream *, long));
- /* Flush buffered data */
- int (*flush)(P1(stream *));
- /* Flush data (if writing) & close stream */
- int (*close)(P1(stream *));
- /* Refill buffer, reset cptr, return count or EOFC */
- int (*read_buf)(P1(stream *));
- /* Write buffer, reset cptr, return 0/error */
- int (*write_buf)(P1(stream *));
- } stream_procs;
- /* Structs for specialized streams -- see below. */
- struct lzw_decode_s;
- struct lzw_encode_s;
- struct stream_s {
- byte *cptr; /* pointer to last byte */
- /* read or written */
- byte *endptr; /* pointer to last byte */
- /* containing data for reading, */
- /* or to be filled for writing */
- byte *cbuf; /* base of buffer */
- uint bsize; /* size of buffer */
- char writing; /* 0 if reading, 1 if writing */
- char eof; /* non-zero if at EOF when buffer */
- /* becomes empty */
- long position; /* file position of beginning of */
- /* buffer, -1 means not seekable */
- stream_procs procs;
- int num_format; /* format for Level 2 */
- /* encoded number reader */
- /* (only used locally) */
- /* strm is non-zero iff this is a filter stream. */
- stream *strm; /* the underlying stream */
- /*
- * If were were able to program in a real object-oriented style,
- * the remaining data would be per-subclass. It's just too much
- * of a nuisance to do this in C, so we allocate space for the
- * private data of ALL subclasses.
- */
- /* The following are for file streams. */
- FILE *file; /* file handle for C library */
- int can_close; /* 0 for stdin/out/err, */
- /* -1 for line/statementedit, */
- /* 1 for other files */
- stream *prev, *next; /* keep track of all files */
- /* The following is used by several decoding filters. */
- int odd; /* odd digit */
- /* The following are for RunLengthEncode filters. */
- uint record_size;
- uint record_left; /* bytes left in current record */
- /* The following are for eexec streams. */
- ushort cstate; /* encryption state */
- int binary; /* true=binary, false=hex */
- /* The following are for LZW encoding/decoding streams. */
- byte bits; /* most recent byte of input or */
- /* current byte of output */
- int bits_left; /* # of unused low bits in above, [0..7] */
- struct lzw_decode_s *decode; /* decoding table */
- struct lzw_encode_s *encode; /* encoding table */
- uint next_code; /* next code to be assigned */
- int code_size; /* current # of bits per code */
- int prev_code; /* previous code recognized */
- /* or assigned */
- uint prev_hash; /* accumulated encoding hash */
- };
-
- /* Stream functions. Some of these are macros -- beware. */
- /* Also note that unlike the C stream library, */
- /* ALL stream procedures take the stream as the first argument. */
-
- /* Following are valid for all streams. */
- /* flush is a no-op for read streams. */
- /* close is NOT a no-op for non-file streams -- */
- /* it actively disables them. */
- #define sseekable(s) ((s)->position >= 0)
- #define savailable(s,pl) (*(s)->procs.available)(s,pl)
- #define sflush(s) (*(s)->procs.flush)(s)
- #define sclose(s) (*(s)->procs.close)(s)
-
- /* Following are only valid for read streams. */
- extern int spgetc(P1(stream *));
- #define sgetc(s)\
- ((s)->cptr < (s)->endptr ? *++((s)->cptr) : spgetc(s))
- extern uint sgets(P3(stream *, byte *, uint));
- extern int sreadhex(P5(stream *, byte *, uint, uint *, int *));
- extern int sungetc(P2(stream *, byte)); /* -1 on error, 0 if OK */
- #define sputback(s) ((s)->cptr--)
- #define seofp(s) ((s)->cptr >= (s)->endptr && (s)->eof)
-
- /* Following are only valid for write streams. */
- extern int spputc(P2(stream *, byte));
- #define sputc(s,c)\
- ((s)->cptr < (s)->endptr ? ((int)(*++((s)->cptr)=(c))) :\
- spputc((s),(c)))
- extern uint sputs(P3(stream *, byte *, uint));
-
- /* Following are only valid for positionable streams. */
- #define stell(s) ((s)->cptr + 1 - (s)->cbuf + (s)->position)
- #define sseek(s,pos) (*(s)->procs.seek)(s,(long)(pos))
-
- /* Following are for high-performance clients. */
- /* bufptr points to the next item, bufend points beyond the last item. */
- #define sbufptr(s) ((s)->cptr + 1)
- #define sbufend(s) ((s)->endptr + 1)
- #define ssetbufptr(s,ptr) ((s)->cptr = (ptr) - 1)
- #define sbufavailable(s) ((s)->endptr - (s)->cptr)
-
- #define EOFC (-1)
-
- /* Stream creation procedures */
- extern void sread_string(P3(stream *, byte *, uint)),
- swrite_string(P3(stream *, byte *, uint));
- extern void sread_file(P4(stream *, FILE *, byte *, uint)),
- swrite_file(P4(stream *, FILE *, byte *, uint));
- extern void sread_decrypt(P5(stream *, stream *, byte *, uint, ushort));
-
- /* Standard stream initialization */
- extern void s_std_init(P5(stream *, byte *, uint, stream_procs *, int /*writing*/));
- /* Standard stream finalization */
- extern void s_disable(P1(stream *));
- /* Generic stream procedures exported for filters */
- extern int s_std_null(P1(stream *)),
- s_std_noavailable(P2(stream *, long *)),
- s_std_close(P1(stream *));
-