#ifndef _LZW_H_
   #define _LZW_H

#include "mbuf.h"

/* a string entry */
struct zentry {
	unsigned short code;	/* codeword of the prefix string */
	char data;		/* character to add to the prefix string */
};

struct zfast {			/* fast version of string entry */
	unsigned short owncode;	/* own codeword */
	unsigned short code;	/* codeword of prefix string */
	char data;		/* character to add to prefix string */
};
#define ZCC		256	/* clear code table codeword */
#define ZFLUSH		257	/* codeword that signals a break in coding */

/* lzw stream structure **/
struct lzw {
	struct mlzw *zin;
	struct mlzw *zout;
};
#define NULLLZW (struct lzw *)0


struct mlzw {
	unsigned short codebits;	/* significant bits in each codeword */
	short maxbits;		/* maximum number of bits per codeword */
#define LZWBITS		9	/* initial number of bits in each codeword */

	char mode;		/* Compact or fast compression mode */
#define LZWCOMPACT	0
#define LZWFAST		1

	/* the following is used by the decoder only */
	union {
		struct zentry **tbl;	/* compact table */
#define LZWBLK		130		/* size of entry arrays to allocate */
		struct nbuf **bpp;	/* mbuf version of table */
#define ZHASH		256	/* hash table size */
		void *p;	/* generic table pointer */
	} tu;			/* table of entries */
	short nextbit;		/* next bit to process in code stream */
	short version;		/* version number of sender */
#define ZVERSION	3	/* version number */
	long code;		/* temporary storage for coding in progress */
	long next;		/* next code to be added to the table */
	long prefix;		/* last processed codeword */
	short flushbit;		/* next bit of the ZFLUSH codeword to send */
	long icnt;		/* count of processed input bytes */
	struct mbuf *ibuf;	/* input buffer */
	long ocnt;		/* count of processed output bytes */
	struct mbuf *obuf;	/* output buffer */
	char last;	/* */
};
#define NULLMLZW (struct mlzw *)0



struct lzw *LZW_Init   (short, short);
void        LZW_Free   (struct lzw *);
void        LZW_Encode (struct lzw *);
void        LZW_Flush  (struct lzw *);
short       LZW_Decode (struct lzw *);

#endif
