
#ifdef INCLUDE_HUFFSTUFF        /* Just for huffman routines    */

#define N       4096            /* buffer size */
#define F       60              /* lookahead buffer size */
#define THRESHOLD   2
#define NIL     N               /* leaf of tree */


#define N_CHAR      (256 - THRESHOLD + F)
                /* kinds of characters (character code = 0..N_CHAR-1) */
#define T       (N_CHAR * 2 - 1)/* size of table */
#define R       (T - 1)         /* position of root */
#define MAX_FREQ    0x8000      /* updates tree when the */
                                /* root frequency comes to this value. */

typedef unsigned char   UCHAR;
typedef char *          PCHAR;
typedef unsigned        USHORT;
typedef short           SHORT;
typedef unsigned long   ULONG;
typedef long            LONG;
typedef unsigned long * PULONG;
typedef long *          PLONG;
typedef void *          PVOID;

struct _HuffStuff{
    SHORT    sType;
    
    PVOID    pInput;
    SHORT    (*fGetNextChar)(struct _HuffStuff *);
    ULONG    ulMaxInput;

    PVOID    pOutput;
    SHORT    (*fPutNextChar)(struct _HuffStuff *, USHORT);
    ULONG    ulMaxOutput;

    ULONG    textsize;
    ULONG    codesize;
    
    UCHAR    text_buf[N + F - 1];
    
    SHORT    match_position;
    SHORT    match_length;
    SHORT    lson[N + 1];
    SHORT    rson[N + 257];
    SHORT    dad[N + 1];
    
    USHORT   freq[T + 1];        /* frequency table */
    
    SHORT    prnt[T + N_CHAR];   /* pointers to parent nodes, except for the */
                                 /* elements [T..T + N_CHAR - 1] which are used to get */
                                 /* the positions of leaves corresponding to the codes. */
    
    SHORT    son[T];             /* pointers to child nodes (son[], son[] + 1) */
    
    USHORT   getbuf;
    UCHAR    getlen;
    USHORT   putbuf;
    UCHAR    putlen;
    USHORT   code;
	 USHORT   len;
	 };

#endif

#define HUFFMAN_FILE_TO_FILE         0x0001
#define HUFFMAN_FILE_TO_BUFFER       0x0002
#define HUFFMAN_BUFFER_TO_FILE       0x0004
#define HUFFMAN_BUFFER_TO_BUFFER     0x0008

#define HUFFMAN_NO_ERROR             0
#define HUFFMAN_FILE_WRITE_ERROR     100
#define HUFFMAN_FILE_LENGTH_ERROR    101
#define HUFFMAN_MEMORY_ERROR         102
#define HUFFMAN_FILE_READ_ERROR      103


#ifndef SHORT
typedef short  SHORT;
#endif
#ifndef ULONG
typedef unsigned long ULONG;
typedef unsigned long *PULONG;
#endif
#ifndef PCHAR
typedef char * PCHAR;
#endif
/*
*   HUFFMAN1
*/
#ifdef _FILE_DEFINED            /* Only if FILE IO is already defined */
SHORT far pascal CompressFileToFile(FILE * , FILE * );
SHORT far pascal ExpandFileToFile(FILE * , FILE * );
SHORT far pascal CompressFileToBuffer(FILE * , char *, ULONG, PULONG);
SHORT far pascal ExpandFileToBuffer(FILE * , char *, ULONG, PULONG);
SHORT far pascal CompressBufferToFile( char *, ULONG, FILE *);
SHORT far pascal ExpandBufferToFile( char *, ULONG, FILE *);
#endif
/*
*   HUFFMAN5
*/
SHORT far pascal CompressBufferToBuffer(char *, ULONG, char *, ULONG, PULONG);
SHORT far pascal ExpandBufferToBuffer(char *, ULONG, char *, ULONG, PULONG);
/*
*   HUFFMAN1
*/
#ifdef INCLUDE_HUFFSTUFF        /* Just for huffman routines    */
SHORT GetNextFileChar(struct _HuffStuff *);
SHORT PutNextFileChar(struct _HuffStuff *, USHORT);
/*
*   HUFFMAN2
*/
SHORT Expand(struct _HuffStuff *);
SHORT ExpandChar(struct _HuffStuff *);
SHORT ExpandPosition(struct _HuffStuff *);
SHORT PutCode(struct _HuffStuff *, SHORT, USHORT);
/*
*   HUFFMAN3
*/
SHORT Compress(struct _HuffStuff *);
SHORT CompressChar(struct _HuffStuff *, USHORT);
SHORT CompressPosition(struct _HuffStuff *, USHORT);
SHORT CompressEnd(struct _HuffStuff *);
SHORT GetBit(struct _HuffStuff *);
SHORT GetByte(struct _HuffStuff *);
/*
*   HUFFMAN4
*/
struct _HuffStuff * StartHuff(void);
void EndHuff(struct _HuffStuff *);
void ReConstructTree(struct _HuffStuff *);
void UpdateTree(struct _HuffStuff *, SHORT);
void InitTree(struct _HuffStuff *);
void InsertNode(struct _HuffStuff *, SHORT);
void DeleteNode(struct _HuffStuff *, SHORT);
/*
*   HUFFMAN5
*/			  
SHORT GetNextBufferChar(struct _HuffStuff *);
SHORT PutNextBufferChar(struct _HuffStuff *, USHORT);
#endif
