/*
 *                              E X P A N D . H
 *
 *  Header file for EXPAND.C, a module in EXPAND.  EXPAND is a file
 *  maintenance utility to decompress files.  It is compatible with the
 *  public domain COMPRESS v4.0 by Spencer W. Thomas, et al.
 *
 *-----------------------------------------------------------------------
 *  Author: Lyle V. Rains
 *      Based on the sources of COMPRESS.C v4.0 by Spencer Thomas, et al.
 *-----------------------------------------------------------------------
 *  Revision History:
 */


typedef unsigned short CODE;
typedef unsigned short HASH;

#define INITBITS    9           /* Various values for bit-length specs  */
#define MAXMAXBITS  16          /* DO NOT CHANGE!!                      */

#define CLEAR       ((CODE)256) /* Code sent when tables are cleared    */

#define OK          0           /* Result codes from press():           */
#define NOMEM       2           /*   Ran out of memory                  */
#define TOKTOOBIG   3           /*   Token longer than MAXTOKLEN chars  */
#define READERR     4           /*   I/O error on input                 */
#define WRITEERR    5           /*   I/O error on output                */
#define INFILEBAD   6           /*   Infile not in compressed format    */
#define CODEBAD     7           /*   Infile contained a bad token code  */
#define TABLEBAD    8           /*   The tables got corrupted (!)       */



#ifdef EXPAND

  /*
   * Internal definitions for use in EXPAND.C
   * They need not be visible to other modules.
   */

# include <stdio.h>
# include <assert.h>
# include "system.h"
# include "nextcode.h"

# define FIRSTFREE  ((CODE)257) /* First free code for token encoding   */
# define MAXTOKLEN  512         /* Max chars in token; size of buffer   */
# define NULLPTR(type)    ((type FAR *)NULL)

  /*
   * Macro to free a pointer with an offset if it is not NULL
   */
# define free_array(type, ptr, offset) \
    if ((ptr) != NULLPTR(type)) { \
      free((ALLOCTYPE FAR *)((ptr) + (offset))); \
      ptr = NULLPTR(type); \
    }

  /*
   * Macro to allocate a number of items of a certain type
   */
# define allocx(type, ptr, size) \
    ((ptr) = (type FAR *) malloc((MALLOCARG)(size) * sizeof(type)))

  /*
   * Macro to allocate new memory to a pointer with an offset value.
   */
# define alloc_array(type, ptr, size, offset) \
    ( allocx(type, ptr, (size) - (offset)) == NULLPTR(type) \
      ? NOMEM \
      : (((ptr) -= (offset)), OK) \
    )

# define suffix(code)        sfx[code]

# if (SPLIT_PFX)
    /*
     * We have to split pfx[] table in half, because it is
     * potentially larger than 64k bytes, and the processor
     * can't handle that
     */
#   define prefix(code)   (pfx[(code) & 1][(code)>>1])
# else
    /*
     * Then pfx[] can't be larger than 64k bytes, or we don't care
     * so we don't need to split it.
     */
#   define prefix(code) (pfx[code])
# endif

#endif


/*
 * Externally visible objects in EXPAND.C
 *    extern CONST int Maxbits;
 *    extern int expand(FILE *in, FILE *out, int maxbits, int clearmode);
 */
extern CONST int Maxbits;
extern int expand();
